Skip to content

Commit

Permalink
Add more builtin traits
Browse files Browse the repository at this point in the history
  • Loading branch information
nordlow committed Oct 7, 2024
1 parent 8ce08e9 commit f59c4aa
Show file tree
Hide file tree
Showing 5 changed files with 514 additions and 387 deletions.
5 changes: 5 additions & 0 deletions compiler/src/dmd/frontend.h
Original file line number Diff line number Diff line change
Expand Up @@ -8888,6 +8888,7 @@ struct Id final
static Identifier* outpw;
static Identifier* builtinsModuleName;
static Identifier* ctfeWrite;
static Identifier* isAggregate;
static Identifier* isAbstractClass;
static Identifier* isArithmetic;
static Identifier* isAssociativeArray;
Expand All @@ -8903,9 +8904,13 @@ struct Id final
static Identifier* isIntegral;
static Identifier* isScalar;
static Identifier* isStaticArray;
static Identifier* isDynamicArray;
static Identifier* isArray;
static Identifier* isUnsigned;
static Identifier* isSigned;
static Identifier* isVirtualFunction;
static Identifier* isVirtualMethod;
static Identifier* isNormalFunction;
static Identifier* isAbstractFunction;
static Identifier* isFinalFunction;
static Identifier* isOverrideFunction;
Expand Down
5 changes: 5 additions & 0 deletions compiler/src/dmd/id.d
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,7 @@ immutable Msgtable[] msgtable =
{ "ctfeWrite", "__ctfeWrite" },

// Traits
{ "isAggregate" },
{ "isAbstractClass" },
{ "isArithmetic" },
{ "isAssociativeArray" },
Expand All @@ -469,9 +470,13 @@ immutable Msgtable[] msgtable =
{ "isIntegral" },
{ "isScalar" },
{ "isStaticArray" },
{ "isDynamicArray" },
{ "isArray" },
{ "isUnsigned" },
{ "isSigned" },
{ "isVirtualFunction" },
{ "isVirtualMethod" },
{ "isNormalFunction" },
{ "isAbstractFunction" },
{ "isFinalFunction" },
{ "isOverrideFunction" },
Expand Down
58 changes: 24 additions & 34 deletions compiler/src/dmd/traits.d
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,10 @@ Expression semanticTraits(TraitsExp e, Scope* sc)
{
return isTypeX(t => t.isUnsigned());
}
if (e.ident == Id.isSigned)
{
return isTypeX(t => t.isIntegral && !t.isUnsigned());
}
if (e.ident == Id.isAssociativeArray)
{
return isTypeX(t => t.toBasetype().ty == Taarray);
Expand All @@ -482,6 +486,19 @@ Expression semanticTraits(TraitsExp e, Scope* sc)
{
return isTypeX(t => t.toBasetype().ty == Tsarray);
}
if (e.ident == Id.isDynamicArray)
{
return isTypeX(t => t.toBasetype().ty == Tarray);
}
if (e.ident == Id.isArray)
{
return isTypeX(t => t.toBasetype().ty == Tsarray ||
t.toBasetype().ty == Tarray);
}
if (e.ident == Id.isAggregate)
{
return isTypeX(t => t.isAggregate() !is null);
}
if (e.ident == Id.isAbstractClass)
{
return isTypeX((t)
Expand All @@ -500,9 +517,6 @@ Expression semanticTraits(TraitsExp e, Scope* sc)
}
if (e.ident == Id.isTemplate)
{
if (dim != 1)
return dimError(1);

return isDsymX((s)
{
if (!s.toAlias().isOverloadable())
Expand Down Expand Up @@ -615,16 +629,14 @@ Expression semanticTraits(TraitsExp e, Scope* sc)
}
if (e.ident == Id.isDisabled)
{
if (dim != 1)
return dimError(1);

return isDeclX(f => f.isDisabled());
}
if (e.ident == Id.isNormalFunction)
{
return isFuncX(f => true);
}
if (e.ident == Id.isAbstractFunction)
{
if (dim != 1)
return dimError(1);

return isFuncX(f => f.isAbstract());
}
if (e.ident == Id.isVirtualFunction)
Expand All @@ -633,37 +645,22 @@ Expression semanticTraits(TraitsExp e, Scope* sc)
// Deprecated in 2.101 - Can be removed from 2.121
deprecation(e.loc, "`traits(isVirtualFunction)` is deprecated. Use `traits(isVirtualMethod)` instead");

if (dim != 1)
return dimError(1);

return isFuncX(f => f.isVirtual());
}
if (e.ident == Id.isVirtualMethod)
{
if (dim != 1)
return dimError(1);

return isFuncX(f => f.isVirtualMethod());
}
if (e.ident == Id.isFinalFunction)
{
if (dim != 1)
return dimError(1);

return isFuncX(f => f.isFinalFunc());
}
if (e.ident == Id.isOverrideFunction)
{
if (dim != 1)
return dimError(1);

return isFuncX(f => f.isOverride());
}
if (e.ident == Id.isStaticFunction)
{
if (dim != 1)
return dimError(1);

return isFuncX(f => !f.needThis() && !f.isNested());
}
if (e.ident == Id.isModule)
Expand All @@ -682,23 +679,14 @@ Expression semanticTraits(TraitsExp e, Scope* sc)
}
if (e.ident == Id.isRef)
{
if (dim != 1)
return dimError(1);

return isDeclX(d => d.isRef());
}
if (e.ident == Id.isOut)
{
if (dim != 1)
return dimError(1);

return isDeclX(d => d.isOut());
}
if (e.ident == Id.isLazy)
{
if (dim != 1)
return dimError(1);

return isDeclX(d => (d.storage_class & STC.lazy_) != 0);
}
if (e.ident == Id.identifier)
Expand Down Expand Up @@ -2282,7 +2270,7 @@ private void traitNotFound(TraitsExp e)
initialized = true; // lazy initialization

// All possible traits
__gshared Identifier*[59] idents =
__gshared Identifier*[61] idents =
[
&Id.allMembers,
&Id.child,
Expand Down Expand Up @@ -2312,7 +2300,9 @@ private void traitNotFound(TraitsExp e)
&Id.hasMember,
&Id.hasPostblit,
&Id.identifier,
&Id.isAggregate,
&Id.isAbstractClass,
&Id.isNormalFunction,
&Id.isAbstractFunction,
&Id.isArithmetic,
&Id.isAssociativeArray,
Expand Down
39 changes: 15 additions & 24 deletions compiler/test/fail_compilation/test17096.d
Original file line number Diff line number Diff line change
@@ -1,29 +1,20 @@
/* TEST_OUTPUT:
---
fail_compilation/test17096.d(29): Error: expected 1 arguments for `isPOD` but had 2
fail_compilation/test17096.d(30): Error: expected 1 arguments for `isNested` but had 2
fail_compilation/test17096.d(31): Deprecation: `traits(isVirtualFunction)` is deprecated. Use `traits(isVirtualMethod)` instead
fail_compilation/test17096.d(31): Error: expected 1 arguments for `isVirtualFunction` but had 2
fail_compilation/test17096.d(32): Error: expected 1 arguments for `isVirtualMethod` but had 2
fail_compilation/test17096.d(33): Error: expected 1 arguments for `isAbstractFunction` but had 2
fail_compilation/test17096.d(34): Error: expected 1 arguments for `isFinalFunction` but had 2
fail_compilation/test17096.d(35): Error: expected 1 arguments for `isOverrideFunction` but had 2
fail_compilation/test17096.d(36): Error: expected 1 arguments for `isStaticFunction` but had 2
fail_compilation/test17096.d(37): Error: expected 1 arguments for `isRef` but had 2
fail_compilation/test17096.d(38): Error: expected 1 arguments for `isOut` but had 2
fail_compilation/test17096.d(39): Error: expected 1 arguments for `isLazy` but had 2
fail_compilation/test17096.d(40): Error: expected 1 arguments for `identifier` but had 2
fail_compilation/test17096.d(41): Error: expected 1 arguments for `getProtection` but had 2
fail_compilation/test17096.d(42): Error: expected 1 arguments for `parent` but had 2
fail_compilation/test17096.d(43): Error: expected 1 arguments for `classInstanceSize` but had 2
fail_compilation/test17096.d(44): Error: expected 1 arguments for `allMembers` but had 2
fail_compilation/test17096.d(45): Error: expected 1 arguments for `derivedMembers` but had 2
fail_compilation/test17096.d(46): Error: expected 1 arguments for `getAliasThis` but had 2
fail_compilation/test17096.d(47): Error: expected 1 arguments for `getAttributes` but had 2
fail_compilation/test17096.d(48): Error: expected 1 arguments for `getFunctionAttributes` but had 2
fail_compilation/test17096.d(49): Error: expected 1 arguments for `getUnitTests` but had 2
fail_compilation/test17096.d(50): Error: expected 1 arguments for `getVirtualIndex` but had 2
fail_compilation/test17096.d(51): Error: a single type expected for trait pointerBitmap
fail_compilation/test17096.d(20): Error: expected 1 arguments for `isPOD` but had 2
fail_compilation/test17096.d(21): Error: expected 1 arguments for `isNested` but had 2
fail_compilation/test17096.d(22): Deprecation: `traits(isVirtualFunction)` is deprecated. Use `traits(isVirtualMethod)` instead
fail_compilation/test17096.d(31): Error: expected 1 arguments for `identifier` but had 2
fail_compilation/test17096.d(32): Error: expected 1 arguments for `getProtection` but had 2
fail_compilation/test17096.d(33): Error: expected 1 arguments for `parent` but had 2
fail_compilation/test17096.d(34): Error: expected 1 arguments for `classInstanceSize` but had 2
fail_compilation/test17096.d(35): Error: expected 1 arguments for `allMembers` but had 2
fail_compilation/test17096.d(36): Error: expected 1 arguments for `derivedMembers` but had 2
fail_compilation/test17096.d(37): Error: expected 1 arguments for `getAliasThis` but had 2
fail_compilation/test17096.d(38): Error: expected 1 arguments for `getAttributes` but had 2
fail_compilation/test17096.d(39): Error: expected 1 arguments for `getFunctionAttributes` but had 2
fail_compilation/test17096.d(40): Error: expected 1 arguments for `getUnitTests` but had 2
fail_compilation/test17096.d(41): Error: expected 1 arguments for `getVirtualIndex` but had 2
fail_compilation/test17096.d(42): Error: a single type expected for trait pointerBitmap
---
*/
enum b03 = __traits(isPOD, 1, 2);
Expand Down
Loading

0 comments on commit f59c4aa

Please sign in to comment.