static if (__ctfe)

Timon Gehr via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu May 8 08:37:15 PDT 2014


On 05/07/2014 12:07 PM, Yuriy wrote:
> On Wednesday, 7 May 2014 at 09:51:01 UTC, John Colvin wrote:
>> On Wednesday, 7 May 2014 at 09:47:20 UTC, Yuriy wrote:
>>> Hello, is there any way to static if(__ctfe)? I want to declare class
>>> members which are only available in ctfe. Thanx.
>>
>> Sadly not as far as I know. What's the use-case? There may be a nice
>> solution none-the-less.
>
> Well, i'm currently playing with std.variant so it can be ctfe-friendly.
> And it works pretty much, however i need to use T.stringof.ptr/length
> instead of typeid (typeid is ctfeable), and i'm not sure if it's good
> for performance in runtime. So for type comparison i'd like to compare
> TypeInfos in rt, and T.stringof in ct. Using both with rt if will likely
> generate more code.

I'd suggest to just look into making typeid's comparable in CTFE.
I have grepped around DMD's source code a little bit, but I am not 
familiar with it. (And I don't want to get involved in DMD development, 
because the code base is horrid.)

If you add in ctfeexpr.c/isCtfeComparable the conjunct 'x->op != 
TOKsymoff' (which probably is a very blunt way of enabling typeid 
comparisons and should be done more precisely), then you will notice 
that comparisons now work but always return 'false' for some types. I 
have attempted a fix for this for classes as follows in typinf.c:

TypeInfoDeclaration *TypeClass::getTypeInfoDeclaration(){
   if(!sym->vclassinfo){
     if (sym->isInterfaceDeclaration())
       return TypeInfoInterfaceDeclaration::create(this); // still buggy!
     else{
       sym->vclassinfo = TypeInfoClassDeclaration::create(this);
     }
   }
   return sym->vclassinfo;
}

I.e. the problem is that the frontend always creates new 
TypeInfoDeclarations instead of reusing a single existing one.

After this, typeid comparisons appear to work correctly for classes at 
least.
You might look into getting this fixed completely for all types (or at 
least file a bug containing the above information.)


More information about the Digitalmars-d-learn mailing list