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 Sep 10, 2024
1 parent 01518c3 commit b827b0d
Show file tree
Hide file tree
Showing 5 changed files with 506 additions and 372 deletions.
3 changes: 3 additions & 0 deletions compiler/src/dmd/frontend.h
Original file line number Diff line number Diff line change
Expand Up @@ -8697,6 +8697,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 @@ -8712,6 +8713,8 @@ struct Id final
static Identifier* isIntegral;
static Identifier* isScalar;
static Identifier* isStaticArray;
static Identifier* isDynamicArray;
static Identifier* isArray;
static Identifier* isUnsigned;
static Identifier* isVirtualFunction;
static Identifier* isVirtualMethod;
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 @@ -462,6 +462,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 @@ -480,6 +484,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 @@ -498,9 +515,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 @@ -613,16 +627,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 @@ -631,37 +643,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 @@ -680,23 +677,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 @@ -2280,7 +2268,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 @@ -2310,7 +2298,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
18 changes: 9 additions & 9 deletions compiler/test/fail_compilation/test17096.d
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ fail_compilation/test17096.d(51): Error: a single type expected for trait pointe
*/
enum b03 = __traits(isPOD, 1, 2);
enum b04 = __traits(isNested, 1, 2);
enum b05 = __traits(isVirtualFunction, 1, 2);
enum b06 = __traits(isVirtualMethod, 1, 2);
enum b07 = __traits(isAbstractFunction, 1, 2);
enum b08 = __traits(isFinalFunction, 1, 2);
enum b09 = __traits(isOverrideFunction, 1, 2);
enum b10 = __traits(isStaticFunction, 1, 2);
enum b11 = __traits(isRef, 1, 2);
enum b12 = __traits(isOut, 1, 2);
enum b13 = __traits(isLazy, 1, 2);
// enum b05 = __traits(isVirtualFunction, 1, 2);
// enum b06 = __traits(isVirtualMethod, 1, 2);
// enum b07 = __traits(isAbstractFunction, 1, 2);
// enum b08 = __traits(isFinalFunction, 1, 2);
// enum b09 = __traits(isOverrideFunction, 1, 2);
// enum b10 = __traits(isStaticFunction, 1, 2);
// enum b11 = __traits(isRef, 1, 2);
// enum b12 = __traits(isOut, 1, 2);
// enum b13 = __traits(isLazy, 1, 2);
enum b14 = __traits(identifier, 1, 2);
enum b15 = __traits(getProtection, 1, 2);
enum b16 = __traits(parent, 1, 2);
Expand Down
Loading

0 comments on commit b827b0d

Please sign in to comment.