-
-
Notifications
You must be signed in to change notification settings - Fork 267
/
Copy pathexpression.d
5365 lines (4747 loc) · 150 KB
/
expression.d
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-2025 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.aggregate;
import dmd.arraytypes;
import dmd.astenums;
import dmd.ast_node;
import dmd.dcast : implicitConvTo;
import dmd.dclass;
import dmd.declaration;
import dmd.dimport;
import dmd.dmodule;
import dmd.dstruct;
import dmd.dsymbol;
import dmd.dtemplate;
import dmd.errors;
import dmd.errorsink;
import dmd.func;
import dmd.globals;
import dmd.hdrgen;
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.common.outbuffer;
import dmd.root.optional;
import dmd.root.rmem;
import dmd.rootobject;
import dmd.root.string;
import dmd.root.utf;
import dmd.target;
import dmd.tokens;
import dmd.typesem;
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;
}
/****************************************
* Expand tuples in-place.
*
* Example:
* When there's a call `f(10, pair: AliasSeq!(20, 30), single: 40)`, the input is:
* `exps = [10, (20, 30), 40]`
* `names = [null, "pair", "single"]`
* The arrays will be modified to:
* `exps = [10, 20, 30, 40]`
* `names = [null, "pair", null, "single"]`
*
* Params:
* exps = array of Expressions
* names = optional array of names corresponding to Expressions
*/
void expandTuples(Expressions* exps, Identifiers* names = null)
{
//printf("expandTuples()\n");
if (exps is null)
return;
if (names)
{
if (exps.length != names.length)
{
printf("exps.length = %d, names.length = %d\n", cast(int) exps.length, cast(int) names.length);
printf("exps = %s, names = %s\n", exps.toChars(), names.toChars());
if (exps.length > 0)
printf("%s\n", (*exps)[0].loc.toChars());
assert(0);
}
}
// At `index`, a tuple of length `length` is expanded. Insert corresponding nulls in `names`.
void expandNames(size_t index, size_t length)
{
if (names)
{
if (length == 0)
{
names.remove(index);
return;
}
foreach (i; 1 .. length)
{
names.insert(index + i, cast(Identifier) null);
}
}
}
for (size_t i = 0; i < exps.length; i++)
{
Expression arg = (*exps)[i];
if (!arg)
continue;
// Look for tuple with 0 members
if (auto e = arg.isTypeExp())
{
if (auto tt = e.type.toBasetype().isTypeTuple())
{
if (!tt.arguments || tt.arguments.length == 0)
{
exps.remove(i);
expandNames(i, 0);
if (i == exps.length)
return;
}
else // Expand a TypeTuple
{
exps.remove(i);
auto texps = new Expressions(tt.arguments.length);
foreach (j, a; *tt.arguments)
(*texps)[j] = new TypeExp(e.loc, a.type);
exps.insert(i, texps);
expandNames(i, texps.length);
}
i--;
continue;
}
}
// Inline expand all the tuples
while (arg.op == EXP.tuple)
{
TupleExp te = cast(TupleExp)arg;
exps.remove(i); // remove arg
exps.insert(i, te.exps); // replace with tuple contents
expandNames(i, te.exps.length);
if (i == exps.length)
return; // empty tuple, no more arguments
(*exps)[i] = Expression.combine(te.e0, (*exps)[i]);
arg = (*exps)[i];
}
}
}
/****************************************
* 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);
}
/***************************************************
* Given an Expression, find the variable it really is.
*
* For example, `a[index]` is really `a`, and `s.f` is really `s`.
* Params:
* e = Expression to look at
* deref = number of dereferences encountered
* Returns:
* variable if there is one, null if not
*/
VarDeclaration expToVariable(Expression e, out int deref)
{
deref = 0;
while (1)
{
switch (e.op)
{
case EXP.variable:
return e.isVarExp().var.isVarDeclaration();
case EXP.dotVariable:
e = e.isDotVarExp().e1;
if (e.type.toBasetype().isTypeClass())
deref++;
continue;
case EXP.index:
{
e = e.isIndexExp().e1;
if (!e.type.toBasetype().isTypeSArray())
deref++;
continue;
}
case EXP.slice:
{
e = e.isSliceExp().e1;
if (!e.type.toBasetype().isTypeSArray())
deref++;
continue;
}
case EXP.super_:
return e.isSuperExp().var.isVarDeclaration();
case EXP.this_:
return e.isThisExp().var.isVarDeclaration();
// Temporaries for rvalues that need destruction
// are of form: (T s = rvalue, s). For these cases
// we can just return var declaration of `s`. However,
// this is intentionally not calling `Expression.extractLast`
// because at this point we cannot infer the var declaration
// of more complex generated comma expressions such as the
// one for the array append hook.
case EXP.comma:
{
if (auto ve = e.isCommaExp().e2.isVarExp())
return ve.var.isVarDeclaration();
return null;
}
default:
return null;
}
}
}
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
*/
// IN_LLVM: instantiated in gen/asm-x86.h (`Handled = createExpression(...)`)
extern (C++) /* IN_LLVM 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
}
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;
}
dinteger_t toInteger()
{
//printf("Expression %s\n", EXPtoString(op).ptr);
if (!type || !type.isTypeError())
error(loc, "integer constant expression expected instead of `%s`", toChars());
return 0;
}
uinteger_t toUInteger()
{
//printf("Expression %s\n", EXPtoString(op).ptr);
return cast(uinteger_t)toInteger();
}
real_t toReal()
{
error(loc, "floating point constant expression expected instead of `%s`", toChars());
return CTFloat.zero;
}
real_t toImaginary()
{
error(loc, "floating point constant expression expected instead of `%s`", toChars());
return CTFloat.zero;
}
complex_t toComplex()
{
error(loc, "floating point constant expression expected instead of `%s`", toChars());
return complex_t(CTFloat.zero);
}
StringExp toStringExp()
{
return null;
}
/***************************************
* Return !=0 if expression is an lvalue.
*/
bool isLvalue()
{
return false;
}
/****************************************
* Check that the expression has a valid type.
* If not, generates an error "... has no type".
* Returns:
* true if the expression is not valid.
* Note:
* When this function returns true, `checkValue()` should also return true.
*/
bool checkType()
{
return false;
}
/******************************
* Take address of expression.
*/
final Expression addressOf()
{
//printf("Expression::addressOf()\n");
debug
{
assert(op == EXP.error || isLvalue());
}
Expression e = new AddrExp(loc, this, type.pointerTo());
return e;
}
/******************************
* 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:
version (IN_LLVM)
{
import gen.dpragma : LDCPragma;
// We don't statically know anything about the address of a weak symbol
// if there is no offset. With an offset, we can at least say that it is
// non-zero.
SymOffExp soe = cast(SymOffExp) this;
if (soe.var.llvmInternal == LDCPragma.LLVMextern_weak && !soe.offset)
{
return 0;
}
}
return 2;
default:
return 0;
}
assert(0);
}
/******
* Identical, not just equal. I.e. NaNs with different bit patterns are not identical
*/
bool isIdentical(const Expression e) const
{
return equals(e);
}
/// Statically evaluate this expression to a `bool` if possible
/// Returns: an optional thath either contains the value or is empty
Optional!bool toBool()
{
return typeof(return)();
}
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);
}
}
/***********************************************************
* A compile-time known integer value
*/
extern (C++) final class IntegerExp : Expression
{
private 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);
if (!type.isScalar())
{
//printf("%s, loc = %d\n", toChars(), loc.linnum);
if (type.ty != Terror)
error(loc, "integral constant must be scalar type, not `%s`", type.toChars());
type = Type.terror;
}
this.type = type;
this.value = normalize(type.toBasetype().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 bool equals(const RootObject o) const
{
if (this == o)
return true;
if (auto ne = (cast(Expression)o).isIntegerExp())
{
if (type.toHeadMutable().equals(ne.type.toHeadMutable()) && value == ne.value)
{
return true;
}
}
return false;
}
override dinteger_t toInteger()
{
// normalize() is necessary until we fix all the paints of 'type'
return value = normalize(type.toBasetype().ty, value);
}
override real_t toReal()
{
// normalize() is necessary until we fix all the paints of 'type'
const ty = type.toBasetype().ty;
const val = normalize(ty, value);
value = val;
return (ty == Tuns64)
? real_t(cast(ulong)val)
: real_t(cast(long)val);
}
override real_t toImaginary()
{
return CTFloat.zero;
}
override complex_t toComplex()
{
return complex_t(toReal());
}
override Optional!bool toBool()
{
bool r = toInteger() != 0;
return typeof(return)(r);
}
override void accept(Visitor v)
{
v.visit(this);
}
dinteger_t getInteger()
{
return value;
}
extern (D) void setInteger(dinteger_t value)
{
this.value = normalize(type.toBasetype().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;
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;
case Tpointer:
if (target.ptrsize == 8)
goto case Tuns64;
if (target.ptrsize == 4)
goto case Tuns32;
if (target.ptrsize == 2)
goto case Tuns16;
assert(0);
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);
}