aboutsummaryrefslogtreecommitdiff
path: root/gcc/d/dmd/traits.c
blob: 2430383526849c3c5e5d2c7c41d47b3762e0681e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445

/* Compiler implementation of the D programming language
 * Copyright (C) 1999-2019 by The D Language Foundation, All Rights Reserved
 * written by Walter Bright
 * http://www.digitalmars.com
 * Distributed under the Boost Software License, Version 1.0.
 * http://www.boost.org/LICENSE_1_0.txt
 * https://github.com/D-Programming-Language/dmd/blob/master/src/traits.c
 */

#include "root/dsystem.h"
#include "root/rmem.h"
#include "root/aav.h"
#include "root/checkedint.h"

#include "errors.h"
#include "mtype.h"
#include "init.h"
#include "expression.h"
#include "template.h"
#include "utf.h"
#include "enum.h"
#include "scope.h"
#include "hdrgen.h"
#include "statement.h"
#include "declaration.h"
#include "aggregate.h"
#include "import.h"
#include "id.h"
#include "dsymbol.h"
#include "module.h"
#include "attrib.h"
#include "parse.h"
#include "root/speller.h"

typedef int (*ForeachDg)(void *ctx, size_t idx, Dsymbol *s);
int ScopeDsymbol_foreach(Scope *sc, Dsymbols *members, ForeachDg dg, void *ctx, size_t *pn = NULL);
void freeFieldinit(Scope *sc);
Expression *resolve(Loc loc, Scope *sc, Dsymbol *s, bool hasOverloads);
Expression *trySemantic(Expression *e, Scope *sc);
Expression *semantic(Expression *e, Scope *sc);
Expression *typeToExpression(Type *t);


/************************************************
 * Delegate to be passed to overloadApply() that looks
 * for functions matching a trait.
 */

struct Ptrait
{
    Expression *e1;
    Expressions *exps;          // collected results
    Identifier *ident;          // which trait we're looking for
};

static int fptraits(void *param, Dsymbol *s)
{
    FuncDeclaration *f = s->isFuncDeclaration();
    if (!f)
        return 0;

    Ptrait *p = (Ptrait *)param;
    if (p->ident == Id::getVirtualFunctions && !f->isVirtual())
        return 0;

    if (p->ident == Id::getVirtualMethods && !f->isVirtualMethod())
        return 0;

    Expression *e;
    FuncAliasDeclaration* ad = new FuncAliasDeclaration(f->ident, f, false);
    ad->protection = f->protection;
    if (p->e1)
        e = new DotVarExp(Loc(), p->e1, ad, false);
    else
        e = new DsymbolExp(Loc(), ad, false);
    p->exps->push(e);
    return 0;
}

/**
 * Collects all unit test functions from the given array of symbols.
 *
 * This is a helper function used by the implementation of __traits(getUnitTests).
 *
 * Input:
 *      symbols             array of symbols to collect the functions from
 *      uniqueUnitTests     an associative array (should actually be a set) to
 *                          keep track of already collected functions. We're
 *                          using an AA here to avoid doing a linear search of unitTests
 *
 * Output:
 *      unitTests           array of DsymbolExp's of the collected unit test functions
 *      uniqueUnitTests     updated with symbols from unitTests[ ]
 */
static void collectUnitTests(Dsymbols *symbols, AA *uniqueUnitTests, Expressions *unitTests)
{
    if (!symbols)
        return;
    for (size_t i = 0; i < symbols->dim; i++)
    {
        Dsymbol *symbol = (*symbols)[i];
        UnitTestDeclaration *unitTest = symbol->isUnitTestDeclaration();
        if (unitTest)
        {
            if (!dmd_aaGetRvalue(uniqueUnitTests, (void *)unitTest))
            {
                FuncAliasDeclaration* ad = new FuncAliasDeclaration(unitTest->ident, unitTest, false);
                ad->protection = unitTest->protection;
                Expression* e = new DsymbolExp(Loc(), ad, false);
                unitTests->push(e);
                bool* value = (bool*) dmd_aaGet(&uniqueUnitTests, (void *)unitTest);
                *value = true;
            }
        }
        else
        {
            AttribDeclaration *attrDecl = symbol->isAttribDeclaration();

            if (attrDecl)
            {
                Dsymbols *decl = attrDecl->include(NULL, NULL);
                collectUnitTests(decl, uniqueUnitTests, unitTests);
            }
        }
    }
}

/************************ TraitsExp ************************************/

static Expression *True(TraitsExp *e)  { return new IntegerExp(e->loc, true, Type::tbool); }
static Expression *False(TraitsExp *e) { return new IntegerExp(e->loc, false, Type::tbool); }

bool isTypeArithmetic(Type *t)       { return t->isintegral() || t->isfloating(); }
bool isTypeFloating(Type *t)         { return t->isfloating(); }
bool isTypeIntegral(Type *t)         { return t->isintegral(); }
bool isTypeScalar(Type *t)           { return t->isscalar(); }
bool isTypeUnsigned(Type *t)         { return t->isunsigned(); }
bool isTypeAssociativeArray(Type *t) { return t->toBasetype()->ty == Taarray; }
bool isTypeStaticArray(Type *t)      { return t->toBasetype()->ty == Tsarray; }
bool isTypeAbstractClass(Type *t)    { return t->toBasetype()->ty == Tclass && ((TypeClass *)t->toBasetype())->sym->isAbstract(); }
bool isTypeFinalClass(Type *t)       { return t->toBasetype()->ty == Tclass && (((TypeClass *)t->toBasetype())->sym->storage_class & STCfinal) != 0; }

Expression *isTypeX(TraitsExp *e, bool (*fp)(Type *t))
{
    if (!e->args || !e->args->dim)
        return False(e);
    for (size_t i = 0; i < e->args->dim; i++)
    {
        Type *t = getType((*e->args)[i]);
        if (!t || !fp(t))
            return False(e);
    }
    return True(e);
}

bool isFuncAbstractFunction(FuncDeclaration *f) { return f->isAbstract(); }
bool isFuncVirtualFunction(FuncDeclaration *f) { return f->isVirtual(); }
bool isFuncVirtualMethod(FuncDeclaration *f) { return f->isVirtualMethod(); }
bool isFuncFinalFunction(FuncDeclaration *f) { return f->isFinalFunc(); }
bool isFuncStaticFunction(FuncDeclaration *f) { return !f->needThis() && !f->isNested(); }
bool isFuncOverrideFunction(FuncDeclaration *f) { return f->isOverride(); }

Expression *isFuncX(TraitsExp *e, bool (*fp)(FuncDeclaration *f))
{
    if (!e->args || !e->args->dim)
        return False(e);
    for (size_t i = 0; i < e->args->dim; i++)
    {
        Dsymbol *s = getDsymbol((*e->args)[i]);
        if (!s)
            return False(e);
        FuncDeclaration *f = s->isFuncDeclaration();
        if (!f || !fp(f))
            return False(e);
    }
    return True(e);
}

bool isDeclRef(Declaration *d) { return d->isRef(); }
bool isDeclOut(Declaration *d) { return d->isOut(); }
bool isDeclLazy(Declaration *d) { return (d->storage_class & STClazy) != 0; }

Expression *isDeclX(TraitsExp *e, bool (*fp)(Declaration *d))
{
    if (!e->args || !e->args->dim)
        return False(e);
    for (size_t i = 0; i < e->args->dim; i++)
    {
        Dsymbol *s = getDsymbol((*e->args)[i]);
        if (!s)
            return False(e);
        Declaration *d = s->isDeclaration();
        if (!d || !fp(d))
            return False(e);
    }
    return True(e);
}

// callback for TypeFunction::attributesApply
struct PushAttributes
{
    Expressions *mods;

    static int fp(void *param, const char *str)
    {
        PushAttributes *p = (PushAttributes *)param;
        p->mods->push(new StringExp(Loc(), const_cast<char *>(str)));
        return 0;
    }
};

StringTable traitsStringTable;

struct TraitsInitializer
{
    TraitsInitializer();
};

static TraitsInitializer traitsinitializer;

TraitsInitializer::TraitsInitializer()
{
    const char* traits[] = {
        "isAbstractClass",
        "isArithmetic",
        "isAssociativeArray",
        "isFinalClass",
        "isPOD",
        "isNested",
        "isFloating",
        "isIntegral",
        "isScalar",
        "isStaticArray",
        "isUnsigned",
        "isVirtualFunction",
        "isVirtualMethod",
        "isAbstractFunction",
        "isFinalFunction",
        "isOverrideFunction",
        "isStaticFunction",
        "isRef",
        "isOut",
        "isLazy",
        "hasMember",
        "identifier",
        "getProtection",
        "parent",
        "getLinkage",
        "getMember",
        "getOverloads",
        "getVirtualFunctions",
        "getVirtualMethods",
        "classInstanceSize",
        "allMembers",
        "derivedMembers",
        "isSame",
        "compiles",
        "parameters",
        "getAliasThis",
        "getAttributes",
        "getFunctionAttributes",
        "getFunctionVariadicStyle",
        "getParameterStorageClasses",
        "getUnitTests",
        "getVirtualIndex",
        "getPointerBitmap",
        NULL
    };

    traitsStringTable._init(40);

    for (size_t idx = 0;; idx++)
    {
        const char *s = traits[idx];
        if (!s) break;
        StringValue *sv = traitsStringTable.insert(s, strlen(s), const_cast<char *>(s));
        assert(sv);
    }
}

void *trait_search_fp(void *, const char *seed, int* cost)
{
    //printf("trait_search_fp('%s')\n", seed);
    size_t len = strlen(seed);
    if (!len)
        return NULL;

    *cost = 0;
    StringValue *sv = traitsStringTable.lookup(seed, len);
    return sv ? (void*)sv->ptrvalue : NULL;
}

static int fpisTemplate(void *, Dsymbol *s)
{
    if (s->isTemplateDeclaration())
        return 1;

    return 0;
}

bool isTemplate(Dsymbol *s)
{
    if (!s->toAlias()->isOverloadable())
        return false;

    return overloadApply(s, NULL, &fpisTemplate) != 0;
}

Expression *isSymbolX(TraitsExp *e, bool (*fp)(Dsymbol *s))
{
    if (!e->args || !e->args->dim)
        return False(e);
    for (size_t i = 0; i < e->args->dim; i++)
    {
        Dsymbol *s = getDsymbol((*e->args)[i]);
        if (!s || !fp(s))
            return False(e);
    }
    return True(e);
}

/**
 * get an array of size_t values that indicate possible pointer words in memory
 *  if interpreted as the type given as argument
 * the first array element is the size of the type for independent interpretation
 *  of the array
 * following elements bits represent one word (4/8 bytes depending on the target
 *  architecture). If set the corresponding memory might contain a pointer/reference.
 *
 *  [T.sizeof, pointerbit0-31/63, pointerbit32/64-63/128, ...]
 */
Expression *pointerBitmap(TraitsExp *e)
{
    if (!e->args || e->args->dim != 1)
    {
        error(e->loc, "a single type expected for trait pointerBitmap");
        return new ErrorExp();
    }
    Type *t = getType((*e->args)[0]);
    if (!t)
    {
        error(e->loc, "%s is not a type", (*e->args)[0]->toChars());
        return new ErrorExp();
    }
    d_uns64 sz;
    if (t->ty == Tclass && !((TypeClass*)t)->sym->isInterfaceDeclaration())
        sz = ((TypeClass*)t)->sym->AggregateDeclaration::size(e->loc);
    else
        sz = t->size(e->loc);
    if (sz == SIZE_INVALID)
        return new ErrorExp();

    const d_uns64 sz_size_t = Type::tsize_t->size(e->loc);
    if (sz > UINT64_MAX - sz_size_t)
    {
        error(e->loc, "size overflow for type %s", t->toChars());
        return new ErrorExp();
    }

    d_uns64 bitsPerWord = sz_size_t * 8;
    d_uns64 cntptr = (sz + sz_size_t - 1) / sz_size_t;
    d_uns64 cntdata = (cntptr + bitsPerWord - 1) / bitsPerWord;
    Array<d_uns64> data;
    data.setDim((size_t)cntdata);
    data.zero();

    class PointerBitmapVisitor : public Visitor
    {
    public:
        PointerBitmapVisitor(Array<d_uns64>* _data, d_uns64 _sz_size_t)
            : data(_data), offset(0), sz_size_t(_sz_size_t), error(false)
        {}

        void setpointer(d_uns64 off)
        {
            d_uns64 ptroff = off / sz_size_t;
            (*data)[(size_t)(ptroff / (8 * sz_size_t))] |= 1LL << (ptroff % (8 * sz_size_t));
        }
        virtual void visit(Type *t)
        {
            Type *tb = t->toBasetype();
            if (tb != t)
                tb->accept(this);
        }
        virtual void visit(TypeError *t) { visit((Type *)t); }
        virtual void visit(TypeNext *) { assert(0); }
        virtual void visit(TypeBasic *t)
        {
            if (t->ty == Tvoid)
                setpointer(offset);
        }
        virtual void visit(TypeVector *) { }
        virtual void visit(TypeArray *) { assert(0); }
        virtual void visit(TypeSArray *t)
        {
            d_uns64 arrayoff = offset;
            d_uns64 nextsize = t->next->size();
            if (nextsize == SIZE_INVALID)
                error = true;
            d_uns64 dim = t->dim->toInteger();
            for (d_uns64 i = 0; i < dim; i++)
            {
                offset = arrayoff + i * nextsize;
                t->next->accept(this);
            }
            offset = arrayoff;
        }
        virtual void visit(TypeDArray *) { setpointer(offset + sz_size_t); } // dynamic array is {length,ptr}
        virtual void visit(TypeAArray *) { setpointer(offset); }
        virtual void visit(TypePointer *t)
        {
            if (t->nextOf()->ty != Tfunction) // don't mark function pointers
                setpointer(offset);
        }
        virtual void visit(TypeReference *) { setpointer(offset); }
        virtual void visit(TypeClass *) { setpointer(offset); }
        virtual void visit(TypeFunction *) { }
        virtual void visit(TypeDelegate *) { setpointer(offset); } // delegate is {context, function}
        virtual void visit(TypeQualified *) { assert(0); } // assume resolved
        virtual void visit(TypeIdentifier *) { assert(0); }
        virtual void visit(TypeInstance *) { assert(0); }
        virtual void visit(TypeTypeof *) { assert(0); }
        virtual void visit(TypeReturn *) { assert(0); }
        virtual void visit(TypeEnum *t) { visit((Type *)t); }
        virtual void visit(TypeTuple *t) { visit((Type *)t); }
        virtual void visit(TypeSlice *) { assert(0); }
        virtual void visit(TypeNull *) { } // always a null pointer

        virtual void visit(TypeStruct *t)
        {
            d_uns64 structoff = offset;
            for (size_t i = 0; i < t->sym->fields.dim; i++)
            {
                VarDeclaration *v = t->sym->fields[i];
                offset = structoff + v->offset;
                if (v->type->ty == Tclass)
                    setpointer(offset);
                else
                    v->type->accept(this);
            }
            offset = structoff;
        }

        // a "toplevel" class is treated as an instance, while TypeClass fields are treated as references
        void visitClass(TypeClass* t)
        {
            d_uns64 classoff = offset;

            // skip vtable-ptr and monitor
            if (t->sym->baseClass)
                visitClass((TypeClass*)t->sym->baseClass->type);

            for (size_t i = 0; i < t->sym->fields.dim; i++)
            {
                VarDeclaration *v = t->sym->fields[i];
                offset = classoff + v->offset;
                v->type->accept(this);
            }
            offset = classoff;
        }

        Array<d_uns64>* data;
        d_uns64 offset;
        d_uns64 sz_size_t;
        bool error;
    };

    PointerBitmapVisitor pbv(&data, sz_size_t);
    if (t->ty == Tclass)
        pbv.visitClass((TypeClass*)t);
    else
        t->accept(&pbv);
    if (pbv.error)
        return new ErrorExp();

    Expressions* exps = new Expressions;
    exps->push(new IntegerExp(e->loc, sz, Type::tsize_t));
    for (d_uns64 i = 0; i < cntdata; i++)
        exps->push(new IntegerExp(e->loc, data[(size_t)i], Type::tsize_t));

    ArrayLiteralExp* ale = new ArrayLiteralExp(e->loc, Type::tsize_t->sarrayOf(cntdata + 1), exps);
    return ale;
}

static Expression *dimError(TraitsExp *e, int expected, int dim)
{
    e->error("expected %d arguments for `%s` but had %d", expected, e->ident->toChars(), dim);
    return new ErrorExp();
}

Expression *semanticTraits(TraitsExp *e, Scope *sc)
{
    if (e->ident != Id::compiles && e->ident != Id::isSame &&
        e->ident != Id::identifier && e->ident != Id::getProtection)
    {
        if (!TemplateInstance::semanticTiargs(e->loc, sc, e->args, 1))
            return new ErrorExp();
    }
    size_t dim = e->args ? e->args->dim : 0;

    if (e->ident == Id::isArithmetic)
    {
        return isTypeX(e, &isTypeArithmetic);
    }
    else if (e->ident == Id::isFloating)
    {
        return isTypeX(e, &isTypeFloating);
    }
    else if (e->ident == Id::isIntegral)
    {
        return isTypeX(e, &isTypeIntegral);
    }
    else if (e->ident == Id::isScalar)
    {
        return isTypeX(e, &isTypeScalar);
    }
    else if (e->ident == Id::isUnsigned)
    {
        return isTypeX(e, &isTypeUnsigned);
    }
    else if (e->ident == Id::isAssociativeArray)
    {
        return isTypeX(e, &isTypeAssociativeArray);
    }
    else if (e->ident == Id::isStaticArray)
    {
        return isTypeX(e, &isTypeStaticArray);
    }
    else if (e->ident == Id::isAbstractClass)
    {
        return isTypeX(e, &isTypeAbstractClass);
    }
    else if (e->ident == Id::isFinalClass)
    {
        return isTypeX(e, &isTypeFinalClass);
    }
    else if (e->ident == Id::isTemplate)
    {
        return isSymbolX(e, &isTemplate);
    }
    else if (e->ident == Id::isPOD)
    {
        if (dim != 1)
            return dimError(e, 1, dim);

        RootObject *o = (*e->args)[0];
        Type *t = isType(o);
        if (!t)
        {
            e->error("type expected as second argument of __traits %s instead of %s",
                e->ident->toChars(), o->toChars());
            return new ErrorExp();
        }

        Type *tb = t->baseElemOf();
        if (StructDeclaration *sd = (tb->ty == Tstruct) ? ((TypeStruct *)tb)->sym : NULL)
        {
            return (sd->isPOD()) ? True(e) : False(e);
        }
        return True(e);
    }
    else if (e->ident == Id::isNested)
    {
        if (dim != 1)
            return dimError(e, 1, dim);

        RootObject *o = (*e->args)[0];
        Dsymbol *s = getDsymbol(o);
        if (!s)
        {
        }
        else if (AggregateDeclaration *a = s->isAggregateDeclaration())
        {
            return a->isNested() ? True(e) : False(e);
        }
        else if (FuncDeclaration *f = s->isFuncDeclaration())
        {
            return f->isNested() ? True(e) : False(e);
        }

        e->error("aggregate or function expected instead of '%s'", o->toChars());
        return new ErrorExp();
    }
    else if (e->ident == Id::isAbstractFunction)
    {
        return isFuncX(e, &isFuncAbstractFunction);
    }
    else if (e->ident == Id::isVirtualFunction)
    {
        return isFuncX(e, &isFuncVirtualFunction);
    }
    else if (e->ident == Id::isVirtualMethod)
    {
        return isFuncX(e, &isFuncVirtualMethod);
    }
    else if (e->ident == Id::isFinalFunction)
    {
        return isFuncX(e, &isFuncFinalFunction);
    }
    else if (e->ident == Id::isOverrideFunction)
    {
        return isFuncX(e, &isFuncOverrideFunction);
    }
    else if (e->ident == Id::isStaticFunction)
    {
        return isFuncX(e, &isFuncStaticFunction);
    }
    else if (e->ident == Id::isRef)
    {
        return isDeclX(e, &isDeclRef);
    }
    else if (e->ident == Id::isOut)
    {
        return isDeclX(e, &isDeclOut);
    }
    else if (e->ident == Id::isLazy)
    {
        return isDeclX(e, &isDeclLazy);
    }
    else if (e->ident == Id::identifier)
    {
        // Get identifier for symbol as a string literal
        /* Specify 0 for bit 0 of the flags argument to semanticTiargs() so that
         * a symbol should not be folded to a constant.
         * Bit 1 means don't convert Parameter to Type if Parameter has an identifier
         */
        if (!TemplateInstance::semanticTiargs(e->loc, sc, e->args, 2))
            return new ErrorExp();
        if (dim != 1)
            return dimError(e, 1, dim);

        RootObject *o = (*e->args)[0];
        Identifier *id = NULL;
        if (Parameter *po = isParameter(o))
        {
            id = po->ident;
            assert(id);
        }
        else
        {
            Dsymbol *s = getDsymbol(o);
            if (!s || !s->ident)
            {
                e->error("argument %s has no identifier", o->toChars());
                return new ErrorExp();
            }
            id = s->ident;
        }

        StringExp *se = new StringExp(e->loc, const_cast<char *>(id->toChars()));
        return semantic(se, sc);
    }
    else if (e->ident == Id::getProtection)
    {
        if (dim != 1)
            return dimError(e, 1, dim);

        Scope *sc2 = sc->push();
        sc2->flags = sc->flags | SCOPEnoaccesscheck;
        bool ok = TemplateInstance::semanticTiargs(e->loc, sc2, e->args, 1);
        sc2->pop();
        if (!ok)
            return new ErrorExp();

        RootObject *o = (*e->args)[0];
        Dsymbol *s = getDsymbol(o);
        if (!s)
        {
            if (!isError(o))
                e->error("argument %s has no protection", o->toChars());
            return new ErrorExp();
        }
        if (s->semanticRun == PASSinit)
            s->semantic(NULL);

        const char *protName = protectionToChars(s->prot().kind);   // TODO: How about package(names)
        assert(protName);
        StringExp *se = new StringExp(e->loc, const_cast<char *>(protName));
        return semantic(se, sc);
    }
    else if (e->ident == Id::parent)
    {
        if (dim != 1)
            return dimError(e, 1, dim);

        RootObject *o = (*e->args)[0];
        Dsymbol *s = getDsymbol(o);
        if (s)
        {
            if (FuncDeclaration *fd = s->isFuncDeclaration())   // Bugzilla 8943
                s = fd->toAliasFunc();
            if (!s->isImport())  // Bugzilla 8922
                s = s->toParent();
        }
        if (!s || s->isImport())
        {
            e->error("argument %s has no parent", o->toChars());
            return new ErrorExp();
        }

        if (FuncDeclaration *f = s->isFuncDeclaration())
        {
            if (TemplateDeclaration *td = getFuncTemplateDecl(f))
            {
                if (td->overroot)       // if not start of overloaded list of TemplateDeclaration's
                    td = td->overroot;  // then get the start
                Expression *ex = new TemplateExp(e->loc, td, f);
                ex = semantic(ex, sc);
                return ex;
            }

            if (FuncLiteralDeclaration *fld = f->isFuncLiteralDeclaration())
            {
                // Directly translate to VarExp instead of FuncExp
                Expression *ex = new VarExp(e->loc, fld, true);
                return semantic(ex, sc);
            }
        }

        return resolve(e->loc, sc, s, false);
    }
    else if (e->ident == Id::hasMember ||
             e->ident == Id::getMember ||
             e->ident == Id::getOverloads ||
             e->ident == Id::getVirtualMethods ||
             e->ident == Id::getVirtualFunctions)
    {
        if (dim != 2)
            return dimError(e, 2, dim);

        RootObject *o = (*e->args)[0];
        Expression *ex = isExpression((*e->args)[1]);
        if (!ex)
        {
            e->error("expression expected as second argument of __traits %s", e->ident->toChars());
            return new ErrorExp();
        }
        ex = ex->ctfeInterpret();

        StringExp *se = ex->toStringExp();
        if (!se || se->len == 0)
        {
            e->error("string expected as second argument of __traits %s instead of %s", e->ident->toChars(), ex->toChars());
            return new ErrorExp();
        }
        se = se->toUTF8(sc);

        if (se->sz != 1)
        {
            e->error("string must be chars");
            return new ErrorExp();
        }
        Identifier *id = Identifier::idPool((char *)se->string, se->len);

        /* Prefer dsymbol, because it might need some runtime contexts.
         */
        Dsymbol *sym = getDsymbol(o);
        if (sym)
        {
            ex = new DsymbolExp(e->loc, sym);
            ex = new DotIdExp(e->loc, ex, id);
        }
        else if (Type *t = isType(o))
            ex = typeDotIdExp(e->loc, t, id);
        else if (Expression *ex2 = isExpression(o))
            ex = new DotIdExp(e->loc, ex2, id);
        else
        {
            e->error("invalid first argument");
            return new ErrorExp();
        }

        if (e->ident == Id::hasMember)
        {
            if (sym)
            {
                if (sym->search(e->loc, id))
                    return True(e);
            }

            /* Take any errors as meaning it wasn't found
             */
            Scope *scx = sc->push();
            scx->flags |= SCOPEignoresymbolvisibility;
            ex = trySemantic(ex, scx);
            scx->pop();
            return ex ? True(e) : False(e);
        }
        else if (e->ident == Id::getMember)
        {
            if (ex->op == TOKdotid)
                // Prevent semantic() from replacing Symbol with its initializer
                ((DotIdExp *)ex)->wantsym = true;
            Scope *scx = sc->push();
            scx->flags |= SCOPEignoresymbolvisibility;
            ex = semantic(ex, scx);
            scx->pop();
            return ex;
        }
        else if (e->ident == Id::getVirtualFunctions ||
                 e->ident == Id::getVirtualMethods ||
                 e->ident == Id::getOverloads)
        {
            unsigned errors = global.errors;
            Expression *eorig = ex;
            Scope *scx = sc->push();
            scx->flags |= SCOPEignoresymbolvisibility;
            ex = semantic(ex, scx);
            if (errors < global.errors)
                e->error("%s cannot be resolved", eorig->toChars());
            //ex->print();

            /* Create tuple of functions of ex
             */
            Expressions *exps = new Expressions();
            FuncDeclaration *f;
            if (ex->op == TOKvar)
            {
                VarExp *ve = (VarExp *)ex;
                f = ve->var->isFuncDeclaration();
                ex = NULL;
            }
            else if (ex->op == TOKdotvar)
            {
                DotVarExp *dve = (DotVarExp *)ex;
                f = dve->var->isFuncDeclaration();
                if (dve->e1->op == TOKdottype || dve->e1->op == TOKthis)
                    ex = NULL;
                else
                    ex = dve->e1;
            }
            else
                f = NULL;
            Ptrait p;
            p.exps = exps;
            p.e1 = ex;
            p.ident = e->ident;
            overloadApply(f, &p, &fptraits);

            ex = new TupleExp(e->loc, exps);
            ex = semantic(ex, scx);
            scx->pop();
            return ex;
        }
        else
            assert(0);
    }
    else if (e->ident == Id::classInstanceSize)
    {
        if (dim != 1)
            return dimError(e, 1, dim);

        RootObject *o = (*e->args)[0];
        Dsymbol *s = getDsymbol(o);
        ClassDeclaration *cd = s ? s->isClassDeclaration() : NULL;
        if (!cd)
        {
            e->error("first argument is not a class");
            return new ErrorExp();
        }
        if (cd->sizeok != SIZEOKdone)
        {
            cd->size(cd->loc);
        }
        if (cd->sizeok != SIZEOKdone)
        {
            e->error("%s %s is forward referenced", cd->kind(), cd->toChars());
            return new ErrorExp();
        }

        return new IntegerExp(e->loc, cd->structsize, Type::tsize_t);
    }
    else if (e->ident == Id::getAliasThis)
    {
        if (dim != 1)
            return dimError(e, 1, dim);

        RootObject *o = (*e->args)[0];
        Dsymbol *s = getDsymbol(o);
        AggregateDeclaration *ad = s ? s->isAggregateDeclaration() : NULL;
        if (!ad)
        {
            e->error("argument is not an aggregate type");
            return new ErrorExp();
        }

        Expressions *exps = new Expressions();
        if (ad->aliasthis)
            exps->push(new StringExp(e->loc, const_cast<char *>(ad->aliasthis->ident->toChars())));
        Expression *ex = new TupleExp(e->loc, exps);
        ex = semantic(ex, sc);
        return ex;
    }
    else if (e->ident == Id::getAttributes)
    {
        if (dim != 1)
            return dimError(e, 1, dim);

        RootObject *o = (*e->args)[0];
        Dsymbol *s = getDsymbol(o);
        if (!s)
        {
            e->error("first argument is not a symbol");
            return new ErrorExp();
        }
        if (Import *imp = s->isImport())
        {
            s = imp->mod;
        }

        //printf("getAttributes %s, attrs = %p, scope = %p\n", s->toChars(), s->userAttribDecl, s->_scope);
        UserAttributeDeclaration *udad = s->userAttribDecl;
        Expressions *exps = udad ? udad->getAttributes() : new Expressions();
        TupleExp *tup = new TupleExp(e->loc, exps);
        return semantic(tup, sc);
    }
    else if (e->ident == Id::getFunctionAttributes)
    {
        /// extract all function attributes as a tuple (const/shared/inout/pure/nothrow/etc) except UDAs.
        if (dim != 1)
            return dimError(e, 1, dim);

        RootObject *o = (*e->args)[0];
        Dsymbol *s = getDsymbol(o);
        Type *t = isType(o);
        TypeFunction *tf = NULL;
        if (s)
        {
            if (FuncDeclaration *f = s->isFuncDeclaration())
                t = f->type;
            else if (VarDeclaration *v = s->isVarDeclaration())
                t = v->type;
        }
        if (t)
        {
            if (t->ty == Tfunction)
                tf = (TypeFunction *)t;
            else if (t->ty == Tdelegate)
                tf = (TypeFunction *)t->nextOf();
            else if (t->ty == Tpointer && t->nextOf()->ty == Tfunction)
                tf = (TypeFunction *)t->nextOf();
        }
        if (!tf)
        {
            e->error("first argument is not a function");
            return new ErrorExp();
        }

        Expressions *mods = new Expressions();
        PushAttributes pa;
        pa.mods = mods;
        tf->modifiersApply(&pa, &PushAttributes::fp);
        tf->attributesApply(&pa, &PushAttributes::fp, TRUSTformatSystem);

        TupleExp *tup = new TupleExp(e->loc, mods);
        return semantic(tup, sc);
    }
    else if (e->ident == Id::getFunctionVariadicStyle)
    {
        /* Accept a symbol or a type. Returns one of the following:
         *  "none"      not a variadic function
         *  "argptr"    extern(D) void dstyle(...), use `__argptr` and `__arguments`
         *  "stdarg"    extern(C) void cstyle(int, ...), use core.stdc.stdarg
         *  "typesafe"  void typesafe(T[] ...)
         */
        // get symbol linkage as a string
        if (dim != 1)
            return dimError(e, 1, dim);

        LINK link;
        int varargs;
        RootObject *o = (*e->args)[0];
        Type *t = isType(o);
        TypeFunction *tf = NULL;
        if (t)
        {
            if (t->ty == Tfunction)
                tf = (TypeFunction *)t;
            else if (t->ty == Tdelegate)
                tf = (TypeFunction *)t->nextOf();
            else if (t->ty == Tpointer && t->nextOf()->ty == Tfunction)
                tf = (TypeFunction *)t->nextOf();
        }
        if (tf)
        {
            link = tf->linkage;
            varargs = tf->varargs;
        }
        else
        {
            Dsymbol *s = getDsymbol(o);
            FuncDeclaration *fd = NULL;
            if (!s || (fd = s->isFuncDeclaration()) == NULL)
            {
                e->error("argument to `__traits(getFunctionVariadicStyle, %s)` is not a function", o->toChars());
                return new ErrorExp();
            }
            link = fd->linkage;
            fd->getParameters(&varargs);
        }
        const char *style;
        switch (varargs)
        {
            case 0: style = "none";                      break;
            case 1: style = (link == LINKd) ? "argptr"
                                            : "stdarg";  break;
            case 2:     style = "typesafe";              break;
            default:
                assert(0);
        }
        StringExp *se = new StringExp(e->loc, const_cast<char*>(style));
        return semantic(se, sc);
    }
    else if (e->ident == Id::getParameterStorageClasses)
    {
        /* Accept a function symbol or a type, followed by a parameter index.
         * Returns a tuple of strings of the parameter's storage classes.
         */
        // get symbol linkage as a string
        if (dim != 2)
            return dimError(e, 2, dim);

        RootObject *o1 = (*e->args)[1];
        RootObject *o = (*e->args)[0];
        Type *t = isType(o);
        TypeFunction *tf = NULL;
        if (t)
        {
            if (t->ty == Tfunction)
                tf = (TypeFunction *)t;
            else if (t->ty == Tdelegate)
                tf = (TypeFunction *)t->nextOf();
            else if (t->ty == Tpointer && t->nextOf()->ty == Tfunction)
                tf = (TypeFunction *)t->nextOf();
        }
        Parameters* fparams;
        if (tf)
        {
            fparams = tf->parameters;
        }
        else
        {
            Dsymbol *s = getDsymbol(o);
            FuncDeclaration *fd = NULL;
            if (!s || (fd = s->isFuncDeclaration()) == NULL)
            {
                e->error("first argument to `__traits(getParameterStorageClasses, %s, %s)` is not a function",
                    o->toChars(), o1->toChars());
                return new ErrorExp();
            }
            fparams = fd->getParameters(NULL);
        }

        StorageClass stc;

        // Set stc to storage class of the ith parameter
        Expression *ex = isExpression((*e->args)[1]);
        if (!ex)
        {
            e->error("expression expected as second argument of `__traits(getParameterStorageClasses, %s, %s)`",
                o->toChars(), o1->toChars());
            return new ErrorExp();
        }
        ex = ex->ctfeInterpret();
        uinteger_t ii = ex->toUInteger();
        if (ii >= Parameter::dim(fparams))
        {
            e->error("parameter index must be in range 0..%u not %s", (unsigned)Parameter::dim(fparams), ex->toChars());
            return new ErrorExp();
        }

        unsigned n = (unsigned)ii;
        Parameter *p = Parameter::getNth(fparams, n);
        stc = p->storageClass;

        // This mirrors hdrgen.visit(Parameter p)
        if (p->type && p->type->mod & MODshared)
            stc &= ~STCshared;

        Expressions *exps = new Expressions;

        if (stc & STCauto)
            exps->push(new StringExp(e->loc, const_cast<char *>("auto")));
        if (stc & STCreturn)
            exps->push(new StringExp(e->loc, const_cast<char *>("return")));

        if (stc & STCout)
            exps->push(new StringExp(e->loc, const_cast<char *>("out")));
        else if (stc & STCref)
            exps->push(new StringExp(e->loc, const_cast<char *>("ref")));
        else if (stc & STCin)
            exps->push(new StringExp(e->loc, const_cast<char *>("in")));
        else if (stc & STClazy)
            exps->push(new StringExp(e->loc, const_cast<char *>("lazy")));
        else if (stc & STCalias)
            exps->push(new StringExp(e->loc, const_cast<char *>("alias")));

        if (stc & STCconst)
            exps->push(new StringExp(e->loc, const_cast<char *>("const")));
        if (stc & STCimmutable)
            exps->push(new StringExp(e->loc, const_cast<char *>("immutable")));
        if (stc & STCwild)
            exps->push(new StringExp(e->loc, const_cast<char *>("inout")));
        if (stc & STCshared)
            exps->push(new StringExp(e->loc, const_cast<char *>("shared")));
        if (stc & STCscope && !(stc & STCscopeinferred))
            exps->push(new StringExp(e->loc, const_cast<char *>("scope")));

        TupleExp *tup = new TupleExp(e->loc, exps);
        return semantic(tup, sc);
    }
    else if (e->ident == Id::getLinkage)
    {
        // get symbol linkage as a string
        if (dim != 1)
            return dimError(e, 1, dim);

        LINK link;
        RootObject *o = (*e->args)[0];
        Type *t = isType(o);
        TypeFunction *tf = NULL;
        if (t)
        {
            if (t->ty == Tfunction)
                tf = (TypeFunction *)t;
            else if (t->ty == Tdelegate)
                tf = (TypeFunction *)t->nextOf();
            else if (t->ty == Tpointer && t->nextOf()->ty == Tfunction)
                tf = (TypeFunction *)t->nextOf();
        }
        if (tf)
            link = tf->linkage;
        else
        {
            Dsymbol *s = getDsymbol(o);
            Declaration *d = NULL;
            if (!s || (d = s->isDeclaration()) == NULL)
            {
                e->error("argument to `__traits(getLinkage, %s)` is not a declaration", o->toChars());
                return new ErrorExp();
            }
            link = d->linkage;
        }
        const char *linkage = linkageToChars(link);
        StringExp *se = new StringExp(e->loc, const_cast<char *>(linkage));
        return semantic(se, sc);
    }
    else if (e->ident == Id::allMembers ||
             e->ident == Id::derivedMembers)
    {
        if (dim != 1)
            return dimError(e, 1, dim);

        RootObject *o = (*e->args)[0];
        Dsymbol *s = getDsymbol(o);
        if (!s)
        {
            e->error("argument has no members");
            return new ErrorExp();
        }
        if (Import *imp = s->isImport())
        {
            // Bugzilla 9692
            s = imp->mod;
        }

        ScopeDsymbol *sds = s->isScopeDsymbol();
        if (!sds || sds->isTemplateDeclaration())
        {
            e->error("%s %s has no members", s->kind(), s->toChars());
            return new ErrorExp();
        }

        // use a struct as local function
        struct PushIdentsDg
        {
            ScopeDsymbol *sds;
            Identifiers *idents;

            static int dg(void *ctx, size_t, Dsymbol *sm)
            {
                if (!sm)
                    return 1;
                //printf("\t[%i] %s %s\n", i, sm->kind(), sm->toChars());
                if (sm->ident)
                {
                    const char *idx = sm->ident->toChars();
                    if (idx[0] == '_' && idx[1] == '_' &&
                        sm->ident != Id::ctor &&
                        sm->ident != Id::dtor &&
                        sm->ident != Id::__xdtor &&
                        sm->ident != Id::postblit &&
                        sm->ident != Id::__xpostblit)
                    {
                        return 0;
                    }

                    if (sm->ident == Id::empty)
                    {
                        return 0;
                    }
                    if (sm->isTypeInfoDeclaration()) // Bugzilla 15177
                        return 0;
                    PushIdentsDg *pid = (PushIdentsDg *)ctx;
                    if (!pid->sds->isModule() && sm->isImport()) // Bugzilla 17057
                        return 0;

                    //printf("\t%s\n", sm->ident->toChars());
                    Identifiers *idents = pid->idents;

                    /* Skip if already present in idents[]
                     */
                    for (size_t j = 0; j < idents->dim; j++)
                    {
                        Identifier *id = (*idents)[j];
                        if (id == sm->ident)
                            return 0;
                    }

                    idents->push(sm->ident);
                }
                else
                {
                    EnumDeclaration *ed = sm->isEnumDeclaration();
                    if (ed)
                    {
                        ScopeDsymbol_foreach(NULL, ed->members, &PushIdentsDg::dg, ctx);
                    }
                }
                return 0;
            }
        };

        Identifiers *idents = new Identifiers;
        PushIdentsDg ctx;
        ctx.sds = sds;
        ctx.idents = idents;
        ScopeDsymbol_foreach(sc, sds->members, &PushIdentsDg::dg, &ctx);
        ClassDeclaration *cd = sds->isClassDeclaration();
        if (cd && e->ident == Id::allMembers)
        {
            if (cd->semanticRun < PASSsemanticdone)
                cd->semantic(NULL);    // Bugzilla 13668: Try to resolve forward reference

            struct PushBaseMembers
            {
                static void dg(ClassDeclaration *cd, PushIdentsDg *ctx)
                {
                    for (size_t i = 0; i < cd->baseclasses->dim; i++)
                    {
                        ClassDeclaration *cb = (*cd->baseclasses)[i]->sym;
                        assert(cb);
                        ScopeDsymbol_foreach(NULL, cb->members, &PushIdentsDg::dg, ctx);
                        if (cb->baseclasses->dim)
                            dg(cb, ctx);
                    }
                }
            };
            PushBaseMembers::dg(cd, &ctx);
        }

        // Turn Identifiers into StringExps reusing the allocated array
        assert(sizeof(Expressions) == sizeof(Identifiers));
        Expressions *exps = (Expressions *)idents;
        for (size_t i = 0; i < idents->dim; i++)
        {
            Identifier *id = (*idents)[i];
            StringExp *se = new StringExp(e->loc, const_cast<char *>(id->toChars()));
            (*exps)[i] = se;
        }

        /* Making this a tuple is more flexible, as it can be statically unrolled.
         * To make an array literal, enclose __traits in [ ]:
         *   [ __traits(allMembers, ...) ]
         */
        Expression *ex = new TupleExp(e->loc, exps);
        ex = semantic(ex, sc);
        return ex;
    }
    else if (e->ident == Id::compiles)
    {
        /* Determine if all the objects - types, expressions, or symbols -
         * compile without error
         */
        if (!dim)
            return False(e);

        for (size_t i = 0; i < dim; i++)
        {
            unsigned errors = global.startGagging();
            Scope *sc2 = sc->push();
            sc2->tinst = NULL;
            sc2->minst = NULL;
            sc2->flags = (sc->flags & ~(SCOPEctfe | SCOPEcondition)) | SCOPEcompile | SCOPEfullinst;
            bool err = false;

            RootObject *o = (*e->args)[i];
            Type *t = isType(o);
            Expression *ex = t ? typeToExpression(t) : isExpression(o);
            if (!ex && t)
            {
                Dsymbol *s;
                t->resolve(e->loc, sc2, &ex, &t, &s);
                if (t)
                {
                    t->semantic(e->loc, sc2);
                    if (t->ty == Terror)
                        err = true;
                }
                else if (s && s->errors)
                    err = true;
            }
            if (ex)
            {
                ex = semantic(ex, sc2);
                ex = resolvePropertiesOnly(sc2, ex);
                ex = ex->optimize(WANTvalue);
                if (sc2->func && sc2->func->type->ty == Tfunction)
                {
                    TypeFunction *tf = (TypeFunction *)sc2->func->type;
                    canThrow(ex, sc2->func, tf->isnothrow);
                }
                ex = checkGC(sc2, ex);
                if (ex->op == TOKerror)
                    err = true;
            }

            // Carefully detach the scope from the parent and throw it away as
            // we only need it to evaluate the expression
            // https://issues.dlang.org/show_bug.cgi?id=15428
            freeFieldinit(sc2);
            sc2->enclosing = NULL;
            sc2->pop();

            if (global.endGagging(errors) || err)
            {
                return False(e);
            }
        }
        return True(e);
    }
    else if (e->ident == Id::isSame)
    {
        /* Determine if two symbols are the same
         */
        if (dim != 2)
            return dimError(e, 2, dim);

        if (!TemplateInstance::semanticTiargs(e->loc, sc, e->args, 0))
            return new ErrorExp();

        RootObject *o1 = (*e->args)[0];
        RootObject *o2 = (*e->args)[1];
        Dsymbol *s1 = getDsymbol(o1);
        Dsymbol *s2 = getDsymbol(o2);
        //printf("isSame: %s, %s\n", o1->toChars(), o2->toChars());
        if (!s1 && !s2)
        {
            Expression *ea1 = isExpression(o1);
            Expression *ea2 = isExpression(o2);
            if (ea1 && ea2)
            {
                if (ea1->equals(ea2))
                    return True(e);
            }
        }
        if (!s1 || !s2)
            return False(e);
        s1 = s1->toAlias();
        s2 = s2->toAlias();

        if (s1->isFuncAliasDeclaration())
            s1 = ((FuncAliasDeclaration *)s1)->toAliasFunc();
        if (s2->isFuncAliasDeclaration())
            s2 = ((FuncAliasDeclaration *)s2)->toAliasFunc();

        return (s1 == s2) ? True(e) : False(e);
    }
    else if (e->ident == Id::getUnitTests)
    {
        if (dim != 1)
            return dimError(e, 1, dim);

        RootObject *o = (*e->args)[0];
        Dsymbol *s = getDsymbol(o);
        if (!s)
        {
            e->error("argument %s to __traits(getUnitTests) must be a module or aggregate",
                o->toChars());
            return new ErrorExp();
        }
        if (Import *imp = s->isImport())  // Bugzilla 10990
            s = imp->mod;

        ScopeDsymbol* sds = s->isScopeDsymbol();
        if (!sds)
        {
            e->error("argument %s to __traits(getUnitTests) must be a module or aggregate, not a %s",
                s->toChars(), s->kind());
            return new ErrorExp();
        }

        Expressions *exps = new Expressions();
        if (global.params.useUnitTests)
        {
            // Should actually be a set
            AA* uniqueUnitTests = NULL;
            collectUnitTests(sds->members, uniqueUnitTests, exps);
        }
        TupleExp *te= new TupleExp(e->loc, exps);
        return semantic(te, sc);
    }
    else if(e->ident == Id::getVirtualIndex)
    {
        if (dim != 1)
            return dimError(e, 1, dim);

        RootObject *o = (*e->args)[0];
        Dsymbol *s = getDsymbol(o);

        FuncDeclaration *fd = s ? s->isFuncDeclaration() : NULL;
        if (!fd)
        {
            e->error("first argument to __traits(getVirtualIndex) must be a function");
            return new ErrorExp();
        }

        fd = fd->toAliasFunc(); // Neccessary to support multiple overloads.
        return new IntegerExp(e->loc, fd->vtblIndex, Type::tptrdiff_t);
    }
    else if (e->ident == Id::getPointerBitmap)
    {
        return pointerBitmap(e);
    }

    if (const char *sub = (const char *)speller(e->ident->toChars(), &trait_search_fp, NULL, idchars))
        e->error("unrecognized trait '%s', did you mean '%s'?", e->ident->toChars(), sub);
    else
        e->error("unrecognized trait '%s'", e->ident->toChars());
    return new ErrorExp();

    e->error("wrong number of arguments %d", (int)dim);
    return new ErrorExp();
}