Debian Codegraph @

gcc/d/expr.cc

raw
1 /* expr.cc -- Lower D frontend expressions to GCC trees.
2 Copyright (C) 2015-2025 Free Software Foundation, Inc.
3
4 GCC is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3, or (at your option)
7 any later version.
8
9 GCC is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with GCC; see the file COPYING3. If not see
16 <http://www.gnu.org/licenses/>. */
17
18 #include "config.h"
19 #include "system.h"
20 #include "coretypes.h"
21
22 #include "dmd/aggregate.h"
23 #include "dmd/ctfe.h"
24 #include "dmd/declaration.h"
25 #include "dmd/enum.h"
26 #include "dmd/expression.h"
27 #include "dmd/identifier.h"
28 #include "dmd/init.h"
29 #include "dmd/module.h"
30 #include "dmd/mtype.h"
31 #include "dmd/template.h"
32
33 #include "tree.h"
34 #include "fold-const.h"
35 #include "diagnostic.h"
36 #include "langhooks.h"
37 #include "tm.h"
38 #include "function.h"
39 #include "toplev.h"
40 #include "varasm.h"
41 #include "predict.h"
42 #include "stor-layout.h"
43
44 #include "d-tree.h"
45
46
47 /* Determine if type T is a struct that has a postblit. */
48
49 static bool
50 needs_postblit (Type *t)
51 {
52 t = t->baseElemOf ();
53
54 if (TypeStruct *ts = t->isTypeStruct ())
55 {
56 if (ts->sym->postblit)
57 return true;
58 }
59
60 return false;
61 }
62
63 /* Determine if type T is a struct that has a destructor. */
64
65 static bool
66 needs_dtor (Type *t)
67 {
68 t = t->baseElemOf ();
69
70 if (TypeStruct *ts = t->isTypeStruct ())
71 {
72 if (ts->sym->dtor)
73 return true;
74 }
75
76 return false;
77 }
78
79 /* Determine if expression E is a suitable lvalue. */
80
81 static bool
82 lvalue_p (Expression *e)
83 {
84 SliceExp *se = e->isSliceExp ();
85 if (se != NULL && se->e1->isLvalue ())
86 return true;
87
88 CastExp *ce = e->isCastExp ();
89 if (ce != NULL && ce->e1->isLvalue ())
90 return true;
91
92 return (e->op != EXP::slice && e->isLvalue ());
93 }
94
95 /* Build an expression of code CODE, data type TYPE, and operands ARG0 and
96 ARG1. Perform relevant conversions needed for correct code operations. */
97
98 static tree
99 binary_op (tree_code code, tree type, tree arg0, tree arg1)
100 {
101 tree t0 = TREE_TYPE (arg0);
102 tree t1 = TREE_TYPE (arg1);
103 tree ret = NULL_TREE;
104
105 /* Deal with float mod expressions immediately. */
106 if (code == FLOAT_MOD_EXPR)
107 return build_float_modulus (type, arg0, arg1);
108
109 if (POINTER_TYPE_P (t0) && INTEGRAL_TYPE_P (t1))
110 return build_nop (type, build_offset_op (code, arg0, arg1));
111
112 if (INTEGRAL_TYPE_P (t0) && POINTER_TYPE_P (t1))
113 return build_nop (type, build_offset_op (code, arg1, arg0));
114
115 if (POINTER_TYPE_P (t0) && POINTER_TYPE_P (t1))
116 {
117 gcc_assert (code == MINUS_EXPR);
118 tree ptrtype = lang_hooks.types.type_for_mode (ptr_mode, 0);
119
120 /* POINTER_DIFF_EXPR requires a signed integer type of the same size as
121 pointers. If some platform cannot provide that, or has a larger
122 ptrdiff_type to support differences larger than half the address
123 space, cast the pointers to some larger integer type and do the
124 computations in that type. */
125 if (TYPE_PRECISION (ptrtype) > TYPE_PRECISION (t0))
126 ret = fold_build2 (MINUS_EXPR, ptrtype,
127 d_convert (ptrtype, arg0),
128 d_convert (ptrtype, arg1));
129 else
130 ret = fold_build2 (POINTER_DIFF_EXPR, ptrtype, arg0, arg1);
131 }
132 else
133 {
134 /* If the operation needs excess precision. */
135 tree eptype = excess_precision_type (type);
136 if (eptype != NULL_TREE)
137 {
138 arg0 = d_convert (eptype, arg0);
139 arg1 = d_convert (eptype, arg1);
140 }
141 else
142 {
143 /* Front-end does not do this conversion and GCC does not
144 always do it right. */
145 if (COMPLEX_FLOAT_TYPE_P (t0) && !COMPLEX_FLOAT_TYPE_P (t1))
146 arg1 = d_convert (t0, arg1);
147 else if (COMPLEX_FLOAT_TYPE_P (t1) && !COMPLEX_FLOAT_TYPE_P (t0))
148 arg0 = d_convert (t1, arg0);
149
150 eptype = type;
151 }
152
153 ret = build2 (code, eptype, arg0, arg1);
154 }
155
156 return d_convert (type, ret);
157 }
158
159 /* Build a binary expression of code CODE, assigning the result into E1. */
160
161 static tree
162 binop_assignment (tree_code code, Expression *e1, Expression *e2)
163 {
164 /* Skip casts for lhs assignment. */
165 Expression *e1b = e1;
166 while (e1b->op == EXP::cast_)
167 {
168 CastExp *ce = e1b->isCastExp ();
169 gcc_assert (same_type_p (ce->type, ce->to));
170 e1b = ce->e1;
171 }
172
173 /* Stabilize LHS for assignment. */
174 tree lhs = build_expr (e1b);
175 tree lexpr = stabilize_expr (&lhs);
176
177 /* The LHS expression could be an assignment, to which its operation gets
178 lost during gimplification. */
179 if (TREE_CODE (lhs) == MODIFY_EXPR)
180 {
181 /* If LHS has side effects, call stabilize_reference on it, so it can
182 be evaluated multiple times. */
183 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
184 lhs = build_assign (MODIFY_EXPR,
185 stabilize_reference (TREE_OPERAND (lhs, 0)),
186 TREE_OPERAND (lhs, 1));
187
188 lexpr = compound_expr (lexpr, lhs);
189 lhs = TREE_OPERAND (lhs, 0);
190 }
191
192 lhs = stabilize_reference (lhs);
193
194 /* Save RHS, to ensure that the expression is evaluated before LHS. */
195 tree rhs = build_expr (e2);
196 tree rexpr = d_save_expr (rhs);
197
198 rhs = binary_op (code, build_ctype (e1->type),
199 convert_expr (lhs, e1b->type, e1->type), rexpr);
200 if (TREE_SIDE_EFFECTS (rhs))
201 rhs = compound_expr (rexpr, rhs);
202
203 tree expr = modify_expr (lhs, convert_expr (rhs, e1->type, e1b->type));
204 return compound_expr (lexpr, expr);
205 }
206
207 /* Compile the function literal body. */
208
209 static void
210 build_lambda_tree (FuncLiteralDeclaration *fld, Type *type = NULL)
211 {
212 /* This check is for lambda's, remove `vthis' as function isn't nested. */
213 if (fld->tok == TOK::reserved && (type == NULL || type->ty == TY::Tpointer))
214 {
215 fld->tok = TOK::function_;
216 fld->vthis = NULL;
217 }
218
219 /* Compile the function literal body. */
220 build_decl_tree (fld);
221 }
222
223 /* Implements the visitor interface to build the GCC trees of all Expression
224 AST classes emitted from the D Front-end.
225 All visit methods accept one parameter E, which holds the frontend AST
226 of the expression to compile. They also don't return any value, instead
227 generated code is cached in RESULT_ and returned from the caller. */
228
229 class ExprVisitor : public Visitor
230 {
231 using Visitor::visit;
232
233 tree result_;
234 bool constp_;
235 bool literalp_;
236
237 public:
238 ExprVisitor (bool constp, bool literalp)
239 {
240 this->result_ = NULL_TREE;
241 this->constp_ = constp;
242 this->literalp_ = literalp;
243 }
244
245 tree result (void)
246 {
247 return this->result_;
248 }
249
250 /* Visitor interfaces, each Expression class should have
251 overridden the default. */
252
253 void visit (Expression *) final override
254 {
255 gcc_unreachable ();
256 }
257
258 /* Build a conditional expression. If either the second or third
259 expression is void, then the resulting type is void. Otherwise
260 they are implicitly converted to a common type. */
261
262 void visit (CondExp *e) final override
263 {
264 tree cond = convert_for_condition (build_expr (e->econd),
265 e->econd->type);
266 tree t1 = build_expr (e->e1);
267 tree t2 = build_expr (e->e2);
268
269 if (e->type->ty != TY::Tvoid)
270 {
271 t1 = convert_expr (t1, e->e1->type, e->type);
272 t2 = convert_expr (t2, e->e2->type, e->type);
273 }
274
275 this->result_ = build_condition (build_ctype (e->type), cond, t1, t2);
276 }
277
278 /* Build an identity comparison expression. Operands go through the
279 usual conversions to bring them to a common type before comparison.
280 The result type is bool. */
281
282 void visit (IdentityExp *e) final override
283 {
284 tree_code code = (e->op == EXP::identity) ? EQ_EXPR : NE_EXPR;
285 Type *tb1 = e->e1->type->toBasetype ();
286 Type *tb2 = e->e2->type->toBasetype ();
287
288 if ((tb1->ty == TY::Tsarray || tb1->ty == TY::Tarray)
289 && (tb2->ty == TY::Tsarray || tb2->ty == TY::Tarray))
290 {
291 /* For static and dynamic arrays, identity is defined as referring to
292 the same array elements and the same number of elements. */
293 tree t1 = d_array_convert (e->e1);
294 tree t2 = d_array_convert (e->e2);
295 this->result_ = d_convert (build_ctype (e->type),
296 build_boolop (code, t1, t2));
297 }
298 else if (tb1->isFloating () && tb1->ty != TY::Tvector)
299 {
300 /* For floating-point values, identity is defined as the bits in the
301 operands being identical. */
302 tree t1 = d_save_expr (build_expr (e->e1));
303 tree t2 = d_save_expr (build_expr (e->e2));
304
305 if (!tb1->isComplex ())
306 this->result_ = build_float_identity (code, t1, t2);
307 else
308 {
309 /* Compare the real and imaginary parts separately. */
310 tree req = build_float_identity (code, real_part (t1),
311 real_part (t2));
312 tree ieq = build_float_identity (code, imaginary_part (t1),
313 imaginary_part (t2));
314
315 if (code == EQ_EXPR)
316 this->result_ = build_boolop (TRUTH_ANDIF_EXPR, req, ieq);
317 else
318 this->result_ = build_boolop (TRUTH_ORIF_EXPR, req, ieq);
319 }
320 }
321 else if (TypeStruct *ts = tb1->isTypeStruct ())
322 {
323 /* For struct objects, identity is defined as bits in operands being
324 identical also. Alignment holes in structs are ignored. */
325 tree t1 = build_expr (e->e1);
326 tree t2 = build_expr (e->e2);
327
328 gcc_assert (same_type_p (tb1, tb2));
329
330 this->result_ = build_struct_comparison (code, ts->sym, t1, t2);
331 }
332 else if (tb1->ty == TY::Tvector && tb2->ty == TY::Tvector)
333 {
334 /* For vectors, identity is defined as all values being equal. */
335 tree t1 = build_expr (e->e1);
336 tree t2 = build_expr (e->e2);
337 tree mask = build_boolop (code, t1, t2);
338
339 /* To reinterpret the vector comparison as a boolean expression, bitcast
340 the bitmask result and generate an additional integer comparison. */
341 opt_scalar_int_mode mode =
342 int_mode_for_mode (TYPE_MODE (TREE_TYPE (mask)));
343 gcc_assert (mode.exists ());
344
345 tree type = lang_hooks.types.type_for_mode (mode.require (), 1);
346 if (type == NULL_TREE)
347 type = make_unsigned_type (GET_MODE_BITSIZE (mode.require ()));
348
349 /* In `t1 is t2', all mask bits must be set for vectors to be equal.
350 Otherwise any bit set is enough for vectors to be not-equal. */
351 tree mask_eq = (code == EQ_EXPR)
352 ? build_all_ones_cst (type) : build_zero_cst (type);
353
354 this->result_ = build_boolop (code, mask_eq,
355 build_vconvert (type, mask));
356 }
357 else
358 {
359 /* For operands of other types, identity is defined as being the
360 same as equality expressions. */
361 tree t1 = build_expr (e->e1);
362 tree t2 = build_expr (e->e2);
363 this->result_ = d_convert (build_ctype (e->type),
364 build_boolop (code, t1, t2));
365 }
366 }
367
368 /* Build an equality expression, which compare the two operands for either
369 equality or inequality. Operands go through the usual conversions to bring
370 them to a common type before comparison. The result type is bool. */
371
372 void visit (EqualExp *e) final override
373 {
374 Type *tb1 = e->e1->type->toBasetype ();
375 Type *tb2 = e->e2->type->toBasetype ();
376 tree_code code = (e->op == EXP::equal) ? EQ_EXPR : NE_EXPR;
377
378 if ((tb1->ty == TY::Tsarray || tb1->ty == TY::Tarray)
379 && (tb2->ty == TY::Tsarray || tb2->ty == TY::Tarray))
380 {
381 /* For static and dynamic arrays, equality is defined as the lengths of
382 the arrays matching, and all the elements are equal. */
383 Type *t1elem = tb1->nextOf ()->toBasetype ();
384 Type *t2elem = tb1->nextOf ()->toBasetype ();
385
386 /* Check if comparisons of arrays can be optimized using memcmp.
387 This will inline EQ expressions as:
388 e1.length == e2.length && memcmp(e1.ptr, e2.ptr, size) == 0;
389 Or when generating a NE expression:
390 e1.length != e2.length || memcmp(e1.ptr, e2.ptr, size) != 0; */
391 if ((t1elem->isIntegral () || t1elem->ty == TY::Tvoid
392 || (t1elem->ty == TY::Tstruct
393 && !t1elem->isTypeStruct ()->sym->xeq))
394 && t1elem->ty == t2elem->ty)
395 {
396 tree t1 = d_array_convert (e->e1);
397 tree t2 = d_array_convert (e->e2);
398 tree result;
399
400 /* Make temporaries to prevent multiple evaluations. */
401 tree t1saved = d_save_expr (t1);
402 tree t2saved = d_save_expr (t2);
403
404 /* Length of arrays, for comparisons done before calling memcmp. */
405 tree t1len = d_array_length (t1saved);
406 tree t2len = d_array_length (t2saved);
407
408 /* Reference to array data. */
409 tree t1ptr = d_array_ptr (t1saved);
410 tree t2ptr = d_array_ptr (t2saved);
411
412 /* Compare arrays using memcmp if possible, otherwise for structs,
413 each field is compared inline. */
414 if (t1elem->ty != TY::Tstruct
415 || identity_compare_p (t1elem->isTypeStruct ()->sym))
416 {
417 tree size =
418 size_mult_expr (t1len, size_int (dmd::size (t1elem)));
419
420 result = build_memcmp_call (t1ptr, t2ptr, size);
421 result = build_boolop (code, result, integer_zero_node);
422 }
423 else
424 {
425 StructDeclaration *sd = t1elem->isTypeStruct ()->sym;
426
427 result = build_array_struct_comparison (code, sd, t1len,
428 t1ptr, t2ptr);
429 }
430
431 /* Check array length first before passing to memcmp.
432 For equality expressions, this becomes:
433 (e1.length == 0 || memcmp);
434 Otherwise for inequality:
435 (e1.length != 0 && memcmp); */
436 tree tsizecmp = build_boolop (code, t1len, size_zero_node);
437 if (e->op == EXP::equal)
438 result = build_boolop (TRUTH_ORIF_EXPR, tsizecmp, result);
439 else
440 result = build_boolop (TRUTH_ANDIF_EXPR, tsizecmp, result);
441
442 /* Finally, check if lengths of both arrays match if dynamic.
443 The frontend should have already guaranteed that static arrays
444 have same size. */
445 if (tb1->ty == TY::Tsarray && tb2->ty == TY::Tsarray)
446 gcc_assert (dmd::size (tb1) == dmd::size (tb2));
447 else
448 {
449 tree tlencmp = build_boolop (code, t1len, t2len);
450 if (e->op == EXP::equal)
451 result = build_boolop (TRUTH_ANDIF_EXPR, tlencmp, result);
452 else
453 result = build_boolop (TRUTH_ORIF_EXPR, tlencmp, result);
454 }
455
456 /* Ensure left-to-right order of evaluation. */
457 if (TREE_SIDE_EFFECTS (t2))
458 result = compound_expr (t2saved, result);
459
460 if (TREE_SIDE_EFFECTS (t1))
461 result = compound_expr (t1saved, result);
462
463 this->result_ = result;
464 }
465 else
466 {
467 /* Use _adEq2() to compare each element. */
468 Type *t1array = dmd::arrayOf (t1elem);
469 tree result = build_libcall (LIBCALL_ADEQ2, e->type, 3,
470 d_array_convert (e->e1),
471 d_array_convert (e->e2),
472 build_typeinfo (e, t1array));
473
474 if (e->op == EXP::notEqual)
475 result = build1 (TRUTH_NOT_EXPR, build_ctype (e->type), result);
476
477 this->result_ = result;
478 }
479 }
480 else if (TypeStruct *ts = tb1->isTypeStruct ())
481 {
482 /* Equality for struct objects means the logical product of all
483 equality results of the corresponding object fields. */
484 tree t1 = build_expr (e->e1);
485 tree t2 = build_expr (e->e2);
486
487 gcc_assert (same_type_p (tb1, tb2));
488
489 this->result_ = build_struct_comparison (code, ts->sym, t1, t2);
490 }
491 else if (tb1->ty == TY::Taarray && tb2->ty == TY::Taarray)
492 {
493 /* Use _aaEqual() for associative arrays. */
494 tree result = build_libcall (LIBCALL_AAEQUAL, e->type, 3,
495 build_typeinfo (e, tb1),
496 build_expr (e->e1),
497 build_expr (e->e2));
498
499 if (e->op == EXP::notEqual)
500 result = build1 (TRUTH_NOT_EXPR, build_ctype (e->type), result);
501
502 this->result_ = result;
503 }
504 else
505 {
506 /* For operands of other types, equality is defined as the bit pattern
507 of the type matches exactly. */
508 tree t1 = build_expr (e->e1);
509 tree t2 = build_expr (e->e2);
510
511 this->result_ = d_convert (build_ctype (e->type),
512 build_boolop (code, t1, t2));
513 }
514 }
515
516 /* Build an `in' expression. This is a condition to see if an element
517 exists in an associative array. The result is a pointer to the
518 element, or null if false. */
519
520 void visit (InExp *e) final override
521 {
522 Type *tb2 = e->e2->type->toBasetype ();
523 Type *tkey = tb2->isTypeAArray ()->index->toBasetype ();
524 tree key = convert_expr (build_expr (e->e1), e->e1->type, tkey);
525
526 /* Build a call to _aaInX(). */
527 this->result_ = build_libcall (LIBCALL_AAINX, e->type, 3,
528 build_expr (e->e2),
529 build_typeinfo (e, tkey),
530 build_address (key));
531 }
532
533 /* Build a relational expression. The result type is bool. */
534
535 void visit (CmpExp *e) final override
536 {
537 Type *tb1 = e->e1->type->toBasetype ();
538 Type *tb2 = e->e2->type->toBasetype ();
539
540 tree result;
541 tree_code code;
542
543 switch (e->op)
544 {
545 case EXP::lessOrEqual:
546 code = LE_EXPR;
547 break;
548
549 case EXP::lessThan:
550 code = LT_EXPR;
551 break;
552
553 case EXP::greaterOrEqual:
554 code = GE_EXPR;
555 break;
556
557 case EXP::greaterThan:
558 code = GT_EXPR;
559 break;
560
561 default:
562 gcc_unreachable ();
563 }
564
565 /* For static and dynamic arrays, the relational op is turned into a
566 library call. It is not lowered during codegen. */
567 if ((tb1->ty == TY::Tsarray || tb1->ty == TY::Tarray)
568 && (tb2->ty == TY::Tsarray || tb2->ty == TY::Tarray))
569 {
570 error ("cannot handle comparison of type %<%s == %s%>",
571 tb1->toChars (), tb2->toChars ());
572 gcc_unreachable ();
573 }
574
575 /* Simple comparison. */
576 result = build_boolop (code, build_expr (e->e1), build_expr (e->e2));
577 this->result_ = d_convert (build_ctype (e->type), result);
578 }
579
580 /* Build a logical `and if' or `or if' expression. If the right operand
581 expression is void, then the resulting type is void. Otherwise the
582 result is bool. */
583
584 void visit (LogicalExp *e) final override
585 {
586 tree_code code = (e->op == EXP::andAnd) ? TRUTH_ANDIF_EXPR : TRUTH_ORIF_EXPR;
587
588 if (e->e2->type->toBasetype ()->ty != TY::Tvoid)
589 {
590 tree t1 = build_expr (e->e1);
591 tree t2 = build_expr (e->e2);
592
593 t1 = convert_for_condition (t1, e->e1->type);
594 t2 = convert_for_condition (t2, e->e2->type);
595
596 this->result_ = d_convert (build_ctype (e->type),
597 build_boolop (code, t1, t2));
598 }
599 else
600 {
601 tree t1 = convert_for_condition (build_expr (e->e1), e->e1->type);
602 tree t2 = build_expr_dtor (e->e2);
603
604 /* Invert condition for logical or if expression. */
605 if (e->op == EXP::orOr)
606 t1 = build1 (TRUTH_NOT_EXPR, d_bool_type, t1);
607
608 this->result_ = build_condition (build_ctype (e->type),
609 t1, t2, void_node);
610 }
611 }
612
613 /* Build a binary operand expression. Operands go through usual arithmetic
614 conversions to bring them to a common type before evaluating. */
615
616 void visit (BinExp *e) final override
617 {
618 tree_code code;
619
620 switch (e->op)
621 {
622 case EXP::add:
623 case EXP::min:
624 if ((e->e1->type->isReal () && e->e2->type->isImaginary ())
625 || (e->e1->type->isImaginary () && e->e2->type->isReal ()))
626 {
627 /* If the result is complex, then we can shortcut binary_op.
628 Frontend should have already validated types and sizes. */
629 tree t1 = build_expr (e->e1);
630 tree t2 = build_expr (e->e2);
631
632 if (e->op == EXP::min)
633 t2 = build1 (NEGATE_EXPR, TREE_TYPE (t2), t2);
634
635 if (e->e1->type->isReal ())
636 this->result_ = complex_expr (build_ctype (e->type), t1, t2);
637 else
638 this->result_ = complex_expr (build_ctype (e->type), t2, t1);
639
640 return;
641 }
642 else
643 code = (e->op == EXP::add)
644 ? PLUS_EXPR : MINUS_EXPR;
645 break;
646
647 case EXP::mul:
648 code = MULT_EXPR;
649 break;
650
651 case EXP::div:
652 /* Determine if the div expression is a lowered pointer diff operation.
653 The front-end rewrites `(p1 - p2)' into `(p1 - p2) / stride'. */
654 if (MinExp *me = e->e1->isMinExp ())
655 {
656 if (me->e1->type->ty == TY::Tpointer
657 && me->e2->type->ty == TY::Tpointer
658 && e->e2->op == EXP::int64)
659 {
660 code = EXACT_DIV_EXPR;
661 break;
662 }
663 }
664
665 code = e->e1->type->isIntegral ()
666 ? TRUNC_DIV_EXPR : RDIV_EXPR;
667 break;
668
669 case EXP::mod:
670 code = e->e1->type->isFloating ()
671 ? FLOAT_MOD_EXPR : TRUNC_MOD_EXPR;
672 break;
673
674 case EXP::and_:
675 code = BIT_AND_EXPR;
676 break;
677
678 case EXP::or_:
679 code = BIT_IOR_EXPR;
680 break;
681
682 case EXP::xor_:
683 code = BIT_XOR_EXPR;
684 break;
685
686 case EXP::leftShift:
687 code = LSHIFT_EXPR;
688 break;
689
690 case EXP::rightShift:
691 code = RSHIFT_EXPR;
692 break;
693
694 case EXP::unsignedRightShift:
695 code = UNSIGNED_RSHIFT_EXPR;
696 break;
697
698 default:
699 gcc_unreachable ();
700 }
701
702 this->result_ = binary_op (code, build_ctype (e->type),
703 build_expr (e->e1), build_expr (e->e2));
704 }
705
706
707 /* Build a concat expression, which concatenates two or more arrays of the
708 same type, producing a dynamic array with the result. If one operand
709 is an element type, that element is converted to an array of length 1. */
710
711 void visit (CatExp *e) final override
712 {
713 /* This error is only emitted during the code generation pass because
714 concatentation is allowed in CTFE. */
715 if (!global.params.useGC)
716 {
717 error_at (make_location_t (e->loc),
718 "array concatenation of expression %qs requires the GC and "
719 "cannot be used with %<-fno-druntime%>", e->toChars ());
720 this->result_ = error_mark_node;
721 return;
722 }
723
724 /* All concat expressions should have been rewritten to `_d_arraycatnTX` in
725 the semantic phase. */
726 gcc_assert (e->lowering);
727
728 this->result_ = build_expr (e->lowering);
729 }
730
731 /* Build an assignment operator expression. The right operand is implicitly
732 converted to the type of the left operand, and assigned to it. */
733
734 void visit (BinAssignExp *e) final override
735 {
736 tree_code code;
737 Expression *e1b = e->e1;
738
739 switch (e->op)
740 {
741 case EXP::addAssign:
742 code = PLUS_EXPR;
743 break;
744
745 case EXP::minAssign:
746 code = MINUS_EXPR;
747 break;
748
749 case EXP::mulAssign:
750 code = MULT_EXPR;
751 break;
752
753 case EXP::divAssign:
754 code = e->e1->type->isIntegral ()
755 ? TRUNC_DIV_EXPR : RDIV_EXPR;
756 break;
757
758 case EXP::modAssign:
759 code = e->e1->type->isFloating ()
760 ? FLOAT_MOD_EXPR : TRUNC_MOD_EXPR;
761 break;
762
763 case EXP::andAssign:
764 code = BIT_AND_EXPR;
765 break;
766
767 case EXP::orAssign:
768 code = BIT_IOR_EXPR;
769 break;
770
771 case EXP::xorAssign:
772 code = BIT_XOR_EXPR;
773 break;
774
775 case EXP::powAssign:
776 gcc_unreachable ();
777
778 case EXP::leftShiftAssign:
779 code = LSHIFT_EXPR;
780 break;
781
782 case EXP::rightShiftAssign:
783 case EXP::unsignedRightShiftAssign:
784 /* Use the original lhs type before it was promoted. The left operand
785 of `>>>=' does not undergo integral promotions before shifting.
786 Strip off casts just incase anyway. */
787 while (e1b->op == EXP::cast_)
788 {
789 CastExp *ce = e1b->isCastExp ();
790 gcc_assert (same_type_p (ce->type, ce->to));
791 e1b = ce->e1;
792 }
793 code = (e->op == EXP::rightShiftAssign) ? RSHIFT_EXPR : UNSIGNED_RSHIFT_EXPR;
794 break;
795
796 default:
797 gcc_unreachable ();
798 }
799
800 tree exp = binop_assignment (code, e1b, e->e2);
801 this->result_ = convert_expr (exp, e1b->type, e->type);
802 }
803
804 /* Build a concat assignment expression. The right operand is appended
805 to the left operand. */
806
807 void visit (CatAssignExp *e) final override
808 {
809 if (!global.params.useGC)
810 {
811 error_at (make_location_t (e->loc),
812 "appending to array in %qs requires the GC and cannot be "
813 "used with %<-fno-druntime%>", e->toChars ());
814 this->result_ = error_mark_node;
815 return;
816 }
817
818 Type *tb1 = e->e1->type->toBasetype ();
819 Type *tb2 = e->e2->type->toBasetype ();
820
821 if (e->op == EXP::concatenateDcharAssign)
822 {
823 /* Append a dchar to a char[] or wchar[]:
824 The assignment is handled by the D run-time library, so only
825 need to call `_d_arrayappend[cw]d(&e1, e2)' */
826 Type *etype = tb1->nextOf ()->toBasetype ();
827
828 /* Save the address of `e1', so it can be evaluated first.
829 As all D run-time library functions for concat assignments update
830 `e1' in-place and then return its value, the saved address can also
831 be used as the result of this expression as well. */
832 tree lhs = build_expr (e->e1);
833 tree lexpr = stabilize_expr (&lhs);
834 tree ptr = d_save_expr (build_address (lhs));
835 tree result = NULL_TREE;
836
837 gcc_assert (tb1->ty == TY::Tarray && tb2->ty == TY::Tdchar
838 && (etype->ty == TY::Tchar || etype->ty == TY::Twchar));
839
840 libcall_fn libcall = (etype->ty == TY::Tchar)
841 ? LIBCALL_ARRAYAPPENDCD : LIBCALL_ARRAYAPPENDWD;
842
843 result = build_libcall (libcall, e->type, 2,
844 ptr, build_expr (e->e2));
845
846 /* Construct in order: ptr = &e1, _d_arrayappend(ptr, e2), *ptr; */
847 result = compound_expr (compound_expr (lexpr, ptr), result);
848 this->result_ = compound_expr (result, build_deref (ptr));
849 }
850 else
851 {
852 gcc_assert (e->op == EXP::concatenateAssign
853 || e->op == EXP::concatenateElemAssign);
854 gcc_assert (tb1->ty == TY::Tarray || tb2->ty == TY::Tsarray);
855 /* Appending an element or array to another array has already been
856 handled by the front-end. */
857 gcc_assert (e->lowering);
858
859 this->result_ = build_expr (e->lowering);
860 }
861 }
862
863 /* Build an assignment expression. The right operand is implicitly
864 converted to the type of the left operand, and assigned to it. */
865
866 void visit (AssignExp *e) final override
867 {
868 /* First, handle special assignment semantics. */
869
870 /* Look for array.length = n; */
871 if (e->e1->op == EXP::arrayLength)
872 {
873 /* This case should have been rewritten to `_d_arraysetlengthT` in the
874 semantic phase. */
875 gcc_unreachable ();
876 }
877
878 /* Look for exp = noreturn; */
879 if (e->e2->type->isTypeNoreturn ())
880 {
881 /* If the RHS is a `noreturn' expression, there is no point generating
882 any code for the assignment, just evaluate side effects. */
883 tree t1 = build_expr (e->e1);
884 tree t2 = build_expr (e->e2);
885 this->result_ = compound_expr (t1, t2);
886 return;
887 }
888
889 /* Look for array[] = n; */
890 if (e->e1->op == EXP::slice)
891 {
892 SliceExp *se = e->e1->isSliceExp ();
893 Type *stype = se->e1->type->toBasetype ();
894 Type *etype = stype->nextOf ()->toBasetype ();
895
896 /* Determine if we need to run postblit or dtor. */
897 bool postblit = needs_postblit (etype) && lvalue_p (e->e2);
898 bool destructor = needs_dtor (etype);
899
900 if (e->memset == MemorySet::blockAssign)
901 {
902 /* Set a range of elements to one value. */
903 tree t1 = build_expr (e->e1);
904 tree t2 = build_expr (e->e2);
905 tree result;
906
907 /* Extract any array bounds checks from the slice expression. */
908 tree init = stabilize_expr (&t1);
909 t1 = d_save_expr (t1);
910
911 if ((postblit || destructor) && e->op != EXP::blit)
912 {
913 /* This case should have been rewritten to `_d_arraysetassign`
914 in the semantic phase. */
915 gcc_unreachable ();
916 }
917
918 if (integer_zerop (t2))
919 {
920 tree size = size_mult_expr (d_array_length (t1),
921 size_int (dmd::size (etype)));
922 result = build_memset_call (d_array_ptr (t1), size);
923 }
924 else
925 result = build_array_set (d_array_ptr (t1),
926 d_array_length (t1), t2);
927
928 result = compound_expr (init, result);
929 this->result_ = compound_expr (result, t1);
930 }
931 else
932 {
933 /* Perform a memcpy operation. */
934 gcc_assert (e->e2->type->ty != TY::Tpointer);
935
936 if (!postblit && !destructor)
937 {
938 tree t1 = d_save_expr (d_array_convert (e->e1));
939 tree t2 = d_save_expr (d_array_convert (e->e2));
940
941 /* References to array data. */
942 tree t1ptr = d_array_ptr (t1);
943 tree t1len = d_array_length (t1);
944 tree t2ptr = d_array_ptr (t2);
945
946 /* Generate: memcpy(to, from, size) */
947 tree size =
948 size_mult_expr (t1len, size_int (dmd::size (etype)));
949 tree result = build_memcpy_call (t1ptr, t2ptr, size);
950
951 /* Insert check that array lengths match and do not overlap. */
952 if (array_bounds_check ())
953 {
954 /* tlencmp = (t1len == t2len) */
955 tree t2len = d_array_length (t2);
956 tree tlencmp = build_boolop (EQ_EXPR, t1len, t2len);
957
958 /* toverlap = (t1ptr + size <= t2ptr
959 || t2ptr + size <= t1ptr) */
960 tree t1ptrcmp = build_boolop (LE_EXPR,
961 build_offset (t1ptr, size),
962 t2ptr);
963 tree t2ptrcmp = build_boolop (LE_EXPR,
964 build_offset (t2ptr, size),
965 t1ptr);
966 tree toverlap = build_boolop (TRUTH_ORIF_EXPR, t1ptrcmp,
967 t2ptrcmp);
968
969 /* (tlencmp && toverlap) ? memcpy() : _d_arraybounds() */
970 tree tassert = build_array_bounds_call (e->loc);
971 tree tboundscheck = build_boolop (TRUTH_ANDIF_EXPR,
972 tlencmp, toverlap);
973
974 result = build_condition (void_type_node, tboundscheck,
975 result, tassert);
976 }
977
978 this->result_ = compound_expr (result, t1);
979 }
980 else if ((postblit || destructor)
981 && e->op != EXP::blit && e->op != EXP::construct)
982 {
983 /* Assigning to a non-trivially copyable array has already been
984 handled by the front-end. */
985 gcc_unreachable ();
986 }
987 else
988 {
989 /* Generate: _d_arraycopy() */
990 this->result_ = build_libcall (LIBCALL_ARRAYCOPY, e->type, 3,
991 size_int (dmd::size (etype)),
992 d_array_convert (e->e2),
993 d_array_convert (e->e1));
994 }
995 }
996
997 return;
998 }
999
1000 /* Look for reference initializations. */
1001 if (e->memset == MemorySet::referenceInit)
1002 {
1003 gcc_assert (e->op == EXP::construct || e->op == EXP::blit);
1004 gcc_assert (e->e1->op == EXP::variable);
1005
1006 Declaration *decl = e->e1->isVarExp ()->var;
1007 if (decl->storage_class & (STCout | STCref))
1008 {
1009 tree t2 = convert_for_assignment (e->e2, e->e1->type);
1010 tree t1 = build_expr (e->e1);
1011 /* Want reference to lhs, not indirect ref. */
1012 t1 = TREE_OPERAND (t1, 0);
1013 t2 = build_address (t2);
1014
1015 this->result_ = indirect_ref (build_ctype (e->type),
1016 build_assign (INIT_EXPR, t1, t2));
1017 return;
1018 }
1019 }
1020
1021 /* Other types of assignments that may require post construction. */
1022 Type *tb1 = e->e1->type->toBasetype ();
1023 tree_code modifycode = (e->op == EXP::construct) ? INIT_EXPR : MODIFY_EXPR;
1024
1025 /* Look for struct assignment. */
1026 if (tb1->ty == TY::Tstruct)
1027 {
1028 tree t1 = build_expr (e->e1);
1029 tree t2 = convert_for_assignment (e->e2, e->e1->type, true);
1030 StructDeclaration *sd = tb1->isTypeStruct ()->sym;
1031
1032 /* Look for struct = 0. */
1033 if (e->e2->op == EXP::int64)
1034 {
1035 /* Use memset to fill struct. */
1036 gcc_assert (e->op == EXP::blit);
1037 tree result = build_memset_call (t1);
1038
1039 /* Maybe set-up hidden pointer to outer scope context. */
1040 if (sd->isNested ())
1041 {
1042 tree field = get_symbol_decl (sd->vthis);
1043 tree value = build_vthis (sd);
1044
1045 tree vthis_exp = modify_expr (component_ref (t1, field), value);
1046 result = compound_expr (result, vthis_exp);
1047 }
1048
1049 this->result_ = compound_expr (result, t1);
1050 }
1051 else
1052 {
1053 /* Simple struct literal assignment. */
1054 tree init = NULL_TREE;
1055
1056 /* Fill any alignment holes in the struct using memset. */
1057 if ((e->op == EXP::construct
1058 || (e->e2->op == EXP::structLiteral && e->op == EXP::blit))
1059 && (sd->isUnionDeclaration () || !identity_compare_p (sd)))
1060 {
1061 t1 = stabilize_reference (t1);
1062 init = build_memset_call (t1);
1063 }
1064
1065 /* Elide generating assignment if init is all zeroes. */
1066 if (init != NULL_TREE && initializer_zerop (t2))
1067 this->result_ = compound_expr (init, t1);
1068 else
1069 {
1070 tree result = build_assign (modifycode, t1, t2);
1071 this->result_ = compound_expr (init, result);
1072 }
1073 }
1074
1075 return;
1076 }
1077
1078 /* Look for static array assignment. */
1079 if (tb1->ty == TY::Tsarray)
1080 {
1081 /* Look for array = 0. */
1082 if (e->e2->op == EXP::int64)
1083 {
1084 /* Use memset to fill the array. */
1085 gcc_assert (e->op == EXP::blit);
1086 this->result_ = build_memset_call (build_expr (e->e1));
1087 return;
1088 }
1089
1090 Type *etype = tb1->nextOf ();
1091 gcc_assert (e->e2->type->toBasetype ()->ty == TY::Tsarray);
1092
1093 /* Determine if we need to run postblit. */
1094 const bool postblit = needs_postblit (etype);
1095 const bool destructor = needs_dtor (etype);
1096 const bool lvalue = lvalue_p (e->e2);
1097
1098 /* Optimize static array assignment with array literal. Even if the
1099 elements in rhs are all rvalues and don't have to call postblits,
1100 this assignment should call dtors on old assigned elements. */
1101 if ((!postblit && !destructor)
1102 || (e->op == EXP::construct && e->e2->op == EXP::arrayLiteral)
1103 || (e->op == EXP::construct && e->e2->op == EXP::call)
1104 || (e->op == EXP::construct && !lvalue && postblit)
1105 || (e->op == EXP::blit || dmd::size (e->e1->type) == 0))
1106 {
1107 tree t1 = build_expr (e->e1);
1108 tree t2 = convert_for_assignment (e->e2, e->e1->type);
1109
1110 this->result_ = build_assign (modifycode, t1, t2);
1111 return;
1112 }
1113
1114 /* All other kinds of lvalue or rvalue static array assignment.
1115 Array construction has already been handled by the front-end. */
1116 gcc_assert (e->op != EXP::construct);
1117 gcc_unreachable ();
1118 }
1119
1120 /* Simple assignment. */
1121 tree t1 = build_expr (e->e1);
1122 tree t2 = convert_for_assignment (e->e2, e->e1->type);
1123
1124 this->result_ = build_assign (modifycode, t1, t2);
1125 }
1126
1127 /* Build an assignment expression that has been lowered in the front-end. */
1128
1129 void visit (LoweredAssignExp *e) final override
1130 {
1131 this->result_ = build_expr (e->lowering);
1132 }
1133
1134 /* Build a throw expression. */
1135
1136 void visit (ThrowExp *e) final override
1137 {
1138 tree arg = build_expr_dtor (e->e1);
1139 this->result_ = build_libcall (LIBCALL_THROW, Type::tvoid, 1, arg);
1140 }
1141
1142 /* Build a postfix expression. */
1143
1144 void visit (PostExp *e) final override
1145 {
1146 tree result;
1147
1148 if (e->op == EXP::plusPlus)
1149 {
1150 result = build2 (POSTINCREMENT_EXPR, build_ctype (e->type),
1151 build_expr (e->e1), build_expr (e->e2));
1152 }
1153 else if (e->op == EXP::minusMinus)
1154 {
1155 result = build2 (POSTDECREMENT_EXPR, build_ctype (e->type),
1156 build_expr (e->e1), build_expr (e->e2));
1157 }
1158 else
1159 gcc_unreachable ();
1160
1161 TREE_SIDE_EFFECTS (result) = 1;
1162 this->result_ = result;
1163 }
1164
1165 /* Build an index expression. */
1166
1167 void visit (IndexExp *e) final override
1168 {
1169 Type *tb1 = e->e1->type->toBasetype ();
1170
1171 if (tb1->ty == TY::Taarray)
1172 {
1173 /* Get the key for the associative array. */
1174 Type *tkey = tb1->isTypeAArray ()->index->toBasetype ();
1175 tree key = convert_expr (build_expr (e->e2), e->e2->type, tkey);
1176 libcall_fn libcall;
1177 tree tinfo, ptr;
1178
1179 if (e->modifiable)
1180 {
1181 libcall = LIBCALL_AAGETY;
1182 ptr = build_address (build_expr (e->e1));
1183 tinfo = build_typeinfo (e, dmd::mutableOf (dmd::unSharedOf (tb1)));
1184 }
1185 else
1186 {
1187 libcall = LIBCALL_AAGETRVALUEX;
1188 ptr = build_expr (e->e1);
1189 tinfo = build_typeinfo (e, tkey);
1190 }
1191
1192 /* Index the associative array. */
1193 tree result = build_libcall (libcall, dmd::pointerTo (e->type), 4,
1194 ptr, tinfo,
1195 size_int (dmd::size (tb1->nextOf ())),
1196 build_address (key));
1197
1198 if (!e->indexIsInBounds && array_bounds_check ())
1199 {
1200 tree tassert = build_array_bounds_call (e->loc);
1201
1202 result = d_save_expr (result);
1203 result = build_condition (TREE_TYPE (result),
1204 d_truthvalue_conversion (result),
1205 result, tassert);
1206 }
1207
1208 this->result_ = indirect_ref (build_ctype (e->type), result);
1209 }
1210 else
1211 {
1212 /* Get the array and length for static and dynamic arrays. */
1213 tree array = d_save_expr (build_expr (e->e1));
1214
1215 tree length = NULL_TREE;
1216 if (tb1->ty != TY::Tpointer)
1217 length = get_array_length (array, tb1);
1218 else
1219 gcc_assert (e->lengthVar == NULL);
1220
1221 /* The __dollar variable just becomes a placeholder for the
1222 actual length. */
1223 if (e->lengthVar)
1224 e->lengthVar->csym = length;
1225
1226 /* Generate the index. */
1227 tree index = build_expr (e->e2);
1228
1229 /* If it's a static array and the index is constant, the front end has
1230 already checked the bounds. */
1231 if (tb1->ty != TY::Tpointer)
1232 index = build_bounds_index_condition (e, index, length);
1233
1234 /* Convert vectors to their underlying array type. */
1235 if (VECTOR_TYPE_P (TREE_TYPE (array)))
1236 {
1237 tree array_type =
1238 build_array_type_nelts (TREE_TYPE (TREE_TYPE (array)),
1239 TYPE_VECTOR_SUBPARTS (TREE_TYPE (array)));
1240 d_mark_addressable (array, false);
1241 array = build1 (VIEW_CONVERT_EXPR, array_type, array);
1242 }
1243
1244 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
1245 {
1246 /* Generate `array[index]'. When the index is non-constant, we must
1247 mark the array as addressable because we'll need to do pointer
1248 arithmetic on its address. */
1249 if (TREE_CODE (index) != INTEGER_CST)
1250 d_mark_addressable (array);
1251
1252 this->result_ = build4 (ARRAY_REF, TREE_TYPE (TREE_TYPE (array)),
1253 array, index, NULL_TREE, NULL_TREE);
1254 }
1255 else
1256 {
1257 /* Generate `array.ptr[index]'. */
1258 tree ptr = convert_expr (array, tb1,
1259 dmd::pointerTo (tb1->nextOf ()));
1260 ptr = void_okay_p (ptr);
1261 this->result_ = indirect_ref (TREE_TYPE (TREE_TYPE (ptr)),
1262 build_pointer_index (ptr, index));
1263 }
1264 }
1265 }
1266
1267 /* Build a comma expression. The type is the type of the right operand. */
1268
1269 void visit (CommaExp *e) final override
1270 {
1271 tree t1 = build_expr (e->e1);
1272 tree t2 = build_expr (e->e2);
1273 tree type = e->type ? build_ctype (e->type) : void_type_node;
1274
1275 this->result_ = build2 (COMPOUND_EXPR, type, t1, t2);
1276 }
1277
1278 /* Build an array length expression. Returns the number of elements
1279 in the array. The result is of type size_t. */
1280
1281 void visit (ArrayLengthExp *e) final override
1282 {
1283 if (e->e1->type->toBasetype ()->ty == TY::Tarray)
1284 this->result_ = d_array_length (build_expr (e->e1));
1285 else
1286 {
1287 /* Static arrays have already been handled by the front-end. */
1288 error ("unexpected type for array length: %qs", e->type->toChars ());
1289 this->result_ = error_mark_node;
1290 }
1291 }
1292
1293 /* Build a delegate pointer expression. This will return the frame
1294 pointer value as a type void*. */
1295
1296 void visit (DelegatePtrExp *e) final override
1297 {
1298 tree t1 = build_expr (e->e1);
1299 this->result_ = delegate_object (t1);
1300 }
1301
1302 /* Build a delegate function pointer expression. This will return the
1303 function pointer value as a function type. */
1304
1305 void visit (DelegateFuncptrExp *e) final override
1306 {
1307 tree t1 = build_expr (e->e1);
1308 this->result_ = delegate_method (t1);
1309 }
1310
1311 /* Build a slice expression. */
1312
1313 void visit (SliceExp *e) final override
1314 {
1315 Type *tb = e->type->toBasetype ();
1316 Type *tb1 = e->e1->type->toBasetype ();
1317 gcc_assert (tb->ty == TY::Tarray || tb->ty == TY::Tsarray);
1318
1319 /* Use convert-to-dynamic-array code if possible. */
1320 if (!e->lwr)
1321 {
1322 tree result = build_expr (e->e1);
1323 if (e->e1->type->toBasetype ()->ty == TY::Tsarray)
1324 result = convert_expr (result, e->e1->type, e->type);
1325
1326 this->result_ = result;
1327 return;
1328 }
1329 else
1330 gcc_assert (e->upr != NULL);
1331
1332 /* Get the data pointer and length for static and dynamic arrays. */
1333 tree array = d_save_expr (build_expr (e->e1));
1334 tree ptr = convert_expr (array, tb1, dmd::pointerTo (tb1->nextOf ()));
1335 tree length = NULL_TREE;
1336
1337 /* Our array is already a SAVE_EXPR if necessary, so we don't make length
1338 a SAVE_EXPR which is, at most, a COMPONENT_REF on top of array. */
1339 if (tb1->ty != TY::Tpointer)
1340 length = get_array_length (array, tb1);
1341 else
1342 gcc_assert (e->lengthVar == NULL);
1343
1344 /* The __dollar variable just becomes a placeholder for the
1345 actual length. */
1346 if (e->lengthVar)
1347 e->lengthVar->csym = length;
1348
1349 /* Generate upper and lower bounds. */
1350 tree lwr_tree = d_save_expr (build_expr (e->lwr));
1351 tree upr_tree = d_save_expr (build_expr (e->upr));
1352
1353 /* If the upper bound has any side effects, then the lower bound should be
1354 copied to a temporary always. */
1355 if (TREE_CODE (upr_tree) == SAVE_EXPR && TREE_CODE (lwr_tree) != SAVE_EXPR)
1356 lwr_tree = save_expr (lwr_tree);
1357
1358 /* Adjust the .ptr offset. */
1359 if (!integer_zerop (lwr_tree))
1360 {
1361 tree ptrtype = TREE_TYPE (ptr);
1362 ptr = build_pointer_index (void_okay_p (ptr), lwr_tree);
1363 ptr = build_nop (ptrtype, ptr);
1364 }
1365
1366 /* Nothing more to do for static arrays, their bounds checking has been
1367 done at compile-time. */
1368 if (tb->ty == TY::Tsarray)
1369 {
1370 this->result_ = indirect_ref (build_ctype (e->type), ptr);
1371 return;
1372 }
1373 else
1374 gcc_assert (tb->ty == TY::Tarray);
1375
1376 /* Generate bounds checking code. */
1377 tree newlength = build_bounds_slice_condition (e, lwr_tree, upr_tree,
1378 length);
1379 tree result = d_array_value (build_ctype (e->type), newlength, ptr);
1380 this->result_ = compound_expr (array, result);
1381 }
1382
1383 /* Build a cast expression, which converts the given unary expression to the
1384 type of result. */
1385
1386 void visit (CastExp *e) final override
1387 {
1388 Type *ebtype = e->e1->type->toBasetype ();
1389 Type *tbtype = e->to->toBasetype ();
1390 tree result = build_expr (e->e1, this->constp_, this->literalp_);
1391
1392 /* Just evaluate e1 if it has any side effects. */
1393 if (tbtype->ty == TY::Tvoid)
1394 this->result_ = build_nop (build_ctype (tbtype), result);
1395 else
1396 this->result_ = convert_for_rvalue (result, ebtype, tbtype);
1397 }
1398
1399 /* Build a delete expression. */
1400
1401 void visit (DeleteExp *e) final override
1402 {
1403 tree t1 = build_expr (e->e1);
1404 Type *tb1 = e->e1->type->toBasetype ();
1405
1406 if (tb1->ty == TY::Tclass)
1407 {
1408 /* For class object references, if there is a destructor for that class,
1409 the destructor is called for the object instance. */
1410 gcc_assert (e->e1->op == EXP::variable);
1411
1412 VarDeclaration *v = e->e1->isVarExp ()->var->isVarDeclaration ();
1413 gcc_assert (v && v->onstack ());
1414
1415 libcall_fn libcall = tb1->isClassHandle ()->isInterfaceDeclaration ()
1416 ? LIBCALL_CALLINTERFACEFINALIZER : LIBCALL_CALLFINALIZER;
1417
1418 this->result_ = build_libcall (libcall, Type::tvoid, 1, t1);
1419 return;
1420 }
1421 else
1422 {
1423 error ("don%'t know how to delete %qs", e->e1->toChars ());
1424 this->result_ = error_mark_node;
1425 }
1426 }
1427
1428 /* Build a remove expression, which removes a particular key from an
1429 associative array. */
1430
1431 void visit (RemoveExp *e) final override
1432 {
1433 /* Check that the array is actually an associative array. */
1434 if (e->e1->type->toBasetype ()->ty == TY::Taarray)
1435 {
1436 Type *tb = e->e1->type->toBasetype ();
1437 Type *tkey = tb->isTypeAArray ()->index->toBasetype ();
1438 tree index = convert_expr (build_expr (e->e2), e->e2->type, tkey);
1439
1440 this->result_ = build_libcall (LIBCALL_AADELX, Type::tbool, 3,
1441 build_expr (e->e1),
1442 build_typeinfo (e, tkey),
1443 build_address (index));
1444 }
1445 else
1446 {
1447 error ("%qs is not an associative array", e->e1->toChars ());
1448 this->result_ = error_mark_node;
1449 }
1450 }
1451
1452 /* Build an unary not expression. */
1453
1454 void visit (NotExp *e) final override
1455 {
1456 tree result = convert_for_condition (build_expr (e->e1), e->e1->type);
1457 /* Need to convert to boolean type or this will fail. */
1458 result = fold_build1 (TRUTH_NOT_EXPR, d_bool_type, result);
1459
1460 this->result_ = d_convert (build_ctype (e->type), result);
1461 }
1462
1463 /* Build a compliment expression, where all the bits in the value are
1464 complemented. Note: unlike in C, the usual integral promotions
1465 are not performed prior to the complement operation. */
1466
1467 void visit (ComExp *e) final override
1468 {
1469 TY ty1 = e->e1->type->toBasetype ()->ty;
1470 gcc_assert (ty1 != TY::Tarray && ty1 != TY::Tsarray);
1471
1472 this->result_ = fold_build1 (BIT_NOT_EXPR, build_ctype (e->type),
1473 build_expr (e->e1));
1474 }
1475
1476 /* Build an unary negation expression. */
1477
1478 void visit (NegExp *e) final override
1479 {
1480 TY ty1 = e->e1->type->toBasetype ()->ty;
1481 gcc_assert (ty1 != TY::Tarray && ty1 != TY::Tsarray);
1482
1483 tree type = build_ctype (e->type);
1484 tree expr = build_expr (e->e1);
1485
1486 /* If the operation needs excess precision. */
1487 tree eptype = excess_precision_type (type);
1488 if (eptype != NULL_TREE)
1489 expr = d_convert (eptype, expr);
1490 else
1491 eptype = type;
1492
1493 tree ret = fold_build1 (NEGATE_EXPR, eptype, expr);
1494 this->result_ = d_convert (type, ret);
1495 }
1496
1497 /* Build a pointer index expression. */
1498
1499 void visit (PtrExp *e) final override
1500 {
1501 Type *tnext = NULL;
1502 dinteger_t offset;
1503 tree result;
1504
1505 if (e->e1->op == EXP::add)
1506 {
1507 AddExp *ae = e->e1->isAddExp ();
1508 if (ae->e1->op == EXP::address
1509 && ae->e2->isConst () && ae->e2->type->isIntegral ())
1510 {
1511 Expression *ex = ae->e1->isAddrExp ()->e1;
1512 tnext = ex->type->toBasetype ();
1513 result = build_expr (ex);
1514 offset = ae->e2->toUInteger ();
1515 }
1516 }
1517 else if (e->e1->op == EXP::symbolOffset)
1518 {
1519 SymOffExp *se = e->e1->isSymOffExp ();
1520 if (!declaration_reference_p (se->var))
1521 {
1522 tnext = se->var->type->toBasetype ();
1523 result = get_decl_tree (se->var);
1524 offset = se->offset;
1525 }
1526 }
1527
1528 /* Produce better code by converting *(#record + n) to
1529 COMPONENT_REFERENCE. Otherwise, the variable will always be
1530 allocated in memory because its address is taken. */
1531 if (tnext && tnext->ty == TY::Tstruct)
1532 {
1533 StructDeclaration *sd = tnext->isTypeStruct ()->sym;
1534
1535 for (size_t i = 0; i < sd->fields.length; i++)
1536 {
1537 VarDeclaration *field = sd->fields[i];
1538
1539 if (field->offset == offset
1540 && same_type_p (field->type, e->type))
1541 {
1542 /* Catch errors, backend will ICE otherwise. */
1543 if (error_operand_p (result))
1544 this->result_ = result;
1545 else
1546 {
1547 result = component_ref (result, get_symbol_decl (field));
1548 this->result_ = result;
1549 }
1550 return;
1551 }
1552 else if (field->offset > offset)
1553 break;
1554 }
1555 }
1556
1557 this->result_ = indirect_ref (build_ctype (e->type), build_expr (e->e1));
1558 }
1559
1560 /* Build an unary address expression. */
1561
1562 void visit (AddrExp *e) final override
1563 {
1564 tree type = build_ctype (e->type);
1565 tree exp;
1566
1567 /* The frontend optimizer can convert const symbol into a struct literal.
1568 Taking the address of a struct literal is otherwise illegal. */
1569 if (e->e1->op == EXP::structLiteral)
1570 {
1571 StructLiteralExp *sle = e->e1->isStructLiteralExp ()->origin;
1572 gcc_assert (sle != NULL);
1573
1574 /* Build the reference symbol, the decl is built first as the
1575 initializer may have recursive references. */
1576 if (!sle->sym)
1577 {
1578 sle->sym = build_artificial_decl (build_ctype (sle->type),
1579 NULL_TREE, "S");
1580 DECL_INITIAL (sle->sym) = build_expr (sle, true);
1581 d_pushdecl (sle->sym);
1582 rest_of_decl_compilation (sle->sym, 1, 0);
1583 }
1584
1585 exp = sle->sym;
1586 }
1587 else
1588 exp = build_expr (e->e1, this->constp_, this->literalp_);
1589
1590 TREE_CONSTANT (exp) = 0;
1591 this->result_ = d_convert (type, build_address (exp));
1592 }
1593
1594 /* Build a function call expression. */
1595
1596 void visit (CallExp *e) final override
1597 {
1598 Type *tb = e->e1->type->toBasetype ();
1599 Expression *e1b = e->e1;
1600
1601 tree callee = NULL_TREE;
1602 tree object = NULL_TREE;
1603 tree cleanup = NULL_TREE;
1604 tree returnvalue = NULL_TREE;
1605 TypeFunction *tf = NULL;
1606
1607 /* Calls to delegates can sometimes look like this. */
1608 if (e1b->op == EXP::comma)
1609 {
1610 e1b = e1b->isCommaExp ()->e2;
1611 gcc_assert (e1b->op == EXP::variable);
1612
1613 Declaration *var = e1b->isVarExp ()->var;
1614 gcc_assert (var->isFuncDeclaration () && !var->needThis ());
1615 }
1616
1617 if (e1b->op == EXP::dotVariable && tb->ty != TY::Tdelegate)
1618 {
1619 DotVarExp *dve = e1b->isDotVarExp ();
1620
1621 /* Don't modify the static initializer for struct literals. */
1622 if (dve->e1->op == EXP::structLiteral)
1623 {
1624 StructLiteralExp *sle = dve->e1->isStructLiteralExp ();
1625 sle->useStaticInit (false);
1626 }
1627
1628 FuncDeclaration *fd = dve->var->isFuncDeclaration ();
1629 if (fd != NULL)
1630 {
1631 /* Get the correct callee from the DotVarExp object. */
1632 tree fndecl = get_symbol_decl (fd);
1633 AggregateDeclaration *ad = fd->isThis ();
1634
1635 /* Static method; ignore the object instance. */
1636 if (!ad)
1637 callee = build_address (fndecl);
1638 else
1639 {
1640 tree thisexp = build_expr (dve->e1);
1641
1642 /* When constructing temporaries, if the constructor throws,
1643 then the object is destructed even though it is not a fully
1644 constructed object yet. And so this call will need to be
1645 moved inside the TARGET_EXPR_INITIAL slot. */
1646 if (fd->isCtorDeclaration ()
1647 && TREE_CODE (thisexp) == COMPOUND_EXPR
1648 && TREE_CODE (TREE_OPERAND (thisexp, 0)) == TARGET_EXPR
1649 && TARGET_EXPR_CLEANUP (TREE_OPERAND (thisexp, 0)))
1650 {
1651 cleanup = TREE_OPERAND (thisexp, 0);
1652 thisexp = TREE_OPERAND (thisexp, 1);
1653 }
1654
1655 if (TREE_CODE (thisexp) == CONSTRUCTOR)
1656 thisexp = force_target_expr (thisexp);
1657
1658 /* Want reference to `this' object. */
1659 if (!POINTER_TYPE_P (TREE_TYPE (thisexp)))
1660 thisexp = build_address (thisexp);
1661
1662 /* Make the callee a virtual call. */
1663 if (fd->isVirtual () && !fd->isFinalFunc () && !e->directcall)
1664 {
1665 tree fntype = build_pointer_type (TREE_TYPE (fndecl));
1666 tree thistype = build_ctype (ad->handleType ());
1667 thisexp = build_nop (thistype, d_save_expr (thisexp));
1668 fndecl = build_vindex_ref (thisexp, fntype, fd->vtblIndex);
1669 }
1670 else
1671 fndecl = build_address (fndecl);
1672
1673 /* C++ constructors return void, even though front-end semantic
1674 treats them as implicitly returning `this'. Set returnvalue
1675 to override the result of this expression. */
1676 if (fd->isCtorDeclaration ())
1677 {
1678 thisexp = d_save_expr (thisexp);
1679 returnvalue = thisexp;
1680 }
1681
1682 callee = build_method_call (fndecl, thisexp, fd->type);
1683 }
1684 }
1685 }
1686
1687 if (callee == NULL_TREE)
1688 callee = build_expr (e1b);
1689
1690 if (METHOD_CALL_EXPR (callee))
1691 {
1692 /* This could be a delegate expression (TY == Tdelegate), but not
1693 actually a delegate variable. */
1694 if (e1b->op == EXP::dotVariable)
1695 {
1696 /* This gets the true function type, getting the function type
1697 from e1->type can sometimes be incorrect, such as when calling
1698 a `ref' return function. */
1699 tf = get_function_type (e1b->isDotVarExp ()->var->type);
1700 }
1701 else
1702 tf = get_function_type (tb);
1703
1704 extract_from_method_call (callee, callee, object);
1705 }
1706 else if (tb->ty == TY::Tdelegate)
1707 {
1708 /* Delegate call, extract .object and .funcptr from var. */
1709 callee = d_save_expr (callee);
1710 tf = get_function_type (tb);
1711 object = delegate_object (callee);
1712 callee = delegate_method (callee);
1713 }
1714 else if (e1b->op == EXP::variable)
1715 {
1716 FuncDeclaration *fd = e1b->isVarExp ()->var->isFuncDeclaration ();
1717 gcc_assert (fd != NULL);
1718 tf = get_function_type (fd->type);
1719
1720 if (fd->isNested ())
1721 {
1722 /* Maybe re-evaluate symbol storage treating `fd' as public. */
1723 if (call_by_alias_p (d_function_chain->function, fd))
1724 TREE_PUBLIC (callee) = 1;
1725
1726 object = get_frame_for_symbol (fd);
1727 }
1728 else if (fd->needThis ())
1729 {
1730 error_at (make_location_t (e1b->loc),
1731 "need %<this%> to access member %qs", fd->toChars ());
1732 /* Continue compiling... */
1733 object = null_pointer_node;
1734 }
1735 }
1736 else
1737 {
1738 /* Normal direct function call. */
1739 tf = get_function_type (tb);
1740 }
1741
1742 gcc_assert (tf != NULL);
1743
1744 /* Now we have the type, callee and maybe object reference,
1745 build the call expression. */
1746 tree exp = d_build_call (tf, callee, object, e->arguments);
1747
1748 /* Record whether the call expression has no side effects, so we can check
1749 for an unused return value later. */
1750 if (TREE_CODE (exp) == CALL_EXPR && CALL_EXPR_FN (exp) != NULL_TREE
1751 && call_side_effect_free_p (e->f, e->e1->type))
1752 CALL_EXPR_WARN_IF_UNUSED (exp) = 1;
1753
1754 if (returnvalue != NULL_TREE)
1755 exp = compound_expr (exp, returnvalue);
1756
1757 if (tf->isRef ())
1758 exp = build_deref (exp);
1759
1760 /* Some library calls are defined to return a generic type.
1761 this->type is the real type we want to return. */
1762 if (e->type->isTypeBasic ())
1763 exp = d_convert (build_ctype (e->type), exp);
1764
1765 /* If this call was found to be a constructor for a temporary with a
1766 cleanup, then move the call inside the TARGET_EXPR. */
1767 if (cleanup != NULL_TREE)
1768 {
1769 tree init = TARGET_EXPR_INITIAL (cleanup);
1770 TARGET_EXPR_INITIAL (cleanup) = compound_expr (init, exp);
1771
1772 /* Keep the return value outside the TARGET_EXPR. */
1773 if (returnvalue != NULL_TREE)
1774 cleanup = compound_expr (cleanup, TREE_OPERAND (exp, 1));
1775
1776 exp = cleanup;
1777 }
1778
1779 this->result_ = exp;
1780 }
1781
1782 /* Build a delegate expression. */
1783
1784 void visit (DelegateExp *e) final override
1785 {
1786 if (e->func->semanticRun () == PASS::semantic3done)
1787 {
1788 /* Add the function as nested function if it belongs to this module.
1789 ie: it is a member of this module, or it is a template instance. */
1790 Dsymbol *owner = e->func->toParent ();
1791 while (!owner->isTemplateInstance () && owner->toParent ())
1792 owner = owner->toParent ();
1793 if (owner->isTemplateInstance () || owner == d_function_chain->module)
1794 build_decl_tree (e->func);
1795 }
1796
1797 tree fndecl;
1798 tree object;
1799
1800 if (e->func->isNested () && !e->func->isThis ())
1801 {
1802 if (e->e1->op == EXP::null_)
1803 object = build_expr (e->e1);
1804 else
1805 object = get_frame_for_symbol (e->func);
1806
1807 fndecl = build_address (get_symbol_decl (e->func));
1808 }
1809 else
1810 {
1811 if (!e->func->isThis ())
1812 {
1813 error ("delegates are only for non-static functions");
1814 this->result_ = error_mark_node;
1815 return;
1816 }
1817
1818 object = build_expr (e->e1);
1819
1820 /* Want reference to `this' object. */
1821 if (e->e1->type->ty != TY::Tclass && e->e1->type->ty != TY::Tpointer)
1822 object = build_address (object);
1823
1824 /* Object reference could be the outer `this' field of a class or
1825 closure of type `void*'. Cast it to the right type. */
1826 if (e->e1->type->ty == TY::Tclass)
1827 object = d_convert (build_ctype (e->e1->type), object);
1828
1829 fndecl = get_symbol_decl (e->func);
1830
1831 /* Get pointer to function out of the virtual table. */
1832 if (e->func->isVirtual () && !e->func->isFinalFunc ()
1833 && e->e1->op != EXP::super_ && e->e1->op != EXP::dotType)
1834 {
1835 tree fntype = build_pointer_type (TREE_TYPE (fndecl));
1836 object = d_save_expr (object);
1837 fndecl = build_vindex_ref (object, fntype, e->func->vtblIndex);
1838 }
1839 else
1840 fndecl = build_address (fndecl);
1841 }
1842
1843 this->result_ = build_method_call (fndecl, object, e->type);
1844 }
1845
1846 /* Build a type component expression. */
1847
1848 void visit (DotTypeExp *e) final override
1849 {
1850 /* Just a pass through to underlying expression. */
1851 this->result_ = build_expr (e->e1);
1852 }
1853
1854 /* Build a component reference expression. */
1855
1856 void visit (DotVarExp *e) final override
1857 {
1858 VarDeclaration *vd = e->var->isVarDeclaration ();
1859
1860 /* This could also be a function, but relying on that being taken
1861 care of by the visitor interface for CallExp. */
1862 if (vd != NULL)
1863 {
1864 if (!vd->isField ())
1865 this->result_ = get_decl_tree (vd);
1866 else
1867 {
1868 tree object = build_expr (e->e1);
1869 Type *tb = e->e1->type->toBasetype ();
1870
1871 if (tb->ty != TY::Tstruct)
1872 object = build_deref (object);
1873
1874 /* __complex is represented as a struct in the front-end, but
1875 underlying is really a complex type. */
1876 if (e->e1->type->ty == TY::Tenum
1877 && e->e1->type->isTypeEnum ()->sym->isSpecial ())
1878 object = underlying_complex_expr (build_ctype (tb), object);
1879
1880 this->result_ = component_ref (object, get_symbol_decl (vd));
1881 }
1882 }
1883 else
1884 {
1885 error ("%qs is not a field, but a %qs",
1886 e->var->toChars (), e->var->kind ());
1887 this->result_ = error_mark_node;
1888 }
1889 }
1890
1891 /* Build an assert expression, used to declare conditions that must hold at
1892 that a given point in the program. */
1893
1894 void visit (AssertExp *e) final override
1895 {
1896 Type *tb1 = e->e1->type->toBasetype ();
1897 tree arg = build_expr (e->e1);
1898 tree tmsg = NULL_TREE;
1899 tree assert_pass = void_node;
1900 tree assert_fail;
1901
1902 if (global.params.useAssert == CHECKENABLEon && !checkaction_trap_p ())
1903 {
1904 /* Generate: ((bool) e1 ? (void)0 : _d_assert (...))
1905 or: (e1 != null ? e1._invariant() : _d_assert (...)) */
1906 bool unittest_p = d_function_chain->function->isUnitTestDeclaration ();
1907 libcall_fn libcall;
1908
1909 if (e->msg)
1910 {
1911 tmsg = build_expr_dtor (e->msg);
1912 libcall = unittest_p ? LIBCALL_UNITTEST_MSG : LIBCALL_ASSERT_MSG;
1913 }
1914 else
1915 libcall = unittest_p ? LIBCALL_UNITTESTP : LIBCALL_ASSERTP;
1916
1917 /* Build a call to _d_assert(). */
1918 assert_fail = build_assert_call (e->loc, libcall, tmsg);
1919
1920 if (global.params.useInvariants == CHECKENABLEon)
1921 {
1922 /* If the condition is a D class or struct object with an invariant,
1923 call it if the condition result is true. */
1924 if (tb1->ty == TY::Tclass)
1925 {
1926 ClassDeclaration *cd = tb1->isClassHandle ();
1927 if (!cd->isInterfaceDeclaration () && !cd->isCPPclass ())
1928 {
1929 arg = d_save_expr (arg);
1930 assert_pass = build_libcall (LIBCALL_INVARIANT,
1931 Type::tvoid, 1, arg);
1932 }
1933 }
1934 else if (tb1->ty == TY::Tpointer
1935 && tb1->nextOf ()->ty == TY::Tstruct)
1936 {
1937 StructDeclaration *sd = tb1->nextOf ()->isTypeStruct ()->sym;
1938 if (sd->inv != NULL)
1939 {
1940 Expressions args;
1941 arg = d_save_expr (arg);
1942 assert_pass = d_build_call_expr (sd->inv, arg, &args);
1943 }
1944 }
1945 }
1946 }
1947 else if (global.params.useAssert == CHECKENABLEon && checkaction_trap_p ())
1948 {
1949 /* Generate: __builtin_trap() */
1950 tree fn = builtin_decl_explicit (BUILT_IN_TRAP);
1951 assert_fail = build_call_expr (fn, 0);
1952 }
1953 else
1954 {
1955 /* Assert contracts are turned off. */
1956 this->result_ = void_node;
1957 return;
1958 }
1959
1960 /* Build condition that we are asserting in this contract. */
1961 tree condition = convert_for_condition (arg, e->e1->type);
1962
1963 /* We expect the condition to always be true, as what happens if an assert
1964 contract is false is undefined behavior. */
1965 tree fn = builtin_decl_explicit (BUILT_IN_EXPECT);
1966 tree arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
1967 tree pred_type = TREE_VALUE (arg_types);
1968 tree expected_type = TREE_VALUE (TREE_CHAIN (arg_types));
1969
1970 condition = build_call_expr (fn, 2, d_convert (pred_type, condition),
1971 build_int_cst (expected_type, 1));
1972 condition = d_truthvalue_conversion (condition);
1973
1974 this->result_ = build_vcondition (condition, assert_pass, assert_fail);
1975 }
1976
1977 /* Build a declaration expression. */
1978
1979 void visit (DeclarationExp *e) final override
1980 {
1981 /* Compile the declaration. */
1982 push_stmt_list ();
1983 build_decl_tree (e->declaration);
1984 tree result = pop_stmt_list ();
1985
1986 /* Construction of an array for typesafe-variadic function arguments
1987 can cause an empty STMT_LIST here. This can causes problems
1988 during gimplification. */
1989 if (TREE_CODE (result) == STATEMENT_LIST && !STATEMENT_LIST_HEAD (result))
1990 result = build_empty_stmt (input_location);
1991
1992 this->result_ = result;
1993 }
1994
1995 /* Build a typeid expression. Returns an instance of class TypeInfo
1996 corresponding to. */
1997
1998 void visit (TypeidExp *e) final override
1999 {
2000 if (Type *tid = dmd::isType (e->obj))
2001 {
2002 tree ti = build_typeinfo (e, tid);
2003
2004 /* If the typeinfo is at an offset. */
2005 if (tid->vtinfo->offset)
2006 ti = build_offset (ti, size_int (tid->vtinfo->offset));
2007
2008 this->result_ = build_nop (build_ctype (e->type), ti);
2009 }
2010 else if (Expression *tid = dmd::isExpression (e->obj))
2011 {
2012 Type *type = tid->type->toBasetype ();
2013 assert (type->ty == TY::Tclass);
2014
2015 /* Generate **classptr to get the classinfo. */
2016 tree ci = build_expr (tid);
2017 ci = indirect_ref (ptr_type_node, ci);
2018 ci = indirect_ref (ptr_type_node, ci);
2019
2020 /* Add extra indirection for interfaces. */
2021 if (type->isTypeClass ()->sym->isInterfaceDeclaration ())
2022 ci = indirect_ref (ptr_type_node, ci);
2023
2024 this->result_ = build_nop (build_ctype (e->type), ci);
2025 }
2026 else
2027 gcc_unreachable ();
2028 }
2029
2030 /* Build a function/lambda expression. */
2031
2032 void visit (FuncExp *e) final override
2033 {
2034 /* Compile the declaration. */
2035 build_lambda_tree (e->fd, e->type->toBasetype ());
2036
2037 tree func = build_address (get_symbol_decl (e->fd));
2038 Type *tb = e->type->toBasetype ();
2039
2040 /* If a delegate is expected, the literal will be inferred as a delegate
2041 even if it accesses no variables from an enclosing function. */
2042 if (tb->ty == TY::Tdelegate)
2043 {
2044 /* Static delegate variables have no context pointer. */
2045 if (this->constp_ || !e->fd->isNested ())
2046 {
2047 this->result_ = build_method_call (func, null_pointer_node,
2048 e->fd->type);
2049 TREE_CONSTANT (this->result_) = 1;
2050 }
2051 else
2052 {
2053 gcc_assert (e->fd->isNested ());
2054 tree object = get_frame_for_symbol (e->fd);
2055 this->result_ = build_method_call (func, object, e->fd->type);
2056 }
2057 }
2058 else
2059 {
2060 /* The function literal is a function pointer. */
2061 gcc_assert (tb->ty == TY::Tpointer);
2062 this->result_ = build_nop (build_ctype (e->type),
2063 build_address (get_symbol_decl (e->fd)));
2064 }
2065 }
2066
2067 /* Build a halt expression. */
2068
2069 void visit (HaltExp *) final override
2070 {
2071 /* Should we use trap() or abort()? */
2072 tree ttrap = builtin_decl_explicit (BUILT_IN_TRAP);
2073 this->result_ = build_call_expr (ttrap, 0);
2074 }
2075
2076 /* Build a symbol pointer offset expression. */
2077
2078 void visit (SymOffExp *e) final override
2079 {
2080 /* Build the address and offset of the symbol. */
2081 dinteger_t soffset = e->isSymOffExp ()->offset;
2082 tree result = get_decl_tree (e->var);
2083 TREE_USED (result) = 1;
2084
2085 if (e->var->isFuncDeclaration ())
2086 result = maybe_reject_intrinsic (result);
2087
2088 /* Emit lambdas, same as is done in FuncExp. */
2089 if (FuncLiteralDeclaration *fld = e->var->isFuncLiteralDeclaration ())
2090 build_lambda_tree (fld);
2091
2092 if (declaration_reference_p (e->var))
2093 gcc_assert (POINTER_TYPE_P (TREE_TYPE (result)));
2094 else
2095 result = build_address (result);
2096
2097 if (!soffset)
2098 result = d_convert (build_ctype (e->type), result);
2099 else
2100 {
2101 tree offset = size_int (soffset);
2102 result = build_nop (build_ctype (e->type),
2103 build_offset (result, offset));
2104 }
2105
2106 this->result_ = result;
2107 }
2108
2109 /* Build a variable expression. */
2110
2111 void visit (VarExp *e) final override
2112 {
2113 if (e->var->needThis ())
2114 {
2115 error ("need %<this%> to access member %qs", e->var->ident->toChars ());
2116 this->result_ = error_mark_node;
2117 return;
2118 }
2119 else if (e->var->ident == Identifier::idPool ("__ctfe"))
2120 {
2121 /* __ctfe is always false at run-time. */
2122 this->result_ = integer_zero_node;
2123 return;
2124 }
2125
2126 /* Emit lambdas, same as is done in FuncExp. */
2127 if (FuncLiteralDeclaration *fld = e->var->isFuncLiteralDeclaration ())
2128 build_lambda_tree (fld);
2129
2130 if (this->constp_)
2131 {
2132 /* Want the initializer, not the expression. */
2133 VarDeclaration *var = e->var->isVarDeclaration ();
2134 SymbolDeclaration *sdecl = e->var->isSymbolDeclaration ();
2135 tree init = NULL_TREE;
2136
2137 if (var && (var->isConst () || var->isImmutable ())
2138 && e->type->toBasetype ()->ty != TY::Tsarray && var->_init)
2139 {
2140 if (var->inuse)
2141 error_at (make_location_t (e->loc), "recursive reference %qs",
2142 e->toChars ());
2143 else
2144 {
2145 var->inuse++;
2146 Expression *vinit = dmd::initializerToExpression (var->_init);
2147 init = build_expr (vinit, true);
2148 var->inuse--;
2149 }
2150 }
2151 else if (sdecl && sdecl->dsym)
2152 {
2153 if (StructDeclaration *sd = sdecl->dsym->isStructDeclaration ())
2154 init = layout_struct_initializer (sd);
2155 else if (ClassDeclaration *cd = sdecl->dsym->isClassDeclaration ())
2156 init = layout_class_initializer (cd);
2157 else
2158 gcc_unreachable ();
2159 }
2160 else
2161 error_at (make_location_t (e->loc), "non-constant expression %qs",
2162 e->toChars ());
2163
2164 if (init != NULL_TREE)
2165 this->result_ = init;
2166 else
2167 this->result_ = error_mark_node;
2168 }
2169 else
2170 {
2171 tree result = get_decl_tree (e->var);
2172 TREE_USED (result) = 1;
2173
2174 /* The variable expression generated for `__traits(initSymbol)'. */
2175 if (SymbolDeclaration *sd = e->var->isSymbolDeclaration ())
2176 {
2177 if (e->type->isTypeDArray ())
2178 {
2179 /* Generate a slice for non-zero initialized aggregates,
2180 otherwise create an empty array. */
2181 gcc_assert (e->type->nextOf ()->isConst ()
2182 && e->type->nextOf ()->ty == TY::Tvoid);
2183
2184 tree type = build_ctype (e->type);
2185 tree length = size_int (sd->dsym->structsize);
2186 tree ptr = (sd->dsym->isStructDeclaration ()
2187 && dmd::isZeroInit (sd->dsym->type, e->loc))
2188 ? null_pointer_node : build_address (result);
2189
2190 this->result_ = d_array_value (type, length, ptr);
2191 return;
2192 }
2193 }
2194
2195 /* For variables that are references - currently only out/inout
2196 arguments; objects don't count - evaluating the variable means
2197 we want what it refers to. */
2198 if (declaration_reference_p (e->var))
2199 result = indirect_ref (build_ctype (e->var->type), result);
2200
2201 this->result_ = result;
2202 }
2203 }
2204
2205 /* Build a this variable expression. */
2206
2207 void visit (ThisExp *e) final override
2208 {
2209 FuncDeclaration *fd = d_function_chain ? d_function_chain->function : NULL;
2210 tree result = NULL_TREE;
2211
2212 if (e->var)
2213 result = get_decl_tree (e->var);
2214 else
2215 {
2216 gcc_assert (fd && fd->vthis);
2217 result = get_decl_tree (fd->vthis);
2218 }
2219
2220 if (e->type->ty == TY::Tstruct)
2221 result = build_deref (result);
2222
2223 this->result_ = result;
2224 }
2225
2226 /* Build a new expression, which allocates memory either on the garbage
2227 collected heap or by using a class or struct specific allocator. */
2228
2229 void visit (NewExp *e) final override
2230 {
2231 Type *tb = e->type->toBasetype ();
2232 tree result;
2233
2234 if (tb->ty == TY::Tclass)
2235 {
2236 /* Allocating a new class. */
2237 tb = e->newtype->toBasetype ();
2238
2239 ClassDeclaration *cd = tb->isTypeClass ()->sym;
2240 tree type = build_ctype (tb);
2241 tree setup_exp = NULL_TREE;
2242 tree new_call;
2243
2244 if (e->onstack)
2245 {
2246 /* If being used as an initializer for a local variable with scope
2247 storage class, then the instance is allocated on the stack
2248 rather than the heap or using the class specific allocator. */
2249 tree var = build_local_temp (TREE_TYPE (type));
2250 SET_DECL_ALIGN (var, cd->alignsize * BITS_PER_UNIT);
2251 DECL_USER_ALIGN (var) = 1;
2252 new_call = build_nop (type, build_address (var));
2253 setup_exp = modify_expr (var, aggregate_initializer_decl (cd));
2254 }
2255 else if (e->placement != NULL)
2256 {
2257 /* Generate: placement_expr = typeid(class).init */
2258 tree placement_expr = build_expr (e->placement);
2259 new_call = build_nop (type, build_address (placement_expr));
2260 tree class_init = build_vconvert (TREE_TYPE (placement_expr),
2261 aggregate_initializer_decl (cd));
2262 setup_exp = modify_expr (placement_expr, class_init);
2263 }
2264 else
2265 {
2266 /* Generate: _d_newclass()
2267 or: _d_newThrowable() */
2268 new_call = build_expr (e->lowering);
2269 }
2270
2271 /* Set the context pointer for nested classes. */
2272 if (cd->isNested ())
2273 {
2274 tree field = get_symbol_decl (cd->vthis);
2275 tree value = NULL_TREE;
2276
2277 if (e->thisexp)
2278 {
2279 ClassDeclaration *tcd = e->thisexp->type->isClassHandle ();
2280 /* The class or function we're nested in. */
2281 Dsymbol *outer = cd->toParentLocal ();
2282
2283 value = build_expr (e->thisexp);
2284
2285 if (outer != tcd)
2286 {
2287 ClassDeclaration *ocd = outer->isClassDeclaration ();
2288 int offset = 0;
2289 gcc_assert (ocd->isBaseOf (tcd, &offset));
2290 /* Could just add offset... */
2291 value = convert_expr (value, e->thisexp->type, ocd->type);
2292 }
2293 }
2294 else
2295 value = build_vthis (cd);
2296
2297 if (value != NULL_TREE)
2298 {
2299 /* Generate: (new())->vthis = this; */
2300 new_call = d_save_expr (new_call);
2301 field = component_ref (build_deref (new_call), field);
2302 setup_exp = compound_expr (setup_exp,
2303 modify_expr (field, value));
2304 }
2305 }
2306 new_call = compound_expr (setup_exp, new_call);
2307
2308 /* Call the class constructor. */
2309 if (e->member)
2310 result = d_build_call_expr (e->member, new_call, e->arguments);
2311 else
2312 result = new_call;
2313
2314 if (e->argprefix)
2315 result = compound_expr (build_expr (e->argprefix), result);
2316 }
2317 else if (tb->ty == TY::Tpointer
2318 && tb->nextOf ()->toBasetype ()->ty == TY::Tstruct)
2319 {
2320 /* Allocating memory for a new struct. */
2321 Type *htype = e->newtype->toBasetype ();
2322 gcc_assert (!e->onstack);
2323
2324 TypeStruct *stype = htype->isTypeStruct ();
2325 StructDeclaration *sd = stype->sym;
2326 tree new_call;
2327
2328 /* Cannot new an opaque struct. */
2329 if (sd->size (e->loc) == 0)
2330 {
2331 this->result_ = d_convert (build_ctype (e->type),
2332 integer_zero_node);
2333 return;
2334 }
2335
2336 if (e->placement != NULL)
2337 {
2338 /* Generate: &placement_expr */
2339 tree placement_expr = build_expr (e->placement);
2340 new_call = build_nop (build_ctype (tb),
2341 build_address (placement_expr));
2342 }
2343 else
2344 {
2345 /* This case should have been rewritten to `_d_newitemT' during the
2346 semantic phase. */
2347 gcc_assert (e->lowering);
2348
2349 /* Generate: _d_newitemT() */
2350 new_call = build_expr (e->lowering);
2351 }
2352
2353 if (e->member || !e->arguments)
2354 {
2355 /* Set the context pointer for nested structs. */
2356 if (sd->isNested ())
2357 {
2358 tree value = build_vthis (sd);
2359 tree field = get_symbol_decl (sd->vthis);
2360 tree type = build_ctype (stype);
2361
2362 new_call = d_save_expr (new_call);
2363 field = component_ref (indirect_ref (type, new_call), field);
2364 new_call = compound_expr (modify_expr (field, value), new_call);
2365 }
2366
2367 /* Call the struct constructor. */
2368 if (e->member)
2369 result = d_build_call_expr (e->member, new_call, e->arguments);
2370 else
2371 result = new_call;
2372 }
2373 else
2374 {
2375 /* If we have a user supplied initializer, then set-up with a
2376 struct literal. */
2377 if (e->arguments != NULL && sd->fields.length != 0)
2378 {
2379 StructLiteralExp *se = StructLiteralExp::create (e->loc, sd,
2380 e->arguments,
2381 htype);
2382 new_call = d_save_expr (new_call);
2383 se->type = sd->type;
2384 se->sym = new_call;
2385
2386 /* Setting `se->sym' would mean that the result of the
2387 constructed struct literal expression is `*(new_call)'.
2388 Strip off the indirect reference, as we don't mean to
2389 compute the value yet. */
2390 result = build_address (build_expr (se));
2391 }
2392 else
2393 result = new_call;
2394 }
2395
2396 if (e->argprefix)
2397 result = compound_expr (build_expr (e->argprefix), result);
2398 }
2399 else if (tb->ty == TY::Tarray)
2400 {
2401 /* Allocating memory for a new D array. */
2402 gcc_assert (e->arguments && e->arguments->length >= 1);
2403
2404 /* Array allocations have already been handled by the front-end. */
2405 gcc_assert (e->lowering != NULL);
2406 result = build_expr (e->lowering);
2407
2408 if (e->argprefix)
2409 result = compound_expr (build_expr (e->argprefix), result);
2410 }
2411 else if (tb->ty == TY::Tpointer)
2412 {
2413 /* Allocating memory for a new pointer. */
2414 TypePointer *tpointer = tb->isTypePointer ();
2415
2416 if (dmd::size (tpointer->next) == 0)
2417 {
2418 /* Pointer element size is unknown. */
2419 this->result_ = d_convert (build_ctype (e->type),
2420 integer_zero_node);
2421 return;
2422 }
2423
2424 if (e->placement != NULL)
2425 {
2426 /* Generate: &placement_expr */
2427 tree placement_expr = build_expr (e->placement);
2428 result = build_nop (build_ctype (tb),
2429 build_address (placement_expr));
2430 }
2431 else
2432 {
2433 /* This case should have been rewritten to `_d_newitemT' during the
2434 semantic phase. */
2435 gcc_assert (e->lowering);
2436
2437 /* Generate: _d_newitemT() */
2438 result = build_expr (e->lowering);
2439 }
2440
2441 if (e->arguments && e->arguments->length == 1)
2442 {
2443 result = d_save_expr (result);
2444 tree init = modify_expr (build_deref (result),
2445 build_expr ((*e->arguments)[0]));
2446 result = compound_expr (init, result);
2447 }
2448
2449 if (e->argprefix)
2450 result = compound_expr (build_expr (e->argprefix), result);
2451 }
2452 else if (tb->ty == TY::Taarray)
2453 {
2454 /* Allocating memory for a new associative array. */
2455 tree arg = build_typeinfo (e, e->newtype);
2456 tree mem = build_libcall (LIBCALL_AANEW, Type::tvoidptr, 1, arg);
2457
2458 /* Return an associative array pointed to by MEM. */
2459 tree aatype = build_ctype (tb);
2460 vec <constructor_elt, va_gc> *ce = NULL;
2461 CONSTRUCTOR_APPEND_ELT (ce, TYPE_FIELDS (aatype), mem);
2462
2463 result = build_nop (build_ctype (e->type),
2464 build_padded_constructor (aatype, ce));
2465 }
2466 else
2467 gcc_unreachable ();
2468
2469 this->result_ = convert_expr (result, tb, e->type);
2470 }
2471
2472 /* Build an integer literal. */
2473
2474 void visit (IntegerExp *e) final override
2475 {
2476 tree ctype = build_ctype (e->type->toBasetype ());
2477 this->result_ = build_integer_cst (e->value, ctype);
2478 }
2479
2480 /* Build a floating-point literal. */
2481
2482 void visit (RealExp *e) final override
2483 {
2484 this->result_ = build_float_cst (e->value, e->type->toBasetype ());
2485 }
2486
2487 /* Build a complex literal. */
2488
2489 void visit (ComplexExp *e) final override
2490 {
2491 Type *tnext;
2492
2493 switch (e->type->toBasetype ()->ty)
2494 {
2495 case TY::Tcomplex32:
2496 tnext = (TypeBasic *) Type::tfloat32;
2497 break;
2498
2499 case TY::Tcomplex64:
2500 tnext = (TypeBasic *) Type::tfloat64;
2501 break;
2502
2503 case TY::Tcomplex80:
2504 tnext = (TypeBasic *) Type::tfloat80;
2505 break;
2506
2507 default:
2508 gcc_unreachable ();
2509 }
2510
2511 this->result_ = build_complex (build_ctype (e->type),
2512 build_float_cst (creall (e->value), tnext),
2513 build_float_cst (cimagl (e->value), tnext));
2514 }
2515
2516 /* Build a string literal, all strings are null terminated except for
2517 static arrays. */
2518
2519 void visit (StringExp *e) final override
2520 {
2521 Type *tb = e->type->toBasetype ();
2522 tree type = build_ctype (e->type);
2523
2524 if (tb->ty == TY::Tsarray)
2525 {
2526 /* Turn the string into a constructor for the static array. */
2527 vec <constructor_elt, va_gc> *elms = NULL;
2528 vec_safe_reserve (elms, e->len);
2529 tree etype = TREE_TYPE (type);
2530
2531 for (size_t i = 0; i < e->len; i++)
2532 {
2533 tree value = build_integer_cst (e->getIndex (i), etype);
2534 CONSTRUCTOR_APPEND_ELT (elms, size_int (i), value);
2535 }
2536
2537 tree ctor = build_padded_constructor (type, elms);
2538 TREE_CONSTANT (ctor) = 1;
2539 this->result_ = ctor;
2540 return;
2541 }
2542 else
2543 {
2544 /* Copy the string contents to a null terminated STRING_CST. */
2545 dinteger_t length = (e->len * e->sz);
2546 char *string = XALLOCAVEC (char, length + e->sz);
2547 memset (string, 0, length + e->sz);
2548 if (length > 0)
2549 memcpy (string, e->string, length);
2550
2551 /* String value and type includes the null terminator. */
2552 tree value = build_string (length + e->sz, string);
2553 if (e->sz <= 4)
2554 TREE_TYPE (value) = make_array_type (tb->nextOf (), length + 1);
2555 else
2556 {
2557 /* Hexadecimal literal strings with an 8-byte character type are
2558 just an alternative way to store an array of `ulong'.
2559 Treat it as if it were a `uint[]' array instead. */
2560 dinteger_t resize = e->sz / 4;
2561 TREE_TYPE (value) = make_array_type (Type::tuns32,
2562 (length * resize) + resize);
2563 }
2564
2565 value = build_address (value);
2566
2567 if (tb->ty == TY::Tarray)
2568 value = d_array_value (type, size_int (e->len), value);
2569
2570 TREE_CONSTANT (value) = 1;
2571 this->result_ = d_convert (type, value);
2572 }
2573 }
2574
2575 /* Build a tuple literal. Just an argument list that may have
2576 side effects that need evaluation. */
2577
2578 void visit (TupleExp *e) final override
2579 {
2580 tree result = NULL_TREE;
2581
2582 if (e->e0)
2583 result = build_expr (e->e0, this->constp_, true);
2584
2585 for (size_t i = 0; i < e->exps->length; ++i)
2586 {
2587 Expression *exp = (*e->exps)[i];
2588 result = compound_expr (result, build_expr (exp, this->constp_, true));
2589 }
2590
2591 if (result == NULL_TREE)
2592 result = void_node;
2593
2594 this->result_ = result;
2595 }
2596
2597 /* Build an array literal. The common type of the all elements is taken to
2598 be the type of the array element, and all elements are implicitly
2599 converted to that type. */
2600
2601 void visit (ArrayLiteralExp *e) final override
2602 {
2603 Type *tb = e->type->toBasetype ();
2604
2605 /* Implicitly convert void[n] to ubyte[n]. */
2606 if (tb->ty == TY::Tsarray && tb->nextOf ()->toBasetype ()->ty == TY::Tvoid)
2607 tb = dmd::sarrayOf (Type::tuns8, tb->isTypeSArray ()->dim->toUInteger ());
2608
2609 gcc_assert (tb->ty == TY::Tarray || tb->ty == TY::Tsarray
2610 || tb->ty == TY::Tpointer);
2611
2612 /* Handle empty array literals. */
2613 if (e->elements->length == 0)
2614 {
2615 if (tb->ty == TY::Tarray)
2616 this->result_ = d_array_value (build_ctype (e->type),
2617 size_int (0), null_pointer_node);
2618 else
2619 {
2620 tree arrtype = make_array_type (tb->nextOf (), 0);
2621 this->result_ = build_padded_constructor (arrtype, NULL);
2622 }
2623
2624 return;
2625 }
2626
2627 /* Build an expression that assigns the expressions in ELEMENTS to
2628 a constructor. */
2629 vec <constructor_elt, va_gc> *elms = NULL;
2630 vec_safe_reserve (elms, e->elements->length);
2631 bool constant_p = true;
2632 tree saved_elems = NULL_TREE;
2633
2634 Type *etype = tb->nextOf ();
2635 tree satype = make_array_type (etype, e->elements->length);
2636
2637 for (size_t i = 0; i < e->elements->length; i++)
2638 {
2639 Expression *expr = e->getElement (i);
2640 tree value = build_expr (expr, this->constp_, true);
2641
2642 /* Only append nonzero values, the backend will zero out the rest
2643 of the constructor as we don't set CONSTRUCTOR_NO_CLEARING. */
2644 if (!initializer_zerop (value))
2645 {
2646 if (!TREE_CONSTANT (value))
2647 constant_p = false;
2648
2649 /* Split construction of values out of the constructor if there
2650 may be side effects. */
2651 tree init = stabilize_expr (&value);
2652 if (init != NULL_TREE)
2653 saved_elems = compound_expr (saved_elems, init);
2654
2655 CONSTRUCTOR_APPEND_ELT (elms, size_int (i),
2656 convert_expr (value, expr->type, etype));
2657 }
2658 }
2659
2660 /* Now return the constructor as the correct type. For static arrays there
2661 is nothing else to do. For dynamic arrays, return a two field struct.
2662 For pointers, return the address. */
2663 tree ctor = build_padded_constructor (satype, elms);
2664 tree type = build_ctype (e->type);
2665
2666 /* Nothing else to do for static arrays. */
2667 if (tb->ty == TY::Tsarray || this->constp_)
2668 {
2669 /* Can't take the address of the constructor, so create an anonymous
2670 static symbol, and then refer to it. */
2671 if (tb->ty != TY::Tsarray)
2672 {
2673 tree decl = build_artificial_decl (TREE_TYPE (ctor), ctor, "A");
2674 ctor = build_address (decl);
2675 if (tb->ty == TY::Tarray)
2676 ctor = d_array_value (type, size_int (e->elements->length), ctor);
2677
2678 /* Immutable literals can be placed in rodata. */
2679 if (tb->isImmutable ())
2680 TREE_READONLY (decl) = 1;
2681
2682 d_pushdecl (decl);
2683 rest_of_decl_compilation (decl, 1, 0);
2684 }
2685
2686 /* If the array literal is readonly or static. */
2687 if (constant_p)
2688 TREE_CONSTANT (ctor) = 1;
2689 if (constant_p && initializer_constant_valid_p (ctor, TREE_TYPE (ctor)))
2690 TREE_STATIC (ctor) = 1;
2691
2692 /* Use memset to fill any alignment holes in the array. */
2693 if (!this->constp_ && !this->literalp_)
2694 {
2695 TypeStruct *ts = etype->baseElemOf ()->isTypeStruct ();
2696
2697 if (ts != NULL && (!identity_compare_p (ts->sym)
2698 || ts->sym->isUnionDeclaration ()))
2699 {
2700 tree var = build_local_temp (TREE_TYPE (ctor));
2701 tree init = build_memset_call (var);
2702 /* Evaluate memset() first, then any saved elements. */
2703 saved_elems = compound_expr (init, saved_elems);
2704 ctor = compound_expr (modify_expr (var, ctor), var);
2705 }
2706 }
2707
2708 this->result_ = compound_expr (saved_elems, d_convert (type, ctor));
2709 }
2710 else if (e->onstack)
2711 {
2712 /* Array literal for a `scope' dynamic array. */
2713 gcc_assert (tb->ty == TY::Tarray);
2714 ctor = force_target_expr (ctor);
2715 ctor = d_array_value (type, size_int (e->elements->length),
2716 build_address (ctor));
2717 this->result_ = compound_expr (saved_elems, ctor);
2718 }
2719 else
2720 {
2721 /* Allocate space on the memory managed heap. */
2722 tree mem = build_libcall (LIBCALL_ARRAYLITERALTX,
2723 dmd::pointerTo (etype), 2,
2724 build_typeinfo (e, dmd::arrayOf (etype)),
2725 size_int (e->elements->length));
2726 mem = d_save_expr (mem);
2727
2728 /* Now copy the constructor into memory. */
2729 tree size = size_mult_expr (size_int (e->elements->length),
2730 size_int (dmd::size (tb->nextOf ())));
2731
2732 tree result = build_memcpy_call (mem, build_address (ctor), size);
2733
2734 /* Return the array pointed to by MEM. */
2735 result = compound_expr (result, mem);
2736
2737 if (tb->ty == TY::Tarray)
2738 result = d_array_value (type, size_int (e->elements->length), result);
2739
2740 this->result_ = compound_expr (saved_elems, result);
2741 }
2742 }
2743
2744 /* Build an associative array literal. The common type of the all keys is
2745 taken to be the key type, and common type of all values the value type.
2746 All keys and values are then implicitly converted as needed. */
2747
2748 void visit (AssocArrayLiteralExp *e) final override
2749 {
2750 if (this->constp_ && e->lowering != NULL)
2751 {
2752 /* When an associative array literal gets lowered, it's converted into a
2753 struct literal suitable for static initialization. */
2754 this->result_ = build_expr (e->lowering, this->constp_, true);
2755 return ;
2756 }
2757
2758 /* Want the mutable type for typeinfo reference. */
2759 Type *tb = dmd::mutableOf (e->type->toBasetype ());
2760
2761 /* Handle empty assoc array literals. */
2762 TypeAArray *ta = tb->isTypeAArray ();
2763 if (e->keys->length == 0)
2764 {
2765 this->result_ = build_padded_constructor (build_ctype (ta), NULL);
2766 return;
2767 }
2768
2769 /* Build an expression that assigns all expressions in KEYS
2770 to a constructor. */
2771 Type *tkarray = dmd::sarrayOf (ta->index, e->keys->length);
2772 tree akeys = build_array_from_exprs (tkarray, e->keys, this->constp_);
2773 tree init = stabilize_expr (&akeys);
2774
2775 /* Do the same with all expressions in VALUES. */
2776 Type *tvarray = dmd::sarrayOf (ta->next, e->values->length);
2777 tree avals = build_array_from_exprs (tvarray, e->values, this->constp_);
2778 init = compound_expr (init, stabilize_expr (&avals));
2779
2780 /* Generate: _d_assocarrayliteralTX (ti, keys, vals); */
2781 tree keys = d_array_value (build_ctype (dmd::arrayOf (ta->index)),
2782 size_int (e->keys->length),
2783 build_address (akeys));
2784 tree vals = d_array_value (build_ctype (dmd::arrayOf (ta->next)),
2785 size_int (e->values->length),
2786 build_address (avals));
2787
2788 tree mem = build_libcall (LIBCALL_ASSOCARRAYLITERALTX, Type::tvoidptr, 3,
2789 build_typeinfo (e, ta), keys, vals);
2790
2791 /* Return an associative array pointed to by MEM. */
2792 tree aatype = build_ctype (ta);
2793 vec <constructor_elt, va_gc> *ce = NULL;
2794 CONSTRUCTOR_APPEND_ELT (ce, TYPE_FIELDS (aatype), mem);
2795
2796 tree result = build_nop (build_ctype (e->type),
2797 build_padded_constructor (aatype, ce));
2798 this->result_ = compound_expr (init, result);
2799 }
2800
2801 /* Build a struct literal. */
2802
2803 void visit (StructLiteralExp *e) final override
2804 {
2805 /* Handle empty struct literals. */
2806 if (e->elements == NULL || e->sd->fields.length == 0)
2807 {
2808 this->result_ = build_padded_constructor (build_ctype (e->type), NULL);
2809 return;
2810 }
2811
2812 /* Building sinit trees are delayed until after frontend semantic
2813 processing has complete. Build the static initializer now. */
2814 if (e->useStaticInit () && !this->constp_ && !e->sd->isCsymbol ())
2815 {
2816 tree init = aggregate_initializer_decl (e->sd);
2817
2818 /* If initializing a symbol, don't forget to set it. */
2819 if (e->sym != NULL)
2820 {
2821 tree var = build_deref (e->sym);
2822 init = compound_expr (modify_expr (var, init), var);
2823 }
2824
2825 this->result_ = init;
2826 return;
2827 }
2828
2829 /* Build a constructor that assigns the expressions in ELEMENTS
2830 at each field index that has been filled in. */
2831 vec <constructor_elt, va_gc> *ve = NULL;
2832 tree saved_elems = NULL_TREE;
2833
2834 /* CTFE may fill the hidden pointer by NullExp. */
2835 gcc_assert (e->elements->length <= e->sd->fields.length);
2836
2837 Type *tb = e->type->toBasetype ();
2838 gcc_assert (tb->ty == TY::Tstruct);
2839
2840 for (size_t i = 0; i < e->elements->length; i++)
2841 {
2842 Expression *exp = (*e->elements)[i];
2843 if (!exp)
2844 continue;
2845
2846 VarDeclaration *field = e->sd->fields[i];
2847 Type *type = exp->type->toBasetype ();
2848 Type *ftype = field->type->toBasetype ();
2849 tree value = NULL_TREE;
2850
2851 if (ftype->ty == TY::Tsarray && !same_type_p (type, ftype))
2852 {
2853 /* Initialize a static array with a single element. */
2854 tree elem = build_expr (exp, this->constp_, true);
2855 saved_elems = compound_expr (saved_elems, stabilize_expr (&elem));
2856 elem = d_save_expr (elem);
2857
2858 if (initializer_zerop (elem))
2859 value = build_padded_constructor (build_ctype (ftype), NULL);
2860 else
2861 value = build_array_from_val (ftype, elem);
2862 }
2863 else
2864 {
2865 value = convert_expr (build_expr (exp, this->constp_, true),
2866 exp->type, field->type);
2867 }
2868
2869 /* Split construction of values out of the constructor. */
2870 saved_elems = compound_expr (saved_elems, stabilize_expr (&value));
2871
2872 CONSTRUCTOR_APPEND_ELT (ve, get_symbol_decl (field), value);
2873 }
2874
2875 /* Maybe setup hidden pointer to outer scope context. */
2876 if (e->sd->isNested () && e->elements->length != e->sd->fields.length
2877 && this->constp_ == false)
2878 {
2879 tree field = get_symbol_decl (e->sd->vthis);
2880 tree value = build_vthis (e->sd);
2881 CONSTRUCTOR_APPEND_ELT (ve, field, value);
2882 gcc_assert (e->useStaticInit () == false);
2883 }
2884
2885 /* Build a constructor in the correct shape of the aggregate type. */
2886 tree ctor = build_struct_literal (build_ctype (e->type), ve);
2887
2888 /* Nothing more to do for constant literals. */
2889 if (this->constp_)
2890 {
2891 /* If the struct literal is a valid for static data. */
2892 if (TREE_CONSTANT (ctor)
2893 && initializer_constant_valid_p (ctor, TREE_TYPE (ctor)))
2894 TREE_STATIC (ctor) = 1;
2895
2896 this->result_ = compound_expr (saved_elems, ctor);
2897 return;
2898 }
2899
2900 /* Construct the struct literal for run-time. */
2901 if (e->sym != NULL)
2902 {
2903 /* Store the result in a symbol to initialize the literal. */
2904 tree var = build_deref (e->sym);
2905 ctor = compound_expr (modify_expr (var, ctor), var);
2906 }
2907 else if (!this->literalp_)
2908 {
2909 /* Use memset to fill any alignment holes in the object. */
2910 if (!identity_compare_p (e->sd) || e->sd->isUnionDeclaration ())
2911 {
2912 tree var = build_local_temp (TREE_TYPE (ctor));
2913 tree init = build_memset_call (var);
2914 /* Evaluate memset() first, then any saved element constructors. */
2915 saved_elems = compound_expr (init, saved_elems);
2916 ctor = compound_expr (modify_expr (var, ctor), var);
2917 }
2918 }
2919
2920 this->result_ = compound_expr (saved_elems, ctor);
2921 }
2922
2923 /* Build a null literal. */
2924
2925 void visit (NullExp *e) final override
2926 {
2927 this->result_ = build_typeof_null_value (e->type);
2928 }
2929
2930 /* Build a vector literal. */
2931
2932 void visit (VectorExp *e) final override
2933 {
2934 /* First handle array literal expressions. */
2935 if (e->e1->op == EXP::arrayLiteral)
2936 {
2937 ArrayLiteralExp *ale = e->e1->isArrayLiteralExp ();
2938 vec <constructor_elt, va_gc> *elms = NULL;
2939 bool constant_p = true;
2940 tree type = build_ctype (e->type);
2941
2942 vec_safe_reserve (elms, ale->elements->length);
2943 for (size_t i = 0; i < ale->elements->length; i++)
2944 {
2945 Expression *expr = ale->getElement (i);
2946 tree value = d_convert (TREE_TYPE (type),
2947 build_expr (expr, this->constp_, true));
2948 if (!CONSTANT_CLASS_P (value))
2949 constant_p = false;
2950
2951 CONSTRUCTOR_APPEND_ELT (elms, size_int (i), value);
2952 }
2953
2954 /* Build a VECTOR_CST from a constant vector constructor. */
2955 if (constant_p)
2956 this->result_ = build_vector_from_ctor (type, elms);
2957 else
2958 this->result_ = build_padded_constructor (type, elms);
2959 }
2960 else if (e->e1->type->toBasetype ()->ty == TY::Tsarray)
2961 {
2962 /* Build a vector representation from a static array. */
2963 this->result_ = convert_expr (build_expr (e->e1, this->constp_),
2964 e->e1->type, e->type);
2965 }
2966 else
2967 {
2968 /* Build constructor from single value. */
2969 tree type = build_ctype (e->type);
2970 tree value = d_convert (TREE_TYPE (type),
2971 build_expr (e->e1, this->constp_, true));
2972 this->result_ = build_vector_from_val (type, value);
2973 }
2974 }
2975
2976 /* Build a static array representation of a vector expression. */
2977
2978 void visit (VectorArrayExp *e) final override
2979 {
2980 this->result_ = convert_expr (build_expr (e->e1, this->constp_, true),
2981 e->e1->type, e->type);
2982 }
2983
2984 /* Build a static class literal, return its reference. */
2985
2986 void visit (ClassReferenceExp *e) final override
2987 {
2988 /* The result of build_new_class_expr is a RECORD_TYPE, we want
2989 the reference. */
2990 tree var = build_address (build_new_class_expr (e));
2991
2992 /* If the type of this literal is an interface, the we must add the
2993 interface offset to symbol. */
2994 if (this->constp_)
2995 {
2996 TypeClass *tc = e->type->toBasetype ()->isTypeClass ();
2997 InterfaceDeclaration *to = tc->sym->isInterfaceDeclaration ();
2998
2999 if (to != NULL)
3000 {
3001 ClassDeclaration *from = e->originalClass ();
3002 int offset = 0;
3003
3004 gcc_assert (to->isBaseOf (from, &offset) != 0);
3005
3006 if (offset != 0)
3007 var = build_offset (var, size_int (offset));
3008 }
3009 }
3010
3011 this->result_ = var;
3012 }
3013
3014 /* Build an uninitialized value, generated from void initializers. */
3015
3016 void visit (VoidInitExp *e) final override
3017 {
3018 /* The front-end only generates these for the initializer of globals.
3019 Represent `void' as zeroes, regardless of the type's default value. */
3020 gcc_assert (this->constp_);
3021 this->result_ = build_zero_cst (build_ctype (e->type));
3022 }
3023
3024 /* These expressions are mainly just a placeholders in the frontend.
3025 We shouldn't see them here. */
3026
3027 void visit (ScopeExp *e) final override
3028 {
3029 error_at (make_location_t (e->loc), "%qs is not an expression",
3030 e->toChars ());
3031 this->result_ = error_mark_node;
3032 }
3033
3034 void visit (TypeExp *e) final override
3035 {
3036 error_at (make_location_t (e->loc), "type %qs is not an expression",
3037 e->toChars ());
3038 this->result_ = error_mark_node;
3039 }
3040 };
3041
3042
3043 /* Main entry point for ExprVisitor interface to generate code for
3044 the Expression AST class E. If CONST_P is true, then E is a
3045 constant expression. If LITERAL_P is true, then E is a value used
3046 in the initialization of another literal. */
3047
3048 tree
3049 build_expr (Expression *e, bool const_p, bool literal_p)
3050 {
3051 ExprVisitor v = ExprVisitor (const_p, literal_p);
3052 location_t saved_location = input_location;
3053
3054 input_location = make_location_t (e->loc);
3055 e->accept (&v);
3056 tree expr = v.result ();
3057 input_location = saved_location;
3058
3059 /* Check if initializer expression is valid constant. */
3060 if (const_p && !initializer_constant_valid_p (expr, TREE_TYPE (expr)))
3061 {
3062 error_at (make_location_t (e->loc), "non-constant expression %qs",
3063 e->toChars ());
3064 return error_mark_node;
3065 }
3066
3067 return expr;
3068 }
3069
3070 /* Same as build_expr, but also calls destructors on any temporaries. */
3071
3072 tree
3073 build_expr_dtor (Expression *e)
3074 {
3075 /* Codegen can be improved by determining if no exceptions can be thrown
3076 between the ctor and dtor, and eliminating the ctor and dtor. */
3077 size_t saved_vars = vec_safe_length (d_function_chain->vars_in_scope);
3078 tree result = build_expr (e);
3079
3080 if (saved_vars != vec_safe_length (d_function_chain->vars_in_scope))
3081 {
3082 result = fold_build_cleanup_point_expr (TREE_TYPE (result), result);
3083 vec_safe_truncate (d_function_chain->vars_in_scope, saved_vars);
3084 }
3085
3086 return result;
3087 }
3088
3089 /* Same as build_expr_dtor, but handles the result of E as a return value. */
3090
3091 tree
3092 build_return_dtor (Expression *e, Type *type, TypeFunction *tf)
3093 {
3094 size_t saved_vars = vec_safe_length (d_function_chain->vars_in_scope);
3095 tree result = build_expr (e);
3096
3097 /* Convert for initializing the DECL_RESULT. */
3098 if (tf->isRef ())
3099 {
3100 /* If we are returning a reference, take the address. */
3101 result = convert_expr (result, e->type, type);
3102 result = build_address (result);
3103 }
3104 else
3105 result = convert_for_rvalue (result, e->type, type);
3106
3107 /* The decl to store the return expression. */
3108 tree decl = DECL_RESULT (cfun->decl);
3109
3110 /* Split comma expressions, so that the result is returned directly. */
3111 tree expr = stabilize_expr (&result);
3112 result = build_assign (INIT_EXPR, decl, result);
3113 result = compound_expr (expr, return_expr (result));
3114
3115 /* May nest the return expression inside the try/finally expression. */
3116 if (saved_vars != vec_safe_length (d_function_chain->vars_in_scope))
3117 {
3118 result = fold_build_cleanup_point_expr (TREE_TYPE (result), result);
3119 vec_safe_truncate (d_function_chain->vars_in_scope, saved_vars);
3120 }
3121
3122 return result;
3123 }
3124