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