]>
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 | { |
c75b4594 | 606 | unsigned HOST_WIDE_INT idx; |
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 |
660 | CONSTRUCTOR_ELTS (init)->ordered_remove (idx--); | |
927b65fb | 661 | num_split_elts++; |
3afe9b43 | 662 | } |
663 | else if (!initializer_constant_valid_p (value, inner_type)) | |
664 | { | |
c75b4594 | 665 | tree code; |
666 | tree sub; | |
667 | ||
668 | /* FIXME: Ordered removal is O(1) so the whole function is | |
669 | worst-case quadratic. This could be fixed using an aside | |
670 | bitmap to record which elements must be removed and remove | |
671 | them all at the same time. Or by merging | |
672 | split_non_constant_init into process_init_constructor_array, | |
673 | that is separating constants from non-constants while building | |
674 | the vector. */ | |
f1f41a6c | 675 | CONSTRUCTOR_ELTS (init)->ordered_remove (idx); |
c75b4594 | 676 | --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 | } |
714 | break; | |
715 | ||
716 | case VECTOR_TYPE: | |
717 | if (!initializer_constant_valid_p (init, type)) | |
718 | { | |
c75b4594 | 719 | tree code; |
4634db76 | 720 | tree cons = copy_node (init); |
3afe9b43 | 721 | CONSTRUCTOR_ELTS (init) = NULL; |
4634db76 | 722 | code = build2 (MODIFY_EXPR, type, dest, cons); |
e60a6f7b | 723 | code = build_stmt (input_location, EXPR_STMT, code); |
2363ef00 | 724 | add_stmt (code); |
927b65fb | 725 | num_split_elts += CONSTRUCTOR_NELTS (init); |
3afe9b43 | 726 | } |
727 | break; | |
728 | ||
729 | default: | |
2e3e31d2 | 730 | gcc_unreachable (); |
3afe9b43 | 731 | } |
52acb923 | 732 | |
733 | /* The rest of the initializer is now a constant. */ | |
734 | TREE_CONSTANT (init) = 1; | |
96fa24ad | 735 | |
736 | /* We didn't split out anything. */ | |
737 | if (num_split_elts == 0) | |
738 | return false; | |
739 | ||
927b65fb | 740 | return complete_p && complete_ctor_at_level_p (TREE_TYPE (init), |
741 | num_split_elts, inner_type); | |
3afe9b43 | 742 | } |
743 | ||
9031d10b | 744 | /* A subroutine of store_init_value. Splits non-constant static |
3afe9b43 | 745 | initializer INIT into a constant part and generates code to |
746 | perform the non-constant part of the initialization to DEST. | |
747 | Returns the code for the runtime init. */ | |
748 | ||
39786c80 | 749 | tree |
3afe9b43 | 750 | split_nonconstant_init (tree dest, tree init) |
751 | { | |
752 | tree code; | |
753 | ||
39786c80 | 754 | if (TREE_CODE (init) == TARGET_EXPR) |
755 | init = TARGET_EXPR_INITIAL (init); | |
3afe9b43 | 756 | if (TREE_CODE (init) == CONSTRUCTOR) |
757 | { | |
d560f985 | 758 | init = cp_fully_fold_init (init); |
2363ef00 | 759 | code = push_stmt_list (); |
927b65fb | 760 | if (split_nonconstant_init_1 (dest, init)) |
761 | init = NULL_TREE; | |
2363ef00 | 762 | code = pop_stmt_list (code); |
3afe9b43 | 763 | DECL_INITIAL (dest) = init; |
09742c66 | 764 | TREE_READONLY (dest) = 0; |
3afe9b43 | 765 | } |
c571b0c6 | 766 | else if (TREE_CODE (init) == STRING_CST |
767 | && array_of_runtime_bound_p (TREE_TYPE (dest))) | |
768 | code = build_vec_init (dest, NULL_TREE, init, /*value-init*/false, | |
769 | /*from array*/1, tf_warning_or_error); | |
3afe9b43 | 770 | else |
831d52a2 | 771 | code = build2 (INIT_EXPR, TREE_TYPE (dest), dest, init); |
3afe9b43 | 772 | |
773 | return code; | |
774 | } | |
775 | ||
471086d6 | 776 | /* Perform appropriate conversions on the initial value of a variable, |
777 | store it in the declaration DECL, | |
778 | and print any error messages that are appropriate. | |
779 | If the init is invalid, store an ERROR_MARK. | |
780 | ||
781 | C++: Note that INIT might be a TREE_LIST, which would mean that it is | |
782 | a base class initializer for some aggregate type, hopefully compatible | |
783 | with DECL. If INIT is a single element, and DECL is an aggregate | |
784 | type, we silently convert INIT into a TREE_LIST, allowing a constructor | |
785 | to be called. | |
786 | ||
787 | If INIT is a TREE_LIST and there is no constructor, turn INIT | |
788 | into a CONSTRUCTOR and use standard initialization techniques. | |
789 | Perhaps a warning should be generated? | |
790 | ||
3afe9b43 | 791 | Returns code to be executed if initialization could not be performed |
792 | for static variable. In that case, caller must emit the code. */ | |
471086d6 | 793 | |
794 | tree | |
f1f41a6c | 795 | store_init_value (tree decl, tree init, vec<tree, va_gc>** cleanups, int flags) |
471086d6 | 796 | { |
cd16867a | 797 | tree value, type; |
471086d6 | 798 | |
799 | /* If variable's type was invalidly declared, just ignore it. */ | |
800 | ||
801 | type = TREE_TYPE (decl); | |
802 | if (TREE_CODE (type) == ERROR_MARK) | |
803 | return NULL_TREE; | |
804 | ||
95397ff9 | 805 | if (MAYBE_CLASS_TYPE_P (type)) |
471086d6 | 806 | { |
771665d8 | 807 | if (TREE_CODE (init) == TREE_LIST) |
471086d6 | 808 | { |
03d6ca9e | 809 | error ("constructor syntax used, but no constructor declared " |
653e5405 | 810 | "for type %qT", type); |
f82f1250 | 811 | init = build_constructor_from_list (init_list_type_node, nreverse (init)); |
471086d6 | 812 | } |
471086d6 | 813 | } |
471086d6 | 814 | |
815 | /* End of special C++ code. */ | |
816 | ||
ce984e5e | 817 | if (flags & LOOKUP_ALREADY_DIGESTED) |
818 | value = init; | |
819 | else | |
c38103e8 | 820 | { |
821 | if (TREE_STATIC (decl)) | |
822 | flags |= LOOKUP_ALLOW_FLEXARRAY_INIT; | |
823 | /* Digest the specified initializer into an expression. */ | |
824 | value = digest_init_flags (type, init, flags, tf_warning_or_error); | |
825 | } | |
ce984e5e | 826 | |
dc0cf270 | 827 | /* Look for braced array initializers for character arrays and |
828 | recursively convert them into STRING_CSTs. */ | |
829 | value = braced_lists_to_strings (type, value); | |
d839099f | 830 | |
8c89c5fc | 831 | current_ref_temp_count = 0; |
c7b89256 | 832 | value = extend_ref_init_temps (decl, value, cleanups); |
833 | ||
a7e27879 | 834 | /* In C++11 constant expression is a semantic, not syntactic, property. |
ce984e5e | 835 | In C++98, make sure that what we thought was a constant expression at |
1fff8e07 | 836 | template definition time is still constant and otherwise perform this |
837 | as optimization, e.g. to fold SIZEOF_EXPRs in the initializer. */ | |
838 | if (decl_maybe_constant_var_p (decl) || TREE_STATIC (decl)) | |
ce984e5e | 839 | { |
840 | bool const_init; | |
006b503a | 841 | tree oldval = value; |
7dffa847 | 842 | value = fold_non_dependent_expr (value); |
cd50b62d | 843 | if (DECL_DECLARED_CONSTEXPR_P (decl) |
bd20946f | 844 | || (DECL_IN_AGGR_P (decl) |
4e60ed29 | 845 | && DECL_INITIALIZED_IN_CLASS_P (decl))) |
03cbe7c9 | 846 | { |
bd20946f | 847 | /* Diagnose a non-constant initializer for constexpr variable or |
848 | non-inline in-class-initialized static data member. */ | |
7e1f8be4 | 849 | if (!require_constant_expression (value)) |
03cbe7c9 | 850 | value = error_mark_node; |
c8e19553 | 851 | else if (processing_template_decl) |
852 | /* In a template we might not have done the necessary | |
853 | transformations to make value actually constant, | |
854 | e.g. extend_ref_init_temps. */ | |
855 | value = maybe_constant_init (value, decl, true); | |
03cbe7c9 | 856 | else |
0c703e14 | 857 | value = cxx_constant_init (value, decl); |
03cbe7c9 | 858 | } |
0c703e14 | 859 | else |
18d371d3 | 860 | value = maybe_constant_init (value, decl, true); |
bbcc9042 | 861 | if (TREE_CODE (value) == CONSTRUCTOR && cp_has_mutable_p (type)) |
862 | /* Poison this CONSTRUCTOR so it can't be copied to another | |
863 | constexpr variable. */ | |
864 | CONSTRUCTOR_MUTABLE_POISON (value) = true; | |
ce984e5e | 865 | const_init = (reduced_constant_expression_p (value) |
866 | || error_operand_p (value)); | |
867 | DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = const_init; | |
39682fd5 | 868 | /* FIXME setting TREE_CONSTANT on refs breaks the back end. */ |
90ad495b | 869 | if (!TYPE_REF_P (type)) |
39682fd5 | 870 | TREE_CONSTANT (decl) = const_init && decl_maybe_constant_var_p (decl); |
006b503a | 871 | if (!const_init) |
872 | value = oldval; | |
ce984e5e | 873 | } |
d560f985 | 874 | value = cp_fully_fold_init (value); |
ce984e5e | 875 | |
ffc5ad9b | 876 | /* Handle aggregate NSDMI in non-constant initializers, too. */ |
877 | value = replace_placeholders (value, decl); | |
cf72f34d | 878 | |
f553d9f8 | 879 | /* DECL may change value; purge caches. */ |
880 | clear_cv_and_fold_caches (); | |
881 | ||
07969d83 | 882 | /* If the initializer is not a constant, fill in DECL_INITIAL with |
883 | the bits that are constant, and then return an expression that | |
884 | will perform the dynamic initialization. */ | |
885 | if (value != error_mark_node | |
886 | && (TREE_SIDE_EFFECTS (value) | |
0b8d327e | 887 | || vla_type_p (type) |
88af6243 | 888 | || ! reduced_constant_expression_p (value))) |
39786c80 | 889 | return split_nonconstant_init (decl, value); |
07969d83 | 890 | /* If the value is a constant, just put it in DECL_INITIAL. If DECL |
891 | is an automatic variable, the middle end will turn this into a | |
892 | dynamic initialization later. */ | |
471086d6 | 893 | DECL_INITIAL (decl) = value; |
894 | return NULL_TREE; | |
895 | } | |
e63bd8ae | 896 | |
471086d6 | 897 | \f |
ee6d34bb | 898 | /* Give diagnostic about narrowing conversions within { }, or as part of |
899 | a converted constant expression. If CONST_ONLY, only check | |
900 | constants. */ | |
f82f1250 | 901 | |
718affe2 | 902 | bool |
ee6d34bb | 903 | check_narrowing (tree type, tree init, tsubst_flags_t complain, bool const_only) |
f82f1250 | 904 | { |
ca01d9d9 | 905 | tree ftype = unlowered_expr_type (init); |
f82f1250 | 906 | bool ok = true; |
907 | REAL_VALUE_TYPE d; | |
908 | ||
718affe2 | 909 | if (((!warn_narrowing || !(complain & tf_warning)) |
910 | && cxx_dialect == cxx98) | |
ee6d34bb | 911 | || !ARITHMETIC_TYPE_P (type) |
912 | /* Don't emit bogus warnings with e.g. value-dependent trees. */ | |
913 | || instantiation_dependent_expression_p (init)) | |
718affe2 | 914 | return ok; |
2732b026 | 915 | |
581edfc4 | 916 | if (BRACE_ENCLOSED_INITIALIZER_P (init) |
917 | && TREE_CODE (type) == COMPLEX_TYPE) | |
918 | { | |
919 | tree elttype = TREE_TYPE (type); | |
a2704374 | 920 | if (CONSTRUCTOR_NELTS (init) > 0) |
718affe2 | 921 | ok &= check_narrowing (elttype, CONSTRUCTOR_ELT (init, 0)->value, |
922 | complain); | |
581edfc4 | 923 | if (CONSTRUCTOR_NELTS (init) > 1) |
718affe2 | 924 | ok &= check_narrowing (elttype, CONSTRUCTOR_ELT (init, 1)->value, |
925 | complain); | |
926 | return ok; | |
581edfc4 | 927 | } |
928 | ||
ee6d34bb | 929 | init = maybe_constant_value (init); |
930 | ||
931 | /* If we were asked to only check constants, return early. */ | |
932 | if (const_only && !TREE_CONSTANT (init)) | |
933 | return ok; | |
f82f1250 | 934 | |
df6bc621 | 935 | if (CP_INTEGRAL_TYPE_P (type) |
f82f1250 | 936 | && TREE_CODE (ftype) == REAL_TYPE) |
937 | ok = false; | |
938 | else if (INTEGRAL_OR_ENUMERATION_TYPE_P (ftype) | |
939 | && CP_INTEGRAL_TYPE_P (type)) | |
940 | { | |
89269893 | 941 | if (TREE_CODE (ftype) == ENUMERAL_TYPE) |
942 | /* Check for narrowing based on the values of the enumeration. */ | |
943 | ftype = ENUM_UNDERLYING_TYPE (ftype); | |
b0337f5c | 944 | if ((tree_int_cst_lt (TYPE_MAX_VALUE (type), |
945 | TYPE_MAX_VALUE (ftype)) | |
946 | || tree_int_cst_lt (TYPE_MIN_VALUE (ftype), | |
947 | TYPE_MIN_VALUE (type))) | |
f82f1250 | 948 | && (TREE_CODE (init) != INTEGER_CST |
949 | || !int_fits_type_p (init, type))) | |
950 | ok = false; | |
951 | } | |
952 | else if (TREE_CODE (ftype) == REAL_TYPE | |
953 | && TREE_CODE (type) == REAL_TYPE) | |
954 | { | |
955 | if (TYPE_PRECISION (type) < TYPE_PRECISION (ftype)) | |
956 | { | |
f82f1250 | 957 | if (TREE_CODE (init) == REAL_CST) |
958 | { | |
eed2e6c3 | 959 | /* Issue 703: Loss of precision is OK as long as the value is |
960 | within the representable range of the new type. */ | |
961 | REAL_VALUE_TYPE r; | |
f82f1250 | 962 | d = TREE_REAL_CST (init); |
eed2e6c3 | 963 | real_convert (&r, TYPE_MODE (type), &d); |
964 | if (real_isinf (&r)) | |
965 | ok = false; | |
f82f1250 | 966 | } |
eed2e6c3 | 967 | else |
968 | ok = false; | |
f82f1250 | 969 | } |
970 | } | |
971 | else if (INTEGRAL_OR_ENUMERATION_TYPE_P (ftype) | |
972 | && TREE_CODE (type) == REAL_TYPE) | |
973 | { | |
974 | ok = false; | |
975 | if (TREE_CODE (init) == INTEGER_CST) | |
976 | { | |
977 | d = real_value_from_int_cst (0, init); | |
978 | if (exact_real_truncate (TYPE_MODE (type), &d)) | |
979 | ok = true; | |
980 | } | |
981 | } | |
982 | ||
d2c63826 | 983 | bool almost_ok = ok; |
984 | if (!ok && !CONSTANT_CLASS_P (init) && (complain & tf_warning_or_error)) | |
985 | { | |
986 | tree folded = cp_fully_fold (init); | |
987 | if (TREE_CONSTANT (folded) && check_narrowing (type, folded, tf_none)) | |
988 | almost_ok = true; | |
989 | } | |
990 | ||
f82f1250 | 991 | if (!ok) |
7e783eb3 | 992 | { |
d3a3cfb8 | 993 | location_t loc = cp_expr_loc_or_loc (init, input_location); |
718affe2 | 994 | if (cxx_dialect == cxx98) |
d2c63826 | 995 | { |
996 | if (complain & tf_warning) | |
997 | warning_at (loc, OPT_Wnarrowing, "narrowing conversion of %qE " | |
ee6d34bb | 998 | "from %qH to %qI is ill-formed in C++11", |
d2c63826 | 999 | init, ftype, type); |
1000 | ok = true; | |
1001 | } | |
1002 | else if (!CONSTANT_CLASS_P (init)) | |
718affe2 | 1003 | { |
1004 | if (complain & tf_warning_or_error) | |
1005 | { | |
bc35ef65 | 1006 | auto_diagnostic_group d; |
22d09025 | 1007 | if ((!almost_ok || pedantic) |
1008 | && pedwarn (loc, OPT_Wnarrowing, | |
ee6d34bb | 1009 | "narrowing conversion of %qE from %qH to %qI", |
22d09025 | 1010 | init, ftype, type) |
1011 | && almost_ok) | |
d2c63826 | 1012 | inform (loc, " the expression has a constant value but is not " |
1013 | "a C++ constant-expression"); | |
718affe2 | 1014 | ok = true; |
1015 | } | |
1016 | } | |
1017 | else if (complain & tf_error) | |
e36ce2a1 | 1018 | { |
eba4cf96 | 1019 | int savederrorcount = errorcount; |
e36ce2a1 | 1020 | global_dc->pedantic_errors = 1; |
d2c63826 | 1021 | pedwarn (loc, OPT_Wnarrowing, |
a9c7b78d | 1022 | "narrowing conversion of %qE from %qH to %qI", |
ee6d34bb | 1023 | init, ftype, type); |
eba4cf96 | 1024 | if (errorcount == savederrorcount) |
d3a3b710 | 1025 | ok = true; |
e36ce2a1 | 1026 | global_dc->pedantic_errors = flag_pedantic_errors; |
1027 | } | |
7e783eb3 | 1028 | } |
718affe2 | 1029 | |
d2c63826 | 1030 | return ok; |
f82f1250 | 1031 | } |
1032 | ||
82362779 | 1033 | /* True iff TYPE is a C++2a "ordinary" character type. */ |
1034 | ||
1035 | bool | |
1036 | ordinary_char_type_p (tree type) | |
1037 | { | |
1038 | type = TYPE_MAIN_VARIANT (type); | |
1039 | return (type == char_type_node | |
1040 | || type == signed_char_type_node | |
1041 | || type == unsigned_char_type_node); | |
1042 | } | |
1043 | ||
c75b4594 | 1044 | /* Process the initializer INIT for a variable of type TYPE, emitting |
1045 | diagnostics for invalid initializers and converting the initializer as | |
1046 | appropriate. | |
471086d6 | 1047 | |
c75b4594 | 1048 | For aggregate types, it assumes that reshape_init has already run, thus the |
f82f1250 | 1049 | initializer will have the right shape (brace elision has been undone). |
471086d6 | 1050 | |
7aac6ee6 | 1051 | NESTED is non-zero iff we are being called for an element of a CONSTRUCTOR, |
1052 | 2 iff the element of a CONSTRUCTOR is inside another CONSTRUCTOR. */ | |
f82f1250 | 1053 | |
1054 | static tree | |
7aac6ee6 | 1055 | digest_init_r (tree type, tree init, int nested, int flags, |
b52bd4ae | 1056 | tsubst_flags_t complain) |
471086d6 | 1057 | { |
1058 | enum tree_code code = TREE_CODE (type); | |
471086d6 | 1059 | |
247f7e36 | 1060 | if (error_operand_p (init)) |
471086d6 | 1061 | return error_mark_node; |
1062 | ||
c75b4594 | 1063 | gcc_assert (init); |
581af848 | 1064 | |
1065 | /* We must strip the outermost array type when completing the type, | |
1066 | because the its bounds might be incomplete at the moment. */ | |
7aac6ee6 | 1067 | if (!complete_type_or_maybe_complain (code == ARRAY_TYPE |
6500d307 | 1068 | ? TREE_TYPE (type) : type, NULL_TREE, |
1069 | complain)) | |
581af848 | 1070 | return error_mark_node; |
9031d10b | 1071 | |
9ab4658b | 1072 | location_t loc = cp_expr_loc_or_loc (init, input_location); |
1073 | ||
1074 | tree stripped_init = init; | |
1075 | ||
c75b4594 | 1076 | /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue |
1077 | (g++.old-deja/g++.law/casts2.C). */ | |
471086d6 | 1078 | if (TREE_CODE (init) == NON_LVALUE_EXPR) |
9ab4658b | 1079 | stripped_init = TREE_OPERAND (init, 0); |
035778de | 1080 | |
9ab4658b | 1081 | stripped_init = tree_strip_any_location_wrapper (stripped_init); |
d582d140 | 1082 | |
c75b4594 | 1083 | /* Initialization of an array of chars from a string constant. The initializer |
1084 | can be optionally enclosed in braces, but reshape_init has already removed | |
1085 | them if they were present. */ | |
471086d6 | 1086 | if (code == ARRAY_TYPE) |
1087 | { | |
6b94e133 | 1088 | if (nested && !TYPE_DOMAIN (type)) |
7aac6ee6 | 1089 | /* C++ flexible array members have a null domain. */ |
c38103e8 | 1090 | { |
1091 | if (flags & LOOKUP_ALLOW_FLEXARRAY_INIT) | |
1092 | pedwarn (loc, OPT_Wpedantic, | |
1093 | "initialization of a flexible array member"); | |
1094 | else | |
1095 | { | |
1096 | if (complain & tf_error) | |
1097 | error_at (loc, "non-static initialization of" | |
1098 | " a flexible array member"); | |
1099 | return error_mark_node; | |
1100 | } | |
1101 | } | |
6b94e133 | 1102 | |
c75b4594 | 1103 | tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type)); |
e67164f5 | 1104 | if (char_type_p (typ1) |
d582d140 | 1105 | && TREE_CODE (stripped_init) == STRING_CST) |
471086d6 | 1106 | { |
c75b4594 | 1107 | tree char_type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (init))); |
82362779 | 1108 | bool incompat_string_cst = false; |
471086d6 | 1109 | |
82362779 | 1110 | if (typ1 != char_type) |
471086d6 | 1111 | { |
82362779 | 1112 | /* The array element type does not match the initializing string |
1113 | literal element type; this is only allowed when both types are | |
1114 | ordinary character type. There are no string literals of | |
1115 | signed or unsigned char type in the language, but we can get | |
1116 | them internally from converting braced-init-lists to | |
1117 | STRING_CST. */ | |
1118 | if (ordinary_char_type_p (typ1) | |
1119 | && ordinary_char_type_p (char_type)) | |
1120 | /* OK */; | |
1121 | else | |
1122 | incompat_string_cst = true; | |
471086d6 | 1123 | } |
82362779 | 1124 | |
1125 | if (incompat_string_cst) | |
471086d6 | 1126 | { |
82362779 | 1127 | if (complain & tf_error) |
1128 | error_at (loc, "cannot initialize array of %qT from " | |
1129 | "a string literal with type array of %qT", | |
1130 | typ1, char_type); | |
1131 | return error_mark_node; | |
471086d6 | 1132 | } |
1133 | ||
7aac6ee6 | 1134 | if (nested == 2 && !TYPE_DOMAIN (type)) |
1135 | { | |
1136 | if (complain & tf_error) | |
1137 | error_at (loc, "initialization of flexible array member " | |
1138 | "in a nested context"); | |
1139 | return error_mark_node; | |
1140 | } | |
1141 | ||
c571b0c6 | 1142 | if (type != TREE_TYPE (init) |
1143 | && !variably_modified_type_p (type, NULL_TREE)) | |
bd46a023 | 1144 | { |
1145 | init = copy_node (init); | |
1146 | TREE_TYPE (init) = type; | |
d582d140 | 1147 | /* If we have a location wrapper, then also copy the wrapped |
1148 | node, and update the copy's type. */ | |
1149 | if (location_wrapper_p (init)) | |
1150 | { | |
1151 | stripped_init = copy_node (stripped_init); | |
1152 | TREE_OPERAND (init, 0) = stripped_init; | |
1153 | TREE_TYPE (stripped_init) = type; | |
1154 | } | |
bd46a023 | 1155 | } |
6b94e133 | 1156 | if (TYPE_DOMAIN (type) && TREE_CONSTANT (TYPE_SIZE (type))) |
471086d6 | 1157 | { |
73d282c6 | 1158 | /* Not a flexible array member. */ |
f9ae6f95 | 1159 | int size = TREE_INT_CST_LOW (TYPE_SIZE (type)); |
471086d6 | 1160 | size = (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT; |
1161 | /* In C it is ok to subtract 1 from the length of the string | |
1162 | because it's ok to ignore the terminating null char that is | |
1163 | counted in the length of the constant, but in C++ this would | |
1164 | be invalid. */ | |
d582d140 | 1165 | if (size < TREE_STRING_LENGTH (stripped_init)) |
35f9aa56 | 1166 | { |
85b9be9b | 1167 | permerror (loc, "initializer-string for %qT is too long", |
1168 | type); | |
35f9aa56 | 1169 | |
d582d140 | 1170 | init = build_string (size, |
1171 | TREE_STRING_POINTER (stripped_init)); | |
35f9aa56 | 1172 | TREE_TYPE (init) = type; |
1173 | } | |
471086d6 | 1174 | } |
c75b4594 | 1175 | return init; |
471086d6 | 1176 | } |
1177 | } | |
1178 | ||
2d42b7d7 | 1179 | /* Handle scalar types (including conversions) and references. */ |
d582d140 | 1180 | if ((code != COMPLEX_TYPE || BRACE_ENCLOSED_INITIALIZER_P (stripped_init)) |
04791a75 | 1181 | && (SCALAR_TYPE_P (type) || code == REFERENCE_TYPE)) |
1b49e7ef | 1182 | { |
7e783eb3 | 1183 | if (nested) |
c4991e3e | 1184 | flags |= LOOKUP_NO_NARROWING; |
cf7aa2e5 | 1185 | init = convert_for_initialization (0, type, init, flags, |
183407ee | 1186 | ICR_INIT, NULL_TREE, 0, |
b52bd4ae | 1187 | complain); |
1b49e7ef | 1188 | |
1189 | return init; | |
1190 | } | |
c75b4594 | 1191 | |
1192 | /* Come here only for aggregates: records, arrays, unions, complex numbers | |
1193 | and vectors. */ | |
7aac6ee6 | 1194 | gcc_assert (code == ARRAY_TYPE |
76249021 | 1195 | || VECTOR_TYPE_P (type) |
7aac6ee6 | 1196 | || code == RECORD_TYPE |
1197 | || code == UNION_TYPE | |
1198 | || code == COMPLEX_TYPE); | |
c75b4594 | 1199 | |
3dd2833b | 1200 | /* "If T is a class type and the initializer list has a single |
1201 | element of type cv U, where U is T or a class derived from T, | |
1202 | the object is initialized from that element." */ | |
67ecd3ae | 1203 | if (cxx_dialect >= cxx11 |
d582d140 | 1204 | && BRACE_ENCLOSED_INITIALIZER_P (stripped_init) |
1205 | && CONSTRUCTOR_NELTS (stripped_init) == 1 | |
671608f2 | 1206 | && ((CLASS_TYPE_P (type) && !CLASSTYPE_NON_AGGREGATE (type)) |
1207 | || VECTOR_TYPE_P (type))) | |
3dd2833b | 1208 | { |
d582d140 | 1209 | tree elt = CONSTRUCTOR_ELT (stripped_init, 0)->value; |
3dd2833b | 1210 | if (reference_related_p (type, TREE_TYPE (elt))) |
751baf9c | 1211 | { |
1212 | /* In C++17, aggregates can have bases, thus participate in | |
1213 | aggregate initialization. In the following case: | |
1214 | ||
1215 | struct B { int c; }; | |
1216 | struct D : B { }; | |
1217 | D d{{D{{42}}}}; | |
1218 | ||
1219 | there's an extra set of braces, so the D temporary initializes | |
1220 | the first element of d, which is the B base subobject. The base | |
1221 | of type B is copy-initialized from the D temporary, causing | |
1222 | object slicing. */ | |
1223 | tree field = next_initializable_field (TYPE_FIELDS (type)); | |
1224 | if (field && DECL_FIELD_IS_BASE (field)) | |
1225 | { | |
1226 | if (warning_at (loc, 0, "initializing a base class of type %qT " | |
1227 | "results in object slicing", TREE_TYPE (field))) | |
1228 | inform (loc, "remove %<{ }%> around initializer"); | |
1229 | } | |
67ecd3ae | 1230 | else if (flag_checking) |
751baf9c | 1231 | /* We should have fixed this in reshape_init. */ |
1232 | gcc_unreachable (); | |
1233 | } | |
3dd2833b | 1234 | } |
1235 | ||
d582d140 | 1236 | if (BRACE_ENCLOSED_INITIALIZER_P (stripped_init) |
1ee7f2fb | 1237 | && !TYPE_NON_AGGREGATE_CLASS (type)) |
c38103e8 | 1238 | return process_init_constructor (type, stripped_init, nested, flags, |
1239 | complain); | |
c75b4594 | 1240 | else |
471086d6 | 1241 | { |
d582d140 | 1242 | if (COMPOUND_LITERAL_P (stripped_init) && code == ARRAY_TYPE) |
471086d6 | 1243 | { |
b52bd4ae | 1244 | if (complain & tf_error) |
035778de | 1245 | error_at (loc, "cannot initialize aggregate of type %qT with " |
1246 | "a compound literal", type); | |
471086d6 | 1247 | |
c75b4594 | 1248 | return error_mark_node; |
1249 | } | |
f8d96752 | 1250 | |
7aac6ee6 | 1251 | if (code == ARRAY_TYPE |
d582d140 | 1252 | && !BRACE_ENCLOSED_INITIALIZER_P (stripped_init)) |
f8d96752 | 1253 | { |
29ada811 | 1254 | /* Allow the result of build_array_copy and of |
1255 | build_value_init_noctor. */ | |
d582d140 | 1256 | if ((TREE_CODE (stripped_init) == VEC_INIT_EXPR |
1257 | || TREE_CODE (stripped_init) == CONSTRUCTOR) | |
a8b75081 | 1258 | && (same_type_ignoring_top_level_qualifiers_p |
1259 | (type, TREE_TYPE (init)))) | |
1260 | return init; | |
1261 | ||
b52bd4ae | 1262 | if (complain & tf_error) |
035778de | 1263 | error_at (loc, "array must be initialized with a brace-enclosed" |
1264 | " initializer"); | |
f8d96752 | 1265 | return error_mark_node; |
1266 | } | |
1267 | ||
c75b4594 | 1268 | return convert_for_initialization (NULL_TREE, type, init, |
cf7aa2e5 | 1269 | flags, |
183407ee | 1270 | ICR_INIT, NULL_TREE, 0, |
b52bd4ae | 1271 | complain); |
471086d6 | 1272 | } |
c75b4594 | 1273 | } |
1274 | ||
f82f1250 | 1275 | tree |
b52bd4ae | 1276 | digest_init (tree type, tree init, tsubst_flags_t complain) |
f82f1250 | 1277 | { |
7aac6ee6 | 1278 | return digest_init_r (type, init, 0, LOOKUP_IMPLICIT, complain); |
cf7aa2e5 | 1279 | } |
1280 | ||
1281 | tree | |
4b373fc1 | 1282 | digest_init_flags (tree type, tree init, int flags, tsubst_flags_t complain) |
cf7aa2e5 | 1283 | { |
7aac6ee6 | 1284 | return digest_init_r (type, init, 0, flags, complain); |
f82f1250 | 1285 | } |
089c9c49 | 1286 | |
1287 | /* Process the initializer INIT for an NSDMI DECL (a FIELD_DECL). */ | |
1288 | tree | |
b9e17a4a | 1289 | digest_nsdmi_init (tree decl, tree init, tsubst_flags_t complain) |
089c9c49 | 1290 | { |
1291 | gcc_assert (TREE_CODE (decl) == FIELD_DECL); | |
1292 | ||
34569785 | 1293 | tree type = TREE_TYPE (decl); |
089c9c49 | 1294 | int flags = LOOKUP_IMPLICIT; |
1295 | if (DIRECT_LIST_INIT_P (init)) | |
1296 | flags = LOOKUP_NORMAL; | |
34569785 | 1297 | if (BRACE_ENCLOSED_INITIALIZER_P (init) |
1298 | && CP_AGGREGATE_TYPE_P (type)) | |
b9e17a4a | 1299 | init = reshape_init (type, init, complain); |
1300 | init = digest_init_flags (type, init, flags, complain); | |
089c9c49 | 1301 | if (TREE_CODE (init) == TARGET_EXPR) |
1302 | /* This represents the whole initialization. */ | |
1303 | TARGET_EXPR_DIRECT_INIT_P (init) = true; | |
1304 | return init; | |
1305 | } | |
c75b4594 | 1306 | \f |
1307 | /* Set of flags used within process_init_constructor to describe the | |
1308 | initializers. */ | |
1309 | #define PICFLAG_ERRONEOUS 1 | |
1310 | #define PICFLAG_NOT_ALL_CONSTANT 2 | |
1311 | #define PICFLAG_NOT_ALL_SIMPLE 4 | |
48f76477 | 1312 | #define PICFLAG_SIDE_EFFECTS 8 |
c75b4594 | 1313 | |
1314 | /* Given an initializer INIT, return the flag (PICFLAG_*) which better | |
1315 | describe it. */ | |
1316 | ||
1317 | static int | |
1318 | picflag_from_initializer (tree init) | |
1319 | { | |
1320 | if (init == error_mark_node) | |
1321 | return PICFLAG_ERRONEOUS; | |
1322 | else if (!TREE_CONSTANT (init)) | |
48f76477 | 1323 | { |
1324 | if (TREE_SIDE_EFFECTS (init)) | |
1325 | return PICFLAG_SIDE_EFFECTS; | |
1326 | else | |
1327 | return PICFLAG_NOT_ALL_CONSTANT; | |
1328 | } | |
c75b4594 | 1329 | else if (!initializer_constant_valid_p (init, TREE_TYPE (init))) |
1330 | return PICFLAG_NOT_ALL_SIMPLE; | |
1331 | return 0; | |
1332 | } | |
471086d6 | 1333 | |
7e173c71 | 1334 | /* Adjust INIT for going into a CONSTRUCTOR. */ |
1335 | ||
1336 | static tree | |
c38103e8 | 1337 | massage_init_elt (tree type, tree init, int nested, int flags, |
1338 | tsubst_flags_t complain) | |
7e173c71 | 1339 | { |
c38103e8 | 1340 | flags &= LOOKUP_ALLOW_FLEXARRAY_INIT; |
1341 | flags |= LOOKUP_IMPLICIT; | |
1342 | init = digest_init_r (type, init, nested ? 2 : 1, flags, complain); | |
7e173c71 | 1343 | /* Strip a simple TARGET_EXPR when we know this is an initializer. */ |
1d56a349 | 1344 | if (SIMPLE_TARGET_EXPR_P (init)) |
7e173c71 | 1345 | init = TARGET_EXPR_INITIAL (init); |
1346 | /* When we defer constant folding within a statement, we may want to | |
1347 | defer this folding as well. */ | |
cf13db0c | 1348 | tree t = fold_non_dependent_init (init, complain); |
0e65aeb6 | 1349 | if (TREE_CONSTANT (t)) |
1350 | init = t; | |
7e173c71 | 1351 | return init; |
1352 | } | |
1353 | ||
c75b4594 | 1354 | /* Subroutine of process_init_constructor, which will process an initializer |
45cc50c5 | 1355 | INIT for an array or vector of type TYPE. Returns the flags (PICFLAG_*) |
1356 | which describe the initializers. */ | |
471086d6 | 1357 | |
c75b4594 | 1358 | static int |
c38103e8 | 1359 | process_init_constructor_array (tree type, tree init, int nested, int flags, |
b52bd4ae | 1360 | tsubst_flags_t complain) |
c75b4594 | 1361 | { |
1362 | unsigned HOST_WIDE_INT i, len = 0; | |
c38103e8 | 1363 | int picflags = 0; |
c75b4594 | 1364 | bool unbounded = false; |
1365 | constructor_elt *ce; | |
f1f41a6c | 1366 | vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (init); |
c75b4594 | 1367 | |
1368 | gcc_assert (TREE_CODE (type) == ARRAY_TYPE | |
76249021 | 1369 | || VECTOR_TYPE_P (type)); |
c75b4594 | 1370 | |
1371 | if (TREE_CODE (type) == ARRAY_TYPE) | |
471086d6 | 1372 | { |
6b94e133 | 1373 | /* C++ flexible array members have a null domain. */ |
c75b4594 | 1374 | tree domain = TYPE_DOMAIN (type); |
6b94e133 | 1375 | if (domain && TREE_CONSTANT (TYPE_MAX_VALUE (domain))) |
1376 | len = wi::ext (wi::to_offset (TYPE_MAX_VALUE (domain)) | |
1377 | - wi::to_offset (TYPE_MIN_VALUE (domain)) + 1, | |
796b6678 | 1378 | TYPE_PRECISION (TREE_TYPE (domain)), |
1379 | TYPE_SIGN (TREE_TYPE (domain))).to_uhwi (); | |
c75b4594 | 1380 | else |
1381 | unbounded = true; /* Take as many as there are. */ | |
7aac6ee6 | 1382 | |
1383 | if (nested == 2 && !domain && !vec_safe_is_empty (v)) | |
1384 | { | |
1385 | if (complain & tf_error) | |
d3a3cfb8 | 1386 | error_at (cp_expr_loc_or_loc (init, input_location), |
7aac6ee6 | 1387 | "initialization of flexible array member " |
1388 | "in a nested context"); | |
1389 | return PICFLAG_ERRONEOUS; | |
1390 | } | |
471086d6 | 1391 | } |
c75b4594 | 1392 | else |
1393 | /* Vectors are like simple fixed-size arrays. */ | |
f08ee65f | 1394 | unbounded = !TYPE_VECTOR_SUBPARTS (type).is_constant (&len); |
471086d6 | 1395 | |
a8fe6bf4 | 1396 | /* There must not be more initializers than needed. */ |
f1f41a6c | 1397 | if (!unbounded && vec_safe_length (v) > len) |
b52bd4ae | 1398 | { |
1399 | if (complain & tf_error) | |
1400 | error ("too many initializers for %qT", type); | |
1401 | else | |
1402 | return PICFLAG_ERRONEOUS; | |
1403 | } | |
c75b4594 | 1404 | |
f1f41a6c | 1405 | FOR_EACH_VEC_SAFE_ELT (v, i, ce) |
471086d6 | 1406 | { |
caf629d3 | 1407 | if (!ce->index) |
c75b4594 | 1408 | ce->index = size_int (i); |
caf629d3 | 1409 | else if (!check_array_designated_initializer (ce, i)) |
1410 | ce->index = error_mark_node; | |
c75b4594 | 1411 | gcc_assert (ce->value); |
7aac6ee6 | 1412 | ce->value |
c38103e8 | 1413 | = massage_init_elt (TREE_TYPE (type), ce->value, nested, flags, |
1414 | complain); | |
48ec80c8 | 1415 | |
ee1f7409 | 1416 | gcc_checking_assert |
1417 | (ce->value == error_mark_node | |
1418 | || (same_type_ignoring_top_level_qualifiers_p | |
1419 | (strip_array_types (TREE_TYPE (type)), | |
1420 | strip_array_types (TREE_TYPE (ce->value))))); | |
2133b374 | 1421 | |
c38103e8 | 1422 | picflags |= picflag_from_initializer (ce->value); |
471086d6 | 1423 | } |
1424 | ||
c75b4594 | 1425 | /* No more initializers. If the array is unbounded, we are done. Otherwise, |
1426 | we must add initializers ourselves. */ | |
1427 | if (!unbounded) | |
1428 | for (; i < len; ++i) | |
1429 | { | |
1430 | tree next; | |
1431 | ||
883e1020 | 1432 | if (type_build_ctor_call (TREE_TYPE (type))) |
c75b4594 | 1433 | { |
1434 | /* If this type needs constructors run for default-initialization, | |
8feda8f8 | 1435 | we can't rely on the back end to do it for us, so make the |
79f1f232 | 1436 | initialization explicit by list-initializing from T{}. */ |
9705da72 | 1437 | next = build_constructor (init_list_type_node, NULL); |
c38103e8 | 1438 | next = massage_init_elt (TREE_TYPE (type), next, nested, flags, |
1439 | complain); | |
8feda8f8 | 1440 | if (initializer_zerop (next)) |
1441 | /* The default zero-initialization is fine for us; don't | |
1442 | add anything to the CONSTRUCTOR. */ | |
1443 | next = NULL_TREE; | |
c75b4594 | 1444 | } |
1445 | else if (!zero_init_p (TREE_TYPE (type))) | |
1446 | next = build_zero_init (TREE_TYPE (type), | |
1447 | /*nelts=*/NULL_TREE, | |
1448 | /*static_storage_p=*/false); | |
1449 | else | |
1450 | /* The default zero-initialization is fine for us; don't | |
1451 | add anything to the CONSTRUCTOR. */ | |
8feda8f8 | 1452 | next = NULL_TREE; |
c75b4594 | 1453 | |
8feda8f8 | 1454 | if (next) |
1455 | { | |
c38103e8 | 1456 | picflags |= picflag_from_initializer (next); |
cf755750 | 1457 | if (len > i+1 |
1458 | && (initializer_constant_valid_p (next, TREE_TYPE (next)) | |
1459 | == null_pointer_node)) | |
1460 | { | |
1461 | tree range = build2 (RANGE_EXPR, size_type_node, | |
1462 | build_int_cst (size_type_node, i), | |
1463 | build_int_cst (size_type_node, len - 1)); | |
1464 | CONSTRUCTOR_APPEND_ELT (v, range, next); | |
1465 | break; | |
1466 | } | |
1467 | else | |
1468 | CONSTRUCTOR_APPEND_ELT (v, size_int (i), next); | |
8feda8f8 | 1469 | } |
cf755750 | 1470 | else |
1471 | /* Don't bother checking all the other elements. */ | |
1472 | break; | |
c75b4594 | 1473 | } |
1474 | ||
1475 | CONSTRUCTOR_ELTS (init) = v; | |
c38103e8 | 1476 | return picflags; |
471086d6 | 1477 | } |
471086d6 | 1478 | |
c75b4594 | 1479 | /* Subroutine of process_init_constructor, which will process an initializer |
1480 | INIT for a class of type TYPE. Returns the flags (PICFLAG_*) which describe | |
1481 | the initializers. */ | |
471086d6 | 1482 | |
c75b4594 | 1483 | static int |
c38103e8 | 1484 | process_init_constructor_record (tree type, tree init, int nested, int flags, |
b52bd4ae | 1485 | tsubst_flags_t complain) |
471086d6 | 1486 | { |
f1f41a6c | 1487 | vec<constructor_elt, va_gc> *v = NULL; |
c75b4594 | 1488 | tree field; |
cf72f34d | 1489 | int skipped = 0; |
c75b4594 | 1490 | |
1491 | gcc_assert (TREE_CODE (type) == RECORD_TYPE); | |
1492 | gcc_assert (!CLASSTYPE_VBASECLASSES (type)); | |
1493 | gcc_assert (!TYPE_BINFO (type) | |
40e2decb | 1494 | || cxx_dialect >= cxx17 |
c75b4594 | 1495 | || !BINFO_N_BASE_BINFOS (TYPE_BINFO (type))); |
1496 | gcc_assert (!TYPE_POLYMORPHIC_P (type)); | |
1497 | ||
cf72f34d | 1498 | restart: |
c38103e8 | 1499 | int picflags = 0; |
cf72f34d | 1500 | unsigned HOST_WIDE_INT idx = 0; |
8e79b5ff | 1501 | int designator_skip = -1; |
c75b4594 | 1502 | /* Generally, we will always have an index for each initializer (which is |
1503 | a FIELD_DECL, put by reshape_init), but compound literals don't go trough | |
1504 | reshape_init. So we need to handle both cases. */ | |
1767a056 | 1505 | for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field)) |
471086d6 | 1506 | { |
c75b4594 | 1507 | tree next; |
ca01d9d9 | 1508 | tree type; |
471086d6 | 1509 | |
a83affb5 | 1510 | if (TREE_CODE (field) != FIELD_DECL |
1511 | || (DECL_ARTIFICIAL (field) | |
40e2decb | 1512 | && !(cxx_dialect >= cxx17 && DECL_FIELD_IS_BASE (field)))) |
c75b4594 | 1513 | continue; |
1514 | ||
3cde3c29 | 1515 | if (DECL_UNNAMED_BIT_FIELD (field)) |
1516 | continue; | |
1517 | ||
ca01d9d9 | 1518 | /* If this is a bitfield, first convert to the declared type. */ |
1519 | type = TREE_TYPE (field); | |
1520 | if (DECL_BIT_FIELD_TYPE (field)) | |
1521 | type = DECL_BIT_FIELD_TYPE (field); | |
53b567c1 | 1522 | if (type == error_mark_node) |
1523 | return PICFLAG_ERRONEOUS; | |
ca01d9d9 | 1524 | |
8e79b5ff | 1525 | next = NULL_TREE; |
8698843f | 1526 | if (idx < CONSTRUCTOR_NELTS (init)) |
471086d6 | 1527 | { |
f1f41a6c | 1528 | constructor_elt *ce = &(*CONSTRUCTOR_ELTS (init))[idx]; |
c75b4594 | 1529 | if (ce->index) |
471086d6 | 1530 | { |
c75b4594 | 1531 | /* We can have either a FIELD_DECL or an IDENTIFIER_NODE. The |
1532 | latter case can happen in templates where lookup has to be | |
1533 | deferred. */ | |
1534 | gcc_assert (TREE_CODE (ce->index) == FIELD_DECL | |
694683bb | 1535 | || identifier_p (ce->index)); |
8e79b5ff | 1536 | if (ce->index == field || ce->index == DECL_NAME (field)) |
1537 | next = ce->value; | |
1538 | else if (ANON_AGGR_TYPE_P (type) | |
1539 | && search_anon_aggr (type, | |
1540 | TREE_CODE (ce->index) == FIELD_DECL | |
1541 | ? DECL_NAME (ce->index) | |
1542 | : ce->index)) | |
1543 | /* If the element is an anonymous union object and the | |
1544 | initializer list is a designated-initializer-list, the | |
1545 | anonymous union object is initialized by the | |
1546 | designated-initializer-list { D }, where D is the | |
1547 | designated-initializer-clause naming a member of the | |
1548 | anonymous union object. */ | |
91fdd720 | 1549 | next = build_constructor_single (init_list_type_node, |
1550 | ce->index, ce->value); | |
8e79b5ff | 1551 | else |
b81452c9 | 1552 | { |
8e79b5ff | 1553 | ce = NULL; |
1554 | if (designator_skip == -1) | |
1555 | designator_skip = 1; | |
b81452c9 | 1556 | } |
471086d6 | 1557 | } |
8e79b5ff | 1558 | else |
1559 | { | |
1560 | designator_skip = 0; | |
1561 | next = ce->value; | |
1562 | } | |
72ba5dfe | 1563 | |
8e79b5ff | 1564 | if (ce) |
1565 | { | |
1566 | gcc_assert (ce->value); | |
c38103e8 | 1567 | next = massage_init_elt (type, next, nested, flags, complain); |
8e79b5ff | 1568 | ++idx; |
1569 | } | |
c75b4594 | 1570 | } |
8e79b5ff | 1571 | if (next) |
1572 | /* Already handled above. */; | |
cf72f34d | 1573 | else if (DECL_INITIAL (field)) |
1574 | { | |
1575 | if (skipped > 0) | |
1576 | { | |
1577 | /* We're using an NSDMI past a field with implicit | |
1578 | zero-init. Go back and make it explicit. */ | |
1579 | skipped = -1; | |
1580 | vec_safe_truncate (v, 0); | |
1581 | goto restart; | |
1582 | } | |
1583 | /* C++14 aggregate NSDMI. */ | |
b9e17a4a | 1584 | next = get_nsdmi (field, /*ctor*/false, complain); |
7604a798 | 1585 | if (!CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init) |
1586 | && find_placeholders (next)) | |
1587 | CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init) = 1; | |
cf72f34d | 1588 | } |
883e1020 | 1589 | else if (type_build_ctor_call (TREE_TYPE (field))) |
c75b4594 | 1590 | { |
1591 | /* If this type needs constructors run for | |
a17c2a3a | 1592 | default-initialization, we can't rely on the back end to do it |
c75b4594 | 1593 | for us, so build up TARGET_EXPRs. If the type in question is |
1594 | a class, just build one up; if it's an array, recurse. */ | |
dbfcf378 | 1595 | next = build_constructor (init_list_type_node, NULL); |
c38103e8 | 1596 | next = massage_init_elt (TREE_TYPE (field), next, nested, flags, |
1597 | complain); | |
c75b4594 | 1598 | |
1599 | /* Warn when some struct elements are implicitly initialized. */ | |
639d66a3 | 1600 | if ((complain & tf_warning) |
1601 | && !EMPTY_CONSTRUCTOR_P (init)) | |
af71319a | 1602 | warning (OPT_Wmissing_field_initializers, |
1603 | "missing initializer for member %qD", field); | |
c75b4594 | 1604 | } |
1605 | else | |
1606 | { | |
73d282c6 | 1607 | const_tree fldtype = TREE_TYPE (field); |
90ad495b | 1608 | if (TYPE_REF_P (fldtype)) |
b52bd4ae | 1609 | { |
1610 | if (complain & tf_error) | |
af71319a | 1611 | error ("member %qD is uninitialized reference", field); |
b52bd4ae | 1612 | else |
1613 | return PICFLAG_ERRONEOUS; | |
1614 | } | |
73d282c6 | 1615 | else if (CLASSTYPE_REF_FIELDS_NEED_INIT (fldtype)) |
b52bd4ae | 1616 | { |
1617 | if (complain & tf_error) | |
af71319a | 1618 | error ("member %qD with uninitialized reference fields", field); |
b52bd4ae | 1619 | else |
1620 | return PICFLAG_ERRONEOUS; | |
1621 | } | |
fd8a4665 | 1622 | /* Do nothing for flexible array members since they need not have any |
1623 | elements. Don't worry about 'skipped' because a flexarray has to | |
1624 | be the last field. */ | |
1625 | else if (TREE_CODE (fldtype) == ARRAY_TYPE && !TYPE_DOMAIN (fldtype)) | |
1626 | continue; | |
c75b4594 | 1627 | |
1628 | /* Warn when some struct elements are implicitly initialized | |
fd8a4665 | 1629 | to zero. */ |
1630 | if ((complain & tf_warning) | |
639d66a3 | 1631 | && !EMPTY_CONSTRUCTOR_P (init)) |
af71319a | 1632 | warning (OPT_Wmissing_field_initializers, |
1633 | "missing initializer for member %qD", field); | |
c75b4594 | 1634 | |
73d282c6 | 1635 | if (!zero_init_p (fldtype) |
cf72f34d | 1636 | || skipped < 0) |
c75b4594 | 1637 | next = build_zero_init (TREE_TYPE (field), /*nelts=*/NULL_TREE, |
1638 | /*static_storage_p=*/false); | |
72ba5dfe | 1639 | else |
cf72f34d | 1640 | { |
1641 | /* The default zero-initialization is fine for us; don't | |
1642 | add anything to the CONSTRUCTOR. */ | |
1643 | skipped = 1; | |
1644 | continue; | |
1645 | } | |
471086d6 | 1646 | } |
c75b4594 | 1647 | |
dcd8f919 | 1648 | if (DECL_SIZE (field) && integer_zerop (DECL_SIZE (field)) |
1649 | && !TREE_SIDE_EFFECTS (next)) | |
1650 | /* Don't add trivial initialization of an empty base/field to the | |
1651 | constructor, as they might not be ordered the way the back-end | |
1652 | expects. */ | |
1653 | continue; | |
1654 | ||
ca01d9d9 | 1655 | /* If this is a bitfield, now convert to the lowered type. */ |
1656 | if (type != TREE_TYPE (field)) | |
c4698a21 | 1657 | next = cp_convert_and_check (TREE_TYPE (field), next, complain); |
c38103e8 | 1658 | picflags |= picflag_from_initializer (next); |
c75b4594 | 1659 | CONSTRUCTOR_APPEND_ELT (v, field, next); |
471086d6 | 1660 | } |
471086d6 | 1661 | |
8698843f | 1662 | if (idx < CONSTRUCTOR_NELTS (init)) |
b52bd4ae | 1663 | { |
1664 | if (complain & tf_error) | |
8e79b5ff | 1665 | { |
1666 | constructor_elt *ce = &(*CONSTRUCTOR_ELTS (init))[idx]; | |
1667 | /* For better diagnostics, try to find out if it is really | |
1668 | the case of too many initializers or if designators are | |
1669 | in incorrect order. */ | |
1670 | if (designator_skip == 1 && ce->index) | |
1671 | { | |
1672 | gcc_assert (TREE_CODE (ce->index) == FIELD_DECL | |
1673 | || identifier_p (ce->index)); | |
1674 | for (field = TYPE_FIELDS (type); | |
1675 | field; field = DECL_CHAIN (field)) | |
1676 | { | |
8e79b5ff | 1677 | if (TREE_CODE (field) != FIELD_DECL |
1678 | || (DECL_ARTIFICIAL (field) | |
1679 | && !(cxx_dialect >= cxx17 | |
1680 | && DECL_FIELD_IS_BASE (field)))) | |
1681 | continue; | |
1682 | ||
3cde3c29 | 1683 | if (DECL_UNNAMED_BIT_FIELD (field)) |
1684 | continue; | |
1685 | ||
8e79b5ff | 1686 | if (ce->index == field || ce->index == DECL_NAME (field)) |
1687 | break; | |
1688 | if (ANON_AGGR_TYPE_P (TREE_TYPE (field))) | |
1689 | { | |
1690 | tree t | |
1691 | = search_anon_aggr (TREE_TYPE (field), | |
1692 | TREE_CODE (ce->index) == FIELD_DECL | |
1693 | ? DECL_NAME (ce->index) | |
1694 | : ce->index); | |
1695 | if (t) | |
1696 | { | |
1697 | field = t; | |
1698 | break; | |
1699 | } | |
1700 | } | |
1701 | } | |
1702 | } | |
1703 | if (field) | |
1704 | error ("designator order for field %qD does not match declaration " | |
1705 | "order in %qT", field, type); | |
1706 | else | |
1707 | error ("too many initializers for %qT", type); | |
1708 | } | |
b52bd4ae | 1709 | else |
1710 | return PICFLAG_ERRONEOUS; | |
1711 | } | |
1712 | ||
c75b4594 | 1713 | CONSTRUCTOR_ELTS (init) = v; |
c38103e8 | 1714 | return picflags; |
c75b4594 | 1715 | } |
471086d6 | 1716 | |
c75b4594 | 1717 | /* Subroutine of process_init_constructor, which will process a single |
cd7043f9 | 1718 | initializer INIT for a union of type TYPE. Returns the flags (PICFLAG_*) |
c75b4594 | 1719 | which describe the initializer. */ |
471086d6 | 1720 | |
c75b4594 | 1721 | static int |
c38103e8 | 1722 | process_init_constructor_union (tree type, tree init, int nested, int flags, |
b52bd4ae | 1723 | tsubst_flags_t complain) |
c75b4594 | 1724 | { |
1725 | constructor_elt *ce; | |
1ee7f2fb | 1726 | int len; |
471086d6 | 1727 | |
5a869ab8 | 1728 | /* If the initializer was empty, use the union's NSDMI if it has one. |
1729 | Otherwise use default zero initialization. */ | |
f1f41a6c | 1730 | if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init))) |
5a869ab8 | 1731 | { |
1732 | for (tree field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field)) | |
1733 | { | |
b06ecd3c | 1734 | if (TREE_CODE (field) == FIELD_DECL |
1735 | && DECL_INITIAL (field) != NULL_TREE) | |
5a869ab8 | 1736 | { |
7604a798 | 1737 | tree val = get_nsdmi (field, /*in_ctor=*/false, complain); |
1738 | if (!CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init) | |
1739 | && find_placeholders (val)) | |
1740 | CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init) = 1; | |
1741 | CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (init), field, val); | |
5a869ab8 | 1742 | break; |
1743 | } | |
1744 | } | |
1745 | ||
1746 | if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init))) | |
1747 | return 0; | |
1748 | } | |
c75b4594 | 1749 | |
f1f41a6c | 1750 | len = CONSTRUCTOR_ELTS (init)->length (); |
1ee7f2fb | 1751 | if (len > 1) |
1752 | { | |
b52bd4ae | 1753 | if (!(complain & tf_error)) |
1754 | return PICFLAG_ERRONEOUS; | |
1ee7f2fb | 1755 | error ("too many initializers for %qT", type); |
f1f41a6c | 1756 | CONSTRUCTOR_ELTS (init)->block_remove (1, len-1); |
1ee7f2fb | 1757 | } |
1758 | ||
f1f41a6c | 1759 | ce = &(*CONSTRUCTOR_ELTS (init))[0]; |
c75b4594 | 1760 | |
1761 | /* If this element specifies a field, initialize via that field. */ | |
1762 | if (ce->index) | |
1763 | { | |
1764 | if (TREE_CODE (ce->index) == FIELD_DECL) | |
1765 | ; | |
694683bb | 1766 | else if (identifier_p (ce->index)) |
c75b4594 | 1767 | { |
1768 | /* This can happen within a cast, see g++.dg/opt/cse2.C. */ | |
1769 | tree name = ce->index; | |
1770 | tree field; | |
1771 | for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field)) | |
1772 | if (DECL_NAME (field) == name) | |
1773 | break; | |
1774 | if (!field) | |
471086d6 | 1775 | { |
b52bd4ae | 1776 | if (complain & tf_error) |
1777 | error ("no field %qD found in union being initialized", | |
1778 | field); | |
c75b4594 | 1779 | ce->value = error_mark_node; |
471086d6 | 1780 | } |
c75b4594 | 1781 | ce->index = field; |
1782 | } | |
1783 | else | |
1784 | { | |
1785 | gcc_assert (TREE_CODE (ce->index) == INTEGER_CST | |
1786 | || TREE_CODE (ce->index) == RANGE_EXPR); | |
b52bd4ae | 1787 | if (complain & tf_error) |
1788 | error ("index value instead of field name in union initializer"); | |
c75b4594 | 1789 | ce->value = error_mark_node; |
471086d6 | 1790 | } |
471086d6 | 1791 | } |
c75b4594 | 1792 | else |
471086d6 | 1793 | { |
471086d6 | 1794 | /* Find the first named field. ANSI decided in September 1990 |
1795 | that only named fields count here. */ | |
c75b4594 | 1796 | tree field = TYPE_FIELDS (type); |
23ed74d8 | 1797 | while (field && (!DECL_NAME (field) || TREE_CODE (field) != FIELD_DECL)) |
471086d6 | 1798 | field = TREE_CHAIN (field); |
23a0ce43 | 1799 | if (field == NULL_TREE) |
1800 | { | |
b52bd4ae | 1801 | if (complain & tf_error) |
1802 | error ("too many initializers for %qT", type); | |
23a0ce43 | 1803 | ce->value = error_mark_node; |
1804 | } | |
c75b4594 | 1805 | ce->index = field; |
1806 | } | |
471086d6 | 1807 | |
c75b4594 | 1808 | if (ce->value && ce->value != error_mark_node) |
7aac6ee6 | 1809 | ce->value = massage_init_elt (TREE_TYPE (ce->index), ce->value, nested, |
c38103e8 | 1810 | flags, complain); |
471086d6 | 1811 | |
c75b4594 | 1812 | return picflag_from_initializer (ce->value); |
1813 | } | |
471086d6 | 1814 | |
c75b4594 | 1815 | /* Process INIT, a constructor for a variable of aggregate type TYPE. The |
1816 | constructor is a brace-enclosed initializer, and will be modified in-place. | |
1817 | ||
1818 | Each element is converted to the right type through digest_init, and | |
1819 | missing initializers are added following the language rules (zero-padding, | |
1820 | etc.). | |
471086d6 | 1821 | |
c75b4594 | 1822 | After the execution, the initializer will have TREE_CONSTANT if all elts are |
1823 | constant, and TREE_STATIC set if, in addition, all elts are simple enough | |
1824 | constants that the assembler and linker can compute them. | |
074ab442 | 1825 | |
c75b4594 | 1826 | The function returns the initializer itself, or error_mark_node in case |
1827 | of error. */ | |
1828 | ||
1829 | static tree | |
c38103e8 | 1830 | process_init_constructor (tree type, tree init, int nested, int flags, |
7aac6ee6 | 1831 | tsubst_flags_t complain) |
c75b4594 | 1832 | { |
c38103e8 | 1833 | int picflags; |
c75b4594 | 1834 | |
1835 | gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init)); | |
1836 | ||
76249021 | 1837 | if (TREE_CODE (type) == ARRAY_TYPE || VECTOR_TYPE_P (type)) |
c38103e8 | 1838 | picflags = process_init_constructor_array (type, init, nested, flags, |
1839 | complain); | |
c75b4594 | 1840 | else if (TREE_CODE (type) == RECORD_TYPE) |
c38103e8 | 1841 | picflags = process_init_constructor_record (type, init, nested, flags, |
1842 | complain); | |
c75b4594 | 1843 | else if (TREE_CODE (type) == UNION_TYPE) |
c38103e8 | 1844 | picflags = process_init_constructor_union (type, init, nested, flags, |
1845 | complain); | |
c75b4594 | 1846 | else |
1847 | gcc_unreachable (); | |
471086d6 | 1848 | |
c38103e8 | 1849 | if (picflags & PICFLAG_ERRONEOUS) |
471086d6 | 1850 | return error_mark_node; |
1851 | ||
c75b4594 | 1852 | TREE_TYPE (init) = type; |
981a251c | 1853 | if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == NULL_TREE) |
c75b4594 | 1854 | cp_complete_array_type (&TREE_TYPE (init), init, /*do_default=*/0); |
c38103e8 | 1855 | if (picflags & PICFLAG_SIDE_EFFECTS) |
48f76477 | 1856 | { |
1857 | TREE_CONSTANT (init) = false; | |
1858 | TREE_SIDE_EFFECTS (init) = true; | |
1859 | } | |
c38103e8 | 1860 | else if (picflags & PICFLAG_NOT_ALL_CONSTANT) |
0138a8ea | 1861 | /* Make sure TREE_CONSTANT isn't set from build_constructor. */ |
1862 | TREE_CONSTANT (init) = false; | |
1863 | else | |
4ee9c684 | 1864 | { |
c75b4594 | 1865 | TREE_CONSTANT (init) = 1; |
c38103e8 | 1866 | if (!(picflags & PICFLAG_NOT_ALL_SIMPLE)) |
c75b4594 | 1867 | TREE_STATIC (init) = 1; |
4ee9c684 | 1868 | } |
c75b4594 | 1869 | return init; |
471086d6 | 1870 | } |
1871 | \f | |
1872 | /* Given a structure or union value DATUM, construct and return | |
1873 | the structure or union component which results from narrowing | |
8ef5085e | 1874 | that value to the base specified in BASETYPE. For example, given the |
471086d6 | 1875 | hierarchy |
1876 | ||
1877 | class L { int ii; }; | |
1878 | class A : L { ... }; | |
1879 | class B : L { ... }; | |
1880 | class C : A, B { ... }; | |
1881 | ||
1882 | and the declaration | |
1883 | ||
1884 | C x; | |
1885 | ||
1886 | then the expression | |
1887 | ||
652e1a2d | 1888 | x.A::ii refers to the ii member of the L part of |
3398e91d | 1889 | the A part of the C object named by X. In this case, |
c161288a | 1890 | DATUM would be x, and BASETYPE would be A. |
1891 | ||
2a88c9e6 | 1892 | I used to think that this was nonconformant, that the standard specified |
1893 | that first we look up ii in A, then convert x to an L& and pull out the | |
1894 | ii part. But in fact, it does say that we convert x to an A&; A here | |
8ef5085e | 1895 | is known as the "naming class". (jason 2000-12-19) |
1896 | ||
1897 | BINFO_P points to a variable initialized either to NULL_TREE or to the | |
1898 | binfo for the specific base subobject we want to convert to. */ | |
471086d6 | 1899 | |
1900 | tree | |
e394ed0f | 1901 | build_scoped_ref (tree datum, tree basetype, tree* binfo_p) |
471086d6 | 1902 | { |
4a2680fc | 1903 | tree binfo; |
471086d6 | 1904 | |
1905 | if (datum == error_mark_node) | |
1906 | return error_mark_node; | |
8ef5085e | 1907 | if (*binfo_p) |
1908 | binfo = *binfo_p; | |
1909 | else | |
ae260dcc | 1910 | binfo = lookup_base (TREE_TYPE (datum), basetype, ba_check, |
1911 | NULL, tf_warning_or_error); | |
471086d6 | 1912 | |
8ef5085e | 1913 | if (!binfo || binfo == error_mark_node) |
1914 | { | |
1915 | *binfo_p = NULL_TREE; | |
1916 | if (!binfo) | |
1917 | error_not_base_type (basetype, TREE_TYPE (datum)); | |
1918 | return error_mark_node; | |
1919 | } | |
471086d6 | 1920 | |
8ef5085e | 1921 | *binfo_p = binfo; |
1e74225a | 1922 | return build_base_path (PLUS_EXPR, datum, binfo, 1, |
1923 | tf_warning_or_error); | |
471086d6 | 1924 | } |
1925 | ||
1926 | /* Build a reference to an object specified by the C++ `->' operator. | |
1927 | Usually this just involves dereferencing the object, but if the | |
1928 | `->' operator is overloaded, then such overloads must be | |
1929 | performed until an object which does not have the `->' operator | |
1930 | overloaded is found. An error is reported when circular pointer | |
1931 | delegation is detected. */ | |
96624a9e | 1932 | |
471086d6 | 1933 | tree |
ef0b0c72 | 1934 | build_x_arrow (location_t loc, tree expr, tsubst_flags_t complain) |
471086d6 | 1935 | { |
13795292 | 1936 | tree orig_expr = expr; |
13795292 | 1937 | tree type = TREE_TYPE (expr); |
034b484a | 1938 | tree last_rval = NULL_TREE; |
f1f41a6c | 1939 | vec<tree, va_gc> *types_memoized = NULL; |
471086d6 | 1940 | |
1941 | if (type == error_mark_node) | |
1942 | return error_mark_node; | |
1943 | ||
3cc0b4b9 | 1944 | if (processing_template_decl) |
13795292 | 1945 | { |
90ad495b | 1946 | if (type && TYPE_PTR_P (type) |
eee80116 | 1947 | && !dependent_scope_p (TREE_TYPE (type))) |
1948 | /* Pointer to current instantiation, don't treat as dependent. */; | |
1949 | else if (type_dependent_expression_p (expr)) | |
255b5d15 | 1950 | return build_min_nt_loc (loc, ARROW_EXPR, expr); |
13795292 | 1951 | expr = build_non_dependent_expr (expr); |
1952 | } | |
e857e9c7 | 1953 | |
95397ff9 | 1954 | if (MAYBE_CLASS_TYPE_P (type)) |
471086d6 | 1955 | { |
9951fe5d | 1956 | struct tinst_level *actual_inst = current_instantiation (); |
1957 | tree fn = NULL; | |
1958 | ||
ef0b0c72 | 1959 | while ((expr = build_new_op (loc, COMPONENT_REF, |
1960 | LOOKUP_NORMAL, expr, NULL_TREE, NULL_TREE, | |
2ad2700d | 1961 | &fn, complain))) |
471086d6 | 1962 | { |
13795292 | 1963 | if (expr == error_mark_node) |
471086d6 | 1964 | return error_mark_node; |
1965 | ||
8f51a05f | 1966 | /* This provides a better instantiation backtrace in case of |
1967 | error. */ | |
9951fe5d | 1968 | if (fn && DECL_USE_TEMPLATE (fn)) |
8f51a05f | 1969 | push_tinst_level_loc (fn, |
1970 | (current_instantiation () != actual_inst) | |
1971 | ? DECL_SOURCE_LOCATION (fn) | |
1972 | : input_location); | |
9951fe5d | 1973 | fn = NULL; |
1974 | ||
1df3c246 | 1975 | if (vec_member (TREE_TYPE (expr), types_memoized)) |
1976 | { | |
2ad2700d | 1977 | if (complain & tf_error) |
1978 | error ("circular pointer delegation detected"); | |
1df3c246 | 1979 | return error_mark_node; |
1980 | } | |
c6723852 | 1981 | |
f1f41a6c | 1982 | vec_safe_push (types_memoized, TREE_TYPE (expr)); |
13795292 | 1983 | last_rval = expr; |
9031d10b | 1984 | } |
02add86d | 1985 | |
9951fe5d | 1986 | while (current_instantiation () != actual_inst) |
1987 | pop_tinst_level (); | |
1988 | ||
02add86d | 1989 | if (last_rval == NULL_TREE) |
1990 | { | |
2ad2700d | 1991 | if (complain & tf_error) |
1992 | error ("base operand of %<->%> has non-pointer type %qT", type); | |
02add86d | 1993 | return error_mark_node; |
1994 | } | |
1995 | ||
90ad495b | 1996 | if (TYPE_REF_P (TREE_TYPE (last_rval))) |
471086d6 | 1997 | last_rval = convert_from_reference (last_rval); |
1998 | } | |
1999 | else | |
4405c1ad | 2000 | last_rval = decay_conversion (expr, complain); |
471086d6 | 2001 | |
c21c015b | 2002 | if (TYPE_PTR_P (TREE_TYPE (last_rval))) |
13795292 | 2003 | { |
2004 | if (processing_template_decl) | |
b6691ff5 | 2005 | { |
4cbcb428 | 2006 | expr = build_min (ARROW_EXPR, TREE_TYPE (TREE_TYPE (last_rval)), |
2007 | orig_expr); | |
2008 | TREE_SIDE_EFFECTS (expr) = TREE_SIDE_EFFECTS (last_rval); | |
b6691ff5 | 2009 | return expr; |
2010 | } | |
13795292 | 2011 | |
0744a0c1 | 2012 | return cp_build_indirect_ref (last_rval, RO_ARROW, complain); |
13795292 | 2013 | } |
471086d6 | 2014 | |
2ad2700d | 2015 | if (complain & tf_error) |
2016 | { | |
2017 | if (types_memoized) | |
2018 | error ("result of %<operator->()%> yields non-pointer result"); | |
2019 | else | |
2020 | error ("base operand of %<->%> is not a pointer"); | |
2021 | } | |
471086d6 | 2022 | return error_mark_node; |
2023 | } | |
2024 | ||
a63bc44c | 2025 | /* Return an expression for "DATUM .* COMPONENT". DATUM has not |
2026 | already been checked out to be of aggregate type. */ | |
96624a9e | 2027 | |
471086d6 | 2028 | tree |
4405c1ad | 2029 | build_m_component_ref (tree datum, tree component, tsubst_flags_t complain) |
471086d6 | 2030 | { |
a63bc44c | 2031 | tree ptrmem_type; |
24f9a660 | 2032 | tree objtype; |
a63bc44c | 2033 | tree type; |
74002e1d | 2034 | tree binfo; |
a6460bf1 | 2035 | tree ctype; |
471086d6 | 2036 | |
24357002 | 2037 | if (error_operand_p (datum) || error_operand_p (component)) |
ab9559dc | 2038 | return error_mark_node; |
2039 | ||
6ac80fa2 | 2040 | datum = mark_lvalue_use (datum); |
2041 | component = mark_rvalue_use (component); | |
c507cbd7 | 2042 | |
a63bc44c | 2043 | ptrmem_type = TREE_TYPE (component); |
05765a91 | 2044 | if (!TYPE_PTRMEM_P (ptrmem_type)) |
471086d6 | 2045 | { |
4405c1ad | 2046 | if (complain & tf_error) |
2047 | error ("%qE cannot be used as a member pointer, since it is of " | |
2048 | "type %qT", component, ptrmem_type); | |
471086d6 | 2049 | return error_mark_node; |
2050 | } | |
9031d10b | 2051 | |
2052 | objtype = TYPE_MAIN_VARIANT (TREE_TYPE (datum)); | |
95397ff9 | 2053 | if (! MAYBE_CLASS_TYPE_P (objtype)) |
ac9386a0 | 2054 | { |
4405c1ad | 2055 | if (complain & tf_error) |
2056 | error ("cannot apply member pointer %qE to %qE, which is of " | |
2057 | "non-class type %qT", component, datum, objtype); | |
ac9386a0 | 2058 | return error_mark_node; |
2059 | } | |
74002e1d | 2060 | |
a63bc44c | 2061 | type = TYPE_PTRMEM_POINTED_TO_TYPE (ptrmem_type); |
a6460bf1 | 2062 | ctype = complete_type (TYPE_PTRMEM_CLASS_TYPE (ptrmem_type)); |
2063 | ||
2064 | if (!COMPLETE_TYPE_P (ctype)) | |
471086d6 | 2065 | { |
a6460bf1 | 2066 | if (!same_type_p (ctype, objtype)) |
2067 | goto mismatch; | |
2068 | binfo = NULL; | |
2069 | } | |
2070 | else | |
2071 | { | |
ae260dcc | 2072 | binfo = lookup_base (objtype, ctype, ba_check, NULL, complain); |
9031d10b | 2073 | |
a6460bf1 | 2074 | if (!binfo) |
2075 | { | |
2076 | mismatch: | |
4405c1ad | 2077 | if (complain & tf_error) |
2078 | error ("pointer to member type %qT incompatible with object " | |
2079 | "type %qT", type, objtype); | |
a6460bf1 | 2080 | return error_mark_node; |
2081 | } | |
2082 | else if (binfo == error_mark_node) | |
2083 | return error_mark_node; | |
471086d6 | 2084 | } |
2085 | ||
05765a91 | 2086 | if (TYPE_PTRDATAMEM_P (ptrmem_type)) |
a63bc44c | 2087 | { |
3f5730c2 | 2088 | bool is_lval = real_lvalue_p (datum); |
69db191c | 2089 | tree ptype; |
2090 | ||
a63bc44c | 2091 | /* Compute the type of the field, as described in [expr.ref]. |
2092 | There's no such thing as a mutable pointer-to-member, so | |
2093 | things are not as complex as they are for references to | |
2094 | non-static data members. */ | |
2095 | type = cp_build_qualified_type (type, | |
9031d10b | 2096 | (cp_type_quals (type) |
a63bc44c | 2097 | | cp_type_quals (TREE_TYPE (datum)))); |
a6460bf1 | 2098 | |
2099 | datum = build_address (datum); | |
9031d10b | 2100 | |
a6460bf1 | 2101 | /* Convert object to the correct base. */ |
2102 | if (binfo) | |
4405c1ad | 2103 | { |
2104 | datum = build_base_path (PLUS_EXPR, datum, binfo, 1, complain); | |
2105 | if (datum == error_mark_node) | |
2106 | return error_mark_node; | |
2107 | } | |
9031d10b | 2108 | |
1bc16cab | 2109 | /* Build an expression for "object + offset" where offset is the |
2110 | value stored in the pointer-to-data-member. */ | |
69db191c | 2111 | ptype = build_pointer_type (type); |
2cc66f2a | 2112 | datum = fold_build_pointer_plus (fold_convert (ptype, datum), component); |
0744a0c1 | 2113 | datum = cp_build_fold_indirect_ref (datum); |
4405c1ad | 2114 | if (datum == error_mark_node) |
2115 | return error_mark_node; | |
2116 | ||
0277b639 | 2117 | /* If the object expression was an rvalue, return an rvalue. */ |
3f5730c2 | 2118 | if (!is_lval) |
0277b639 | 2119 | datum = move (datum); |
2120 | return datum; | |
a63bc44c | 2121 | } |
2122 | else | |
e116411c | 2123 | { |
2124 | /* 5.5/6: In a .* expression whose object expression is an rvalue, the | |
2125 | program is ill-formed if the second operand is a pointer to member | |
dabb58ae | 2126 | function with ref-qualifier & (for C++2A: unless its cv-qualifier-seq |
2127 | is const). In a .* expression whose object expression is an lvalue, | |
2128 | the program is ill-formed if the second operand is a pointer to member | |
2129 | function with ref-qualifier &&. */ | |
e116411c | 2130 | if (FUNCTION_REF_QUALIFIED (type)) |
2131 | { | |
18bede74 | 2132 | bool lval = lvalue_p (datum); |
e116411c | 2133 | if (lval && FUNCTION_RVALUE_QUALIFIED (type)) |
6f2aadc8 | 2134 | { |
2135 | if (complain & tf_error) | |
2136 | error ("pointer-to-member-function type %qT requires an rvalue", | |
2137 | ptrmem_type); | |
2138 | return error_mark_node; | |
2139 | } | |
5753ddf9 | 2140 | else if (!lval && !FUNCTION_RVALUE_QUALIFIED (type)) |
6f2aadc8 | 2141 | { |
5753ddf9 | 2142 | if ((type_memfn_quals (type) |
2143 | & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)) | |
2144 | != TYPE_QUAL_CONST) | |
2145 | { | |
2146 | if (complain & tf_error) | |
2147 | error ("pointer-to-member-function type %qT requires " | |
2148 | "an lvalue", ptrmem_type); | |
2149 | return error_mark_node; | |
2150 | } | |
2151 | else if (cxx_dialect < cxx2a) | |
2152 | { | |
2153 | if (complain & tf_warning_or_error) | |
2154 | pedwarn (input_location, OPT_Wpedantic, | |
2155 | "pointer-to-member-function type %qT requires " | |
2156 | "an lvalue before C++2a", ptrmem_type); | |
2157 | else | |
2158 | return error_mark_node; | |
2159 | } | |
6f2aadc8 | 2160 | } |
e116411c | 2161 | } |
2162 | return build2 (OFFSET_REF, type, datum, component); | |
2163 | } | |
471086d6 | 2164 | } |
2165 | ||
d2a15a12 | 2166 | /* Return a tree node for the expression TYPENAME '(' PARMS ')'. */ |
96624a9e | 2167 | |
471086d6 | 2168 | tree |
ebd21de4 | 2169 | build_functional_cast (tree exp, tree parms, tsubst_flags_t complain) |
471086d6 | 2170 | { |
2171 | /* This is either a call to a constructor, | |
2172 | or a C cast in C++'s `functional' notation. */ | |
930e8175 | 2173 | |
2174 | /* The type to which we are casting. */ | |
d2a15a12 | 2175 | tree type; |
471086d6 | 2176 | |
bd157edd | 2177 | if (error_operand_p (exp) || parms == error_mark_node) |
471086d6 | 2178 | return error_mark_node; |
2179 | ||
b7d1e8ea | 2180 | if (TREE_CODE (exp) == TYPE_DECL) |
2b2b5104 | 2181 | { |
2182 | type = TREE_TYPE (exp); | |
2183 | ||
17847cff | 2184 | if (DECL_ARTIFICIAL (exp)) |
e57bcc46 | 2185 | cp_warn_deprecated_use (type); |
2b2b5104 | 2186 | } |
471086d6 | 2187 | else |
2188 | type = exp; | |
2189 | ||
04725249 | 2190 | /* We need to check this explicitly, since value-initialization of |
2191 | arrays is allowed in other situations. */ | |
2192 | if (TREE_CODE (type) == ARRAY_TYPE) | |
2193 | { | |
2194 | if (complain & tf_error) | |
2195 | error ("functional cast to array type %qT", type); | |
2196 | return error_mark_node; | |
2197 | } | |
2198 | ||
c2ab9194 | 2199 | if (tree anode = type_uses_auto (type)) |
32d9c766 | 2200 | { |
c2ab9194 | 2201 | if (!CLASS_PLACEHOLDER_TEMPLATE (anode)) |
2202 | { | |
2203 | if (complain & tf_error) | |
cb7f839b | 2204 | error_at (DECL_SOURCE_LOCATION (TEMPLATE_TYPE_DECL (anode)), |
2205 | "invalid use of %qT", anode); | |
c2ab9194 | 2206 | return error_mark_node; |
2207 | } | |
2208 | else if (!parms) | |
2209 | { | |
6080dd2f | 2210 | /* Even if there are no parameters, we might be able to deduce from |
2211 | default template arguments. Pass TF_NONE so that we don't | |
2212 | generate redundant diagnostics. */ | |
2213 | type = do_auto_deduction (type, parms, anode, tf_none, | |
2214 | adc_variable_type); | |
2215 | if (type == error_mark_node) | |
2216 | { | |
2217 | if (complain & tf_error) | |
85b9be9b | 2218 | error ("cannot deduce template arguments for %qT from %<()%>", |
6080dd2f | 2219 | anode); |
2220 | return error_mark_node; | |
2221 | } | |
c2ab9194 | 2222 | } |
2223 | else | |
2224 | type = do_auto_deduction (type, parms, anode, complain, | |
2225 | adc_variable_type); | |
32d9c766 | 2226 | } |
2227 | ||
3cc0b4b9 | 2228 | if (processing_template_decl) |
b6691ff5 | 2229 | { |
74b7a9bc | 2230 | tree t; |
2231 | ||
2232 | /* Diagnose this even in a template. We could also try harder | |
2233 | to give all the usual errors when the type and args are | |
2234 | non-dependent... */ | |
90ad495b | 2235 | if (TYPE_REF_P (type) && !parms) |
74b7a9bc | 2236 | { |
2237 | if (complain & tf_error) | |
2238 | error ("invalid value-initialization of reference type"); | |
2239 | return error_mark_node; | |
2240 | } | |
2241 | ||
2242 | t = build_min (CAST_EXPR, type, parms); | |
b6691ff5 | 2243 | /* We don't know if it will or will not have side effects. */ |
2244 | TREE_SIDE_EFFECTS (t) = 1; | |
2245 | return t; | |
2246 | } | |
e857e9c7 | 2247 | |
95397ff9 | 2248 | if (! MAYBE_CLASS_TYPE_P (type)) |
471086d6 | 2249 | { |
471086d6 | 2250 | if (parms == NULL_TREE) |
61e4f7d9 | 2251 | { |
2252 | if (VOID_TYPE_P (type)) | |
3ab4693e | 2253 | return void_node; |
6f905cd1 | 2254 | return build_value_init (cv_unqualified (type), complain); |
61e4f7d9 | 2255 | } |
1a3f833b | 2256 | |
5c775397 | 2257 | /* This must build a C cast. */ |
1f3d2e3f | 2258 | parms = build_x_compound_expr_from_list (parms, ELK_FUNC_CAST, complain); |
717ecce9 | 2259 | return cp_build_c_cast (type, parms, complain); |
471086d6 | 2260 | } |
2261 | ||
e178a5a9 | 2262 | /* Prepare to evaluate as a call to a constructor. If this expression |
2263 | is actually used, for example, | |
9031d10b | 2264 | |
e178a5a9 | 2265 | return X (arg1, arg2, ...); |
9031d10b | 2266 | |
e178a5a9 | 2267 | then the slot being initialized will be filled in. */ |
2268 | ||
a5f2d620 | 2269 | if (!complete_type_or_maybe_complain (type, NULL_TREE, complain)) |
4b72716d | 2270 | return error_mark_node; |
d28993f1 | 2271 | if (abstract_virtuals_error_sfinae (ACU_CAST, type, complain)) |
8c18e707 | 2272 | return error_mark_node; |
471086d6 | 2273 | |
930e8175 | 2274 | /* [expr.type.conv] |
2275 | ||
2276 | If the expression list is a single-expression, the type | |
2277 | conversion is equivalent (in definedness, and if defined in | |
2278 | meaning) to the corresponding cast expression. */ | |
471086d6 | 2279 | if (parms && TREE_CHAIN (parms) == NULL_TREE) |
717ecce9 | 2280 | return cp_build_c_cast (type, TREE_VALUE (parms), complain); |
471086d6 | 2281 | |
930e8175 | 2282 | /* [expr.type.conv] |
2283 | ||
2284 | The expression T(), where T is a simple-type-specifier for a | |
2285 | non-array complete object type or the (possibly cv-qualified) | |
2286 | void type, creates an rvalue of the specified type, which is | |
2287 | value-initialized. */ | |
2288 | ||
a366cfa9 | 2289 | if (parms == NULL_TREE) |
df7d45e1 | 2290 | { |
a5f2d620 | 2291 | exp = build_value_init (type, complain); |
9e505437 | 2292 | exp = get_target_expr_sfinae (exp, complain); |
ce984e5e | 2293 | return exp; |
df7d45e1 | 2294 | } |
2295 | ||
930e8175 | 2296 | /* Call the constructor. */ |
02fafda8 | 2297 | releasing_vec parmvec; |
f352a3fb | 2298 | for (; parms != NULL_TREE; parms = TREE_CHAIN (parms)) |
f1f41a6c | 2299 | vec_safe_push (parmvec, TREE_VALUE (parms)); |
f352a3fb | 2300 | exp = build_special_member_call (NULL_TREE, complete_ctor_identifier, |
2301 | &parmvec, type, LOOKUP_NORMAL, complain); | |
471086d6 | 2302 | |
d2a15a12 | 2303 | if (exp == error_mark_node) |
ddb9bca7 | 2304 | return error_mark_node; |
471086d6 | 2305 | |
3d4bed93 | 2306 | return build_cplus_new (type, exp, complain); |
471086d6 | 2307 | } |
2308 | \f | |
0e0a0dea | 2309 | |
316b7a44 | 2310 | /* Add new exception specifier SPEC, to the LIST we currently have. |
2311 | If it's already in LIST then do nothing. | |
2312 | Moan if it's bad and we're allowed to. COMPLAIN < 0 means we | |
2313 | know what we're doing. */ | |
2314 | ||
2315 | tree | |
a470c772 | 2316 | add_exception_specifier (tree list, tree spec, tsubst_flags_t complain) |
316b7a44 | 2317 | { |
bd57a622 | 2318 | bool ok; |
316b7a44 | 2319 | tree core = spec; |
bd57a622 | 2320 | bool is_ptr; |
a52d5726 | 2321 | diagnostic_t diag_type = DK_UNSPECIFIED; /* none */ |
9031d10b | 2322 | |
316b7a44 | 2323 | if (spec == error_mark_node) |
2324 | return list; | |
9031d10b | 2325 | |
b4df430b | 2326 | gcc_assert (spec && (!list || TREE_VALUE (list))); |
9031d10b | 2327 | |
316b7a44 | 2328 | /* [except.spec] 1, type in an exception specifier shall not be |
2329 | incomplete, or pointer or ref to incomplete other than pointer | |
2330 | to cv void. */ | |
c21c015b | 2331 | is_ptr = TYPE_PTR_P (core); |
90ad495b | 2332 | if (is_ptr || TYPE_REF_P (core)) |
316b7a44 | 2333 | core = TREE_TYPE (core); |
2334 | if (complain < 0) | |
bd57a622 | 2335 | ok = true; |
e3cfe3ce | 2336 | else if (VOID_TYPE_P (core)) |
316b7a44 | 2337 | ok = is_ptr; |
2338 | else if (TREE_CODE (core) == TEMPLATE_TYPE_PARM) | |
bd57a622 | 2339 | ok = true; |
ba1f2de0 | 2340 | else if (processing_template_decl) |
bd57a622 | 2341 | ok = true; |
316b7a44 | 2342 | else |
6fee0737 | 2343 | { |
bd57a622 | 2344 | ok = true; |
6fee0737 | 2345 | /* 15.4/1 says that types in an exception specifier must be complete, |
653e5405 | 2346 | but it seems more reasonable to only require this on definitions |
2347 | and calls. So just give a pedwarn at this point; we will give an | |
2348 | error later if we hit one of those two cases. */ | |
6fee0737 | 2349 | if (!COMPLETE_TYPE_P (complete_type (core))) |
a52d5726 | 2350 | diag_type = DK_PEDWARN; /* pedwarn */ |
6fee0737 | 2351 | } |
ba1f2de0 | 2352 | |
316b7a44 | 2353 | if (ok) |
2354 | { | |
2355 | tree probe; | |
9031d10b | 2356 | |
316b7a44 | 2357 | for (probe = list; probe; probe = TREE_CHAIN (probe)) |
653e5405 | 2358 | if (same_type_p (TREE_VALUE (probe), spec)) |
2359 | break; | |
316b7a44 | 2360 | if (!probe) |
0a7d30b7 | 2361 | list = tree_cons (NULL_TREE, spec, list); |
316b7a44 | 2362 | } |
6fee0737 | 2363 | else |
a52d5726 | 2364 | diag_type = DK_ERROR; /* error */ |
9031d10b | 2365 | |
86359a65 | 2366 | if (diag_type != DK_UNSPECIFIED |
2367 | && (complain & tf_warning_or_error)) | |
6fee0737 | 2368 | cxx_incomplete_type_diagnostic (NULL_TREE, core, diag_type); |
2369 | ||
316b7a44 | 2370 | return list; |
2371 | } | |
d8ae7647 | 2372 | |
3239620b | 2373 | /* Like nothrow_spec_p, but don't abort on deferred noexcept. */ |
2374 | ||
2375 | static bool | |
2376 | nothrow_spec_p_uninst (const_tree spec) | |
2377 | { | |
2378 | if (DEFERRED_NOEXCEPT_SPEC_P (spec)) | |
2379 | return false; | |
2380 | return nothrow_spec_p (spec); | |
2381 | } | |
2382 | ||
d8ae7647 | 2383 | /* Combine the two exceptions specifier lists LIST and ADD, and return |
f2c1aabc | 2384 | their union. */ |
d8ae7647 | 2385 | |
2386 | tree | |
f2c1aabc | 2387 | merge_exception_specifiers (tree list, tree add) |
d8ae7647 | 2388 | { |
3239620b | 2389 | tree noex, orig_list; |
2390 | ||
ce7f22f1 | 2391 | if (list == error_mark_node || add == error_mark_node) |
2392 | return error_mark_node; | |
2393 | ||
9f354539 | 2394 | /* No exception-specifier or noexcept(false) are less strict than |
2395 | anything else. Prefer the newer variant (LIST). */ | |
2396 | if (!list || list == noexcept_false_spec) | |
2397 | return list; | |
2398 | else if (!add || add == noexcept_false_spec) | |
2399 | return add; | |
6bb4902d | 2400 | |
3239620b | 2401 | /* noexcept(true) and throw() are stricter than anything else. |
2402 | As above, prefer the more recent one (LIST). */ | |
2403 | if (nothrow_spec_p_uninst (add)) | |
d8ae7647 | 2404 | return list; |
3239620b | 2405 | |
f2c1aabc | 2406 | /* Two implicit noexcept specs (e.g. on a destructor) are equivalent. */ |
2407 | if (UNEVALUATED_NOEXCEPT_SPEC_P (add) | |
2408 | && UNEVALUATED_NOEXCEPT_SPEC_P (list)) | |
2409 | return list; | |
2410 | /* We should have instantiated other deferred noexcept specs by now. */ | |
2411 | gcc_assert (!DEFERRED_NOEXCEPT_SPEC_P (add)); | |
2412 | ||
2413 | if (nothrow_spec_p_uninst (list)) | |
02fe61a4 | 2414 | return add; |
f2c1aabc | 2415 | noex = TREE_PURPOSE (list); |
2416 | gcc_checking_assert (!TREE_PURPOSE (add) | |
4c3bd1e3 | 2417 | || errorcount || !flag_exceptions |
f2c1aabc | 2418 | || cp_tree_equal (noex, TREE_PURPOSE (add))); |
3239620b | 2419 | |
2420 | /* Combine the dynamic-exception-specifiers, if any. */ | |
2421 | orig_list = list; | |
2422 | for (; add && TREE_VALUE (add); add = TREE_CHAIN (add)) | |
d8ae7647 | 2423 | { |
3239620b | 2424 | tree spec = TREE_VALUE (add); |
2425 | tree probe; | |
9031d10b | 2426 | |
3239620b | 2427 | for (probe = orig_list; probe && TREE_VALUE (probe); |
2428 | probe = TREE_CHAIN (probe)) | |
2429 | if (same_type_p (TREE_VALUE (probe), spec)) | |
2430 | break; | |
2431 | if (!probe) | |
653e5405 | 2432 | { |
3239620b | 2433 | spec = build_tree_list (NULL_TREE, spec); |
2434 | TREE_CHAIN (spec) = list; | |
2435 | list = spec; | |
653e5405 | 2436 | } |
d8ae7647 | 2437 | } |
3239620b | 2438 | |
2439 | /* Keep the noexcept-specifier at the beginning of the list. */ | |
2440 | if (noex != TREE_PURPOSE (list)) | |
2441 | list = tree_cons (noex, TREE_VALUE (list), TREE_CHAIN (list)); | |
2442 | ||
d8ae7647 | 2443 | return list; |
2444 | } | |
6fee0737 | 2445 | |
2446 | /* Subroutine of build_call. Ensure that each of the types in the | |
2447 | exception specification is complete. Technically, 15.4/1 says that | |
2448 | they need to be complete when we see a declaration of the function, | |
2449 | but we should be able to get away with only requiring this when the | |
2450 | function is defined or called. See also add_exception_specifier. */ | |
2451 | ||
2452 | void | |
e394ed0f | 2453 | require_complete_eh_spec_types (tree fntype, tree decl) |
6fee0737 | 2454 | { |
2455 | tree raises; | |
2456 | /* Don't complain about calls to op new. */ | |
2457 | if (decl && DECL_ARTIFICIAL (decl)) | |
2458 | return; | |
2459 | for (raises = TYPE_RAISES_EXCEPTIONS (fntype); raises; | |
2460 | raises = TREE_CHAIN (raises)) | |
2461 | { | |
2462 | tree type = TREE_VALUE (raises); | |
2463 | if (type && !COMPLETE_TYPE_P (type)) | |
2464 | { | |
2465 | if (decl) | |
2466 | error | |
03d6ca9e | 2467 | ("call to function %qD which throws incomplete type %q#T", |
6fee0737 | 2468 | decl, type); |
2469 | else | |
03d6ca9e | 2470 | error ("call to function which throws incomplete type %q#T", |
6fee0737 | 2471 | decl); |
2472 | } | |
2473 | } | |
2474 | } | |
d97a7640 | 2475 | |
2476 | \f | |
2477 | #include "gt-cp-typeck2.h" |