Debian Codegraph @

gcc/d/dmd/expression.d

raw
1 /**
2 * Defines the bulk of the classes which represent the AST at the expression level.
3 *
4 * Specification: ($LINK2 https://dlang.org/spec/expression.html, Expressions)
5 *
6 * Copyright: Copyright (C) 1999-2025 by The D Language Foundation, All Rights Reserved
7 * Authors: $(LINK2 https://www.digitalmars.com, Walter Bright)
8 * License: $(LINK2 https://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
9 * Source: $(LINK2 https://github.com/dlang/dmd/blob/master/compiler/src/dmd/expression.d, _expression.d)
10 * Documentation: https://dlang.org/phobos/dmd_expression.html
11 * Coverage: https://codecov.io/gh/dlang/dmd/src/master/compiler/src/dmd/expression.d
12 */
13
14 module dmd.expression;
15
16 import core.stdc.stdarg;
17 import core.stdc.stdio;
18 import core.stdc.string;
19
20 import dmd.aggregate;
21 import dmd.arraytypes;
22 import dmd.astenums;
23 import dmd.ast_node;
24 import dmd.dcast : implicitConvTo;
25 import dmd.dclass;
26 import dmd.declaration;
27 import dmd.dimport;
28 import dmd.dmodule;
29 import dmd.dstruct;
30 import dmd.dsymbol;
31 import dmd.dtemplate;
32 import dmd.errors;
33 import dmd.errorsink;
34 import dmd.func;
35 import dmd.globals;
36 import dmd.hdrgen;
37 import dmd.id;
38 import dmd.identifier;
39 import dmd.init;
40 import dmd.location;
41 import dmd.mtype;
42 import dmd.root.complex;
43 import dmd.root.ctfloat;
44 import dmd.common.outbuffer;
45 import dmd.root.optional;
46 import dmd.root.rmem;
47 import dmd.rootobject;
48 import dmd.root.string;
49 import dmd.root.utf;
50 import dmd.target;
51 import dmd.tokens;
52 import dmd.typesem;
53 import dmd.visitor;
54
55 enum LOGSEMANTIC = false;
56
57 /****************************************
58 * Find the last non-comma expression.
59 * Params:
60 * e = Expressions connected by commas
61 * Returns:
62 * right-most non-comma expression
63 */
64
65 inout(Expression) lastComma(inout Expression e)
66 {
67 Expression ex = cast()e;
68 while (ex.op == EXP.comma)
69 ex = (cast(CommaExp)ex).e2;
70 return cast(inout)ex;
71
72 }
73
74 /****************************************
75 * Expand tuples in-place.
76 *
77 * Example:
78 * When there's a call `f(10, pair: AliasSeq!(20, 30), single: 40)`, the input is:
79 * `exps = [10, (20, 30), 40]`
80 * `names = [null, "pair", "single"]`
81 * The arrays will be modified to:
82 * `exps = [10, 20, 30, 40]`
83 * `names = [null, "pair", null, "single"]`
84 *
85 * Params:
86 * exps = array of Expressions
87 * names = optional array of names corresponding to Expressions
88 */
89 void expandTuples(Expressions* exps, Identifiers* names = null)
90 {
91 //printf("expandTuples()\n");
92 if (exps is null)
93 return;
94
95 if (names)
96 {
97 if (exps.length != names.length)
98 {
99 printf("exps.length = %d, names.length = %d\n", cast(int) exps.length, cast(int) names.length);
100 printf("exps = %s, names = %s\n", exps.toChars(), names.toChars());
101 if (exps.length > 0)
102 printf("%s\n", (*exps)[0].loc.toChars());
103 assert(0);
104 }
105 }
106
107 // At `index`, a tuple of length `length` is expanded. Insert corresponding nulls in `names`.
108 void expandNames(size_t index, size_t length)
109 {
110 if (names)
111 {
112 if (length == 0)
113 {
114 names.remove(index);
115 return;
116 }
117 foreach (i; 1 .. length)
118 {
119 names.insert(index + i, cast(Identifier) null);
120 }
121 }
122 }
123
124 for (size_t i = 0; i < exps.length; i++)
125 {
126 Expression arg = (*exps)[i];
127 if (!arg)
128 continue;
129
130 // Look for tuple with 0 members
131 if (auto e = arg.isTypeExp())
132 {
133 if (auto tt = e.type.toBasetype().isTypeTuple())
134 {
135 if (!tt.arguments || tt.arguments.length == 0)
136 {
137 exps.remove(i);
138 expandNames(i, 0);
139 if (i == exps.length)
140 return;
141 }
142 else // Expand a TypeTuple
143 {
144 exps.remove(i);
145 auto texps = new Expressions(tt.arguments.length);
146 foreach (j, a; *tt.arguments)
147 (*texps)[j] = new TypeExp(e.loc, a.type);
148 exps.insert(i, texps);
149 expandNames(i, texps.length);
150 }
151 i--;
152 continue;
153 }
154 }
155
156 // Inline expand all the tuples
157 while (arg.op == EXP.tuple)
158 {
159 TupleExp te = cast(TupleExp)arg;
160 exps.remove(i); // remove arg
161 exps.insert(i, te.exps); // replace with tuple contents
162 expandNames(i, te.exps.length);
163 if (i == exps.length)
164 return; // empty tuple, no more arguments
165 (*exps)[i] = Expression.combine(te.e0, (*exps)[i]);
166 arg = (*exps)[i];
167 }
168 }
169 }
170
171 /****************************************
172 * If `s` is a function template, i.e. the only member of a template
173 * and that member is a function, return that template.
174 * Params:
175 * s = symbol that might be a function template
176 * Returns:
177 * template for that function, otherwise null
178 */
179 TemplateDeclaration getFuncTemplateDecl(Dsymbol s) @safe
180 {
181 FuncDeclaration f = s.isFuncDeclaration();
182 if (f && f.parent)
183 {
184 if (auto ti = f.parent.isTemplateInstance())
185 {
186 if (!ti.isTemplateMixin() && ti.tempdecl)
187 {
188 auto td = ti.tempdecl.isTemplateDeclaration();
189 if (td.onemember && td.ident == f.ident)
190 {
191 return td;
192 }
193 }
194 }
195 }
196 return null;
197 }
198
199 /************************ TypeDotIdExp ************************************/
200 /* Things like:
201 * int.size
202 * foo.size
203 * (foo).size
204 * cast(foo).size
205 */
206 DotIdExp typeDotIdExp(Loc loc, Type type, Identifier ident) @safe
207 {
208 return new DotIdExp(loc, new TypeExp(loc, type), ident);
209 }
210
211 /***************************************************
212 * Given an Expression, find the variable it really is.
213 *
214 * For example, `a[index]` is really `a`, and `s.f` is really `s`.
215 * Params:
216 * e = Expression to look at
217 * deref = number of dereferences encountered
218 * Returns:
219 * variable if there is one, null if not
220 */
221 VarDeclaration expToVariable(Expression e, out int deref)
222 {
223 deref = 0;
224 while (1)
225 {
226 switch (e.op)
227 {
228 case EXP.variable:
229 return e.isVarExp().var.isVarDeclaration();
230
231 case EXP.dotVariable:
232 e = e.isDotVarExp().e1;
233 if (e.type.toBasetype().isTypeClass())
234 deref++;
235
236 continue;
237
238 case EXP.index:
239 {
240 e = e.isIndexExp().e1;
241 if (!e.type.toBasetype().isTypeSArray())
242 deref++;
243
244 continue;
245 }
246
247 case EXP.slice:
248 {
249 e = e.isSliceExp().e1;
250 if (!e.type.toBasetype().isTypeSArray())
251 deref++;
252
253 continue;
254 }
255
256 case EXP.super_:
257 return e.isSuperExp().var.isVarDeclaration();
258 case EXP.this_:
259 return e.isThisExp().var.isVarDeclaration();
260
261 // Temporaries for rvalues that need destruction
262 // are of form: (T s = rvalue, s). For these cases
263 // we can just return var declaration of `s`. However,
264 // this is intentionally not calling `Expression.extractLast`
265 // because at this point we cannot infer the var declaration
266 // of more complex generated comma expressions such as the
267 // one for the array append hook.
268 case EXP.comma:
269 {
270 if (auto ve = e.isCommaExp().e2.isVarExp())
271 return ve.var.isVarDeclaration();
272
273 return null;
274 }
275 default:
276 return null;
277 }
278 }
279 }
280
281 enum OwnedBy : ubyte
282 {
283 code, // normal code expression in AST
284 ctfe, // value expression for CTFE
285 cache, // constant value cached for CTFE
286 }
287
288 enum WANTvalue = 0; // default
289 enum WANTexpand = 1; // expand const/immutable variables if possible
290
291 /***********************************************************
292 * https://dlang.org/spec/expression.html#expression
293 */
294 extern (C++) abstract class Expression : ASTNode
295 {
296 /// Usually, this starts out as `null` and gets set to the final expression type by
297 /// `expressionSemantic`. However, for some expressions (such as `TypeExp`,`RealExp`,
298 /// `VarExp`), the field can get set to an assigned type before running semantic.
299 /// See `expressionSemanticDone`
300 Type type;
301
302 Loc loc; // file location
303 const EXP op; // to minimize use of dynamic_cast
304
305 static struct BitFields
306 {
307 bool parens; // if this is a parenthesized expression
308 bool rvalue; // true if this is considered to be an rvalue, even if it is an lvalue
309 }
310 import dmd.common.bitfields;
311 mixin(generateBitFields!(BitFields, ubyte));
312
313 extern (D) this(Loc loc, EXP op) scope @safe
314 {
315 //printf("Expression::Expression(op = %d) this = %p\n", op, this);
316 this.loc = loc;
317 this.op = op;
318 }
319
320 /// Returns: class instance size of this expression (implemented manually because `extern(C++)`)
321 final size_t size() nothrow @nogc pure @safe const { return expSize[op]; }
322
323 static void _init()
324 {
325 CTFEExp.cantexp = new CTFEExp(EXP.cantExpression);
326 CTFEExp.voidexp = new CTFEExp(EXP.voidExpression);
327 CTFEExp.breakexp = new CTFEExp(EXP.break_);
328 CTFEExp.continueexp = new CTFEExp(EXP.continue_);
329 CTFEExp.gotoexp = new CTFEExp(EXP.goto_);
330 CTFEExp.showcontext = new CTFEExp(EXP.showCtfeContext);
331 }
332
333 /**
334 * Deinitializes the global state of the compiler.
335 *
336 * This can be used to restore the state set by `_init` to its original
337 * state.
338 */
339 static void deinitialize()
340 {
341 CTFEExp.cantexp = CTFEExp.cantexp.init;
342 CTFEExp.voidexp = CTFEExp.voidexp.init;
343 CTFEExp.breakexp = CTFEExp.breakexp.init;
344 CTFEExp.continueexp = CTFEExp.continueexp.init;
345 CTFEExp.gotoexp = CTFEExp.gotoexp.init;
346 CTFEExp.showcontext = CTFEExp.showcontext.init;
347 }
348
349 /*********************************
350 * Does *not* do a deep copy.
351 */
352 extern (D) final Expression copy()
353 {
354 Expression e;
355 if (!size)
356 {
357 debug
358 {
359 fprintf(stderr, "No expression copy for: %s\n", toChars());
360 printf("op = %d\n", op);
361 }
362 assert(0);
363 }
364
365 // memory never freed, so can use the faster bump-pointer-allocation
366 e = cast(Expression)allocmemory(size);
367 //printf("Expression::copy(op = %d) e = %p\n", op, e);
368 return cast(Expression)memcpy(cast(void*)e, cast(void*)this, size);
369 }
370
371 Expression syntaxCopy()
372 {
373 //printf("Expression::syntaxCopy()\n");
374 //print();
375 return copy();
376 }
377
378 // kludge for template.isExpression()
379 override final DYNCAST dyncast() const
380 {
381 return DYNCAST.expression;
382 }
383
384 final override const(char)* toChars() const
385 {
386 // FIXME: mangling (see runnable/mangle.d) relies on toChars outputting __lambdaXXX here
387 if (auto fe = isFuncExp())
388 return fe.fd.toChars();
389
390 return .toChars(this);
391 }
392
393 /**********************************
394 * Combine e1 and e2 by CommaExp if both are not NULL.
395 */
396 extern (D) static Expression combine(Expression e1, Expression e2) @safe
397 {
398 if (e1)
399 {
400 if (e2)
401 {
402 e1 = new CommaExp(e1.loc, e1, e2);
403 e1.type = e2.type;
404 }
405 }
406 else
407 e1 = e2;
408 return e1;
409 }
410
411 extern (D) static Expression combine(Expression e1, Expression e2, Expression e3) @safe
412 {
413 return combine(combine(e1, e2), e3);
414 }
415
416 extern (D) static Expression combine(Expression e1, Expression e2, Expression e3, Expression e4) @safe
417 {
418 return combine(combine(e1, e2), combine(e3, e4));
419 }
420
421 /**********************************
422 * If 'e' is a tree of commas, returns the rightmost expression
423 * by stripping off it from the tree. The remained part of the tree
424 * is returned via e0.
425 * Otherwise 'e' is directly returned and e0 is set to NULL.
426 */
427 extern (D) static Expression extractLast(Expression e, out Expression e0) @trusted
428 {
429 if (e.op != EXP.comma)
430 {
431 return e;
432 }
433
434 CommaExp ce = cast(CommaExp)e;
435 if (ce.e2.op != EXP.comma)
436 {
437 e0 = ce.e1;
438 return ce.e2;
439 }
440 else
441 {
442 e0 = e;
443
444 Expression* pce = &ce.e2;
445 while ((cast(CommaExp)(*pce)).e2.op == EXP.comma)
446 {
447 pce = &(cast(CommaExp)(*pce)).e2;
448 }
449 assert((*pce).op == EXP.comma);
450 ce = cast(CommaExp)(*pce);
451 *pce = ce.e1;
452
453 return ce.e2;
454 }
455 }
456
457 extern (D) static Expressions* arraySyntaxCopy(Expressions* exps)
458 {
459 Expressions* a = null;
460 if (exps)
461 {
462 a = new Expressions(exps.length);
463 foreach (i, e; *exps)
464 {
465 (*a)[i] = e ? e.syntaxCopy() : null;
466 }
467 }
468 return a;
469 }
470
471 dinteger_t toInteger()
472 {
473 //printf("Expression %s\n", EXPtoString(op).ptr);
474 if (!type || !type.isTypeError())
475 error(loc, "integer constant expression expected instead of `%s`", toChars());
476 return 0;
477 }
478
479 uinteger_t toUInteger()
480 {
481 //printf("Expression %s\n", EXPtoString(op).ptr);
482 return cast(uinteger_t)toInteger();
483 }
484
485 real_t toReal()
486 {
487 error(loc, "floating point constant expression expected instead of `%s`", toChars());
488 return CTFloat.zero;
489 }
490
491 real_t toImaginary()
492 {
493 error(loc, "floating point constant expression expected instead of `%s`", toChars());
494 return CTFloat.zero;
495 }
496
497 complex_t toComplex()
498 {
499 error(loc, "floating point constant expression expected instead of `%s`", toChars());
500 return complex_t(CTFloat.zero);
501 }
502
503 StringExp toStringExp()
504 {
505 return null;
506 }
507
508 /***************************************
509 * Return !=0 if expression is an lvalue.
510 */
511 bool isLvalue()
512 {
513 return false;
514 }
515
516 /****************************************
517 * Check that the expression has a valid type.
518 * If not, generates an error "... has no type".
519 * Returns:
520 * true if the expression is not valid.
521 * Note:
522 * When this function returns true, `checkValue()` should also return true.
523 */
524 bool checkType()
525 {
526 return false;
527 }
528
529 /******************************
530 * Take address of expression.
531 */
532 final Expression addressOf()
533 {
534 //printf("Expression::addressOf()\n");
535 debug
536 {
537 assert(op == EXP.error || isLvalue());
538 }
539 Expression e = new AddrExp(loc, this, type.pointerTo());
540 return e;
541 }
542
543 /******************************
544 * If this is a reference, dereference it.
545 */
546 final Expression deref()
547 {
548 //printf("Expression::deref()\n");
549 // type could be null if forward referencing an 'auto' variable
550 if (type)
551 if (auto tr = type.isTypeReference())
552 {
553 Expression e = new PtrExp(loc, this, tr.next);
554 return e;
555 }
556 return this;
557 }
558
559 final int isConst()
560 {
561 //printf("Expression::isConst(): %s\n", e.toChars());
562 switch (op)
563 {
564 case EXP.int64:
565 case EXP.float64:
566 case EXP.complex80:
567 return 1;
568 case EXP.null_:
569 return 0;
570 case EXP.symbolOffset:
571 return 2;
572 default:
573 return 0;
574 }
575 assert(0);
576 }
577
578 /******
579 * Identical, not just equal. I.e. NaNs with different bit patterns are not identical
580 */
581 bool isIdentical(const Expression e) const
582 {
583 return equals(e);
584 }
585
586
587 /// Statically evaluate this expression to a `bool` if possible
588 /// Returns: an optional thath either contains the value or is empty
589 Optional!bool toBool()
590 {
591 return typeof(return)();
592 }
593
594 bool hasCode()
595 {
596 return true;
597 }
598
599 final pure inout nothrow @nogc @trusted
600 {
601 inout(IntegerExp) isIntegerExp() { return op == EXP.int64 ? cast(typeof(return))this : null; }
602 inout(ErrorExp) isErrorExp() { return op == EXP.error ? cast(typeof(return))this : null; }
603 inout(VoidInitExp) isVoidInitExp() { return op == EXP.void_ ? cast(typeof(return))this : null; }
604 inout(RealExp) isRealExp() { return op == EXP.float64 ? cast(typeof(return))this : null; }
605 inout(ComplexExp) isComplexExp() { return op == EXP.complex80 ? cast(typeof(return))this : null; }
606 inout(IdentifierExp) isIdentifierExp() { return op == EXP.identifier ? cast(typeof(return))this : null; }
607 inout(DollarExp) isDollarExp() { return op == EXP.dollar ? cast(typeof(return))this : null; }
608 inout(DsymbolExp) isDsymbolExp() { return op == EXP.dSymbol ? cast(typeof(return))this : null; }
609 inout(ThisExp) isThisExp() { return op == EXP.this_ ? cast(typeof(return))this : null; }
610 inout(SuperExp) isSuperExp() { return op == EXP.super_ ? cast(typeof(return))this : null; }
611 inout(NullExp) isNullExp() { return op == EXP.null_ ? cast(typeof(return))this : null; }
612 inout(StringExp) isStringExp() { return op == EXP.string_ ? cast(typeof(return))this : null; }
613 inout(InterpExp) isInterpExp() { return op == EXP.interpolated ? cast(typeof(return))this : null; }
614 inout(TupleExp) isTupleExp() { return op == EXP.tuple ? cast(typeof(return))this : null; }
615 inout(ArrayLiteralExp) isArrayLiteralExp() { return op == EXP.arrayLiteral ? cast(typeof(return))this : null; }
616 inout(AssocArrayLiteralExp) isAssocArrayLiteralExp() { return op == EXP.assocArrayLiteral ? cast(typeof(return))this : null; }
617 inout(StructLiteralExp) isStructLiteralExp() { return op == EXP.structLiteral ? cast(typeof(return))this : null; }
618 inout(CompoundLiteralExp) isCompoundLiteralExp() { return op == EXP.compoundLiteral ? cast(typeof(return))this : null; }
619 inout(TypeExp) isTypeExp() { return op == EXP.type ? cast(typeof(return))this : null; }
620 inout(ScopeExp) isScopeExp() { return op == EXP.scope_ ? cast(typeof(return))this : null; }
621 inout(TemplateExp) isTemplateExp() { return op == EXP.template_ ? cast(typeof(return))this : null; }
622 inout(NewExp) isNewExp() { return op == EXP.new_ ? cast(typeof(return))this : null; }
623 inout(NewAnonClassExp) isNewAnonClassExp() { return op == EXP.newAnonymousClass ? cast(typeof(return))this : null; }
624 inout(SymOffExp) isSymOffExp() { return op == EXP.symbolOffset ? cast(typeof(return))this : null; }
625 inout(VarExp) isVarExp() { return op == EXP.variable ? cast(typeof(return))this : null; }
626 inout(OverExp) isOverExp() { return op == EXP.overloadSet ? cast(typeof(return))this : null; }
627 inout(FuncExp) isFuncExp() { return op == EXP.function_ ? cast(typeof(return))this : null; }
628 inout(DeclarationExp) isDeclarationExp() { return op == EXP.declaration ? cast(typeof(return))this : null; }
629 inout(TypeidExp) isTypeidExp() { return op == EXP.typeid_ ? cast(typeof(return))this : null; }
630 inout(TraitsExp) isTraitsExp() { return op == EXP.traits ? cast(typeof(return))this : null; }
631 inout(HaltExp) isHaltExp() { return op == EXP.halt ? cast(typeof(return))this : null; }
632 inout(IsExp) isIsExp() { return op == EXP.is_ ? cast(typeof(return))this : null; }
633 inout(MixinExp) isMixinExp() { return op == EXP.mixin_ ? cast(typeof(return))this : null; }
634 inout(ImportExp) isImportExp() { return op == EXP.import_ ? cast(typeof(return))this : null; }
635 inout(AssertExp) isAssertExp() { return op == EXP.assert_ ? cast(typeof(return))this : null; }
636 inout(ThrowExp) isThrowExp() { return op == EXP.throw_ ? cast(typeof(return))this : null; }
637 inout(DotIdExp) isDotIdExp() { return op == EXP.dotIdentifier ? cast(typeof(return))this : null; }
638 inout(DotTemplateExp) isDotTemplateExp() { return op == EXP.dotTemplateDeclaration ? cast(typeof(return))this : null; }
639 inout(DotVarExp) isDotVarExp() { return op == EXP.dotVariable ? cast(typeof(return))this : null; }
640 inout(DotTemplateInstanceExp) isDotTemplateInstanceExp() { return op == EXP.dotTemplateInstance ? cast(typeof(return))this : null; }
641 inout(DelegateExp) isDelegateExp() { return op == EXP.delegate_ ? cast(typeof(return))this : null; }
642 inout(DotTypeExp) isDotTypeExp() { return op == EXP.dotType ? cast(typeof(return))this : null; }
643 inout(CallExp) isCallExp() { return op == EXP.call ? cast(typeof(return))this : null; }
644 inout(AddrExp) isAddrExp() { return op == EXP.address ? cast(typeof(return))this : null; }
645 inout(PtrExp) isPtrExp() { return op == EXP.star ? cast(typeof(return))this : null; }
646 inout(NegExp) isNegExp() { return op == EXP.negate ? cast(typeof(return))this : null; }
647 inout(UAddExp) isUAddExp() { return op == EXP.uadd ? cast(typeof(return))this : null; }
648 inout(ComExp) isComExp() { return op == EXP.tilde ? cast(typeof(return))this : null; }
649 inout(NotExp) isNotExp() { return op == EXP.not ? cast(typeof(return))this : null; }
650 inout(DeleteExp) isDeleteExp() { return op == EXP.delete_ ? cast(typeof(return))this : null; }
651 inout(CastExp) isCastExp() { return op == EXP.cast_ ? cast(typeof(return))this : null; }
652 inout(VectorExp) isVectorExp() { return op == EXP.vector ? cast(typeof(return))this : null; }
653 inout(VectorArrayExp) isVectorArrayExp() { return op == EXP.vectorArray ? cast(typeof(return))this : null; }
654 inout(SliceExp) isSliceExp() { return op == EXP.slice ? cast(typeof(return))this : null; }
655 inout(ArrayLengthExp) isArrayLengthExp() { return op == EXP.arrayLength ? cast(typeof(return))this : null; }
656 inout(ArrayExp) isArrayExp() { return op == EXP.array ? cast(typeof(return))this : null; }
657 inout(DotExp) isDotExp() { return op == EXP.dot ? cast(typeof(return))this : null; }
658 inout(CommaExp) isCommaExp() { return op == EXP.comma ? cast(typeof(return))this : null; }
659 inout(IntervalExp) isIntervalExp() { return op == EXP.interval ? cast(typeof(return))this : null; }
660 inout(DelegatePtrExp) isDelegatePtrExp() { return op == EXP.delegatePointer ? cast(typeof(return))this : null; }
661 inout(DelegateFuncptrExp) isDelegateFuncptrExp() { return op == EXP.delegateFunctionPointer ? cast(typeof(return))this : null; }
662 inout(IndexExp) isIndexExp() { return op == EXP.index ? cast(typeof(return))this : null; }
663 inout(PostExp) isPostExp() { return (op == EXP.plusPlus || op == EXP.minusMinus) ? cast(typeof(return))this : null; }
664 inout(PreExp) isPreExp() { return (op == EXP.prePlusPlus || op == EXP.preMinusMinus) ? cast(typeof(return))this : null; }
665 inout(AssignExp) isAssignExp() { return op == EXP.assign ? cast(typeof(return))this : null; }
666 inout(LoweredAssignExp) isLoweredAssignExp() { return op == EXP.loweredAssignExp ? cast(typeof(return))this : null; }
667 inout(ConstructExp) isConstructExp() { return op == EXP.construct ? cast(typeof(return))this : null; }
668 inout(BlitExp) isBlitExp() { return op == EXP.blit ? cast(typeof(return))this : null; }
669 inout(AddAssignExp) isAddAssignExp() { return op == EXP.addAssign ? cast(typeof(return))this : null; }
670 inout(MinAssignExp) isMinAssignExp() { return op == EXP.minAssign ? cast(typeof(return))this : null; }
671 inout(MulAssignExp) isMulAssignExp() { return op == EXP.mulAssign ? cast(typeof(return))this : null; }
672
673 inout(DivAssignExp) isDivAssignExp() { return op == EXP.divAssign ? cast(typeof(return))this : null; }
674 inout(ModAssignExp) isModAssignExp() { return op == EXP.modAssign ? cast(typeof(return))this : null; }
675 inout(AndAssignExp) isAndAssignExp() { return op == EXP.andAssign ? cast(typeof(return))this : null; }
676 inout(OrAssignExp) isOrAssignExp() { return op == EXP.orAssign ? cast(typeof(return))this : null; }
677 inout(XorAssignExp) isXorAssignExp() { return op == EXP.xorAssign ? cast(typeof(return))this : null; }
678 inout(PowAssignExp) isPowAssignExp() { return op == EXP.powAssign ? cast(typeof(return))this : null; }
679
680 inout(ShlAssignExp) isShlAssignExp() { return op == EXP.leftShiftAssign ? cast(typeof(return))this : null; }
681 inout(ShrAssignExp) isShrAssignExp() { return op == EXP.rightShiftAssign ? cast(typeof(return))this : null; }
682 inout(UshrAssignExp) isUshrAssignExp() { return op == EXP.unsignedRightShiftAssign ? cast(typeof(return))this : null; }
683
684 inout(CatAssignExp) isCatAssignExp() { return op == EXP.concatenateAssign
685 ? cast(typeof(return))this
686 : null; }
687
688 inout(CatElemAssignExp) isCatElemAssignExp() { return op == EXP.concatenateElemAssign
689 ? cast(typeof(return))this
690 : null; }
691
692 inout(CatDcharAssignExp) isCatDcharAssignExp() { return op == EXP.concatenateDcharAssign
693 ? cast(typeof(return))this
694 : null; }
695
696 inout(AddExp) isAddExp() { return op == EXP.add ? cast(typeof(return))this : null; }
697 inout(MinExp) isMinExp() { return op == EXP.min ? cast(typeof(return))this : null; }
698 inout(CatExp) isCatExp() { return op == EXP.concatenate ? cast(typeof(return))this : null; }
699 inout(MulExp) isMulExp() { return op == EXP.mul ? cast(typeof(return))this : null; }
700 inout(DivExp) isDivExp() { return op == EXP.div ? cast(typeof(return))this : null; }
701 inout(ModExp) isModExp() { return op == EXP.mod ? cast(typeof(return))this : null; }
702 inout(PowExp) isPowExp() { return op == EXP.pow ? cast(typeof(return))this : null; }
703 inout(ShlExp) isShlExp() { return op == EXP.leftShift ? cast(typeof(return))this : null; }
704 inout(ShrExp) isShrExp() { return op == EXP.rightShift ? cast(typeof(return))this : null; }
705 inout(UshrExp) isUshrExp() { return op == EXP.unsignedRightShift ? cast(typeof(return))this : null; }
706 inout(AndExp) isAndExp() { return op == EXP.and ? cast(typeof(return))this : null; }
707 inout(OrExp) isOrExp() { return op == EXP.or ? cast(typeof(return))this : null; }
708 inout(XorExp) isXorExp() { return op == EXP.xor ? cast(typeof(return))this : null; }
709 inout(LogicalExp) isLogicalExp() { return (op == EXP.andAnd || op == EXP.orOr) ? cast(typeof(return))this : null; }
710 //inout(CmpExp) isCmpExp() { return op == EXP. ? cast(typeof(return))this : null; }
711 inout(InExp) isInExp() { return op == EXP.in_ ? cast(typeof(return))this : null; }
712 inout(RemoveExp) isRemoveExp() { return op == EXP.remove ? cast(typeof(return))this : null; }
713 inout(EqualExp) isEqualExp() { return (op == EXP.equal || op == EXP.notEqual) ? cast(typeof(return))this : null; }
714 inout(IdentityExp) isIdentityExp() { return (op == EXP.identity || op == EXP.notIdentity) ? cast(typeof(return))this : null; }
715 inout(CondExp) isCondExp() { return op == EXP.question ? cast(typeof(return))this : null; }
716 inout(GenericExp) isGenericExp() { return op == EXP._Generic ? cast(typeof(return))this : null; }
717 inout(DefaultInitExp) isDefaultInitExp() { return
718 (op == EXP.prettyFunction || op == EXP.functionString ||
719 op == EXP.line || op == EXP.moduleString ||
720 op == EXP.file || op == EXP.fileFullPath ) ? cast(typeof(return))this : null; }
721 inout(FileInitExp) isFileInitExp() { return (op == EXP.file || op == EXP.fileFullPath) ? cast(typeof(return))this : null; }
722 inout(LineInitExp) isLineInitExp() { return op == EXP.line ? cast(typeof(return))this : null; }
723 inout(ModuleInitExp) isModuleInitExp() { return op == EXP.moduleString ? cast(typeof(return))this : null; }
724 inout(FuncInitExp) isFuncInitExp() { return op == EXP.functionString ? cast(typeof(return))this : null; }
725 inout(PrettyFuncInitExp) isPrettyFuncInitExp() { return op == EXP.prettyFunction ? cast(typeof(return))this : null; }
726 inout(ObjcClassReferenceExp) isObjcClassReferenceExp() { return op == EXP.objcClassReference ? cast(typeof(return))this : null; }
727 inout(ClassReferenceExp) isClassReferenceExp() { return op == EXP.classReference ? cast(typeof(return))this : null; }
728 inout(ThrownExceptionExp) isThrownExceptionExp() { return op == EXP.thrownException ? cast(typeof(return))this : null; }
729
730 inout(UnaExp) isUnaExp() pure inout nothrow @nogc
731 {
732 return exptab[op] & EXPFLAGS.unary ? cast(typeof(return))this : null;
733 }
734
735 inout(BinExp) isBinExp() pure inout nothrow @nogc
736 {
737 return exptab[op] & EXPFLAGS.binary ? cast(typeof(return))this : null;
738 }
739
740 inout(BinAssignExp) isBinAssignExp() pure inout nothrow @nogc
741 {
742 return exptab[op] & EXPFLAGS.binaryAssign ? cast(typeof(return))this : null;
743 }
744 }
745
746 override void accept(Visitor v)
747 {
748 v.visit(this);
749 }
750 }
751
752 /***********************************************************
753 * A compile-time known integer value
754 */
755 extern (C++) final class IntegerExp : Expression
756 {
757 private dinteger_t value;
758
759 extern (D) this(Loc loc, dinteger_t value, Type type)
760 {
761 super(loc, EXP.int64);
762 //printf("IntegerExp(value = %lld, type = '%s')\n", value, type ? type.toChars() : "");
763 assert(type);
764 if (!type.isScalar())
765 {
766 //printf("%s, loc = %d\n", toChars(), loc.linnum);
767 if (type.ty != Terror)
768 error(loc, "integral constant must be scalar type, not `%s`", type.toChars());
769 type = Type.terror;
770 }
771 this.type = type;
772 this.value = normalize(type.toBasetype().ty, value);
773 }
774
775 extern (D) this(dinteger_t value)
776 {
777 super(Loc.initial, EXP.int64);
778 this.type = Type.tint32;
779 this.value = cast(int)value;
780 }
781
782 static IntegerExp create(Loc loc, dinteger_t value, Type type)
783 {
784 return new IntegerExp(loc, value, type);
785 }
786
787 override bool equals(const RootObject o) const
788 {
789 if (this == o)
790 return true;
791 if (auto ne = (cast(Expression)o).isIntegerExp())
792 {
793 if (type.toHeadMutable().equals(ne.type.toHeadMutable()) && value == ne.value)
794 {
795 return true;
796 }
797 }
798 return false;
799 }
800
801 override dinteger_t toInteger()
802 {
803 // normalize() is necessary until we fix all the paints of 'type'
804 return value = normalize(type.toBasetype().ty, value);
805 }
806
807 override real_t toReal()
808 {
809 // normalize() is necessary until we fix all the paints of 'type'
810 const ty = type.toBasetype().ty;
811 const val = normalize(ty, value);
812 value = val;
813 return (ty == Tuns64)
814 ? real_t(cast(ulong)val)
815 : real_t(cast(long)val);
816 }
817
818 override real_t toImaginary()
819 {
820 return CTFloat.zero;
821 }
822
823 override complex_t toComplex()
824 {
825 return complex_t(toReal());
826 }
827
828 override Optional!bool toBool()
829 {
830 bool r = toInteger() != 0;
831 return typeof(return)(r);
832 }
833
834 override void accept(Visitor v)
835 {
836 v.visit(this);
837 }
838
839 dinteger_t getInteger()
840 {
841 return value;
842 }
843
844 extern (D) void setInteger(dinteger_t value)
845 {
846 this.value = normalize(type.toBasetype().ty, value);
847 }
848
849 extern (D) static dinteger_t normalize(TY ty, dinteger_t value)
850 {
851 /* 'Normalize' the value of the integer to be in range of the type
852 */
853 dinteger_t result;
854 switch (ty)
855 {
856 case Tbool:
857 result = (value != 0);
858 break;
859
860 case Tint8:
861 result = cast(byte)value;
862 break;
863
864 case Tchar:
865 case Tuns8:
866 result = cast(ubyte)value;
867 break;
868
869 case Tint16:
870 result = cast(short)value;
871 break;
872
873 case Twchar:
874 case Tuns16:
875 result = cast(ushort)value;
876 break;
877
878 case Tint32:
879 result = cast(int)value;
880 break;
881
882 case Tdchar:
883 case Tuns32:
884 result = cast(uint)value;
885 break;
886
887 case Tint64:
888 result = cast(long)value;
889 break;
890
891 case Tuns64:
892 result = cast(ulong)value;
893 break;
894
895 case Tpointer:
896 if (target.ptrsize == 8)
897 goto case Tuns64;
898 if (target.ptrsize == 4)
899 goto case Tuns32;
900 if (target.ptrsize == 2)
901 goto case Tuns16;
902 assert(0);
903
904 default:
905 break;
906 }
907 return result;
908 }
909
910 override IntegerExp syntaxCopy()
911 {
912 return this;
913 }
914
915 /**
916 * Use this instead of creating new instances for commonly used literals
917 * such as 0 or 1.
918 *
919 * Parameters:
920 * v = The value of the expression
921 * Returns:
922 * A static instance of the expression, typed as `Tint32`.
923 */
924 static IntegerExp literal(int v)()
925 {
926 __gshared IntegerExp theConstant;
927 if (!theConstant)
928 theConstant = new IntegerExp(v);
929 return theConstant;
930 }
931
932 /**
933 * Use this instead of creating new instances for commonly used bools.
934 *
935 * Parameters:
936 * b = The value of the expression
937 * Returns:
938 * A static instance of the expression, typed as `Type.tbool`.
939 */
940 static IntegerExp createBool(bool b)
941 {
942 __gshared IntegerExp trueExp, falseExp;
943 if (!trueExp)
944 {
945 trueExp = new IntegerExp(Loc.initial, 1, Type.tbool);
946 falseExp = new IntegerExp(Loc.initial, 0, Type.tbool);
947 }
948 return b ? trueExp : falseExp;
949 }
950 }
951
952 /***********************************************************
953 * Use this expression for error recovery.
954 *
955 * It should behave as a 'sink' to prevent further cascaded error messages.
956 */
957 extern (C++) final class ErrorExp : Expression
958 {
959 extern (D) this()
960 {
961 super(Loc.initial, EXP.error);
962 type = Type.terror;
963 }
964
965 static ErrorExp get ()
966 {
967 if (errorexp is null)
968 errorexp = new ErrorExp();
969
970 if (global.errors == 0 && global.gaggedErrors == 0)
971 {
972 /* Unfortunately, errors can still leak out of gagged errors,
973 * and we need to set the error count to prevent bogus code
974 * generation. At least give a message.
975 */
976 .error(Loc.initial, "unknown, please file report at https://github.com/dlang/dmd/issues/new");
977 }
978
979 return errorexp;
980 }
981
982 override void accept(Visitor v)
983 {
984 v.visit(this);
985 }
986
987 extern (C++) __gshared ErrorExp errorexp; // handy shared value
988 }
989
990
991 /***********************************************************
992 * An uninitialized value,
993 * generated from void initializers.
994 *
995 * https://dlang.org/spec/declaration.html#void_init
996 */
997 extern (C++) final class VoidInitExp : Expression
998 {
999 VarDeclaration var; /// the variable from where the void value came from, null if not known
1000 /// Useful for error messages
1001
1002 extern (D) this(VarDeclaration var) @safe
1003 {
1004 super(var.loc, EXP.void_);
1005 this.var = var;
1006 this.type = var.type;
1007 }
1008
1009 override void accept(Visitor v)
1010 {
1011 v.visit(this);
1012 }
1013 }
1014
1015
1016 /***********************************************************
1017 * A compile-time known floating point number
1018 */
1019 extern (C++) final class RealExp : Expression
1020 {
1021 real_t value;
1022
1023 extern (D) this(Loc loc, real_t value, Type type) @safe
1024 {
1025 super(loc, EXP.float64);
1026 //printf("RealExp::RealExp(%Lg)\n", value);
1027 this.value = value;
1028 this.type = type;
1029 }
1030
1031 static RealExp create(Loc loc, real_t value, Type type) @safe
1032 {
1033 return new RealExp(loc, value, type);
1034 }
1035
1036 /********************************
1037 * Test to see if two reals are the same.
1038 * Regard NaN's as equivalent.
1039 * Regard +0 and -0 as different.
1040 * Params:
1041 * x1 = first operand
1042 * x2 = second operand
1043 * Returns:
1044 * true if x1 is x2
1045 * else false
1046 */
1047 private static bool RealIdentical(real_t x1, real_t x2) @safe
1048 {
1049 return (CTFloat.isNaN(x1) && CTFloat.isNaN(x2)) || CTFloat.isIdentical(x1, x2);
1050 }
1051 override bool equals(const RootObject o) const
1052 {
1053 if (this == o)
1054 return true;
1055 if (auto ne = (cast(Expression)o).isRealExp())
1056 {
1057 if (type.toHeadMutable().equals(ne.type.toHeadMutable()) && RealIdentical(value, ne.value))
1058 {
1059 return true;
1060 }
1061 }
1062 return false;
1063 }
1064
1065 override bool isIdentical(const Expression e) const
1066 {
1067 if (!equals(e))
1068 return false;
1069 return CTFloat.isIdentical(value, e.isRealExp().value);
1070 }
1071
1072 override dinteger_t toInteger()
1073 {
1074 return cast(sinteger_t)toReal();
1075 }
1076
1077 override uinteger_t toUInteger()
1078 {
1079 return cast(uinteger_t)toReal();
1080 }
1081
1082 override real_t toReal()
1083 {
1084 return type.isReal() ? value : CTFloat.zero;
1085 }
1086
1087 override real_t toImaginary()
1088 {
1089 return type.isReal() ? CTFloat.zero : value;
1090 }
1091
1092 override complex_t toComplex()
1093 {
1094 return complex_t(toReal(), toImaginary());
1095 }
1096
1097 override Optional!bool toBool()
1098 {
1099 return typeof(return)(!!value);
1100 }
1101
1102 override void accept(Visitor v)
1103 {
1104 v.visit(this);
1105 }
1106 }
1107
1108 /***********************************************************
1109 * A compile-time complex number (deprecated)
1110 */
1111 extern (C++) final class ComplexExp : Expression
1112 {
1113 complex_t value;
1114
1115 extern (D) this(Loc loc, complex_t value, Type type) @safe
1116 {
1117 super(loc, EXP.complex80);
1118 this.value = value;
1119 this.type = type;
1120 //printf("ComplexExp::ComplexExp(%s)\n", toChars());
1121 }
1122
1123 static ComplexExp create(Loc loc, complex_t value, Type type) @safe
1124 {
1125 return new ComplexExp(loc, value, type);
1126 }
1127
1128 override bool equals(const RootObject o) const
1129 {
1130 if (this == o)
1131 return true;
1132 if (auto ne = (cast(Expression)o).isComplexExp())
1133 {
1134 if (type.toHeadMutable().equals(ne.type.toHeadMutable()) &&
1135 RealExp.RealIdentical(creall(value), creall(ne.value)) &&
1136 RealExp.RealIdentical(cimagl(value), cimagl(ne.value)))
1137 {
1138 return true;
1139 }
1140 }
1141 return false;
1142 }
1143
1144 override bool isIdentical(const Expression e) const
1145 {
1146 if (!equals(e))
1147 return false;
1148 // equals() regards different NaN values as 'equals'
1149 auto c = e.isComplexExp();
1150 return CTFloat.isIdentical(creall(value), creall(c.value)) &&
1151 CTFloat.isIdentical(cimagl(value), cimagl(c.value));
1152 }
1153
1154 override dinteger_t toInteger()
1155 {
1156 return cast(sinteger_t)toReal();
1157 }
1158
1159 override uinteger_t toUInteger()
1160 {
1161 return cast(uinteger_t)toReal();
1162 }
1163
1164 override real_t toReal()
1165 {
1166 return creall(value);
1167 }
1168
1169 override real_t toImaginary()
1170 {
1171 return cimagl(value);
1172 }
1173
1174 override complex_t toComplex()
1175 {
1176 return value;
1177 }
1178
1179 override Optional!bool toBool()
1180 {
1181 return typeof(return)(!!value);
1182 }
1183
1184 override void accept(Visitor v)
1185 {
1186 v.visit(this);
1187 }
1188 }
1189
1190 /***********************************************************
1191 * An identifier in the context of an expression (as opposed to a declaration)
1192 *
1193 * ---
1194 * int x; // VarDeclaration with Identifier
1195 * x++; // PostExp with IdentifierExp
1196 * ---
1197 */
1198 extern (C++) class IdentifierExp : Expression
1199 {
1200 Identifier ident;
1201
1202 extern (D) this(Loc loc, Identifier ident) scope @safe
1203 {
1204 super(loc, EXP.identifier);
1205 this.ident = ident;
1206 }
1207
1208 static IdentifierExp create(Loc loc, Identifier ident) @safe
1209 {
1210 return new IdentifierExp(loc, ident);
1211 }
1212
1213 override final bool isLvalue()
1214 {
1215 return !this.rvalue;
1216 }
1217
1218 override void accept(Visitor v)
1219 {
1220 v.visit(this);
1221 }
1222 }
1223
1224 /***********************************************************
1225 * The dollar operator used when indexing or slicing an array. E.g `a[$]`, `a[1 .. $]` etc.
1226 *
1227 * https://dlang.org/spec/arrays.html#array-length
1228 */
1229 extern (C++) final class DollarExp : IdentifierExp
1230 {
1231 extern (D) this(Loc loc)
1232 {
1233 super(loc, Id.dollar);
1234 }
1235
1236 override void accept(Visitor v)
1237 {
1238 v.visit(this);
1239 }
1240 }
1241
1242 /***********************************************************
1243 * Won't be generated by parser.
1244 */
1245 extern (C++) final class DsymbolExp : Expression
1246 {
1247 Dsymbol s;
1248 bool hasOverloads;
1249
1250 extern (D) this(Loc loc, Dsymbol s, bool hasOverloads = true) @safe
1251 {
1252 super(loc, EXP.dSymbol);
1253 this.s = s;
1254 this.hasOverloads = hasOverloads;
1255 }
1256
1257 override bool isLvalue()
1258 {
1259 return !rvalue;
1260 }
1261
1262 override void accept(Visitor v)
1263 {
1264 v.visit(this);
1265 }
1266 }
1267
1268 /***********************************************************
1269 * https://dlang.org/spec/expression.html#this
1270 */
1271 extern (C++) class ThisExp : Expression
1272 {
1273 VarDeclaration var;
1274
1275 extern (D) this(Loc loc) @safe
1276 {
1277 super(loc, EXP.this_);
1278 //printf("ThisExp::ThisExp() loc = %d\n", loc.linnum);
1279 }
1280
1281 this(Loc loc, const EXP tok) @safe
1282 {
1283 super(loc, tok);
1284 //printf("ThisExp::ThisExp() loc = %d\n", loc.linnum);
1285 }
1286
1287 override ThisExp syntaxCopy()
1288 {
1289 auto r = cast(ThisExp) super.syntaxCopy();
1290 // require new semantic (possibly new `var` etc.)
1291 r.type = null;
1292 r.var = null;
1293 return r;
1294 }
1295
1296 override Optional!bool toBool()
1297 {
1298 // `this` is never null (what about structs?)
1299 return typeof(return)(true);
1300 }
1301
1302 override final bool isLvalue()
1303 {
1304 // Class `this` should be an rvalue; struct `this` should be an lvalue.
1305 return !rvalue && type.toBasetype().ty != Tclass;
1306 }
1307
1308 override void accept(Visitor v)
1309 {
1310 v.visit(this);
1311 }
1312 }
1313
1314 /***********************************************************
1315 * https://dlang.org/spec/expression.html#super
1316 */
1317 extern (C++) final class SuperExp : ThisExp
1318 {
1319 extern (D) this(Loc loc) @safe
1320 {
1321 super(loc, EXP.super_);
1322 }
1323
1324 override void accept(Visitor v)
1325 {
1326 v.visit(this);
1327 }
1328 }
1329
1330 /***********************************************************
1331 * A compile-time known `null` value
1332 *
1333 * https://dlang.org/spec/expression.html#null
1334 */
1335 extern (C++) final class NullExp : Expression
1336 {
1337 extern (D) this(Loc loc, Type type = null) scope @safe
1338 {
1339 super(loc, EXP.null_);
1340 this.type = type;
1341 }
1342
1343 override bool equals(const RootObject o) const
1344 {
1345 if (auto e = o.isExpression())
1346 {
1347 if (e.op == EXP.null_ && type.equals(e.type))
1348 {
1349 return true;
1350 }
1351 }
1352 return false;
1353 }
1354
1355 override Optional!bool toBool()
1356 {
1357 // null in any type is false
1358 return typeof(return)(false);
1359 }
1360
1361 override StringExp toStringExp()
1362 {
1363 if (this.type.implicitConvTo(Type.tstring))
1364 {
1365 auto se = new StringExp(loc, (cast(char*)mem.xcalloc(1, 1))[0 .. 0]);
1366 se.type = Type.tstring;
1367 return se;
1368 }
1369
1370 return null;
1371 }
1372
1373 override void accept(Visitor v)
1374 {
1375 v.visit(this);
1376 }
1377 }
1378
1379 /***********************************************************
1380 * https://dlang.org/spec/expression.html#string_literals
1381 */
1382 extern (C++) final class StringExp : Expression
1383 {
1384 char postfix = NoPostfix; // 'c', 'w', 'd'
1385 OwnedBy ownedByCtfe = OwnedBy.code;
1386 private union
1387 {
1388 char* string; // if sz == 1
1389 wchar* wstring; // if sz == 2
1390 dchar* dstring; // if sz == 4
1391 ulong* lstring; // if sz == 8
1392 } // (const if ownedByCtfe == OwnedBy.code)
1393 size_t len; // number of code units
1394 ubyte sz = 1; // 1: char, 2: wchar, 4: dchar
1395
1396 /**
1397 * Whether the string literal's type is fixed
1398 * Example:
1399 * ---
1400 * wstring x = "abc"; // OK, string literal is flexible
1401 * wstring y = cast(string) "abc"; // Error: type was committed after cast
1402 * ---
1403 */
1404 bool committed;
1405
1406 /// If the string is parsed from a hex string literal
1407 bool hexString = false;
1408
1409 enum char NoPostfix = 0;
1410
1411 extern (D) this(Loc loc, const(void)[] string) scope
1412 {
1413 super(loc, EXP.string_);
1414 this.string = cast(char*)string.ptr; // note that this.string should be const
1415 this.len = string.length;
1416 this.sz = 1; // work around LDC bug #1286
1417 }
1418
1419 extern (D) this(Loc loc, const(void)[] string, size_t len, ubyte sz, char postfix = NoPostfix) scope
1420 {
1421 super(loc, EXP.string_);
1422 this.string = cast(char*)string.ptr; // note that this.string should be const
1423 this.len = len;
1424 this.sz = sz;
1425 this.postfix = postfix;
1426 }
1427
1428 static StringExp create(Loc loc, const(char)* s)
1429 {
1430 return new StringExp(loc, s.toDString());
1431 }
1432
1433 static StringExp create(Loc loc, const(void)* string, size_t len)
1434 {
1435 return new StringExp(loc, string[0 .. len]);
1436 }
1437
1438 override bool equals(const RootObject o) const
1439 {
1440 //printf("StringExp::equals('%s') %s\n", o.toChars(), toChars());
1441 if (auto e = o.isExpression())
1442 {
1443 if (auto se = e.isStringExp())
1444 {
1445 return compare(se) == 0;
1446 }
1447 }
1448 return false;
1449 }
1450
1451 /**********************************
1452 * Return the number of code units the string would be if it were re-encoded
1453 * as tynto.
1454 * Params:
1455 * tynto = code unit type of the target encoding
1456 * Returns:
1457 * number of code units
1458 */
1459 size_t numberOfCodeUnits(int tynto = 0) const
1460 {
1461 int encSize;
1462 switch (tynto)
1463 {
1464 case 0: return len;
1465 case Tchar: encSize = 1; break;
1466 case Twchar: encSize = 2; break;
1467 case Tdchar: encSize = 4; break;
1468 default:
1469 assert(0);
1470 }
1471 if (sz == encSize)
1472 return len;
1473
1474 size_t result = 0;
1475 dchar c;
1476
1477 switch (sz)
1478 {
1479 case 1:
1480 for (size_t u = 0; u < len;)
1481 {
1482 if (const s = utf_decodeChar(string[0 .. len], u, c))
1483 {
1484 error(loc, "%.*s", cast(int)s.length, s.ptr);
1485 return 0;
1486 }
1487 result += utf_codeLength(encSize, c);
1488 }
1489 break;
1490
1491 case 2:
1492 for (size_t u = 0; u < len;)
1493 {
1494 if (const s = utf_decodeWchar(wstring[0 .. len], u, c))
1495 {
1496 error(loc, "%.*s", cast(int)s.length, s.ptr);
1497 return 0;
1498 }
1499 result += utf_codeLength(encSize, c);
1500 }
1501 break;
1502
1503 case 4:
1504 foreach (u; 0 .. len)
1505 {
1506 result += utf_codeLength(encSize, dstring[u]);
1507 }
1508 break;
1509
1510 default:
1511 assert(0);
1512 }
1513 return result;
1514 }
1515
1516 /**********************************************
1517 * Write the contents of the string to dest.
1518 * Use numberOfCodeUnits() to determine size of result.
1519 * Params:
1520 * dest = destination
1521 * tyto = encoding type of the result
1522 * zero = add terminating 0
1523 */
1524 void writeTo(void* dest, bool zero, int tyto = 0) const
1525 {
1526 int encSize;
1527 switch (tyto)
1528 {
1529 case 0: encSize = sz; break;
1530 case Tchar: encSize = 1; break;
1531 case Twchar: encSize = 2; break;
1532 case Tdchar: encSize = 4; break;
1533 default:
1534 assert(0);
1535 }
1536 if (sz == encSize)
1537 {
1538 memcpy(dest, string, len * sz);
1539 if (zero)
1540 memset(dest + len * sz, 0, sz);
1541 }
1542 else
1543 assert(0);
1544 }
1545
1546 /*********************************************
1547 * Get the code unit at index i
1548 * Params:
1549 * i = index
1550 * Returns:
1551 * code unit at index i
1552 */
1553 dchar getCodeUnit(size_t i) const pure
1554 {
1555 assert(this.sz <= dchar.sizeof);
1556 return cast(dchar) getIndex(i);
1557 }
1558
1559 /// Returns: integer at index `i`
1560 dinteger_t getIndex(size_t i) const pure
1561 {
1562 assert(i < len);
1563 final switch (sz)
1564 {
1565 case 1:
1566 return string[i];
1567 case 2:
1568 return wstring[i];
1569 case 4:
1570 return dstring[i];
1571 case 8:
1572 return lstring[i];
1573 }
1574 }
1575
1576 /*********************************************
1577 * Set the code unit at index i to c
1578 * Params:
1579 * i = index
1580 * c = code unit to set it to
1581 */
1582 extern (D) void setCodeUnit(size_t i, dchar c)
1583 {
1584 return setIndex(i, c);
1585 }
1586
1587 extern (D) void setIndex(size_t i, long c)
1588 {
1589 assert(i < len);
1590 final switch (sz)
1591 {
1592 case 1:
1593 string[i] = cast(char)c;
1594 break;
1595 case 2:
1596 wstring[i] = cast(wchar)c;
1597 break;
1598 case 4:
1599 dstring[i] = cast(dchar) c;
1600 break;
1601 case 8:
1602 lstring[i] = c;
1603 break;
1604 }
1605 }
1606
1607 override StringExp toStringExp()
1608 {
1609 return this;
1610 }
1611
1612
1613 /**
1614 * Compare two `StringExp` by length, then value
1615 *
1616 * The comparison is not the usual C-style comparison as seen with
1617 * `strcmp` or `memcmp`, but instead first compare based on the length.
1618 * This allows both faster lookup and sorting when comparing sparse data.
1619 *
1620 * This ordering scheme is relied on by the string-switching feature.
1621 * Code in Druntime's `core.internal.switch_` relies on this ordering
1622 * when doing a binary search among case statements.
1623 *
1624 * Both `StringExp` should be of the same encoding.
1625 *
1626 * Params:
1627 * se2 = String expression to compare `this` to
1628 *
1629 * Returns:
1630 * `0` when `this` is equal to se2, a value greater than `0` if
1631 * `this` should be considered greater than `se2`,
1632 * and a value less than `0` if `this` is lesser than `se2`.
1633 */
1634 int compare(const StringExp se2) const nothrow pure @nogc
1635 {
1636 //printf("StringExp::compare()\n");
1637 const len1 = len;
1638 const len2 = se2.len;
1639
1640 assert(this.sz == se2.sz, "Comparing string expressions of different sizes");
1641 //printf("sz = %d, len1 = %d, len2 = %d\n", sz, cast(int)len1, cast(int)len2);
1642 if (len1 == len2)
1643 {
1644 switch (sz)
1645 {
1646 case 1:
1647 return memcmp(string, se2.string, len1);
1648
1649 case 2:
1650 {
1651 wchar* s1 = cast(wchar*)string;
1652 wchar* s2 = cast(wchar*)se2.string;
1653 foreach (u; 0 .. len)
1654 {
1655 if (s1[u] != s2[u])
1656 return s1[u] - s2[u];
1657 }
1658 }
1659 break;
1660 case 4:
1661 {
1662 dchar* s1 = cast(dchar*)string;
1663 dchar* s2 = cast(dchar*)se2.string;
1664 foreach (u; 0 .. len)
1665 {
1666 if (s1[u] != s2[u])
1667 return s1[u] - s2[u];
1668 }
1669 }
1670 break;
1671 default:
1672 assert(0);
1673 }
1674 }
1675 return cast(int)(len1 - len2);
1676 }
1677
1678 override Optional!bool toBool()
1679 {
1680 // Keep the old behaviour for this refactoring
1681 // Should probably match language spec instead and check for length
1682 return typeof(return)(true);
1683 }
1684
1685 override bool isLvalue()
1686 {
1687 /* string literal is rvalue in default, but
1688 * conversion to reference of static array is only allowed.
1689 */
1690 return !rvalue && (type && type.toBasetype().ty == Tsarray);
1691 }
1692
1693 /********************************
1694 * Convert string contents to a 0 terminated string,
1695 * allocated by mem.xmalloc().
1696 */
1697 extern (D) const(char)[] toStringz() const
1698 {
1699 auto nbytes = len * sz;
1700 char* s = cast(char*)mem.xmalloc(nbytes + sz);
1701 writeTo(s, true);
1702 return s[0 .. nbytes];
1703 }
1704
1705 extern (D) const(char)[] peekString() const
1706 {
1707 assert(sz == 1);
1708 return this.string[0 .. len];
1709 }
1710
1711 extern (D) const(wchar)[] peekWstring() const
1712 {
1713 assert(sz == 2);
1714 return this.wstring[0 .. len];
1715 }
1716
1717 extern (D) const(dchar)[] peekDstring() const
1718 {
1719 assert(sz == 4);
1720 return this.dstring[0 .. len];
1721 }
1722
1723 /*******************
1724 * Get a slice of the data.
1725 */
1726 extern (D) const(ubyte)[] peekData() const
1727 {
1728 return cast(const(ubyte)[])this.string[0 .. len * sz];
1729 }
1730
1731 /*******************
1732 * Borrow a slice of the data, so the caller can modify
1733 * it in-place (!)
1734 */
1735 extern (D) ubyte[] borrowData()
1736 {
1737 return cast(ubyte[])this.string[0 .. len * sz];
1738 }
1739
1740 /***********************
1741 * Set new string data.
1742 * `this` becomes the new owner of the data.
1743 */
1744 extern (D) void setData(void* s, size_t len, ubyte sz)
1745 {
1746 this.string = cast(char*)s;
1747 this.len = len;
1748 this.sz = sz;
1749 }
1750
1751 override void accept(Visitor v)
1752 {
1753 v.visit(this);
1754 }
1755 }
1756
1757 extern (C++) final class InterpExp : Expression
1758 {
1759 char postfix = NoPostfix; // 'c', 'w', 'd'
1760 OwnedBy ownedByCtfe = OwnedBy.code;
1761 InterpolatedSet* interpolatedSet;
1762
1763 enum char NoPostfix = 0;
1764
1765 extern (D) this(Loc loc, InterpolatedSet* set, char postfix = NoPostfix) scope @safe
1766 {
1767 super(loc, EXP.interpolated);
1768 this.interpolatedSet = set;
1769 this.postfix = postfix;
1770 }
1771
1772 override void accept(Visitor v)
1773 {
1774 v.visit(this);
1775 }
1776 }
1777
1778
1779 /***********************************************************
1780 * A sequence of expressions
1781 *
1782 * ---
1783 * alias AliasSeq(T...) = T;
1784 * alias Tup = AliasSeq!(3, int, "abc");
1785 * ---
1786 */
1787 extern (C++) final class TupleExp : Expression
1788 {
1789 /* Tuple-field access may need to take out its side effect part.
1790 * For example:
1791 * foo().tupleof
1792 * is rewritten as:
1793 * (ref __tup = foo(); tuple(__tup.field0, __tup.field1, ...))
1794 * The declaration of temporary variable __tup will be stored in TupleExp.e0.
1795 */
1796 Expression e0;
1797
1798 Expressions* exps;
1799
1800 extern (D) this(Loc loc, Expression e0, Expressions* exps) @safe
1801 {
1802 super(loc, EXP.tuple);
1803 //printf("TupleExp(this = %p)\n", this);
1804 this.e0 = e0;
1805 this.exps = exps;
1806 }
1807
1808 extern (D) this(Loc loc, Expressions* exps) @safe
1809 {
1810 super(loc, EXP.tuple);
1811 //printf("TupleExp(this = %p)\n", this);
1812 this.exps = exps;
1813 }
1814
1815 extern (D) this(Loc loc, TupleDeclaration tup)
1816 {
1817 super(loc, EXP.tuple);
1818 this.exps = new Expressions();
1819
1820 this.exps.reserve(tup.objects.length);
1821 foreach (o; *tup.objects)
1822 {
1823 if (Dsymbol s = getDsymbol(o))
1824 {
1825 /* If tuple element represents a symbol, translate to DsymbolExp
1826 * to supply implicit 'this' if needed later.
1827 */
1828 Expression e = new DsymbolExp(loc, s);
1829 this.exps.push(e);
1830 }
1831 else if (auto eo = o.isExpression())
1832 {
1833 auto e = eo.copy();
1834 e.loc = loc; // https://issues.dlang.org/show_bug.cgi?id=15669
1835 this.exps.push(e);
1836 }
1837 else if (auto t = o.isType())
1838 {
1839 Expression e = new TypeExp(loc, t);
1840 this.exps.push(e);
1841 }
1842 else
1843 {
1844 error(loc, "`%s` is not an expression", o.toChars());
1845 }
1846 }
1847 }
1848
1849 static TupleExp create(Loc loc, Expressions* exps) @safe
1850 {
1851 return new TupleExp(loc, exps);
1852 }
1853
1854 override TupleExp syntaxCopy()
1855 {
1856 return new TupleExp(loc, e0 ? e0.syntaxCopy() : null, arraySyntaxCopy(exps));
1857 }
1858
1859 override bool equals(const RootObject o) const
1860 {
1861 if (this == o)
1862 return true;
1863 if (auto e = o.isExpression())
1864 if (auto te = e.isTupleExp())
1865 {
1866 if (exps.length != te.exps.length)
1867 return false;
1868 if (e0 && !e0.equals(te.e0) || !e0 && te.e0)
1869 return false;
1870 foreach (i, e1; *exps)
1871 {
1872 auto e2 = (*te.exps)[i];
1873 if (!e1.equals(e2))
1874 return false;
1875 }
1876 return true;
1877 }
1878 return false;
1879 }
1880
1881 override void accept(Visitor v)
1882 {
1883 v.visit(this);
1884 }
1885 }
1886
1887 /***********************************************************
1888 * [ e1, e2, e3, ... ]
1889 *
1890 * https://dlang.org/spec/expression.html#array_literals
1891 */
1892 extern (C++) final class ArrayLiteralExp : Expression
1893 {
1894 OwnedBy ownedByCtfe = OwnedBy.code;
1895 bool onstack = false;
1896
1897 /** If !is null, elements[] can be sparse and basis is used for the
1898 * "default" element value. In other words, non-null elements[i] overrides
1899 * this 'basis' value.
1900 */
1901 Expression basis;
1902
1903 Expressions* elements;
1904
1905 extern (D) this(Loc loc, Type type, Expressions* elements) @safe
1906 {
1907 super(loc, EXP.arrayLiteral);
1908 this.type = type;
1909 this.elements = elements;
1910 }
1911
1912 extern (D) this(Loc loc, Type type, Expression e)
1913 {
1914 super(loc, EXP.arrayLiteral);
1915 this.type = type;
1916 elements = new Expressions();
1917 elements.push(e);
1918 }
1919
1920 extern (D) this(Loc loc, Type type, Expression basis, Expressions* elements) @safe
1921 {
1922 super(loc, EXP.arrayLiteral);
1923 this.type = type;
1924 this.basis = basis;
1925 this.elements = elements;
1926 }
1927
1928 static ArrayLiteralExp create(Loc loc, Expressions* elements) @safe
1929 {
1930 return new ArrayLiteralExp(loc, null, elements);
1931 }
1932
1933 override ArrayLiteralExp syntaxCopy()
1934 {
1935 return new ArrayLiteralExp(loc,
1936 null,
1937 basis ? basis.syntaxCopy() : null,
1938 arraySyntaxCopy(elements));
1939 }
1940
1941 override bool equals(const RootObject o) const
1942 {
1943 if (this == o)
1944 return true;
1945 auto e = o.isExpression();
1946 if (!e)
1947 return false;
1948 if (auto ae = e.isArrayLiteralExp())
1949 {
1950 if (elements.length != ae.elements.length)
1951 return false;
1952 if (elements.length == 0 && !type.equals(ae.type))
1953 {
1954 return false;
1955 }
1956
1957 foreach (i, e1; *elements)
1958 {
1959 auto e2 = (*ae.elements)[i];
1960 auto e1x = e1 ? e1 : basis;
1961 auto e2x = e2 ? e2 : ae.basis;
1962
1963 if (e1x != e2x && (!e1x || !e2x || !e1x.equals(e2x)))
1964 return false;
1965 }
1966 return true;
1967 }
1968 return false;
1969 }
1970
1971 Expression getElement(size_t i) // use opIndex instead
1972 {
1973 return this[i];
1974 }
1975
1976 extern (D) Expression opIndex(size_t i)
1977 {
1978 auto el = (*elements)[i];
1979 return el ? el : basis;
1980 }
1981
1982 override Optional!bool toBool()
1983 {
1984 size_t dim = elements ? elements.length : 0;
1985 return typeof(return)(dim != 0);
1986 }
1987
1988 override StringExp toStringExp()
1989 {
1990 TY telem = type.nextOf().toBasetype().ty;
1991 if (telem.isSomeChar || (telem == Tvoid && (!elements || elements.length == 0)))
1992 {
1993 ubyte sz = 1;
1994 if (telem == Twchar)
1995 sz = 2;
1996 else if (telem == Tdchar)
1997 sz = 4;
1998
1999 OutBuffer buf;
2000 if (elements)
2001 {
2002 foreach (i; 0 .. elements.length)
2003 {
2004 auto ch = this[i];
2005 if (ch.op != EXP.int64)
2006 return null;
2007 if (sz == 1)
2008 buf.writeByte(cast(uint)ch.toInteger());
2009 else if (sz == 2)
2010 buf.writeword(cast(uint)ch.toInteger());
2011 else
2012 buf.write4(cast(uint)ch.toInteger());
2013 }
2014 }
2015 char prefix;
2016 if (sz == 1)
2017 {
2018 prefix = 'c';
2019 buf.writeByte(0);
2020 }
2021 else if (sz == 2)
2022 {
2023 prefix = 'w';
2024 buf.writeword(0);
2025 }
2026 else
2027 {
2028 prefix = 'd';
2029 buf.write4(0);
2030 }
2031
2032 const size_t len = buf.length / sz - 1;
2033 auto se = new StringExp(loc, buf.extractSlice()[0 .. len * sz], len, sz, prefix);
2034 se.sz = sz;
2035 se.type = type;
2036 return se;
2037 }
2038 return null;
2039 }
2040
2041 override void accept(Visitor v)
2042 {
2043 v.visit(this);
2044 }
2045 }
2046
2047 /***********************************************************
2048 * [ key0 : value0, key1 : value1, ... ]
2049 *
2050 * https://dlang.org/spec/expression.html#associative_array_literals
2051 */
2052 extern (C++) final class AssocArrayLiteralExp : Expression
2053 {
2054 OwnedBy ownedByCtfe = OwnedBy.code;
2055
2056 Expressions* keys;
2057 Expressions* values;
2058 /// Lower to core.internal.newaa for static initializaton
2059 Expression lowering;
2060
2061 extern (D) this(Loc loc, Expressions* keys, Expressions* values) @safe
2062 {
2063 super(loc, EXP.assocArrayLiteral);
2064 assert(keys.length == values.length);
2065 this.keys = keys;
2066 this.values = values;
2067 }
2068
2069 override bool equals(const RootObject o) const
2070 {
2071 if (this == o)
2072 return true;
2073 auto e = o.isExpression();
2074 if (!e)
2075 return false;
2076 if (auto ae = e.isAssocArrayLiteralExp())
2077 {
2078 if (keys.length != ae.keys.length)
2079 return false;
2080 size_t count = 0;
2081 foreach (i, key; *keys)
2082 {
2083 foreach (j, akey; *ae.keys)
2084 {
2085 if (key.equals(akey))
2086 {
2087 if (!(*values)[i].equals((*ae.values)[j]))
2088 return false;
2089 ++count;
2090 }
2091 }
2092 }
2093 return count == keys.length;
2094 }
2095 return false;
2096 }
2097
2098 override AssocArrayLiteralExp syntaxCopy()
2099 {
2100 return new AssocArrayLiteralExp(loc, arraySyntaxCopy(keys), arraySyntaxCopy(values));
2101 }
2102
2103 override Optional!bool toBool()
2104 {
2105 size_t dim = keys.length;
2106 return typeof(return)(dim != 0);
2107 }
2108
2109 override void accept(Visitor v)
2110 {
2111 v.visit(this);
2112 }
2113 }
2114
2115 /***********************************************************
2116 * sd( e1, e2, e3, ... )
2117 */
2118 extern (C++) final class StructLiteralExp : Expression
2119 {
2120 struct BitFields
2121 {
2122 bool useStaticInit; /// if this is true, use the StructDeclaration's init symbol
2123 bool isOriginal = false; /// used when moving instances to indicate `this is this.origin`
2124 OwnedBy ownedByCtfe = OwnedBy.code;
2125 }
2126 import dmd.common.bitfields;
2127 mixin(generateBitFields!(BitFields, ubyte));
2128 StageFlags stageflags;
2129
2130 StructDeclaration sd; /// which aggregate this is for
2131 Expressions* elements; /// parallels sd.fields[] with null entries for fields to skip
2132 Type stype; /// final type of result (can be different from sd's type)
2133
2134 // `inlineCopy` is only used temporarily in the `inline.d` pass,
2135 // while `sym` is only used in `e2ir/s2ir/tocsym` which comes after
2136 union
2137 {
2138 void* sym; /// back end symbol to initialize with literal (used as a Symbol*)
2139
2140 /// those fields need to prevent a infinite recursion when one field of struct initialized with 'this' pointer.
2141 StructLiteralExp inlinecopy;
2142 }
2143
2144 /** pointer to the origin instance of the expression.
2145 * once a new expression is created, origin is set to 'this'.
2146 * anytime when an expression copy is created, 'origin' pointer is set to
2147 * 'origin' pointer value of the original expression.
2148 */
2149 StructLiteralExp origin;
2150
2151
2152 /** anytime when recursive function is calling, 'stageflags' marks with bit flag of
2153 * current stage and unmarks before return from this function.
2154 * 'inlinecopy' uses similar 'stageflags' and from multiple evaluation 'doInline'
2155 * (with infinite recursion) of this expression.
2156 */
2157 enum StageFlags : ubyte
2158 {
2159 none = 0x0,
2160 scrub = 0x1, /// scrubReturnValue is running
2161 searchPointers = 0x2, /// hasNonConstPointers is running
2162 optimize = 0x4, /// optimize is running
2163 apply = 0x8, /// apply is running
2164 inlineScan = 0x10, /// inlineScan is running
2165 toCBuffer = 0x20 /// toCBuffer is running
2166 }
2167
2168 extern (D) this(Loc loc, StructDeclaration sd, Expressions* elements, Type stype = null) @safe
2169 {
2170 super(loc, EXP.structLiteral);
2171 this.sd = sd;
2172 if (!elements)
2173 elements = new Expressions();
2174 this.elements = elements;
2175 this.stype = stype;
2176 this.origin = this;
2177 //printf("StructLiteralExp::StructLiteralExp(%s)\n", toChars());
2178 }
2179
2180 static StructLiteralExp create(Loc loc, StructDeclaration sd, void* elements, Type stype = null)
2181 {
2182 return new StructLiteralExp(loc, sd, cast(Expressions*)elements, stype);
2183 }
2184
2185 override bool equals(const RootObject o) const
2186 {
2187 if (this == o)
2188 return true;
2189 auto e = o.isExpression();
2190 if (!e)
2191 return false;
2192 if (auto se = e.isStructLiteralExp())
2193 {
2194 if (!type.equals(se.type))
2195 return false;
2196 if (elements.length != se.elements.length)
2197 return false;
2198 foreach (i, e1; *elements)
2199 {
2200 auto e2 = (*se.elements)[i];
2201 if (e1 != e2 && (!e1 || !e2 || !e1.equals(e2)))
2202 return false;
2203 }
2204 return true;
2205 }
2206 return false;
2207 }
2208
2209 override StructLiteralExp syntaxCopy()
2210 {
2211 auto exp = new StructLiteralExp(loc, sd, arraySyntaxCopy(elements), type ? type : stype);
2212 exp.origin = this;
2213 return exp;
2214 }
2215
2216 /**************************************
2217 * Gets expression at offset of type.
2218 * Returns NULL if not found.
2219 */
2220 extern (D) Expression getField(Type type, uint offset)
2221 {
2222 //printf("StructLiteralExp::getField(this = %s, type = %s, offset = %u)\n",
2223 // /*toChars()*/"", type.toChars(), offset);
2224 Expression e = null;
2225 int i = getFieldIndex(type, offset);
2226
2227 if (i != -1)
2228 {
2229 //printf("\ti = %d\n", i);
2230 if (i >= sd.nonHiddenFields())
2231 return null;
2232
2233 assert(i < elements.length);
2234 e = (*elements)[i];
2235 if (e)
2236 {
2237 //printf("e = %s, e.type = %s\n", e.toChars(), e.type.toChars());
2238
2239 /* If type is a static array, and e is an initializer for that array,
2240 * then the field initializer should be an array literal of e.
2241 */
2242 auto tsa = type.isTypeSArray();
2243 if (tsa && e.type.castMod(0) != type.castMod(0))
2244 {
2245 const length = cast(size_t)tsa.dim.toInteger();
2246 auto z = new Expressions(length);
2247 foreach (ref q; *z)
2248 q = e.copy();
2249 e = new ArrayLiteralExp(loc, type, z);
2250 }
2251 else
2252 {
2253 e = e.copy();
2254 e.type = type;
2255 }
2256 if (useStaticInit && e.type.needsNested())
2257 if (auto se = e.isStructLiteralExp())
2258 {
2259 se.useStaticInit = true;
2260 }
2261 }
2262 }
2263 return e;
2264 }
2265
2266 /************************************
2267 * Get index of field.
2268 * Returns -1 if not found.
2269 */
2270 extern (D) int getFieldIndex(Type type, uint offset)
2271 {
2272 /* Find which field offset is by looking at the field offsets
2273 */
2274 if (elements.length)
2275 {
2276 const sz = type.size();
2277 if (sz == SIZE_INVALID)
2278 return -1;
2279 foreach (i, v; sd.fields)
2280 {
2281 if (offset == v.offset && sz == v.type.size())
2282 {
2283 /* context fields might not be filled. */
2284 if (i >= sd.nonHiddenFields())
2285 return cast(int)i;
2286 if (auto e = (*elements)[i])
2287 {
2288 return cast(int)i;
2289 }
2290 break;
2291 }
2292 }
2293 }
2294 return -1;
2295 }
2296
2297 override void accept(Visitor v)
2298 {
2299 v.visit(this);
2300 }
2301 }
2302
2303 /***********************************************************
2304 * C11 6.5.2.5
2305 * ( type-name ) { initializer-list }
2306 */
2307 extern (C++) final class CompoundLiteralExp : Expression
2308 {
2309 Initializer initializer; /// initializer-list
2310
2311 extern (D) this(Loc loc, Type type_name, Initializer initializer) @safe
2312 {
2313 super(loc, EXP.compoundLiteral);
2314 super.type = type_name;
2315 this.initializer = initializer;
2316 //printf("CompoundLiteralExp::CompoundLiteralExp(%s)\n", toChars());
2317 }
2318
2319 override void accept(Visitor v)
2320 {
2321 v.visit(this);
2322 }
2323 }
2324
2325 /***********************************************************
2326 * Mainly just a placeholder
2327 */
2328 extern (C++) final class TypeExp : Expression
2329 {
2330 extern (D) this(Loc loc, Type type) @safe
2331 {
2332 super(loc, EXP.type);
2333 //printf("TypeExp::TypeExp(%s)\n", type.toChars());
2334 this.type = type;
2335 }
2336
2337 override TypeExp syntaxCopy()
2338 {
2339 return new TypeExp(loc, type.syntaxCopy());
2340 }
2341
2342 override bool checkType()
2343 {
2344 error(loc, "type `%s` is not an expression", toChars());
2345 return true;
2346 }
2347
2348 override void accept(Visitor v)
2349 {
2350 v.visit(this);
2351 }
2352 }
2353
2354 /***********************************************************
2355 * Mainly just a placeholder of
2356 * Package, Module, Nspace, and TemplateInstance (including TemplateMixin)
2357 *
2358 * A template instance that requires IFTI:
2359 * foo!tiargs(fargs) // foo!tiargs
2360 * is left until CallExp::semantic() or resolveProperties()
2361 */
2362 extern (C++) final class ScopeExp : Expression
2363 {
2364 ScopeDsymbol sds;
2365
2366 extern (D) this(Loc loc, ScopeDsymbol sds) @safe
2367 {
2368 super(loc, EXP.scope_);
2369 //printf("ScopeExp::ScopeExp(sds = '%s')\n", sds.toChars());
2370 //static int count; if (++count == 38) *(char*)0=0;
2371 this.sds = sds;
2372 assert(!sds.isTemplateDeclaration()); // instead, you should use TemplateExp
2373 }
2374
2375 override ScopeExp syntaxCopy()
2376 {
2377 return new ScopeExp(loc, sds.syntaxCopy(null));
2378 }
2379
2380 override bool checkType()
2381 {
2382 if (sds.isPackage())
2383 {
2384 error(loc, "%s `%s` has no type", sds.kind(), sds.toChars());
2385 return true;
2386 }
2387 if (auto ti = sds.isTemplateInstance())
2388 {
2389 //assert(ti.needsTypeInference(sc));
2390 if (ti.tempdecl &&
2391 ti.semantictiargsdone &&
2392 ti.semanticRun == PASS.initial)
2393 {
2394 error(loc, "partial %s `%s` has no type", sds.kind(), toChars());
2395 return true;
2396 }
2397 }
2398 return false;
2399 }
2400
2401 override void accept(Visitor v)
2402 {
2403 v.visit(this);
2404 }
2405 }
2406
2407 /***********************************************************
2408 * Mainly just a placeholder
2409 */
2410 extern (C++) final class TemplateExp : Expression
2411 {
2412 TemplateDeclaration td;
2413 FuncDeclaration fd;
2414
2415 extern (D) this(Loc loc, TemplateDeclaration td, FuncDeclaration fd = null) @safe
2416 {
2417 super(loc, EXP.template_);
2418 //printf("TemplateExp(): %s\n", td.toChars());
2419 this.td = td;
2420 this.fd = fd;
2421 }
2422
2423 override bool isLvalue()
2424 {
2425 return fd !is null;
2426 }
2427
2428 override bool checkType()
2429 {
2430 error(loc, "%s `%s` has no type", td.kind(), toChars());
2431 return true;
2432 }
2433
2434 override void accept(Visitor v)
2435 {
2436 v.visit(this);
2437 }
2438 }
2439
2440 /***********************************************************
2441 * newtype(arguments)
2442 */
2443 extern (C++) final class NewExp : Expression
2444 {
2445 Expression thisexp; // if !=null, 'this' for class being allocated
2446 Type newtype;
2447 Expressions* arguments; // Array of Expression's
2448 Identifiers* names; // Array of names corresponding to expressions
2449 Expression placement; // if !=null, then PlacementExpression
2450
2451 Expression argprefix; // expression to be evaluated just before arguments[]
2452 CtorDeclaration member; // constructor function
2453 bool onstack; // allocate on stack
2454 bool thrownew; // this NewExp is the expression of a ThrowStatement
2455
2456 Expression lowering; // lowered druntime hook: `_d_new{class,itemT}`
2457
2458 /// Puts the `arguments` and `names` into an `ArgumentList` for easily passing them around.
2459 /// The fields are still separate for backwards compatibility
2460 extern (D) ArgumentList argumentList() { return ArgumentList(arguments, names); }
2461
2462 extern (D) this(Loc loc, Expression placement, Expression thisexp, Type newtype, Expressions* arguments, Identifiers* names = null) @safe
2463 {
2464 super(loc, EXP.new_);
2465 this.placement = placement;
2466 this.thisexp = thisexp;
2467 this.newtype = newtype;
2468 this.arguments = arguments;
2469 this.names = names;
2470 }
2471
2472 static NewExp create(Loc loc, Expression placement, Expression thisexp, Type newtype, Expressions* arguments) @safe
2473 {
2474 return new NewExp(loc, placement, thisexp, newtype, arguments);
2475 }
2476
2477 override NewExp syntaxCopy()
2478 {
2479 return new NewExp(loc,
2480 placement ? placement.syntaxCopy() : null,
2481 thisexp ? thisexp.syntaxCopy() : null,
2482 newtype.syntaxCopy(),
2483 arraySyntaxCopy(arguments),
2484 names ? names.copy() : null);
2485 }
2486
2487 override void accept(Visitor v)
2488 {
2489 v.visit(this);
2490 }
2491 }
2492
2493 /***********************************************************
2494 * class baseclasses { } (arguments)
2495 */
2496 extern (C++) final class NewAnonClassExp : Expression
2497 {
2498 Expression thisexp; // if !=null, 'this' for class being allocated
2499 ClassDeclaration cd; // class being instantiated
2500 Expressions* arguments; // Array of Expression's to call class constructor
2501 Expression placement; // if !=null, then PlacementExpression
2502
2503 extern (D) this(Loc loc, Expression placement, Expression thisexp, ClassDeclaration cd, Expressions* arguments) @safe
2504 {
2505 super(loc, EXP.newAnonymousClass);
2506 this.placement = placement;
2507 this.thisexp = thisexp;
2508 this.cd = cd;
2509 this.arguments = arguments;
2510 }
2511
2512 override NewAnonClassExp syntaxCopy()
2513 {
2514 return new NewAnonClassExp(loc, placement ? placement.syntaxCopy : null,
2515 thisexp ? thisexp.syntaxCopy() : null,
2516 cd.syntaxCopy(null), arraySyntaxCopy(arguments));
2517 }
2518
2519 override void accept(Visitor v)
2520 {
2521 v.visit(this);
2522 }
2523 }
2524
2525 /***********************************************************
2526 */
2527 extern (C++) class SymbolExp : Expression
2528 {
2529 Declaration var;
2530 Dsymbol originalScope; // original scope before inlining
2531 bool hasOverloads;
2532
2533 extern (D) this(Loc loc, EXP op, Declaration var, bool hasOverloads) @safe
2534 {
2535 super(loc, op);
2536 assert(var);
2537 this.var = var;
2538 this.hasOverloads = hasOverloads;
2539 }
2540
2541 override void accept(Visitor v)
2542 {
2543 v.visit(this);
2544 }
2545 }
2546
2547 /***********************************************************
2548 * Offset from symbol
2549 */
2550 extern (C++) final class SymOffExp : SymbolExp
2551 {
2552 dinteger_t offset;
2553
2554 extern (D) this(Loc loc, Declaration var, dinteger_t offset, bool hasOverloads = true)
2555 {
2556 if (auto v = var.isVarDeclaration())
2557 {
2558 // FIXME: This error report will never be handled anyone.
2559 // It should be done before the SymOffExp construction.
2560 if (v.needThis())
2561 {
2562 auto t = v.isThis();
2563 assert(t);
2564 .error(loc, "taking the address of non-static variable `%s` requires an instance of `%s`", v.toChars(), t.toChars());
2565 }
2566 hasOverloads = false;
2567 }
2568 super(loc, EXP.symbolOffset, var, hasOverloads);
2569 this.offset = offset;
2570 }
2571
2572 override Optional!bool toBool()
2573 {
2574 return typeof(return)(true);
2575 }
2576
2577 override void accept(Visitor v)
2578 {
2579 v.visit(this);
2580 }
2581 }
2582
2583 /***********************************************************
2584 * Variable
2585 */
2586 extern (C++) final class VarExp : SymbolExp
2587 {
2588 bool delegateWasExtracted;
2589 extern (D) this(Loc loc, Declaration var, bool hasOverloads = true) @safe
2590 {
2591 if (var.isVarDeclaration())
2592 hasOverloads = false;
2593
2594 super(loc, EXP.variable, var, hasOverloads);
2595 //printf("VarExp(this = %p, '%s', loc = %s)\n", this, var.toChars(), loc.toChars());
2596 //if (strcmp(var.ident.toChars(), "func") == 0) assert(0);
2597 this.type = var.type;
2598 }
2599
2600 static VarExp create(Loc loc, Declaration var, bool hasOverloads = true) @safe
2601 {
2602 return new VarExp(loc, var, hasOverloads);
2603 }
2604
2605 override bool equals(const RootObject o) const
2606 {
2607 if (this == o)
2608 return true;
2609 if (auto ne = o.isExpression().isVarExp())
2610 {
2611 if (type.toHeadMutable().equals(ne.type.toHeadMutable()) && var == ne.var)
2612 {
2613 return true;
2614 }
2615 }
2616 return false;
2617 }
2618
2619 override bool isLvalue()
2620 {
2621 if (rvalue || var.storage_class & (STC.lazy_ | STC.rvalue | STC.manifest))
2622 return false;
2623 return true;
2624 }
2625
2626 override void accept(Visitor v)
2627 {
2628 v.visit(this);
2629 }
2630 }
2631
2632 /***********************************************************
2633 * Overload Set
2634 */
2635 extern (C++) final class OverExp : Expression
2636 {
2637 OverloadSet vars;
2638
2639 extern (D) this(Loc loc, OverloadSet s)
2640 {
2641 super(loc, EXP.overloadSet);
2642 //printf("OverExp(this = %p, '%s')\n", this, var.toChars());
2643 vars = s;
2644 type = Type.tvoid;
2645 }
2646
2647 override bool isLvalue()
2648 {
2649 return true;
2650 }
2651
2652 override void accept(Visitor v)
2653 {
2654 v.visit(this);
2655 }
2656 }
2657
2658 /***********************************************************
2659 * Function/Delegate literal
2660 */
2661
2662 extern (C++) final class FuncExp : Expression
2663 {
2664 FuncLiteralDeclaration fd;
2665 TemplateDeclaration td;
2666 TOK tok; // TOK.reserved, TOK.delegate_, TOK.function_
2667
2668 extern (D) this(Loc loc, Dsymbol s)
2669 {
2670 super(loc, EXP.function_);
2671 this.td = s.isTemplateDeclaration();
2672 this.fd = s.isFuncLiteralDeclaration();
2673 if (td)
2674 {
2675 assert(td.literal);
2676 assert(td.members && td.members.length == 1);
2677 fd = (*td.members)[0].isFuncLiteralDeclaration();
2678 }
2679 tok = fd.tok; // save original kind of function/delegate/(infer)
2680 assert(fd.fbody);
2681 }
2682
2683 override bool equals(const RootObject o) const
2684 {
2685 if (this == o)
2686 return true;
2687 auto e = o.isExpression();
2688 if (!e)
2689 return false;
2690 if (auto fe = e.isFuncExp())
2691 {
2692 return fd == fe.fd;
2693 }
2694 return false;
2695 }
2696
2697 override FuncExp syntaxCopy()
2698 {
2699 if (td)
2700 return new FuncExp(loc, td.syntaxCopy(null));
2701 if (fd.semanticRun == PASS.initial)
2702 return new FuncExp(loc, fd.syntaxCopy(null));
2703 // https://issues.dlang.org/show_bug.cgi?id=13481
2704 // Prevent multiple semantic analysis of lambda body.
2705 return new FuncExp(loc, fd);
2706 }
2707
2708 override bool checkType()
2709 {
2710 if (td)
2711 {
2712 error(loc, "template lambda has no type");
2713 return true;
2714 }
2715 return false;
2716 }
2717
2718 override void accept(Visitor v)
2719 {
2720 v.visit(this);
2721 }
2722 }
2723
2724 /***********************************************************
2725 * Declaration of a symbol
2726 *
2727 * D grammar allows declarations only as statements. However in AST representation
2728 * it can be part of any expression. This is used, for example, during internal
2729 * syntax re-writes to inject hidden symbols.
2730 */
2731 extern (C++) final class DeclarationExp : Expression
2732 {
2733 Dsymbol declaration;
2734
2735 extern (D) this(Loc loc, Dsymbol declaration) @safe
2736 {
2737 super(loc, EXP.declaration);
2738 this.declaration = declaration;
2739 }
2740
2741 override DeclarationExp syntaxCopy()
2742 {
2743 return new DeclarationExp(loc, declaration.syntaxCopy(null));
2744 }
2745
2746 override bool hasCode()
2747 {
2748 if (auto vd = declaration.isVarDeclaration())
2749 {
2750 return !(vd.storage_class & (STC.manifest | STC.static_));
2751 }
2752 return false;
2753 }
2754
2755 override void accept(Visitor v)
2756 {
2757 v.visit(this);
2758 }
2759 }
2760
2761 /***********************************************************
2762 * typeid(int)
2763 */
2764 extern (C++) final class TypeidExp : Expression
2765 {
2766 RootObject obj;
2767
2768 extern (D) this(Loc loc, RootObject o) @safe
2769 {
2770 super(loc, EXP.typeid_);
2771 this.obj = o;
2772 }
2773
2774 override TypeidExp syntaxCopy()
2775 {
2776 return new TypeidExp(loc, objectSyntaxCopy(obj));
2777 }
2778
2779 override void accept(Visitor v)
2780 {
2781 v.visit(this);
2782 }
2783 }
2784
2785 /***********************************************************
2786 * __traits(identifier, args...)
2787 */
2788 extern (C++) final class TraitsExp : Expression
2789 {
2790 Identifier ident;
2791 Objects* args;
2792
2793 extern (D) this(Loc loc, Identifier ident, Objects* args) @safe
2794 {
2795 super(loc, EXP.traits);
2796 this.ident = ident;
2797 this.args = args;
2798 }
2799
2800 override TraitsExp syntaxCopy()
2801 {
2802 return new TraitsExp(loc, ident, TemplateInstance.arraySyntaxCopy(args));
2803 }
2804
2805 override void accept(Visitor v)
2806 {
2807 v.visit(this);
2808 }
2809 }
2810
2811 /***********************************************************
2812 * Generates a halt instruction
2813 *
2814 * `assert(0)` gets rewritten to this with `CHECKACTION.halt`
2815 */
2816 extern (C++) final class HaltExp : Expression
2817 {
2818 extern (D) this(Loc loc) @safe
2819 {
2820 super(loc, EXP.halt);
2821 }
2822
2823 override void accept(Visitor v)
2824 {
2825 v.visit(this);
2826 }
2827 }
2828
2829 /***********************************************************
2830 * is(targ id tok tspec)
2831 * is(targ id == tok2)
2832 */
2833 extern (C++) final class IsExp : Expression
2834 {
2835 Type targ;
2836 Identifier id; // can be null
2837 Type tspec; // can be null
2838 TemplateParameters* parameters;
2839 TOK tok; // ':' or '=='
2840 TOK tok2; // 'struct', 'union', etc.
2841
2842 extern (D) this(Loc loc, Type targ, Identifier id, TOK tok, Type tspec, TOK tok2, TemplateParameters* parameters) scope @safe
2843 {
2844 super(loc, EXP.is_);
2845 this.targ = targ;
2846 this.id = id;
2847 this.tok = tok;
2848 this.tspec = tspec;
2849 this.tok2 = tok2;
2850 this.parameters = parameters;
2851 }
2852
2853 override IsExp syntaxCopy()
2854 {
2855 // This section is identical to that in TemplateDeclaration::syntaxCopy()
2856 TemplateParameters* p = null;
2857 if (parameters)
2858 {
2859 p = new TemplateParameters(parameters.length);
2860 foreach (i, el; *parameters)
2861 (*p)[i] = el.syntaxCopy();
2862 }
2863 return new IsExp(loc, targ.syntaxCopy(), id, tok, tspec ? tspec.syntaxCopy() : null, tok2, p);
2864 }
2865
2866 override void accept(Visitor v)
2867 {
2868 v.visit(this);
2869 }
2870 }
2871
2872 /***********************************************************
2873 * Base class for unary operators
2874 *
2875 * https://dlang.org/spec/expression.html#unary-expression
2876 */
2877 extern (C++) abstract class UnaExp : Expression
2878 {
2879 Expression e1;
2880
2881 extern (D) this(Loc loc, EXP op, Expression e1) scope @safe
2882 {
2883 super(loc, op);
2884 this.e1 = e1;
2885 }
2886
2887 override UnaExp syntaxCopy()
2888 {
2889 UnaExp e = cast(UnaExp)copy();
2890 e.type = null;
2891 e.e1 = e.e1.syntaxCopy();
2892 return e;
2893 }
2894
2895 /*********************
2896 * Mark the operand as will never be dereferenced,
2897 * which is useful info for @safe checks.
2898 * Do before semantic() on operands rewrites them.
2899 */
2900 final void setNoderefOperand()
2901 {
2902 if (auto edi = e1.isDotIdExp())
2903 edi.noderef = true;
2904
2905 }
2906
2907 override void accept(Visitor v)
2908 {
2909 v.visit(this);
2910 }
2911 }
2912
2913 /***********************************************************
2914 * Base class for binary operators
2915 */
2916 extern (C++) abstract class BinExp : Expression
2917 {
2918 Expression e1;
2919 Expression e2;
2920
2921 extern (D) this(Loc loc, EXP op, Expression e1, Expression e2) scope @safe
2922 {
2923 super(loc, op);
2924 this.e1 = e1;
2925 this.e2 = e2;
2926 }
2927
2928 override BinExp syntaxCopy()
2929 {
2930 BinExp e = cast(BinExp)copy();
2931 e.type = null;
2932 e.e1 = e.e1.syntaxCopy();
2933 e.e2 = e.e2.syntaxCopy();
2934 return e;
2935 }
2936
2937 /*********************
2938 * Mark the operands as will never be dereferenced,
2939 * which is useful info for @safe checks.
2940 * Do before semantic() on operands rewrites them.
2941 */
2942 final void setNoderefOperands()
2943 {
2944 if (auto edi = e1.isDotIdExp())
2945 edi.noderef = true;
2946 if (auto edi = e2.isDotIdExp())
2947 edi.noderef = true;
2948
2949 }
2950
2951 override void accept(Visitor v)
2952 {
2953 v.visit(this);
2954 }
2955 }
2956
2957 /***********************************************************
2958 * Binary operator assignment, `+=` `-=` `*=` etc.
2959 */
2960 extern (C++) class BinAssignExp : BinExp
2961 {
2962 extern (D) this(Loc loc, EXP op, Expression e1, Expression e2) scope @safe
2963 {
2964 super(loc, op, e1, e2);
2965 }
2966
2967 override final bool isLvalue()
2968 {
2969 return !rvalue;
2970 }
2971
2972 override void accept(Visitor v)
2973 {
2974 v.visit(this);
2975 }
2976 }
2977
2978 /***********************************************************
2979 * A string mixin, `mixin("x")`
2980 *
2981 * https://dlang.org/spec/expression.html#mixin_expressions
2982 */
2983 extern (C++) final class MixinExp : Expression
2984 {
2985 Expressions* exps;
2986
2987 extern (D) this(Loc loc, Expressions* exps) @safe
2988 {
2989 super(loc, EXP.mixin_);
2990 this.exps = exps;
2991 }
2992
2993 override MixinExp syntaxCopy()
2994 {
2995 return new MixinExp(loc, arraySyntaxCopy(exps));
2996 }
2997
2998 override bool equals(const RootObject o) const
2999 {
3000 if (this == o)
3001 return true;
3002 auto e = o.isExpression();
3003 if (!e)
3004 return false;
3005 if (auto ce = e.isMixinExp())
3006 {
3007 if (exps.length != ce.exps.length)
3008 return false;
3009 foreach (i, e1; *exps)
3010 {
3011 auto e2 = (*ce.exps)[i];
3012 if (e1 != e2 && (!e1 || !e2 || !e1.equals(e2)))
3013 return false;
3014 }
3015 return true;
3016 }
3017 return false;
3018 }
3019
3020 override void accept(Visitor v)
3021 {
3022 v.visit(this);
3023 }
3024 }
3025
3026 /***********************************************************
3027 * An import expression, `import("file.txt")`
3028 *
3029 * Not to be confused with module imports, `import std.stdio`, which is an `ImportStatement`
3030 *
3031 * https://dlang.org/spec/expression.html#import_expressions
3032 */
3033 extern (C++) final class ImportExp : UnaExp
3034 {
3035 extern (D) this(Loc loc, Expression e) @safe
3036 {
3037 super(loc, EXP.import_, e);
3038 }
3039
3040 override void accept(Visitor v)
3041 {
3042 v.visit(this);
3043 }
3044 }
3045
3046 /***********************************************************
3047 * An assert expression, `assert(x == y)`
3048 *
3049 * https://dlang.org/spec/expression.html#assert_expressions
3050 */
3051 extern (C++) final class AssertExp : UnaExp
3052 {
3053 Expression msg;
3054
3055 extern (D) this(Loc loc, Expression e, Expression msg = null) @safe
3056 {
3057 super(loc, EXP.assert_, e);
3058 this.msg = msg;
3059 }
3060
3061 override AssertExp syntaxCopy()
3062 {
3063 return new AssertExp(loc, e1.syntaxCopy(), msg ? msg.syntaxCopy() : null);
3064 }
3065
3066 override void accept(Visitor v)
3067 {
3068 v.visit(this);
3069 }
3070 }
3071
3072 /***********************************************************
3073 * `throw <e1>` as proposed by DIP 1034.
3074 *
3075 * Replacement for the deprecated `ThrowStatement` that can be nested
3076 * in other expression.
3077 */
3078 extern (C++) final class ThrowExp : UnaExp
3079 {
3080 extern (D) this(Loc loc, Expression e)
3081 {
3082 super(loc, EXP.throw_, e);
3083 }
3084
3085 override ThrowExp syntaxCopy()
3086 {
3087 return new ThrowExp(loc, e1.syntaxCopy());
3088 }
3089
3090 override void accept(Visitor v)
3091 {
3092 v.visit(this);
3093 }
3094 }
3095
3096 /***********************************************************
3097 */
3098 extern (C++) final class DotIdExp : UnaExp
3099 {
3100 Identifier ident;
3101 bool noderef; // true if the result of the expression will never be dereferenced
3102 bool wantsym; // do not replace Symbol with its initializer during semantic()
3103 bool arrow; // ImportC: if -> instead of .
3104
3105 extern (D) this(Loc loc, Expression e, Identifier ident) @safe
3106 {
3107 super(loc, EXP.dotIdentifier, e);
3108 this.ident = ident;
3109 }
3110
3111 static DotIdExp create(Loc loc, Expression e, Identifier ident) @safe
3112 {
3113 return new DotIdExp(loc, e, ident);
3114 }
3115
3116 override void accept(Visitor v)
3117 {
3118 v.visit(this);
3119 }
3120 }
3121
3122 /***********************************************************
3123 * Mainly just a placeholder
3124 */
3125 extern (C++) final class DotTemplateExp : UnaExp
3126 {
3127 TemplateDeclaration td;
3128
3129 extern (D) this(Loc loc, Expression e, TemplateDeclaration td) @safe
3130 {
3131 super(loc, EXP.dotTemplateDeclaration, e);
3132 this.td = td;
3133 }
3134
3135 override bool checkType()
3136 {
3137 error(loc, "%s `%s` has no type", td.kind(), toChars());
3138 return true;
3139 }
3140
3141 override void accept(Visitor v)
3142 {
3143 v.visit(this);
3144 }
3145 }
3146
3147 /***********************************************************
3148 */
3149 extern (C++) final class DotVarExp : UnaExp
3150 {
3151 Declaration var;
3152 bool hasOverloads;
3153
3154 extern (D) this(Loc loc, Expression e, Declaration var, bool hasOverloads = true) @safe
3155 {
3156 if (var.isVarDeclaration())
3157 hasOverloads = false;
3158
3159 super(loc, EXP.dotVariable, e);
3160 //printf("DotVarExp()\n");
3161 this.var = var;
3162 this.hasOverloads = hasOverloads;
3163 }
3164
3165 override bool isLvalue()
3166 {
3167 if (rvalue)
3168 return false;
3169 if (e1.op != EXP.structLiteral)
3170 return true;
3171 auto vd = var.isVarDeclaration();
3172 return !(vd && vd.isField());
3173 }
3174
3175 override void accept(Visitor v)
3176 {
3177 v.visit(this);
3178 }
3179 }
3180
3181 /***********************************************************
3182 * foo.bar!(args)
3183 */
3184 extern (C++) final class DotTemplateInstanceExp : UnaExp
3185 {
3186 TemplateInstance ti;
3187
3188 extern (D) this(Loc loc, Expression e, Identifier name, Objects* tiargs)
3189 {
3190 super(loc, EXP.dotTemplateInstance, e);
3191 //printf("DotTemplateInstanceExp()\n");
3192 this.ti = new TemplateInstance(loc, name, tiargs);
3193 }
3194
3195 extern (D) this(Loc loc, Expression e, TemplateInstance ti) @safe
3196 {
3197 super(loc, EXP.dotTemplateInstance, e);
3198 this.ti = ti;
3199 }
3200
3201 override DotTemplateInstanceExp syntaxCopy()
3202 {
3203 return new DotTemplateInstanceExp(loc, e1.syntaxCopy(), ti.name, TemplateInstance.arraySyntaxCopy(ti.tiargs));
3204 }
3205
3206 override bool checkType()
3207 {
3208 // Same logic as ScopeExp.checkType()
3209 if (ti.tempdecl &&
3210 ti.semantictiargsdone &&
3211 ti.semanticRun == PASS.initial)
3212 {
3213 error(loc, "partial %s `%s` has no type", ti.kind(), toChars());
3214 return true;
3215 }
3216 return false;
3217 }
3218
3219 override void accept(Visitor v)
3220 {
3221 v.visit(this);
3222 }
3223 }
3224
3225 /***********************************************************
3226 */
3227 extern (C++) final class DelegateExp : UnaExp
3228 {
3229 FuncDeclaration func;
3230 bool hasOverloads;
3231 VarDeclaration vthis2; // container for multi-context
3232
3233 extern (D) this(Loc loc, Expression e, FuncDeclaration f, bool hasOverloads = true, VarDeclaration vthis2 = null) @safe
3234 {
3235 super(loc, EXP.delegate_, e);
3236 this.func = f;
3237 this.hasOverloads = hasOverloads;
3238 this.vthis2 = vthis2;
3239 }
3240
3241 override void accept(Visitor v)
3242 {
3243 v.visit(this);
3244 }
3245 }
3246
3247 /***********************************************************
3248 */
3249 extern (C++) final class DotTypeExp : UnaExp
3250 {
3251 Dsymbol sym; // symbol that represents a type
3252
3253 extern (D) this(Loc loc, Expression e, Dsymbol s) @safe
3254 {
3255 super(loc, EXP.dotType, e);
3256 this.sym = s;
3257 }
3258
3259 override void accept(Visitor v)
3260 {
3261 v.visit(this);
3262 }
3263 }
3264
3265 /**
3266 * The arguments of a function call
3267 *
3268 * Contains a list of expressions. If it is a named argument, the `names`
3269 * list has a non-null entry at the same index.
3270 */
3271 struct ArgumentList
3272 {
3273 Expressions* arguments; // function arguments
3274 Identifiers* names; // named argument identifiers
3275
3276 size_t length() const @nogc nothrow pure @safe { return arguments ? arguments.length : 0; }
3277
3278 /// Returns: whether this argument list contains any named arguments
3279 bool hasNames() const @nogc nothrow pure @safe
3280 {
3281 if (names is null)
3282 return false;
3283 foreach (name; *names)
3284 if (name !is null)
3285 return true;
3286
3287 return false;
3288 }
3289 }
3290
3291 /***********************************************************
3292 */
3293 extern (C++) final class CallExp : UnaExp
3294 {
3295 Expressions* arguments; // function arguments
3296 Identifiers* names; // named argument identifiers
3297 FuncDeclaration f; // symbol to call
3298 bool directcall; // true if a virtual call is devirtualized
3299 bool inDebugStatement; /// true if this was in a debug statement
3300 bool ignoreAttributes; /// don't enforce attributes (e.g. call @gc function in @nogc code)
3301 bool isUfcsRewrite; /// the first argument was pushed in here by a UFCS rewrite
3302 VarDeclaration vthis2; // container for multi-context
3303
3304 /// Puts the `arguments` and `names` into an `ArgumentList` for easily passing them around.
3305 /// The fields are still separate for backwards compatibility
3306 extern (D) ArgumentList argumentList() { return ArgumentList(arguments, names); }
3307
3308 extern (D) this(Loc loc, Expression e, Expressions* exps, Identifiers* names = null) @safe
3309 {
3310 super(loc, EXP.call, e);
3311 this.arguments = exps;
3312 this.names = names;
3313 }
3314
3315 extern (D) this(Loc loc, Expression e) @safe
3316 {
3317 super(loc, EXP.call, e);
3318 }
3319
3320 extern (D) this(Loc loc, Expression e, Expression earg1)
3321 {
3322 super(loc, EXP.call, e);
3323 this.arguments = new Expressions();
3324 if (earg1)
3325 this.arguments.push(earg1);
3326 }
3327
3328 extern (D) this(Loc loc, Expression e, Expression earg1, Expression earg2)
3329 {
3330 super(loc, EXP.call, e);
3331 auto arguments = new Expressions(2);
3332 (*arguments)[0] = earg1;
3333 (*arguments)[1] = earg2;
3334 this.arguments = arguments;
3335 }
3336
3337 /***********************************************************
3338 * Instatiates a new function call expression
3339 * Params:
3340 * loc = location
3341 * fd = the declaration of the function to call
3342 * earg1 = the function argument
3343 */
3344 extern(D) this(Loc loc, FuncDeclaration fd, Expression earg1)
3345 {
3346 this(loc, new VarExp(loc, fd, false), earg1);
3347 this.f = fd;
3348 }
3349
3350 static CallExp create(Loc loc, Expression e, Expressions* exps) @safe
3351 {
3352 return new CallExp(loc, e, exps);
3353 }
3354
3355 static CallExp create(Loc loc, Expression e) @safe
3356 {
3357 return new CallExp(loc, e);
3358 }
3359
3360 static CallExp create(Loc loc, Expression e, Expression earg1)
3361 {
3362 return new CallExp(loc, e, earg1);
3363 }
3364
3365 /***********************************************************
3366 * Creates a new function call expression
3367 * Params:
3368 * loc = location
3369 * fd = the declaration of the function to call
3370 * earg1 = the function argument
3371 */
3372 static CallExp create(Loc loc, FuncDeclaration fd, Expression earg1)
3373 {
3374 return new CallExp(loc, fd, earg1);
3375 }
3376
3377 override CallExp syntaxCopy()
3378 {
3379 return new CallExp(loc, e1.syntaxCopy(), arraySyntaxCopy(arguments), names ? names.copy() : null);
3380 }
3381
3382 override bool isLvalue()
3383 {
3384 if (rvalue)
3385 return false;
3386 Type tb = e1.type.toBasetype();
3387 if (tb.ty == Tdelegate || tb.ty == Tpointer)
3388 tb = tb.nextOf();
3389 auto tf = tb.isTypeFunction();
3390 if (tf && tf.isRef)
3391 {
3392 if (auto dve = e1.isDotVarExp())
3393 if (dve.var.isCtorDeclaration())
3394 return false;
3395 return true; // function returns a reference
3396 }
3397 return false;
3398 }
3399
3400 override void accept(Visitor v)
3401 {
3402 v.visit(this);
3403 }
3404 }
3405
3406 /**
3407 * Get the called function type from a call expression
3408 * Params:
3409 * ce = function call expression. Must have had semantic analysis done.
3410 * Returns: called function type, or `null` if error / no semantic analysis done
3411 */
3412 TypeFunction calledFunctionType(CallExp ce)
3413 {
3414 Type t = ce.e1.type;
3415 if (!t)
3416 return null;
3417 t = t.toBasetype();
3418 if (auto tf = t.isTypeFunction())
3419 return tf;
3420 if (auto td = t.isTypeDelegate())
3421 return td.nextOf().isTypeFunction();
3422 return null;
3423 }
3424
3425 FuncDeclaration isFuncAddress(Expression e, bool* hasOverloads = null) @safe
3426 {
3427 if (auto ae = e.isAddrExp())
3428 {
3429 auto ae1 = ae.e1;
3430 if (auto ve = ae1.isVarExp())
3431 {
3432 if (hasOverloads)
3433 *hasOverloads = ve.hasOverloads;
3434 return ve.var.isFuncDeclaration();
3435 }
3436 if (auto dve = ae1.isDotVarExp())
3437 {
3438 if (hasOverloads)
3439 *hasOverloads = dve.hasOverloads;
3440 return dve.var.isFuncDeclaration();
3441 }
3442 }
3443 else
3444 {
3445 if (auto soe = e.isSymOffExp())
3446 {
3447 if (hasOverloads)
3448 *hasOverloads = soe.hasOverloads;
3449 return soe.var.isFuncDeclaration();
3450 }
3451 if (auto dge = e.isDelegateExp())
3452 {
3453 if (hasOverloads)
3454 *hasOverloads = dge.hasOverloads;
3455 return dge.func.isFuncDeclaration();
3456 }
3457 }
3458 return null;
3459 }
3460
3461 /***********************************************************
3462 * The 'address of' operator, `&p`
3463 */
3464 extern (C++) final class AddrExp : UnaExp
3465 {
3466 extern (D) this(Loc loc, Expression e) @safe
3467 {
3468 super(loc, EXP.address, e);
3469 }
3470
3471 extern (D) this(Loc loc, Expression e, Type t) @safe
3472 {
3473 this(loc, e);
3474 type = t;
3475 }
3476
3477 override void accept(Visitor v)
3478 {
3479 v.visit(this);
3480 }
3481 }
3482
3483 /***********************************************************
3484 * The pointer dereference operator, `*p`
3485 */
3486 extern (C++) final class PtrExp : UnaExp
3487 {
3488 extern (D) this(Loc loc, Expression e) @safe
3489 {
3490 super(loc, EXP.star, e);
3491 //if (e.type)
3492 // type = ((TypePointer *)e.type).next;
3493 }
3494
3495 extern (D) this(Loc loc, Expression e, Type t) @safe
3496 {
3497 super(loc, EXP.star, e);
3498 type = t;
3499 }
3500
3501 override bool isLvalue()
3502 {
3503 return !rvalue;
3504 }
3505
3506 override void accept(Visitor v)
3507 {
3508 v.visit(this);
3509 }
3510 }
3511
3512 /***********************************************************
3513 * The negation operator, `-x`
3514 */
3515 extern (C++) final class NegExp : UnaExp
3516 {
3517 extern (D) this(Loc loc, Expression e) @safe
3518 {
3519 super(loc, EXP.negate, e);
3520 }
3521
3522 override void accept(Visitor v)
3523 {
3524 v.visit(this);
3525 }
3526 }
3527
3528 /***********************************************************
3529 * The unary add operator, `+x`
3530 */
3531 extern (C++) final class UAddExp : UnaExp
3532 {
3533 extern (D) this(Loc loc, Expression e) scope @safe
3534 {
3535 super(loc, EXP.uadd, e);
3536 }
3537
3538 override void accept(Visitor v)
3539 {
3540 v.visit(this);
3541 }
3542 }
3543
3544 /***********************************************************
3545 * The bitwise complement operator, `~x`
3546 */
3547 extern (C++) final class ComExp : UnaExp
3548 {
3549 extern (D) this(Loc loc, Expression e) @safe
3550 {
3551 super(loc, EXP.tilde, e);
3552 }
3553
3554 override void accept(Visitor v)
3555 {
3556 v.visit(this);
3557 }
3558 }
3559
3560 /***********************************************************
3561 * The logical not operator, `!x`
3562 */
3563 extern (C++) final class NotExp : UnaExp
3564 {
3565 extern (D) this(Loc loc, Expression e) @safe
3566 {
3567 super(loc, EXP.not, e);
3568 }
3569
3570 override void accept(Visitor v)
3571 {
3572 v.visit(this);
3573 }
3574 }
3575
3576 /***********************************************************
3577 * The delete operator, `delete x` (deprecated)
3578 *
3579 * https://dlang.org/spec/expression.html#delete_expressions
3580 */
3581 extern (C++) final class DeleteExp : UnaExp
3582 {
3583 bool isRAII; // true if called automatically as a result of scoped destruction
3584
3585 extern (D) this(Loc loc, Expression e, bool isRAII) @safe
3586 {
3587 super(loc, EXP.delete_, e);
3588 this.isRAII = isRAII;
3589 }
3590
3591 override void accept(Visitor v)
3592 {
3593 v.visit(this);
3594 }
3595 }
3596
3597 /***********************************************************
3598 * The type cast operator, `cast(T) x`
3599 *
3600 * It's possible to cast to one type while painting to another type
3601 *
3602 * https://dlang.org/spec/expression.html#cast_expressions
3603 */
3604 extern (C++) final class CastExp : UnaExp
3605 {
3606 Type to; // type to cast to
3607 ubyte mod = cast(ubyte)~0; // MODxxxxx
3608 bool trusted; // assume cast is safe
3609
3610 extern (D) this(Loc loc, Expression e, Type t) @safe
3611 {
3612 super(loc, EXP.cast_, e);
3613 this.to = t;
3614 }
3615
3616 /* For cast(const) and cast(immutable)
3617 */
3618 extern (D) this(Loc loc, Expression e, ubyte mod) @safe
3619 {
3620 super(loc, EXP.cast_, e);
3621 this.mod = mod;
3622 }
3623
3624 override CastExp syntaxCopy()
3625 {
3626 return to ? new CastExp(loc, e1.syntaxCopy(), to.syntaxCopy()) : new CastExp(loc, e1.syntaxCopy(), mod);
3627 }
3628
3629 override bool isLvalue()
3630 {
3631 //printf("e1.type = %s, to.type = %s\n", e1.type.toChars(), to.toChars());
3632 if (rvalue || !e1.isLvalue())
3633 return false;
3634 return (to.ty == Tsarray && (e1.type.ty == Tvector || e1.type.ty == Tsarray)) ||
3635 (to.ty == Taarray && e1.type.ty == Taarray) ||
3636 e1.type.mutableOf.unSharedOf().equals(to.mutableOf().unSharedOf());
3637 }
3638
3639 override void accept(Visitor v)
3640 {
3641 v.visit(this);
3642 }
3643 }
3644
3645 /***********************************************************
3646 */
3647 extern (C++) final class VectorExp : UnaExp
3648 {
3649 TypeVector to; // the target vector type before semantic()
3650 uint dim = ~0; // number of elements in the vector
3651 OwnedBy ownedByCtfe = OwnedBy.code;
3652
3653 extern (D) this(Loc loc, Expression e, Type t) @trusted
3654 {
3655 super(loc, EXP.vector, e);
3656 assert(t.ty == Tvector);
3657 to = cast(TypeVector)t;
3658 }
3659
3660 static VectorExp create(Loc loc, Expression e, Type t) @safe
3661 {
3662 return new VectorExp(loc, e, t);
3663 }
3664
3665 override VectorExp syntaxCopy()
3666 {
3667 return new VectorExp(loc, e1.syntaxCopy(), to.syntaxCopy());
3668 }
3669
3670 override void accept(Visitor v)
3671 {
3672 v.visit(this);
3673 }
3674 }
3675
3676 /***********************************************************
3677 * e1.array property for vectors.
3678 *
3679 * https://dlang.org/spec/simd.html#properties
3680 */
3681 extern (C++) final class VectorArrayExp : UnaExp
3682 {
3683 extern (D) this(Loc loc, Expression e1) @safe
3684 {
3685 super(loc, EXP.vectorArray, e1);
3686 }
3687
3688 override bool isLvalue()
3689 {
3690 return !rvalue && e1.isLvalue();
3691 }
3692
3693 override void accept(Visitor v)
3694 {
3695 v.visit(this);
3696 }
3697 }
3698
3699 /***********************************************************
3700 * e1 [lwr .. upr]
3701 *
3702 * https://dlang.org/spec/expression.html#slice_expressions
3703 */
3704 extern (C++) final class SliceExp : UnaExp
3705 {
3706 Expression upr; // null if implicit 0
3707 Expression lwr; // null if implicit [length - 1]
3708
3709 VarDeclaration lengthVar;
3710
3711 private extern(D) static struct BitFields
3712 {
3713 bool upperIsInBounds; // true if upr <= e1.length
3714 bool lowerIsLessThanUpper; // true if lwr <= upr
3715 bool arrayop; // an array operation, rather than a slice
3716 }
3717 import dmd.common.bitfields : generateBitFields;
3718 mixin(generateBitFields!(BitFields, ubyte));
3719
3720 /************************************************************/
3721 extern (D) this(Loc loc, Expression e1, IntervalExp ie) @safe
3722 {
3723 super(loc, EXP.slice, e1);
3724 this.upr = ie ? ie.upr : null;
3725 this.lwr = ie ? ie.lwr : null;
3726 }
3727
3728 extern (D) this(Loc loc, Expression e1, Expression lwr, Expression upr) @safe
3729 {
3730 super(loc, EXP.slice, e1);
3731 this.upr = upr;
3732 this.lwr = lwr;
3733 }
3734
3735 override SliceExp syntaxCopy()
3736 {
3737 auto se = new SliceExp(loc, e1.syntaxCopy(), lwr ? lwr.syntaxCopy() : null, upr ? upr.syntaxCopy() : null);
3738 se.lengthVar = this.lengthVar; // bug7871
3739 return se;
3740 }
3741
3742 override bool isLvalue()
3743 {
3744 /* slice expression is rvalue in default, but
3745 * conversion to reference of static array is only allowed.
3746 */
3747 return !rvalue && (type && type.toBasetype().ty == Tsarray);
3748 }
3749
3750 override Optional!bool toBool()
3751 {
3752 return e1.toBool();
3753 }
3754
3755 override void accept(Visitor v)
3756 {
3757 v.visit(this);
3758 }
3759 }
3760
3761 /***********************************************************
3762 * The `.length` property of an array
3763 */
3764 extern (C++) final class ArrayLengthExp : UnaExp
3765 {
3766 extern (D) this(Loc loc, Expression e1) @safe
3767 {
3768 super(loc, EXP.arrayLength, e1);
3769 }
3770
3771 override void accept(Visitor v)
3772 {
3773 v.visit(this);
3774 }
3775 }
3776
3777 /***********************************************************
3778 * e1 [ a0, a1, a2, a3 ,... ]
3779 *
3780 * https://dlang.org/spec/expression.html#index_expressions
3781 */
3782 extern (C++) final class ArrayExp : UnaExp
3783 {
3784 Expressions* arguments; // Array of Expression's a0..an
3785
3786 size_t currentDimension; // for opDollar
3787 VarDeclaration lengthVar;
3788
3789 extern (D) this(Loc loc, Expression e1, Expression index = null)
3790 {
3791 super(loc, EXP.array, e1);
3792 arguments = new Expressions();
3793 if (index)
3794 arguments.push(index);
3795 }
3796
3797 extern (D) this(Loc loc, Expression e1, Expressions* args) @safe
3798 {
3799 super(loc, EXP.array, e1);
3800 arguments = args;
3801 }
3802
3803 override ArrayExp syntaxCopy()
3804 {
3805 auto ae = new ArrayExp(loc, e1.syntaxCopy(), arraySyntaxCopy(arguments));
3806 ae.lengthVar = this.lengthVar; // bug7871
3807 return ae;
3808 }
3809
3810 override bool isLvalue()
3811 {
3812 if (rvalue)
3813 return false;
3814 if (type && type.toBasetype().ty == Tvoid)
3815 return false;
3816 return true;
3817 }
3818
3819 override void accept(Visitor v)
3820 {
3821 v.visit(this);
3822 }
3823 }
3824
3825 /***********************************************************
3826 */
3827 extern (C++) final class DotExp : BinExp
3828 {
3829 extern (D) this(Loc loc, Expression e1, Expression e2) @safe
3830 {
3831 super(loc, EXP.dot, e1, e2);
3832 }
3833
3834 override void accept(Visitor v)
3835 {
3836 v.visit(this);
3837 }
3838 }
3839
3840 /***********************************************************
3841 */
3842 extern (C++) final class CommaExp : BinExp
3843 {
3844 /// This is needed because AssignExp rewrites CommaExp, hence it needs
3845 /// to trigger the deprecation.
3846 const bool isGenerated;
3847
3848 /// Temporary variable to enable / disable deprecation of comma expression
3849 /// depending on the context.
3850 /// Since most constructor calls are rewritting, the only place where
3851 /// false will be passed will be from the parser.
3852 bool allowCommaExp;
3853
3854
3855 extern (D) this(Loc loc, Expression e1, Expression e2, bool generated = true) @safe
3856 {
3857 super(loc, EXP.comma, e1, e2);
3858 allowCommaExp = isGenerated = generated;
3859 }
3860
3861 override bool isLvalue()
3862 {
3863 return !rvalue && e2.isLvalue();
3864 }
3865
3866 override Optional!bool toBool()
3867 {
3868 return e2.toBool();
3869 }
3870
3871 override void accept(Visitor v)
3872 {
3873 v.visit(this);
3874 }
3875
3876 /**
3877 * If the argument is a CommaExp, set a flag to prevent deprecation messages
3878 *
3879 * It's impossible to know from CommaExp.semantic if the result will
3880 * be used, hence when there is a result (type != void), a deprecation
3881 * message is always emitted.
3882 * However, some construct can produce a result but won't use it
3883 * (ExpStatement and for loop increment). Those should call this function
3884 * to prevent unwanted deprecations to be emitted.
3885 *
3886 * Params:
3887 * exp = An expression that discards its result.
3888 * If the argument is null or not a CommaExp, nothing happens.
3889 */
3890 static void allow(Expression exp) @safe
3891 {
3892 if (exp)
3893 if (auto ce = exp.isCommaExp())
3894 ce.allowCommaExp = true;
3895 }
3896 }
3897
3898 /***********************************************************
3899 * Mainly just a placeholder
3900 */
3901 extern (C++) final class IntervalExp : Expression
3902 {
3903 Expression lwr;
3904 Expression upr;
3905
3906 extern (D) this(Loc loc, Expression lwr, Expression upr) @safe
3907 {
3908 super(loc, EXP.interval);
3909 this.lwr = lwr;
3910 this.upr = upr;
3911 }
3912
3913 override Expression syntaxCopy()
3914 {
3915 return new IntervalExp(loc, lwr.syntaxCopy(), upr.syntaxCopy());
3916 }
3917
3918 override void accept(Visitor v)
3919 {
3920 v.visit(this);
3921 }
3922 }
3923
3924 /***********************************************************
3925 * The `dg.ptr` property, pointing to the delegate's 'context'
3926 *
3927 * c.f.`DelegateFuncptrExp` for the delegate's function pointer `dg.funcptr`
3928 */
3929 extern (C++) final class DelegatePtrExp : UnaExp
3930 {
3931 extern (D) this(Loc loc, Expression e1) @safe
3932 {
3933 super(loc, EXP.delegatePointer, e1);
3934 }
3935
3936 override bool isLvalue()
3937 {
3938 return !rvalue && e1.isLvalue();
3939 }
3940
3941 override void accept(Visitor v)
3942 {
3943 v.visit(this);
3944 }
3945 }
3946
3947 /***********************************************************
3948 * The `dg.funcptr` property, pointing to the delegate's function
3949 *
3950 * c.f.`DelegatePtrExp` for the delegate's function pointer `dg.ptr`
3951 */
3952 extern (C++) final class DelegateFuncptrExp : UnaExp
3953 {
3954 extern (D) this(Loc loc, Expression e1) @safe
3955 {
3956 super(loc, EXP.delegateFunctionPointer, e1);
3957 }
3958
3959 override bool isLvalue()
3960 {
3961 return !rvalue && e1.isLvalue();
3962 }
3963
3964 override void accept(Visitor v)
3965 {
3966 v.visit(this);
3967 }
3968 }
3969
3970 /***********************************************************
3971 * e1 [ e2 ]
3972 */
3973 extern (C++) final class IndexExp : BinExp
3974 {
3975 VarDeclaration lengthVar;
3976 bool modifiable = false; // assume it is an rvalue
3977 bool indexIsInBounds; // true if 0 <= e2 && e2 <= e1.length - 1
3978
3979 extern (D) this(Loc loc, Expression e1, Expression e2) @safe
3980 {
3981 super(loc, EXP.index, e1, e2);
3982 //printf("IndexExp::IndexExp('%s')\n", toChars());
3983 }
3984
3985 extern (D) this(Loc loc, Expression e1, Expression e2, bool indexIsInBounds) @safe
3986 {
3987 super(loc, EXP.index, e1, e2);
3988 this.indexIsInBounds = indexIsInBounds;
3989 //printf("IndexExp::IndexExp('%s')\n", toChars());
3990 }
3991
3992 override IndexExp syntaxCopy()
3993 {
3994 auto ie = new IndexExp(loc, e1.syntaxCopy(), e2.syntaxCopy());
3995 ie.lengthVar = this.lengthVar; // bug7871
3996 return ie;
3997 }
3998
3999 override bool isLvalue()
4000 {
4001 if (rvalue)
4002 return false;
4003 auto t1b = e1.type.toBasetype();
4004 if (t1b.isTypeAArray() || t1b.isTypeSArray() ||
4005 (e1.isIndexExp() && t1b != t1b.isTypeDArray()))
4006 {
4007 return e1.isLvalue();
4008 }
4009 return true;
4010 }
4011
4012 extern (D) Expression markSettingAAElem()
4013 {
4014 if (e1.type.toBasetype().ty == Taarray)
4015 {
4016 Type t2b = e2.type.toBasetype();
4017 if (t2b.ty == Tarray && t2b.nextOf().isMutable())
4018 {
4019 error(loc, "associative arrays can only be assigned values with immutable keys, not `%s`", e2.type.toChars());
4020 return ErrorExp.get();
4021 }
4022 modifiable = true;
4023
4024 if (auto ie = e1.isIndexExp())
4025 {
4026 Expression ex = ie.markSettingAAElem();
4027 if (ex.op == EXP.error)
4028 return ex;
4029 assert(ex == e1);
4030 }
4031 }
4032 return this;
4033 }
4034
4035 override void accept(Visitor v)
4036 {
4037 v.visit(this);
4038 }
4039 }
4040
4041 /***********************************************************
4042 * The postfix increment/decrement operator, `i++` / `i--`
4043 */
4044 extern (C++) final class PostExp : BinExp
4045 {
4046 extern (D) this(EXP op, Loc loc, Expression e)
4047 {
4048 super(loc, op, e, IntegerExp.literal!1);
4049 assert(op == EXP.minusMinus || op == EXP.plusPlus);
4050 }
4051
4052 override void accept(Visitor v)
4053 {
4054 v.visit(this);
4055 }
4056 }
4057
4058 /***********************************************************
4059 * The prefix increment/decrement operator, `++i` / `--i`
4060 */
4061 extern (C++) final class PreExp : UnaExp
4062 {
4063 extern (D) this(EXP op, Loc loc, Expression e) @safe
4064 {
4065 super(loc, op, e);
4066 assert(op == EXP.preMinusMinus || op == EXP.prePlusPlus);
4067 }
4068
4069 override void accept(Visitor v)
4070 {
4071 v.visit(this);
4072 }
4073 }
4074
4075 enum MemorySet
4076 {
4077 none = 0, // simple assignment
4078 blockAssign = 1, // setting the contents of an array
4079 referenceInit = 2, // setting the reference of STC.ref_ variable
4080 }
4081
4082 /***********************************************************
4083 * The assignment / initialization operator, `=`
4084 *
4085 * Note: operator assignment `op=` has a different base class, `BinAssignExp`
4086 */
4087 extern (C++) class AssignExp : BinExp
4088 {
4089 MemorySet memset;
4090
4091 /************************************************************/
4092 /* op can be EXP.assign, EXP.construct, or EXP.blit */
4093 extern (D) this(Loc loc, Expression e1, Expression e2) @safe
4094 {
4095 super(loc, EXP.assign, e1, e2);
4096 }
4097
4098 this(Loc loc, EXP tok, Expression e1, Expression e2) @safe
4099 {
4100 super(loc, tok, e1, e2);
4101 }
4102
4103 override final bool isLvalue()
4104 {
4105 // Array-op 'x[] = y[]' should make an rvalue.
4106 // Setting array length 'x.length = v' should make an rvalue.
4107 if (e1.op == EXP.slice || e1.op == EXP.arrayLength)
4108 {
4109 return false;
4110 }
4111 return !rvalue;
4112 }
4113
4114 override void accept(Visitor v)
4115 {
4116 v.visit(this);
4117 }
4118 }
4119
4120 /***********************************************************
4121 * When an assignment expression is lowered to a druntime call
4122 * this class is used to store the lowering.
4123 * It essentially behaves the same as an AssignExp, but it is
4124 * used to not waste space for other AssignExp that are not
4125 * lowered to anything.
4126 */
4127 extern (C++) final class LoweredAssignExp : AssignExp
4128 {
4129 Expression lowering;
4130 extern (D) this(AssignExp exp, Expression lowering) @safe
4131 {
4132 super(exp.loc, EXP.loweredAssignExp, exp.e1, exp.e2);
4133 this.lowering = lowering;
4134 }
4135
4136 override void accept(Visitor v)
4137 {
4138 v.visit(this);
4139 }
4140 }
4141
4142 /***********************************************************
4143 */
4144 extern (C++) final class ConstructExp : AssignExp
4145 {
4146 extern (D) this(Loc loc, Expression e1, Expression e2) @safe
4147 {
4148 super(loc, EXP.construct, e1, e2);
4149 }
4150
4151 // Internal use only. If `v` is a reference variable, the assignment
4152 // will become a reference initialization automatically.
4153 extern (D) this(Loc loc, VarDeclaration v, Expression e2) @safe
4154 {
4155 auto ve = new VarExp(loc, v);
4156 assert(v.type && ve.type);
4157
4158 super(loc, EXP.construct, ve, e2);
4159
4160 if (v.isReference())
4161 memset = MemorySet.referenceInit;
4162 }
4163
4164 override void accept(Visitor v)
4165 {
4166 v.visit(this);
4167 }
4168 }
4169
4170 /***********************************************************
4171 * A bit-for-bit copy from `e2` to `e1`
4172 */
4173 extern (C++) final class BlitExp : AssignExp
4174 {
4175 extern (D) this(Loc loc, Expression e1, Expression e2) @safe
4176 {
4177 super(loc, EXP.blit, e1, e2);
4178 }
4179
4180 // Internal use only. If `v` is a reference variable, the assinment
4181 // will become a reference rebinding automatically.
4182 extern (D) this(Loc loc, VarDeclaration v, Expression e2) @safe
4183 {
4184 auto ve = new VarExp(loc, v);
4185 assert(v.type && ve.type);
4186
4187 super(loc, EXP.blit, ve, e2);
4188
4189 if (v.isReference())
4190 memset = MemorySet.referenceInit;
4191 }
4192
4193 override void accept(Visitor v)
4194 {
4195 v.visit(this);
4196 }
4197 }
4198
4199 /***********************************************************
4200 * `x += y`
4201 */
4202 extern (C++) final class AddAssignExp : BinAssignExp
4203 {
4204 extern (D) this(Loc loc, Expression e1, Expression e2) @safe
4205 {
4206 super(loc, EXP.addAssign, e1, e2);
4207 }
4208
4209 override void accept(Visitor v)
4210 {
4211 v.visit(this);
4212 }
4213 }
4214
4215 /***********************************************************
4216 * `x -= y`
4217 */
4218 extern (C++) final class MinAssignExp : BinAssignExp
4219 {
4220 extern (D) this(Loc loc, Expression e1, Expression e2) @safe
4221 {
4222 super(loc, EXP.minAssign, e1, e2);
4223 }
4224
4225 override void accept(Visitor v)
4226 {
4227 v.visit(this);
4228 }
4229 }
4230
4231 /***********************************************************
4232 * `x *= y`
4233 */
4234 extern (C++) final class MulAssignExp : BinAssignExp
4235 {
4236 extern (D) this(Loc loc, Expression e1, Expression e2) @safe
4237 {
4238 super(loc, EXP.mulAssign, e1, e2);
4239 }
4240
4241 override void accept(Visitor v)
4242 {
4243 v.visit(this);
4244 }
4245 }
4246
4247 /***********************************************************
4248 * `x /= y`
4249 */
4250 extern (C++) final class DivAssignExp : BinAssignExp
4251 {
4252 extern (D) this(Loc loc, Expression e1, Expression e2) @safe
4253 {
4254 super(loc, EXP.divAssign, e1, e2);
4255 }
4256
4257 override void accept(Visitor v)
4258 {
4259 v.visit(this);
4260 }
4261 }
4262
4263 /***********************************************************
4264 * `x %= y`
4265 */
4266 extern (C++) final class ModAssignExp : BinAssignExp
4267 {
4268 extern (D) this(Loc loc, Expression e1, Expression e2) @safe
4269 {
4270 super(loc, EXP.modAssign, e1, e2);
4271 }
4272
4273 override void accept(Visitor v)
4274 {
4275 v.visit(this);
4276 }
4277 }
4278
4279 /***********************************************************
4280 * `x &= y`
4281 */
4282 extern (C++) final class AndAssignExp : BinAssignExp
4283 {
4284 extern (D) this(Loc loc, Expression e1, Expression e2) @safe
4285 {
4286 super(loc, EXP.andAssign, e1, e2);
4287 }
4288
4289 override void accept(Visitor v)
4290 {
4291 v.visit(this);
4292 }
4293 }
4294
4295 /***********************************************************
4296 * `x |= y`
4297 */
4298 extern (C++) final class OrAssignExp : BinAssignExp
4299 {
4300 extern (D) this(Loc loc, Expression e1, Expression e2) @safe
4301 {
4302 super(loc, EXP.orAssign, e1, e2);
4303 }
4304
4305 override void accept(Visitor v)
4306 {
4307 v.visit(this);
4308 }
4309 }
4310
4311 /***********************************************************
4312 * `x ^= y`
4313 */
4314 extern (C++) final class XorAssignExp : BinAssignExp
4315 {
4316 extern (D) this(Loc loc, Expression e1, Expression e2) @safe
4317 {
4318 super(loc, EXP.xorAssign, e1, e2);
4319 }
4320
4321 override void accept(Visitor v)
4322 {
4323 v.visit(this);
4324 }
4325 }
4326
4327 /***********************************************************
4328 * `x ^^= y`
4329 */
4330 extern (C++) final class PowAssignExp : BinAssignExp
4331 {
4332 extern (D) this(Loc loc, Expression e1, Expression e2) @safe
4333 {
4334 super(loc, EXP.powAssign, e1, e2);
4335 }
4336
4337 override void accept(Visitor v)
4338 {
4339 v.visit(this);
4340 }
4341 }
4342
4343 /***********************************************************
4344 * `x <<= y`
4345 */
4346 extern (C++) final class ShlAssignExp : BinAssignExp
4347 {
4348 extern (D) this(Loc loc, Expression e1, Expression e2) @safe
4349 {
4350 super(loc, EXP.leftShiftAssign, e1, e2);
4351 }
4352
4353 override void accept(Visitor v)
4354 {
4355 v.visit(this);
4356 }
4357 }
4358
4359 /***********************************************************
4360 * `x >>= y`
4361 */
4362 extern (C++) final class ShrAssignExp : BinAssignExp
4363 {
4364 extern (D) this(Loc loc, Expression e1, Expression e2) @safe
4365 {
4366 super(loc, EXP.rightShiftAssign, e1, e2);
4367 }
4368
4369 override void accept(Visitor v)
4370 {
4371 v.visit(this);
4372 }
4373 }
4374
4375 /***********************************************************
4376 * `x >>>= y`
4377 */
4378 extern (C++) final class UshrAssignExp : BinAssignExp
4379 {
4380 extern (D) this(Loc loc, Expression e1, Expression e2) @safe
4381 {
4382 super(loc, EXP.unsignedRightShiftAssign, e1, e2);
4383 }
4384
4385 override void accept(Visitor v)
4386 {
4387 v.visit(this);
4388 }
4389 }
4390
4391 /***********************************************************
4392 * The `~=` operator.
4393 *
4394 * It can have one of the following operators:
4395 *
4396 * EXP.concatenateAssign - appending T[] to T[]
4397 * EXP.concatenateElemAssign - appending T to T[]
4398 * EXP.concatenateDcharAssign - appending dchar to T[]
4399 *
4400 * The parser initially sets it to EXP.concatenateAssign, and semantic() later decides which
4401 * of the three it will be set to.
4402 */
4403 extern (C++) class CatAssignExp : BinAssignExp
4404 {
4405 Expression lowering; // lowered druntime hook `_d_arrayappend{cTX,T}`
4406
4407 extern (D) this(Loc loc, Expression e1, Expression e2) @safe
4408 {
4409 super(loc, EXP.concatenateAssign, e1, e2);
4410 }
4411
4412 extern (D) this(Loc loc, EXP tok, Expression e1, Expression e2) @safe
4413 {
4414 super(loc, tok, e1, e2);
4415 }
4416
4417 override void accept(Visitor v)
4418 {
4419 v.visit(this);
4420 }
4421 }
4422
4423 /***********************************************************
4424 * The `~=` operator when appending a single element
4425 */
4426 extern (C++) final class CatElemAssignExp : CatAssignExp
4427 {
4428 extern (D) this(Loc loc, Type type, Expression e1, Expression e2) @safe
4429 {
4430 super(loc, EXP.concatenateElemAssign, e1, e2);
4431 this.type = type;
4432 }
4433
4434 override void accept(Visitor v)
4435 {
4436 v.visit(this);
4437 }
4438 }
4439
4440 /***********************************************************
4441 * The `~=` operator when appending a single `dchar`
4442 */
4443 extern (C++) final class CatDcharAssignExp : CatAssignExp
4444 {
4445 extern (D) this(Loc loc, Type type, Expression e1, Expression e2) @safe
4446 {
4447 super(loc, EXP.concatenateDcharAssign, e1, e2);
4448 this.type = type;
4449 }
4450
4451 override void accept(Visitor v)
4452 {
4453 v.visit(this);
4454 }
4455 }
4456
4457 /***********************************************************
4458 * The addition operator, `x + y`
4459 *
4460 * https://dlang.org/spec/expression.html#add_expressions
4461 */
4462 extern (C++) final class AddExp : BinExp
4463 {
4464 extern (D) this(Loc loc, Expression e1, Expression e2) @safe
4465 {
4466 super(loc, EXP.add, e1, e2);
4467 }
4468
4469 override void accept(Visitor v)
4470 {
4471 v.visit(this);
4472 }
4473 }
4474
4475 /***********************************************************
4476 * The minus operator, `x - y`
4477 *
4478 * https://dlang.org/spec/expression.html#add_expressions
4479 */
4480 extern (C++) final class MinExp : BinExp
4481 {
4482 extern (D) this(Loc loc, Expression e1, Expression e2) @safe
4483 {
4484 super(loc, EXP.min, e1, e2);
4485 }
4486
4487 override void accept(Visitor v)
4488 {
4489 v.visit(this);
4490 }
4491 }
4492
4493 /***********************************************************
4494 * The concatenation operator, `x ~ y`
4495 *
4496 * https://dlang.org/spec/expression.html#cat_expressions
4497 */
4498 extern (C++) final class CatExp : BinExp
4499 {
4500 Expression lowering; // call to druntime hook `_d_arraycatnTX`
4501
4502 extern (D) this(Loc loc, Expression e1, Expression e2) scope @safe
4503 {
4504 super(loc, EXP.concatenate, e1, e2);
4505 }
4506
4507 override void accept(Visitor v)
4508 {
4509 v.visit(this);
4510 }
4511 }
4512
4513 /***********************************************************
4514 * The multiplication operator, `x * y`
4515 *
4516 * https://dlang.org/spec/expression.html#mul_expressions
4517 */
4518 extern (C++) final class MulExp : BinExp
4519 {
4520 extern (D) this(Loc loc, Expression e1, Expression e2) @safe
4521 {
4522 super(loc, EXP.mul, e1, e2);
4523 }
4524
4525 override void accept(Visitor v)
4526 {
4527 v.visit(this);
4528 }
4529 }
4530
4531 /***********************************************************
4532 * The division operator, `x / y`
4533 *
4534 * https://dlang.org/spec/expression.html#mul_expressions
4535 */
4536 extern (C++) final class DivExp : BinExp
4537 {
4538 extern (D) this(Loc loc, Expression e1, Expression e2) @safe
4539 {
4540 super(loc, EXP.div, e1, e2);
4541 }
4542
4543 override void accept(Visitor v)
4544 {
4545 v.visit(this);
4546 }
4547 }
4548
4549 /***********************************************************
4550 * The modulo operator, `x % y`
4551 *
4552 * https://dlang.org/spec/expression.html#mul_expressions
4553 */
4554 extern (C++) final class ModExp : BinExp
4555 {
4556 extern (D) this(Loc loc, Expression e1, Expression e2) @safe
4557 {
4558 super(loc, EXP.mod, e1, e2);
4559 }
4560
4561 override void accept(Visitor v)
4562 {
4563 v.visit(this);
4564 }
4565 }
4566
4567 /***********************************************************
4568 * The 'power' operator, `x ^^ y`
4569 *
4570 * https://dlang.org/spec/expression.html#pow_expressions
4571 */
4572 extern (C++) final class PowExp : BinExp
4573 {
4574 extern (D) this(Loc loc, Expression e1, Expression e2) @safe
4575 {
4576 super(loc, EXP.pow, e1, e2);
4577 }
4578
4579 override void accept(Visitor v)
4580 {
4581 v.visit(this);
4582 }
4583 }
4584
4585 /***********************************************************
4586 * The 'shift left' operator, `x << y`
4587 *
4588 * https://dlang.org/spec/expression.html#shift_expressions
4589 */
4590 extern (C++) final class ShlExp : BinExp
4591 {
4592 extern (D) this(Loc loc, Expression e1, Expression e2) @safe
4593 {
4594 super(loc, EXP.leftShift, e1, e2);
4595 }
4596
4597 override void accept(Visitor v)
4598 {
4599 v.visit(this);
4600 }
4601 }
4602
4603 /***********************************************************
4604 * The 'shift right' operator, `x >> y`
4605 *
4606 * https://dlang.org/spec/expression.html#shift_expressions
4607 */
4608 extern (C++) final class ShrExp : BinExp
4609 {
4610 extern (D) this(Loc loc, Expression e1, Expression e2) @safe
4611 {
4612 super(loc, EXP.rightShift, e1, e2);
4613 }
4614
4615 override void accept(Visitor v)
4616 {
4617 v.visit(this);
4618 }
4619 }
4620
4621 /***********************************************************
4622 * The 'unsigned shift right' operator, `x >>> y`
4623 *
4624 * https://dlang.org/spec/expression.html#shift_expressions
4625 */
4626 extern (C++) final class UshrExp : BinExp
4627 {
4628 extern (D) this(Loc loc, Expression e1, Expression e2) @safe
4629 {
4630 super(loc, EXP.unsignedRightShift, e1, e2);
4631 }
4632
4633 override void accept(Visitor v)
4634 {
4635 v.visit(this);
4636 }
4637 }
4638
4639 /***********************************************************
4640 * The bitwise 'and' operator, `x & y`
4641 *
4642 * https://dlang.org/spec/expression.html#and_expressions
4643 */
4644 extern (C++) final class AndExp : BinExp
4645 {
4646 extern (D) this(Loc loc, Expression e1, Expression e2) @safe
4647 {
4648 super(loc, EXP.and, e1, e2);
4649 }
4650
4651 override void accept(Visitor v)
4652 {
4653 v.visit(this);
4654 }
4655 }
4656
4657 /***********************************************************
4658 * The bitwise 'or' operator, `x | y`
4659 *
4660 * https://dlang.org/spec/expression.html#or_expressions
4661 */
4662 extern (C++) final class OrExp : BinExp
4663 {
4664 extern (D) this(Loc loc, Expression e1, Expression e2) @safe
4665 {
4666 super(loc, EXP.or, e1, e2);
4667 }
4668
4669 override void accept(Visitor v)
4670 {
4671 v.visit(this);
4672 }
4673 }
4674
4675 /***********************************************************
4676 * The bitwise 'xor' operator, `x ^ y`
4677 *
4678 * https://dlang.org/spec/expression.html#xor_expressions
4679 */
4680 extern (C++) final class XorExp : BinExp
4681 {
4682 extern (D) this(Loc loc, Expression e1, Expression e2) @safe
4683 {
4684 super(loc, EXP.xor, e1, e2);
4685 }
4686
4687 override void accept(Visitor v)
4688 {
4689 v.visit(this);
4690 }
4691 }
4692
4693 /***********************************************************
4694 * The logical 'and' / 'or' operator, `X && Y` / `X || Y`
4695 *
4696 * https://dlang.org/spec/expression.html#andand_expressions
4697 * https://dlang.org/spec/expression.html#oror_expressions
4698 */
4699 extern (C++) final class LogicalExp : BinExp
4700 {
4701 extern (D) this(Loc loc, EXP op, Expression e1, Expression e2) @safe
4702 {
4703 super(loc, op, e1, e2);
4704 assert(op == EXP.andAnd || op == EXP.orOr);
4705 }
4706
4707 override void accept(Visitor v)
4708 {
4709 v.visit(this);
4710 }
4711 }
4712
4713 /***********************************************************
4714 * A comparison operator, `<` `<=` `>` `>=`
4715 *
4716 * `op` is one of:
4717 * EXP.lessThan, EXP.lessOrEqual, EXP.greaterThan, EXP.greaterOrEqual
4718 *
4719 * https://dlang.org/spec/expression.html#relation_expressions
4720 */
4721 extern (C++) final class CmpExp : BinExp
4722 {
4723 extern (D) this(EXP op, Loc loc, Expression e1, Expression e2) @safe
4724 {
4725 super(loc, op, e1, e2);
4726 assert(op == EXP.lessThan || op == EXP.lessOrEqual || op == EXP.greaterThan || op == EXP.greaterOrEqual);
4727 }
4728
4729 override void accept(Visitor v)
4730 {
4731 v.visit(this);
4732 }
4733 }
4734
4735 /***********************************************************
4736 * The `in` operator, `"a" in ["a": 1]`
4737 *
4738 * Note: `x !in y` is rewritten to `!(x in y)` in the parser
4739 *
4740 * https://dlang.org/spec/expression.html#in_expressions
4741 */
4742 extern (C++) final class InExp : BinExp
4743 {
4744 extern (D) this(Loc loc, Expression e1, Expression e2) @safe
4745 {
4746 super(loc, EXP.in_, e1, e2);
4747 }
4748
4749 override void accept(Visitor v)
4750 {
4751 v.visit(this);
4752 }
4753 }
4754
4755 /***********************************************************
4756 * Associative array removal, `aa.remove(arg)`
4757 *
4758 * This deletes the key e1 from the associative array e2
4759 */
4760 extern (C++) final class RemoveExp : BinExp
4761 {
4762 extern (D) this(Loc loc, Expression e1, Expression e2)
4763 {
4764 super(loc, EXP.remove, e1, e2);
4765 type = Type.tbool;
4766 }
4767
4768 override void accept(Visitor v)
4769 {
4770 v.visit(this);
4771 }
4772 }
4773
4774 /***********************************************************
4775 * `==` and `!=`
4776 *
4777 * EXP.equal and EXP.notEqual
4778 *
4779 * https://dlang.org/spec/expression.html#equality_expressions
4780 */
4781 extern (C++) final class EqualExp : BinExp
4782 {
4783 extern (D) this(EXP op, Loc loc, Expression e1, Expression e2) @safe
4784 {
4785 super(loc, op, e1, e2);
4786 assert(op == EXP.equal || op == EXP.notEqual);
4787 }
4788
4789 override void accept(Visitor v)
4790 {
4791 v.visit(this);
4792 }
4793 }
4794
4795 /***********************************************************
4796 * `is` and `!is`
4797 *
4798 * EXP.identity and EXP.notIdentity
4799 *
4800 * https://dlang.org/spec/expression.html#identity_expressions
4801 */
4802 extern (C++) final class IdentityExp : BinExp
4803 {
4804 extern (D) this(EXP op, Loc loc, Expression e1, Expression e2) @safe
4805 {
4806 super(loc, op, e1, e2);
4807 assert(op == EXP.identity || op == EXP.notIdentity);
4808 }
4809
4810 override void accept(Visitor v)
4811 {
4812 v.visit(this);
4813 }
4814 }
4815
4816 /***********************************************************
4817 * The ternary operator, `econd ? e1 : e2`
4818 *
4819 * https://dlang.org/spec/expression.html#conditional_expressions
4820 */
4821 extern (C++) final class CondExp : BinExp
4822 {
4823 Expression econd;
4824
4825 extern (D) this(Loc loc, Expression econd, Expression e1, Expression e2) scope @safe
4826 {
4827 super(loc, EXP.question, e1, e2);
4828 this.econd = econd;
4829 }
4830
4831 override CondExp syntaxCopy()
4832 {
4833 return new CondExp(loc, econd.syntaxCopy(), e1.syntaxCopy(), e2.syntaxCopy());
4834 }
4835
4836 override bool isLvalue()
4837 {
4838 return !rvalue && e1.isLvalue() && e2.isLvalue();
4839 }
4840
4841 override void accept(Visitor v)
4842 {
4843 v.visit(this);
4844 }
4845 }
4846
4847 /***********************************************************
4848 * A special keyword when used as a function's default argument
4849 *
4850 * When possible, special keywords are resolved in the parser, but when
4851 * appearing as a default argument, they result in an expression deriving
4852 * from this base class that is resolved for each function call.
4853 *
4854 * ---
4855 * const x = __LINE__; // resolved in the parser
4856 * void foo(string file = __FILE__, int line = __LINE__); // DefaultInitExp
4857 * ---
4858 *
4859 * https://dlang.org/spec/expression.html#specialkeywords
4860 */
4861 extern (C++) class DefaultInitExp : Expression
4862 {
4863 /*************************
4864 * Params:
4865 * loc = location
4866 * op = EXP.prettyFunction, EXP.functionString, EXP.moduleString,
4867 * EXP.line, EXP.file, EXP.fileFullPath
4868 */
4869 extern (D) this(Loc loc, EXP op) @safe
4870 {
4871 super(loc, op);
4872 }
4873
4874 override void accept(Visitor v)
4875 {
4876 v.visit(this);
4877 }
4878 }
4879
4880 /***********************************************************
4881 * The `__FILE__` token as a default argument
4882 */
4883 extern (C++) final class FileInitExp : DefaultInitExp
4884 {
4885 extern (D) this(Loc loc, EXP tok) @safe
4886 {
4887 super(loc, tok);
4888 }
4889
4890 override void accept(Visitor v)
4891 {
4892 v.visit(this);
4893 }
4894 }
4895
4896 /***********************************************************
4897 * The `__LINE__` token as a default argument
4898 */
4899 extern (C++) final class LineInitExp : DefaultInitExp
4900 {
4901 extern (D) this(Loc loc) @safe
4902 {
4903 super(loc, EXP.line);
4904 }
4905
4906 override void accept(Visitor v)
4907 {
4908 v.visit(this);
4909 }
4910 }
4911
4912 /***********************************************************
4913 * The `__MODULE__` token as a default argument
4914 */
4915 extern (C++) final class ModuleInitExp : DefaultInitExp
4916 {
4917 extern (D) this(Loc loc) @safe
4918 {
4919 super(loc, EXP.moduleString);
4920 }
4921
4922 override void accept(Visitor v)
4923 {
4924 v.visit(this);
4925 }
4926 }
4927
4928 /***********************************************************
4929 * The `__FUNCTION__` token as a default argument
4930 */
4931 extern (C++) final class FuncInitExp : DefaultInitExp
4932 {
4933 extern (D) this(Loc loc) @safe
4934 {
4935 super(loc, EXP.functionString);
4936 }
4937
4938 override void accept(Visitor v)
4939 {
4940 v.visit(this);
4941 }
4942 }
4943
4944 /***********************************************************
4945 * The `__PRETTY_FUNCTION__` token as a default argument
4946 */
4947 extern (C++) final class PrettyFuncInitExp : DefaultInitExp
4948 {
4949 extern (D) this(Loc loc) @safe
4950 {
4951 super(loc, EXP.prettyFunction);
4952 }
4953
4954 override void accept(Visitor v)
4955 {
4956 v.visit(this);
4957 }
4958 }
4959
4960 /***********************************************************
4961 * A reference to a class, or an interface. We need this when we
4962 * point to a base class (we must record what the type is).
4963 */
4964 extern (C++) final class ClassReferenceExp : Expression
4965 {
4966 StructLiteralExp value;
4967
4968 extern (D) this(Loc loc, StructLiteralExp lit, Type type) @safe
4969 {
4970 super(loc, EXP.classReference);
4971 assert(lit && lit.sd && lit.sd.isClassDeclaration());
4972 this.value = lit;
4973 this.type = type;
4974 }
4975
4976 ClassDeclaration originalClass()
4977 {
4978 return value.sd.isClassDeclaration();
4979 }
4980
4981 // Return index of the field, or -1 if not found
4982 int getFieldIndex(Type fieldtype, uint fieldoffset)
4983 {
4984 ClassDeclaration cd = originalClass();
4985 uint fieldsSoFar = 0;
4986 for (size_t j = 0; j < value.elements.length; j++)
4987 {
4988 while (j - fieldsSoFar >= cd.fields.length)
4989 {
4990 fieldsSoFar += cd.fields.length;
4991 cd = cd.baseClass;
4992 }
4993 VarDeclaration v2 = cd.fields[j - fieldsSoFar];
4994 if (fieldoffset == v2.offset && fieldtype.size() == v2.type.size())
4995 {
4996 return cast(int)(value.elements.length - fieldsSoFar - cd.fields.length + (j - fieldsSoFar));
4997 }
4998 }
4999 return -1;
5000 }
5001
5002 // Return index of the field, or -1 if not found
5003 // Same as getFieldIndex, but checks for a direct match with the VarDeclaration
5004 int findFieldIndexByName(VarDeclaration v)
5005 {
5006 ClassDeclaration cd = originalClass();
5007 size_t fieldsSoFar = 0;
5008 for (size_t j = 0; j < value.elements.length; j++)
5009 {
5010 while (j - fieldsSoFar >= cd.fields.length)
5011 {
5012 fieldsSoFar += cd.fields.length;
5013 cd = cd.baseClass;
5014 }
5015 VarDeclaration v2 = cd.fields[j - fieldsSoFar];
5016 if (v == v2)
5017 {
5018 return cast(int)(value.elements.length - fieldsSoFar - cd.fields.length + (j - fieldsSoFar));
5019 }
5020 }
5021 return -1;
5022 }
5023
5024 override void accept(Visitor v)
5025 {
5026 v.visit(this);
5027 }
5028 }
5029
5030 /***********************************************************
5031 * This type is only used by the interpreter.
5032 */
5033 extern (C++) final class CTFEExp : Expression
5034 {
5035 extern (D) this(EXP tok)
5036 {
5037 super(Loc.initial, tok);
5038 type = Type.tvoid;
5039 }
5040
5041 extern (D) __gshared CTFEExp cantexp;
5042 extern (D) __gshared CTFEExp voidexp;
5043 extern (D) __gshared CTFEExp breakexp;
5044 extern (D) __gshared CTFEExp continueexp;
5045 extern (D) __gshared CTFEExp gotoexp;
5046 /* Used when additional information is needed regarding
5047 * a ctfe error.
5048 */
5049 extern (D) __gshared CTFEExp showcontext;
5050
5051 extern (D) static bool isCantExp(const Expression e) @safe
5052 {
5053 return e && e.op == EXP.cantExpression;
5054 }
5055
5056 extern (D) static bool isGotoExp(const Expression e) @safe
5057 {
5058 return e && e.op == EXP.goto_;
5059 }
5060 }
5061
5062 /***********************************************************
5063 * Fake class which holds the thrown exception.
5064 * Used for implementing exception handling.
5065 */
5066 extern (C++) final class ThrownExceptionExp : Expression
5067 {
5068 ClassReferenceExp thrown; // the thing being tossed
5069
5070 extern (D) this(Loc loc, ClassReferenceExp victim) @safe
5071 {
5072 super(loc, EXP.thrownException);
5073 this.thrown = victim;
5074 this.type = victim.type;
5075 }
5076
5077 override void accept(Visitor v)
5078 {
5079 v.visit(this);
5080 }
5081 }
5082
5083 /**
5084 * Objective-C class reference expression.
5085 *
5086 * Used to get the metaclass of an Objective-C class, `NSObject.Class`.
5087 */
5088 extern (C++) final class ObjcClassReferenceExp : Expression
5089 {
5090 ClassDeclaration classDeclaration;
5091
5092 extern (D) this(Loc loc, ClassDeclaration classDeclaration) @safe
5093 {
5094 super(loc, EXP.objcClassReference);
5095 this.classDeclaration = classDeclaration;
5096 }
5097
5098 override void accept(Visitor v)
5099 {
5100 v.visit(this);
5101 }
5102 }
5103
5104 /*******************
5105 * C11 6.5.1.1 Generic Selection
5106 * For ImportC
5107 */
5108 extern (C++) final class GenericExp : Expression
5109 {
5110 Expression cntlExp; /// controlling expression of a generic selection (not evaluated)
5111 Types* types; /// type-names for generic associations (null entry for `default`)
5112 Expressions* exps; /// 1:1 mapping of typeNames to exps
5113
5114 extern (D) this(Loc loc, Expression cntlExp, Types* types, Expressions* exps) @safe
5115 {
5116 super(loc, EXP._Generic);
5117 this.cntlExp = cntlExp;
5118 this.types = types;
5119 this.exps = exps;
5120 assert(types.length == exps.length); // must be the same and >=1
5121 }
5122
5123 override GenericExp syntaxCopy()
5124 {
5125 return new GenericExp(loc, cntlExp.syntaxCopy(), Type.arraySyntaxCopy(types), Expression.arraySyntaxCopy(exps));
5126 }
5127
5128 override void accept(Visitor v)
5129 {
5130 v.visit(this);
5131 }
5132 }
5133
5134 /**
5135 * Verify if the given identifier is _d_array{,set}ctor.
5136 *
5137 * Params:
5138 * id = the identifier to verify
5139 *
5140 * Returns:
5141 * `true` if the identifier corresponds to a construction runtime hook,
5142 * `false` otherwise.
5143 */
5144 bool isArrayConstruction(const Identifier id)
5145 {
5146 import dmd.id : Id;
5147
5148 return id == Id._d_arrayctor || id == Id._d_arraysetctor;
5149 }
5150
5151 /******************************
5152 * Provide efficient way to implement isUnaExp(), isBinExp(), isBinAssignExp()
5153 */
5154 private immutable ubyte[EXP.max + 1] exptab =
5155 () {
5156 ubyte[EXP.max + 1] tab;
5157 with (EXPFLAGS)
5158 {
5159 foreach (i; Eunary) { tab[i] |= unary; }
5160 foreach (i; Ebinary) { tab[i] |= unary | binary; }
5161 foreach (i; EbinaryAssign) { tab[i] |= unary | binary | binaryAssign; }
5162 }
5163 return tab;
5164 } ();
5165
5166 private enum EXPFLAGS : ubyte
5167 {
5168 unary = 1,
5169 binary = 2,
5170 binaryAssign = 4,
5171 }
5172
5173 private enum Eunary =
5174 [
5175 EXP.import_, EXP.assert_, EXP.throw_, EXP.dotIdentifier, EXP.dotTemplateDeclaration,
5176 EXP.dotVariable, EXP.dotTemplateInstance, EXP.delegate_, EXP.dotType, EXP.call,
5177 EXP.address, EXP.star, EXP.negate, EXP.uadd, EXP.tilde, EXP.not, EXP.delete_, EXP.cast_,
5178 EXP.vector, EXP.vectorArray, EXP.slice, EXP.arrayLength, EXP.array, EXP.delegatePointer,
5179 EXP.delegateFunctionPointer, EXP.preMinusMinus, EXP.prePlusPlus,
5180 ];
5181
5182 private enum Ebinary =
5183 [
5184 EXP.dot, EXP.comma, EXP.index, EXP.minusMinus, EXP.plusPlus, EXP.assign,
5185 EXP.add, EXP.min, EXP.concatenate, EXP.mul, EXP.div, EXP.mod, EXP.pow, EXP.leftShift,
5186 EXP.rightShift, EXP.unsignedRightShift, EXP.and, EXP.or, EXP.xor, EXP.andAnd, EXP.orOr,
5187 EXP.lessThan, EXP.lessOrEqual, EXP.greaterThan, EXP.greaterOrEqual,
5188 EXP.in_, EXP.remove, EXP.equal, EXP.notEqual, EXP.identity, EXP.notIdentity,
5189 EXP.question,
5190 EXP.construct, EXP.blit,
5191 ];
5192
5193 private enum EbinaryAssign =
5194 [
5195 EXP.addAssign, EXP.minAssign, EXP.mulAssign, EXP.divAssign, EXP.modAssign,
5196 EXP.andAssign, EXP.orAssign, EXP.xorAssign, EXP.powAssign,
5197 EXP.leftShiftAssign, EXP.rightShiftAssign, EXP.unsignedRightShiftAssign,
5198 EXP.concatenateAssign, EXP.concatenateElemAssign, EXP.concatenateDcharAssign,
5199 ];
5200
5201 /// Given a member of the EXP enum, get the class instance size of the corresponding Expression class.
5202 /// Needed because the classes are `extern(C++)`
5203 private immutable ubyte[EXP.max+1] expSize = [
5204 EXP.reserved: 0,
5205 EXP.negate: __traits(classInstanceSize, NegExp),
5206 EXP.cast_: __traits(classInstanceSize, CastExp),
5207 EXP.null_: __traits(classInstanceSize, NullExp),
5208 EXP.assert_: __traits(classInstanceSize, AssertExp),
5209 EXP.array: __traits(classInstanceSize, ArrayExp),
5210 EXP.call: __traits(classInstanceSize, CallExp),
5211 EXP.address: __traits(classInstanceSize, AddrExp),
5212 EXP.type: __traits(classInstanceSize, TypeExp),
5213 EXP.throw_: __traits(classInstanceSize, ThrowExp),
5214 EXP.new_: __traits(classInstanceSize, NewExp),
5215 EXP.delete_: __traits(classInstanceSize, DeleteExp),
5216 EXP.star: __traits(classInstanceSize, PtrExp),
5217 EXP.symbolOffset: __traits(classInstanceSize, SymOffExp),
5218 EXP.variable: __traits(classInstanceSize, VarExp),
5219 EXP.dotVariable: __traits(classInstanceSize, DotVarExp),
5220 EXP.dotIdentifier: __traits(classInstanceSize, DotIdExp),
5221 EXP.dotTemplateInstance: __traits(classInstanceSize, DotTemplateInstanceExp),
5222 EXP.dotType: __traits(classInstanceSize, DotTypeExp),
5223 EXP.slice: __traits(classInstanceSize, SliceExp),
5224 EXP.arrayLength: __traits(classInstanceSize, ArrayLengthExp),
5225 EXP.dollar: __traits(classInstanceSize, DollarExp),
5226 EXP.template_: __traits(classInstanceSize, TemplateExp),
5227 EXP.dotTemplateDeclaration: __traits(classInstanceSize, DotTemplateExp),
5228 EXP.declaration: __traits(classInstanceSize, DeclarationExp),
5229 EXP.dSymbol: __traits(classInstanceSize, DsymbolExp),
5230 EXP.typeid_: __traits(classInstanceSize, TypeidExp),
5231 EXP.uadd: __traits(classInstanceSize, UAddExp),
5232 EXP.remove: __traits(classInstanceSize, RemoveExp),
5233 EXP.newAnonymousClass: __traits(classInstanceSize, NewAnonClassExp),
5234 EXP.arrayLiteral: __traits(classInstanceSize, ArrayLiteralExp),
5235 EXP.assocArrayLiteral: __traits(classInstanceSize, AssocArrayLiteralExp),
5236 EXP.structLiteral: __traits(classInstanceSize, StructLiteralExp),
5237 EXP.classReference: __traits(classInstanceSize, ClassReferenceExp),
5238 EXP.thrownException: __traits(classInstanceSize, ThrownExceptionExp),
5239 EXP.delegatePointer: __traits(classInstanceSize, DelegatePtrExp),
5240 EXP.delegateFunctionPointer: __traits(classInstanceSize, DelegateFuncptrExp),
5241 EXP.lessThan: __traits(classInstanceSize, CmpExp),
5242 EXP.greaterThan: __traits(classInstanceSize, CmpExp),
5243 EXP.lessOrEqual: __traits(classInstanceSize, CmpExp),
5244 EXP.greaterOrEqual: __traits(classInstanceSize, CmpExp),
5245 EXP.equal: __traits(classInstanceSize, EqualExp),
5246 EXP.notEqual: __traits(classInstanceSize, EqualExp),
5247 EXP.identity: __traits(classInstanceSize, IdentityExp),
5248 EXP.notIdentity: __traits(classInstanceSize, IdentityExp),
5249 EXP.index: __traits(classInstanceSize, IndexExp),
5250 EXP.is_: __traits(classInstanceSize, IsExp),
5251 EXP.leftShift: __traits(classInstanceSize, ShlExp),
5252 EXP.rightShift: __traits(classInstanceSize, ShrExp),
5253 EXP.leftShiftAssign: __traits(classInstanceSize, ShlAssignExp),
5254 EXP.rightShiftAssign: __traits(classInstanceSize, ShrAssignExp),
5255 EXP.unsignedRightShift: __traits(classInstanceSize, UshrExp),
5256 EXP.unsignedRightShiftAssign: __traits(classInstanceSize, UshrAssignExp),
5257 EXP.concatenate: __traits(classInstanceSize, CatExp),
5258 EXP.concatenateAssign: __traits(classInstanceSize, CatAssignExp),
5259 EXP.concatenateElemAssign: __traits(classInstanceSize, CatElemAssignExp),
5260 EXP.concatenateDcharAssign: __traits(classInstanceSize, CatDcharAssignExp),
5261 EXP.add: __traits(classInstanceSize, AddExp),
5262 EXP.min: __traits(classInstanceSize, MinExp),
5263 EXP.addAssign: __traits(classInstanceSize, AddAssignExp),
5264 EXP.minAssign: __traits(classInstanceSize, MinAssignExp),
5265 EXP.mul: __traits(classInstanceSize, MulExp),
5266 EXP.div: __traits(classInstanceSize, DivExp),
5267 EXP.mod: __traits(classInstanceSize, ModExp),
5268 EXP.mulAssign: __traits(classInstanceSize, MulAssignExp),
5269 EXP.divAssign: __traits(classInstanceSize, DivAssignExp),
5270 EXP.modAssign: __traits(classInstanceSize, ModAssignExp),
5271 EXP.and: __traits(classInstanceSize, AndExp),
5272 EXP.or: __traits(classInstanceSize, OrExp),
5273 EXP.xor: __traits(classInstanceSize, XorExp),
5274 EXP.andAssign: __traits(classInstanceSize, AndAssignExp),
5275 EXP.orAssign: __traits(classInstanceSize, OrAssignExp),
5276 EXP.xorAssign: __traits(classInstanceSize, XorAssignExp),
5277 EXP.assign: __traits(classInstanceSize, AssignExp),
5278 EXP.not: __traits(classInstanceSize, NotExp),
5279 EXP.tilde: __traits(classInstanceSize, ComExp),
5280 EXP.plusPlus: __traits(classInstanceSize, PostExp),
5281 EXP.minusMinus: __traits(classInstanceSize, PostExp),
5282 EXP.construct: __traits(classInstanceSize, ConstructExp),
5283 EXP.blit: __traits(classInstanceSize, BlitExp),
5284 EXP.dot: __traits(classInstanceSize, DotExp),
5285 EXP.comma: __traits(classInstanceSize, CommaExp),
5286 EXP.question: __traits(classInstanceSize, CondExp),
5287 EXP.andAnd: __traits(classInstanceSize, LogicalExp),
5288 EXP.orOr: __traits(classInstanceSize, LogicalExp),
5289 EXP.prePlusPlus: __traits(classInstanceSize, PreExp),
5290 EXP.preMinusMinus: __traits(classInstanceSize, PreExp),
5291 EXP.identifier: __traits(classInstanceSize, IdentifierExp),
5292 EXP.string_: __traits(classInstanceSize, StringExp),
5293 EXP.interpolated: __traits(classInstanceSize, InterpExp),
5294 EXP.this_: __traits(classInstanceSize, ThisExp),
5295 EXP.super_: __traits(classInstanceSize, SuperExp),
5296 EXP.halt: __traits(classInstanceSize, HaltExp),
5297 EXP.tuple: __traits(classInstanceSize, TupleExp),
5298 EXP.error: __traits(classInstanceSize, ErrorExp),
5299 EXP.void_: __traits(classInstanceSize, VoidInitExp),
5300 EXP.int64: __traits(classInstanceSize, IntegerExp),
5301 EXP.float64: __traits(classInstanceSize, RealExp),
5302 EXP.complex80: __traits(classInstanceSize, ComplexExp),
5303 EXP.import_: __traits(classInstanceSize, ImportExp),
5304 EXP.delegate_: __traits(classInstanceSize, DelegateExp),
5305 EXP.function_: __traits(classInstanceSize, FuncExp),
5306 EXP.mixin_: __traits(classInstanceSize, MixinExp),
5307 EXP.in_: __traits(classInstanceSize, InExp),
5308 EXP.break_: __traits(classInstanceSize, CTFEExp),
5309 EXP.continue_: __traits(classInstanceSize, CTFEExp),
5310 EXP.goto_: __traits(classInstanceSize, CTFEExp),
5311 EXP.scope_: __traits(classInstanceSize, ScopeExp),
5312 EXP.traits: __traits(classInstanceSize, TraitsExp),
5313 EXP.overloadSet: __traits(classInstanceSize, OverExp),
5314 EXP.line: __traits(classInstanceSize, LineInitExp),
5315 EXP.file: __traits(classInstanceSize, FileInitExp),
5316 EXP.fileFullPath: __traits(classInstanceSize, FileInitExp),
5317 EXP.moduleString: __traits(classInstanceSize, ModuleInitExp),
5318 EXP.functionString: __traits(classInstanceSize, FuncInitExp),
5319 EXP.prettyFunction: __traits(classInstanceSize, PrettyFuncInitExp),
5320 EXP.pow: __traits(classInstanceSize, PowExp),
5321 EXP.powAssign: __traits(classInstanceSize, PowAssignExp),
5322 EXP.vector: __traits(classInstanceSize, VectorExp),
5323 EXP.voidExpression: __traits(classInstanceSize, CTFEExp),
5324 EXP.cantExpression: __traits(classInstanceSize, CTFEExp),
5325 EXP.showCtfeContext: __traits(classInstanceSize, CTFEExp),
5326 EXP.objcClassReference: __traits(classInstanceSize, ObjcClassReferenceExp),
5327 EXP.vectorArray: __traits(classInstanceSize, VectorArrayExp),
5328 EXP.compoundLiteral: __traits(classInstanceSize, CompoundLiteralExp),
5329 EXP._Generic: __traits(classInstanceSize, GenericExp),
5330 EXP.interval: __traits(classInstanceSize, IntervalExp),
5331 EXP.loweredAssignExp : __traits(classInstanceSize, LoweredAssignExp),
5332 ];