return build_constructor (build_ctype (type), elms);
}
+/* Build a static array of type TYPE from an array of EXPS.
+ If CONST_P is true, then all elements in EXPS are constants. */
+
+tree
+build_array_from_exprs (Type *type, Expressions *exps, bool const_p)
+{
+ /* Build a CONSTRUCTOR from all expressions. */
+ vec <constructor_elt, va_gc> *elms = NULL;
+ vec_safe_reserve (elms, exps->length);
+
+ Type *etype = type->nextOf ();
+ tree satype = make_array_type (etype, exps->length);
+
+ for (size_t i = 0; i < exps->length; i++)
+ {
+ Expression *expr = (*exps)[i];
+ tree t = build_expr (expr, const_p);
+ CONSTRUCTOR_APPEND_ELT (elms, size_int (i),
+ convert_expr (t, expr->type, etype));
+ }
+
+ /* Create a new temporary to store the array. */
+ tree var = build_local_temp (satype);
+
+ /* Fill any alignment holes with zeroes. */
+ TypeStruct *ts = etype->baseElemOf ()->isTypeStruct ();
+ tree init = NULL;
+ if (ts && (!identity_compare_p (ts->sym) || ts->sym->isUnionDeclaration ()))
+ init = build_memset_call (var);
+
+ /* Initialize the temporary. */
+ tree assign = modify_expr (var, build_constructor (satype, elms));
+ return compound_expr (compound_expr (init, assign), var);
+}
+
+
/* Implicitly converts void* T to byte* as D allows { void[] a; &a[3]; } */
tree
extern tree build_memref (tree, tree, tree);
extern tree build_array_set (tree, tree, tree);
extern tree build_array_from_val (Type *, tree);
+extern tree build_array_from_exprs (Type *, Expressions *, bool);
extern tree void_okay_p (tree);
extern tree build_array_bounds_call (const Loc &);
extern tree build_bounds_condition (const Loc &, tree, tree, bool);
/* Build an expression that assigns all expressions in KEYS
to a constructor. */
- vec <constructor_elt, va_gc> *kelts = NULL;
- vec_safe_reserve (kelts, e->keys->length);
- for (size_t i = 0; i < e->keys->length; i++)
- {
- Expression *key = (*e->keys)[i];
- tree t = build_expr (key);
- CONSTRUCTOR_APPEND_ELT (kelts, size_int (i),
- convert_expr (t, key->type, ta->index));
- }
- tree tkeys = make_array_type (ta->index, e->keys->length);
- tree akeys = build_constructor (tkeys, kelts);
+ tree akeys = build_array_from_exprs (ta->index->sarrayOf (e->keys->length),
+ e->keys, this->constp_);
+ tree init = stabilize_expr (&akeys);
/* Do the same with all expressions in VALUES. */
- vec <constructor_elt, va_gc> *velts = NULL;
- vec_safe_reserve (velts, e->values->length);
- for (size_t i = 0; i < e->values->length; i++)
- {
- Expression *value = (*e->values)[i];
- tree t = build_expr (value);
- CONSTRUCTOR_APPEND_ELT (velts, size_int (i),
- convert_expr (t, value->type, ta->next));
- }
- tree tvals = make_array_type (ta->next, e->values->length);
- tree avals = build_constructor (tvals, velts);
+ tree avals = build_array_from_exprs (ta->next->sarrayOf (e->values->length),
+ e->values, this->constp_);
+ init = compound_expr (init, stabilize_expr (&avals));
/* Generate: _d_assocarrayliteralTX (ti, keys, vals); */
tree keys = d_array_value (build_ctype (ta->index->arrayOf ()),
vec <constructor_elt, va_gc> *ce = NULL;
CONSTRUCTOR_APPEND_ELT (ce, TYPE_FIELDS (aatype), mem);
- this->result_ = build_nop (build_ctype (e->type),
- build_constructor (aatype, ce));
+ tree result = build_nop (build_ctype (e->type),
+ build_constructor (aatype, ce));
+ this->result_ = compound_expr (init, result);
}
/* Build a struct literal. */
--- /dev/null
+// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96152
+// { dg-additional-options "-fmain -funittest" }
+// { dg-do run { target hw } }
+// { dg-skip-if "needs gcc/config.d" { ! d_runtime } }
+auto assocArray(Keys, Values)(Keys keys, Values values)
+{
+ void* aa;
+ {
+ if (values.length > keys.length)
+ values = values[0 .. keys.length];
+ else if (keys.length > values.length)
+ keys = keys[0 .. values.length];
+ aa = aaLiteral(keys, values);
+ }
+ alias Key = typeof(keys[0]);
+ alias Value = typeof(values[0]);
+ return (() @trusted => cast(Value[Key]) aa)();
+}
+
+@safe unittest
+{
+ struct ThrowingElement
+ {
+ int i;
+ static bool b;
+ ~this(){
+ if (b)
+ throw new Exception("");
+ }
+ }
+ assert(assocArray([ThrowingElement()], [0]) == [ThrowingElement(): 0]);
+}