]>
Commit | Line | Data |
---|---|---|
471086d6 | 1 | /* Report error messages, build initializers, and perform |
2 | some front-end optimizations for C++ compiler. | |
fbd26352 | 3 | Copyright (C) 1987-2019 Free Software Foundation, Inc. |
471086d6 | 4 | Hacked by Michael Tiemann (tiemann@cygnus.com) |
5 | ||
6f0d25a6 | 6 | This file is part of GCC. |
471086d6 | 7 | |
6f0d25a6 | 8 | GCC is free software; you can redistribute it and/or modify |
471086d6 | 9 | it under the terms of the GNU General Public License as published by |
aa139c3f | 10 | the Free Software Foundation; either version 3, or (at your option) |
471086d6 | 11 | any later version. |
12 | ||
6f0d25a6 | 13 | GCC is distributed in the hope that it will be useful, |
471086d6 | 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
aa139c3f | 19 | along with GCC; see the file COPYING3. If not see |
20 | <http://www.gnu.org/licenses/>. */ | |
471086d6 | 21 | |
22 | ||
23 | /* This file is part of the C++ front end. | |
24 | It contains routines to build C++ expressions given their operands, | |
25 | including computing the types of the result, C and C++ specific error | |
c857cd60 | 26 | checks, and some optimization. */ |
471086d6 | 27 | |
28 | #include "config.h" | |
b3ef7553 | 29 | #include "system.h" |
805e22b2 | 30 | #include "coretypes.h" |
4cba6f60 | 31 | #include "cp-tree.h" |
9ed99284 | 32 | #include "stor-layout.h" |
33 | #include "varasm.h" | |
5c892d4a | 34 | #include "intl.h" |
6639e735 | 35 | #include "gcc-rich-location.h" |
471086d6 | 36 | |
c75b4594 | 37 | static tree |
c38103e8 | 38 | process_init_constructor (tree type, tree init, int nested, int flags, |
7aac6ee6 | 39 | tsubst_flags_t complain); |
c75b4594 | 40 | |
471086d6 | 41 | |
471086d6 | 42 | /* Print an error message stemming from an attempt to use |
43 | BASETYPE as a base class for TYPE. */ | |
96624a9e | 44 | |
471086d6 | 45 | tree |
e394ed0f | 46 | error_not_base_type (tree basetype, tree type) |
471086d6 | 47 | { |
48 | if (TREE_CODE (basetype) == FUNCTION_DECL) | |
9ba4048d | 49 | basetype = DECL_CONTEXT (basetype); |
03d6ca9e | 50 | error ("type %qT is not a base type for type %qT", basetype, type); |
471086d6 | 51 | return error_mark_node; |
52 | } | |
53 | ||
54 | tree | |
e394ed0f | 55 | binfo_or_else (tree base, tree type) |
471086d6 | 56 | { |
ae260dcc | 57 | tree binfo = lookup_base (type, base, ba_unique, |
58 | NULL, tf_warning_or_error); | |
7e6960e0 | 59 | |
60 | if (binfo == error_mark_node) | |
61 | return NULL_TREE; | |
62 | else if (!binfo) | |
63 | error_not_base_type (base, type); | |
64 | return binfo; | |
471086d6 | 65 | } |
66 | ||
471086d6 | 67 | /* According to ARM $7.1.6, "A `const' object may be initialized, but its |
2c592404 | 68 | value may not be changed thereafter. */ |
96624a9e | 69 | |
471086d6 | 70 | void |
91e06df2 | 71 | cxx_readonly_error (location_t loc, tree arg, enum lvalue_use errstring) |
471086d6 | 72 | { |
5c892d4a | 73 | |
74 | /* This macro is used to emit diagnostics to ensure that all format | |
75 | strings are complete sentences, visible to gettext and checked at | |
76 | compile time. */ | |
77 | ||
91e06df2 | 78 | #define ERROR_FOR_ASSIGNMENT(LOC, AS, ASM, IN, DE, ARG) \ |
5c892d4a | 79 | do { \ |
80 | switch (errstring) \ | |
81 | { \ | |
a1f90215 | 82 | case lv_assign: \ |
91e06df2 | 83 | error_at (LOC, AS, ARG); \ |
5c892d4a | 84 | break; \ |
a1f90215 | 85 | case lv_asm: \ |
91e06df2 | 86 | error_at (LOC, ASM, ARG); \ |
5c892d4a | 87 | break; \ |
a1f90215 | 88 | case lv_increment: \ |
91e06df2 | 89 | error_at (LOC, IN, ARG); \ |
5c892d4a | 90 | break; \ |
91e06df2 | 91 | case lv_decrement: \ |
92 | error_at (LOC, DE, ARG); \ | |
5c892d4a | 93 | break; \ |
94 | default: \ | |
95 | gcc_unreachable (); \ | |
96 | } \ | |
97 | } while (0) | |
471086d6 | 98 | |
a1f90215 | 99 | /* Handle C++-specific things first. */ |
100 | ||
80a58eb0 | 101 | if (VAR_P (arg) |
a1f90215 | 102 | && DECL_LANG_SPECIFIC (arg) |
103 | && DECL_IN_AGGR_P (arg) | |
104 | && !TREE_STATIC (arg)) | |
91e06df2 | 105 | ERROR_FOR_ASSIGNMENT (loc, |
106 | G_("assignment of constant field %qD"), | |
107 | G_("constant field %qD used as %<asm%> output"), | |
108 | G_("increment of constant field %qD"), | |
109 | G_("decrement of constant field %qD"), | |
a1f90215 | 110 | arg); |
86ccc124 | 111 | else if (INDIRECT_REF_P (arg) |
90ad495b | 112 | && TYPE_REF_P (TREE_TYPE (TREE_OPERAND (arg, 0))) |
80a58eb0 | 113 | && (VAR_P (TREE_OPERAND (arg, 0)) |
653e5405 | 114 | || TREE_CODE (TREE_OPERAND (arg, 0)) == PARM_DECL)) |
91e06df2 | 115 | ERROR_FOR_ASSIGNMENT (loc, |
116 | G_("assignment of read-only reference %qD"), | |
117 | G_("read-only reference %qD used as %<asm%> output"), | |
118 | G_("increment of read-only reference %qD"), | |
119 | G_("decrement of read-only reference %qD"), | |
120 | TREE_OPERAND (arg, 0)); | |
938b347c | 121 | else |
91e06df2 | 122 | readonly_error (loc, arg, errstring); |
471086d6 | 123 | } |
d97a7640 | 124 | \f |
125 | /* Structure that holds information about declarations whose type was | |
126 | incomplete and we could not check whether it was abstract or not. */ | |
127 | ||
2ef51f0e | 128 | struct GTY((chain_next ("%h.next"), for_user)) pending_abstract_type { |
d97a7640 | 129 | /* Declaration which we are checking for abstractness. It is either |
130 | a DECL node, or an IDENTIFIER_NODE if we do not have a full | |
131 | declaration available. */ | |
132 | tree decl; | |
133 | ||
134 | /* Type which will be checked for abstractness. */ | |
135 | tree type; | |
136 | ||
d28993f1 | 137 | /* Kind of use in an unnamed declarator. */ |
6dc50383 | 138 | enum abstract_class_use use; |
d28993f1 | 139 | |
d97a7640 | 140 | /* Position of the declaration. This is only needed for IDENTIFIER_NODEs, |
141 | because DECLs already carry locus information. */ | |
142 | location_t locus; | |
143 | ||
144 | /* Link to the next element in list. */ | |
145 | struct pending_abstract_type* next; | |
146 | }; | |
147 | ||
b594087e | 148 | struct abstract_type_hasher : ggc_ptr_hash<pending_abstract_type> |
2ef51f0e | 149 | { |
150 | typedef tree compare_type; | |
151 | static hashval_t hash (pending_abstract_type *); | |
152 | static bool equal (pending_abstract_type *, tree); | |
153 | }; | |
d97a7640 | 154 | |
155 | /* Compute the hash value of the node VAL. This function is used by the | |
156 | hash table abstract_pending_vars. */ | |
157 | ||
2ef51f0e | 158 | hashval_t |
159 | abstract_type_hasher::hash (pending_abstract_type *pat) | |
d97a7640 | 160 | { |
d97a7640 | 161 | return (hashval_t) TYPE_UID (pat->type); |
162 | } | |
163 | ||
164 | ||
165 | /* Compare node VAL1 with the type VAL2. This function is used by the | |
166 | hash table abstract_pending_vars. */ | |
167 | ||
2ef51f0e | 168 | bool |
169 | abstract_type_hasher::equal (pending_abstract_type *pat1, tree type2) | |
d97a7640 | 170 | { |
d97a7640 | 171 | return (pat1->type == type2); |
172 | } | |
173 | ||
174 | /* Hash table that maintains pending_abstract_type nodes, for which we still | |
175 | need to check for type abstractness. The key of the table is the type | |
176 | of the declaration. */ | |
2ef51f0e | 177 | static GTY (()) hash_table<abstract_type_hasher> *abstract_pending_vars = NULL; |
d97a7640 | 178 | |
d28993f1 | 179 | static int abstract_virtuals_error_sfinae (tree, tree, abstract_class_use, tsubst_flags_t); |
d97a7640 | 180 | |
181 | /* This function is called after TYPE is completed, and will check if there | |
182 | are pending declarations for which we still need to verify the abstractness | |
183 | of TYPE, and emit a diagnostic (through abstract_virtuals_error) if TYPE | |
184 | turned out to be incomplete. */ | |
185 | ||
186 | void | |
187 | complete_type_check_abstract (tree type) | |
188 | { | |
d97a7640 | 189 | struct pending_abstract_type *pat; |
190 | location_t cur_loc = input_location; | |
191 | ||
b4df430b | 192 | gcc_assert (COMPLETE_TYPE_P (type)); |
d97a7640 | 193 | |
194 | if (!abstract_pending_vars) | |
195 | return; | |
196 | ||
197 | /* Retrieve the list of pending declarations for this type. */ | |
2ef51f0e | 198 | pending_abstract_type **slot |
199 | = abstract_pending_vars->find_slot_with_hash (type, TYPE_UID (type), | |
200 | NO_INSERT); | |
d97a7640 | 201 | if (!slot) |
202 | return; | |
2ef51f0e | 203 | pat = *slot; |
b4df430b | 204 | gcc_assert (pat); |
d97a7640 | 205 | |
206 | /* If the type is not abstract, do not do anything. */ | |
207 | if (CLASSTYPE_PURE_VIRTUALS (type)) | |
208 | { | |
209 | struct pending_abstract_type *prev = 0, *next; | |
210 | ||
211 | /* Reverse the list to emit the errors in top-down order. */ | |
212 | for (; pat; pat = next) | |
213 | { | |
214 | next = pat->next; | |
215 | pat->next = prev; | |
216 | prev = pat; | |
217 | } | |
218 | pat = prev; | |
219 | ||
220 | /* Go through the list, and call abstract_virtuals_error for each | |
4a44ba29 | 221 | element: it will issue a diagnostic if the type is abstract. */ |
d97a7640 | 222 | while (pat) |
223 | { | |
b4df430b | 224 | gcc_assert (type == pat->type); |
d97a7640 | 225 | |
226 | /* Tweak input_location so that the diagnostic appears at the correct | |
227 | location. Notice that this is only needed if the decl is an | |
3cf8b391 | 228 | IDENTIFIER_NODE. */ |
d97a7640 | 229 | input_location = pat->locus; |
d28993f1 | 230 | abstract_virtuals_error_sfinae (pat->decl, pat->type, pat->use, |
231 | tf_warning_or_error); | |
d97a7640 | 232 | pat = pat->next; |
233 | } | |
234 | } | |
235 | ||
2ef51f0e | 236 | abstract_pending_vars->clear_slot (slot); |
d97a7640 | 237 | |
238 | input_location = cur_loc; | |
239 | } | |
240 | ||
241 | ||
8c18e707 | 242 | /* If TYPE has abstract virtual functions, issue an error about trying |
243 | to create an object of that type. DECL is the object declared, or | |
d28993f1 | 244 | NULL_TREE if the declaration is unavailable, in which case USE specifies |
245 | the kind of invalid use. Returns 1 if an error occurred; zero if | |
246 | all was well. */ | |
96624a9e | 247 | |
d28993f1 | 248 | static int |
249 | abstract_virtuals_error_sfinae (tree decl, tree type, abstract_class_use use, | |
250 | tsubst_flags_t complain) | |
471086d6 | 251 | { |
f1f41a6c | 252 | vec<tree, va_gc> *pure; |
9031d10b | 253 | |
d97a7640 | 254 | /* This function applies only to classes. Any other entity can never |
255 | be abstract. */ | |
256 | if (!CLASS_TYPE_P (type)) | |
257 | return 0; | |
013143e0 | 258 | type = TYPE_MAIN_VARIANT (type); |
d97a7640 | 259 | |
5fc994dd | 260 | #if 0 |
261 | /* Instantiation here seems to be required by the standard, | |
262 | but breaks e.g. boost::bind. FIXME! */ | |
fac5b155 | 263 | /* In SFINAE, non-N3276 context, force instantiation. */ |
264 | if (!(complain & (tf_error|tf_decltype))) | |
f8f092de | 265 | complete_type (type); |
5fc994dd | 266 | #endif |
f8f092de | 267 | |
d97a7640 | 268 | /* If the type is incomplete, we register it within a hash table, |
269 | so that we can check again once it is completed. This makes sense | |
270 | only for objects for which we have a declaration or at least a | |
271 | name. */ | |
fac5b155 | 272 | if (!COMPLETE_TYPE_P (type) && (complain & tf_error)) |
d97a7640 | 273 | { |
d97a7640 | 274 | struct pending_abstract_type *pat; |
275 | ||
694683bb | 276 | gcc_assert (!decl || DECL_P (decl) || identifier_p (decl)); |
d97a7640 | 277 | |
278 | if (!abstract_pending_vars) | |
2ef51f0e | 279 | abstract_pending_vars |
280 | = hash_table<abstract_type_hasher>::create_ggc (31); | |
d97a7640 | 281 | |
2ef51f0e | 282 | pending_abstract_type **slot |
283 | = abstract_pending_vars->find_slot_with_hash (type, TYPE_UID (type), | |
284 | INSERT); | |
d97a7640 | 285 | |
25a27413 | 286 | pat = ggc_alloc<pending_abstract_type> (); |
d97a7640 | 287 | pat->type = type; |
288 | pat->decl = decl; | |
d28993f1 | 289 | pat->use = use; |
d97a7640 | 290 | pat->locus = ((decl && DECL_P (decl)) |
291 | ? DECL_SOURCE_LOCATION (decl) | |
292 | : input_location); | |
293 | ||
2ef51f0e | 294 | pat->next = *slot; |
d97a7640 | 295 | *slot = pat; |
296 | ||
297 | return 0; | |
298 | } | |
299 | ||
5f30d323 | 300 | if (!TYPE_SIZE (type)) |
301 | /* TYPE is being defined, and during that time | |
302 | CLASSTYPE_PURE_VIRTUALS holds the inline friends. */ | |
303 | return 0; | |
304 | ||
03106e7d | 305 | pure = CLASSTYPE_PURE_VIRTUALS (type); |
306 | if (!pure) | |
307 | return 0; | |
308 | ||
70cac69d | 309 | if (!(complain & tf_error)) |
310 | return 1; | |
311 | ||
bc35ef65 | 312 | auto_diagnostic_group d; |
471086d6 | 313 | if (decl) |
314 | { | |
80a58eb0 | 315 | if (VAR_P (decl)) |
3cf8b391 | 316 | error ("cannot declare variable %q+D to be of abstract " |
317 | "type %qT", decl, type); | |
471086d6 | 318 | else if (TREE_CODE (decl) == PARM_DECL) |
d28993f1 | 319 | { |
320 | if (DECL_NAME (decl)) | |
321 | error ("cannot declare parameter %q+D to be of abstract type %qT", | |
322 | decl, type); | |
323 | else | |
324 | error ("cannot declare parameter to be of abstract type %qT", | |
325 | type); | |
326 | } | |
471086d6 | 327 | else if (TREE_CODE (decl) == FIELD_DECL) |
3cf8b391 | 328 | error ("cannot declare field %q+D to be of abstract type %qT", |
329 | decl, type); | |
471086d6 | 330 | else if (TREE_CODE (decl) == FUNCTION_DECL |
331 | && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE) | |
3cf8b391 | 332 | error ("invalid abstract return type for member function %q+#D", decl); |
471086d6 | 333 | else if (TREE_CODE (decl) == FUNCTION_DECL) |
3cf8b391 | 334 | error ("invalid abstract return type for function %q+#D", decl); |
694683bb | 335 | else if (identifier_p (decl)) |
3cf8b391 | 336 | /* Here we do not have location information. */ |
03d6ca9e | 337 | error ("invalid abstract type %qT for %qE", type, decl); |
8837568f | 338 | else |
3cf8b391 | 339 | error ("invalid abstract type for %q+D", decl); |
471086d6 | 340 | } |
d28993f1 | 341 | else switch (use) |
342 | { | |
343 | case ACU_ARRAY: | |
344 | error ("creating array of %qT, which is an abstract class type", type); | |
345 | break; | |
346 | case ACU_CAST: | |
347 | error ("invalid cast to abstract class type %qT", type); | |
348 | break; | |
349 | case ACU_NEW: | |
350 | error ("invalid new-expression of abstract class type %qT", type); | |
351 | break; | |
352 | case ACU_RETURN: | |
353 | error ("invalid abstract return type %qT", type); | |
354 | break; | |
355 | case ACU_PARM: | |
356 | error ("invalid abstract parameter type %qT", type); | |
357 | break; | |
358 | case ACU_THROW: | |
359 | error ("expression of abstract class type %qT cannot " | |
360 | "be used in throw-expression", type); | |
361 | break; | |
362 | case ACU_CATCH: | |
85b9be9b | 363 | error ("cannot declare %<catch%> parameter to be of abstract " |
d28993f1 | 364 | "class type %qT", type); |
365 | break; | |
366 | default: | |
367 | error ("cannot allocate an object of abstract type %qT", type); | |
368 | } | |
6588242c | 369 | |
471086d6 | 370 | /* Only go through this once. */ |
f1f41a6c | 371 | if (pure->length ()) |
471086d6 | 372 | { |
03106e7d | 373 | unsigned ix; |
374 | tree fn; | |
9031d10b | 375 | |
712d2297 | 376 | inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)), |
377 | " because the following virtual functions are pure within %qT:", | |
378 | type); | |
8837568f | 379 | |
f1f41a6c | 380 | FOR_EACH_VEC_ELT (*pure, ix, fn) |
bd97209d | 381 | if (! DECL_CLONED_FUNCTION_P (fn) |
382 | || DECL_COMPLETE_DESTRUCTOR_P (fn)) | |
85b9be9b | 383 | inform (DECL_SOURCE_LOCATION (fn), " %#qD", fn); |
bd97209d | 384 | |
03106e7d | 385 | /* Now truncate the vector. This leaves it non-null, so we know |
653e5405 | 386 | there are pure virtuals, but empty so we don't list them out |
387 | again. */ | |
f1f41a6c | 388 | pure->truncate (0); |
471086d6 | 389 | } |
8c18e707 | 390 | |
391 | return 1; | |
471086d6 | 392 | } |
393 | ||
d28993f1 | 394 | int |
395 | abstract_virtuals_error_sfinae (tree decl, tree type, tsubst_flags_t complain) | |
396 | { | |
397 | return abstract_virtuals_error_sfinae (decl, type, ACU_UNKNOWN, complain); | |
398 | } | |
399 | ||
400 | int | |
401 | abstract_virtuals_error_sfinae (abstract_class_use use, tree type, | |
402 | tsubst_flags_t complain) | |
403 | { | |
404 | return abstract_virtuals_error_sfinae (NULL_TREE, type, use, complain); | |
405 | } | |
406 | ||
407 | ||
70cac69d | 408 | /* Wrapper for the above function in the common case of wanting errors. */ |
409 | ||
410 | int | |
411 | abstract_virtuals_error (tree decl, tree type) | |
412 | { | |
413 | return abstract_virtuals_error_sfinae (decl, type, tf_warning_or_error); | |
414 | } | |
415 | ||
d28993f1 | 416 | int |
417 | abstract_virtuals_error (abstract_class_use use, tree type) | |
418 | { | |
419 | return abstract_virtuals_error_sfinae (use, type, tf_warning_or_error); | |
420 | } | |
421 | ||
775b9c9f | 422 | /* Print an inform about the declaration of the incomplete type TYPE. */ |
423 | ||
424 | void | |
425 | cxx_incomplete_type_inform (const_tree type) | |
426 | { | |
5444a0b4 | 427 | if (!TYPE_MAIN_DECL (type)) |
428 | return; | |
429 | ||
775b9c9f | 430 | location_t loc = DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)); |
431 | tree ptype = strip_top_quals (CONST_CAST_TREE (type)); | |
432 | ||
433 | if (current_class_type | |
434 | && TYPE_BEING_DEFINED (current_class_type) | |
435 | && same_type_p (ptype, current_class_type)) | |
436 | inform (loc, "definition of %q#T is not complete until " | |
437 | "the closing brace", ptype); | |
438 | else if (!TYPE_TEMPLATE_INFO (ptype)) | |
439 | inform (loc, "forward declaration of %q#T", ptype); | |
440 | else | |
441 | inform (loc, "declaration of %q#T", ptype); | |
442 | } | |
443 | ||
471086d6 | 444 | /* Print an error message for invalid use of an incomplete type. |
445 | VALUE is the expression that was used (or 0 if that isn't known) | |
a52d5726 | 446 | and TYPE is the type that was invalid. DIAG_KIND indicates the |
447 | type of diagnostic (see diagnostic.def). */ | |
471086d6 | 448 | |
449 | void | |
22a3f7bd | 450 | cxx_incomplete_type_diagnostic (location_t loc, const_tree value, |
451 | const_tree type, diagnostic_t diag_kind) | |
471086d6 | 452 | { |
a4d705f9 | 453 | bool is_decl = false, complained = false; |
e097fb33 | 454 | |
a52d5726 | 455 | gcc_assert (diag_kind == DK_WARNING |
456 | || diag_kind == DK_PEDWARN | |
457 | || diag_kind == DK_ERROR); | |
9031d10b | 458 | |
471086d6 | 459 | /* Avoid duplicate error message. */ |
460 | if (TREE_CODE (type) == ERROR_MARK) | |
461 | return; | |
462 | ||
d582d140 | 463 | if (value) |
313d903f | 464 | { |
d582d140 | 465 | STRIP_ANY_LOCATION_WRAPPER (value); |
466 | ||
467 | if (VAR_P (value) | |
468 | || TREE_CODE (value) == PARM_DECL | |
469 | || TREE_CODE (value) == FIELD_DECL) | |
470 | { | |
471 | complained = emit_diagnostic (diag_kind, DECL_SOURCE_LOCATION (value), 0, | |
472 | "%qD has incomplete type", value); | |
473 | is_decl = true; | |
474 | } | |
475 | } | |
2e3e31d2 | 476 | retry: |
aeef2be5 | 477 | /* We must print an error message. Be clever about what it says. */ |
478 | ||
479 | switch (TREE_CODE (type)) | |
471086d6 | 480 | { |
aeef2be5 | 481 | case RECORD_TYPE: |
482 | case UNION_TYPE: | |
483 | case ENUMERAL_TYPE: | |
a4d705f9 | 484 | if (!is_decl) |
d09c3780 | 485 | complained = emit_diagnostic (diag_kind, loc, 0, |
a4d705f9 | 486 | "invalid use of incomplete type %q#T", |
487 | type); | |
488 | if (complained) | |
775b9c9f | 489 | cxx_incomplete_type_inform (type); |
aeef2be5 | 490 | break; |
491 | ||
492 | case VOID_TYPE: | |
d09c3780 | 493 | emit_diagnostic (diag_kind, loc, 0, |
a52d5726 | 494 | "invalid use of %qT", type); |
aeef2be5 | 495 | break; |
496 | ||
497 | case ARRAY_TYPE: | |
498 | if (TYPE_DOMAIN (type)) | |
653e5405 | 499 | { |
500 | type = TREE_TYPE (type); | |
501 | goto retry; | |
502 | } | |
d09c3780 | 503 | emit_diagnostic (diag_kind, loc, 0, |
a52d5726 | 504 | "invalid use of array with unspecified bounds"); |
aeef2be5 | 505 | break; |
506 | ||
507 | case OFFSET_TYPE: | |
508 | bad_member: | |
5f72d880 | 509 | { |
126b78a0 | 510 | tree member = TREE_OPERAND (value, 1); |
511 | if (is_overloaded_fn (member)) | |
512 | member = get_first_fn (member); | |
bc9c1170 | 513 | |
5f72d880 | 514 | if (DECL_FUNCTION_MEMBER_P (member) |
515 | && ! flag_ms_extensions) | |
6639e735 | 516 | { |
517 | gcc_rich_location richloc (loc); | |
518 | /* If "member" has no arguments (other than "this"), then | |
519 | add a fix-it hint. */ | |
520 | if (type_num_arguments (TREE_TYPE (member)) == 1) | |
521 | richloc.add_fixit_insert_after ("()"); | |
522 | emit_diagnostic (diag_kind, &richloc, 0, | |
523 | "invalid use of member function %qD " | |
524 | "(did you forget the %<()%> ?)", member); | |
525 | } | |
5f72d880 | 526 | else |
d09c3780 | 527 | emit_diagnostic (diag_kind, loc, 0, |
442e766c | 528 | "invalid use of member %qD " |
529 | "(did you forget the %<&%> ?)", member); | |
5f72d880 | 530 | } |
aeef2be5 | 531 | break; |
532 | ||
533 | case TEMPLATE_TYPE_PARM: | |
46f4817e | 534 | if (is_auto (type)) |
11aaa98e | 535 | { |
536 | if (CLASS_PLACEHOLDER_TEMPLATE (type)) | |
537 | emit_diagnostic (diag_kind, loc, 0, | |
538 | "invalid use of placeholder %qT", type); | |
539 | else | |
540 | emit_diagnostic (diag_kind, loc, 0, | |
541 | "invalid use of %qT", type); | |
542 | } | |
46f4817e | 543 | else |
d09c3780 | 544 | emit_diagnostic (diag_kind, loc, 0, |
46f4817e | 545 | "invalid use of template type parameter %qT", type); |
8a757045 | 546 | break; |
547 | ||
548 | case BOUND_TEMPLATE_TEMPLATE_PARM: | |
d09c3780 | 549 | emit_diagnostic (diag_kind, loc, 0, |
a52d5726 | 550 | "invalid use of template template parameter %qT", |
551 | TYPE_NAME (type)); | |
aeef2be5 | 552 | break; |
553 | ||
3de2e08e | 554 | case TYPENAME_TYPE: |
24011c2c | 555 | case DECLTYPE_TYPE: |
d09c3780 | 556 | emit_diagnostic (diag_kind, loc, 0, |
a52d5726 | 557 | "invalid use of dependent type %qT", type); |
3de2e08e | 558 | break; |
559 | ||
4fdaf896 | 560 | case LANG_TYPE: |
54c44343 | 561 | if (type == init_list_type_node) |
562 | { | |
d09c3780 | 563 | emit_diagnostic (diag_kind, loc, 0, |
54c44343 | 564 | "invalid use of brace-enclosed initializer list"); |
565 | break; | |
566 | } | |
4fdaf896 | 567 | gcc_assert (type == unknown_type_node); |
aeef2be5 | 568 | if (value && TREE_CODE (value) == COMPONENT_REF) |
653e5405 | 569 | goto bad_member; |
aeef2be5 | 570 | else if (value && TREE_CODE (value) == ADDR_EXPR) |
d09c3780 | 571 | emit_diagnostic (diag_kind, loc, 0, |
a52d5726 | 572 | "address of overloaded function with no contextual " |
573 | "type information"); | |
aeef2be5 | 574 | else if (value && TREE_CODE (value) == OVERLOAD) |
d09c3780 | 575 | emit_diagnostic (diag_kind, loc, 0, |
a52d5726 | 576 | "overloaded function with no contextual type information"); |
aeef2be5 | 577 | else |
d09c3780 | 578 | emit_diagnostic (diag_kind, loc, 0, |
a52d5726 | 579 | "insufficient contextual information to determine type"); |
aeef2be5 | 580 | break; |
9031d10b | 581 | |
aeef2be5 | 582 | default: |
2e3e31d2 | 583 | gcc_unreachable (); |
471086d6 | 584 | } |
585 | } | |
586 | ||
22a3f7bd | 587 | /* Print an error message for invalid use of an incomplete type. |
588 | VALUE is the expression that was used (or 0 if that isn't known) | |
589 | and TYPE is the type that was invalid. */ | |
590 | ||
e097fb33 | 591 | void |
22a3f7bd | 592 | cxx_incomplete_type_error (location_t loc, const_tree value, const_tree type) |
e097fb33 | 593 | { |
22a3f7bd | 594 | cxx_incomplete_type_diagnostic (loc, value, type, DK_ERROR); |
e097fb33 | 595 | } |
596 | ||
471086d6 | 597 | \f |
3afe9b43 | 598 | /* The recursive part of split_nonconstant_init. DEST is an lvalue |
927b65fb | 599 | expression to which INIT should be assigned. INIT is a CONSTRUCTOR. |
600 | Return true if the whole of the value was initialized by the | |
601 | generated statements. */ | |
3afe9b43 | 602 | |
927b65fb | 603 | static bool |
604 | split_nonconstant_init_1 (tree dest, tree init) | |
3afe9b43 | 605 | { |
fec050ac | 606 | unsigned HOST_WIDE_INT idx, tidx = HOST_WIDE_INT_M1U; |
c75b4594 | 607 | tree field_index, value; |
608 | tree type = TREE_TYPE (dest); | |
609 | tree inner_type = NULL; | |
3afe9b43 | 610 | bool array_type_p = false; |
927b65fb | 611 | bool complete_p = true; |
612 | HOST_WIDE_INT num_split_elts = 0; | |
3afe9b43 | 613 | |
3afe9b43 | 614 | switch (TREE_CODE (type)) |
615 | { | |
616 | case ARRAY_TYPE: | |
617 | inner_type = TREE_TYPE (type); | |
618 | array_type_p = true; | |
39786c80 | 619 | if ((TREE_SIDE_EFFECTS (init) |
620 | && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)) | |
0b8d327e | 621 | || vla_type_p (type)) |
39786c80 | 622 | { |
623 | /* For an array, we only need/want a single cleanup region rather | |
624 | than one per element. */ | |
625 | tree code = build_vec_init (dest, NULL_TREE, init, false, 1, | |
626 | tf_warning_or_error); | |
627 | add_stmt (code); | |
628 | return true; | |
629 | } | |
3afe9b43 | 630 | /* FALLTHRU */ |
631 | ||
632 | case RECORD_TYPE: | |
633 | case UNION_TYPE: | |
634 | case QUAL_UNION_TYPE: | |
c75b4594 | 635 | FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), idx, |
636 | field_index, value) | |
3afe9b43 | 637 | { |
c75b4594 | 638 | /* The current implementation of this algorithm assumes that |
639 | the field was set for all the elements. This is usually done | |
640 | by process_init_constructor. */ | |
641 | gcc_assert (field_index); | |
3afe9b43 | 642 | |
643 | if (!array_type_p) | |
644 | inner_type = TREE_TYPE (field_index); | |
645 | ||
646 | if (TREE_CODE (value) == CONSTRUCTOR) | |
647 | { | |
c75b4594 | 648 | tree sub; |
649 | ||
3afe9b43 | 650 | if (array_type_p) |
653e5405 | 651 | sub = build4 (ARRAY_REF, inner_type, dest, field_index, |
831d52a2 | 652 | NULL_TREE, NULL_TREE); |
3afe9b43 | 653 | else |
653e5405 | 654 | sub = build3 (COMPONENT_REF, inner_type, dest, field_index, |
831d52a2 | 655 | NULL_TREE); |
3afe9b43 | 656 | |
927b65fb | 657 | if (!split_nonconstant_init_1 (sub, value)) |
658 | complete_p = false; | |
d552a498 | 659 | else |
fec050ac | 660 | { |
661 | /* Mark element for removal. */ | |
662 | CONSTRUCTOR_ELT (init, idx)->index = NULL_TREE; | |
663 | if (idx < tidx) | |
664 | tidx = idx; | |
665 | num_split_elts++; | |
666 | } | |
3afe9b43 | 667 | } |
668 | else if (!initializer_constant_valid_p (value, inner_type)) | |
669 | { | |
c75b4594 | 670 | tree code; |
671 | tree sub; | |
672 | ||
c89f472e | 673 | /* Mark element for removal. */ |
674 | CONSTRUCTOR_ELT (init, idx)->index = NULL_TREE; | |
fec050ac | 675 | if (idx < tidx) |
676 | tidx = idx; | |
3afe9b43 | 677 | |
7e173c71 | 678 | if (TREE_CODE (field_index) == RANGE_EXPR) |
679 | { | |
680 | /* Use build_vec_init to initialize a range. */ | |
681 | tree low = TREE_OPERAND (field_index, 0); | |
682 | tree hi = TREE_OPERAND (field_index, 1); | |
683 | sub = build4 (ARRAY_REF, inner_type, dest, low, | |
684 | NULL_TREE, NULL_TREE); | |
685 | sub = cp_build_addr_expr (sub, tf_warning_or_error); | |
686 | tree max = size_binop (MINUS_EXPR, hi, low); | |
687 | code = build_vec_init (sub, max, value, false, 0, | |
688 | tf_warning_or_error); | |
689 | add_stmt (code); | |
e5827c8e | 690 | if (tree_fits_shwi_p (max)) |
691 | num_split_elts += tree_to_shwi (max); | |
7e173c71 | 692 | } |
3afe9b43 | 693 | else |
b3c93f85 | 694 | { |
7e173c71 | 695 | if (array_type_p) |
696 | sub = build4 (ARRAY_REF, inner_type, dest, field_index, | |
697 | NULL_TREE, NULL_TREE); | |
698 | else | |
699 | sub = build3 (COMPONENT_REF, inner_type, dest, field_index, | |
700 | NULL_TREE); | |
701 | ||
702 | code = build2 (INIT_EXPR, inner_type, sub, value); | |
703 | code = build_stmt (input_location, EXPR_STMT, code); | |
704 | code = maybe_cleanup_point_expr_void (code); | |
705 | add_stmt (code); | |
9b89ee44 | 706 | if (tree cleanup |
707 | = cxx_maybe_build_cleanup (sub, tf_warning_or_error)) | |
708 | finish_eh_cleanup (cleanup); | |
b3c93f85 | 709 | } |
38c55c8a | 710 | |
927b65fb | 711 | num_split_elts++; |
3afe9b43 | 712 | } |
3afe9b43 | 713 | } |
fec050ac | 714 | if (num_split_elts == 1) |
715 | CONSTRUCTOR_ELTS (init)->ordered_remove (tidx); | |
716 | else if (num_split_elts > 1) | |
c89f472e | 717 | { |
718 | /* Perform the delayed ordered removal of non-constant elements | |
719 | we split out. */ | |
fec050ac | 720 | for (idx = tidx; idx < CONSTRUCTOR_NELTS (init); ++idx) |
c89f472e | 721 | if (CONSTRUCTOR_ELT (init, idx)->index == NULL_TREE) |
722 | ; | |
723 | else | |
724 | { | |
fec050ac | 725 | *CONSTRUCTOR_ELT (init, tidx) = *CONSTRUCTOR_ELT (init, idx); |
c89f472e | 726 | ++tidx; |
727 | } | |
728 | vec_safe_truncate (CONSTRUCTOR_ELTS (init), tidx); | |
729 | } | |
3afe9b43 | 730 | break; |
731 | ||
732 | case VECTOR_TYPE: | |
733 | if (!initializer_constant_valid_p (init, type)) | |
734 | { | |
c75b4594 | 735 | tree code; |
4634db76 | 736 | tree cons = copy_node (init); |
3afe9b43 | 737 | CONSTRUCTOR_ELTS (init) = NULL; |
4634db76 | 738 | code = build2 (MODIFY_EXPR, type, dest, cons); |
e60a6f7b | 739 | code = build_stmt (input_location, EXPR_STMT, code); |
2363ef00 | 740 | add_stmt (code); |
927b65fb | 741 | num_split_elts += CONSTRUCTOR_NELTS (init); |
3afe9b43 | 742 | } |
743 | break; | |
744 | ||
745 | default: | |
2e3e31d2 | 746 | gcc_unreachable (); |
3afe9b43 | 747 | } |
52acb923 | 748 | |
749 | /* The rest of the initializer is now a constant. */ | |
750 | TREE_CONSTANT (init) = 1; | |
96fa24ad | 751 | |
752 | /* We didn't split out anything. */ | |
753 | if (num_split_elts == 0) | |
754 | return false; | |
755 | ||
927b65fb | 756 | return complete_p && complete_ctor_at_level_p (TREE_TYPE (init), |
757 | num_split_elts, inner_type); | |
3afe9b43 | 758 | } |
759 | ||
9031d10b | 760 | /* A subroutine of store_init_value. Splits non-constant static |
3afe9b43 | 761 | initializer INIT into a constant part and generates code to |
762 | perform the non-constant part of the initialization to DEST. | |
763 | Returns the code for the runtime init. */ | |
764 | ||
39786c80 | 765 | tree |
3afe9b43 | 766 | split_nonconstant_init (tree dest, tree init) |
767 | { | |
768 | tree code; | |
769 | ||
39786c80 | 770 | if (TREE_CODE (init) == TARGET_EXPR) |
771 | init = TARGET_EXPR_INITIAL (init); | |
3afe9b43 | 772 | if (TREE_CODE (init) == CONSTRUCTOR) |
773 | { | |
d560f985 | 774 | init = cp_fully_fold_init (init); |
2363ef00 | 775 | code = push_stmt_list (); |
927b65fb | 776 | if (split_nonconstant_init_1 (dest, init)) |
777 | init = NULL_TREE; | |
2363ef00 | 778 | code = pop_stmt_list (code); |
3afe9b43 | 779 | DECL_INITIAL (dest) = init; |
09742c66 | 780 | TREE_READONLY (dest) = 0; |
3afe9b43 | 781 | } |
c571b0c6 | 782 | else if (TREE_CODE (init) == STRING_CST |
783 | && array_of_runtime_bound_p (TREE_TYPE (dest))) | |
784 | code = build_vec_init (dest, NULL_TREE, init, /*value-init*/false, | |
785 | /*from array*/1, tf_warning_or_error); | |
3afe9b43 | 786 | else |
831d52a2 | 787 | code = build2 (INIT_EXPR, TREE_TYPE (dest), dest, init); |
3afe9b43 | 788 | |
789 | return code; | |
790 | } | |
791 | ||
471086d6 | 792 | /* Perform appropriate conversions on the initial value of a variable, |
793 | store it in the declaration DECL, | |
794 | and print any error messages that are appropriate. | |
795 | If the init is invalid, store an ERROR_MARK. | |
796 | ||
797 | C++: Note that INIT might be a TREE_LIST, which would mean that it is | |
798 | a base class initializer for some aggregate type, hopefully compatible | |
799 | with DECL. If INIT is a single element, and DECL is an aggregate | |
800 | type, we silently convert INIT into a TREE_LIST, allowing a constructor | |
801 | to be called. | |
802 | ||
803 | If INIT is a TREE_LIST and there is no constructor, turn INIT | |
804 | into a CONSTRUCTOR and use standard initialization techniques. | |
805 | Perhaps a warning should be generated? | |
806 | ||
3afe9b43 | 807 | Returns code to be executed if initialization could not be performed |
808 | for static variable. In that case, caller must emit the code. */ | |
471086d6 | 809 | |
810 | tree | |
f1f41a6c | 811 | store_init_value (tree decl, tree init, vec<tree, va_gc>** cleanups, int flags) |
471086d6 | 812 | { |
cd16867a | 813 | tree value, type; |
471086d6 | 814 | |
815 | /* If variable's type was invalidly declared, just ignore it. */ | |
816 | ||
817 | type = TREE_TYPE (decl); | |
818 | if (TREE_CODE (type) == ERROR_MARK) | |
819 | return NULL_TREE; | |
820 | ||
95397ff9 | 821 | if (MAYBE_CLASS_TYPE_P (type)) |
471086d6 | 822 | { |
771665d8 | 823 | if (TREE_CODE (init) == TREE_LIST) |
471086d6 | 824 | { |
03d6ca9e | 825 | error ("constructor syntax used, but no constructor declared " |
653e5405 | 826 | "for type %qT", type); |
f82f1250 | 827 | init = build_constructor_from_list (init_list_type_node, nreverse (init)); |
471086d6 | 828 | } |
471086d6 | 829 | } |
471086d6 | 830 | |
831 | /* End of special C++ code. */ | |
832 | ||
ce984e5e | 833 | if (flags & LOOKUP_ALREADY_DIGESTED) |
834 | value = init; | |
835 | else | |
c38103e8 | 836 | { |
837 | if (TREE_STATIC (decl)) | |
838 | flags |= LOOKUP_ALLOW_FLEXARRAY_INIT; | |
839 | /* Digest the specified initializer into an expression. */ | |
840 | value = digest_init_flags (type, init, flags, tf_warning_or_error); | |
841 | } | |
ce984e5e | 842 | |
dc0cf270 | 843 | /* Look for braced array initializers for character arrays and |
844 | recursively convert them into STRING_CSTs. */ | |
845 | value = braced_lists_to_strings (type, value); | |
d839099f | 846 | |
8c89c5fc | 847 | current_ref_temp_count = 0; |
c7b89256 | 848 | value = extend_ref_init_temps (decl, value, cleanups); |
849 | ||
a7e27879 | 850 | /* In C++11 constant expression is a semantic, not syntactic, property. |
ce984e5e | 851 | In C++98, make sure that what we thought was a constant expression at |
1fff8e07 | 852 | template definition time is still constant and otherwise perform this |
853 | as optimization, e.g. to fold SIZEOF_EXPRs in the initializer. */ | |
854 | if (decl_maybe_constant_var_p (decl) || TREE_STATIC (decl)) | |
ce984e5e | 855 | { |
856 | bool const_init; | |
006b503a | 857 | tree oldval = value; |
7dffa847 | 858 | value = fold_non_dependent_expr (value); |
cd50b62d | 859 | if (DECL_DECLARED_CONSTEXPR_P (decl) |
bd20946f | 860 | || (DECL_IN_AGGR_P (decl) |
4e60ed29 | 861 | && DECL_INITIALIZED_IN_CLASS_P (decl))) |
03cbe7c9 | 862 | { |
bd20946f | 863 | /* Diagnose a non-constant initializer for constexpr variable or |
864 | non-inline in-class-initialized static data member. */ | |
7e1f8be4 | 865 | if (!require_constant_expression (value)) |
03cbe7c9 | 866 | value = error_mark_node; |
c8e19553 | 867 | else if (processing_template_decl) |
868 | /* In a template we might not have done the necessary | |
869 | transformations to make value actually constant, | |
870 | e.g. extend_ref_init_temps. */ | |
871 | value = maybe_constant_init (value, decl, true); | |
03cbe7c9 | 872 | else |
0c703e14 | 873 | value = cxx_constant_init (value, decl); |
03cbe7c9 | 874 | } |
0c703e14 | 875 | else |
18d371d3 | 876 | value = maybe_constant_init (value, decl, true); |
bbcc9042 | 877 | if (TREE_CODE (value) == CONSTRUCTOR && cp_has_mutable_p (type)) |
878 | /* Poison this CONSTRUCTOR so it can't be copied to another | |
879 | constexpr variable. */ | |
880 | CONSTRUCTOR_MUTABLE_POISON (value) = true; | |
ce984e5e | 881 | const_init = (reduced_constant_expression_p (value) |
882 | || error_operand_p (value)); | |
883 | DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = const_init; | |
39682fd5 | 884 | /* FIXME setting TREE_CONSTANT on refs breaks the back end. */ |
90ad495b | 885 | if (!TYPE_REF_P (type)) |
39682fd5 | 886 | TREE_CONSTANT (decl) = const_init && decl_maybe_constant_var_p (decl); |
006b503a | 887 | if (!const_init) |
888 | value = oldval; | |
ce984e5e | 889 | } |
d560f985 | 890 | value = cp_fully_fold_init (value); |
ce984e5e | 891 | |
ffc5ad9b | 892 | /* Handle aggregate NSDMI in non-constant initializers, too. */ |
893 | value = replace_placeholders (value, decl); | |
cf72f34d | 894 | |
f553d9f8 | 895 | /* DECL may change value; purge caches. */ |
896 | clear_cv_and_fold_caches (); | |
897 | ||
07969d83 | 898 | /* If the initializer is not a constant, fill in DECL_INITIAL with |
899 | the bits that are constant, and then return an expression that | |
900 | will perform the dynamic initialization. */ | |
901 | if (value != error_mark_node | |
902 | && (TREE_SIDE_EFFECTS (value) | |
0b8d327e | 903 | || vla_type_p (type) |
88af6243 | 904 | || ! reduced_constant_expression_p (value))) |
39786c80 | 905 | return split_nonconstant_init (decl, value); |
07969d83 | 906 | /* If the value is a constant, just put it in DECL_INITIAL. If DECL |
907 | is an automatic variable, the middle end will turn this into a | |
908 | dynamic initialization later. */ | |
471086d6 | 909 | DECL_INITIAL (decl) = value; |
910 | return NULL_TREE; | |
911 | } | |
e63bd8ae | 912 | |
471086d6 | 913 | \f |
ee6d34bb | 914 | /* Give diagnostic about narrowing conversions within { }, or as part of |
915 | a converted constant expression. If CONST_ONLY, only check | |
916 | constants. */ | |
f82f1250 | 917 | |
718affe2 | 918 | bool |
ee6d34bb | 919 | check_narrowing (tree type, tree init, tsubst_flags_t complain, bool const_only) |
f82f1250 | 920 | { |
ca01d9d9 | 921 | tree ftype = unlowered_expr_type (init); |
f82f1250 | 922 | bool ok = true; |
923 | REAL_VALUE_TYPE d; | |
924 | ||
718affe2 | 925 | if (((!warn_narrowing || !(complain & tf_warning)) |
926 | && cxx_dialect == cxx98) | |
ee6d34bb | 927 | || !ARITHMETIC_TYPE_P (type) |
928 | /* Don't emit bogus warnings with e.g. value-dependent trees. */ | |
929 | || instantiation_dependent_expression_p (init)) | |
718affe2 | 930 | return ok; |
2732b026 | 931 | |
581edfc4 | 932 | if (BRACE_ENCLOSED_INITIALIZER_P (init) |
933 | && TREE_CODE (type) == COMPLEX_TYPE) | |
934 | { | |
935 | tree elttype = TREE_TYPE (type); | |
a2704374 | 936 | if (CONSTRUCTOR_NELTS (init) > 0) |
718affe2 | 937 | ok &= check_narrowing (elttype, CONSTRUCTOR_ELT (init, 0)->value, |
938 | complain); | |
581edfc4 | 939 | if (CONSTRUCTOR_NELTS (init) > 1) |
718affe2 | 940 | ok &= check_narrowing (elttype, CONSTRUCTOR_ELT (init, 1)->value, |
941 | complain); | |
942 | return ok; | |
581edfc4 | 943 | } |
944 | ||
ee6d34bb | 945 | init = maybe_constant_value (init); |
946 | ||
947 | /* If we were asked to only check constants, return early. */ | |
948 | if (const_only && !TREE_CONSTANT (init)) | |
949 | return ok; | |
f82f1250 | 950 | |
df6bc621 | 951 | if (CP_INTEGRAL_TYPE_P (type) |
f82f1250 | 952 | && TREE_CODE (ftype) == REAL_TYPE) |
953 | ok = false; | |
954 | else if (INTEGRAL_OR_ENUMERATION_TYPE_P (ftype) | |
955 | && CP_INTEGRAL_TYPE_P (type)) | |
956 | { | |
89269893 | 957 | if (TREE_CODE (ftype) == ENUMERAL_TYPE) |
958 | /* Check for narrowing based on the values of the enumeration. */ | |
959 | ftype = ENUM_UNDERLYING_TYPE (ftype); | |
b0337f5c | 960 | if ((tree_int_cst_lt (TYPE_MAX_VALUE (type), |
961 | TYPE_MAX_VALUE (ftype)) | |
962 | || tree_int_cst_lt (TYPE_MIN_VALUE (ftype), | |
963 | TYPE_MIN_VALUE (type))) | |
f82f1250 | 964 | && (TREE_CODE (init) != INTEGER_CST |
965 | || !int_fits_type_p (init, type))) | |
966 | ok = false; | |
967 | } | |
968 | else if (TREE_CODE (ftype) == REAL_TYPE | |
969 | && TREE_CODE (type) == REAL_TYPE) | |
970 | { | |
971 | if (TYPE_PRECISION (type) < TYPE_PRECISION (ftype)) | |
972 | { | |
f82f1250 | 973 | if (TREE_CODE (init) == REAL_CST) |
974 | { | |
eed2e6c3 | 975 | /* Issue 703: Loss of precision is OK as long as the value is |
976 | within the representable range of the new type. */ | |
977 | REAL_VALUE_TYPE r; | |
f82f1250 | 978 | d = TREE_REAL_CST (init); |
eed2e6c3 | 979 | real_convert (&r, TYPE_MODE (type), &d); |
980 | if (real_isinf (&r)) | |
981 | ok = false; | |
f82f1250 | 982 | } |
eed2e6c3 | 983 | else |
984 | ok = false; | |
f82f1250 | 985 | } |
986 | } | |
987 | else if (INTEGRAL_OR_ENUMERATION_TYPE_P (ftype) | |
988 | && TREE_CODE (type) == REAL_TYPE) | |
989 | { | |
990 | ok = false; | |
991 | if (TREE_CODE (init) == INTEGER_CST) | |
992 | { | |
993 | d = real_value_from_int_cst (0, init); | |
994 | if (exact_real_truncate (TYPE_MODE (type), &d)) | |
995 | ok = true; | |
996 | } | |
997 | } | |
998 | ||
d2c63826 | 999 | bool almost_ok = ok; |
1000 | if (!ok && !CONSTANT_CLASS_P (init) && (complain & tf_warning_or_error)) | |
1001 | { | |
1002 | tree folded = cp_fully_fold (init); | |
1003 | if (TREE_CONSTANT (folded) && check_narrowing (type, folded, tf_none)) | |
1004 | almost_ok = true; | |
1005 | } | |
1006 | ||
f82f1250 | 1007 | if (!ok) |
7e783eb3 | 1008 | { |
d3a3cfb8 | 1009 | location_t loc = cp_expr_loc_or_loc (init, input_location); |
718affe2 | 1010 | if (cxx_dialect == cxx98) |
d2c63826 | 1011 | { |
1012 | if (complain & tf_warning) | |
1013 | warning_at (loc, OPT_Wnarrowing, "narrowing conversion of %qE " | |
ee6d34bb | 1014 | "from %qH to %qI is ill-formed in C++11", |
d2c63826 | 1015 | init, ftype, type); |
1016 | ok = true; | |
1017 | } | |
1018 | else if (!CONSTANT_CLASS_P (init)) | |
718affe2 | 1019 | { |
1020 | if (complain & tf_warning_or_error) | |
1021 | { | |
bc35ef65 | 1022 | auto_diagnostic_group d; |
22d09025 | 1023 | if ((!almost_ok || pedantic) |
1024 | && pedwarn (loc, OPT_Wnarrowing, | |
ee6d34bb | 1025 | "narrowing conversion of %qE from %qH to %qI", |
22d09025 | 1026 | init, ftype, type) |
1027 | && almost_ok) | |
d2c63826 | 1028 | inform (loc, " the expression has a constant value but is not " |
1029 | "a C++ constant-expression"); | |
718affe2 | 1030 | ok = true; |
1031 | } | |
1032 | } | |
1033 | else if (complain & tf_error) | |
e36ce2a1 | 1034 | { |
eba4cf96 | 1035 | int savederrorcount = errorcount; |
e36ce2a1 | 1036 | global_dc->pedantic_errors = 1; |
d2c63826 | 1037 | pedwarn (loc, OPT_Wnarrowing, |
a9c7b78d | 1038 | "narrowing conversion of %qE from %qH to %qI", |
ee6d34bb | 1039 | init, ftype, type); |
eba4cf96 | 1040 | if (errorcount == savederrorcount) |
d3a3b710 | 1041 | ok = true; |
e36ce2a1 | 1042 | global_dc->pedantic_errors = flag_pedantic_errors; |
1043 | } | |
7e783eb3 | 1044 | } |
718affe2 | 1045 | |
d2c63826 | 1046 | return ok; |
f82f1250 | 1047 | } |
1048 | ||
82362779 | 1049 | /* True iff TYPE is a C++2a "ordinary" character type. */ |
1050 | ||
1051 | bool | |
1052 | ordinary_char_type_p (tree type) | |
1053 | { | |
1054 | type = TYPE_MAIN_VARIANT (type); | |
1055 | return (type == char_type_node | |
1056 | || type == signed_char_type_node | |
1057 | || type == unsigned_char_type_node); | |
1058 | } | |
1059 | ||
c75b4594 | 1060 | /* Process the initializer INIT for a variable of type TYPE, emitting |
1061 | diagnostics for invalid initializers and converting the initializer as | |
1062 | appropriate. | |
471086d6 | 1063 | |
c75b4594 | 1064 | For aggregate types, it assumes that reshape_init has already run, thus the |
f82f1250 | 1065 | initializer will have the right shape (brace elision has been undone). |
471086d6 | 1066 | |
7aac6ee6 | 1067 | NESTED is non-zero iff we are being called for an element of a CONSTRUCTOR, |
1068 | 2 iff the element of a CONSTRUCTOR is inside another CONSTRUCTOR. */ | |
f82f1250 | 1069 | |
1070 | static tree | |
7aac6ee6 | 1071 | digest_init_r (tree type, tree init, int nested, int flags, |
b52bd4ae | 1072 | tsubst_flags_t complain) |
471086d6 | 1073 | { |
1074 | enum tree_code code = TREE_CODE (type); | |
471086d6 | 1075 | |
247f7e36 | 1076 | if (error_operand_p (init)) |
471086d6 | 1077 | return error_mark_node; |
1078 | ||
c75b4594 | 1079 | gcc_assert (init); |
581af848 | 1080 | |
1081 | /* We must strip the outermost array type when completing the type, | |
1082 | because the its bounds might be incomplete at the moment. */ | |
7aac6ee6 | 1083 | if (!complete_type_or_maybe_complain (code == ARRAY_TYPE |
6500d307 | 1084 | ? TREE_TYPE (type) : type, NULL_TREE, |
1085 | complain)) | |
581af848 | 1086 | return error_mark_node; |
9031d10b | 1087 | |
9ab4658b | 1088 | location_t loc = cp_expr_loc_or_loc (init, input_location); |
1089 | ||
1090 | tree stripped_init = init; | |
1091 | ||
c75b4594 | 1092 | /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue |
1093 | (g++.old-deja/g++.law/casts2.C). */ | |
471086d6 | 1094 | if (TREE_CODE (init) == NON_LVALUE_EXPR) |
9ab4658b | 1095 | stripped_init = TREE_OPERAND (init, 0); |
035778de | 1096 | |
9ab4658b | 1097 | stripped_init = tree_strip_any_location_wrapper (stripped_init); |
d582d140 | 1098 | |
c75b4594 | 1099 | /* Initialization of an array of chars from a string constant. The initializer |
1100 | can be optionally enclosed in braces, but reshape_init has already removed | |
1101 | them if they were present. */ | |
471086d6 | 1102 | if (code == ARRAY_TYPE) |
1103 | { | |
6b94e133 | 1104 | if (nested && !TYPE_DOMAIN (type)) |
7aac6ee6 | 1105 | /* C++ flexible array members have a null domain. */ |
c38103e8 | 1106 | { |
1107 | if (flags & LOOKUP_ALLOW_FLEXARRAY_INIT) | |
1108 | pedwarn (loc, OPT_Wpedantic, | |
1109 | "initialization of a flexible array member"); | |
1110 | else | |
1111 | { | |
1112 | if (complain & tf_error) | |
1113 | error_at (loc, "non-static initialization of" | |
1114 | " a flexible array member"); | |
1115 | return error_mark_node; | |
1116 | } | |
1117 | } | |
6b94e133 | 1118 | |
c75b4594 | 1119 | tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type)); |
e67164f5 | 1120 | if (char_type_p (typ1) |
d582d140 | 1121 | && TREE_CODE (stripped_init) == STRING_CST) |
471086d6 | 1122 | { |
c75b4594 | 1123 | tree char_type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (init))); |
82362779 | 1124 | bool incompat_string_cst = false; |
471086d6 | 1125 | |
82362779 | 1126 | if (typ1 != char_type) |
471086d6 | 1127 | { |
82362779 | 1128 | /* The array element type does not match the initializing string |
1129 | literal element type; this is only allowed when both types are | |
1130 | ordinary character type. There are no string literals of | |
1131 | signed or unsigned char type in the language, but we can get | |
1132 | them internally from converting braced-init-lists to | |
1133 | STRING_CST. */ | |
1134 | if (ordinary_char_type_p (typ1) | |
1135 | && ordinary_char_type_p (char_type)) | |
1136 | /* OK */; | |
1137 | else | |
1138 | incompat_string_cst = true; | |
471086d6 | 1139 | } |
82362779 | 1140 | |
1141 | if (incompat_string_cst) | |
471086d6 | 1142 | { |
82362779 | 1143 | if (complain & tf_error) |
1144 | error_at (loc, "cannot initialize array of %qT from " | |
1145 | "a string literal with type array of %qT", | |
1146 | typ1, char_type); | |
1147 | return error_mark_node; | |
471086d6 | 1148 | } |
1149 | ||
7aac6ee6 | 1150 | if (nested == 2 && !TYPE_DOMAIN (type)) |
1151 | { | |
1152 | if (complain & tf_error) | |
1153 | error_at (loc, "initialization of flexible array member " | |
1154 | "in a nested context"); | |
1155 | return error_mark_node; | |
1156 | } | |
1157 | ||
c571b0c6 | 1158 | if (type != TREE_TYPE (init) |
1159 | && !variably_modified_type_p (type, NULL_TREE)) | |
bd46a023 | 1160 | { |
1161 | init = copy_node (init); | |
1162 | TREE_TYPE (init) = type; | |
d582d140 | 1163 | /* If we have a location wrapper, then also copy the wrapped |
1164 | node, and update the copy's type. */ | |
1165 | if (location_wrapper_p (init)) | |
1166 | { | |
1167 | stripped_init = copy_node (stripped_init); | |
1168 | TREE_OPERAND (init, 0) = stripped_init; | |
1169 | TREE_TYPE (stripped_init) = type; | |
1170 | } | |
bd46a023 | 1171 | } |
6b94e133 | 1172 | if (TYPE_DOMAIN (type) && TREE_CONSTANT (TYPE_SIZE (type))) |
471086d6 | 1173 | { |
73d282c6 | 1174 | /* Not a flexible array member. */ |
f9ae6f95 | 1175 | int size = TREE_INT_CST_LOW (TYPE_SIZE (type)); |
471086d6 | 1176 | size = (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT; |
1177 | /* In C it is ok to subtract 1 from the length of the string | |
1178 | because it's ok to ignore the terminating null char that is | |
1179 | counted in the length of the constant, but in C++ this would | |
1180 | be invalid. */ | |
d582d140 | 1181 | if (size < TREE_STRING_LENGTH (stripped_init)) |
35f9aa56 | 1182 | { |
85b9be9b | 1183 | permerror (loc, "initializer-string for %qT is too long", |
1184 | type); | |
35f9aa56 | 1185 | |
d582d140 | 1186 | init = build_string (size, |
1187 | TREE_STRING_POINTER (stripped_init)); | |
35f9aa56 | 1188 | TREE_TYPE (init) = type; |
1189 | } | |
471086d6 | 1190 | } |
c75b4594 | 1191 | return init; |
471086d6 | 1192 | } |
1193 | } | |
1194 | ||
2d42b7d7 | 1195 | /* Handle scalar types (including conversions) and references. */ |
d582d140 | 1196 | if ((code != COMPLEX_TYPE || BRACE_ENCLOSED_INITIALIZER_P (stripped_init)) |
04791a75 | 1197 | && (SCALAR_TYPE_P (type) || code == REFERENCE_TYPE)) |
1b49e7ef | 1198 | { |
7e783eb3 | 1199 | if (nested) |
c4991e3e | 1200 | flags |= LOOKUP_NO_NARROWING; |
cf7aa2e5 | 1201 | init = convert_for_initialization (0, type, init, flags, |
183407ee | 1202 | ICR_INIT, NULL_TREE, 0, |
b52bd4ae | 1203 | complain); |
1b49e7ef | 1204 | |
1205 | return init; | |
1206 | } | |
c75b4594 | 1207 | |
1208 | /* Come here only for aggregates: records, arrays, unions, complex numbers | |
1209 | and vectors. */ | |
7aac6ee6 | 1210 | gcc_assert (code == ARRAY_TYPE |
76249021 | 1211 | || VECTOR_TYPE_P (type) |
7aac6ee6 | 1212 | || code == RECORD_TYPE |
1213 | || code == UNION_TYPE | |
1214 | || code == COMPLEX_TYPE); | |
c75b4594 | 1215 | |
3dd2833b | 1216 | /* "If T is a class type and the initializer list has a single |
1217 | element of type cv U, where U is T or a class derived from T, | |
1218 | the object is initialized from that element." */ | |
67ecd3ae | 1219 | if (cxx_dialect >= cxx11 |
d582d140 | 1220 | && BRACE_ENCLOSED_INITIALIZER_P (stripped_init) |
1221 | && CONSTRUCTOR_NELTS (stripped_init) == 1 | |
671608f2 | 1222 | && ((CLASS_TYPE_P (type) && !CLASSTYPE_NON_AGGREGATE (type)) |
1223 | || VECTOR_TYPE_P (type))) | |
3dd2833b | 1224 | { |
d582d140 | 1225 | tree elt = CONSTRUCTOR_ELT (stripped_init, 0)->value; |
3dd2833b | 1226 | if (reference_related_p (type, TREE_TYPE (elt))) |
751baf9c | 1227 | { |
1228 | /* In C++17, aggregates can have bases, thus participate in | |
1229 | aggregate initialization. In the following case: | |
1230 | ||
1231 | struct B { int c; }; | |
1232 | struct D : B { }; | |
1233 | D d{{D{{42}}}}; | |
1234 | ||
1235 | there's an extra set of braces, so the D temporary initializes | |
1236 | the first element of d, which is the B base subobject. The base | |
1237 | of type B is copy-initialized from the D temporary, causing | |
1238 | object slicing. */ | |
1239 | tree field = next_initializable_field (TYPE_FIELDS (type)); | |
1240 | if (field && DECL_FIELD_IS_BASE (field)) | |
1241 | { | |
1242 | if (warning_at (loc, 0, "initializing a base class of type %qT " | |
1243 | "results in object slicing", TREE_TYPE (field))) | |
1244 | inform (loc, "remove %<{ }%> around initializer"); | |
1245 | } | |
67ecd3ae | 1246 | else if (flag_checking) |
751baf9c | 1247 | /* We should have fixed this in reshape_init. */ |
1248 | gcc_unreachable (); | |
1249 | } | |
3dd2833b | 1250 | } |
1251 | ||
d582d140 | 1252 | if (BRACE_ENCLOSED_INITIALIZER_P (stripped_init) |
1ee7f2fb | 1253 | && !TYPE_NON_AGGREGATE_CLASS (type)) |
c38103e8 | 1254 | return process_init_constructor (type, stripped_init, nested, flags, |
1255 | complain); | |
c75b4594 | 1256 | else |
471086d6 | 1257 | { |
d582d140 | 1258 | if (COMPOUND_LITERAL_P (stripped_init) && code == ARRAY_TYPE) |
471086d6 | 1259 | { |
b52bd4ae | 1260 | if (complain & tf_error) |
035778de | 1261 | error_at (loc, "cannot initialize aggregate of type %qT with " |
1262 | "a compound literal", type); | |
471086d6 | 1263 | |
c75b4594 | 1264 | return error_mark_node; |
1265 | } | |
f8d96752 | 1266 | |
7aac6ee6 | 1267 | if (code == ARRAY_TYPE |
d582d140 | 1268 | && !BRACE_ENCLOSED_INITIALIZER_P (stripped_init)) |
f8d96752 | 1269 | { |
29ada811 | 1270 | /* Allow the result of build_array_copy and of |
1271 | build_value_init_noctor. */ | |
d582d140 | 1272 | if ((TREE_CODE (stripped_init) == VEC_INIT_EXPR |
1273 | || TREE_CODE (stripped_init) == CONSTRUCTOR) | |
a8b75081 | 1274 | && (same_type_ignoring_top_level_qualifiers_p |
1275 | (type, TREE_TYPE (init)))) | |
1276 | return init; | |
1277 | ||
b52bd4ae | 1278 | if (complain & tf_error) |
035778de | 1279 | error_at (loc, "array must be initialized with a brace-enclosed" |
1280 | " initializer"); | |
f8d96752 | 1281 | return error_mark_node; |
1282 | } | |
1283 | ||
c75b4594 | 1284 | return convert_for_initialization (NULL_TREE, type, init, |
cf7aa2e5 | 1285 | flags, |
183407ee | 1286 | ICR_INIT, NULL_TREE, 0, |
b52bd4ae | 1287 | complain); |
471086d6 | 1288 | } |
c75b4594 | 1289 | } |
1290 | ||
f82f1250 | 1291 | tree |
b52bd4ae | 1292 | digest_init (tree type, tree init, tsubst_flags_t complain) |
f82f1250 | 1293 | { |
7aac6ee6 | 1294 | return digest_init_r (type, init, 0, LOOKUP_IMPLICIT, complain); |
cf7aa2e5 | 1295 | } |
1296 | ||
1297 | tree | |
4b373fc1 | 1298 | digest_init_flags (tree type, tree init, int flags, tsubst_flags_t complain) |
cf7aa2e5 | 1299 | { |
7aac6ee6 | 1300 | return digest_init_r (type, init, 0, flags, complain); |
f82f1250 | 1301 | } |
089c9c49 | 1302 | |
1303 | /* Process the initializer INIT for an NSDMI DECL (a FIELD_DECL). */ | |
1304 | tree | |
b9e17a4a | 1305 | digest_nsdmi_init (tree decl, tree init, tsubst_flags_t complain) |
089c9c49 | 1306 | { |
1307 | gcc_assert (TREE_CODE (decl) == FIELD_DECL); | |
1308 | ||
34569785 | 1309 | tree type = TREE_TYPE (decl); |
089c9c49 | 1310 | int flags = LOOKUP_IMPLICIT; |
1311 | if (DIRECT_LIST_INIT_P (init)) | |
3609f654 | 1312 | { |
1313 | flags = LOOKUP_NORMAL; | |
1314 | complain |= tf_no_cleanup; | |
1315 | } | |
34569785 | 1316 | if (BRACE_ENCLOSED_INITIALIZER_P (init) |
1317 | && CP_AGGREGATE_TYPE_P (type)) | |
b9e17a4a | 1318 | init = reshape_init (type, init, complain); |
1319 | init = digest_init_flags (type, init, flags, complain); | |
089c9c49 | 1320 | if (TREE_CODE (init) == TARGET_EXPR) |
1321 | /* This represents the whole initialization. */ | |
1322 | TARGET_EXPR_DIRECT_INIT_P (init) = true; | |
1323 | return init; | |
1324 | } | |
c75b4594 | 1325 | \f |
1326 | /* Set of flags used within process_init_constructor to describe the | |
1327 | initializers. */ | |
1328 | #define PICFLAG_ERRONEOUS 1 | |
1329 | #define PICFLAG_NOT_ALL_CONSTANT 2 | |
1330 | #define PICFLAG_NOT_ALL_SIMPLE 4 | |
48f76477 | 1331 | #define PICFLAG_SIDE_EFFECTS 8 |
c75b4594 | 1332 | |
1333 | /* Given an initializer INIT, return the flag (PICFLAG_*) which better | |
1334 | describe it. */ | |
1335 | ||
1336 | static int | |
1337 | picflag_from_initializer (tree init) | |
1338 | { | |
1339 | if (init == error_mark_node) | |
1340 | return PICFLAG_ERRONEOUS; | |
1341 | else if (!TREE_CONSTANT (init)) | |
48f76477 | 1342 | { |
1343 | if (TREE_SIDE_EFFECTS (init)) | |
1344 | return PICFLAG_SIDE_EFFECTS; | |
1345 | else | |
1346 | return PICFLAG_NOT_ALL_CONSTANT; | |
1347 | } | |
c75b4594 | 1348 | else if (!initializer_constant_valid_p (init, TREE_TYPE (init))) |
1349 | return PICFLAG_NOT_ALL_SIMPLE; | |
1350 | return 0; | |
1351 | } | |
471086d6 | 1352 | |
7e173c71 | 1353 | /* Adjust INIT for going into a CONSTRUCTOR. */ |
1354 | ||
1355 | static tree | |
c38103e8 | 1356 | massage_init_elt (tree type, tree init, int nested, int flags, |
1357 | tsubst_flags_t complain) | |
7e173c71 | 1358 | { |
c38103e8 | 1359 | flags &= LOOKUP_ALLOW_FLEXARRAY_INIT; |
1360 | flags |= LOOKUP_IMPLICIT; | |
1361 | init = digest_init_r (type, init, nested ? 2 : 1, flags, complain); | |
7e173c71 | 1362 | /* Strip a simple TARGET_EXPR when we know this is an initializer. */ |
1d56a349 | 1363 | if (SIMPLE_TARGET_EXPR_P (init)) |
7e173c71 | 1364 | init = TARGET_EXPR_INITIAL (init); |
1365 | /* When we defer constant folding within a statement, we may want to | |
1366 | defer this folding as well. */ | |
cf13db0c | 1367 | tree t = fold_non_dependent_init (init, complain); |
0e65aeb6 | 1368 | if (TREE_CONSTANT (t)) |
1369 | init = t; | |
7e173c71 | 1370 | return init; |
1371 | } | |
1372 | ||
c75b4594 | 1373 | /* Subroutine of process_init_constructor, which will process an initializer |
45cc50c5 | 1374 | INIT for an array or vector of type TYPE. Returns the flags (PICFLAG_*) |
1375 | which describe the initializers. */ | |
471086d6 | 1376 | |
c75b4594 | 1377 | static int |
c38103e8 | 1378 | process_init_constructor_array (tree type, tree init, int nested, int flags, |
b52bd4ae | 1379 | tsubst_flags_t complain) |
c75b4594 | 1380 | { |
1381 | unsigned HOST_WIDE_INT i, len = 0; | |
c38103e8 | 1382 | int picflags = 0; |
c75b4594 | 1383 | bool unbounded = false; |
1384 | constructor_elt *ce; | |
f1f41a6c | 1385 | vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (init); |
c75b4594 | 1386 | |
1387 | gcc_assert (TREE_CODE (type) == ARRAY_TYPE | |
76249021 | 1388 | || VECTOR_TYPE_P (type)); |
c75b4594 | 1389 | |
1390 | if (TREE_CODE (type) == ARRAY_TYPE) | |
471086d6 | 1391 | { |
6b94e133 | 1392 | /* C++ flexible array members have a null domain. */ |
c75b4594 | 1393 | tree domain = TYPE_DOMAIN (type); |
6b94e133 | 1394 | if (domain && TREE_CONSTANT (TYPE_MAX_VALUE (domain))) |
1395 | len = wi::ext (wi::to_offset (TYPE_MAX_VALUE (domain)) | |
1396 | - wi::to_offset (TYPE_MIN_VALUE (domain)) + 1, | |
796b6678 | 1397 | TYPE_PRECISION (TREE_TYPE (domain)), |
1398 | TYPE_SIGN (TREE_TYPE (domain))).to_uhwi (); | |
c75b4594 | 1399 | else |
1400 | unbounded = true; /* Take as many as there are. */ | |
7aac6ee6 | 1401 | |
1402 | if (nested == 2 && !domain && !vec_safe_is_empty (v)) | |
1403 | { | |
1404 | if (complain & tf_error) | |
d3a3cfb8 | 1405 | error_at (cp_expr_loc_or_loc (init, input_location), |
7aac6ee6 | 1406 | "initialization of flexible array member " |
1407 | "in a nested context"); | |
1408 | return PICFLAG_ERRONEOUS; | |
1409 | } | |
471086d6 | 1410 | } |
c75b4594 | 1411 | else |
1412 | /* Vectors are like simple fixed-size arrays. */ | |
f08ee65f | 1413 | unbounded = !TYPE_VECTOR_SUBPARTS (type).is_constant (&len); |
471086d6 | 1414 | |
a8fe6bf4 | 1415 | /* There must not be more initializers than needed. */ |
f1f41a6c | 1416 | if (!unbounded && vec_safe_length (v) > len) |
b52bd4ae | 1417 | { |
1418 | if (complain & tf_error) | |
1419 | error ("too many initializers for %qT", type); | |
1420 | else | |
1421 | return PICFLAG_ERRONEOUS; | |
1422 | } | |
c75b4594 | 1423 | |
f1f41a6c | 1424 | FOR_EACH_VEC_SAFE_ELT (v, i, ce) |
471086d6 | 1425 | { |
caf629d3 | 1426 | if (!ce->index) |
c75b4594 | 1427 | ce->index = size_int (i); |
caf629d3 | 1428 | else if (!check_array_designated_initializer (ce, i)) |
1429 | ce->index = error_mark_node; | |
c75b4594 | 1430 | gcc_assert (ce->value); |
7aac6ee6 | 1431 | ce->value |
c38103e8 | 1432 | = massage_init_elt (TREE_TYPE (type), ce->value, nested, flags, |
1433 | complain); | |
48ec80c8 | 1434 | |
ee1f7409 | 1435 | gcc_checking_assert |
1436 | (ce->value == error_mark_node | |
1437 | || (same_type_ignoring_top_level_qualifiers_p | |
1438 | (strip_array_types (TREE_TYPE (type)), | |
1439 | strip_array_types (TREE_TYPE (ce->value))))); | |
2133b374 | 1440 | |
c38103e8 | 1441 | picflags |= picflag_from_initializer (ce->value); |
471086d6 | 1442 | } |
1443 | ||
c75b4594 | 1444 | /* No more initializers. If the array is unbounded, we are done. Otherwise, |
1445 | we must add initializers ourselves. */ | |
1446 | if (!unbounded) | |
1447 | for (; i < len; ++i) | |
1448 | { | |
1449 | tree next; | |
1450 | ||
883e1020 | 1451 | if (type_build_ctor_call (TREE_TYPE (type))) |
c75b4594 | 1452 | { |
1453 | /* If this type needs constructors run for default-initialization, | |
8feda8f8 | 1454 | we can't rely on the back end to do it for us, so make the |
79f1f232 | 1455 | initialization explicit by list-initializing from T{}. */ |
9705da72 | 1456 | next = build_constructor (init_list_type_node, NULL); |
c38103e8 | 1457 | next = massage_init_elt (TREE_TYPE (type), next, nested, flags, |
1458 | complain); | |
8feda8f8 | 1459 | if (initializer_zerop (next)) |
1460 | /* The default zero-initialization is fine for us; don't | |
1461 | add anything to the CONSTRUCTOR. */ | |
1462 | next = NULL_TREE; | |
c75b4594 | 1463 | } |
1464 | else if (!zero_init_p (TREE_TYPE (type))) | |
1465 | next = build_zero_init (TREE_TYPE (type), | |
1466 | /*nelts=*/NULL_TREE, | |
1467 | /*static_storage_p=*/false); | |
1468 | else | |
1469 | /* The default zero-initialization is fine for us; don't | |
1470 | add anything to the CONSTRUCTOR. */ | |
8feda8f8 | 1471 | next = NULL_TREE; |
c75b4594 | 1472 | |
8feda8f8 | 1473 | if (next) |
1474 | { | |
c38103e8 | 1475 | picflags |= picflag_from_initializer (next); |
cf755750 | 1476 | if (len > i+1 |
1477 | && (initializer_constant_valid_p (next, TREE_TYPE (next)) | |
1478 | == null_pointer_node)) | |
1479 | { | |
1480 | tree range = build2 (RANGE_EXPR, size_type_node, | |
1481 | build_int_cst (size_type_node, i), | |
1482 | build_int_cst (size_type_node, len - 1)); | |
1483 | CONSTRUCTOR_APPEND_ELT (v, range, next); | |
1484 | break; | |
1485 | } | |
1486 | else | |
1487 | CONSTRUCTOR_APPEND_ELT (v, size_int (i), next); | |
8feda8f8 | 1488 | } |
cf755750 | 1489 | else |
1490 | /* Don't bother checking all the other elements. */ | |
1491 | break; | |
c75b4594 | 1492 | } |
1493 | ||
1494 | CONSTRUCTOR_ELTS (init) = v; | |
c38103e8 | 1495 | return picflags; |
471086d6 | 1496 | } |
471086d6 | 1497 | |
c75b4594 | 1498 | /* Subroutine of process_init_constructor, which will process an initializer |
1499 | INIT for a class of type TYPE. Returns the flags (PICFLAG_*) which describe | |
1500 | the initializers. */ | |
471086d6 | 1501 | |
c75b4594 | 1502 | static int |
c38103e8 | 1503 | process_init_constructor_record (tree type, tree init, int nested, int flags, |
b52bd4ae | 1504 | tsubst_flags_t complain) |
471086d6 | 1505 | { |
f1f41a6c | 1506 | vec<constructor_elt, va_gc> *v = NULL; |
c75b4594 | 1507 | tree field; |
cf72f34d | 1508 | int skipped = 0; |
c75b4594 | 1509 | |
1510 | gcc_assert (TREE_CODE (type) == RECORD_TYPE); | |
1511 | gcc_assert (!CLASSTYPE_VBASECLASSES (type)); | |
1512 | gcc_assert (!TYPE_BINFO (type) | |
40e2decb | 1513 | || cxx_dialect >= cxx17 |
c75b4594 | 1514 | || !BINFO_N_BASE_BINFOS (TYPE_BINFO (type))); |
1515 | gcc_assert (!TYPE_POLYMORPHIC_P (type)); | |
1516 | ||
cf72f34d | 1517 | restart: |
c38103e8 | 1518 | int picflags = 0; |
cf72f34d | 1519 | unsigned HOST_WIDE_INT idx = 0; |
8e79b5ff | 1520 | int designator_skip = -1; |
c75b4594 | 1521 | /* Generally, we will always have an index for each initializer (which is |
1522 | a FIELD_DECL, put by reshape_init), but compound literals don't go trough | |
1523 | reshape_init. So we need to handle both cases. */ | |
1767a056 | 1524 | for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field)) |
471086d6 | 1525 | { |
c75b4594 | 1526 | tree next; |
ca01d9d9 | 1527 | tree type; |
471086d6 | 1528 | |
a83affb5 | 1529 | if (TREE_CODE (field) != FIELD_DECL |
1530 | || (DECL_ARTIFICIAL (field) | |
40e2decb | 1531 | && !(cxx_dialect >= cxx17 && DECL_FIELD_IS_BASE (field)))) |
c75b4594 | 1532 | continue; |
1533 | ||
3cde3c29 | 1534 | if (DECL_UNNAMED_BIT_FIELD (field)) |
1535 | continue; | |
1536 | ||
ca01d9d9 | 1537 | /* If this is a bitfield, first convert to the declared type. */ |
1538 | type = TREE_TYPE (field); | |
1539 | if (DECL_BIT_FIELD_TYPE (field)) | |
1540 | type = DECL_BIT_FIELD_TYPE (field); | |
53b567c1 | 1541 | if (type == error_mark_node) |
1542 | return PICFLAG_ERRONEOUS; | |
ca01d9d9 | 1543 | |
8e79b5ff | 1544 | next = NULL_TREE; |
8698843f | 1545 | if (idx < CONSTRUCTOR_NELTS (init)) |
471086d6 | 1546 | { |
f1f41a6c | 1547 | constructor_elt *ce = &(*CONSTRUCTOR_ELTS (init))[idx]; |
c75b4594 | 1548 | if (ce->index) |
471086d6 | 1549 | { |
c75b4594 | 1550 | /* We can have either a FIELD_DECL or an IDENTIFIER_NODE. The |
1551 | latter case can happen in templates where lookup has to be | |
1552 | deferred. */ | |
1553 | gcc_assert (TREE_CODE (ce->index) == FIELD_DECL | |
694683bb | 1554 | || identifier_p (ce->index)); |
8e79b5ff | 1555 | if (ce->index == field || ce->index == DECL_NAME (field)) |
1556 | next = ce->value; | |
1557 | else if (ANON_AGGR_TYPE_P (type) | |
1558 | && search_anon_aggr (type, | |
1559 | TREE_CODE (ce->index) == FIELD_DECL | |
1560 | ? DECL_NAME (ce->index) | |
1561 | : ce->index)) | |
1562 | /* If the element is an anonymous union object and the | |
1563 | initializer list is a designated-initializer-list, the | |
1564 | anonymous union object is initialized by the | |
1565 | designated-initializer-list { D }, where D is the | |
1566 | designated-initializer-clause naming a member of the | |
1567 | anonymous union object. */ | |
91fdd720 | 1568 | next = build_constructor_single (init_list_type_node, |
1569 | ce->index, ce->value); | |
8e79b5ff | 1570 | else |
b81452c9 | 1571 | { |
8e79b5ff | 1572 | ce = NULL; |
1573 | if (designator_skip == -1) | |
1574 | designator_skip = 1; | |
b81452c9 | 1575 | } |
471086d6 | 1576 | } |
8e79b5ff | 1577 | else |
1578 | { | |
1579 | designator_skip = 0; | |
1580 | next = ce->value; | |
1581 | } | |
72ba5dfe | 1582 | |
8e79b5ff | 1583 | if (ce) |
1584 | { | |
1585 | gcc_assert (ce->value); | |
c38103e8 | 1586 | next = massage_init_elt (type, next, nested, flags, complain); |
8e79b5ff | 1587 | ++idx; |
1588 | } | |
c75b4594 | 1589 | } |
8e79b5ff | 1590 | if (next) |
1591 | /* Already handled above. */; | |
cf72f34d | 1592 | else if (DECL_INITIAL (field)) |
1593 | { | |
1594 | if (skipped > 0) | |
1595 | { | |
1596 | /* We're using an NSDMI past a field with implicit | |
1597 | zero-init. Go back and make it explicit. */ | |
1598 | skipped = -1; | |
1599 | vec_safe_truncate (v, 0); | |
1600 | goto restart; | |
1601 | } | |
1602 | /* C++14 aggregate NSDMI. */ | |
b9e17a4a | 1603 | next = get_nsdmi (field, /*ctor*/false, complain); |
7604a798 | 1604 | if (!CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init) |
1605 | && find_placeholders (next)) | |
1606 | CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init) = 1; | |
cf72f34d | 1607 | } |
883e1020 | 1608 | else if (type_build_ctor_call (TREE_TYPE (field))) |
c75b4594 | 1609 | { |
1610 | /* If this type needs constructors run for | |
a17c2a3a | 1611 | default-initialization, we can't rely on the back end to do it |
c75b4594 | 1612 | for us, so build up TARGET_EXPRs. If the type in question is |
1613 | a class, just build one up; if it's an array, recurse. */ | |
dbfcf378 | 1614 | next = build_constructor (init_list_type_node, NULL); |
c38103e8 | 1615 | next = massage_init_elt (TREE_TYPE (field), next, nested, flags, |
1616 | complain); | |
c75b4594 | 1617 | |
1618 | /* Warn when some struct elements are implicitly initialized. */ | |
639d66a3 | 1619 | if ((complain & tf_warning) |
1620 | && !EMPTY_CONSTRUCTOR_P (init)) | |
af71319a | 1621 | warning (OPT_Wmissing_field_initializers, |
1622 | "missing initializer for member %qD", field); | |
c75b4594 | 1623 | } |
1624 | else | |
1625 | { | |
73d282c6 | 1626 | const_tree fldtype = TREE_TYPE (field); |
90ad495b | 1627 | if (TYPE_REF_P (fldtype)) |
b52bd4ae | 1628 | { |
1629 | if (complain & tf_error) | |
af71319a | 1630 | error ("member %qD is uninitialized reference", field); |
b52bd4ae | 1631 | else |
1632 | return PICFLAG_ERRONEOUS; | |
1633 | } | |
73d282c6 | 1634 | else if (CLASSTYPE_REF_FIELDS_NEED_INIT (fldtype)) |
b52bd4ae | 1635 | { |
1636 | if (complain & tf_error) | |
af71319a | 1637 | error ("member %qD with uninitialized reference fields", field); |
b52bd4ae | 1638 | else |
1639 | return PICFLAG_ERRONEOUS; | |
1640 | } | |
fd8a4665 | 1641 | /* Do nothing for flexible array members since they need not have any |
1642 | elements. Don't worry about 'skipped' because a flexarray has to | |
1643 | be the last field. */ | |
1644 | else if (TREE_CODE (fldtype) == ARRAY_TYPE && !TYPE_DOMAIN (fldtype)) | |
1645 | continue; | |
c75b4594 | 1646 | |
1647 | /* Warn when some struct elements are implicitly initialized | |
fd8a4665 | 1648 | to zero. */ |
1649 | if ((complain & tf_warning) | |
639d66a3 | 1650 | && !EMPTY_CONSTRUCTOR_P (init)) |
af71319a | 1651 | warning (OPT_Wmissing_field_initializers, |
1652 | "missing initializer for member %qD", field); | |
c75b4594 | 1653 | |
73d282c6 | 1654 | if (!zero_init_p (fldtype) |
cf72f34d | 1655 | || skipped < 0) |
c75b4594 | 1656 | next = build_zero_init (TREE_TYPE (field), /*nelts=*/NULL_TREE, |
1657 | /*static_storage_p=*/false); | |
72ba5dfe | 1658 | else |
cf72f34d | 1659 | { |
1660 | /* The default zero-initialization is fine for us; don't | |
1661 | add anything to the CONSTRUCTOR. */ | |
1662 | skipped = 1; | |
1663 | continue; | |
1664 | } | |
471086d6 | 1665 | } |
c75b4594 | 1666 | |
dcd8f919 | 1667 | if (DECL_SIZE (field) && integer_zerop (DECL_SIZE (field)) |
1668 | && !TREE_SIDE_EFFECTS (next)) | |
1669 | /* Don't add trivial initialization of an empty base/field to the | |
1670 | constructor, as they might not be ordered the way the back-end | |
1671 | expects. */ | |
1672 | continue; | |
1673 | ||
ca01d9d9 | 1674 | /* If this is a bitfield, now convert to the lowered type. */ |
1675 | if (type != TREE_TYPE (field)) | |
c4698a21 | 1676 | next = cp_convert_and_check (TREE_TYPE (field), next, complain); |
c38103e8 | 1677 | picflags |= picflag_from_initializer (next); |
c75b4594 | 1678 | CONSTRUCTOR_APPEND_ELT (v, field, next); |
471086d6 | 1679 | } |
471086d6 | 1680 | |
8698843f | 1681 | if (idx < CONSTRUCTOR_NELTS (init)) |
b52bd4ae | 1682 | { |
1683 | if (complain & tf_error) | |
8e79b5ff | 1684 | { |
1685 | constructor_elt *ce = &(*CONSTRUCTOR_ELTS (init))[idx]; | |
1686 | /* For better diagnostics, try to find out if it is really | |
1687 | the case of too many initializers or if designators are | |
1688 | in incorrect order. */ | |
1689 | if (designator_skip == 1 && ce->index) | |
1690 | { | |
1691 | gcc_assert (TREE_CODE (ce->index) == FIELD_DECL | |
1692 | || identifier_p (ce->index)); | |
1693 | for (field = TYPE_FIELDS (type); | |
1694 | field; field = DECL_CHAIN (field)) | |
1695 | { | |
8e79b5ff | 1696 | if (TREE_CODE (field) != FIELD_DECL |
1697 | || (DECL_ARTIFICIAL (field) | |
1698 | && !(cxx_dialect >= cxx17 | |
1699 | && DECL_FIELD_IS_BASE (field)))) | |
1700 | continue; | |
1701 | ||
3cde3c29 | 1702 | if (DECL_UNNAMED_BIT_FIELD (field)) |
1703 | continue; | |
1704 | ||
8e79b5ff | 1705 | if (ce->index == field || ce->index == DECL_NAME (field)) |
1706 | break; | |
1707 | if (ANON_AGGR_TYPE_P (TREE_TYPE (field))) | |
1708 | { | |
1709 | tree t | |
1710 | = search_anon_aggr (TREE_TYPE (field), | |
1711 | TREE_CODE (ce->index) == FIELD_DECL | |
1712 | ? DECL_NAME (ce->index) | |
1713 | : ce->index); | |
1714 | if (t) | |
1715 | { | |
1716 | field = t; | |
1717 | break; | |
1718 | } | |
1719 | } | |
1720 | } | |
1721 | } | |
1722 | if (field) | |
1723 | error ("designator order for field %qD does not match declaration " | |
1724 | "order in %qT", field, type); | |
1725 | else | |
1726 | error ("too many initializers for %qT", type); | |
1727 | } | |
b52bd4ae | 1728 | else |
1729 | return PICFLAG_ERRONEOUS; | |
1730 | } | |
1731 | ||
c75b4594 | 1732 | CONSTRUCTOR_ELTS (init) = v; |
c38103e8 | 1733 | return picflags; |
c75b4594 | 1734 | } |
471086d6 | 1735 | |
c75b4594 | 1736 | /* Subroutine of process_init_constructor, which will process a single |
cd7043f9 | 1737 | initializer INIT for a union of type TYPE. Returns the flags (PICFLAG_*) |
c75b4594 | 1738 | which describe the initializer. */ |
471086d6 | 1739 | |
c75b4594 | 1740 | static int |
c38103e8 | 1741 | process_init_constructor_union (tree type, tree init, int nested, int flags, |
b52bd4ae | 1742 | tsubst_flags_t complain) |
c75b4594 | 1743 | { |
1744 | constructor_elt *ce; | |
1ee7f2fb | 1745 | int len; |
471086d6 | 1746 | |
5a869ab8 | 1747 | /* If the initializer was empty, use the union's NSDMI if it has one. |
1748 | Otherwise use default zero initialization. */ | |
f1f41a6c | 1749 | if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init))) |
5a869ab8 | 1750 | { |
1751 | for (tree field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field)) | |
1752 | { | |
b06ecd3c | 1753 | if (TREE_CODE (field) == FIELD_DECL |
1754 | && DECL_INITIAL (field) != NULL_TREE) | |
5a869ab8 | 1755 | { |
7604a798 | 1756 | tree val = get_nsdmi (field, /*in_ctor=*/false, complain); |
1757 | if (!CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init) | |
1758 | && find_placeholders (val)) | |
1759 | CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init) = 1; | |
1760 | CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (init), field, val); | |
5a869ab8 | 1761 | break; |
1762 | } | |
1763 | } | |
1764 | ||
1765 | if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init))) | |
1766 | return 0; | |
1767 | } | |
c75b4594 | 1768 | |
f1f41a6c | 1769 | len = CONSTRUCTOR_ELTS (init)->length (); |
1ee7f2fb | 1770 | if (len > 1) |
1771 | { | |
b52bd4ae | 1772 | if (!(complain & tf_error)) |
1773 | return PICFLAG_ERRONEOUS; | |
1ee7f2fb | 1774 | error ("too many initializers for %qT", type); |
f1f41a6c | 1775 | CONSTRUCTOR_ELTS (init)->block_remove (1, len-1); |
1ee7f2fb | 1776 | } |
1777 | ||
f1f41a6c | 1778 | ce = &(*CONSTRUCTOR_ELTS (init))[0]; |
c75b4594 | 1779 | |
1780 | /* If this element specifies a field, initialize via that field. */ | |
1781 | if (ce->index) | |
1782 | { | |
1783 | if (TREE_CODE (ce->index) == FIELD_DECL) | |
1784 | ; | |
694683bb | 1785 | else if (identifier_p (ce->index)) |
c75b4594 | 1786 | { |
1787 | /* This can happen within a cast, see g++.dg/opt/cse2.C. */ | |
1788 | tree name = ce->index; | |
1789 | tree field; | |
1790 | for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field)) | |
1791 | if (DECL_NAME (field) == name) | |
1792 | break; | |
1793 | if (!field) | |
471086d6 | 1794 | { |
b52bd4ae | 1795 | if (complain & tf_error) |
1796 | error ("no field %qD found in union being initialized", | |
1797 | field); | |
c75b4594 | 1798 | ce->value = error_mark_node; |
471086d6 | 1799 | } |
c75b4594 | 1800 | ce->index = field; |
1801 | } | |
1802 | else | |
1803 | { | |
1804 | gcc_assert (TREE_CODE (ce->index) == INTEGER_CST | |
1805 | || TREE_CODE (ce->index) == RANGE_EXPR); | |
b52bd4ae | 1806 | if (complain & tf_error) |
1807 | error ("index value instead of field name in union initializer"); | |
c75b4594 | 1808 | ce->value = error_mark_node; |
471086d6 | 1809 | } |
471086d6 | 1810 | } |
c75b4594 | 1811 | else |
471086d6 | 1812 | { |
471086d6 | 1813 | /* Find the first named field. ANSI decided in September 1990 |
1814 | that only named fields count here. */ | |
c75b4594 | 1815 | tree field = TYPE_FIELDS (type); |
23ed74d8 | 1816 | while (field && (!DECL_NAME (field) || TREE_CODE (field) != FIELD_DECL)) |
471086d6 | 1817 | field = TREE_CHAIN (field); |
23a0ce43 | 1818 | if (field == NULL_TREE) |
1819 | { | |
b52bd4ae | 1820 | if (complain & tf_error) |
1821 | error ("too many initializers for %qT", type); | |
23a0ce43 | 1822 | ce->value = error_mark_node; |
1823 | } | |
c75b4594 | 1824 | ce->index = field; |
1825 | } | |
471086d6 | 1826 | |
c75b4594 | 1827 | if (ce->value && ce->value != error_mark_node) |
7aac6ee6 | 1828 | ce->value = massage_init_elt (TREE_TYPE (ce->index), ce->value, nested, |
c38103e8 | 1829 | flags, complain); |
471086d6 | 1830 | |
c75b4594 | 1831 | return picflag_from_initializer (ce->value); |
1832 | } | |
471086d6 | 1833 | |
c75b4594 | 1834 | /* Process INIT, a constructor for a variable of aggregate type TYPE. The |
1835 | constructor is a brace-enclosed initializer, and will be modified in-place. | |
1836 | ||
1837 | Each element is converted to the right type through digest_init, and | |
1838 | missing initializers are added following the language rules (zero-padding, | |
1839 | etc.). | |
471086d6 | 1840 | |
c75b4594 | 1841 | After the execution, the initializer will have TREE_CONSTANT if all elts are |
1842 | constant, and TREE_STATIC set if, in addition, all elts are simple enough | |
1843 | constants that the assembler and linker can compute them. | |
074ab442 | 1844 | |
c75b4594 | 1845 | The function returns the initializer itself, or error_mark_node in case |
1846 | of error. */ | |
1847 | ||
1848 | static tree | |
c38103e8 | 1849 | process_init_constructor (tree type, tree init, int nested, int flags, |
7aac6ee6 | 1850 | tsubst_flags_t complain) |
c75b4594 | 1851 | { |
c38103e8 | 1852 | int picflags; |
c75b4594 | 1853 | |
1854 | gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init)); | |
1855 | ||
76249021 | 1856 | if (TREE_CODE (type) == ARRAY_TYPE || VECTOR_TYPE_P (type)) |
c38103e8 | 1857 | picflags = process_init_constructor_array (type, init, nested, flags, |
1858 | complain); | |
c75b4594 | 1859 | else if (TREE_CODE (type) == RECORD_TYPE) |
c38103e8 | 1860 | picflags = process_init_constructor_record (type, init, nested, flags, |
1861 | complain); | |
c75b4594 | 1862 | else if (TREE_CODE (type) == UNION_TYPE) |
c38103e8 | 1863 | picflags = process_init_constructor_union (type, init, nested, flags, |
1864 | complain); | |
c75b4594 | 1865 | else |
1866 | gcc_unreachable (); | |
471086d6 | 1867 | |
c38103e8 | 1868 | if (picflags & PICFLAG_ERRONEOUS) |
471086d6 | 1869 | return error_mark_node; |
1870 | ||
c75b4594 | 1871 | TREE_TYPE (init) = type; |
981a251c | 1872 | if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == NULL_TREE) |
c75b4594 | 1873 | cp_complete_array_type (&TREE_TYPE (init), init, /*do_default=*/0); |
c38103e8 | 1874 | if (picflags & PICFLAG_SIDE_EFFECTS) |
48f76477 | 1875 | { |
1876 | TREE_CONSTANT (init) = false; | |
1877 | TREE_SIDE_EFFECTS (init) = true; | |
1878 | } | |
c38103e8 | 1879 | else if (picflags & PICFLAG_NOT_ALL_CONSTANT) |
0138a8ea | 1880 | /* Make sure TREE_CONSTANT isn't set from build_constructor. */ |
1881 | TREE_CONSTANT (init) = false; | |
1882 | else | |
4ee9c684 | 1883 | { |
c75b4594 | 1884 | TREE_CONSTANT (init) = 1; |
c38103e8 | 1885 | if (!(picflags & PICFLAG_NOT_ALL_SIMPLE)) |
c75b4594 | 1886 | TREE_STATIC (init) = 1; |
4ee9c684 | 1887 | } |
c75b4594 | 1888 | return init; |
471086d6 | 1889 | } |
1890 | \f | |
1891 | /* Given a structure or union value DATUM, construct and return | |
1892 | the structure or union component which results from narrowing | |
8ef5085e | 1893 | that value to the base specified in BASETYPE. For example, given the |
471086d6 | 1894 | hierarchy |
1895 | ||
1896 | class L { int ii; }; | |
1897 | class A : L { ... }; | |
1898 | class B : L { ... }; | |
1899 | class C : A, B { ... }; | |
1900 | ||
1901 | and the declaration | |
1902 | ||
1903 | C x; | |
1904 | ||
1905 | then the expression | |
1906 | ||
652e1a2d | 1907 | x.A::ii refers to the ii member of the L part of |
3398e91d | 1908 | the A part of the C object named by X. In this case, |
c161288a | 1909 | DATUM would be x, and BASETYPE would be A. |
1910 | ||
2a88c9e6 | 1911 | I used to think that this was nonconformant, that the standard specified |
1912 | that first we look up ii in A, then convert x to an L& and pull out the | |
1913 | ii part. But in fact, it does say that we convert x to an A&; A here | |
8ef5085e | 1914 | is known as the "naming class". (jason 2000-12-19) |
1915 | ||
1916 | BINFO_P points to a variable initialized either to NULL_TREE or to the | |
1917 | binfo for the specific base subobject we want to convert to. */ | |
471086d6 | 1918 | |
1919 | tree | |
e394ed0f | 1920 | build_scoped_ref (tree datum, tree basetype, tree* binfo_p) |
471086d6 | 1921 | { |
4a2680fc | 1922 | tree binfo; |
471086d6 | 1923 | |
1924 | if (datum == error_mark_node) | |
1925 | return error_mark_node; | |
8ef5085e | 1926 | if (*binfo_p) |
1927 | binfo = *binfo_p; | |
1928 | else | |
ae260dcc | 1929 | binfo = lookup_base (TREE_TYPE (datum), basetype, ba_check, |
1930 | NULL, tf_warning_or_error); | |
471086d6 | 1931 | |
8ef5085e | 1932 | if (!binfo || binfo == error_mark_node) |
1933 | { | |
1934 | *binfo_p = NULL_TREE; | |
1935 | if (!binfo) | |
1936 | error_not_base_type (basetype, TREE_TYPE (datum)); | |
1937 | return error_mark_node; | |
1938 | } | |
471086d6 | 1939 | |
8ef5085e | 1940 | *binfo_p = binfo; |
1e74225a | 1941 | return build_base_path (PLUS_EXPR, datum, binfo, 1, |
1942 | tf_warning_or_error); | |
471086d6 | 1943 | } |
1944 | ||
1945 | /* Build a reference to an object specified by the C++ `->' operator. | |
1946 | Usually this just involves dereferencing the object, but if the | |
1947 | `->' operator is overloaded, then such overloads must be | |
1948 | performed until an object which does not have the `->' operator | |
1949 | overloaded is found. An error is reported when circular pointer | |
1950 | delegation is detected. */ | |
96624a9e | 1951 | |
471086d6 | 1952 | tree |
ef0b0c72 | 1953 | build_x_arrow (location_t loc, tree expr, tsubst_flags_t complain) |
471086d6 | 1954 | { |
13795292 | 1955 | tree orig_expr = expr; |
13795292 | 1956 | tree type = TREE_TYPE (expr); |
034b484a | 1957 | tree last_rval = NULL_TREE; |
f1f41a6c | 1958 | vec<tree, va_gc> *types_memoized = NULL; |
471086d6 | 1959 | |
1960 | if (type == error_mark_node) | |
1961 | return error_mark_node; | |
1962 | ||
3cc0b4b9 | 1963 | if (processing_template_decl) |
13795292 | 1964 | { |
90ad495b | 1965 | if (type && TYPE_PTR_P (type) |
eee80116 | 1966 | && !dependent_scope_p (TREE_TYPE (type))) |
1967 | /* Pointer to current instantiation, don't treat as dependent. */; | |
1968 | else if (type_dependent_expression_p (expr)) | |
255b5d15 | 1969 | return build_min_nt_loc (loc, ARROW_EXPR, expr); |
13795292 | 1970 | expr = build_non_dependent_expr (expr); |
1971 | } | |
e857e9c7 | 1972 | |
95397ff9 | 1973 | if (MAYBE_CLASS_TYPE_P (type)) |
471086d6 | 1974 | { |
9951fe5d | 1975 | struct tinst_level *actual_inst = current_instantiation (); |
1976 | tree fn = NULL; | |
1977 | ||
ef0b0c72 | 1978 | while ((expr = build_new_op (loc, COMPONENT_REF, |
1979 | LOOKUP_NORMAL, expr, NULL_TREE, NULL_TREE, | |
2ad2700d | 1980 | &fn, complain))) |
471086d6 | 1981 | { |
13795292 | 1982 | if (expr == error_mark_node) |
471086d6 | 1983 | return error_mark_node; |
1984 | ||
8f51a05f | 1985 | /* This provides a better instantiation backtrace in case of |
1986 | error. */ | |
9951fe5d | 1987 | if (fn && DECL_USE_TEMPLATE (fn)) |
8f51a05f | 1988 | push_tinst_level_loc (fn, |
1989 | (current_instantiation () != actual_inst) | |
1990 | ? DECL_SOURCE_LOCATION (fn) | |
1991 | : input_location); | |
9951fe5d | 1992 | fn = NULL; |
1993 | ||
1df3c246 | 1994 | if (vec_member (TREE_TYPE (expr), types_memoized)) |
1995 | { | |
2ad2700d | 1996 | if (complain & tf_error) |
1997 | error ("circular pointer delegation detected"); | |
1df3c246 | 1998 | return error_mark_node; |
1999 | } | |
c6723852 | 2000 | |
f1f41a6c | 2001 | vec_safe_push (types_memoized, TREE_TYPE (expr)); |
13795292 | 2002 | last_rval = expr; |
9031d10b | 2003 | } |
02add86d | 2004 | |
9951fe5d | 2005 | while (current_instantiation () != actual_inst) |
2006 | pop_tinst_level (); | |
2007 | ||
02add86d | 2008 | if (last_rval == NULL_TREE) |
2009 | { | |
2ad2700d | 2010 | if (complain & tf_error) |
2011 | error ("base operand of %<->%> has non-pointer type %qT", type); | |
02add86d | 2012 | return error_mark_node; |
2013 | } | |
2014 | ||
90ad495b | 2015 | if (TYPE_REF_P (TREE_TYPE (last_rval))) |
471086d6 | 2016 | last_rval = convert_from_reference (last_rval); |
2017 | } | |
2018 | else | |
4405c1ad | 2019 | last_rval = decay_conversion (expr, complain); |
471086d6 | 2020 | |
c21c015b | 2021 | if (TYPE_PTR_P (TREE_TYPE (last_rval))) |
13795292 | 2022 | { |
2023 | if (processing_template_decl) | |
b6691ff5 | 2024 | { |
4cbcb428 | 2025 | expr = build_min (ARROW_EXPR, TREE_TYPE (TREE_TYPE (last_rval)), |
2026 | orig_expr); | |
2027 | TREE_SIDE_EFFECTS (expr) = TREE_SIDE_EFFECTS (last_rval); | |
b6691ff5 | 2028 | return expr; |
2029 | } | |
13795292 | 2030 | |
0744a0c1 | 2031 | return cp_build_indirect_ref (last_rval, RO_ARROW, complain); |
13795292 | 2032 | } |
471086d6 | 2033 | |
2ad2700d | 2034 | if (complain & tf_error) |
2035 | { | |
2036 | if (types_memoized) | |
2037 | error ("result of %<operator->()%> yields non-pointer result"); | |
2038 | else | |
2039 | error ("base operand of %<->%> is not a pointer"); | |
2040 | } | |
471086d6 | 2041 | return error_mark_node; |
2042 | } | |
2043 | ||
a63bc44c | 2044 | /* Return an expression for "DATUM .* COMPONENT". DATUM has not |
2045 | already been checked out to be of aggregate type. */ | |
96624a9e | 2046 | |
471086d6 | 2047 | tree |
4405c1ad | 2048 | build_m_component_ref (tree datum, tree component, tsubst_flags_t complain) |
471086d6 | 2049 | { |
a63bc44c | 2050 | tree ptrmem_type; |
24f9a660 | 2051 | tree objtype; |
a63bc44c | 2052 | tree type; |
74002e1d | 2053 | tree binfo; |
a6460bf1 | 2054 | tree ctype; |
471086d6 | 2055 | |
24357002 | 2056 | if (error_operand_p (datum) || error_operand_p (component)) |
ab9559dc | 2057 | return error_mark_node; |
2058 | ||
6ac80fa2 | 2059 | datum = mark_lvalue_use (datum); |
2060 | component = mark_rvalue_use (component); | |
c507cbd7 | 2061 | |
a63bc44c | 2062 | ptrmem_type = TREE_TYPE (component); |
05765a91 | 2063 | if (!TYPE_PTRMEM_P (ptrmem_type)) |
471086d6 | 2064 | { |
4405c1ad | 2065 | if (complain & tf_error) |
2066 | error ("%qE cannot be used as a member pointer, since it is of " | |
2067 | "type %qT", component, ptrmem_type); | |
471086d6 | 2068 | return error_mark_node; |
2069 | } | |
9031d10b | 2070 | |
2071 | objtype = TYPE_MAIN_VARIANT (TREE_TYPE (datum)); | |
95397ff9 | 2072 | if (! MAYBE_CLASS_TYPE_P (objtype)) |
ac9386a0 | 2073 | { |
4405c1ad | 2074 | if (complain & tf_error) |
2075 | error ("cannot apply member pointer %qE to %qE, which is of " | |
2076 | "non-class type %qT", component, datum, objtype); | |
ac9386a0 | 2077 | return error_mark_node; |
2078 | } | |
74002e1d | 2079 | |
a63bc44c | 2080 | type = TYPE_PTRMEM_POINTED_TO_TYPE (ptrmem_type); |
a6460bf1 | 2081 | ctype = complete_type (TYPE_PTRMEM_CLASS_TYPE (ptrmem_type)); |
2082 | ||
2083 | if (!COMPLETE_TYPE_P (ctype)) | |
471086d6 | 2084 | { |
a6460bf1 | 2085 | if (!same_type_p (ctype, objtype)) |
2086 | goto mismatch; | |
2087 | binfo = NULL; | |
2088 | } | |
2089 | else | |
2090 | { | |
ae260dcc | 2091 | binfo = lookup_base (objtype, ctype, ba_check, NULL, complain); |
9031d10b | 2092 | |
a6460bf1 | 2093 | if (!binfo) |
2094 | { | |
2095 | mismatch: | |
4405c1ad | 2096 | if (complain & tf_error) |
2097 | error ("pointer to member type %qT incompatible with object " | |
2098 | "type %qT", type, objtype); | |
a6460bf1 | 2099 | return error_mark_node; |
2100 | } | |
2101 | else if (binfo == error_mark_node) | |
2102 | return error_mark_node; | |
471086d6 | 2103 | } |
2104 | ||
05765a91 | 2105 | if (TYPE_PTRDATAMEM_P (ptrmem_type)) |
a63bc44c | 2106 | { |
3f5730c2 | 2107 | bool is_lval = real_lvalue_p (datum); |
69db191c | 2108 | tree ptype; |
2109 | ||
a63bc44c | 2110 | /* Compute the type of the field, as described in [expr.ref]. |
2111 | There's no such thing as a mutable pointer-to-member, so | |
2112 | things are not as complex as they are for references to | |
2113 | non-static data members. */ | |
2114 | type = cp_build_qualified_type (type, | |
9031d10b | 2115 | (cp_type_quals (type) |
a63bc44c | 2116 | | cp_type_quals (TREE_TYPE (datum)))); |
a6460bf1 | 2117 | |
2118 | datum = build_address (datum); | |
9031d10b | 2119 | |
a6460bf1 | 2120 | /* Convert object to the correct base. */ |
2121 | if (binfo) | |
4405c1ad | 2122 | { |
2123 | datum = build_base_path (PLUS_EXPR, datum, binfo, 1, complain); | |
2124 | if (datum == error_mark_node) | |
2125 | return error_mark_node; | |
2126 | } | |
9031d10b | 2127 | |
1bc16cab | 2128 | /* Build an expression for "object + offset" where offset is the |
2129 | value stored in the pointer-to-data-member. */ | |
69db191c | 2130 | ptype = build_pointer_type (type); |
2cc66f2a | 2131 | datum = fold_build_pointer_plus (fold_convert (ptype, datum), component); |
0744a0c1 | 2132 | datum = cp_build_fold_indirect_ref (datum); |
4405c1ad | 2133 | if (datum == error_mark_node) |
2134 | return error_mark_node; | |
2135 | ||
0277b639 | 2136 | /* If the object expression was an rvalue, return an rvalue. */ |
3f5730c2 | 2137 | if (!is_lval) |
0277b639 | 2138 | datum = move (datum); |
2139 | return datum; | |
a63bc44c | 2140 | } |
2141 | else | |
e116411c | 2142 | { |
2143 | /* 5.5/6: In a .* expression whose object expression is an rvalue, the | |
2144 | program is ill-formed if the second operand is a pointer to member | |
dabb58ae | 2145 | function with ref-qualifier & (for C++2A: unless its cv-qualifier-seq |
2146 | is const). In a .* expression whose object expression is an lvalue, | |
2147 | the program is ill-formed if the second operand is a pointer to member | |
2148 | function with ref-qualifier &&. */ | |
e116411c | 2149 | if (FUNCTION_REF_QUALIFIED (type)) |
2150 | { | |
18bede74 | 2151 | bool lval = lvalue_p (datum); |
e116411c | 2152 | if (lval && FUNCTION_RVALUE_QUALIFIED (type)) |
6f2aadc8 | 2153 | { |
2154 | if (complain & tf_error) | |
2155 | error ("pointer-to-member-function type %qT requires an rvalue", | |
2156 | ptrmem_type); | |
2157 | return error_mark_node; | |
2158 | } | |
5753ddf9 | 2159 | else if (!lval && !FUNCTION_RVALUE_QUALIFIED (type)) |
6f2aadc8 | 2160 | { |
5753ddf9 | 2161 | if ((type_memfn_quals (type) |
2162 | & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)) | |
2163 | != TYPE_QUAL_CONST) | |
2164 | { | |
2165 | if (complain & tf_error) | |
2166 | error ("pointer-to-member-function type %qT requires " | |
2167 | "an lvalue", ptrmem_type); | |
2168 | return error_mark_node; | |
2169 | } | |
2170 | else if (cxx_dialect < cxx2a) | |
2171 | { | |
2172 | if (complain & tf_warning_or_error) | |
2173 | pedwarn (input_location, OPT_Wpedantic, | |
2174 | "pointer-to-member-function type %qT requires " | |
2175 | "an lvalue before C++2a", ptrmem_type); | |
2176 | else | |
2177 | return error_mark_node; | |
2178 | } | |
6f2aadc8 | 2179 | } |
e116411c | 2180 | } |
2181 | return build2 (OFFSET_REF, type, datum, component); | |
2182 | } | |
471086d6 | 2183 | } |
2184 | ||
d2a15a12 | 2185 | /* Return a tree node for the expression TYPENAME '(' PARMS ')'. */ |
96624a9e | 2186 | |
471086d6 | 2187 | tree |
ebd21de4 | 2188 | build_functional_cast (tree exp, tree parms, tsubst_flags_t complain) |
471086d6 | 2189 | { |
2190 | /* This is either a call to a constructor, | |
2191 | or a C cast in C++'s `functional' notation. */ | |
930e8175 | 2192 | |
2193 | /* The type to which we are casting. */ | |
d2a15a12 | 2194 | tree type; |
471086d6 | 2195 | |
bd157edd | 2196 | if (error_operand_p (exp) || parms == error_mark_node) |
471086d6 | 2197 | return error_mark_node; |
2198 | ||
b7d1e8ea | 2199 | if (TREE_CODE (exp) == TYPE_DECL) |
2b2b5104 | 2200 | { |
2201 | type = TREE_TYPE (exp); | |
2202 | ||
17847cff | 2203 | if (DECL_ARTIFICIAL (exp)) |
e57bcc46 | 2204 | cp_warn_deprecated_use (type); |
2b2b5104 | 2205 | } |
471086d6 | 2206 | else |
2207 | type = exp; | |
2208 | ||
04725249 | 2209 | /* We need to check this explicitly, since value-initialization of |
2210 | arrays is allowed in other situations. */ | |
2211 | if (TREE_CODE (type) == ARRAY_TYPE) | |
2212 | { | |
2213 | if (complain & tf_error) | |
2214 | error ("functional cast to array type %qT", type); | |
2215 | return error_mark_node; | |
2216 | } | |
2217 | ||
c2ab9194 | 2218 | if (tree anode = type_uses_auto (type)) |
32d9c766 | 2219 | { |
c2ab9194 | 2220 | if (!CLASS_PLACEHOLDER_TEMPLATE (anode)) |
2221 | { | |
2222 | if (complain & tf_error) | |
cb7f839b | 2223 | error_at (DECL_SOURCE_LOCATION (TEMPLATE_TYPE_DECL (anode)), |
2224 | "invalid use of %qT", anode); | |
c2ab9194 | 2225 | return error_mark_node; |
2226 | } | |
2227 | else if (!parms) | |
2228 | { | |
6080dd2f | 2229 | /* Even if there are no parameters, we might be able to deduce from |
2230 | default template arguments. Pass TF_NONE so that we don't | |
2231 | generate redundant diagnostics. */ | |
2232 | type = do_auto_deduction (type, parms, anode, tf_none, | |
2233 | adc_variable_type); | |
2234 | if (type == error_mark_node) | |
2235 | { | |
2236 | if (complain & tf_error) | |
85b9be9b | 2237 | error ("cannot deduce template arguments for %qT from %<()%>", |
6080dd2f | 2238 | anode); |
2239 | return error_mark_node; | |
2240 | } | |
c2ab9194 | 2241 | } |
2242 | else | |
2243 | type = do_auto_deduction (type, parms, anode, complain, | |
2244 | adc_variable_type); | |
32d9c766 | 2245 | } |
2246 | ||
3cc0b4b9 | 2247 | if (processing_template_decl) |
b6691ff5 | 2248 | { |
74b7a9bc | 2249 | tree t; |
2250 | ||
2251 | /* Diagnose this even in a template. We could also try harder | |
2252 | to give all the usual errors when the type and args are | |
2253 | non-dependent... */ | |
90ad495b | 2254 | if (TYPE_REF_P (type) && !parms) |
74b7a9bc | 2255 | { |
2256 | if (complain & tf_error) | |
2257 | error ("invalid value-initialization of reference type"); | |
2258 | return error_mark_node; | |
2259 | } | |
2260 | ||
2261 | t = build_min (CAST_EXPR, type, parms); | |
b6691ff5 | 2262 | /* We don't know if it will or will not have side effects. */ |
2263 | TREE_SIDE_EFFECTS (t) = 1; | |
2264 | return t; | |
2265 | } | |
e857e9c7 | 2266 | |
95397ff9 | 2267 | if (! MAYBE_CLASS_TYPE_P (type)) |
471086d6 | 2268 | { |
471086d6 | 2269 | if (parms == NULL_TREE) |
61e4f7d9 | 2270 | { |
2271 | if (VOID_TYPE_P (type)) | |
3ab4693e | 2272 | return void_node; |
6f905cd1 | 2273 | return build_value_init (cv_unqualified (type), complain); |
61e4f7d9 | 2274 | } |
1a3f833b | 2275 | |
5c775397 | 2276 | /* This must build a C cast. */ |
1f3d2e3f | 2277 | parms = build_x_compound_expr_from_list (parms, ELK_FUNC_CAST, complain); |
717ecce9 | 2278 | return cp_build_c_cast (type, parms, complain); |
471086d6 | 2279 | } |
2280 | ||
e178a5a9 | 2281 | /* Prepare to evaluate as a call to a constructor. If this expression |
2282 | is actually used, for example, | |
9031d10b | 2283 | |
e178a5a9 | 2284 | return X (arg1, arg2, ...); |
9031d10b | 2285 | |
e178a5a9 | 2286 | then the slot being initialized will be filled in. */ |
2287 | ||
a5f2d620 | 2288 | if (!complete_type_or_maybe_complain (type, NULL_TREE, complain)) |
4b72716d | 2289 | return error_mark_node; |
d28993f1 | 2290 | if (abstract_virtuals_error_sfinae (ACU_CAST, type, complain)) |
8c18e707 | 2291 | return error_mark_node; |
471086d6 | 2292 | |
930e8175 | 2293 | /* [expr.type.conv] |
2294 | ||
2295 | If the expression list is a single-expression, the type | |
2296 | conversion is equivalent (in definedness, and if defined in | |
2297 | meaning) to the corresponding cast expression. */ | |
471086d6 | 2298 | if (parms && TREE_CHAIN (parms) == NULL_TREE) |
717ecce9 | 2299 | return cp_build_c_cast (type, TREE_VALUE (parms), complain); |
471086d6 | 2300 | |
930e8175 | 2301 | /* [expr.type.conv] |
2302 | ||
2303 | The expression T(), where T is a simple-type-specifier for a | |
2304 | non-array complete object type or the (possibly cv-qualified) | |
2305 | void type, creates an rvalue of the specified type, which is | |
2306 | value-initialized. */ | |
2307 | ||
a366cfa9 | 2308 | if (parms == NULL_TREE) |
df7d45e1 | 2309 | { |
a5f2d620 | 2310 | exp = build_value_init (type, complain); |
9e505437 | 2311 | exp = get_target_expr_sfinae (exp, complain); |
ce984e5e | 2312 | return exp; |
df7d45e1 | 2313 | } |
2314 | ||
930e8175 | 2315 | /* Call the constructor. */ |
02fafda8 | 2316 | releasing_vec parmvec; |
f352a3fb | 2317 | for (; parms != NULL_TREE; parms = TREE_CHAIN (parms)) |
f1f41a6c | 2318 | vec_safe_push (parmvec, TREE_VALUE (parms)); |
f352a3fb | 2319 | exp = build_special_member_call (NULL_TREE, complete_ctor_identifier, |
2320 | &parmvec, type, LOOKUP_NORMAL, complain); | |
471086d6 | 2321 | |
d2a15a12 | 2322 | if (exp == error_mark_node) |
ddb9bca7 | 2323 | return error_mark_node; |
471086d6 | 2324 | |
3d4bed93 | 2325 | return build_cplus_new (type, exp, complain); |
471086d6 | 2326 | } |
2327 | \f | |
0e0a0dea | 2328 | |
316b7a44 | 2329 | /* Add new exception specifier SPEC, to the LIST we currently have. |
2330 | If it's already in LIST then do nothing. | |
2331 | Moan if it's bad and we're allowed to. COMPLAIN < 0 means we | |
2332 | know what we're doing. */ | |
2333 | ||
2334 | tree | |
a470c772 | 2335 | add_exception_specifier (tree list, tree spec, tsubst_flags_t complain) |
316b7a44 | 2336 | { |
bd57a622 | 2337 | bool ok; |
316b7a44 | 2338 | tree core = spec; |
bd57a622 | 2339 | bool is_ptr; |
a52d5726 | 2340 | diagnostic_t diag_type = DK_UNSPECIFIED; /* none */ |
9031d10b | 2341 | |
316b7a44 | 2342 | if (spec == error_mark_node) |
2343 | return list; | |
9031d10b | 2344 | |
b4df430b | 2345 | gcc_assert (spec && (!list || TREE_VALUE (list))); |
9031d10b | 2346 | |
316b7a44 | 2347 | /* [except.spec] 1, type in an exception specifier shall not be |
2348 | incomplete, or pointer or ref to incomplete other than pointer | |
2349 | to cv void. */ | |
c21c015b | 2350 | is_ptr = TYPE_PTR_P (core); |
90ad495b | 2351 | if (is_ptr || TYPE_REF_P (core)) |
316b7a44 | 2352 | core = TREE_TYPE (core); |
2353 | if (complain < 0) | |
bd57a622 | 2354 | ok = true; |
e3cfe3ce | 2355 | else if (VOID_TYPE_P (core)) |
316b7a44 | 2356 | ok = is_ptr; |
2357 | else if (TREE_CODE (core) == TEMPLATE_TYPE_PARM) | |
bd57a622 | 2358 | ok = true; |
ba1f2de0 | 2359 | else if (processing_template_decl) |
bd57a622 | 2360 | ok = true; |
316b7a44 | 2361 | else |
6fee0737 | 2362 | { |
bd57a622 | 2363 | ok = true; |
6fee0737 | 2364 | /* 15.4/1 says that types in an exception specifier must be complete, |
653e5405 | 2365 | but it seems more reasonable to only require this on definitions |
2366 | and calls. So just give a pedwarn at this point; we will give an | |
2367 | error later if we hit one of those two cases. */ | |
6fee0737 | 2368 | if (!COMPLETE_TYPE_P (complete_type (core))) |
a52d5726 | 2369 | diag_type = DK_PEDWARN; /* pedwarn */ |
6fee0737 | 2370 | } |
ba1f2de0 | 2371 | |
316b7a44 | 2372 | if (ok) |
2373 | { | |
2374 | tree probe; | |
9031d10b | 2375 | |
316b7a44 | 2376 | for (probe = list; probe; probe = TREE_CHAIN (probe)) |
653e5405 | 2377 | if (same_type_p (TREE_VALUE (probe), spec)) |
2378 | break; | |
316b7a44 | 2379 | if (!probe) |
0a7d30b7 | 2380 | list = tree_cons (NULL_TREE, spec, list); |
316b7a44 | 2381 | } |
6fee0737 | 2382 | else |
a52d5726 | 2383 | diag_type = DK_ERROR; /* error */ |
9031d10b | 2384 | |
86359a65 | 2385 | if (diag_type != DK_UNSPECIFIED |
2386 | && (complain & tf_warning_or_error)) | |
6fee0737 | 2387 | cxx_incomplete_type_diagnostic (NULL_TREE, core, diag_type); |
2388 | ||
316b7a44 | 2389 | return list; |
2390 | } | |
d8ae7647 | 2391 | |
3239620b | 2392 | /* Like nothrow_spec_p, but don't abort on deferred noexcept. */ |
2393 | ||
2394 | static bool | |
2395 | nothrow_spec_p_uninst (const_tree spec) | |
2396 | { | |
2397 | if (DEFERRED_NOEXCEPT_SPEC_P (spec)) | |
2398 | return false; | |
2399 | return nothrow_spec_p (spec); | |
2400 | } | |
2401 | ||
d8ae7647 | 2402 | /* Combine the two exceptions specifier lists LIST and ADD, and return |
f2c1aabc | 2403 | their union. */ |
d8ae7647 | 2404 | |
2405 | tree | |
f2c1aabc | 2406 | merge_exception_specifiers (tree list, tree add) |
d8ae7647 | 2407 | { |
3239620b | 2408 | tree noex, orig_list; |
2409 | ||
ce7f22f1 | 2410 | if (list == error_mark_node || add == error_mark_node) |
2411 | return error_mark_node; | |
2412 | ||
9f354539 | 2413 | /* No exception-specifier or noexcept(false) are less strict than |
2414 | anything else. Prefer the newer variant (LIST). */ | |
2415 | if (!list || list == noexcept_false_spec) | |
2416 | return list; | |
2417 | else if (!add || add == noexcept_false_spec) | |
2418 | return add; | |
6bb4902d | 2419 | |
3239620b | 2420 | /* noexcept(true) and throw() are stricter than anything else. |
2421 | As above, prefer the more recent one (LIST). */ | |
2422 | if (nothrow_spec_p_uninst (add)) | |
d8ae7647 | 2423 | return list; |
3239620b | 2424 | |
f2c1aabc | 2425 | /* Two implicit noexcept specs (e.g. on a destructor) are equivalent. */ |
2426 | if (UNEVALUATED_NOEXCEPT_SPEC_P (add) | |
2427 | && UNEVALUATED_NOEXCEPT_SPEC_P (list)) | |
2428 | return list; | |
2429 | /* We should have instantiated other deferred noexcept specs by now. */ | |
2430 | gcc_assert (!DEFERRED_NOEXCEPT_SPEC_P (add)); | |
2431 | ||
2432 | if (nothrow_spec_p_uninst (list)) | |
02fe61a4 | 2433 | return add; |
f2c1aabc | 2434 | noex = TREE_PURPOSE (list); |
2435 | gcc_checking_assert (!TREE_PURPOSE (add) | |
4c3bd1e3 | 2436 | || errorcount || !flag_exceptions |
f2c1aabc | 2437 | || cp_tree_equal (noex, TREE_PURPOSE (add))); |
3239620b | 2438 | |
2439 | /* Combine the dynamic-exception-specifiers, if any. */ | |
2440 | orig_list = list; | |
2441 | for (; add && TREE_VALUE (add); add = TREE_CHAIN (add)) | |
d8ae7647 | 2442 | { |
3239620b | 2443 | tree spec = TREE_VALUE (add); |
2444 | tree probe; | |
9031d10b | 2445 | |
3239620b | 2446 | for (probe = orig_list; probe && TREE_VALUE (probe); |
2447 | probe = TREE_CHAIN (probe)) | |
2448 | if (same_type_p (TREE_VALUE (probe), spec)) | |
2449 | break; | |
2450 | if (!probe) | |
653e5405 | 2451 | { |
3239620b | 2452 | spec = build_tree_list (NULL_TREE, spec); |
2453 | TREE_CHAIN (spec) = list; | |
2454 | list = spec; | |
653e5405 | 2455 | } |
d8ae7647 | 2456 | } |
3239620b | 2457 | |
2458 | /* Keep the noexcept-specifier at the beginning of the list. */ | |
2459 | if (noex != TREE_PURPOSE (list)) | |
2460 | list = tree_cons (noex, TREE_VALUE (list), TREE_CHAIN (list)); | |
2461 | ||
d8ae7647 | 2462 | return list; |
2463 | } | |
6fee0737 | 2464 | |
2465 | /* Subroutine of build_call. Ensure that each of the types in the | |
2466 | exception specification is complete. Technically, 15.4/1 says that | |
2467 | they need to be complete when we see a declaration of the function, | |
2468 | but we should be able to get away with only requiring this when the | |
2469 | function is defined or called. See also add_exception_specifier. */ | |
2470 | ||
2471 | void | |
e394ed0f | 2472 | require_complete_eh_spec_types (tree fntype, tree decl) |
6fee0737 | 2473 | { |
2474 | tree raises; | |
2475 | /* Don't complain about calls to op new. */ | |
2476 | if (decl && DECL_ARTIFICIAL (decl)) | |
2477 | return; | |
2478 | for (raises = TYPE_RAISES_EXCEPTIONS (fntype); raises; | |
2479 | raises = TREE_CHAIN (raises)) | |
2480 | { | |
2481 | tree type = TREE_VALUE (raises); | |
2482 | if (type && !COMPLETE_TYPE_P (type)) | |
2483 | { | |
2484 | if (decl) | |
2485 | error | |
03d6ca9e | 2486 | ("call to function %qD which throws incomplete type %q#T", |
6fee0737 | 2487 | decl, type); |
2488 | else | |
03d6ca9e | 2489 | error ("call to function which throws incomplete type %q#T", |
6fee0737 | 2490 | decl); |
2491 | } | |
2492 | } | |
2493 | } | |
d97a7640 | 2494 | |
2495 | \f | |
2496 | #include "gt-cp-typeck2.h" |