+/* For element type ELT_TYPE, return the appropriate type of the heap object
+ containing such element(s). COOKIE_SIZE is NULL or the size of cookie
+ in bytes. FULL_SIZE is NULL if it is unknown how big the heap allocation
+ will be, otherwise size of the heap object. If COOKIE_SIZE is NULL,
+ return array type ELT_TYPE[FULL_SIZE / sizeof(ELT_TYPE)], otherwise return
+ struct { size_t[COOKIE_SIZE/sizeof(size_t)]; ELT_TYPE[N]; }
+ where N is nothing (flexible array member) if FULL_SIZE is NULL, otherwise
+ it is computed such that the size of the struct fits into FULL_SIZE. */
+
+tree
+build_new_constexpr_heap_type (tree elt_type, tree cookie_size, tree full_size)
+{
+ gcc_assert (cookie_size == NULL_TREE || tree_fits_uhwi_p (cookie_size));
+ gcc_assert (full_size == NULL_TREE || tree_fits_uhwi_p (full_size));
+ unsigned HOST_WIDE_INT csz = cookie_size ? tree_to_uhwi (cookie_size) : 0;
+ tree itype2 = NULL_TREE;
+ if (full_size)
+ {
+ unsigned HOST_WIDE_INT fsz = tree_to_uhwi (full_size);
+ gcc_assert (fsz >= csz);
+ fsz -= csz;
+ fsz /= int_size_in_bytes (elt_type);
+ itype2 = build_index_type (size_int (fsz - 1));
+ if (!cookie_size)
+ return build_cplus_array_type (elt_type, itype2);
+ }
+ else
+ gcc_assert (cookie_size);
+ csz /= int_size_in_bytes (sizetype);
+ tree itype1 = build_index_type (size_int (csz - 1));
+ tree atype1 = build_cplus_array_type (sizetype, itype1);
+ tree atype2 = build_cplus_array_type (elt_type, itype2);
+ tree rtype = cxx_make_type (RECORD_TYPE);
+ TYPE_NAME (rtype) = heap_identifier;
+ tree fld1 = build_decl (UNKNOWN_LOCATION, FIELD_DECL, NULL_TREE, atype1);
+ tree fld2 = build_decl (UNKNOWN_LOCATION, FIELD_DECL, NULL_TREE, atype2);
+ DECL_FIELD_CONTEXT (fld1) = rtype;
+ DECL_FIELD_CONTEXT (fld2) = rtype;
+ DECL_ARTIFICIAL (fld1) = true;
+ DECL_ARTIFICIAL (fld2) = true;
+ TYPE_FIELDS (rtype) = fld1;
+ DECL_CHAIN (fld1) = fld2;
+ layout_type (rtype);
+ return rtype;
+}
+
+/* Help the constexpr code to find the right type for the heap variable
+ by adding a NOP_EXPR around ALLOC_CALL if needed for cookie_size.
+ Return ALLOC_CALL or ALLOC_CALL cast to a pointer to
+ struct { size_t[cookie_size/sizeof(size_t)]; elt_type[]; }. */
+
+static tree
+maybe_wrap_new_for_constexpr (tree alloc_call, tree elt_type, tree cookie_size)
+{
+ if (cxx_dialect < cxx2a)
+ return alloc_call;
+
+ if (current_function_decl != NULL_TREE
+ && !DECL_DECLARED_CONSTEXPR_P (current_function_decl))
+ return alloc_call;
+
+ tree call_expr = extract_call_expr (alloc_call);
+ if (call_expr == error_mark_node)
+ return alloc_call;
+
+ tree alloc_call_fndecl = cp_get_callee_fndecl_nofold (call_expr);
+ if (alloc_call_fndecl == NULL_TREE
+ || !IDENTIFIER_NEW_OP_P (DECL_NAME (alloc_call_fndecl))
+ || CP_DECL_CONTEXT (alloc_call_fndecl) != global_namespace)
+ return alloc_call;
+
+ tree rtype = build_new_constexpr_heap_type (elt_type, cookie_size,
+ NULL_TREE);
+ return build_nop (build_pointer_type (rtype), alloc_call);
+}
+