-
-
Notifications
You must be signed in to change notification settings - Fork 701
Expand file tree
/
Copy pathexpression.d
More file actions
4245 lines (3732 loc) · 121 KB
/
expression.d
File metadata and controls
4245 lines (3732 loc) · 121 KB
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
/**
* Defines the bulk of the classes which represent the AST at the expression level.
*
* Specification: ($LINK2 https://dlang.org/spec/expression.html, Expressions)
*
* Copyright: Copyright (C) 1999-2026 by The D Language Foundation, All Rights Reserved
* Authors: $(LINK2 https://www.digitalmars.com, Walter Bright)
* License: $(LINK2 https://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
* Source: $(LINK2 https://github.com/dlang/dmd/blob/master/compiler/src/dmd/expression.d, _expression.d)
* Documentation: https://dlang.org/phobos/dmd_expression.html
* Coverage: https://codecov.io/gh/dlang/dmd/src/master/compiler/src/dmd/expression.d
*/
module dmd.expression;
import core.stdc.stdarg;
import core.stdc.stdio;
import core.stdc.string;
import dmd.arraytypes;
import dmd.astenums;
import dmd.ast_node;
import dmd.dclass;
import dmd.declaration;
import dmd.dstruct;
import dmd.dsymbol;
import dmd.dtemplate;
import dmd.errors;
import dmd.func;
import dmd.globals;
import dmd.hdrgen : toChars;
import dmd.id;
import dmd.identifier;
import dmd.init;
import dmd.location;
import dmd.mtype;
import dmd.root.complex;
import dmd.root.ctfloat;
import dmd.root.rmem;
import dmd.rootobject;
import dmd.root.string;
import dmd.root.utf;
import dmd.tokens;
import dmd.visitor;
enum LOGSEMANTIC = false;
/****************************************
* Find the last non-comma expression.
* Params:
* e = Expressions connected by commas
* Returns:
* right-most non-comma expression
*/
inout(Expression) lastComma(inout Expression e)
{
Expression ex = cast()e;
while (ex.op == EXP.comma)
ex = (cast(CommaExp)ex).e2;
return cast(inout)ex;
}
/****************************************
* If `s` is a function template, i.e. the only member of a template
* and that member is a function, return that template.
* Params:
* s = symbol that might be a function template
* Returns:
* template for that function, otherwise null
*/
TemplateDeclaration getFuncTemplateDecl(Dsymbol s) @safe
{
FuncDeclaration f = s.isFuncDeclaration();
if (f && f.parent)
{
if (auto ti = f.parent.isTemplateInstance())
{
if (!ti.isTemplateMixin() && ti.tempdecl)
{
auto td = ti.tempdecl.isTemplateDeclaration();
if (td.onemember && td.ident == f.ident)
{
return td;
}
}
}
}
return null;
}
/************************ TypeDotIdExp ************************************/
/* Things like:
* int.size
* foo.size
* (foo).size
* cast(foo).size
*/
DotIdExp typeDotIdExp(Loc loc, Type type, Identifier ident) @safe
{
return new DotIdExp(loc, new TypeExp(loc, type), ident);
}
enum OwnedBy : ubyte
{
code, // normal code expression in AST
ctfe, // value expression for CTFE
cache, // constant value cached for CTFE
}
enum WANTvalue = 0; // default
enum WANTexpand = 1; // expand const/immutable variables if possible
/***********************************************************
* https://dlang.org/spec/expression.html#expression
*/
extern (C++) abstract class Expression : ASTNode
{
/// Usually, this starts out as `null` and gets set to the final expression type by
/// `expressionSemantic`. However, for some expressions (such as `TypeExp`,`RealExp`,
/// `VarExp`), the field can get set to an assigned type before running semantic.
/// See `expressionSemanticDone`
Type type;
Loc loc; // file location
const EXP op; // to minimize use of dynamic_cast
static struct BitFields
{
bool parens; // if this is a parenthesized expression
bool rvalue; // true if this is considered to be an rvalue, even if it is an lvalue
bool gcPassDone; // `checkGC` has been run on this expression
}
import dmd.common.bitfields;
mixin(generateBitFields!(BitFields, ubyte));
extern (D) this(Loc loc, EXP op) scope @safe
{
//printf("Expression::Expression(op = %d) this = %p\n", op, this);
this.loc = loc;
this.op = op;
}
/// Returns: class instance size of this expression (implemented manually because `extern(C++)`)
final size_t size() nothrow @nogc pure @safe const { return expSize[op]; }
static void _init()
{
CTFEExp.cantexp = new CTFEExp(EXP.cantExpression);
CTFEExp.voidexp = new CTFEExp(EXP.voidExpression);
CTFEExp.breakexp = new CTFEExp(EXP.break_);
CTFEExp.continueexp = new CTFEExp(EXP.continue_);
CTFEExp.gotoexp = new CTFEExp(EXP.goto_);
CTFEExp.showcontext = new CTFEExp(EXP.showCtfeContext);
}
/**
* Deinitializes the global state of the compiler.
*
* This can be used to restore the state set by `_init` to its original
* state.
*/
static void deinitialize()
{
CTFEExp.cantexp = CTFEExp.cantexp.init;
CTFEExp.voidexp = CTFEExp.voidexp.init;
CTFEExp.breakexp = CTFEExp.breakexp.init;
CTFEExp.continueexp = CTFEExp.continueexp.init;
CTFEExp.gotoexp = CTFEExp.gotoexp.init;
CTFEExp.showcontext = CTFEExp.showcontext.init;
}
/*********************************
* Does *not* do a deep copy.
*/
extern (D) final Expression copy()
{
Expression e;
if (!size)
{
debug
{
fprintf(stderr, "No expression copy for: %s\n", toChars());
printf("op = %d\n", op);
}
assert(0);
}
// memory never freed, so can use the faster bump-pointer-allocation
e = cast(Expression)allocmemory(size);
//printf("Expression::copy(op = %d) e = %p\n", op, e);
return cast(Expression)memcpy(cast(void*)e, cast(void*)this, size);
}
Expression syntaxCopy()
{
//printf("Expression::syntaxCopy()\n");
//print();
return copy();
}
// kludge for template.isExpression()
override final DYNCAST dyncast() const
{
return DYNCAST.expression;
}
final override const(char)* toChars() const
{
// FIXME: mangling (see runnable/mangle.d) relies on toChars outputting __lambdaXXX here
if (auto fe = isFuncExp())
return fe.fd.toChars();
return .toChars(this);
}
/**********************************
* Combine e1 and e2 by CommaExp if both are not NULL.
*/
extern (D) static Expression combine(Expression e1, Expression e2) @safe
{
if (e1)
{
if (e2)
{
e1 = new CommaExp(e1.loc, e1, e2);
e1.type = e2.type;
}
}
else
e1 = e2;
return e1;
}
extern (D) static Expression combine(Expression e1, Expression e2, Expression e3) @safe
{
return combine(combine(e1, e2), e3);
}
extern (D) static Expression combine(Expression e1, Expression e2, Expression e3, Expression e4) @safe
{
return combine(combine(e1, e2), combine(e3, e4));
}
/**********************************
* If 'e' is a tree of commas, returns the rightmost expression
* by stripping off it from the tree. The remained part of the tree
* is returned via e0.
* Otherwise 'e' is directly returned and e0 is set to NULL.
*/
extern (D) static Expression extractLast(Expression e, out Expression e0) @trusted
{
if (e.op != EXP.comma)
{
return e;
}
CommaExp ce = cast(CommaExp)e;
if (ce.e2.op != EXP.comma)
{
e0 = ce.e1;
return ce.e2;
}
else
{
e0 = e;
Expression* pce = &ce.e2;
while ((cast(CommaExp)(*pce)).e2.op == EXP.comma)
{
pce = &(cast(CommaExp)(*pce)).e2;
}
assert((*pce).op == EXP.comma);
ce = cast(CommaExp)(*pce);
*pce = ce.e1;
return ce.e2;
}
}
extern (D) static Expressions* arraySyntaxCopy(Expressions* exps)
{
Expressions* a = null;
if (exps)
{
a = new Expressions(exps.length);
foreach (i, e; *exps)
{
(*a)[i] = e ? e.syntaxCopy() : null;
}
}
return a;
}
/******************************
* If this is a reference, dereference it.
*/
final Expression deref()
{
//printf("Expression::deref()\n");
// type could be null if forward referencing an 'auto' variable
if (type)
if (auto tr = type.isTypeReference())
{
Expression e = new PtrExp(loc, this, tr.next);
return e;
}
return this;
}
final int isConst()
{
//printf("Expression::isConst(): %s\n", e.toChars());
switch (op)
{
case EXP.int64:
case EXP.float64:
case EXP.complex80:
return 1;
case EXP.null_:
return 0;
case EXP.symbolOffset:
return 2;
default:
return 0;
}
assert(0);
}
bool hasCode()
{
return true;
}
final pure inout nothrow @nogc @trusted
{
inout(IntegerExp) isIntegerExp() { return op == EXP.int64 ? cast(typeof(return))this : null; }
inout(ErrorExp) isErrorExp() { return op == EXP.error ? cast(typeof(return))this : null; }
inout(VoidInitExp) isVoidInitExp() { return op == EXP.void_ ? cast(typeof(return))this : null; }
inout(RealExp) isRealExp() { return op == EXP.float64 ? cast(typeof(return))this : null; }
inout(ComplexExp) isComplexExp() { return op == EXP.complex80 ? cast(typeof(return))this : null; }
inout(IdentifierExp) isIdentifierExp() { return op == EXP.identifier ? cast(typeof(return))this : null; }
inout(DollarExp) isDollarExp() { return op == EXP.dollar ? cast(typeof(return))this : null; }
inout(DsymbolExp) isDsymbolExp() { return op == EXP.dSymbol ? cast(typeof(return))this : null; }
inout(ThisExp) isThisExp() { return op == EXP.this_ ? cast(typeof(return))this : null; }
inout(SuperExp) isSuperExp() { return op == EXP.super_ ? cast(typeof(return))this : null; }
inout(NullExp) isNullExp() { return op == EXP.null_ ? cast(typeof(return))this : null; }
inout(StringExp) isStringExp() { return op == EXP.string_ ? cast(typeof(return))this : null; }
inout(InterpExp) isInterpExp() { return op == EXP.interpolated ? cast(typeof(return))this : null; }
inout(TupleExp) isTupleExp() { return op == EXP.tuple ? cast(typeof(return))this : null; }
inout(ArrayLiteralExp) isArrayLiteralExp() { return op == EXP.arrayLiteral ? cast(typeof(return))this : null; }
inout(AssocArrayLiteralExp) isAssocArrayLiteralExp() { return op == EXP.assocArrayLiteral ? cast(typeof(return))this : null; }
inout(StructLiteralExp) isStructLiteralExp() { return op == EXP.structLiteral ? cast(typeof(return))this : null; }
inout(CompoundLiteralExp) isCompoundLiteralExp() { return op == EXP.compoundLiteral ? cast(typeof(return))this : null; }
inout(TypeExp) isTypeExp() { return op == EXP.type ? cast(typeof(return))this : null; }
inout(ScopeExp) isScopeExp() { return op == EXP.scope_ ? cast(typeof(return))this : null; }
inout(TemplateExp) isTemplateExp() { return op == EXP.template_ ? cast(typeof(return))this : null; }
inout(NewExp) isNewExp() { return op == EXP.new_ ? cast(typeof(return))this : null; }
inout(NewAnonClassExp) isNewAnonClassExp() { return op == EXP.newAnonymousClass ? cast(typeof(return))this : null; }
inout(SymOffExp) isSymOffExp() { return op == EXP.symbolOffset ? cast(typeof(return))this : null; }
inout(VarExp) isVarExp() { return op == EXP.variable ? cast(typeof(return))this : null; }
inout(OverExp) isOverExp() { return op == EXP.overloadSet ? cast(typeof(return))this : null; }
inout(FuncExp) isFuncExp() { return op == EXP.function_ ? cast(typeof(return))this : null; }
inout(DeclarationExp) isDeclarationExp() { return op == EXP.declaration ? cast(typeof(return))this : null; }
inout(TypeidExp) isTypeidExp() { return op == EXP.typeid_ ? cast(typeof(return))this : null; }
inout(TraitsExp) isTraitsExp() { return op == EXP.traits ? cast(typeof(return))this : null; }
inout(HaltExp) isHaltExp() { return op == EXP.halt ? cast(typeof(return))this : null; }
inout(IsExp) isIsExp() { return op == EXP.is_ ? cast(typeof(return))this : null; }
inout(MixinExp) isMixinExp() { return op == EXP.mixin_ ? cast(typeof(return))this : null; }
inout(ImportExp) isImportExp() { return op == EXP.import_ ? cast(typeof(return))this : null; }
inout(AssertExp) isAssertExp() { return op == EXP.assert_ ? cast(typeof(return))this : null; }
inout(ThrowExp) isThrowExp() { return op == EXP.throw_ ? cast(typeof(return))this : null; }
inout(DotIdExp) isDotIdExp() { return op == EXP.dotIdentifier ? cast(typeof(return))this : null; }
inout(DotTemplateExp) isDotTemplateExp() { return op == EXP.dotTemplateDeclaration ? cast(typeof(return))this : null; }
inout(DotVarExp) isDotVarExp() { return op == EXP.dotVariable ? cast(typeof(return))this : null; }
inout(DotTemplateInstanceExp) isDotTemplateInstanceExp() { return op == EXP.dotTemplateInstance ? cast(typeof(return))this : null; }
inout(DelegateExp) isDelegateExp() { return op == EXP.delegate_ ? cast(typeof(return))this : null; }
inout(DotTypeExp) isDotTypeExp() { return op == EXP.dotType ? cast(typeof(return))this : null; }
inout(CallExp) isCallExp() { return op == EXP.call ? cast(typeof(return))this : null; }
inout(AddrExp) isAddrExp() { return op == EXP.address ? cast(typeof(return))this : null; }
inout(PtrExp) isPtrExp() { return op == EXP.star ? cast(typeof(return))this : null; }
inout(NegExp) isNegExp() { return op == EXP.negate ? cast(typeof(return))this : null; }
inout(UAddExp) isUAddExp() { return op == EXP.uadd ? cast(typeof(return))this : null; }
inout(ComExp) isComExp() { return op == EXP.tilde ? cast(typeof(return))this : null; }
inout(NotExp) isNotExp() { return op == EXP.not ? cast(typeof(return))this : null; }
inout(DeleteExp) isDeleteExp() { return op == EXP.delete_ ? cast(typeof(return))this : null; }
inout(CastExp) isCastExp() { return op == EXP.cast_ ? cast(typeof(return))this : null; }
inout(VectorExp) isVectorExp() { return op == EXP.vector ? cast(typeof(return))this : null; }
inout(VectorArrayExp) isVectorArrayExp() { return op == EXP.vectorArray ? cast(typeof(return))this : null; }
inout(SliceExp) isSliceExp() { return op == EXP.slice ? cast(typeof(return))this : null; }
inout(ArrayLengthExp) isArrayLengthExp() { return op == EXP.arrayLength ? cast(typeof(return))this : null; }
inout(ArrayExp) isArrayExp() { return op == EXP.array ? cast(typeof(return))this : null; }
inout(DotExp) isDotExp() { return op == EXP.dot ? cast(typeof(return))this : null; }
inout(CommaExp) isCommaExp() { return op == EXP.comma ? cast(typeof(return))this : null; }
inout(IntervalExp) isIntervalExp() { return op == EXP.interval ? cast(typeof(return))this : null; }
inout(DelegatePtrExp) isDelegatePtrExp() { return op == EXP.delegatePointer ? cast(typeof(return))this : null; }
inout(DelegateFuncptrExp) isDelegateFuncptrExp() { return op == EXP.delegateFunctionPointer ? cast(typeof(return))this : null; }
inout(IndexExp) isIndexExp() { return op == EXP.index ? cast(typeof(return))this : null; }
inout(PostExp) isPostExp() { return (op == EXP.plusPlus || op == EXP.minusMinus) ? cast(typeof(return))this : null; }
inout(PreExp) isPreExp() { return (op == EXP.prePlusPlus || op == EXP.preMinusMinus) ? cast(typeof(return))this : null; }
inout(AssignExp) isAssignExp() { return op == EXP.assign ? cast(typeof(return))this : null; }
inout(LoweredAssignExp) isLoweredAssignExp() { return op == EXP.loweredAssignExp ? cast(typeof(return))this : null; }
inout(ConstructExp) isConstructExp() { return op == EXP.construct ? cast(typeof(return))this : null; }
inout(BlitExp) isBlitExp() { return op == EXP.blit ? cast(typeof(return))this : null; }
inout(AddAssignExp) isAddAssignExp() { return op == EXP.addAssign ? cast(typeof(return))this : null; }
inout(MinAssignExp) isMinAssignExp() { return op == EXP.minAssign ? cast(typeof(return))this : null; }
inout(MulAssignExp) isMulAssignExp() { return op == EXP.mulAssign ? cast(typeof(return))this : null; }
inout(DivAssignExp) isDivAssignExp() { return op == EXP.divAssign ? cast(typeof(return))this : null; }
inout(ModAssignExp) isModAssignExp() { return op == EXP.modAssign ? cast(typeof(return))this : null; }
inout(AndAssignExp) isAndAssignExp() { return op == EXP.andAssign ? cast(typeof(return))this : null; }
inout(OrAssignExp) isOrAssignExp() { return op == EXP.orAssign ? cast(typeof(return))this : null; }
inout(XorAssignExp) isXorAssignExp() { return op == EXP.xorAssign ? cast(typeof(return))this : null; }
inout(PowAssignExp) isPowAssignExp() { return op == EXP.powAssign ? cast(typeof(return))this : null; }
inout(ShlAssignExp) isShlAssignExp() { return op == EXP.leftShiftAssign ? cast(typeof(return))this : null; }
inout(ShrAssignExp) isShrAssignExp() { return op == EXP.rightShiftAssign ? cast(typeof(return))this : null; }
inout(UshrAssignExp) isUshrAssignExp() { return op == EXP.unsignedRightShiftAssign ? cast(typeof(return))this : null; }
inout(CatAssignExp) isCatAssignExp() { return op == EXP.concatenateAssign
? cast(typeof(return))this
: null; }
inout(CatElemAssignExp) isCatElemAssignExp() { return op == EXP.concatenateElemAssign
? cast(typeof(return))this
: null; }
inout(CatDcharAssignExp) isCatDcharAssignExp() { return op == EXP.concatenateDcharAssign
? cast(typeof(return))this
: null; }
inout(AddExp) isAddExp() { return op == EXP.add ? cast(typeof(return))this : null; }
inout(MinExp) isMinExp() { return op == EXP.min ? cast(typeof(return))this : null; }
inout(CatExp) isCatExp() { return op == EXP.concatenate ? cast(typeof(return))this : null; }
inout(MulExp) isMulExp() { return op == EXP.mul ? cast(typeof(return))this : null; }
inout(DivExp) isDivExp() { return op == EXP.div ? cast(typeof(return))this : null; }
inout(ModExp) isModExp() { return op == EXP.mod ? cast(typeof(return))this : null; }
inout(PowExp) isPowExp() { return op == EXP.pow ? cast(typeof(return))this : null; }
inout(ShlExp) isShlExp() { return op == EXP.leftShift ? cast(typeof(return))this : null; }
inout(ShrExp) isShrExp() { return op == EXP.rightShift ? cast(typeof(return))this : null; }
inout(UshrExp) isUshrExp() { return op == EXP.unsignedRightShift ? cast(typeof(return))this : null; }
inout(AndExp) isAndExp() { return op == EXP.and ? cast(typeof(return))this : null; }
inout(OrExp) isOrExp() { return op == EXP.or ? cast(typeof(return))this : null; }
inout(XorExp) isXorExp() { return op == EXP.xor ? cast(typeof(return))this : null; }
inout(LogicalExp) isLogicalExp() { return (op == EXP.andAnd || op == EXP.orOr) ? cast(typeof(return))this : null; }
//inout(CmpExp) isCmpExp() { return op == EXP. ? cast(typeof(return))this : null; }
inout(InExp) isInExp() { return op == EXP.in_ ? cast(typeof(return))this : null; }
inout(RemoveExp) isRemoveExp() { return op == EXP.remove ? cast(typeof(return))this : null; }
inout(EqualExp) isEqualExp() { return (op == EXP.equal || op == EXP.notEqual) ? cast(typeof(return))this : null; }
inout(IdentityExp) isIdentityExp() { return (op == EXP.identity || op == EXP.notIdentity) ? cast(typeof(return))this : null; }
inout(CondExp) isCondExp() { return op == EXP.question ? cast(typeof(return))this : null; }
inout(GenericExp) isGenericExp() { return op == EXP._Generic ? cast(typeof(return))this : null; }
inout(DefaultInitExp) isDefaultInitExp() { return
(op == EXP.prettyFunction || op == EXP.functionString ||
op == EXP.line || op == EXP.moduleString ||
op == EXP.file || op == EXP.fileFullPath ) ? cast(typeof(return))this : null; }
inout(FileInitExp) isFileInitExp() { return (op == EXP.file || op == EXP.fileFullPath) ? cast(typeof(return))this : null; }
inout(LineInitExp) isLineInitExp() { return op == EXP.line ? cast(typeof(return))this : null; }
inout(ModuleInitExp) isModuleInitExp() { return op == EXP.moduleString ? cast(typeof(return))this : null; }
inout(FuncInitExp) isFuncInitExp() { return op == EXP.functionString ? cast(typeof(return))this : null; }
inout(PrettyFuncInitExp) isPrettyFuncInitExp() { return op == EXP.prettyFunction ? cast(typeof(return))this : null; }
inout(ObjcClassReferenceExp) isObjcClassReferenceExp() { return op == EXP.objcClassReference ? cast(typeof(return))this : null; }
inout(ClassReferenceExp) isClassReferenceExp() { return op == EXP.classReference ? cast(typeof(return))this : null; }
inout(ThrownExceptionExp) isThrownExceptionExp() { return op == EXP.thrownException ? cast(typeof(return))this : null; }
inout(UnaExp) isUnaExp() pure inout nothrow @nogc
{
return exptab[op] & EXPFLAGS.unary ? cast(typeof(return))this : null;
}
inout(BinExp) isBinExp() pure inout nothrow @nogc
{
return exptab[op] & EXPFLAGS.binary ? cast(typeof(return))this : null;
}
inout(BinAssignExp) isBinAssignExp() pure inout nothrow @nogc
{
return exptab[op] & EXPFLAGS.binaryAssign ? cast(typeof(return))this : null;
}
}
override void accept(Visitor v)
{
v.visit(this);
}
}
// Approximate Non-semantic version of the `Type.isScalar` function in `typesem`
bool _isRoughlyScalar(Type _this)
{
if (auto tb = _this.isTypeBasic())
return (tb.flags & TFlags.integral | TFlags.floating) != 0;
else if (_this.ty == Tenum || _this.ty == Tpointer) // the enum is possibly scalar
return true;
return false;
}
/***********************************************************
* A compile-time known integer value
*/
extern (C++) final class IntegerExp : Expression
{
dinteger_t value;
extern (D) this(Loc loc, dinteger_t value, Type type)
{
super(loc, EXP.int64);
//printf("IntegerExp(value = %lld, type = '%s')\n", value, type ? type.toChars() : "");
assert(type);
/* Verify no path to the following assert failure.
* Weirdly, the isScalar() includes floats - see enumsem.enumMemberSemantic() for the
* base type. This is possibly a bug.
*/
assert(_isRoughlyScalar(type) || type.ty == Terror);
this.type = type;
this.value = normalize(type.toBaseTypeNonSemantic().ty, value);
}
extern (D) this(dinteger_t value)
{
super(Loc.initial, EXP.int64);
this.type = Type.tint32;
this.value = cast(int)value;
}
static IntegerExp create(Loc loc, dinteger_t value, Type type)
{
return new IntegerExp(loc, value, type);
}
override void accept(Visitor v)
{
v.visit(this);
}
dinteger_t getInteger()
{
return value;
}
extern (D) void setInteger(dinteger_t value)
{
this.value = normalize(type.toBaseTypeNonSemantic().ty, value);
}
extern (D) static dinteger_t normalize(TY ty, dinteger_t value)
{
/* 'Normalize' the value of the integer to be in range of the type
*/
dinteger_t result;
if (ty == Tpointer)
ty = Type.tsize_t.ty;
switch (ty)
{
case Tbool:
result = (value != 0);
break;
case Tint8:
result = cast(byte)value;
break;
case Tchar:
case Tuns8:
result = cast(ubyte)value;
break;
case Tint16:
result = cast(short)value;
break;
case Twchar:
case Tuns16:
result = cast(ushort)value;
break;
case Tint32:
result = cast(int)value;
break;
case Tdchar:
case Tuns32:
result = cast(uint)value;
break;
case Tint64:
result = cast(long)value;
break;
case Tuns64:
result = cast(ulong)value;
break;
default:
break;
}
return result;
}
override IntegerExp syntaxCopy()
{
return this;
}
/**
* Use this instead of creating new instances for commonly used literals
* such as 0 or 1.
*
* Parameters:
* v = The value of the expression
* Returns:
* A static instance of the expression, typed as `Tint32`.
*/
static IntegerExp literal(int v)()
{
__gshared IntegerExp theConstant;
if (!theConstant)
theConstant = new IntegerExp(v);
return theConstant;
}
/**
* Use this instead of creating new instances for commonly used bools.
*
* Parameters:
* b = The value of the expression
* Returns:
* A static instance of the expression, typed as `Type.tbool`.
*/
static IntegerExp createBool(bool b)
{
__gshared IntegerExp trueExp, falseExp;
if (!trueExp)
{
trueExp = new IntegerExp(Loc.initial, 1, Type.tbool);
falseExp = new IntegerExp(Loc.initial, 0, Type.tbool);
}
return b ? trueExp : falseExp;
}
}
/***********************************************************
* Use this expression for error recovery.
*
* It should behave as a 'sink' to prevent further cascaded error messages.
*/
extern (C++) final class ErrorExp : Expression
{
extern (D) this()
{
super(Loc.initial, EXP.error);
type = Type.terror;
}
static ErrorExp get ()
{
if (errorexp is null)
errorexp = new ErrorExp();
if (global.errors == 0 && global.gaggedErrors == 0)
{
/* Unfortunately, errors can still leak out of gagged errors,
* and we need to set the error count to prevent bogus code
* generation. At least give a message.
*/
.error(Loc.initial, "unknown, please file report at https://github.com/dlang/dmd/issues/new");
}
return errorexp;
}
override void accept(Visitor v)
{
v.visit(this);
}
extern (C++) __gshared ErrorExp errorexp; // handy shared value
}
/***********************************************************
* An uninitialized value,
* generated from void initializers.
*
* https://dlang.org/spec/declaration.html#void_init
*/
extern (C++) final class VoidInitExp : Expression
{
VarDeclaration var; /// the variable from where the void value came from, null if not known
/// Useful for error messages
extern (D) this(VarDeclaration var) @safe
{
super(var.loc, EXP.void_);
this.var = var;
this.type = var.type;
}
override void accept(Visitor v)
{
v.visit(this);
}
}
/***********************************************************
* A compile-time known floating point number
*/
extern (C++) final class RealExp : Expression
{
real_t value;
extern (D) this(Loc loc, real_t value, Type type) @safe
{
super(loc, EXP.float64);
//printf("RealExp::RealExp(%Lg)\n", value);
this.value = value;
this.type = type;
}
static RealExp create(Loc loc, real_t value, Type type) @safe
{
return new RealExp(loc, value, type);
}
override void accept(Visitor v)
{
v.visit(this);
}
}
/***********************************************************
* A compile-time complex number (deprecated)
*/
extern (C++) final class ComplexExp : Expression
{
complex_t value;
extern (D) this(Loc loc, complex_t value, Type type) @safe
{
super(loc, EXP.complex80);
this.value = value;
this.type = type;
//printf("ComplexExp::ComplexExp(%s)\n", toChars());
}
static ComplexExp create(Loc loc, complex_t value, Type type) @safe
{
return new ComplexExp(loc, value, type);
}
override void accept(Visitor v)
{
v.visit(this);
}
}
/***********************************************************
* An identifier in the context of an expression (as opposed to a declaration)
*
* ---
* int x; // VarDeclaration with Identifier
* x++; // PostExp with IdentifierExp
* ---
*/
extern (C++) class IdentifierExp : Expression
{
Identifier ident;
extern (D) this(Loc loc, Identifier ident) scope @safe
{
super(loc, EXP.identifier);
this.ident = ident;
}
static IdentifierExp create(Loc loc, Identifier ident) @safe
{
return new IdentifierExp(loc, ident);
}
override void accept(Visitor v)
{
v.visit(this);
}
}
/***********************************************************
* The dollar operator used when indexing or slicing an array. E.g `a[$]`, `a[1 .. $]` etc.
*
* https://dlang.org/spec/arrays.html#array-length
*/
extern (C++) final class DollarExp : IdentifierExp
{
extern (D) this(Loc loc)
{
super(loc, Id.dollar);
}
override void accept(Visitor v)
{
v.visit(this);
}
}
/***********************************************************
* Won't be generated by parser.
*/
extern (C++) final class DsymbolExp : Expression
{
Dsymbol s;
bool hasOverloads;
extern (D) this(Loc loc, Dsymbol s, bool hasOverloads = true) @safe
{
super(loc, EXP.dSymbol);
this.s = s;
this.hasOverloads = hasOverloads;
}
override void accept(Visitor v)
{
v.visit(this);
}
}
/***********************************************************
* https://dlang.org/spec/expression.html#this
*/
extern (C++) class ThisExp : Expression
{
VarDeclaration var;
extern (D) this(Loc loc) @safe
{
super(loc, EXP.this_);
//printf("ThisExp::ThisExp() loc = %d\n", loc.linnum);
}
this(Loc loc, const EXP tok) @safe
{
super(loc, tok);
//printf("ThisExp::ThisExp() loc = %d\n", loc.linnum);
}
override ThisExp syntaxCopy()
{
auto r = cast(ThisExp) super.syntaxCopy();
// require new semantic (possibly new `var` etc.)
r.type = null;
r.var = null;
return r;
}
override void accept(Visitor v)
{
v.visit(this);
}
}
/***********************************************************
* https://dlang.org/spec/expression.html#super
*/
extern (C++) final class SuperExp : ThisExp
{
extern (D) this(Loc loc) @safe
{
super(loc, EXP.super_);
}
override void accept(Visitor v)
{
v.visit(this);
}
}
/***********************************************************
* A compile-time known `null` value
*
* https://dlang.org/spec/expression.html#null
*/
extern (C++) final class NullExp : Expression
{
extern (D) this(Loc loc, Type type = null) scope @safe
{
super(loc, EXP.null_);
this.type = type;
}
override void accept(Visitor v)
{
v.visit(this);
}
}
/***********************************************************
* https://dlang.org/spec/expression.html#string_literals
*/
extern (C++) final class StringExp : Expression
{
char postfix = NoPostfix; // 'c', 'w', 'd'
OwnedBy ownedByCtfe = OwnedBy.code;
private union
{
char* string; // if sz == 1
wchar* wstring; // if sz == 2
dchar* dstring; // if sz == 4
ulong* lstring; // if sz == 8
} // (const if ownedByCtfe == OwnedBy.code)
size_t len; // number of code units
ubyte sz = 1; // 1: char, 2: wchar, 4: dchar
/**
* Whether the string literal's type is fixed
* Example:
* ---
* wstring x = "abc"; // OK, string literal is flexible
* wstring y = cast(string) "abc"; // Error: type was committed after cast
* ---
*/
bool committed;
/// If the string is parsed from a hex string literal
bool hexString = false;
/// If the string is from a collected C macro
bool cMacro = false;
enum char NoPostfix = 0;
extern (D) this(Loc loc, const(void)[] string) scope
{
super(loc, EXP.string_);
this.string = cast(char*)string.ptr; // note that this.string should be const
this.len = string.length;
this.sz = 1; // work around LDC bug #1286
}
extern (D) this(Loc loc, const(void)[] string, size_t len, ubyte sz, char postfix = NoPostfix, bool cMacro = false) scope
{
super(loc, EXP.string_);
this.string = cast(char*)string.ptr; // note that this.string should be const
this.len = len;
this.sz = sz;
this.postfix = postfix;
this.cMacro = cMacro;
}
static StringExp create(Loc loc, const(char)* s)
{
return new StringExp(loc, s.toDString());
}
static StringExp create(Loc loc, const(void)* string, size_t len)
{
return new StringExp(loc, string[0 .. len]);
}
/**********************************
* Return the number of code units the string would be if it were re-encoded
* as tynto.
* Params:
* tynto = code unit type of the target encoding
* s = set to error message on invalid string
* Returns:
* number of code units
*/
extern (D) size_t numberOfCodeUnits(int tynto, out .string s) const
{
int encSize;
switch (tynto)
{
case 0: return len;
case Tchar: encSize = 1; break;
case Twchar: encSize = 2; break;
case Tdchar: encSize = 4; break;
default:
assert(0);
}
if (sz == encSize)
return len;
size_t result = 0;
dchar c;
switch (sz)
{
case 1:
for (size_t u = 0; u < len;)
{
s = utf_decodeChar(string[0 .. len], u, c);
if (s)
return 0;
result += utf_codeLength(encSize, c);
}
break;
case 2:
for (size_t u = 0; u < len;)