1 | /* Process declarations and variables for -*- C++ -*- compiler. |
2 | Copyright (C) 1988-2023 Free Software Foundation, Inc. |
3 | Contributed by Michael Tiemann (tiemann@cygnus.com) |
4 | |
5 | This file is part of GCC. |
6 | |
7 | GCC is free software; you can redistribute it and/or modify |
8 | it under the terms of the GNU General Public License as published by |
9 | the Free Software Foundation; either version 3, or (at your option) |
10 | any later version. |
11 | |
12 | GCC is distributed in the hope that it will be useful, |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | GNU General Public License for more details. |
16 | |
17 | You should have received a copy of the GNU General Public License |
18 | along with GCC; see the file COPYING3. If not see |
19 | <http://www.gnu.org/licenses/>. */ |
20 | |
21 | |
22 | /* Process declarations and symbol lookup for C++ front end. |
23 | Also constructs types; the standard scalar types at initialization, |
24 | and structure, union, array and enum types when they are declared. */ |
25 | |
26 | /* ??? not all decl nodes are given the most useful possible |
27 | line numbers. For example, the CONST_DECLs for enum values. */ |
28 | |
29 | #include "config.h" |
30 | #define INCLUDE_MEMORY |
31 | #include "system.h" |
32 | #include "coretypes.h" |
33 | #include "target.h" |
34 | #include "c-family/c-target.h" |
35 | #include "cp-tree.h" |
36 | #include "timevar.h" |
37 | #include "stringpool.h" |
38 | #include "cgraph.h" |
39 | #include "stor-layout.h" |
40 | #include "varasm.h" |
41 | #include "attribs.h" |
42 | #include "flags.h" |
43 | #include "tree-iterator.h" |
44 | #include "decl.h" |
45 | #include "intl.h" |
46 | #include "toplev.h" |
47 | #include "c-family/c-objc.h" |
48 | #include "c-family/c-pragma.h" |
49 | #include "c-family/c-ubsan.h" |
50 | #include "cp/cp-name-hint.h" |
51 | #include "debug.h" |
52 | #include "plugin.h" |
53 | #include "builtins.h" |
54 | #include "gimplify.h" |
55 | #include "asan.h" |
56 | #include "gcc-rich-location.h" |
57 | #include "langhooks.h" |
58 | #include "context.h" /* For 'g'. */ |
59 | #include "omp-general.h" |
60 | #include "omp-offload.h" /* For offload_vars. */ |
61 | #include "opts.h" |
62 | #include "langhooks-def.h" /* For lhd_simulate_record_decl */ |
63 | |
64 | /* Possible cases of bad specifiers type used by bad_specifiers. */ |
65 | enum bad_spec_place { |
66 | BSP_VAR, /* variable */ |
67 | BSP_PARM, /* parameter */ |
68 | BSP_TYPE, /* type */ |
69 | BSP_FIELD /* field */ |
70 | }; |
71 | |
72 | static const char *redeclaration_error_message (tree, tree); |
73 | |
74 | static bool decl_jump_unsafe (tree); |
75 | static void require_complete_types_for_parms (tree); |
76 | static tree grok_reference_init (tree, tree, tree, int); |
77 | static tree grokvardecl (tree, tree, tree, const cp_decl_specifier_seq *, |
78 | int, int, int, bool, int, tree, location_t); |
79 | static void check_static_variable_definition (tree, tree); |
80 | static void record_unknown_type (tree, const char *); |
81 | static int member_function_or_else (tree, tree, enum overload_flags); |
82 | static tree local_variable_p_walkfn (tree *, int *, void *); |
83 | static const char *tag_name (enum tag_types); |
84 | static tree lookup_and_check_tag (enum tag_types, tree, TAG_how, bool); |
85 | static void maybe_deduce_size_from_array_init (tree, tree); |
86 | static void layout_var_decl (tree); |
87 | static tree check_initializer (tree, tree, int, vec<tree, va_gc> **); |
88 | static void make_rtl_for_nonlocal_decl (tree, tree, const char *); |
89 | static void copy_type_enum (tree , tree); |
90 | static void check_function_type (tree, tree); |
91 | static void begin_destructor_body (void); |
92 | static void record_key_method_defined (tree); |
93 | static tree create_array_type_for_decl (tree, tree, tree, location_t); |
94 | static tree get_atexit_node (void); |
95 | static tree get_dso_handle_node (void); |
96 | static tree start_cleanup_fn (void); |
97 | static void end_cleanup_fn (void); |
98 | static tree cp_make_fname_decl (location_t, tree, int); |
99 | static void initialize_predefined_identifiers (void); |
100 | static tree check_special_function_return_type |
101 | (special_function_kind, tree, tree, int, const location_t*); |
102 | static tree push_cp_library_fn (enum tree_code, tree, int); |
103 | static tree build_cp_library_fn (tree, enum tree_code, tree, int); |
104 | static void store_parm_decls (tree); |
105 | static void initialize_local_var (tree, tree); |
106 | static void expand_static_init (tree, tree); |
107 | static location_t smallest_type_location (const cp_decl_specifier_seq*); |
108 | |
109 | /* The following symbols are subsumed in the cp_global_trees array, and |
110 | listed here individually for documentation purposes. |
111 | |
112 | C++ extensions |
113 | tree wchar_decl_node; |
114 | |
115 | tree vtable_entry_type; |
116 | tree delta_type_node; |
117 | tree __t_desc_type_node; |
118 | |
119 | tree class_type_node; |
120 | tree unknown_type_node; |
121 | |
122 | Array type `vtable_entry_type[]' |
123 | |
124 | tree vtbl_type_node; |
125 | tree vtbl_ptr_type_node; |
126 | |
127 | Namespaces, |
128 | |
129 | tree std_node; |
130 | tree abi_node; |
131 | |
132 | A FUNCTION_DECL which can call `abort'. Not necessarily the |
133 | one that the user will declare, but sufficient to be called |
134 | by routines that want to abort the program. |
135 | |
136 | tree abort_fndecl; |
137 | |
138 | Used by RTTI |
139 | tree type_info_type_node, tinfo_decl_id, tinfo_decl_type; |
140 | tree tinfo_var_id; */ |
141 | |
142 | tree cp_global_trees[CPTI_MAX]; |
143 | |
144 | /* A list of objects which have constructors or destructors |
145 | which reside in namespace scope. The decl is stored in |
146 | the TREE_VALUE slot and the initializer is stored |
147 | in the TREE_PURPOSE slot. */ |
148 | tree static_aggregates; |
149 | |
150 | /* Like static_aggregates, but for thread_local variables. */ |
151 | tree tls_aggregates; |
152 | |
153 | /* A hash-map mapping from variable decls to the dynamic initializer for |
154 | the decl. This is currently only used by OpenMP. */ |
155 | decl_tree_map *dynamic_initializers; |
156 | |
157 | /* -- end of C++ */ |
158 | |
159 | /* A node for the integer constant 2. */ |
160 | |
161 | tree integer_two_node; |
162 | |
163 | /* vector of static decls. */ |
164 | vec<tree, va_gc> *static_decls; |
165 | |
166 | /* vector of keyed classes. */ |
167 | vec<tree, va_gc> *keyed_classes; |
168 | |
169 | /* Used only for jumps to as-yet undefined labels, since jumps to |
170 | defined labels can have their validity checked immediately. */ |
171 | |
172 | struct GTY((chain_next ("%h.next" ))) named_label_use_entry { |
173 | struct named_label_use_entry *next; |
174 | /* The binding level to which this entry is *currently* attached. |
175 | This is initially the binding level in which the goto appeared, |
176 | but is modified as scopes are closed. */ |
177 | cp_binding_level *binding_level; |
178 | /* The head of the names list that was current when the goto appeared, |
179 | or the inner scope popped. These are the decls that will *not* be |
180 | skipped when jumping to the label. */ |
181 | tree names_in_scope; |
182 | /* The location of the goto, for error reporting. */ |
183 | location_t o_goto_locus; |
184 | /* True if an OpenMP structured block scope has been closed since |
185 | the goto appeared. This means that the branch from the label will |
186 | illegally exit an OpenMP scope. */ |
187 | bool in_omp_scope; |
188 | }; |
189 | |
190 | /* A list of all LABEL_DECLs in the function that have names. Here so |
191 | we can clear out their names' definitions at the end of the |
192 | function, and so we can check the validity of jumps to these labels. */ |
193 | |
194 | struct GTY((for_user)) named_label_entry { |
195 | |
196 | tree name; /* Name of decl. */ |
197 | |
198 | tree label_decl; /* LABEL_DECL, unless deleted local label. */ |
199 | |
200 | named_label_entry *outer; /* Outer shadowed chain. */ |
201 | |
202 | /* The binding level to which the label is *currently* attached. |
203 | This is initially set to the binding level in which the label |
204 | is defined, but is modified as scopes are closed. */ |
205 | cp_binding_level *binding_level; |
206 | |
207 | /* The head of the names list that was current when the label was |
208 | defined, or the inner scope popped. These are the decls that will |
209 | be skipped when jumping to the label. */ |
210 | tree names_in_scope; |
211 | |
212 | /* A vector of all decls from all binding levels that would be |
213 | crossed by a backward branch to the label. */ |
214 | vec<tree, va_gc> *bad_decls; |
215 | |
216 | /* A list of uses of the label, before the label is defined. */ |
217 | named_label_use_entry *uses; |
218 | |
219 | /* The following bits are set after the label is defined, and are |
220 | updated as scopes are popped. They indicate that a jump to the |
221 | label will illegally enter a scope of the given flavor. */ |
222 | bool in_try_scope; |
223 | bool in_catch_scope; |
224 | bool in_omp_scope; |
225 | bool in_transaction_scope; |
226 | bool in_constexpr_if; |
227 | bool in_consteval_if; |
228 | bool in_stmt_expr; |
229 | }; |
230 | |
231 | #define named_labels cp_function_chain->x_named_labels |
232 | |
233 | /* The number of function bodies which we are currently processing. |
234 | (Zero if we are at namespace scope, one inside the body of a |
235 | function, two inside the body of a function in a local class, etc.) */ |
236 | int function_depth; |
237 | |
238 | /* Whether the exception-specifier is part of a function type (i.e. C++17). */ |
239 | bool flag_noexcept_type; |
240 | |
241 | /* States indicating how grokdeclarator() should handle declspecs marked |
242 | with __attribute__((deprecated)). An object declared as |
243 | __attribute__((deprecated)) suppresses warnings of uses of other |
244 | deprecated items. */ |
245 | enum deprecated_states deprecated_state = DEPRECATED_NORMAL; |
246 | |
247 | |
248 | /* A list of VAR_DECLs whose type was incomplete at the time the |
249 | variable was declared. */ |
250 | |
251 | struct GTY(()) incomplete_var { |
252 | tree decl; |
253 | tree incomplete_type; |
254 | }; |
255 | |
256 | |
257 | static GTY(()) vec<incomplete_var, va_gc> *incomplete_vars; |
258 | |
259 | /* Returns the kind of template specialization we are currently |
260 | processing, given that it's declaration contained N_CLASS_SCOPES |
261 | explicit scope qualifications. */ |
262 | |
263 | tmpl_spec_kind |
264 | current_tmpl_spec_kind (int n_class_scopes) |
265 | { |
266 | int n_template_parm_scopes = 0; |
267 | int seen_specialization_p = 0; |
268 | int innermost_specialization_p = 0; |
269 | cp_binding_level *b; |
270 | |
271 | /* Scan through the template parameter scopes. */ |
272 | for (b = current_binding_level; |
273 | b->kind == sk_template_parms; |
274 | b = b->level_chain) |
275 | { |
276 | /* If we see a specialization scope inside a parameter scope, |
277 | then something is wrong. That corresponds to a declaration |
278 | like: |
279 | |
280 | template <class T> template <> ... |
281 | |
282 | which is always invalid since [temp.expl.spec] forbids the |
283 | specialization of a class member template if the enclosing |
284 | class templates are not explicitly specialized as well. */ |
285 | if (b->explicit_spec_p) |
286 | { |
287 | if (n_template_parm_scopes == 0) |
288 | innermost_specialization_p = 1; |
289 | else |
290 | seen_specialization_p = 1; |
291 | } |
292 | else if (seen_specialization_p == 1) |
293 | return tsk_invalid_member_spec; |
294 | |
295 | ++n_template_parm_scopes; |
296 | } |
297 | |
298 | /* Handle explicit instantiations. */ |
299 | if (processing_explicit_instantiation) |
300 | { |
301 | if (n_template_parm_scopes != 0) |
302 | /* We've seen a template parameter list during an explicit |
303 | instantiation. For example: |
304 | |
305 | template <class T> template void f(int); |
306 | |
307 | This is erroneous. */ |
308 | return tsk_invalid_expl_inst; |
309 | else |
310 | return tsk_expl_inst; |
311 | } |
312 | |
313 | if (n_template_parm_scopes < n_class_scopes) |
314 | /* We've not seen enough template headers to match all the |
315 | specialized classes present. For example: |
316 | |
317 | template <class T> void R<T>::S<T>::f(int); |
318 | |
319 | This is invalid; there needs to be one set of template |
320 | parameters for each class. */ |
321 | return tsk_insufficient_parms; |
322 | else if (n_template_parm_scopes == n_class_scopes) |
323 | /* We're processing a non-template declaration (even though it may |
324 | be a member of a template class.) For example: |
325 | |
326 | template <class T> void S<T>::f(int); |
327 | |
328 | The `class T' matches the `S<T>', leaving no template headers |
329 | corresponding to the `f'. */ |
330 | return tsk_none; |
331 | else if (n_template_parm_scopes > n_class_scopes + 1) |
332 | /* We've got too many template headers. For example: |
333 | |
334 | template <> template <class T> void f (T); |
335 | |
336 | There need to be more enclosing classes. */ |
337 | return tsk_excessive_parms; |
338 | else |
339 | /* This must be a template. It's of the form: |
340 | |
341 | template <class T> template <class U> void S<T>::f(U); |
342 | |
343 | This is a specialization if the innermost level was a |
344 | specialization; otherwise it's just a definition of the |
345 | template. */ |
346 | return innermost_specialization_p ? tsk_expl_spec : tsk_template; |
347 | } |
348 | |
349 | /* Exit the current scope. */ |
350 | |
351 | void |
352 | finish_scope (void) |
353 | { |
354 | poplevel (0, 0, 0); |
355 | } |
356 | |
357 | /* When a label goes out of scope, check to see if that label was used |
358 | in a valid manner, and issue any appropriate warnings or errors. */ |
359 | |
360 | static void |
361 | check_label_used (tree label) |
362 | { |
363 | if (!processing_template_decl) |
364 | { |
365 | if (DECL_INITIAL (label) == NULL_TREE) |
366 | { |
367 | location_t location; |
368 | |
369 | error ("label %q+D used but not defined" , label); |
370 | location = input_location; |
371 | /* FIXME want (LOCATION_FILE (input_location), (line)0) */ |
372 | /* Avoid crashing later. */ |
373 | define_label (location, DECL_NAME (label)); |
374 | } |
375 | else |
376 | warn_for_unused_label (label); |
377 | } |
378 | } |
379 | |
380 | /* Helper function to sort named label entries in a vector by DECL_UID. */ |
381 | |
382 | static int |
383 | sort_labels (const void *a, const void *b) |
384 | { |
385 | tree label1 = *(tree const *) a; |
386 | tree label2 = *(tree const *) b; |
387 | |
388 | /* DECL_UIDs can never be equal. */ |
389 | return DECL_UID (label1) > DECL_UID (label2) ? -1 : +1; |
390 | } |
391 | |
392 | /* At the end of a function, all labels declared within the function |
393 | go out of scope. BLOCK is the top-level block for the |
394 | function. */ |
395 | |
396 | static void |
397 | pop_labels (tree block) |
398 | { |
399 | if (!named_labels) |
400 | return; |
401 | |
402 | /* We need to add the labels to the block chain, so debug |
403 | information is emitted. But, we want the order to be stable so |
404 | need to sort them first. Otherwise the debug output could be |
405 | randomly ordered. I guess it's mostly stable, unless the hash |
406 | table implementation changes. */ |
407 | auto_vec<tree, 32> labels (named_labels->elements ()); |
408 | hash_table<named_label_hash>::iterator end (named_labels->end ()); |
409 | for (hash_table<named_label_hash>::iterator iter |
410 | (named_labels->begin ()); iter != end; ++iter) |
411 | { |
412 | named_label_entry *ent = *iter; |
413 | |
414 | gcc_checking_assert (!ent->outer); |
415 | if (ent->label_decl) |
416 | labels.quick_push (obj: ent->label_decl); |
417 | ggc_free (ent); |
418 | } |
419 | named_labels = NULL; |
420 | labels.qsort (sort_labels); |
421 | |
422 | while (labels.length ()) |
423 | { |
424 | tree label = labels.pop (); |
425 | |
426 | DECL_CHAIN (label) = BLOCK_VARS (block); |
427 | BLOCK_VARS (block) = label; |
428 | |
429 | check_label_used (label); |
430 | } |
431 | } |
432 | |
433 | /* At the end of a block with local labels, restore the outer definition. */ |
434 | |
435 | static void |
436 | pop_local_label (tree id, tree label) |
437 | { |
438 | check_label_used (label); |
439 | named_label_entry **slot = named_labels->find_slot_with_hash |
440 | (comparable: id, IDENTIFIER_HASH_VALUE (id), insert: NO_INSERT); |
441 | named_label_entry *ent = *slot; |
442 | |
443 | if (ent->outer) |
444 | ent = ent->outer; |
445 | else |
446 | { |
447 | ent = ggc_cleared_alloc<named_label_entry> (); |
448 | ent->name = id; |
449 | } |
450 | *slot = ent; |
451 | } |
452 | |
453 | /* The following two routines are used to interface to Objective-C++. |
454 | The binding level is purposely treated as an opaque type. */ |
455 | |
456 | void * |
457 | objc_get_current_scope (void) |
458 | { |
459 | return current_binding_level; |
460 | } |
461 | |
462 | /* The following routine is used by the NeXT-style SJLJ exceptions; |
463 | variables get marked 'volatile' so as to not be clobbered by |
464 | _setjmp()/_longjmp() calls. All variables in the current scope, |
465 | as well as parent scopes up to (but not including) ENCLOSING_BLK |
466 | shall be thusly marked. */ |
467 | |
468 | void |
469 | objc_mark_locals_volatile (void *enclosing_blk) |
470 | { |
471 | cp_binding_level *scope; |
472 | |
473 | for (scope = current_binding_level; |
474 | scope && scope != enclosing_blk; |
475 | scope = scope->level_chain) |
476 | { |
477 | tree decl; |
478 | |
479 | for (decl = scope->names; decl; decl = TREE_CHAIN (decl)) |
480 | objc_volatilize_decl (decl); |
481 | |
482 | /* Do not climb up past the current function. */ |
483 | if (scope->kind == sk_function_parms) |
484 | break; |
485 | } |
486 | } |
487 | |
488 | /* True if B is the level for the condition of a constexpr if. */ |
489 | |
490 | static bool |
491 | level_for_constexpr_if (cp_binding_level *b) |
492 | { |
493 | return (b->kind == sk_cond && b->this_entity |
494 | && TREE_CODE (b->this_entity) == IF_STMT |
495 | && IF_STMT_CONSTEXPR_P (b->this_entity)); |
496 | } |
497 | |
498 | /* True if B is the level for the condition of a consteval if. */ |
499 | |
500 | static bool |
501 | level_for_consteval_if (cp_binding_level *b) |
502 | { |
503 | return (b->kind == sk_cond && b->this_entity |
504 | && TREE_CODE (b->this_entity) == IF_STMT |
505 | && IF_STMT_CONSTEVAL_P (b->this_entity)); |
506 | } |
507 | |
508 | /* Update data for defined and undefined labels when leaving a scope. */ |
509 | |
510 | int |
511 | poplevel_named_label_1 (named_label_entry **slot, cp_binding_level *bl) |
512 | { |
513 | named_label_entry *ent = *slot; |
514 | cp_binding_level *obl = bl->level_chain; |
515 | |
516 | if (ent->binding_level == bl) |
517 | { |
518 | tree decl; |
519 | |
520 | /* ENT->NAMES_IN_SCOPE may contain a mixture of DECLs and |
521 | TREE_LISTs representing OVERLOADs, so be careful. */ |
522 | for (decl = ent->names_in_scope; decl; decl = (DECL_P (decl) |
523 | ? DECL_CHAIN (decl) |
524 | : TREE_CHAIN (decl))) |
525 | if (decl_jump_unsafe (decl)) |
526 | vec_safe_push (v&: ent->bad_decls, obj: decl); |
527 | |
528 | ent->binding_level = obl; |
529 | ent->names_in_scope = obl->names; |
530 | switch (bl->kind) |
531 | { |
532 | case sk_try: |
533 | ent->in_try_scope = true; |
534 | break; |
535 | case sk_catch: |
536 | ent->in_catch_scope = true; |
537 | break; |
538 | case sk_omp: |
539 | ent->in_omp_scope = true; |
540 | break; |
541 | case sk_transaction: |
542 | ent->in_transaction_scope = true; |
543 | break; |
544 | case sk_stmt_expr: |
545 | ent->in_stmt_expr = true; |
546 | break; |
547 | case sk_block: |
548 | if (level_for_constexpr_if (b: bl->level_chain)) |
549 | ent->in_constexpr_if = true; |
550 | else if (level_for_consteval_if (b: bl->level_chain)) |
551 | ent->in_consteval_if = true; |
552 | break; |
553 | default: |
554 | break; |
555 | } |
556 | } |
557 | else if (ent->uses) |
558 | { |
559 | struct named_label_use_entry *use; |
560 | |
561 | for (use = ent->uses; use ; use = use->next) |
562 | if (use->binding_level == bl) |
563 | { |
564 | use->binding_level = obl; |
565 | use->names_in_scope = obl->names; |
566 | if (bl->kind == sk_omp) |
567 | use->in_omp_scope = true; |
568 | } |
569 | } |
570 | |
571 | return 1; |
572 | } |
573 | |
574 | /* Saved errorcount to avoid -Wunused-but-set-{parameter,variable} warnings |
575 | when errors were reported, except for -Werror-unused-but-set-*. */ |
576 | static int unused_but_set_errorcount; |
577 | |
578 | /* Exit a binding level. |
579 | Pop the level off, and restore the state of the identifier-decl mappings |
580 | that were in effect when this level was entered. |
581 | |
582 | If KEEP == 1, this level had explicit declarations, so |
583 | and create a "block" (a BLOCK node) for the level |
584 | to record its declarations and subblocks for symbol table output. |
585 | |
586 | If FUNCTIONBODY is nonzero, this level is the body of a function, |
587 | so create a block as if KEEP were set and also clear out all |
588 | label names. |
589 | |
590 | If REVERSE is nonzero, reverse the order of decls before putting |
591 | them into the BLOCK. */ |
592 | |
593 | tree |
594 | poplevel (int keep, int reverse, int functionbody) |
595 | { |
596 | tree link; |
597 | /* The chain of decls was accumulated in reverse order. |
598 | Put it into forward order, just for cleanliness. */ |
599 | tree decls; |
600 | tree subblocks; |
601 | tree block; |
602 | tree decl; |
603 | scope_kind kind; |
604 | |
605 | auto_cond_timevar tv (TV_NAME_LOOKUP); |
606 | restart: |
607 | |
608 | block = NULL_TREE; |
609 | |
610 | gcc_assert (current_binding_level->kind != sk_class |
611 | && current_binding_level->kind != sk_namespace); |
612 | |
613 | if (current_binding_level->kind == sk_cleanup) |
614 | functionbody = 0; |
615 | subblocks = functionbody >= 0 ? current_binding_level->blocks : 0; |
616 | |
617 | gcc_assert (!vec_safe_length (current_binding_level->class_shadowed)); |
618 | |
619 | /* We used to use KEEP == 2 to indicate that the new block should go |
620 | at the beginning of the list of blocks at this binding level, |
621 | rather than the end. This hack is no longer used. */ |
622 | gcc_assert (keep == 0 || keep == 1); |
623 | |
624 | if (current_binding_level->keep) |
625 | keep = 1; |
626 | |
627 | /* Any uses of undefined labels, and any defined labels, now operate |
628 | under constraints of next binding contour. */ |
629 | if (cfun && !functionbody && named_labels) |
630 | named_labels->traverse<cp_binding_level *, poplevel_named_label_1> |
631 | (current_binding_level); |
632 | |
633 | /* Get the decls in the order they were written. |
634 | Usually current_binding_level->names is in reverse order. |
635 | But parameter decls were previously put in forward order. */ |
636 | |
637 | decls = current_binding_level->names; |
638 | if (reverse) |
639 | { |
640 | decls = nreverse (decls); |
641 | current_binding_level->names = decls; |
642 | } |
643 | |
644 | /* If there were any declarations or structure tags in that level, |
645 | or if this level is a function body, |
646 | create a BLOCK to record them for the life of this function. */ |
647 | block = NULL_TREE; |
648 | /* Avoid function body block if possible. */ |
649 | if (functionbody && subblocks && BLOCK_CHAIN (subblocks) == NULL_TREE) |
650 | keep = 0; |
651 | else if (keep == 1 || functionbody) |
652 | block = make_node (BLOCK); |
653 | if (block != NULL_TREE) |
654 | { |
655 | BLOCK_VARS (block) = decls; |
656 | BLOCK_SUBBLOCKS (block) = subblocks; |
657 | } |
658 | |
659 | /* In each subblock, record that this is its superior. */ |
660 | if (keep >= 0) |
661 | for (link = subblocks; link; link = BLOCK_CHAIN (link)) |
662 | BLOCK_SUPERCONTEXT (link) = block; |
663 | |
664 | /* Before we remove the declarations first check for unused variables. */ |
665 | if ((warn_unused_variable || warn_unused_but_set_variable) |
666 | && current_binding_level->kind != sk_template_parms |
667 | && !processing_template_decl) |
668 | for (tree d = get_local_decls (); d; d = TREE_CHAIN (d)) |
669 | { |
670 | /* There are cases where D itself is a TREE_LIST. See in |
671 | push_local_binding where the list of decls returned by |
672 | getdecls is built. */ |
673 | decl = TREE_CODE (d) == TREE_LIST ? TREE_VALUE (d) : d; |
674 | |
675 | tree type = TREE_TYPE (decl); |
676 | if (VAR_P (decl) |
677 | && (! TREE_USED (decl) || !DECL_READ_P (decl)) |
678 | && ! DECL_IN_SYSTEM_HEADER (decl) |
679 | /* For structured bindings, consider only real variables, not |
680 | subobjects. */ |
681 | && (DECL_DECOMPOSITION_P (decl) ? !DECL_DECOMP_BASE (decl) |
682 | : (DECL_NAME (decl) && !DECL_ARTIFICIAL (decl))) |
683 | && type != error_mark_node |
684 | && (!CLASS_TYPE_P (type) |
685 | || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type) |
686 | || lookup_attribute (attr_name: "warn_unused" , |
687 | TYPE_ATTRIBUTES (TREE_TYPE (decl))))) |
688 | { |
689 | if (! TREE_USED (decl)) |
690 | { |
691 | if (!DECL_NAME (decl) && DECL_DECOMPOSITION_P (decl)) |
692 | warning_at (DECL_SOURCE_LOCATION (decl), |
693 | OPT_Wunused_variable, |
694 | "unused structured binding declaration" ); |
695 | else |
696 | warning_at (DECL_SOURCE_LOCATION (decl), |
697 | OPT_Wunused_variable, "unused variable %qD" , decl); |
698 | suppress_warning (decl, OPT_Wunused_variable); |
699 | } |
700 | else if (DECL_CONTEXT (decl) == current_function_decl |
701 | // For -Wunused-but-set-variable leave references alone. |
702 | && !TYPE_REF_P (TREE_TYPE (decl)) |
703 | && errorcount == unused_but_set_errorcount) |
704 | { |
705 | if (!DECL_NAME (decl) && DECL_DECOMPOSITION_P (decl)) |
706 | warning_at (DECL_SOURCE_LOCATION (decl), |
707 | OPT_Wunused_but_set_variable, "structured " |
708 | "binding declaration set but not used" ); |
709 | else |
710 | warning_at (DECL_SOURCE_LOCATION (decl), |
711 | OPT_Wunused_but_set_variable, |
712 | "variable %qD set but not used" , decl); |
713 | unused_but_set_errorcount = errorcount; |
714 | } |
715 | } |
716 | } |
717 | |
718 | /* Remove declarations for all the DECLs in this level. */ |
719 | for (link = decls; link; link = TREE_CHAIN (link)) |
720 | { |
721 | tree name; |
722 | if (TREE_CODE (link) == TREE_LIST) |
723 | { |
724 | decl = TREE_VALUE (link); |
725 | name = TREE_PURPOSE (link); |
726 | gcc_checking_assert (name); |
727 | } |
728 | else |
729 | { |
730 | decl = link; |
731 | name = DECL_NAME (decl); |
732 | } |
733 | |
734 | /* Remove the binding. */ |
735 | if (TREE_CODE (decl) == LABEL_DECL) |
736 | pop_local_label (id: name, label: decl); |
737 | else |
738 | pop_local_binding (name, decl); |
739 | } |
740 | |
741 | /* Restore the IDENTIFIER_TYPE_VALUEs. */ |
742 | for (link = current_binding_level->type_shadowed; |
743 | link; link = TREE_CHAIN (link)) |
744 | SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link), TREE_VALUE (link)); |
745 | |
746 | /* There may be OVERLOADs (wrapped in TREE_LISTs) on the BLOCK_VARs |
747 | list if a `using' declaration put them there. The debugging |
748 | back ends won't understand OVERLOAD, so we remove them here. |
749 | Because the BLOCK_VARS are (temporarily) shared with |
750 | CURRENT_BINDING_LEVEL->NAMES we must do this fixup after we have |
751 | popped all the bindings. Also remove undeduced 'auto' decls, |
752 | which LTO doesn't understand, and can't have been used by anything. */ |
753 | if (block) |
754 | { |
755 | tree* d; |
756 | |
757 | for (d = &BLOCK_VARS (block); *d; ) |
758 | { |
759 | if (TREE_CODE (*d) == TREE_LIST |
760 | || (!processing_template_decl |
761 | && undeduced_auto_decl (*d))) |
762 | *d = TREE_CHAIN (*d); |
763 | else |
764 | d = &DECL_CHAIN (*d); |
765 | } |
766 | } |
767 | |
768 | /* If the level being exited is the top level of a function, |
769 | check over all the labels. */ |
770 | if (functionbody) |
771 | { |
772 | if (block) |
773 | { |
774 | /* Since this is the top level block of a function, the vars are |
775 | the function's parameters. Don't leave them in the BLOCK |
776 | because they are found in the FUNCTION_DECL instead. */ |
777 | BLOCK_VARS (block) = 0; |
778 | pop_labels (block); |
779 | } |
780 | else |
781 | pop_labels (block: subblocks); |
782 | } |
783 | |
784 | kind = current_binding_level->kind; |
785 | if (kind == sk_cleanup) |
786 | { |
787 | tree stmt; |
788 | |
789 | /* If this is a temporary binding created for a cleanup, then we'll |
790 | have pushed a statement list level. Pop that, create a new |
791 | BIND_EXPR for the block, and insert it into the stream. */ |
792 | stmt = pop_stmt_list (current_binding_level->statement_list); |
793 | stmt = c_build_bind_expr (input_location, block, stmt); |
794 | add_stmt (stmt); |
795 | } |
796 | |
797 | leave_scope (); |
798 | if (functionbody) |
799 | { |
800 | /* The current function is being defined, so its DECL_INITIAL |
801 | should be error_mark_node. */ |
802 | gcc_assert (DECL_INITIAL (current_function_decl) == error_mark_node); |
803 | DECL_INITIAL (current_function_decl) = block ? block : subblocks; |
804 | if (subblocks) |
805 | { |
806 | if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl)) |
807 | { |
808 | if (BLOCK_SUBBLOCKS (subblocks)) |
809 | BLOCK_OUTER_CURLY_BRACE_P (BLOCK_SUBBLOCKS (subblocks)) = 1; |
810 | } |
811 | else |
812 | BLOCK_OUTER_CURLY_BRACE_P (subblocks) = 1; |
813 | } |
814 | } |
815 | else if (block) |
816 | current_binding_level->blocks |
817 | = block_chainon (current_binding_level->blocks, block); |
818 | |
819 | /* If we did not make a block for the level just exited, |
820 | any blocks made for inner levels |
821 | (since they cannot be recorded as subblocks in that level) |
822 | must be carried forward so they will later become subblocks |
823 | of something else. */ |
824 | else if (subblocks) |
825 | current_binding_level->blocks |
826 | = block_chainon (current_binding_level->blocks, subblocks); |
827 | |
828 | /* Each and every BLOCK node created here in `poplevel' is important |
829 | (e.g. for proper debugging information) so if we created one |
830 | earlier, mark it as "used". */ |
831 | if (block) |
832 | TREE_USED (block) = 1; |
833 | |
834 | /* All temporary bindings created for cleanups are popped silently. */ |
835 | if (kind == sk_cleanup) |
836 | goto restart; |
837 | |
838 | return block; |
839 | } |
840 | |
841 | /* Call wrapup_globals_declarations for the globals in NAMESPACE. */ |
842 | /* Diagnose odr-used extern inline variables without definitions |
843 | in the current TU. */ |
844 | |
845 | int |
846 | wrapup_namespace_globals () |
847 | { |
848 | if (vec<tree, va_gc> *statics = static_decls) |
849 | { |
850 | for (tree decl : *statics) |
851 | { |
852 | if (warn_unused_function |
853 | && TREE_CODE (decl) == FUNCTION_DECL |
854 | && DECL_INITIAL (decl) == 0 |
855 | && DECL_EXTERNAL (decl) |
856 | && !TREE_PUBLIC (decl) |
857 | && !DECL_ARTIFICIAL (decl) |
858 | && !DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl) |
859 | && !warning_suppressed_p (decl, OPT_Wunused_function)) |
860 | warning_at (DECL_SOURCE_LOCATION (decl), |
861 | OPT_Wunused_function, |
862 | "%qF declared %<static%> but never defined" , decl); |
863 | |
864 | if (VAR_P (decl) |
865 | && DECL_EXTERNAL (decl) |
866 | && DECL_INLINE_VAR_P (decl) |
867 | && DECL_ODR_USED (decl)) |
868 | error_at (DECL_SOURCE_LOCATION (decl), |
869 | "odr-used inline variable %qD is not defined" , decl); |
870 | } |
871 | |
872 | /* Clear out the list, so we don't rescan next time. */ |
873 | static_decls = NULL; |
874 | |
875 | /* Write out any globals that need to be output. */ |
876 | return wrapup_global_declarations (statics->address (), |
877 | statics->length ()); |
878 | } |
879 | return 0; |
880 | } |
881 | |
882 | /* In C++, you don't have to write `struct S' to refer to `S'; you |
883 | can just use `S'. We accomplish this by creating a TYPE_DECL as |
884 | if the user had written `typedef struct S S'. Create and return |
885 | the TYPE_DECL for TYPE. */ |
886 | |
887 | tree |
888 | create_implicit_typedef (tree name, tree type) |
889 | { |
890 | tree decl; |
891 | |
892 | decl = build_decl (input_location, TYPE_DECL, name, type); |
893 | DECL_ARTIFICIAL (decl) = 1; |
894 | /* There are other implicit type declarations, like the one *within* |
895 | a class that allows you to write `S::S'. We must distinguish |
896 | amongst these. */ |
897 | SET_DECL_IMPLICIT_TYPEDEF_P (decl); |
898 | TYPE_NAME (type) = decl; |
899 | TYPE_STUB_DECL (type) = decl; |
900 | |
901 | return decl; |
902 | } |
903 | |
904 | /* Function-scope local entities that need discriminators. Each entry |
905 | is a {decl,name} pair. VAR_DECLs for anon unions get their name |
906 | smashed, so we cannot rely on DECL_NAME. */ |
907 | |
908 | static GTY((deletable)) vec<tree, va_gc> *local_entities; |
909 | |
910 | /* Determine the mangling discriminator of local DECL. There are |
911 | generally very few of these in any particular function. */ |
912 | |
913 | void |
914 | determine_local_discriminator (tree decl, tree name) |
915 | { |
916 | auto_cond_timevar tv (TV_NAME_LOOKUP); |
917 | retrofit_lang_decl (decl); |
918 | tree ctx = DECL_CONTEXT (decl); |
919 | size_t nelts = vec_safe_length (v: local_entities); |
920 | if (name == NULL_TREE) |
921 | name = (TREE_CODE (decl) == TYPE_DECL |
922 | && TYPE_UNNAMED_P (TREE_TYPE (decl)) |
923 | ? NULL_TREE : DECL_NAME (decl)); |
924 | for (size_t i = 0; i < nelts; i += 2) |
925 | { |
926 | tree *pair = &(*local_entities)[i]; |
927 | tree d = pair[0]; |
928 | tree n = pair[1]; |
929 | gcc_checking_assert (d != decl); |
930 | if (name == n |
931 | && TREE_CODE (decl) == TREE_CODE (d) |
932 | && ctx == DECL_CONTEXT (d)) |
933 | { |
934 | tree disc = integer_one_node; |
935 | if (DECL_DISCRIMINATOR (d)) |
936 | disc = build_int_cst (TREE_TYPE (disc), |
937 | TREE_INT_CST_LOW (DECL_DISCRIMINATOR (d)) + 1); |
938 | DECL_DISCRIMINATOR (decl) = disc; |
939 | /* Replace the saved decl. */ |
940 | pair[0] = decl; |
941 | decl = NULL_TREE; |
942 | break; |
943 | } |
944 | } |
945 | |
946 | if (decl) |
947 | { |
948 | vec_safe_reserve (v&: local_entities, nelems: 2); |
949 | local_entities->quick_push (obj: decl); |
950 | local_entities->quick_push (obj: name); |
951 | } |
952 | } |
953 | |
954 | |
955 | /* True if DECL is a constrained hidden friend as per [temp.friend]/9: |
956 | |
957 | A non-template friend declaration with a requires-clause shall be a |
958 | definition. A friend function template with a constraint that depends on a |
959 | template parameter from an enclosing template shall be a definition. Such a |
960 | constrained friend function or function template declaration does not |
961 | declare the same function or function template as a declaration in any other |
962 | scope. |
963 | |
964 | The ABI calls this a "member-like constrained friend" and mangles it like a |
965 | member function to avoid collisions. */ |
966 | |
967 | bool |
968 | member_like_constrained_friend_p (tree decl) |
969 | { |
970 | return (TREE_CODE (decl) == FUNCTION_DECL |
971 | && DECL_UNIQUE_FRIEND_P (decl) |
972 | && DECL_FRIEND_CONTEXT (decl) |
973 | && get_constraints (decl) |
974 | && (!DECL_TEMPLATE_INFO (decl) |
975 | || !PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)) |
976 | || (uses_outer_template_parms_in_constraints |
977 | (most_general_template (decl))))); |
978 | } |
979 | |
980 | /* Returns true if functions FN1 and FN2 have equivalent trailing |
981 | requires clauses. */ |
982 | |
983 | static bool |
984 | function_requirements_equivalent_p (tree newfn, tree oldfn) |
985 | { |
986 | /* In the concepts TS, the combined constraints are compared. */ |
987 | if (cxx_dialect < cxx20) |
988 | { |
989 | tree ci1 = get_constraints (oldfn); |
990 | tree ci2 = get_constraints (newfn); |
991 | tree req1 = ci1 ? CI_ASSOCIATED_CONSTRAINTS (ci1) : NULL_TREE; |
992 | tree req2 = ci2 ? CI_ASSOCIATED_CONSTRAINTS (ci2) : NULL_TREE; |
993 | return cp_tree_equal (req1, req2); |
994 | } |
995 | |
996 | /* [temp.friend]/9 "Such a constrained friend function does not declare the |
997 | same function as a declaration in any other scope." So no need to |
998 | actually compare the requirements. */ |
999 | if (member_like_constrained_friend_p (decl: newfn) |
1000 | || member_like_constrained_friend_p (decl: oldfn)) |
1001 | return false; |
1002 | |
1003 | /* Compare only trailing requirements. */ |
1004 | tree reqs1 = get_trailing_function_requirements (newfn); |
1005 | tree reqs2 = get_trailing_function_requirements (oldfn); |
1006 | if ((reqs1 != NULL_TREE) != (reqs2 != NULL_TREE)) |
1007 | return false; |
1008 | |
1009 | /* Substitution is needed when friends are involved. */ |
1010 | reqs1 = maybe_substitute_reqs_for (reqs1, newfn); |
1011 | reqs2 = maybe_substitute_reqs_for (reqs2, oldfn); |
1012 | |
1013 | return cp_tree_equal (reqs1, reqs2); |
1014 | } |
1015 | |
1016 | /* Two functions of the same name correspond [basic.scope.scope] if |
1017 | |
1018 | + both declare functions with the same non-object-parameter-type-list, |
1019 | equivalent ([temp.over.link]) trailing requires-clauses (if any, except as |
1020 | specified in [temp.friend]), and, if both are non-static members, they have |
1021 | corresponding object parameters, or |
1022 | |
1023 | + both declare function templates with equivalent |
1024 | non-object-parameter-type-lists, return types (if any), template-heads, and |
1025 | trailing requires-clauses (if any), and, if both are non-static members, |
1026 | they have corresponding object parameters. |
1027 | |
1028 | This is a subset of decls_match: it identifies declarations that cannot be |
1029 | overloaded with one another. This function does not consider DECL_NAME. */ |
1030 | |
1031 | bool |
1032 | fns_correspond (tree newdecl, tree olddecl) |
1033 | { |
1034 | if (TREE_CODE (newdecl) != TREE_CODE (olddecl)) |
1035 | return false; |
1036 | |
1037 | if (TREE_CODE (newdecl) == TEMPLATE_DECL) |
1038 | { |
1039 | if (!template_heads_equivalent_p (newdecl, olddecl)) |
1040 | return 0; |
1041 | newdecl = DECL_TEMPLATE_RESULT (newdecl); |
1042 | olddecl = DECL_TEMPLATE_RESULT (olddecl); |
1043 | } |
1044 | |
1045 | tree f1 = TREE_TYPE (newdecl); |
1046 | tree f2 = TREE_TYPE (olddecl); |
1047 | |
1048 | int rq1 = type_memfn_rqual (f1); |
1049 | int rq2 = type_memfn_rqual (f2); |
1050 | |
1051 | /* If only one is a non-static member function, ignore ref-quals. */ |
1052 | if (TREE_CODE (f1) != TREE_CODE (f2)) |
1053 | rq1 = rq2; |
1054 | /* Two non-static member functions have corresponding object parameters if: |
1055 | + exactly one is an implicit object member function with no ref-qualifier |
1056 | and the types of their object parameters ([dcl.fct]), after removing |
1057 | top-level references, are the same, or |
1058 | + their object parameters have the same type. */ |
1059 | /* ??? We treat member functions of different classes as corresponding even |
1060 | though that means the object parameters have different types. */ |
1061 | else if ((rq1 == REF_QUAL_NONE) != (rq2 == REF_QUAL_NONE)) |
1062 | rq1 = rq2; |
1063 | |
1064 | bool types_match = rq1 == rq2; |
1065 | |
1066 | if (types_match) |
1067 | { |
1068 | tree p1 = FUNCTION_FIRST_USER_PARMTYPE (newdecl); |
1069 | tree p2 = FUNCTION_FIRST_USER_PARMTYPE (olddecl); |
1070 | types_match = compparms (p1, p2); |
1071 | } |
1072 | |
1073 | /* Two function declarations match if either has a requires-clause |
1074 | then both have a requires-clause and their constraints-expressions |
1075 | are equivalent. */ |
1076 | if (types_match && flag_concepts) |
1077 | types_match = function_requirements_equivalent_p (newfn: newdecl, oldfn: olddecl); |
1078 | |
1079 | return types_match; |
1080 | } |
1081 | |
1082 | /* Subroutine of duplicate_decls: return truthvalue of whether |
1083 | or not types of these decls match. |
1084 | |
1085 | For C++, we must compare the parameter list so that `int' can match |
1086 | `int&' in a parameter position, but `int&' is not confused with |
1087 | `const int&'. */ |
1088 | |
1089 | int |
1090 | decls_match (tree newdecl, tree olddecl, bool record_versions /* = true */) |
1091 | { |
1092 | int types_match; |
1093 | |
1094 | if (newdecl == olddecl) |
1095 | return 1; |
1096 | |
1097 | if (TREE_CODE (newdecl) != TREE_CODE (olddecl)) |
1098 | /* If the two DECLs are not even the same kind of thing, we're not |
1099 | interested in their types. */ |
1100 | return 0; |
1101 | |
1102 | gcc_assert (DECL_P (newdecl)); |
1103 | |
1104 | if (TREE_CODE (newdecl) == FUNCTION_DECL) |
1105 | { |
1106 | /* Specializations of different templates are different functions |
1107 | even if they have the same type. */ |
1108 | tree t1 = (DECL_USE_TEMPLATE (newdecl) |
1109 | ? DECL_TI_TEMPLATE (newdecl) |
1110 | : NULL_TREE); |
1111 | tree t2 = (DECL_USE_TEMPLATE (olddecl) |
1112 | ? DECL_TI_TEMPLATE (olddecl) |
1113 | : NULL_TREE); |
1114 | if (t1 != t2) |
1115 | return 0; |
1116 | |
1117 | if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl) |
1118 | && ! (DECL_EXTERN_C_P (newdecl) |
1119 | && DECL_EXTERN_C_P (olddecl))) |
1120 | return 0; |
1121 | |
1122 | /* A new declaration doesn't match a built-in one unless it |
1123 | is also extern "C". */ |
1124 | if (DECL_IS_UNDECLARED_BUILTIN (olddecl) |
1125 | && DECL_EXTERN_C_P (olddecl) && !DECL_EXTERN_C_P (newdecl)) |
1126 | return 0; |
1127 | |
1128 | tree f1 = TREE_TYPE (newdecl); |
1129 | tree f2 = TREE_TYPE (olddecl); |
1130 | if (TREE_CODE (f1) != TREE_CODE (f2)) |
1131 | return 0; |
1132 | |
1133 | /* A declaration with deduced return type should use its pre-deduction |
1134 | type for declaration matching. */ |
1135 | tree r2 = fndecl_declared_return_type (olddecl); |
1136 | tree r1 = fndecl_declared_return_type (newdecl); |
1137 | |
1138 | tree p1 = TYPE_ARG_TYPES (f1); |
1139 | tree p2 = TYPE_ARG_TYPES (f2); |
1140 | |
1141 | if (same_type_p (r1, r2)) |
1142 | { |
1143 | if (!prototype_p (f2) && DECL_EXTERN_C_P (olddecl) |
1144 | && fndecl_built_in_p (node: olddecl)) |
1145 | { |
1146 | types_match = self_promoting_args_p (p1); |
1147 | if (p1 == void_list_node) |
1148 | TREE_TYPE (newdecl) = TREE_TYPE (olddecl); |
1149 | } |
1150 | else |
1151 | types_match = |
1152 | compparms (p1, p2) |
1153 | && type_memfn_rqual (f1) == type_memfn_rqual (f2) |
1154 | && (TYPE_ATTRIBUTES (TREE_TYPE (newdecl)) == NULL_TREE |
1155 | || comp_type_attributes (TREE_TYPE (newdecl), |
1156 | TREE_TYPE (olddecl)) != 0); |
1157 | } |
1158 | else |
1159 | types_match = 0; |
1160 | |
1161 | /* Two function declarations match if either has a requires-clause |
1162 | then both have a requires-clause and their constraints-expressions |
1163 | are equivalent. */ |
1164 | if (types_match && flag_concepts) |
1165 | types_match = function_requirements_equivalent_p (newfn: newdecl, oldfn: olddecl); |
1166 | |
1167 | /* The decls dont match if they correspond to two different versions |
1168 | of the same function. Disallow extern "C" functions to be |
1169 | versions for now. */ |
1170 | if (types_match |
1171 | && !DECL_EXTERN_C_P (newdecl) |
1172 | && !DECL_EXTERN_C_P (olddecl) |
1173 | && targetm.target_option.function_versions (newdecl, olddecl)) |
1174 | { |
1175 | if (record_versions) |
1176 | maybe_version_functions (newdecl, olddecl, |
1177 | (!DECL_FUNCTION_VERSIONED (newdecl) |
1178 | || !DECL_FUNCTION_VERSIONED (olddecl))); |
1179 | return 0; |
1180 | } |
1181 | } |
1182 | else if (TREE_CODE (newdecl) == TEMPLATE_DECL) |
1183 | { |
1184 | if (!template_heads_equivalent_p (newdecl, olddecl)) |
1185 | return 0; |
1186 | |
1187 | tree oldres = DECL_TEMPLATE_RESULT (olddecl); |
1188 | tree newres = DECL_TEMPLATE_RESULT (newdecl); |
1189 | |
1190 | if (TREE_CODE (newres) != TREE_CODE (oldres)) |
1191 | return 0; |
1192 | |
1193 | /* Two template types match if they are the same. Otherwise, compare |
1194 | the underlying declarations. */ |
1195 | if (TREE_CODE (newres) == TYPE_DECL) |
1196 | types_match = same_type_p (TREE_TYPE (newres), TREE_TYPE (oldres)); |
1197 | else |
1198 | types_match = decls_match (newdecl: newres, olddecl: oldres); |
1199 | } |
1200 | else |
1201 | { |
1202 | /* Need to check scope for variable declaration (VAR_DECL). |
1203 | For typedef (TYPE_DECL), scope is ignored. */ |
1204 | if (VAR_P (newdecl) |
1205 | && CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl) |
1206 | /* [dcl.link] |
1207 | Two declarations for an object with C language linkage |
1208 | with the same name (ignoring the namespace that qualify |
1209 | it) that appear in different namespace scopes refer to |
1210 | the same object. */ |
1211 | && !(DECL_EXTERN_C_P (olddecl) && DECL_EXTERN_C_P (newdecl))) |
1212 | return 0; |
1213 | |
1214 | if (TREE_TYPE (newdecl) == error_mark_node) |
1215 | types_match = TREE_TYPE (olddecl) == error_mark_node; |
1216 | else if (TREE_TYPE (olddecl) == NULL_TREE) |
1217 | types_match = TREE_TYPE (newdecl) == NULL_TREE; |
1218 | else if (TREE_TYPE (newdecl) == NULL_TREE) |
1219 | types_match = 0; |
1220 | else |
1221 | types_match = comptypes (TREE_TYPE (newdecl), |
1222 | TREE_TYPE (olddecl), |
1223 | COMPARE_REDECLARATION); |
1224 | } |
1225 | |
1226 | return types_match; |
1227 | } |
1228 | |
1229 | /* Mark DECL as versioned if it isn't already. */ |
1230 | |
1231 | static void |
1232 | maybe_mark_function_versioned (tree decl) |
1233 | { |
1234 | if (!DECL_FUNCTION_VERSIONED (decl)) |
1235 | { |
1236 | DECL_FUNCTION_VERSIONED (decl) = 1; |
1237 | /* If DECL_ASSEMBLER_NAME has already been set, re-mangle |
1238 | to include the version marker. */ |
1239 | if (DECL_ASSEMBLER_NAME_SET_P (decl)) |
1240 | mangle_decl (decl); |
1241 | } |
1242 | } |
1243 | |
1244 | /* NEWDECL and OLDDECL have identical signatures. If they are |
1245 | different versions adjust them and return true. |
1246 | If RECORD is set to true, record function versions. */ |
1247 | |
1248 | bool |
1249 | maybe_version_functions (tree newdecl, tree olddecl, bool record) |
1250 | { |
1251 | if (!targetm.target_option.function_versions (newdecl, olddecl)) |
1252 | return false; |
1253 | |
1254 | maybe_mark_function_versioned (decl: olddecl); |
1255 | if (DECL_LOCAL_DECL_P (olddecl)) |
1256 | { |
1257 | olddecl = DECL_LOCAL_DECL_ALIAS (olddecl); |
1258 | maybe_mark_function_versioned (decl: olddecl); |
1259 | } |
1260 | |
1261 | maybe_mark_function_versioned (decl: newdecl); |
1262 | if (DECL_LOCAL_DECL_P (newdecl)) |
1263 | { |
1264 | /* Unfortunately, we can get here before pushdecl naturally calls |
1265 | push_local_extern_decl_alias, so we need to call it directly. */ |
1266 | if (!DECL_LOCAL_DECL_ALIAS (newdecl)) |
1267 | push_local_extern_decl_alias (decl: newdecl); |
1268 | newdecl = DECL_LOCAL_DECL_ALIAS (newdecl); |
1269 | maybe_mark_function_versioned (decl: newdecl); |
1270 | } |
1271 | |
1272 | if (record) |
1273 | cgraph_node::record_function_versions (decl1: olddecl, decl2: newdecl); |
1274 | |
1275 | return true; |
1276 | } |
1277 | |
1278 | /* If NEWDECL is `static' and an `extern' was seen previously, |
1279 | warn about it. OLDDECL is the previous declaration. |
1280 | |
1281 | Note that this does not apply to the C++ case of declaring |
1282 | a variable `extern const' and then later `const'. |
1283 | |
1284 | Don't complain about built-in functions, since they are beyond |
1285 | the user's control. */ |
1286 | |
1287 | void |
1288 | warn_extern_redeclared_static (tree newdecl, tree olddecl) |
1289 | { |
1290 | if (TREE_CODE (newdecl) == TYPE_DECL |
1291 | || TREE_CODE (newdecl) == TEMPLATE_DECL |
1292 | || TREE_CODE (newdecl) == CONST_DECL |
1293 | || TREE_CODE (newdecl) == NAMESPACE_DECL) |
1294 | return; |
1295 | |
1296 | /* Don't get confused by static member functions; that's a different |
1297 | use of `static'. */ |
1298 | if (TREE_CODE (newdecl) == FUNCTION_DECL |
1299 | && DECL_STATIC_FUNCTION_P (newdecl)) |
1300 | return; |
1301 | |
1302 | /* If the old declaration was `static', or the new one isn't, then |
1303 | everything is OK. */ |
1304 | if (DECL_THIS_STATIC (olddecl) || !DECL_THIS_STATIC (newdecl)) |
1305 | return; |
1306 | |
1307 | /* It's OK to declare a builtin function as `static'. */ |
1308 | if (TREE_CODE (olddecl) == FUNCTION_DECL |
1309 | && DECL_ARTIFICIAL (olddecl)) |
1310 | return; |
1311 | |
1312 | auto_diagnostic_group d; |
1313 | if (permerror (DECL_SOURCE_LOCATION (newdecl), |
1314 | "%qD was declared %<extern%> and later %<static%>" , newdecl)) |
1315 | inform (DECL_SOURCE_LOCATION (olddecl), |
1316 | "previous declaration of %qD" , olddecl); |
1317 | } |
1318 | |
1319 | /* NEW_DECL is a redeclaration of OLD_DECL; both are functions or |
1320 | function templates. If their exception specifications do not |
1321 | match, issue a diagnostic. */ |
1322 | |
1323 | static void |
1324 | check_redeclaration_exception_specification (tree new_decl, |
1325 | tree old_decl) |
1326 | { |
1327 | tree new_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (new_decl)); |
1328 | tree old_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old_decl)); |
1329 | |
1330 | /* Two default specs are equivalent, don't force evaluation. */ |
1331 | if (UNEVALUATED_NOEXCEPT_SPEC_P (new_exceptions) |
1332 | && UNEVALUATED_NOEXCEPT_SPEC_P (old_exceptions)) |
1333 | return; |
1334 | |
1335 | if (!type_dependent_expression_p (old_decl)) |
1336 | { |
1337 | maybe_instantiate_noexcept (new_decl); |
1338 | maybe_instantiate_noexcept (old_decl); |
1339 | } |
1340 | new_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (new_decl)); |
1341 | old_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old_decl)); |
1342 | |
1343 | /* [except.spec] |
1344 | |
1345 | If any declaration of a function has an exception-specification, |
1346 | all declarations, including the definition and an explicit |
1347 | specialization, of that function shall have an |
1348 | exception-specification with the same set of type-ids. */ |
1349 | if (!DECL_IS_UNDECLARED_BUILTIN (old_decl) |
1350 | && !DECL_IS_UNDECLARED_BUILTIN (new_decl) |
1351 | && !comp_except_specs (new_exceptions, old_exceptions, ce_normal)) |
1352 | { |
1353 | const char *const msg |
1354 | = G_("declaration of %qF has a different exception specifier" ); |
1355 | bool complained = true; |
1356 | location_t new_loc = DECL_SOURCE_LOCATION (new_decl); |
1357 | auto_diagnostic_group d; |
1358 | if (DECL_IN_SYSTEM_HEADER (old_decl)) |
1359 | complained = pedwarn (new_loc, OPT_Wsystem_headers, msg, new_decl); |
1360 | else if (!flag_exceptions) |
1361 | /* We used to silently permit mismatched eh specs with |
1362 | -fno-exceptions, so make them a pedwarn now. */ |
1363 | complained = pedwarn (new_loc, OPT_Wpedantic, msg, new_decl); |
1364 | else |
1365 | error_at (new_loc, msg, new_decl); |
1366 | if (complained) |
1367 | inform (DECL_SOURCE_LOCATION (old_decl), |
1368 | "from previous declaration %qF" , old_decl); |
1369 | } |
1370 | } |
1371 | |
1372 | /* Return true if OLD_DECL and NEW_DECL agree on constexprness. |
1373 | Otherwise issue diagnostics. */ |
1374 | |
1375 | static bool |
1376 | validate_constexpr_redeclaration (tree old_decl, tree new_decl) |
1377 | { |
1378 | old_decl = STRIP_TEMPLATE (old_decl); |
1379 | new_decl = STRIP_TEMPLATE (new_decl); |
1380 | if (!VAR_OR_FUNCTION_DECL_P (old_decl) |
1381 | || !VAR_OR_FUNCTION_DECL_P (new_decl)) |
1382 | return true; |
1383 | if (DECL_DECLARED_CONSTEXPR_P (old_decl) |
1384 | == DECL_DECLARED_CONSTEXPR_P (new_decl)) |
1385 | { |
1386 | if (TREE_CODE (old_decl) != FUNCTION_DECL) |
1387 | return true; |
1388 | if (DECL_IMMEDIATE_FUNCTION_P (old_decl) |
1389 | == DECL_IMMEDIATE_FUNCTION_P (new_decl)) |
1390 | return true; |
1391 | } |
1392 | if (TREE_CODE (old_decl) == FUNCTION_DECL) |
1393 | { |
1394 | /* With -fimplicit-constexpr, ignore changes in the constexpr |
1395 | keyword. */ |
1396 | if (flag_implicit_constexpr |
1397 | && (DECL_IMMEDIATE_FUNCTION_P (new_decl) |
1398 | == DECL_IMMEDIATE_FUNCTION_P (old_decl))) |
1399 | return true; |
1400 | if (fndecl_built_in_p (node: old_decl)) |
1401 | { |
1402 | /* Hide a built-in declaration. */ |
1403 | DECL_DECLARED_CONSTEXPR_P (old_decl) |
1404 | = DECL_DECLARED_CONSTEXPR_P (new_decl); |
1405 | if (DECL_IMMEDIATE_FUNCTION_P (new_decl)) |
1406 | SET_DECL_IMMEDIATE_FUNCTION_P (old_decl); |
1407 | return true; |
1408 | } |
1409 | /* 7.1.5 [dcl.constexpr] |
1410 | Note: An explicit specialization can differ from the template |
1411 | declaration with respect to the constexpr specifier. */ |
1412 | if (! DECL_TEMPLATE_SPECIALIZATION (old_decl) |
1413 | && DECL_TEMPLATE_SPECIALIZATION (new_decl)) |
1414 | return true; |
1415 | |
1416 | const char *kind = "constexpr" ; |
1417 | if (DECL_IMMEDIATE_FUNCTION_P (old_decl) |
1418 | || DECL_IMMEDIATE_FUNCTION_P (new_decl)) |
1419 | kind = "consteval" ; |
1420 | error_at (DECL_SOURCE_LOCATION (new_decl), |
1421 | "redeclaration %qD differs in %qs " |
1422 | "from previous declaration" , new_decl, |
1423 | kind); |
1424 | inform (DECL_SOURCE_LOCATION (old_decl), |
1425 | "previous declaration %qD" , old_decl); |
1426 | return false; |
1427 | } |
1428 | return true; |
1429 | } |
1430 | |
1431 | // If OLDDECL and NEWDECL are concept declarations with the same type |
1432 | // (i.e., and template parameters), but different requirements, |
1433 | // emit diagnostics and return true. Otherwise, return false. |
1434 | static inline bool |
1435 | check_concept_refinement (tree olddecl, tree newdecl) |
1436 | { |
1437 | if (!DECL_DECLARED_CONCEPT_P (olddecl) || !DECL_DECLARED_CONCEPT_P (newdecl)) |
1438 | return false; |
1439 | |
1440 | tree d1 = DECL_TEMPLATE_RESULT (olddecl); |
1441 | tree d2 = DECL_TEMPLATE_RESULT (newdecl); |
1442 | if (TREE_CODE (d1) != TREE_CODE (d2)) |
1443 | return false; |
1444 | |
1445 | tree t1 = TREE_TYPE (d1); |
1446 | tree t2 = TREE_TYPE (d2); |
1447 | if (TREE_CODE (d1) == FUNCTION_DECL) |
1448 | { |
1449 | if (compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)) |
1450 | && comp_template_parms (DECL_TEMPLATE_PARMS (olddecl), |
1451 | DECL_TEMPLATE_PARMS (newdecl)) |
1452 | && !equivalently_constrained (olddecl, newdecl)) |
1453 | { |
1454 | error ("cannot specialize concept %q#D" , olddecl); |
1455 | return true; |
1456 | } |
1457 | } |
1458 | return false; |
1459 | } |
1460 | |
1461 | /* DECL is a redeclaration of a function or function template. If |
1462 | it does have default arguments issue a diagnostic. Note: this |
1463 | function is used to enforce the requirements in C++11 8.3.6 about |
1464 | no default arguments in redeclarations. */ |
1465 | |
1466 | static void |
1467 | check_redeclaration_no_default_args (tree decl) |
1468 | { |
1469 | gcc_assert (DECL_DECLARES_FUNCTION_P (decl)); |
1470 | |
1471 | for (tree t = FUNCTION_FIRST_USER_PARMTYPE (decl); |
1472 | t && t != void_list_node; t = TREE_CHAIN (t)) |
1473 | if (TREE_PURPOSE (t)) |
1474 | { |
1475 | permerror (DECL_SOURCE_LOCATION (decl), |
1476 | "redeclaration of %q#D may not have default " |
1477 | "arguments" , decl); |
1478 | return; |
1479 | } |
1480 | } |
1481 | |
1482 | /* NEWDECL is a redeclaration of a function or function template OLDDECL, |
1483 | in any case represented as FUNCTION_DECLs (the DECL_TEMPLATE_RESULTs of |
1484 | the TEMPLATE_DECLs in case of function templates). This function is used |
1485 | to enforce the final part of C++17 11.3.6/4, about a single declaration: |
1486 | "If a friend declaration specifies a default argument expression, that |
1487 | declaration shall be a definition and shall be the only declaration of |
1488 | the function or function template in the translation unit." */ |
1489 | |
1490 | static void |
1491 | check_no_redeclaration_friend_default_args (tree olddecl, tree newdecl) |
1492 | { |
1493 | if (!DECL_UNIQUE_FRIEND_P (olddecl) && !DECL_UNIQUE_FRIEND_P (newdecl)) |
1494 | return; |
1495 | |
1496 | for (tree t1 = FUNCTION_FIRST_USER_PARMTYPE (olddecl), |
1497 | t2 = FUNCTION_FIRST_USER_PARMTYPE (newdecl); |
1498 | t1 && t1 != void_list_node; |
1499 | t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2)) |
1500 | if ((DECL_UNIQUE_FRIEND_P (olddecl) && TREE_PURPOSE (t1)) |
1501 | || (DECL_UNIQUE_FRIEND_P (newdecl) && TREE_PURPOSE (t2))) |
1502 | { |
1503 | auto_diagnostic_group d; |
1504 | if (permerror (DECL_SOURCE_LOCATION (newdecl), |
1505 | "friend declaration of %q#D specifies default " |
1506 | "arguments and isn%'t the only declaration" , newdecl)) |
1507 | inform (DECL_SOURCE_LOCATION (olddecl), |
1508 | "previous declaration of %q#D" , olddecl); |
1509 | return; |
1510 | } |
1511 | } |
1512 | |
1513 | /* Merge tree bits that correspond to attributes noreturn, nothrow, |
1514 | const, malloc, and pure from NEWDECL with those of OLDDECL. */ |
1515 | |
1516 | static void |
1517 | merge_attribute_bits (tree newdecl, tree olddecl) |
1518 | { |
1519 | TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl); |
1520 | TREE_THIS_VOLATILE (olddecl) |= TREE_THIS_VOLATILE (newdecl); |
1521 | TREE_NOTHROW (newdecl) |= TREE_NOTHROW (olddecl); |
1522 | TREE_NOTHROW (olddecl) |= TREE_NOTHROW (newdecl); |
1523 | TREE_READONLY (newdecl) |= TREE_READONLY (olddecl); |
1524 | TREE_READONLY (olddecl) |= TREE_READONLY (newdecl); |
1525 | DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl); |
1526 | DECL_IS_MALLOC (olddecl) |= DECL_IS_MALLOC (newdecl); |
1527 | DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl); |
1528 | DECL_PURE_P (olddecl) |= DECL_PURE_P (newdecl); |
1529 | DECL_UNINLINABLE (newdecl) |= DECL_UNINLINABLE (olddecl); |
1530 | DECL_UNINLINABLE (olddecl) |= DECL_UNINLINABLE (newdecl); |
1531 | } |
1532 | |
1533 | #define GNU_INLINE_P(fn) (DECL_DECLARED_INLINE_P (fn) \ |
1534 | && lookup_attribute ("gnu_inline", \ |
1535 | DECL_ATTRIBUTES (fn))) |
1536 | |
1537 | /* A subroutine of duplicate_decls. Emits a diagnostic when newdecl |
1538 | ambiguates olddecl. Returns true if an error occurs. */ |
1539 | |
1540 | static bool |
1541 | duplicate_function_template_decls (tree newdecl, tree olddecl) |
1542 | { |
1543 | |
1544 | tree newres = DECL_TEMPLATE_RESULT (newdecl); |
1545 | tree oldres = DECL_TEMPLATE_RESULT (olddecl); |
1546 | /* Function template declarations can be differentiated by parameter |
1547 | and return type. */ |
1548 | if (compparms (TYPE_ARG_TYPES (TREE_TYPE (oldres)), |
1549 | TYPE_ARG_TYPES (TREE_TYPE (newres))) |
1550 | && same_type_p (TREE_TYPE (TREE_TYPE (newdecl)), |
1551 | TREE_TYPE (TREE_TYPE (olddecl)))) |
1552 | { |
1553 | /* ... and also by their template-heads and requires-clauses. */ |
1554 | if (template_heads_equivalent_p (newdecl, olddecl) |
1555 | && function_requirements_equivalent_p (newfn: newres, oldfn: oldres)) |
1556 | { |
1557 | error ("ambiguating new declaration %q+#D" , newdecl); |
1558 | inform (DECL_SOURCE_LOCATION (olddecl), |
1559 | "old declaration %q#D" , olddecl); |
1560 | return true; |
1561 | } |
1562 | |
1563 | /* FIXME: The types are the same but the are differences |
1564 | in either the template heads or function requirements. |
1565 | We should be able to diagnose a set of common errors |
1566 | stemming from these declarations. For example: |
1567 | |
1568 | template<typename T> requires C void f(...); |
1569 | template<typename T> void f(...) requires C; |
1570 | |
1571 | These are functionally equivalent but not equivalent. */ |
1572 | } |
1573 | |
1574 | return false; |
1575 | } |
1576 | |
1577 | /* OLD_PARMS is the innermost set of template parameters for some template |
1578 | declaration, and NEW_PARMS is the corresponding set of template parameters |
1579 | for a redeclaration of that template. Merge the default arguments within |
1580 | these two sets of parameters. CLASS_P is true iff the template in |
1581 | question is a class template. */ |
1582 | |
1583 | bool |
1584 | merge_default_template_args (tree new_parms, tree old_parms, bool class_p) |
1585 | { |
1586 | gcc_checking_assert (TREE_VEC_LENGTH (new_parms) |
1587 | == TREE_VEC_LENGTH (old_parms)); |
1588 | for (int i = 0; i < TREE_VEC_LENGTH (new_parms); i++) |
1589 | { |
1590 | tree new_parm = TREE_VALUE (TREE_VEC_ELT (new_parms, i)); |
1591 | tree old_parm = TREE_VALUE (TREE_VEC_ELT (old_parms, i)); |
1592 | tree& new_default = TREE_PURPOSE (TREE_VEC_ELT (new_parms, i)); |
1593 | tree& old_default = TREE_PURPOSE (TREE_VEC_ELT (old_parms, i)); |
1594 | if (error_operand_p (t: new_parm) || error_operand_p (t: old_parm)) |
1595 | return false; |
1596 | if (new_default != NULL_TREE && old_default != NULL_TREE) |
1597 | { |
1598 | auto_diagnostic_group d; |
1599 | error ("redefinition of default argument for %q+#D" , new_parm); |
1600 | inform (DECL_SOURCE_LOCATION (old_parm), |
1601 | "original definition appeared here" ); |
1602 | return false; |
1603 | } |
1604 | else if (new_default != NULL_TREE) |
1605 | /* Update the previous template parameters (which are the ones |
1606 | that will really count) with the new default value. */ |
1607 | old_default = new_default; |
1608 | else if (class_p && old_default != NULL_TREE) |
1609 | /* Update the new parameters, too; they'll be used as the |
1610 | parameters for any members. */ |
1611 | new_default = old_default; |
1612 | } |
1613 | return true; |
1614 | } |
1615 | |
1616 | /* If NEWDECL is a redeclaration of OLDDECL, merge the declarations. |
1617 | If the redeclaration is invalid, a diagnostic is issued, and the |
1618 | error_mark_node is returned. Otherwise, OLDDECL is returned. |
1619 | |
1620 | If NEWDECL is not a redeclaration of OLDDECL, NULL_TREE is |
1621 | returned. |
1622 | |
1623 | HIDING is true if the new decl is being hidden. WAS_HIDDEN is true |
1624 | if the old decl was hidden. |
1625 | |
1626 | Hidden decls can be anticipated builtins, injected friends, or |
1627 | (coming soon) injected from a local-extern decl. */ |
1628 | |
1629 | tree |
1630 | duplicate_decls (tree newdecl, tree olddecl, bool hiding, bool was_hidden) |
1631 | { |
1632 | unsigned olddecl_uid = DECL_UID (olddecl); |
1633 | int types_match = 0; |
1634 | int new_defines_function = 0; |
1635 | tree new_template_info; |
1636 | location_t olddecl_loc = DECL_SOURCE_LOCATION (olddecl); |
1637 | location_t newdecl_loc = DECL_SOURCE_LOCATION (newdecl); |
1638 | |
1639 | if (newdecl == olddecl) |
1640 | return olddecl; |
1641 | |
1642 | types_match = decls_match (newdecl, olddecl); |
1643 | |
1644 | /* If either the type of the new decl or the type of the old decl is an |
1645 | error_mark_node, then that implies that we have already issued an |
1646 | error (earlier) for some bogus type specification, and in that case, |
1647 | it is rather pointless to harass the user with yet more error message |
1648 | about the same declaration, so just pretend the types match here. */ |
1649 | if (TREE_TYPE (newdecl) == error_mark_node |
1650 | || TREE_TYPE (olddecl) == error_mark_node) |
1651 | return error_mark_node; |
1652 | |
1653 | /* Check for redeclaration and other discrepancies. */ |
1654 | if (TREE_CODE (olddecl) == FUNCTION_DECL |
1655 | && DECL_IS_UNDECLARED_BUILTIN (olddecl)) |
1656 | { |
1657 | if (TREE_CODE (newdecl) != FUNCTION_DECL) |
1658 | { |
1659 | /* Avoid warnings redeclaring built-ins which have not been |
1660 | explicitly declared. */ |
1661 | if (was_hidden) |
1662 | { |
1663 | if (TREE_PUBLIC (newdecl) |
1664 | && CP_DECL_CONTEXT (newdecl) == global_namespace) |
1665 | warning_at (newdecl_loc, |
1666 | OPT_Wbuiltin_declaration_mismatch, |
1667 | "built-in function %qD declared as non-function" , |
1668 | newdecl); |
1669 | return NULL_TREE; |
1670 | } |
1671 | |
1672 | /* If you declare a built-in or predefined function name as static, |
1673 | the old definition is overridden, but optionally warn this was a |
1674 | bad choice of name. */ |
1675 | if (! TREE_PUBLIC (newdecl)) |
1676 | { |
1677 | warning_at (newdecl_loc, |
1678 | OPT_Wshadow, |
1679 | fndecl_built_in_p (node: olddecl) |
1680 | ? G_("shadowing built-in function %q#D" ) |
1681 | : G_("shadowing library function %q#D" ), olddecl); |
1682 | /* Discard the old built-in function. */ |
1683 | return NULL_TREE; |
1684 | } |
1685 | /* If the built-in is not ansi, then programs can override |
1686 | it even globally without an error. */ |
1687 | else if (! fndecl_built_in_p (node: olddecl)) |
1688 | warning_at (newdecl_loc, 0, |
1689 | "library function %q#D redeclared as non-function %q#D" , |
1690 | olddecl, newdecl); |
1691 | else |
1692 | error_at (newdecl_loc, |
1693 | "declaration of %q#D conflicts with built-in " |
1694 | "declaration %q#D" , newdecl, olddecl); |
1695 | return NULL_TREE; |
1696 | } |
1697 | else if (!types_match) |
1698 | { |
1699 | /* Avoid warnings redeclaring built-ins which have not been |
1700 | explicitly declared. */ |
1701 | if (was_hidden) |
1702 | { |
1703 | tree t1, t2; |
1704 | |
1705 | /* A new declaration doesn't match a built-in one unless it |
1706 | is also extern "C". */ |
1707 | gcc_assert (DECL_IS_UNDECLARED_BUILTIN (olddecl)); |
1708 | gcc_assert (DECL_EXTERN_C_P (olddecl)); |
1709 | if (!DECL_EXTERN_C_P (newdecl)) |
1710 | return NULL_TREE; |
1711 | |
1712 | for (t1 = TYPE_ARG_TYPES (TREE_TYPE (newdecl)), |
1713 | t2 = TYPE_ARG_TYPES (TREE_TYPE (olddecl)); |
1714 | t1 || t2; |
1715 | t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2)) |
1716 | { |
1717 | if (!t1 || !t2) |
1718 | break; |
1719 | /* FILE, tm types are not known at the time |
1720 | we create the builtins. */ |
1721 | for (unsigned i = 0; |
1722 | i < sizeof (builtin_structptr_types) |
1723 | / sizeof (builtin_structptr_type); |
1724 | ++i) |
1725 | if (TREE_VALUE (t2) == builtin_structptr_types[i].node) |
1726 | { |
1727 | tree t = TREE_VALUE (t1); |
1728 | |
1729 | if (TYPE_PTR_P (t) |
1730 | && TYPE_IDENTIFIER (TREE_TYPE (t)) |
1731 | == get_identifier (builtin_structptr_types[i].str) |
1732 | && compparms (TREE_CHAIN (t1), TREE_CHAIN (t2))) |
1733 | { |
1734 | tree oldargs = TYPE_ARG_TYPES (TREE_TYPE (olddecl)); |
1735 | |
1736 | TYPE_ARG_TYPES (TREE_TYPE (olddecl)) |
1737 | = TYPE_ARG_TYPES (TREE_TYPE (newdecl)); |
1738 | types_match = decls_match (newdecl, olddecl); |
1739 | if (types_match) |
1740 | return duplicate_decls (newdecl, olddecl, |
1741 | hiding, was_hidden); |
1742 | TYPE_ARG_TYPES (TREE_TYPE (olddecl)) = oldargs; |
1743 | } |
1744 | goto next_arg; |
1745 | } |
1746 | |
1747 | if (! same_type_p (TREE_VALUE (t1), TREE_VALUE (t2))) |
1748 | break; |
1749 | next_arg:; |
1750 | } |
1751 | |
1752 | warning_at (newdecl_loc, |
1753 | OPT_Wbuiltin_declaration_mismatch, |
1754 | "declaration of %q#D conflicts with built-in " |
1755 | "declaration %q#D" , newdecl, olddecl); |
1756 | } |
1757 | else if ((DECL_EXTERN_C_P (newdecl) |
1758 | && DECL_EXTERN_C_P (olddecl)) |
1759 | || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)), |
1760 | TYPE_ARG_TYPES (TREE_TYPE (olddecl)))) |
1761 | { |
1762 | /* Don't really override olddecl for __* prefixed builtins |
1763 | except for __[^b]*_chk, the compiler might be using those |
1764 | explicitly. */ |
1765 | if (fndecl_built_in_p (node: olddecl)) |
1766 | { |
1767 | tree id = DECL_NAME (olddecl); |
1768 | const char *name = IDENTIFIER_POINTER (id); |
1769 | size_t len; |
1770 | |
1771 | if (name[0] == '_' |
1772 | && name[1] == '_' |
1773 | && (startswith (str: name + 2, prefix: "builtin_" ) |
1774 | || (len = strlen (s: name)) <= strlen (s: "___chk" ) |
1775 | || memcmp (s1: name + len - strlen (s: "_chk" ), |
1776 | s2: "_chk" , n: strlen (s: "_chk" ) + 1) != 0)) |
1777 | { |
1778 | if (DECL_INITIAL (newdecl)) |
1779 | { |
1780 | error_at (newdecl_loc, |
1781 | "definition of %q#D ambiguates built-in " |
1782 | "declaration %q#D" , newdecl, olddecl); |
1783 | return error_mark_node; |
1784 | } |
1785 | auto_diagnostic_group d; |
1786 | if (permerror (newdecl_loc, |
1787 | "new declaration %q#D ambiguates built-in" |
1788 | " declaration %q#D" , newdecl, olddecl) |
1789 | && flag_permissive) |
1790 | inform (newdecl_loc, |
1791 | "ignoring the %q#D declaration" , newdecl); |
1792 | return flag_permissive ? olddecl : error_mark_node; |
1793 | } |
1794 | } |
1795 | |
1796 | /* A near match; override the builtin. */ |
1797 | |
1798 | if (TREE_PUBLIC (newdecl)) |
1799 | warning_at (newdecl_loc, |
1800 | OPT_Wbuiltin_declaration_mismatch, |
1801 | "new declaration %q#D ambiguates built-in " |
1802 | "declaration %q#D" , newdecl, olddecl); |
1803 | else |
1804 | warning (OPT_Wshadow, |
1805 | fndecl_built_in_p (node: olddecl) |
1806 | ? G_("shadowing built-in function %q#D" ) |
1807 | : G_("shadowing library function %q#D" ), olddecl); |
1808 | } |
1809 | else |
1810 | /* Discard the old built-in function. */ |
1811 | return NULL_TREE; |
1812 | |
1813 | /* Replace the old RTL to avoid problems with inlining. */ |
1814 | COPY_DECL_RTL (newdecl, olddecl); |
1815 | } |
1816 | else |
1817 | { |
1818 | /* Even if the types match, prefer the new declarations type |
1819 | for built-ins which have not been explicitly declared, |
1820 | for exception lists, etc... */ |
1821 | tree type = TREE_TYPE (newdecl); |
1822 | tree attribs = (*targetm.merge_type_attributes) |
1823 | (TREE_TYPE (olddecl), type); |
1824 | |
1825 | type = cp_build_type_attribute_variant (type, attribs); |
1826 | TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = type; |
1827 | } |
1828 | |
1829 | /* If a function is explicitly declared "throw ()", propagate that to |
1830 | the corresponding builtin. */ |
1831 | if (DECL_BUILT_IN_CLASS (olddecl) == BUILT_IN_NORMAL |
1832 | && was_hidden |
1833 | && TREE_NOTHROW (newdecl) |
1834 | && !TREE_NOTHROW (olddecl)) |
1835 | { |
1836 | enum built_in_function fncode = DECL_FUNCTION_CODE (decl: olddecl); |
1837 | tree tmpdecl = builtin_decl_explicit (fncode); |
1838 | if (tmpdecl && tmpdecl != olddecl && types_match) |
1839 | TREE_NOTHROW (tmpdecl) = 1; |
1840 | } |
1841 | |
1842 | /* Whether or not the builtin can throw exceptions has no |
1843 | bearing on this declarator. */ |
1844 | TREE_NOTHROW (olddecl) = 0; |
1845 | |
1846 | if (DECL_THIS_STATIC (newdecl) && !DECL_THIS_STATIC (olddecl)) |
1847 | { |
1848 | /* If a builtin function is redeclared as `static', merge |
1849 | the declarations, but make the original one static. */ |
1850 | DECL_THIS_STATIC (olddecl) = 1; |
1851 | TREE_PUBLIC (olddecl) = 0; |
1852 | |
1853 | /* Make the old declaration consistent with the new one so |
1854 | that all remnants of the builtin-ness of this function |
1855 | will be banished. */ |
1856 | SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl)); |
1857 | COPY_DECL_RTL (newdecl, olddecl); |
1858 | } |
1859 | } |
1860 | else if (TREE_CODE (olddecl) != TREE_CODE (newdecl)) |
1861 | { |
1862 | /* C++ Standard, 3.3, clause 4: |
1863 | "[Note: a namespace name or a class template name must be unique |
1864 | in its declarative region (7.3.2, clause 14). ]" */ |
1865 | if (TREE_CODE (olddecl) == NAMESPACE_DECL |
1866 | || TREE_CODE (newdecl) == NAMESPACE_DECL) |
1867 | /* Namespace conflicts with not namespace. */; |
1868 | else if (DECL_TYPE_TEMPLATE_P (olddecl) |
1869 | || DECL_TYPE_TEMPLATE_P (newdecl)) |
1870 | /* Class template conflicts. */; |
1871 | else if ((TREE_CODE (olddecl) == TEMPLATE_DECL |
1872 | && DECL_TEMPLATE_RESULT (olddecl) |
1873 | && TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == VAR_DECL) |
1874 | || (TREE_CODE (newdecl) == TEMPLATE_DECL |
1875 | && DECL_TEMPLATE_RESULT (newdecl) |
1876 | && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == VAR_DECL)) |
1877 | /* Variable template conflicts. */; |
1878 | else if (concept_definition_p (t: olddecl) |
1879 | || concept_definition_p (t: newdecl)) |
1880 | /* Concept conflicts. */; |
1881 | else if ((TREE_CODE (newdecl) == FUNCTION_DECL |
1882 | && DECL_FUNCTION_TEMPLATE_P (olddecl)) |
1883 | || (TREE_CODE (olddecl) == FUNCTION_DECL |
1884 | && DECL_FUNCTION_TEMPLATE_P (newdecl))) |
1885 | { |
1886 | /* One is a function and the other is a template |
1887 | function. */ |
1888 | if (!UDLIT_OPER_P (DECL_NAME (newdecl))) |
1889 | return NULL_TREE; |
1890 | |
1891 | /* There can only be one! */ |
1892 | if (TREE_CODE (newdecl) == TEMPLATE_DECL |
1893 | && check_raw_literal_operator (decl: olddecl)) |
1894 | error_at (newdecl_loc, |
1895 | "literal operator %q#D conflicts with" |
1896 | " raw literal operator" , newdecl); |
1897 | else if (check_raw_literal_operator (decl: newdecl)) |
1898 | error_at (newdecl_loc, |
1899 | "raw literal operator %q#D conflicts with" |
1900 | " literal operator template" , newdecl); |
1901 | else |
1902 | return NULL_TREE; |
1903 | |
1904 | inform (olddecl_loc, "previous declaration %q#D" , olddecl); |
1905 | return error_mark_node; |
1906 | } |
1907 | else if ((VAR_P (olddecl) && DECL_DECOMPOSITION_P (olddecl)) |
1908 | || (VAR_P (newdecl) && DECL_DECOMPOSITION_P (newdecl))) |
1909 | /* A structured binding must be unique in its declarative region. */; |
1910 | else if (DECL_IMPLICIT_TYPEDEF_P (olddecl) |
1911 | || DECL_IMPLICIT_TYPEDEF_P (newdecl)) |
1912 | /* One is an implicit typedef, that's ok. */ |
1913 | return NULL_TREE; |
1914 | |
1915 | error ("%q#D redeclared as different kind of entity" , newdecl); |
1916 | inform (olddecl_loc, "previous declaration %q#D" , olddecl); |
1917 | |
1918 | return error_mark_node; |
1919 | } |
1920 | else if (!types_match) |
1921 | { |
1922 | if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)) |
1923 | /* These are certainly not duplicate declarations; they're |
1924 | from different scopes. */ |
1925 | return NULL_TREE; |
1926 | |
1927 | if (TREE_CODE (newdecl) == TEMPLATE_DECL) |
1928 | { |
1929 | tree oldres = DECL_TEMPLATE_RESULT (olddecl); |
1930 | tree newres = DECL_TEMPLATE_RESULT (newdecl); |
1931 | |
1932 | /* The name of a class template may not be declared to refer to |
1933 | any other template, class, function, object, namespace, value, |
1934 | or type in the same scope. */ |
1935 | if (TREE_CODE (oldres) == TYPE_DECL |
1936 | || TREE_CODE (newres) == TYPE_DECL) |
1937 | { |
1938 | error_at (newdecl_loc, |
1939 | "conflicting declaration of template %q#D" , newdecl); |
1940 | inform (olddecl_loc, |
1941 | "previous declaration %q#D" , olddecl); |
1942 | return error_mark_node; |
1943 | } |
1944 | |
1945 | else if (TREE_CODE (oldres) == FUNCTION_DECL |
1946 | && TREE_CODE (newres) == FUNCTION_DECL) |
1947 | { |
1948 | if (duplicate_function_template_decls (newdecl, olddecl)) |
1949 | return error_mark_node; |
1950 | return NULL_TREE; |
1951 | } |
1952 | else if (check_concept_refinement (olddecl, newdecl)) |
1953 | return error_mark_node; |
1954 | return NULL_TREE; |
1955 | } |
1956 | if (TREE_CODE (newdecl) == FUNCTION_DECL) |
1957 | { |
1958 | if (DECL_EXTERN_C_P (newdecl) && DECL_EXTERN_C_P (olddecl)) |
1959 | { |
1960 | error_at (newdecl_loc, |
1961 | "conflicting declaration of C function %q#D" , |
1962 | newdecl); |
1963 | inform (olddecl_loc, |
1964 | "previous declaration %q#D" , olddecl); |
1965 | return error_mark_node; |
1966 | } |
1967 | /* For function versions, params and types match, but they |
1968 | are not ambiguous. */ |
1969 | else if ((!DECL_FUNCTION_VERSIONED (newdecl) |
1970 | && !DECL_FUNCTION_VERSIONED (olddecl)) |
1971 | /* Let constrained hidden friends coexist for now, we'll |
1972 | check satisfaction later. */ |
1973 | && !member_like_constrained_friend_p (decl: newdecl) |
1974 | && !member_like_constrained_friend_p (decl: olddecl) |
1975 | // The functions have the same parameter types. |
1976 | && compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)), |
1977 | TYPE_ARG_TYPES (TREE_TYPE (olddecl))) |
1978 | // And the same constraints. |
1979 | && equivalently_constrained (newdecl, olddecl)) |
1980 | { |
1981 | error_at (newdecl_loc, |
1982 | "ambiguating new declaration of %q#D" , newdecl); |
1983 | inform (olddecl_loc, |
1984 | "old declaration %q#D" , olddecl); |
1985 | return error_mark_node; |
1986 | } |
1987 | else |
1988 | return NULL_TREE; |
1989 | } |
1990 | else |
1991 | { |
1992 | error_at (newdecl_loc, "conflicting declaration %q#D" , newdecl); |
1993 | inform (olddecl_loc, |
1994 | "previous declaration as %q#D" , olddecl); |
1995 | return error_mark_node; |
1996 | } |
1997 | } |
1998 | else if (TREE_CODE (newdecl) == FUNCTION_DECL |
1999 | && DECL_OMP_DECLARE_REDUCTION_P (newdecl)) |
2000 | { |
2001 | /* OMP UDRs are never duplicates. */ |
2002 | gcc_assert (DECL_OMP_DECLARE_REDUCTION_P (olddecl)); |
2003 | error_at (newdecl_loc, |
2004 | "redeclaration of %<pragma omp declare reduction%>" ); |
2005 | inform (olddecl_loc, |
2006 | "previous %<pragma omp declare reduction%> declaration" ); |
2007 | return error_mark_node; |
2008 | } |
2009 | else if (TREE_CODE (newdecl) == FUNCTION_DECL |
2010 | && ((DECL_TEMPLATE_SPECIALIZATION (olddecl) |
2011 | && (!DECL_TEMPLATE_INFO (newdecl) |
2012 | || (DECL_TI_TEMPLATE (newdecl) |
2013 | != DECL_TI_TEMPLATE (olddecl)))) |
2014 | || (DECL_TEMPLATE_SPECIALIZATION (newdecl) |
2015 | && (!DECL_TEMPLATE_INFO (olddecl) |
2016 | || (DECL_TI_TEMPLATE (olddecl) |
2017 | != DECL_TI_TEMPLATE (newdecl)))))) |
2018 | /* It's OK to have a template specialization and a non-template |
2019 | with the same type, or to have specializations of two |
2020 | different templates with the same type. Note that if one is a |
2021 | specialization, and the other is an instantiation of the same |
2022 | template, that we do not exit at this point. That situation |
2023 | can occur if we instantiate a template class, and then |
2024 | specialize one of its methods. This situation is valid, but |
2025 | the declarations must be merged in the usual way. */ |
2026 | return NULL_TREE; |
2027 | else if (TREE_CODE (newdecl) == FUNCTION_DECL |
2028 | && ((DECL_TEMPLATE_INSTANTIATION (olddecl) |
2029 | && !DECL_USE_TEMPLATE (newdecl)) |
2030 | || (DECL_TEMPLATE_INSTANTIATION (newdecl) |
2031 | && !DECL_USE_TEMPLATE (olddecl)))) |
2032 | /* One of the declarations is a template instantiation, and the |
2033 | other is not a template at all. That's OK. */ |
2034 | return NULL_TREE; |
2035 | else if (TREE_CODE (newdecl) == NAMESPACE_DECL) |
2036 | { |
2037 | /* In [namespace.alias] we have: |
2038 | |
2039 | In a declarative region, a namespace-alias-definition can be |
2040 | used to redefine a namespace-alias declared in that declarative |
2041 | region to refer only to the namespace to which it already |
2042 | refers. |
2043 | |
2044 | Therefore, if we encounter a second alias directive for the same |
2045 | alias, we can just ignore the second directive. */ |
2046 | if (DECL_NAMESPACE_ALIAS (newdecl) |
2047 | && (DECL_NAMESPACE_ALIAS (newdecl) |
2048 | == DECL_NAMESPACE_ALIAS (olddecl))) |
2049 | return olddecl; |
2050 | |
2051 | /* Leave it to update_binding to merge or report error. */ |
2052 | return NULL_TREE; |
2053 | } |
2054 | else |
2055 | { |
2056 | const char *errmsg = redeclaration_error_message (newdecl, olddecl); |
2057 | if (errmsg) |
2058 | { |
2059 | auto_diagnostic_group d; |
2060 | error_at (newdecl_loc, errmsg, newdecl); |
2061 | if (DECL_NAME (olddecl) != NULL_TREE) |
2062 | inform (olddecl_loc, |
2063 | (DECL_INITIAL (olddecl) && namespace_bindings_p ()) |
2064 | ? G_("%q#D previously defined here" ) |
2065 | : G_("%q#D previously declared here" ), olddecl); |
2066 | return error_mark_node; |
2067 | } |
2068 | else if (TREE_CODE (olddecl) == FUNCTION_DECL |
2069 | && DECL_INITIAL (olddecl) != NULL_TREE |
2070 | && !prototype_p (TREE_TYPE (olddecl)) |
2071 | && prototype_p (TREE_TYPE (newdecl))) |
2072 | { |
2073 | /* Prototype decl follows defn w/o prototype. */ |
2074 | auto_diagnostic_group d; |
2075 | if (warning_at (newdecl_loc, 0, |
2076 | "prototype specified for %q#D" , newdecl)) |
2077 | inform (olddecl_loc, |
2078 | "previous non-prototype definition here" ); |
2079 | } |
2080 | else if (VAR_OR_FUNCTION_DECL_P (olddecl) |
2081 | && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl)) |
2082 | { |
2083 | /* [dcl.link] |
2084 | If two declarations of the same function or object |
2085 | specify different linkage-specifications ..., the program |
2086 | is ill-formed.... Except for functions with C++ linkage, |
2087 | a function declaration without a linkage specification |
2088 | shall not precede the first linkage specification for |
2089 | that function. A function can be declared without a |
2090 | linkage specification after an explicit linkage |
2091 | specification has been seen; the linkage explicitly |
2092 | specified in the earlier declaration is not affected by |
2093 | such a function declaration. |
2094 | |
2095 | DR 563 raises the question why the restrictions on |
2096 | functions should not also apply to objects. Older |
2097 | versions of G++ silently ignore the linkage-specification |
2098 | for this example: |
2099 | |
2100 | namespace N { |
2101 | extern int i; |
2102 | extern "C" int i; |
2103 | } |
2104 | |
2105 | which is clearly wrong. Therefore, we now treat objects |
2106 | like functions. */ |
2107 | if (current_lang_depth () == 0) |
2108 | { |
2109 | /* There is no explicit linkage-specification, so we use |
2110 | the linkage from the previous declaration. */ |
2111 | retrofit_lang_decl (newdecl); |
2112 | SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl)); |
2113 | } |
2114 | else |
2115 | { |
2116 | auto_diagnostic_group d; |
2117 | error_at (newdecl_loc, |
2118 | "conflicting declaration of %q#D with %qL linkage" , |
2119 | newdecl, DECL_LANGUAGE (newdecl)); |
2120 | inform (olddecl_loc, |
2121 | "previous declaration with %qL linkage" , |
2122 | DECL_LANGUAGE (olddecl)); |
2123 | } |
2124 | } |
2125 | |
2126 | if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl)) |
2127 | ; |
2128 | else if (TREE_CODE (olddecl) == FUNCTION_DECL) |
2129 | { |
2130 | /* Note: free functions, as TEMPLATE_DECLs, are handled below. */ |
2131 | if (DECL_FUNCTION_MEMBER_P (olddecl) |
2132 | && (/* grokfndecl passes member function templates too |
2133 | as FUNCTION_DECLs. */ |
2134 | DECL_TEMPLATE_INFO (olddecl) |
2135 | /* C++11 8.3.6/6. |
2136 | Default arguments for a member function of a class |
2137 | template shall be specified on the initial declaration |
2138 | of the member function within the class template. */ |
2139 | || CLASSTYPE_TEMPLATE_INFO (CP_DECL_CONTEXT (olddecl)))) |
2140 | { |
2141 | check_redeclaration_no_default_args (decl: newdecl); |
2142 | |
2143 | if (DECL_TEMPLATE_INFO (olddecl) |
2144 | && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (olddecl))) |
2145 | { |
2146 | tree new_parms = DECL_TEMPLATE_INFO (newdecl) |
2147 | ? DECL_INNERMOST_TEMPLATE_PARMS (DECL_TI_TEMPLATE (newdecl)) |
2148 | : INNERMOST_TEMPLATE_PARMS (current_template_parms); |
2149 | tree old_parms |
2150 | = DECL_INNERMOST_TEMPLATE_PARMS (DECL_TI_TEMPLATE (olddecl)); |
2151 | merge_default_template_args (new_parms, old_parms, |
2152 | /*class_p=*/false); |
2153 | } |
2154 | } |
2155 | else |
2156 | { |
2157 | tree t1 = FUNCTION_FIRST_USER_PARMTYPE (olddecl); |
2158 | tree t2 = FUNCTION_FIRST_USER_PARMTYPE (newdecl); |
2159 | int i = 1; |
2160 | |
2161 | for (; t1 && t1 != void_list_node; |
2162 | t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++) |
2163 | if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2)) |
2164 | { |
2165 | if (simple_cst_equal (TREE_PURPOSE (t1), |
2166 | TREE_PURPOSE (t2)) == 1) |
2167 | { |
2168 | auto_diagnostic_group d; |
2169 | if (permerror (newdecl_loc, |
2170 | "default argument given for parameter " |
2171 | "%d of %q#D" , i, newdecl)) |
2172 | inform (olddecl_loc, |
2173 | "previous specification in %q#D here" , |
2174 | olddecl); |
2175 | } |
2176 | else |
2177 | { |
2178 | auto_diagnostic_group d; |
2179 | error_at (newdecl_loc, |
2180 | "default argument given for parameter %d " |
2181 | "of %q#D" , i, newdecl); |
2182 | inform (olddecl_loc, |
2183 | "previous specification in %q#D here" , |
2184 | olddecl); |
2185 | } |
2186 | } |
2187 | |
2188 | /* C++17 11.3.6/4: "If a friend declaration specifies a default |
2189 | argument expression, that declaration... shall be the only |
2190 | declaration of the function or function template in the |
2191 | translation unit." */ |
2192 | check_no_redeclaration_friend_default_args (olddecl, newdecl); |
2193 | } |
2194 | } |
2195 | } |
2196 | |
2197 | /* Do not merge an implicit typedef with an explicit one. In: |
2198 | |
2199 | class A; |
2200 | ... |
2201 | typedef class A A __attribute__ ((foo)); |
2202 | |
2203 | the attribute should apply only to the typedef. */ |
2204 | if (TREE_CODE (olddecl) == TYPE_DECL |
2205 | && (DECL_IMPLICIT_TYPEDEF_P (olddecl) |
2206 | || DECL_IMPLICIT_TYPEDEF_P (newdecl))) |
2207 | return NULL_TREE; |
2208 | |
2209 | if (DECL_TEMPLATE_PARM_P (olddecl) != DECL_TEMPLATE_PARM_P (newdecl)) |
2210 | return NULL_TREE; |
2211 | |
2212 | if (!validate_constexpr_redeclaration (old_decl: olddecl, new_decl: newdecl)) |
2213 | return error_mark_node; |
2214 | |
2215 | if (modules_p () |
2216 | && TREE_CODE (CP_DECL_CONTEXT (olddecl)) == NAMESPACE_DECL |
2217 | && TREE_CODE (olddecl) != NAMESPACE_DECL |
2218 | && !hiding) |
2219 | { |
2220 | if (!module_may_redeclare (decl: olddecl)) |
2221 | { |
2222 | if (DECL_ARTIFICIAL (olddecl)) |
2223 | error ("declaration %qD conflicts with builtin" , newdecl); |
2224 | else |
2225 | { |
2226 | error ("declaration %qD conflicts with import" , newdecl); |
2227 | inform (olddecl_loc, "import declared %q#D here" , olddecl); |
2228 | } |
2229 | |
2230 | return error_mark_node; |
2231 | } |
2232 | |
2233 | tree not_tmpl = STRIP_TEMPLATE (olddecl); |
2234 | if (DECL_LANG_SPECIFIC (not_tmpl) && DECL_MODULE_ATTACH_P (not_tmpl)) |
2235 | { |
2236 | if (DECL_MODULE_EXPORT_P (STRIP_TEMPLATE (newdecl)) |
2237 | && !DECL_MODULE_EXPORT_P (not_tmpl)) |
2238 | { |
2239 | error ("conflicting exporting declaration %qD" , newdecl); |
2240 | inform (olddecl_loc, "previous declaration %q#D here" , olddecl); |
2241 | } |
2242 | } |
2243 | else if (DECL_MODULE_EXPORT_P (newdecl)) |
2244 | DECL_MODULE_EXPORT_P (not_tmpl) = true; |
2245 | } |
2246 | |
2247 | /* We have committed to returning OLDDECL at this point. */ |
2248 | |
2249 | /* If new decl is `static' and an `extern' was seen previously, |
2250 | warn about it. */ |
2251 | warn_extern_redeclared_static (newdecl, olddecl); |
2252 | |
2253 | /* True to merge attributes between the declarations, false to |
2254 | set OLDDECL's attributes to those of NEWDECL (for template |
2255 | explicit specializations that specify their own attributes |
2256 | independent of those specified for the primary template). */ |
2257 | const bool merge_attr = (TREE_CODE (newdecl) != FUNCTION_DECL |
2258 | || !DECL_TEMPLATE_SPECIALIZATION (newdecl) |
2259 | || DECL_TEMPLATE_SPECIALIZATION (olddecl)); |
2260 | |
2261 | if (TREE_CODE (newdecl) == FUNCTION_DECL) |
2262 | { |
2263 | if (merge_attr) |
2264 | { |
2265 | if (diagnose_mismatched_attributes (olddecl, newdecl)) |
2266 | inform (olddecl_loc, DECL_INITIAL (olddecl) |
2267 | ? G_("previous definition of %qD here" ) |
2268 | : G_("previous declaration of %qD here" ), olddecl); |
2269 | |
2270 | /* [dcl.attr.noreturn]: The first declaration of a function shall |
2271 | specify the noreturn attribute if any declaration of that function |
2272 | specifies the noreturn attribute. */ |
2273 | tree a; |
2274 | if (TREE_THIS_VOLATILE (newdecl) |
2275 | && !TREE_THIS_VOLATILE (olddecl) |
2276 | /* This applies to [[noreturn]] only, not its GNU variants. */ |
2277 | && (a = lookup_attribute (attr_name: "noreturn" , DECL_ATTRIBUTES (newdecl))) |
2278 | && cxx11_attribute_p (a) |
2279 | && get_attribute_namespace (a) == NULL_TREE) |
2280 | { |
2281 | error_at (newdecl_loc, "function %qD declared %<[[noreturn]]%> " |
2282 | "but its first declaration was not" , newdecl); |
2283 | inform (olddecl_loc, "previous declaration of %qD" , olddecl); |
2284 | } |
2285 | } |
2286 | |
2287 | /* Now that functions must hold information normally held |
2288 | by field decls, there is extra work to do so that |
2289 | declaration information does not get destroyed during |
2290 | definition. */ |
2291 | if (DECL_VINDEX (olddecl)) |
2292 | DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl); |
2293 | if (DECL_CONTEXT (olddecl)) |
2294 | DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl); |
2295 | DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl); |
2296 | DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl); |
2297 | DECL_PURE_VIRTUAL_P (newdecl) |= DECL_PURE_VIRTUAL_P (olddecl); |
2298 | DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl); |
2299 | DECL_INVALID_OVERRIDER_P (newdecl) |= DECL_INVALID_OVERRIDER_P (olddecl); |
2300 | DECL_FINAL_P (newdecl) |= DECL_FINAL_P (olddecl); |
2301 | DECL_OVERRIDE_P (newdecl) |= DECL_OVERRIDE_P (olddecl); |
2302 | DECL_THIS_STATIC (newdecl) |= DECL_THIS_STATIC (olddecl); |
2303 | DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (newdecl) |
2304 | |= DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (olddecl); |
2305 | if (DECL_OVERLOADED_OPERATOR_P (olddecl)) |
2306 | DECL_OVERLOADED_OPERATOR_CODE_RAW (newdecl) |
2307 | = DECL_OVERLOADED_OPERATOR_CODE_RAW (olddecl); |
2308 | new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE; |
2309 | |
2310 | duplicate_contracts (newdecl, olddecl); |
2311 | |
2312 | /* Optionally warn about more than one declaration for the same |
2313 | name, but don't warn about a function declaration followed by a |
2314 | definition. */ |
2315 | if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl) |
2316 | && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE) |
2317 | /* Don't warn about extern decl followed by definition. */ |
2318 | && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl)) |
2319 | /* Don't warn if at least one is/was hidden. */ |
2320 | && !(hiding || was_hidden) |
2321 | /* Don't warn about declaration followed by specialization. */ |
2322 | && (! DECL_TEMPLATE_SPECIALIZATION (newdecl) |
2323 | || DECL_TEMPLATE_SPECIALIZATION (olddecl))) |
2324 | { |
2325 | auto_diagnostic_group d; |
2326 | if (warning_at (newdecl_loc, |
2327 | OPT_Wredundant_decls, |
2328 | "redundant redeclaration of %qD in same scope" , |
2329 | newdecl)) |
2330 | inform (olddecl_loc, |
2331 | "previous declaration of %qD" , olddecl); |
2332 | } |
2333 | |
2334 | /* [dcl.fct.def.delete] A deleted definition of a function shall be the |
2335 | first declaration of the function or, for an explicit specialization |
2336 | of a function template, the first declaration of that |
2337 | specialization. */ |
2338 | if (!(DECL_TEMPLATE_INSTANTIATION (olddecl) |
2339 | && DECL_TEMPLATE_SPECIALIZATION (newdecl))) |
2340 | { |
2341 | if (DECL_DELETED_FN (newdecl)) |
2342 | { |
2343 | auto_diagnostic_group d; |
2344 | if (pedwarn (newdecl_loc, 0, "deleted definition of %qD " |
2345 | "is not first declaration" , newdecl)) |
2346 | inform (olddecl_loc, |
2347 | "previous declaration of %qD" , olddecl); |
2348 | } |
2349 | DECL_DELETED_FN (newdecl) |= DECL_DELETED_FN (olddecl); |
2350 | } |
2351 | } |
2352 | |
2353 | /* Deal with C++: must preserve virtual function table size. */ |
2354 | if (TREE_CODE (olddecl) == TYPE_DECL) |
2355 | { |
2356 | tree newtype = TREE_TYPE (newdecl); |
2357 | tree oldtype = TREE_TYPE (olddecl); |
2358 | |
2359 | if (newtype != error_mark_node && oldtype != error_mark_node |
2360 | && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype)) |
2361 | CLASSTYPE_FRIEND_CLASSES (newtype) |
2362 | = CLASSTYPE_FRIEND_CLASSES (oldtype); |
2363 | |
2364 | DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl); |
2365 | } |
2366 | |
2367 | /* Copy all the DECL_... slots specified in the new decl except for |
2368 | any that we copy here from the old type. */ |
2369 | if (merge_attr) |
2370 | DECL_ATTRIBUTES (newdecl) |
2371 | = (*targetm.merge_decl_attributes) (olddecl, newdecl); |
2372 | else |
2373 | DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl); |
2374 | |
2375 | if (TREE_CODE (newdecl) == TEMPLATE_DECL) |
2376 | { |
2377 | tree old_result = DECL_TEMPLATE_RESULT (olddecl); |
2378 | tree new_result = DECL_TEMPLATE_RESULT (newdecl); |
2379 | TREE_TYPE (olddecl) = TREE_TYPE (old_result); |
2380 | |
2381 | /* The new decl should not already have gathered any |
2382 | specializations. */ |
2383 | gcc_assert (!DECL_TEMPLATE_SPECIALIZATIONS (newdecl)); |
2384 | |
2385 | /* Make sure the contracts are equivalent. */ |
2386 | duplicate_contracts (newdecl, olddecl); |
2387 | |
2388 | /* Remove contracts from old_result so they aren't appended to |
2389 | old_result by the merge function. */ |
2390 | remove_contract_attributes (old_result); |
2391 | |
2392 | DECL_ATTRIBUTES (old_result) |
2393 | = (*targetm.merge_decl_attributes) (old_result, new_result); |
2394 | |
2395 | if (DECL_FUNCTION_TEMPLATE_P (newdecl)) |
2396 | { |
2397 | if (DECL_SOURCE_LOCATION (newdecl) |
2398 | != DECL_SOURCE_LOCATION (olddecl)) |
2399 | { |
2400 | /* Per C++11 8.3.6/4, default arguments cannot be added in |
2401 | later declarations of a function template. */ |
2402 | check_redeclaration_no_default_args (decl: newdecl); |
2403 | /* C++17 11.3.6/4: "If a friend declaration specifies a default |
2404 | argument expression, that declaration... shall be the only |
2405 | declaration of the function or function template in the |
2406 | translation unit." */ |
2407 | check_no_redeclaration_friend_default_args |
2408 | (olddecl: old_result, newdecl: new_result); |
2409 | |
2410 | tree new_parms = DECL_INNERMOST_TEMPLATE_PARMS (newdecl); |
2411 | tree old_parms = DECL_INNERMOST_TEMPLATE_PARMS (olddecl); |
2412 | merge_default_template_args (new_parms, old_parms, |
2413 | /*class_p=*/false); |
2414 | } |
2415 | if (!DECL_UNIQUE_FRIEND_P (new_result)) |
2416 | DECL_UNIQUE_FRIEND_P (old_result) = false; |
2417 | |
2418 | check_default_args (newdecl); |
2419 | |
2420 | if (GNU_INLINE_P (old_result) != GNU_INLINE_P (new_result) |
2421 | && DECL_INITIAL (new_result)) |
2422 | { |
2423 | if (DECL_INITIAL (old_result)) |
2424 | DECL_UNINLINABLE (old_result) = 1; |
2425 | else |
2426 | DECL_UNINLINABLE (old_result) = DECL_UNINLINABLE (new_result); |
2427 | DECL_EXTERNAL (old_result) = DECL_EXTERNAL (new_result); |
2428 | DECL_NOT_REALLY_EXTERN (old_result) |
2429 | = DECL_NOT_REALLY_EXTERN (new_result); |
2430 | DECL_INTERFACE_KNOWN (old_result) |
2431 | = DECL_INTERFACE_KNOWN (new_result); |
2432 | DECL_DECLARED_INLINE_P (old_result) |
2433 | = DECL_DECLARED_INLINE_P (new_result); |
2434 | DECL_DISREGARD_INLINE_LIMITS (old_result) |
2435 | |= DECL_DISREGARD_INLINE_LIMITS (new_result); |
2436 | } |
2437 | else |
2438 | { |
2439 | DECL_DECLARED_INLINE_P (old_result) |
2440 | |= DECL_DECLARED_INLINE_P (new_result); |
2441 | DECL_DISREGARD_INLINE_LIMITS (old_result) |
2442 | |= DECL_DISREGARD_INLINE_LIMITS (new_result); |
2443 | check_redeclaration_exception_specification (new_decl: newdecl, old_decl: olddecl); |
2444 | |
2445 | merge_attribute_bits (newdecl: new_result, olddecl: old_result); |
2446 | } |
2447 | } |
2448 | |
2449 | /* If the new declaration is a definition, update the file and |
2450 | line information on the declaration, and also make |
2451 | the old declaration the same definition. */ |
2452 | if (DECL_INITIAL (new_result) != NULL_TREE) |
2453 | { |
2454 | DECL_SOURCE_LOCATION (olddecl) |
2455 | = DECL_SOURCE_LOCATION (old_result) |
2456 | = DECL_SOURCE_LOCATION (newdecl); |
2457 | DECL_INITIAL (old_result) = DECL_INITIAL (new_result); |
2458 | if (DECL_FUNCTION_TEMPLATE_P (newdecl)) |
2459 | { |
2460 | DECL_ARGUMENTS (old_result) = DECL_ARGUMENTS (new_result); |
2461 | for (tree p = DECL_ARGUMENTS (old_result); p; p = DECL_CHAIN (p)) |
2462 | DECL_CONTEXT (p) = old_result; |
2463 | |
2464 | if (tree fc = DECL_FRIEND_CONTEXT (new_result)) |
2465 | SET_DECL_FRIEND_CONTEXT (old_result, fc); |
2466 | } |
2467 | } |
2468 | |
2469 | return olddecl; |
2470 | } |
2471 | |
2472 | if (types_match) |
2473 | { |
2474 | if (TREE_CODE (newdecl) == FUNCTION_DECL) |
2475 | check_redeclaration_exception_specification (new_decl: newdecl, old_decl: olddecl); |
2476 | |
2477 | /* Automatically handles default parameters. */ |
2478 | tree oldtype = TREE_TYPE (olddecl); |
2479 | tree newtype; |
2480 | |
2481 | /* For typedefs use the old type, as the new type's DECL_NAME points |
2482 | at newdecl, which will be ggc_freed. */ |
2483 | if (TREE_CODE (newdecl) == TYPE_DECL) |
2484 | { |
2485 | /* But NEWTYPE might have an attribute, honor that. */ |
2486 | tree tem = TREE_TYPE (newdecl); |
2487 | newtype = oldtype; |
2488 | |
2489 | if (TYPE_USER_ALIGN (tem)) |
2490 | { |
2491 | if (TYPE_ALIGN (tem) > TYPE_ALIGN (newtype)) |
2492 | SET_TYPE_ALIGN (newtype, TYPE_ALIGN (tem)); |
2493 | TYPE_USER_ALIGN (newtype) = true; |
2494 | } |
2495 | |
2496 | /* And remove the new type from the variants list. */ |
2497 | if (TYPE_NAME (TREE_TYPE (newdecl)) == newdecl) |
2498 | { |
2499 | tree remove = TREE_TYPE (newdecl); |
2500 | if (TYPE_MAIN_VARIANT (remove) == remove) |
2501 | { |
2502 | gcc_assert (TYPE_NEXT_VARIANT (remove) == NULL_TREE); |
2503 | /* If remove is the main variant, no need to remove that |
2504 | from the list. One of the DECL_ORIGINAL_TYPE |
2505 | variants, e.g. created for aligned attribute, might still |
2506 | refer to the newdecl TYPE_DECL though, so remove that one |
2507 | in that case. */ |
2508 | if (tree orig = DECL_ORIGINAL_TYPE (newdecl)) |
2509 | if (orig != remove) |
2510 | for (tree t = TYPE_MAIN_VARIANT (orig); t; |
2511 | t = TYPE_MAIN_VARIANT (t)) |
2512 | if (TYPE_NAME (TYPE_NEXT_VARIANT (t)) == newdecl) |
2513 | { |
2514 | TYPE_NEXT_VARIANT (t) |
2515 | = TYPE_NEXT_VARIANT (TYPE_NEXT_VARIANT (t)); |
2516 | break; |
2517 | } |
2518 | } |
2519 | else |
2520 | for (tree t = TYPE_MAIN_VARIANT (remove); ; |
2521 | t = TYPE_NEXT_VARIANT (t)) |
2522 | if (TYPE_NEXT_VARIANT (t) == remove) |
2523 | { |
2524 | TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (remove); |
2525 | break; |
2526 | } |
2527 | } |
2528 | } |
2529 | else if (merge_attr) |
2530 | newtype = merge_types (TREE_TYPE (newdecl), TREE_TYPE (olddecl)); |
2531 | else |
2532 | newtype = TREE_TYPE (newdecl); |
2533 | |
2534 | if (VAR_P (newdecl)) |
2535 | { |
2536 | DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl); |
2537 | /* For already initialized vars, TREE_READONLY could have been |
2538 | cleared in cp_finish_decl, because the var needs runtime |
2539 | initialization or destruction. Make sure not to set |
2540 | TREE_READONLY on it again. */ |
2541 | if (DECL_INITIALIZED_P (olddecl) |
2542 | && !DECL_EXTERNAL (olddecl) |
2543 | && !TREE_READONLY (olddecl)) |
2544 | TREE_READONLY (newdecl) = 0; |
2545 | DECL_INITIALIZED_P (newdecl) |= DECL_INITIALIZED_P (olddecl); |
2546 | DECL_NONTRIVIALLY_INITIALIZED_P (newdecl) |
2547 | |= DECL_NONTRIVIALLY_INITIALIZED_P (olddecl); |
2548 | if (DECL_DEPENDENT_INIT_P (olddecl)) |
2549 | SET_DECL_DEPENDENT_INIT_P (newdecl, true); |
2550 | DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (newdecl) |
2551 | |= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (olddecl); |
2552 | DECL_DECLARED_CONSTEXPR_P (newdecl) |
2553 | |= DECL_DECLARED_CONSTEXPR_P (olddecl); |
2554 | DECL_DECLARED_CONSTINIT_P (newdecl) |
2555 | |= DECL_DECLARED_CONSTINIT_P (olddecl); |
2556 | |
2557 | /* Merge the threadprivate attribute from OLDDECL into NEWDECL. */ |
2558 | if (DECL_LANG_SPECIFIC (olddecl) |
2559 | && CP_DECL_THREADPRIVATE_P (olddecl)) |
2560 | { |
2561 | /* Allocate a LANG_SPECIFIC structure for NEWDECL, if needed. */ |
2562 | retrofit_lang_decl (newdecl); |
2563 | CP_DECL_THREADPRIVATE_P (newdecl) = 1; |
2564 | } |
2565 | } |
2566 | |
2567 | /* An explicit specialization of a function template or of a member |
2568 | function of a class template can be declared transaction_safe |
2569 | independently of whether the corresponding template entity is declared |
2570 | transaction_safe. */ |
2571 | if (flag_tm && TREE_CODE (newdecl) == FUNCTION_DECL |
2572 | && DECL_TEMPLATE_INSTANTIATION (olddecl) |
2573 | && DECL_TEMPLATE_SPECIALIZATION (newdecl) |
2574 | && tx_safe_fn_type_p (newtype) |
2575 | && !tx_safe_fn_type_p (TREE_TYPE (newdecl))) |
2576 | newtype = tx_unsafe_fn_variant (newtype); |
2577 | |
2578 | TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype; |
2579 | |
2580 | if (TREE_CODE (newdecl) == FUNCTION_DECL) |
2581 | check_default_args (newdecl); |
2582 | |
2583 | /* Lay the type out, unless already done. */ |
2584 | if (! same_type_p (newtype, oldtype) |
2585 | && TREE_TYPE (newdecl) != error_mark_node |
2586 | && !(processing_template_decl && uses_template_parms (newdecl))) |
2587 | layout_type (TREE_TYPE (newdecl)); |
2588 | |
2589 | if ((VAR_P (newdecl) |
2590 | || TREE_CODE (newdecl) == PARM_DECL |
2591 | || TREE_CODE (newdecl) == RESULT_DECL |
2592 | || TREE_CODE (newdecl) == FIELD_DECL |
2593 | || TREE_CODE (newdecl) == TYPE_DECL) |
2594 | && !(processing_template_decl && uses_template_parms (newdecl))) |
2595 | layout_decl (newdecl, 0); |
2596 | |
2597 | /* Merge deprecatedness. */ |
2598 | if (TREE_DEPRECATED (newdecl)) |
2599 | TREE_DEPRECATED (olddecl) = 1; |
2600 | |
2601 | /* Merge unavailability. */ |
2602 | if (TREE_UNAVAILABLE (newdecl)) |
2603 | TREE_UNAVAILABLE (olddecl) = 1; |
2604 | |
2605 | /* Preserve function specific target and optimization options */ |
2606 | if (TREE_CODE (newdecl) == FUNCTION_DECL) |
2607 | { |
2608 | if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl) |
2609 | && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl)) |
2610 | DECL_FUNCTION_SPECIFIC_TARGET (newdecl) |
2611 | = DECL_FUNCTION_SPECIFIC_TARGET (olddecl); |
2612 | |
2613 | if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl) |
2614 | && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)) |
2615 | DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl) |
2616 | = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl); |
2617 | |
2618 | if (!DECL_UNIQUE_FRIEND_P (olddecl)) |
2619 | DECL_UNIQUE_FRIEND_P (newdecl) = false; |
2620 | } |
2621 | else |
2622 | { |
2623 | /* Merge the const type qualifier. */ |
2624 | if (TREE_READONLY (newdecl)) |
2625 | TREE_READONLY (olddecl) = 1; |
2626 | /* Merge the volatile type qualifier. */ |
2627 | if (TREE_THIS_VOLATILE (newdecl)) |
2628 | TREE_THIS_VOLATILE (olddecl) = 1; |
2629 | } |
2630 | |
2631 | /* Merge the initialization information. */ |
2632 | if (DECL_INITIAL (newdecl) == NULL_TREE |
2633 | && DECL_INITIAL (olddecl) != NULL_TREE) |
2634 | { |
2635 | DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl); |
2636 | DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl); |
2637 | if (TREE_CODE (newdecl) == FUNCTION_DECL) |
2638 | { |
2639 | DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl); |
2640 | DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl); |
2641 | } |
2642 | } |
2643 | |
2644 | if (TREE_CODE (newdecl) == FUNCTION_DECL) |
2645 | { |
2646 | DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl) |
2647 | |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl); |
2648 | DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl); |
2649 | if (DECL_IS_OPERATOR_NEW_P (olddecl)) |
2650 | DECL_SET_IS_OPERATOR_NEW (newdecl, true); |
2651 | DECL_LOOPING_CONST_OR_PURE_P (newdecl) |
2652 | |= DECL_LOOPING_CONST_OR_PURE_P (olddecl); |
2653 | DECL_IS_REPLACEABLE_OPERATOR (newdecl) |
2654 | |= DECL_IS_REPLACEABLE_OPERATOR (olddecl); |
2655 | |
2656 | if (merge_attr) |
2657 | merge_attribute_bits (newdecl, olddecl); |
2658 | else |
2659 | { |
2660 | /* Merge the noreturn bit. */ |
2661 | TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl); |
2662 | TREE_READONLY (olddecl) = TREE_READONLY (newdecl); |
2663 | TREE_NOTHROW (olddecl) = TREE_NOTHROW (newdecl); |
2664 | DECL_IS_MALLOC (olddecl) = DECL_IS_MALLOC (newdecl); |
2665 | DECL_PURE_P (olddecl) = DECL_PURE_P (newdecl); |
2666 | } |
2667 | /* Keep the old RTL. */ |
2668 | COPY_DECL_RTL (olddecl, newdecl); |
2669 | } |
2670 | else if (VAR_P (newdecl) |
2671 | && (DECL_SIZE (olddecl) || !DECL_SIZE (newdecl))) |
2672 | { |
2673 | /* Keep the old RTL. We cannot keep the old RTL if the old |
2674 | declaration was for an incomplete object and the new |
2675 | declaration is not since many attributes of the RTL will |
2676 | change. */ |
2677 | COPY_DECL_RTL (olddecl, newdecl); |
2678 | } |
2679 | } |
2680 | /* If cannot merge, then use the new type and qualifiers, |
2681 | and don't preserve the old rtl. */ |
2682 | else |
2683 | { |
2684 | /* Clean out any memory we had of the old declaration. */ |
2685 | tree oldstatic = value_member (olddecl, static_aggregates); |
2686 | if (oldstatic) |
2687 | TREE_VALUE (oldstatic) = error_mark_node; |
2688 | |
2689 | TREE_TYPE (olddecl) = TREE_TYPE (newdecl); |
2690 | TREE_READONLY (olddecl) = TREE_READONLY (newdecl); |
2691 | TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl); |
2692 | TREE_NOTHROW (olddecl) = TREE_NOTHROW (newdecl); |
2693 | TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl); |
2694 | } |
2695 | |
2696 | /* Merge the storage class information. */ |
2697 | merge_weak (newdecl, olddecl); |
2698 | |
2699 | DECL_DEFER_OUTPUT (newdecl) |= DECL_DEFER_OUTPUT (olddecl); |
2700 | TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl); |
2701 | TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl); |
2702 | if (! DECL_EXTERNAL (olddecl)) |
2703 | DECL_EXTERNAL (newdecl) = 0; |
2704 | if (! DECL_COMDAT (olddecl)) |
2705 | DECL_COMDAT (newdecl) = 0; |
2706 | |
2707 | if (VAR_OR_FUNCTION_DECL_P (newdecl) && DECL_LOCAL_DECL_P (newdecl)) |
2708 | { |
2709 | if (!DECL_LOCAL_DECL_P (olddecl)) |
2710 | /* This can happen if olddecl was brought in from the |
2711 | enclosing namespace via a using-decl. The new decl is |
2712 | then not a block-scope extern at all. */ |
2713 | DECL_LOCAL_DECL_P (newdecl) = false; |
2714 | else |
2715 | { |
2716 | retrofit_lang_decl (newdecl); |
2717 | tree alias = DECL_LOCAL_DECL_ALIAS (newdecl) |
2718 | = DECL_LOCAL_DECL_ALIAS (olddecl); |
2719 | DECL_ATTRIBUTES (alias) |
2720 | = (*targetm.merge_decl_attributes) (alias, newdecl); |
2721 | if (TREE_CODE (newdecl) == FUNCTION_DECL) |
2722 | merge_attribute_bits (newdecl, olddecl: alias); |
2723 | } |
2724 | } |
2725 | |
2726 | new_template_info = NULL_TREE; |
2727 | if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl)) |
2728 | { |
2729 | bool new_redefines_gnu_inline = false; |
2730 | |
2731 | if (new_defines_function |
2732 | && ((DECL_INTERFACE_KNOWN (olddecl) |
2733 | && TREE_CODE (olddecl) == FUNCTION_DECL) |
2734 | || (TREE_CODE (olddecl) == TEMPLATE_DECL |
2735 | && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) |
2736 | == FUNCTION_DECL)))) |
2737 | new_redefines_gnu_inline = GNU_INLINE_P (STRIP_TEMPLATE (olddecl)); |
2738 | |
2739 | if (!new_redefines_gnu_inline) |
2740 | { |
2741 | DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl); |
2742 | DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl); |
2743 | DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl); |
2744 | } |
2745 | |
2746 | if (TREE_CODE (newdecl) != TYPE_DECL) |
2747 | { |
2748 | DECL_TEMPLATE_INSTANTIATED (newdecl) |
2749 | |= DECL_TEMPLATE_INSTANTIATED (olddecl); |
2750 | DECL_ODR_USED (newdecl) |= DECL_ODR_USED (olddecl); |
2751 | |
2752 | /* If the OLDDECL is an instantiation and/or specialization, |
2753 | then the NEWDECL must be too. But, it may not yet be marked |
2754 | as such if the caller has created NEWDECL, but has not yet |
2755 | figured out that it is a redeclaration. */ |
2756 | if (!DECL_USE_TEMPLATE (newdecl)) |
2757 | DECL_USE_TEMPLATE (newdecl) = DECL_USE_TEMPLATE (olddecl); |
2758 | |
2759 | if (!DECL_TEMPLATE_SPECIALIZATION (newdecl)) |
2760 | DECL_INITIALIZED_IN_CLASS_P (newdecl) |
2761 | |= DECL_INITIALIZED_IN_CLASS_P (olddecl); |
2762 | } |
2763 | |
2764 | /* Don't really know how much of the language-specific |
2765 | values we should copy from old to new. */ |
2766 | DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl); |
2767 | |
2768 | if (LANG_DECL_HAS_MIN (newdecl)) |
2769 | { |
2770 | DECL_ACCESS (newdecl) = DECL_ACCESS (olddecl); |
2771 | if (DECL_TEMPLATE_INFO (newdecl)) |
2772 | { |
2773 | new_template_info = DECL_TEMPLATE_INFO (newdecl); |
2774 | if (DECL_TEMPLATE_INSTANTIATION (olddecl) |
2775 | && DECL_TEMPLATE_SPECIALIZATION (newdecl)) |
2776 | /* Remember the presence of explicit specialization args. */ |
2777 | TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (olddecl)) |
2778 | = TINFO_USED_TEMPLATE_ID (new_template_info); |
2779 | } |
2780 | |
2781 | /* We don't want to copy template info from a non-templated friend |
2782 | (PR105761), but these shouldn't have DECL_TEMPLATE_INFO now. */ |
2783 | gcc_checking_assert (!DECL_TEMPLATE_INFO (olddecl) |
2784 | || !non_templated_friend_p (olddecl)); |
2785 | DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl); |
2786 | } |
2787 | |
2788 | if (DECL_DECLARES_FUNCTION_P (newdecl)) |
2789 | { |
2790 | /* Only functions have these fields. */ |
2791 | DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl); |
2792 | DECL_BEFRIENDING_CLASSES (newdecl) |
2793 | = chainon (DECL_BEFRIENDING_CLASSES (newdecl), |
2794 | DECL_BEFRIENDING_CLASSES (olddecl)); |
2795 | /* DECL_THUNKS is only valid for virtual functions, |
2796 | otherwise it is a DECL_FRIEND_CONTEXT. */ |
2797 | if (DECL_VIRTUAL_P (newdecl)) |
2798 | SET_DECL_THUNKS (newdecl, DECL_THUNKS (olddecl)); |
2799 | else if (tree fc = DECL_FRIEND_CONTEXT (newdecl)) |
2800 | SET_DECL_FRIEND_CONTEXT (olddecl, fc); |
2801 | } |
2802 | else if (VAR_P (newdecl)) |
2803 | { |
2804 | /* Only variables have this field. */ |
2805 | if (VAR_HAD_UNKNOWN_BOUND (olddecl)) |
2806 | SET_VAR_HAD_UNKNOWN_BOUND (newdecl); |
2807 | } |
2808 | } |
2809 | |
2810 | if (TREE_CODE (newdecl) == FUNCTION_DECL) |
2811 | { |
2812 | tree parm; |
2813 | |
2814 | /* Merge parameter attributes. */ |
2815 | tree oldarg, newarg; |
2816 | for (oldarg = DECL_ARGUMENTS(olddecl), newarg = DECL_ARGUMENTS(newdecl); |
2817 | oldarg && newarg; |
2818 | oldarg = DECL_CHAIN(oldarg), newarg = DECL_CHAIN(newarg)) |
2819 | { |
2820 | DECL_ATTRIBUTES (newarg) |
2821 | = (*targetm.merge_decl_attributes) (oldarg, newarg); |
2822 | DECL_ATTRIBUTES (oldarg) = DECL_ATTRIBUTES (newarg); |
2823 | } |
2824 | |
2825 | if (DECL_TEMPLATE_INSTANTIATION (olddecl) |
2826 | && !DECL_TEMPLATE_INSTANTIATION (newdecl)) |
2827 | { |
2828 | /* If newdecl is not a specialization, then it is not a |
2829 | template-related function at all. And that means that we |
2830 | should have exited above, returning 0. */ |
2831 | gcc_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl)); |
2832 | |
2833 | if (DECL_ODR_USED (olddecl)) |
2834 | /* From [temp.expl.spec]: |
2835 | |
2836 | If a template, a member template or the member of a class |
2837 | template is explicitly specialized then that |
2838 | specialization shall be declared before the first use of |
2839 | that specialization that would cause an implicit |
2840 | instantiation to take place, in every translation unit in |
2841 | which such a use occurs. */ |
2842 | error ("explicit specialization of %qD after first use" , |
2843 | olddecl); |
2844 | |
2845 | SET_DECL_TEMPLATE_SPECIALIZATION (olddecl); |
2846 | DECL_COMDAT (newdecl) = (TREE_PUBLIC (newdecl) |
2847 | && DECL_DECLARED_INLINE_P (newdecl)); |
2848 | |
2849 | /* Don't propagate visibility from the template to the |
2850 | specialization here. We'll do that in determine_visibility if |
2851 | appropriate. */ |
2852 | DECL_VISIBILITY_SPECIFIED (olddecl) = 0; |
2853 | |
2854 | /* [temp.expl.spec/14] We don't inline explicit specialization |
2855 | just because the primary template says so. */ |
2856 | gcc_assert (!merge_attr); |
2857 | |
2858 | DECL_DECLARED_INLINE_P (olddecl) |
2859 | = DECL_DECLARED_INLINE_P (newdecl); |
2860 | |
2861 | DECL_DISREGARD_INLINE_LIMITS (olddecl) |
2862 | = DECL_DISREGARD_INLINE_LIMITS (newdecl); |
2863 | |
2864 | DECL_UNINLINABLE (olddecl) = DECL_UNINLINABLE (newdecl); |
2865 | } |
2866 | else if (new_defines_function && DECL_INITIAL (olddecl)) |
2867 | { |
2868 | /* Never inline re-defined extern inline functions. |
2869 | FIXME: this could be better handled by keeping both |
2870 | function as separate declarations. */ |
2871 | DECL_UNINLINABLE (newdecl) = 1; |
2872 | } |
2873 | else |
2874 | { |
2875 | if (DECL_PENDING_INLINE_P (olddecl)) |
2876 | { |
2877 | DECL_PENDING_INLINE_P (newdecl) = 1; |
2878 | DECL_PENDING_INLINE_INFO (newdecl) |
2879 | = DECL_PENDING_INLINE_INFO (olddecl); |
2880 | } |
2881 | else if (DECL_PENDING_INLINE_P (newdecl)) |
2882 | ; |
2883 | else if (DECL_SAVED_AUTO_RETURN_TYPE (newdecl) == NULL) |
2884 | DECL_SAVED_AUTO_RETURN_TYPE (newdecl) |
2885 | = DECL_SAVED_AUTO_RETURN_TYPE (olddecl); |
2886 | |
2887 | DECL_DECLARED_INLINE_P (newdecl) |= DECL_DECLARED_INLINE_P (olddecl); |
2888 | |
2889 | DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl) |
2890 | = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl)); |
2891 | |
2892 | DECL_DISREGARD_INLINE_LIMITS (newdecl) |
2893 | = DECL_DISREGARD_INLINE_LIMITS (olddecl) |
2894 | = (DECL_DISREGARD_INLINE_LIMITS (newdecl) |
2895 | || DECL_DISREGARD_INLINE_LIMITS (olddecl)); |
2896 | } |
2897 | |
2898 | /* Preserve abstractness on cloned [cd]tors. */ |
2899 | DECL_ABSTRACT_P (newdecl) = DECL_ABSTRACT_P (olddecl); |
2900 | |
2901 | /* Update newdecl's parms to point at olddecl. */ |
2902 | for (parm = DECL_ARGUMENTS (newdecl); parm; |
2903 | parm = DECL_CHAIN (parm)) |
2904 | DECL_CONTEXT (parm) = olddecl; |
2905 | |
2906 | if (! types_match) |
2907 | { |
2908 | SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl)); |
2909 | COPY_DECL_ASSEMBLER_NAME (newdecl, olddecl); |
2910 | COPY_DECL_RTL (newdecl, olddecl); |
2911 | } |
2912 | if (! types_match || new_defines_function) |
2913 | { |
2914 | /* These are the final DECL_ARGUMENTS that will be used within the |
2915 | body; update any references to old DECL_ARGUMENTS in the |
2916 | contracts, if present. */ |
2917 | if (tree contracts = DECL_CONTRACTS (newdecl)) |
2918 | remap_contracts (olddecl, newdecl, contracts, true); |
2919 | |
2920 | /* These need to be copied so that the names are available. |
2921 | Note that if the types do match, we'll preserve inline |
2922 | info and other bits, but if not, we won't. */ |
2923 | DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl); |
2924 | DECL_RESULT (olddecl) = DECL_RESULT (newdecl); |
2925 | |
2926 | /* In some cases, duplicate_contracts will remove contracts from |
2927 | OLDDECL, to avoid duplications. Sometimes, the contracts end up |
2928 | shared. If we removed them, re-add them. */ |
2929 | if (!DECL_CONTRACTS (olddecl)) |
2930 | copy_contract_attributes (olddecl, newdecl); |
2931 | } |
2932 | /* If redeclaring a builtin function, it stays built in |
2933 | if newdecl is a gnu_inline definition, or if newdecl is just |
2934 | a declaration. */ |
2935 | if (fndecl_built_in_p (node: olddecl) |
2936 | && (new_defines_function ? GNU_INLINE_P (newdecl) : types_match)) |
2937 | { |
2938 | copy_decl_built_in_function (newdecl, olddecl); |
2939 | /* If we're keeping the built-in definition, keep the rtl, |
2940 | regardless of declaration matches. */ |
2941 | COPY_DECL_RTL (olddecl, newdecl); |
2942 | if (DECL_BUILT_IN_CLASS (newdecl) == BUILT_IN_NORMAL) |
2943 | { |
2944 | enum built_in_function fncode = DECL_FUNCTION_CODE (decl: newdecl); |
2945 | if (builtin_decl_explicit_p (fncode)) |
2946 | { |
2947 | /* A compatible prototype of these builtin functions |
2948 | is seen, assume the runtime implements it with |
2949 | the expected semantics. */ |
2950 | switch (fncode) |
2951 | { |
2952 | case BUILT_IN_STPCPY: |
2953 | set_builtin_decl_implicit_p (fncode, implicit_p: true); |
2954 | break; |
2955 | default: |
2956 | set_builtin_decl_declared_p (fncode, declared_p: true); |
2957 | break; |
2958 | } |
2959 | } |
2960 | |
2961 | copy_attributes_to_builtin (newdecl); |
2962 | } |
2963 | } |
2964 | if (new_defines_function) |
2965 | /* If defining a function declared with other language |
2966 | linkage, use the previously declared language linkage. */ |
2967 | SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl)); |
2968 | else if (types_match) |
2969 | { |
2970 | DECL_RESULT (newdecl) = DECL_RESULT (olddecl); |
2971 | /* Don't clear out the arguments if we're just redeclaring a |
2972 | function. */ |
2973 | if (DECL_ARGUMENTS (olddecl)) |
2974 | { |
2975 | /* If we removed contracts from previous definition, re-attach |
2976 | them. Otherwise, rewrite the contracts so they match the |
2977 | parameters of the new declaration. */ |
2978 | if (DECL_INITIAL (olddecl) |
2979 | && DECL_CONTRACTS (newdecl) |
2980 | && !DECL_CONTRACTS (olddecl)) |
2981 | copy_contract_attributes (olddecl, newdecl); |
2982 | else |
2983 | { |
2984 | /* Temporarily undo the re-contexting of parameters so we can |
2985 | actually remap parameters. The inliner won't replace |
2986 | parameters if we don't do this. */ |
2987 | tree args = DECL_ARGUMENTS (newdecl); |
2988 | for (tree p = args; p; p = DECL_CHAIN (p)) |
2989 | DECL_CONTEXT (p) = newdecl; |
2990 | |
2991 | /* Save new argument names for use in contracts parsing, |
2992 | unless we've already started parsing the body of olddecl |
2993 | (particular issues arise when newdecl is from a prior |
2994 | friend decl with no argument names, see |
2995 | modules/contracts-tpl-friend-1). */ |
2996 | if (tree contracts = DECL_CONTRACTS (olddecl)) |
2997 | remap_contracts (newdecl, olddecl, contracts, true); |
2998 | |
2999 | /* And reverse this operation again. */ |
3000 | for (tree p = args; p; p = DECL_CHAIN (p)) |
3001 | DECL_CONTEXT (p) = olddecl; |
3002 | } |
3003 | |
3004 | DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl); |
3005 | } |
3006 | } |
3007 | } |
3008 | else if (TREE_CODE (newdecl) == NAMESPACE_DECL) |
3009 | NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl); |
3010 | |
3011 | /* Now preserve various other info from the definition. */ |
3012 | TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl); |
3013 | TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl); |
3014 | DECL_COMMON (newdecl) = DECL_COMMON (olddecl); |
3015 | COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl); |
3016 | |
3017 | /* Warn about conflicting visibility specifications. */ |
3018 | if (DECL_VISIBILITY_SPECIFIED (olddecl) |
3019 | && DECL_VISIBILITY_SPECIFIED (newdecl) |
3020 | && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl)) |
3021 | { |
3022 | auto_diagnostic_group d; |
3023 | if (warning_at (newdecl_loc, OPT_Wattributes, |
3024 | "%qD: visibility attribute ignored because it " |
3025 | "conflicts with previous declaration" , newdecl)) |
3026 | inform (olddecl_loc, |
3027 | "previous declaration of %qD" , olddecl); |
3028 | } |
3029 | /* Choose the declaration which specified visibility. */ |
3030 | if (DECL_VISIBILITY_SPECIFIED (olddecl)) |
3031 | { |
3032 | DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl); |
3033 | DECL_VISIBILITY_SPECIFIED (newdecl) = 1; |
3034 | } |
3035 | /* Init priority used to be merged from newdecl to olddecl by the memcpy, |
3036 | so keep this behavior. */ |
3037 | if (VAR_P (newdecl) && DECL_HAS_INIT_PRIORITY_P (newdecl)) |
3038 | { |
3039 | SET_DECL_INIT_PRIORITY (olddecl, DECL_INIT_PRIORITY (newdecl)); |
3040 | DECL_HAS_INIT_PRIORITY_P (olddecl) = 1; |
3041 | } |
3042 | /* Likewise for DECL_ALIGN, DECL_USER_ALIGN and DECL_PACKED. */ |
3043 | if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl)) |
3044 | { |
3045 | SET_DECL_ALIGN (newdecl, DECL_ALIGN (olddecl)); |
3046 | DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl); |
3047 | } |
3048 | else if (DECL_ALIGN (olddecl) == DECL_ALIGN (newdecl) |
3049 | && DECL_USER_ALIGN (olddecl) != DECL_USER_ALIGN (newdecl)) |
3050 | DECL_USER_ALIGN (newdecl) = 1; |
3051 | |
3052 | DECL_USER_ALIGN (olddecl) = DECL_USER_ALIGN (newdecl); |
3053 | if (DECL_WARN_IF_NOT_ALIGN (olddecl) |
3054 | > DECL_WARN_IF_NOT_ALIGN (newdecl)) |
3055 | SET_DECL_WARN_IF_NOT_ALIGN (newdecl, |
3056 | DECL_WARN_IF_NOT_ALIGN (olddecl)); |
3057 | if (TREE_CODE (newdecl) == FIELD_DECL) |
3058 | DECL_PACKED (olddecl) = DECL_PACKED (newdecl); |
3059 | |
3060 | /* The DECL_LANG_SPECIFIC information in OLDDECL will be replaced |
3061 | with that from NEWDECL below. */ |
3062 | if (DECL_LANG_SPECIFIC (olddecl)) |
3063 | { |
3064 | gcc_checking_assert (DECL_LANG_SPECIFIC (olddecl) |
3065 | != DECL_LANG_SPECIFIC (newdecl)); |
3066 | ggc_free (DECL_LANG_SPECIFIC (olddecl)); |
3067 | } |
3068 | |
3069 | /* Merge the USED information. */ |
3070 | if (TREE_USED (olddecl)) |
3071 | TREE_USED (newdecl) = 1; |
3072 | else if (TREE_USED (newdecl)) |
3073 | TREE_USED (olddecl) = 1; |
3074 | |
3075 | if (VAR_P (newdecl)) |
3076 | { |
3077 | if (DECL_READ_P (olddecl)) |
3078 | DECL_READ_P (newdecl) = 1; |
3079 | else if (DECL_READ_P (newdecl)) |
3080 | DECL_READ_P (olddecl) = 1; |
3081 | } |
3082 | |
3083 | if (DECL_PRESERVE_P (olddecl)) |
3084 | DECL_PRESERVE_P (newdecl) = 1; |
3085 | else if (DECL_PRESERVE_P (newdecl)) |
3086 | DECL_PRESERVE_P (olddecl) = 1; |
3087 | |
3088 | /* Merge the DECL_FUNCTION_VERSIONED information. newdecl will be copied |
3089 | to olddecl and deleted. */ |
3090 | if (TREE_CODE (newdecl) == FUNCTION_DECL |
3091 | && DECL_FUNCTION_VERSIONED (olddecl)) |
3092 | { |
3093 | /* Set the flag for newdecl so that it gets copied to olddecl. */ |
3094 | DECL_FUNCTION_VERSIONED (newdecl) = 1; |
3095 | /* newdecl will be purged after copying to olddecl and is no longer |
3096 | a version. */ |
3097 | cgraph_node::delete_function_version_by_decl (decl: newdecl); |
3098 | } |
3099 | |
3100 | if (TREE_CODE (newdecl) == FUNCTION_DECL) |
3101 | { |
3102 | int function_size; |
3103 | struct symtab_node *snode = symtab_node::get (decl: olddecl); |
3104 | |
3105 | function_size = sizeof (struct tree_decl_common); |
3106 | |
3107 | memcpy (dest: (char *) olddecl + sizeof (struct tree_common), |
3108 | src: (char *) newdecl + sizeof (struct tree_common), |
3109 | n: function_size - sizeof (struct tree_common)); |
3110 | |
3111 | memcpy (dest: (char *) olddecl + sizeof (struct tree_decl_common), |
3112 | src: (char *) newdecl + sizeof (struct tree_decl_common), |
3113 | n: sizeof (struct tree_function_decl) - sizeof (struct tree_decl_common)); |
3114 | |
3115 | /* Preserve symtab node mapping. */ |
3116 | olddecl->decl_with_vis.symtab_node = snode; |
3117 | |
3118 | if (new_template_info) |
3119 | /* If newdecl is a template instantiation, it is possible that |
3120 | the following sequence of events has occurred: |
3121 | |
3122 | o A friend function was declared in a class template. The |
3123 | class template was instantiated. |
3124 | |
3125 | o The instantiation of the friend declaration was |
3126 | recorded on the instantiation list, and is newdecl. |
3127 | |
3128 | o Later, however, instantiate_class_template called pushdecl |
3129 | on the newdecl to perform name injection. But, pushdecl in |
3130 | turn called duplicate_decls when it discovered that another |
3131 | declaration of a global function with the same name already |
3132 | existed. |
3133 | |
3134 | o Here, in duplicate_decls, we decided to clobber newdecl. |
3135 | |
3136 | If we're going to do that, we'd better make sure that |
3137 | olddecl, and not newdecl, is on the list of |
3138 | instantiations so that if we try to do the instantiation |
3139 | again we won't get the clobbered declaration. */ |
3140 | reregister_specialization (newdecl, |
3141 | new_template_info, |
3142 | olddecl); |
3143 | } |
3144 | else |
3145 | { |
3146 | size_t size = tree_code_size (TREE_CODE (newdecl)); |
3147 | |
3148 | memcpy (dest: (char *) olddecl + sizeof (struct tree_common), |
3149 | src: (char *) newdecl + sizeof (struct tree_common), |
3150 | n: sizeof (struct tree_decl_common) - sizeof (struct tree_common)); |
3151 | |
3152 | switch (TREE_CODE (newdecl)) |
3153 | { |
3154 | case LABEL_DECL: |
3155 | case VAR_DECL: |
3156 | case RESULT_DECL: |
3157 | case PARM_DECL: |
3158 | case FIELD_DECL: |
3159 | case TYPE_DECL: |
3160 | case CONST_DECL: |
3161 | { |
3162 | struct symtab_node *snode = NULL; |
3163 | |
3164 | if (VAR_P (newdecl) |
3165 | && (TREE_STATIC (olddecl) || TREE_PUBLIC (olddecl) |
3166 | || DECL_EXTERNAL (olddecl))) |
3167 | snode = symtab_node::get (decl: olddecl); |
3168 | memcpy (dest: (char *) olddecl + sizeof (struct tree_decl_common), |
3169 | src: (char *) newdecl + sizeof (struct tree_decl_common), |
3170 | n: size - sizeof (struct tree_decl_common) |
3171 | + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *)); |
3172 | if (VAR_P (newdecl)) |
3173 | olddecl->decl_with_vis.symtab_node = snode; |
3174 | } |
3175 | break; |
3176 | default: |
3177 | memcpy (dest: (char *) olddecl + sizeof (struct tree_decl_common), |
3178 | src: (char *) newdecl + sizeof (struct tree_decl_common), |
3179 | n: sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common) |
3180 | + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *)); |
3181 | break; |
3182 | } |
3183 | } |
3184 | |
3185 | if (VAR_OR_FUNCTION_DECL_P (newdecl)) |
3186 | { |
3187 | if (DECL_EXTERNAL (olddecl) |
3188 | || TREE_PUBLIC (olddecl) |
3189 | || TREE_STATIC (olddecl)) |
3190 | { |
3191 | /* Merge the section attribute. |
3192 | We want to issue an error if the sections conflict but that must be |
3193 | done later in decl_attributes since we are called before attributes |
3194 | are assigned. */ |
3195 | if (DECL_SECTION_NAME (newdecl) != NULL) |
3196 | set_decl_section_name (olddecl, newdecl); |
3197 | |
3198 | if (DECL_ONE_ONLY (newdecl)) |
3199 | { |
3200 | struct symtab_node *oldsym, *newsym; |
3201 | if (TREE_CODE (olddecl) == FUNCTION_DECL) |
3202 | oldsym = cgraph_node::get_create (olddecl); |
3203 | else |
3204 | oldsym = varpool_node::get_create (decl: olddecl); |
3205 | newsym = symtab_node::get (decl: newdecl); |
3206 | oldsym->set_comdat_group (newsym->get_comdat_group ()); |
3207 | } |
3208 | } |
3209 | |
3210 | if (VAR_P (newdecl) |
3211 | && CP_DECL_THREAD_LOCAL_P (newdecl)) |
3212 | { |
3213 | CP_DECL_THREAD_LOCAL_P (olddecl) = true; |
3214 | if (!processing_template_decl) |
3215 | set_decl_tls_model (olddecl, DECL_TLS_MODEL (newdecl)); |
3216 | } |
3217 | } |
3218 | |
3219 | DECL_UID (olddecl) = olddecl_uid; |
3220 | |
3221 | /* NEWDECL contains the merged attribute lists. |
3222 | Update OLDDECL to be the same. */ |
3223 | DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl); |
3224 | |
3225 | /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl |
3226 | so that encode_section_info has a chance to look at the new decl |
3227 | flags and attributes. */ |
3228 | if (DECL_RTL_SET_P (olddecl) |
3229 | && (TREE_CODE (olddecl) == FUNCTION_DECL |
3230 | || (VAR_P (olddecl) |
3231 | && TREE_STATIC (olddecl)))) |
3232 | make_decl_rtl (olddecl); |
3233 | |
3234 | /* The NEWDECL will no longer be needed. Because every out-of-class |
3235 | declaration of a member results in a call to duplicate_decls, |
3236 | freeing these nodes represents in a significant savings. |
3237 | |
3238 | Before releasing the node, be sore to remove function from symbol |
3239 | table that might have been inserted there to record comdat group. |
3240 | Be sure to however do not free DECL_STRUCT_FUNCTION because this |
3241 | structure is shared in between newdecl and oldecl. */ |
3242 | if (TREE_CODE (newdecl) == FUNCTION_DECL) |
3243 | DECL_STRUCT_FUNCTION (newdecl) = NULL; |
3244 | if (VAR_OR_FUNCTION_DECL_P (newdecl)) |
3245 | { |
3246 | struct symtab_node *snode = symtab_node::get (decl: newdecl); |
3247 | if (snode) |
3248 | snode->remove (); |
3249 | } |
3250 | |
3251 | if (TREE_CODE (olddecl) == FUNCTION_DECL) |
3252 | { |
3253 | tree clone; |
3254 | FOR_EACH_CLONE (clone, olddecl) |
3255 | { |
3256 | DECL_ATTRIBUTES (clone) = DECL_ATTRIBUTES (olddecl); |
3257 | DECL_PRESERVE_P (clone) |= DECL_PRESERVE_P (olddecl); |
3258 | } |
3259 | } |
3260 | |
3261 | /* Remove the associated constraints for newdecl, if any, before |
3262 | reclaiming memory. */ |
3263 | if (flag_concepts) |
3264 | remove_constraints (newdecl); |
3265 | |
3266 | ggc_free (newdecl); |
3267 | |
3268 | return olddecl; |
3269 | } |
3270 | |
3271 | /* Return zero if the declaration NEWDECL is valid |
3272 | when the declaration OLDDECL (assumed to be for the same name) |
3273 | has already been seen. |
3274 | Otherwise return an error message format string with a %s |
3275 | where the identifier should go. */ |
3276 | |
3277 | static const char * |
3278 | redeclaration_error_message (tree newdecl, tree olddecl) |
3279 | { |
3280 | if (TREE_CODE (newdecl) == TYPE_DECL) |
3281 | { |
3282 | /* Because C++ can put things into name space for free, |
3283 | constructs like "typedef struct foo { ... } foo" |
3284 | would look like an erroneous redeclaration. */ |
3285 | if (same_type_p (TREE_TYPE (newdecl), TREE_TYPE (olddecl))) |
3286 | return NULL; |
3287 | else |
3288 | return G_("redefinition of %q#D" ); |
3289 | } |
3290 | else if (TREE_CODE (newdecl) == FUNCTION_DECL) |
3291 | { |
3292 | /* If this is a pure function, its olddecl will actually be |
3293 | the original initialization to `0' (which we force to call |
3294 | abort()). Don't complain about redefinition in this case. */ |
3295 | if (DECL_LANG_SPECIFIC (olddecl) && DECL_PURE_VIRTUAL_P (olddecl) |
3296 | && DECL_INITIAL (olddecl) == NULL_TREE) |
3297 | return NULL; |
3298 | |
3299 | /* If both functions come from different namespaces, this is not |
3300 | a redeclaration - this is a conflict with a used function. */ |
3301 | if (DECL_NAMESPACE_SCOPE_P (olddecl) |
3302 | && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl) |
3303 | && ! decls_match (newdecl: olddecl, olddecl: newdecl)) |
3304 | return G_("%qD conflicts with used function" ); |
3305 | |
3306 | /* We'll complain about linkage mismatches in |
3307 | warn_extern_redeclared_static. */ |
3308 | |
3309 | /* Defining the same name twice is no good. */ |
3310 | if (decl_defined_p (olddecl) |
3311 | && decl_defined_p (newdecl)) |
3312 | { |
3313 | if (DECL_NAME (olddecl) == NULL_TREE) |
3314 | return G_("%q#D not declared in class" ); |
3315 | else if (!GNU_INLINE_P (olddecl) |
3316 | || GNU_INLINE_P (newdecl)) |
3317 | return G_("redefinition of %q#D" ); |
3318 | } |
3319 | |
3320 | if (DECL_DECLARED_INLINE_P (olddecl) && DECL_DECLARED_INLINE_P (newdecl)) |
3321 | { |
3322 | bool olda = GNU_INLINE_P (olddecl); |
3323 | bool newa = GNU_INLINE_P (newdecl); |
3324 | |
3325 | if (olda != newa) |
3326 | { |
3327 | if (newa) |
3328 | return G_("%q+D redeclared inline with " |
3329 | "%<gnu_inline%> attribute" ); |
3330 | else |
3331 | return G_("%q+D redeclared inline without " |
3332 | "%<gnu_inline%> attribute" ); |
3333 | } |
3334 | } |
3335 | |
3336 | /* [class.compare.default]: A definition of a comparison operator as |
3337 | defaulted that appears in a class shall be the first declaration of |
3338 | that function. */ |
3339 | special_function_kind sfk = special_function_p (olddecl); |
3340 | if (sfk == sfk_comparison && DECL_DEFAULTED_FN (newdecl)) |
3341 | return G_("comparison operator %q+D defaulted after " |
3342 | "its first declaration" ); |
3343 | |
3344 | check_abi_tag_redeclaration |
3345 | (olddecl, lookup_attribute (attr_name: "abi_tag" , DECL_ATTRIBUTES (olddecl)), |
3346 | lookup_attribute (attr_name: "abi_tag" , DECL_ATTRIBUTES (newdecl))); |
3347 | |
3348 | return NULL; |
3349 | } |
3350 | else if (TREE_CODE (newdecl) == TEMPLATE_DECL) |
3351 | { |
3352 | tree nt, ot; |
3353 | |
3354 | if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == CONCEPT_DECL) |
3355 | return G_("redefinition of %q#D" ); |
3356 | |
3357 | if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) != FUNCTION_DECL) |
3358 | return redeclaration_error_message (DECL_TEMPLATE_RESULT (newdecl), |
3359 | DECL_TEMPLATE_RESULT (olddecl)); |
3360 | |
3361 | if (DECL_TEMPLATE_RESULT (newdecl) == DECL_TEMPLATE_RESULT (olddecl)) |
3362 | return NULL; |
3363 | |
3364 | nt = DECL_TEMPLATE_RESULT (newdecl); |
3365 | if (DECL_TEMPLATE_INFO (nt)) |
3366 | nt = DECL_TEMPLATE_RESULT (template_for_substitution (nt)); |
3367 | ot = DECL_TEMPLATE_RESULT (olddecl); |
3368 | if (DECL_TEMPLATE_INFO (ot)) |
3369 | ot = DECL_TEMPLATE_RESULT (template_for_substitution (ot)); |
3370 | if (DECL_INITIAL (nt) && DECL_INITIAL (ot) |
3371 | && (!GNU_INLINE_P (ot) || GNU_INLINE_P (nt))) |
3372 | return G_("redefinition of %q#D" ); |
3373 | |
3374 | if (DECL_DECLARED_INLINE_P (ot) && DECL_DECLARED_INLINE_P (nt)) |
3375 | { |
3376 | bool olda = GNU_INLINE_P (ot); |
3377 | bool newa = GNU_INLINE_P (nt); |
3378 | |
3379 | if (olda != newa) |
3380 | { |
3381 | if (newa) |
3382 | return G_("%q+D redeclared inline with " |
3383 | "%<gnu_inline%> attribute" ); |
3384 | else |
3385 | return G_("%q+D redeclared inline without " |
3386 | "%<gnu_inline%> attribute" ); |
3387 | } |
3388 | } |
3389 | |
3390 | /* Core issue #226 (C++11): |
3391 | |
3392 | If a friend function template declaration specifies a |
3393 | default template-argument, that declaration shall be a |
3394 | definition and shall be the only declaration of the |
3395 | function template in the translation unit. */ |
3396 | if ((cxx_dialect != cxx98) |
3397 | && TREE_CODE (ot) == FUNCTION_DECL && DECL_UNIQUE_FRIEND_P (ot) |
3398 | && !check_default_tmpl_args (nt, DECL_TEMPLATE_PARMS (newdecl), |
3399 | /*is_primary=*/true, |
3400 | /*is_partial=*/false, |
3401 | /*is_friend_decl=*/2)) |
3402 | return G_("redeclaration of friend %q#D " |
3403 | "may not have default template arguments" ); |
3404 | |
3405 | return NULL; |
3406 | } |
3407 | else if (VAR_P (newdecl) |
3408 | && (CP_DECL_THREAD_LOCAL_P (newdecl) |
3409 | != CP_DECL_THREAD_LOCAL_P (olddecl)) |
3410 | && (! DECL_LANG_SPECIFIC (olddecl) |
3411 | || ! CP_DECL_THREADPRIVATE_P (olddecl) |
3412 | || CP_DECL_THREAD_LOCAL_P (newdecl))) |
3413 | { |
3414 | /* Only variables can be thread-local, and all declarations must |
3415 | agree on this property. */ |
3416 | if (CP_DECL_THREAD_LOCAL_P (newdecl)) |
3417 | return G_("thread-local declaration of %q#D follows " |
3418 | "non-thread-local declaration" ); |
3419 | else |
3420 | return G_("non-thread-local declaration of %q#D follows " |
3421 | "thread-local declaration" ); |
3422 | } |
3423 | else if (toplevel_bindings_p () || DECL_NAMESPACE_SCOPE_P (newdecl)) |
3424 | { |
3425 | /* The objects have been declared at namespace scope. If either |
3426 | is a member of an anonymous union, then this is an invalid |
3427 | redeclaration. For example: |
3428 | |
3429 | int i; |
3430 | union { int i; }; |
3431 | |
3432 | is invalid. */ |
3433 | if ((VAR_P (newdecl) && DECL_ANON_UNION_VAR_P (newdecl)) |
3434 | || (VAR_P (olddecl) && DECL_ANON_UNION_VAR_P (olddecl))) |
3435 | return G_("redeclaration of %q#D" ); |
3436 | /* If at least one declaration is a reference, there is no |
3437 | conflict. For example: |
3438 | |
3439 | int i = 3; |
3440 | extern int i; |
3441 | |
3442 | is valid. */ |
3443 | if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl)) |
3444 | return NULL; |
3445 | |
3446 | /* Static data member declared outside a class definition |
3447 | if the variable is defined within the class with constexpr |
3448 | specifier is declaration rather than definition (and |
3449 | deprecated). */ |
3450 | if (cxx_dialect >= cxx17 |
3451 | && VAR_P (olddecl) |
3452 | && DECL_CLASS_SCOPE_P (olddecl) |
3453 | && DECL_DECLARED_CONSTEXPR_P (olddecl) |
3454 | && !DECL_INITIAL (newdecl)) |
3455 | { |
3456 | DECL_EXTERNAL (newdecl) = 1; |
3457 | /* For now, only warn with explicit -Wdeprecated. */ |
3458 | if (OPTION_SET_P (warn_deprecated)) |
3459 | { |
3460 | auto_diagnostic_group d; |
3461 | if (warning_at (DECL_SOURCE_LOCATION (newdecl), OPT_Wdeprecated, |
3462 | "redundant redeclaration of %<constexpr%> " |
3463 | "static data member %qD" , newdecl)) |
3464 | inform (DECL_SOURCE_LOCATION (olddecl), |
3465 | "previous declaration of %qD" , olddecl); |
3466 | } |
3467 | return NULL; |
3468 | } |
3469 | |
3470 | /* Reject two definitions. */ |
3471 | return G_("redefinition of %q#D" ); |
3472 | } |
3473 | else |
3474 | { |
3475 | /* Objects declared with block scope: */ |
3476 | /* Reject two definitions, and reject a definition |
3477 | together with an external reference. */ |
3478 | if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl))) |
3479 | return G_("redeclaration of %q#D" ); |
3480 | return NULL; |
3481 | } |
3482 | } |
3483 | |
3484 | |
3485 | /* Hash and equality functions for the named_label table. */ |
3486 | |
3487 | hashval_t |
3488 | named_label_hash::hash (const value_type entry) |
3489 | { |
3490 | return IDENTIFIER_HASH_VALUE (entry->name); |
3491 | } |
3492 | |
3493 | bool |
3494 | named_label_hash::equal (const value_type entry, compare_type name) |
3495 | { |
3496 | return name == entry->name; |
3497 | } |
3498 | |
3499 | /* Look for a label named ID in the current function. If one cannot |
3500 | be found, create one. Return the named_label_entry, or NULL on |
3501 | failure. */ |
3502 | |
3503 | static named_label_entry * |
3504 | lookup_label_1 (tree id, bool making_local_p) |
3505 | { |
3506 | auto_cond_timevar tv (TV_NAME_LOOKUP); |
3507 | |
3508 | /* You can't use labels at global scope. */ |
3509 | if (current_function_decl == NULL_TREE) |
3510 | { |
3511 | error ("label %qE referenced outside of any function" , id); |
3512 | return NULL; |
3513 | } |
3514 | |
3515 | if (!named_labels) |
3516 | named_labels = hash_table<named_label_hash>::create_ggc (n: 13); |
3517 | |
3518 | hashval_t hash = IDENTIFIER_HASH_VALUE (id); |
3519 | named_label_entry **slot |
3520 | = named_labels->find_slot_with_hash (comparable: id, hash, insert: INSERT); |
3521 | named_label_entry *old = *slot; |
3522 | |
3523 | if (old && old->label_decl) |
3524 | { |
3525 | if (!making_local_p) |
3526 | return old; |
3527 | |
3528 | if (old->binding_level == current_binding_level) |
3529 | { |
3530 | error ("local label %qE conflicts with existing label" , id); |
3531 | inform (DECL_SOURCE_LOCATION (old->label_decl), "previous label" ); |
3532 | return NULL; |
3533 | } |
3534 | } |
3535 | |
3536 | /* We are making a new decl, create or reuse the named_label_entry */ |
3537 | named_label_entry *ent = NULL; |
3538 | if (old && !old->label_decl) |
3539 | ent = old; |
3540 | else |
3541 | { |
3542 | ent = ggc_cleared_alloc<named_label_entry> (); |
3543 | ent->name = id; |
3544 | ent->outer = old; |
3545 | *slot = ent; |
3546 | } |
3547 | |
3548 | /* Now create the LABEL_DECL. */ |
3549 | tree decl = build_decl (input_location, LABEL_DECL, id, void_type_node); |
3550 | |
3551 | DECL_CONTEXT (decl) = current_function_decl; |
3552 | SET_DECL_MODE (decl, VOIDmode); |
3553 | if (making_local_p) |
3554 | { |
3555 | C_DECLARED_LABEL_FLAG (decl) = true; |
3556 | DECL_CHAIN (decl) = current_binding_level->names; |
3557 | current_binding_level->names = decl; |
3558 | } |
3559 | |
3560 | ent->label_decl = decl; |
3561 | |
3562 | return ent; |
3563 | } |
3564 | |
3565 | /* Wrapper for lookup_label_1. */ |
3566 | |
3567 | tree |
3568 | lookup_label (tree id) |
3569 | { |
3570 | named_label_entry *ent = lookup_label_1 (id, making_local_p: false); |
3571 | return ent ? ent->label_decl : NULL_TREE; |
3572 | } |
3573 | |
3574 | tree |
3575 | declare_local_label (tree id) |
3576 | { |
3577 | named_label_entry *ent = lookup_label_1 (id, making_local_p: true); |
3578 | return ent ? ent->label_decl : NULL_TREE; |
3579 | } |
3580 | |
3581 | /* Returns true if it is ill-formed to jump past the declaration of DECL. */ |
3582 | |
3583 | static bool |
3584 | decl_jump_unsafe (tree decl) |
3585 | { |
3586 | /* [stmt.dcl]/3: A program that jumps from a point where a local variable |
3587 | with automatic storage duration is not in scope to a point where it is |
3588 | in scope is ill-formed unless the variable has scalar type, class type |
3589 | with a trivial default constructor and a trivial destructor, a |
3590 | cv-qualified version of one of these types, or an array of one of the |
3591 | preceding types and is declared without an initializer (8.5). */ |
3592 | tree type = TREE_TYPE (decl); |
3593 | |
3594 | return (type != error_mark_node |
3595 | && VAR_P (decl) |
3596 | && !TREE_STATIC (decl) |
3597 | && (DECL_NONTRIVIALLY_INITIALIZED_P (decl) |
3598 | || variably_modified_type_p (type, NULL_TREE))); |
3599 | } |
3600 | |
3601 | /* A subroutine of check_previous_goto_1 and check_goto to identify a branch |
3602 | to the user. */ |
3603 | |
3604 | static bool |
3605 | identify_goto (tree decl, location_t loc, const location_t *locus, |
3606 | diagnostic_t diag_kind) |
3607 | { |
3608 | bool complained |
3609 | = emit_diagnostic (diag_kind, loc, 0, |
3610 | decl ? G_("jump to label %qD" ) |
3611 | : G_("jump to case label" ), decl); |
3612 | if (complained && locus) |
3613 | inform (*locus, " from here" ); |
3614 | return complained; |
3615 | } |
3616 | |
3617 | /* Check that a single previously seen jump to a newly defined label |
3618 | is OK. DECL is the LABEL_DECL or 0; LEVEL is the binding_level for |
3619 | the jump context; NAMES are the names in scope in LEVEL at the jump |
3620 | context; LOCUS is the source position of the jump or 0. Returns |
3621 | true if all is well. */ |
3622 | |
3623 | static bool |
3624 | check_previous_goto_1 (tree decl, cp_binding_level* level, tree names, |
3625 | bool exited_omp, const location_t *locus) |
3626 | { |
3627 | cp_binding_level *b; |
3628 | bool complained = false; |
3629 | int identified = 0; |
3630 | bool saw_eh = false, saw_omp = false, saw_tm = false, saw_cxif = false; |
3631 | bool saw_ceif = false, saw_se = false; |
3632 | |
3633 | if (exited_omp) |
3634 | { |
3635 | complained = identify_goto (decl, loc: input_location, locus, diag_kind: DK_ERROR); |
3636 | if (complained) |
3637 | inform (input_location, " exits OpenMP structured block" ); |
3638 | saw_omp = true; |
3639 | identified = 2; |
3640 | } |
3641 | |
3642 | for (b = current_binding_level; b ; b = b->level_chain) |
3643 | { |
3644 | tree new_decls, old_decls = (b == level ? names : NULL_TREE); |
3645 | |
3646 | for (new_decls = b->names; new_decls != old_decls; |
3647 | new_decls = (DECL_P (new_decls) ? DECL_CHAIN (new_decls) |
3648 | : TREE_CHAIN (new_decls))) |
3649 | { |
3650 | bool problem = decl_jump_unsafe (decl: new_decls); |
3651 | if (! problem) |
3652 | continue; |
3653 | |
3654 | if (!identified) |
3655 | { |
3656 | complained = identify_goto (decl, loc: input_location, locus, diag_kind: DK_ERROR); |
3657 | identified = 1; |
3658 | } |
3659 | if (complained) |
3660 | inform (DECL_SOURCE_LOCATION (new_decls), |
3661 | " crosses initialization of %q#D" , new_decls); |
3662 | } |
3663 | |
3664 | if (b == level) |
3665 | break; |
3666 | |
3667 | const char *inf = NULL; |
3668 | location_t loc = input_location; |
3669 | switch (b->kind) |
3670 | { |
3671 | case sk_try: |
3672 | if (!saw_eh) |
3673 | inf = G_(" enters %<try%> block" ); |
3674 | saw_eh = true; |
3675 | break; |
3676 | |
3677 | case sk_catch: |
3678 | if (!saw_eh) |
3679 | inf = G_(" enters %<catch%> block" ); |
3680 | saw_eh = true; |
3681 | break; |
3682 | |
3683 | case sk_omp: |
3684 | if (!saw_omp) |
3685 | inf = G_(" enters OpenMP structured block" ); |
3686 | saw_omp = true; |
3687 | break; |
3688 | |
3689 | case sk_transaction: |
3690 | if (!saw_tm) |
3691 | inf = G_(" enters synchronized or atomic statement" ); |
3692 | saw_tm = true; |
3693 | break; |
3694 | |
3695 | case sk_stmt_expr: |
3696 | if (!saw_se) |
3697 | inf = G_(" enters statement expression" ); |
3698 | saw_se = true; |
3699 | break; |
3700 | |
3701 | case sk_block: |
3702 | if (!saw_cxif && level_for_constexpr_if (b: b->level_chain)) |
3703 | { |
3704 | inf = G_(" enters %<constexpr if%> statement" ); |
3705 | loc = EXPR_LOCATION (b->level_chain->this_entity); |
3706 | saw_cxif = true; |
3707 | } |
3708 | else if (!saw_ceif && level_for_consteval_if (b: b->level_chain)) |
3709 | { |
3710 | inf = G_(" enters %<consteval if%> statement" ); |
3711 | loc = EXPR_LOCATION (b->level_chain->this_entity); |
3712 | saw_ceif = true; |
3713 | } |
3714 | break; |
3715 | |
3716 | default: |
3717 | break; |
3718 | } |
3719 | |
3720 | if (inf) |
3721 | { |
3722 | if (identified < 2) |
3723 | complained = identify_goto (decl, loc: input_location, locus, diag_kind: DK_ERROR); |
3724 | identified = 2; |
3725 | if (complained) |
3726 | inform (loc, inf); |
3727 | } |
3728 | } |
3729 | |
3730 | return !identified; |
3731 | } |
3732 | |
3733 | static void |
3734 | check_previous_goto (tree decl, struct named_label_use_entry *use) |
3735 | { |
3736 | check_previous_goto_1 (decl, level: use->binding_level, |
3737 | names: use->names_in_scope, exited_omp: use->in_omp_scope, |
3738 | locus: &use->o_goto_locus); |
3739 | } |
3740 | |
3741 | static bool |
3742 | check_switch_goto (cp_binding_level* level) |
3743 | { |
3744 | return check_previous_goto_1 (NULL_TREE, level, names: level->names, exited_omp: false, NULL); |
3745 | } |
3746 | |
3747 | /* Check that a new jump to a label DECL is OK. Called by |
3748 | finish_goto_stmt. */ |
3749 | |
3750 | void |
3751 | check_goto (tree decl) |
3752 | { |
3753 | /* We can't know where a computed goto is jumping. |
3754 | So we assume that it's OK. */ |
3755 | if (TREE_CODE (decl) != LABEL_DECL) |
3756 | return; |
3757 | |
3758 | hashval_t hash = IDENTIFIER_HASH_VALUE (DECL_NAME (decl)); |
3759 | named_label_entry **slot |
3760 | = named_labels->find_slot_with_hash (DECL_NAME (decl), hash, insert: NO_INSERT); |
3761 | named_label_entry *ent = *slot; |
3762 | |
3763 | /* If the label hasn't been defined yet, defer checking. */ |
3764 | if (! DECL_INITIAL (decl)) |
3765 | { |
3766 | /* Don't bother creating another use if the last goto had the |
3767 | same data, and will therefore create the same set of errors. */ |
3768 | if (ent->uses |
3769 | && ent->uses->names_in_scope == current_binding_level->names) |
3770 | return; |
3771 | |
3772 | named_label_use_entry *new_use |
3773 | = ggc_alloc<named_label_use_entry> (); |
3774 | new_use->binding_level = current_binding_level; |
3775 | new_use->names_in_scope = current_binding_level->names; |
3776 | new_use->o_goto_locus = input_location; |
3777 | new_use->in_omp_scope = false; |
3778 | |
3779 | new_use->next = ent->uses; |
3780 | ent->uses = new_use; |
3781 | return; |
3782 | } |
3783 | |
3784 | bool saw_catch = false, complained = false; |
3785 | int identified = 0; |
3786 | tree bad; |
3787 | unsigned ix; |
3788 | |
3789 | if (ent->in_try_scope || ent->in_catch_scope || ent->in_transaction_scope |
3790 | || ent->in_constexpr_if || ent->in_consteval_if |
3791 | || ent->in_omp_scope || ent->in_stmt_expr |
3792 | || !vec_safe_is_empty (v: ent->bad_decls)) |
3793 | { |
3794 | diagnostic_t diag_kind = DK_PERMERROR; |
3795 | if (ent->in_try_scope || ent->in_catch_scope || ent->in_constexpr_if |
3796 | || ent->in_consteval_if || ent->in_transaction_scope |
3797 | || ent->in_omp_scope || ent->in_stmt_expr) |
3798 | diag_kind = DK_ERROR; |
3799 | complained = identify_goto (decl, DECL_SOURCE_LOCATION (decl), |
3800 | locus: &input_location, diag_kind); |
3801 | identified = 1 + (diag_kind == DK_ERROR); |
3802 | } |
3803 | |
3804 | FOR_EACH_VEC_SAFE_ELT (ent->bad_decls, ix, bad) |
3805 | { |
3806 | bool problem = decl_jump_unsafe (decl: bad); |
3807 | |
3808 | if (problem && DECL_ARTIFICIAL (bad)) |
3809 | { |
3810 | /* Can't skip init of __exception_info. */ |
3811 | if (identified == 1) |
3812 | { |
3813 | complained = identify_goto (decl, DECL_SOURCE_LOCATION (decl), |
3814 | locus: &input_location, diag_kind: DK_ERROR); |
3815 | identified = 2; |
3816 | } |
3817 | if (complained) |
3818 | inform (DECL_SOURCE_LOCATION (bad), " enters %<catch%> block" ); |
3819 | saw_catch = true; |
3820 | } |
3821 | else if (complained) |
3822 | inform (DECL_SOURCE_LOCATION (bad), |
3823 | " skips initialization of %q#D" , bad); |
3824 | } |
3825 | |
3826 | if (complained) |
3827 | { |
3828 | if (ent->in_try_scope) |
3829 | inform (input_location, " enters %<try%> block" ); |
3830 | else if (ent->in_catch_scope && !saw_catch) |
3831 | inform (input_location, " enters %<catch%> block" ); |
3832 | else if (ent->in_transaction_scope) |
3833 | inform (input_location, " enters synchronized or atomic statement" ); |
3834 | else if (ent->in_constexpr_if) |
3835 | inform (input_location, " enters %<constexpr if%> statement" ); |
3836 | else if (ent->in_consteval_if) |
3837 | inform (input_location, " enters %<consteval if%> statement" ); |
3838 | else if (ent->in_stmt_expr) |
3839 | inform (input_location, " enters statement expression" ); |
3840 | } |
3841 | |
3842 | if (ent->in_omp_scope) |
3843 | { |
3844 | if (complained) |
3845 | inform (input_location, " enters OpenMP structured block" ); |
3846 | } |
3847 | else if (flag_openmp) |
3848 | for (cp_binding_level *b = current_binding_level; b ; b = b->level_chain) |
3849 | { |
3850 | if (b == ent->binding_level) |
3851 | break; |
3852 | if (b->kind == sk_omp) |
3853 | { |
3854 | if (identified < 2) |
3855 | { |
3856 | complained = identify_goto (decl, |
3857 | DECL_SOURCE_LOCATION (decl), |
3858 | locus: &input_location, diag_kind: DK_ERROR); |
3859 | identified = 2; |
3860 | } |
3861 | if (complained) |
3862 | inform (input_location, " exits OpenMP structured block" ); |
3863 | break; |
3864 | } |
3865 | } |
3866 | } |
3867 | |
3868 | /* Check that a return is ok wrt OpenMP structured blocks. |
3869 | Called by finish_return_stmt. Returns true if all is well. */ |
3870 | |
3871 | bool |
3872 | check_omp_return (void) |
3873 | { |
3874 | for (cp_binding_level *b = current_binding_level; b ; b = b->level_chain) |
3875 | if (b->kind == sk_omp) |
3876 | { |
3877 | error ("invalid exit from OpenMP structured block" ); |
3878 | return false; |
3879 | } |
3880 | else if (b->kind == sk_function_parms) |
3881 | break; |
3882 | return true; |
3883 | } |
3884 | |
3885 | /* Define a label, specifying the location in the source file. |
3886 | Return the LABEL_DECL node for the label. */ |
3887 | |
3888 | tree |
3889 | define_label (location_t location, tree name) |
3890 | { |
3891 | auto_cond_timevar tv (TV_NAME_LOOKUP); |
3892 | |
3893 | /* After labels, make any new cleanups in the function go into their |
3894 | own new (temporary) binding contour. */ |
3895 | for (cp_binding_level *p = current_binding_level; |
3896 | p->kind != sk_function_parms; |
3897 | p = p->level_chain) |
3898 | p->more_cleanups_ok = 0; |
3899 | |
3900 | named_label_entry *ent = lookup_label_1 (id: name, making_local_p: false); |
3901 | tree decl = ent->label_decl; |
3902 | |
3903 | if (DECL_INITIAL (decl) != NULL_TREE) |
3904 | { |
3905 | error ("duplicate label %qD" , decl); |
3906 | return error_mark_node; |
3907 | } |
3908 | else |
3909 | { |
3910 | /* Mark label as having been defined. */ |
3911 | DECL_INITIAL (decl) = error_mark_node; |
3912 | /* Say where in the source. */ |
3913 | DECL_SOURCE_LOCATION (decl) = location; |
3914 | |
3915 | ent->binding_level = current_binding_level; |
3916 | ent->names_in_scope = current_binding_level->names; |
3917 | |
3918 | for (named_label_use_entry *use = ent->uses; use; use = use->next) |
3919 | check_previous_goto (decl, use); |
3920 | ent->uses = NULL; |
3921 | } |
3922 | |
3923 | return decl; |
3924 | } |
3925 | |
3926 | struct cp_switch |
3927 | { |
3928 | cp_binding_level *level; |
3929 | struct cp_switch *next; |
3930 | /* The SWITCH_STMT being built. */ |
3931 | tree switch_stmt; |
3932 | /* A splay-tree mapping the low element of a case range to the high |
3933 | element, or NULL_TREE if there is no high element. Used to |
3934 | determine whether or not a new case label duplicates an old case |
3935 | label. We need a tree, rather than simply a hash table, because |
3936 | of the GNU case range extension. */ |
3937 | splay_tree cases; |
3938 | /* Remember whether a default: case label has been seen. */ |
3939 | bool has_default_p; |
3940 | /* Remember whether a BREAK_STMT has been seen in this SWITCH_STMT. */ |
3941 | bool break_stmt_seen_p; |
3942 | /* Set if inside of {FOR,DO,WHILE}_BODY nested inside of a switch, |
3943 | where BREAK_STMT doesn't belong to the SWITCH_STMT. */ |
3944 | bool in_loop_body_p; |
3945 | }; |
3946 | |
3947 | /* A stack of the currently active switch statements. The innermost |
3948 | switch statement is on the top of the stack. There is no need to |
3949 | mark the stack for garbage collection because it is only active |
3950 | during the processing of the body of a function, and we never |
3951 | collect at that point. */ |
3952 | |
3953 | static struct cp_switch *switch_stack; |
3954 | |
3955 | /* Called right after a switch-statement condition is parsed. |
3956 | SWITCH_STMT is the switch statement being parsed. */ |
3957 | |
3958 | void |
3959 | push_switch (tree switch_stmt) |
3960 | { |
3961 | struct cp_switch *p = XNEW (struct cp_switch); |
3962 | p->level = current_binding_level; |
3963 | p->next = switch_stack; |
3964 | p->switch_stmt = switch_stmt; |
3965 | p->cases = splay_tree_new (case_compare, NULL, NULL); |
3966 | p->has_default_p = false; |
3967 | p->break_stmt_seen_p = false; |
3968 | p->in_loop_body_p = false; |
3969 | switch_stack = p; |
3970 | } |
3971 | |
3972 | void |
3973 | pop_switch (void) |
3974 | { |
3975 | struct cp_switch *cs = switch_stack; |
3976 | |
3977 | /* Emit warnings as needed. */ |
3978 | location_t switch_location = cp_expr_loc_or_input_loc (t: cs->switch_stmt); |
3979 | tree cond = SWITCH_STMT_COND (cs->switch_stmt); |
3980 | const bool bool_cond_p |
3981 | = (SWITCH_STMT_TYPE (cs->switch_stmt) |
3982 | && TREE_CODE (SWITCH_STMT_TYPE (cs->switch_stmt)) == BOOLEAN_TYPE); |
3983 | if (!processing_template_decl) |
3984 | c_do_switch_warnings (cs->cases, switch_location, |
3985 | SWITCH_STMT_TYPE (cs->switch_stmt), cond, |
3986 | bool_cond_p); |
3987 | |
3988 | /* For the benefit of block_may_fallthru remember if the switch body |
3989 | case labels cover all possible values and if there are break; stmts. */ |
3990 | if (cs->has_default_p |
3991 | || (!processing_template_decl |
3992 | && c_switch_covers_all_cases_p (cs->cases, |
3993 | SWITCH_STMT_TYPE (cs->switch_stmt)))) |
3994 | SWITCH_STMT_ALL_CASES_P (cs->switch_stmt) = 1; |
3995 | if (!cs->break_stmt_seen_p) |
3996 | SWITCH_STMT_NO_BREAK_P (cs->switch_stmt) = 1; |
3997 | /* Now that we're done with the switch warnings, set the switch type |
3998 | to the type of the condition if the index type was of scoped enum type. |
3999 | (Such types don't participate in the integer promotions.) We do this |
4000 | because of bit-fields whose declared type is a scoped enum type: |
4001 | gimplification will use the lowered index type, but convert the |
4002 | case values to SWITCH_STMT_TYPE, which would have been the declared type |
4003 | and verify_gimple_switch doesn't accept that. */ |
4004 | if (is_bitfield_expr_with_lowered_type (cond)) |
4005 | SWITCH_STMT_TYPE (cs->switch_stmt) = TREE_TYPE (cond); |
4006 | gcc_assert (!cs->in_loop_body_p); |
4007 | splay_tree_delete (cs->cases); |
4008 | switch_stack = switch_stack->next; |
4009 | free (ptr: cs); |
4010 | } |
4011 | |
4012 | /* Note that a BREAK_STMT is about to be added. If it is inside of |
4013 | a SWITCH_STMT and not inside of a loop body inside of it, note |
4014 | in switch_stack we've seen a BREAK_STMT. */ |
4015 | |
4016 | void |
4017 | note_break_stmt (void) |
4018 | { |
4019 | if (switch_stack && !switch_stack->in_loop_body_p) |
4020 | switch_stack->break_stmt_seen_p = true; |
4021 | } |
4022 | |
4023 | /* Note the start of processing of an iteration statement's body. |
4024 | The note_break_stmt function will do nothing while processing it. |
4025 | Return a flag that should be passed to note_iteration_stmt_body_end. */ |
4026 | |
4027 | bool |
4028 | note_iteration_stmt_body_start (void) |
4029 | { |
4030 | if (!switch_stack) |
4031 | return false; |
4032 | bool ret = switch_stack->in_loop_body_p; |
4033 | switch_stack->in_loop_body_p = true; |
4034 | return ret; |
4035 | } |
4036 | |
4037 | /* Note the end of processing of an iteration statement's body. */ |
4038 | |
4039 | void |
4040 | note_iteration_stmt_body_end (bool prev) |
4041 | { |
4042 | if (switch_stack) |
4043 | switch_stack->in_loop_body_p = prev; |
4044 | } |
4045 | |
4046 | /* Convert a case constant VALUE in a switch to the type TYPE of the switch |
4047 | condition. Note that if TYPE and VALUE are already integral we don't |
4048 | really do the conversion because the language-independent |
4049 | warning/optimization code will work better that way. */ |
4050 | |
4051 | static tree |
4052 | case_conversion (tree type, tree value) |
4053 | { |
4054 | if (value == NULL_TREE) |
4055 | return value; |
4056 | |
4057 | value = mark_rvalue_use (value); |
4058 | |
4059 | if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type)) |
4060 | type = type_promotes_to (type); |
4061 | |
4062 | tree ovalue = value; |
4063 | /* The constant-expression VALUE shall be a converted constant expression |
4064 | of the adjusted type of the switch condition, which doesn't allow |
4065 | narrowing conversions. */ |
4066 | value = build_converted_constant_expr (type, value, tf_warning_or_error); |
4067 | |
4068 | if (cxx_dialect >= cxx11 |
4069 | && (SCOPED_ENUM_P (type) |
4070 | || !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (ovalue)))) |
4071 | /* Use the converted value. */; |
4072 | else |
4073 | /* The already integral case. */ |
4074 | value = ovalue; |
4075 | |
4076 | return cxx_constant_value (value); |
4077 | } |
4078 | |
4079 | /* Note that we've seen a definition of a case label, and complain if this |
4080 | is a bad place for one. */ |
4081 | |
4082 | tree |
4083 | finish_case_label (location_t loc, tree low_value, tree high_value) |
4084 | { |
4085 | tree cond, r; |
4086 | cp_binding_level *p; |
4087 | tree type; |
4088 | |
4089 | if (low_value == NULL_TREE && high_value == NULL_TREE) |
4090 | switch_stack->has_default_p = true; |
4091 | |
4092 | if (processing_template_decl) |
4093 | { |
4094 | tree label; |
4095 | |
4096 | /* For templates, just add the case label; we'll do semantic |
4097 | analysis at instantiation-time. */ |
4098 | label = build_decl (loc, LABEL_DECL, NULL_TREE, void_type_node); |
4099 | return add_stmt (build_case_label (low_value, high_value, label)); |
4100 | } |
4101 | |
4102 | /* Find the condition on which this switch statement depends. */ |
4103 | cond = SWITCH_STMT_COND (switch_stack->switch_stmt); |
4104 | if (cond && TREE_CODE (cond) == TREE_LIST) |
4105 | cond = TREE_VALUE (cond); |
4106 | |
4107 | if (!check_switch_goto (level: switch_stack->level)) |
4108 | return error_mark_node; |
4109 | |
4110 | type = SWITCH_STMT_TYPE (switch_stack->switch_stmt); |
4111 | if (type == error_mark_node) |
4112 | return error_mark_node; |
4113 | |
4114 | low_value = case_conversion (type, value: low_value); |
4115 | high_value = case_conversion (type, value: high_value); |
4116 | |
4117 | r = c_add_case_label (loc, switch_stack->cases, cond, low_value, high_value); |
4118 | |
4119 | /* After labels, make any new cleanups in the function go into their |
4120 | own new (temporary) binding contour. */ |
4121 | for (p = current_binding_level; |
4122 | p->kind != sk_function_parms; |
4123 | p = p->level_chain) |
4124 | p->more_cleanups_ok = 0; |
4125 | |
4126 | return r; |
4127 | } |
4128 | |
4129 | struct typename_info { |
4130 | tree scope; |
4131 | tree name; |
4132 | tree template_id; |
4133 | bool enum_p; |
4134 | bool class_p; |
4135 | }; |
4136 | |
4137 | struct typename_hasher : ggc_ptr_hash<tree_node> |
4138 | { |
4139 | typedef typename_info *compare_type; |
4140 | |
4141 | /* Hash a TYPENAME_TYPE. */ |
4142 | |
4143 | static hashval_t |
4144 | hash (tree context, tree fullname) |
4145 | { |
4146 | hashval_t hash = 0; |
4147 | hash = iterative_hash_object (context, hash); |
4148 | hash = iterative_hash_object (fullname, hash); |
4149 | return hash; |
4150 | } |
4151 | |
4152 | static hashval_t |
4153 | hash (const typename_info *ti) |
4154 | { |
4155 | return typename_hasher::hash (context: ti->scope, fullname: ti->template_id); |
4156 | } |
4157 | |
4158 | static hashval_t |
4159 | hash (tree t) |
4160 | { |
4161 | return typename_hasher::hash (TYPE_CONTEXT (t), TYPENAME_TYPE_FULLNAME (t)); |
4162 | } |
4163 | |
4164 | /* Compare two TYPENAME_TYPEs. */ |
4165 | |
4166 | static bool |
4167 | equal (tree t1, const typename_info *t2) |
4168 | { |
4169 | return (TYPE_IDENTIFIER (t1) == t2->name |
4170 | && TYPE_CONTEXT (t1) == t2->scope |
4171 | && TYPENAME_TYPE_FULLNAME (t1) == t2->template_id |
4172 | && TYPENAME_IS_ENUM_P (t1) == t2->enum_p |
4173 | && TYPENAME_IS_CLASS_P (t1) == t2->class_p); |
4174 | } |
4175 | }; |
4176 | |
4177 | /* Build a TYPENAME_TYPE. If the type is `typename T::t', CONTEXT is |
4178 | the type of `T', NAME is the IDENTIFIER_NODE for `t'. |
4179 | |
4180 | Returns the new TYPENAME_TYPE. */ |
4181 | |
4182 | static GTY (()) hash_table<typename_hasher> *typename_htab; |
4183 | |
4184 | tree |
4185 | build_typename_type (tree context, tree name, tree fullname, |
4186 | enum tag_types tag_type) |
4187 | { |
4188 | typename_info ti; |
4189 | |
4190 | if (typename_htab == NULL) |
4191 | typename_htab = hash_table<typename_hasher>::create_ggc (n: 61); |
4192 | |
4193 | ti.scope = FROB_CONTEXT (context); |
4194 | ti.name = name; |
4195 | ti.template_id = fullname; |
4196 | ti.enum_p = tag_type == enum_type; |
4197 | ti.class_p = (tag_type == class_type |
4198 | || tag_type == record_type |
4199 | || tag_type == union_type); |
4200 | hashval_t hash = typename_hasher::hash (ti: &ti); |
4201 | |
4202 | /* See if we already have this type. */ |
4203 | tree *e = typename_htab->find_slot_with_hash (comparable: &ti, hash, insert: INSERT); |
4204 | tree t = *e; |
4205 | if (*e) |
4206 | t = *e; |
4207 | else |
4208 | { |
4209 | /* Build the TYPENAME_TYPE. */ |
4210 | t = cxx_make_type (TYPENAME_TYPE); |
4211 | TYPE_CONTEXT (t) = ti.scope; |
4212 | TYPENAME_TYPE_FULLNAME (t) = ti.template_id; |
4213 | TYPENAME_IS_ENUM_P (t) = ti.enum_p; |
4214 | TYPENAME_IS_CLASS_P (t) = ti.class_p; |
4215 | |
4216 | /* Build the corresponding TYPE_DECL. */ |
4217 | tree d = build_decl (input_location, TYPE_DECL, name, t); |
4218 | TYPE_NAME (t) = d; |
4219 | TYPE_STUB_DECL (t) = d; |
4220 | DECL_CONTEXT (d) = ti.scope; |
4221 | DECL_ARTIFICIAL (d) = 1; |
4222 | |
4223 | /* Store it in the hash table. */ |
4224 | *e = t; |
4225 | |
4226 | /* TYPENAME_TYPEs must always be compared structurally, because |
4227 | they may or may not resolve down to another type depending on |
4228 | the currently open classes. */ |
4229 | SET_TYPE_STRUCTURAL_EQUALITY (t); |
4230 | } |
4231 | |
4232 | return t; |
4233 | } |
4234 | |
4235 | /* Resolve `typename CONTEXT::NAME'. TAG_TYPE indicates the tag |
4236 | provided to name the type. Returns an appropriate type, unless an |
4237 | error occurs, in which case error_mark_node is returned. If we |
4238 | locate a non-artificial TYPE_DECL and TF_KEEP_TYPE_DECL is set, we |
4239 | return that, rather than the _TYPE it corresponds to, in other |
4240 | cases we look through the type decl. If TF_ERROR is set, complain |
4241 | about errors, otherwise be quiet. */ |
4242 | |
4243 | tree |
4244 | make_typename_type (tree context, tree name, enum tag_types tag_type, |
4245 | tsubst_flags_t complain) |
4246 | { |
4247 | tree fullname; |
4248 | tree t; |
4249 | bool want_template; |
4250 | |
4251 | if (name == error_mark_node |
4252 | || context == NULL_TREE |
4253 | || context == error_mark_node) |
4254 | return error_mark_node; |
4255 | |
4256 | if (TYPE_P (name)) |
4257 | { |
4258 | if (!(TYPE_LANG_SPECIFIC (name) |
4259 | && (CLASSTYPE_IS_TEMPLATE (name) |
4260 | || CLASSTYPE_USE_TEMPLATE (name)))) |
4261 | name = TYPE_IDENTIFIER (name); |
4262 | else |
4263 | /* Create a TEMPLATE_ID_EXPR for the type. */ |
4264 | name = build_nt (TEMPLATE_ID_EXPR, |
4265 | CLASSTYPE_TI_TEMPLATE (name), |
4266 | CLASSTYPE_TI_ARGS (name)); |
4267 | } |
4268 | else if (TREE_CODE (name) == TYPE_DECL) |
4269 | name = DECL_NAME (name); |
4270 | |
4271 | fullname = name; |
4272 | |
4273 | if (TREE_CODE (name) == TEMPLATE_ID_EXPR) |
4274 | { |
4275 | name = TREE_OPERAND (name, 0); |
4276 | if (DECL_TYPE_TEMPLATE_P (name)) |
4277 | name = TREE_OPERAND (fullname, 0) = DECL_NAME (name); |
4278 | if (TREE_CODE (name) != IDENTIFIER_NODE) |
4279 | { |
4280 | if (complain & tf_error) |
4281 | error ("%qD is not a type" , name); |
4282 | return error_mark_node; |
4283 | } |
4284 | } |
4285 | if (TREE_CODE (name) == TEMPLATE_DECL) |
4286 | { |
4287 | if (complain & tf_error) |
4288 | error ("%qD used without template arguments" , name); |
4289 | return error_mark_node; |
4290 | } |
4291 | else if (is_overloaded_fn (name)) |
4292 | { |
4293 | if (complain & tf_error) |
4294 | error ("%qD is a function, not a type" , name); |
4295 | return error_mark_node; |
4296 | } |
4297 | gcc_assert (identifier_p (name)); |
4298 | gcc_assert (TYPE_P (context)); |
4299 | |
4300 | if (TREE_CODE (context) == TYPE_PACK_EXPANSION) |
4301 | /* This can happen for C++17 variadic using (c++/88986). */; |
4302 | else if (!MAYBE_CLASS_TYPE_P (context)) |
4303 | { |
4304 | if (complain & tf_error) |
4305 | error ("%q#T is not a class" , context); |
4306 | return error_mark_node; |
4307 | } |
4308 | |
4309 | /* When the CONTEXT is a dependent type, NAME could refer to a |
4310 | dependent base class of CONTEXT. But look inside it anyway |
4311 | if CONTEXT is a currently open scope, in case it refers to a |
4312 | member of the current instantiation or a non-dependent base; |
4313 | lookup will stop when we hit a dependent base. */ |
4314 | if (!dependent_scope_p (context)) |
4315 | { |
4316 | /* We generally don't ignore non-types during TYPENAME_TYPE lookup |
4317 | (as per [temp.res.general]/3), unless |
4318 | - the tag corresponds to a class-key or 'enum' so |
4319 | [basic.lookup.elab] applies, or |
4320 | - the tag corresponds to scope_type or tf_qualifying_scope is |
4321 | set so [basic.lookup.qual]/1 applies. |
4322 | TODO: If we'd set/track the scope_type tag thoroughly on all |
4323 | TYPENAME_TYPEs that are followed by :: then we wouldn't need the |
4324 | tf_qualifying_scope flag. */ |
4325 | bool want_type = (tag_type != none_type && tag_type != typename_type) |
4326 | || (complain & tf_qualifying_scope); |
4327 | t = lookup_member (context, name, /*protect=*/2, want_type, complain); |
4328 | } |
4329 | else |
4330 | t = NULL_TREE; |
4331 | |
4332 | if ((!t || TREE_CODE (t) == TREE_LIST) && dependent_type_p (context)) |
4333 | return build_typename_type (context, name, fullname, tag_type); |
4334 | |
4335 | want_template = TREE_CODE (fullname) == TEMPLATE_ID_EXPR; |
4336 | |
4337 | if (!t) |
4338 | { |
4339 | if (complain & tf_error) |
4340 | { |
4341 | if (!COMPLETE_TYPE_P (context)) |
4342 | cxx_incomplete_type_error (NULL_TREE, type: context); |
4343 | else |
4344 | error (want_template ? G_("no class template named %q#T in %q#T" ) |
4345 | : G_("no type named %q#T in %q#T" ), name, context); |
4346 | } |
4347 | return error_mark_node; |
4348 | } |
4349 | |
4350 | /* Pull out the template from an injected-class-name (or multiple). */ |
4351 | if (want_template) |
4352 | t = maybe_get_template_decl_from_type_decl (t); |
4353 | |
4354 | if (TREE_CODE (t) == TREE_LIST) |
4355 | { |
4356 | if (complain & tf_error) |
4357 | { |
4358 | error ("lookup of %qT in %qT is ambiguous" , name, context); |
4359 | print_candidates (t); |
4360 | } |
4361 | return error_mark_node; |
4362 | } |
4363 | |
4364 | if (want_template && !DECL_TYPE_TEMPLATE_P (t)) |
4365 | { |
4366 | if (complain & tf_error) |
4367 | error ("%<typename %T::%D%> names %q#T, which is not a class template" , |
4368 | context, name, t); |
4369 | return error_mark_node; |
4370 | } |
4371 | if (!want_template && TREE_CODE (t) != TYPE_DECL) |
4372 | { |
4373 | if ((complain & tf_tst_ok) && cxx_dialect >= cxx17 |
4374 | && DECL_TYPE_TEMPLATE_P (t)) |
4375 | /* The caller permits this typename-specifier to name a template |
4376 | (because it appears in a CTAD-enabled context). */; |
4377 | else |
4378 | { |
4379 | if (complain & tf_error) |
4380 | error ("%<typename %T::%D%> names %q#D, which is not a type" , |
4381 | context, name, t); |
4382 | return error_mark_node; |
4383 | } |
4384 | } |
4385 | |
4386 | if (!check_accessibility_of_qualified_id (t, /*object_type=*/NULL_TREE, |
4387 | context, complain)) |
4388 | return error_mark_node; |
4389 | |
4390 | if (!want_template && DECL_TYPE_TEMPLATE_P (t)) |
4391 | return make_template_placeholder (t); |
4392 | |
4393 | if (want_template) |
4394 | { |
4395 | t = lookup_template_class (t, TREE_OPERAND (fullname, 1), |
4396 | NULL_TREE, context, |
4397 | /*entering_scope=*/0, |
4398 | complain | tf_user); |
4399 | if (t == error_mark_node) |
4400 | return error_mark_node; |
4401 | t = TYPE_NAME (t); |
4402 | } |
4403 | |
4404 | if (DECL_ARTIFICIAL (t) || !(complain & tf_keep_type_decl)) |
4405 | t = TREE_TYPE (t); |
4406 | |
4407 | maybe_record_typedef_use (t); |
4408 | |
4409 | return t; |
4410 | } |
4411 | |
4412 | /* Resolve `CONTEXT::template NAME'. Returns a TEMPLATE_DECL if the name |
4413 | can be resolved or an UNBOUND_CLASS_TEMPLATE, unless an error occurs, |
4414 | in which case error_mark_node is returned. |
4415 | |
4416 | If PARM_LIST is non-NULL, also make sure that the template parameter |
4417 | list of TEMPLATE_DECL matches. |
4418 | |
4419 | If COMPLAIN zero, don't complain about any errors that occur. */ |
4420 | |
4421 | tree |
4422 | make_unbound_class_template (tree context, tree name, tree parm_list, |
4423 | tsubst_flags_t complain) |
4424 | { |
4425 | if (TYPE_P (name)) |
4426 | name = TYPE_IDENTIFIER (name); |
4427 | else if (DECL_P (name)) |
4428 | name = DECL_NAME (name); |
4429 | gcc_assert (identifier_p (name)); |
4430 | |
4431 | if (!dependent_type_p (context) |
4432 | || currently_open_class (context)) |
4433 | { |
4434 | tree tmpl = NULL_TREE; |
4435 | |
4436 | if (MAYBE_CLASS_TYPE_P (context)) |
4437 | tmpl = lookup_field (context, name, 0, false); |
4438 | |
4439 | if (tmpl && TREE_CODE (tmpl) == TYPE_DECL) |
4440 | tmpl = maybe_get_template_decl_from_type_decl (tmpl); |
4441 | |
4442 | if (!tmpl || !DECL_TYPE_TEMPLATE_P (tmpl)) |
4443 | { |
4444 | if (complain & tf_error) |
4445 | error ("no class template named %q#T in %q#T" , name, context); |
4446 | return error_mark_node; |
4447 | } |
4448 | |
4449 | if (parm_list |
4450 | && !comp_template_parms (DECL_TEMPLATE_PARMS (tmpl), parm_list)) |
4451 | { |
4452 | if (complain & tf_error) |
4453 | { |
4454 | error ("template parameters do not match template %qD" , tmpl); |
4455 | inform (DECL_SOURCE_LOCATION (tmpl), |
4456 | "%qD declared here" , tmpl); |
4457 | } |
4458 | return error_mark_node; |
4459 | } |
4460 | |
4461 | if (!perform_or_defer_access_check (TYPE_BINFO (context), tmpl, tmpl, |
4462 | complain)) |
4463 | return error_mark_node; |
4464 | |
4465 | return tmpl; |
4466 | } |
4467 | |
4468 | return make_unbound_class_template_raw (context, name, parm_list); |
4469 | } |
4470 | |
4471 | /* Build an UNBOUND_CLASS_TEMPLATE. */ |
4472 | |
4473 | tree |
4474 | make_unbound_class_template_raw (tree context, tree name, tree parm_list) |
4475 | { |
4476 | /* Build the UNBOUND_CLASS_TEMPLATE. */ |
4477 | tree t = cxx_make_type (UNBOUND_CLASS_TEMPLATE); |
4478 | TYPE_CONTEXT (t) = FROB_CONTEXT (context); |
4479 | TREE_TYPE (t) = NULL_TREE; |
4480 | SET_TYPE_STRUCTURAL_EQUALITY (t); |
4481 | |
4482 | /* Build the corresponding TEMPLATE_DECL. */ |
4483 | tree d = build_decl (input_location, TEMPLATE_DECL, name, t); |
4484 | TYPE_NAME (t) = d; |
4485 | TYPE_STUB_DECL (t) = d; |
4486 | DECL_CONTEXT (d) = TYPE_CONTEXT (t); |
4487 | DECL_ARTIFICIAL (d) = 1; |
4488 | DECL_TEMPLATE_PARMS (d) = parm_list; |
4489 | |
4490 | return t; |
4491 | } |
4492 | |
4493 | |
4494 | |
4495 | /* Push the declarations of builtin types into the global namespace. |
4496 | RID_INDEX is the index of the builtin type in the array |
4497 | RID_POINTERS. NAME is the name used when looking up the builtin |
4498 | type. TYPE is the _TYPE node for the builtin type. |
4499 | |
4500 | The calls to set_global_binding below should be |
4501 | eliminated. Built-in types should not be looked up name; their |
4502 | names are keywords that the parser can recognize. However, there |
4503 | is code in c-common.cc that uses identifier_global_value to look up |
4504 | built-in types by name. */ |
4505 | |
4506 | void |
4507 | record_builtin_type (enum rid rid_index, |
4508 | const char* name, |
4509 | tree type) |
4510 | { |
4511 | tree decl = NULL_TREE; |
4512 | |
4513 | if (name) |
4514 | { |
4515 | tree tname = get_identifier (name); |
4516 | tree tdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, tname, type); |
4517 | DECL_ARTIFICIAL (tdecl) = 1; |
4518 | set_global_binding (tdecl); |
4519 | decl = tdecl; |
4520 | } |
4521 | |
4522 | if ((int) rid_index < (int) RID_MAX) |
4523 | if (tree rname = ridpointers[(int) rid_index]) |
4524 | if (!decl || DECL_NAME (decl) != rname) |
4525 | { |
4526 | tree rdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, rname, type); |
4527 | DECL_ARTIFICIAL (rdecl) = 1; |
4528 | set_global_binding (rdecl); |
4529 | if (!decl) |
4530 | decl = rdecl; |
4531 | } |
4532 | |
4533 | if (decl) |
4534 | { |
4535 | if (!TYPE_NAME (type)) |
4536 | TYPE_NAME (type) = decl; |
4537 | debug_hooks->type_decl (decl, 0); |
4538 | } |
4539 | } |
4540 | |
4541 | /* Push a type into the namespace so that the back ends ignore it. */ |
4542 | |
4543 | static void |
4544 | record_unknown_type (tree type, const char* name) |
4545 | { |
4546 | tree decl = pushdecl (build_decl (UNKNOWN_LOCATION, |
4547 | TYPE_DECL, get_identifier (name), type)); |
4548 | /* Make sure the "unknown type" typedecl gets ignored for debug info. */ |
4549 | DECL_IGNORED_P (decl) = 1; |
4550 | TYPE_DECL_SUPPRESS_DEBUG (decl) = 1; |
4551 | TYPE_SIZE (type) = TYPE_SIZE (void_type_node); |
4552 | SET_TYPE_ALIGN (type, 1); |
4553 | TYPE_USER_ALIGN (type) = 0; |
4554 | SET_TYPE_MODE (type, TYPE_MODE (void_type_node)); |
4555 | } |
4556 | |
4557 | /* Create all the predefined identifiers. */ |
4558 | |
4559 | static void |
4560 | initialize_predefined_identifiers (void) |
4561 | { |
4562 | struct predefined_identifier |
4563 | { |
4564 | const char *name; /* Name. */ |
4565 | tree *node; /* Node to store it in. */ |
4566 | cp_identifier_kind kind; /* Kind of identifier. */ |
4567 | }; |
4568 | |
4569 | /* A table of identifiers to create at startup. */ |
4570 | static const predefined_identifier predefined_identifiers[] = { |
4571 | {.name: "C++" , .node: &lang_name_cplusplus, .kind: cik_normal}, |
4572 | {.name: "C" , .node: &lang_name_c, .kind: cik_normal}, |
4573 | /* Some of these names have a trailing space so that it is |
4574 | impossible for them to conflict with names written by users. */ |
4575 | {.name: "__ct " , .node: &ctor_identifier, .kind: cik_ctor}, |
4576 | {.name: "__ct_base " , .node: &base_ctor_identifier, .kind: cik_ctor}, |
4577 | {.name: "__ct_comp " , .node: &complete_ctor_identifier, .kind: cik_ctor}, |
4578 | {.name: "__dt " , .node: &dtor_identifier, .kind: cik_dtor}, |
4579 | {.name: "__dt_base " , .node: &base_dtor_identifier, .kind: cik_dtor}, |
4580 | {.name: "__dt_comp " , .node: &complete_dtor_identifier, .kind: cik_dtor}, |
4581 | {.name: "__dt_del " , .node: &deleting_dtor_identifier, .kind: cik_dtor}, |
4582 | {.name: "__conv_op " , .node: &conv_op_identifier, .kind: cik_conv_op}, |
4583 | {.name: "__in_chrg" , .node: &in_charge_identifier, .kind: cik_normal}, |
4584 | {.name: "__as_base " , .node: &as_base_identifier, .kind: cik_normal}, |
4585 | {.name: "this" , .node: &this_identifier, .kind: cik_normal}, |
4586 | {.name: "__delta" , .node: &delta_identifier, .kind: cik_normal}, |
4587 | {.name: "__pfn" , .node: &pfn_identifier, .kind: cik_normal}, |
4588 | {.name: "_vptr" , .node: &vptr_identifier, .kind: cik_normal}, |
4589 | {.name: "__vtt_parm" , .node: &vtt_parm_identifier, .kind: cik_normal}, |
4590 | {.name: "::" , .node: &global_identifier, .kind: cik_normal}, |
4591 | /* The demangler expects anonymous namespaces to be called |
4592 | something starting with '_GLOBAL__N_'. It no longer needs |
4593 | to be unique to the TU. */ |
4594 | {.name: "_GLOBAL__N_1" , .node: &anon_identifier, .kind: cik_normal}, |
4595 | {.name: "auto" , .node: &auto_identifier, .kind: cik_normal}, |
4596 | {.name: "decltype(auto)" , .node: &decltype_auto_identifier, .kind: cik_normal}, |
4597 | {.name: "initializer_list" , .node: &init_list_identifier, .kind: cik_normal}, |
4598 | {.name: "__for_range " , .node: &for_range__identifier, .kind: cik_normal}, |
4599 | {.name: "__for_begin " , .node: &for_begin__identifier, .kind: cik_normal}, |
4600 | {.name: "__for_end " , .node: &for_end__identifier, .kind: cik_normal}, |
4601 | {.name: "__for_range" , .node: &for_range_identifier, .kind: cik_normal}, |
4602 | {.name: "__for_begin" , .node: &for_begin_identifier, .kind: cik_normal}, |
4603 | {.name: "__for_end" , .node: &for_end_identifier, .kind: cik_normal}, |
4604 | {.name: "abi_tag" , .node: &abi_tag_identifier, .kind: cik_normal}, |
4605 | {.name: "aligned" , .node: &aligned_identifier, .kind: cik_normal}, |
4606 | {.name: "begin" , .node: &begin_identifier, .kind: cik_normal}, |
4607 | {.name: "end" , .node: &end_identifier, .kind: cik_normal}, |
4608 | {.name: "get" , .node: &get__identifier, .kind: cik_normal}, |
4609 | {.name: "gnu" , .node: &gnu_identifier, .kind: cik_normal}, |
4610 | {.name: "tuple_element" , .node: &tuple_element_identifier, .kind: cik_normal}, |
4611 | {.name: "tuple_size" , .node: &tuple_size_identifier, .kind: cik_normal}, |
4612 | {.name: "type" , .node: &type_identifier, .kind: cik_normal}, |
4613 | {.name: "value" , .node: &value_identifier, .kind: cik_normal}, |
4614 | {.name: "_FUN" , .node: &fun_identifier, .kind: cik_normal}, |
4615 | {.name: "__closure" , .node: &closure_identifier, .kind: cik_normal}, |
4616 | {.name: "heap uninit" , .node: &heap_uninit_identifier, .kind: cik_normal}, |
4617 | {.name: "heap " , .node: &heap_identifier, .kind: cik_normal}, |
4618 | {.name: "heap deleted" , .node: &heap_deleted_identifier, .kind: cik_normal}, |
4619 | {.name: "heap [] uninit" , .node: &heap_vec_uninit_identifier, .kind: cik_normal}, |
4620 | {.name: "heap []" , .node: &heap_vec_identifier, .kind: cik_normal}, |
4621 | {.name: "omp" , .node: &omp_identifier, .kind: cik_normal}, |
4622 | {NULL, NULL, .kind: cik_normal} |
4623 | }; |
4624 | |
4625 | for (const predefined_identifier *pid = predefined_identifiers; |
4626 | pid->name; ++pid) |
4627 | { |
4628 | *pid->node = get_identifier (pid->name); |
4629 | /* Some of these identifiers already have a special kind. */ |
4630 | if (pid->kind != cik_normal) |
4631 | set_identifier_kind (*pid->node, pid->kind); |
4632 | } |
4633 | } |
4634 | |
4635 | /* Create the predefined scalar types of C, |
4636 | and some nodes representing standard constants (0, 1, (void *)0). |
4637 | Initialize the global binding level. |
4638 | Make definitions for built-in primitive functions. */ |
4639 | |
4640 | void |
4641 | cxx_init_decl_processing (void) |
4642 | { |
4643 | tree void_ftype; |
4644 | tree void_ftype_ptr; |
4645 | |
4646 | /* Create all the identifiers we need. */ |
4647 | initialize_predefined_identifiers (); |
4648 | |
4649 | /* Create the global variables. */ |
4650 | push_to_top_level (); |
4651 | |
4652 | current_function_decl = NULL_TREE; |
4653 | current_binding_level = NULL; |
4654 | /* Enter the global namespace. */ |
4655 | gcc_assert (global_namespace == NULL_TREE); |
4656 | global_namespace = build_lang_decl (NAMESPACE_DECL, global_identifier, |
4657 | void_type_node); |
4658 | TREE_PUBLIC (global_namespace) = true; |
4659 | DECL_MODULE_EXPORT_P (global_namespace) = true; |
4660 | DECL_CONTEXT (global_namespace) |
4661 | = build_translation_unit_decl (get_identifier (main_input_filename)); |
4662 | /* Remember whether we want the empty class passing ABI change warning |
4663 | in this TU. */ |
4664 | TRANSLATION_UNIT_WARN_EMPTY_P (DECL_CONTEXT (global_namespace)) |
4665 | = warn_abi && abi_version_crosses (12); |
4666 | debug_hooks->register_main_translation_unit |
4667 | (DECL_CONTEXT (global_namespace)); |
4668 | begin_scope (sk_namespace, global_namespace); |
4669 | current_namespace = global_namespace; |
4670 | |
4671 | if (flag_visibility_ms_compat) |
4672 | default_visibility = VISIBILITY_HIDDEN; |
4673 | |
4674 | /* Initially, C. */ |
4675 | current_lang_name = lang_name_c; |
4676 | |
4677 | /* Create the `std' namespace. */ |
4678 | push_namespace (get_identifier ("std" )); |
4679 | std_node = current_namespace; |
4680 | pop_namespace (); |
4681 | |
4682 | flag_noexcept_type = (cxx_dialect >= cxx17); |
4683 | |
4684 | c_common_nodes_and_builtins (); |
4685 | |
4686 | tree bool_ftype = build_function_type_list (boolean_type_node, NULL_TREE); |
4687 | tree decl |
4688 | = add_builtin_function (name: "__builtin_is_constant_evaluated" , |
4689 | type: bool_ftype, function_code: CP_BUILT_IN_IS_CONSTANT_EVALUATED, |
4690 | cl: BUILT_IN_FRONTEND, NULL, NULL_TREE); |
4691 | set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF); |
4692 | |
4693 | /* The concrete return type of __builtin_source_location is |
4694 | const std::source_location::__impl*, but we can't form the type |
4695 | at this point. So we initially declare it with an auto return |
4696 | type which we then "deduce" from require_deduced_type upon first use. */ |
4697 | tree auto_ftype = build_function_type_list (make_auto (), NULL_TREE); |
4698 | decl = add_builtin_function (name: "__builtin_source_location" , |
4699 | type: auto_ftype, function_code: CP_BUILT_IN_SOURCE_LOCATION, |
4700 | cl: BUILT_IN_FRONTEND, NULL, NULL_TREE); |
4701 | set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF); |
4702 | |
4703 | tree bool_vaftype = build_varargs_function_type_list (boolean_type_node, |
4704 | NULL_TREE); |
4705 | decl |
4706 | = add_builtin_function (name: "__builtin_is_corresponding_member" , |
4707 | type: bool_vaftype, |
4708 | function_code: CP_BUILT_IN_IS_CORRESPONDING_MEMBER, |
4709 | cl: BUILT_IN_FRONTEND, NULL, NULL_TREE); |
4710 | set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF); |
4711 | |
4712 | decl |
4713 | = add_builtin_function (name: "__builtin_is_pointer_interconvertible_with_class" , |
4714 | type: bool_vaftype, |
4715 | function_code: CP_BUILT_IN_IS_POINTER_INTERCONVERTIBLE_WITH_CLASS, |
4716 | cl: BUILT_IN_FRONTEND, NULL, NULL_TREE); |
4717 | set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF); |
4718 | |
4719 | integer_two_node = build_int_cst (NULL_TREE, 2); |
4720 | |
4721 | /* Guess at the initial static decls size. */ |
4722 | vec_alloc (v&: static_decls, nelems: 500); |
4723 | |
4724 | /* ... and keyed classes. */ |
4725 | vec_alloc (v&: keyed_classes, nelems: 100); |
4726 | |
4727 | record_builtin_type (rid_index: RID_BOOL, name: "bool" , boolean_type_node); |
4728 | truthvalue_type_node = boolean_type_node; |
4729 | truthvalue_false_node = boolean_false_node; |
4730 | truthvalue_true_node = boolean_true_node; |
4731 | |
4732 | empty_except_spec = build_tree_list (NULL_TREE, NULL_TREE); |
4733 | noexcept_true_spec = build_tree_list (boolean_true_node, NULL_TREE); |
4734 | noexcept_false_spec = build_tree_list (boolean_false_node, NULL_TREE); |
4735 | noexcept_deferred_spec = build_tree_list (make_node (DEFERRED_NOEXCEPT), |
4736 | NULL_TREE); |
4737 | |
4738 | #if 0 |
4739 | record_builtin_type (RID_MAX, NULL, string_type_node); |
4740 | #endif |
4741 | |
4742 | delta_type_node = ptrdiff_type_node; |
4743 | vtable_index_type = ptrdiff_type_node; |
4744 | |
4745 | vtt_parm_type = build_pointer_type (const_ptr_type_node); |
4746 | void_ftype = build_function_type_list (void_type_node, NULL_TREE); |
4747 | void_ftype_ptr = build_function_type_list (void_type_node, |
4748 | ptr_type_node, NULL_TREE); |
4749 | void_ftype_ptr |
4750 | = build_exception_variant (void_ftype_ptr, empty_except_spec); |
4751 | |
4752 | /* Create the conversion operator marker. This operator's DECL_NAME |
4753 | is in the identifier table, so we can use identifier equality to |
4754 | find it. */ |
4755 | conv_op_marker = build_lang_decl (FUNCTION_DECL, conv_op_identifier, |
4756 | void_ftype); |
4757 | |
4758 | /* C++ extensions */ |
4759 | |
4760 | unknown_type_node = make_node (LANG_TYPE); |
4761 | record_unknown_type (unknown_type_node, name: "unknown type" ); |
4762 | |
4763 | /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node. */ |
4764 | TREE_TYPE (unknown_type_node) = unknown_type_node; |
4765 | |
4766 | /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same |
4767 | result. */ |
4768 | TYPE_POINTER_TO (unknown_type_node) = unknown_type_node; |
4769 | TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node; |
4770 | |
4771 | init_list_type_node = make_node (LANG_TYPE); |
4772 | record_unknown_type (init_list_type_node, name: "init list" ); |
4773 | |
4774 | /* Used when parsing to distinguish parameter-lists () and (void). */ |
4775 | explicit_void_list_node = build_tree_list (NULL_TREE, void_type_node); |
4776 | |
4777 | { |
4778 | /* Make sure we get a unique function type, so we can give |
4779 | its pointer type a name. (This wins for gdb.) */ |
4780 | tree vfunc_type = make_node (FUNCTION_TYPE); |
4781 | TREE_TYPE (vfunc_type) = integer_type_node; |
4782 | TYPE_ARG_TYPES (vfunc_type) = NULL_TREE; |
4783 | layout_type (vfunc_type); |
4784 | |
4785 | vtable_entry_type = build_pointer_type (vfunc_type); |
4786 | } |
4787 | record_builtin_type (rid_index: RID_MAX, name: "__vtbl_ptr_type" , vtable_entry_type); |
4788 | |
4789 | vtbl_type_node |
4790 | = build_cplus_array_type (vtable_entry_type, NULL_TREE); |
4791 | layout_type (vtbl_type_node); |
4792 | vtbl_type_node = cp_build_qualified_type (vtbl_type_node, TYPE_QUAL_CONST); |
4793 | record_builtin_type (rid_index: RID_MAX, NULL, vtbl_type_node); |
4794 | vtbl_ptr_type_node = build_pointer_type (vtable_entry_type); |
4795 | layout_type (vtbl_ptr_type_node); |
4796 | record_builtin_type (rid_index: RID_MAX, NULL, vtbl_ptr_type_node); |
4797 | |
4798 | push_namespace (get_identifier ("__cxxabiv1" )); |
4799 | abi_node = current_namespace; |
4800 | pop_namespace (); |
4801 | |
4802 | any_targ_node = make_node (LANG_TYPE); |
4803 | record_unknown_type (any_targ_node, name: "any type" ); |
4804 | |
4805 | /* Now, C++. */ |
4806 | current_lang_name = lang_name_cplusplus; |
4807 | |
4808 | if (aligned_new_threshold > 1 |
4809 | && !pow2p_hwi (aligned_new_threshold)) |
4810 | { |
4811 | error ("%<-faligned-new=%d%> is not a power of two" , |
4812 | aligned_new_threshold); |
4813 | aligned_new_threshold = 1; |
4814 | } |
4815 | if (aligned_new_threshold == -1) |
4816 | aligned_new_threshold = (cxx_dialect >= cxx17) ? 1 : 0; |
4817 | if (aligned_new_threshold == 1) |
4818 | aligned_new_threshold = malloc_alignment () / BITS_PER_UNIT; |
4819 | |
4820 | { |
4821 | tree newattrs, extvisattr; |
4822 | tree newtype, deltype; |
4823 | tree ptr_ftype_sizetype; |
4824 | tree new_eh_spec; |
4825 | |
4826 | ptr_ftype_sizetype |
4827 | = build_function_type_list (ptr_type_node, size_type_node, NULL_TREE); |
4828 | if (cxx_dialect == cxx98) |
4829 | { |
4830 | tree bad_alloc_id; |
4831 | tree bad_alloc_type_node; |
4832 | tree bad_alloc_decl; |
4833 | |
4834 | push_nested_namespace (std_node); |
4835 | bad_alloc_id = get_identifier ("bad_alloc" ); |
4836 | bad_alloc_type_node = make_class_type (RECORD_TYPE); |
4837 | TYPE_CONTEXT (bad_alloc_type_node) = current_namespace; |
4838 | bad_alloc_decl |
4839 | = create_implicit_typedef (name: bad_alloc_id, type: bad_alloc_type_node); |
4840 | DECL_CONTEXT (bad_alloc_decl) = current_namespace; |
4841 | pop_nested_namespace (std_node); |
4842 | |
4843 | new_eh_spec |
4844 | = add_exception_specifier (NULL_TREE, bad_alloc_type_node, -1); |
4845 | } |
4846 | else |
4847 | new_eh_spec = noexcept_false_spec; |
4848 | |
4849 | /* Ensure attribs.cc is initialized. */ |
4850 | init_attributes (); |
4851 | |
4852 | extvisattr = build_tree_list (get_identifier ("externally_visible" ), |
4853 | NULL_TREE); |
4854 | newattrs = tree_cons (get_identifier ("alloc_size" ), |
4855 | build_tree_list (NULL_TREE, integer_one_node), |
4856 | extvisattr); |
4857 | newtype = cp_build_type_attribute_variant (ptr_ftype_sizetype, newattrs); |
4858 | newtype = build_exception_variant (newtype, new_eh_spec); |
4859 | deltype = cp_build_type_attribute_variant (void_ftype_ptr, extvisattr); |
4860 | deltype = build_exception_variant (deltype, empty_except_spec); |
4861 | tree opnew = push_cp_library_fn (NEW_EXPR, newtype, 0); |
4862 | DECL_IS_MALLOC (opnew) = 1; |
4863 | DECL_SET_IS_OPERATOR_NEW (opnew, true); |
4864 | DECL_IS_REPLACEABLE_OPERATOR (opnew) = 1; |
4865 | opnew = push_cp_library_fn (VEC_NEW_EXPR, newtype, 0); |
4866 | DECL_IS_MALLOC (opnew) = 1; |
4867 | DECL_SET_IS_OPERATOR_NEW (opnew, true); |
4868 | DECL_IS_REPLACEABLE_OPERATOR (opnew) = 1; |
4869 | tree opdel = push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW); |
4870 | DECL_SET_IS_OPERATOR_DELETE (opdel, true); |
4871 | DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1; |
4872 | opdel = push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW); |
4873 | DECL_SET_IS_OPERATOR_DELETE (opdel, true); |
4874 | DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1; |
4875 | if (flag_sized_deallocation) |
4876 | { |
4877 | /* Also push the sized deallocation variants: |
4878 | void operator delete(void*, std::size_t) throw(); |
4879 | void operator delete[](void*, std::size_t) throw(); */ |
4880 | tree void_ftype_ptr_size |
4881 | = build_function_type_list (void_type_node, ptr_type_node, |
4882 | size_type_node, NULL_TREE); |
4883 | deltype = cp_build_type_attribute_variant (void_ftype_ptr_size, |
4884 | extvisattr); |
4885 | deltype = build_exception_variant (deltype, empty_except_spec); |
4886 | opdel = push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW); |
4887 | DECL_SET_IS_OPERATOR_DELETE (opdel, true); |
4888 | DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1; |
4889 | opdel = push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW); |
4890 | DECL_SET_IS_OPERATOR_DELETE (opdel, true); |
4891 | DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1; |
4892 | } |
4893 | |
4894 | if (aligned_new_threshold) |
4895 | { |
4896 | push_nested_namespace (std_node); |
4897 | tree align_id = get_identifier ("align_val_t" ); |
4898 | align_type_node = start_enum (align_id, NULL_TREE, size_type_node, |
4899 | NULL_TREE, /*scoped*/true, NULL); |
4900 | pop_nested_namespace (std_node); |
4901 | |
4902 | /* operator new (size_t, align_val_t); */ |
4903 | newtype = build_function_type_list (ptr_type_node, size_type_node, |
4904 | align_type_node, NULL_TREE); |
4905 | newtype = cp_build_type_attribute_variant (newtype, newattrs); |
4906 | newtype = build_exception_variant (newtype, new_eh_spec); |
4907 | opnew = push_cp_library_fn (NEW_EXPR, newtype, 0); |
4908 | DECL_IS_MALLOC (opnew) = 1; |
4909 | DECL_SET_IS_OPERATOR_NEW (opnew, true); |
4910 | DECL_IS_REPLACEABLE_OPERATOR (opnew) = 1; |
4911 | opnew = push_cp_library_fn (VEC_NEW_EXPR, newtype, 0); |
4912 | DECL_IS_MALLOC (opnew) = 1; |
4913 | DECL_SET_IS_OPERATOR_NEW (opnew, true); |
4914 | DECL_IS_REPLACEABLE_OPERATOR (opnew) = 1; |
4915 | |
4916 | /* operator delete (void *, align_val_t); */ |
4917 | deltype = build_function_type_list (void_type_node, ptr_type_node, |
4918 | align_type_node, NULL_TREE); |
4919 | deltype = cp_build_type_attribute_variant (deltype, extvisattr); |
4920 | deltype = build_exception_variant (deltype, empty_except_spec); |
4921 | opdel = push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW); |
4922 | DECL_SET_IS_OPERATOR_DELETE (opdel, true); |
4923 | DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1; |
4924 | opdel = push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW); |
4925 | DECL_SET_IS_OPERATOR_DELETE (opdel, true); |
4926 | DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1; |
4927 | |
4928 | if (flag_sized_deallocation) |
4929 | { |
4930 | /* operator delete (void *, size_t, align_val_t); */ |
4931 | deltype = build_function_type_list (void_type_node, ptr_type_node, |
4932 | size_type_node, align_type_node, |
4933 | NULL_TREE); |
4934 | deltype = cp_build_type_attribute_variant (deltype, extvisattr); |
4935 | deltype = build_exception_variant (deltype, empty_except_spec); |
4936 | opdel = push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW); |
4937 | DECL_SET_IS_OPERATOR_DELETE (opdel, true); |
4938 | DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1; |
4939 | opdel = push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW); |
4940 | DECL_SET_IS_OPERATOR_DELETE (opdel, true); |
4941 | DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1; |
4942 | } |
4943 | } |
4944 | |
4945 | /* C++-specific nullptr initialization. */ |
4946 | if (abi_version_at_least (9)) |
4947 | SET_TYPE_ALIGN (nullptr_type_node, GET_MODE_ALIGNMENT (ptr_mode)); |
4948 | record_builtin_type (rid_index: RID_MAX, name: "decltype(nullptr)" , nullptr_type_node); |
4949 | } |
4950 | |
4951 | if (! supports_one_only ()) |
4952 | flag_weak = 0; |
4953 | |
4954 | abort_fndecl |
4955 | = build_library_fn_ptr ("__cxa_pure_virtual" , void_ftype, |
4956 | ECF_NORETURN | ECF_NOTHROW | ECF_COLD); |
4957 | if (flag_weak) |
4958 | /* If no definition is available, resolve references to NULL. */ |
4959 | declare_weak (abort_fndecl); |
4960 | |
4961 | /* Perform other language dependent initializations. */ |
4962 | init_class_processing (); |
4963 | init_rtti_processing (); |
4964 | init_template_processing (); |
4965 | |
4966 | if (flag_exceptions) |
4967 | init_exception_processing (); |
4968 | |
4969 | if (modules_p ()) |
4970 | init_modules (parse_in); |
4971 | |
4972 | make_fname_decl = cp_make_fname_decl; |
4973 | start_fname_decls (); |
4974 | |
4975 | /* Show we use EH for cleanups. */ |
4976 | if (flag_exceptions) |
4977 | using_eh_for_cleanups (); |
4978 | |
4979 | /* Check that the hardware interference sizes are at least |
4980 | alignof(max_align_t), as required by the standard. */ |
4981 | const int max_align = max_align_t_align () / BITS_PER_UNIT; |
4982 | if (OPTION_SET_P (param_destruct_interfere_size)) |
4983 | { |
4984 | if (param_destruct_interfere_size < max_align) |
4985 | error ("%<--param destructive-interference-size=%d%> is less than " |
4986 | "%d" , param_destruct_interfere_size, max_align); |
4987 | else if (param_destruct_interfere_size < param_l1_cache_line_size) |
4988 | warning (OPT_Winterference_size, |
4989 | "%<--param destructive-interference-size=%d%> " |
4990 | "is less than %<--param l1-cache-line-size=%d%>" , |
4991 | param_destruct_interfere_size, param_l1_cache_line_size); |
4992 | } |
4993 | else if (param_destruct_interfere_size) |
4994 | /* Assume the internal value is OK. */; |
4995 | else if (param_l1_cache_line_size >= max_align) |
4996 | param_destruct_interfere_size = param_l1_cache_line_size; |
4997 | /* else leave it unset. */ |
4998 | |
4999 | if (OPTION_SET_P (param_construct_interfere_size)) |
5000 | { |
5001 | if (param_construct_interfere_size < max_align) |
5002 | error ("%<--param constructive-interference-size=%d%> is less than " |
5003 | "%d" , param_construct_interfere_size, max_align); |
5004 | else if (param_construct_interfere_size > param_l1_cache_line_size |
5005 | && param_l1_cache_line_size >= max_align) |
5006 | warning (OPT_Winterference_size, |
5007 | "%<--param constructive-interference-size=%d%> " |
5008 | "is greater than %<--param l1-cache-line-size=%d%>" , |
5009 | param_construct_interfere_size, param_l1_cache_line_size); |
5010 | } |
5011 | else if (param_construct_interfere_size) |
5012 | /* Assume the internal value is OK. */; |
5013 | else if (param_l1_cache_line_size >= max_align) |
5014 | param_construct_interfere_size = param_l1_cache_line_size; |
5015 | } |
5016 | |
5017 | /* Enter an abi node in global-module context. returns a cookie to |
5018 | give to pop_abi_namespace. */ |
5019 | |
5020 | unsigned |
5021 | push_abi_namespace (tree node) |
5022 | { |
5023 | push_nested_namespace (node); |
5024 | push_visibility ("default" , 2); |
5025 | unsigned flags = module_kind; |
5026 | module_kind = 0; |
5027 | return flags; |
5028 | } |
5029 | |
5030 | /* Pop an abi namespace, FLAGS is the cookie push_abi_namespace gave |
5031 | you. */ |
5032 | |
5033 | void |
5034 | pop_abi_namespace (unsigned flags, tree node) |
5035 | { |
5036 | module_kind = flags; |
5037 | pop_visibility (2); |
5038 | pop_nested_namespace (node); |
5039 | } |
5040 | |
5041 | /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give |
5042 | the decl, LOC is the location to give the decl, NAME is the |
5043 | initialization string and TYPE_DEP indicates whether NAME depended |
5044 | on the type of the function. We make use of that to detect |
5045 | __PRETTY_FUNCTION__ inside a template fn. This is being done lazily |
5046 | at the point of first use, so we mustn't push the decl now. */ |
5047 | |
5048 | static tree |
5049 | cp_make_fname_decl (location_t loc, tree id, int type_dep) |
5050 | { |
5051 | tree domain = NULL_TREE; |
5052 | tree init = NULL_TREE; |
5053 | |
5054 | if (!(type_dep && current_function_decl && in_template_context)) |
5055 | { |
5056 | const char *name = NULL; |
5057 | bool release_name = false; |
5058 | |
5059 | if (current_function_decl == NULL_TREE) |
5060 | name = "top level" ; |
5061 | else if (type_dep == 0) |
5062 | { |
5063 | /* __FUNCTION__ */ |
5064 | name = fname_as_string (type_dep); |
5065 | release_name = true; |
5066 | } |
5067 | else |
5068 | { |
5069 | /* __PRETTY_FUNCTION__ */ |
5070 | gcc_checking_assert (type_dep == 1); |
5071 | name = cxx_printable_name (current_function_decl, 2); |
5072 | } |
5073 | |
5074 | size_t length = strlen (s: name); |
5075 | domain = build_index_type (size_int (length)); |
5076 | init = build_string (length + 1, name); |
5077 | if (release_name) |
5078 | free (ptr: const_cast<char *> (name)); |
5079 | } |
5080 | |
5081 | tree type = cp_build_qualified_type (char_type_node, TYPE_QUAL_CONST); |
5082 | type = build_cplus_array_type (type, domain); |
5083 | |
5084 | if (init) |
5085 | TREE_TYPE (init) = type; |
5086 | else |
5087 | init = error_mark_node; |
5088 | |
5089 | tree decl = build_decl (loc, VAR_DECL, id, type); |
5090 | |
5091 | TREE_READONLY (decl) = 1; |
5092 | DECL_ARTIFICIAL (decl) = 1; |
5093 | DECL_DECLARED_CONSTEXPR_P (decl) = 1; |
5094 | TREE_STATIC (decl) = 1; |
5095 | |
5096 | TREE_USED (decl) = 1; |
5097 | |
5098 | SET_DECL_VALUE_EXPR (decl, init); |
5099 | DECL_HAS_VALUE_EXPR_P (decl) = 1; |
5100 | /* For decl_constant_var_p. */ |
5101 | DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1; |
5102 | |
5103 | if (current_function_decl) |
5104 | { |
5105 | DECL_CONTEXT (decl) = current_function_decl; |
5106 | decl = pushdecl_outermost_localscope (decl); |
5107 | if (decl != error_mark_node) |
5108 | add_decl_expr (decl); |
5109 | } |
5110 | else |
5111 | { |
5112 | DECL_THIS_STATIC (decl) = true; |
5113 | decl = pushdecl_top_level_and_finish (decl, NULL_TREE); |
5114 | } |
5115 | |
5116 | return decl; |
5117 | } |
5118 | |
5119 | /* Install DECL as a builtin function at current global scope. Return |
5120 | the new decl (if we found an existing version). Also installs it |
5121 | into ::std, if it's not '_*'. */ |
5122 | |
5123 | tree |
5124 | cxx_builtin_function (tree decl) |
5125 | { |
5126 | retrofit_lang_decl (decl); |
5127 | |
5128 | DECL_ARTIFICIAL (decl) = 1; |
5129 | SET_DECL_LANGUAGE (decl, lang_c); |
5130 | /* Runtime library routines are, by definition, available in an |
5131 | external shared object. */ |
5132 | DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT; |
5133 | DECL_VISIBILITY_SPECIFIED (decl) = 1; |
5134 | |
5135 | tree id = DECL_NAME (decl); |
5136 | const char *name = IDENTIFIER_POINTER (id); |
5137 | bool hiding = false; |
5138 | if (name[0] != '_' || name[1] != '_') |
5139 | /* In the user's namespace, it must be declared before use. */ |
5140 | hiding = true; |
5141 | else if (IDENTIFIER_LENGTH (id) > strlen (s: "___chk" ) |
5142 | && !startswith (str: name + 2, prefix: "builtin_" ) |
5143 | && 0 == memcmp (s1: name + IDENTIFIER_LENGTH (id) - strlen (s: "_chk" ), |
5144 | s2: "_chk" , n: strlen (s: "_chk" ) + 1)) |
5145 | /* Treat __*_chk fortification functions as anticipated as well, |
5146 | unless they are __builtin_*_chk. */ |
5147 | hiding = true; |
5148 | |
5149 | /* All builtins that don't begin with an '_' should additionally |
5150 | go in the 'std' namespace. */ |
5151 | if (name[0] != '_') |
5152 | { |
5153 | tree std_decl = copy_decl (decl); |
5154 | |
5155 | push_nested_namespace (std_node); |
5156 | DECL_CONTEXT (std_decl) = FROB_CONTEXT (std_node); |
5157 | pushdecl (std_decl, hiding); |
5158 | pop_nested_namespace (std_node); |
5159 | } |
5160 | |
5161 | DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace); |
5162 | decl = pushdecl (decl, hiding); |
5163 | |
5164 | return decl; |
5165 | } |
5166 | |
5167 | /* Like cxx_builtin_function, but guarantee the function is added to the global |
5168 | scope. This is to allow function specific options to add new machine |
5169 | dependent builtins when the target ISA changes via attribute((target(...))) |
5170 | which saves space on program startup if the program does not use non-generic |
5171 | ISAs. */ |
5172 | |
5173 | tree |
5174 | cxx_builtin_function_ext_scope (tree decl) |
5175 | { |
5176 | push_nested_namespace (global_namespace); |
5177 | decl = cxx_builtin_function (decl); |
5178 | pop_nested_namespace (global_namespace); |
5179 | |
5180 | return decl; |
5181 | } |
5182 | |
5183 | /* Implement LANG_HOOKS_SIMULATE_BUILTIN_FUNCTION_DECL. */ |
5184 | |
5185 | tree |
5186 | cxx_simulate_builtin_function_decl (tree decl) |
5187 | { |
5188 | retrofit_lang_decl (decl); |
5189 | |
5190 | DECL_ARTIFICIAL (decl) = 1; |
5191 | SET_DECL_LANGUAGE (decl, lang_cplusplus); |
5192 | DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace); |
5193 | return pushdecl (decl); |
5194 | } |
5195 | |
5196 | /* Generate a FUNCTION_DECL with the typical flags for a runtime library |
5197 | function. Not called directly. */ |
5198 | |
5199 | static tree |
5200 | build_library_fn (tree name, enum tree_code operator_code, tree type, |
5201 | int ecf_flags) |
5202 | { |
5203 | tree fn = build_lang_decl (FUNCTION_DECL, name, type); |
5204 | DECL_EXTERNAL (fn) = 1; |
5205 | TREE_PUBLIC (fn) = 1; |
5206 | DECL_ARTIFICIAL (fn) = 1; |
5207 | DECL_OVERLOADED_OPERATOR_CODE_RAW (fn) |
5208 | = OVL_OP_INFO (false, operator_code)->ovl_op_code; |
5209 | SET_DECL_LANGUAGE (fn, lang_c); |
5210 | /* Runtime library routines are, by definition, available in an |
5211 | external shared object. */ |
5212 | DECL_VISIBILITY (fn) = VISIBILITY_DEFAULT; |
5213 | DECL_VISIBILITY_SPECIFIED (fn) = 1; |
5214 | set_call_expr_flags (fn, ecf_flags); |
5215 | return fn; |
5216 | } |
5217 | |
5218 | /* Returns the _DECL for a library function with C++ linkage. */ |
5219 | |
5220 | static tree |
5221 | build_cp_library_fn (tree name, enum tree_code operator_code, tree type, |
5222 | int ecf_flags) |
5223 | { |
5224 | tree fn = build_library_fn (name, operator_code, type, ecf_flags); |
5225 | DECL_CONTEXT (fn) = FROB_CONTEXT (current_namespace); |
5226 | SET_DECL_LANGUAGE (fn, lang_cplusplus); |
5227 | return fn; |
5228 | } |
5229 | |
5230 | /* Like build_library_fn, but takes a C string instead of an |
5231 | IDENTIFIER_NODE. */ |
5232 | |
5233 | tree |
5234 | build_library_fn_ptr (const char* name, tree type, int ecf_flags) |
5235 | { |
5236 | return build_library_fn (get_identifier (name), operator_code: ERROR_MARK, type, ecf_flags); |
5237 | } |
5238 | |
5239 | /* Like build_cp_library_fn, but takes a C string instead of an |
5240 | IDENTIFIER_NODE. */ |
5241 | |
5242 | tree |
5243 | build_cp_library_fn_ptr (const char* name, tree type, int ecf_flags) |
5244 | { |
5245 | return build_cp_library_fn (get_identifier (name), operator_code: ERROR_MARK, type, |
5246 | ecf_flags); |
5247 | } |
5248 | |
5249 | /* Like build_library_fn, but also pushes the function so that we will |
5250 | be able to find it via get_global_binding. Also, the function |
5251 | may throw exceptions listed in RAISES. */ |
5252 | |
5253 | tree |
5254 | push_library_fn (tree name, tree type, tree raises, int ecf_flags) |
5255 | { |
5256 | if (raises) |
5257 | type = build_exception_variant (type, raises); |
5258 | |
5259 | tree fn = build_library_fn (name, operator_code: ERROR_MARK, type, ecf_flags); |
5260 | return pushdecl_top_level (fn); |
5261 | } |
5262 | |
5263 | /* Like build_cp_library_fn, but also pushes the function so that it |
5264 | will be found by normal lookup. */ |
5265 | |
5266 | static tree |
5267 | push_cp_library_fn (enum tree_code operator_code, tree type, |
5268 | int ecf_flags) |
5269 | { |
5270 | tree fn = build_cp_library_fn (name: ovl_op_identifier (isass: false, code: operator_code), |
5271 | operator_code, type, ecf_flags); |
5272 | pushdecl (fn); |
5273 | if (flag_tm) |
5274 | apply_tm_attr (fn, get_identifier ("transaction_safe" )); |
5275 | return fn; |
5276 | } |
5277 | |
5278 | /* Like push_library_fn, but also note that this function throws |
5279 | and does not return. Used for __throw_foo and the like. */ |
5280 | |
5281 | tree |
5282 | push_throw_library_fn (tree name, tree type) |
5283 | { |
5284 | tree fn = push_library_fn (name, type, NULL_TREE, |
5285 | ECF_NORETURN | ECF_XTHROW | ECF_COLD); |
5286 | return fn; |
5287 | } |
5288 | |
5289 | /* When we call finish_struct for an anonymous union, we create |
5290 | default copy constructors and such. But, an anonymous union |
5291 | shouldn't have such things; this function undoes the damage to the |
5292 | anonymous union type T. |
5293 | |
5294 | (The reason that we create the synthesized methods is that we don't |
5295 | distinguish `union { int i; }' from `typedef union { int i; } U'. |
5296 | The first is an anonymous union; the second is just an ordinary |
5297 | union type.) */ |
5298 | |
5299 | void |
5300 | fixup_anonymous_aggr (tree t) |
5301 | { |
5302 | /* Wipe out memory of synthesized methods. */ |
5303 | TYPE_HAS_USER_CONSTRUCTOR (t) = 0; |
5304 | TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0; |
5305 | TYPE_HAS_COPY_CTOR (t) = 0; |
5306 | TYPE_HAS_CONST_COPY_CTOR (t) = 0; |
5307 | TYPE_HAS_COPY_ASSIGN (t) = 0; |
5308 | TYPE_HAS_CONST_COPY_ASSIGN (t) = 0; |
5309 | |
5310 | /* Splice the implicitly generated functions out of TYPE_FIELDS and diagnose |
5311 | invalid members. */ |
5312 | for (tree probe, *prev_p = &TYPE_FIELDS (t); (probe = *prev_p);) |
5313 | { |
5314 | if (TREE_CODE (probe) == FUNCTION_DECL && DECL_ARTIFICIAL (probe)) |
5315 | *prev_p = DECL_CHAIN (probe); |
5316 | else |
5317 | prev_p = &DECL_CHAIN (probe); |
5318 | |
5319 | if (DECL_ARTIFICIAL (probe) |
5320 | && (!DECL_IMPLICIT_TYPEDEF_P (probe) |
5321 | || TYPE_ANON_P (TREE_TYPE (probe)))) |
5322 | continue; |
5323 | |
5324 | if (TREE_CODE (probe) != FIELD_DECL |
5325 | || (TREE_PRIVATE (probe) || TREE_PROTECTED (probe))) |
5326 | { |
5327 | /* We already complained about static data members in |
5328 | finish_static_data_member_decl. */ |
5329 | if (!VAR_P (probe)) |
5330 | { |
5331 | auto_diagnostic_group d; |
5332 | if (permerror (DECL_SOURCE_LOCATION (probe), |
5333 | TREE_CODE (t) == UNION_TYPE |
5334 | ? "%q#D invalid; an anonymous union may " |
5335 | "only have public non-static data members" |
5336 | : "%q#D invalid; an anonymous struct may " |
5337 | "only have public non-static data members" , probe)) |
5338 | { |
5339 | static bool hint; |
5340 | if (flag_permissive && !hint) |
5341 | { |
5342 | hint = true; |
5343 | inform (DECL_SOURCE_LOCATION (probe), |
5344 | "this flexibility is deprecated and will be " |
5345 | "removed" ); |
5346 | } |
5347 | } |
5348 | } |
5349 | } |
5350 | } |
5351 | |
5352 | /* Splice all functions out of CLASSTYPE_MEMBER_VEC. */ |
5353 | vec<tree,va_gc>* vec = CLASSTYPE_MEMBER_VEC (t); |
5354 | unsigned store = 0; |
5355 | for (tree elt : vec) |
5356 | if (!is_overloaded_fn (elt)) |
5357 | (*vec)[store++] = elt; |
5358 | vec_safe_truncate (v: vec, size: store); |
5359 | |
5360 | /* Wipe RTTI info. */ |
5361 | CLASSTYPE_TYPEINFO_VAR (t) = NULL_TREE; |
5362 | |
5363 | /* Anonymous aggregates cannot have fields with ctors, dtors or complex |
5364 | assignment operators (because they cannot have these methods themselves). |
5365 | For anonymous unions this is already checked because they are not allowed |
5366 | in any union, otherwise we have to check it. */ |
5367 | if (TREE_CODE (t) != UNION_TYPE) |
5368 | { |
5369 | tree field, type; |
5370 | |
5371 | if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t))) |
5372 | { |
5373 | error_at (location_of (t), "anonymous struct with base classes" ); |
5374 | /* Avoid ICE after error on anon-struct9.C. */ |
5375 | TYPE_NEEDS_CONSTRUCTING (t) = false; |
5376 | } |
5377 | |
5378 | for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field)) |
5379 | if (TREE_CODE (field) == FIELD_DECL) |
5380 | { |
5381 | type = TREE_TYPE (field); |
5382 | if (CLASS_TYPE_P (type)) |
5383 | { |
5384 | if (TYPE_NEEDS_CONSTRUCTING (type)) |
5385 | error ("member %q+#D with constructor not allowed " |
5386 | "in anonymous aggregate" , field); |
5387 | if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)) |
5388 | error ("member %q+#D with destructor not allowed " |
5389 | "in anonymous aggregate" , field); |
5390 | if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type)) |
5391 | error ("member %q+#D with copy assignment operator " |
5392 | "not allowed in anonymous aggregate" , field); |
5393 | } |
5394 | } |
5395 | } |
5396 | } |
5397 | |
5398 | /* Warn for an attribute located at LOCATION that appertains to the |
5399 | class type CLASS_TYPE that has not been properly placed after its |
5400 | class-key, in it class-specifier. */ |
5401 | |
5402 | void |
5403 | warn_misplaced_attr_for_class_type (location_t location, |
5404 | tree class_type) |
5405 | { |
5406 | gcc_assert (OVERLOAD_TYPE_P (class_type)); |
5407 | |
5408 | auto_diagnostic_group d; |
5409 | if (warning_at (location, OPT_Wattributes, |
5410 | "attribute ignored in declaration " |
5411 | "of %q#T" , class_type)) |
5412 | inform (location, |
5413 | "attribute for %q#T must follow the %qs keyword" , |
5414 | class_type, class_key_or_enum_as_string (class_type)); |
5415 | } |
5416 | |
5417 | /* Returns the cv-qualifiers that apply to the type specified |
5418 | by the DECLSPECS. */ |
5419 | |
5420 | static int |
5421 | get_type_quals (const cp_decl_specifier_seq *declspecs) |
5422 | { |
5423 | int type_quals = TYPE_UNQUALIFIED; |
5424 | |
5425 | if (decl_spec_seq_has_spec_p (declspecs, ds_const)) |
5426 | type_quals |= TYPE_QUAL_CONST; |
5427 | if (decl_spec_seq_has_spec_p (declspecs, ds_volatile)) |
5428 | type_quals |= TYPE_QUAL_VOLATILE; |
5429 | if (decl_spec_seq_has_spec_p (declspecs, ds_restrict)) |
5430 | type_quals |= TYPE_QUAL_RESTRICT; |
5431 | |
5432 | return type_quals; |
5433 | } |
5434 | |
5435 | /* Make sure that a declaration with no declarator is well-formed, i.e. |
5436 | just declares a tagged type or anonymous union. |
5437 | |
5438 | Returns the type declared; or NULL_TREE if none. */ |
5439 | |
5440 | tree |
5441 | check_tag_decl (cp_decl_specifier_seq *declspecs, |
5442 | bool explicit_type_instantiation_p) |
5443 | { |
5444 | int saw_friend = decl_spec_seq_has_spec_p (declspecs, ds_friend); |
5445 | int saw_typedef = decl_spec_seq_has_spec_p (declspecs, ds_typedef); |
5446 | /* If a class, struct, or enum type is declared by the DECLSPECS |
5447 | (i.e, if a class-specifier, enum-specifier, or non-typename |
5448 | elaborated-type-specifier appears in the DECLSPECS), |
5449 | DECLARED_TYPE is set to the corresponding type. */ |
5450 | tree declared_type = NULL_TREE; |
5451 | bool error_p = false; |
5452 | |
5453 | if (declspecs->multiple_types_p) |
5454 | error_at (smallest_type_location (declspecs), |
5455 | "multiple types in one declaration" ); |
5456 | else if (declspecs->redefined_builtin_type) |
5457 | { |
5458 | location_t loc = declspecs->locations[ds_redefined_builtin_type_spec]; |
5459 | if (!in_system_header_at (loc)) |
5460 | permerror (loc, "redeclaration of C++ built-in type %qT" , |
5461 | declspecs->redefined_builtin_type); |
5462 | return NULL_TREE; |
5463 | } |
5464 | |
5465 | if (declspecs->type |
5466 | && TYPE_P (declspecs->type) |
5467 | && ((TREE_CODE (declspecs->type) != TYPENAME_TYPE |
5468 | && MAYBE_CLASS_TYPE_P (declspecs->type)) |
5469 | || TREE_CODE (declspecs->type) == ENUMERAL_TYPE)) |
5470 | declared_type = declspecs->type; |
5471 | else if (declspecs->type == error_mark_node) |
5472 | error_p = true; |
5473 | |
5474 | if (type_uses_auto (declared_type)) |
5475 | { |
5476 | error_at (declspecs->locations[ds_type_spec], |
5477 | "%<auto%> can only be specified for variables " |
5478 | "or function declarations" ); |
5479 | return error_mark_node; |
5480 | } |
5481 | |
5482 | if (declared_type && !OVERLOAD_TYPE_P (declared_type)) |
5483 | declared_type = NULL_TREE; |
5484 | |
5485 | if (!declared_type && !saw_friend && !error_p) |
5486 | permerror (input_location, "declaration does not declare anything" ); |
5487 | /* Check for an anonymous union. */ |
5488 | else if (declared_type && RECORD_OR_UNION_CODE_P (TREE_CODE (declared_type)) |
5489 | && TYPE_UNNAMED_P (declared_type)) |
5490 | { |
5491 | /* 7/3 In a simple-declaration, the optional init-declarator-list |
5492 | can be omitted only when declaring a class (clause 9) or |
5493 | enumeration (7.2), that is, when the decl-specifier-seq contains |
5494 | either a class-specifier, an elaborated-type-specifier with |
5495 | a class-key (9.1), or an enum-specifier. In these cases and |
5496 | whenever a class-specifier or enum-specifier is present in the |
5497 | decl-specifier-seq, the identifiers in these specifiers are among |
5498 | the names being declared by the declaration (as class-name, |
5499 | enum-names, or enumerators, depending on the syntax). In such |
5500 | cases, and except for the declaration of an unnamed bit-field (9.6), |
5501 | the decl-specifier-seq shall introduce one or more names into the |
5502 | program, or shall redeclare a name introduced by a previous |
5503 | declaration. [Example: |
5504 | enum { }; // ill-formed |
5505 | typedef class { }; // ill-formed |
5506 | --end example] */ |
5507 | if (saw_typedef) |
5508 | { |
5509 | error_at (declspecs->locations[ds_typedef], |
5510 | "missing type-name in typedef-declaration" ); |
5511 | return NULL_TREE; |
5512 | } |
5513 | /* Anonymous unions are objects, so they can have specifiers. */; |
5514 | SET_ANON_AGGR_TYPE_P (declared_type); |
5515 | |
5516 | if (TREE_CODE (declared_type) != UNION_TYPE) |
5517 | pedwarn (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (declared_type)), |
5518 | OPT_Wpedantic, "ISO C++ prohibits anonymous structs" ); |
5519 | } |
5520 | |
5521 | else |
5522 | { |
5523 | if (decl_spec_seq_has_spec_p (declspecs, ds_inline)) |
5524 | error_at (declspecs->locations[ds_inline], |
5525 | "%<inline%> can only be specified for functions" ); |
5526 | else if (decl_spec_seq_has_spec_p (declspecs, ds_virtual)) |
5527 | error_at (declspecs->locations[ds_virtual], |
5528 | "%<virtual%> can only be specified for functions" ); |
5529 | else if (saw_friend |
5530 | && (!current_class_type |
5531 | || current_scope () != current_class_type)) |
5532 | error_at (declspecs->locations[ds_friend], |
5533 | "%<friend%> can only be specified inside a class" ); |
5534 | else if (decl_spec_seq_has_spec_p (declspecs, ds_explicit)) |
5535 | error_at (declspecs->locations[ds_explicit], |
5536 | "%<explicit%> can only be specified for constructors" ); |
5537 | else if (declspecs->storage_class) |
5538 | error_at (declspecs->locations[ds_storage_class], |
5539 | "a storage class can only be specified for objects " |
5540 | "and functions" ); |
5541 | else if (decl_spec_seq_has_spec_p (declspecs, ds_const)) |
5542 | error_at (declspecs->locations[ds_const], |
5543 | "%<const%> can only be specified for objects and " |
5544 | "functions" ); |
5545 | else if (decl_spec_seq_has_spec_p (declspecs, ds_volatile)) |
5546 | error_at (declspecs->locations[ds_volatile], |
5547 | "%<volatile%> can only be specified for objects and " |
5548 | "functions" ); |
5549 | else if (decl_spec_seq_has_spec_p (declspecs, ds_restrict)) |
5550 | error_at (declspecs->locations[ds_restrict], |
5551 | "%<__restrict%> can only be specified for objects and " |
5552 | "functions" ); |
5553 | else if (decl_spec_seq_has_spec_p (declspecs, ds_thread)) |
5554 | error_at (declspecs->locations[ds_thread], |
5555 | "%<__thread%> can only be specified for objects " |
5556 | "and functions" ); |
5557 | else if (saw_typedef) |
5558 | warning_at (declspecs->locations[ds_typedef], 0, |
5559 | "%<typedef%> was ignored in this declaration" ); |
5560 | else if (decl_spec_seq_has_spec_p (declspecs, ds_constexpr)) |
5561 | error_at (declspecs->locations[ds_constexpr], |
5562 | "%qs cannot be used for type declarations" , "constexpr" ); |
5563 | else if (decl_spec_seq_has_spec_p (declspecs, ds_constinit)) |
5564 | error_at (declspecs->locations[ds_constinit], |
5565 | "%qs cannot be used for type declarations" , "constinit" ); |
5566 | else if (decl_spec_seq_has_spec_p (declspecs, ds_consteval)) |
5567 | error_at (declspecs->locations[ds_consteval], |
5568 | "%qs cannot be used for type declarations" , "consteval" ); |
5569 | } |
5570 | |
5571 | if (declspecs->attributes && warn_attributes && declared_type) |
5572 | { |
5573 | location_t loc; |
5574 | if (!CLASS_TYPE_P (declared_type) |
5575 | || !CLASSTYPE_TEMPLATE_INSTANTIATION (declared_type)) |
5576 | /* For a non-template class, use the name location. */ |
5577 | loc = location_of (declared_type); |
5578 | else |
5579 | /* For a template class (an explicit instantiation), use the |
5580 | current location. */ |
5581 | loc = input_location; |
5582 | |
5583 | if (explicit_type_instantiation_p) |
5584 | /* [dcl.attr.grammar]/4: |
5585 | |
5586 | No attribute-specifier-seq shall appertain to an explicit |
5587 | instantiation. */ |
5588 | { |
5589 | if (warning_at (loc, OPT_Wattributes, |
5590 | "attribute ignored in explicit instantiation %q#T" , |
5591 | declared_type)) |
5592 | inform (loc, |
5593 | "no attribute can be applied to " |
5594 | "an explicit instantiation" ); |
5595 | } |
5596 | else |
5597 | warn_misplaced_attr_for_class_type (location: loc, class_type: declared_type); |
5598 | } |
5599 | |
5600 | /* Diagnose invalid application of contracts, if any. */ |
5601 | if (find_contract (attrs: declspecs->attributes)) |
5602 | diagnose_misapplied_contracts (declspecs->attributes); |
5603 | else |
5604 | diagnose_misapplied_contracts (declspecs->std_attributes); |
5605 | |
5606 | return declared_type; |
5607 | } |
5608 | |
5609 | /* Called when a declaration is seen that contains no names to declare. |
5610 | If its type is a reference to a structure, union or enum inherited |
5611 | from a containing scope, shadow that tag name for the current scope |
5612 | with a forward reference. |
5613 | If its type defines a new named structure or union |
5614 | or defines an enum, it is valid but we need not do anything here. |
5615 | Otherwise, it is an error. |
5616 | |
5617 | C++: may have to grok the declspecs to learn about static, |
5618 | complain for anonymous unions. |
5619 | |
5620 | Returns the TYPE declared -- or NULL_TREE if none. */ |
5621 | |
5622 | tree |
5623 | shadow_tag (cp_decl_specifier_seq *declspecs) |
5624 | { |
5625 | tree t = check_tag_decl (declspecs, |
5626 | /*explicit_type_instantiation_p=*/false); |
5627 | |
5628 | if (!t) |
5629 | return NULL_TREE; |
5630 | |
5631 | t = maybe_process_partial_specialization (t); |
5632 | if (t == error_mark_node) |
5633 | return NULL_TREE; |
5634 | |
5635 | /* This is where the variables in an anonymous union are |
5636 | declared. An anonymous union declaration looks like: |
5637 | union { ... } ; |
5638 | because there is no declarator after the union, the parser |
5639 | sends that declaration here. */ |
5640 | if (ANON_AGGR_TYPE_P (t)) |
5641 | { |
5642 | fixup_anonymous_aggr (t); |
5643 | |
5644 | if (TYPE_FIELDS (t)) |
5645 | { |
5646 | tree decl = grokdeclarator (/*declarator=*/NULL, |
5647 | declspecs, NORMAL, 0, NULL); |
5648 | finish_anon_union (decl); |
5649 | } |
5650 | } |
5651 | |
5652 | return t; |
5653 | } |
5654 | |
5655 | /* Decode a "typename", such as "int **", returning a ..._TYPE node. */ |
5656 | |
5657 | tree |
5658 | groktypename (cp_decl_specifier_seq *type_specifiers, |
5659 | const cp_declarator *declarator, |
5660 | bool is_template_arg) |
5661 | { |
5662 | tree attrs; |
5663 | tree type; |
5664 | enum decl_context context |
5665 | = is_template_arg ? TEMPLATE_TYPE_ARG : TYPENAME; |
5666 | attrs = type_specifiers->attributes; |
5667 | type_specifiers->attributes = NULL_TREE; |
5668 | type = grokdeclarator (declarator, type_specifiers, context, 0, &attrs); |
5669 | if (attrs && type != error_mark_node) |
5670 | { |
5671 | if (CLASS_TYPE_P (type)) |
5672 | warning (OPT_Wattributes, "ignoring attributes applied to class type %qT " |
5673 | "outside of definition" , type); |
5674 | else if (MAYBE_CLASS_TYPE_P (type)) |
5675 | /* A template type parameter or other dependent type. */ |
5676 | warning (OPT_Wattributes, "ignoring attributes applied to dependent " |
5677 | "type %qT without an associated declaration" , type); |
5678 | else |
5679 | cplus_decl_attributes (&type, attrs, 0); |
5680 | } |
5681 | return type; |
5682 | } |
5683 | |
5684 | /* Process a DECLARATOR for a function-scope or namespace-scope |
5685 | variable or function declaration. |
5686 | (Function definitions go through start_function; class member |
5687 | declarations appearing in the body of the class go through |
5688 | grokfield.) The DECL corresponding to the DECLARATOR is returned. |
5689 | If an error occurs, the error_mark_node is returned instead. |
5690 | |
5691 | DECLSPECS are the decl-specifiers for the declaration. INITIALIZED is |
5692 | SD_INITIALIZED if an explicit initializer is present, or SD_DEFAULTED |
5693 | for an explicitly defaulted function, or SD_DELETED for an explicitly |
5694 | deleted function, but 0 (SD_UNINITIALIZED) if this is a variable |
5695 | implicitly initialized via a default constructor. It can also be |
5696 | SD_DECOMPOSITION which behaves much like SD_INITIALIZED, but we also |
5697 | mark the new decl as DECL_DECOMPOSITION_P. |
5698 | |
5699 | ATTRIBUTES and PREFIX_ATTRIBUTES are GNU attributes associated with this |
5700 | declaration. |
5701 | |
5702 | The scope represented by the context of the returned DECL is pushed |
5703 | (if it is not the global namespace) and is assigned to |
5704 | *PUSHED_SCOPE_P. The caller is then responsible for calling |
5705 | pop_scope on *PUSHED_SCOPE_P if it is set. */ |
5706 | |
5707 | tree |
5708 | start_decl (const cp_declarator *declarator, |
5709 | cp_decl_specifier_seq *declspecs, |
5710 | int initialized, |
5711 | tree attributes, |
5712 | tree prefix_attributes, |
5713 | tree *pushed_scope_p) |
5714 | { |
5715 | tree decl; |
5716 | tree context; |
5717 | bool was_public; |
5718 | int flags; |
5719 | bool alias; |
5720 | tree initial; |
5721 | |
5722 | *pushed_scope_p = NULL_TREE; |
5723 | |
5724 | if (prefix_attributes != error_mark_node) |
5725 | attributes = attr_chainon (attributes, prefix_attributes); |
5726 | |
5727 | decl = grokdeclarator (declarator, declspecs, NORMAL, initialized, |
5728 | &attributes); |
5729 | |
5730 | if (decl == NULL_TREE || VOID_TYPE_P (decl) |
5731 | || decl == error_mark_node |
5732 | || prefix_attributes == error_mark_node) |
5733 | return error_mark_node; |
5734 | |
5735 | context = CP_DECL_CONTEXT (decl); |
5736 | if (context != global_namespace) |
5737 | *pushed_scope_p = push_scope (context); |
5738 | |
5739 | if (initialized && TREE_CODE (decl) == TYPE_DECL) |
5740 | { |
5741 | error_at (DECL_SOURCE_LOCATION (decl), |
5742 | "typedef %qD is initialized (use %qs instead)" , |
5743 | decl, "decltype" ); |
5744 | return error_mark_node; |
5745 | } |
5746 | |
5747 | /* Save the DECL_INITIAL value in case it gets clobbered to assist |
5748 | with attribute validation. */ |
5749 | initial = DECL_INITIAL (decl); |
5750 | |
5751 | if (initialized) |
5752 | { |
5753 | if (! toplevel_bindings_p () |
5754 | && DECL_EXTERNAL (decl)) |
5755 | warning (0, "declaration of %q#D has %<extern%> and is initialized" , |
5756 | decl); |
5757 | DECL_EXTERNAL (decl) = 0; |
5758 | if (toplevel_bindings_p ()) |
5759 | TREE_STATIC (decl) = 1; |
5760 | /* Tell 'cplus_decl_attributes' this is an initialized decl, |
5761 | even though we might not yet have the initializer expression. */ |
5762 | if (!DECL_INITIAL (decl)) |
5763 | DECL_INITIAL (decl) = error_mark_node; |
5764 | } |
5765 | alias = lookup_attribute (attr_name: "alias" , DECL_ATTRIBUTES (decl)) != 0; |
5766 | |
5767 | if (alias && TREE_CODE (decl) == FUNCTION_DECL) |
5768 | record_key_method_defined (decl); |
5769 | |
5770 | /* If this is a typedef that names the class for linkage purposes |
5771 | (7.1.3p8), apply any attributes directly to the type. */ |
5772 | if (TREE_CODE (decl) == TYPE_DECL |
5773 | && OVERLOAD_TYPE_P (TREE_TYPE (decl)) |
5774 | && decl == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (decl)))) |
5775 | flags = ATTR_FLAG_TYPE_IN_PLACE; |
5776 | else |
5777 | flags = 0; |
5778 | |
5779 | /* Set attributes here so if duplicate decl, will have proper attributes. */ |
5780 | cplus_decl_attributes (&decl, attributes, flags); |
5781 | |
5782 | /* Restore the original DECL_INITIAL that we may have clobbered earlier to |
5783 | assist with attribute validation. */ |
5784 | DECL_INITIAL (decl) = initial; |
5785 | |
5786 | /* Dllimported symbols cannot be defined. Static data members (which |
5787 | can be initialized in-class and dllimported) go through grokfield, |
5788 | not here, so we don't need to exclude those decls when checking for |
5789 | a definition. */ |
5790 | if (initialized && DECL_DLLIMPORT_P (decl)) |
5791 | { |
5792 | error_at (DECL_SOURCE_LOCATION (decl), |
5793 | "definition of %q#D is marked %<dllimport%>" , decl); |
5794 | DECL_DLLIMPORT_P (decl) = 0; |
5795 | } |
5796 | |
5797 | /* If #pragma weak was used, mark the decl weak now. */ |
5798 | if (!processing_template_decl && !DECL_DECOMPOSITION_P (decl)) |
5799 | maybe_apply_pragma_weak (decl); |
5800 | |
5801 | if (TREE_CODE (decl) == FUNCTION_DECL |
5802 | && DECL_DECLARED_INLINE_P (decl) |
5803 | && DECL_UNINLINABLE (decl) |
5804 | && lookup_attribute (attr_name: "noinline" , DECL_ATTRIBUTES (decl))) |
5805 | warning_at (DECL_SOURCE_LOCATION (decl), 0, |
5806 | "inline function %qD given attribute %qs" , decl, "noinline" ); |
5807 | |
5808 | if (TYPE_P (context) && COMPLETE_TYPE_P (complete_type (context))) |
5809 | { |
5810 | bool this_tmpl = (current_template_depth |
5811 | > template_class_depth (context)); |
5812 | if (VAR_P (decl)) |
5813 | { |
5814 | tree field = lookup_field (context, DECL_NAME (decl), 0, false); |
5815 | if (field == NULL_TREE |
5816 | || !(VAR_P (field) || variable_template_p (t: field))) |
5817 | error ("%q+#D is not a static data member of %q#T" , decl, context); |
5818 | else if (variable_template_p (t: field) |
5819 | && (DECL_LANG_SPECIFIC (decl) |
5820 | && DECL_TEMPLATE_SPECIALIZATION (decl))) |
5821 | /* OK, specialization was already checked. */; |
5822 | else if (variable_template_p (t: field) && !this_tmpl) |
5823 | { |
5824 | error_at (DECL_SOURCE_LOCATION (decl), |
5825 | "non-member-template declaration of %qD" , decl); |
5826 | inform (DECL_SOURCE_LOCATION (field), "does not match " |
5827 | "member template declaration here" ); |
5828 | return error_mark_node; |
5829 | } |
5830 | else |
5831 | { |
5832 | if (variable_template_p (t: field)) |
5833 | field = DECL_TEMPLATE_RESULT (field); |
5834 | |
5835 | if (DECL_CONTEXT (field) != context) |
5836 | { |
5837 | if (!same_type_p (DECL_CONTEXT (field), context)) |
5838 | permerror (input_location, "ISO C++ does not permit %<%T::%D%> " |
5839 | "to be defined as %<%T::%D%>" , |
5840 | DECL_CONTEXT (field), DECL_NAME (decl), |
5841 | context, DECL_NAME (decl)); |
5842 | DECL_CONTEXT (decl) = DECL_CONTEXT (field); |
5843 | } |
5844 | /* Static data member are tricky; an in-class initialization |
5845 | still doesn't provide a definition, so the in-class |
5846 | declaration will have DECL_EXTERNAL set, but will have an |
5847 | initialization. Thus, duplicate_decls won't warn |
5848 | about this situation, and so we check here. */ |
5849 | if (initialized && DECL_INITIALIZED_IN_CLASS_P (field)) |
5850 | error ("duplicate initialization of %qD" , decl); |
5851 | field = duplicate_decls (newdecl: decl, olddecl: field); |
5852 | if (field == error_mark_node) |
5853 | return error_mark_node; |
5854 | else if (field) |
5855 | decl = field; |
5856 | } |
5857 | } |
5858 | else |
5859 | { |
5860 | tree field = check_classfn (context, decl, |
5861 | this_tmpl |
5862 | ? current_template_parms |
5863 | : NULL_TREE); |
5864 | if (field && field != error_mark_node |
5865 | && duplicate_decls (newdecl: decl, olddecl: field)) |
5866 | decl = field; |
5867 | } |
5868 | |
5869 | /* cp_finish_decl sets DECL_EXTERNAL if DECL_IN_AGGR_P is set. */ |
5870 | DECL_IN_AGGR_P (decl) = 0; |
5871 | /* Do not mark DECL as an explicit specialization if it was not |
5872 | already marked as an instantiation; a declaration should |
5873 | never be marked as a specialization unless we know what |
5874 | template is being specialized. */ |
5875 | if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl)) |
5876 | { |
5877 | SET_DECL_TEMPLATE_SPECIALIZATION (decl); |
5878 | if (TREE_CODE (decl) == FUNCTION_DECL) |
5879 | DECL_COMDAT (decl) = (TREE_PUBLIC (decl) |
5880 | && DECL_DECLARED_INLINE_P (decl)); |
5881 | else |
5882 | DECL_COMDAT (decl) = false; |
5883 | |
5884 | /* [temp.expl.spec] An explicit specialization of a static data |
5885 | member of a template is a definition if the declaration |
5886 | includes an initializer; otherwise, it is a declaration. |
5887 | |
5888 | We check for processing_specialization so this only applies |
5889 | to the new specialization syntax. */ |
5890 | if (!initialized && processing_specialization) |
5891 | DECL_EXTERNAL (decl) = 1; |
5892 | } |
5893 | |
5894 | if (DECL_EXTERNAL (decl) && ! DECL_TEMPLATE_SPECIALIZATION (decl) |
5895 | /* Aliases are definitions. */ |
5896 | && !alias) |
5897 | { |
5898 | if (DECL_VIRTUAL_P (decl) || !flag_contracts) |
5899 | permerror (declarator->id_loc, |
5900 | "declaration of %q#D outside of class is not definition" , |
5901 | decl); |
5902 | else if (flag_contract_strict_declarations) |
5903 | warning_at (declarator->id_loc, OPT_fcontract_strict_declarations_, |
5904 | "declaration of %q#D outside of class is not definition" , |
5905 | decl); |
5906 | } |
5907 | } |
5908 | |
5909 | /* Create a DECL_LANG_SPECIFIC so that DECL_DECOMPOSITION_P works. */ |
5910 | if (initialized == SD_DECOMPOSITION) |
5911 | fit_decomposition_lang_decl (decl, NULL_TREE); |
5912 | |
5913 | was_public = TREE_PUBLIC (decl); |
5914 | |
5915 | if ((DECL_EXTERNAL (decl) || TREE_CODE (decl) == FUNCTION_DECL) |
5916 | && current_function_decl) |
5917 | { |
5918 | /* A function-scope decl of some namespace-scope decl. */ |
5919 | DECL_LOCAL_DECL_P (decl) = true; |
5920 | if (named_module_purview_p ()) |
5921 | error_at (declarator->id_loc, |
5922 | "block-scope extern declaration %q#D not permitted" |
5923 | " in module purview" , decl); |
5924 | } |
5925 | |
5926 | /* Enter this declaration into the symbol table. Don't push the plain |
5927 | VAR_DECL for a variable template. */ |
5928 | if (!template_parm_scope_p () |
5929 | || !VAR_P (decl)) |
5930 | decl = maybe_push_decl (decl); |
5931 | |
5932 | if (processing_template_decl) |
5933 | decl = push_template_decl (decl); |
5934 | |
5935 | if (decl == error_mark_node) |
5936 | return error_mark_node; |
5937 | |
5938 | if (VAR_P (decl) |
5939 | && DECL_NAMESPACE_SCOPE_P (decl) && !TREE_PUBLIC (decl) && !was_public |
5940 | && !DECL_THIS_STATIC (decl) && !DECL_ARTIFICIAL (decl) |
5941 | /* But not templated variables. */ |
5942 | && !(DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))) |
5943 | { |
5944 | /* This is a const variable with implicit 'static'. Set |
5945 | DECL_THIS_STATIC so we can tell it from variables that are |
5946 | !TREE_PUBLIC because of the anonymous namespace. */ |
5947 | gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (decl)) || errorcount); |
5948 | DECL_THIS_STATIC (decl) = 1; |
5949 | } |
5950 | |
5951 | if (current_function_decl && VAR_P (decl) |
5952 | && DECL_DECLARED_CONSTEXPR_P (current_function_decl) |
5953 | && cxx_dialect < cxx23) |
5954 | { |
5955 | bool ok = false; |
5956 | if (CP_DECL_THREAD_LOCAL_P (decl) && !DECL_REALLY_EXTERN (decl)) |
5957 | error_at (DECL_SOURCE_LOCATION (decl), |
5958 | "%qD defined %<thread_local%> in %qs function only " |
5959 | "available with %<-std=c++2b%> or %<-std=gnu++2b%>" , decl, |
5960 | DECL_IMMEDIATE_FUNCTION_P (current_function_decl) |
5961 | ? "consteval" : "constexpr" ); |
5962 | else if (TREE_STATIC (decl)) |
5963 | error_at (DECL_SOURCE_LOCATION (decl), |
5964 | "%qD defined %<static%> in %qs function only available " |
5965 | "with %<-std=c++2b%> or %<-std=gnu++2b%>" , decl, |
5966 | DECL_IMMEDIATE_FUNCTION_P (current_function_decl) |
5967 | ? "consteval" : "constexpr" ); |
5968 | else |
5969 | ok = true; |
5970 | if (!ok) |
5971 | cp_function_chain->invalid_constexpr = true; |
5972 | } |
5973 | |
5974 | if (!processing_template_decl && VAR_P (decl)) |
5975 | start_decl_1 (decl, initialized); |
5976 | |
5977 | return decl; |
5978 | } |
5979 | |
5980 | /* Process the declaration of a variable DECL. INITIALIZED is true |
5981 | iff DECL is explicitly initialized. (INITIALIZED is false if the |
5982 | variable is initialized via an implicitly-called constructor.) |
5983 | This function must be called for ordinary variables (including, for |
5984 | example, implicit instantiations of templates), but must not be |
5985 | called for template declarations. */ |
5986 | |
5987 | void |
5988 | start_decl_1 (tree decl, bool initialized) |
5989 | { |
5990 | gcc_checking_assert (!processing_template_decl); |
5991 | |
5992 | if (error_operand_p (t: decl)) |
5993 | return; |
5994 | |
5995 | gcc_checking_assert (VAR_P (decl)); |
5996 | |
5997 | tree type = TREE_TYPE (decl); |
5998 | bool complete_p = COMPLETE_TYPE_P (type); |
5999 | bool aggregate_definition_p |
6000 | = MAYBE_CLASS_TYPE_P (type) && !DECL_EXTERNAL (decl); |
6001 | |
6002 | /* If an explicit initializer is present, or if this is a definition |
6003 | of an aggregate, then we need a complete type at this point. |
6004 | (Scalars are always complete types, so there is nothing to |
6005 | check.) This code just sets COMPLETE_P; errors (if necessary) |
6006 | are issued below. */ |
6007 | if ((initialized || aggregate_definition_p) |
6008 | && !complete_p |
6009 | && COMPLETE_TYPE_P (complete_type (type))) |
6010 | { |
6011 | complete_p = true; |
6012 | /* We will not yet have set TREE_READONLY on DECL if the type |
6013 | was "const", but incomplete, before this point. But, now, we |
6014 | have a complete type, so we can try again. */ |
6015 | cp_apply_type_quals_to_decl (cp_type_quals (type), decl); |
6016 | } |
6017 | |
6018 | if (initialized) |
6019 | /* Is it valid for this decl to have an initializer at all? */ |
6020 | { |
6021 | /* Don't allow initializations for incomplete types except for |
6022 | arrays which might be completed by the initialization. */ |
6023 | if (complete_p) |
6024 | ; /* A complete type is ok. */ |
6025 | else if (type_uses_auto (type)) |
6026 | ; /* An auto type is ok. */ |
6027 | else if (TREE_CODE (type) != ARRAY_TYPE) |
6028 | { |
6029 | auto_diagnostic_group d; |
6030 | error ("variable %q#D has initializer but incomplete type" , decl); |
6031 | maybe_suggest_missing_header (input_location, |
6032 | TYPE_IDENTIFIER (type), |
6033 | CP_TYPE_CONTEXT (type)); |
6034 | type = TREE_TYPE (decl) = error_mark_node; |
6035 | } |
6036 | else if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type)))) |
6037 | { |
6038 | if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)) |
6039 | error ("elements of array %q#D have incomplete type" , decl); |
6040 | /* else we already gave an error in start_decl. */ |
6041 | } |
6042 | } |
6043 | else if (aggregate_definition_p && !complete_p) |
6044 | { |
6045 | if (type_uses_auto (type)) |
6046 | gcc_assert (CLASS_PLACEHOLDER_TEMPLATE (type)); |
6047 | else |
6048 | { |
6049 | auto_diagnostic_group d; |
6050 | error ("aggregate %q#D has incomplete type and cannot be defined" , |
6051 | decl); |
6052 | maybe_suggest_missing_header (input_location, |
6053 | TYPE_IDENTIFIER (type), |
6054 | CP_TYPE_CONTEXT (type)); |
6055 | /* Change the type so that assemble_variable will give |
6056 | DECL an rtl we can live with: (mem (const_int 0)). */ |
6057 | type = TREE_TYPE (decl) = error_mark_node; |
6058 | } |
6059 | } |
6060 | |
6061 | /* Create a new scope to hold this declaration if necessary. |
6062 | Whether or not a new scope is necessary cannot be determined |
6063 | until after the type has been completed; if the type is a |
6064 | specialization of a class template it is not until after |
6065 | instantiation has occurred that TYPE_HAS_NONTRIVIAL_DESTRUCTOR |
6066 | will be set correctly. */ |
6067 | maybe_push_cleanup_level (type); |
6068 | } |
6069 | |
6070 | /* Given a parenthesized list of values INIT, create a CONSTRUCTOR to handle |
6071 | C++20 P0960. TYPE is the type of the object we're initializing. */ |
6072 | |
6073 | tree |
6074 | do_aggregate_paren_init (tree init, tree type) |
6075 | { |
6076 | tree val = TREE_VALUE (init); |
6077 | |
6078 | if (TREE_CHAIN (init) == NULL_TREE) |
6079 | { |
6080 | /* If the list has a single element and it's a string literal, |
6081 | then it's the initializer for the array as a whole. */ |
6082 | if (TREE_CODE (type) == ARRAY_TYPE |
6083 | && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type))) |
6084 | && TREE_CODE (tree_strip_any_location_wrapper (val)) |
6085 | == STRING_CST) |
6086 | return val; |
6087 | /* Handle non-standard extensions like compound literals. This also |
6088 | prevents triggering aggregate parenthesized-initialization in |
6089 | compiler-generated code for =default. */ |
6090 | else if (same_type_ignoring_top_level_qualifiers_p (type, |
6091 | TREE_TYPE (val))) |
6092 | return val; |
6093 | } |
6094 | |
6095 | init = build_constructor_from_list (init_list_type_node, init); |
6096 | CONSTRUCTOR_IS_DIRECT_INIT (init) = true; |
6097 | CONSTRUCTOR_IS_PAREN_INIT (init) = true; |
6098 | return init; |
6099 | } |
6100 | |
6101 | /* Handle initialization of references. DECL, TYPE, and INIT have the |
6102 | same meaning as in cp_finish_decl. *CLEANUP must be NULL on entry, |
6103 | but will be set to a new CLEANUP_STMT if a temporary is created |
6104 | that must be destroyed subsequently. |
6105 | |
6106 | Returns an initializer expression to use to initialize DECL, or |
6107 | NULL if the initialization can be performed statically. |
6108 | |
6109 | Quotes on semantics can be found in ARM 8.4.3. */ |
6110 | |
6111 | static tree |
6112 | grok_reference_init (tree decl, tree type, tree init, int flags) |
6113 | { |
6114 | if (init == NULL_TREE) |
6115 | { |
6116 | if ((DECL_LANG_SPECIFIC (decl) == 0 |
6117 | || DECL_IN_AGGR_P (decl) == 0) |
6118 | && ! DECL_THIS_EXTERN (decl)) |
6119 | error_at (DECL_SOURCE_LOCATION (decl), |
6120 | "%qD declared as reference but not initialized" , decl); |
6121 | return NULL_TREE; |
6122 | } |
6123 | |
6124 | tree ttype = TREE_TYPE (type); |
6125 | if (TREE_CODE (init) == TREE_LIST) |
6126 | { |
6127 | /* This handles (C++20 only) code like |
6128 | |
6129 | const A& r(1, 2, 3); |
6130 | |
6131 | where we treat the parenthesized list as a CONSTRUCTOR. */ |
6132 | if (TREE_TYPE (init) == NULL_TREE |
6133 | && CP_AGGREGATE_TYPE_P (ttype) |
6134 | && !DECL_DECOMPOSITION_P (decl) |
6135 | && (cxx_dialect >= cxx20)) |
6136 | { |
6137 | /* We don't know yet if we should treat const A& r(1) as |
6138 | const A& r{1}. */ |
6139 | if (list_length (init) == 1) |
6140 | { |
6141 | flags |= LOOKUP_AGGREGATE_PAREN_INIT; |
6142 | init = build_x_compound_expr_from_list (init, ELK_INIT, |
6143 | tf_warning_or_error); |
6144 | } |
6145 | /* If the list had more than one element, the code is ill-formed |
6146 | pre-C++20, so we can build a constructor right away. */ |
6147 | else |
6148 | init = do_aggregate_paren_init (init, type: ttype); |
6149 | } |
6150 | else |
6151 | init = build_x_compound_expr_from_list (init, ELK_INIT, |
6152 | tf_warning_or_error); |
6153 | } |
6154 | |
6155 | if (TREE_CODE (ttype) != ARRAY_TYPE |
6156 | && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE) |
6157 | /* Note: default conversion is only called in very special cases. */ |
6158 | init = decay_conversion (init, tf_warning_or_error); |
6159 | |
6160 | /* check_initializer handles this for non-reference variables, but for |
6161 | references we need to do it here or the initializer will get the |
6162 | incomplete array type and confuse later calls to |
6163 | cp_complete_array_type. */ |
6164 | if (TREE_CODE (ttype) == ARRAY_TYPE |
6165 | && TYPE_DOMAIN (ttype) == NULL_TREE |
6166 | && (BRACE_ENCLOSED_INITIALIZER_P (init) |
6167 | || TREE_CODE (init) == STRING_CST)) |
6168 | { |
6169 | cp_complete_array_type (&ttype, init, false); |
6170 | if (ttype != TREE_TYPE (type)) |
6171 | type = cp_build_reference_type (ttype, TYPE_REF_IS_RVALUE (type)); |
6172 | } |
6173 | |
6174 | /* Convert INIT to the reference type TYPE. This may involve the |
6175 | creation of a temporary, whose lifetime must be the same as that |
6176 | of the reference. If so, a DECL_EXPR for the temporary will be |
6177 | added just after the DECL_EXPR for DECL. That's why we don't set |
6178 | DECL_INITIAL for local references (instead assigning to them |
6179 | explicitly); we need to allow the temporary to be initialized |
6180 | first. */ |
6181 | return initialize_reference (type, init, flags, |
6182 | tf_warning_or_error); |
6183 | } |
6184 | |
6185 | /* Designated initializers in arrays are not supported in GNU C++. |
6186 | The parser cannot detect this error since it does not know whether |
6187 | a given brace-enclosed initializer is for a class type or for an |
6188 | array. This function checks that CE does not use a designated |
6189 | initializer. If it does, an error is issued. Returns true if CE |
6190 | is valid, i.e., does not have a designated initializer. */ |
6191 | |
6192 | bool |
6193 | check_array_designated_initializer (constructor_elt *ce, |
6194 | unsigned HOST_WIDE_INT index) |
6195 | { |
6196 | /* Designated initializers for array elements are not supported. */ |
6197 | if (ce->index) |
6198 | { |
6199 | /* The parser only allows identifiers as designated |
6200 | initializers. */ |
6201 | if (ce->index == error_mark_node) |
6202 | { |
6203 | error ("name used in a GNU-style designated " |
6204 | "initializer for an array" ); |
6205 | return false; |
6206 | } |
6207 | else if (identifier_p (t: ce->index)) |
6208 | { |
6209 | error ("name %qD used in a GNU-style designated " |
6210 | "initializer for an array" , ce->index); |
6211 | return false; |
6212 | } |
6213 | |
6214 | tree ce_index = build_expr_type_conversion (WANT_INT | WANT_ENUM, |
6215 | ce->index, true); |
6216 | if (ce_index |
6217 | && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (ce_index)) |
6218 | && (TREE_CODE (ce_index = fold_non_dependent_expr (ce_index)) |
6219 | == INTEGER_CST)) |
6220 | { |
6221 | /* A C99 designator is OK if it matches the current index. */ |
6222 | if (wi::to_wide (t: ce_index) == index) |
6223 | { |
6224 | ce->index = ce_index; |
6225 | return true; |
6226 | } |
6227 | else |
6228 | sorry ("non-trivial designated initializers not supported" ); |
6229 | } |
6230 | else |
6231 | error_at (cp_expr_loc_or_input_loc (t: ce->index), |
6232 | "C99 designator %qE is not an integral constant-expression" , |
6233 | ce->index); |
6234 | |
6235 | return false; |
6236 | } |
6237 | |
6238 | return true; |
6239 | } |
6240 | |
6241 | /* When parsing `int a[] = {1, 2};' we don't know the size of the |
6242 | array until we finish parsing the initializer. If that's the |
6243 | situation we're in, update DECL accordingly. */ |
6244 | |
6245 | static void |
6246 | maybe_deduce_size_from_array_init (tree decl, tree init) |
6247 | { |
6248 | tree type = TREE_TYPE (decl); |
6249 | |
6250 | if (TREE_CODE (type) == ARRAY_TYPE |
6251 | && TYPE_DOMAIN (type) == NULL_TREE |
6252 | && TREE_CODE (decl) != TYPE_DECL) |
6253 | { |
6254 | /* do_default is really a C-ism to deal with tentative definitions. |
6255 | But let's leave it here to ease the eventual merge. */ |
6256 | int do_default = !DECL_EXTERNAL (decl); |
6257 | tree initializer = init ? init : DECL_INITIAL (decl); |
6258 | int failure = 0; |
6259 | |
6260 | /* Check that there are no designated initializers in INIT, as |
6261 | those are not supported in GNU C++, and as the middle-end |
6262 | will crash if presented with a non-numeric designated |
6263 | initializer. */ |
6264 | if (initializer && BRACE_ENCLOSED_INITIALIZER_P (initializer)) |
6265 | { |
6266 | vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initializer); |
6267 | constructor_elt *ce; |
6268 | HOST_WIDE_INT i; |
6269 | FOR_EACH_VEC_SAFE_ELT (v, i, ce) |
6270 | { |
6271 | if (instantiation_dependent_expression_p (ce->index)) |
6272 | return; |
6273 | if (!check_array_designated_initializer (ce, index: i)) |
6274 | failure = 1; |
6275 | /* If an un-designated initializer is type-dependent, we can't |
6276 | check brace elision yet. */ |
6277 | if (ce->index == NULL_TREE |
6278 | && type_dependent_expression_p (ce->value)) |
6279 | return; |
6280 | } |
6281 | } |
6282 | |
6283 | if (failure) |
6284 | TREE_TYPE (decl) = error_mark_node; |
6285 | else |
6286 | { |
6287 | failure = cp_complete_array_type (&TREE_TYPE (decl), initializer, |
6288 | do_default); |
6289 | if (failure == 1) |
6290 | { |
6291 | error_at (cp_expr_loc_or_loc (t: initializer, |
6292 | DECL_SOURCE_LOCATION (decl)), |
6293 | "initializer fails to determine size of %qD" , decl); |
6294 | } |
6295 | else if (failure == 2) |
6296 | { |
6297 | if (do_default) |
6298 | { |
6299 | error_at (DECL_SOURCE_LOCATION (decl), |
6300 | "array size missing in %qD" , decl); |
6301 | } |
6302 | /* If a `static' var's size isn't known, make it extern as |
6303 | well as static, so it does not get allocated. If it's not |
6304 | `static', then don't mark it extern; finish_incomplete_decl |
6305 | will give it a default size and it will get allocated. */ |
6306 | else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl)) |
6307 | DECL_EXTERNAL (decl) = 1; |
6308 | } |
6309 | else if (failure == 3) |
6310 | { |
6311 | error_at (DECL_SOURCE_LOCATION (decl), |
6312 | "zero-size array %qD" , decl); |
6313 | } |
6314 | } |
6315 | |
6316 | cp_apply_type_quals_to_decl (cp_type_quals (TREE_TYPE (decl)), decl); |
6317 | |
6318 | relayout_decl (decl); |
6319 | } |
6320 | } |
6321 | |
6322 | /* Set DECL_SIZE, DECL_ALIGN, etc. for DECL (a VAR_DECL), and issue |
6323 | any appropriate error messages regarding the layout. */ |
6324 | |
6325 | static void |
6326 | layout_var_decl (tree decl) |
6327 | { |
6328 | tree type; |
6329 | |
6330 | type = TREE_TYPE (decl); |
6331 | if (type == error_mark_node) |
6332 | return; |
6333 | |
6334 | /* If we haven't already laid out this declaration, do so now. |
6335 | Note that we must not call complete type for an external object |
6336 | because it's type might involve templates that we are not |
6337 | supposed to instantiate yet. (And it's perfectly valid to say |
6338 | `extern X x' for some incomplete type `X'.) */ |
6339 | if (!DECL_EXTERNAL (decl)) |
6340 | complete_type (type); |
6341 | if (!DECL_SIZE (decl) |
6342 | && TREE_TYPE (decl) != error_mark_node |
6343 | && complete_or_array_type_p (type)) |
6344 | layout_decl (decl, 0); |
6345 | |
6346 | if (!DECL_EXTERNAL (decl) && DECL_SIZE (decl) == NULL_TREE) |
6347 | { |
6348 | /* An automatic variable with an incomplete type: that is an error. |
6349 | Don't talk about array types here, since we took care of that |
6350 | message in grokdeclarator. */ |
6351 | error_at (DECL_SOURCE_LOCATION (decl), |
6352 | "storage size of %qD isn%'t known" , decl); |
6353 | TREE_TYPE (decl) = error_mark_node; |
6354 | } |
6355 | #if 0 |
6356 | /* Keep this code around in case we later want to control debug info |
6357 | based on whether a type is "used". (jason 1999-11-11) */ |
6358 | |
6359 | else if (!DECL_EXTERNAL (decl) && MAYBE_CLASS_TYPE_P (ttype)) |
6360 | /* Let debugger know it should output info for this type. */ |
6361 | note_debug_info_needed (ttype); |
6362 | |
6363 | if (TREE_STATIC (decl) && DECL_CLASS_SCOPE_P (decl)) |
6364 | note_debug_info_needed (DECL_CONTEXT (decl)); |
6365 | #endif |
6366 | |
6367 | if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl)) |
6368 | && DECL_SIZE (decl) != NULL_TREE |
6369 | && ! TREE_CONSTANT (DECL_SIZE (decl))) |
6370 | { |
6371 | if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST |
6372 | && !DECL_LOCAL_DECL_P (decl)) |
6373 | constant_expression_warning (DECL_SIZE (decl)); |
6374 | else |
6375 | { |
6376 | error_at (DECL_SOURCE_LOCATION (decl), |
6377 | "storage size of %qD isn%'t constant" , decl); |
6378 | TREE_TYPE (decl) = error_mark_node; |
6379 | type = error_mark_node; |
6380 | } |
6381 | } |
6382 | |
6383 | /* If the final element initializes a flexible array field, add the size of |
6384 | that initializer to DECL's size. */ |
6385 | if (type != error_mark_node |
6386 | && DECL_INITIAL (decl) |
6387 | && TREE_CODE (DECL_INITIAL (decl)) == CONSTRUCTOR |
6388 | && !vec_safe_is_empty (CONSTRUCTOR_ELTS (DECL_INITIAL (decl))) |
6389 | && DECL_SIZE (decl) != NULL_TREE |
6390 | && TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST |
6391 | && COMPLETE_TYPE_P (type) |
6392 | && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST |
6393 | && tree_int_cst_equal (DECL_SIZE (decl), TYPE_SIZE (type))) |
6394 | { |
6395 | constructor_elt &elt = CONSTRUCTOR_ELTS (DECL_INITIAL (decl))->last (); |
6396 | if (elt.index) |
6397 | { |
6398 | tree itype = TREE_TYPE (elt.index); |
6399 | tree vtype = TREE_TYPE (elt.value); |
6400 | if (TREE_CODE (itype) == ARRAY_TYPE |
6401 | && TYPE_DOMAIN (itype) == NULL |
6402 | && TREE_CODE (vtype) == ARRAY_TYPE |
6403 | && COMPLETE_TYPE_P (vtype)) |
6404 | { |
6405 | DECL_SIZE (decl) |
6406 | = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (vtype)); |
6407 | DECL_SIZE_UNIT (decl) |
6408 | = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl), |
6409 | TYPE_SIZE_UNIT (vtype)); |
6410 | } |
6411 | } |
6412 | } |
6413 | } |
6414 | |
6415 | /* If a local static variable is declared in an inline function, or if |
6416 | we have a weak definition, we must endeavor to create only one |
6417 | instance of the variable at link-time. */ |
6418 | |
6419 | void |
6420 | maybe_commonize_var (tree decl) |
6421 | { |
6422 | /* Don't mess with __FUNCTION__ and similar. But do handle structured |
6423 | bindings. */ |
6424 | if (DECL_ARTIFICIAL (decl) && !DECL_DECOMPOSITION_P (decl)) |
6425 | return; |
6426 | |
6427 | /* Static data in a function with comdat linkage also has comdat |
6428 | linkage. */ |
6429 | if ((TREE_STATIC (decl) |
6430 | && DECL_FUNCTION_SCOPE_P (decl) |
6431 | && vague_linkage_p (DECL_CONTEXT (decl))) |
6432 | || (TREE_PUBLIC (decl) && DECL_INLINE_VAR_P (decl))) |
6433 | { |
6434 | if (flag_weak) |
6435 | { |
6436 | /* With weak symbols, we simply make the variable COMDAT; |
6437 | that will cause copies in multiple translations units to |
6438 | be merged. */ |
6439 | comdat_linkage (decl); |
6440 | } |
6441 | else |
6442 | { |
6443 | if (DECL_INITIAL (decl) == NULL_TREE |
6444 | || DECL_INITIAL (decl) == error_mark_node) |
6445 | { |
6446 | /* Without weak symbols, we can use COMMON to merge |
6447 | uninitialized variables. */ |
6448 | TREE_PUBLIC (decl) = 1; |
6449 | DECL_COMMON (decl) = 1; |
6450 | } |
6451 | else |
6452 | { |
6453 | /* While for initialized variables, we must use internal |
6454 | linkage -- which means that multiple copies will not |
6455 | be merged. */ |
6456 | TREE_PUBLIC (decl) = 0; |
6457 | DECL_COMMON (decl) = 0; |
6458 | DECL_INTERFACE_KNOWN (decl) = 1; |
6459 | const char *msg; |
6460 | if (DECL_INLINE_VAR_P (decl)) |
6461 | msg = G_("sorry: semantics of inline variable " |
6462 | "%q#D are wrong (you%'ll wind up with " |
6463 | "multiple copies)" ); |
6464 | else |
6465 | msg = G_("sorry: semantics of inline function " |
6466 | "static data %q#D are wrong (you%'ll wind " |
6467 | "up with multiple copies)" ); |
6468 | if (warning_at (DECL_SOURCE_LOCATION (decl), 0, |
6469 | msg, decl)) |
6470 | inform (DECL_SOURCE_LOCATION (decl), |
6471 | "you can work around this by removing the initializer" ); |
6472 | } |
6473 | } |
6474 | } |
6475 | } |
6476 | |
6477 | /* Issue an error message if DECL is an uninitialized const variable. |
6478 | CONSTEXPR_CONTEXT_P is true when the function is called in a constexpr |
6479 | context from potential_constant_expression. Returns true if all is well, |
6480 | false otherwise. */ |
6481 | |
6482 | bool |
6483 | check_for_uninitialized_const_var (tree decl, bool constexpr_context_p, |
6484 | tsubst_flags_t complain) |
6485 | { |
6486 | tree type = strip_array_types (TREE_TYPE (decl)); |
6487 | |
6488 | /* ``Unless explicitly declared extern, a const object does not have |
6489 | external linkage and must be initialized. ($8.4; $12.1)'' ARM |
6490 | 7.1.6 */ |
6491 | if (VAR_P (decl) |
6492 | && !TYPE_REF_P (type) |
6493 | && (CP_TYPE_CONST_P (type) |
6494 | /* C++20 permits trivial default initialization in constexpr |
6495 | context (P1331R2). */ |
6496 | || (cxx_dialect < cxx20 |
6497 | && (constexpr_context_p |
6498 | || var_in_constexpr_fn (decl)))) |
6499 | && !DECL_NONTRIVIALLY_INITIALIZED_P (decl)) |
6500 | { |
6501 | tree field = default_init_uninitialized_part (type); |
6502 | if (!field) |
6503 | return true; |
6504 | |
6505 | bool show_notes = true; |
6506 | |
6507 | if (!constexpr_context_p || cxx_dialect >= cxx20) |
6508 | { |
6509 | if (CP_TYPE_CONST_P (type)) |
6510 | { |
6511 | if (complain & tf_error) |
6512 | show_notes = permerror (DECL_SOURCE_LOCATION (decl), |
6513 | "uninitialized %<const %D%>" , decl); |
6514 | } |
6515 | else |
6516 | { |
6517 | if (!is_instantiation_of_constexpr (current_function_decl) |
6518 | && (complain & tf_error)) |
6519 | error_at (DECL_SOURCE_LOCATION (decl), |
6520 | "uninitialized variable %qD in %<constexpr%> " |
6521 | "function" , decl); |
6522 | else |
6523 | show_notes = false; |
6524 | cp_function_chain->invalid_constexpr = true; |
6525 | } |
6526 | } |
6527 | else if (complain & tf_error) |
6528 | error_at (DECL_SOURCE_LOCATION (decl), |
6529 | "uninitialized variable %qD in %<constexpr%> context" , |
6530 | decl); |
6531 | |
6532 | if (show_notes && CLASS_TYPE_P (type) && (complain & tf_error)) |
6533 | { |
6534 | tree defaulted_ctor; |
6535 | |
6536 | inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)), |
6537 | "%q#T has no user-provided default constructor" , type); |
6538 | defaulted_ctor = in_class_defaulted_default_constructor (type); |
6539 | if (defaulted_ctor) |
6540 | inform (DECL_SOURCE_LOCATION (defaulted_ctor), |
6541 | "constructor is not user-provided because it is " |
6542 | "explicitly defaulted in the class body" ); |
6543 | inform (DECL_SOURCE_LOCATION (field), |
6544 | "and the implicitly-defined constructor does not " |
6545 | "initialize %q#D" , field); |
6546 | } |
6547 | |
6548 | return false; |
6549 | } |
6550 | |
6551 | return true; |
6552 | } |
6553 | |
6554 | /* Structure holding the current initializer being processed by reshape_init. |
6555 | CUR is a pointer to the current element being processed, END is a pointer |
6556 | after the last element present in the initializer. */ |
6557 | struct reshape_iter |
6558 | { |
6559 | constructor_elt *cur; |
6560 | constructor_elt *end; |
6561 | }; |
6562 | |
6563 | static tree reshape_init_r (tree, reshape_iter *, tree, tsubst_flags_t); |
6564 | |
6565 | /* FIELD is an element of TYPE_FIELDS or NULL. In the former case, the value |
6566 | returned is the next FIELD_DECL (possibly FIELD itself) that can be |
6567 | initialized as if for an aggregate class. If there are no more such fields, |
6568 | the return value will be NULL. */ |
6569 | |
6570 | tree |
6571 | next_aggregate_field (tree field) |
6572 | { |
6573 | while (field |
6574 | && (TREE_CODE (field) != FIELD_DECL |
6575 | || DECL_UNNAMED_BIT_FIELD (field) |
6576 | || (DECL_ARTIFICIAL (field) |
6577 | /* In C++17, aggregates can have bases. */ |
6578 | && !(cxx_dialect >= cxx17 && DECL_FIELD_IS_BASE (field))))) |
6579 | field = DECL_CHAIN (field); |
6580 | |
6581 | return field; |
6582 | } |
6583 | |
6584 | /* FIELD is an element of TYPE_FIELDS or NULL. In the former case, the value |
6585 | returned is the next FIELD_DECL (possibly FIELD itself) that corresponds |
6586 | to a subobject. If there are no more such fields, the return value will be |
6587 | NULL. */ |
6588 | |
6589 | tree |
6590 | next_subobject_field (tree field) |
6591 | { |
6592 | while (field |
6593 | && (TREE_CODE (field) != FIELD_DECL |
6594 | || DECL_UNNAMED_BIT_FIELD (field) |
6595 | || (DECL_ARTIFICIAL (field) |
6596 | && !DECL_FIELD_IS_BASE (field) |
6597 | && !DECL_VIRTUAL_P (field)))) |
6598 | field = DECL_CHAIN (field); |
6599 | |
6600 | return field; |
6601 | } |
6602 | |
6603 | /* Return true for [dcl.init.list] direct-list-initialization from |
6604 | single element of enumeration with a fixed underlying type. */ |
6605 | |
6606 | bool |
6607 | is_direct_enum_init (tree type, tree init) |
6608 | { |
6609 | if (cxx_dialect >= cxx17 |
6610 | && TREE_CODE (type) == ENUMERAL_TYPE |
6611 | && ENUM_FIXED_UNDERLYING_TYPE_P (type) |
6612 | && TREE_CODE (init) == CONSTRUCTOR |
6613 | && CONSTRUCTOR_IS_DIRECT_INIT (init) |
6614 | && CONSTRUCTOR_NELTS (init) == 1 |
6615 | /* DR 2374: The single element needs to be implicitly |
6616 | convertible to the underlying type of the enum. */ |
6617 | && can_convert_arg (ENUM_UNDERLYING_TYPE (type), |
6618 | TREE_TYPE (CONSTRUCTOR_ELT (init, 0)->value), |
6619 | CONSTRUCTOR_ELT (init, 0)->value, |
6620 | LOOKUP_IMPLICIT, tf_none)) |
6621 | return true; |
6622 | return false; |
6623 | } |
6624 | |
6625 | /* Subroutine of reshape_init_array and reshape_init_vector, which does |
6626 | the actual work. ELT_TYPE is the element type of the array. MAX_INDEX is an |
6627 | INTEGER_CST representing the size of the array minus one (the maximum index), |
6628 | or NULL_TREE if the array was declared without specifying the size. D is |
6629 | the iterator within the constructor. */ |
6630 | |
6631 | static tree |
6632 | reshape_init_array_1 (tree elt_type, tree max_index, reshape_iter *d, |
6633 | tree first_initializer_p, tsubst_flags_t complain) |
6634 | { |
6635 | tree new_init; |
6636 | bool sized_array_p = (max_index && TREE_CONSTANT (max_index)); |
6637 | unsigned HOST_WIDE_INT max_index_cst = 0; |
6638 | unsigned HOST_WIDE_INT index; |
6639 | |
6640 | /* The initializer for an array is always a CONSTRUCTOR. If this is the |
6641 | outermost CONSTRUCTOR and the element type is non-aggregate, we don't need |
6642 | to build a new one. But don't reuse if not complaining; if this is |
6643 | tentative, we might also reshape to another type (95319). */ |
6644 | bool reuse = (first_initializer_p |
6645 | && (complain & tf_error) |
6646 | && !CP_AGGREGATE_TYPE_P (elt_type) |
6647 | && !TREE_SIDE_EFFECTS (first_initializer_p)); |
6648 | if (reuse) |
6649 | new_init = first_initializer_p; |
6650 | else |
6651 | new_init = build_constructor (init_list_type_node, NULL); |
6652 | |
6653 | if (sized_array_p) |
6654 | { |
6655 | /* Minus 1 is used for zero sized arrays. */ |
6656 | if (integer_all_onesp (max_index)) |
6657 | return new_init; |
6658 | |
6659 | if (tree_fits_uhwi_p (max_index)) |
6660 | max_index_cst = tree_to_uhwi (max_index); |
6661 | /* sizetype is sign extended, not zero extended. */ |
6662 | else |
6663 | max_index_cst = tree_to_uhwi (fold_convert (size_type_node, max_index)); |
6664 | } |
6665 | |
6666 | /* Loop until there are no more initializers. */ |
6667 | for (index = 0; |
6668 | d->cur != d->end && (!sized_array_p || index <= max_index_cst); |
6669 | ++index) |
6670 | { |
6671 | tree elt_init; |
6672 | constructor_elt *old_cur = d->cur; |
6673 | |
6674 | if (d->cur->index) |
6675 | CONSTRUCTOR_IS_DESIGNATED_INIT (new_init) = true; |
6676 | check_array_designated_initializer (ce: d->cur, index); |
6677 | elt_init = reshape_init_r (elt_type, d, |
6678 | /*first_initializer_p=*/NULL_TREE, |
6679 | complain); |
6680 | if (elt_init == error_mark_node) |
6681 | return error_mark_node; |
6682 | tree idx = size_int (index); |
6683 | if (reuse) |
6684 | { |
6685 | old_cur->index = idx; |
6686 | old_cur->value = elt_init; |
6687 | } |
6688 | else |
6689 | CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init), |
6690 | idx, elt_init); |
6691 | if (!TREE_CONSTANT (elt_init)) |
6692 | TREE_CONSTANT (new_init) = false; |
6693 | |
6694 | /* This can happen with an invalid initializer (c++/54501). */ |
6695 | if (d->cur == old_cur && !sized_array_p) |
6696 | break; |
6697 | } |
6698 | |
6699 | return new_init; |
6700 | } |
6701 | |
6702 | /* Subroutine of reshape_init_r, processes the initializers for arrays. |
6703 | Parameters are the same of reshape_init_r. */ |
6704 | |
6705 | static tree |
6706 | reshape_init_array (tree type, reshape_iter *d, tree first_initializer_p, |
6707 | tsubst_flags_t complain) |
6708 | { |
6709 | tree max_index = NULL_TREE; |
6710 | |
6711 | gcc_assert (TREE_CODE (type) == ARRAY_TYPE); |
6712 | |
6713 | if (TYPE_DOMAIN (type)) |
6714 | max_index = array_type_nelts (type); |
6715 | |
6716 | return reshape_init_array_1 (TREE_TYPE (type), max_index, d, |
6717 | first_initializer_p, complain); |
6718 | } |
6719 | |
6720 | /* Subroutine of reshape_init_r, processes the initializers for vectors. |
6721 | Parameters are the same of reshape_init_r. */ |
6722 | |
6723 | static tree |
6724 | reshape_init_vector (tree type, reshape_iter *d, tsubst_flags_t complain) |
6725 | { |
6726 | tree max_index = NULL_TREE; |
6727 | |
6728 | gcc_assert (VECTOR_TYPE_P (type)); |
6729 | |
6730 | if (COMPOUND_LITERAL_P (d->cur->value)) |
6731 | { |
6732 | tree value = d->cur->value; |
6733 | if (!same_type_p (TREE_TYPE (value), type)) |
6734 | { |
6735 | if (complain & tf_error) |
6736 | error ("invalid type %qT as initializer for a vector of type %qT" , |
6737 | TREE_TYPE (d->cur->value), type); |
6738 | value = error_mark_node; |
6739 | } |
6740 | ++d->cur; |
6741 | return value; |
6742 | } |
6743 | |
6744 | /* For a vector, we initialize it as an array of the appropriate size. */ |
6745 | if (VECTOR_TYPE_P (type)) |
6746 | max_index = size_int (TYPE_VECTOR_SUBPARTS (type) - 1); |
6747 | |
6748 | return reshape_init_array_1 (TREE_TYPE (type), max_index, d, |
6749 | NULL_TREE, complain); |
6750 | } |
6751 | |
6752 | /* Subroutine of reshape_init*: We're initializing an element with TYPE from |
6753 | INIT, in isolation from any designator or other initializers. */ |
6754 | |
6755 | static tree |
6756 | reshape_single_init (tree type, tree init, tsubst_flags_t complain) |
6757 | { |
6758 | /* We could also implement this by wrapping init in a new CONSTRUCTOR and |
6759 | calling reshape_init, but this way can just live on the stack. */ |
6760 | constructor_elt elt = { /*index=*/NULL_TREE, .value: init }; |
6761 | reshape_iter iter = { .cur: &elt, .end: &elt + 1 }; |
6762 | return reshape_init_r (type, &iter, |
6763 | /*first_initializer_p=*/NULL_TREE, |
6764 | complain); |
6765 | } |
6766 | |
6767 | /* Subroutine of reshape_init_r, processes the initializers for classes |
6768 | or union. Parameters are the same of reshape_init_r. */ |
6769 | |
6770 | static tree |
6771 | reshape_init_class (tree type, reshape_iter *d, bool first_initializer_p, |
6772 | tsubst_flags_t complain) |
6773 | { |
6774 | tree field; |
6775 | tree new_init; |
6776 | |
6777 | gcc_assert (CLASS_TYPE_P (type)); |
6778 | |
6779 | /* The initializer for a class is always a CONSTRUCTOR. */ |
6780 | new_init = build_constructor (init_list_type_node, NULL); |
6781 | |
6782 | int binfo_idx = -1; |
6783 | tree binfo = TYPE_BINFO (type); |
6784 | tree base_binfo = NULL_TREE; |
6785 | if (cxx_dialect >= cxx17 && uses_template_parms (type)) |
6786 | { |
6787 | /* We get here from maybe_aggr_guide for C++20 class template argument |
6788 | deduction. In this case we need to look through the binfo because a |
6789 | template doesn't have base fields. */ |
6790 | binfo_idx = 0; |
6791 | BINFO_BASE_ITERATE (binfo, binfo_idx, base_binfo); |
6792 | } |
6793 | if (base_binfo) |
6794 | field = base_binfo; |
6795 | else |
6796 | field = next_aggregate_field (TYPE_FIELDS (type)); |
6797 | |
6798 | if (!field) |
6799 | { |
6800 | /* [dcl.init.aggr] |
6801 | |
6802 | An initializer for an aggregate member that is an |
6803 | empty class shall have the form of an empty |
6804 | initializer-list {}. */ |
6805 | if (!first_initializer_p) |
6806 | { |
6807 | if (complain & tf_error) |
6808 | error ("initializer for %qT must be brace-enclosed" , type); |
6809 | return error_mark_node; |
6810 | } |
6811 | return new_init; |
6812 | } |
6813 | |
6814 | /* For C++20 CTAD, handle pack expansions in the base list. */ |
6815 | tree last_was_pack_expansion = NULL_TREE; |
6816 | |
6817 | /* Loop through the initializable fields, gathering initializers. */ |
6818 | while (d->cur != d->end) |
6819 | { |
6820 | tree field_init; |
6821 | constructor_elt *old_cur = d->cur; |
6822 | bool direct_desig = false; |
6823 | |
6824 | /* Handle C++20 designated initializers. */ |
6825 | if (d->cur->index) |
6826 | { |
6827 | if (d->cur->index == error_mark_node) |
6828 | return error_mark_node; |
6829 | |
6830 | if (TREE_CODE (d->cur->index) == FIELD_DECL) |
6831 | { |
6832 | /* We already reshaped this; we should have returned early from |
6833 | reshape_init. */ |
6834 | gcc_checking_assert (false); |
6835 | if (field != d->cur->index) |
6836 | { |
6837 | if (tree id = DECL_NAME (d->cur->index)) |
6838 | gcc_checking_assert (d->cur->index |
6839 | == get_class_binding (type, id)); |
6840 | field = d->cur->index; |
6841 | } |
6842 | } |
6843 | else if (TREE_CODE (d->cur->index) == IDENTIFIER_NODE) |
6844 | { |
6845 | CONSTRUCTOR_IS_DESIGNATED_INIT (new_init) = true; |
6846 | field = get_class_binding (type, d->cur->index); |
6847 | direct_desig = true; |
6848 | } |
6849 | else |
6850 | { |
6851 | if (complain & tf_error) |
6852 | error ("%<[%E] =%> used in a GNU-style designated initializer" |
6853 | " for class %qT" , d->cur->index, type); |
6854 | return error_mark_node; |
6855 | } |
6856 | |
6857 | if (!field && ANON_AGGR_TYPE_P (type)) |
6858 | /* Apparently the designator isn't for a member of this anonymous |
6859 | struct, so head back to the enclosing class. */ |
6860 | break; |
6861 | |
6862 | if (!field || TREE_CODE (field) != FIELD_DECL) |
6863 | { |
6864 | if (complain & tf_error) |
6865 | error ("%qT has no non-static data member named %qD" , type, |
6866 | d->cur->index); |
6867 | return error_mark_node; |
6868 | } |
6869 | |
6870 | /* If the element is an anonymous union object and the initializer |
6871 | list is a designated-initializer-list, the anonymous union object |
6872 | is initialized by the designated-initializer-list { D }, where D |
6873 | is the designated-initializer-clause naming a member of the |
6874 | anonymous union object. */ |
6875 | tree ictx = DECL_CONTEXT (field); |
6876 | if (!same_type_ignoring_top_level_qualifiers_p (ictx, type)) |
6877 | { |
6878 | /* Find the anon aggr that is a direct member of TYPE. */ |
6879 | while (ANON_AGGR_TYPE_P (ictx)) |
6880 | { |
6881 | tree cctx = TYPE_CONTEXT (ictx); |
6882 | if (same_type_ignoring_top_level_qualifiers_p (cctx, type)) |
6883 | goto found; |
6884 | ictx = cctx; |
6885 | } |
6886 | |
6887 | /* Not found, e.g. FIELD is a member of a base class. */ |
6888 | if (complain & tf_error) |
6889 | error ("%qD is not a direct member of %qT" , field, type); |
6890 | return error_mark_node; |
6891 | |
6892 | found: |
6893 | /* Now find the TYPE member with that anon aggr type. */ |
6894 | tree aafield = TYPE_FIELDS (type); |
6895 | for (; aafield; aafield = TREE_CHAIN (aafield)) |
6896 | if (TREE_TYPE (aafield) == ictx) |
6897 | break; |
6898 | gcc_assert (aafield); |
6899 | field = aafield; |
6900 | direct_desig = false; |
6901 | } |
6902 | } |
6903 | |
6904 | /* If we processed all the member of the class, we are done. */ |
6905 | if (!field) |
6906 | break; |
6907 | |
6908 | last_was_pack_expansion = (PACK_EXPANSION_P (TREE_TYPE (field)) |
6909 | ? field : NULL_TREE); |
6910 | if (last_was_pack_expansion) |
6911 | /* Each non-trailing aggregate element that is a pack expansion is |
6912 | assumed to correspond to no elements of the initializer list. */ |
6913 | goto continue_; |
6914 | |
6915 | if (direct_desig) |
6916 | { |
6917 | /* The designated field F is initialized from this one element. |
6918 | |
6919 | Note that we don't want to do this if we found the designator |
6920 | inside an anon aggr above; we use the normal code to implement: |
6921 | |
6922 | "If the element is an anonymous union member and the initializer |
6923 | list is a brace-enclosed designated- initializer-list, the element |
6924 | is initialized by the designated-initializer-list { D }, where D |
6925 | is the designated- initializer-clause naming a member of the |
6926 | anonymous union member." */ |
6927 | field_init = reshape_single_init (TREE_TYPE (field), |
6928 | init: d->cur->value, complain); |
6929 | d->cur++; |
6930 | } |
6931 | else |
6932 | field_init = reshape_init_r (TREE_TYPE (field), d, |
6933 | /*first_initializer_p=*/NULL_TREE, |
6934 | complain); |
6935 | |
6936 | if (field_init == error_mark_node) |
6937 | return error_mark_node; |
6938 | |
6939 | if (d->cur == old_cur && d->cur->index) |
6940 | { |
6941 | /* This can happen with an invalid initializer for a flexible |
6942 | array member (c++/54441). */ |
6943 | if (complain & tf_error) |
6944 | error ("invalid initializer for %q#D" , field); |
6945 | return error_mark_node; |
6946 | } |
6947 | |
6948 | CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init), field, field_init); |
6949 | |
6950 | /* [dcl.init.aggr] |
6951 | |
6952 | When a union is initialized with a brace-enclosed |
6953 | initializer, the braces shall only contain an |
6954 | initializer for the first member of the union. */ |
6955 | if (TREE_CODE (type) == UNION_TYPE) |
6956 | break; |
6957 | |
6958 | continue_: |
6959 | if (base_binfo) |
6960 | { |
6961 | if (BINFO_BASE_ITERATE (binfo, ++binfo_idx, base_binfo)) |
6962 | field = base_binfo; |
6963 | else |
6964 | field = next_aggregate_field (TYPE_FIELDS (type)); |
6965 | } |
6966 | else |
6967 | field = next_aggregate_field (DECL_CHAIN (field)); |
6968 | } |
6969 | |
6970 | /* A trailing aggregate element that is a pack expansion is assumed to |
6971 | correspond to all remaining elements of the initializer list (if any). */ |
6972 | if (last_was_pack_expansion) |
6973 | { |
6974 | CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init), |
6975 | last_was_pack_expansion, d->cur->value); |
6976 | while (d->cur != d->end) |
6977 | d->cur++; |
6978 | } |
6979 | |
6980 | return new_init; |
6981 | } |
6982 | |
6983 | /* Subroutine of reshape_init_r. We're in a context where C99 initializer |
6984 | designators are not valid; either complain or return true to indicate |
6985 | that reshape_init_r should return error_mark_node. */ |
6986 | |
6987 | static bool |
6988 | has_designator_problem (reshape_iter *d, tsubst_flags_t complain) |
6989 | { |
6990 | if (d->cur->index) |
6991 | { |
6992 | if (complain & tf_error) |
6993 | error_at (cp_expr_loc_or_input_loc (t: d->cur->index), |
6994 | "C99 designator %qE outside aggregate initializer" , |
6995 | d->cur->index); |
6996 | else |
6997 | return true; |
6998 | } |
6999 | return false; |
7000 | } |
7001 | |
7002 | /* Subroutine of reshape_init, which processes a single initializer (part of |
7003 | a CONSTRUCTOR). TYPE is the type of the variable being initialized, D is the |
7004 | iterator within the CONSTRUCTOR which points to the initializer to process. |
7005 | If this is the first initializer of the outermost CONSTRUCTOR node, |
7006 | FIRST_INITIALIZER_P is that CONSTRUCTOR; otherwise, it is NULL_TREE. */ |
7007 | |
7008 | static tree |
7009 | reshape_init_r (tree type, reshape_iter *d, tree first_initializer_p, |
7010 | tsubst_flags_t complain) |
7011 | { |
7012 | tree init = d->cur->value; |
7013 | |
7014 | if (error_operand_p (t: init)) |
7015 | return error_mark_node; |
7016 | |
7017 | if (first_initializer_p && !CP_AGGREGATE_TYPE_P (type) |
7018 | && has_designator_problem (d, complain)) |
7019 | return error_mark_node; |
7020 | |
7021 | tree stripped_init = tree_strip_any_location_wrapper (exp: init); |
7022 | |
7023 | if (TREE_CODE (type) == COMPLEX_TYPE) |
7024 | { |
7025 | /* A complex type can be initialized from one or two initializers, |
7026 | but braces are not elided. */ |
7027 | d->cur++; |
7028 | if (BRACE_ENCLOSED_INITIALIZER_P (stripped_init)) |
7029 | { |
7030 | if (CONSTRUCTOR_NELTS (stripped_init) > 2) |
7031 | { |
7032 | if (complain & tf_error) |
7033 | error ("too many initializers for %qT" , type); |
7034 | else |
7035 | return error_mark_node; |
7036 | } |
7037 | } |
7038 | else if (first_initializer_p && d->cur != d->end) |
7039 | { |
7040 | vec<constructor_elt, va_gc> *v = 0; |
7041 | CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, init); |
7042 | CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, d->cur->value); |
7043 | if (has_designator_problem (d, complain)) |
7044 | return error_mark_node; |
7045 | d->cur++; |
7046 | init = build_constructor (init_list_type_node, v); |
7047 | } |
7048 | return init; |
7049 | } |
7050 | |
7051 | /* A non-aggregate type is always initialized with a single |
7052 | initializer. */ |
7053 | if (!CP_AGGREGATE_TYPE_P (type) |
7054 | /* As is an array with dependent bound, which we can see |
7055 | during C++20 aggregate CTAD. */ |
7056 | || (cxx_dialect >= cxx20 |
7057 | && TREE_CODE (type) == ARRAY_TYPE |
7058 | && uses_template_parms (TYPE_DOMAIN (type)))) |
7059 | { |
7060 | /* It is invalid to initialize a non-aggregate type with a |
7061 | brace-enclosed initializer before C++0x. |
7062 | We need to check for BRACE_ENCLOSED_INITIALIZER_P here because |
7063 | of g++.old-deja/g++.mike/p7626.C: a pointer-to-member constant is |
7064 | a CONSTRUCTOR (with a record type). */ |
7065 | if (TREE_CODE (stripped_init) == CONSTRUCTOR |
7066 | /* Don't complain about a capture-init. */ |
7067 | && !CONSTRUCTOR_IS_DIRECT_INIT (stripped_init) |
7068 | && BRACE_ENCLOSED_INITIALIZER_P (stripped_init)) /* p7626.C */ |
7069 | { |
7070 | if (SCALAR_TYPE_P (type)) |
7071 | { |
7072 | if (cxx_dialect < cxx11) |
7073 | { |
7074 | if (complain & tf_error) |
7075 | error ("braces around scalar initializer for type %qT" , |
7076 | type); |
7077 | init = error_mark_node; |
7078 | } |
7079 | else if (first_initializer_p |
7080 | || (CONSTRUCTOR_NELTS (stripped_init) > 0 |
7081 | && (BRACE_ENCLOSED_INITIALIZER_P |
7082 | (CONSTRUCTOR_ELT (stripped_init,0)->value)))) |
7083 | { |
7084 | if (complain & tf_error) |
7085 | error ("too many braces around scalar initializer " |
7086 | "for type %qT" , type); |
7087 | init = error_mark_node; |
7088 | } |
7089 | } |
7090 | else |
7091 | maybe_warn_cpp0x (str: CPP0X_INITIALIZER_LISTS); |
7092 | } |
7093 | |
7094 | d->cur++; |
7095 | return init; |
7096 | } |
7097 | |
7098 | /* "If T is a class type and the initializer list has a single element of |
7099 | type cv U, where U is T or a class derived from T, the object is |
7100 | initialized from that element." Even if T is an aggregate. */ |
7101 | if (cxx_dialect >= cxx11 && (CLASS_TYPE_P (type) || VECTOR_TYPE_P (type)) |
7102 | && first_initializer_p |
7103 | /* But not if it's a designated init. */ |
7104 | && !d->cur->index |
7105 | && d->end - d->cur == 1 |
7106 | && reference_related_p (type, TREE_TYPE (init))) |
7107 | { |
7108 | d->cur++; |
7109 | return init; |
7110 | } |
7111 | |
7112 | /* [dcl.init.aggr] |
7113 | |
7114 | All implicit type conversions (clause _conv_) are considered when |
7115 | initializing the aggregate member with an initializer from an |
7116 | initializer-list. If the initializer can initialize a member, |
7117 | the member is initialized. Otherwise, if the member is itself a |
7118 | non-empty subaggregate, brace elision is assumed and the |
7119 | initializer is considered for the initialization of the first |
7120 | member of the subaggregate. */ |
7121 | if ((TREE_CODE (init) != CONSTRUCTOR || COMPOUND_LITERAL_P (init)) |
7122 | /* But don't try this for the first initializer, since that would be |
7123 | looking through the outermost braces; A a2 = { a1 }; is not a |
7124 | valid aggregate initialization. */ |
7125 | && !first_initializer_p |
7126 | && (same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (init)) |
7127 | || can_convert_arg (type, TREE_TYPE (init), init, LOOKUP_NORMAL, |
7128 | complain))) |
7129 | { |
7130 | d->cur++; |
7131 | return init; |
7132 | } |
7133 | |
7134 | /* [dcl.init.string] |
7135 | |
7136 | A char array (whether plain char, signed char, or unsigned char) |
7137 | can be initialized by a string-literal (optionally enclosed in |
7138 | braces); a wchar_t array can be initialized by a wide |
7139 | string-literal (optionally enclosed in braces). */ |
7140 | if (TREE_CODE (type) == ARRAY_TYPE |
7141 | && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type)))) |
7142 | { |
7143 | tree str_init = init; |
7144 | tree stripped_str_init = stripped_init; |
7145 | reshape_iter stripd = {}; |
7146 | |
7147 | /* Strip one level of braces if and only if they enclose a single |
7148 | element (as allowed by [dcl.init.string]). */ |
7149 | if (!first_initializer_p |
7150 | && TREE_CODE (stripped_str_init) == CONSTRUCTOR |
7151 | && CONSTRUCTOR_NELTS (stripped_str_init) == 1) |
7152 | { |
7153 | stripd.cur = CONSTRUCTOR_ELT (stripped_str_init, 0); |
7154 | str_init = stripd.cur->value; |
7155 | stripped_str_init = tree_strip_any_location_wrapper (exp: str_init); |
7156 | } |
7157 | |
7158 | /* If it's a string literal, then it's the initializer for the array |
7159 | as a whole. Otherwise, continue with normal initialization for |
7160 | array types (one value per array element). */ |
7161 | if (TREE_CODE (stripped_str_init) == STRING_CST) |
7162 | { |
7163 | if ((first_initializer_p && has_designator_problem (d, complain)) |
7164 | || (stripd.cur && has_designator_problem (d: &stripd, complain))) |
7165 | return error_mark_node; |
7166 | d->cur++; |
7167 | return str_init; |
7168 | } |
7169 | } |
7170 | |
7171 | /* The following cases are about aggregates. If we are not within a full |
7172 | initializer already, and there is not a CONSTRUCTOR, it means that there |
7173 | is a missing set of braces (that is, we are processing the case for |
7174 | which reshape_init exists). */ |
7175 | bool braces_elided_p = false; |
7176 | if (!first_initializer_p) |
7177 | { |
7178 | if (TREE_CODE (stripped_init) == CONSTRUCTOR) |
7179 | { |
7180 | tree init_type = TREE_TYPE (init); |
7181 | if (init_type && TYPE_PTRMEMFUNC_P (init_type)) |
7182 | /* There is no need to call reshape_init for pointer-to-member |
7183 | function initializers, as they are always constructed correctly |
7184 | by the front end. Here we have e.g. {.__pfn=0B, .__delta=0}, |
7185 | which is missing outermost braces. We should warn below, and |
7186 | one of the routines below will wrap it in additional { }. */; |
7187 | /* For a nested compound literal, proceed to specialized routines, |
7188 | to handle initialization of arrays and similar. */ |
7189 | else if (COMPOUND_LITERAL_P (stripped_init)) |
7190 | gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (stripped_init)); |
7191 | /* If we have an unresolved designator, we need to find the member it |
7192 | designates within TYPE, so proceed to the routines below. For |
7193 | FIELD_DECL or INTEGER_CST designators, we're already initializing |
7194 | the designated element. */ |
7195 | else if (d->cur->index |
7196 | && TREE_CODE (d->cur->index) == IDENTIFIER_NODE) |
7197 | /* Brace elision with designators is only permitted for anonymous |
7198 | aggregates. */ |
7199 | gcc_checking_assert (ANON_AGGR_TYPE_P (type)); |
7200 | /* A CONSTRUCTOR of the target's type is a previously |
7201 | digested initializer. */ |
7202 | else if (same_type_ignoring_top_level_qualifiers_p (type, init_type)) |
7203 | { |
7204 | ++d->cur; |
7205 | return init; |
7206 | } |
7207 | else |
7208 | { |
7209 | /* Something that hasn't been reshaped yet. */ |
7210 | ++d->cur; |
7211 | gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (stripped_init)); |
7212 | return reshape_init (type, init, complain); |
7213 | } |
7214 | } |
7215 | |
7216 | if (complain & tf_warning) |
7217 | warning (OPT_Wmissing_braces, |
7218 | "missing braces around initializer for %qT" , |
7219 | type); |
7220 | braces_elided_p = true; |
7221 | } |
7222 | |
7223 | /* Dispatch to specialized routines. */ |
7224 | tree new_init; |
7225 | if (CLASS_TYPE_P (type)) |
7226 | new_init = reshape_init_class (type, d, first_initializer_p, complain); |
7227 | else if (TREE_CODE (type) == ARRAY_TYPE) |
7228 | new_init = reshape_init_array (type, d, first_initializer_p, complain); |
7229 | else if (VECTOR_TYPE_P (type)) |
7230 | new_init = reshape_init_vector (type, d, complain); |
7231 | else |
7232 | gcc_unreachable(); |
7233 | |
7234 | if (braces_elided_p |
7235 | && TREE_CODE (new_init) == CONSTRUCTOR) |
7236 | CONSTRUCTOR_BRACES_ELIDED_P (new_init) = true; |
7237 | |
7238 | return new_init; |
7239 | } |
7240 | |
7241 | /* Undo the brace-elision allowed by [dcl.init.aggr] in a |
7242 | brace-enclosed aggregate initializer. |
7243 | |
7244 | INIT is the CONSTRUCTOR containing the list of initializers describing |
7245 | a brace-enclosed initializer for an entity of the indicated aggregate TYPE. |
7246 | It may not presently match the shape of the TYPE; for example: |
7247 | |
7248 | struct S { int a; int b; }; |
7249 | struct S a[] = { 1, 2, 3, 4 }; |
7250 | |
7251 | Here INIT will hold a vector of four elements, rather than a |
7252 | vector of two elements, each itself a vector of two elements. This |
7253 | routine transforms INIT from the former form into the latter. The |
7254 | revised CONSTRUCTOR node is returned. */ |
7255 | |
7256 | tree |
7257 | reshape_init (tree type, tree init, tsubst_flags_t complain) |
7258 | { |
7259 | vec<constructor_elt, va_gc> *v; |
7260 | reshape_iter d; |
7261 | tree new_init; |
7262 | |
7263 | gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init)); |
7264 | |
7265 | v = CONSTRUCTOR_ELTS (init); |
7266 | |
7267 | /* An empty constructor does not need reshaping, and it is always a valid |
7268 | initializer. */ |
7269 | if (vec_safe_is_empty (v)) |
7270 | return init; |
7271 | |
7272 | if ((*v)[0].index && TREE_CODE ((*v)[0].index) == FIELD_DECL) |
7273 | /* Already reshaped. */ |
7274 | return init; |
7275 | |
7276 | /* Brace elision is not performed for a CONSTRUCTOR representing |
7277 | parenthesized aggregate initialization. */ |
7278 | if (CONSTRUCTOR_IS_PAREN_INIT (init)) |
7279 | { |
7280 | tree elt = (*v)[0].value; |
7281 | /* If we're initializing a char array from a string-literal that is |
7282 | enclosed in braces, unwrap it here. */ |
7283 | if (TREE_CODE (type) == ARRAY_TYPE |
7284 | && vec_safe_length (v) == 1 |
7285 | && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type))) |
7286 | && TREE_CODE (tree_strip_any_location_wrapper (elt)) == STRING_CST) |
7287 | return elt; |
7288 | return init; |
7289 | } |
7290 | |
7291 | /* Handle [dcl.init.list] direct-list-initialization from |
7292 | single element of enumeration with a fixed underlying type. */ |
7293 | if (is_direct_enum_init (type, init)) |
7294 | { |
7295 | tree elt = CONSTRUCTOR_ELT (init, 0)->value; |
7296 | type = cv_unqualified (type); |
7297 | if (check_narrowing (ENUM_UNDERLYING_TYPE (type), elt, complain)) |
7298 | { |
7299 | warning_sentinel w (warn_useless_cast); |
7300 | warning_sentinel w2 (warn_ignored_qualifiers); |
7301 | return cp_build_c_cast (input_location, type, elt, |
7302 | tf_warning_or_error); |
7303 | } |
7304 | else |
7305 | return error_mark_node; |
7306 | } |
7307 | |
7308 | /* Recurse on this CONSTRUCTOR. */ |
7309 | d.cur = &(*v)[0]; |
7310 | d.end = d.cur + v->length (); |
7311 | |
7312 | new_init = reshape_init_r (type, d: &d, first_initializer_p: init, complain); |
7313 | if (new_init == error_mark_node) |
7314 | return error_mark_node; |
7315 | |
7316 | /* Make sure all the element of the constructor were used. Otherwise, |
7317 | issue an error about exceeding initializers. */ |
7318 | if (d.cur != d.end) |
7319 | { |
7320 | if (complain & tf_error) |
7321 | error ("too many initializers for %qT" , type); |
7322 | return error_mark_node; |
7323 | } |
7324 | |
7325 | if (CONSTRUCTOR_IS_DIRECT_INIT (init) |
7326 | && BRACE_ENCLOSED_INITIALIZER_P (new_init)) |
7327 | CONSTRUCTOR_IS_DIRECT_INIT (new_init) = true; |
7328 | if (CONSTRUCTOR_IS_DESIGNATED_INIT (init) |
7329 | && BRACE_ENCLOSED_INITIALIZER_P (new_init)) |
7330 | gcc_checking_assert (CONSTRUCTOR_IS_DESIGNATED_INIT (new_init) |
7331 | || seen_error ()); |
7332 | |
7333 | return new_init; |
7334 | } |
7335 | |
7336 | /* Verify array initializer. Returns true if errors have been reported. */ |
7337 | |
7338 | bool |
7339 | check_array_initializer (tree decl, tree type, tree init) |
7340 | { |
7341 | tree element_type = TREE_TYPE (type); |
7342 | |
7343 | /* Structured binding when initialized with an array type needs |
7344 | to have complete type. */ |
7345 | if (decl |
7346 | && DECL_DECOMPOSITION_P (decl) |
7347 | && !DECL_DECOMP_BASE (decl) |
7348 | && !COMPLETE_TYPE_P (type)) |
7349 | { |
7350 | error_at (DECL_SOURCE_LOCATION (decl), |
7351 | "structured binding has incomplete type %qT" , type); |
7352 | TREE_TYPE (decl) = error_mark_node; |
7353 | return true; |
7354 | } |
7355 | |
7356 | /* The array type itself need not be complete, because the |
7357 | initializer may tell us how many elements are in the array. |
7358 | But, the elements of the array must be complete. */ |
7359 | if (!COMPLETE_TYPE_P (complete_type (element_type))) |
7360 | { |
7361 | if (decl) |
7362 | error_at (DECL_SOURCE_LOCATION (decl), |
7363 | "elements of array %q#D have incomplete type" , decl); |
7364 | else |
7365 | error ("elements of array %q#T have incomplete type" , type); |
7366 | return true; |
7367 | } |
7368 | |
7369 | location_t loc = (decl ? location_of (decl) : input_location); |
7370 | if (!verify_type_context (loc, TCTX_ARRAY_ELEMENT, element_type)) |
7371 | return true; |
7372 | |
7373 | /* A compound literal can't have variable size. */ |
7374 | if (init && !decl |
7375 | && ((COMPLETE_TYPE_P (type) && !TREE_CONSTANT (TYPE_SIZE (type))) |
7376 | || !TREE_CONSTANT (TYPE_SIZE (element_type)))) |
7377 | { |
7378 | error ("variable-sized compound literal" ); |
7379 | return true; |
7380 | } |
7381 | return false; |
7382 | } |
7383 | |
7384 | /* Subroutine of check_initializer; args are passed down from that function. |
7385 | Set stmts_are_full_exprs_p to 1 across a call to build_aggr_init. */ |
7386 | |
7387 | static tree |
7388 | build_aggr_init_full_exprs (tree decl, tree init, int flags) |
7389 | { |
7390 | gcc_assert (stmts_are_full_exprs_p ()); |
7391 | if (init) |
7392 | maybe_warn_pessimizing_move (init, TREE_TYPE (decl), /*return_p*/false); |
7393 | return build_aggr_init (decl, init, flags, tf_warning_or_error); |
7394 | } |
7395 | |
7396 | /* Verify INIT (the initializer for DECL), and record the |
7397 | initialization in DECL_INITIAL, if appropriate. CLEANUP is as for |
7398 | grok_reference_init. |
7399 | |
7400 | If the return value is non-NULL, it is an expression that must be |
7401 | evaluated dynamically to initialize DECL. */ |
7402 | |
7403 | static tree |
7404 | check_initializer (tree decl, tree init, int flags, vec<tree, va_gc> **cleanups) |
7405 | { |
7406 | tree type; |
7407 | tree init_code = NULL; |
7408 | tree core_type; |
7409 | |
7410 | /* Things that are going to be initialized need to have complete |
7411 | type. */ |
7412 | TREE_TYPE (decl) = type = complete_type (TREE_TYPE (decl)); |
7413 | |
7414 | if (DECL_HAS_VALUE_EXPR_P (decl)) |
7415 | { |
7416 | /* A variable with DECL_HAS_VALUE_EXPR_P set is just a placeholder, |
7417 | it doesn't have storage to be initialized. */ |
7418 | gcc_assert (init == NULL_TREE); |
7419 | return NULL_TREE; |
7420 | } |
7421 | |
7422 | if (type == error_mark_node) |
7423 | /* We will have already complained. */ |
7424 | return NULL_TREE; |
7425 | |
7426 | if (TREE_CODE (type) == ARRAY_TYPE) |
7427 | { |
7428 | if (check_array_initializer (decl, type, init)) |
7429 | return NULL_TREE; |
7430 | } |
7431 | else if (!COMPLETE_TYPE_P (type)) |
7432 | { |
7433 | error_at (DECL_SOURCE_LOCATION (decl), |
7434 | "%q#D has incomplete type" , decl); |
7435 | TREE_TYPE (decl) = error_mark_node; |
7436 | return NULL_TREE; |
7437 | } |
7438 | else |
7439 | /* There is no way to make a variable-sized class type in GNU C++. */ |
7440 | gcc_assert (TREE_CONSTANT (TYPE_SIZE (type))); |
7441 | |
7442 | if (init && BRACE_ENCLOSED_INITIALIZER_P (init)) |
7443 | { |
7444 | int init_len = CONSTRUCTOR_NELTS (init); |
7445 | if (SCALAR_TYPE_P (type)) |
7446 | { |
7447 | if (init_len == 0) |
7448 | { |
7449 | maybe_warn_cpp0x (str: CPP0X_INITIALIZER_LISTS); |
7450 | init = build_zero_init (type, NULL_TREE, false); |
7451 | } |
7452 | else if (init_len != 1 && TREE_CODE (type) != COMPLEX_TYPE) |
7453 | { |
7454 | error_at (cp_expr_loc_or_loc (t: init, DECL_SOURCE_LOCATION (decl)), |
7455 | "scalar object %qD requires one element in " |
7456 | "initializer" , decl); |
7457 | TREE_TYPE (decl) = error_mark_node; |
7458 | return NULL_TREE; |
7459 | } |
7460 | } |
7461 | } |
7462 | |
7463 | if (TREE_CODE (decl) == CONST_DECL) |
7464 | { |
7465 | gcc_assert (!TYPE_REF_P (type)); |
7466 | |
7467 | DECL_INITIAL (decl) = init; |
7468 | |
7469 | gcc_assert (init != NULL_TREE); |
7470 | init = NULL_TREE; |
7471 | } |
7472 | else if (!init && DECL_REALLY_EXTERN (decl)) |
7473 | ; |
7474 | else if (init || type_build_ctor_call (type) |
7475 | || TYPE_REF_P (type)) |
7476 | { |
7477 | if (TYPE_REF_P (type)) |
7478 | { |
7479 | init = grok_reference_init (decl, type, init, flags); |
7480 | flags |= LOOKUP_ALREADY_DIGESTED; |
7481 | } |
7482 | else if (!init) |
7483 | check_for_uninitialized_const_var (decl, /*constexpr_context_p=*/false, |
7484 | complain: tf_warning_or_error); |
7485 | /* Do not reshape constructors of vectors (they don't need to be |
7486 | reshaped. */ |
7487 | else if (BRACE_ENCLOSED_INITIALIZER_P (init)) |
7488 | { |
7489 | if (is_std_init_list (type)) |
7490 | { |
7491 | init = perform_implicit_conversion (type, init, |
7492 | tf_warning_or_error); |
7493 | flags |= LOOKUP_ALREADY_DIGESTED; |
7494 | } |
7495 | else if (TYPE_NON_AGGREGATE_CLASS (type)) |
7496 | { |
7497 | /* Don't reshape if the class has constructors. */ |
7498 | if (cxx_dialect == cxx98) |
7499 | error_at (cp_expr_loc_or_loc (t: init, DECL_SOURCE_LOCATION (decl)), |
7500 | "in C++98 %qD must be initialized by " |
7501 | "constructor, not by %<{...}%>" , |
7502 | decl); |
7503 | } |
7504 | else if (VECTOR_TYPE_P (type) && TYPE_VECTOR_OPAQUE (type)) |
7505 | { |
7506 | error ("opaque vector types cannot be initialized" ); |
7507 | init = error_mark_node; |
7508 | } |
7509 | else |
7510 | { |
7511 | init = reshape_init (type, init, complain: tf_warning_or_error); |
7512 | flags |= LOOKUP_NO_NARROWING; |
7513 | } |
7514 | } |
7515 | /* [dcl.init] "Otherwise, if the destination type is an array, the object |
7516 | is initialized as follows..." So handle things like |
7517 | |
7518 | int a[](1, 2, 3); |
7519 | |
7520 | which is permitted in C++20 by P0960. */ |
7521 | else if (TREE_CODE (init) == TREE_LIST |
7522 | && TREE_TYPE (init) == NULL_TREE |
7523 | && TREE_CODE (type) == ARRAY_TYPE |
7524 | && !DECL_DECOMPOSITION_P (decl) |
7525 | && (cxx_dialect >= cxx20)) |
7526 | init = do_aggregate_paren_init (init, type); |
7527 | else if (TREE_CODE (init) == TREE_LIST |
7528 | && TREE_TYPE (init) != unknown_type_node |
7529 | && !MAYBE_CLASS_TYPE_P (type)) |
7530 | { |
7531 | gcc_assert (TREE_CODE (decl) != RESULT_DECL); |
7532 | |
7533 | /* We get here with code like `int a (2);' */ |
7534 | init = build_x_compound_expr_from_list (init, ELK_INIT, |
7535 | tf_warning_or_error); |
7536 | } |
7537 | |
7538 | /* If DECL has an array type without a specific bound, deduce the |
7539 | array size from the initializer. */ |
7540 | maybe_deduce_size_from_array_init (decl, init); |
7541 | type = TREE_TYPE (decl); |
7542 | if (type == error_mark_node) |
7543 | return NULL_TREE; |
7544 | |
7545 | if (((type_build_ctor_call (type) || CLASS_TYPE_P (type)) |
7546 | && !(flags & LOOKUP_ALREADY_DIGESTED) |
7547 | && !(init && BRACE_ENCLOSED_INITIALIZER_P (init) |
7548 | && CP_AGGREGATE_TYPE_P (type) |
7549 | && (CLASS_TYPE_P (type) |
7550 | /* The call to build_aggr_init below could end up |
7551 | calling build_vec_init, which may break when we |
7552 | are processing a template. */ |
7553 | || processing_template_decl |
7554 | || !TYPE_NEEDS_CONSTRUCTING (type) |
7555 | || type_has_extended_temps (type)))) |
7556 | || (DECL_DECOMPOSITION_P (decl) && TREE_CODE (type) == ARRAY_TYPE)) |
7557 | { |
7558 | init_code = build_aggr_init_full_exprs (decl, init, flags); |
7559 | |
7560 | /* A constructor call is a non-trivial initializer even if |
7561 | it isn't explicitly written. */ |
7562 | if (TREE_SIDE_EFFECTS (init_code)) |
7563 | DECL_NONTRIVIALLY_INITIALIZED_P (decl) = true; |
7564 | |
7565 | /* If this is a constexpr initializer, expand_default_init will |
7566 | have returned an INIT_EXPR rather than a CALL_EXPR. In that |
7567 | case, pull the initializer back out and pass it down into |
7568 | store_init_value. */ |
7569 | while (true) |
7570 | { |
7571 | if (TREE_CODE (init_code) == EXPR_STMT |
7572 | || TREE_CODE (init_code) == STMT_EXPR |
7573 | || TREE_CODE (init_code) == CONVERT_EXPR) |
7574 | init_code = TREE_OPERAND (init_code, 0); |
7575 | else if (TREE_CODE (init_code) == BIND_EXPR) |
7576 | init_code = BIND_EXPR_BODY (init_code); |
7577 | else |
7578 | break; |
7579 | } |
7580 | if (TREE_CODE (init_code) == INIT_EXPR) |
7581 | { |
7582 | /* In C++20, the call to build_aggr_init could have created |
7583 | an INIT_EXPR with a CONSTRUCTOR as the RHS to handle |
7584 | A(1, 2). */ |
7585 | tree rhs = TREE_OPERAND (init_code, 1); |
7586 | if (processing_template_decl && TREE_CODE (rhs) == TARGET_EXPR) |
7587 | /* Avoid leaking TARGET_EXPR into template trees. */ |
7588 | rhs = build_implicit_conv_flags (type, init, flags); |
7589 | init = rhs; |
7590 | |
7591 | init_code = NULL_TREE; |
7592 | /* Don't call digest_init; it's unnecessary and will complain |
7593 | about aggregate initialization of non-aggregate classes. */ |
7594 | flags |= LOOKUP_ALREADY_DIGESTED; |
7595 | } |
7596 | else if (DECL_DECLARED_CONSTEXPR_P (decl) |
7597 | || DECL_DECLARED_CONSTINIT_P (decl)) |
7598 | { |
7599 | /* Declared constexpr or constinit, but no suitable initializer; |
7600 | massage init appropriately so we can pass it into |
7601 | store_init_value for the error. */ |
7602 | tree new_init = NULL_TREE; |
7603 | if (!processing_template_decl |
7604 | && TREE_CODE (init_code) == CALL_EXPR) |
7605 | new_init = build_cplus_new (type, init_code, tf_none); |
7606 | else if (CLASS_TYPE_P (type) |
7607 | && (!init || TREE_CODE (init) == TREE_LIST)) |
7608 | new_init = build_functional_cast (input_location, type, |
7609 | init, tf_none); |
7610 | if (new_init) |
7611 | { |
7612 | init = new_init; |
7613 | if (TREE_CODE (init) == TARGET_EXPR |
7614 | && !(flags & LOOKUP_ONLYCONVERTING)) |
7615 | TARGET_EXPR_DIRECT_INIT_P (init) = true; |
7616 | } |
7617 | init_code = NULL_TREE; |
7618 | } |
7619 | else |
7620 | init = NULL_TREE; |
7621 | } |
7622 | |
7623 | if (init && TREE_CODE (init) != TREE_VEC) |
7624 | { |
7625 | init_code = store_init_value (decl, init, cleanups, flags); |
7626 | |
7627 | if (DECL_INITIAL (decl) |
7628 | && TREE_CODE (DECL_INITIAL (decl)) == CONSTRUCTOR |
7629 | && !vec_safe_is_empty (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)))) |
7630 | { |
7631 | tree elt = CONSTRUCTOR_ELTS (DECL_INITIAL (decl))->last ().value; |
7632 | if (TREE_CODE (TREE_TYPE (elt)) == ARRAY_TYPE |
7633 | && TYPE_SIZE (TREE_TYPE (elt)) == NULL_TREE) |
7634 | cp_complete_array_type (&TREE_TYPE (elt), elt, false); |
7635 | } |
7636 | |
7637 | if (pedantic && TREE_CODE (type) == ARRAY_TYPE |
7638 | && DECL_INITIAL (decl) |
7639 | && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST |
7640 | && PAREN_STRING_LITERAL_P (DECL_INITIAL (decl))) |
7641 | warning_at (cp_expr_loc_or_loc (DECL_INITIAL (decl), |
7642 | DECL_SOURCE_LOCATION (decl)), |
7643 | 0, "array %qD initialized by parenthesized " |
7644 | "string literal %qE" , |
7645 | decl, DECL_INITIAL (decl)); |
7646 | init = NULL_TREE; |
7647 | } |
7648 | } |
7649 | else |
7650 | { |
7651 | if (CLASS_TYPE_P (core_type = strip_array_types (type)) |
7652 | && (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type) |
7653 | || CLASSTYPE_REF_FIELDS_NEED_INIT (core_type))) |
7654 | diagnose_uninitialized_cst_or_ref_member (core_type, /*using_new=*/false, |
7655 | /*complain=*/true); |
7656 | |
7657 | check_for_uninitialized_const_var (decl, /*constexpr_context_p=*/false, |
7658 | complain: tf_warning_or_error); |
7659 | } |
7660 | |
7661 | if (init && init != error_mark_node) |
7662 | init_code = cp_build_init_expr (t: decl, i: init); |
7663 | |
7664 | if (init_code && !TREE_SIDE_EFFECTS (init_code) |
7665 | && init_code != error_mark_node) |
7666 | init_code = NULL_TREE; |
7667 | |
7668 | if (init_code) |
7669 | { |
7670 | /* We might have set these in cp_finish_decl. */ |
7671 | DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = false; |
7672 | TREE_CONSTANT (decl) = false; |
7673 | } |
7674 | |
7675 | if (init_code |
7676 | && DECL_IN_AGGR_P (decl) |
7677 | && DECL_INITIALIZED_IN_CLASS_P (decl)) |
7678 | { |
7679 | static int explained = 0; |
7680 | |
7681 | if (cxx_dialect < cxx11) |
7682 | error ("initializer invalid for static member with constructor" ); |
7683 | else if (cxx_dialect < cxx17) |
7684 | error ("non-constant in-class initialization invalid for static " |
7685 | "member %qD" , decl); |
7686 | else |
7687 | error ("non-constant in-class initialization invalid for non-inline " |
7688 | "static member %qD" , decl); |
7689 | if (!explained) |
7690 | { |
7691 | inform (input_location, |
7692 | "(an out of class initialization is required)" ); |
7693 | explained = 1; |
7694 | } |
7695 | return NULL_TREE; |
7696 | } |
7697 | |
7698 | return init_code; |
7699 | } |
7700 | |
7701 | /* If DECL is not a local variable, give it RTL. */ |
7702 | |
7703 | static void |
7704 | make_rtl_for_nonlocal_decl (tree decl, tree init, const char* asmspec) |
7705 | { |
7706 | int toplev = toplevel_bindings_p (); |
7707 | int defer_p; |
7708 | |
7709 | /* Set the DECL_ASSEMBLER_NAME for the object. */ |
7710 | if (asmspec) |
7711 | { |
7712 | /* The `register' keyword, when used together with an |
7713 | asm-specification, indicates that the variable should be |
7714 | placed in a particular register. */ |
7715 | if (VAR_P (decl) && DECL_REGISTER (decl)) |
7716 | { |
7717 | set_user_assembler_name (decl, asmspec); |
7718 | DECL_HARD_REGISTER (decl) = 1; |
7719 | } |
7720 | else |
7721 | { |
7722 | if (TREE_CODE (decl) == FUNCTION_DECL |
7723 | && fndecl_built_in_p (node: decl, klass: BUILT_IN_NORMAL)) |
7724 | set_builtin_user_assembler_name (decl, asmspec); |
7725 | set_user_assembler_name (decl, asmspec); |
7726 | if (DECL_LOCAL_DECL_P (decl)) |
7727 | if (auto ns_decl = DECL_LOCAL_DECL_ALIAS (decl)) |
7728 | /* We have to propagate the name to the ns-alias. |
7729 | This is horrible, as we're affecting a |
7730 | possibly-shared decl. Again, a one-true-decl |
7731 | model breaks down. */ |
7732 | if (ns_decl != error_mark_node) |
7733 | set_user_assembler_name (ns_decl, asmspec); |
7734 | } |
7735 | } |
7736 | |
7737 | /* Handle non-variables up front. */ |
7738 | if (!VAR_P (decl)) |
7739 | { |
7740 | rest_of_decl_compilation (decl, toplev, at_eof); |
7741 | return; |
7742 | } |
7743 | |
7744 | /* If we see a class member here, it should be a static data |
7745 | member. */ |
7746 | if (DECL_LANG_SPECIFIC (decl) && DECL_IN_AGGR_P (decl)) |
7747 | { |
7748 | gcc_assert (TREE_STATIC (decl)); |
7749 | /* An in-class declaration of a static data member should be |
7750 | external; it is only a declaration, and not a definition. */ |
7751 | if (init == NULL_TREE) |
7752 | gcc_assert (DECL_EXTERNAL (decl) |
7753 | || !TREE_PUBLIC (decl)); |
7754 | } |
7755 | |
7756 | /* We don't create any RTL for local variables. */ |
7757 | if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl)) |
7758 | return; |
7759 | |
7760 | /* We defer emission of local statics until the corresponding |
7761 | DECL_EXPR is expanded. But with constexpr its function might never |
7762 | be expanded, so go ahead and tell cgraph about the variable now. */ |
7763 | defer_p = ((DECL_FUNCTION_SCOPE_P (decl) |
7764 | && !var_in_maybe_constexpr_fn (decl)) |
7765 | || DECL_VIRTUAL_P (decl)); |
7766 | |
7767 | /* Defer template instantiations. */ |
7768 | if (DECL_LANG_SPECIFIC (decl) |
7769 | && DECL_IMPLICIT_INSTANTIATION (decl)) |
7770 | defer_p = 1; |
7771 | |
7772 | /* If we're not deferring, go ahead and assemble the variable. */ |
7773 | if (!defer_p) |
7774 | rest_of_decl_compilation (decl, toplev, at_eof); |
7775 | } |
7776 | |
7777 | /* walk_tree helper for wrap_temporary_cleanups, below. */ |
7778 | |
7779 | static tree |
7780 | wrap_cleanups_r (tree *stmt_p, int *walk_subtrees, void *data) |
7781 | { |
7782 | /* Stop at types or full-expression boundaries. */ |
7783 | if (TYPE_P (*stmt_p) |
7784 | || TREE_CODE (*stmt_p) == CLEANUP_POINT_EXPR) |
7785 | { |
7786 | *walk_subtrees = 0; |
7787 | return NULL_TREE; |
7788 | } |
7789 | |
7790 | if (TREE_CODE (*stmt_p) == TARGET_EXPR) |
7791 | { |
7792 | tree guard = (tree)data; |
7793 | tree tcleanup = TARGET_EXPR_CLEANUP (*stmt_p); |
7794 | |
7795 | if (tcleanup && !CLEANUP_EH_ONLY (*stmt_p) |
7796 | && !expr_noexcept_p (tcleanup, tf_none)) |
7797 | { |
7798 | tcleanup = build2 (TRY_CATCH_EXPR, void_type_node, tcleanup, guard); |
7799 | /* Tell honor_protect_cleanup_actions to handle this as a separate |
7800 | cleanup. */ |
7801 | TRY_CATCH_IS_CLEANUP (tcleanup) = 1; |
7802 | TARGET_EXPR_CLEANUP (*stmt_p) = tcleanup; |
7803 | } |
7804 | } |
7805 | |
7806 | return NULL_TREE; |
7807 | } |
7808 | |
7809 | /* We're initializing a local variable which has a cleanup GUARD. If there |
7810 | are any temporaries used in the initializer INIT of this variable, we |
7811 | need to wrap their cleanups with TRY_CATCH_EXPR (, GUARD) so that the |
7812 | variable will be cleaned up properly if one of them throws. |
7813 | |
7814 | Unfortunately, there's no way to express this properly in terms of |
7815 | nesting, as the regions for the temporaries overlap the region for the |
7816 | variable itself; if there are two temporaries, the variable needs to be |
7817 | the first thing destroyed if either of them throws. However, we only |
7818 | want to run the variable's cleanup if it actually got constructed. So |
7819 | we need to guard the temporary cleanups with the variable's cleanup if |
7820 | they are run on the normal path, but not if they are run on the |
7821 | exceptional path. We implement this by telling |
7822 | honor_protect_cleanup_actions to strip the variable cleanup from the |
7823 | exceptional path. |
7824 | |
7825 | Another approach could be to make the variable cleanup region enclose |
7826 | initialization, but depend on a flag to indicate that the variable is |
7827 | initialized; that's effectively what we do for arrays. But the current |
7828 | approach works fine for non-arrays, and has no code overhead in the usual |
7829 | case where the temporary destructors are noexcept. */ |
7830 | |
7831 | static void |
7832 | wrap_temporary_cleanups (tree init, tree guard) |
7833 | { |
7834 | if (TREE_CODE (guard) == BIND_EXPR) |
7835 | { |
7836 | /* An array cleanup region already encloses any temporary cleanups, |
7837 | don't wrap it around them again. */ |
7838 | gcc_checking_assert (BIND_EXPR_VEC_DTOR (guard)); |
7839 | return; |
7840 | } |
7841 | cp_walk_tree_without_duplicates (&init, wrap_cleanups_r, (void *)guard); |
7842 | } |
7843 | |
7844 | /* Generate code to initialize DECL (a local variable). */ |
7845 | |
7846 | static void |
7847 | initialize_local_var (tree decl, tree init) |
7848 | { |
7849 | tree type = TREE_TYPE (decl); |
7850 | tree cleanup; |
7851 | int already_used; |
7852 | |
7853 | gcc_assert (VAR_P (decl) |
7854 | || TREE_CODE (decl) == RESULT_DECL); |
7855 | gcc_assert (!TREE_STATIC (decl)); |
7856 | |
7857 | if (DECL_SIZE (decl) == NULL_TREE) |
7858 | { |
7859 | /* If we used it already as memory, it must stay in memory. */ |
7860 | DECL_INITIAL (decl) = NULL_TREE; |
7861 | TREE_ADDRESSABLE (decl) = TREE_USED (decl); |
7862 | return; |
7863 | } |
7864 | |
7865 | if (type == error_mark_node) |
7866 | return; |
7867 | |
7868 | /* Compute and store the initial value. */ |
7869 | already_used = TREE_USED (decl) || TREE_USED (type); |
7870 | if (TREE_USED (type)) |
7871 | DECL_READ_P (decl) = 1; |
7872 | |
7873 | /* Generate a cleanup, if necessary. */ |
7874 | cleanup = cxx_maybe_build_cleanup (decl, tf_warning_or_error); |
7875 | |
7876 | /* Perform the initialization. */ |
7877 | if (init) |
7878 | { |
7879 | tree rinit = (TREE_CODE (init) == INIT_EXPR |
7880 | ? TREE_OPERAND (init, 1) : NULL_TREE); |
7881 | if (rinit && !TREE_SIDE_EFFECTS (rinit) |
7882 | && TREE_OPERAND (init, 0) == decl) |
7883 | { |
7884 | /* Stick simple initializers in DECL_INITIAL so that |
7885 | -Wno-init-self works (c++/34772). */ |
7886 | DECL_INITIAL (decl) = rinit; |
7887 | |
7888 | if (warn_init_self && TYPE_REF_P (type)) |
7889 | { |
7890 | STRIP_NOPS (rinit); |
7891 | if (rinit == decl) |
7892 | warning_at (DECL_SOURCE_LOCATION (decl), |
7893 | OPT_Winit_self, |
7894 | "reference %qD is initialized with itself" , decl); |
7895 | } |
7896 | } |
7897 | else |
7898 | { |
7899 | int saved_stmts_are_full_exprs_p; |
7900 | |
7901 | /* If we're only initializing a single object, guard the |
7902 | destructors of any temporaries used in its initializer with |
7903 | its destructor. */ |
7904 | if (cleanup) |
7905 | wrap_temporary_cleanups (init, guard: cleanup); |
7906 | |
7907 | gcc_assert (building_stmt_list_p ()); |
7908 | saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p (); |
7909 | current_stmt_tree ()->stmts_are_full_exprs_p = 1; |
7910 | finish_expr_stmt (init); |
7911 | current_stmt_tree ()->stmts_are_full_exprs_p = |
7912 | saved_stmts_are_full_exprs_p; |
7913 | } |
7914 | } |
7915 | |
7916 | /* Set this to 0 so we can tell whether an aggregate which was |
7917 | initialized was ever used. Don't do this if it has a |
7918 | destructor, so we don't complain about the 'resource |
7919 | allocation is initialization' idiom. Now set |
7920 | attribute((unused)) on types so decls of that type will be |
7921 | marked used. (see TREE_USED, above.) */ |
7922 | if (TYPE_NEEDS_CONSTRUCTING (type) |
7923 | && ! already_used |
7924 | && TYPE_HAS_TRIVIAL_DESTRUCTOR (type) |
7925 | && DECL_NAME (decl)) |
7926 | TREE_USED (decl) = 0; |
7927 | else if (already_used) |
7928 | TREE_USED (decl) = 1; |
7929 | |
7930 | if (cleanup) |
7931 | finish_decl_cleanup (decl, cleanup); |
7932 | } |
7933 | |
7934 | /* DECL is a VAR_DECL for a compiler-generated variable with static |
7935 | storage duration (like a virtual table) whose initializer is a |
7936 | compile-time constant. Initialize the variable and provide it to the |
7937 | back end. */ |
7938 | |
7939 | void |
7940 | initialize_artificial_var (tree decl, vec<constructor_elt, va_gc> *v) |
7941 | { |
7942 | tree init; |
7943 | gcc_assert (DECL_ARTIFICIAL (decl)); |
7944 | init = build_constructor (TREE_TYPE (decl), v); |
7945 | gcc_assert (TREE_CODE (init) == CONSTRUCTOR); |
7946 | DECL_INITIAL (decl) = init; |
7947 | DECL_INITIALIZED_P (decl) = 1; |
7948 | /* Mark the decl as constexpr so that we can access its content |
7949 | at compile time. */ |
7950 | DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true; |
7951 | DECL_DECLARED_CONSTEXPR_P (decl) = true; |
7952 | determine_visibility (decl); |
7953 | layout_var_decl (decl); |
7954 | maybe_commonize_var (decl); |
7955 | make_rtl_for_nonlocal_decl (decl, init, /*asmspec=*/NULL); |
7956 | } |
7957 | |
7958 | /* INIT is the initializer for a variable, as represented by the |
7959 | parser. Returns true iff INIT is value-dependent. */ |
7960 | |
7961 | static bool |
7962 | value_dependent_init_p (tree init) |
7963 | { |
7964 | if (TREE_CODE (init) == TREE_LIST) |
7965 | /* A parenthesized initializer, e.g.: int i (3, 2); ? */ |
7966 | return any_value_dependent_elements_p (init); |
7967 | else if (TREE_CODE (init) == CONSTRUCTOR) |
7968 | /* A brace-enclosed initializer, e.g.: int i = { 3 }; ? */ |
7969 | { |
7970 | if (dependent_type_p (TREE_TYPE (init))) |
7971 | return true; |
7972 | |
7973 | vec<constructor_elt, va_gc> *elts; |
7974 | size_t nelts; |
7975 | size_t i; |
7976 | |
7977 | elts = CONSTRUCTOR_ELTS (init); |
7978 | nelts = vec_safe_length (v: elts); |
7979 | for (i = 0; i < nelts; ++i) |
7980 | if (value_dependent_init_p (init: (*elts)[i].value)) |
7981 | return true; |
7982 | } |
7983 | else |
7984 | /* It must be a simple expression, e.g., int i = 3; */ |
7985 | return value_dependent_expression_p (init); |
7986 | |
7987 | return false; |
7988 | } |
7989 | |
7990 | // Returns true if a DECL is VAR_DECL with the concept specifier. |
7991 | static inline bool |
7992 | is_concept_var (tree decl) |
7993 | { |
7994 | return (VAR_P (decl) |
7995 | // Not all variables have DECL_LANG_SPECIFIC. |
7996 | && DECL_LANG_SPECIFIC (decl) |
7997 | && DECL_DECLARED_CONCEPT_P (decl)); |
7998 | } |
7999 | |
8000 | /* A helper function to be called via walk_tree. If any label exists |
8001 | under *TP, it is (going to be) forced. Set has_forced_label_in_static. */ |
8002 | |
8003 | static tree |
8004 | notice_forced_label_r (tree *tp, int *walk_subtrees, void *) |
8005 | { |
8006 | if (TYPE_P (*tp)) |
8007 | *walk_subtrees = 0; |
8008 | if (TREE_CODE (*tp) == LABEL_DECL) |
8009 | cfun->has_forced_label_in_static = 1; |
8010 | return NULL_TREE; |
8011 | } |
8012 | |
8013 | /* Return true if DECL has either a trivial destructor, or for C++20 |
8014 | is constexpr and has a constexpr destructor. */ |
8015 | |
8016 | static bool |
8017 | decl_maybe_constant_destruction (tree decl, tree type) |
8018 | { |
8019 | return (TYPE_HAS_TRIVIAL_DESTRUCTOR (type) |
8020 | || (cxx_dialect >= cxx20 |
8021 | && VAR_P (decl) |
8022 | && DECL_DECLARED_CONSTEXPR_P (decl) |
8023 | && type_has_constexpr_destructor (strip_array_types (type)))); |
8024 | } |
8025 | |
8026 | static tree declare_simd_adjust_this (tree *, int *, void *); |
8027 | |
8028 | /* Helper function of omp_declare_variant_finalize. Finalize one |
8029 | "omp declare variant base" attribute. Return true if it should be |
8030 | removed. */ |
8031 | |
8032 | static bool |
8033 | omp_declare_variant_finalize_one (tree decl, tree attr) |
8034 | { |
8035 | if (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE) |
8036 | { |
8037 | walk_tree (&TREE_VALUE (TREE_VALUE (attr)), declare_simd_adjust_this, |
8038 | DECL_ARGUMENTS (decl), NULL); |
8039 | walk_tree (&TREE_PURPOSE (TREE_VALUE (attr)), declare_simd_adjust_this, |
8040 | DECL_ARGUMENTS (decl), NULL); |
8041 | } |
8042 | |
8043 | tree ctx = TREE_VALUE (TREE_VALUE (attr)); |
8044 | tree simd = omp_get_context_selector (ctx, "construct" , "simd" ); |
8045 | if (simd) |
8046 | { |
8047 | TREE_VALUE (simd) |
8048 | = c_omp_declare_simd_clauses_to_numbers (DECL_ARGUMENTS (decl), |
8049 | TREE_VALUE (simd)); |
8050 | /* FIXME, adjusting simd args unimplemented. */ |
8051 | return true; |
8052 | } |
8053 | |
8054 | tree chain = TREE_CHAIN (TREE_VALUE (attr)); |
8055 | location_t varid_loc |
8056 | = cp_expr_loc_or_input_loc (TREE_PURPOSE (TREE_CHAIN (chain))); |
8057 | location_t match_loc = cp_expr_loc_or_input_loc (TREE_PURPOSE (chain)); |
8058 | cp_id_kind idk = (cp_id_kind) tree_to_uhwi (TREE_VALUE (chain)); |
8059 | tree variant = TREE_PURPOSE (TREE_VALUE (attr)); |
8060 | |
8061 | location_t save_loc = input_location; |
8062 | input_location = varid_loc; |
8063 | |
8064 | releasing_vec args; |
8065 | tree parm = DECL_ARGUMENTS (decl); |
8066 | if (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE) |
8067 | parm = DECL_CHAIN (parm); |
8068 | for (; parm; parm = DECL_CHAIN (parm)) |
8069 | if (type_dependent_expression_p (parm)) |
8070 | vec_safe_push (r&: args, t: build_constructor (TREE_TYPE (parm), NULL)); |
8071 | else if (MAYBE_CLASS_TYPE_P (TREE_TYPE (parm))) |
8072 | vec_safe_push (r&: args, t: build_local_temp (TREE_TYPE (parm))); |
8073 | else |
8074 | vec_safe_push (r&: args, t: build_zero_cst (TREE_TYPE (parm))); |
8075 | |
8076 | bool koenig_p = false; |
8077 | if (idk == CP_ID_KIND_UNQUALIFIED || idk == CP_ID_KIND_TEMPLATE_ID) |
8078 | { |
8079 | if (identifier_p (t: variant) |
8080 | /* In C++20, we may need to perform ADL for a template |
8081 | name. */ |
8082 | || (TREE_CODE (variant) == TEMPLATE_ID_EXPR |
8083 | && identifier_p (TREE_OPERAND (variant, 0)))) |
8084 | { |
8085 | if (!args->is_empty ()) |
8086 | { |
8087 | koenig_p = true; |
8088 | if (!any_type_dependent_arguments_p (args)) |
8089 | variant = perform_koenig_lookup (variant, args, |
8090 | tf_warning_or_error); |
8091 | } |
8092 | else |
8093 | variant = unqualified_fn_lookup_error (variant); |
8094 | } |
8095 | else if (!args->is_empty () && is_overloaded_fn (variant)) |
8096 | { |
8097 | tree fn = get_first_fn (variant); |
8098 | fn = STRIP_TEMPLATE (fn); |
8099 | if (!((TREE_CODE (fn) == USING_DECL && DECL_DEPENDENT_P (fn)) |
8100 | || DECL_FUNCTION_MEMBER_P (fn) |
8101 | || DECL_LOCAL_DECL_P (fn))) |
8102 | { |
8103 | koenig_p = true; |
8104 | if (!any_type_dependent_arguments_p (args)) |
8105 | variant = perform_koenig_lookup (variant, args, |
8106 | tf_warning_or_error); |
8107 | } |
8108 | } |
8109 | } |
8110 | |
8111 | if (idk == CP_ID_KIND_QUALIFIED) |
8112 | variant = finish_call_expr (variant, &args, /*disallow_virtual=*/true, |
8113 | koenig_p, tf_warning_or_error); |
8114 | else |
8115 | variant = finish_call_expr (variant, &args, /*disallow_virtual=*/false, |
8116 | koenig_p, tf_warning_or_error); |
8117 | if (variant == error_mark_node && !processing_template_decl) |
8118 | return true; |
8119 | |
8120 | variant = cp_get_callee_fndecl_nofold (variant); |
8121 | input_location = save_loc; |
8122 | |
8123 | if (variant) |
8124 | { |
8125 | const char *varname = IDENTIFIER_POINTER (DECL_NAME (variant)); |
8126 | if (!comptypes (TREE_TYPE (decl), TREE_TYPE (variant), 0)) |
8127 | { |
8128 | error_at (varid_loc, "variant %qD and base %qD have incompatible " |
8129 | "types" , variant, decl); |
8130 | return true; |
8131 | } |
8132 | if (fndecl_built_in_p (node: variant) |
8133 | && (startswith (str: varname, prefix: "__builtin_" ) |
8134 | || startswith (str: varname, prefix: "__sync_" ) |
8135 | || startswith (str: varname, prefix: "__atomic_" ))) |
8136 | { |
8137 | error_at (varid_loc, "variant %qD is a built-in" , variant); |
8138 | return true; |
8139 | } |
8140 | else |
8141 | { |
8142 | tree construct = omp_get_context_selector (ctx, "construct" , NULL); |
8143 | omp_mark_declare_variant (loc: match_loc, variant, construct); |
8144 | if (!omp_context_selector_matches (ctx)) |
8145 | return true; |
8146 | TREE_PURPOSE (TREE_VALUE (attr)) = variant; |
8147 | } |
8148 | } |
8149 | else if (!processing_template_decl) |
8150 | { |
8151 | error_at (varid_loc, "could not find variant declaration" ); |
8152 | return true; |
8153 | } |
8154 | |
8155 | return false; |
8156 | } |
8157 | |
8158 | /* Helper function, finish up "omp declare variant base" attribute |
8159 | now that there is a DECL. ATTR is the first "omp declare variant base" |
8160 | attribute. */ |
8161 | |
8162 | void |
8163 | omp_declare_variant_finalize (tree decl, tree attr) |
8164 | { |
8165 | size_t attr_len = strlen (s: "omp declare variant base" ); |
8166 | tree *list = &DECL_ATTRIBUTES (decl); |
8167 | bool remove_all = false; |
8168 | location_t match_loc = DECL_SOURCE_LOCATION (decl); |
8169 | if (TREE_CHAIN (TREE_VALUE (attr)) |
8170 | && TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr))) |
8171 | && EXPR_HAS_LOCATION (TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr))))) |
8172 | match_loc = EXPR_LOCATION (TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr)))); |
8173 | if (DECL_CONSTRUCTOR_P (decl)) |
8174 | { |
8175 | error_at (match_loc, "%<declare variant%> on constructor %qD" , decl); |
8176 | remove_all = true; |
8177 | } |
8178 | else if (DECL_DESTRUCTOR_P (decl)) |
8179 | { |
8180 | error_at (match_loc, "%<declare variant%> on destructor %qD" , decl); |
8181 | remove_all = true; |
8182 | } |
8183 | else if (DECL_DEFAULTED_FN (decl)) |
8184 | { |
8185 | error_at (match_loc, "%<declare variant%> on defaulted %qD" , decl); |
8186 | remove_all = true; |
8187 | } |
8188 | else if (DECL_DELETED_FN (decl)) |
8189 | { |
8190 | error_at (match_loc, "%<declare variant%> on deleted %qD" , decl); |
8191 | remove_all = true; |
8192 | } |
8193 | else if (DECL_VIRTUAL_P (decl)) |
8194 | { |
8195 | error_at (match_loc, "%<declare variant%> on virtual %qD" , decl); |
8196 | remove_all = true; |
8197 | } |
8198 | /* This loop is like private_lookup_attribute, except that it works |
8199 | with tree * rather than tree, as we might want to remove the |
8200 | attributes that are diagnosed as errorneous. */ |
8201 | while (*list) |
8202 | { |
8203 | tree attr = get_attribute_name (*list); |
8204 | size_t ident_len = IDENTIFIER_LENGTH (attr); |
8205 | if (cmp_attribs (attr1: "omp declare variant base" , attr1_len: attr_len, |
8206 | IDENTIFIER_POINTER (attr), attr2_len: ident_len)) |
8207 | { |
8208 | if (remove_all || omp_declare_variant_finalize_one (decl, attr: *list)) |
8209 | { |
8210 | *list = TREE_CHAIN (*list); |
8211 | continue; |
8212 | } |
8213 | } |
8214 | list = &TREE_CHAIN (*list); |
8215 | } |
8216 | } |
8217 | |
8218 | static void cp_maybe_mangle_decomp (tree, cp_decomp *); |
8219 | |
8220 | /* Finish processing of a declaration; |
8221 | install its line number and initial value. |
8222 | If the length of an array type is not known before, |
8223 | it must be determined now, from the initial value, or it is an error. |
8224 | |
8225 | INIT is the initializer (if any) for DECL. If INIT_CONST_EXPR_P is |
8226 | true, then INIT is an integral constant expression. |
8227 | |
8228 | FLAGS is LOOKUP_ONLYCONVERTING if the = init syntax was used, else 0 |
8229 | if the (init) syntax was used. |
8230 | |
8231 | DECOMP is first identifier's DECL and identifier count in a structured |
8232 | bindings, nullptr if not a structured binding. */ |
8233 | |
8234 | void |
8235 | cp_finish_decl (tree decl, tree init, bool init_const_expr_p, |
8236 | tree asmspec_tree, int flags, cp_decomp *decomp) |
8237 | { |
8238 | vec<tree, va_gc> *cleanups = NULL; |
8239 | const char *asmspec = NULL; |
8240 | int was_readonly = 0; |
8241 | bool var_definition_p = false; |
8242 | tree auto_node; |
8243 | |
8244 | if (decl == error_mark_node) |
8245 | return; |
8246 | else if (! decl) |
8247 | { |
8248 | if (init) |
8249 | error ("assignment (not initialization) in declaration" ); |
8250 | return; |
8251 | } |
8252 | |
8253 | gcc_assert (TREE_CODE (decl) != RESULT_DECL); |
8254 | /* Parameters are handled by store_parm_decls, not cp_finish_decl. */ |
8255 | gcc_assert (TREE_CODE (decl) != PARM_DECL); |
8256 | |
8257 | tree type = TREE_TYPE (decl); |
8258 | if (type == error_mark_node) |
8259 | return; |
8260 | |
8261 | if (VAR_P (decl) && is_copy_initialization (init)) |
8262 | flags |= LOOKUP_ONLYCONVERTING; |
8263 | |
8264 | /* Warn about register storage specifiers except when in GNU global |
8265 | or local register variable extension. */ |
8266 | if (VAR_P (decl) && DECL_REGISTER (decl) && asmspec_tree == NULL_TREE) |
8267 | { |
8268 | if (cxx_dialect >= cxx17) |
8269 | pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wregister, |
8270 | "ISO C++17 does not allow %<register%> storage " |
8271 | "class specifier" ); |
8272 | else |
8273 | warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wregister, |
8274 | "%<register%> storage class specifier used" ); |
8275 | } |
8276 | |
8277 | /* If a name was specified, get the string. */ |
8278 | if (at_namespace_scope_p ()) |
8279 | asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree); |
8280 | if (asmspec_tree && asmspec_tree != error_mark_node) |
8281 | asmspec = TREE_STRING_POINTER (asmspec_tree); |
8282 | |
8283 | bool in_class_decl |
8284 | = (current_class_type |
8285 | && CP_DECL_CONTEXT (decl) == current_class_type |
8286 | && TYPE_BEING_DEFINED (current_class_type) |
8287 | && !CLASSTYPE_TEMPLATE_INSTANTIATION (current_class_type)); |
8288 | |
8289 | if (in_class_decl |
8290 | && (DECL_INITIAL (decl) || init)) |
8291 | DECL_INITIALIZED_IN_CLASS_P (decl) = 1; |
8292 | |
8293 | if (VAR_P (decl) |
8294 | && (auto_node = type_uses_auto (type))) |
8295 | { |
8296 | tree d_init; |
8297 | if (init == NULL_TREE) |
8298 | { |
8299 | if (DECL_LANG_SPECIFIC (decl) |
8300 | && DECL_TEMPLATE_INSTANTIATION (decl) |
8301 | && !DECL_TEMPLATE_INSTANTIATED (decl)) |
8302 | { |
8303 | /* init is null because we're deferring instantiating the |
8304 | initializer until we need it. Well, we need it now. */ |
8305 | instantiate_decl (decl, /*defer_ok*/true, /*expl*/false); |
8306 | return; |
8307 | } |
8308 | |
8309 | if (CLASS_PLACEHOLDER_TEMPLATE (auto_node)) |
8310 | /* Class deduction with no initializer is OK. */; |
8311 | else |
8312 | { |
8313 | /* Ordinary auto deduction without an initializer, a situation |
8314 | which grokdeclarator already detects and rejects for the most |
8315 | part. But we can still get here if we're instantiating a |
8316 | variable template before we've fully parsed (and attached) its |
8317 | initializer, e.g. template<class> auto x = x<int>; */ |
8318 | error_at (DECL_SOURCE_LOCATION (decl), |
8319 | "declaration of %q#D has no initializer" , decl); |
8320 | TREE_TYPE (decl) = error_mark_node; |
8321 | return; |
8322 | } |
8323 | } |
8324 | d_init = init; |
8325 | if (d_init) |
8326 | { |
8327 | if (TREE_CODE (d_init) == TREE_LIST |
8328 | && !CLASS_PLACEHOLDER_TEMPLATE (auto_node)) |
8329 | d_init = build_x_compound_expr_from_list (d_init, ELK_INIT, |
8330 | tf_warning_or_error); |
8331 | d_init = resolve_nondeduced_context (d_init, tf_warning_or_error); |
8332 | /* Force auto deduction now. Use tf_none to avoid redundant warnings |
8333 | on deprecated-14.C. */ |
8334 | mark_single_function (d_init, tf_none); |
8335 | } |
8336 | enum auto_deduction_context adc = adc_variable_type; |
8337 | if (DECL_DECOMPOSITION_P (decl)) |
8338 | adc = adc_decomp_type; |
8339 | tree outer_targs = NULL_TREE; |
8340 | if (PLACEHOLDER_TYPE_CONSTRAINTS_INFO (auto_node) |
8341 | && DECL_LANG_SPECIFIC (decl) |
8342 | && DECL_TEMPLATE_INFO (decl) |
8343 | && !DECL_FUNCTION_SCOPE_P (decl)) |
8344 | /* The outer template arguments might be needed for satisfaction. |
8345 | (For function scope variables, do_auto_deduction will obtain the |
8346 | outer template arguments from current_function_decl.) */ |
8347 | outer_targs = DECL_TI_ARGS (decl); |
8348 | type = TREE_TYPE (decl) = do_auto_deduction (type, d_init, auto_node, |
8349 | tf_warning_or_error, adc, |
8350 | outer_targs, flags); |
8351 | if (type == error_mark_node) |
8352 | return; |
8353 | if (TREE_CODE (type) == FUNCTION_TYPE) |
8354 | { |
8355 | error ("initializer for %<decltype(auto) %D%> has function type; " |
8356 | "did you forget the %<()%>?" , decl); |
8357 | TREE_TYPE (decl) = error_mark_node; |
8358 | return; |
8359 | } |
8360 | /* As in start_decl_1, complete so TREE_READONLY is set properly. */ |
8361 | if (!processing_template_decl |
8362 | && !type_uses_auto (type) |
8363 | && !COMPLETE_TYPE_P (complete_type (type))) |
8364 | { |
8365 | error_at (location_of (decl), |
8366 | "deduced type %qT for %qD is incomplete" , type, decl); |
8367 | cxx_incomplete_type_inform (type); |
8368 | TREE_TYPE (decl) = error_mark_node; |
8369 | return; |
8370 | } |
8371 | cp_apply_type_quals_to_decl (cp_type_quals (type), decl); |
8372 | |
8373 | /* Update the type of the corresponding TEMPLATE_DECL to match. */ |
8374 | if (DECL_LANG_SPECIFIC (decl) |
8375 | && DECL_TEMPLATE_INFO (decl) |
8376 | && DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (decl)) == decl) |
8377 | TREE_TYPE (DECL_TI_TEMPLATE (decl)) = type; |
8378 | } |
8379 | |
8380 | if (ensure_literal_type_for_constexpr_object (decl) == error_mark_node) |
8381 | { |
8382 | DECL_DECLARED_CONSTEXPR_P (decl) = 0; |
8383 | if (VAR_P (decl) && DECL_CLASS_SCOPE_P (decl)) |
8384 | { |
8385 | init = NULL_TREE; |
8386 | DECL_EXTERNAL (decl) = 1; |
8387 | } |
8388 | } |
8389 | |
8390 | if (VAR_P (decl) |
8391 | && DECL_CLASS_SCOPE_P (decl) |
8392 | && verify_type_context (DECL_SOURCE_LOCATION (decl), |
8393 | TCTX_STATIC_STORAGE, type) |
8394 | && DECL_INITIALIZED_IN_CLASS_P (decl)) |
8395 | check_static_variable_definition (decl, type); |
8396 | |
8397 | if (!processing_template_decl && VAR_P (decl) && is_global_var (t: decl)) |
8398 | { |
8399 | type_context_kind context = (DECL_THREAD_LOCAL_P (decl) |
8400 | ? TCTX_THREAD_STORAGE |
8401 | : TCTX_STATIC_STORAGE); |
8402 | verify_type_context (input_location, context, TREE_TYPE (decl)); |
8403 | } |
8404 | |
8405 | if (init && TREE_CODE (decl) == FUNCTION_DECL) |
8406 | { |
8407 | tree clone; |
8408 | if (init == ridpointers[(int)RID_DELETE]) |
8409 | { |
8410 | /* FIXME check this is 1st decl. */ |
8411 | DECL_DELETED_FN (decl) = 1; |
8412 | DECL_DECLARED_INLINE_P (decl) = 1; |
8413 | DECL_INITIAL (decl) = error_mark_node; |
8414 | FOR_EACH_CLONE (clone, decl) |
8415 | { |
8416 | DECL_DELETED_FN (clone) = 1; |
8417 | DECL_DECLARED_INLINE_P (clone) = 1; |
8418 | DECL_INITIAL (clone) = error_mark_node; |
8419 | } |
8420 | init = NULL_TREE; |
8421 | } |
8422 | else if (init == ridpointers[(int)RID_DEFAULT]) |
8423 | { |
8424 | if (defaultable_fn_check (decl)) |
8425 | DECL_DEFAULTED_FN (decl) = 1; |
8426 | else |
8427 | DECL_INITIAL (decl) = NULL_TREE; |
8428 | } |
8429 | } |
8430 | |
8431 | if (init && VAR_P (decl)) |
8432 | { |
8433 | DECL_NONTRIVIALLY_INITIALIZED_P (decl) = 1; |
8434 | /* If DECL is a reference, then we want to know whether init is a |
8435 | reference constant; init_const_expr_p as passed tells us whether |
8436 | it's an rvalue constant. */ |
8437 | if (TYPE_REF_P (type)) |
8438 | init_const_expr_p = potential_constant_expression (init); |
8439 | if (init_const_expr_p) |
8440 | { |
8441 | /* Set these flags now for templates. We'll update the flags in |
8442 | store_init_value for instantiations. */ |
8443 | DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1; |
8444 | if (decl_maybe_constant_var_p (decl) |
8445 | /* FIXME setting TREE_CONSTANT on refs breaks the back end. */ |
8446 | && !TYPE_REF_P (type)) |
8447 | TREE_CONSTANT (decl) = true; |
8448 | } |
8449 | /* This is handled mostly by gimplify.cc, but we have to deal with |
8450 | not warning about int x = x; as it is a GCC extension to turn off |
8451 | this warning but only if warn_init_self is zero. */ |
8452 | if (!DECL_EXTERNAL (decl) |
8453 | && !TREE_STATIC (decl) |
8454 | && decl == tree_strip_any_location_wrapper (exp: init) |
8455 | && !warning_enabled_at (DECL_SOURCE_LOCATION (decl), opt: OPT_Winit_self)) |
8456 | suppress_warning (decl, OPT_Winit_self); |
8457 | } |
8458 | else if (VAR_P (decl) |
8459 | && COMPLETE_TYPE_P (type) |
8460 | && !TYPE_REF_P (type) |
8461 | && !dependent_type_p (type) |
8462 | && is_really_empty_class (type, /*ignore_vptr*/false)) |
8463 | /* We have no initializer but there's nothing to initialize anyway. |
8464 | Treat DECL as constant due to c++/109876. */ |
8465 | TREE_CONSTANT (decl) = true; |
8466 | |
8467 | if (flag_openmp |
8468 | && TREE_CODE (decl) == FUNCTION_DECL |
8469 | /* #pragma omp declare variant on methods handled in finish_struct |
8470 | instead. */ |
8471 | && (!DECL_NONSTATIC_MEMBER_FUNCTION_P (decl) |
8472 | || COMPLETE_TYPE_P (DECL_CONTEXT (decl)))) |
8473 | if (tree attr = lookup_attribute (attr_name: "omp declare variant base" , |
8474 | DECL_ATTRIBUTES (decl))) |
8475 | omp_declare_variant_finalize (decl, attr); |
8476 | |
8477 | if (processing_template_decl) |
8478 | { |
8479 | bool type_dependent_p; |
8480 | |
8481 | /* Add this declaration to the statement-tree. */ |
8482 | if (at_function_scope_p ()) |
8483 | add_decl_expr (decl); |
8484 | |
8485 | type_dependent_p = dependent_type_p (type); |
8486 | |
8487 | if (check_for_bare_parameter_packs (init)) |
8488 | { |
8489 | init = NULL_TREE; |
8490 | DECL_INITIAL (decl) = NULL_TREE; |
8491 | } |
8492 | |
8493 | /* Generally, initializers in templates are expanded when the |
8494 | template is instantiated. But, if DECL is a variable constant |
8495 | then it can be used in future constant expressions, so its value |
8496 | must be available. */ |
8497 | |
8498 | bool dep_init = false; |
8499 | |
8500 | if (!VAR_P (decl) || type_dependent_p) |
8501 | /* We can't do anything if the decl has dependent type. */; |
8502 | else if (!init && is_concept_var (decl)) |
8503 | { |
8504 | error ("variable concept has no initializer" ); |
8505 | init = boolean_true_node; |
8506 | } |
8507 | else if (init |
8508 | && (init_const_expr_p || DECL_DECLARED_CONSTEXPR_P (decl)) |
8509 | && !TYPE_REF_P (type) |
8510 | && decl_maybe_constant_var_p (decl) |
8511 | && !(dep_init = value_dependent_init_p (init))) |
8512 | { |
8513 | /* This variable seems to be a non-dependent constant, so process |
8514 | its initializer. If check_initializer returns non-null the |
8515 | initialization wasn't constant after all. */ |
8516 | tree init_code; |
8517 | cleanups = make_tree_vector (); |
8518 | init_code = check_initializer (decl, init, flags, cleanups: &cleanups); |
8519 | if (init_code == NULL_TREE) |
8520 | init = NULL_TREE; |
8521 | release_tree_vector (cleanups); |
8522 | } |
8523 | else |
8524 | { |
8525 | gcc_assert (!DECL_PRETTY_FUNCTION_P (decl)); |
8526 | /* Try to deduce array size. */ |
8527 | maybe_deduce_size_from_array_init (decl, init); |
8528 | /* And complain about multiple initializers. */ |
8529 | if (init && TREE_CODE (init) == TREE_LIST && TREE_CHAIN (init) |
8530 | && !MAYBE_CLASS_TYPE_P (type)) |
8531 | init = build_x_compound_expr_from_list (init, ELK_INIT, |
8532 | tf_warning_or_error); |
8533 | } |
8534 | |
8535 | if (init) |
8536 | DECL_INITIAL (decl) = init; |
8537 | |
8538 | if (dep_init) |
8539 | { |
8540 | retrofit_lang_decl (decl); |
8541 | SET_DECL_DEPENDENT_INIT_P (decl, true); |
8542 | } |
8543 | |
8544 | if (VAR_P (decl) && DECL_REGISTER (decl) && asmspec) |
8545 | { |
8546 | set_user_assembler_name (decl, asmspec); |
8547 | DECL_HARD_REGISTER (decl) = 1; |
8548 | } |
8549 | return; |
8550 | } |
8551 | |
8552 | /* Just store non-static data member initializers for later. */ |
8553 | if (init && TREE_CODE (decl) == FIELD_DECL) |
8554 | DECL_INITIAL (decl) = init; |
8555 | |
8556 | /* Take care of TYPE_DECLs up front. */ |
8557 | if (TREE_CODE (decl) == TYPE_DECL) |
8558 | { |
8559 | if (type != error_mark_node |
8560 | && MAYBE_CLASS_TYPE_P (type) && DECL_NAME (decl)) |
8561 | { |
8562 | if (TREE_TYPE (DECL_NAME (decl)) && TREE_TYPE (decl) != type) |
8563 | warning (0, "shadowing previous type declaration of %q#D" , decl); |
8564 | set_identifier_type_value (DECL_NAME (decl), decl); |
8565 | } |
8566 | |
8567 | /* If we have installed this as the canonical typedef for this |
8568 | type, and that type has not been defined yet, delay emitting |
8569 | the debug information for it, as we will emit it later. */ |
8570 | if (TYPE_MAIN_DECL (TREE_TYPE (decl)) == decl |
8571 | && !COMPLETE_TYPE_P (TREE_TYPE (decl))) |
8572 | TYPE_DECL_SUPPRESS_DEBUG (decl) = 1; |
8573 | |
8574 | rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), |
8575 | at_eof); |
8576 | return; |
8577 | } |
8578 | |
8579 | /* A reference will be modified here, as it is initialized. */ |
8580 | if (! DECL_EXTERNAL (decl) |
8581 | && TREE_READONLY (decl) |
8582 | && TYPE_REF_P (type)) |
8583 | { |
8584 | was_readonly = 1; |
8585 | TREE_READONLY (decl) = 0; |
8586 | } |
8587 | |
8588 | /* This needs to happen before extend_ref_init_temps. */ |
8589 | if (VAR_OR_FUNCTION_DECL_P (decl)) |
8590 | { |
8591 | if (VAR_P (decl)) |
8592 | maybe_commonize_var (decl); |
8593 | determine_visibility (decl); |
8594 | } |
8595 | |
8596 | if (VAR_P (decl)) |
8597 | { |
8598 | duration_kind dk = decl_storage_duration (decl); |
8599 | /* [dcl.constinit]/1 "The constinit specifier shall be applied |
8600 | only to a declaration of a variable with static or thread storage |
8601 | duration." */ |
8602 | if (DECL_DECLARED_CONSTINIT_P (decl) |
8603 | && !(dk == dk_thread || dk == dk_static)) |
8604 | { |
8605 | error_at (DECL_SOURCE_LOCATION (decl), |
8606 | "%<constinit%> can only be applied to a variable with " |
8607 | "static or thread storage duration" ); |
8608 | return; |
8609 | } |
8610 | |
8611 | if (decomp) |
8612 | cp_maybe_mangle_decomp (decl, decomp); |
8613 | |
8614 | /* If this is a local variable that will need a mangled name, |
8615 | register it now. We must do this before processing the |
8616 | initializer for the variable, since the initialization might |
8617 | require a guard variable, and since the mangled name of the |
8618 | guard variable will depend on the mangled name of this |
8619 | variable. */ |
8620 | if (DECL_FUNCTION_SCOPE_P (decl) |
8621 | && TREE_STATIC (decl) |
8622 | && !DECL_ARTIFICIAL (decl)) |
8623 | { |
8624 | /* The variable holding an anonymous union will have had its |
8625 | discriminator set in finish_anon_union, after which it's |
8626 | NAME will have been cleared. */ |
8627 | if (DECL_NAME (decl)) |
8628 | determine_local_discriminator (decl); |
8629 | /* Normally has_forced_label_in_static is set during GIMPLE |
8630 | lowering, but [cd]tors are never actually compiled directly. |
8631 | We need to set this early so we can deal with the label |
8632 | address extension. */ |
8633 | if ((DECL_CONSTRUCTOR_P (current_function_decl) |
8634 | || DECL_DESTRUCTOR_P (current_function_decl)) |
8635 | && init) |
8636 | { |
8637 | walk_tree (&init, notice_forced_label_r, NULL, NULL); |
8638 | add_local_decl (cfun, d: decl); |
8639 | } |
8640 | /* And make sure it's in the symbol table for |
8641 | c_parse_final_cleanups to find. */ |
8642 | varpool_node::get_create (decl); |
8643 | } |
8644 | |
8645 | /* Convert the initializer to the type of DECL, if we have not |
8646 | already initialized DECL. */ |
8647 | if (!DECL_INITIALIZED_P (decl) |
8648 | /* If !DECL_EXTERNAL then DECL is being defined. In the |
8649 | case of a static data member initialized inside the |
8650 | class-specifier, there can be an initializer even if DECL |
8651 | is *not* defined. */ |
8652 | && (!DECL_EXTERNAL (decl) || init)) |
8653 | { |
8654 | cleanups = make_tree_vector (); |
8655 | init = check_initializer (decl, init, flags, cleanups: &cleanups); |
8656 | |
8657 | /* Handle: |
8658 | |
8659 | [dcl.init] |
8660 | |
8661 | The memory occupied by any object of static storage |
8662 | duration is zero-initialized at program startup before |
8663 | any other initialization takes place. |
8664 | |
8665 | We cannot create an appropriate initializer until after |
8666 | the type of DECL is finalized. If DECL_INITIAL is set, |
8667 | then the DECL is statically initialized, and any |
8668 | necessary zero-initialization has already been performed. */ |
8669 | if (TREE_STATIC (decl) && !DECL_INITIAL (decl)) |
8670 | DECL_INITIAL (decl) = build_zero_init (TREE_TYPE (decl), |
8671 | /*nelts=*/NULL_TREE, |
8672 | /*static_storage_p=*/true); |
8673 | /* Remember that the initialization for this variable has |
8674 | taken place. */ |
8675 | DECL_INITIALIZED_P (decl) = 1; |
8676 | /* This declaration is the definition of this variable, |
8677 | unless we are initializing a static data member within |
8678 | the class specifier. */ |
8679 | if (!DECL_EXTERNAL (decl)) |
8680 | var_definition_p = true; |
8681 | } |
8682 | /* If the variable has an array type, lay out the type, even if |
8683 | there is no initializer. It is valid to index through the |
8684 | array, and we must get TYPE_ALIGN set correctly on the array |
8685 | type. */ |
8686 | else if (TREE_CODE (type) == ARRAY_TYPE) |
8687 | layout_type (type); |
8688 | |
8689 | if (TREE_STATIC (decl) |
8690 | && !at_function_scope_p () |
8691 | && current_function_decl == NULL) |
8692 | /* So decl is a global variable or a static member of a |
8693 | non local class. Record the types it uses |
8694 | so that we can decide later to emit debug info for them. */ |
8695 | record_types_used_by_current_var_decl (decl); |
8696 | } |
8697 | |
8698 | /* Add this declaration to the statement-tree. This needs to happen |
8699 | after the call to check_initializer so that the DECL_EXPR for a |
8700 | reference temp is added before the DECL_EXPR for the reference itself. */ |
8701 | if (DECL_FUNCTION_SCOPE_P (decl)) |
8702 | { |
8703 | /* If we're building a variable sized type, and we might be |
8704 | reachable other than via the top of the current binding |
8705 | level, then create a new BIND_EXPR so that we deallocate |
8706 | the object at the right time. */ |
8707 | if (VAR_P (decl) |
8708 | && DECL_SIZE (decl) |
8709 | && !TREE_CONSTANT (DECL_SIZE (decl)) |
8710 | && STATEMENT_LIST_HAS_LABEL (cur_stmt_list)) |
8711 | { |
8712 | tree bind; |
8713 | bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL); |
8714 | TREE_SIDE_EFFECTS (bind) = 1; |
8715 | add_stmt (bind); |
8716 | BIND_EXPR_BODY (bind) = push_stmt_list (); |
8717 | } |
8718 | add_decl_expr (decl); |
8719 | } |
8720 | |
8721 | /* Let the middle end know about variables and functions -- but not |
8722 | static data members in uninstantiated class templates. */ |
8723 | if (VAR_OR_FUNCTION_DECL_P (decl)) |
8724 | { |
8725 | if (VAR_P (decl)) |
8726 | { |
8727 | layout_var_decl (decl); |
8728 | if (!flag_weak) |
8729 | /* Check again now that we have an initializer. */ |
8730 | maybe_commonize_var (decl); |
8731 | /* A class-scope constexpr variable with an out-of-class declaration. |
8732 | C++17 makes them implicitly inline, but still force it out. */ |
8733 | if (DECL_INLINE_VAR_P (decl) |
8734 | && !DECL_VAR_DECLARED_INLINE_P (decl) |
8735 | && !DECL_TEMPLATE_INSTANTIATION (decl) |
8736 | && !in_class_decl) |
8737 | mark_needed (decl); |
8738 | } |
8739 | |
8740 | if (var_definition_p |
8741 | /* With -fmerge-all-constants, gimplify_init_constructor |
8742 | might add TREE_STATIC to aggregate variables. */ |
8743 | && (TREE_STATIC (decl) |
8744 | || (flag_merge_constants >= 2 |
8745 | && AGGREGATE_TYPE_P (type)))) |
8746 | { |
8747 | /* If a TREE_READONLY variable needs initialization |
8748 | at runtime, it is no longer readonly and we need to |
8749 | avoid MEM_READONLY_P being set on RTL created for it. */ |
8750 | if (init) |
8751 | { |
8752 | if (TREE_READONLY (decl)) |
8753 | TREE_READONLY (decl) = 0; |
8754 | was_readonly = 0; |
8755 | } |
8756 | else if (was_readonly) |
8757 | TREE_READONLY (decl) = 1; |
8758 | |
8759 | /* Likewise if it needs destruction. */ |
8760 | if (!decl_maybe_constant_destruction (decl, type)) |
8761 | TREE_READONLY (decl) = 0; |
8762 | } |
8763 | else if (VAR_P (decl) |
8764 | && CP_DECL_THREAD_LOCAL_P (decl) |
8765 | && (!DECL_EXTERNAL (decl) || flag_extern_tls_init) |
8766 | && (was_readonly || TREE_READONLY (decl)) |
8767 | && var_needs_tls_wrapper (decl)) |
8768 | { |
8769 | /* TLS variables need dynamic initialization by the TLS wrapper |
8770 | function, we don't want to hoist accesses to it before the |
8771 | wrapper. */ |
8772 | was_readonly = 0; |
8773 | TREE_READONLY (decl) = 0; |
8774 | } |
8775 | |
8776 | make_rtl_for_nonlocal_decl (decl, init, asmspec); |
8777 | |
8778 | /* Check for abstractness of the type. */ |
8779 | if (var_definition_p) |
8780 | abstract_virtuals_error (decl, type); |
8781 | |
8782 | if (TREE_TYPE (decl) == error_mark_node) |
8783 | /* No initialization required. */ |
8784 | ; |
8785 | else if (TREE_CODE (decl) == FUNCTION_DECL) |
8786 | { |
8787 | if (init) |
8788 | { |
8789 | if (init == ridpointers[(int)RID_DEFAULT]) |
8790 | { |
8791 | /* An out-of-class default definition is defined at |
8792 | the point where it is explicitly defaulted. */ |
8793 | if (DECL_DELETED_FN (decl)) |
8794 | maybe_explain_implicit_delete (decl); |
8795 | else if (DECL_INITIAL (decl) == error_mark_node) |
8796 | synthesize_method (decl); |
8797 | } |
8798 | else |
8799 | error_at (cp_expr_loc_or_loc (t: init, |
8800 | DECL_SOURCE_LOCATION (decl)), |
8801 | "function %q#D is initialized like a variable" , |
8802 | decl); |
8803 | } |
8804 | /* else no initialization required. */ |
8805 | } |
8806 | else if (DECL_EXTERNAL (decl) |
8807 | && ! (DECL_LANG_SPECIFIC (decl) |
8808 | && DECL_NOT_REALLY_EXTERN (decl))) |
8809 | { |
8810 | /* check_initializer will have done any constant initialization. */ |
8811 | } |
8812 | /* A variable definition. */ |
8813 | else if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl)) |
8814 | /* Initialize the local variable. */ |
8815 | initialize_local_var (decl, init); |
8816 | |
8817 | /* If a variable is defined, and then a subsequent |
8818 | definition with external linkage is encountered, we will |
8819 | get here twice for the same variable. We want to avoid |
8820 | calling expand_static_init more than once. For variables |
8821 | that are not static data members, we can call |
8822 | expand_static_init only when we actually process the |
8823 | initializer. It is not legal to redeclare a static data |
8824 | member, so this issue does not arise in that case. */ |
8825 | else if (var_definition_p && TREE_STATIC (decl)) |
8826 | expand_static_init (decl, init); |
8827 | } |
8828 | |
8829 | /* If a CLEANUP_STMT was created to destroy a temporary bound to a |
8830 | reference, insert it in the statement-tree now. */ |
8831 | if (cleanups) |
8832 | { |
8833 | for (tree t : *cleanups) |
8834 | { |
8835 | push_cleanup (NULL_TREE, t, false); |
8836 | /* As in initialize_local_var. */ |
8837 | wrap_temporary_cleanups (init, guard: t); |
8838 | } |
8839 | release_tree_vector (cleanups); |
8840 | } |
8841 | |
8842 | if (was_readonly) |
8843 | TREE_READONLY (decl) = 1; |
8844 | |
8845 | if (flag_openmp |
8846 | && VAR_P (decl) |
8847 | && lookup_attribute (attr_name: "omp declare target implicit" , |
8848 | DECL_ATTRIBUTES (decl))) |
8849 | { |
8850 | DECL_ATTRIBUTES (decl) |
8851 | = remove_attribute ("omp declare target implicit" , |
8852 | DECL_ATTRIBUTES (decl)); |
8853 | complete_type (TREE_TYPE (decl)); |
8854 | if (!omp_mappable_type (TREE_TYPE (decl))) |
8855 | { |
8856 | error ("%q+D in declare target directive does not have mappable" |
8857 | " type" , decl); |
8858 | if (TREE_TYPE (decl) != error_mark_node |
8859 | && !COMPLETE_TYPE_P (TREE_TYPE (decl))) |
8860 | cxx_incomplete_type_inform (TREE_TYPE (decl)); |
8861 | } |
8862 | else if (!lookup_attribute (attr_name: "omp declare target" , |
8863 | DECL_ATTRIBUTES (decl)) |
8864 | && !lookup_attribute (attr_name: "omp declare target link" , |
8865 | DECL_ATTRIBUTES (decl))) |
8866 | { |
8867 | DECL_ATTRIBUTES (decl) |
8868 | = tree_cons (get_identifier ("omp declare target" ), |
8869 | NULL_TREE, DECL_ATTRIBUTES (decl)); |
8870 | symtab_node *node = symtab_node::get (decl); |
8871 | if (node != NULL) |
8872 | { |
8873 | node->offloadable = 1; |
8874 | if (ENABLE_OFFLOADING) |
8875 | { |
8876 | g->have_offload = true; |
8877 | if (is_a <varpool_node *> (p: node)) |
8878 | vec_safe_push (v&: offload_vars, obj: decl); |
8879 | } |
8880 | } |
8881 | } |
8882 | } |
8883 | |
8884 | /* This is the last point we can lower alignment so give the target the |
8885 | chance to do so. */ |
8886 | if (VAR_P (decl) |
8887 | && !is_global_var (t: decl) |
8888 | && !DECL_HARD_REGISTER (decl)) |
8889 | targetm.lower_local_decl_alignment (decl); |
8890 | |
8891 | invoke_plugin_callbacks (event: PLUGIN_FINISH_DECL, gcc_data: decl); |
8892 | } |
8893 | |
8894 | /* For class TYPE return itself or some its bases that contain |
8895 | any direct non-static data members. Return error_mark_node if an |
8896 | error has been diagnosed. */ |
8897 | |
8898 | static tree |
8899 | find_decomp_class_base (location_t loc, tree type, tree ret) |
8900 | { |
8901 | bool member_seen = false; |
8902 | for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field)) |
8903 | if (TREE_CODE (field) != FIELD_DECL |
8904 | || DECL_ARTIFICIAL (field) |
8905 | || DECL_UNNAMED_BIT_FIELD (field)) |
8906 | continue; |
8907 | else if (ret) |
8908 | return type; |
8909 | else if (ANON_AGGR_TYPE_P (TREE_TYPE (field))) |
8910 | { |
8911 | if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE) |
8912 | error_at (loc, "cannot decompose class type %qT because it has an " |
8913 | "anonymous struct member" , type); |
8914 | else |
8915 | error_at (loc, "cannot decompose class type %qT because it has an " |
8916 | "anonymous union member" , type); |
8917 | inform (DECL_SOURCE_LOCATION (field), "declared here" ); |
8918 | return error_mark_node; |
8919 | } |
8920 | else if (!accessible_p (type, field, true)) |
8921 | { |
8922 | error_at (loc, "cannot decompose inaccessible member %qD of %qT" , |
8923 | field, type); |
8924 | inform (DECL_SOURCE_LOCATION (field), |
8925 | TREE_PRIVATE (field) |
8926 | ? G_("declared private here" ) |
8927 | : G_("declared protected here" )); |
8928 | return error_mark_node; |
8929 | } |
8930 | else |
8931 | member_seen = true; |
8932 | |
8933 | tree base_binfo, binfo; |
8934 | tree orig_ret = ret; |
8935 | int i; |
8936 | if (member_seen) |
8937 | ret = type; |
8938 | for (binfo = TYPE_BINFO (type), i = 0; |
8939 | BINFO_BASE_ITERATE (binfo, i, base_binfo); i++) |
8940 | { |
8941 | tree t = find_decomp_class_base (loc, TREE_TYPE (base_binfo), ret); |
8942 | if (t == error_mark_node) |
8943 | return error_mark_node; |
8944 | if (t != NULL_TREE && t != ret) |
8945 | { |
8946 | if (ret == type) |
8947 | { |
8948 | error_at (loc, "cannot decompose class type %qT: both it and " |
8949 | "its base class %qT have non-static data members" , |
8950 | type, t); |
8951 | return error_mark_node; |
8952 | } |
8953 | else if (orig_ret != NULL_TREE) |
8954 | return t; |
8955 | else if (ret != NULL_TREE) |
8956 | { |
8957 | error_at (loc, "cannot decompose class type %qT: its base " |
8958 | "classes %qT and %qT have non-static data " |
8959 | "members" , type, ret, t); |
8960 | return error_mark_node; |
8961 | } |
8962 | else |
8963 | ret = t; |
8964 | } |
8965 | } |
8966 | return ret; |
8967 | } |
8968 | |
8969 | /* Return std::tuple_size<TYPE>::value. */ |
8970 | |
8971 | static tree |
8972 | get_tuple_size (tree type) |
8973 | { |
8974 | tree args = make_tree_vec (1); |
8975 | TREE_VEC_ELT (args, 0) = type; |
8976 | tree inst = lookup_template_class (tuple_size_identifier, args, |
8977 | /*in_decl*/NULL_TREE, |
8978 | /*context*/std_node, |
8979 | /*entering_scope*/false, tf_none); |
8980 | inst = complete_type (inst); |
8981 | if (inst == error_mark_node |
8982 | || !COMPLETE_TYPE_P (inst) |
8983 | || !CLASS_TYPE_P (type)) |
8984 | return NULL_TREE; |
8985 | tree val = lookup_qualified_name (scope: inst, value_identifier, |
8986 | LOOK_want::NORMAL, /*complain*/false); |
8987 | if (val == error_mark_node) |
8988 | return NULL_TREE; |
8989 | if (VAR_P (val) || TREE_CODE (val) == CONST_DECL) |
8990 | val = maybe_constant_value (val); |
8991 | if (TREE_CODE (val) == INTEGER_CST) |
8992 | return val; |
8993 | else |
8994 | return error_mark_node; |
8995 | } |
8996 | |
8997 | /* Return std::tuple_element<I,TYPE>::type. */ |
8998 | |
8999 | static tree |
9000 | get_tuple_element_type (tree type, unsigned i) |
9001 | { |
9002 | tree args = make_tree_vec (2); |
9003 | TREE_VEC_ELT (args, 0) = build_int_cst (integer_type_node, i); |
9004 | TREE_VEC_ELT (args, 1) = type; |
9005 | tree inst = lookup_template_class (tuple_element_identifier, args, |
9006 | /*in_decl*/NULL_TREE, |
9007 | /*context*/std_node, |
9008 | /*entering_scope*/false, |
9009 | tf_warning_or_error); |
9010 | return make_typename_type (context: inst, type_identifier, |
9011 | tag_type: none_type, complain: tf_warning_or_error); |
9012 | } |
9013 | |
9014 | /* Return e.get<i>() or get<i>(e). */ |
9015 | |
9016 | static tree |
9017 | get_tuple_decomp_init (tree decl, unsigned i) |
9018 | { |
9019 | tree targs = make_tree_vec (1); |
9020 | TREE_VEC_ELT (targs, 0) = build_int_cst (integer_type_node, i); |
9021 | |
9022 | tree etype = TREE_TYPE (decl); |
9023 | tree e = convert_from_reference (decl); |
9024 | |
9025 | /* [The id-expression] e is an lvalue if the type of the entity e is an |
9026 | lvalue reference and an xvalue otherwise. */ |
9027 | if (!TYPE_REF_P (etype) |
9028 | || TYPE_REF_IS_RVALUE (etype)) |
9029 | e = move (e); |
9030 | |
9031 | tree fns = lookup_qualified_name (TREE_TYPE (e), get__identifier, |
9032 | LOOK_want::NORMAL, /*complain*/false); |
9033 | bool use_member_get = false; |
9034 | |
9035 | /* To use a member get, member lookup must find at least one |
9036 | declaration that is a function template |
9037 | whose first template parameter is a non-type parameter. */ |
9038 | for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (fns)); iter; ++iter) |
9039 | { |
9040 | tree fn = *iter; |
9041 | if (TREE_CODE (fn) == TEMPLATE_DECL) |
9042 | { |
9043 | tree tparms = DECL_TEMPLATE_PARMS (fn); |
9044 | tree parm = TREE_VEC_ELT (INNERMOST_TEMPLATE_PARMS (tparms), 0); |
9045 | if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL) |
9046 | { |
9047 | use_member_get = true; |
9048 | break; |
9049 | } |
9050 | } |
9051 | } |
9052 | |
9053 | if (use_member_get) |
9054 | { |
9055 | fns = lookup_template_function (fns, targs); |
9056 | return build_new_method_call (e, fns, /*args*/NULL, |
9057 | /*path*/NULL_TREE, LOOKUP_NORMAL, |
9058 | /*fn_p*/NULL, tf_warning_or_error); |
9059 | } |
9060 | else |
9061 | { |
9062 | releasing_vec args (make_tree_vector_single (e)); |
9063 | fns = lookup_template_function (get__identifier, targs); |
9064 | fns = perform_koenig_lookup (fns, args, tf_warning_or_error); |
9065 | return finish_call_expr (fns, &args, /*novirt*/false, |
9066 | /*koenig*/true, tf_warning_or_error); |
9067 | } |
9068 | } |
9069 | |
9070 | /* It's impossible to recover the decltype of a tuple decomposition variable |
9071 | based on the actual type of the variable, so store it in a hash table. */ |
9072 | |
9073 | static GTY((cache)) decl_tree_cache_map *decomp_type_table; |
9074 | |
9075 | tree |
9076 | lookup_decomp_type (tree v) |
9077 | { |
9078 | return *decomp_type_table->get (k: v); |
9079 | } |
9080 | |
9081 | /* Mangle a decomposition declaration if needed. Arguments like |
9082 | in cp_finish_decomp. */ |
9083 | |
9084 | static void |
9085 | cp_maybe_mangle_decomp (tree decl, cp_decomp *decomp) |
9086 | { |
9087 | if (!processing_template_decl |
9088 | && !error_operand_p (t: decl) |
9089 | && TREE_STATIC (decl)) |
9090 | { |
9091 | auto_vec<tree, 16> v; |
9092 | v.safe_grow (len: decomp->count, exact: true); |
9093 | tree d = decomp->decl; |
9094 | for (unsigned int i = 0; i < decomp->count; i++, d = DECL_CHAIN (d)) |
9095 | v[decomp->count - i - 1] = d; |
9096 | if (DECL_FUNCTION_SCOPE_P (decl)) |
9097 | { |
9098 | size_t sz = 3; |
9099 | for (unsigned int i = 0; i < decomp->count; ++i) |
9100 | sz += IDENTIFIER_LENGTH (DECL_NAME (v[i])) + 1; |
9101 | char *name = XALLOCAVEC (char, sz); |
9102 | name[0] = 'D'; |
9103 | name[1] = 'C'; |
9104 | char *p = name + 2; |
9105 | for (unsigned int i = 0; i < decomp->count; ++i) |
9106 | { |
9107 | size_t len = IDENTIFIER_LENGTH (DECL_NAME (v[i])); |
9108 | *p++ = ' '; |
9109 | memcpy (dest: p, IDENTIFIER_POINTER (DECL_NAME (v[i])), n: len); |
9110 | p += len; |
9111 | } |
9112 | *p = '\0'; |
9113 | determine_local_discriminator (decl, get_identifier (name)); |
9114 | } |
9115 | SET_DECL_ASSEMBLER_NAME (decl, mangle_decomp (decl, v)); |
9116 | maybe_apply_pragma_weak (decl); |
9117 | } |
9118 | } |
9119 | |
9120 | /* Finish a decomposition declaration. DECL is the underlying declaration |
9121 | "e", FIRST is the head of a chain of decls for the individual identifiers |
9122 | chained through DECL_CHAIN in reverse order and COUNT is the number of |
9123 | those decls. */ |
9124 | |
9125 | void |
9126 | cp_finish_decomp (tree decl, cp_decomp *decomp) |
9127 | { |
9128 | tree first = decomp->decl; |
9129 | unsigned count = decomp->count; |
9130 | if (error_operand_p (t: decl)) |
9131 | { |
9132 | error_out: |
9133 | while (count--) |
9134 | { |
9135 | TREE_TYPE (first) = error_mark_node; |
9136 | if (DECL_HAS_VALUE_EXPR_P (first)) |
9137 | { |
9138 | SET_DECL_VALUE_EXPR (first, NULL_TREE); |
9139 | DECL_HAS_VALUE_EXPR_P (first) = 0; |
9140 | } |
9141 | first = DECL_CHAIN (first); |
9142 | } |
9143 | if (DECL_P (decl) && DECL_NAMESPACE_SCOPE_P (decl)) |
9144 | SET_DECL_ASSEMBLER_NAME (decl, get_identifier ("<decomp>" )); |
9145 | return; |
9146 | } |
9147 | |
9148 | location_t loc = DECL_SOURCE_LOCATION (decl); |
9149 | if (type_dependent_expression_p (decl) |
9150 | /* This happens for range for when not in templates. |
9151 | Still add the DECL_VALUE_EXPRs for later processing. */ |
9152 | || (!processing_template_decl |
9153 | && type_uses_auto (TREE_TYPE (decl)))) |
9154 | { |
9155 | for (unsigned int i = 0; i < count; i++) |
9156 | { |
9157 | if (!DECL_HAS_VALUE_EXPR_P (first)) |
9158 | { |
9159 | tree v = build_nt (ARRAY_REF, decl, |
9160 | size_int (count - i - 1), |
9161 | NULL_TREE, NULL_TREE); |
9162 | SET_DECL_VALUE_EXPR (first, v); |
9163 | DECL_HAS_VALUE_EXPR_P (first) = 1; |
9164 | } |
9165 | if (processing_template_decl) |
9166 | fit_decomposition_lang_decl (first, decl); |
9167 | first = DECL_CHAIN (first); |
9168 | } |
9169 | return; |
9170 | } |
9171 | |
9172 | auto_vec<tree, 16> v; |
9173 | v.safe_grow (len: count, exact: true); |
9174 | tree d = first; |
9175 | for (unsigned int i = 0; i < count; i++, d = DECL_CHAIN (d)) |
9176 | { |
9177 | v[count - i - 1] = d; |
9178 | fit_decomposition_lang_decl (d, decl); |
9179 | } |
9180 | |
9181 | tree type = TREE_TYPE (decl); |
9182 | tree dexp = decl; |
9183 | |
9184 | if (TYPE_REF_P (type)) |
9185 | { |
9186 | dexp = convert_from_reference (dexp); |
9187 | type = complete_type (TREE_TYPE (type)); |
9188 | if (type == error_mark_node) |
9189 | goto error_out; |
9190 | if (!COMPLETE_TYPE_P (type)) |
9191 | { |
9192 | error_at (loc, "structured binding refers to incomplete type %qT" , |
9193 | type); |
9194 | goto error_out; |
9195 | } |
9196 | } |
9197 | |
9198 | tree eltype = NULL_TREE; |
9199 | unsigned HOST_WIDE_INT eltscnt = 0; |
9200 | if (TREE_CODE (type) == ARRAY_TYPE) |
9201 | { |
9202 | tree nelts; |
9203 | nelts = array_type_nelts_top (type); |
9204 | if (nelts == error_mark_node) |
9205 | goto error_out; |
9206 | if (!tree_fits_uhwi_p (nelts)) |
9207 | { |
9208 | error_at (loc, "cannot decompose variable length array %qT" , type); |
9209 | goto error_out; |
9210 | } |
9211 | eltscnt = tree_to_uhwi (nelts); |
9212 | if (count != eltscnt) |
9213 | { |
9214 | cnt_mismatch: |
9215 | if (count > eltscnt) |
9216 | error_n (loc, count, |
9217 | "%u name provided for structured binding" , |
9218 | "%u names provided for structured binding" , count); |
9219 | else |
9220 | error_n (loc, count, |
9221 | "only %u name provided for structured binding" , |
9222 | "only %u names provided for structured binding" , count); |
9223 | inform_n (loc, eltscnt, |
9224 | "while %qT decomposes into %wu element" , |
9225 | "while %qT decomposes into %wu elements" , |
9226 | type, eltscnt); |
9227 | goto error_out; |
9228 | } |
9229 | eltype = TREE_TYPE (type); |
9230 | for (unsigned int i = 0; i < count; i++) |
9231 | { |
9232 | TREE_TYPE (v[i]) = eltype; |
9233 | layout_decl (v[i], 0); |
9234 | if (processing_template_decl) |
9235 | continue; |
9236 | tree t = unshare_expr (dexp); |
9237 | t = build4 (ARRAY_REF, eltype, t, size_int (i), NULL_TREE, NULL_TREE); |
9238 | SET_DECL_VALUE_EXPR (v[i], t); |
9239 | DECL_HAS_VALUE_EXPR_P (v[i]) = 1; |
9240 | } |
9241 | } |
9242 | /* 2 GNU extensions. */ |
9243 | else if (TREE_CODE (type) == COMPLEX_TYPE) |
9244 | { |
9245 | eltscnt = 2; |
9246 | if (count != eltscnt) |
9247 | goto cnt_mismatch; |
9248 | eltype = cp_build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type)); |
9249 | for (unsigned int i = 0; i < count; i++) |
9250 | { |
9251 | TREE_TYPE (v[i]) = eltype; |
9252 | layout_decl (v[i], 0); |
9253 | if (processing_template_decl) |
9254 | continue; |
9255 | tree t = unshare_expr (dexp); |
9256 | t = build1 (i ? IMAGPART_EXPR : REALPART_EXPR, eltype, t); |
9257 | SET_DECL_VALUE_EXPR (v[i], t); |
9258 | DECL_HAS_VALUE_EXPR_P (v[i]) = 1; |
9259 | } |
9260 | } |
9261 | else if (TREE_CODE (type) == VECTOR_TYPE) |
9262 | { |
9263 | if (!TYPE_VECTOR_SUBPARTS (node: type).is_constant (const_value: &eltscnt)) |
9264 | { |
9265 | error_at (loc, "cannot decompose variable length vector %qT" , type); |
9266 | goto error_out; |
9267 | } |
9268 | if (count != eltscnt) |
9269 | goto cnt_mismatch; |
9270 | eltype = cp_build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type)); |
9271 | for (unsigned int i = 0; i < count; i++) |
9272 | { |
9273 | TREE_TYPE (v[i]) = eltype; |
9274 | layout_decl (v[i], 0); |
9275 | if (processing_template_decl) |
9276 | continue; |
9277 | tree t = unshare_expr (dexp); |
9278 | convert_vector_to_array_for_subscript (DECL_SOURCE_LOCATION (v[i]), |
9279 | &t, size_int (i)); |
9280 | t = build4 (ARRAY_REF, eltype, t, size_int (i), NULL_TREE, NULL_TREE); |
9281 | SET_DECL_VALUE_EXPR (v[i], t); |
9282 | DECL_HAS_VALUE_EXPR_P (v[i]) = 1; |
9283 | } |
9284 | } |
9285 | else if (tree tsize = get_tuple_size (type)) |
9286 | { |
9287 | if (tsize == error_mark_node) |
9288 | { |
9289 | error_at (loc, "%<std::tuple_size<%T>::value%> is not an integral " |
9290 | "constant expression" , type); |
9291 | goto error_out; |
9292 | } |
9293 | if (!tree_fits_uhwi_p (tsize)) |
9294 | { |
9295 | error_n (loc, count, |
9296 | "%u name provided for structured binding" , |
9297 | "%u names provided for structured binding" , count); |
9298 | inform (loc, "while %qT decomposes into %E elements" , |
9299 | type, tsize); |
9300 | goto error_out; |
9301 | } |
9302 | eltscnt = tree_to_uhwi (tsize); |
9303 | if (count != eltscnt) |
9304 | goto cnt_mismatch; |
9305 | int save_read = DECL_READ_P (decl); |
9306 | for (unsigned i = 0; i < count; ++i) |
9307 | { |
9308 | location_t sloc = input_location; |
9309 | location_t dloc = DECL_SOURCE_LOCATION (v[i]); |
9310 | |
9311 | input_location = dloc; |
9312 | tree init = get_tuple_decomp_init (decl, i); |
9313 | tree eltype = (init == error_mark_node ? error_mark_node |
9314 | : get_tuple_element_type (type, i)); |
9315 | input_location = sloc; |
9316 | |
9317 | if (VOID_TYPE_P (eltype)) |
9318 | { |
9319 | error ("%<std::tuple_element<%u, %T>::type%> is %<void%>" , |
9320 | i, type); |
9321 | eltype = error_mark_node; |
9322 | } |
9323 | if (init == error_mark_node || eltype == error_mark_node) |
9324 | { |
9325 | inform (dloc, "in initialization of structured binding " |
9326 | "variable %qD" , v[i]); |
9327 | goto error_out; |
9328 | } |
9329 | /* Save the decltype away before reference collapse. */ |
9330 | hash_map_safe_put<hm_ggc> (h&: decomp_type_table, k: v[i], v: eltype); |
9331 | eltype = cp_build_reference_type (eltype, !lvalue_p (init)); |
9332 | TREE_TYPE (v[i]) = eltype; |
9333 | layout_decl (v[i], 0); |
9334 | if (DECL_HAS_VALUE_EXPR_P (v[i])) |
9335 | { |
9336 | /* In this case the names are variables, not just proxies. */ |
9337 | SET_DECL_VALUE_EXPR (v[i], NULL_TREE); |
9338 | DECL_HAS_VALUE_EXPR_P (v[i]) = 0; |
9339 | } |
9340 | if (!processing_template_decl) |
9341 | { |
9342 | copy_linkage (v[i], decl); |
9343 | cp_finish_decl (decl: v[i], init, /*constexpr*/init_const_expr_p: false, |
9344 | /*asm*/NULL_TREE, LOOKUP_NORMAL); |
9345 | } |
9346 | } |
9347 | /* Ignore reads from the underlying decl performed during initialization |
9348 | of the individual variables. If those will be read, we'll mark |
9349 | the underlying decl as read at that point. */ |
9350 | DECL_READ_P (decl) = save_read; |
9351 | } |
9352 | else if (TREE_CODE (type) == UNION_TYPE) |
9353 | { |
9354 | error_at (loc, "cannot decompose union type %qT" , type); |
9355 | goto error_out; |
9356 | } |
9357 | else if (!CLASS_TYPE_P (type)) |
9358 | { |
9359 | error_at (loc, "cannot decompose non-array non-class type %qT" , type); |
9360 | goto error_out; |
9361 | } |
9362 | else if (LAMBDA_TYPE_P (type)) |
9363 | { |
9364 | error_at (loc, "cannot decompose lambda closure type %qT" , type); |
9365 | goto error_out; |
9366 | } |
9367 | else if (processing_template_decl && complete_type (type) == error_mark_node) |
9368 | goto error_out; |
9369 | else if (processing_template_decl && !COMPLETE_TYPE_P (type)) |
9370 | pedwarn (loc, 0, "structured binding refers to incomplete class type %qT" , |
9371 | type); |
9372 | else |
9373 | { |
9374 | tree btype = find_decomp_class_base (loc, type, NULL_TREE); |
9375 | if (btype == error_mark_node) |
9376 | goto error_out; |
9377 | else if (btype == NULL_TREE) |
9378 | { |
9379 | error_at (loc, "cannot decompose class type %qT without non-static " |
9380 | "data members" , type); |
9381 | goto error_out; |
9382 | } |
9383 | for (tree field = TYPE_FIELDS (btype); field; field = TREE_CHAIN (field)) |
9384 | if (TREE_CODE (field) != FIELD_DECL |
9385 | || DECL_ARTIFICIAL (field) |
9386 | || DECL_UNNAMED_BIT_FIELD (field)) |
9387 | continue; |
9388 | else |
9389 | eltscnt++; |
9390 | if (count != eltscnt) |
9391 | goto cnt_mismatch; |
9392 | tree t = dexp; |
9393 | if (type != btype) |
9394 | { |
9395 | t = convert_to_base (t, btype, /*check_access*/true, |
9396 | /*nonnull*/false, tf_warning_or_error); |
9397 | type = btype; |
9398 | } |
9399 | unsigned int i = 0; |
9400 | for (tree field = TYPE_FIELDS (btype); field; field = TREE_CHAIN (field)) |
9401 | if (TREE_CODE (field) != FIELD_DECL |
9402 | || DECL_ARTIFICIAL (field) |
9403 | || DECL_UNNAMED_BIT_FIELD (field)) |
9404 | continue; |
9405 | else |
9406 | { |
9407 | tree tt = finish_non_static_data_member (field, unshare_expr (t), |
9408 | NULL_TREE); |
9409 | if (REFERENCE_REF_P (tt)) |
9410 | tt = TREE_OPERAND (tt, 0); |
9411 | TREE_TYPE (v[i]) = TREE_TYPE (tt); |
9412 | layout_decl (v[i], 0); |
9413 | if (!processing_template_decl) |
9414 | { |
9415 | SET_DECL_VALUE_EXPR (v[i], tt); |
9416 | DECL_HAS_VALUE_EXPR_P (v[i]) = 1; |
9417 | } |
9418 | i++; |
9419 | } |
9420 | } |
9421 | if (processing_template_decl) |
9422 | { |
9423 | for (unsigned int i = 0; i < count; i++) |
9424 | if (!DECL_HAS_VALUE_EXPR_P (v[i])) |
9425 | { |
9426 | tree a = build_nt (ARRAY_REF, decl, size_int (i), |
9427 | NULL_TREE, NULL_TREE); |
9428 | SET_DECL_VALUE_EXPR (v[i], a); |
9429 | DECL_HAS_VALUE_EXPR_P (v[i]) = 1; |
9430 | } |
9431 | } |
9432 | } |
9433 | |
9434 | /* Returns a declaration for a VAR_DECL as if: |
9435 | |
9436 | extern "C" TYPE NAME; |
9437 | |
9438 | had been seen. Used to create compiler-generated global |
9439 | variables. */ |
9440 | |
9441 | static tree |
9442 | declare_global_var (tree name, tree type) |
9443 | { |
9444 | auto cookie = push_abi_namespace (global_namespace); |
9445 | tree decl = build_decl (input_location, VAR_DECL, name, type); |
9446 | TREE_PUBLIC (decl) = 1; |
9447 | DECL_EXTERNAL (decl) = 1; |
9448 | DECL_ARTIFICIAL (decl) = 1; |
9449 | DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace); |
9450 | /* If the user has explicitly declared this variable (perhaps |
9451 | because the code we are compiling is part of a low-level runtime |
9452 | library), then it is possible that our declaration will be merged |
9453 | with theirs by pushdecl. */ |
9454 | decl = pushdecl (decl); |
9455 | cp_finish_decl (decl, NULL_TREE, init_const_expr_p: false, NULL_TREE, flags: 0); |
9456 | pop_abi_namespace (flags: cookie, global_namespace); |
9457 | |
9458 | return decl; |
9459 | } |
9460 | |
9461 | /* Returns the type for the argument to "__cxa_atexit" (or "atexit", |
9462 | if "__cxa_atexit" is not being used) corresponding to the function |
9463 | to be called when the program exits. */ |
9464 | |
9465 | static tree |
9466 | get_atexit_fn_ptr_type (void) |
9467 | { |
9468 | tree fn_type; |
9469 | |
9470 | if (!atexit_fn_ptr_type_node) |
9471 | { |
9472 | tree arg_type; |
9473 | if (flag_use_cxa_atexit |
9474 | && !targetm.cxx.use_atexit_for_cxa_atexit ()) |
9475 | /* The parameter to "__cxa_atexit" is "void (*)(void *)". */ |
9476 | arg_type = ptr_type_node; |
9477 | else |
9478 | /* The parameter to "atexit" is "void (*)(void)". */ |
9479 | arg_type = NULL_TREE; |
9480 | |
9481 | fn_type = build_function_type_list (void_type_node, |
9482 | arg_type, NULL_TREE); |
9483 | atexit_fn_ptr_type_node = build_pointer_type (fn_type); |
9484 | } |
9485 | |
9486 | return atexit_fn_ptr_type_node; |
9487 | } |
9488 | |
9489 | /* Returns a pointer to the `atexit' function. Note that if |
9490 | FLAG_USE_CXA_ATEXIT is nonzero, then this will actually be the new |
9491 | `__cxa_atexit' function specified in the IA64 C++ ABI. */ |
9492 | |
9493 | static tree |
9494 | get_atexit_node (void) |
9495 | { |
9496 | tree atexit_fndecl; |
9497 | tree fn_type; |
9498 | tree fn_ptr_type; |
9499 | const char *name; |
9500 | bool use_aeabi_atexit; |
9501 | tree ctx = global_namespace; |
9502 | |
9503 | if (atexit_node) |
9504 | return atexit_node; |
9505 | |
9506 | if (flag_use_cxa_atexit && !targetm.cxx.use_atexit_for_cxa_atexit ()) |
9507 | { |
9508 | /* The declaration for `__cxa_atexit' is: |
9509 | |
9510 | int __cxa_atexit (void (*)(void *), void *, void *) |
9511 | |
9512 | We build up the argument types and then the function type |
9513 | itself. */ |
9514 | tree argtype0, argtype1, argtype2; |
9515 | |
9516 | use_aeabi_atexit = targetm.cxx.use_aeabi_atexit (); |
9517 | /* First, build the pointer-to-function type for the first |
9518 | argument. */ |
9519 | fn_ptr_type = get_atexit_fn_ptr_type (); |
9520 | /* Then, build the rest of the argument types. */ |
9521 | argtype2 = ptr_type_node; |
9522 | if (use_aeabi_atexit) |
9523 | { |
9524 | argtype1 = fn_ptr_type; |
9525 | argtype0 = ptr_type_node; |
9526 | } |
9527 | else |
9528 | { |
9529 | argtype1 = ptr_type_node; |
9530 | argtype0 = fn_ptr_type; |
9531 | } |
9532 | /* And the final __cxa_atexit type. */ |
9533 | fn_type = build_function_type_list (integer_type_node, |
9534 | argtype0, argtype1, argtype2, |
9535 | NULL_TREE); |
9536 | /* ... which needs noexcept. */ |
9537 | fn_type = build_exception_variant (fn_type, noexcept_true_spec); |
9538 | if (use_aeabi_atexit) |
9539 | { |
9540 | name = "__aeabi_atexit" ; |
9541 | push_to_top_level (); |
9542 | int n = push_namespace (get_identifier ("__aeabiv1" ), make_inline: false); |
9543 | ctx = current_namespace; |
9544 | while (n--) |
9545 | pop_namespace (); |
9546 | pop_from_top_level (); |
9547 | } |
9548 | else |
9549 | { |
9550 | name = "__cxa_atexit" ; |
9551 | ctx = abi_node; |
9552 | } |
9553 | } |
9554 | else |
9555 | { |
9556 | /* The declaration for `atexit' is: |
9557 | |
9558 | int atexit (void (*)()); |
9559 | |
9560 | We build up the argument types and then the function type |
9561 | itself. */ |
9562 | fn_ptr_type = get_atexit_fn_ptr_type (); |
9563 | /* Build the final atexit type. */ |
9564 | fn_type = build_function_type_list (integer_type_node, |
9565 | fn_ptr_type, NULL_TREE); |
9566 | /* ... which needs noexcept. */ |
9567 | fn_type = build_exception_variant (fn_type, noexcept_true_spec); |
9568 | name = "atexit" ; |
9569 | } |
9570 | |
9571 | /* Now, build the function declaration. */ |
9572 | push_lang_context (lang_name_c); |
9573 | auto cookie = push_abi_namespace (node: ctx); |
9574 | atexit_fndecl = build_library_fn_ptr (name, type: fn_type, ECF_LEAF | ECF_NOTHROW); |
9575 | DECL_CONTEXT (atexit_fndecl) = FROB_CONTEXT (current_namespace); |
9576 | /* Install as hidden builtin so we're (a) more relaxed about |
9577 | exception spec matching and (b) will not give a confusing location |
9578 | in diagnostic and (c) won't magically appear in user-visible name |
9579 | lookups. */ |
9580 | DECL_SOURCE_LOCATION (atexit_fndecl) = BUILTINS_LOCATION; |
9581 | atexit_fndecl = pushdecl (atexit_fndecl, /*hiding=*/true); |
9582 | pop_abi_namespace (flags: cookie, node: ctx); |
9583 | mark_used (atexit_fndecl); |
9584 | pop_lang_context (); |
9585 | atexit_node = decay_conversion (atexit_fndecl, tf_warning_or_error); |
9586 | |
9587 | return atexit_node; |
9588 | } |
9589 | |
9590 | /* Like get_atexit_node, but for thread-local cleanups. */ |
9591 | |
9592 | static tree |
9593 | get_thread_atexit_node (void) |
9594 | { |
9595 | /* The declaration for `__cxa_thread_atexit' is: |
9596 | |
9597 | int __cxa_thread_atexit (void (*)(void *), void *, void *) */ |
9598 | tree fn_type = build_function_type_list (integer_type_node, |
9599 | get_atexit_fn_ptr_type (), |
9600 | ptr_type_node, ptr_type_node, |
9601 | NULL_TREE); |
9602 | |
9603 | /* Now, build the function declaration. */ |
9604 | tree atexit_fndecl = build_library_fn_ptr (name: "__cxa_thread_atexit" , type: fn_type, |
9605 | ECF_LEAF | ECF_NOTHROW); |
9606 | return decay_conversion (atexit_fndecl, tf_warning_or_error); |
9607 | } |
9608 | |
9609 | /* Returns the __dso_handle VAR_DECL. */ |
9610 | |
9611 | static tree |
9612 | get_dso_handle_node (void) |
9613 | { |
9614 | if (dso_handle_node) |
9615 | return dso_handle_node; |
9616 | |
9617 | /* Declare the variable. */ |
9618 | dso_handle_node = declare_global_var (get_identifier ("__dso_handle" ), |
9619 | ptr_type_node); |
9620 | |
9621 | #ifdef HAVE_GAS_HIDDEN |
9622 | if (dso_handle_node != error_mark_node) |
9623 | { |
9624 | DECL_VISIBILITY (dso_handle_node) = VISIBILITY_HIDDEN; |
9625 | DECL_VISIBILITY_SPECIFIED (dso_handle_node) = 1; |
9626 | } |
9627 | #endif |
9628 | |
9629 | return dso_handle_node; |
9630 | } |
9631 | |
9632 | /* Begin a new function with internal linkage whose job will be simply |
9633 | to destroy some particular variable. */ |
9634 | |
9635 | static GTY(()) int start_cleanup_cnt; |
9636 | |
9637 | static tree |
9638 | start_cleanup_fn (void) |
9639 | { |
9640 | char name[32]; |
9641 | |
9642 | push_to_top_level (); |
9643 | |
9644 | /* No need to mangle this. */ |
9645 | push_lang_context (lang_name_c); |
9646 | |
9647 | /* Build the name of the function. */ |
9648 | sprintf (s: name, format: "__tcf_%d" , start_cleanup_cnt++); |
9649 | /* Build the function declaration. */ |
9650 | tree fntype = TREE_TYPE (get_atexit_fn_ptr_type ()); |
9651 | tree fndecl = build_lang_decl (FUNCTION_DECL, get_identifier (name), fntype); |
9652 | DECL_CONTEXT (fndecl) = FROB_CONTEXT (current_namespace); |
9653 | /* It's a function with internal linkage, generated by the |
9654 | compiler. */ |
9655 | TREE_PUBLIC (fndecl) = 0; |
9656 | DECL_ARTIFICIAL (fndecl) = 1; |
9657 | /* Make the function `inline' so that it is only emitted if it is |
9658 | actually needed. It is unlikely that it will be inlined, since |
9659 | it is only called via a function pointer, but we avoid unnecessary |
9660 | emissions this way. */ |
9661 | DECL_DECLARED_INLINE_P (fndecl) = 1; |
9662 | DECL_INTERFACE_KNOWN (fndecl) = 1; |
9663 | if (flag_use_cxa_atexit && !targetm.cxx.use_atexit_for_cxa_atexit ()) |
9664 | { |
9665 | /* Build the parameter. */ |
9666 | tree parmdecl = cp_build_parm_decl (fndecl, NULL_TREE, ptr_type_node); |
9667 | TREE_USED (parmdecl) = 1; |
9668 | DECL_READ_P (parmdecl) = 1; |
9669 | DECL_ARGUMENTS (fndecl) = parmdecl; |
9670 | } |
9671 | |
9672 | fndecl = pushdecl (fndecl, /*hidden=*/hiding: true); |
9673 | start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED); |
9674 | |
9675 | pop_lang_context (); |
9676 | |
9677 | return current_function_decl; |
9678 | } |
9679 | |
9680 | /* Finish the cleanup function begun by start_cleanup_fn. */ |
9681 | |
9682 | static void |
9683 | end_cleanup_fn (void) |
9684 | { |
9685 | expand_or_defer_fn (finish_function (/*inline_p=*/false)); |
9686 | |
9687 | pop_from_top_level (); |
9688 | } |
9689 | |
9690 | /* Generate code to handle the destruction of DECL, an object with |
9691 | static storage duration. */ |
9692 | |
9693 | tree |
9694 | register_dtor_fn (tree decl) |
9695 | { |
9696 | tree cleanup; |
9697 | tree addr; |
9698 | tree compound_stmt; |
9699 | tree fcall; |
9700 | tree type; |
9701 | bool ob_parm, dso_parm, use_dtor; |
9702 | tree arg0, arg1, arg2; |
9703 | tree atex_node; |
9704 | |
9705 | type = TREE_TYPE (decl); |
9706 | if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type)) |
9707 | return void_node; |
9708 | |
9709 | if (decl_maybe_constant_destruction (decl, type) |
9710 | && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl)) |
9711 | { |
9712 | cxx_maybe_build_cleanup (decl, tf_warning_or_error); |
9713 | return void_node; |
9714 | } |
9715 | |
9716 | /* If we're using "__cxa_atexit" (or "__cxa_thread_atexit" or |
9717 | "__aeabi_atexit"), and DECL is a class object, we can just pass the |
9718 | destructor to "__cxa_atexit"; we don't have to build a temporary |
9719 | function to do the cleanup. */ |
9720 | dso_parm = (flag_use_cxa_atexit |
9721 | && !targetm.cxx.use_atexit_for_cxa_atexit ()); |
9722 | ob_parm = (CP_DECL_THREAD_LOCAL_P (decl) || dso_parm); |
9723 | use_dtor = ob_parm && CLASS_TYPE_P (type); |
9724 | if (use_dtor) |
9725 | { |
9726 | cleanup = get_class_binding (type, complete_dtor_identifier); |
9727 | |
9728 | /* Make sure it is accessible. */ |
9729 | perform_or_defer_access_check (TYPE_BINFO (type), cleanup, cleanup, |
9730 | tf_warning_or_error); |
9731 | } |
9732 | else |
9733 | { |
9734 | /* Call build_cleanup before we enter the anonymous function so |
9735 | that any access checks will be done relative to the current |
9736 | scope, rather than the scope of the anonymous function. */ |
9737 | build_cleanup (decl); |
9738 | |
9739 | /* Now start the function. */ |
9740 | cleanup = start_cleanup_fn (); |
9741 | |
9742 | /* Now, recompute the cleanup. It may contain SAVE_EXPRs that refer |
9743 | to the original function, rather than the anonymous one. That |
9744 | will make the back end think that nested functions are in use, |
9745 | which causes confusion. */ |
9746 | push_deferring_access_checks (dk_no_check); |
9747 | fcall = build_cleanup (decl); |
9748 | pop_deferring_access_checks (); |
9749 | |
9750 | /* Create the body of the anonymous function. */ |
9751 | compound_stmt = begin_compound_stmt (BCS_FN_BODY); |
9752 | finish_expr_stmt (fcall); |
9753 | finish_compound_stmt (compound_stmt); |
9754 | end_cleanup_fn (); |
9755 | } |
9756 | |
9757 | /* Call atexit with the cleanup function. */ |
9758 | mark_used (cleanup); |
9759 | cleanup = build_address (cleanup); |
9760 | |
9761 | if (CP_DECL_THREAD_LOCAL_P (decl)) |
9762 | atex_node = get_thread_atexit_node (); |
9763 | else |
9764 | atex_node = get_atexit_node (); |
9765 | |
9766 | if (use_dtor) |
9767 | { |
9768 | /* We must convert CLEANUP to the type that "__cxa_atexit" |
9769 | expects. */ |
9770 | cleanup = build_nop (get_atexit_fn_ptr_type (), cleanup); |
9771 | /* "__cxa_atexit" will pass the address of DECL to the |
9772 | cleanup function. */ |
9773 | mark_used (decl); |
9774 | addr = build_address (decl); |
9775 | /* The declared type of the parameter to "__cxa_atexit" is |
9776 | "void *". For plain "T*", we could just let the |
9777 | machinery in cp_build_function_call convert it -- but if the |
9778 | type is "cv-qualified T *", then we need to convert it |
9779 | before passing it in, to avoid spurious errors. */ |
9780 | addr = build_nop (ptr_type_node, addr); |
9781 | } |
9782 | else |
9783 | /* Since the cleanup functions we build ignore the address |
9784 | they're given, there's no reason to pass the actual address |
9785 | in, and, in general, it's cheaper to pass NULL than any |
9786 | other value. */ |
9787 | addr = null_pointer_node; |
9788 | |
9789 | if (dso_parm) |
9790 | arg2 = cp_build_addr_expr (get_dso_handle_node (), |
9791 | tf_warning_or_error); |
9792 | else if (ob_parm) |
9793 | /* Just pass NULL to the dso handle parm if we don't actually |
9794 | have a DSO handle on this target. */ |
9795 | arg2 = null_pointer_node; |
9796 | else |
9797 | arg2 = NULL_TREE; |
9798 | |
9799 | if (ob_parm) |
9800 | { |
9801 | if (!CP_DECL_THREAD_LOCAL_P (decl) |
9802 | && targetm.cxx.use_aeabi_atexit ()) |
9803 | { |
9804 | arg1 = cleanup; |
9805 | arg0 = addr; |
9806 | } |
9807 | else |
9808 | { |
9809 | arg1 = addr; |
9810 | arg0 = cleanup; |
9811 | } |
9812 | } |
9813 | else |
9814 | { |
9815 | arg0 = cleanup; |
9816 | arg1 = NULL_TREE; |
9817 | } |
9818 | return cp_build_function_call_nary (atex_node, tf_warning_or_error, |
9819 | arg0, arg1, arg2, NULL_TREE); |
9820 | } |
9821 | |
9822 | /* DECL is a VAR_DECL with static storage duration. INIT, if present, |
9823 | is its initializer. Generate code to handle the construction |
9824 | and destruction of DECL. */ |
9825 | |
9826 | static void |
9827 | expand_static_init (tree decl, tree init) |
9828 | { |
9829 | gcc_assert (VAR_P (decl)); |
9830 | gcc_assert (TREE_STATIC (decl)); |
9831 | |
9832 | /* Some variables require no dynamic initialization. */ |
9833 | if (decl_maybe_constant_destruction (decl, TREE_TYPE (decl))) |
9834 | { |
9835 | /* Make sure the destructor is callable. */ |
9836 | cxx_maybe_build_cleanup (decl, tf_warning_or_error); |
9837 | if (!init) |
9838 | return; |
9839 | } |
9840 | |
9841 | if (CP_DECL_THREAD_LOCAL_P (decl) && DECL_GNU_TLS_P (decl) |
9842 | && !DECL_FUNCTION_SCOPE_P (decl)) |
9843 | { |
9844 | location_t dloc = DECL_SOURCE_LOCATION (decl); |
9845 | if (init) |
9846 | error_at (dloc, "non-local variable %qD declared %<__thread%> " |
9847 | "needs dynamic initialization" , decl); |
9848 | else |
9849 | error_at (dloc, "non-local variable %qD declared %<__thread%> " |
9850 | "has a non-trivial destructor" , decl); |
9851 | static bool informed; |
9852 | if (!informed) |
9853 | { |
9854 | inform (dloc, "C++11 %<thread_local%> allows dynamic " |
9855 | "initialization and destruction" ); |
9856 | informed = true; |
9857 | } |
9858 | return; |
9859 | } |
9860 | |
9861 | if (DECL_FUNCTION_SCOPE_P (decl)) |
9862 | { |
9863 | /* Emit code to perform this initialization but once. */ |
9864 | tree if_stmt = NULL_TREE, inner_if_stmt = NULL_TREE; |
9865 | tree then_clause = NULL_TREE, inner_then_clause = NULL_TREE; |
9866 | tree guard, guard_addr; |
9867 | tree flag, begin; |
9868 | /* We don't need thread-safety code for thread-local vars. */ |
9869 | bool thread_guard = (flag_threadsafe_statics |
9870 | && !CP_DECL_THREAD_LOCAL_P (decl)); |
9871 | |
9872 | /* Emit code to perform this initialization but once. This code |
9873 | looks like: |
9874 | |
9875 | static <type> guard; |
9876 | if (!__atomic_load (guard.first_byte)) { |
9877 | if (__cxa_guard_acquire (&guard)) { |
9878 | bool flag = false; |
9879 | try { |
9880 | // Do initialization. |
9881 | flag = true; __cxa_guard_release (&guard); |
9882 | // Register variable for destruction at end of program. |
9883 | } catch { |
9884 | if (!flag) __cxa_guard_abort (&guard); |
9885 | } |
9886 | } |
9887 | } |
9888 | |
9889 | Note that the `flag' variable is only set to 1 *after* the |
9890 | initialization is complete. This ensures that an exception, |
9891 | thrown during the construction, will cause the variable to |
9892 | reinitialized when we pass through this code again, as per: |
9893 | |
9894 | [stmt.dcl] |
9895 | |
9896 | If the initialization exits by throwing an exception, the |
9897 | initialization is not complete, so it will be tried again |
9898 | the next time control enters the declaration. |
9899 | |
9900 | This process should be thread-safe, too; multiple threads |
9901 | should not be able to initialize the variable more than |
9902 | once. */ |
9903 | |
9904 | /* Create the guard variable. */ |
9905 | guard = get_guard (decl); |
9906 | |
9907 | /* Begin the conditional initialization. */ |
9908 | if_stmt = begin_if_stmt (); |
9909 | |
9910 | finish_if_stmt_cond (get_guard_cond (guard, thread_guard), if_stmt); |
9911 | then_clause = begin_compound_stmt (BCS_NO_SCOPE); |
9912 | |
9913 | if (thread_guard) |
9914 | { |
9915 | tree vfntype = NULL_TREE; |
9916 | tree acquire_name, release_name, abort_name; |
9917 | tree acquire_fn, release_fn, abort_fn; |
9918 | guard_addr = build_address (guard); |
9919 | |
9920 | acquire_name = get_identifier ("__cxa_guard_acquire" ); |
9921 | release_name = get_identifier ("__cxa_guard_release" ); |
9922 | abort_name = get_identifier ("__cxa_guard_abort" ); |
9923 | acquire_fn = get_global_binding (id: acquire_name); |
9924 | release_fn = get_global_binding (id: release_name); |
9925 | abort_fn = get_global_binding (id: abort_name); |
9926 | if (!acquire_fn) |
9927 | acquire_fn = push_library_fn |
9928 | (name: acquire_name, type: build_function_type_list (integer_type_node, |
9929 | TREE_TYPE (guard_addr), |
9930 | NULL_TREE), |
9931 | NULL_TREE, ECF_NOTHROW); |
9932 | if (!release_fn || !abort_fn) |
9933 | vfntype = build_function_type_list (void_type_node, |
9934 | TREE_TYPE (guard_addr), |
9935 | NULL_TREE); |
9936 | if (!release_fn) |
9937 | release_fn = push_library_fn (name: release_name, type: vfntype, NULL_TREE, |
9938 | ECF_NOTHROW); |
9939 | if (!abort_fn) |
9940 | abort_fn = push_library_fn (name: abort_name, type: vfntype, NULL_TREE, |
9941 | ECF_NOTHROW | ECF_LEAF); |
9942 | |
9943 | inner_if_stmt = begin_if_stmt (); |
9944 | finish_if_stmt_cond (build_call_n (acquire_fn, 1, guard_addr), |
9945 | inner_if_stmt); |
9946 | |
9947 | inner_then_clause = begin_compound_stmt (BCS_NO_SCOPE); |
9948 | begin = get_target_expr (boolean_false_node); |
9949 | flag = TARGET_EXPR_SLOT (begin); |
9950 | |
9951 | TARGET_EXPR_CLEANUP (begin) |
9952 | = build3 (COND_EXPR, void_type_node, flag, |
9953 | void_node, |
9954 | build_call_n (abort_fn, 1, guard_addr)); |
9955 | CLEANUP_EH_ONLY (begin) = 1; |
9956 | |
9957 | /* Do the initialization itself. */ |
9958 | init = add_stmt_to_compound (begin, init); |
9959 | init = add_stmt_to_compound (init, |
9960 | build2 (MODIFY_EXPR, void_type_node, |
9961 | flag, boolean_true_node)); |
9962 | |
9963 | /* Use atexit to register a function for destroying this static |
9964 | variable. Do this before calling __cxa_guard_release. */ |
9965 | init = add_stmt_to_compound (init, register_dtor_fn (decl)); |
9966 | |
9967 | init = add_stmt_to_compound (init, build_call_n (release_fn, 1, |
9968 | guard_addr)); |
9969 | } |
9970 | else |
9971 | { |
9972 | init = add_stmt_to_compound (init, set_guard (guard)); |
9973 | |
9974 | /* Use atexit to register a function for destroying this static |
9975 | variable. */ |
9976 | init = add_stmt_to_compound (init, register_dtor_fn (decl)); |
9977 | } |
9978 | |
9979 | finish_expr_stmt (init); |
9980 | |
9981 | if (thread_guard) |
9982 | { |
9983 | finish_compound_stmt (inner_then_clause); |
9984 | finish_then_clause (inner_if_stmt); |
9985 | finish_if_stmt (inner_if_stmt); |
9986 | } |
9987 | |
9988 | finish_compound_stmt (then_clause); |
9989 | finish_then_clause (if_stmt); |
9990 | finish_if_stmt (if_stmt); |
9991 | } |
9992 | else if (CP_DECL_THREAD_LOCAL_P (decl)) |
9993 | tls_aggregates = tree_cons (init, decl, tls_aggregates); |
9994 | else |
9995 | static_aggregates = tree_cons (init, decl, static_aggregates); |
9996 | } |
9997 | |
9998 | |
9999 | /* Make TYPE a complete type based on INITIAL_VALUE. |
10000 | Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered, |
10001 | 2 if there was no information (in which case assume 0 if DO_DEFAULT), |
10002 | 3 if the initializer list is empty (in pedantic mode). */ |
10003 | |
10004 | int |
10005 | cp_complete_array_type (tree *ptype, tree initial_value, bool do_default) |
10006 | { |
10007 | int failure; |
10008 | tree type, elt_type; |
10009 | |
10010 | /* Don't get confused by a CONSTRUCTOR for some other type. */ |
10011 | if (initial_value && TREE_CODE (initial_value) == CONSTRUCTOR |
10012 | && !BRACE_ENCLOSED_INITIALIZER_P (initial_value) |
10013 | && TREE_CODE (TREE_TYPE (initial_value)) != ARRAY_TYPE) |
10014 | return 1; |
10015 | |
10016 | if (initial_value) |
10017 | { |
10018 | /* An array of character type can be initialized from a |
10019 | brace-enclosed string constant so call reshape_init to |
10020 | remove the optional braces from a braced string literal. */ |
10021 | if (char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (*ptype))) |
10022 | && BRACE_ENCLOSED_INITIALIZER_P (initial_value)) |
10023 | initial_value = reshape_init (type: *ptype, init: initial_value, |
10024 | complain: tf_warning_or_error); |
10025 | |
10026 | /* If any of the elements are parameter packs, we can't actually |
10027 | complete this type now because the array size is dependent. */ |
10028 | if (TREE_CODE (initial_value) == CONSTRUCTOR) |
10029 | for (auto &e: CONSTRUCTOR_ELTS (initial_value)) |
10030 | if (PACK_EXPANSION_P (e.value)) |
10031 | return 0; |
10032 | } |
10033 | |
10034 | failure = complete_array_type (ptype, initial_value, do_default); |
10035 | |
10036 | /* We can create the array before the element type is complete, which |
10037 | means that we didn't have these two bits set in the original type |
10038 | either. In completing the type, we are expected to propagate these |
10039 | bits. See also complete_type which does the same thing for arrays |
10040 | of fixed size. */ |
10041 | type = *ptype; |
10042 | if (type != error_mark_node && TYPE_DOMAIN (type)) |
10043 | { |
10044 | elt_type = TREE_TYPE (type); |
10045 | TYPE_NEEDS_CONSTRUCTING (type) = TYPE_NEEDS_CONSTRUCTING (elt_type); |
10046 | TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type) |
10047 | = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (elt_type); |
10048 | } |
10049 | |
10050 | return failure; |
10051 | } |
10052 | |
10053 | /* As above, but either give an error or reject zero-size arrays, depending |
10054 | on COMPLAIN. */ |
10055 | |
10056 | int |
10057 | cp_complete_array_type_or_error (tree *ptype, tree initial_value, |
10058 | bool do_default, tsubst_flags_t complain) |
10059 | { |
10060 | int failure; |
10061 | bool sfinae = !(complain & tf_error); |
10062 | /* In SFINAE context we can't be lenient about zero-size arrays. */ |
10063 | if (sfinae) |
10064 | ++pedantic; |
10065 | failure = cp_complete_array_type (ptype, initial_value, do_default); |
10066 | if (sfinae) |
10067 | --pedantic; |
10068 | if (failure) |
10069 | { |
10070 | if (sfinae) |
10071 | /* Not an error. */; |
10072 | else if (failure == 1) |
10073 | error ("initializer fails to determine size of %qT" , *ptype); |
10074 | else if (failure == 2) |
10075 | { |
10076 | if (do_default) |
10077 | error ("array size missing in %qT" , *ptype); |
10078 | } |
10079 | else if (failure == 3) |
10080 | error ("zero-size array %qT" , *ptype); |
10081 | *ptype = error_mark_node; |
10082 | } |
10083 | return failure; |
10084 | } |
10085 | |
10086 | /* Return zero if something is declared to be a member of type |
10087 | CTYPE when in the context of CUR_TYPE. STRING is the error |
10088 | message to print in that case. Otherwise, quietly return 1. */ |
10089 | |
10090 | static int |
10091 | member_function_or_else (tree ctype, tree cur_type, enum overload_flags flags) |
10092 | { |
10093 | if (ctype && ctype != cur_type) |
10094 | { |
10095 | if (flags == DTOR_FLAG) |
10096 | error ("destructor for alien class %qT cannot be a member" , ctype); |
10097 | else |
10098 | error ("constructor for alien class %qT cannot be a member" , ctype); |
10099 | return 0; |
10100 | } |
10101 | return 1; |
10102 | } |
10103 | |
10104 | /* Subroutine of `grokdeclarator'. */ |
10105 | |
10106 | /* Generate errors possibly applicable for a given set of specifiers. |
10107 | This is for ARM $7.1.2. */ |
10108 | |
10109 | static void |
10110 | bad_specifiers (tree object, |
10111 | enum bad_spec_place type, |
10112 | int virtualp, |
10113 | int quals, |
10114 | int inlinep, |
10115 | int friendp, |
10116 | int raises, |
10117 | const location_t* locations) |
10118 | { |
10119 | switch (type) |
10120 | { |
10121 | case BSP_VAR: |
10122 | if (virtualp) |
10123 | error_at (locations[ds_virtual], |
10124 | "%qD declared as a %<virtual%> variable" , object); |
10125 | if (quals) |
10126 | error ("%<const%> and %<volatile%> function specifiers on " |
10127 | "%qD invalid in variable declaration" , object); |
10128 | break; |
10129 | case BSP_PARM: |
10130 | if (virtualp) |
10131 | error_at (locations[ds_virtual], |
10132 | "%qD declared as a %<virtual%> parameter" , object); |
10133 | if (inlinep) |
10134 | error_at (locations[ds_inline], |
10135 | "%qD declared as an %<inline%> parameter" , object); |
10136 | if (quals) |
10137 | error ("%<const%> and %<volatile%> function specifiers on " |
10138 | "%qD invalid in parameter declaration" , object); |
10139 | break; |
10140 | case BSP_TYPE: |
10141 | if (virtualp) |
10142 | error_at (locations[ds_virtual], |
10143 | "%qD declared as a %<virtual%> type" , object); |
10144 | if (inlinep) |
10145 | error_at (locations[ds_inline], |
10146 | "%qD declared as an %<inline%> type" , object); |
10147 | if (quals) |
10148 | error ("%<const%> and %<volatile%> function specifiers on " |
10149 | "%qD invalid in type declaration" , object); |
10150 | break; |
10151 | case BSP_FIELD: |
10152 | if (virtualp) |
10153 | error_at (locations[ds_virtual], |
10154 | "%qD declared as a %<virtual%> field" , object); |
10155 | if (inlinep) |
10156 | error_at (locations[ds_inline], |
10157 | "%qD declared as an %<inline%> field" , object); |
10158 | if (quals) |
10159 | error ("%<const%> and %<volatile%> function specifiers on " |
10160 | "%qD invalid in field declaration" , object); |
10161 | break; |
10162 | default: |
10163 | gcc_unreachable(); |
10164 | } |
10165 | if (friendp) |
10166 | error ("%q+D declared as a friend" , object); |
10167 | if (raises |
10168 | && !flag_noexcept_type |
10169 | && (TREE_CODE (object) == TYPE_DECL |
10170 | || (!TYPE_PTRFN_P (TREE_TYPE (object)) |
10171 | && !TYPE_REFFN_P (TREE_TYPE (object)) |
10172 | && !TYPE_PTRMEMFUNC_P (TREE_TYPE (object))))) |
10173 | error ("%q+D declared with an exception specification" , object); |
10174 | } |
10175 | |
10176 | /* DECL is a member function or static data member and is presently |
10177 | being defined. Check that the definition is taking place in a |
10178 | valid namespace. */ |
10179 | |
10180 | static void |
10181 | check_class_member_definition_namespace (tree decl) |
10182 | { |
10183 | /* These checks only apply to member functions and static data |
10184 | members. */ |
10185 | gcc_assert (VAR_OR_FUNCTION_DECL_P (decl)); |
10186 | /* We check for problems with specializations in pt.cc in |
10187 | check_specialization_namespace, where we can issue better |
10188 | diagnostics. */ |
10189 | if (processing_specialization) |
10190 | return; |
10191 | /* We check this in check_explicit_instantiation_namespace. */ |
10192 | if (processing_explicit_instantiation) |
10193 | return; |
10194 | /* [class.mfct] |
10195 | |
10196 | A member function definition that appears outside of the |
10197 | class definition shall appear in a namespace scope enclosing |
10198 | the class definition. |
10199 | |
10200 | [class.static.data] |
10201 | |
10202 | The definition for a static data member shall appear in a |
10203 | namespace scope enclosing the member's class definition. */ |
10204 | if (!is_ancestor (current_namespace, DECL_CONTEXT (decl))) |
10205 | permerror (input_location, "definition of %qD is not in namespace enclosing %qT" , |
10206 | decl, DECL_CONTEXT (decl)); |
10207 | } |
10208 | |
10209 | /* Build a PARM_DECL for the "this" parameter of FN. TYPE is the |
10210 | METHOD_TYPE for a non-static member function; QUALS are the |
10211 | cv-qualifiers that apply to the function. */ |
10212 | |
10213 | tree |
10214 | build_this_parm (tree fn, tree type, cp_cv_quals quals) |
10215 | { |
10216 | tree this_type; |
10217 | tree qual_type; |
10218 | tree parm; |
10219 | cp_cv_quals this_quals; |
10220 | |
10221 | if (CLASS_TYPE_P (type)) |
10222 | { |
10223 | this_type |
10224 | = cp_build_qualified_type (type, quals & ~TYPE_QUAL_RESTRICT); |
10225 | this_type = build_pointer_type (this_type); |
10226 | } |
10227 | else |
10228 | this_type = type_of_this_parm (fntype: type); |
10229 | /* The `this' parameter is implicitly `const'; it cannot be |
10230 | assigned to. */ |
10231 | this_quals = (quals & TYPE_QUAL_RESTRICT) | TYPE_QUAL_CONST; |
10232 | qual_type = cp_build_qualified_type (this_type, this_quals); |
10233 | parm = build_artificial_parm (fn, this_identifier, qual_type); |
10234 | cp_apply_type_quals_to_decl (this_quals, parm); |
10235 | return parm; |
10236 | } |
10237 | |
10238 | /* DECL is a static member function. Complain if it was declared |
10239 | with function-cv-quals. */ |
10240 | |
10241 | static void |
10242 | check_static_quals (tree decl, cp_cv_quals quals) |
10243 | { |
10244 | if (quals != TYPE_UNQUALIFIED) |
10245 | error ("static member function %q#D declared with type qualifiers" , |
10246 | decl); |
10247 | } |
10248 | |
10249 | // Check that FN takes no arguments and returns bool. |
10250 | static void |
10251 | check_concept_fn (tree fn) |
10252 | { |
10253 | // A constraint is nullary. |
10254 | if (DECL_ARGUMENTS (fn)) |
10255 | error_at (DECL_SOURCE_LOCATION (fn), |
10256 | "concept %q#D declared with function parameters" , fn); |
10257 | |
10258 | // The declared return type of the concept shall be bool, and |
10259 | // it shall not be deduced from it definition. |
10260 | tree type = TREE_TYPE (TREE_TYPE (fn)); |
10261 | if (is_auto (type)) |
10262 | error_at (DECL_SOURCE_LOCATION (fn), |
10263 | "concept %q#D declared with a deduced return type" , fn); |
10264 | else if (type != boolean_type_node) |
10265 | error_at (DECL_SOURCE_LOCATION (fn), |
10266 | "concept %q#D with non-%<bool%> return type %qT" , fn, type); |
10267 | } |
10268 | |
10269 | /* Helper function. Replace the temporary this parameter injected |
10270 | during cp_finish_omp_declare_simd with the real this parameter. */ |
10271 | |
10272 | static tree |
10273 | declare_simd_adjust_this (tree *tp, int *walk_subtrees, void *data) |
10274 | { |
10275 | tree this_parm = (tree) data; |
10276 | if (TREE_CODE (*tp) == PARM_DECL |
10277 | && DECL_NAME (*tp) == this_identifier |
10278 | && *tp != this_parm) |
10279 | *tp = this_parm; |
10280 | else if (TYPE_P (*tp)) |
10281 | *walk_subtrees = 0; |
10282 | return NULL_TREE; |
10283 | } |
10284 | |
10285 | /* CTYPE is class type, or null if non-class. |
10286 | TYPE is type this FUNCTION_DECL should have, either FUNCTION_TYPE |
10287 | or METHOD_TYPE. |
10288 | DECLARATOR is the function's name. |
10289 | PARMS is a chain of PARM_DECLs for the function. |
10290 | VIRTUALP is truthvalue of whether the function is virtual or not. |
10291 | FLAGS are to be passed through to `grokclassfn'. |
10292 | QUALS are qualifiers indicating whether the function is `const' |
10293 | or `volatile'. |
10294 | RAISES is a list of exceptions that this function can raise. |
10295 | CHECK is 1 if we must find this method in CTYPE, 0 if we should |
10296 | not look, and -1 if we should not call `grokclassfn' at all. |
10297 | |
10298 | SFK is the kind of special function (if any) for the new function. |
10299 | |
10300 | Returns `NULL_TREE' if something goes wrong, after issuing |
10301 | applicable error messages. */ |
10302 | |
10303 | static tree |
10304 | grokfndecl (tree ctype, |
10305 | tree type, |
10306 | tree declarator, |
10307 | tree parms, |
10308 | tree orig_declarator, |
10309 | const cp_decl_specifier_seq *declspecs, |
10310 | tree decl_reqs, |
10311 | int virtualp, |
10312 | enum overload_flags flags, |
10313 | cp_cv_quals quals, |
10314 | cp_ref_qualifier rqual, |
10315 | tree raises, |
10316 | int check, |
10317 | int friendp, |
10318 | int publicp, |
10319 | int inlinep, |
10320 | bool deletedp, |
10321 | special_function_kind sfk, |
10322 | bool funcdef_flag, |
10323 | bool late_return_type_p, |
10324 | int template_count, |
10325 | tree in_namespace, |
10326 | tree* attrlist, |
10327 | location_t location) |
10328 | { |
10329 | tree decl; |
10330 | int staticp = ctype && TREE_CODE (type) == FUNCTION_TYPE; |
10331 | tree t; |
10332 | |
10333 | if (location == UNKNOWN_LOCATION) |
10334 | location = input_location; |
10335 | |
10336 | /* Was the concept specifier present? */ |
10337 | bool concept_p = inlinep & 4; |
10338 | |
10339 | /* Concept declarations must have a corresponding definition. */ |
10340 | if (concept_p && !funcdef_flag) |
10341 | { |
10342 | error_at (location, "concept %qD has no definition" , declarator); |
10343 | return NULL_TREE; |
10344 | } |
10345 | |
10346 | type = build_cp_fntype_variant (type, rqual, raises, late_return_type_p); |
10347 | |
10348 | decl = build_lang_decl_loc (location, FUNCTION_DECL, declarator, type); |
10349 | |
10350 | /* Set the constraints on the declaration. */ |
10351 | if (flag_concepts) |
10352 | { |
10353 | tree tmpl_reqs = NULL_TREE; |
10354 | tree ctx = friendp ? current_class_type : ctype; |
10355 | bool block_local = TREE_CODE (current_scope ()) == FUNCTION_DECL; |
10356 | bool memtmpl = (!block_local |
10357 | && (current_template_depth |
10358 | > template_class_depth (ctx))); |
10359 | if (memtmpl) |
10360 | { |
10361 | if (!current_template_parms) |
10362 | /* If there are no template parameters, something must have |
10363 | gone wrong. */ |
10364 | gcc_assert (seen_error ()); |
10365 | else |
10366 | tmpl_reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms); |
10367 | } |
10368 | tree ci = build_constraints (tmpl_reqs, decl_reqs); |
10369 | if (concept_p && ci) |
10370 | { |
10371 | error_at (location, "a function concept cannot be constrained" ); |
10372 | ci = NULL_TREE; |
10373 | } |
10374 | /* C++20 CA378: Remove non-templated constrained functions. */ |
10375 | /* [temp.friend]/9 A non-template friend declaration with a |
10376 | requires-clause shall be a definition. A friend function template with |
10377 | a constraint that depends on a template parameter from an enclosing |
10378 | template shall be a definition. */ |
10379 | if (ci |
10380 | && (block_local |
10381 | || (!flag_concepts_ts |
10382 | && (!processing_template_decl |
10383 | || (friendp && !memtmpl && !funcdef_flag))))) |
10384 | { |
10385 | if (!friendp || !processing_template_decl) |
10386 | error_at (location, "constraints on a non-templated function" ); |
10387 | else |
10388 | error_at (location, "constrained non-template friend declaration" |
10389 | " must be a definition" ); |
10390 | ci = NULL_TREE; |
10391 | } |
10392 | set_constraints (decl, ci); |
10393 | if (ci && friendp && memtmpl && !funcdef_flag |
10394 | && uses_outer_template_parms_in_constraints (decl, ctx)) |
10395 | error_at (location, "friend function template with constraints that " |
10396 | "depend on outer template parameters must be a definition" ); |
10397 | } |
10398 | |
10399 | if (TREE_CODE (type) == METHOD_TYPE) |
10400 | { |
10401 | tree parm = build_this_parm (fn: decl, type, quals); |
10402 | DECL_CHAIN (parm) = parms; |
10403 | parms = parm; |
10404 | |
10405 | /* Allocate space to hold the vptr bit if needed. */ |
10406 | SET_DECL_ALIGN (decl, MINIMUM_METHOD_BOUNDARY); |
10407 | } |
10408 | |
10409 | DECL_ARGUMENTS (decl) = parms; |
10410 | for (t = parms; t; t = DECL_CHAIN (t)) |
10411 | DECL_CONTEXT (t) = decl; |
10412 | |
10413 | /* Propagate volatile out from type to decl. */ |
10414 | if (TYPE_VOLATILE (type)) |
10415 | TREE_THIS_VOLATILE (decl) = 1; |
10416 | |
10417 | /* Setup decl according to sfk. */ |
10418 | switch (sfk) |
10419 | { |
10420 | case sfk_constructor: |
10421 | case sfk_copy_constructor: |
10422 | case sfk_move_constructor: |
10423 | DECL_CXX_CONSTRUCTOR_P (decl) = 1; |
10424 | DECL_NAME (decl) = ctor_identifier; |
10425 | break; |
10426 | case sfk_destructor: |
10427 | DECL_CXX_DESTRUCTOR_P (decl) = 1; |
10428 | DECL_NAME (decl) = dtor_identifier; |
10429 | break; |
10430 | case sfk_deduction_guide: |
10431 | /* Give deduction guides a definition even though they don't really |
10432 | have one: the restriction that you can't repeat a deduction guide |
10433 | makes them more like a definition anyway. */ |
10434 | DECL_INITIAL (decl) = void_node; |
10435 | break; |
10436 | default: |
10437 | break; |
10438 | } |
10439 | |
10440 | if (friendp && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR) |
10441 | { |
10442 | if (funcdef_flag) |
10443 | error_at (location, |
10444 | "defining explicit specialization %qD in friend declaration" , |
10445 | orig_declarator); |
10446 | else |
10447 | { |
10448 | tree fns = TREE_OPERAND (orig_declarator, 0); |
10449 | tree args = TREE_OPERAND (orig_declarator, 1); |
10450 | |
10451 | if (PROCESSING_REAL_TEMPLATE_DECL_P ()) |
10452 | { |
10453 | /* Something like `template <class T> friend void f<T>()'. */ |
10454 | error_at (location, |
10455 | "invalid use of template-id %qD in declaration " |
10456 | "of primary template" , |
10457 | orig_declarator); |
10458 | return NULL_TREE; |
10459 | } |
10460 | |
10461 | /* A friend declaration of the form friend void f<>(). Record |
10462 | the information in the TEMPLATE_ID_EXPR. */ |
10463 | SET_DECL_IMPLICIT_INSTANTIATION (decl); |
10464 | |
10465 | gcc_assert (identifier_p (fns) || OVL_P (fns)); |
10466 | DECL_TEMPLATE_INFO (decl) = build_template_info (fns, args); |
10467 | |
10468 | for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t)) |
10469 | if (TREE_PURPOSE (t) |
10470 | && TREE_CODE (TREE_PURPOSE (t)) == DEFERRED_PARSE) |
10471 | { |
10472 | error_at (defparse_location (TREE_PURPOSE (t)), |
10473 | "default arguments are not allowed in declaration " |
10474 | "of friend template specialization %qD" , |
10475 | decl); |
10476 | return NULL_TREE; |
10477 | } |
10478 | |
10479 | if (inlinep & 1) |
10480 | { |
10481 | error_at (declspecs->locations[ds_inline], |
10482 | "%<inline%> is not allowed in declaration of friend " |
10483 | "template specialization %qD" , |
10484 | decl); |
10485 | return NULL_TREE; |
10486 | } |
10487 | } |
10488 | } |
10489 | |
10490 | /* C++17 11.3.6/4: "If a friend declaration specifies a default argument |
10491 | expression, that declaration shall be a definition..." */ |
10492 | if (friendp && !funcdef_flag) |
10493 | { |
10494 | for (tree t = FUNCTION_FIRST_USER_PARMTYPE (decl); |
10495 | t && t != void_list_node; t = TREE_CHAIN (t)) |
10496 | if (TREE_PURPOSE (t)) |
10497 | { |
10498 | permerror (DECL_SOURCE_LOCATION (decl), |
10499 | "friend declaration of %qD specifies default " |
10500 | "arguments and isn%'t a definition" , decl); |
10501 | break; |
10502 | } |
10503 | } |
10504 | |
10505 | /* If this decl has namespace scope, set that up. */ |
10506 | if (in_namespace) |
10507 | set_decl_namespace (decl, in_namespace, friendp); |
10508 | else if (ctype) |
10509 | DECL_CONTEXT (decl) = ctype; |
10510 | else |
10511 | DECL_CONTEXT (decl) = FROB_CONTEXT (current_decl_namespace ()); |
10512 | |
10513 | /* `main' and builtins have implicit 'C' linkage. */ |
10514 | if (ctype == NULL_TREE |
10515 | && DECL_FILE_SCOPE_P (decl) |
10516 | && current_lang_name == lang_name_cplusplus |
10517 | && (MAIN_NAME_P (declarator) |
10518 | || (IDENTIFIER_LENGTH (declarator) > 10 |
10519 | && IDENTIFIER_POINTER (declarator)[0] == '_' |
10520 | && IDENTIFIER_POINTER (declarator)[1] == '_' |
10521 | && startswith (IDENTIFIER_POINTER (declarator) + 2, |
10522 | prefix: "builtin_" )) |
10523 | || (targetcm.cxx_implicit_extern_c |
10524 | && (targetcm.cxx_implicit_extern_c |
10525 | (IDENTIFIER_POINTER (declarator)))))) |
10526 | SET_DECL_LANGUAGE (decl, lang_c); |
10527 | |
10528 | /* Should probably propagate const out from type to decl I bet (mrs). */ |
10529 | if (staticp) |
10530 | { |
10531 | DECL_STATIC_FUNCTION_P (decl) = 1; |
10532 | DECL_CONTEXT (decl) = ctype; |
10533 | } |
10534 | |
10535 | if (deletedp) |
10536 | DECL_DELETED_FN (decl) = 1; |
10537 | |
10538 | if (ctype && funcdef_flag) |
10539 | check_class_member_definition_namespace (decl); |
10540 | |
10541 | if (ctype == NULL_TREE && DECL_MAIN_P (decl)) |
10542 | { |
10543 | if (PROCESSING_REAL_TEMPLATE_DECL_P()) |
10544 | error_at (location, "cannot declare %<::main%> to be a template" ); |
10545 | if (inlinep & 1) |
10546 | error_at (declspecs->locations[ds_inline], |
10547 | "cannot declare %<::main%> to be inline" ); |
10548 | if (inlinep & 2) |
10549 | error_at (declspecs->locations[ds_constexpr], |
10550 | "cannot declare %<::main%> to be %qs" , "constexpr" ); |
10551 | if (inlinep & 8) |
10552 | error_at (declspecs->locations[ds_consteval], |
10553 | "cannot declare %<::main%> to be %qs" , "consteval" ); |
10554 | if (!publicp) |
10555 | error_at (location, "cannot declare %<::main%> to be static" ); |
10556 | inlinep = 0; |
10557 | publicp = 1; |
10558 | } |
10559 | |
10560 | /* Members of anonymous types and local classes have no linkage; make |
10561 | them internal. If a typedef is made later, this will be changed. */ |
10562 | if (ctype && (!TREE_PUBLIC (TYPE_MAIN_DECL (ctype)) |
10563 | || decl_function_context (TYPE_MAIN_DECL (ctype)))) |
10564 | publicp = 0; |
10565 | |
10566 | if (publicp && cxx_dialect == cxx98) |
10567 | { |
10568 | /* [basic.link]: A name with no linkage (notably, the name of a class |
10569 | or enumeration declared in a local scope) shall not be used to |
10570 | declare an entity with linkage. |
10571 | |
10572 | DR 757 relaxes this restriction for C++0x. */ |
10573 | no_linkage_error (decl); |
10574 | } |
10575 | |
10576 | TREE_PUBLIC (decl) = publicp; |
10577 | if (! publicp) |
10578 | { |
10579 | DECL_INTERFACE_KNOWN (decl) = 1; |
10580 | DECL_NOT_REALLY_EXTERN (decl) = 1; |
10581 | } |
10582 | |
10583 | /* If the declaration was declared inline, mark it as such. */ |
10584 | if (inlinep) |
10585 | { |
10586 | DECL_DECLARED_INLINE_P (decl) = 1; |
10587 | if (publicp) |
10588 | DECL_COMDAT (decl) = 1; |
10589 | } |
10590 | if (inlinep & 2) |
10591 | DECL_DECLARED_CONSTEXPR_P (decl) = true; |
10592 | else if (inlinep & 8) |
10593 | { |
10594 | DECL_DECLARED_CONSTEXPR_P (decl) = true; |
10595 | SET_DECL_IMMEDIATE_FUNCTION_P (decl); |
10596 | } |
10597 | |
10598 | // If the concept declaration specifier was found, check |
10599 | // that the declaration satisfies the necessary requirements. |
10600 | if (concept_p) |
10601 | { |
10602 | DECL_DECLARED_CONCEPT_P (decl) = true; |
10603 | check_concept_fn (fn: decl); |
10604 | } |
10605 | |
10606 | DECL_EXTERNAL (decl) = 1; |
10607 | if (TREE_CODE (type) == FUNCTION_TYPE) |
10608 | { |
10609 | if (quals || rqual) |
10610 | TREE_TYPE (decl) = apply_memfn_quals (TREE_TYPE (decl), |
10611 | TYPE_UNQUALIFIED, |
10612 | REF_QUAL_NONE); |
10613 | |
10614 | if (quals) |
10615 | { |
10616 | error (ctype |
10617 | ? G_("static member function %qD cannot have cv-qualifier" ) |
10618 | : G_("non-member function %qD cannot have cv-qualifier" ), |
10619 | decl); |
10620 | quals = TYPE_UNQUALIFIED; |
10621 | } |
10622 | |
10623 | if (rqual) |
10624 | { |
10625 | error (ctype |
10626 | ? G_("static member function %qD cannot have ref-qualifier" ) |
10627 | : G_("non-member function %qD cannot have ref-qualifier" ), |
10628 | decl); |
10629 | rqual = REF_QUAL_NONE; |
10630 | } |
10631 | } |
10632 | |
10633 | if (deduction_guide_p (decl)) |
10634 | { |
10635 | tree type = TREE_TYPE (DECL_NAME (decl)); |
10636 | if (in_namespace == NULL_TREE |
10637 | && CP_DECL_CONTEXT (decl) != CP_TYPE_CONTEXT (type)) |
10638 | { |
10639 | error_at (location, "deduction guide %qD must be declared in the " |
10640 | "same scope as %qT" , decl, type); |
10641 | inform (location_of (type), " declared here" ); |
10642 | return NULL_TREE; |
10643 | } |
10644 | if (DECL_CLASS_SCOPE_P (decl) |
10645 | && current_access_specifier != declared_access (TYPE_NAME (type))) |
10646 | { |
10647 | error_at (location, "deduction guide %qD must have the same access " |
10648 | "as %qT" , decl, type); |
10649 | inform (location_of (type), " declared here" ); |
10650 | } |
10651 | if (funcdef_flag) |
10652 | error_at (location, |
10653 | "deduction guide %qD must not have a function body" , decl); |
10654 | } |
10655 | else if (IDENTIFIER_ANY_OP_P (DECL_NAME (decl)) |
10656 | && !grok_op_properties (decl, /*complain=*/true)) |
10657 | return NULL_TREE; |
10658 | else if (UDLIT_OPER_P (DECL_NAME (decl))) |
10659 | { |
10660 | bool long_long_unsigned_p; |
10661 | bool long_double_p; |
10662 | const char *suffix = NULL; |
10663 | /* [over.literal]/6: Literal operators shall not have C linkage. */ |
10664 | if (DECL_LANGUAGE (decl) == lang_c) |
10665 | { |
10666 | error_at (location, "literal operator with C linkage" ); |
10667 | maybe_show_extern_c_location (); |
10668 | return NULL_TREE; |
10669 | } |
10670 | |
10671 | if (DECL_NAMESPACE_SCOPE_P (decl)) |
10672 | { |
10673 | if (!check_literal_operator_args (decl, &long_long_unsigned_p, |
10674 | &long_double_p)) |
10675 | { |
10676 | error_at (location, "%qD has invalid argument list" , decl); |
10677 | return NULL_TREE; |
10678 | } |
10679 | |
10680 | suffix = UDLIT_OP_SUFFIX (DECL_NAME (decl)); |
10681 | if (long_long_unsigned_p) |
10682 | { |
10683 | if (cpp_interpret_int_suffix (parse_in, suffix, strlen (s: suffix))) |
10684 | warning_at (location, 0, "integer suffix %qs" |
10685 | " shadowed by implementation" , suffix); |
10686 | } |
10687 | else if (long_double_p) |
10688 | { |
10689 | if (cpp_interpret_float_suffix (parse_in, suffix, strlen (s: suffix))) |
10690 | warning_at (location, 0, "floating-point suffix %qs" |
10691 | " shadowed by implementation" , suffix); |
10692 | } |
10693 | /* 17.6.3.3.5 */ |
10694 | if (suffix[0] != '_' |
10695 | && !current_function_decl && !(friendp && !funcdef_flag)) |
10696 | warning_at (location, OPT_Wliteral_suffix, |
10697 | "literal operator suffixes not preceded by %<_%>" |
10698 | " are reserved for future standardization" ); |
10699 | } |
10700 | else |
10701 | { |
10702 | error_at (location, "%qD must be a non-member function" , decl); |
10703 | return NULL_TREE; |
10704 | } |
10705 | } |
10706 | |
10707 | if (funcdef_flag) |
10708 | /* Make the init_value nonzero so pushdecl knows this is not |
10709 | tentative. error_mark_node is replaced later with the BLOCK. */ |
10710 | DECL_INITIAL (decl) = error_mark_node; |
10711 | |
10712 | if (TYPE_NOTHROW_P (type) || nothrow_libfn_p (decl)) |
10713 | TREE_NOTHROW (decl) = 1; |
10714 | |
10715 | if (flag_openmp || flag_openmp_simd) |
10716 | { |
10717 | /* Adjust "omp declare simd" attributes. */ |
10718 | tree ods = lookup_attribute (attr_name: "omp declare simd" , list: *attrlist); |
10719 | if (ods) |
10720 | { |
10721 | tree attr; |
10722 | for (attr = ods; attr; |
10723 | attr = lookup_attribute (attr_name: "omp declare simd" , TREE_CHAIN (attr))) |
10724 | { |
10725 | if (TREE_CODE (type) == METHOD_TYPE) |
10726 | walk_tree (&TREE_VALUE (attr), declare_simd_adjust_this, |
10727 | DECL_ARGUMENTS (decl), NULL); |
10728 | if (TREE_VALUE (attr) != NULL_TREE) |
10729 | { |
10730 | tree cl = TREE_VALUE (TREE_VALUE (attr)); |
10731 | cl = c_omp_declare_simd_clauses_to_numbers |
10732 | (DECL_ARGUMENTS (decl), cl); |
10733 | if (cl) |
10734 | TREE_VALUE (TREE_VALUE (attr)) = cl; |
10735 | else |
10736 | TREE_VALUE (attr) = NULL_TREE; |
10737 | } |
10738 | } |
10739 | } |
10740 | } |
10741 | |
10742 | /* Caller will do the rest of this. */ |
10743 | if (check < 0) |
10744 | return decl; |
10745 | |
10746 | if (ctype != NULL_TREE) |
10747 | grokclassfn (ctype, decl, flags); |
10748 | |
10749 | /* 12.4/3 */ |
10750 | if (cxx_dialect >= cxx11 |
10751 | && DECL_DESTRUCTOR_P (decl) |
10752 | && !TYPE_BEING_DEFINED (DECL_CONTEXT (decl)) |
10753 | && !processing_template_decl) |
10754 | deduce_noexcept_on_destructor (decl); |
10755 | |
10756 | set_originating_module (decl); |
10757 | |
10758 | decl = check_explicit_specialization (orig_declarator, decl, |
10759 | template_count, |
10760 | 2 * funcdef_flag + |
10761 | 4 * (friendp != 0) + |
10762 | 8 * concept_p, |
10763 | *attrlist); |
10764 | if (decl == error_mark_node) |
10765 | return NULL_TREE; |
10766 | |
10767 | if (DECL_STATIC_FUNCTION_P (decl)) |
10768 | check_static_quals (decl, quals); |
10769 | |
10770 | if (attrlist) |
10771 | { |
10772 | cplus_decl_attributes (&decl, *attrlist, 0); |
10773 | *attrlist = NULL_TREE; |
10774 | } |
10775 | |
10776 | if (DECL_HAS_CONTRACTS_P (decl)) |
10777 | rebuild_postconditions (decl); |
10778 | |
10779 | /* Check main's type after attributes have been applied. */ |
10780 | if (ctype == NULL_TREE && DECL_MAIN_P (decl)) |
10781 | { |
10782 | if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)), |
10783 | integer_type_node)) |
10784 | { |
10785 | tree oldtypeargs = TYPE_ARG_TYPES (TREE_TYPE (decl)); |
10786 | tree newtype; |
10787 | error_at (declspecs->locations[ds_type_spec], |
10788 | "%<::main%> must return %<int%>" ); |
10789 | newtype = build_function_type (integer_type_node, oldtypeargs); |
10790 | TREE_TYPE (decl) = newtype; |
10791 | } |
10792 | if (warn_main) |
10793 | check_main_parameter_types (decl); |
10794 | } |
10795 | |
10796 | if (ctype != NULL_TREE && check) |
10797 | { |
10798 | tree old_decl = check_classfn (ctype, decl, |
10799 | (current_template_depth |
10800 | > template_class_depth (ctype)) |
10801 | ? current_template_parms |
10802 | : NULL_TREE); |
10803 | |
10804 | if (old_decl == error_mark_node) |
10805 | return NULL_TREE; |
10806 | |
10807 | if (old_decl) |
10808 | { |
10809 | tree ok; |
10810 | tree pushed_scope; |
10811 | |
10812 | if (TREE_CODE (old_decl) == TEMPLATE_DECL) |
10813 | /* Because grokfndecl is always supposed to return a |
10814 | FUNCTION_DECL, we pull out the DECL_TEMPLATE_RESULT |
10815 | here. We depend on our callers to figure out that its |
10816 | really a template that's being returned. */ |
10817 | old_decl = DECL_TEMPLATE_RESULT (old_decl); |
10818 | |
10819 | if (DECL_STATIC_FUNCTION_P (old_decl) |
10820 | && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE) |
10821 | { |
10822 | /* Remove the `this' parm added by grokclassfn. */ |
10823 | revert_static_member_fn (decl); |
10824 | check_static_quals (decl, quals); |
10825 | } |
10826 | if (DECL_ARTIFICIAL (old_decl)) |
10827 | { |
10828 | error ("definition of implicitly-declared %qD" , old_decl); |
10829 | return NULL_TREE; |
10830 | } |
10831 | else if (DECL_DEFAULTED_FN (old_decl)) |
10832 | { |
10833 | error ("definition of explicitly-defaulted %q+D" , decl); |
10834 | inform (DECL_SOURCE_LOCATION (old_decl), |
10835 | "%q#D explicitly defaulted here" , old_decl); |
10836 | return NULL_TREE; |
10837 | } |
10838 | |
10839 | /* Since we've smashed OLD_DECL to its |
10840 | DECL_TEMPLATE_RESULT, we must do the same to DECL. */ |
10841 | if (TREE_CODE (decl) == TEMPLATE_DECL) |
10842 | decl = DECL_TEMPLATE_RESULT (decl); |
10843 | |
10844 | /* Attempt to merge the declarations. This can fail, in |
10845 | the case of some invalid specialization declarations. */ |
10846 | pushed_scope = push_scope (ctype); |
10847 | ok = duplicate_decls (newdecl: decl, olddecl: old_decl); |
10848 | if (pushed_scope) |
10849 | pop_scope (pushed_scope); |
10850 | if (!ok) |
10851 | { |
10852 | error ("no %q#D member function declared in class %qT" , |
10853 | decl, ctype); |
10854 | return NULL_TREE; |
10855 | } |
10856 | if (ok == error_mark_node) |
10857 | return NULL_TREE; |
10858 | return old_decl; |
10859 | } |
10860 | } |
10861 | |
10862 | if (DECL_CONSTRUCTOR_P (decl) && !grok_ctor_properties (ctype, decl)) |
10863 | return NULL_TREE; |
10864 | |
10865 | if (ctype == NULL_TREE || check) |
10866 | return decl; |
10867 | |
10868 | if (virtualp) |
10869 | DECL_VIRTUAL_P (decl) = 1; |
10870 | |
10871 | return decl; |
10872 | } |
10873 | |
10874 | /* decl is a FUNCTION_DECL. |
10875 | specifiers are the parsed virt-specifiers. |
10876 | |
10877 | Set flags to reflect the virt-specifiers. |
10878 | |
10879 | Returns decl. */ |
10880 | |
10881 | static tree |
10882 | set_virt_specifiers (tree decl, cp_virt_specifiers specifiers) |
10883 | { |
10884 | if (decl == NULL_TREE) |
10885 | return decl; |
10886 | if (specifiers & VIRT_SPEC_OVERRIDE) |
10887 | DECL_OVERRIDE_P (decl) = 1; |
10888 | if (specifiers & VIRT_SPEC_FINAL) |
10889 | DECL_FINAL_P (decl) = 1; |
10890 | return decl; |
10891 | } |
10892 | |
10893 | /* DECL is a VAR_DECL for a static data member. Set flags to reflect |
10894 | the linkage that DECL will receive in the object file. */ |
10895 | |
10896 | static void |
10897 | set_linkage_for_static_data_member (tree decl) |
10898 | { |
10899 | /* A static data member always has static storage duration and |
10900 | external linkage. Note that static data members are forbidden in |
10901 | local classes -- the only situation in which a class has |
10902 | non-external linkage. */ |
10903 | TREE_PUBLIC (decl) = 1; |
10904 | TREE_STATIC (decl) = 1; |
10905 | /* For non-template classes, static data members are always put |
10906 | out in exactly those files where they are defined, just as |
10907 | with ordinary namespace-scope variables. */ |
10908 | if (!processing_template_decl) |
10909 | DECL_INTERFACE_KNOWN (decl) = 1; |
10910 | } |
10911 | |
10912 | /* Create a VAR_DECL named NAME with the indicated TYPE. |
10913 | |
10914 | If SCOPE is non-NULL, it is the class type or namespace containing |
10915 | the variable. If SCOPE is NULL, the variable should is created in |
10916 | the innermost enclosing scope. */ |
10917 | |
10918 | static tree |
10919 | grokvardecl (tree type, |
10920 | tree name, |
10921 | tree orig_declarator, |
10922 | const cp_decl_specifier_seq *declspecs, |
10923 | int initialized, |
10924 | int type_quals, |
10925 | int inlinep, |
10926 | bool conceptp, |
10927 | int template_count, |
10928 | tree scope, |
10929 | location_t location) |
10930 | { |
10931 | tree decl; |
10932 | tree explicit_scope; |
10933 | |
10934 | gcc_assert (!name || identifier_p (name)); |
10935 | |
10936 | bool constp = (type_quals & TYPE_QUAL_CONST) != 0; |
10937 | bool volatilep = (type_quals & TYPE_QUAL_VOLATILE) != 0; |
10938 | |
10939 | /* Compute the scope in which to place the variable, but remember |
10940 | whether or not that scope was explicitly specified by the user. */ |
10941 | explicit_scope = scope; |
10942 | if (!scope) |
10943 | { |
10944 | /* An explicit "extern" specifier indicates a namespace-scope |
10945 | variable. */ |
10946 | if (declspecs->storage_class == sc_extern) |
10947 | scope = current_decl_namespace (); |
10948 | else if (!at_function_scope_p ()) |
10949 | scope = current_scope (); |
10950 | } |
10951 | |
10952 | if (scope |
10953 | && (/* If the variable is a namespace-scope variable declared in a |
10954 | template, we need DECL_LANG_SPECIFIC. */ |
10955 | (TREE_CODE (scope) == NAMESPACE_DECL && processing_template_decl) |
10956 | /* Similarly for namespace-scope variables with language linkage |
10957 | other than C++. */ |
10958 | || (TREE_CODE (scope) == NAMESPACE_DECL |
10959 | && current_lang_name != lang_name_cplusplus) |
10960 | /* Similarly for static data members. */ |
10961 | || TYPE_P (scope) |
10962 | /* Similarly for explicit specializations. */ |
10963 | || (orig_declarator |
10964 | && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR))) |
10965 | decl = build_lang_decl_loc (location, VAR_DECL, name, type); |
10966 | else |
10967 | decl = build_decl (location, VAR_DECL, name, type); |
10968 | |
10969 | if (explicit_scope && TREE_CODE (explicit_scope) == NAMESPACE_DECL) |
10970 | set_decl_namespace (decl, explicit_scope, 0); |
10971 | else |
10972 | DECL_CONTEXT (decl) = FROB_CONTEXT (scope); |
10973 | |
10974 | if (declspecs->storage_class == sc_extern) |
10975 | { |
10976 | DECL_THIS_EXTERN (decl) = 1; |
10977 | DECL_EXTERNAL (decl) = !initialized; |
10978 | } |
10979 | |
10980 | if (DECL_CLASS_SCOPE_P (decl)) |
10981 | { |
10982 | set_linkage_for_static_data_member (decl); |
10983 | /* This function is only called with out-of-class definitions. */ |
10984 | DECL_EXTERNAL (decl) = 0; |
10985 | check_class_member_definition_namespace (decl); |
10986 | } |
10987 | /* At top level, either `static' or no s.c. makes a definition |
10988 | (perhaps tentative), and absence of `static' makes it public. */ |
10989 | else if (toplevel_bindings_p ()) |
10990 | { |
10991 | TREE_PUBLIC (decl) = (declspecs->storage_class != sc_static |
10992 | && (DECL_THIS_EXTERN (decl) |
10993 | || ! constp |
10994 | || volatilep |
10995 | || inlinep)); |
10996 | TREE_STATIC (decl) = ! DECL_EXTERNAL (decl); |
10997 | } |
10998 | /* Not at top level, only `static' makes a static definition. */ |
10999 | else |
11000 | { |
11001 | TREE_STATIC (decl) = declspecs->storage_class == sc_static; |
11002 | TREE_PUBLIC (decl) = DECL_EXTERNAL (decl); |
11003 | } |
11004 | |
11005 | set_originating_module (decl); |
11006 | |
11007 | if (decl_spec_seq_has_spec_p (declspecs, ds_thread)) |
11008 | { |
11009 | if (DECL_EXTERNAL (decl) || TREE_STATIC (decl)) |
11010 | { |
11011 | CP_DECL_THREAD_LOCAL_P (decl) = true; |
11012 | if (!processing_template_decl) |
11013 | set_decl_tls_model (decl, decl_default_tls_model (decl)); |
11014 | } |
11015 | if (declspecs->gnu_thread_keyword_p) |
11016 | SET_DECL_GNU_TLS_P (decl); |
11017 | } |
11018 | |
11019 | /* If the type of the decl has no linkage, make sure that we'll |
11020 | notice that in mark_used. */ |
11021 | if (cxx_dialect > cxx98 |
11022 | && decl_linkage (decl) != lk_none |
11023 | && DECL_LANG_SPECIFIC (decl) == NULL |
11024 | && !DECL_EXTERN_C_P (decl) |
11025 | && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false)) |
11026 | retrofit_lang_decl (decl); |
11027 | |
11028 | if (TREE_PUBLIC (decl)) |
11029 | { |
11030 | /* [basic.link]: A name with no linkage (notably, the name of a class |
11031 | or enumeration declared in a local scope) shall not be used to |
11032 | declare an entity with linkage. |
11033 | |
11034 | DR 757 relaxes this restriction for C++0x. */ |
11035 | if (cxx_dialect < cxx11) |
11036 | no_linkage_error (decl); |
11037 | } |
11038 | else |
11039 | DECL_INTERFACE_KNOWN (decl) = 1; |
11040 | |
11041 | if (DECL_NAME (decl) |
11042 | && MAIN_NAME_P (DECL_NAME (decl)) |
11043 | && scope == global_namespace) |
11044 | error_at (DECL_SOURCE_LOCATION (decl), |
11045 | "cannot declare %<::main%> to be a global variable" ); |
11046 | |
11047 | /* Check that the variable can be safely declared as a concept. |
11048 | Note that this also forbids explicit specializations. */ |
11049 | if (conceptp) |
11050 | { |
11051 | if (!processing_template_decl) |
11052 | { |
11053 | error_at (declspecs->locations[ds_concept], |
11054 | "a non-template variable cannot be %<concept%>" ); |
11055 | return NULL_TREE; |
11056 | } |
11057 | else if (!at_namespace_scope_p ()) |
11058 | { |
11059 | error_at (declspecs->locations[ds_concept], |
11060 | "concept must be defined at namespace scope" ); |
11061 | return NULL_TREE; |
11062 | } |
11063 | else |
11064 | DECL_DECLARED_CONCEPT_P (decl) = true; |
11065 | if (!same_type_ignoring_top_level_qualifiers_p (type, boolean_type_node)) |
11066 | error_at (declspecs->locations[ds_type_spec], |
11067 | "concept must have type %<bool%>" ); |
11068 | if (TEMPLATE_PARMS_CONSTRAINTS (current_template_parms)) |
11069 | { |
11070 | error_at (location, "a variable concept cannot be constrained" ); |
11071 | TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = NULL_TREE; |
11072 | } |
11073 | } |
11074 | else if (flag_concepts |
11075 | && current_template_depth > template_class_depth (scope)) |
11076 | { |
11077 | tree ci = current_template_constraints (); |
11078 | set_constraints (decl, ci); |
11079 | } |
11080 | |
11081 | // Handle explicit specializations and instantiations of variable templates. |
11082 | if (orig_declarator) |
11083 | decl = check_explicit_specialization (orig_declarator, decl, |
11084 | template_count, conceptp * 8); |
11085 | |
11086 | return decl != error_mark_node ? decl : NULL_TREE; |
11087 | } |
11088 | |
11089 | /* Create and return a canonical pointer to member function type, for |
11090 | TYPE, which is a POINTER_TYPE to a METHOD_TYPE. */ |
11091 | |
11092 | tree |
11093 | build_ptrmemfunc_type (tree type) |
11094 | { |
11095 | tree field, fields; |
11096 | tree t; |
11097 | |
11098 | if (type == error_mark_node) |
11099 | return type; |
11100 | |
11101 | /* Make sure that we always have the unqualified pointer-to-member |
11102 | type first. */ |
11103 | if (cp_cv_quals quals = cp_type_quals (type)) |
11104 | { |
11105 | tree unqual = build_ptrmemfunc_type (TYPE_MAIN_VARIANT (type)); |
11106 | return cp_build_qualified_type (unqual, quals); |
11107 | } |
11108 | |
11109 | /* If a canonical type already exists for this type, use it. We use |
11110 | this method instead of type_hash_canon, because it only does a |
11111 | simple equality check on the list of field members. */ |
11112 | |
11113 | t = TYPE_PTRMEMFUNC_TYPE (type); |
11114 | if (t) |
11115 | return t; |
11116 | |
11117 | t = make_node (RECORD_TYPE); |
11118 | |
11119 | /* Let the front end know this is a pointer to member function. */ |
11120 | TYPE_PTRMEMFUNC_FLAG (t) = 1; |
11121 | |
11122 | field = build_decl (input_location, FIELD_DECL, pfn_identifier, type); |
11123 | DECL_NONADDRESSABLE_P (field) = 1; |
11124 | fields = field; |
11125 | |
11126 | field = build_decl (input_location, FIELD_DECL, delta_identifier, |
11127 | delta_type_node); |
11128 | DECL_NONADDRESSABLE_P (field) = 1; |
11129 | DECL_CHAIN (field) = fields; |
11130 | fields = field; |
11131 | |
11132 | finish_builtin_struct (t, "__ptrmemfunc_type" , fields, ptr_type_node); |
11133 | |
11134 | /* Zap out the name so that the back end will give us the debugging |
11135 | information for this anonymous RECORD_TYPE. */ |
11136 | TYPE_NAME (t) = NULL_TREE; |
11137 | |
11138 | /* Cache this pointer-to-member type so that we can find it again |
11139 | later. */ |
11140 | TYPE_PTRMEMFUNC_TYPE (type) = t; |
11141 | |
11142 | if (TYPE_STRUCTURAL_EQUALITY_P (type)) |
11143 | SET_TYPE_STRUCTURAL_EQUALITY (t); |
11144 | else if (TYPE_CANONICAL (type) != type) |
11145 | TYPE_CANONICAL (t) = build_ptrmemfunc_type (TYPE_CANONICAL (type)); |
11146 | |
11147 | return t; |
11148 | } |
11149 | |
11150 | /* Create and return a pointer to data member type. */ |
11151 | |
11152 | tree |
11153 | build_ptrmem_type (tree class_type, tree member_type) |
11154 | { |
11155 | if (TREE_CODE (member_type) == METHOD_TYPE) |
11156 | { |
11157 | cp_cv_quals quals = type_memfn_quals (member_type); |
11158 | cp_ref_qualifier rqual = type_memfn_rqual (member_type); |
11159 | member_type = build_memfn_type (member_type, class_type, quals, rqual); |
11160 | return build_ptrmemfunc_type (type: build_pointer_type (member_type)); |
11161 | } |
11162 | else |
11163 | { |
11164 | gcc_assert (TREE_CODE (member_type) != FUNCTION_TYPE); |
11165 | return build_offset_type (class_type, member_type); |
11166 | } |
11167 | } |
11168 | |
11169 | /* DECL is a VAR_DECL defined in-class, whose TYPE is also given. |
11170 | Check to see that the definition is valid. Issue appropriate error |
11171 | messages. */ |
11172 | |
11173 | static void |
11174 | check_static_variable_definition (tree decl, tree type) |
11175 | { |
11176 | /* Avoid redundant diagnostics on out-of-class definitions. */ |
11177 | if (!current_class_type || !TYPE_BEING_DEFINED (current_class_type)) |
11178 | ; |
11179 | /* Can't check yet if we don't know the type. */ |
11180 | else if (dependent_type_p (type)) |
11181 | ; |
11182 | /* If DECL is declared constexpr, we'll do the appropriate checks |
11183 | in check_initializer. Similarly for inline static data members. */ |
11184 | else if (DECL_P (decl) |
11185 | && (DECL_DECLARED_CONSTEXPR_P (decl) |
11186 | || DECL_VAR_DECLARED_INLINE_P (decl))) |
11187 | ; |
11188 | else if (cxx_dialect >= cxx11 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)) |
11189 | { |
11190 | if (!COMPLETE_TYPE_P (type)) |
11191 | error_at (DECL_SOURCE_LOCATION (decl), |
11192 | "in-class initialization of static data member %q#D of " |
11193 | "incomplete type" , decl); |
11194 | else if (literal_type_p (type)) |
11195 | permerror (DECL_SOURCE_LOCATION (decl), |
11196 | "%<constexpr%> needed for in-class initialization of " |
11197 | "static data member %q#D of non-integral type" , decl); |
11198 | else |
11199 | error_at (DECL_SOURCE_LOCATION (decl), |
11200 | "in-class initialization of static data member %q#D of " |
11201 | "non-literal type" , decl); |
11202 | } |
11203 | /* Motion 10 at San Diego: If a static const integral data member is |
11204 | initialized with an integral constant expression, the initializer |
11205 | may appear either in the declaration (within the class), or in |
11206 | the definition, but not both. If it appears in the class, the |
11207 | member is a member constant. The file-scope definition is always |
11208 | required. */ |
11209 | else if (!ARITHMETIC_TYPE_P (type) && TREE_CODE (type) != ENUMERAL_TYPE) |
11210 | error_at (DECL_SOURCE_LOCATION (decl), |
11211 | "invalid in-class initialization of static data member " |
11212 | "of non-integral type %qT" , |
11213 | type); |
11214 | else if (!CP_TYPE_CONST_P (type)) |
11215 | error_at (DECL_SOURCE_LOCATION (decl), |
11216 | "ISO C++ forbids in-class initialization of non-const " |
11217 | "static member %qD" , |
11218 | decl); |
11219 | else if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type)) |
11220 | pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic, |
11221 | "ISO C++ forbids initialization of member constant " |
11222 | "%qD of non-integral type %qT" , decl, type); |
11223 | } |
11224 | |
11225 | /* *expr_p is part of the TYPE_SIZE of a variably-sized array. If any |
11226 | SAVE_EXPRs in *expr_p wrap expressions with side-effects, break those |
11227 | expressions out into temporary variables so that walk_tree doesn't |
11228 | step into them (c++/15764). */ |
11229 | |
11230 | static tree |
11231 | stabilize_save_expr_r (tree *expr_p, int *walk_subtrees, void *data) |
11232 | { |
11233 | hash_set<tree> *pset = (hash_set<tree> *)data; |
11234 | tree expr = *expr_p; |
11235 | if (TREE_CODE (expr) == SAVE_EXPR) |
11236 | { |
11237 | tree op = TREE_OPERAND (expr, 0); |
11238 | cp_walk_tree (&op, stabilize_save_expr_r, data, pset); |
11239 | if (TREE_SIDE_EFFECTS (op)) |
11240 | TREE_OPERAND (expr, 0) = get_temp_regvar (TREE_TYPE (op), op); |
11241 | *walk_subtrees = 0; |
11242 | } |
11243 | else if (!EXPR_P (expr) || !TREE_SIDE_EFFECTS (expr)) |
11244 | *walk_subtrees = 0; |
11245 | return NULL; |
11246 | } |
11247 | |
11248 | /* Entry point for the above. */ |
11249 | |
11250 | static void |
11251 | stabilize_vla_size (tree size) |
11252 | { |
11253 | hash_set<tree> pset; |
11254 | /* Break out any function calls into temporary variables. */ |
11255 | cp_walk_tree (&size, stabilize_save_expr_r, &pset, &pset); |
11256 | } |
11257 | |
11258 | /* Reduce a SIZEOF_EXPR to its value. */ |
11259 | |
11260 | tree |
11261 | fold_sizeof_expr (tree t) |
11262 | { |
11263 | tree r; |
11264 | if (SIZEOF_EXPR_TYPE_P (t)) |
11265 | r = cxx_sizeof_or_alignof_type (EXPR_LOCATION (t), |
11266 | TREE_TYPE (TREE_OPERAND (t, 0)), |
11267 | SIZEOF_EXPR, false, false); |
11268 | else if (TYPE_P (TREE_OPERAND (t, 0))) |
11269 | r = cxx_sizeof_or_alignof_type (EXPR_LOCATION (t), |
11270 | TREE_OPERAND (t, 0), SIZEOF_EXPR, |
11271 | false, false); |
11272 | else |
11273 | r = cxx_sizeof_or_alignof_expr (EXPR_LOCATION (t), |
11274 | TREE_OPERAND (t, 0), SIZEOF_EXPR, |
11275 | false, false); |
11276 | if (r == error_mark_node) |
11277 | r = size_one_node; |
11278 | return r; |
11279 | } |
11280 | |
11281 | /* Given the SIZE (i.e., number of elements) in an array, compute |
11282 | an appropriate index type for the array. If non-NULL, NAME is |
11283 | the name of the entity being declared. */ |
11284 | |
11285 | static tree |
11286 | compute_array_index_type_loc (location_t name_loc, tree name, tree size, |
11287 | tsubst_flags_t complain) |
11288 | { |
11289 | if (error_operand_p (t: size)) |
11290 | return error_mark_node; |
11291 | |
11292 | /* The type of the index being computed. */ |
11293 | tree itype; |
11294 | |
11295 | /* The original numeric size as seen in the source code before |
11296 | conversion to size_t. */ |
11297 | tree origsize = size; |
11298 | |
11299 | location_t loc = cp_expr_loc_or_loc (t: size, or_loc: name ? name_loc : input_location); |
11300 | |
11301 | if (!type_dependent_expression_p (size)) |
11302 | { |
11303 | origsize = size = mark_rvalue_use (size); |
11304 | |
11305 | if (cxx_dialect < cxx11 && TREE_CODE (size) == NOP_EXPR |
11306 | && TREE_SIDE_EFFECTS (size)) |
11307 | /* In C++98, we mark a non-constant array bound with a magic |
11308 | NOP_EXPR with TREE_SIDE_EFFECTS; don't fold in that case. */; |
11309 | else |
11310 | { |
11311 | size = build_converted_constant_expr (size_type_node, size, complain); |
11312 | /* Pedantically a constant expression is required here and so |
11313 | __builtin_is_constant_evaluated () should fold to true if it |
11314 | is successfully folded into a constant. */ |
11315 | size = fold_non_dependent_expr (size, complain, |
11316 | /*manifestly_const_eval=*/true); |
11317 | |
11318 | if (!TREE_CONSTANT (size)) |
11319 | size = origsize; |
11320 | } |
11321 | |
11322 | if (error_operand_p (t: size)) |
11323 | return error_mark_node; |
11324 | |
11325 | /* The array bound must be an integer type. */ |
11326 | tree type = TREE_TYPE (size); |
11327 | if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type)) |
11328 | { |
11329 | if (!(complain & tf_error)) |
11330 | return error_mark_node; |
11331 | if (name) |
11332 | error_at (loc, "size of array %qD has non-integral type %qT" , |
11333 | name, type); |
11334 | else |
11335 | error_at (loc, "size of array has non-integral type %qT" , type); |
11336 | size = integer_one_node; |
11337 | } |
11338 | } |
11339 | |
11340 | /* A type is dependent if it is...an array type constructed from any |
11341 | dependent type or whose size is specified by a constant expression |
11342 | that is value-dependent. */ |
11343 | /* We can only call value_dependent_expression_p on integral constant |
11344 | expressions. */ |
11345 | if (processing_template_decl |
11346 | && potential_constant_expression (size) |
11347 | && value_dependent_expression_p (size)) |
11348 | { |
11349 | /* Just build the index type and mark that it requires |
11350 | structural equality checks. */ |
11351 | in_template: |
11352 | itype = build_index_type (build_min (MINUS_EXPR, sizetype, |
11353 | size, size_one_node)); |
11354 | TYPE_DEPENDENT_P (itype) = 1; |
11355 | TYPE_DEPENDENT_P_VALID (itype) = 1; |
11356 | SET_TYPE_STRUCTURAL_EQUALITY (itype); |
11357 | return itype; |
11358 | } |
11359 | |
11360 | if (TREE_CODE (size) != INTEGER_CST) |
11361 | { |
11362 | tree folded = cp_fully_fold (size); |
11363 | if (TREE_CODE (folded) == INTEGER_CST) |
11364 | { |
11365 | if (name) |
11366 | pedwarn (loc, OPT_Wpedantic, "size of array %qD is not an " |
11367 | "integral constant-expression" , name); |
11368 | else |
11369 | pedwarn (loc, OPT_Wpedantic, |
11370 | "size of array is not an integral constant-expression" ); |
11371 | } |
11372 | if (TREE_CONSTANT (size) && !TREE_CONSTANT (folded)) |
11373 | /* We might have lost the TREE_CONSTANT flag e.g. when we are |
11374 | folding a conversion from a pointer to integral type. In that |
11375 | case issue an error below and don't treat this as a VLA. */; |
11376 | else |
11377 | /* Use the folded result for VLAs, too; it will have resolved |
11378 | SIZEOF_EXPR. */ |
11379 | size = folded; |
11380 | } |
11381 | |
11382 | /* Normally, the array-bound will be a constant. */ |
11383 | if (TREE_CODE (size) == INTEGER_CST) |
11384 | { |
11385 | /* The size to use in diagnostics that reflects the constant |
11386 | size used in the source, rather than SIZE massaged above. */ |
11387 | tree diagsize = size; |
11388 | |
11389 | /* If the original size before conversion to size_t was signed |
11390 | and negative, convert it to ssizetype to restore the sign. */ |
11391 | if (!TYPE_UNSIGNED (TREE_TYPE (origsize)) |
11392 | && TREE_CODE (size) == INTEGER_CST |
11393 | && tree_int_cst_sign_bit (size)) |
11394 | { |
11395 | diagsize = fold_convert (ssizetype, size); |
11396 | |
11397 | /* Clear the overflow bit that may have been set as a result |
11398 | of the conversion from the sizetype of the new size to |
11399 | ssizetype. */ |
11400 | TREE_OVERFLOW (diagsize) = false; |
11401 | } |
11402 | |
11403 | /* Verify that the array has a positive number of elements |
11404 | and issue the appropriate diagnostic if it doesn't. */ |
11405 | if (!valid_array_size_p (loc, diagsize, name, (complain & tf_error))) |
11406 | { |
11407 | if (!(complain & tf_error)) |
11408 | return error_mark_node; |
11409 | size = integer_one_node; |
11410 | } |
11411 | /* As an extension we allow zero-sized arrays. */ |
11412 | else if (integer_zerop (size)) |
11413 | { |
11414 | if (!(complain & tf_error)) |
11415 | /* We must fail if performing argument deduction (as |
11416 | indicated by the state of complain), so that |
11417 | another substitution can be found. */ |
11418 | return error_mark_node; |
11419 | else if (name) |
11420 | pedwarn (loc, OPT_Wpedantic, |
11421 | "ISO C++ forbids zero-size array %qD" , name); |
11422 | else |
11423 | pedwarn (loc, OPT_Wpedantic, |
11424 | "ISO C++ forbids zero-size array" ); |
11425 | } |
11426 | } |
11427 | else if (TREE_CONSTANT (size) |
11428 | /* We don't allow VLAs at non-function scopes, or during |
11429 | tentative template substitution. */ |
11430 | || !at_function_scope_p () |
11431 | || !(complain & tf_error)) |
11432 | { |
11433 | if (!(complain & tf_error)) |
11434 | return error_mark_node; |
11435 | /* `(int) &fn' is not a valid array bound. */ |
11436 | if (name) |
11437 | error_at (loc, |
11438 | "size of array %qD is not an integral constant-expression" , |
11439 | name); |
11440 | else |
11441 | error_at (loc, "size of array is not an integral constant-expression" ); |
11442 | size = integer_one_node; |
11443 | } |
11444 | else if (pedantic && warn_vla != 0) |
11445 | { |
11446 | if (name) |
11447 | pedwarn (name_loc, OPT_Wvla, |
11448 | "ISO C++ forbids variable length array %qD" , name); |
11449 | else |
11450 | pedwarn (input_location, OPT_Wvla, |
11451 | "ISO C++ forbids variable length array" ); |
11452 | } |
11453 | else if (warn_vla > 0) |
11454 | { |
11455 | if (name) |
11456 | warning_at (name_loc, OPT_Wvla, |
11457 | "variable length array %qD is used" , name); |
11458 | else |
11459 | warning (OPT_Wvla, |
11460 | "variable length array is used" ); |
11461 | } |
11462 | |
11463 | if (processing_template_decl && !TREE_CONSTANT (size)) |
11464 | goto in_template; |
11465 | else |
11466 | { |
11467 | if (!TREE_CONSTANT (size)) |
11468 | { |
11469 | /* A variable sized array. Arrange for the SAVE_EXPR on the inside |
11470 | of the MINUS_EXPR, which allows the -1 to get folded with the +1 |
11471 | that happens when building TYPE_SIZE. */ |
11472 | size = variable_size (size); |
11473 | stabilize_vla_size (size); |
11474 | } |
11475 | |
11476 | /* Compute the index of the largest element in the array. It is |
11477 | one less than the number of elements in the array. We save |
11478 | and restore PROCESSING_TEMPLATE_DECL so that computations in |
11479 | cp_build_binary_op will be appropriately folded. */ |
11480 | { |
11481 | processing_template_decl_sentinel s; |
11482 | itype = cp_build_binary_op (input_location, |
11483 | MINUS_EXPR, |
11484 | cp_convert (ssizetype, size, complain), |
11485 | cp_convert (ssizetype, integer_one_node, |
11486 | complain), |
11487 | complain); |
11488 | itype = maybe_constant_value (itype, NULL_TREE, mce_true); |
11489 | } |
11490 | |
11491 | if (!TREE_CONSTANT (itype)) |
11492 | { |
11493 | if (sanitize_flags_p (flag: SANITIZE_VLA) |
11494 | && current_function_decl != NULL_TREE) |
11495 | { |
11496 | /* We have to add 1 -- in the ubsan routine we generate |
11497 | LE_EXPR rather than LT_EXPR. */ |
11498 | tree t = fold_build2 (PLUS_EXPR, TREE_TYPE (itype), itype, |
11499 | build_one_cst (TREE_TYPE (itype))); |
11500 | t = ubsan_instrument_vla (input_location, t); |
11501 | finish_expr_stmt (t); |
11502 | } |
11503 | } |
11504 | /* Make sure that there was no overflow when creating to a signed |
11505 | index type. (For example, on a 32-bit machine, an array with |
11506 | size 2^32 - 1 is too big.) */ |
11507 | else if (TREE_CODE (itype) == INTEGER_CST |
11508 | && TREE_OVERFLOW (itype)) |
11509 | { |
11510 | if (!(complain & tf_error)) |
11511 | return error_mark_node; |
11512 | error ("overflow in array dimension" ); |
11513 | TREE_OVERFLOW (itype) = 0; |
11514 | } |
11515 | } |
11516 | |
11517 | /* Create and return the appropriate index type. */ |
11518 | itype = build_index_type (itype); |
11519 | |
11520 | /* If the index type were dependent, we would have returned early, so |
11521 | remember that it isn't. */ |
11522 | TYPE_DEPENDENT_P (itype) = 0; |
11523 | TYPE_DEPENDENT_P_VALID (itype) = 1; |
11524 | return itype; |
11525 | } |
11526 | |
11527 | tree |
11528 | compute_array_index_type (tree name, tree size, tsubst_flags_t complain) |
11529 | { |
11530 | return compute_array_index_type_loc (name_loc: input_location, name, size, complain); |
11531 | } |
11532 | |
11533 | /* Returns the scope (if any) in which the entity declared by |
11534 | DECLARATOR will be located. If the entity was declared with an |
11535 | unqualified name, NULL_TREE is returned. */ |
11536 | |
11537 | tree |
11538 | get_scope_of_declarator (const cp_declarator *declarator) |
11539 | { |
11540 | while (declarator && declarator->kind != cdk_id) |
11541 | declarator = declarator->declarator; |
11542 | |
11543 | /* If the declarator-id is a SCOPE_REF, the scope in which the |
11544 | declaration occurs is the first operand. */ |
11545 | if (declarator |
11546 | && declarator->u.id.qualifying_scope) |
11547 | return declarator->u.id.qualifying_scope; |
11548 | |
11549 | /* Otherwise, the declarator is not a qualified name; the entity will |
11550 | be declared in the current scope. */ |
11551 | return NULL_TREE; |
11552 | } |
11553 | |
11554 | /* Returns an ARRAY_TYPE for an array with SIZE elements of the |
11555 | indicated TYPE. If non-NULL, NAME is the NAME of the declaration |
11556 | with this type. */ |
11557 | |
11558 | static tree |
11559 | create_array_type_for_decl (tree name, tree type, tree size, location_t loc) |
11560 | { |
11561 | tree itype = NULL_TREE; |
11562 | |
11563 | /* If things have already gone awry, bail now. */ |
11564 | if (type == error_mark_node || size == error_mark_node) |
11565 | return error_mark_node; |
11566 | |
11567 | /* [dcl.type.class.deduct] prohibits forming an array of placeholder |
11568 | for a deduced class type. */ |
11569 | if (template_placeholder_p (type)) |
11570 | { |
11571 | if (name) |
11572 | error_at (loc, "%qD declared as array of template placeholder " |
11573 | "type %qT" , name, type); |
11574 | else |
11575 | error ("creating array of template placeholder type %qT" , type); |
11576 | return error_mark_node; |
11577 | } |
11578 | |
11579 | /* If there are some types which cannot be array elements, |
11580 | issue an error-message and return. */ |
11581 | switch (TREE_CODE (type)) |
11582 | { |
11583 | case VOID_TYPE: |
11584 | if (name) |
11585 | error_at (loc, "declaration of %qD as array of void" , name); |
11586 | else |
11587 | error ("creating array of void" ); |
11588 | return error_mark_node; |
11589 | |
11590 | case FUNCTION_TYPE: |
11591 | if (name) |
11592 | error_at (loc, "declaration of %qD as array of functions" , name); |
11593 | else |
11594 | error ("creating array of functions" ); |
11595 | return error_mark_node; |
11596 | |
11597 | case REFERENCE_TYPE: |
11598 | if (name) |
11599 | error_at (loc, "declaration of %qD as array of references" , name); |
11600 | else |
11601 | error ("creating array of references" ); |
11602 | return error_mark_node; |
11603 | |
11604 | case METHOD_TYPE: |
11605 | if (name) |
11606 | error_at (loc, "declaration of %qD as array of function members" , |
11607 | name); |
11608 | else |
11609 | error ("creating array of function members" ); |
11610 | return error_mark_node; |
11611 | |
11612 | default: |
11613 | break; |
11614 | } |
11615 | |
11616 | if (!verify_type_context (name ? loc : input_location, |
11617 | TCTX_ARRAY_ELEMENT, type)) |
11618 | return error_mark_node; |
11619 | |
11620 | /* [dcl.array] |
11621 | |
11622 | The constant expressions that specify the bounds of the arrays |
11623 | can be omitted only for the first member of the sequence. */ |
11624 | if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)) |
11625 | { |
11626 | if (name) |
11627 | error_at (loc, "declaration of %qD as multidimensional array must " |
11628 | "have bounds for all dimensions except the first" , |
11629 | name); |
11630 | else |
11631 | error ("multidimensional array must have bounds for all " |
11632 | "dimensions except the first" ); |
11633 | |
11634 | return error_mark_node; |
11635 | } |
11636 | |
11637 | /* Figure out the index type for the array. */ |
11638 | if (size) |
11639 | { |
11640 | itype = compute_array_index_type_loc (name_loc: loc, name, size, |
11641 | complain: tf_warning_or_error); |
11642 | if (type_uses_auto (type) |
11643 | && variably_modified_type_p (itype, /*fn=*/NULL_TREE)) |
11644 | { |
11645 | sorry_at (loc, "variable-length array of %<auto%>" ); |
11646 | return error_mark_node; |
11647 | } |
11648 | } |
11649 | |
11650 | return build_cplus_array_type (type, itype); |
11651 | } |
11652 | |
11653 | /* Returns the smallest location that is not UNKNOWN_LOCATION. */ |
11654 | |
11655 | static location_t |
11656 | min_location (location_t loca, location_t locb) |
11657 | { |
11658 | if (loca == UNKNOWN_LOCATION |
11659 | || (locb != UNKNOWN_LOCATION |
11660 | && linemap_location_before_p (set: line_table, loc_a: locb, loc_b: loca))) |
11661 | return locb; |
11662 | return loca; |
11663 | } |
11664 | |
11665 | /* Returns the smallest location != UNKNOWN_LOCATION among the |
11666 | three stored in LOCATIONS[ds_const], LOCATIONS[ds_volatile], |
11667 | and LOCATIONS[ds_restrict]. */ |
11668 | |
11669 | static location_t |
11670 | smallest_type_quals_location (int type_quals, const location_t* locations) |
11671 | { |
11672 | location_t loc = UNKNOWN_LOCATION; |
11673 | |
11674 | if (type_quals & TYPE_QUAL_CONST) |
11675 | loc = locations[ds_const]; |
11676 | |
11677 | if (type_quals & TYPE_QUAL_VOLATILE) |
11678 | loc = min_location (loca: loc, locb: locations[ds_volatile]); |
11679 | |
11680 | if (type_quals & TYPE_QUAL_RESTRICT) |
11681 | loc = min_location (loca: loc, locb: locations[ds_restrict]); |
11682 | |
11683 | return loc; |
11684 | } |
11685 | |
11686 | /* Returns the smallest among the latter and locations[ds_type_spec]. */ |
11687 | |
11688 | static location_t |
11689 | smallest_type_location (int type_quals, const location_t* locations) |
11690 | { |
11691 | location_t loc = smallest_type_quals_location (type_quals, locations); |
11692 | return min_location (loca: loc, locb: locations[ds_type_spec]); |
11693 | } |
11694 | |
11695 | static location_t |
11696 | smallest_type_location (const cp_decl_specifier_seq *declspecs) |
11697 | { |
11698 | int type_quals = get_type_quals (declspecs); |
11699 | return smallest_type_location (type_quals, locations: declspecs->locations); |
11700 | } |
11701 | |
11702 | /* Check that it's OK to declare a function with the indicated TYPE |
11703 | and TYPE_QUALS. SFK indicates the kind of special function (if any) |
11704 | that this function is. OPTYPE is the type given in a conversion |
11705 | operator declaration, or the class type for a constructor/destructor. |
11706 | Returns the actual return type of the function; that may be different |
11707 | than TYPE if an error occurs, or for certain special functions. */ |
11708 | |
11709 | static tree |
11710 | check_special_function_return_type (special_function_kind sfk, |
11711 | tree type, |
11712 | tree optype, |
11713 | int type_quals, |
11714 | const location_t* locations) |
11715 | { |
11716 | switch (sfk) |
11717 | { |
11718 | case sfk_constructor: |
11719 | if (type) |
11720 | error_at (smallest_type_location (type_quals, locations), |
11721 | "return type specification for constructor invalid" ); |
11722 | else if (type_quals != TYPE_UNQUALIFIED) |
11723 | error_at (smallest_type_quals_location (type_quals, locations), |
11724 | "qualifiers are not allowed on constructor declaration" ); |
11725 | |
11726 | if (targetm.cxx.cdtor_returns_this ()) |
11727 | type = build_pointer_type (optype); |
11728 | else |
11729 | type = void_type_node; |
11730 | break; |
11731 | |
11732 | case sfk_destructor: |
11733 | if (type) |
11734 | error_at (smallest_type_location (type_quals, locations), |
11735 | "return type specification for destructor invalid" ); |
11736 | else if (type_quals != TYPE_UNQUALIFIED) |
11737 | error_at (smallest_type_quals_location (type_quals, locations), |
11738 | "qualifiers are not allowed on destructor declaration" ); |
11739 | |
11740 | /* We can't use the proper return type here because we run into |
11741 | problems with ambiguous bases and covariant returns. */ |
11742 | if (targetm.cxx.cdtor_returns_this ()) |
11743 | type = build_pointer_type (void_type_node); |
11744 | else |
11745 | type = void_type_node; |
11746 | break; |
11747 | |
11748 | case sfk_conversion: |
11749 | if (type) |
11750 | error_at (smallest_type_location (type_quals, locations), |
11751 | "return type specified for %<operator %T%>" , optype); |
11752 | else if (type_quals != TYPE_UNQUALIFIED) |
11753 | error_at (smallest_type_quals_location (type_quals, locations), |
11754 | "qualifiers are not allowed on declaration of " |
11755 | "%<operator %T%>" , optype); |
11756 | |
11757 | type = optype; |
11758 | break; |
11759 | |
11760 | case sfk_deduction_guide: |
11761 | if (type) |
11762 | error_at (smallest_type_location (type_quals, locations), |
11763 | "return type specified for deduction guide" ); |
11764 | else if (type_quals != TYPE_UNQUALIFIED) |
11765 | error_at (smallest_type_quals_location (type_quals, locations), |
11766 | "qualifiers are not allowed on declaration of " |
11767 | "deduction guide" ); |
11768 | if (TREE_CODE (optype) == TEMPLATE_TEMPLATE_PARM) |
11769 | { |
11770 | error ("template template parameter %qT in declaration of " |
11771 | "deduction guide" , optype); |
11772 | type = error_mark_node; |
11773 | } |
11774 | else |
11775 | type = make_template_placeholder (CLASSTYPE_TI_TEMPLATE (optype)); |
11776 | for (int i = 0; i < ds_last; ++i) |
11777 | if (i != ds_explicit && locations[i]) |
11778 | error_at (locations[i], |
11779 | "%<decl-specifier%> in declaration of deduction guide" ); |
11780 | break; |
11781 | |
11782 | default: |
11783 | gcc_unreachable (); |
11784 | } |
11785 | |
11786 | return type; |
11787 | } |
11788 | |
11789 | /* A variable or data member (whose unqualified name is IDENTIFIER) |
11790 | has been declared with the indicated TYPE. If the TYPE is not |
11791 | acceptable, issue an error message and return a type to use for |
11792 | error-recovery purposes. */ |
11793 | |
11794 | tree |
11795 | check_var_type (tree identifier, tree type, location_t loc) |
11796 | { |
11797 | if (VOID_TYPE_P (type)) |
11798 | { |
11799 | if (!identifier) |
11800 | error_at (loc, "unnamed variable or field declared void" ); |
11801 | else if (identifier_p (t: identifier)) |
11802 | { |
11803 | gcc_assert (!IDENTIFIER_ANY_OP_P (identifier)); |
11804 | error_at (loc, "variable or field %qE declared void" , |
11805 | identifier); |
11806 | } |
11807 | else |
11808 | error_at (loc, "variable or field declared void" ); |
11809 | type = error_mark_node; |
11810 | } |
11811 | |
11812 | return type; |
11813 | } |
11814 | |
11815 | /* Handle declaring DECL as an inline variable. */ |
11816 | |
11817 | static void |
11818 | mark_inline_variable (tree decl, location_t loc) |
11819 | { |
11820 | bool inlinep = true; |
11821 | if (! toplevel_bindings_p ()) |
11822 | { |
11823 | error_at (loc, "%<inline%> specifier invalid for variable " |
11824 | "%qD declared at block scope" , decl); |
11825 | inlinep = false; |
11826 | } |
11827 | else if (cxx_dialect < cxx17) |
11828 | pedwarn (loc, OPT_Wc__17_extensions, "inline variables are only available " |
11829 | "with %<-std=c++17%> or %<-std=gnu++17%>" ); |
11830 | if (inlinep) |
11831 | { |
11832 | retrofit_lang_decl (decl); |
11833 | SET_DECL_VAR_DECLARED_INLINE_P (decl); |
11834 | } |
11835 | } |
11836 | |
11837 | |
11838 | /* Assign a typedef-given name to a class or enumeration type declared |
11839 | as anonymous at first. This was split out of grokdeclarator |
11840 | because it is also used in libcc1. */ |
11841 | |
11842 | void |
11843 | name_unnamed_type (tree type, tree decl) |
11844 | { |
11845 | gcc_assert (TYPE_UNNAMED_P (type)); |
11846 | |
11847 | /* Replace the anonymous decl with the real decl. Be careful not to |
11848 | rename other typedefs (such as the self-reference) of type. */ |
11849 | tree orig = TYPE_NAME (type); |
11850 | for (tree t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t)) |
11851 | if (TYPE_NAME (t) == orig) |
11852 | TYPE_NAME (t) = decl; |
11853 | |
11854 | /* If this is a typedef within a template class, the nested |
11855 | type is a (non-primary) template. The name for the |
11856 | template needs updating as well. */ |
11857 | if (TYPE_LANG_SPECIFIC (type) && CLASSTYPE_TEMPLATE_INFO (type)) |
11858 | DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)) = DECL_NAME (decl); |
11859 | |
11860 | /* Adjust linkage now that we aren't unnamed anymore. */ |
11861 | reset_type_linkage (type); |
11862 | |
11863 | /* FIXME remangle member functions; member functions of a |
11864 | type with external linkage have external linkage. */ |
11865 | |
11866 | /* Check that our job is done, and that it would fail if we |
11867 | attempted to do it again. */ |
11868 | gcc_assert (!TYPE_UNNAMED_P (type)); |
11869 | } |
11870 | |
11871 | /* Check that decltype(auto) was well-formed: only plain decltype(auto) |
11872 | is allowed. TYPE might contain a decltype(auto). Returns true if |
11873 | there was a problem, false otherwise. */ |
11874 | |
11875 | static bool |
11876 | check_decltype_auto (location_t loc, tree type) |
11877 | { |
11878 | if (tree a = type_uses_auto (type)) |
11879 | { |
11880 | if (AUTO_IS_DECLTYPE (a)) |
11881 | { |
11882 | if (a != type) |
11883 | { |
11884 | error_at (loc, "%qT as type rather than plain " |
11885 | "%<decltype(auto)%>" , type); |
11886 | return true; |
11887 | } |
11888 | else if (TYPE_QUALS (type) != TYPE_UNQUALIFIED) |
11889 | { |
11890 | error_at (loc, "%<decltype(auto)%> cannot be cv-qualified" ); |
11891 | return true; |
11892 | } |
11893 | } |
11894 | } |
11895 | return false; |
11896 | } |
11897 | |
11898 | /* Given declspecs and a declarator (abstract or otherwise), determine |
11899 | the name and type of the object declared and construct a DECL node |
11900 | for it. |
11901 | |
11902 | DECLSPECS points to the representation of declaration-specifier |
11903 | sequence that precedes declarator. |
11904 | |
11905 | DECL_CONTEXT says which syntactic context this declaration is in: |
11906 | NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL. |
11907 | FUNCDEF for a function definition. Like NORMAL but a few different |
11908 | error messages in each case. Return value may be zero meaning |
11909 | this definition is too screwy to try to parse. |
11910 | MEMFUNCDEF for a function definition. Like FUNCDEF but prepares to |
11911 | handle member functions (which have FIELD context). |
11912 | Return value may be zero meaning this definition is too screwy to |
11913 | try to parse. |
11914 | PARM for a parameter declaration (either within a function prototype |
11915 | or before a function body). Make a PARM_DECL, or return void_type_node. |
11916 | TPARM for a template parameter declaration. |
11917 | CATCHPARM for a parameter declaration before a catch clause. |
11918 | TYPENAME if for a typename (in a cast or sizeof). |
11919 | Don't make a DECL node; just return the ..._TYPE node. |
11920 | FIELD for a struct or union field; make a FIELD_DECL. |
11921 | BITFIELD for a field with specified width. |
11922 | |
11923 | INITIALIZED is as for start_decl. |
11924 | |
11925 | ATTRLIST is a pointer to the list of attributes, which may be NULL |
11926 | if there are none; *ATTRLIST may be modified if attributes from inside |
11927 | the declarator should be applied to the declaration. |
11928 | |
11929 | When this function is called, scoping variables (such as |
11930 | CURRENT_CLASS_TYPE) should reflect the scope in which the |
11931 | declaration occurs, not the scope in which the new declaration will |
11932 | be placed. For example, on: |
11933 | |
11934 | void S::f() { ... } |
11935 | |
11936 | when grokdeclarator is called for `S::f', the CURRENT_CLASS_TYPE |
11937 | should not be `S'. |
11938 | |
11939 | Returns a DECL (if a declarator is present), a TYPE (if there is no |
11940 | declarator, in cases like "struct S;"), or the ERROR_MARK_NODE if an |
11941 | error occurs. */ |
11942 | |
11943 | tree |
11944 | grokdeclarator (const cp_declarator *declarator, |
11945 | cp_decl_specifier_seq *declspecs, |
11946 | enum decl_context decl_context, |
11947 | int initialized, |
11948 | tree* attrlist) |
11949 | { |
11950 | tree type = NULL_TREE; |
11951 | int longlong = 0; |
11952 | int explicit_intN = 0; |
11953 | int int_n_alt = 0; |
11954 | int virtualp, explicitp, friendp, inlinep, staticp; |
11955 | int explicit_int = 0; |
11956 | int explicit_char = 0; |
11957 | int defaulted_int = 0; |
11958 | |
11959 | tree typedef_decl = NULL_TREE; |
11960 | const char *name = NULL; |
11961 | tree typedef_type = NULL_TREE; |
11962 | /* True if this declarator is a function definition. */ |
11963 | bool funcdef_flag = false; |
11964 | cp_declarator_kind innermost_code = cdk_error; |
11965 | int bitfield = 0; |
11966 | #if 0 |
11967 | /* See the code below that used this. */ |
11968 | tree decl_attr = NULL_TREE; |
11969 | #endif |
11970 | |
11971 | /* Keep track of what sort of function is being processed |
11972 | so that we can warn about default return values, or explicit |
11973 | return values which do not match prescribed defaults. */ |
11974 | special_function_kind sfk = sfk_none; |
11975 | |
11976 | tree dname = NULL_TREE; |
11977 | tree ctor_return_type = NULL_TREE; |
11978 | enum overload_flags flags = NO_SPECIAL; |
11979 | /* cv-qualifiers that apply to the declarator, for a declaration of |
11980 | a member function. */ |
11981 | cp_cv_quals memfn_quals = TYPE_UNQUALIFIED; |
11982 | /* virt-specifiers that apply to the declarator, for a declaration of |
11983 | a member function. */ |
11984 | cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED; |
11985 | /* ref-qualifier that applies to the declarator, for a declaration of |
11986 | a member function. */ |
11987 | cp_ref_qualifier rqual = REF_QUAL_NONE; |
11988 | /* cv-qualifiers that apply to the type specified by the DECLSPECS. */ |
11989 | int type_quals = get_type_quals (declspecs); |
11990 | tree raises = NULL_TREE; |
11991 | int template_count = 0; |
11992 | tree returned_attrs = NULL_TREE; |
11993 | tree parms = NULL_TREE; |
11994 | const cp_declarator *id_declarator; |
11995 | /* The unqualified name of the declarator; either an |
11996 | IDENTIFIER_NODE, BIT_NOT_EXPR, or TEMPLATE_ID_EXPR. */ |
11997 | tree unqualified_id; |
11998 | /* The class type, if any, in which this entity is located, |
11999 | or NULL_TREE if none. Note that this value may be different from |
12000 | the current class type; for example if an attempt is made to declare |
12001 | "A::f" inside "B", this value will be "A". */ |
12002 | tree ctype = current_class_type; |
12003 | /* The NAMESPACE_DECL for the namespace in which this entity is |
12004 | located. If an unqualified name is used to declare the entity, |
12005 | this value will be NULL_TREE, even if the entity is located at |
12006 | namespace scope. */ |
12007 | tree in_namespace = NULL_TREE; |
12008 | cp_storage_class storage_class; |
12009 | bool unsigned_p, signed_p, short_p, long_p, thread_p; |
12010 | bool type_was_error_mark_node = false; |
12011 | bool parameter_pack_p = declarator ? declarator->parameter_pack_p : false; |
12012 | bool template_type_arg = false; |
12013 | bool template_parm_flag = false; |
12014 | bool typedef_p = decl_spec_seq_has_spec_p (declspecs, ds_typedef); |
12015 | bool constexpr_p = decl_spec_seq_has_spec_p (declspecs, ds_constexpr); |
12016 | bool constinit_p = decl_spec_seq_has_spec_p (declspecs, ds_constinit); |
12017 | bool consteval_p = decl_spec_seq_has_spec_p (declspecs, ds_consteval); |
12018 | bool late_return_type_p = false; |
12019 | bool array_parameter_p = false; |
12020 | tree reqs = NULL_TREE; |
12021 | |
12022 | signed_p = decl_spec_seq_has_spec_p (declspecs, ds_signed); |
12023 | unsigned_p = decl_spec_seq_has_spec_p (declspecs, ds_unsigned); |
12024 | short_p = decl_spec_seq_has_spec_p (declspecs, ds_short); |
12025 | long_p = decl_spec_seq_has_spec_p (declspecs, ds_long); |
12026 | longlong = decl_spec_seq_has_spec_p (declspecs, ds_long_long); |
12027 | explicit_intN = declspecs->explicit_intN_p; |
12028 | int_n_alt = declspecs->int_n_alt; |
12029 | thread_p = decl_spec_seq_has_spec_p (declspecs, ds_thread); |
12030 | |
12031 | // Was concept_p specified? Note that ds_concept |
12032 | // implies ds_constexpr! |
12033 | bool concept_p = decl_spec_seq_has_spec_p (declspecs, ds_concept); |
12034 | if (concept_p) |
12035 | constexpr_p = true; |
12036 | |
12037 | if (decl_context == FUNCDEF) |
12038 | funcdef_flag = true, decl_context = NORMAL; |
12039 | else if (decl_context == MEMFUNCDEF) |
12040 | funcdef_flag = true, decl_context = FIELD; |
12041 | else if (decl_context == BITFIELD) |
12042 | bitfield = 1, decl_context = FIELD; |
12043 | else if (decl_context == TEMPLATE_TYPE_ARG) |
12044 | template_type_arg = true, decl_context = TYPENAME; |
12045 | else if (decl_context == TPARM) |
12046 | template_parm_flag = true, decl_context = PARM; |
12047 | |
12048 | if (initialized == SD_DEFAULTED || initialized == SD_DELETED) |
12049 | funcdef_flag = true; |
12050 | |
12051 | location_t typespec_loc = loc_or_input_loc (loc: smallest_type_location |
12052 | (type_quals, |
12053 | locations: declspecs->locations)); |
12054 | location_t id_loc; |
12055 | location_t init_loc; |
12056 | if (declarator) |
12057 | { |
12058 | id_loc = loc_or_input_loc (loc: declarator->id_loc); |
12059 | init_loc = loc_or_input_loc (loc: declarator->init_loc); |
12060 | } |
12061 | else |
12062 | init_loc = id_loc = input_location; |
12063 | |
12064 | /* Look inside a declarator for the name being declared |
12065 | and get it as a string, for an error message. */ |
12066 | for (id_declarator = declarator; |
12067 | id_declarator; |
12068 | id_declarator = id_declarator->declarator) |
12069 | { |
12070 | if (id_declarator->kind != cdk_id) |
12071 | innermost_code = id_declarator->kind; |
12072 | |
12073 | switch (id_declarator->kind) |
12074 | { |
12075 | case cdk_function: |
12076 | if (id_declarator->declarator |
12077 | && id_declarator->declarator->kind == cdk_id) |
12078 | { |
12079 | sfk = id_declarator->declarator->u.id.sfk; |
12080 | if (sfk == sfk_destructor) |
12081 | flags = DTOR_FLAG; |
12082 | } |
12083 | break; |
12084 | |
12085 | case cdk_id: |
12086 | { |
12087 | tree qualifying_scope = id_declarator->u.id.qualifying_scope; |
12088 | tree decl = id_declarator->u.id.unqualified_name; |
12089 | if (!decl) |
12090 | break; |
12091 | if (qualifying_scope) |
12092 | { |
12093 | if (check_for_bare_parameter_packs (qualifying_scope, |
12094 | id_declarator->id_loc)) |
12095 | return error_mark_node; |
12096 | if (at_function_scope_p ()) |
12097 | { |
12098 | /* [dcl.meaning] |
12099 | |
12100 | A declarator-id shall not be qualified except |
12101 | for ... |
12102 | |
12103 | None of the cases are permitted in block |
12104 | scope. */ |
12105 | if (qualifying_scope == global_namespace) |
12106 | error ("invalid use of qualified-name %<::%D%>" , |
12107 | decl); |
12108 | else if (TYPE_P (qualifying_scope)) |
12109 | error ("invalid use of qualified-name %<%T::%D%>" , |
12110 | qualifying_scope, decl); |
12111 | else |
12112 | error ("invalid use of qualified-name %<%D::%D%>" , |
12113 | qualifying_scope, decl); |
12114 | return error_mark_node; |
12115 | } |
12116 | else if (TYPE_P (qualifying_scope)) |
12117 | { |
12118 | ctype = qualifying_scope; |
12119 | if (!MAYBE_CLASS_TYPE_P (ctype)) |
12120 | { |
12121 | error_at (id_declarator->id_loc, |
12122 | "%q#T is not a class or namespace" , ctype); |
12123 | ctype = NULL_TREE; |
12124 | } |
12125 | else if (innermost_code != cdk_function |
12126 | && current_class_type |
12127 | && !uniquely_derived_from_p (ctype, |
12128 | current_class_type)) |
12129 | { |
12130 | error_at (id_declarator->id_loc, |
12131 | "invalid use of qualified-name %<%T::%D%>" , |
12132 | qualifying_scope, decl); |
12133 | return error_mark_node; |
12134 | } |
12135 | } |
12136 | else if (TREE_CODE (qualifying_scope) == NAMESPACE_DECL) |
12137 | in_namespace = qualifying_scope; |
12138 | } |
12139 | switch (TREE_CODE (decl)) |
12140 | { |
12141 | case BIT_NOT_EXPR: |
12142 | { |
12143 | if (innermost_code != cdk_function) |
12144 | { |
12145 | error_at (EXPR_LOCATION (decl), |
12146 | "declaration of %qE as non-function" , decl); |
12147 | return error_mark_node; |
12148 | } |
12149 | else if (!qualifying_scope |
12150 | && !(current_class_type && at_class_scope_p ())) |
12151 | { |
12152 | error_at (EXPR_LOCATION (decl), |
12153 | "declaration of %qE as non-member" , decl); |
12154 | return error_mark_node; |
12155 | } |
12156 | |
12157 | tree type = TREE_OPERAND (decl, 0); |
12158 | if (TYPE_P (type)) |
12159 | type = constructor_name (type); |
12160 | name = identifier_to_locale (IDENTIFIER_POINTER (type)); |
12161 | dname = decl; |
12162 | } |
12163 | break; |
12164 | |
12165 | case TEMPLATE_ID_EXPR: |
12166 | { |
12167 | tree fns = TREE_OPERAND (decl, 0); |
12168 | |
12169 | dname = fns; |
12170 | if (!identifier_p (t: dname)) |
12171 | dname = OVL_NAME (dname); |
12172 | } |
12173 | /* Fall through. */ |
12174 | |
12175 | case IDENTIFIER_NODE: |
12176 | if (identifier_p (t: decl)) |
12177 | dname = decl; |
12178 | |
12179 | if (IDENTIFIER_KEYWORD_P (dname)) |
12180 | { |
12181 | error ("declarator-id missing; using reserved word %qD" , |
12182 | dname); |
12183 | name = identifier_to_locale (IDENTIFIER_POINTER (dname)); |
12184 | } |
12185 | else if (!IDENTIFIER_CONV_OP_P (dname)) |
12186 | name = identifier_to_locale (IDENTIFIER_POINTER (dname)); |
12187 | else |
12188 | { |
12189 | gcc_assert (flags == NO_SPECIAL); |
12190 | flags = TYPENAME_FLAG; |
12191 | sfk = sfk_conversion; |
12192 | tree glob = get_global_binding (id: dname); |
12193 | if (glob && TREE_CODE (glob) == TYPE_DECL) |
12194 | name = identifier_to_locale (IDENTIFIER_POINTER (dname)); |
12195 | else |
12196 | name = "<invalid operator>" ; |
12197 | } |
12198 | break; |
12199 | |
12200 | default: |
12201 | gcc_unreachable (); |
12202 | } |
12203 | break; |
12204 | } |
12205 | |
12206 | case cdk_array: |
12207 | case cdk_pointer: |
12208 | case cdk_reference: |
12209 | case cdk_ptrmem: |
12210 | break; |
12211 | |
12212 | case cdk_decomp: |
12213 | name = "structured binding" ; |
12214 | break; |
12215 | |
12216 | case cdk_error: |
12217 | return error_mark_node; |
12218 | |
12219 | default: |
12220 | gcc_unreachable (); |
12221 | } |
12222 | if (id_declarator->kind == cdk_id) |
12223 | break; |
12224 | } |
12225 | |
12226 | /* [dcl.fct.edf] |
12227 | |
12228 | The declarator in a function-definition shall have the form |
12229 | D1 ( parameter-declaration-clause) ... */ |
12230 | if (funcdef_flag && innermost_code != cdk_function) |
12231 | { |
12232 | error_at (id_loc, "function definition does not declare parameters" ); |
12233 | return error_mark_node; |
12234 | } |
12235 | |
12236 | if (flags == TYPENAME_FLAG |
12237 | && innermost_code != cdk_function |
12238 | && ! (ctype && !declspecs->any_specifiers_p)) |
12239 | { |
12240 | error_at (id_loc, "declaration of %qD as non-function" , dname); |
12241 | return error_mark_node; |
12242 | } |
12243 | |
12244 | if (dname && identifier_p (t: dname)) |
12245 | { |
12246 | if (UDLIT_OPER_P (dname) |
12247 | && innermost_code != cdk_function) |
12248 | { |
12249 | error_at (id_loc, "declaration of %qD as non-function" , dname); |
12250 | return error_mark_node; |
12251 | } |
12252 | |
12253 | if (IDENTIFIER_ANY_OP_P (dname)) |
12254 | { |
12255 | if (typedef_p) |
12256 | { |
12257 | error_at (id_loc, "declaration of %qD as %<typedef%>" , dname); |
12258 | return error_mark_node; |
12259 | } |
12260 | else if (decl_context == PARM || decl_context == CATCHPARM) |
12261 | { |
12262 | error_at (id_loc, "declaration of %qD as parameter" , dname); |
12263 | return error_mark_node; |
12264 | } |
12265 | } |
12266 | } |
12267 | |
12268 | /* Anything declared one level down from the top level |
12269 | must be one of the parameters of a function |
12270 | (because the body is at least two levels down). */ |
12271 | |
12272 | /* This heuristic cannot be applied to C++ nodes! Fixed, however, |
12273 | by not allowing C++ class definitions to specify their parameters |
12274 | with xdecls (must be spec.d in the parmlist). |
12275 | |
12276 | Since we now wait to push a class scope until we are sure that |
12277 | we are in a legitimate method context, we must set oldcname |
12278 | explicitly (since current_class_name is not yet alive). |
12279 | |
12280 | We also want to avoid calling this a PARM if it is in a namespace. */ |
12281 | |
12282 | if (decl_context == NORMAL && !toplevel_bindings_p ()) |
12283 | { |
12284 | cp_binding_level *b = current_binding_level; |
12285 | current_binding_level = b->level_chain; |
12286 | if (current_binding_level != 0 && toplevel_bindings_p ()) |
12287 | decl_context = PARM; |
12288 | current_binding_level = b; |
12289 | } |
12290 | |
12291 | if (name == NULL) |
12292 | name = decl_context == PARM ? "parameter" : "type name" ; |
12293 | |
12294 | if (consteval_p && constexpr_p) |
12295 | { |
12296 | error_at (declspecs->locations[ds_consteval], |
12297 | "both %qs and %qs specified" , "constexpr" , "consteval" ); |
12298 | return error_mark_node; |
12299 | } |
12300 | |
12301 | if (concept_p && typedef_p) |
12302 | { |
12303 | error_at (declspecs->locations[ds_concept], |
12304 | "%qs cannot appear in a typedef declaration" , "concept" ); |
12305 | return error_mark_node; |
12306 | } |
12307 | |
12308 | if (constexpr_p && typedef_p) |
12309 | { |
12310 | error_at (declspecs->locations[ds_constexpr], |
12311 | "%qs cannot appear in a typedef declaration" , "constexpr" ); |
12312 | return error_mark_node; |
12313 | } |
12314 | |
12315 | if (consteval_p && typedef_p) |
12316 | { |
12317 | error_at (declspecs->locations[ds_consteval], |
12318 | "%qs cannot appear in a typedef declaration" , "consteval" ); |
12319 | return error_mark_node; |
12320 | } |
12321 | |
12322 | if (constinit_p && typedef_p) |
12323 | { |
12324 | error_at (declspecs->locations[ds_constinit], |
12325 | "%qs cannot appear in a typedef declaration" , "constinit" ); |
12326 | return error_mark_node; |
12327 | } |
12328 | |
12329 | /* [dcl.spec]/2 "At most one of the constexpr, consteval, and constinit |
12330 | keywords shall appear in a decl-specifier-seq." */ |
12331 | if (constinit_p && constexpr_p) |
12332 | { |
12333 | gcc_rich_location richloc (declspecs->locations[ds_constinit]); |
12334 | richloc.add_range (loc: declspecs->locations[ds_constexpr]); |
12335 | error_at (&richloc, |
12336 | "can use at most one of the %<constinit%> and %<constexpr%> " |
12337 | "specifiers" ); |
12338 | return error_mark_node; |
12339 | } |
12340 | |
12341 | /* If there were multiple types specified in the decl-specifier-seq, |
12342 | issue an error message. */ |
12343 | if (declspecs->multiple_types_p) |
12344 | { |
12345 | error_at (typespec_loc, |
12346 | "two or more data types in declaration of %qs" , name); |
12347 | return error_mark_node; |
12348 | } |
12349 | |
12350 | if (declspecs->conflicting_specifiers_p) |
12351 | return error_mark_node; |
12352 | |
12353 | /* Extract the basic type from the decl-specifier-seq. */ |
12354 | type = declspecs->type; |
12355 | if (type == error_mark_node) |
12356 | { |
12357 | type = NULL_TREE; |
12358 | type_was_error_mark_node = true; |
12359 | } |
12360 | |
12361 | /* Ignore erroneous attributes. */ |
12362 | if (attrlist && *attrlist == error_mark_node) |
12363 | *attrlist = NULL_TREE; |
12364 | |
12365 | /* An object declared as __attribute__((unavailable)) suppresses |
12366 | any reports of being declared with unavailable or deprecated |
12367 | items. An object declared as __attribute__((deprecated)) |
12368 | suppresses warnings of uses of other deprecated items. */ |
12369 | auto ds = make_temp_override (var&: deprecated_state); |
12370 | if (attrlist && lookup_attribute (attr_name: "unavailable" , list: *attrlist)) |
12371 | deprecated_state = UNAVAILABLE_DEPRECATED_SUPPRESS; |
12372 | else if (attrlist && lookup_attribute (attr_name: "deprecated" , list: *attrlist)) |
12373 | deprecated_state = DEPRECATED_SUPPRESS; |
12374 | |
12375 | cp_handle_deprecated_or_unavailable (type); |
12376 | if (type && TREE_CODE (type) == TYPE_DECL) |
12377 | { |
12378 | cp_warn_deprecated_use_scopes (CP_DECL_CONTEXT (type)); |
12379 | typedef_decl = type; |
12380 | type = TREE_TYPE (typedef_decl); |
12381 | if (DECL_ARTIFICIAL (typedef_decl)) |
12382 | cp_handle_deprecated_or_unavailable (type); |
12383 | } |
12384 | /* No type at all: default to `int', and set DEFAULTED_INT |
12385 | because it was not a user-defined typedef. */ |
12386 | if (type == NULL_TREE) |
12387 | { |
12388 | if (signed_p || unsigned_p || long_p || short_p) |
12389 | { |
12390 | /* These imply 'int'. */ |
12391 | type = integer_type_node; |
12392 | defaulted_int = 1; |
12393 | } |
12394 | /* If we just have "complex", it is equivalent to "complex double". */ |
12395 | else if (!longlong && !explicit_intN |
12396 | && decl_spec_seq_has_spec_p (declspecs, ds_complex)) |
12397 | { |
12398 | type = double_type_node; |
12399 | pedwarn (declspecs->locations[ds_complex], OPT_Wpedantic, |
12400 | "ISO C++ does not support plain %<complex%> meaning " |
12401 | "%<double complex%>" ); |
12402 | } |
12403 | } |
12404 | /* Gather flags. */ |
12405 | explicit_int = declspecs->explicit_int_p; |
12406 | explicit_char = declspecs->explicit_char_p; |
12407 | |
12408 | #if 0 |
12409 | /* See the code below that used this. */ |
12410 | if (typedef_decl) |
12411 | decl_attr = DECL_ATTRIBUTES (typedef_decl); |
12412 | #endif |
12413 | typedef_type = type; |
12414 | |
12415 | if (sfk == sfk_conversion || sfk == sfk_deduction_guide) |
12416 | ctor_return_type = TREE_TYPE (dname); |
12417 | else |
12418 | ctor_return_type = ctype; |
12419 | |
12420 | if (sfk != sfk_none) |
12421 | { |
12422 | type = check_special_function_return_type (sfk, type, |
12423 | optype: ctor_return_type, |
12424 | type_quals, |
12425 | locations: declspecs->locations); |
12426 | type_quals = TYPE_UNQUALIFIED; |
12427 | } |
12428 | else if (type == NULL_TREE) |
12429 | { |
12430 | int is_main; |
12431 | |
12432 | explicit_int = -1; |
12433 | |
12434 | /* We handle `main' specially here, because 'main () { }' is so |
12435 | common. With no options, it is allowed. With -Wreturn-type, |
12436 | it is a warning. It is only an error with -pedantic-errors. */ |
12437 | is_main = (funcdef_flag |
12438 | && dname && identifier_p (t: dname) |
12439 | && MAIN_NAME_P (dname) |
12440 | && ctype == NULL_TREE |
12441 | && in_namespace == NULL_TREE |
12442 | && current_namespace == global_namespace); |
12443 | |
12444 | if (type_was_error_mark_node) |
12445 | /* We've already issued an error, don't complain more. */; |
12446 | else if (in_system_header_at (loc: id_loc) || flag_ms_extensions) |
12447 | /* Allow it, sigh. */; |
12448 | else if (! is_main) |
12449 | permerror (id_loc, "ISO C++ forbids declaration of %qs with no type" , |
12450 | name); |
12451 | else if (pedantic) |
12452 | pedwarn (id_loc, OPT_Wpedantic, |
12453 | "ISO C++ forbids declaration of %qs with no type" , name); |
12454 | else |
12455 | warning_at (id_loc, OPT_Wreturn_type, |
12456 | "ISO C++ forbids declaration of %qs with no type" , name); |
12457 | |
12458 | if (type_was_error_mark_node && template_parm_flag) |
12459 | /* FIXME we should be able to propagate the error_mark_node as is |
12460 | for other contexts too. */ |
12461 | type = error_mark_node; |
12462 | else |
12463 | type = integer_type_node; |
12464 | } |
12465 | |
12466 | ctype = NULL_TREE; |
12467 | |
12468 | if (explicit_intN) |
12469 | { |
12470 | if (! int_n_enabled_p[declspecs->int_n_idx]) |
12471 | { |
12472 | error_at (declspecs->locations[ds_type_spec], |
12473 | "%<__int%d%> is not supported by this target" , |
12474 | int_n_data[declspecs->int_n_idx].bitsize); |
12475 | explicit_intN = false; |
12476 | } |
12477 | /* Don't pedwarn if the alternate "__intN__" form has been used instead |
12478 | of "__intN". */ |
12479 | else if (!int_n_alt && pedantic) |
12480 | pedwarn (declspecs->locations[ds_type_spec], OPT_Wpedantic, |
12481 | "ISO C++ does not support %<__int%d%> for %qs" , |
12482 | int_n_data[declspecs->int_n_idx].bitsize, name); |
12483 | } |
12484 | |
12485 | /* Now process the modifiers that were specified |
12486 | and check for invalid combinations. */ |
12487 | |
12488 | /* Long double is a special combination. */ |
12489 | if (long_p && !longlong && TYPE_MAIN_VARIANT (type) == double_type_node) |
12490 | { |
12491 | long_p = false; |
12492 | type = cp_build_qualified_type (long_double_type_node, |
12493 | cp_type_quals (type)); |
12494 | } |
12495 | |
12496 | /* Check all other uses of type modifiers. */ |
12497 | |
12498 | if (unsigned_p || signed_p || long_p || short_p) |
12499 | { |
12500 | location_t loc; |
12501 | const char *key; |
12502 | if (unsigned_p) |
12503 | { |
12504 | key = "unsigned" ; |
12505 | loc = declspecs->locations[ds_unsigned]; |
12506 | } |
12507 | else if (signed_p) |
12508 | { |
12509 | key = "signed" ; |
12510 | loc = declspecs->locations[ds_signed]; |
12511 | } |
12512 | else if (longlong) |
12513 | { |
12514 | key = "long long" ; |
12515 | loc = declspecs->locations[ds_long_long]; |
12516 | } |
12517 | else if (long_p) |
12518 | { |
12519 | key = "long" ; |
12520 | loc = declspecs->locations[ds_long]; |
12521 | } |
12522 | else /* if (short_p) */ |
12523 | { |
12524 | key = "short" ; |
12525 | loc = declspecs->locations[ds_short]; |
12526 | } |
12527 | |
12528 | int ok = 0; |
12529 | |
12530 | if (signed_p && unsigned_p) |
12531 | { |
12532 | gcc_rich_location richloc (declspecs->locations[ds_signed]); |
12533 | richloc.add_range (loc: declspecs->locations[ds_unsigned]); |
12534 | error_at (&richloc, |
12535 | "%<signed%> and %<unsigned%> specified together" ); |
12536 | } |
12537 | else if (long_p && short_p) |
12538 | { |
12539 | gcc_rich_location richloc (declspecs->locations[ds_long]); |
12540 | richloc.add_range (loc: declspecs->locations[ds_short]); |
12541 | error_at (&richloc, "%<long%> and %<short%> specified together" ); |
12542 | } |
12543 | else if (TREE_CODE (type) != INTEGER_TYPE |
12544 | || type == char8_type_node |
12545 | || type == char16_type_node |
12546 | || type == char32_type_node |
12547 | || ((long_p || short_p) |
12548 | && (explicit_char || explicit_intN))) |
12549 | error_at (loc, "%qs specified with %qT" , key, type); |
12550 | else if (!explicit_int && !defaulted_int |
12551 | && !explicit_char && !explicit_intN) |
12552 | { |
12553 | if (typedef_decl) |
12554 | { |
12555 | pedwarn (loc, OPT_Wpedantic, |
12556 | "%qs specified with typedef-name %qD" , |
12557 | key, typedef_decl); |
12558 | ok = !flag_pedantic_errors; |
12559 | /* PR108099: __int128_t comes from c_common_nodes_and_builtins, |
12560 | and is not built as a typedef. */ |
12561 | if (is_typedef_decl (x: typedef_decl)) |
12562 | type = DECL_ORIGINAL_TYPE (typedef_decl); |
12563 | } |
12564 | else if (declspecs->decltype_p) |
12565 | error_at (loc, "%qs specified with %<decltype%>" , key); |
12566 | else |
12567 | error_at (loc, "%qs specified with %<typeof%>" , key); |
12568 | } |
12569 | else |
12570 | ok = 1; |
12571 | |
12572 | /* Discard the type modifiers if they are invalid. */ |
12573 | if (! ok) |
12574 | { |
12575 | unsigned_p = false; |
12576 | signed_p = false; |
12577 | long_p = false; |
12578 | short_p = false; |
12579 | longlong = 0; |
12580 | } |
12581 | } |
12582 | |
12583 | /* Decide whether an integer type is signed or not. |
12584 | Optionally treat bitfields as signed by default. */ |
12585 | if (unsigned_p |
12586 | /* [class.bit] |
12587 | |
12588 | It is implementation-defined whether a plain (neither |
12589 | explicitly signed or unsigned) char, short, int, or long |
12590 | bit-field is signed or unsigned. |
12591 | |
12592 | Naturally, we extend this to long long as well. Note that |
12593 | this does not include wchar_t. */ |
12594 | || (bitfield && !flag_signed_bitfields |
12595 | && !signed_p |
12596 | /* A typedef for plain `int' without `signed' can be |
12597 | controlled just like plain `int', but a typedef for |
12598 | `signed int' cannot be so controlled. */ |
12599 | && !(typedef_decl |
12600 | && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)) |
12601 | && TREE_CODE (type) == INTEGER_TYPE |
12602 | && !same_type_p (TYPE_MAIN_VARIANT (type), wchar_type_node))) |
12603 | { |
12604 | if (explicit_intN) |
12605 | type = int_n_trees[declspecs->int_n_idx].unsigned_type; |
12606 | else if (longlong) |
12607 | type = long_long_unsigned_type_node; |
12608 | else if (long_p) |
12609 | type = long_unsigned_type_node; |
12610 | else if (short_p) |
12611 | type = short_unsigned_type_node; |
12612 | else if (type == char_type_node) |
12613 | type = unsigned_char_type_node; |
12614 | else if (typedef_decl) |
12615 | type = c_common_unsigned_type (type); |
12616 | else |
12617 | type = unsigned_type_node; |
12618 | } |
12619 | else if (signed_p && type == char_type_node) |
12620 | type = signed_char_type_node; |
12621 | else if (explicit_intN) |
12622 | type = int_n_trees[declspecs->int_n_idx].signed_type; |
12623 | else if (longlong) |
12624 | type = long_long_integer_type_node; |
12625 | else if (long_p) |
12626 | type = long_integer_type_node; |
12627 | else if (short_p) |
12628 | type = short_integer_type_node; |
12629 | else if (signed_p && typedef_decl) |
12630 | type = c_common_signed_type (type); |
12631 | |
12632 | if (decl_spec_seq_has_spec_p (declspecs, ds_complex)) |
12633 | { |
12634 | if (TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE) |
12635 | error_at (declspecs->locations[ds_complex], |
12636 | "complex invalid for %qs" , name); |
12637 | /* If a modifier is specified, the resulting complex is the complex |
12638 | form of TYPE. E.g, "complex short" is "complex short int". */ |
12639 | else if (type == integer_type_node) |
12640 | type = complex_integer_type_node; |
12641 | else if (type == float_type_node) |
12642 | type = complex_float_type_node; |
12643 | else if (type == double_type_node) |
12644 | type = complex_double_type_node; |
12645 | else if (type == long_double_type_node) |
12646 | type = complex_long_double_type_node; |
12647 | else |
12648 | type = build_complex_type (type); |
12649 | } |
12650 | |
12651 | /* If we're using the injected-class-name to form a compound type or a |
12652 | declaration, replace it with the underlying class so we don't get |
12653 | redundant typedefs in the debug output. But if we are returning the |
12654 | type unchanged, leave it alone so that it's available to |
12655 | maybe_get_template_decl_from_type_decl. */ |
12656 | if (CLASS_TYPE_P (type) |
12657 | && DECL_SELF_REFERENCE_P (TYPE_NAME (type)) |
12658 | && type == TREE_TYPE (TYPE_NAME (type)) |
12659 | && (declarator || type_quals)) |
12660 | type = DECL_ORIGINAL_TYPE (TYPE_NAME (type)); |
12661 | |
12662 | type_quals |= cp_type_quals (type); |
12663 | type = cp_build_qualified_type |
12664 | (type, type_quals, ((((typedef_decl && !DECL_ARTIFICIAL (typedef_decl)) |
12665 | || declspecs->decltype_p) |
12666 | ? tf_ignore_bad_quals : 0) | tf_warning_or_error)); |
12667 | /* We might have ignored or rejected some of the qualifiers. */ |
12668 | type_quals = cp_type_quals (type); |
12669 | |
12670 | if (cxx_dialect >= cxx17 && type && is_auto (type) |
12671 | && innermost_code != cdk_function |
12672 | /* Placeholder in parm gets a better error below. */ |
12673 | && !(decl_context == PARM || decl_context == CATCHPARM) |
12674 | && id_declarator && declarator != id_declarator) |
12675 | if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (type)) |
12676 | { |
12677 | auto_diagnostic_group g; |
12678 | gcc_rich_location richloc (typespec_loc); |
12679 | richloc.add_fixit_insert_after (new_content: "<>" ); |
12680 | error_at (&richloc, "missing template argument list after %qE; " |
12681 | "for deduction, template placeholder must be followed " |
12682 | "by a simple declarator-id" , tmpl); |
12683 | inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here" , tmpl); |
12684 | type = error_mark_node; |
12685 | } |
12686 | |
12687 | staticp = 0; |
12688 | inlinep = decl_spec_seq_has_spec_p (declspecs, ds_inline); |
12689 | virtualp = decl_spec_seq_has_spec_p (declspecs, ds_virtual); |
12690 | explicitp = decl_spec_seq_has_spec_p (declspecs, ds_explicit); |
12691 | |
12692 | storage_class = declspecs->storage_class; |
12693 | if (storage_class == sc_static) |
12694 | staticp = 1 + (decl_context == FIELD); |
12695 | else if (decl_context == FIELD && sfk == sfk_deduction_guide) |
12696 | /* Treat class-scope deduction guides as static member functions |
12697 | so that they get a FUNCTION_TYPE instead of a METHOD_TYPE. */ |
12698 | staticp = 2; |
12699 | |
12700 | if (virtualp) |
12701 | { |
12702 | if (staticp == 2) |
12703 | { |
12704 | gcc_rich_location richloc (declspecs->locations[ds_virtual]); |
12705 | richloc.add_range (loc: declspecs->locations[ds_storage_class]); |
12706 | error_at (&richloc, "member %qD cannot be declared both %<virtual%> " |
12707 | "and %<static%>" , dname); |
12708 | storage_class = sc_none; |
12709 | staticp = 0; |
12710 | } |
12711 | if (constexpr_p && pedantic && cxx_dialect < cxx20) |
12712 | { |
12713 | gcc_rich_location richloc (declspecs->locations[ds_virtual]); |
12714 | richloc.add_range (loc: declspecs->locations[ds_constexpr]); |
12715 | pedwarn (&richloc, OPT_Wc__20_extensions, "member %qD can be " |
12716 | "declared both %<virtual%> and %<constexpr%> only in " |
12717 | "%<-std=c++20%> or %<-std=gnu++20%>" , dname); |
12718 | } |
12719 | } |
12720 | friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend); |
12721 | |
12722 | /* Issue errors about use of storage classes for parameters. */ |
12723 | if (decl_context == PARM) |
12724 | { |
12725 | if (typedef_p) |
12726 | { |
12727 | error_at (declspecs->locations[ds_typedef], |
12728 | "typedef declaration invalid in parameter declaration" ); |
12729 | return error_mark_node; |
12730 | } |
12731 | else if (template_parm_flag && storage_class != sc_none) |
12732 | { |
12733 | error_at (min_location (loca: declspecs->locations[ds_thread], |
12734 | locb: declspecs->locations[ds_storage_class]), |
12735 | "storage class specified for template parameter %qs" , |
12736 | name); |
12737 | return error_mark_node; |
12738 | } |
12739 | else if (storage_class == sc_static |
12740 | || storage_class == sc_extern |
12741 | || thread_p) |
12742 | { |
12743 | error_at (min_location (loca: declspecs->locations[ds_thread], |
12744 | locb: declspecs->locations[ds_storage_class]), |
12745 | "storage class specified for parameter %qs" , name); |
12746 | return error_mark_node; |
12747 | } |
12748 | |
12749 | /* Function parameters cannot be concept. */ |
12750 | if (concept_p) |
12751 | { |
12752 | error_at (declspecs->locations[ds_concept], |
12753 | "a parameter cannot be declared %qs" , "concept" ); |
12754 | concept_p = 0; |
12755 | constexpr_p = 0; |
12756 | } |
12757 | /* Function parameters cannot be constexpr. If we saw one, moan |
12758 | and pretend it wasn't there. */ |
12759 | else if (constexpr_p) |
12760 | { |
12761 | error_at (declspecs->locations[ds_constexpr], |
12762 | "a parameter cannot be declared %qs" , "constexpr" ); |
12763 | constexpr_p = 0; |
12764 | } |
12765 | if (constinit_p) |
12766 | { |
12767 | error_at (declspecs->locations[ds_constinit], |
12768 | "a parameter cannot be declared %qs" , "constinit" ); |
12769 | constinit_p = 0; |
12770 | } |
12771 | if (consteval_p) |
12772 | { |
12773 | error_at (declspecs->locations[ds_consteval], |
12774 | "a parameter cannot be declared %qs" , "consteval" ); |
12775 | consteval_p = 0; |
12776 | } |
12777 | } |
12778 | |
12779 | /* Give error if `virtual' is used outside of class declaration. */ |
12780 | if (virtualp |
12781 | && (current_class_name == NULL_TREE || decl_context != FIELD)) |
12782 | { |
12783 | error_at (declspecs->locations[ds_virtual], |
12784 | "%<virtual%> outside class declaration" ); |
12785 | virtualp = 0; |
12786 | } |
12787 | |
12788 | if (innermost_code == cdk_decomp) |
12789 | { |
12790 | location_t loc = (declarator->kind == cdk_reference |
12791 | ? declarator->declarator->id_loc : declarator->id_loc); |
12792 | if (inlinep) |
12793 | error_at (declspecs->locations[ds_inline], |
12794 | "structured binding declaration cannot be %qs" , "inline" ); |
12795 | if (typedef_p) |
12796 | error_at (declspecs->locations[ds_typedef], |
12797 | "structured binding declaration cannot be %qs" , "typedef" ); |
12798 | if (constexpr_p && !concept_p) |
12799 | error_at (declspecs->locations[ds_constexpr], "structured " |
12800 | "binding declaration cannot be %qs" , "constexpr" ); |
12801 | if (consteval_p) |
12802 | error_at (declspecs->locations[ds_consteval], "structured " |
12803 | "binding declaration cannot be %qs" , "consteval" ); |
12804 | if (thread_p && cxx_dialect < cxx20) |
12805 | pedwarn (declspecs->locations[ds_thread], OPT_Wc__20_extensions, |
12806 | "structured binding declaration can be %qs only in " |
12807 | "%<-std=c++20%> or %<-std=gnu++20%>" , |
12808 | declspecs->gnu_thread_keyword_p |
12809 | ? "__thread" : "thread_local" ); |
12810 | if (concept_p) |
12811 | error_at (declspecs->locations[ds_concept], |
12812 | "structured binding declaration cannot be %qs" , "concept" ); |
12813 | /* [dcl.struct.bind] "A cv that includes volatile is deprecated." */ |
12814 | if (type_quals & TYPE_QUAL_VOLATILE) |
12815 | warning_at (declspecs->locations[ds_volatile], OPT_Wvolatile, |
12816 | "%<volatile%>-qualified structured binding is deprecated" ); |
12817 | switch (storage_class) |
12818 | { |
12819 | case sc_none: |
12820 | break; |
12821 | case sc_register: |
12822 | error_at (loc, "structured binding declaration cannot be %qs" , |
12823 | "register" ); |
12824 | break; |
12825 | case sc_static: |
12826 | if (cxx_dialect < cxx20) |
12827 | pedwarn (loc, OPT_Wc__20_extensions, |
12828 | "structured binding declaration can be %qs only in " |
12829 | "%<-std=c++20%> or %<-std=gnu++20%>" , "static" ); |
12830 | break; |
12831 | case sc_extern: |
12832 | error_at (loc, "structured binding declaration cannot be %qs" , |
12833 | "extern" ); |
12834 | break; |
12835 | case sc_mutable: |
12836 | error_at (loc, "structured binding declaration cannot be %qs" , |
12837 | "mutable" ); |
12838 | break; |
12839 | case sc_auto: |
12840 | error_at (loc, "structured binding declaration cannot be " |
12841 | "C++98 %<auto%>" ); |
12842 | break; |
12843 | default: |
12844 | gcc_unreachable (); |
12845 | } |
12846 | if (TREE_CODE (type) != TEMPLATE_TYPE_PARM |
12847 | || TYPE_IDENTIFIER (type) != auto_identifier) |
12848 | { |
12849 | if (type != error_mark_node) |
12850 | { |
12851 | auto_diagnostic_group d; |
12852 | error_at (loc, "structured binding declaration cannot have " |
12853 | "type %qT" , type); |
12854 | inform (loc, |
12855 | "type must be cv-qualified %<auto%> or reference to " |
12856 | "cv-qualified %<auto%>" ); |
12857 | } |
12858 | type = build_qualified_type (make_auto (), type_quals); |
12859 | declspecs->type = type; |
12860 | } |
12861 | else if (PLACEHOLDER_TYPE_CONSTRAINTS_INFO (type)) |
12862 | pedwarn (loc, OPT_Wpedantic, |
12863 | "structured binding declaration cannot have constrained " |
12864 | "%<auto%> type %qT" , type); |
12865 | inlinep = 0; |
12866 | typedef_p = 0; |
12867 | constexpr_p = 0; |
12868 | consteval_p = 0; |
12869 | concept_p = 0; |
12870 | if (storage_class != sc_static) |
12871 | { |
12872 | storage_class = sc_none; |
12873 | declspecs->storage_class = sc_none; |
12874 | } |
12875 | } |
12876 | |
12877 | /* Static anonymous unions are dealt with here. */ |
12878 | if (staticp && decl_context == TYPENAME |
12879 | && declspecs->type |
12880 | && ANON_AGGR_TYPE_P (declspecs->type)) |
12881 | decl_context = FIELD; |
12882 | |
12883 | /* Warn about storage classes that are invalid for certain |
12884 | kinds of declarations (parameters, typenames, etc.). */ |
12885 | if (thread_p |
12886 | && ((storage_class |
12887 | && storage_class != sc_extern |
12888 | && storage_class != sc_static) |
12889 | || typedef_p)) |
12890 | { |
12891 | location_t loc |
12892 | = min_location (loca: declspecs->locations[ds_thread], |
12893 | locb: declspecs->locations[ds_storage_class]); |
12894 | error_at (loc, "multiple storage classes in declaration of %qs" , name); |
12895 | thread_p = false; |
12896 | } |
12897 | if (decl_context != NORMAL |
12898 | && ((storage_class != sc_none |
12899 | && storage_class != sc_mutable) |
12900 | || thread_p)) |
12901 | { |
12902 | if ((decl_context == PARM || decl_context == CATCHPARM) |
12903 | && (storage_class == sc_register |
12904 | || storage_class == sc_auto)) |
12905 | ; |
12906 | else if (typedef_p) |
12907 | ; |
12908 | else if (decl_context == FIELD |
12909 | /* C++ allows static class elements. */ |
12910 | && storage_class == sc_static) |
12911 | /* C++ also allows inlines and signed and unsigned elements, |
12912 | but in those cases we don't come in here. */ |
12913 | ; |
12914 | else |
12915 | { |
12916 | location_t loc |
12917 | = min_location (loca: declspecs->locations[ds_thread], |
12918 | locb: declspecs->locations[ds_storage_class]); |
12919 | if (decl_context == FIELD) |
12920 | error_at (loc, "storage class specified for %qs" , name); |
12921 | else if (decl_context == PARM || decl_context == CATCHPARM) |
12922 | error_at (loc, "storage class specified for parameter %qs" , name); |
12923 | else |
12924 | error_at (loc, "storage class specified for typename" ); |
12925 | if (storage_class == sc_register |
12926 | || storage_class == sc_auto |
12927 | || storage_class == sc_extern |
12928 | || thread_p) |
12929 | storage_class = sc_none; |
12930 | } |
12931 | } |
12932 | else if (storage_class == sc_extern && funcdef_flag |
12933 | && ! toplevel_bindings_p ()) |
12934 | error ("nested function %qs declared %<extern%>" , name); |
12935 | else if (toplevel_bindings_p ()) |
12936 | { |
12937 | if (storage_class == sc_auto) |
12938 | error_at (declspecs->locations[ds_storage_class], |
12939 | "top-level declaration of %qs specifies %<auto%>" , name); |
12940 | } |
12941 | else if (thread_p |
12942 | && storage_class != sc_extern |
12943 | && storage_class != sc_static) |
12944 | { |
12945 | if (declspecs->gnu_thread_keyword_p) |
12946 | pedwarn (declspecs->locations[ds_thread], |
12947 | 0, "function-scope %qs implicitly auto and " |
12948 | "declared %<__thread%>" , name); |
12949 | |
12950 | /* When thread_local is applied to a variable of block scope the |
12951 | storage-class-specifier static is implied if it does not appear |
12952 | explicitly. */ |
12953 | storage_class = declspecs->storage_class = sc_static; |
12954 | staticp = 1; |
12955 | } |
12956 | |
12957 | if (storage_class && friendp) |
12958 | { |
12959 | error_at (min_location (loca: declspecs->locations[ds_thread], |
12960 | locb: declspecs->locations[ds_storage_class]), |
12961 | "storage class specifiers invalid in friend function " |
12962 | "declarations" ); |
12963 | storage_class = sc_none; |
12964 | staticp = 0; |
12965 | } |
12966 | |
12967 | if (!id_declarator) |
12968 | unqualified_id = NULL_TREE; |
12969 | else |
12970 | { |
12971 | unqualified_id = id_declarator->u.id.unqualified_name; |
12972 | switch (TREE_CODE (unqualified_id)) |
12973 | { |
12974 | case BIT_NOT_EXPR: |
12975 | unqualified_id = TREE_OPERAND (unqualified_id, 0); |
12976 | if (TYPE_P (unqualified_id)) |
12977 | unqualified_id = constructor_name (unqualified_id); |
12978 | break; |
12979 | |
12980 | case IDENTIFIER_NODE: |
12981 | case TEMPLATE_ID_EXPR: |
12982 | break; |
12983 | |
12984 | default: |
12985 | gcc_unreachable (); |
12986 | } |
12987 | } |
12988 | |
12989 | if (declspecs->std_attributes |
12990 | && !diagnose_misapplied_contracts (declspecs->std_attributes)) |
12991 | { |
12992 | location_t attr_loc = declspecs->locations[ds_std_attribute]; |
12993 | if (warning_at (attr_loc, OPT_Wattributes, "attribute ignored" )) |
12994 | inform (attr_loc, "an attribute that appertains to a type-specifier " |
12995 | "is ignored" ); |
12996 | } |
12997 | |
12998 | if (attrlist) |
12999 | diagnose_misapplied_contracts (*attrlist); |
13000 | |
13001 | /* Determine the type of the entity declared by recurring on the |
13002 | declarator. */ |
13003 | for (; declarator; declarator = declarator->declarator) |
13004 | { |
13005 | const cp_declarator *inner_declarator; |
13006 | tree attrs; |
13007 | |
13008 | if (type == error_mark_node) |
13009 | return error_mark_node; |
13010 | |
13011 | attrs = declarator->attributes; |
13012 | if (attrs) |
13013 | { |
13014 | int attr_flags; |
13015 | |
13016 | attr_flags = 0; |
13017 | if (declarator->kind == cdk_id) |
13018 | attr_flags |= (int) ATTR_FLAG_DECL_NEXT; |
13019 | if (declarator->kind == cdk_function) |
13020 | attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT; |
13021 | if (declarator->kind == cdk_array) |
13022 | attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT; |
13023 | tree late_attrs = NULL_TREE; |
13024 | if (decl_context != PARM && decl_context != TYPENAME) |
13025 | /* Assume that any attributes that get applied late to |
13026 | templates will DTRT when applied to the declaration |
13027 | as a whole. */ |
13028 | late_attrs = splice_template_attributes (&attrs, type); |
13029 | returned_attrs = decl_attributes (&type, |
13030 | attr_chainon (returned_attrs, |
13031 | attrs), |
13032 | attr_flags); |
13033 | returned_attrs = attr_chainon (late_attrs, returned_attrs); |
13034 | } |
13035 | |
13036 | inner_declarator = declarator->declarator; |
13037 | |
13038 | /* Check that contracts aren't misapplied. */ |
13039 | if (tree contract_attr = find_contract (attrs: declarator->std_attributes)) |
13040 | if (declarator->kind != cdk_function |
13041 | || innermost_code != cdk_function) |
13042 | diagnose_misapplied_contracts (contract_attr); |
13043 | |
13044 | /* We don't want to warn in parameter context because we don't |
13045 | yet know if the parse will succeed, and this might turn out |
13046 | to be a constructor call. */ |
13047 | if (decl_context != PARM |
13048 | && decl_context != TYPENAME |
13049 | && !typedef_p |
13050 | && declarator->parenthesized != UNKNOWN_LOCATION |
13051 | /* If the type is class-like and the inner name used a |
13052 | global namespace qualifier, we need the parens. |
13053 | Unfortunately all we can tell is whether a qualified name |
13054 | was used or not. */ |
13055 | && !(inner_declarator |
13056 | && inner_declarator->kind == cdk_id |
13057 | && inner_declarator->u.id.qualifying_scope |
13058 | && (MAYBE_CLASS_TYPE_P (type) |
13059 | || TREE_CODE (type) == ENUMERAL_TYPE))) |
13060 | { |
13061 | if (warning_at (declarator->parenthesized, OPT_Wparentheses, |
13062 | "unnecessary parentheses in declaration of %qs" , |
13063 | name)) |
13064 | { |
13065 | gcc_rich_location iloc (declarator->parenthesized); |
13066 | iloc.add_fixit_remove (where: get_start (loc: declarator->parenthesized)); |
13067 | iloc.add_fixit_remove (where: get_finish (loc: declarator->parenthesized)); |
13068 | inform (&iloc, "remove parentheses" ); |
13069 | } |
13070 | } |
13071 | if (declarator->kind == cdk_id || declarator->kind == cdk_decomp) |
13072 | break; |
13073 | |
13074 | switch (declarator->kind) |
13075 | { |
13076 | case cdk_array: |
13077 | type = create_array_type_for_decl (name: dname, type, |
13078 | size: declarator->u.array.bounds, |
13079 | loc: declarator->id_loc); |
13080 | if (!valid_array_size_p (dname |
13081 | ? declarator->id_loc : input_location, |
13082 | type, dname)) |
13083 | type = error_mark_node; |
13084 | |
13085 | if (declarator->std_attributes) |
13086 | /* [dcl.array]/1: |
13087 | |
13088 | The optional attribute-specifier-seq appertains to the |
13089 | array. */ |
13090 | returned_attrs = attr_chainon (returned_attrs, |
13091 | declarator->std_attributes); |
13092 | break; |
13093 | |
13094 | case cdk_function: |
13095 | { |
13096 | tree arg_types; |
13097 | int funcdecl_p; |
13098 | |
13099 | /* Declaring a function type. */ |
13100 | |
13101 | /* Pick up type qualifiers which should be applied to `this'. */ |
13102 | memfn_quals = declarator->u.function.qualifiers; |
13103 | /* Pick up virt-specifiers. */ |
13104 | virt_specifiers = declarator->u.function.virt_specifiers; |
13105 | /* And ref-qualifier, too */ |
13106 | rqual = declarator->u.function.ref_qualifier; |
13107 | /* And tx-qualifier. */ |
13108 | tree tx_qual = declarator->u.function.tx_qualifier; |
13109 | /* Pick up the exception specifications. */ |
13110 | raises = declarator->u.function.exception_specification; |
13111 | /* If the exception-specification is ill-formed, let's pretend |
13112 | there wasn't one. */ |
13113 | if (raises == error_mark_node) |
13114 | raises = NULL_TREE; |
13115 | |
13116 | if (reqs) |
13117 | error_at (location_of (reqs), "requires-clause on return type" ); |
13118 | reqs = declarator->u.function.requires_clause; |
13119 | |
13120 | /* Say it's a definition only for the CALL_EXPR |
13121 | closest to the identifier. */ |
13122 | funcdecl_p = inner_declarator && inner_declarator->kind == cdk_id; |
13123 | |
13124 | /* Handle a late-specified return type. */ |
13125 | tree late_return_type = declarator->u.function.late_return_type; |
13126 | if (tree auto_node = type_uses_auto (type)) |
13127 | { |
13128 | if (!late_return_type) |
13129 | { |
13130 | if (!funcdecl_p) |
13131 | /* auto (*fp)() = f; is OK. */; |
13132 | else if (current_class_type |
13133 | && LAMBDA_TYPE_P (current_class_type)) |
13134 | /* OK for C++11 lambdas. */; |
13135 | else if (cxx_dialect < cxx14) |
13136 | { |
13137 | error_at (typespec_loc, "%qs function uses " |
13138 | "%<auto%> type specifier without " |
13139 | "trailing return type" , name); |
13140 | inform (typespec_loc, |
13141 | "deduced return type only available " |
13142 | "with %<-std=c++14%> or %<-std=gnu++14%>" ); |
13143 | } |
13144 | else if (virtualp) |
13145 | { |
13146 | error_at (typespec_loc, "virtual function " |
13147 | "cannot have deduced return type" ); |
13148 | virtualp = false; |
13149 | } |
13150 | } |
13151 | else if (!is_auto (type) && sfk != sfk_conversion) |
13152 | { |
13153 | error_at (typespec_loc, "%qs function with trailing " |
13154 | "return type has %qT as its type rather " |
13155 | "than plain %<auto%>" , name, type); |
13156 | return error_mark_node; |
13157 | } |
13158 | else if (is_auto (type) && AUTO_IS_DECLTYPE (type)) |
13159 | { |
13160 | if (funcdecl_p) |
13161 | error_at (typespec_loc, |
13162 | "%qs function with trailing return type " |
13163 | "has %<decltype(auto)%> as its type " |
13164 | "rather than plain %<auto%>" , name); |
13165 | else |
13166 | error_at (typespec_loc, |
13167 | "invalid use of %<decltype(auto)%>" ); |
13168 | return error_mark_node; |
13169 | } |
13170 | tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node); |
13171 | if (!tmpl) |
13172 | if (tree late_auto = type_uses_auto (late_return_type)) |
13173 | tmpl = CLASS_PLACEHOLDER_TEMPLATE (late_auto); |
13174 | if (tmpl) |
13175 | { |
13176 | if (!funcdecl_p || !dguide_name_p (unqualified_id)) |
13177 | { |
13178 | auto_diagnostic_group g; |
13179 | error_at (typespec_loc, "deduced class " |
13180 | "type %qD in function return type" , |
13181 | DECL_NAME (tmpl)); |
13182 | inform (DECL_SOURCE_LOCATION (tmpl), |
13183 | "%qD declared here" , tmpl); |
13184 | return error_mark_node; |
13185 | } |
13186 | else if (!late_return_type) |
13187 | { |
13188 | error_at (declarator->id_loc, "deduction guide " |
13189 | "for %qT must have trailing return " |
13190 | "type" , TREE_TYPE (tmpl)); |
13191 | inform (DECL_SOURCE_LOCATION (tmpl), |
13192 | "%qD declared here" , tmpl); |
13193 | return error_mark_node; |
13194 | } |
13195 | else if (CLASS_TYPE_P (late_return_type) |
13196 | && CLASSTYPE_TEMPLATE_INFO (late_return_type) |
13197 | && (CLASSTYPE_TI_TEMPLATE (late_return_type) |
13198 | == tmpl)) |
13199 | /* OK */; |
13200 | else |
13201 | error ("trailing return type %qT of deduction guide " |
13202 | "is not a specialization of %qT" , |
13203 | late_return_type, TREE_TYPE (tmpl)); |
13204 | } |
13205 | } |
13206 | else if (late_return_type |
13207 | && sfk != sfk_conversion) |
13208 | { |
13209 | if (late_return_type == error_mark_node) |
13210 | return error_mark_node; |
13211 | if (cxx_dialect < cxx11) |
13212 | /* Not using maybe_warn_cpp0x because this should |
13213 | always be an error. */ |
13214 | error_at (typespec_loc, |
13215 | "trailing return type only available " |
13216 | "with %<-std=c++11%> or %<-std=gnu++11%>" ); |
13217 | else |
13218 | error_at (typespec_loc, "%qs function with trailing " |
13219 | "return type not declared with %<auto%> " |
13220 | "type specifier" , name); |
13221 | return error_mark_node; |
13222 | } |
13223 | if (late_return_type && sfk == sfk_conversion) |
13224 | { |
13225 | error ("a conversion function cannot have a trailing return type" ); |
13226 | return error_mark_node; |
13227 | } |
13228 | type = splice_late_return_type (type, late_return_type); |
13229 | if (type == error_mark_node) |
13230 | return error_mark_node; |
13231 | |
13232 | if (late_return_type) |
13233 | { |
13234 | late_return_type_p = true; |
13235 | type_quals = cp_type_quals (type); |
13236 | } |
13237 | |
13238 | if (type_quals != TYPE_UNQUALIFIED) |
13239 | { |
13240 | /* It's wrong, for instance, to issue a -Wignored-qualifiers |
13241 | warning for |
13242 | static_assert(!is_same_v<void(*)(), const void(*)()>); |
13243 | because there the qualifier matters. */ |
13244 | if (funcdecl_p && (SCALAR_TYPE_P (type) || VOID_TYPE_P (type))) |
13245 | warning_at (typespec_loc, OPT_Wignored_qualifiers, "type " |
13246 | "qualifiers ignored on function return type" ); |
13247 | /* [dcl.fct] "A volatile-qualified return type is |
13248 | deprecated." */ |
13249 | if (type_quals & TYPE_QUAL_VOLATILE) |
13250 | warning_at (typespec_loc, OPT_Wvolatile, |
13251 | "%<volatile%>-qualified return type is " |
13252 | "deprecated" ); |
13253 | |
13254 | /* We now know that the TYPE_QUALS don't apply to the |
13255 | decl, but to its return type. */ |
13256 | type_quals = TYPE_UNQUALIFIED; |
13257 | } |
13258 | |
13259 | /* Error about some types functions can't return. */ |
13260 | |
13261 | if (TREE_CODE (type) == FUNCTION_TYPE) |
13262 | { |
13263 | error_at (typespec_loc, "%qs declared as function returning " |
13264 | "a function" , name); |
13265 | return error_mark_node; |
13266 | } |
13267 | if (TREE_CODE (type) == ARRAY_TYPE) |
13268 | { |
13269 | error_at (typespec_loc, "%qs declared as function returning " |
13270 | "an array" , name); |
13271 | return error_mark_node; |
13272 | } |
13273 | if (constinit_p && funcdecl_p) |
13274 | { |
13275 | error_at (declspecs->locations[ds_constinit], |
13276 | "%<constinit%> on function return type is not " |
13277 | "allowed" ); |
13278 | return error_mark_node; |
13279 | } |
13280 | |
13281 | if (check_decltype_auto (loc: typespec_loc, type)) |
13282 | return error_mark_node; |
13283 | |
13284 | if (ctype == NULL_TREE |
13285 | && decl_context == FIELD |
13286 | && funcdecl_p |
13287 | && friendp == 0) |
13288 | ctype = current_class_type; |
13289 | |
13290 | if (ctype && (sfk == sfk_constructor |
13291 | || sfk == sfk_destructor)) |
13292 | { |
13293 | /* We are within a class's scope. If our declarator name |
13294 | is the same as the class name, and we are defining |
13295 | a function, then it is a constructor/destructor, and |
13296 | therefore returns a void type. */ |
13297 | |
13298 | /* ISO C++ 12.4/2. A destructor may not be declared |
13299 | const or volatile. A destructor may not be static. |
13300 | A destructor may not be declared with ref-qualifier. |
13301 | |
13302 | ISO C++ 12.1. A constructor may not be declared |
13303 | const or volatile. A constructor may not be |
13304 | virtual. A constructor may not be static. |
13305 | A constructor may not be declared with ref-qualifier. */ |
13306 | if (staticp == 2) |
13307 | error_at (declspecs->locations[ds_storage_class], |
13308 | (flags == DTOR_FLAG) |
13309 | ? G_("destructor cannot be static member " |
13310 | "function" ) |
13311 | : G_("constructor cannot be static member " |
13312 | "function" )); |
13313 | if (memfn_quals) |
13314 | { |
13315 | error ((flags == DTOR_FLAG) |
13316 | ? G_("destructors may not be cv-qualified" ) |
13317 | : G_("constructors may not be cv-qualified" )); |
13318 | memfn_quals = TYPE_UNQUALIFIED; |
13319 | } |
13320 | |
13321 | if (rqual) |
13322 | { |
13323 | maybe_warn_cpp0x (str: CPP0X_REF_QUALIFIER); |
13324 | error ((flags == DTOR_FLAG) |
13325 | ? G_("destructors may not be ref-qualified" ) |
13326 | : G_("constructors may not be ref-qualified" )); |
13327 | rqual = REF_QUAL_NONE; |
13328 | } |
13329 | |
13330 | if (decl_context == FIELD |
13331 | && !member_function_or_else (ctype, |
13332 | current_class_type, |
13333 | flags)) |
13334 | return error_mark_node; |
13335 | |
13336 | if (flags != DTOR_FLAG) |
13337 | { |
13338 | /* It's a constructor. */ |
13339 | if (explicitp == 1) |
13340 | explicitp = 2; |
13341 | if (virtualp) |
13342 | { |
13343 | permerror (declspecs->locations[ds_virtual], |
13344 | "constructors cannot be declared %<virtual%>" ); |
13345 | virtualp = 0; |
13346 | } |
13347 | if (decl_context == FIELD |
13348 | && sfk != sfk_constructor) |
13349 | return error_mark_node; |
13350 | } |
13351 | if (decl_context == FIELD) |
13352 | staticp = 0; |
13353 | } |
13354 | else if (friendp) |
13355 | { |
13356 | if (virtualp) |
13357 | { |
13358 | /* Cannot be both friend and virtual. */ |
13359 | gcc_rich_location richloc (declspecs->locations[ds_virtual]); |
13360 | richloc.add_range (loc: declspecs->locations[ds_friend]); |
13361 | error_at (&richloc, "virtual functions cannot be friends" ); |
13362 | friendp = 0; |
13363 | } |
13364 | if (decl_context == NORMAL) |
13365 | error_at (declarator->id_loc, |
13366 | "friend declaration not in class definition" ); |
13367 | if (current_function_decl && funcdef_flag) |
13368 | { |
13369 | error_at (declarator->id_loc, |
13370 | "cannot define friend function %qs in a local " |
13371 | "class definition" , name); |
13372 | friendp = 0; |
13373 | } |
13374 | /* [class.friend]/6: A function can be defined in a friend |
13375 | declaration if the function name is unqualified. */ |
13376 | if (funcdef_flag && in_namespace) |
13377 | { |
13378 | if (in_namespace == global_namespace) |
13379 | error_at (declarator->id_loc, |
13380 | "friend function definition %qs cannot have " |
13381 | "a name qualified with %<::%>" , name); |
13382 | else |
13383 | error_at (declarator->id_loc, |
13384 | "friend function definition %qs cannot have " |
13385 | "a name qualified with %<%D::%>" , name, |
13386 | in_namespace); |
13387 | } |
13388 | } |
13389 | else if (ctype && sfk == sfk_conversion) |
13390 | { |
13391 | if (explicitp == 1) |
13392 | { |
13393 | maybe_warn_cpp0x (str: CPP0X_EXPLICIT_CONVERSION); |
13394 | explicitp = 2; |
13395 | } |
13396 | } |
13397 | else if (sfk == sfk_deduction_guide) |
13398 | { |
13399 | if (explicitp == 1) |
13400 | explicitp = 2; |
13401 | } |
13402 | |
13403 | tree pushed_scope = NULL_TREE; |
13404 | if (funcdecl_p |
13405 | && decl_context != FIELD |
13406 | && inner_declarator->u.id.qualifying_scope |
13407 | && CLASS_TYPE_P (inner_declarator->u.id.qualifying_scope)) |
13408 | pushed_scope |
13409 | = push_scope (inner_declarator->u.id.qualifying_scope); |
13410 | |
13411 | arg_types = grokparms (declarator->u.function.parameters, &parms); |
13412 | |
13413 | if (pushed_scope) |
13414 | pop_scope (pushed_scope); |
13415 | |
13416 | if (inner_declarator |
13417 | && inner_declarator->kind == cdk_id |
13418 | && inner_declarator->u.id.sfk == sfk_destructor |
13419 | && arg_types != void_list_node) |
13420 | { |
13421 | error_at (declarator->id_loc, |
13422 | "destructors may not have parameters" ); |
13423 | arg_types = void_list_node; |
13424 | parms = NULL_TREE; |
13425 | } |
13426 | |
13427 | type = build_function_type (type, arg_types); |
13428 | |
13429 | tree attrs = declarator->std_attributes; |
13430 | if (tx_qual) |
13431 | { |
13432 | tree att = build_tree_list (tx_qual, NULL_TREE); |
13433 | /* transaction_safe applies to the type, but |
13434 | transaction_safe_dynamic applies to the function. */ |
13435 | if (is_attribute_p (attr_name: "transaction_safe" , ident: tx_qual)) |
13436 | attrs = attr_chainon (attrs, att); |
13437 | else |
13438 | returned_attrs = attr_chainon (returned_attrs, att); |
13439 | } |
13440 | |
13441 | /* Actually apply the contract attributes to the declaration. */ |
13442 | for (tree *p = &attrs; *p;) |
13443 | { |
13444 | tree l = *p; |
13445 | if (cxx_contract_attribute_p (l)) |
13446 | { |
13447 | *p = TREE_CHAIN (l); |
13448 | /* Intentionally reverse order of contracts so they're |
13449 | reversed back into their lexical order. */ |
13450 | TREE_CHAIN (l) = NULL_TREE; |
13451 | returned_attrs = chainon (l, returned_attrs); |
13452 | } |
13453 | else |
13454 | p = &TREE_CHAIN (l); |
13455 | } |
13456 | |
13457 | if (attrs) |
13458 | /* [dcl.fct]/2: |
13459 | |
13460 | The optional attribute-specifier-seq appertains to |
13461 | the function type. */ |
13462 | cplus_decl_attributes (&type, attrs, 0); |
13463 | |
13464 | if (raises) |
13465 | type = build_exception_variant (type, raises); |
13466 | } |
13467 | break; |
13468 | |
13469 | case cdk_pointer: |
13470 | case cdk_reference: |
13471 | case cdk_ptrmem: |
13472 | /* Filter out pointers-to-references and references-to-references. |
13473 | We can get these if a TYPE_DECL is used. */ |
13474 | |
13475 | if (TYPE_REF_P (type)) |
13476 | { |
13477 | if (declarator->kind != cdk_reference) |
13478 | { |
13479 | error ("cannot declare pointer to %q#T" , type); |
13480 | type = TREE_TYPE (type); |
13481 | } |
13482 | |
13483 | /* In C++0x, we allow reference to reference declarations |
13484 | that occur indirectly through typedefs [7.1.3/8 dcl.typedef] |
13485 | and template type arguments [14.3.1/4 temp.arg.type]. The |
13486 | check for direct reference to reference declarations, which |
13487 | are still forbidden, occurs below. Reasoning behind the change |
13488 | can be found in DR106, DR540, and the rvalue reference |
13489 | proposals. */ |
13490 | else if (cxx_dialect == cxx98) |
13491 | { |
13492 | error ("cannot declare reference to %q#T" , type); |
13493 | type = TREE_TYPE (type); |
13494 | } |
13495 | } |
13496 | else if (VOID_TYPE_P (type)) |
13497 | { |
13498 | if (declarator->kind == cdk_reference) |
13499 | error ("cannot declare reference to %q#T" , type); |
13500 | else if (declarator->kind == cdk_ptrmem) |
13501 | error ("cannot declare pointer to %q#T member" , type); |
13502 | } |
13503 | |
13504 | /* We now know that the TYPE_QUALS don't apply to the decl, |
13505 | but to the target of the pointer. */ |
13506 | type_quals = TYPE_UNQUALIFIED; |
13507 | |
13508 | /* This code used to handle METHOD_TYPE, but I don't think it's |
13509 | possible to get it here anymore. */ |
13510 | gcc_assert (TREE_CODE (type) != METHOD_TYPE); |
13511 | if (declarator->kind == cdk_ptrmem |
13512 | && TREE_CODE (type) == FUNCTION_TYPE) |
13513 | { |
13514 | memfn_quals |= type_memfn_quals (type); |
13515 | type = build_memfn_type (type, |
13516 | declarator->u.pointer.class_type, |
13517 | memfn_quals, |
13518 | rqual); |
13519 | if (type == error_mark_node) |
13520 | return error_mark_node; |
13521 | |
13522 | rqual = REF_QUAL_NONE; |
13523 | memfn_quals = TYPE_UNQUALIFIED; |
13524 | } |
13525 | |
13526 | if (TREE_CODE (type) == FUNCTION_TYPE |
13527 | && (type_memfn_quals (type) != TYPE_UNQUALIFIED |
13528 | || type_memfn_rqual (type) != REF_QUAL_NONE)) |
13529 | error (declarator->kind == cdk_reference |
13530 | ? G_("cannot declare reference to qualified function type %qT" ) |
13531 | : G_("cannot declare pointer to qualified function type %qT" ), |
13532 | type); |
13533 | |
13534 | /* When the pointed-to type involves components of variable size, |
13535 | care must be taken to ensure that the size evaluation code is |
13536 | emitted early enough to dominate all the possible later uses |
13537 | and late enough for the variables on which it depends to have |
13538 | been assigned. |
13539 | |
13540 | This is expected to happen automatically when the pointed-to |
13541 | type has a name/declaration of it's own, but special attention |
13542 | is required if the type is anonymous. |
13543 | |
13544 | We handle the NORMAL and FIELD contexts here by inserting a |
13545 | dummy statement that just evaluates the size at a safe point |
13546 | and ensures it is not deferred until e.g. within a deeper |
13547 | conditional context (c++/43555). |
13548 | |
13549 | We expect nothing to be needed here for PARM or TYPENAME. |
13550 | Evaluating the size at this point for TYPENAME would |
13551 | actually be incorrect, as we might be in the middle of an |
13552 | expression with side effects on the pointed-to type size |
13553 | "arguments" prior to the pointer declaration point and the |
13554 | size evaluation could end up prior to the side effects. */ |
13555 | |
13556 | if (!TYPE_NAME (type) |
13557 | && (decl_context == NORMAL || decl_context == FIELD) |
13558 | && at_function_scope_p () |
13559 | && variably_modified_type_p (type, NULL_TREE)) |
13560 | { |
13561 | TYPE_NAME (type) = build_decl (UNKNOWN_LOCATION, TYPE_DECL, |
13562 | NULL_TREE, type); |
13563 | add_decl_expr (TYPE_NAME (type)); |
13564 | } |
13565 | |
13566 | if (declarator->kind == cdk_reference) |
13567 | { |
13568 | /* In C++0x, the type we are creating a reference to might be |
13569 | a typedef which is itself a reference type. In that case, |
13570 | we follow the reference collapsing rules in |
13571 | [7.1.3/8 dcl.typedef] to create the final reference type: |
13572 | |
13573 | "If a typedef TD names a type that is a reference to a type |
13574 | T, an attempt to create the type 'lvalue reference to cv TD' |
13575 | creates the type 'lvalue reference to T,' while an attempt |
13576 | to create the type "rvalue reference to cv TD' creates the |
13577 | type TD." |
13578 | */ |
13579 | if (VOID_TYPE_P (type)) |
13580 | /* We already gave an error. */; |
13581 | else if (TYPE_REF_P (type)) |
13582 | { |
13583 | if (declarator->u.reference.rvalue_ref) |
13584 | /* Leave type alone. */; |
13585 | else |
13586 | type = cp_build_reference_type (TREE_TYPE (type), false); |
13587 | } |
13588 | else |
13589 | type = cp_build_reference_type |
13590 | (type, declarator->u.reference.rvalue_ref); |
13591 | |
13592 | /* In C++0x, we need this check for direct reference to |
13593 | reference declarations, which are forbidden by |
13594 | [8.3.2/5 dcl.ref]. Reference to reference declarations |
13595 | are only allowed indirectly through typedefs and template |
13596 | type arguments. Example: |
13597 | |
13598 | void foo(int & &); // invalid ref-to-ref decl |
13599 | |
13600 | typedef int & int_ref; |
13601 | void foo(int_ref &); // valid ref-to-ref decl |
13602 | */ |
13603 | if (inner_declarator && inner_declarator->kind == cdk_reference) |
13604 | error ("cannot declare reference to %q#T, which is not " |
13605 | "a typedef or a template type argument" , type); |
13606 | } |
13607 | else if (TREE_CODE (type) == METHOD_TYPE) |
13608 | type = build_ptrmemfunc_type (type: build_pointer_type (type)); |
13609 | else if (declarator->kind == cdk_ptrmem) |
13610 | { |
13611 | gcc_assert (TREE_CODE (declarator->u.pointer.class_type) |
13612 | != NAMESPACE_DECL); |
13613 | if (declarator->u.pointer.class_type == error_mark_node) |
13614 | /* We will already have complained. */ |
13615 | type = error_mark_node; |
13616 | else |
13617 | type = build_ptrmem_type (class_type: declarator->u.pointer.class_type, |
13618 | member_type: type); |
13619 | } |
13620 | else |
13621 | type = build_pointer_type (type); |
13622 | |
13623 | /* Process a list of type modifier keywords (such as |
13624 | const or volatile) that were given inside the `*' or `&'. */ |
13625 | |
13626 | if (declarator->u.pointer.qualifiers) |
13627 | { |
13628 | type |
13629 | = cp_build_qualified_type (type, |
13630 | declarator->u.pointer.qualifiers); |
13631 | type_quals = cp_type_quals (type); |
13632 | } |
13633 | |
13634 | /* Apply C++11 attributes to the pointer, and not to the |
13635 | type pointed to. This is unlike what is done for GNU |
13636 | attributes above. It is to comply with [dcl.ptr]/1: |
13637 | |
13638 | [the optional attribute-specifier-seq (7.6.1) appertains |
13639 | to the pointer and not to the object pointed to]. */ |
13640 | if (declarator->std_attributes) |
13641 | decl_attributes (&type, declarator->std_attributes, |
13642 | 0); |
13643 | |
13644 | ctype = NULL_TREE; |
13645 | break; |
13646 | |
13647 | case cdk_error: |
13648 | break; |
13649 | |
13650 | default: |
13651 | gcc_unreachable (); |
13652 | } |
13653 | } |
13654 | |
13655 | id_loc = declarator ? declarator->id_loc : input_location; |
13656 | |
13657 | if (innermost_code != cdk_function |
13658 | /* Don't check this if it can be the artifical decltype(auto) |
13659 | we created when building a constraint in a compound-requirement: |
13660 | that the type-constraint is plain is going to be checked in |
13661 | cp_parser_compound_requirement. */ |
13662 | && decl_context != TYPENAME |
13663 | && check_decltype_auto (loc: id_loc, type)) |
13664 | return error_mark_node; |
13665 | |
13666 | /* A `constexpr' specifier used in an object declaration declares |
13667 | the object as `const'. */ |
13668 | if (constexpr_p && innermost_code != cdk_function) |
13669 | { |
13670 | /* DR1688 says that a `constexpr' specifier in combination with |
13671 | `volatile' is valid. */ |
13672 | |
13673 | if (!TYPE_REF_P (type)) |
13674 | { |
13675 | type_quals |= TYPE_QUAL_CONST; |
13676 | type = cp_build_qualified_type (type, type_quals); |
13677 | } |
13678 | } |
13679 | |
13680 | if (unqualified_id && TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR |
13681 | && !FUNC_OR_METHOD_TYPE_P (type) |
13682 | && !variable_template_p (TREE_OPERAND (unqualified_id, 0))) |
13683 | { |
13684 | error ("template-id %qD used as a declarator" , |
13685 | unqualified_id); |
13686 | unqualified_id = dname; |
13687 | } |
13688 | |
13689 | /* If TYPE is a FUNCTION_TYPE, but the function name was explicitly |
13690 | qualified with a class-name, turn it into a METHOD_TYPE, unless |
13691 | we know that the function is static. We take advantage of this |
13692 | opportunity to do other processing that pertains to entities |
13693 | explicitly declared to be class members. Note that if DECLARATOR |
13694 | is non-NULL, we know it is a cdk_id declarator; otherwise, we |
13695 | would not have exited the loop above. */ |
13696 | if (declarator |
13697 | && declarator->kind == cdk_id |
13698 | && declarator->u.id.qualifying_scope |
13699 | && MAYBE_CLASS_TYPE_P (declarator->u.id.qualifying_scope)) |
13700 | { |
13701 | ctype = declarator->u.id.qualifying_scope; |
13702 | ctype = TYPE_MAIN_VARIANT (ctype); |
13703 | template_count = num_template_headers_for_class (ctype); |
13704 | |
13705 | if (ctype == current_class_type) |
13706 | { |
13707 | if (friendp) |
13708 | { |
13709 | permerror (declspecs->locations[ds_friend], |
13710 | "member functions are implicitly " |
13711 | "friends of their class" ); |
13712 | friendp = 0; |
13713 | } |
13714 | else |
13715 | permerror (id_loc, "extra qualification %<%T::%> on member %qs" , |
13716 | ctype, name); |
13717 | } |
13718 | else if (/* If the qualifying type is already complete, then we |
13719 | can skip the following checks. */ |
13720 | !COMPLETE_TYPE_P (ctype) |
13721 | && (/* If the function is being defined, then |
13722 | qualifying type must certainly be complete. */ |
13723 | funcdef_flag |
13724 | /* A friend declaration of "T::f" is OK, even if |
13725 | "T" is a template parameter. But, if this |
13726 | function is not a friend, the qualifying type |
13727 | must be a class. */ |
13728 | || (!friendp && !CLASS_TYPE_P (ctype)) |
13729 | /* For a declaration, the type need not be |
13730 | complete, if either it is dependent (since there |
13731 | is no meaningful definition of complete in that |
13732 | case) or the qualifying class is currently being |
13733 | defined. */ |
13734 | || !(dependent_type_p (ctype) |
13735 | || currently_open_class (ctype))) |
13736 | /* Check that the qualifying type is complete. */ |
13737 | && !complete_type_or_else (ctype, NULL_TREE)) |
13738 | return error_mark_node; |
13739 | else if (TREE_CODE (type) == FUNCTION_TYPE) |
13740 | { |
13741 | if (current_class_type |
13742 | && (!friendp || funcdef_flag || initialized)) |
13743 | { |
13744 | error_at (id_loc, funcdef_flag || initialized |
13745 | ? G_("cannot define member function %<%T::%s%> " |
13746 | "within %qT" ) |
13747 | : G_("cannot declare member function %<%T::%s%> " |
13748 | "within %qT" ), |
13749 | ctype, name, current_class_type); |
13750 | return error_mark_node; |
13751 | } |
13752 | } |
13753 | else if (typedef_p && current_class_type) |
13754 | { |
13755 | error_at (id_loc, "cannot declare member %<%T::%s%> within %qT" , |
13756 | ctype, name, current_class_type); |
13757 | return error_mark_node; |
13758 | } |
13759 | } |
13760 | |
13761 | if (ctype == NULL_TREE && decl_context == FIELD && friendp == 0) |
13762 | ctype = current_class_type; |
13763 | |
13764 | /* Now TYPE has the actual type. */ |
13765 | |
13766 | if (returned_attrs) |
13767 | { |
13768 | if (attrlist) |
13769 | *attrlist = attr_chainon (returned_attrs, *attrlist); |
13770 | else |
13771 | attrlist = &returned_attrs; |
13772 | } |
13773 | |
13774 | if (declarator |
13775 | && declarator->kind == cdk_id |
13776 | && declarator->std_attributes |
13777 | && attrlist != NULL) |
13778 | { |
13779 | /* [dcl.meaning]/1: The optional attribute-specifier-seq following |
13780 | a declarator-id appertains to the entity that is declared. */ |
13781 | if (declarator->std_attributes != error_mark_node) |
13782 | *attrlist = attr_chainon (declarator->std_attributes, *attrlist); |
13783 | else |
13784 | /* We should have already diagnosed the issue (c++/78344). */ |
13785 | gcc_assert (seen_error ()); |
13786 | } |
13787 | |
13788 | /* Handle parameter packs. */ |
13789 | if (parameter_pack_p) |
13790 | { |
13791 | if (decl_context == PARM) |
13792 | /* Turn the type into a pack expansion.*/ |
13793 | type = make_pack_expansion (type); |
13794 | else |
13795 | error ("non-parameter %qs cannot be a parameter pack" , name); |
13796 | } |
13797 | |
13798 | if ((decl_context == FIELD || decl_context == PARM) |
13799 | && !processing_template_decl |
13800 | && variably_modified_type_p (type, NULL_TREE)) |
13801 | { |
13802 | if (decl_context == FIELD) |
13803 | error_at (id_loc, |
13804 | "data member may not have variably modified type %qT" , type); |
13805 | else |
13806 | error_at (id_loc, |
13807 | "parameter may not have variably modified type %qT" , type); |
13808 | type = error_mark_node; |
13809 | } |
13810 | |
13811 | if (explicitp == 1 || (explicitp && friendp)) |
13812 | { |
13813 | /* [dcl.fct.spec] (C++11) The explicit specifier shall be used only |
13814 | in the declaration of a constructor or conversion function within |
13815 | a class definition. */ |
13816 | if (!current_class_type) |
13817 | error_at (declspecs->locations[ds_explicit], |
13818 | "%<explicit%> outside class declaration" ); |
13819 | else if (friendp) |
13820 | error_at (declspecs->locations[ds_explicit], |
13821 | "%<explicit%> in friend declaration" ); |
13822 | else |
13823 | error_at (declspecs->locations[ds_explicit], |
13824 | "only declarations of constructors and conversion operators " |
13825 | "can be %<explicit%>" ); |
13826 | explicitp = 0; |
13827 | } |
13828 | |
13829 | if (storage_class == sc_mutable) |
13830 | { |
13831 | location_t sloc = declspecs->locations[ds_storage_class]; |
13832 | if (decl_context != FIELD || friendp) |
13833 | { |
13834 | error_at (sloc, "non-member %qs cannot be declared %<mutable%>" , |
13835 | name); |
13836 | storage_class = sc_none; |
13837 | } |
13838 | else if (decl_context == TYPENAME || typedef_p) |
13839 | { |
13840 | error_at (sloc, |
13841 | "non-object member %qs cannot be declared %<mutable%>" , |
13842 | name); |
13843 | storage_class = sc_none; |
13844 | } |
13845 | else if (FUNC_OR_METHOD_TYPE_P (type)) |
13846 | { |
13847 | error_at (sloc, "function %qs cannot be declared %<mutable%>" , |
13848 | name); |
13849 | storage_class = sc_none; |
13850 | } |
13851 | else if (staticp) |
13852 | { |
13853 | error_at (sloc, "%<static%> %qs cannot be declared %<mutable%>" , |
13854 | name); |
13855 | storage_class = sc_none; |
13856 | } |
13857 | else if (type_quals & TYPE_QUAL_CONST) |
13858 | { |
13859 | error_at (sloc, "%<const%> %qs cannot be declared %<mutable%>" , |
13860 | name); |
13861 | storage_class = sc_none; |
13862 | } |
13863 | else if (TYPE_REF_P (type)) |
13864 | { |
13865 | permerror (sloc, "reference %qs cannot be declared %<mutable%>" , |
13866 | name); |
13867 | storage_class = sc_none; |
13868 | } |
13869 | } |
13870 | |
13871 | /* If this is declaring a typedef name, return a TYPE_DECL. */ |
13872 | if (typedef_p && decl_context != TYPENAME) |
13873 | { |
13874 | bool alias_p = decl_spec_seq_has_spec_p (declspecs, ds_alias); |
13875 | tree decl; |
13876 | |
13877 | if (funcdef_flag) |
13878 | { |
13879 | if (decl_context == NORMAL) |
13880 | error_at (id_loc, |
13881 | "typedef may not be a function definition" ); |
13882 | else |
13883 | error_at (id_loc, |
13884 | "typedef may not be a member function definition" ); |
13885 | return error_mark_node; |
13886 | } |
13887 | |
13888 | /* This declaration: |
13889 | |
13890 | typedef void f(int) const; |
13891 | |
13892 | declares a function type which is not a member of any |
13893 | particular class, but which is cv-qualified; for |
13894 | example "f S::*" declares a pointer to a const-qualified |
13895 | member function of S. We record the cv-qualification in the |
13896 | function type. */ |
13897 | if ((rqual || memfn_quals) && TREE_CODE (type) == FUNCTION_TYPE) |
13898 | { |
13899 | type = apply_memfn_quals (type, memfn_quals, rqual); |
13900 | |
13901 | /* We have now dealt with these qualifiers. */ |
13902 | memfn_quals = TYPE_UNQUALIFIED; |
13903 | rqual = REF_QUAL_NONE; |
13904 | } |
13905 | |
13906 | if (type_uses_auto (type)) |
13907 | { |
13908 | if (alias_p) |
13909 | error_at (declspecs->locations[ds_type_spec], |
13910 | "%<auto%> not allowed in alias declaration" ); |
13911 | else |
13912 | error_at (declspecs->locations[ds_type_spec], |
13913 | "typedef declared %<auto%>" ); |
13914 | type = error_mark_node; |
13915 | } |
13916 | |
13917 | if (reqs) |
13918 | error_at (location_of (reqs), "requires-clause on typedef" ); |
13919 | |
13920 | if (id_declarator && declarator->u.id.qualifying_scope) |
13921 | { |
13922 | error_at (id_loc, "typedef name may not be a nested-name-specifier" ); |
13923 | type = error_mark_node; |
13924 | } |
13925 | |
13926 | if (decl_context == FIELD) |
13927 | decl = build_lang_decl_loc (id_loc, TYPE_DECL, unqualified_id, type); |
13928 | else |
13929 | decl = build_decl (id_loc, TYPE_DECL, unqualified_id, type); |
13930 | |
13931 | if (decl_context != FIELD) |
13932 | { |
13933 | if (!current_function_decl) |
13934 | DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace); |
13935 | else if (DECL_MAYBE_IN_CHARGE_CDTOR_P (current_function_decl)) |
13936 | /* The TYPE_DECL is "abstract" because there will be |
13937 | clones of this constructor/destructor, and there will |
13938 | be copies of this TYPE_DECL generated in those |
13939 | clones. The decloning optimization (for space) may |
13940 | revert this subsequently if it determines that |
13941 | the clones should share a common implementation. */ |
13942 | DECL_ABSTRACT_P (decl) = true; |
13943 | |
13944 | set_originating_module (decl); |
13945 | } |
13946 | else if (current_class_type |
13947 | && constructor_name_p (unqualified_id, current_class_type)) |
13948 | permerror (id_loc, "ISO C++ forbids nested type %qD with same name " |
13949 | "as enclosing class" , |
13950 | unqualified_id); |
13951 | |
13952 | /* If the user declares "typedef struct {...} foo" then the |
13953 | struct will have an anonymous name. Fill that name in now. |
13954 | Nothing can refer to it, so nothing needs know about the name |
13955 | change. */ |
13956 | if (type != error_mark_node |
13957 | && unqualified_id |
13958 | && TYPE_NAME (type) |
13959 | && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL |
13960 | && TYPE_UNNAMED_P (type) |
13961 | && declspecs->type_definition_p |
13962 | && attributes_naming_typedef_ok (*attrlist) |
13963 | && cp_type_quals (type) == TYPE_UNQUALIFIED) |
13964 | name_unnamed_type (type, decl); |
13965 | |
13966 | if (signed_p |
13967 | || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl))) |
13968 | C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1; |
13969 | |
13970 | bad_specifiers (object: decl, type: BSP_TYPE, virtualp, |
13971 | quals: memfn_quals != TYPE_UNQUALIFIED, |
13972 | inlinep, friendp, raises: raises != NULL_TREE, |
13973 | locations: declspecs->locations); |
13974 | |
13975 | if (alias_p) |
13976 | /* Acknowledge that this was written: |
13977 | `using analias = atype;'. */ |
13978 | TYPE_DECL_ALIAS_P (decl) = 1; |
13979 | |
13980 | return decl; |
13981 | } |
13982 | |
13983 | /* Detect the case of an array type of unspecified size |
13984 | which came, as such, direct from a typedef name. |
13985 | We must copy the type, so that the array's domain can be |
13986 | individually set by the object's initializer. */ |
13987 | |
13988 | if (type && typedef_type |
13989 | && TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type) |
13990 | && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type)) |
13991 | type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE); |
13992 | |
13993 | /* Detect where we're using a typedef of function type to declare a |
13994 | function. PARMS will not be set, so we must create it now. */ |
13995 | |
13996 | if (type == typedef_type && TREE_CODE (type) == FUNCTION_TYPE) |
13997 | { |
13998 | tree decls = NULL_TREE; |
13999 | tree args; |
14000 | |
14001 | for (args = TYPE_ARG_TYPES (type); |
14002 | args && args != void_list_node; |
14003 | args = TREE_CHAIN (args)) |
14004 | { |
14005 | tree decl = cp_build_parm_decl (NULL_TREE, NULL_TREE, |
14006 | TREE_VALUE (args)); |
14007 | |
14008 | DECL_CHAIN (decl) = decls; |
14009 | decls = decl; |
14010 | } |
14011 | |
14012 | parms = nreverse (decls); |
14013 | |
14014 | if (decl_context != TYPENAME) |
14015 | { |
14016 | /* The qualifiers on the function type become the qualifiers on |
14017 | the non-static member function. */ |
14018 | memfn_quals |= type_memfn_quals (type); |
14019 | rqual = type_memfn_rqual (type); |
14020 | type_quals = TYPE_UNQUALIFIED; |
14021 | raises = TYPE_RAISES_EXCEPTIONS (type); |
14022 | } |
14023 | } |
14024 | |
14025 | /* If this is a type name (such as, in a cast or sizeof), |
14026 | compute the type and return it now. */ |
14027 | |
14028 | if (decl_context == TYPENAME) |
14029 | { |
14030 | /* Note that here we don't care about type_quals. */ |
14031 | |
14032 | /* Special case: "friend class foo" looks like a TYPENAME context. */ |
14033 | if (friendp) |
14034 | { |
14035 | if (inlinep) |
14036 | { |
14037 | error ("%<inline%> specified for friend class declaration" ); |
14038 | inlinep = 0; |
14039 | } |
14040 | |
14041 | if (!current_aggr) |
14042 | { |
14043 | /* Don't allow friend declaration without a class-key. */ |
14044 | if (TREE_CODE (type) == TEMPLATE_TYPE_PARM) |
14045 | permerror (input_location, "template parameters cannot be friends" ); |
14046 | else if (TREE_CODE (type) == TYPENAME_TYPE) |
14047 | permerror (input_location, "friend declaration requires class-key, " |
14048 | "i.e. %<friend class %T::%D%>" , |
14049 | TYPE_CONTEXT (type), TYPENAME_TYPE_FULLNAME (type)); |
14050 | else |
14051 | permerror (input_location, "friend declaration requires class-key, " |
14052 | "i.e. %<friend %#T%>" , |
14053 | type); |
14054 | } |
14055 | |
14056 | /* Only try to do this stuff if we didn't already give up. */ |
14057 | if (type != integer_type_node) |
14058 | { |
14059 | /* A friendly class? */ |
14060 | if (current_class_type) |
14061 | make_friend_class (current_class_type, TYPE_MAIN_VARIANT (type), |
14062 | /*complain=*/true); |
14063 | else |
14064 | error ("trying to make class %qT a friend of global scope" , |
14065 | type); |
14066 | |
14067 | type = void_type_node; |
14068 | } |
14069 | } |
14070 | else if (memfn_quals || rqual) |
14071 | { |
14072 | if (ctype == NULL_TREE |
14073 | && TREE_CODE (type) == METHOD_TYPE) |
14074 | ctype = TYPE_METHOD_BASETYPE (type); |
14075 | |
14076 | if (ctype) |
14077 | type = build_memfn_type (type, ctype, memfn_quals, rqual); |
14078 | /* Core issue #547: need to allow this in template type args. |
14079 | Allow it in general in C++11 for alias-declarations. */ |
14080 | else if ((template_type_arg || cxx_dialect >= cxx11) |
14081 | && TREE_CODE (type) == FUNCTION_TYPE) |
14082 | type = apply_memfn_quals (type, memfn_quals, rqual); |
14083 | else |
14084 | error ("invalid qualifiers on non-member function type" ); |
14085 | } |
14086 | |
14087 | if (reqs) |
14088 | error_at (location_of (reqs), "requires-clause on type-id" ); |
14089 | |
14090 | return type; |
14091 | } |
14092 | else if (unqualified_id == NULL_TREE && decl_context != PARM |
14093 | && decl_context != CATCHPARM |
14094 | && TREE_CODE (type) != UNION_TYPE |
14095 | && ! bitfield |
14096 | && innermost_code != cdk_decomp) |
14097 | { |
14098 | error ("abstract declarator %qT used as declaration" , type); |
14099 | return error_mark_node; |
14100 | } |
14101 | |
14102 | if (!FUNC_OR_METHOD_TYPE_P (type)) |
14103 | { |
14104 | /* Only functions may be declared using an operator-function-id. */ |
14105 | if (dname && IDENTIFIER_ANY_OP_P (dname)) |
14106 | { |
14107 | error_at (id_loc, "declaration of %qD as non-function" , dname); |
14108 | return error_mark_node; |
14109 | } |
14110 | |
14111 | if (reqs) |
14112 | error_at (location_of (reqs), |
14113 | "requires-clause on declaration of non-function type %qT" , |
14114 | type); |
14115 | } |
14116 | |
14117 | /* We don't check parameter types here because we can emit a better |
14118 | error message later. */ |
14119 | if (decl_context != PARM) |
14120 | { |
14121 | type = check_var_type (identifier: unqualified_id, type, loc: id_loc); |
14122 | if (type == error_mark_node) |
14123 | return error_mark_node; |
14124 | } |
14125 | |
14126 | /* Now create the decl, which may be a VAR_DECL, a PARM_DECL |
14127 | or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */ |
14128 | |
14129 | if (decl_context == PARM || decl_context == CATCHPARM) |
14130 | { |
14131 | if (ctype || in_namespace) |
14132 | error ("cannot use %<::%> in parameter declaration" ); |
14133 | |
14134 | tree auto_node = type_uses_auto (type); |
14135 | if (auto_node && !(cxx_dialect >= cxx17 && template_parm_flag)) |
14136 | { |
14137 | if (cxx_dialect >= cxx14) |
14138 | { |
14139 | if (decl_context == PARM && AUTO_IS_DECLTYPE (auto_node)) |
14140 | error_at (typespec_loc, |
14141 | "cannot declare a parameter with %<decltype(auto)%>" ); |
14142 | else if (tree c = CLASS_PLACEHOLDER_TEMPLATE (auto_node)) |
14143 | { |
14144 | auto_diagnostic_group g; |
14145 | gcc_rich_location richloc (typespec_loc); |
14146 | richloc.add_fixit_insert_after (new_content: "<>" ); |
14147 | error_at (&richloc, |
14148 | "missing template argument list after %qE; template " |
14149 | "placeholder not permitted in parameter" , c); |
14150 | if (decl_context == PARM && cxx_dialect >= cxx20) |
14151 | inform (typespec_loc, "or use %<auto%> for an " |
14152 | "abbreviated function template" ); |
14153 | inform (DECL_SOURCE_LOCATION (c), "%qD declared here" , c); |
14154 | } |
14155 | else |
14156 | error_at (typespec_loc, |
14157 | "%<auto%> parameter not permitted in this context" ); |
14158 | } |
14159 | else |
14160 | error_at (typespec_loc, "parameter declared %<auto%>" ); |
14161 | type = error_mark_node; |
14162 | } |
14163 | |
14164 | /* A parameter declared as an array of T is really a pointer to T. |
14165 | One declared as a function is really a pointer to a function. |
14166 | One declared as a member is really a pointer to member. */ |
14167 | |
14168 | if (TREE_CODE (type) == ARRAY_TYPE) |
14169 | { |
14170 | /* Transfer const-ness of array into that of type pointed to. */ |
14171 | type = build_pointer_type (TREE_TYPE (type)); |
14172 | type_quals = TYPE_UNQUALIFIED; |
14173 | array_parameter_p = true; |
14174 | } |
14175 | else if (TREE_CODE (type) == FUNCTION_TYPE) |
14176 | type = build_pointer_type (type); |
14177 | } |
14178 | |
14179 | if (ctype && TREE_CODE (type) == FUNCTION_TYPE && staticp < 2 |
14180 | && !(unqualified_id |
14181 | && identifier_p (t: unqualified_id) |
14182 | && IDENTIFIER_NEWDEL_OP_P (unqualified_id))) |
14183 | { |
14184 | cp_cv_quals real_quals = memfn_quals; |
14185 | if (cxx_dialect < cxx14 && constexpr_p |
14186 | && sfk != sfk_constructor && sfk != sfk_destructor) |
14187 | real_quals |= TYPE_QUAL_CONST; |
14188 | type = build_memfn_type (type, ctype, real_quals, rqual); |
14189 | } |
14190 | |
14191 | { |
14192 | tree decl = NULL_TREE; |
14193 | |
14194 | if (decl_context == PARM) |
14195 | { |
14196 | decl = cp_build_parm_decl (NULL_TREE, unqualified_id, type); |
14197 | DECL_ARRAY_PARAMETER_P (decl) = array_parameter_p; |
14198 | |
14199 | bad_specifiers (object: decl, type: BSP_PARM, virtualp, |
14200 | quals: memfn_quals != TYPE_UNQUALIFIED, |
14201 | inlinep, friendp, raises: raises != NULL_TREE, |
14202 | locations: declspecs->locations); |
14203 | } |
14204 | else if (decl_context == FIELD) |
14205 | { |
14206 | if (!staticp && !friendp && !FUNC_OR_METHOD_TYPE_P (type)) |
14207 | if (tree auto_node = type_uses_auto (type)) |
14208 | { |
14209 | location_t tloc = declspecs->locations[ds_type_spec]; |
14210 | if (CLASS_PLACEHOLDER_TEMPLATE (auto_node)) |
14211 | error_at (tloc, "invalid use of template-name %qE without an " |
14212 | "argument list" , |
14213 | CLASS_PLACEHOLDER_TEMPLATE (auto_node)); |
14214 | else |
14215 | error_at (tloc, "non-static data member declared with " |
14216 | "placeholder %qT" , auto_node); |
14217 | type = error_mark_node; |
14218 | } |
14219 | |
14220 | /* The C99 flexible array extension. */ |
14221 | if (!staticp && TREE_CODE (type) == ARRAY_TYPE |
14222 | && TYPE_DOMAIN (type) == NULL_TREE) |
14223 | { |
14224 | if (ctype |
14225 | && (TREE_CODE (ctype) == UNION_TYPE |
14226 | || TREE_CODE (ctype) == QUAL_UNION_TYPE)) |
14227 | { |
14228 | error_at (id_loc, "flexible array member in union" ); |
14229 | type = error_mark_node; |
14230 | } |
14231 | else |
14232 | { |
14233 | /* Array is a flexible member. */ |
14234 | if (name) |
14235 | pedwarn (id_loc, OPT_Wpedantic, |
14236 | "ISO C++ forbids flexible array member %qs" , name); |
14237 | else |
14238 | pedwarn (input_location, OPT_Wpedantic, |
14239 | "ISO C++ forbids flexible array members" ); |
14240 | |
14241 | /* Flexible array member has a null domain. */ |
14242 | type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE); |
14243 | } |
14244 | } |
14245 | |
14246 | if (type == error_mark_node) |
14247 | { |
14248 | /* Happens when declaring arrays of sizes which |
14249 | are error_mark_node, for example. */ |
14250 | decl = NULL_TREE; |
14251 | } |
14252 | else if (in_namespace && !friendp) |
14253 | { |
14254 | /* Something like struct S { int N::j; }; */ |
14255 | error_at (id_loc, "invalid use of %<::%>" ); |
14256 | return error_mark_node; |
14257 | } |
14258 | else if (FUNC_OR_METHOD_TYPE_P (type) && unqualified_id) |
14259 | { |
14260 | int publicp = 0; |
14261 | tree function_context; |
14262 | |
14263 | if (friendp == 0) |
14264 | { |
14265 | /* This should never happen in pure C++ (the check |
14266 | could be an assert). It could happen in |
14267 | Objective-C++ if someone writes invalid code that |
14268 | uses a function declaration for an instance |
14269 | variable or property (instance variables and |
14270 | properties are parsed as FIELD_DECLs, but they are |
14271 | part of an Objective-C class, not a C++ class). |
14272 | That code is invalid and is caught by this |
14273 | check. */ |
14274 | if (!ctype) |
14275 | { |
14276 | error ("declaration of function %qD in invalid context" , |
14277 | unqualified_id); |
14278 | return error_mark_node; |
14279 | } |
14280 | |
14281 | /* ``A union may [ ... ] not [ have ] virtual functions.'' |
14282 | ARM 9.5 */ |
14283 | if (virtualp && TREE_CODE (ctype) == UNION_TYPE) |
14284 | { |
14285 | error_at (declspecs->locations[ds_virtual], |
14286 | "function %qD declared %<virtual%> inside a union" , |
14287 | unqualified_id); |
14288 | return error_mark_node; |
14289 | } |
14290 | |
14291 | if (virtualp |
14292 | && identifier_p (t: unqualified_id) |
14293 | && IDENTIFIER_NEWDEL_OP_P (unqualified_id)) |
14294 | { |
14295 | error_at (declspecs->locations[ds_virtual], |
14296 | "%qD cannot be declared %<virtual%>, since it " |
14297 | "is always static" , unqualified_id); |
14298 | virtualp = 0; |
14299 | } |
14300 | } |
14301 | |
14302 | /* Check that the name used for a destructor makes sense. */ |
14303 | if (sfk == sfk_destructor) |
14304 | { |
14305 | tree uqname = id_declarator->u.id.unqualified_name; |
14306 | |
14307 | if (!ctype) |
14308 | { |
14309 | gcc_assert (friendp); |
14310 | error_at (id_loc, "expected qualified name in friend " |
14311 | "declaration for destructor %qD" , uqname); |
14312 | return error_mark_node; |
14313 | } |
14314 | |
14315 | if (!check_dtor_name (ctype, TREE_OPERAND (uqname, 0))) |
14316 | { |
14317 | error_at (id_loc, "declaration of %qD as member of %qT" , |
14318 | uqname, ctype); |
14319 | return error_mark_node; |
14320 | } |
14321 | if (concept_p) |
14322 | { |
14323 | error_at (declspecs->locations[ds_concept], |
14324 | "a destructor cannot be %qs" , "concept" ); |
14325 | return error_mark_node; |
14326 | } |
14327 | if (constexpr_p && cxx_dialect < cxx20) |
14328 | { |
14329 | error_at (declspecs->locations[ds_constexpr], |
14330 | "%<constexpr%> destructors only available" |
14331 | " with %<-std=c++20%> or %<-std=gnu++20%>" ); |
14332 | return error_mark_node; |
14333 | } |
14334 | if (consteval_p) |
14335 | { |
14336 | error_at (declspecs->locations[ds_consteval], |
14337 | "a destructor cannot be %qs" , "consteval" ); |
14338 | return error_mark_node; |
14339 | } |
14340 | } |
14341 | else if (sfk == sfk_constructor && friendp && !ctype) |
14342 | { |
14343 | error ("expected qualified name in friend declaration " |
14344 | "for constructor %qD" , |
14345 | id_declarator->u.id.unqualified_name); |
14346 | return error_mark_node; |
14347 | } |
14348 | if (sfk == sfk_constructor) |
14349 | if (concept_p) |
14350 | { |
14351 | error_at (declspecs->locations[ds_concept], |
14352 | "a constructor cannot be %<concept%>" ); |
14353 | return error_mark_node; |
14354 | } |
14355 | if (concept_p) |
14356 | { |
14357 | error_at (declspecs->locations[ds_concept], |
14358 | "a concept cannot be a member function" ); |
14359 | concept_p = false; |
14360 | } |
14361 | else if (consteval_p |
14362 | && identifier_p (t: unqualified_id) |
14363 | && IDENTIFIER_NEWDEL_OP_P (unqualified_id)) |
14364 | { |
14365 | error_at (declspecs->locations[ds_consteval], |
14366 | "%qD cannot be %qs" , unqualified_id, "consteval" ); |
14367 | consteval_p = false; |
14368 | } |
14369 | |
14370 | if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR) |
14371 | { |
14372 | tree tmpl = TREE_OPERAND (unqualified_id, 0); |
14373 | if (variable_template_p (t: tmpl)) |
14374 | { |
14375 | error_at (id_loc, "specialization of variable template " |
14376 | "%qD declared as function" , tmpl); |
14377 | inform (DECL_SOURCE_LOCATION (tmpl), |
14378 | "variable template declared here" ); |
14379 | return error_mark_node; |
14380 | } |
14381 | } |
14382 | |
14383 | /* Tell grokfndecl if it needs to set TREE_PUBLIC on the node. */ |
14384 | function_context |
14385 | = (ctype != NULL_TREE |
14386 | ? decl_function_context (TYPE_MAIN_DECL (ctype)) : NULL_TREE); |
14387 | publicp = ((! friendp || ! staticp) |
14388 | && function_context == NULL_TREE); |
14389 | |
14390 | decl = grokfndecl (ctype, type, |
14391 | TREE_CODE (unqualified_id) != TEMPLATE_ID_EXPR |
14392 | ? unqualified_id : dname, |
14393 | parms, |
14394 | orig_declarator: unqualified_id, |
14395 | declspecs, |
14396 | decl_reqs: reqs, |
14397 | virtualp, flags, quals: memfn_quals, rqual, raises, |
14398 | check: friendp ? -1 : 0, friendp, publicp, |
14399 | inlinep: inlinep | (2 * constexpr_p) | (4 * concept_p) |
14400 | | (8 * consteval_p), |
14401 | deletedp: initialized == SD_DELETED, sfk, |
14402 | funcdef_flag, late_return_type_p, |
14403 | template_count, in_namespace, |
14404 | attrlist, location: id_loc); |
14405 | decl = set_virt_specifiers (decl, specifiers: virt_specifiers); |
14406 | if (decl == NULL_TREE) |
14407 | return error_mark_node; |
14408 | #if 0 |
14409 | /* This clobbers the attrs stored in `decl' from `attrlist'. */ |
14410 | /* The decl and setting of decl_attr is also turned off. */ |
14411 | decl = build_decl_attribute_variant (decl, decl_attr); |
14412 | #endif |
14413 | |
14414 | /* [class.conv.ctor] |
14415 | |
14416 | A constructor declared without the function-specifier |
14417 | explicit that can be called with a single parameter |
14418 | specifies a conversion from the type of its first |
14419 | parameter to the type of its class. Such a constructor |
14420 | is called a converting constructor. */ |
14421 | if (explicitp == 2) |
14422 | DECL_NONCONVERTING_P (decl) = 1; |
14423 | |
14424 | if (declspecs->explicit_specifier) |
14425 | store_explicit_specifier (decl, declspecs->explicit_specifier); |
14426 | } |
14427 | else if (!staticp |
14428 | && ((current_class_type |
14429 | && same_type_p (type, current_class_type)) |
14430 | || (!dependent_type_p (type) |
14431 | && !COMPLETE_TYPE_P (complete_type (type)) |
14432 | && (!complete_or_array_type_p (type) |
14433 | || initialized == SD_UNINITIALIZED)))) |
14434 | { |
14435 | if (TREE_CODE (type) != ARRAY_TYPE |
14436 | || !COMPLETE_TYPE_P (TREE_TYPE (type))) |
14437 | { |
14438 | if (unqualified_id) |
14439 | { |
14440 | error_at (id_loc, "field %qD has incomplete type %qT" , |
14441 | unqualified_id, type); |
14442 | cxx_incomplete_type_inform (strip_array_types (type)); |
14443 | } |
14444 | else |
14445 | error ("name %qT has incomplete type" , type); |
14446 | |
14447 | type = error_mark_node; |
14448 | decl = NULL_TREE; |
14449 | } |
14450 | } |
14451 | else if (!verify_type_context (input_location, |
14452 | staticp |
14453 | ? TCTX_STATIC_STORAGE |
14454 | : TCTX_FIELD, type)) |
14455 | { |
14456 | type = error_mark_node; |
14457 | decl = NULL_TREE; |
14458 | } |
14459 | else |
14460 | { |
14461 | if (friendp) |
14462 | { |
14463 | if (unqualified_id) |
14464 | error_at (id_loc, |
14465 | "%qE is neither function nor member function; " |
14466 | "cannot be declared friend" , unqualified_id); |
14467 | else |
14468 | error ("unnamed field is neither function nor member " |
14469 | "function; cannot be declared friend" ); |
14470 | return error_mark_node; |
14471 | } |
14472 | decl = NULL_TREE; |
14473 | } |
14474 | |
14475 | if (friendp) |
14476 | { |
14477 | /* Packages tend to use GNU attributes on friends, so we only |
14478 | warn for standard attributes. */ |
14479 | if (attrlist |
14480 | && !funcdef_flag |
14481 | && cxx11_attribute_p (*attrlist) |
14482 | && !all_attributes_are_contracts_p (*attrlist)) |
14483 | { |
14484 | *attrlist = NULL_TREE; |
14485 | if (warning_at (id_loc, OPT_Wattributes, "attribute ignored" )) |
14486 | inform (id_loc, "an attribute that appertains to a friend " |
14487 | "declaration that is not a definition is ignored" ); |
14488 | } |
14489 | /* Friends are treated specially. */ |
14490 | if (ctype == current_class_type) |
14491 | ; /* We already issued a permerror. */ |
14492 | else if (decl && DECL_NAME (decl)) |
14493 | { |
14494 | set_originating_module (decl, friend_p: true); |
14495 | |
14496 | if (initialized) |
14497 | /* Kludge: We need funcdef_flag to be true in do_friend for |
14498 | in-class defaulted functions, but that breaks grokfndecl. |
14499 | So set it here. */ |
14500 | funcdef_flag = true; |
14501 | |
14502 | cplus_decl_attributes (&decl, *attrlist, 0); |
14503 | *attrlist = NULL_TREE; |
14504 | |
14505 | tree scope = ctype ? ctype : in_namespace; |
14506 | decl = do_friend (scope, unqualified_id, decl, |
14507 | flags, funcdef_flag); |
14508 | return decl; |
14509 | } |
14510 | else |
14511 | return error_mark_node; |
14512 | } |
14513 | |
14514 | /* Structure field. It may not be a function, except for C++. */ |
14515 | |
14516 | if (decl == NULL_TREE) |
14517 | { |
14518 | if (staticp) |
14519 | { |
14520 | /* C++ allows static class members. All other work |
14521 | for this is done by grokfield. */ |
14522 | decl = build_lang_decl_loc (id_loc, VAR_DECL, |
14523 | dname, type); |
14524 | if (unqualified_id |
14525 | && TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR) |
14526 | { |
14527 | decl = check_explicit_specialization (unqualified_id, decl, |
14528 | template_count, |
14529 | concept_p * 8); |
14530 | if (decl == error_mark_node) |
14531 | return error_mark_node; |
14532 | } |
14533 | set_linkage_for_static_data_member (decl); |
14534 | if (concept_p) |
14535 | error_at (declspecs->locations[ds_concept], |
14536 | "static data member %qE declared %qs" , |
14537 | unqualified_id, "concept" ); |
14538 | else if (constexpr_p && !initialized) |
14539 | { |
14540 | error_at (DECL_SOURCE_LOCATION (decl), |
14541 | "%<constexpr%> static data member %qD must " |
14542 | "have an initializer" , decl); |
14543 | constexpr_p = false; |
14544 | } |
14545 | if (consteval_p) |
14546 | error_at (declspecs->locations[ds_consteval], |
14547 | "static data member %qE declared %qs" , |
14548 | unqualified_id, "consteval" ); |
14549 | |
14550 | if (inlinep) |
14551 | mark_inline_variable (decl, loc: declspecs->locations[ds_inline]); |
14552 | |
14553 | if (!DECL_VAR_DECLARED_INLINE_P (decl) |
14554 | && !(cxx_dialect >= cxx17 && constexpr_p)) |
14555 | /* Even if there is an in-class initialization, DECL |
14556 | is considered undefined until an out-of-class |
14557 | definition is provided, unless this is an inline |
14558 | variable. */ |
14559 | DECL_EXTERNAL (decl) = 1; |
14560 | |
14561 | if (thread_p) |
14562 | { |
14563 | CP_DECL_THREAD_LOCAL_P (decl) = true; |
14564 | if (!processing_template_decl) |
14565 | set_decl_tls_model (decl, decl_default_tls_model (decl)); |
14566 | if (declspecs->gnu_thread_keyword_p) |
14567 | SET_DECL_GNU_TLS_P (decl); |
14568 | } |
14569 | |
14570 | /* Set the constraints on the declaration. */ |
14571 | bool memtmpl = (current_template_depth |
14572 | > template_class_depth (current_class_type)); |
14573 | if (memtmpl) |
14574 | { |
14575 | tree ci = current_template_constraints (); |
14576 | set_constraints (decl, ci); |
14577 | } |
14578 | } |
14579 | else |
14580 | { |
14581 | if (concept_p) |
14582 | { |
14583 | error_at (declspecs->locations[ds_concept], |
14584 | "non-static data member %qE declared %qs" , |
14585 | unqualified_id, "concept" ); |
14586 | concept_p = false; |
14587 | constexpr_p = false; |
14588 | } |
14589 | else if (constexpr_p) |
14590 | { |
14591 | error_at (declspecs->locations[ds_constexpr], |
14592 | "non-static data member %qE declared %qs" , |
14593 | unqualified_id, "constexpr" ); |
14594 | constexpr_p = false; |
14595 | } |
14596 | if (constinit_p) |
14597 | { |
14598 | error_at (declspecs->locations[ds_constinit], |
14599 | "non-static data member %qE declared %qs" , |
14600 | unqualified_id, "constinit" ); |
14601 | constinit_p = false; |
14602 | } |
14603 | if (consteval_p) |
14604 | { |
14605 | error_at (declspecs->locations[ds_consteval], |
14606 | "non-static data member %qE declared %qs" , |
14607 | unqualified_id, "consteval" ); |
14608 | consteval_p = false; |
14609 | } |
14610 | decl = build_decl (id_loc, FIELD_DECL, unqualified_id, type); |
14611 | DECL_NONADDRESSABLE_P (decl) = bitfield; |
14612 | if (bitfield && !unqualified_id) |
14613 | DECL_PADDING_P (decl) = 1; |
14614 | |
14615 | if (storage_class == sc_mutable) |
14616 | { |
14617 | DECL_MUTABLE_P (decl) = 1; |
14618 | storage_class = sc_none; |
14619 | } |
14620 | |
14621 | if (initialized) |
14622 | { |
14623 | /* An attempt is being made to initialize a non-static |
14624 | member. This is new in C++11. */ |
14625 | maybe_warn_cpp0x (str: CPP0X_NSDMI, init_loc); |
14626 | |
14627 | /* If this has been parsed with static storage class, but |
14628 | errors forced staticp to be cleared, ensure NSDMI is |
14629 | not present. */ |
14630 | if (declspecs->storage_class == sc_static) |
14631 | DECL_INITIAL (decl) = error_mark_node; |
14632 | } |
14633 | } |
14634 | |
14635 | bad_specifiers (object: decl, type: BSP_FIELD, virtualp, |
14636 | quals: memfn_quals != TYPE_UNQUALIFIED, |
14637 | inlinep: staticp ? false : inlinep, friendp, |
14638 | raises: raises != NULL_TREE, |
14639 | locations: declspecs->locations); |
14640 | } |
14641 | } |
14642 | else if (FUNC_OR_METHOD_TYPE_P (type)) |
14643 | { |
14644 | tree original_name; |
14645 | int publicp = 0; |
14646 | |
14647 | if (!unqualified_id) |
14648 | return error_mark_node; |
14649 | |
14650 | if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR) |
14651 | original_name = dname; |
14652 | else |
14653 | original_name = unqualified_id; |
14654 | // FIXME:gcc_assert (original_name == dname); |
14655 | |
14656 | if (storage_class == sc_auto) |
14657 | error_at (declspecs->locations[ds_storage_class], |
14658 | "storage class %<auto%> invalid for function %qs" , name); |
14659 | else if (storage_class == sc_register) |
14660 | error_at (declspecs->locations[ds_storage_class], |
14661 | "storage class %<register%> invalid for function %qs" , |
14662 | name); |
14663 | else if (thread_p) |
14664 | { |
14665 | if (declspecs->gnu_thread_keyword_p) |
14666 | error_at (declspecs->locations[ds_thread], |
14667 | "storage class %<__thread%> invalid for function %qs" , |
14668 | name); |
14669 | else |
14670 | error_at (declspecs->locations[ds_thread], |
14671 | "storage class %<thread_local%> invalid for " |
14672 | "function %qs" , name); |
14673 | } |
14674 | else if (constinit_p) |
14675 | error_at (declspecs->locations[ds_constinit], |
14676 | "%<constinit%> specifier invalid for function %qs" , name); |
14677 | |
14678 | if (virt_specifiers) |
14679 | error ("virt-specifiers in %qs not allowed outside a class " |
14680 | "definition" , name); |
14681 | /* Function declaration not at top level. |
14682 | Storage classes other than `extern' are not allowed |
14683 | and `extern' makes no difference. */ |
14684 | if (! toplevel_bindings_p () |
14685 | && (storage_class == sc_static |
14686 | || decl_spec_seq_has_spec_p (declspecs, ds_inline)) |
14687 | && pedantic) |
14688 | { |
14689 | if (storage_class == sc_static) |
14690 | pedwarn (declspecs->locations[ds_storage_class], OPT_Wpedantic, |
14691 | "%<static%> specifier invalid for function %qs " |
14692 | "declared out of global scope" , name); |
14693 | else |
14694 | pedwarn (declspecs->locations[ds_inline], OPT_Wpedantic, |
14695 | "%<inline%> specifier invalid for function %qs " |
14696 | "declared out of global scope" , name); |
14697 | } |
14698 | |
14699 | if (ctype == NULL_TREE) |
14700 | { |
14701 | if (virtualp) |
14702 | { |
14703 | error ("virtual non-class function %qs" , name); |
14704 | virtualp = 0; |
14705 | } |
14706 | else if (sfk == sfk_constructor |
14707 | || sfk == sfk_destructor) |
14708 | { |
14709 | error (funcdef_flag |
14710 | ? G_("%qs defined in a non-class scope" ) |
14711 | : G_("%qs declared in a non-class scope" ), name); |
14712 | sfk = sfk_none; |
14713 | } |
14714 | } |
14715 | if (consteval_p |
14716 | && identifier_p (t: unqualified_id) |
14717 | && IDENTIFIER_NEWDEL_OP_P (unqualified_id)) |
14718 | { |
14719 | error_at (declspecs->locations[ds_consteval], |
14720 | "%qD cannot be %qs" , unqualified_id, "consteval" ); |
14721 | consteval_p = false; |
14722 | } |
14723 | |
14724 | /* Record whether the function is public. */ |
14725 | publicp = (ctype != NULL_TREE |
14726 | || storage_class != sc_static); |
14727 | |
14728 | decl = grokfndecl (ctype, type, declarator: original_name, parms, orig_declarator: unqualified_id, |
14729 | declspecs, |
14730 | decl_reqs: reqs, virtualp, flags, quals: memfn_quals, rqual, raises, |
14731 | check: 1, friendp, |
14732 | publicp, |
14733 | inlinep: inlinep | (2 * constexpr_p) | (4 * concept_p) |
14734 | | (8 * consteval_p), |
14735 | deletedp: initialized == SD_DELETED, |
14736 | sfk, |
14737 | funcdef_flag, |
14738 | late_return_type_p, |
14739 | template_count, in_namespace, attrlist, |
14740 | location: id_loc); |
14741 | if (decl == NULL_TREE) |
14742 | return error_mark_node; |
14743 | |
14744 | if (explicitp == 2) |
14745 | DECL_NONCONVERTING_P (decl) = 1; |
14746 | if (staticp == 1) |
14747 | { |
14748 | int invalid_static = 0; |
14749 | |
14750 | /* Don't allow a static member function in a class, and forbid |
14751 | declaring main to be static. */ |
14752 | if (TREE_CODE (type) == METHOD_TYPE) |
14753 | { |
14754 | permerror (input_location, "cannot declare member function %qD to have " |
14755 | "static linkage" , decl); |
14756 | invalid_static = 1; |
14757 | } |
14758 | else if (current_function_decl) |
14759 | { |
14760 | /* 7.1.1: There can be no static function declarations within a |
14761 | block. */ |
14762 | error_at (declspecs->locations[ds_storage_class], |
14763 | "cannot declare static function inside another function" ); |
14764 | invalid_static = 1; |
14765 | } |
14766 | |
14767 | if (invalid_static) |
14768 | { |
14769 | staticp = 0; |
14770 | storage_class = sc_none; |
14771 | } |
14772 | } |
14773 | if (declspecs->explicit_specifier) |
14774 | store_explicit_specifier (decl, declspecs->explicit_specifier); |
14775 | } |
14776 | else |
14777 | { |
14778 | /* It's a variable. */ |
14779 | |
14780 | /* An uninitialized decl with `extern' is a reference. */ |
14781 | decl = grokvardecl (type, name: dname, orig_declarator: unqualified_id, |
14782 | declspecs, |
14783 | initialized, |
14784 | type_quals, |
14785 | inlinep, |
14786 | conceptp: concept_p, |
14787 | template_count, |
14788 | scope: ctype ? ctype : in_namespace, |
14789 | location: id_loc); |
14790 | if (decl == NULL_TREE) |
14791 | return error_mark_node; |
14792 | |
14793 | bad_specifiers (object: decl, type: BSP_VAR, virtualp, |
14794 | quals: memfn_quals != TYPE_UNQUALIFIED, |
14795 | inlinep, friendp, raises: raises != NULL_TREE, |
14796 | locations: declspecs->locations); |
14797 | |
14798 | if (ctype) |
14799 | { |
14800 | DECL_CONTEXT (decl) = ctype; |
14801 | if (staticp == 1) |
14802 | { |
14803 | permerror (declspecs->locations[ds_storage_class], |
14804 | "%<static%> may not be used when defining " |
14805 | "(as opposed to declaring) a static data member" ); |
14806 | staticp = 0; |
14807 | storage_class = sc_none; |
14808 | } |
14809 | if (storage_class == sc_register && TREE_STATIC (decl)) |
14810 | { |
14811 | error ("static member %qD declared %<register%>" , decl); |
14812 | storage_class = sc_none; |
14813 | } |
14814 | if (storage_class == sc_extern && pedantic) |
14815 | { |
14816 | pedwarn (input_location, OPT_Wpedantic, |
14817 | "cannot explicitly declare member %q#D to have " |
14818 | "extern linkage" , decl); |
14819 | storage_class = sc_none; |
14820 | } |
14821 | } |
14822 | else if (constexpr_p && DECL_EXTERNAL (decl)) |
14823 | { |
14824 | error_at (DECL_SOURCE_LOCATION (decl), |
14825 | "declaration of %<constexpr%> variable %qD " |
14826 | "is not a definition" , decl); |
14827 | constexpr_p = false; |
14828 | } |
14829 | if (consteval_p) |
14830 | { |
14831 | error_at (DECL_SOURCE_LOCATION (decl), |
14832 | "a variable cannot be declared %<consteval%>" ); |
14833 | consteval_p = false; |
14834 | } |
14835 | |
14836 | if (inlinep) |
14837 | mark_inline_variable (decl, loc: declspecs->locations[ds_inline]); |
14838 | if (innermost_code == cdk_decomp) |
14839 | { |
14840 | gcc_assert (declarator && declarator->kind == cdk_decomp); |
14841 | DECL_SOURCE_LOCATION (decl) = id_loc; |
14842 | DECL_ARTIFICIAL (decl) = 1; |
14843 | fit_decomposition_lang_decl (decl, NULL_TREE); |
14844 | } |
14845 | } |
14846 | |
14847 | if (VAR_P (decl) && !initialized) |
14848 | if (tree auto_node = type_uses_auto (type)) |
14849 | if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node)) |
14850 | { |
14851 | location_t loc = declspecs->locations[ds_type_spec]; |
14852 | error_at (loc, "declaration of %q#D has no initializer" , decl); |
14853 | TREE_TYPE (decl) = error_mark_node; |
14854 | } |
14855 | |
14856 | if (storage_class == sc_extern && initialized && !funcdef_flag) |
14857 | { |
14858 | if (toplevel_bindings_p ()) |
14859 | { |
14860 | /* It's common practice (and completely valid) to have a const |
14861 | be initialized and declared extern. */ |
14862 | if (!(type_quals & TYPE_QUAL_CONST)) |
14863 | warning_at (DECL_SOURCE_LOCATION (decl), 0, |
14864 | "%qs initialized and declared %<extern%>" , name); |
14865 | } |
14866 | else |
14867 | { |
14868 | error_at (DECL_SOURCE_LOCATION (decl), |
14869 | "%qs has both %<extern%> and initializer" , name); |
14870 | return error_mark_node; |
14871 | } |
14872 | } |
14873 | |
14874 | /* Record `register' declaration for warnings on & |
14875 | and in case doing stupid register allocation. */ |
14876 | |
14877 | if (storage_class == sc_register) |
14878 | { |
14879 | DECL_REGISTER (decl) = 1; |
14880 | /* Warn about register storage specifiers on PARM_DECLs. */ |
14881 | if (TREE_CODE (decl) == PARM_DECL) |
14882 | { |
14883 | if (cxx_dialect >= cxx17) |
14884 | pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wregister, |
14885 | "ISO C++17 does not allow %<register%> storage " |
14886 | "class specifier" ); |
14887 | else |
14888 | warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wregister, |
14889 | "%<register%> storage class specifier used" ); |
14890 | } |
14891 | } |
14892 | else if (storage_class == sc_extern) |
14893 | DECL_THIS_EXTERN (decl) = 1; |
14894 | else if (storage_class == sc_static) |
14895 | DECL_THIS_STATIC (decl) = 1; |
14896 | |
14897 | if (VAR_P (decl)) |
14898 | { |
14899 | /* Set constexpr flag on vars (functions got it in grokfndecl). */ |
14900 | if (constexpr_p) |
14901 | DECL_DECLARED_CONSTEXPR_P (decl) = true; |
14902 | /* And the constinit flag (which only applies to variables). */ |
14903 | else if (constinit_p) |
14904 | DECL_DECLARED_CONSTINIT_P (decl) = true; |
14905 | } |
14906 | else if (TREE_CODE (decl) == FUNCTION_DECL) |
14907 | { |
14908 | /* If we saw a return type, record its location. */ |
14909 | location_t loc = declspecs->locations[ds_type_spec]; |
14910 | if (loc == UNKNOWN_LOCATION) |
14911 | /* Build DECL_RESULT in start_preparsed_function. */; |
14912 | else if (!DECL_RESULT (decl)) |
14913 | { |
14914 | tree restype = TREE_TYPE (TREE_TYPE (decl)); |
14915 | tree resdecl = build_decl (loc, RESULT_DECL, 0, restype); |
14916 | DECL_ARTIFICIAL (resdecl) = 1; |
14917 | DECL_IGNORED_P (resdecl) = 1; |
14918 | DECL_RESULT (decl) = resdecl; |
14919 | } |
14920 | else if (funcdef_flag) |
14921 | DECL_SOURCE_LOCATION (DECL_RESULT (decl)) = loc; |
14922 | } |
14923 | |
14924 | /* Record constancy and volatility on the DECL itself . There's |
14925 | no need to do this when processing a template; we'll do this |
14926 | for the instantiated declaration based on the type of DECL. */ |
14927 | if (!processing_template_decl) |
14928 | cp_apply_type_quals_to_decl (type_quals, decl); |
14929 | |
14930 | return decl; |
14931 | } |
14932 | } |
14933 | |
14934 | /* Subroutine of start_function. Ensure that each of the parameter |
14935 | types (as listed in PARMS) is complete, as is required for a |
14936 | function definition. */ |
14937 | |
14938 | static void |
14939 | require_complete_types_for_parms (tree parms) |
14940 | { |
14941 | for (; parms; parms = DECL_CHAIN (parms)) |
14942 | { |
14943 | if (dependent_type_p (TREE_TYPE (parms))) |
14944 | continue; |
14945 | if (!VOID_TYPE_P (TREE_TYPE (parms)) |
14946 | && complete_type_or_else (TREE_TYPE (parms), parms)) |
14947 | { |
14948 | relayout_decl (parms); |
14949 | DECL_ARG_TYPE (parms) = type_passed_as (TREE_TYPE (parms)); |
14950 | |
14951 | abstract_virtuals_error (parms, TREE_TYPE (parms)); |
14952 | maybe_warn_parm_abi (TREE_TYPE (parms), |
14953 | DECL_SOURCE_LOCATION (parms)); |
14954 | } |
14955 | else |
14956 | /* grokparms or complete_type_or_else will have already issued |
14957 | an error. */ |
14958 | TREE_TYPE (parms) = error_mark_node; |
14959 | } |
14960 | } |
14961 | |
14962 | /* Returns nonzero if T is a local variable. */ |
14963 | |
14964 | int |
14965 | local_variable_p (const_tree t) |
14966 | { |
14967 | if ((VAR_P (t) |
14968 | && (DECL_LOCAL_DECL_P (t) |
14969 | || !DECL_CONTEXT (t) |
14970 | || TREE_CODE (DECL_CONTEXT (t)) == FUNCTION_DECL)) |
14971 | || (TREE_CODE (t) == PARM_DECL)) |
14972 | return 1; |
14973 | |
14974 | return 0; |
14975 | } |
14976 | |
14977 | /* Like local_variable_p, but suitable for use as a tree-walking |
14978 | function. */ |
14979 | |
14980 | static tree |
14981 | local_variable_p_walkfn (tree *tp, int *walk_subtrees, |
14982 | void * /*data*/) |
14983 | { |
14984 | if (unevaluated_p (TREE_CODE (*tp))) |
14985 | { |
14986 | /* DR 2082 permits local variables in unevaluated contexts |
14987 | within a default argument. */ |
14988 | *walk_subtrees = 0; |
14989 | return NULL_TREE; |
14990 | } |
14991 | |
14992 | if (local_variable_p (t: *tp) |
14993 | && (!DECL_ARTIFICIAL (*tp) || DECL_NAME (*tp) == this_identifier)) |
14994 | return *tp; |
14995 | else if (TYPE_P (*tp)) |
14996 | *walk_subtrees = 0; |
14997 | |
14998 | return NULL_TREE; |
14999 | } |
15000 | |
15001 | /* Check that ARG, which is a default-argument expression for a |
15002 | parameter DECL, is valid. Returns ARG, or ERROR_MARK_NODE, if |
15003 | something goes wrong. DECL may also be a _TYPE node, rather than a |
15004 | DECL, if there is no DECL available. */ |
15005 | |
15006 | tree |
15007 | check_default_argument (tree decl, tree arg, tsubst_flags_t complain) |
15008 | { |
15009 | tree var; |
15010 | tree decl_type; |
15011 | |
15012 | if (TREE_CODE (arg) == DEFERRED_PARSE) |
15013 | /* We get a DEFERRED_PARSE when looking at an in-class declaration |
15014 | with a default argument. Ignore the argument for now; we'll |
15015 | deal with it after the class is complete. */ |
15016 | return arg; |
15017 | |
15018 | if (TYPE_P (decl)) |
15019 | { |
15020 | decl_type = decl; |
15021 | decl = NULL_TREE; |
15022 | } |
15023 | else |
15024 | decl_type = TREE_TYPE (decl); |
15025 | |
15026 | if (arg == error_mark_node |
15027 | || decl == error_mark_node |
15028 | || TREE_TYPE (arg) == error_mark_node |
15029 | || decl_type == error_mark_node) |
15030 | /* Something already went wrong. There's no need to check |
15031 | further. */ |
15032 | return error_mark_node; |
15033 | |
15034 | /* [dcl.fct.default] |
15035 | |
15036 | A default argument expression is implicitly converted to the |
15037 | parameter type. */ |
15038 | ++cp_unevaluated_operand; |
15039 | /* Avoid digest_init clobbering the initializer. */ |
15040 | tree carg = BRACE_ENCLOSED_INITIALIZER_P (arg) ? unshare_expr (arg): arg; |
15041 | perform_implicit_conversion_flags (decl_type, carg, complain, |
15042 | LOOKUP_IMPLICIT); |
15043 | --cp_unevaluated_operand; |
15044 | |
15045 | /* Avoid redundant -Wzero-as-null-pointer-constant warnings at |
15046 | the call sites. */ |
15047 | if (TYPE_PTR_OR_PTRMEM_P (decl_type) |
15048 | && null_ptr_cst_p (arg) |
15049 | /* Don't lose side-effects as in PR90473. */ |
15050 | && !TREE_SIDE_EFFECTS (arg)) |
15051 | return nullptr_node; |
15052 | |
15053 | /* [dcl.fct.default] |
15054 | |
15055 | Local variables shall not be used in default argument |
15056 | expressions. |
15057 | |
15058 | The keyword `this' shall not be used in a default argument of a |
15059 | member function. */ |
15060 | var = cp_walk_tree_without_duplicates (&arg, local_variable_p_walkfn, NULL); |
15061 | if (var) |
15062 | { |
15063 | if (complain & tf_warning_or_error) |
15064 | { |
15065 | if (DECL_NAME (var) == this_identifier) |
15066 | permerror (input_location, "default argument %qE uses %qD" , |
15067 | arg, var); |
15068 | else |
15069 | error ("default argument %qE uses local variable %qD" , arg, var); |
15070 | } |
15071 | return error_mark_node; |
15072 | } |
15073 | |
15074 | /* All is well. */ |
15075 | return arg; |
15076 | } |
15077 | |
15078 | /* Returns a deprecated type used within TYPE, or NULL_TREE if none. */ |
15079 | |
15080 | static tree |
15081 | type_is_deprecated (tree type) |
15082 | { |
15083 | enum tree_code code; |
15084 | if (TREE_DEPRECATED (type)) |
15085 | return type; |
15086 | if (TYPE_NAME (type)) |
15087 | { |
15088 | if (TREE_DEPRECATED (TYPE_NAME (type))) |
15089 | return type; |
15090 | else |
15091 | { |
15092 | cp_warn_deprecated_use_scopes (CP_DECL_CONTEXT (TYPE_NAME (type))); |
15093 | return NULL_TREE; |
15094 | } |
15095 | } |
15096 | |
15097 | /* Do warn about using typedefs to a deprecated class. */ |
15098 | if (OVERLOAD_TYPE_P (type) && type != TYPE_MAIN_VARIANT (type)) |
15099 | return type_is_deprecated (TYPE_MAIN_VARIANT (type)); |
15100 | |
15101 | code = TREE_CODE (type); |
15102 | |
15103 | if (code == POINTER_TYPE || code == REFERENCE_TYPE |
15104 | || code == OFFSET_TYPE || code == FUNCTION_TYPE |
15105 | || code == METHOD_TYPE || code == ARRAY_TYPE) |
15106 | return type_is_deprecated (TREE_TYPE (type)); |
15107 | |
15108 | if (TYPE_PTRMEMFUNC_P (type)) |
15109 | return type_is_deprecated |
15110 | (TREE_TYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type)))); |
15111 | |
15112 | return NULL_TREE; |
15113 | } |
15114 | |
15115 | /* Returns an unavailable type used within TYPE, or NULL_TREE if none. */ |
15116 | |
15117 | static tree |
15118 | type_is_unavailable (tree type) |
15119 | { |
15120 | enum tree_code code; |
15121 | if (TREE_UNAVAILABLE (type)) |
15122 | return type; |
15123 | if (TYPE_NAME (type)) |
15124 | { |
15125 | if (TREE_UNAVAILABLE (TYPE_NAME (type))) |
15126 | return type; |
15127 | else |
15128 | { |
15129 | cp_warn_deprecated_use_scopes (CP_DECL_CONTEXT (TYPE_NAME (type))); |
15130 | return NULL_TREE; |
15131 | } |
15132 | } |
15133 | |
15134 | /* Do warn about using typedefs to a deprecated class. */ |
15135 | if (OVERLOAD_TYPE_P (type) && type != TYPE_MAIN_VARIANT (type)) |
15136 | return type_is_deprecated (TYPE_MAIN_VARIANT (type)); |
15137 | |
15138 | code = TREE_CODE (type); |
15139 | |
15140 | if (code == POINTER_TYPE || code == REFERENCE_TYPE |
15141 | || code == OFFSET_TYPE || code == FUNCTION_TYPE |
15142 | || code == METHOD_TYPE || code == ARRAY_TYPE) |
15143 | return type_is_unavailable (TREE_TYPE (type)); |
15144 | |
15145 | if (TYPE_PTRMEMFUNC_P (type)) |
15146 | return type_is_unavailable |
15147 | (TREE_TYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type)))); |
15148 | |
15149 | return NULL_TREE; |
15150 | } |
15151 | |
15152 | /* Decode the list of parameter types for a function type. |
15153 | Given the list of things declared inside the parens, |
15154 | return a list of types. |
15155 | |
15156 | If this parameter does not end with an ellipsis, we append |
15157 | void_list_node. |
15158 | |
15159 | *PARMS is set to the chain of PARM_DECLs created. */ |
15160 | |
15161 | tree |
15162 | grokparms (tree parmlist, tree *parms) |
15163 | { |
15164 | tree result = NULL_TREE; |
15165 | tree decls = NULL_TREE; |
15166 | tree parm; |
15167 | int any_error = 0; |
15168 | |
15169 | for (parm = parmlist; parm != NULL_TREE; parm = TREE_CHAIN (parm)) |
15170 | { |
15171 | tree type = NULL_TREE; |
15172 | tree init = TREE_PURPOSE (parm); |
15173 | tree decl = TREE_VALUE (parm); |
15174 | |
15175 | if (parm == void_list_node || parm == explicit_void_list_node) |
15176 | break; |
15177 | |
15178 | if (! decl || TREE_TYPE (decl) == error_mark_node) |
15179 | { |
15180 | any_error = 1; |
15181 | continue; |
15182 | } |
15183 | |
15184 | type = TREE_TYPE (decl); |
15185 | if (VOID_TYPE_P (type)) |
15186 | { |
15187 | if (same_type_p (type, void_type_node) |
15188 | && !init |
15189 | && !DECL_NAME (decl) && !result |
15190 | && TREE_CHAIN (parm) == void_list_node) |
15191 | /* DR 577: A parameter list consisting of a single |
15192 | unnamed parameter of non-dependent type 'void'. */ |
15193 | break; |
15194 | else if (cv_qualified_p (type)) |
15195 | error_at (DECL_SOURCE_LOCATION (decl), |
15196 | "invalid use of cv-qualified type %qT in " |
15197 | "parameter declaration" , type); |
15198 | else |
15199 | error_at (DECL_SOURCE_LOCATION (decl), |
15200 | "invalid use of type %<void%> in parameter " |
15201 | "declaration" ); |
15202 | /* It's not a good idea to actually create parameters of |
15203 | type `void'; other parts of the compiler assume that a |
15204 | void type terminates the parameter list. */ |
15205 | type = error_mark_node; |
15206 | TREE_TYPE (decl) = error_mark_node; |
15207 | } |
15208 | |
15209 | if (type != error_mark_node) |
15210 | { |
15211 | if (deprecated_state != UNAVAILABLE_DEPRECATED_SUPPRESS) |
15212 | { |
15213 | tree unavailtype = type_is_unavailable (type); |
15214 | if (unavailtype) |
15215 | cp_handle_deprecated_or_unavailable (unavailtype); |
15216 | } |
15217 | if (deprecated_state != DEPRECATED_SUPPRESS |
15218 | && deprecated_state != UNAVAILABLE_DEPRECATED_SUPPRESS) |
15219 | { |
15220 | tree deptype = type_is_deprecated (type); |
15221 | if (deptype) |
15222 | cp_handle_deprecated_or_unavailable (deptype); |
15223 | } |
15224 | |
15225 | /* [dcl.fct] "A parameter with volatile-qualified type is |
15226 | deprecated." */ |
15227 | if (CP_TYPE_VOLATILE_P (type)) |
15228 | warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wvolatile, |
15229 | "%<volatile%>-qualified parameter is " |
15230 | "deprecated" ); |
15231 | |
15232 | /* Top-level qualifiers on the parameters are |
15233 | ignored for function types. */ |
15234 | type = cp_build_qualified_type (type, 0); |
15235 | if (TREE_CODE (type) == METHOD_TYPE) |
15236 | { |
15237 | error ("parameter %qD invalidly declared method type" , decl); |
15238 | type = build_pointer_type (type); |
15239 | TREE_TYPE (decl) = type; |
15240 | } |
15241 | else if (cxx_dialect < cxx17 && INDIRECT_TYPE_P (type)) |
15242 | { |
15243 | /* Before C++17 DR 393: |
15244 | [dcl.fct]/6, parameter types cannot contain pointers |
15245 | (references) to arrays of unknown bound. */ |
15246 | tree t = TREE_TYPE (type); |
15247 | int ptr = TYPE_PTR_P (type); |
15248 | |
15249 | while (1) |
15250 | { |
15251 | if (TYPE_PTR_P (t)) |
15252 | ptr = 1; |
15253 | else if (TREE_CODE (t) != ARRAY_TYPE) |
15254 | break; |
15255 | else if (!TYPE_DOMAIN (t)) |
15256 | break; |
15257 | t = TREE_TYPE (t); |
15258 | } |
15259 | if (TREE_CODE (t) == ARRAY_TYPE) |
15260 | pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic, |
15261 | ptr |
15262 | ? G_("parameter %qD includes pointer to array of " |
15263 | "unknown bound %qT" ) |
15264 | : G_("parameter %qD includes reference to array of " |
15265 | "unknown bound %qT" ), |
15266 | decl, t); |
15267 | } |
15268 | |
15269 | if (init && !processing_template_decl) |
15270 | init = check_default_argument (decl, arg: init, complain: tf_warning_or_error); |
15271 | } |
15272 | |
15273 | DECL_CHAIN (decl) = decls; |
15274 | decls = decl; |
15275 | result = tree_cons (init, type, result); |
15276 | } |
15277 | decls = nreverse (decls); |
15278 | result = nreverse (result); |
15279 | if (parm) |
15280 | result = chainon (result, void_list_node); |
15281 | *parms = decls; |
15282 | if (any_error) |
15283 | result = NULL_TREE; |
15284 | |
15285 | if (any_error) |
15286 | /* We had parm errors, recover by giving the function (...) type. */ |
15287 | result = NULL_TREE; |
15288 | |
15289 | return result; |
15290 | } |
15291 | |
15292 | |
15293 | /* D is a constructor or overloaded `operator='. |
15294 | |
15295 | Let T be the class in which D is declared. Then, this function |
15296 | returns: |
15297 | |
15298 | -1 if D's is an ill-formed constructor or copy assignment operator |
15299 | whose first parameter is of type `T'. |
15300 | 0 if D is not a copy constructor or copy assignment |
15301 | operator. |
15302 | 1 if D is a copy constructor or copy assignment operator whose |
15303 | first parameter is a reference to non-const qualified T. |
15304 | 2 if D is a copy constructor or copy assignment operator whose |
15305 | first parameter is a reference to const qualified T. |
15306 | |
15307 | This function can be used as a predicate. Positive values indicate |
15308 | a copy constructor and nonzero values indicate a copy assignment |
15309 | operator. */ |
15310 | |
15311 | int |
15312 | copy_fn_p (const_tree d) |
15313 | { |
15314 | tree args; |
15315 | tree arg_type; |
15316 | int result = 1; |
15317 | |
15318 | gcc_assert (DECL_FUNCTION_MEMBER_P (d)); |
15319 | |
15320 | if (TREE_CODE (d) == TEMPLATE_DECL |
15321 | || (DECL_TEMPLATE_INFO (d) |
15322 | && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d)))) |
15323 | /* Instantiations of template member functions are never copy |
15324 | functions. Note that member functions of templated classes are |
15325 | represented as template functions internally, and we must |
15326 | accept those as copy functions. */ |
15327 | return 0; |
15328 | |
15329 | if (!DECL_CONSTRUCTOR_P (d) |
15330 | && DECL_NAME (d) != assign_op_identifier) |
15331 | return 0; |
15332 | |
15333 | args = FUNCTION_FIRST_USER_PARMTYPE (d); |
15334 | if (!args) |
15335 | return 0; |
15336 | |
15337 | arg_type = TREE_VALUE (args); |
15338 | if (arg_type == error_mark_node) |
15339 | return 0; |
15340 | |
15341 | if (TYPE_MAIN_VARIANT (arg_type) == DECL_CONTEXT (d)) |
15342 | { |
15343 | /* Pass by value copy assignment operator. */ |
15344 | result = -1; |
15345 | } |
15346 | else if (TYPE_REF_P (arg_type) |
15347 | && !TYPE_REF_IS_RVALUE (arg_type) |
15348 | && TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)) == DECL_CONTEXT (d)) |
15349 | { |
15350 | if (CP_TYPE_CONST_P (TREE_TYPE (arg_type))) |
15351 | result = 2; |
15352 | } |
15353 | else |
15354 | return 0; |
15355 | |
15356 | args = TREE_CHAIN (args); |
15357 | |
15358 | if (args && args != void_list_node && !TREE_PURPOSE (args)) |
15359 | /* There are more non-optional args. */ |
15360 | return 0; |
15361 | |
15362 | return result; |
15363 | } |
15364 | |
15365 | /* D is a constructor or overloaded `operator='. |
15366 | |
15367 | Let T be the class in which D is declared. Then, this function |
15368 | returns true when D is a move constructor or move assignment |
15369 | operator, false otherwise. */ |
15370 | |
15371 | bool |
15372 | move_fn_p (const_tree d) |
15373 | { |
15374 | if (cxx_dialect == cxx98) |
15375 | /* There are no move constructors if we are in C++98 mode. */ |
15376 | return false; |
15377 | |
15378 | if (TREE_CODE (d) == TEMPLATE_DECL |
15379 | || (DECL_TEMPLATE_INFO (d) |
15380 | && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d)))) |
15381 | /* Instantiations of template member functions are never move |
15382 | functions. Note that member functions of templated classes are |
15383 | represented as template functions internally, and we must |
15384 | accept those as move functions. */ |
15385 | return 0; |
15386 | |
15387 | return move_signature_fn_p (d); |
15388 | } |
15389 | |
15390 | /* D is a constructor or overloaded `operator='. |
15391 | |
15392 | Then, this function returns true when D has the same signature as a move |
15393 | constructor or move assignment operator (because either it is such a |
15394 | ctor/op= or it is a template specialization with the same signature), |
15395 | false otherwise. */ |
15396 | |
15397 | bool |
15398 | move_signature_fn_p (const_tree d) |
15399 | { |
15400 | tree args; |
15401 | tree arg_type; |
15402 | bool result = false; |
15403 | |
15404 | if (!DECL_CONSTRUCTOR_P (d) |
15405 | && DECL_NAME (d) != assign_op_identifier) |
15406 | return 0; |
15407 | |
15408 | args = FUNCTION_FIRST_USER_PARMTYPE (d); |
15409 | if (!args) |
15410 | return 0; |
15411 | |
15412 | arg_type = TREE_VALUE (args); |
15413 | if (arg_type == error_mark_node) |
15414 | return 0; |
15415 | |
15416 | if (TYPE_REF_P (arg_type) |
15417 | && TYPE_REF_IS_RVALUE (arg_type) |
15418 | && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)), |
15419 | DECL_CONTEXT (d))) |
15420 | result = true; |
15421 | |
15422 | args = TREE_CHAIN (args); |
15423 | |
15424 | if (args && args != void_list_node && !TREE_PURPOSE (args)) |
15425 | /* There are more non-optional args. */ |
15426 | return false; |
15427 | |
15428 | return result; |
15429 | } |
15430 | |
15431 | /* Remember any special properties of member function DECL. */ |
15432 | |
15433 | void |
15434 | grok_special_member_properties (tree decl) |
15435 | { |
15436 | tree class_type; |
15437 | |
15438 | if (TREE_CODE (decl) == USING_DECL |
15439 | || !DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) |
15440 | return; |
15441 | |
15442 | class_type = DECL_CONTEXT (decl); |
15443 | if (IDENTIFIER_CTOR_P (DECL_NAME (decl))) |
15444 | { |
15445 | int ctor = copy_fn_p (d: decl); |
15446 | |
15447 | if (!DECL_ARTIFICIAL (decl)) |
15448 | TYPE_HAS_USER_CONSTRUCTOR (class_type) = 1; |
15449 | |
15450 | if (ctor > 0) |
15451 | { |
15452 | /* [class.copy] |
15453 | |
15454 | A non-template constructor for class X is a copy |
15455 | constructor if its first parameter is of type X&, const |
15456 | X&, volatile X& or const volatile X&, and either there |
15457 | are no other parameters or else all other parameters have |
15458 | default arguments. */ |
15459 | TYPE_HAS_COPY_CTOR (class_type) = 1; |
15460 | if (ctor > 1) |
15461 | TYPE_HAS_CONST_COPY_CTOR (class_type) = 1; |
15462 | } |
15463 | |
15464 | if (sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (decl))) |
15465 | TYPE_HAS_DEFAULT_CONSTRUCTOR (class_type) = 1; |
15466 | |
15467 | if (is_list_ctor (decl)) |
15468 | TYPE_HAS_LIST_CTOR (class_type) = 1; |
15469 | |
15470 | if (maybe_constexpr_fn (decl) |
15471 | && !ctor && !move_fn_p (d: decl)) |
15472 | TYPE_HAS_CONSTEXPR_CTOR (class_type) = 1; |
15473 | } |
15474 | else if (DECL_NAME (decl) == assign_op_identifier) |
15475 | { |
15476 | /* [class.copy] |
15477 | |
15478 | A non-template assignment operator for class X is a copy |
15479 | assignment operator if its parameter is of type X, X&, const |
15480 | X&, volatile X& or const volatile X&. */ |
15481 | |
15482 | int assop = copy_fn_p (d: decl); |
15483 | |
15484 | if (assop) |
15485 | { |
15486 | TYPE_HAS_COPY_ASSIGN (class_type) = 1; |
15487 | if (assop != 1) |
15488 | TYPE_HAS_CONST_COPY_ASSIGN (class_type) = 1; |
15489 | } |
15490 | } |
15491 | else if (IDENTIFIER_CONV_OP_P (DECL_NAME (decl))) |
15492 | TYPE_HAS_CONVERSION (class_type) = true; |
15493 | |
15494 | /* Destructors are handled in check_methods. */ |
15495 | } |
15496 | |
15497 | /* Check a constructor DECL has the correct form. Complains |
15498 | if the class has a constructor of the form X(X). */ |
15499 | |
15500 | bool |
15501 | grok_ctor_properties (const_tree ctype, const_tree decl) |
15502 | { |
15503 | int ctor_parm = copy_fn_p (d: decl); |
15504 | |
15505 | if (ctor_parm < 0) |
15506 | { |
15507 | /* [class.copy] |
15508 | |
15509 | A declaration of a constructor for a class X is ill-formed if |
15510 | its first parameter is of type (optionally cv-qualified) X |
15511 | and either there are no other parameters or else all other |
15512 | parameters have default arguments. |
15513 | |
15514 | We *don't* complain about member template instantiations that |
15515 | have this form, though; they can occur as we try to decide |
15516 | what constructor to use during overload resolution. Since |
15517 | overload resolution will never prefer such a constructor to |
15518 | the non-template copy constructor (which is either explicitly |
15519 | or implicitly defined), there's no need to worry about their |
15520 | existence. Theoretically, they should never even be |
15521 | instantiated, but that's hard to forestall. */ |
15522 | error_at (DECL_SOURCE_LOCATION (decl), |
15523 | "invalid constructor; you probably meant %<%T (const %T&)%>" , |
15524 | ctype, ctype); |
15525 | return false; |
15526 | } |
15527 | |
15528 | return true; |
15529 | } |
15530 | |
15531 | /* DECL is a declaration for an overloaded or conversion operator. If |
15532 | COMPLAIN is true, errors are issued for invalid declarations. */ |
15533 | |
15534 | bool |
15535 | grok_op_properties (tree decl, bool complain) |
15536 | { |
15537 | tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl)); |
15538 | bool methodp = TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE; |
15539 | tree name = DECL_NAME (decl); |
15540 | location_t loc = DECL_SOURCE_LOCATION (decl); |
15541 | |
15542 | tree class_type = DECL_CONTEXT (decl); |
15543 | if (class_type && !CLASS_TYPE_P (class_type)) |
15544 | class_type = NULL_TREE; |
15545 | |
15546 | tree_code operator_code; |
15547 | unsigned op_flags; |
15548 | if (IDENTIFIER_CONV_OP_P (name)) |
15549 | { |
15550 | /* Conversion operators are TYPE_EXPR for the purposes of this |
15551 | function. */ |
15552 | operator_code = TYPE_EXPR; |
15553 | op_flags = OVL_OP_FLAG_UNARY; |
15554 | } |
15555 | else |
15556 | { |
15557 | const ovl_op_info_t *ovl_op = IDENTIFIER_OVL_OP_INFO (name); |
15558 | |
15559 | operator_code = ovl_op->tree_code; |
15560 | op_flags = ovl_op->flags; |
15561 | gcc_checking_assert (operator_code != ERROR_MARK); |
15562 | DECL_OVERLOADED_OPERATOR_CODE_RAW (decl) = ovl_op->ovl_op_code; |
15563 | } |
15564 | |
15565 | if (op_flags & OVL_OP_FLAG_ALLOC) |
15566 | { |
15567 | /* operator new and operator delete are quite special. */ |
15568 | if (class_type) |
15569 | switch (op_flags) |
15570 | { |
15571 | case OVL_OP_FLAG_ALLOC: |
15572 | TYPE_HAS_NEW_OPERATOR (class_type) = 1; |
15573 | break; |
15574 | |
15575 | case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_DELETE: |
15576 | TYPE_GETS_DELETE (class_type) |= 1; |
15577 | break; |
15578 | |
15579 | case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_VEC: |
15580 | TYPE_HAS_ARRAY_NEW_OPERATOR (class_type) = 1; |
15581 | break; |
15582 | |
15583 | case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_DELETE | OVL_OP_FLAG_VEC: |
15584 | TYPE_GETS_DELETE (class_type) |= 2; |
15585 | break; |
15586 | |
15587 | default: |
15588 | gcc_unreachable (); |
15589 | } |
15590 | |
15591 | /* [basic.std.dynamic.allocation]/1: |
15592 | |
15593 | A program is ill-formed if an allocation function is declared |
15594 | in a namespace scope other than global scope or declared |
15595 | static in global scope. |
15596 | |
15597 | The same also holds true for deallocation functions. */ |
15598 | if (DECL_NAMESPACE_SCOPE_P (decl)) |
15599 | { |
15600 | if (CP_DECL_CONTEXT (decl) != global_namespace) |
15601 | { |
15602 | error_at (loc, "%qD may not be declared within a namespace" , |
15603 | decl); |
15604 | return false; |
15605 | } |
15606 | |
15607 | if (!TREE_PUBLIC (decl)) |
15608 | { |
15609 | error_at (loc, "%qD may not be declared as static" , decl); |
15610 | return false; |
15611 | } |
15612 | } |
15613 | |
15614 | if (op_flags & OVL_OP_FLAG_DELETE) |
15615 | { |
15616 | DECL_SET_IS_OPERATOR_DELETE (decl, true); |
15617 | coerce_delete_type (decl, loc); |
15618 | } |
15619 | else |
15620 | { |
15621 | DECL_SET_IS_OPERATOR_NEW (decl, true); |
15622 | TREE_TYPE (decl) = coerce_new_type (TREE_TYPE (decl), loc); |
15623 | } |
15624 | |
15625 | return true; |
15626 | } |
15627 | |
15628 | /* An operator function must either be a non-static member function |
15629 | or have at least one parameter of a class, a reference to a class, |
15630 | an enumeration, or a reference to an enumeration. 13.4.0.6 */ |
15631 | if (! methodp || DECL_STATIC_FUNCTION_P (decl)) |
15632 | { |
15633 | if (operator_code == TYPE_EXPR |
15634 | || operator_code == COMPONENT_REF |
15635 | || operator_code == NOP_EXPR) |
15636 | { |
15637 | error_at (loc, "%qD must be a non-static member function" , decl); |
15638 | return false; |
15639 | } |
15640 | |
15641 | if (operator_code == CALL_EXPR || operator_code == ARRAY_REF) |
15642 | { |
15643 | if (! DECL_STATIC_FUNCTION_P (decl)) |
15644 | { |
15645 | error_at (loc, "%qD must be a member function" , decl); |
15646 | return false; |
15647 | } |
15648 | if (cxx_dialect < cxx23 |
15649 | /* For lambdas we diagnose static lambda specifier elsewhere. */ |
15650 | && (operator_code == ARRAY_REF || ! LAMBDA_FUNCTION_P (decl)) |
15651 | /* For instantiations, we have diagnosed this already. */ |
15652 | && ! DECL_USE_TEMPLATE (decl)) |
15653 | pedwarn (loc, OPT_Wc__23_extensions, "%qD may be a static member " |
15654 | "function only with %<-std=c++23%> or %<-std=gnu++23%>" , |
15655 | decl); |
15656 | if (operator_code == ARRAY_REF) |
15657 | /* static operator[] should have exactly one argument |
15658 | for C++20 and earlier, so that it isn't multidimensional. */ |
15659 | op_flags = OVL_OP_FLAG_UNARY; |
15660 | } |
15661 | else if (DECL_STATIC_FUNCTION_P (decl)) |
15662 | { |
15663 | error_at (loc, "%qD must be either a non-static member " |
15664 | "function or a non-member function" , decl); |
15665 | return false; |
15666 | } |
15667 | else |
15668 | for (tree arg = argtypes; ; arg = TREE_CHAIN (arg)) |
15669 | { |
15670 | if (!arg || arg == void_list_node) |
15671 | { |
15672 | if (complain) |
15673 | error_at (loc, "%qD must have an argument of class or " |
15674 | "enumerated type" , decl); |
15675 | return false; |
15676 | } |
15677 | |
15678 | tree type = non_reference (TREE_VALUE (arg)); |
15679 | if (type == error_mark_node) |
15680 | return false; |
15681 | |
15682 | /* MAYBE_CLASS_TYPE_P, rather than CLASS_TYPE_P, is used |
15683 | because these checks are performed even on template |
15684 | functions. */ |
15685 | if (MAYBE_CLASS_TYPE_P (type) |
15686 | || TREE_CODE (type) == ENUMERAL_TYPE) |
15687 | break; |
15688 | } |
15689 | } |
15690 | |
15691 | if (operator_code == CALL_EXPR) |
15692 | /* There are no further restrictions on the arguments to an overloaded |
15693 | "operator ()". */ |
15694 | return true; |
15695 | |
15696 | /* C++23 allows an arbitrary number of parameters and default arguments for |
15697 | operator[], and none of the other checks below apply. */ |
15698 | if (operator_code == ARRAY_REF && cxx_dialect >= cxx23) |
15699 | return true; |
15700 | |
15701 | if (operator_code == COND_EXPR) |
15702 | { |
15703 | /* 13.4.0.3 */ |
15704 | error_at (loc, "ISO C++ prohibits overloading %<operator ?:%>" ); |
15705 | return false; |
15706 | } |
15707 | |
15708 | /* Count the number of arguments and check for ellipsis. */ |
15709 | int arity = 0; |
15710 | for (tree arg = argtypes; arg != void_list_node; arg = TREE_CHAIN (arg)) |
15711 | { |
15712 | if (!arg) |
15713 | { |
15714 | error_at (loc, "%qD must not have variable number of arguments" , |
15715 | decl); |
15716 | return false; |
15717 | } |
15718 | ++arity; |
15719 | } |
15720 | |
15721 | /* Verify correct number of arguments. */ |
15722 | switch (op_flags) |
15723 | { |
15724 | case OVL_OP_FLAG_AMBIARY: |
15725 | if (arity == 1) |
15726 | { |
15727 | /* We have a unary instance of an ambi-ary op. Remap to the |
15728 | unary one. */ |
15729 | unsigned alt = ovl_op_alternate[ovl_op_mapping [operator_code]]; |
15730 | const ovl_op_info_t *ovl_op = &ovl_op_info[false][alt]; |
15731 | gcc_checking_assert (ovl_op->flags == OVL_OP_FLAG_UNARY); |
15732 | operator_code = ovl_op->tree_code; |
15733 | DECL_OVERLOADED_OPERATOR_CODE_RAW (decl) = ovl_op->ovl_op_code; |
15734 | } |
15735 | else if (arity != 2) |
15736 | { |
15737 | /* This was an ambiguous operator but is invalid. */ |
15738 | error_at (loc, |
15739 | methodp |
15740 | ? G_("%qD must have either zero or one argument" ) |
15741 | : G_("%qD must have either one or two arguments" ), decl); |
15742 | return false; |
15743 | } |
15744 | else if ((operator_code == POSTINCREMENT_EXPR |
15745 | || operator_code == POSTDECREMENT_EXPR) |
15746 | && ! processing_template_decl |
15747 | /* x++ and x--'s second argument must be an int. */ |
15748 | && ! same_type_p (TREE_VALUE (TREE_CHAIN (argtypes)), |
15749 | integer_type_node)) |
15750 | { |
15751 | error_at (loc, |
15752 | methodp |
15753 | ? G_("postfix %qD must have %<int%> as its argument" ) |
15754 | : G_("postfix %qD must have %<int%> as its second argument" ), |
15755 | decl); |
15756 | return false; |
15757 | } |
15758 | break; |
15759 | |
15760 | case OVL_OP_FLAG_UNARY: |
15761 | if (arity != 1) |
15762 | { |
15763 | error_at (loc, |
15764 | methodp |
15765 | ? G_("%qD must have no arguments" ) |
15766 | : G_("%qD must have exactly one argument" ), decl); |
15767 | return false; |
15768 | } |
15769 | break; |
15770 | |
15771 | case OVL_OP_FLAG_BINARY: |
15772 | if (arity != 2) |
15773 | { |
15774 | error_at (loc, |
15775 | methodp |
15776 | ? G_("%qD must have exactly one argument" ) |
15777 | : G_("%qD must have exactly two arguments" ), decl); |
15778 | return false; |
15779 | } |
15780 | break; |
15781 | |
15782 | default: |
15783 | gcc_unreachable (); |
15784 | } |
15785 | |
15786 | /* There can be no default arguments. */ |
15787 | for (tree arg = argtypes; arg && arg != void_list_node; |
15788 | arg = TREE_CHAIN (arg)) |
15789 | if (TREE_PURPOSE (arg)) |
15790 | { |
15791 | TREE_PURPOSE (arg) = NULL_TREE; |
15792 | error_at (loc, "%qD cannot have default arguments" , decl); |
15793 | return false; |
15794 | } |
15795 | |
15796 | /* At this point the declaration is well-formed. It may not be |
15797 | sensible though. */ |
15798 | |
15799 | /* Check member function warnings only on the in-class declaration. |
15800 | There's no point warning on an out-of-class definition. */ |
15801 | if (class_type && class_type != current_class_type) |
15802 | return true; |
15803 | |
15804 | /* Warn about conversion operators that will never be used. */ |
15805 | if (IDENTIFIER_CONV_OP_P (name) |
15806 | && ! DECL_TEMPLATE_INFO (decl) |
15807 | && warn_class_conversion) |
15808 | { |
15809 | tree t = TREE_TYPE (name); |
15810 | int ref = TYPE_REF_P (t); |
15811 | |
15812 | if (ref) |
15813 | t = TYPE_MAIN_VARIANT (TREE_TYPE (t)); |
15814 | |
15815 | if (VOID_TYPE_P (t)) |
15816 | warning_at (loc, OPT_Wclass_conversion, "converting %qT to %<void%> " |
15817 | "will never use a type conversion operator" , class_type); |
15818 | else if (class_type) |
15819 | { |
15820 | if (same_type_ignoring_top_level_qualifiers_p (t, class_type)) |
15821 | warning_at (loc, OPT_Wclass_conversion, |
15822 | ref |
15823 | ? G_("converting %qT to a reference to the same type " |
15824 | "will never use a type conversion operator" ) |
15825 | : G_("converting %qT to the same type " |
15826 | "will never use a type conversion operator" ), |
15827 | class_type); |
15828 | /* Don't force t to be complete here. */ |
15829 | else if (MAYBE_CLASS_TYPE_P (t) |
15830 | && COMPLETE_TYPE_P (t) |
15831 | && DERIVED_FROM_P (t, class_type)) |
15832 | warning_at (loc, OPT_Wclass_conversion, |
15833 | ref |
15834 | ? G_("converting %qT to a reference to a base class " |
15835 | "%qT will never use a type conversion operator" ) |
15836 | : G_("converting %qT to a base class %qT " |
15837 | "will never use a type conversion operator" ), |
15838 | class_type, t); |
15839 | } |
15840 | } |
15841 | |
15842 | if (!warn_ecpp) |
15843 | return true; |
15844 | |
15845 | /* Effective C++ rules below. */ |
15846 | |
15847 | /* More Effective C++ rule 7. */ |
15848 | if (operator_code == TRUTH_ANDIF_EXPR |
15849 | || operator_code == TRUTH_ORIF_EXPR |
15850 | || operator_code == COMPOUND_EXPR) |
15851 | warning_at (loc, OPT_Weffc__, |
15852 | "user-defined %qD always evaluates both arguments" , decl); |
15853 | |
15854 | /* More Effective C++ rule 6. */ |
15855 | if (operator_code == POSTINCREMENT_EXPR |
15856 | || operator_code == POSTDECREMENT_EXPR |
15857 | || operator_code == PREINCREMENT_EXPR |
15858 | || operator_code == PREDECREMENT_EXPR) |
15859 | { |
15860 | tree arg = TREE_VALUE (argtypes); |
15861 | tree ret = TREE_TYPE (TREE_TYPE (decl)); |
15862 | if (methodp || TYPE_REF_P (arg)) |
15863 | arg = TREE_TYPE (arg); |
15864 | arg = TYPE_MAIN_VARIANT (arg); |
15865 | |
15866 | if (operator_code == PREINCREMENT_EXPR |
15867 | || operator_code == PREDECREMENT_EXPR) |
15868 | { |
15869 | if (!TYPE_REF_P (ret) |
15870 | || !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (ret)), arg)) |
15871 | warning_at (loc, OPT_Weffc__, "prefix %qD should return %qT" , decl, |
15872 | build_reference_type (arg)); |
15873 | } |
15874 | else |
15875 | { |
15876 | if (!same_type_p (TYPE_MAIN_VARIANT (ret), arg)) |
15877 | warning_at (loc, OPT_Weffc__, |
15878 | "postfix %qD should return %qT" , decl, arg); |
15879 | } |
15880 | } |
15881 | |
15882 | /* Effective C++ rule 23. */ |
15883 | if (!DECL_ASSIGNMENT_OPERATOR_P (decl) |
15884 | && (operator_code == PLUS_EXPR |
15885 | || operator_code == MINUS_EXPR |
15886 | || operator_code == TRUNC_DIV_EXPR |
15887 | || operator_code == MULT_EXPR |
15888 | || operator_code == TRUNC_MOD_EXPR) |
15889 | && TYPE_REF_P (TREE_TYPE (TREE_TYPE (decl)))) |
15890 | warning_at (loc, OPT_Weffc__, "%qD should return by value" , decl); |
15891 | |
15892 | return true; |
15893 | } |
15894 | |
15895 | /* Return a string giving the keyword associate with CODE. */ |
15896 | |
15897 | static const char * |
15898 | tag_name (enum tag_types code) |
15899 | { |
15900 | switch (code) |
15901 | { |
15902 | case record_type: |
15903 | return "struct" ; |
15904 | case class_type: |
15905 | return "class" ; |
15906 | case union_type: |
15907 | return "union" ; |
15908 | case enum_type: |
15909 | return "enum" ; |
15910 | case typename_type: |
15911 | return "typename" ; |
15912 | default: |
15913 | gcc_unreachable (); |
15914 | } |
15915 | } |
15916 | |
15917 | /* Name lookup in an elaborated-type-specifier (after the keyword |
15918 | indicated by TAG_CODE) has found the TYPE_DECL DECL. If the |
15919 | elaborated-type-specifier is invalid, issue a diagnostic and return |
15920 | error_mark_node; otherwise, return the *_TYPE to which it referred. |
15921 | If ALLOW_TEMPLATE_P is true, TYPE may be a class template. */ |
15922 | |
15923 | tree |
15924 | check_elaborated_type_specifier (enum tag_types tag_code, |
15925 | tree decl, |
15926 | bool allow_template_p) |
15927 | { |
15928 | tree type; |
15929 | |
15930 | /* In the case of: |
15931 | |
15932 | struct S { struct S *p; }; |
15933 | |
15934 | name lookup will find the TYPE_DECL for the implicit "S::S" |
15935 | typedef. Adjust for that here. */ |
15936 | if (DECL_SELF_REFERENCE_P (decl)) |
15937 | decl = TYPE_NAME (TREE_TYPE (decl)); |
15938 | |
15939 | type = TREE_TYPE (decl); |
15940 | |
15941 | /* Check TEMPLATE_TYPE_PARM first because DECL_IMPLICIT_TYPEDEF_P |
15942 | is false for this case as well. */ |
15943 | if (TREE_CODE (type) == TEMPLATE_TYPE_PARM) |
15944 | { |
15945 | error ("using template type parameter %qT after %qs" , |
15946 | type, tag_name (code: tag_code)); |
15947 | return error_mark_node; |
15948 | } |
15949 | /* Accept template template parameters. */ |
15950 | else if (allow_template_p |
15951 | && (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM |
15952 | || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)) |
15953 | ; |
15954 | /* [dcl.type.elab] |
15955 | |
15956 | If the identifier resolves to a typedef-name or the |
15957 | simple-template-id resolves to an alias template |
15958 | specialization, the elaborated-type-specifier is ill-formed. |
15959 | |
15960 | In other words, the only legitimate declaration to use in the |
15961 | elaborated type specifier is the implicit typedef created when |
15962 | the type is declared. */ |
15963 | else if (!DECL_IMPLICIT_TYPEDEF_P (decl) |
15964 | && !DECL_SELF_REFERENCE_P (decl) |
15965 | && tag_code != typename_type) |
15966 | { |
15967 | if (alias_template_specialization_p (type, nt_opaque)) |
15968 | error ("using alias template specialization %qT after %qs" , |
15969 | type, tag_name (code: tag_code)); |
15970 | else |
15971 | error ("using typedef-name %qD after %qs" , decl, tag_name (code: tag_code)); |
15972 | inform (DECL_SOURCE_LOCATION (decl), |
15973 | "%qD has a previous declaration here" , decl); |
15974 | return error_mark_node; |
15975 | } |
15976 | else if (TREE_CODE (type) != RECORD_TYPE |
15977 | && TREE_CODE (type) != UNION_TYPE |
15978 | && tag_code != enum_type |
15979 | && tag_code != typename_type) |
15980 | { |
15981 | error ("%qT referred to as %qs" , type, tag_name (code: tag_code)); |
15982 | inform (location_of (type), "%qT has a previous declaration here" , type); |
15983 | return error_mark_node; |
15984 | } |
15985 | else if (TREE_CODE (type) != ENUMERAL_TYPE |
15986 | && tag_code == enum_type) |
15987 | { |
15988 | error ("%qT referred to as enum" , type); |
15989 | inform (location_of (type), "%qT has a previous declaration here" , type); |
15990 | return error_mark_node; |
15991 | } |
15992 | else if (!allow_template_p |
15993 | && TREE_CODE (type) == RECORD_TYPE |
15994 | && CLASSTYPE_IS_TEMPLATE (type)) |
15995 | { |
15996 | /* If a class template appears as elaborated type specifier |
15997 | without a template header such as: |
15998 | |
15999 | template <class T> class C {}; |
16000 | void f(class C); // No template header here |
16001 | |
16002 | then the required template argument is missing. */ |
16003 | error ("template argument required for %<%s %T%>" , |
16004 | tag_name (code: tag_code), |
16005 | DECL_NAME (CLASSTYPE_TI_TEMPLATE (type))); |
16006 | return error_mark_node; |
16007 | } |
16008 | |
16009 | return type; |
16010 | } |
16011 | |
16012 | /* Lookup NAME of an elaborated type specifier according to SCOPE and |
16013 | issue diagnostics if necessary. Return *_TYPE node upon success, |
16014 | NULL_TREE when the NAME is not found, and ERROR_MARK_NODE for type |
16015 | error. */ |
16016 | |
16017 | static tree |
16018 | lookup_and_check_tag (enum tag_types tag_code, tree name, |
16019 | TAG_how how, bool ) |
16020 | { |
16021 | tree decl; |
16022 | if (how == TAG_how::GLOBAL) |
16023 | { |
16024 | /* First try ordinary name lookup, ignoring hidden class name |
16025 | injected via friend declaration. */ |
16026 | decl = lookup_name (name, want: LOOK_want::TYPE); |
16027 | decl = strip_using_decl (decl); |
16028 | /* If that fails, the name will be placed in the smallest |
16029 | non-class, non-function-prototype scope according to 3.3.1/5. |
16030 | We may already have a hidden name declared as friend in this |
16031 | scope. So lookup again but not ignoring hidden names. |
16032 | If we find one, that name will be made visible rather than |
16033 | creating a new tag. */ |
16034 | if (!decl) |
16035 | decl = lookup_elaborated_type (name, TAG_how::INNERMOST_NON_CLASS); |
16036 | } |
16037 | else |
16038 | decl = lookup_elaborated_type (name, how); |
16039 | |
16040 | if (!decl) |
16041 | /* We found nothing. */ |
16042 | return NULL_TREE; |
16043 | |
16044 | if (TREE_CODE (decl) == TREE_LIST) |
16045 | { |
16046 | error ("reference to %qD is ambiguous" , name); |
16047 | print_candidates (decl); |
16048 | return error_mark_node; |
16049 | } |
16050 | |
16051 | if (DECL_CLASS_TEMPLATE_P (decl) |
16052 | && !template_header_p |
16053 | && how == TAG_how::CURRENT_ONLY) |
16054 | { |
16055 | error ("class template %qD redeclared as non-template" , name); |
16056 | inform (location_of (decl), "previous declaration here" ); |
16057 | CLASSTYPE_ERRONEOUS (TREE_TYPE (decl)) = true; |
16058 | return error_mark_node; |
16059 | } |
16060 | |
16061 | if (DECL_CLASS_TEMPLATE_P (decl) |
16062 | /* If scope is TAG_how::CURRENT_ONLY we're defining a class, |
16063 | so ignore a template template parameter. */ |
16064 | || (how != TAG_how::CURRENT_ONLY && DECL_TEMPLATE_TEMPLATE_PARM_P (decl))) |
16065 | decl = DECL_TEMPLATE_RESULT (decl); |
16066 | |
16067 | if (TREE_CODE (decl) != TYPE_DECL) |
16068 | /* Found not-a-type. */ |
16069 | return NULL_TREE; |
16070 | |
16071 | /* Look for invalid nested type: |
16072 | class C { |
16073 | class C {}; |
16074 | }; */ |
16075 | if (how == TAG_how::CURRENT_ONLY && DECL_SELF_REFERENCE_P (decl)) |
16076 | { |
16077 | error ("%qD has the same name as the class in which it is " |
16078 | "declared" , decl); |
16079 | return error_mark_node; |
16080 | } |
16081 | |
16082 | /* Two cases we need to consider when deciding if a class |
16083 | template is allowed as an elaborated type specifier: |
16084 | 1. It is a self reference to its own class. |
16085 | 2. It comes with a template header. |
16086 | |
16087 | For example: |
16088 | |
16089 | template <class T> class C { |
16090 | class C *c1; // DECL_SELF_REFERENCE_P is true |
16091 | class D; |
16092 | }; |
16093 | template <class U> class C; // template_header_p is true |
16094 | template <class T> class C<T>::D { |
16095 | class C *c2; // DECL_SELF_REFERENCE_P is true |
16096 | }; */ |
16097 | |
16098 | tree t = check_elaborated_type_specifier (tag_code, decl, |
16099 | allow_template_p: template_header_p |
16100 | | DECL_SELF_REFERENCE_P (decl)); |
16101 | if (template_header_p && t && CLASS_TYPE_P (t) |
16102 | && (!CLASSTYPE_TEMPLATE_INFO (t) |
16103 | || (!PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t))))) |
16104 | { |
16105 | error ("%qT is not a template" , t); |
16106 | inform (location_of (t), "previous declaration here" ); |
16107 | if (TYPE_CLASS_SCOPE_P (t) |
16108 | && CLASSTYPE_TEMPLATE_INFO (TYPE_CONTEXT (t))) |
16109 | inform (input_location, |
16110 | "perhaps you want to explicitly add %<%T::%>" , |
16111 | TYPE_CONTEXT (t)); |
16112 | return error_mark_node; |
16113 | } |
16114 | |
16115 | return t; |
16116 | } |
16117 | |
16118 | /* Get the struct, enum or union (TAG_CODE says which) with tag NAME. |
16119 | Define the tag as a forward-reference if it is not defined. |
16120 | |
16121 | If a declaration is given, process it here, and report an error if |
16122 | multiple declarations are not identical. |
16123 | |
16124 | SCOPE is TS_CURRENT when this is also a definition. Only look in |
16125 | the current frame for the name (since C++ allows new names in any |
16126 | scope.) It is TS_WITHIN_ENCLOSING_NON_CLASS if this is a friend |
16127 | declaration. Only look beginning from the current scope outward up |
16128 | till the nearest non-class scope. Otherwise it is TS_GLOBAL. |
16129 | |
16130 | TEMPLATE_HEADER_P is true when this declaration is preceded by |
16131 | a set of template parameters. */ |
16132 | |
16133 | tree |
16134 | xref_tag (enum tag_types tag_code, tree name, |
16135 | TAG_how how, bool ) |
16136 | { |
16137 | enum tree_code code; |
16138 | tree context = NULL_TREE; |
16139 | |
16140 | auto_cond_timevar tv (TV_NAME_LOOKUP); |
16141 | |
16142 | gcc_assert (identifier_p (name)); |
16143 | |
16144 | switch (tag_code) |
16145 | { |
16146 | case record_type: |
16147 | case class_type: |
16148 | code = RECORD_TYPE; |
16149 | break; |
16150 | case union_type: |
16151 | code = UNION_TYPE; |
16152 | break; |
16153 | case enum_type: |
16154 | code = ENUMERAL_TYPE; |
16155 | break; |
16156 | default: |
16157 | gcc_unreachable (); |
16158 | } |
16159 | |
16160 | /* In case of anonymous name, xref_tag is only called to |
16161 | make type node and push name. Name lookup is not required. */ |
16162 | tree t = NULL_TREE; |
16163 | if (!IDENTIFIER_ANON_P (name)) |
16164 | t = lookup_and_check_tag (tag_code, name, how, template_header_p); |
16165 | |
16166 | if (t == error_mark_node) |
16167 | return error_mark_node; |
16168 | |
16169 | if (how != TAG_how::CURRENT_ONLY && t && current_class_type |
16170 | && template_class_depth (current_class_type) |
16171 | && template_header_p) |
16172 | { |
16173 | if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM) |
16174 | return t; |
16175 | |
16176 | /* Since HOW is not TAG_how::CURRENT_ONLY, we are not looking at |
16177 | a definition of this tag. Since, in addition, we are |
16178 | currently processing a (member) template declaration of a |
16179 | template class, we must be very careful; consider: |
16180 | |
16181 | template <class X> struct S1 |
16182 | |
16183 | template <class U> struct S2 |
16184 | { |
16185 | template <class V> friend struct S1; |
16186 | }; |
16187 | |
16188 | Here, the S2::S1 declaration should not be confused with the |
16189 | outer declaration. In particular, the inner version should |
16190 | have a template parameter of level 2, not level 1. |
16191 | |
16192 | On the other hand, when presented with: |
16193 | |
16194 | template <class T> struct S1 |
16195 | { |
16196 | template <class U> struct S2 {}; |
16197 | template <class U> friend struct S2; |
16198 | }; |
16199 | |
16200 | the friend must find S1::S2 eventually. We accomplish this |
16201 | by making sure that the new type we create to represent this |
16202 | declaration has the right TYPE_CONTEXT. */ |
16203 | context = TYPE_CONTEXT (t); |
16204 | t = NULL_TREE; |
16205 | } |
16206 | |
16207 | if (! t) |
16208 | { |
16209 | /* If no such tag is yet defined, create a forward-reference node |
16210 | and record it as the "definition". |
16211 | When a real declaration of this type is found, |
16212 | the forward-reference will be altered into a real type. */ |
16213 | if (code == ENUMERAL_TYPE) |
16214 | { |
16215 | error ("use of enum %q#D without previous declaration" , name); |
16216 | return error_mark_node; |
16217 | } |
16218 | |
16219 | t = make_class_type (code); |
16220 | TYPE_CONTEXT (t) = context; |
16221 | if (IDENTIFIER_LAMBDA_P (name)) |
16222 | /* Mark it as a lambda type right now. Our caller will |
16223 | correct the value. */ |
16224 | CLASSTYPE_LAMBDA_EXPR (t) = error_mark_node; |
16225 | t = pushtag (name, t, how); |
16226 | } |
16227 | else |
16228 | { |
16229 | if (template_header_p && MAYBE_CLASS_TYPE_P (t)) |
16230 | { |
16231 | /* Check that we aren't trying to overload a class with different |
16232 | constraints. */ |
16233 | if (!redeclare_class_template (t, current_template_parms, |
16234 | current_template_constraints ())) |
16235 | return error_mark_node; |
16236 | } |
16237 | else if (!processing_template_decl |
16238 | && CLASS_TYPE_P (t) |
16239 | && CLASSTYPE_IS_TEMPLATE (t)) |
16240 | { |
16241 | error ("redeclaration of %qT as a non-template" , t); |
16242 | inform (location_of (t), "previous declaration %qD" , t); |
16243 | return error_mark_node; |
16244 | } |
16245 | |
16246 | if (modules_p () |
16247 | && how == TAG_how::CURRENT_ONLY) |
16248 | { |
16249 | tree decl = TYPE_NAME (t); |
16250 | if (!module_may_redeclare (decl)) |
16251 | { |
16252 | error ("cannot declare %qD in a different module" , decl); |
16253 | inform (DECL_SOURCE_LOCATION (decl), "declared here" ); |
16254 | return error_mark_node; |
16255 | } |
16256 | |
16257 | tree maybe_tmpl = decl; |
16258 | if (CLASS_TYPE_P (t) && CLASSTYPE_IS_TEMPLATE (t)) |
16259 | maybe_tmpl = CLASSTYPE_TI_TEMPLATE (t); |
16260 | |
16261 | if (DECL_LANG_SPECIFIC (decl) |
16262 | && DECL_MODULE_IMPORT_P (decl) |
16263 | && TREE_CODE (CP_DECL_CONTEXT (decl)) == NAMESPACE_DECL) |
16264 | { |
16265 | /* Push it into this TU's symbol slot. */ |
16266 | gcc_checking_assert (current_namespace == CP_DECL_CONTEXT (decl)); |
16267 | if (maybe_tmpl != decl) |
16268 | /* We're in the template parm binding level. |
16269 | Pushtag has logic to slide under that, but we're |
16270 | not pushing a *new* type. */ |
16271 | push_nested_namespace (CP_DECL_CONTEXT (decl)); |
16272 | |
16273 | pushdecl (maybe_tmpl); |
16274 | if (maybe_tmpl != decl) |
16275 | pop_nested_namespace (CP_DECL_CONTEXT (decl)); |
16276 | } |
16277 | |
16278 | set_instantiating_module (maybe_tmpl); |
16279 | } |
16280 | } |
16281 | |
16282 | return t; |
16283 | } |
16284 | |
16285 | /* Create the binfo hierarchy for REF with (possibly NULL) base list |
16286 | BASE_LIST. For each element on BASE_LIST the TREE_PURPOSE is an |
16287 | access_* node, and the TREE_VALUE is the type of the base-class. |
16288 | Non-NULL TREE_TYPE indicates virtual inheritance. */ |
16289 | |
16290 | void |
16291 | xref_basetypes (tree ref, tree base_list) |
16292 | { |
16293 | tree *basep; |
16294 | tree binfo, base_binfo; |
16295 | unsigned max_vbases = 0; /* Maximum direct & indirect virtual bases. */ |
16296 | unsigned max_bases = 0; /* Maximum direct bases. */ |
16297 | unsigned max_dvbases = 0; /* Maximum direct virtual bases. */ |
16298 | int i; |
16299 | tree default_access; |
16300 | tree igo_prev; /* Track Inheritance Graph Order. */ |
16301 | |
16302 | if (ref == error_mark_node) |
16303 | return; |
16304 | |
16305 | /* The base of a derived class is private by default, all others are |
16306 | public. */ |
16307 | default_access = (TREE_CODE (ref) == RECORD_TYPE |
16308 | && CLASSTYPE_DECLARED_CLASS (ref) |
16309 | ? access_private_node : access_public_node); |
16310 | |
16311 | /* First, make sure that any templates in base-classes are |
16312 | instantiated. This ensures that if we call ourselves recursively |
16313 | we do not get confused about which classes are marked and which |
16314 | are not. */ |
16315 | basep = &base_list; |
16316 | while (*basep) |
16317 | { |
16318 | tree basetype = TREE_VALUE (*basep); |
16319 | |
16320 | /* The dependent_type_p call below should really be dependent_scope_p |
16321 | so that we give a hard error about using an incomplete type as a |
16322 | base, but we allow it with a pedwarn for backward |
16323 | compatibility. */ |
16324 | if (processing_template_decl |
16325 | && CLASS_TYPE_P (basetype) && TYPE_BEING_DEFINED (basetype)) |
16326 | cxx_incomplete_type_diagnostic (NULL_TREE, type: basetype, diag_kind: DK_PEDWARN); |
16327 | if (!dependent_type_p (basetype) |
16328 | && !complete_type_or_else (basetype, NULL)) |
16329 | /* An incomplete type. Remove it from the list. */ |
16330 | *basep = TREE_CHAIN (*basep); |
16331 | else |
16332 | { |
16333 | max_bases++; |
16334 | if (TREE_TYPE (*basep)) |
16335 | max_dvbases++; |
16336 | if (CLASS_TYPE_P (basetype)) |
16337 | max_vbases += vec_safe_length (CLASSTYPE_VBASECLASSES (basetype)); |
16338 | basep = &TREE_CHAIN (*basep); |
16339 | } |
16340 | } |
16341 | max_vbases += max_dvbases; |
16342 | |
16343 | TYPE_MARKED_P (ref) = 1; |
16344 | |
16345 | /* The binfo slot should be empty, unless this is an (ill-formed) |
16346 | redefinition. */ |
16347 | gcc_assert (!TYPE_BINFO (ref) || TYPE_SIZE (ref)); |
16348 | |
16349 | gcc_assert (TYPE_MAIN_VARIANT (ref) == ref); |
16350 | |
16351 | binfo = make_tree_binfo (max_bases); |
16352 | |
16353 | TYPE_BINFO (ref) = binfo; |
16354 | BINFO_OFFSET (binfo) = size_zero_node; |
16355 | BINFO_TYPE (binfo) = ref; |
16356 | |
16357 | /* Apply base-class info set up to the variants of this type. */ |
16358 | fixup_type_variants (ref); |
16359 | |
16360 | if (max_bases) |
16361 | { |
16362 | vec_alloc (BINFO_BASE_ACCESSES (binfo), nelems: max_bases); |
16363 | /* A C++98 POD cannot have base classes. */ |
16364 | CLASSTYPE_NON_LAYOUT_POD_P (ref) = true; |
16365 | |
16366 | if (TREE_CODE (ref) == UNION_TYPE) |
16367 | { |
16368 | error ("derived union %qT invalid" , ref); |
16369 | return; |
16370 | } |
16371 | } |
16372 | |
16373 | if (max_bases > 1) |
16374 | warning (OPT_Wmultiple_inheritance, |
16375 | "%qT defined with multiple direct bases" , ref); |
16376 | |
16377 | if (max_vbases) |
16378 | { |
16379 | /* An aggregate can't have virtual base classes. */ |
16380 | CLASSTYPE_NON_AGGREGATE (ref) = true; |
16381 | |
16382 | vec_alloc (CLASSTYPE_VBASECLASSES (ref), nelems: max_vbases); |
16383 | |
16384 | if (max_dvbases) |
16385 | warning (OPT_Wvirtual_inheritance, |
16386 | "%qT defined with direct virtual base" , ref); |
16387 | } |
16388 | |
16389 | for (igo_prev = binfo; base_list; base_list = TREE_CHAIN (base_list)) |
16390 | { |
16391 | tree access = TREE_PURPOSE (base_list); |
16392 | int via_virtual = TREE_TYPE (base_list) != NULL_TREE; |
16393 | tree basetype = TREE_VALUE (base_list); |
16394 | |
16395 | if (access == access_default_node) |
16396 | access = default_access; |
16397 | |
16398 | /* Before C++17, an aggregate cannot have base classes. In C++17, an |
16399 | aggregate can't have virtual, private, or protected base classes. */ |
16400 | if (cxx_dialect < cxx17 |
16401 | || access != access_public_node |
16402 | || via_virtual) |
16403 | CLASSTYPE_NON_AGGREGATE (ref) = true; |
16404 | |
16405 | if (PACK_EXPANSION_P (basetype)) |
16406 | basetype = PACK_EXPANSION_PATTERN (basetype); |
16407 | if (TREE_CODE (basetype) == TYPE_DECL) |
16408 | basetype = TREE_TYPE (basetype); |
16409 | if (!MAYBE_CLASS_TYPE_P (basetype) || TREE_CODE (basetype) == UNION_TYPE) |
16410 | { |
16411 | error ("base type %qT fails to be a struct or class type" , |
16412 | basetype); |
16413 | goto dropped_base; |
16414 | } |
16415 | |
16416 | base_binfo = NULL_TREE; |
16417 | if (CLASS_TYPE_P (basetype) && !dependent_scope_p (basetype)) |
16418 | { |
16419 | base_binfo = TYPE_BINFO (basetype); |
16420 | /* The original basetype could have been a typedef'd type. */ |
16421 | basetype = BINFO_TYPE (base_binfo); |
16422 | |
16423 | /* Inherit flags from the base. */ |
16424 | TYPE_HAS_NEW_OPERATOR (ref) |
16425 | |= TYPE_HAS_NEW_OPERATOR (basetype); |
16426 | TYPE_HAS_ARRAY_NEW_OPERATOR (ref) |
16427 | |= TYPE_HAS_ARRAY_NEW_OPERATOR (basetype); |
16428 | TYPE_GETS_DELETE (ref) |= TYPE_GETS_DELETE (basetype); |
16429 | TYPE_HAS_CONVERSION (ref) |= TYPE_HAS_CONVERSION (basetype); |
16430 | CLASSTYPE_DIAMOND_SHAPED_P (ref) |
16431 | |= CLASSTYPE_DIAMOND_SHAPED_P (basetype); |
16432 | CLASSTYPE_REPEATED_BASE_P (ref) |
16433 | |= CLASSTYPE_REPEATED_BASE_P (basetype); |
16434 | } |
16435 | |
16436 | /* We must do this test after we've seen through a typedef |
16437 | type. */ |
16438 | if (TYPE_MARKED_P (basetype)) |
16439 | { |
16440 | if (basetype == ref) |
16441 | error ("recursive type %qT undefined" , basetype); |
16442 | else |
16443 | error ("duplicate base type %qT invalid" , basetype); |
16444 | goto dropped_base; |
16445 | } |
16446 | |
16447 | if (PACK_EXPANSION_P (TREE_VALUE (base_list))) |
16448 | /* Regenerate the pack expansion for the bases. */ |
16449 | basetype = make_pack_expansion (basetype); |
16450 | |
16451 | TYPE_MARKED_P (basetype) = 1; |
16452 | |
16453 | base_binfo = copy_binfo (base_binfo, basetype, ref, |
16454 | &igo_prev, via_virtual); |
16455 | if (!BINFO_INHERITANCE_CHAIN (base_binfo)) |
16456 | BINFO_INHERITANCE_CHAIN (base_binfo) = binfo; |
16457 | |
16458 | BINFO_BASE_APPEND (binfo, base_binfo); |
16459 | BINFO_BASE_ACCESS_APPEND (binfo, access); |
16460 | continue; |
16461 | |
16462 | dropped_base: |
16463 | /* Update max_vbases to reflect the reality that we are dropping |
16464 | this base: if it reaches zero we want to undo the vec_alloc |
16465 | above to avoid inconsistencies during error-recovery: eg, in |
16466 | build_special_member_call, CLASSTYPE_VBASECLASSES non null |
16467 | and vtt null (c++/27952). */ |
16468 | if (via_virtual) |
16469 | max_vbases--; |
16470 | if (CLASS_TYPE_P (basetype)) |
16471 | max_vbases |
16472 | -= vec_safe_length (CLASSTYPE_VBASECLASSES (basetype)); |
16473 | } |
16474 | |
16475 | if (CLASSTYPE_VBASECLASSES (ref) |
16476 | && max_vbases == 0) |
16477 | vec_free (CLASSTYPE_VBASECLASSES (ref)); |
16478 | |
16479 | if (vec_safe_length (CLASSTYPE_VBASECLASSES (ref)) < max_vbases) |
16480 | /* If we didn't get max_vbases vbases, we must have shared at |
16481 | least one of them, and are therefore diamond shaped. */ |
16482 | CLASSTYPE_DIAMOND_SHAPED_P (ref) = 1; |
16483 | |
16484 | /* Unmark all the types. */ |
16485 | for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++) |
16486 | TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0; |
16487 | TYPE_MARKED_P (ref) = 0; |
16488 | |
16489 | /* Now see if we have a repeated base type. */ |
16490 | if (!CLASSTYPE_REPEATED_BASE_P (ref)) |
16491 | { |
16492 | for (base_binfo = binfo; base_binfo; |
16493 | base_binfo = TREE_CHAIN (base_binfo)) |
16494 | { |
16495 | if (TYPE_MARKED_P (BINFO_TYPE (base_binfo))) |
16496 | { |
16497 | CLASSTYPE_REPEATED_BASE_P (ref) = 1; |
16498 | break; |
16499 | } |
16500 | TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 1; |
16501 | } |
16502 | for (base_binfo = binfo; base_binfo; |
16503 | base_binfo = TREE_CHAIN (base_binfo)) |
16504 | if (TYPE_MARKED_P (BINFO_TYPE (base_binfo))) |
16505 | TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0; |
16506 | else |
16507 | break; |
16508 | } |
16509 | } |
16510 | |
16511 | |
16512 | /* Copies the enum-related properties from type SRC to type DST. |
16513 | Used with the underlying type of an enum and the enum itself. */ |
16514 | static void |
16515 | copy_type_enum (tree dst, tree src) |
16516 | { |
16517 | tree t; |
16518 | for (t = dst; t; t = TYPE_NEXT_VARIANT (t)) |
16519 | { |
16520 | TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (src); |
16521 | TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (src); |
16522 | TYPE_SIZE (t) = TYPE_SIZE (src); |
16523 | TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (src); |
16524 | SET_TYPE_MODE (dst, TYPE_MODE (src)); |
16525 | TYPE_PRECISION (t) = TYPE_PRECISION (src); |
16526 | unsigned valign = TYPE_ALIGN (src); |
16527 | if (TYPE_USER_ALIGN (t)) |
16528 | valign = MAX (valign, TYPE_ALIGN (t)); |
16529 | else |
16530 | TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (src); |
16531 | SET_TYPE_ALIGN (t, valign); |
16532 | TYPE_UNSIGNED (t) = TYPE_UNSIGNED (src); |
16533 | } |
16534 | } |
16535 | |
16536 | /* Begin compiling the definition of an enumeration type. |
16537 | NAME is its name, |
16538 | |
16539 | if ENUMTYPE is not NULL_TREE then the type has alredy been found. |
16540 | |
16541 | UNDERLYING_TYPE is the type that will be used as the storage for |
16542 | the enumeration type. This should be NULL_TREE if no storage type |
16543 | was specified. |
16544 | |
16545 | ATTRIBUTES are any attributes specified after the enum-key. |
16546 | |
16547 | SCOPED_ENUM_P is true if this is a scoped enumeration type. |
16548 | |
16549 | if IS_NEW is not NULL, gets TRUE iff a new type is created. |
16550 | |
16551 | Returns the type object, as yet incomplete. |
16552 | Also records info about it so that build_enumerator |
16553 | may be used to declare the individual values as they are read. */ |
16554 | |
16555 | tree |
16556 | start_enum (tree name, tree enumtype, tree underlying_type, |
16557 | tree attributes, bool scoped_enum_p, bool *is_new) |
16558 | { |
16559 | tree prevtype = NULL_TREE; |
16560 | gcc_assert (identifier_p (name)); |
16561 | |
16562 | if (is_new) |
16563 | *is_new = false; |
16564 | /* [C++0x dcl.enum]p5: |
16565 | |
16566 | If not explicitly specified, the underlying type of a scoped |
16567 | enumeration type is int. */ |
16568 | if (!underlying_type && scoped_enum_p) |
16569 | underlying_type = integer_type_node; |
16570 | |
16571 | if (underlying_type) |
16572 | underlying_type = cv_unqualified (underlying_type); |
16573 | |
16574 | /* If this is the real definition for a previous forward reference, |
16575 | fill in the contents in the same object that used to be the |
16576 | forward reference. */ |
16577 | if (!enumtype) |
16578 | enumtype = lookup_and_check_tag (tag_code: enum_type, name, |
16579 | /*tag_scope=*/how: TAG_how::CURRENT_ONLY, |
16580 | /*template_header_p=*/false); |
16581 | |
16582 | /* In case of a template_decl, the only check that should be deferred |
16583 | to instantiation time is the comparison of underlying types. */ |
16584 | if (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE) |
16585 | { |
16586 | if (scoped_enum_p != SCOPED_ENUM_P (enumtype)) |
16587 | { |
16588 | error_at (input_location, "scoped/unscoped mismatch " |
16589 | "in enum %q#T" , enumtype); |
16590 | inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)), |
16591 | "previous definition here" ); |
16592 | enumtype = error_mark_node; |
16593 | } |
16594 | else if (ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) != !! underlying_type) |
16595 | { |
16596 | error_at (input_location, "underlying type mismatch " |
16597 | "in enum %q#T" , enumtype); |
16598 | inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)), |
16599 | "previous definition here" ); |
16600 | enumtype = error_mark_node; |
16601 | } |
16602 | else if (underlying_type && ENUM_UNDERLYING_TYPE (enumtype) |
16603 | && !same_type_p (underlying_type, |
16604 | ENUM_UNDERLYING_TYPE (enumtype))) |
16605 | { |
16606 | error_at (input_location, "different underlying type " |
16607 | "in enum %q#T" , enumtype); |
16608 | inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)), |
16609 | "previous definition here" ); |
16610 | underlying_type = NULL_TREE; |
16611 | } |
16612 | |
16613 | if (modules_p ()) |
16614 | { |
16615 | if (!module_may_redeclare (TYPE_NAME (enumtype))) |
16616 | { |
16617 | error ("cannot define %qD in different module" , |
16618 | TYPE_NAME (enumtype)); |
16619 | inform (DECL_SOURCE_LOCATION (TYPE_NAME (enumtype)), |
16620 | "declared here" ); |
16621 | enumtype = error_mark_node; |
16622 | } |
16623 | set_instantiating_module (TYPE_NAME (enumtype)); |
16624 | } |
16625 | } |
16626 | |
16627 | if (!enumtype || TREE_CODE (enumtype) != ENUMERAL_TYPE |
16628 | || processing_template_decl) |
16629 | { |
16630 | /* In case of error, make a dummy enum to allow parsing to |
16631 | continue. */ |
16632 | if (enumtype == error_mark_node) |
16633 | { |
16634 | name = make_anon_name (); |
16635 | enumtype = NULL_TREE; |
16636 | } |
16637 | |
16638 | /* enumtype may be an ENUMERAL_TYPE if this is a redefinition |
16639 | of an opaque enum, or an opaque enum of an already defined |
16640 | enumeration (C++11). |
16641 | In any other case, it'll be NULL_TREE. */ |
16642 | if (!enumtype) |
16643 | { |
16644 | if (is_new) |
16645 | *is_new = true; |
16646 | } |
16647 | prevtype = enumtype; |
16648 | |
16649 | /* Do not push the decl more than once. */ |
16650 | if (!enumtype |
16651 | || TREE_CODE (enumtype) != ENUMERAL_TYPE) |
16652 | { |
16653 | enumtype = cxx_make_type (ENUMERAL_TYPE); |
16654 | enumtype = pushtag (name, enumtype); |
16655 | |
16656 | /* std::byte aliases anything. */ |
16657 | if (enumtype != error_mark_node |
16658 | && TYPE_CONTEXT (enumtype) == std_node |
16659 | && !strcmp (s1: "byte" , TYPE_NAME_STRING (enumtype))) |
16660 | TYPE_ALIAS_SET (enumtype) = 0; |
16661 | } |
16662 | else |
16663 | enumtype = xref_tag (tag_code: enum_type, name); |
16664 | |
16665 | if (enumtype == error_mark_node) |
16666 | return error_mark_node; |
16667 | |
16668 | /* The enum is considered opaque until the opening '{' of the |
16669 | enumerator list. */ |
16670 | SET_OPAQUE_ENUM_P (enumtype, true); |
16671 | ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) = !! underlying_type; |
16672 | } |
16673 | |
16674 | SET_SCOPED_ENUM_P (enumtype, scoped_enum_p); |
16675 | |
16676 | cplus_decl_attributes (&enumtype, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE); |
16677 | |
16678 | if (underlying_type) |
16679 | { |
16680 | if (ENUM_UNDERLYING_TYPE (enumtype)) |
16681 | /* We already checked that it matches, don't change it to a different |
16682 | typedef variant. */; |
16683 | else if (CP_INTEGRAL_TYPE_P (underlying_type)) |
16684 | { |
16685 | copy_type_enum (dst: enumtype, src: underlying_type); |
16686 | ENUM_UNDERLYING_TYPE (enumtype) = underlying_type; |
16687 | } |
16688 | else if (dependent_type_p (underlying_type)) |
16689 | ENUM_UNDERLYING_TYPE (enumtype) = underlying_type; |
16690 | else |
16691 | { |
16692 | error ("underlying type %qT of %qT must be an integral type" , |
16693 | underlying_type, enumtype); |
16694 | ENUM_UNDERLYING_TYPE (enumtype) = integer_type_node; |
16695 | } |
16696 | } |
16697 | |
16698 | /* If into a template class, the returned enum is always the first |
16699 | declaration (opaque or not) seen. This way all the references to |
16700 | this type will be to the same declaration. The following ones are used |
16701 | only to check for definition errors. */ |
16702 | if (prevtype && processing_template_decl) |
16703 | return prevtype; |
16704 | else |
16705 | return enumtype; |
16706 | } |
16707 | |
16708 | /* After processing and defining all the values of an enumeration type, |
16709 | install their decls in the enumeration type. |
16710 | ENUMTYPE is the type object. */ |
16711 | |
16712 | void |
16713 | finish_enum_value_list (tree enumtype) |
16714 | { |
16715 | tree values; |
16716 | tree underlying_type; |
16717 | tree decl; |
16718 | tree value; |
16719 | tree minnode, maxnode; |
16720 | tree t; |
16721 | |
16722 | bool fixed_underlying_type_p |
16723 | = ENUM_UNDERLYING_TYPE (enumtype) != NULL_TREE; |
16724 | |
16725 | /* We built up the VALUES in reverse order. */ |
16726 | TYPE_VALUES (enumtype) = nreverse (TYPE_VALUES (enumtype)); |
16727 | |
16728 | /* For an enum defined in a template, just set the type of the values; |
16729 | all further processing is postponed until the template is |
16730 | instantiated. We need to set the type so that tsubst of a CONST_DECL |
16731 | works. */ |
16732 | if (processing_template_decl) |
16733 | { |
16734 | for (values = TYPE_VALUES (enumtype); |
16735 | values; |
16736 | values = TREE_CHAIN (values)) |
16737 | TREE_TYPE (TREE_VALUE (values)) = enumtype; |
16738 | return; |
16739 | } |
16740 | |
16741 | /* Determine the minimum and maximum values of the enumerators. */ |
16742 | if (TYPE_VALUES (enumtype)) |
16743 | { |
16744 | minnode = maxnode = NULL_TREE; |
16745 | |
16746 | for (values = TYPE_VALUES (enumtype); |
16747 | values; |
16748 | values = TREE_CHAIN (values)) |
16749 | { |
16750 | decl = TREE_VALUE (values); |
16751 | |
16752 | /* [dcl.enum]: Following the closing brace of an enum-specifier, |
16753 | each enumerator has the type of its enumeration. Prior to the |
16754 | closing brace, the type of each enumerator is the type of its |
16755 | initializing value. */ |
16756 | TREE_TYPE (decl) = enumtype; |
16757 | |
16758 | /* Update the minimum and maximum values, if appropriate. */ |
16759 | value = DECL_INITIAL (decl); |
16760 | if (TREE_CODE (value) != INTEGER_CST) |
16761 | value = integer_zero_node; |
16762 | /* Figure out what the minimum and maximum values of the |
16763 | enumerators are. */ |
16764 | if (!minnode) |
16765 | minnode = maxnode = value; |
16766 | else if (tree_int_cst_lt (t1: maxnode, t2: value)) |
16767 | maxnode = value; |
16768 | else if (tree_int_cst_lt (t1: value, t2: minnode)) |
16769 | minnode = value; |
16770 | } |
16771 | } |
16772 | else |
16773 | /* [dcl.enum] |
16774 | |
16775 | If the enumerator-list is empty, the underlying type is as if |
16776 | the enumeration had a single enumerator with value 0. */ |
16777 | minnode = maxnode = integer_zero_node; |
16778 | |
16779 | if (!fixed_underlying_type_p) |
16780 | { |
16781 | /* Compute the number of bits require to represent all values of the |
16782 | enumeration. We must do this before the type of MINNODE and |
16783 | MAXNODE are transformed, since tree_int_cst_min_precision relies |
16784 | on the TREE_TYPE of the value it is passed. */ |
16785 | signop sgn = tree_int_cst_sgn (minnode) >= 0 ? UNSIGNED : SIGNED; |
16786 | int lowprec = tree_int_cst_min_precision (minnode, sgn); |
16787 | int highprec = tree_int_cst_min_precision (maxnode, sgn); |
16788 | int precision = MAX (lowprec, highprec); |
16789 | unsigned int itk; |
16790 | bool use_short_enum; |
16791 | |
16792 | /* Determine the underlying type of the enumeration. |
16793 | |
16794 | [dcl.enum] |
16795 | |
16796 | The underlying type of an enumeration is an integral type that |
16797 | can represent all the enumerator values defined in the |
16798 | enumeration. It is implementation-defined which integral type is |
16799 | used as the underlying type for an enumeration except that the |
16800 | underlying type shall not be larger than int unless the value of |
16801 | an enumerator cannot fit in an int or unsigned int. |
16802 | |
16803 | We use "int" or an "unsigned int" as the underlying type, even if |
16804 | a smaller integral type would work, unless the user has |
16805 | explicitly requested that we use the smallest possible type. The |
16806 | user can request that for all enumerations with a command line |
16807 | flag, or for just one enumeration with an attribute. */ |
16808 | |
16809 | use_short_enum = flag_short_enums |
16810 | || lookup_attribute (attr_name: "packed" , TYPE_ATTRIBUTES (enumtype)); |
16811 | |
16812 | /* If the precision of the type was specified with an attribute and it |
16813 | was too small, give an error. Otherwise, use it. */ |
16814 | if (TYPE_PRECISION (enumtype)) |
16815 | { |
16816 | if (precision > TYPE_PRECISION (enumtype)) |
16817 | error ("specified mode too small for enumerated values" ); |
16818 | else |
16819 | { |
16820 | use_short_enum = true; |
16821 | precision = TYPE_PRECISION (enumtype); |
16822 | } |
16823 | } |
16824 | |
16825 | for (itk = (use_short_enum ? itk_char : itk_int); |
16826 | itk != itk_none; |
16827 | itk++) |
16828 | { |
16829 | underlying_type = integer_types[itk]; |
16830 | if (underlying_type != NULL_TREE |
16831 | && TYPE_PRECISION (underlying_type) >= precision |
16832 | && TYPE_SIGN (underlying_type) == sgn) |
16833 | break; |
16834 | } |
16835 | if (itk == itk_none) |
16836 | { |
16837 | /* DR 377 |
16838 | |
16839 | IF no integral type can represent all the enumerator values, the |
16840 | enumeration is ill-formed. */ |
16841 | error ("no integral type can represent all of the enumerator values " |
16842 | "for %qT" , enumtype); |
16843 | precision = TYPE_PRECISION (long_long_integer_type_node); |
16844 | underlying_type = integer_types[itk_unsigned_long_long]; |
16845 | } |
16846 | |
16847 | /* [dcl.enum] |
16848 | |
16849 | The value of sizeof() applied to an enumeration type, an object |
16850 | of an enumeration type, or an enumerator, is the value of sizeof() |
16851 | applied to the underlying type. */ |
16852 | copy_type_enum (dst: enumtype, src: underlying_type); |
16853 | |
16854 | /* Compute the minimum and maximum values for the type. |
16855 | |
16856 | [dcl.enum] |
16857 | |
16858 | For an enumeration where emin is the smallest enumerator and emax |
16859 | is the largest, the values of the enumeration are the values of the |
16860 | underlying type in the range bmin to bmax, where bmin and bmax are, |
16861 | respectively, the smallest and largest values of the smallest bit- |
16862 | field that can store emin and emax. */ |
16863 | |
16864 | /* The middle-end currently assumes that types with TYPE_PRECISION |
16865 | narrower than their underlying type are suitably zero or sign |
16866 | extended to fill their mode. Similarly, it assumes that the front |
16867 | end assures that a value of a particular type must be within |
16868 | TYPE_MIN_VALUE and TYPE_MAX_VALUE. |
16869 | |
16870 | We used to set these fields based on bmin and bmax, but that led |
16871 | to invalid assumptions like optimizing away bounds checking. So |
16872 | now we just set the TYPE_PRECISION, TYPE_MIN_VALUE, and |
16873 | TYPE_MAX_VALUE to the values for the mode above and only restrict |
16874 | the ENUM_UNDERLYING_TYPE for the benefit of diagnostics. */ |
16875 | ENUM_UNDERLYING_TYPE (enumtype) |
16876 | = build_distinct_type_copy (underlying_type); |
16877 | TYPE_PRECISION (ENUM_UNDERLYING_TYPE (enumtype)) = precision; |
16878 | set_min_and_max_values_for_integral_type |
16879 | (ENUM_UNDERLYING_TYPE (enumtype), precision, sgn); |
16880 | |
16881 | /* If -fstrict-enums, still constrain TYPE_MIN/MAX_VALUE. */ |
16882 | if (flag_strict_enums) |
16883 | set_min_and_max_values_for_integral_type (enumtype, precision, sgn); |
16884 | } |
16885 | else |
16886 | underlying_type = ENUM_UNDERLYING_TYPE (enumtype); |
16887 | |
16888 | /* If the enum is exported, mark the consts too. */ |
16889 | bool export_p = (UNSCOPED_ENUM_P (enumtype) |
16890 | && DECL_MODULE_EXPORT_P (TYPE_STUB_DECL (enumtype)) |
16891 | && at_namespace_scope_p ()); |
16892 | |
16893 | /* Convert each of the enumerators to the type of the underlying |
16894 | type of the enumeration. */ |
16895 | for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values)) |
16896 | { |
16897 | decl = TREE_VALUE (values); |
16898 | iloc_sentinel ils (DECL_SOURCE_LOCATION (decl)); |
16899 | if (fixed_underlying_type_p) |
16900 | /* If the enumeration type has a fixed underlying type, we |
16901 | already checked all of the enumerator values. */ |
16902 | value = DECL_INITIAL (decl); |
16903 | else |
16904 | value = perform_implicit_conversion (underlying_type, |
16905 | DECL_INITIAL (decl), |
16906 | tf_warning_or_error); |
16907 | /* Do not clobber shared ints. */ |
16908 | if (value != error_mark_node) |
16909 | { |
16910 | value = copy_node (value); |
16911 | |
16912 | TREE_TYPE (value) = enumtype; |
16913 | } |
16914 | DECL_INITIAL (decl) = value; |
16915 | if (export_p) |
16916 | DECL_MODULE_EXPORT_P (decl) = true; |
16917 | } |
16918 | |
16919 | /* Fix up all variant types of this enum type. */ |
16920 | for (t = TYPE_MAIN_VARIANT (enumtype); t; t = TYPE_NEXT_VARIANT (t)) |
16921 | TYPE_VALUES (t) = TYPE_VALUES (enumtype); |
16922 | |
16923 | if (at_class_scope_p () |
16924 | && COMPLETE_TYPE_P (current_class_type) |
16925 | && UNSCOPED_ENUM_P (enumtype)) |
16926 | { |
16927 | insert_late_enum_def_bindings (current_class_type, enumtype); |
16928 | /* TYPE_FIELDS needs fixup. */ |
16929 | fixup_type_variants (current_class_type); |
16930 | } |
16931 | |
16932 | /* Finish debugging output for this type. */ |
16933 | rest_of_type_compilation (enumtype, namespace_bindings_p ()); |
16934 | |
16935 | /* Each enumerator now has the type of its enumeration. Clear the cache |
16936 | so that this change in types doesn't confuse us later on. */ |
16937 | clear_cv_and_fold_caches (); |
16938 | } |
16939 | |
16940 | /* Finishes the enum type. This is called only the first time an |
16941 | enumeration is seen, be it opaque or odinary. |
16942 | ENUMTYPE is the type object. */ |
16943 | |
16944 | void |
16945 | finish_enum (tree enumtype) |
16946 | { |
16947 | if (processing_template_decl) |
16948 | { |
16949 | if (at_function_scope_p ()) |
16950 | add_stmt (build_min (TAG_DEFN, enumtype)); |
16951 | return; |
16952 | } |
16953 | |
16954 | /* If this is a forward declaration, there should not be any variants, |
16955 | though we can get a variant in the middle of an enum-specifier with |
16956 | wacky code like 'enum E { e = sizeof(const E*) };' */ |
16957 | gcc_assert (enumtype == TYPE_MAIN_VARIANT (enumtype) |
16958 | && (TYPE_VALUES (enumtype) |
16959 | || !TYPE_NEXT_VARIANT (enumtype))); |
16960 | } |
16961 | |
16962 | /* Build and install a CONST_DECL for an enumeration constant of the |
16963 | enumeration type ENUMTYPE whose NAME and VALUE (if any) are provided. |
16964 | Apply ATTRIBUTES if available. LOC is the location of NAME. |
16965 | Assignment of sequential values by default is handled here. */ |
16966 | |
16967 | tree |
16968 | build_enumerator (tree name, tree value, tree enumtype, tree attributes, |
16969 | location_t loc) |
16970 | { |
16971 | tree decl; |
16972 | tree context; |
16973 | tree type; |
16974 | |
16975 | /* scalar_constant_value will pull out this expression, so make sure |
16976 | it's folded as appropriate. */ |
16977 | if (processing_template_decl) |
16978 | value = fold_non_dependent_expr (value); |
16979 | |
16980 | /* If the VALUE was erroneous, pretend it wasn't there; that will |
16981 | result in the enum being assigned the next value in sequence. */ |
16982 | if (value == error_mark_node) |
16983 | value = NULL_TREE; |
16984 | |
16985 | /* Remove no-op casts from the value. */ |
16986 | if (value) |
16987 | STRIP_TYPE_NOPS (value); |
16988 | |
16989 | if (! processing_template_decl) |
16990 | { |
16991 | /* Validate and default VALUE. */ |
16992 | if (value != NULL_TREE) |
16993 | { |
16994 | if (!ENUM_UNDERLYING_TYPE (enumtype)) |
16995 | { |
16996 | tree tmp_value = build_expr_type_conversion (WANT_INT | WANT_ENUM, |
16997 | value, true); |
16998 | if (tmp_value) |
16999 | value = tmp_value; |
17000 | } |
17001 | else if (! INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P |
17002 | (TREE_TYPE (value))) |
17003 | value = perform_implicit_conversion_flags |
17004 | (ENUM_UNDERLYING_TYPE (enumtype), value, tf_warning_or_error, |
17005 | LOOKUP_IMPLICIT | LOOKUP_NO_NARROWING); |
17006 | |
17007 | if (value == error_mark_node) |
17008 | value = NULL_TREE; |
17009 | |
17010 | if (value != NULL_TREE) |
17011 | { |
17012 | if (! INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P |
17013 | (TREE_TYPE (value))) |
17014 | { |
17015 | error_at (cp_expr_loc_or_input_loc (t: value), |
17016 | "enumerator value for %qD must have integral or " |
17017 | "unscoped enumeration type" , name); |
17018 | value = NULL_TREE; |
17019 | } |
17020 | else |
17021 | { |
17022 | value = cxx_constant_value (value); |
17023 | |
17024 | if (TREE_CODE (value) != INTEGER_CST) |
17025 | { |
17026 | error ("enumerator value for %qD is not an integer " |
17027 | "constant" , name); |
17028 | value = NULL_TREE; |
17029 | } |
17030 | } |
17031 | } |
17032 | } |
17033 | |
17034 | /* Default based on previous value. */ |
17035 | if (value == NULL_TREE) |
17036 | { |
17037 | if (TYPE_VALUES (enumtype)) |
17038 | { |
17039 | tree prev_value; |
17040 | |
17041 | /* C++03 7.2/4: If no initializer is specified for the first |
17042 | enumerator, the type is an unspecified integral |
17043 | type. Otherwise the type is the same as the type of the |
17044 | initializing value of the preceding enumerator unless the |
17045 | incremented value is not representable in that type, in |
17046 | which case the type is an unspecified integral type |
17047 | sufficient to contain the incremented value. */ |
17048 | prev_value = DECL_INITIAL (TREE_VALUE (TYPE_VALUES (enumtype))); |
17049 | if (TREE_CODE (prev_value) != INTEGER_CST) |
17050 | value = error_mark_node; |
17051 | else |
17052 | { |
17053 | wi::overflow_type overflowed; |
17054 | tree type = TREE_TYPE (prev_value); |
17055 | signop sgn = TYPE_SIGN (type); |
17056 | widest_int wi = wi::add (x: wi::to_widest (t: prev_value), y: 1, sgn, |
17057 | overflow: &overflowed); |
17058 | if (!overflowed) |
17059 | { |
17060 | bool pos = !wi::neg_p (x: wi, sgn); |
17061 | if (!wi::fits_to_tree_p (x: wi, type)) |
17062 | { |
17063 | unsigned int itk; |
17064 | for (itk = itk_int; itk != itk_none; itk++) |
17065 | { |
17066 | type = integer_types[itk]; |
17067 | if (type != NULL_TREE |
17068 | && (pos || !TYPE_UNSIGNED (type)) |
17069 | && wi::fits_to_tree_p (x: wi, type)) |
17070 | break; |
17071 | } |
17072 | if (type && cxx_dialect < cxx11 |
17073 | && itk > itk_unsigned_long) |
17074 | pedwarn (input_location, OPT_Wlong_long, |
17075 | pos ? G_("\ |
17076 | incremented enumerator value is too large for %<unsigned long%>" ) : G_("\ |
17077 | incremented enumerator value is too large for %<long%>" )); |
17078 | } |
17079 | if (type == NULL_TREE) |
17080 | overflowed = wi::OVF_UNKNOWN; |
17081 | else |
17082 | value = wide_int_to_tree (type, cst: wi); |
17083 | } |
17084 | |
17085 | if (overflowed) |
17086 | { |
17087 | error ("overflow in enumeration values at %qD" , name); |
17088 | value = error_mark_node; |
17089 | } |
17090 | } |
17091 | } |
17092 | else |
17093 | value = integer_zero_node; |
17094 | } |
17095 | |
17096 | /* Remove no-op casts from the value. */ |
17097 | STRIP_TYPE_NOPS (value); |
17098 | |
17099 | /* If the underlying type of the enum is fixed, check whether |
17100 | the enumerator values fits in the underlying type. If it |
17101 | does not fit, the program is ill-formed [C++0x dcl.enum]. */ |
17102 | if (ENUM_UNDERLYING_TYPE (enumtype) |
17103 | && value |
17104 | && TREE_CODE (value) == INTEGER_CST) |
17105 | { |
17106 | if (!int_fits_type_p (value, ENUM_UNDERLYING_TYPE (enumtype))) |
17107 | error ("enumerator value %qE is outside the range of underlying " |
17108 | "type %qT" , value, ENUM_UNDERLYING_TYPE (enumtype)); |
17109 | |
17110 | /* Convert the value to the appropriate type. */ |
17111 | value = fold_convert (ENUM_UNDERLYING_TYPE (enumtype), value); |
17112 | } |
17113 | } |
17114 | |
17115 | /* C++ associates enums with global, function, or class declarations. */ |
17116 | context = current_scope (); |
17117 | |
17118 | /* Build the actual enumeration constant. Note that the enumeration |
17119 | constants have the underlying type of the enum (if it is fixed) |
17120 | or the type of their initializer (if the underlying type of the |
17121 | enum is not fixed): |
17122 | |
17123 | [ C++0x dcl.enum ] |
17124 | |
17125 | If the underlying type is fixed, the type of each enumerator |
17126 | prior to the closing brace is the underlying type; if the |
17127 | initializing value of an enumerator cannot be represented by |
17128 | the underlying type, the program is ill-formed. If the |
17129 | underlying type is not fixed, the type of each enumerator is |
17130 | the type of its initializing value. |
17131 | |
17132 | If the underlying type is not fixed, it will be computed by |
17133 | finish_enum and we will reset the type of this enumerator. Of |
17134 | course, if we're processing a template, there may be no value. */ |
17135 | type = value ? TREE_TYPE (value) : NULL_TREE; |
17136 | |
17137 | decl = build_decl (loc, CONST_DECL, name, type); |
17138 | |
17139 | DECL_CONTEXT (decl) = enumtype; |
17140 | TREE_CONSTANT (decl) = 1; |
17141 | TREE_READONLY (decl) = 1; |
17142 | DECL_INITIAL (decl) = value; |
17143 | |
17144 | if (attributes) |
17145 | cplus_decl_attributes (&decl, attributes, 0); |
17146 | |
17147 | if (context && context == current_class_type && !SCOPED_ENUM_P (enumtype)) |
17148 | { |
17149 | /* In something like `struct S { enum E { i = 7 }; };' we put `i' |
17150 | on the TYPE_FIELDS list for `S'. (That's so that you can say |
17151 | things like `S::i' later.) */ |
17152 | |
17153 | /* The enumerator may be getting declared outside of its enclosing |
17154 | class, like so: |
17155 | |
17156 | class S { public: enum E : int; }; enum S::E : int { i = 7; }; |
17157 | |
17158 | For which case we need to make sure that the access of `S::i' |
17159 | matches the access of `S::E'. */ |
17160 | auto cas = make_temp_override (current_access_specifier); |
17161 | set_current_access_from_decl (TYPE_NAME (enumtype)); |
17162 | finish_member_declaration (decl); |
17163 | } |
17164 | else |
17165 | pushdecl (decl); |
17166 | |
17167 | /* Add this enumeration constant to the list for this type. */ |
17168 | TYPE_VALUES (enumtype) = tree_cons (name, decl, TYPE_VALUES (enumtype)); |
17169 | |
17170 | return decl; |
17171 | } |
17172 | |
17173 | /* Look for an enumerator with the given NAME within the enumeration |
17174 | type ENUMTYPE. This routine is used primarily for qualified name |
17175 | lookup into an enumerator in C++0x, e.g., |
17176 | |
17177 | enum class Color { Red, Green, Blue }; |
17178 | |
17179 | Color color = Color::Red; |
17180 | |
17181 | Returns the value corresponding to the enumerator, or |
17182 | NULL_TREE if no such enumerator was found. */ |
17183 | tree |
17184 | lookup_enumerator (tree enumtype, tree name) |
17185 | { |
17186 | tree e; |
17187 | gcc_assert (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE); |
17188 | |
17189 | e = purpose_member (name, TYPE_VALUES (enumtype)); |
17190 | return e? TREE_VALUE (e) : NULL_TREE; |
17191 | } |
17192 | |
17193 | /* Implement LANG_HOOKS_SIMULATE_ENUM_DECL. */ |
17194 | |
17195 | tree |
17196 | cxx_simulate_enum_decl (location_t loc, const char *name, |
17197 | vec<string_int_pair> *values) |
17198 | { |
17199 | location_t saved_loc = input_location; |
17200 | input_location = loc; |
17201 | |
17202 | tree enumtype = start_enum (get_identifier (name), NULL_TREE, NULL_TREE, |
17203 | NULL_TREE, scoped_enum_p: false, NULL); |
17204 | if (!OPAQUE_ENUM_P (enumtype)) |
17205 | { |
17206 | error_at (loc, "multiple definition of %q#T" , enumtype); |
17207 | inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)), |
17208 | "previous definition here" ); |
17209 | return enumtype; |
17210 | } |
17211 | SET_OPAQUE_ENUM_P (enumtype, false); |
17212 | DECL_SOURCE_LOCATION (TYPE_NAME (enumtype)) = loc; |
17213 | |
17214 | for (const string_int_pair &value : values) |
17215 | build_enumerator (get_identifier (value.first), |
17216 | value: build_int_cst (integer_type_node, value.second), |
17217 | enumtype, NULL_TREE, loc); |
17218 | |
17219 | finish_enum_value_list (enumtype); |
17220 | finish_enum (enumtype); |
17221 | |
17222 | input_location = saved_loc; |
17223 | return enumtype; |
17224 | } |
17225 | |
17226 | /* Implement LANG_HOOKS_SIMULATE_RECORD_DECL. */ |
17227 | |
17228 | tree |
17229 | cxx_simulate_record_decl (location_t loc, const char *name, |
17230 | array_slice<const tree> fields) |
17231 | { |
17232 | iloc_sentinel ils (loc); |
17233 | |
17234 | tree ident = get_identifier (name); |
17235 | tree type = xref_tag (/*tag_code=*/record_type, name: ident); |
17236 | if (type != error_mark_node |
17237 | && (TREE_CODE (type) != RECORD_TYPE || COMPLETE_TYPE_P (type))) |
17238 | { |
17239 | error ("redefinition of %q#T" , type); |
17240 | type = error_mark_node; |
17241 | } |
17242 | if (type == error_mark_node) |
17243 | return lhd_simulate_record_decl (loc, name, fields); |
17244 | |
17245 | xref_basetypes (ref: type, NULL_TREE); |
17246 | type = begin_class_definition (type); |
17247 | if (type == error_mark_node) |
17248 | return lhd_simulate_record_decl (loc, name, fields); |
17249 | |
17250 | for (tree field : fields) |
17251 | finish_member_declaration (field); |
17252 | |
17253 | type = finish_struct (type, NULL_TREE); |
17254 | |
17255 | tree decl = build_decl (loc, TYPE_DECL, ident, type); |
17256 | set_underlying_type (decl); |
17257 | lang_hooks.decls.pushdecl (decl); |
17258 | |
17259 | return type; |
17260 | } |
17261 | |
17262 | /* We're defining DECL. Make sure that its type is OK. */ |
17263 | |
17264 | static void |
17265 | check_function_type (tree decl, tree current_function_parms) |
17266 | { |
17267 | tree fntype = TREE_TYPE (decl); |
17268 | tree return_type = complete_type (TREE_TYPE (fntype)); |
17269 | |
17270 | /* In a function definition, arg types must be complete. */ |
17271 | require_complete_types_for_parms (parms: current_function_parms); |
17272 | |
17273 | if (dependent_type_p (return_type) |
17274 | || type_uses_auto (return_type)) |
17275 | return; |
17276 | if (!COMPLETE_OR_VOID_TYPE_P (return_type)) |
17277 | { |
17278 | tree args = TYPE_ARG_TYPES (fntype); |
17279 | |
17280 | error ("return type %q#T is incomplete" , return_type); |
17281 | |
17282 | /* Make it return void instead. */ |
17283 | if (TREE_CODE (fntype) == METHOD_TYPE) |
17284 | fntype = build_method_type_directly (TREE_TYPE (TREE_VALUE (args)), |
17285 | void_type_node, |
17286 | TREE_CHAIN (args)); |
17287 | else |
17288 | fntype = build_function_type (void_type_node, args); |
17289 | fntype = (cp_build_type_attribute_variant |
17290 | (fntype, TYPE_ATTRIBUTES (TREE_TYPE (decl)))); |
17291 | fntype = cxx_copy_lang_qualifiers (fntype, TREE_TYPE (decl)); |
17292 | TREE_TYPE (decl) = fntype; |
17293 | } |
17294 | else |
17295 | { |
17296 | abstract_virtuals_error (decl, TREE_TYPE (fntype)); |
17297 | maybe_warn_parm_abi (TREE_TYPE (fntype), |
17298 | DECL_SOURCE_LOCATION (decl)); |
17299 | } |
17300 | } |
17301 | |
17302 | /* True iff FN is an implicitly-defined default constructor. */ |
17303 | |
17304 | static bool |
17305 | implicit_default_ctor_p (tree fn) |
17306 | { |
17307 | return (DECL_CONSTRUCTOR_P (fn) |
17308 | && !user_provided_p (fn) |
17309 | && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn))); |
17310 | } |
17311 | |
17312 | /* Clobber the contents of *this to let the back end know that the object |
17313 | storage is dead when we enter the constructor or leave the destructor. */ |
17314 | |
17315 | static tree |
17316 | build_clobber_this () |
17317 | { |
17318 | /* Clobbering an empty base is pointless, and harmful if its one byte |
17319 | TYPE_SIZE overlays real data. */ |
17320 | if (is_empty_class (current_class_type)) |
17321 | return void_node; |
17322 | |
17323 | /* If we have virtual bases, clobber the whole object, but only if we're in |
17324 | charge. If we don't have virtual bases, clobber the as-base type so we |
17325 | don't mess with tail padding. */ |
17326 | bool vbases = CLASSTYPE_VBASECLASSES (current_class_type); |
17327 | |
17328 | tree ctype = current_class_type; |
17329 | if (!vbases) |
17330 | ctype = CLASSTYPE_AS_BASE (ctype); |
17331 | |
17332 | tree clobber = build_clobber (ctype); |
17333 | |
17334 | tree thisref = current_class_ref; |
17335 | if (ctype != current_class_type) |
17336 | { |
17337 | thisref = build_nop (build_reference_type (ctype), current_class_ptr); |
17338 | thisref = convert_from_reference (thisref); |
17339 | } |
17340 | |
17341 | tree exprstmt = build2 (MODIFY_EXPR, void_type_node, thisref, clobber); |
17342 | if (vbases) |
17343 | exprstmt = build_if_in_charge (true_stmt: exprstmt); |
17344 | |
17345 | return exprstmt; |
17346 | } |
17347 | |
17348 | /* Create the FUNCTION_DECL for a function definition. |
17349 | DECLSPECS and DECLARATOR are the parts of the declaration; |
17350 | they describe the function's name and the type it returns, |
17351 | but twisted together in a fashion that parallels the syntax of C. |
17352 | |
17353 | FLAGS is a bitwise or of SF_PRE_PARSED (indicating that the |
17354 | DECLARATOR is really the DECL for the function we are about to |
17355 | process and that DECLSPECS should be ignored), SF_INCLASS_INLINE |
17356 | indicating that the function is an inline defined in-class. |
17357 | |
17358 | This function creates a binding context for the function body |
17359 | as well as setting up the FUNCTION_DECL in current_function_decl. |
17360 | |
17361 | For C++, we must first check whether that datum makes any sense. |
17362 | For example, "class A local_a(1,2);" means that variable local_a |
17363 | is an aggregate of type A, which should have a constructor |
17364 | applied to it with the argument list [1, 2]. |
17365 | |
17366 | On entry, DECL_INITIAL (decl1) should be NULL_TREE or error_mark_node, |
17367 | or may be a BLOCK if the function has been defined previously |
17368 | in this translation unit. On exit, DECL_INITIAL (decl1) will be |
17369 | error_mark_node if the function has never been defined, or |
17370 | a BLOCK if the function has been defined somewhere. */ |
17371 | |
17372 | bool |
17373 | start_preparsed_function (tree decl1, tree attrs, int flags) |
17374 | { |
17375 | tree ctype = NULL_TREE; |
17376 | bool doing_friend = false; |
17377 | |
17378 | /* Sanity check. */ |
17379 | gcc_assert (VOID_TYPE_P (TREE_VALUE (void_list_node))); |
17380 | gcc_assert (TREE_CHAIN (void_list_node) == NULL_TREE); |
17381 | |
17382 | tree fntype = TREE_TYPE (decl1); |
17383 | if (TREE_CODE (fntype) == METHOD_TYPE) |
17384 | ctype = TYPE_METHOD_BASETYPE (fntype); |
17385 | else |
17386 | { |
17387 | ctype = DECL_FRIEND_CONTEXT (decl1); |
17388 | |
17389 | if (ctype) |
17390 | doing_friend = true; |
17391 | } |
17392 | |
17393 | if (DECL_DECLARED_INLINE_P (decl1) |
17394 | && lookup_attribute (attr_name: "noinline" , list: attrs)) |
17395 | warning_at (DECL_SOURCE_LOCATION (decl1), 0, |
17396 | "inline function %qD given attribute %qs" , decl1, "noinline" ); |
17397 | |
17398 | /* Handle gnu_inline attribute. */ |
17399 | if (GNU_INLINE_P (decl1)) |
17400 | { |
17401 | DECL_EXTERNAL (decl1) = 1; |
17402 | DECL_NOT_REALLY_EXTERN (decl1) = 0; |
17403 | DECL_INTERFACE_KNOWN (decl1) = 1; |
17404 | DECL_DISREGARD_INLINE_LIMITS (decl1) = 1; |
17405 | } |
17406 | |
17407 | if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl1)) |
17408 | /* This is a constructor, we must ensure that any default args |
17409 | introduced by this definition are propagated to the clones |
17410 | now. The clones are used directly in overload resolution. */ |
17411 | adjust_clone_args (decl1); |
17412 | |
17413 | /* Sometimes we don't notice that a function is a static member, and |
17414 | build a METHOD_TYPE for it. Fix that up now. */ |
17415 | gcc_assert (!(ctype != NULL_TREE && DECL_STATIC_FUNCTION_P (decl1) |
17416 | && TREE_CODE (TREE_TYPE (decl1)) == METHOD_TYPE)); |
17417 | |
17418 | /* Set up current_class_type, and enter the scope of the class, if |
17419 | appropriate. */ |
17420 | if (ctype) |
17421 | push_nested_class (ctype); |
17422 | else if (DECL_STATIC_FUNCTION_P (decl1)) |
17423 | push_nested_class (DECL_CONTEXT (decl1)); |
17424 | |
17425 | /* Now that we have entered the scope of the class, we must restore |
17426 | the bindings for any template parameters surrounding DECL1, if it |
17427 | is an inline member template. (Order is important; consider the |
17428 | case where a template parameter has the same name as a field of |
17429 | the class.) It is not until after this point that |
17430 | PROCESSING_TEMPLATE_DECL is guaranteed to be set up correctly. */ |
17431 | if (flags & SF_INCLASS_INLINE) |
17432 | maybe_begin_member_template_processing (decl1); |
17433 | |
17434 | /* Effective C++ rule 15. */ |
17435 | if (warn_ecpp |
17436 | && DECL_ASSIGNMENT_OPERATOR_P (decl1) |
17437 | && DECL_OVERLOADED_OPERATOR_IS (decl1, NOP_EXPR) |
17438 | && VOID_TYPE_P (TREE_TYPE (fntype))) |
17439 | warning (OPT_Weffc__, |
17440 | "%<operator=%> should return a reference to %<*this%>" ); |
17441 | |
17442 | /* Make the init_value nonzero so pushdecl knows this is not tentative. |
17443 | error_mark_node is replaced below (in poplevel) with the BLOCK. */ |
17444 | if (!DECL_INITIAL (decl1)) |
17445 | DECL_INITIAL (decl1) = error_mark_node; |
17446 | |
17447 | /* This function exists in static storage. |
17448 | (This does not mean `static' in the C sense!) */ |
17449 | TREE_STATIC (decl1) = 1; |
17450 | |
17451 | /* We must call push_template_decl after current_class_type is set |
17452 | up. (If we are processing inline definitions after exiting a |
17453 | class scope, current_class_type will be NULL_TREE until set above |
17454 | by push_nested_class.) */ |
17455 | if (processing_template_decl) |
17456 | { |
17457 | tree newdecl1 = push_template_decl (decl1, is_friend: doing_friend); |
17458 | if (newdecl1 == error_mark_node) |
17459 | { |
17460 | if (ctype || DECL_STATIC_FUNCTION_P (decl1)) |
17461 | pop_nested_class (); |
17462 | return false; |
17463 | } |
17464 | decl1 = newdecl1; |
17465 | } |
17466 | |
17467 | /* Make sure the parameter and return types are reasonable. When |
17468 | you declare a function, these types can be incomplete, but they |
17469 | must be complete when you define the function. */ |
17470 | check_function_type (decl: decl1, DECL_ARGUMENTS (decl1)); |
17471 | |
17472 | /* Build the return declaration for the function. */ |
17473 | tree restype = TREE_TYPE (fntype); |
17474 | |
17475 | if (DECL_RESULT (decl1) == NULL_TREE) |
17476 | { |
17477 | /* In a template instantiation, copy the return type location. When |
17478 | parsing, the location will be set in grokdeclarator. */ |
17479 | location_t loc = input_location; |
17480 | if (DECL_TEMPLATE_INSTANTIATION (decl1)) |
17481 | { |
17482 | tree tmpl = template_for_substitution (decl1); |
17483 | if (tree res = DECL_RESULT (DECL_TEMPLATE_RESULT (tmpl))) |
17484 | loc = DECL_SOURCE_LOCATION (res); |
17485 | } |
17486 | |
17487 | tree resdecl = build_decl (loc, RESULT_DECL, 0, restype); |
17488 | DECL_ARTIFICIAL (resdecl) = 1; |
17489 | DECL_IGNORED_P (resdecl) = 1; |
17490 | DECL_RESULT (decl1) = resdecl; |
17491 | |
17492 | cp_apply_type_quals_to_decl (cp_type_quals (restype), resdecl); |
17493 | } |
17494 | |
17495 | /* Record the decl so that the function name is defined. |
17496 | If we already have a decl for this name, and it is a FUNCTION_DECL, |
17497 | use the old decl. */ |
17498 | if (!processing_template_decl && !(flags & SF_PRE_PARSED)) |
17499 | { |
17500 | /* A specialization is not used to guide overload resolution. */ |
17501 | if (!DECL_FUNCTION_MEMBER_P (decl1) |
17502 | && !(DECL_USE_TEMPLATE (decl1) && |
17503 | PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl1)))) |
17504 | { |
17505 | tree olddecl = pushdecl (decl1); |
17506 | |
17507 | if (olddecl == error_mark_node) |
17508 | /* If something went wrong when registering the declaration, |
17509 | use DECL1; we have to have a FUNCTION_DECL to use when |
17510 | parsing the body of the function. */ |
17511 | ; |
17512 | else |
17513 | { |
17514 | /* Otherwise, OLDDECL is either a previous declaration |
17515 | of the same function or DECL1 itself. */ |
17516 | |
17517 | if (warn_missing_declarations |
17518 | && olddecl == decl1 |
17519 | && !DECL_MAIN_P (decl1) |
17520 | && TREE_PUBLIC (decl1) |
17521 | && !DECL_DECLARED_INLINE_P (decl1)) |
17522 | { |
17523 | tree context; |
17524 | |
17525 | /* Check whether DECL1 is in an anonymous |
17526 | namespace. */ |
17527 | for (context = DECL_CONTEXT (decl1); |
17528 | context; |
17529 | context = DECL_CONTEXT (context)) |
17530 | { |
17531 | if (TREE_CODE (context) == NAMESPACE_DECL |
17532 | && DECL_NAME (context) == NULL_TREE) |
17533 | break; |
17534 | } |
17535 | |
17536 | if (context == NULL) |
17537 | warning_at (DECL_SOURCE_LOCATION (decl1), |
17538 | OPT_Wmissing_declarations, |
17539 | "no previous declaration for %qD" , decl1); |
17540 | } |
17541 | |
17542 | decl1 = olddecl; |
17543 | } |
17544 | } |
17545 | else |
17546 | { |
17547 | /* We need to set the DECL_CONTEXT. */ |
17548 | if (!DECL_CONTEXT (decl1) && DECL_TEMPLATE_INFO (decl1)) |
17549 | DECL_CONTEXT (decl1) = DECL_CONTEXT (DECL_TI_TEMPLATE (decl1)); |
17550 | } |
17551 | fntype = TREE_TYPE (decl1); |
17552 | restype = TREE_TYPE (fntype); |
17553 | |
17554 | /* If #pragma weak applies, mark the decl appropriately now. |
17555 | The pragma only applies to global functions. Because |
17556 | determining whether or not the #pragma applies involves |
17557 | computing the mangled name for the declaration, we cannot |
17558 | apply the pragma until after we have merged this declaration |
17559 | with any previous declarations; if the original declaration |
17560 | has a linkage specification, that specification applies to |
17561 | the definition as well, and may affect the mangled name. */ |
17562 | if (DECL_FILE_SCOPE_P (decl1)) |
17563 | maybe_apply_pragma_weak (decl1); |
17564 | } |
17565 | |
17566 | /* We are now in the scope of the function being defined. */ |
17567 | current_function_decl = decl1; |
17568 | |
17569 | /* Save the parm names or decls from this function's declarator |
17570 | where store_parm_decls will find them. */ |
17571 | tree current_function_parms = DECL_ARGUMENTS (decl1); |
17572 | |
17573 | /* Let the user know we're compiling this function. */ |
17574 | announce_function (decl1); |
17575 | |
17576 | gcc_assert (DECL_INITIAL (decl1)); |
17577 | |
17578 | /* This function may already have been parsed, in which case just |
17579 | return; our caller will skip over the body without parsing. */ |
17580 | if (DECL_INITIAL (decl1) != error_mark_node) |
17581 | return true; |
17582 | |
17583 | /* Initialize RTL machinery. We cannot do this until |
17584 | CURRENT_FUNCTION_DECL and DECL_RESULT are set up. We do this |
17585 | even when processing a template; this is how we get |
17586 | CFUN set up, and our per-function variables initialized. |
17587 | FIXME factor out the non-RTL stuff. */ |
17588 | cp_binding_level *bl = current_binding_level; |
17589 | allocate_struct_function (decl1, processing_template_decl); |
17590 | |
17591 | /* Initialize the language data structures. Whenever we start |
17592 | a new function, we destroy temporaries in the usual way. */ |
17593 | cfun->language = ggc_cleared_alloc<language_function> (); |
17594 | current_stmt_tree ()->stmts_are_full_exprs_p = 1; |
17595 | current_binding_level = bl; |
17596 | |
17597 | /* If we are (erroneously) defining a function that we have already |
17598 | defined before, wipe out what we knew before. */ |
17599 | gcc_checking_assert (!DECL_PENDING_INLINE_P (decl1)); |
17600 | FNDECL_USED_AUTO (decl1) = false; |
17601 | DECL_SAVED_AUTO_RETURN_TYPE (decl1) = NULL; |
17602 | |
17603 | if (!processing_template_decl && type_uses_auto (restype)) |
17604 | { |
17605 | FNDECL_USED_AUTO (decl1) = true; |
17606 | DECL_SAVED_AUTO_RETURN_TYPE (decl1) = restype; |
17607 | } |
17608 | |
17609 | /* Start the statement-tree, start the tree now. */ |
17610 | DECL_SAVED_TREE (decl1) = push_stmt_list (); |
17611 | |
17612 | if (ctype && !doing_friend && !DECL_STATIC_FUNCTION_P (decl1)) |
17613 | { |
17614 | /* We know that this was set up by `grokclassfn'. We do not |
17615 | wait until `store_parm_decls', since evil parse errors may |
17616 | never get us to that point. Here we keep the consistency |
17617 | between `current_class_type' and `current_class_ptr'. */ |
17618 | tree t = DECL_ARGUMENTS (decl1); |
17619 | |
17620 | gcc_assert (t != NULL_TREE && TREE_CODE (t) == PARM_DECL); |
17621 | gcc_assert (TYPE_PTR_P (TREE_TYPE (t))); |
17622 | |
17623 | cp_function_chain->x_current_class_ref |
17624 | = cp_build_fold_indirect_ref (t); |
17625 | /* Set this second to avoid shortcut in cp_build_indirect_ref. */ |
17626 | cp_function_chain->x_current_class_ptr = t; |
17627 | |
17628 | /* Constructors and destructors need to know whether they're "in |
17629 | charge" of initializing virtual base classes. */ |
17630 | t = DECL_CHAIN (t); |
17631 | if (DECL_HAS_IN_CHARGE_PARM_P (decl1)) |
17632 | { |
17633 | current_in_charge_parm = t; |
17634 | t = DECL_CHAIN (t); |
17635 | } |
17636 | if (DECL_HAS_VTT_PARM_P (decl1)) |
17637 | { |
17638 | gcc_assert (DECL_NAME (t) == vtt_parm_identifier); |
17639 | current_vtt_parm = t; |
17640 | } |
17641 | } |
17642 | |
17643 | bool honor_interface = (!DECL_TEMPLATE_INSTANTIATION (decl1) |
17644 | /* Implicitly-defined methods (like the |
17645 | destructor for a class in which no destructor |
17646 | is explicitly declared) must not be defined |
17647 | until their definition is needed. So, we |
17648 | ignore interface specifications for |
17649 | compiler-generated functions. */ |
17650 | && !DECL_ARTIFICIAL (decl1)); |
17651 | struct c_fileinfo *finfo |
17652 | = get_fileinfo (LOCATION_FILE (DECL_SOURCE_LOCATION (decl1))); |
17653 | |
17654 | if (processing_template_decl) |
17655 | /* Don't mess with interface flags. */; |
17656 | else if (DECL_INTERFACE_KNOWN (decl1)) |
17657 | { |
17658 | tree ctx = decl_function_context (decl1); |
17659 | |
17660 | if (DECL_NOT_REALLY_EXTERN (decl1)) |
17661 | DECL_EXTERNAL (decl1) = 0; |
17662 | |
17663 | if (ctx != NULL_TREE && vague_linkage_p (ctx)) |
17664 | /* This is a function in a local class in an extern inline |
17665 | or template function. */ |
17666 | comdat_linkage (decl1); |
17667 | } |
17668 | /* If this function belongs to an interface, it is public. |
17669 | If it belongs to someone else's interface, it is also external. |
17670 | This only affects inlines and template instantiations. */ |
17671 | else if (!finfo->interface_unknown && honor_interface) |
17672 | { |
17673 | if (DECL_DECLARED_INLINE_P (decl1) |
17674 | || DECL_TEMPLATE_INSTANTIATION (decl1)) |
17675 | { |
17676 | DECL_EXTERNAL (decl1) |
17677 | = (finfo->interface_only |
17678 | || (DECL_DECLARED_INLINE_P (decl1) |
17679 | && ! flag_implement_inlines |
17680 | && !DECL_VINDEX (decl1))); |
17681 | |
17682 | /* For WIN32 we also want to put these in linkonce sections. */ |
17683 | maybe_make_one_only (decl1); |
17684 | } |
17685 | else |
17686 | DECL_EXTERNAL (decl1) = 0; |
17687 | DECL_INTERFACE_KNOWN (decl1) = 1; |
17688 | /* If this function is in an interface implemented in this file, |
17689 | make sure that the back end knows to emit this function |
17690 | here. */ |
17691 | if (!DECL_EXTERNAL (decl1)) |
17692 | mark_needed (decl1); |
17693 | } |
17694 | else if (finfo->interface_unknown && finfo->interface_only |
17695 | && honor_interface) |
17696 | { |
17697 | /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma |
17698 | interface, we will have both finfo->interface_unknown and |
17699 | finfo->interface_only set. In that case, we don't want to |
17700 | use the normal heuristics because someone will supply a |
17701 | #pragma implementation elsewhere, and deducing it here would |
17702 | produce a conflict. */ |
17703 | comdat_linkage (decl1); |
17704 | DECL_EXTERNAL (decl1) = 0; |
17705 | DECL_INTERFACE_KNOWN (decl1) = 1; |
17706 | DECL_DEFER_OUTPUT (decl1) = 1; |
17707 | } |
17708 | else |
17709 | { |
17710 | /* This is a definition, not a reference. |
17711 | So clear DECL_EXTERNAL, unless this is a GNU extern inline. */ |
17712 | if (!GNU_INLINE_P (decl1)) |
17713 | DECL_EXTERNAL (decl1) = 0; |
17714 | |
17715 | if ((DECL_DECLARED_INLINE_P (decl1) |
17716 | || DECL_TEMPLATE_INSTANTIATION (decl1)) |
17717 | && ! DECL_INTERFACE_KNOWN (decl1)) |
17718 | DECL_DEFER_OUTPUT (decl1) = 1; |
17719 | else |
17720 | DECL_INTERFACE_KNOWN (decl1) = 1; |
17721 | } |
17722 | |
17723 | /* Determine the ELF visibility attribute for the function. We must not |
17724 | do this before calling "pushdecl", as we must allow "duplicate_decls" |
17725 | to merge any attributes appropriately. We also need to wait until |
17726 | linkage is set. */ |
17727 | if (!DECL_CLONED_FUNCTION_P (decl1)) |
17728 | determine_visibility (decl1); |
17729 | |
17730 | if (!processing_template_decl) |
17731 | maybe_instantiate_noexcept (decl1); |
17732 | |
17733 | begin_scope (sk_function_parms, decl1); |
17734 | |
17735 | ++function_depth; |
17736 | |
17737 | start_fname_decls (); |
17738 | |
17739 | store_parm_decls (current_function_parms); |
17740 | |
17741 | start_function_contracts (decl1); |
17742 | |
17743 | if (!processing_template_decl |
17744 | && (flag_lifetime_dse > 1) |
17745 | && DECL_CONSTRUCTOR_P (decl1) |
17746 | && !DECL_CLONED_FUNCTION_P (decl1) |
17747 | /* Clobbering an empty base is harmful if it overlays real data. */ |
17748 | && !is_empty_class (current_class_type) |
17749 | /* We can't clobber safely for an implicitly-defined default constructor |
17750 | because part of the initialization might happen before we enter the |
17751 | constructor, via AGGR_INIT_ZERO_FIRST (c++/68006). */ |
17752 | && !implicit_default_ctor_p (fn: decl1)) |
17753 | finish_expr_stmt (build_clobber_this ()); |
17754 | |
17755 | if (!processing_template_decl |
17756 | && DECL_CONSTRUCTOR_P (decl1) |
17757 | && sanitize_flags_p (flag: SANITIZE_VPTR) |
17758 | && !DECL_CLONED_FUNCTION_P (decl1) |
17759 | && !implicit_default_ctor_p (fn: decl1)) |
17760 | cp_ubsan_maybe_initialize_vtbl_ptrs (current_class_ptr); |
17761 | |
17762 | if (!DECL_OMP_DECLARE_REDUCTION_P (decl1)) |
17763 | start_lambda_scope (decl: decl1); |
17764 | |
17765 | return true; |
17766 | } |
17767 | |
17768 | |
17769 | /* Like start_preparsed_function, except that instead of a |
17770 | FUNCTION_DECL, this function takes DECLSPECS and DECLARATOR. |
17771 | |
17772 | Returns true on success. If the DECLARATOR is not suitable |
17773 | for a function, we return false, which tells the parser to |
17774 | skip the entire function. */ |
17775 | |
17776 | bool |
17777 | start_function (cp_decl_specifier_seq *declspecs, |
17778 | const cp_declarator *declarator, |
17779 | tree attrs) |
17780 | { |
17781 | tree decl1; |
17782 | |
17783 | decl1 = grokdeclarator (declarator, declspecs, decl_context: FUNCDEF, initialized: 1, attrlist: &attrs); |
17784 | invoke_plugin_callbacks (event: PLUGIN_START_PARSE_FUNCTION, gcc_data: decl1); |
17785 | if (decl1 == error_mark_node) |
17786 | return false; |
17787 | |
17788 | if (DECL_MAIN_P (decl1)) |
17789 | /* main must return int. grokfndecl should have corrected it |
17790 | (and issued a diagnostic) if the user got it wrong. */ |
17791 | gcc_assert (same_type_p (TREE_TYPE (TREE_TYPE (decl1)), |
17792 | integer_type_node)); |
17793 | |
17794 | return start_preparsed_function (decl1, attrs, /*flags=*/SF_DEFAULT); |
17795 | } |
17796 | |
17797 | /* Returns true iff an EH_SPEC_BLOCK should be created in the body of |
17798 | FN. */ |
17799 | |
17800 | static bool |
17801 | use_eh_spec_block (tree fn) |
17802 | { |
17803 | return (flag_exceptions && flag_enforce_eh_specs |
17804 | && !processing_template_decl |
17805 | /* We insert the EH_SPEC_BLOCK only in the original |
17806 | function; then, it is copied automatically to the |
17807 | clones. */ |
17808 | && !DECL_CLONED_FUNCTION_P (fn) |
17809 | /* Implicitly-generated constructors and destructors have |
17810 | exception specifications. However, those specifications |
17811 | are the union of the possible exceptions specified by the |
17812 | constructors/destructors for bases and members, so no |
17813 | unallowed exception will ever reach this function. By |
17814 | not creating the EH_SPEC_BLOCK we save a little memory, |
17815 | and we avoid spurious warnings about unreachable |
17816 | code. */ |
17817 | && !DECL_DEFAULTED_FN (fn) |
17818 | && !type_throw_all_p (TREE_TYPE (fn))); |
17819 | } |
17820 | |
17821 | /* Helper function to push ARGS into the current lexical scope. DECL |
17822 | is the function declaration. NONPARMS is used to handle enum |
17823 | constants. */ |
17824 | |
17825 | void |
17826 | do_push_parm_decls (tree decl, tree args, tree *nonparms) |
17827 | { |
17828 | /* If we're doing semantic analysis, then we'll call pushdecl |
17829 | for each of these. We must do them in reverse order so that |
17830 | they end in the correct forward order. */ |
17831 | args = nreverse (args); |
17832 | |
17833 | tree next; |
17834 | for (tree parm = args; parm; parm = next) |
17835 | { |
17836 | next = DECL_CHAIN (parm); |
17837 | if (TREE_CODE (parm) == PARM_DECL) |
17838 | pushdecl (parm); |
17839 | else if (nonparms) |
17840 | { |
17841 | /* If we find an enum constant or a type tag, put it aside for |
17842 | the moment. */ |
17843 | TREE_CHAIN (parm) = NULL_TREE; |
17844 | *nonparms = chainon (*nonparms, parm); |
17845 | } |
17846 | } |
17847 | |
17848 | /* Get the decls in their original chain order and record in the |
17849 | function. This is all and only the PARM_DECLs that were |
17850 | pushed into scope by the loop above. */ |
17851 | DECL_ARGUMENTS (decl) = get_local_decls (); |
17852 | } |
17853 | |
17854 | /* Store the parameter declarations into the current function declaration. |
17855 | This is called after parsing the parameter declarations, before |
17856 | digesting the body of the function. |
17857 | |
17858 | Also install to binding contour return value identifier, if any. */ |
17859 | |
17860 | static void |
17861 | store_parm_decls (tree current_function_parms) |
17862 | { |
17863 | tree fndecl = current_function_decl; |
17864 | |
17865 | /* This is a chain of any other decls that came in among the parm |
17866 | declarations. If a parm is declared with enum {foo, bar} x; |
17867 | then CONST_DECLs for foo and bar are put here. */ |
17868 | tree nonparms = NULL_TREE; |
17869 | |
17870 | if (current_function_parms) |
17871 | { |
17872 | /* This case is when the function was defined with an ANSI prototype. |
17873 | The parms already have decls, so we need not do anything here |
17874 | except record them as in effect |
17875 | and complain if any redundant old-style parm decls were written. */ |
17876 | |
17877 | tree specparms = current_function_parms; |
17878 | |
17879 | /* Must clear this because it might contain TYPE_DECLs declared |
17880 | at class level. */ |
17881 | current_binding_level->names = NULL; |
17882 | |
17883 | do_push_parm_decls (decl: fndecl, args: specparms, nonparms: &nonparms); |
17884 | } |
17885 | else |
17886 | DECL_ARGUMENTS (fndecl) = NULL_TREE; |
17887 | |
17888 | /* Now store the final chain of decls for the arguments |
17889 | as the decl-chain of the current lexical scope. |
17890 | Put the enumerators in as well, at the front so that |
17891 | DECL_ARGUMENTS is not modified. */ |
17892 | current_binding_level->names = chainon (nonparms, DECL_ARGUMENTS (fndecl)); |
17893 | |
17894 | if (use_eh_spec_block (fn: current_function_decl)) |
17895 | current_eh_spec_block = begin_eh_spec_block (); |
17896 | } |
17897 | |
17898 | |
17899 | /* Set the return value of the [cd]tor if the ABI wants that. */ |
17900 | |
17901 | void |
17902 | maybe_return_this (void) |
17903 | { |
17904 | if (targetm.cxx.cdtor_returns_this ()) |
17905 | { |
17906 | /* Return the address of the object. */ |
17907 | tree val = DECL_ARGUMENTS (current_function_decl); |
17908 | suppress_warning (val, OPT_Wuse_after_free); |
17909 | val = fold_convert (TREE_TYPE (DECL_RESULT (current_function_decl)), val); |
17910 | val = build2 (MODIFY_EXPR, TREE_TYPE (val), |
17911 | DECL_RESULT (current_function_decl), val); |
17912 | tree exprstmt = build_stmt (input_location, RETURN_EXPR, val); |
17913 | add_stmt (exprstmt); |
17914 | } |
17915 | } |
17916 | |
17917 | /* Do all the processing for the beginning of a destructor; set up the |
17918 | vtable pointers and cleanups for bases and members. */ |
17919 | |
17920 | static void |
17921 | begin_destructor_body (void) |
17922 | { |
17923 | tree compound_stmt; |
17924 | |
17925 | /* If the CURRENT_CLASS_TYPE is incomplete, we will have already |
17926 | issued an error message. We still want to try to process the |
17927 | body of the function, but initialize_vtbl_ptrs will crash if |
17928 | TYPE_BINFO is NULL. */ |
17929 | if (COMPLETE_TYPE_P (current_class_type)) |
17930 | { |
17931 | compound_stmt = begin_compound_stmt (0); |
17932 | /* Make all virtual function table pointers in non-virtual base |
17933 | classes point to CURRENT_CLASS_TYPE's virtual function |
17934 | tables. */ |
17935 | initialize_vtbl_ptrs (current_class_ptr); |
17936 | finish_compound_stmt (compound_stmt); |
17937 | |
17938 | if (flag_lifetime_dse |
17939 | /* Clobbering an empty base is harmful if it overlays real data. */ |
17940 | && !is_empty_class (current_class_type)) |
17941 | { |
17942 | if (sanitize_flags_p (flag: SANITIZE_VPTR) |
17943 | && (flag_sanitize_recover & SANITIZE_VPTR) == 0 |
17944 | && TYPE_CONTAINS_VPTR_P (current_class_type)) |
17945 | { |
17946 | tree binfo = TYPE_BINFO (current_class_type); |
17947 | tree ref |
17948 | = cp_build_fold_indirect_ref (current_class_ptr); |
17949 | |
17950 | tree vtbl_ptr = build_vfield_ref (ref, TREE_TYPE (binfo)); |
17951 | tree vtbl = build_zero_cst (TREE_TYPE (vtbl_ptr)); |
17952 | tree stmt = cp_build_modify_expr (input_location, vtbl_ptr, |
17953 | NOP_EXPR, vtbl, |
17954 | tf_warning_or_error); |
17955 | /* If the vptr is shared with some virtual nearly empty base, |
17956 | don't clear it if not in charge, the dtor of the virtual |
17957 | nearly empty base will do that later. */ |
17958 | if (CLASSTYPE_VBASECLASSES (current_class_type)) |
17959 | { |
17960 | tree c = current_class_type; |
17961 | while (CLASSTYPE_PRIMARY_BINFO (c)) |
17962 | { |
17963 | if (BINFO_VIRTUAL_P (CLASSTYPE_PRIMARY_BINFO (c))) |
17964 | { |
17965 | stmt = convert_to_void (stmt, ICV_STATEMENT, |
17966 | tf_warning_or_error); |
17967 | stmt = build_if_in_charge (true_stmt: stmt); |
17968 | break; |
17969 | } |
17970 | c = BINFO_TYPE (CLASSTYPE_PRIMARY_BINFO (c)); |
17971 | } |
17972 | } |
17973 | finish_decl_cleanup (NULL_TREE, stmt); |
17974 | } |
17975 | else |
17976 | finish_decl_cleanup (NULL_TREE, build_clobber_this ()); |
17977 | } |
17978 | |
17979 | /* And insert cleanups for our bases and members so that they |
17980 | will be properly destroyed if we throw. */ |
17981 | push_base_cleanups (); |
17982 | } |
17983 | } |
17984 | |
17985 | /* Do the necessary processing for the beginning of a function body, which |
17986 | in this case includes member-initializers, but not the catch clauses of |
17987 | a function-try-block. Currently, this means opening a binding level |
17988 | for the member-initializers (in a ctor), member cleanups (in a dtor), |
17989 | and capture proxies (in a lambda operator()). */ |
17990 | |
17991 | tree |
17992 | begin_function_body (void) |
17993 | { |
17994 | if (! FUNCTION_NEEDS_BODY_BLOCK (current_function_decl)) |
17995 | return NULL_TREE; |
17996 | |
17997 | if (processing_template_decl) |
17998 | /* Do nothing now. */; |
17999 | else |
18000 | /* Always keep the BLOCK node associated with the outermost pair of |
18001 | curly braces of a function. These are needed for correct |
18002 | operation of dwarfout.c. */ |
18003 | keep_next_level (true); |
18004 | |
18005 | tree stmt = begin_compound_stmt (BCS_FN_BODY); |
18006 | current_binding_level->artificial = 1; |
18007 | |
18008 | if (processing_template_decl) |
18009 | /* Do nothing now. */; |
18010 | else if (DECL_DESTRUCTOR_P (current_function_decl)) |
18011 | begin_destructor_body (); |
18012 | |
18013 | return stmt; |
18014 | } |
18015 | |
18016 | /* Do the processing for the end of a function body. Currently, this means |
18017 | closing out the cleanups for fully-constructed bases and members, and in |
18018 | the case of the destructor, deleting the object if desired. Again, this |
18019 | is only meaningful for [cd]tors, since they are the only functions where |
18020 | there is a significant distinction between the main body and any |
18021 | function catch clauses. Handling, say, main() return semantics here |
18022 | would be wrong, as flowing off the end of a function catch clause for |
18023 | main() would also need to return 0. */ |
18024 | |
18025 | void |
18026 | finish_function_body (tree compstmt) |
18027 | { |
18028 | if (compstmt == NULL_TREE) |
18029 | return; |
18030 | |
18031 | /* Close the block. */ |
18032 | finish_compound_stmt (compstmt); |
18033 | |
18034 | if (processing_template_decl) |
18035 | /* Do nothing now. */; |
18036 | else if (DECL_CONSTRUCTOR_P (current_function_decl) |
18037 | || DECL_DESTRUCTOR_P (current_function_decl)) |
18038 | maybe_return_this (); |
18039 | } |
18040 | |
18041 | /* Given a function, returns the BLOCK corresponding to the outermost level |
18042 | of curly braces, skipping the artificial block created for constructor |
18043 | initializers. */ |
18044 | |
18045 | tree |
18046 | outer_curly_brace_block (tree fndecl) |
18047 | { |
18048 | tree block = DECL_INITIAL (fndecl); |
18049 | if (BLOCK_OUTER_CURLY_BRACE_P (block)) |
18050 | return block; |
18051 | block = BLOCK_SUBBLOCKS (block); |
18052 | if (BLOCK_OUTER_CURLY_BRACE_P (block)) |
18053 | return block; |
18054 | block = BLOCK_SUBBLOCKS (block); |
18055 | gcc_assert (BLOCK_OUTER_CURLY_BRACE_P (block)); |
18056 | return block; |
18057 | } |
18058 | |
18059 | /* If FNDECL is a class's key method, add the class to the list of |
18060 | keyed classes that should be emitted. */ |
18061 | |
18062 | static void |
18063 | record_key_method_defined (tree fndecl) |
18064 | { |
18065 | if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fndecl) |
18066 | && DECL_VIRTUAL_P (fndecl) |
18067 | && !processing_template_decl) |
18068 | { |
18069 | tree fnclass = DECL_CONTEXT (fndecl); |
18070 | if (fndecl == CLASSTYPE_KEY_METHOD (fnclass)) |
18071 | vec_safe_push (v&: keyed_classes, obj: fnclass); |
18072 | } |
18073 | } |
18074 | |
18075 | /* Attempt to add a fix-it hint to RICHLOC suggesting the insertion |
18076 | of "return *this;" immediately before its location, using FNDECL's |
18077 | first statement (if any) to give the indentation, if appropriate. */ |
18078 | |
18079 | static void |
18080 | add_return_star_this_fixit (gcc_rich_location *richloc, tree fndecl) |
18081 | { |
18082 | location_t indent = UNKNOWN_LOCATION; |
18083 | tree stmts = expr_first (DECL_SAVED_TREE (fndecl)); |
18084 | if (stmts) |
18085 | indent = EXPR_LOCATION (stmts); |
18086 | richloc->add_fixit_insert_formatted (content: "return *this;" , |
18087 | insertion_point: richloc->get_loc (), |
18088 | indent); |
18089 | } |
18090 | |
18091 | /* This function carries out the subset of finish_function operations needed |
18092 | to emit the compiler-generated outlined helper functions used by the |
18093 | coroutines implementation. */ |
18094 | |
18095 | static void |
18096 | emit_coro_helper (tree helper) |
18097 | { |
18098 | /* This is a partial set of the operations done by finish_function() |
18099 | plus emitting the result. */ |
18100 | set_cfun (NULL); |
18101 | current_function_decl = helper; |
18102 | begin_scope (sk_function_parms, NULL); |
18103 | store_parm_decls (DECL_ARGUMENTS (helper)); |
18104 | announce_function (helper); |
18105 | allocate_struct_function (helper, false); |
18106 | cfun->language = ggc_cleared_alloc<language_function> (); |
18107 | poplevel (keep: 1, reverse: 0, functionbody: 1); |
18108 | maybe_save_constexpr_fundef (helper); |
18109 | /* We must start each function with a clear fold cache. */ |
18110 | clear_fold_cache (); |
18111 | cp_fold_function (helper); |
18112 | DECL_CONTEXT (DECL_RESULT (helper)) = helper; |
18113 | BLOCK_SUPERCONTEXT (DECL_INITIAL (helper)) = helper; |
18114 | /* This function has coroutine IFNs that we should handle in middle |
18115 | end lowering. */ |
18116 | cfun->coroutine_component = true; |
18117 | cp_genericize (helper); |
18118 | expand_or_defer_fn (helper); |
18119 | } |
18120 | |
18121 | /* Finish up a function declaration and compile that function |
18122 | all the way to assembler language output. The free the storage |
18123 | for the function definition. INLINE_P is TRUE if we just |
18124 | finished processing the body of an in-class inline function |
18125 | definition. (This processing will have taken place after the |
18126 | class definition is complete.) */ |
18127 | |
18128 | tree |
18129 | finish_function (bool inline_p) |
18130 | { |
18131 | tree fndecl = current_function_decl; |
18132 | tree fntype, ctype = NULL_TREE; |
18133 | tree resumer = NULL_TREE, destroyer = NULL_TREE; |
18134 | bool coro_p = flag_coroutines |
18135 | && !processing_template_decl |
18136 | && DECL_COROUTINE_P (fndecl); |
18137 | bool coro_emit_helpers = false; |
18138 | |
18139 | /* When we get some parse errors, we can end up without a |
18140 | current_function_decl, so cope. */ |
18141 | if (fndecl == NULL_TREE) |
18142 | return error_mark_node; |
18143 | |
18144 | if (!DECL_OMP_DECLARE_REDUCTION_P (fndecl)) |
18145 | finish_lambda_scope (); |
18146 | |
18147 | if (c_dialect_objc ()) |
18148 | objc_finish_function (); |
18149 | |
18150 | record_key_method_defined (fndecl); |
18151 | |
18152 | fntype = TREE_TYPE (fndecl); |
18153 | |
18154 | /* TREE_READONLY (fndecl) = 1; |
18155 | This caused &foo to be of type ptr-to-const-function |
18156 | which then got a warning when stored in a ptr-to-function variable. */ |
18157 | |
18158 | gcc_assert (building_stmt_list_p ()); |
18159 | /* The current function is being defined, so its DECL_INITIAL should |
18160 | be set, and unless there's a multiple definition, it should be |
18161 | error_mark_node. */ |
18162 | gcc_assert (DECL_INITIAL (fndecl) == error_mark_node); |
18163 | |
18164 | if (coro_p) |
18165 | { |
18166 | /* Only try to emit the coroutine outlined helper functions if the |
18167 | transforms succeeded. Otherwise, treat errors in the same way as |
18168 | a regular function. */ |
18169 | coro_emit_helpers = morph_fn_to_coro (fndecl, &resumer, &destroyer); |
18170 | |
18171 | /* We should handle coroutine IFNs in middle end lowering. */ |
18172 | cfun->coroutine_component = true; |
18173 | |
18174 | /* Do not try to process the ramp's EH unless outlining succeeded. */ |
18175 | if (coro_emit_helpers && use_eh_spec_block (fn: fndecl)) |
18176 | finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS |
18177 | (TREE_TYPE (fndecl)), |
18178 | current_eh_spec_block); |
18179 | } |
18180 | else |
18181 | /* For a cloned function, we've already got all the code we need; |
18182 | there's no need to add any extra bits. */ |
18183 | if (!DECL_CLONED_FUNCTION_P (fndecl)) |
18184 | { |
18185 | /* Make it so that `main' always returns 0 by default. */ |
18186 | if (DECL_MAIN_FREESTANDING_P (current_function_decl) |
18187 | && !TREE_THIS_VOLATILE (current_function_decl)) |
18188 | finish_return_stmt (integer_zero_node); |
18189 | |
18190 | if (use_eh_spec_block (fn: current_function_decl)) |
18191 | finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS |
18192 | (TREE_TYPE (current_function_decl)), |
18193 | current_eh_spec_block); |
18194 | } |
18195 | |
18196 | /* If we're saving up tree structure, tie off the function now. */ |
18197 | DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl)); |
18198 | |
18199 | finish_fname_decls (); |
18200 | |
18201 | /* This must come after expand_function_end because cleanups might |
18202 | have declarations (from inline functions) that need to go into |
18203 | this function's blocks. */ |
18204 | |
18205 | /* If the current binding level isn't the outermost binding level |
18206 | for this function, either there is a bug, or we have experienced |
18207 | syntax errors and the statement tree is malformed. */ |
18208 | if (current_binding_level->kind != sk_function_parms) |
18209 | { |
18210 | /* Make sure we have already experienced errors. */ |
18211 | gcc_assert (errorcount); |
18212 | |
18213 | /* Throw away the broken statement tree and extra binding |
18214 | levels. */ |
18215 | DECL_SAVED_TREE (fndecl) = alloc_stmt_list (); |
18216 | |
18217 | while (current_binding_level->kind != sk_function_parms) |
18218 | { |
18219 | if (current_binding_level->kind == sk_class) |
18220 | pop_nested_class (); |
18221 | else |
18222 | poplevel (keep: 0, reverse: 0, functionbody: 0); |
18223 | } |
18224 | } |
18225 | poplevel (keep: 1, reverse: 0, functionbody: 1); |
18226 | |
18227 | /* Statements should always be full-expressions at the outermost set |
18228 | of curly braces for a function. */ |
18229 | gcc_assert (stmts_are_full_exprs_p ()); |
18230 | |
18231 | /* If there are no return statements in a function with auto return type, |
18232 | the return type is void. But if the declared type is something like |
18233 | auto*, this is an error. */ |
18234 | if (!processing_template_decl && FNDECL_USED_AUTO (fndecl) |
18235 | && TREE_TYPE (fntype) == DECL_SAVED_AUTO_RETURN_TYPE (fndecl)) |
18236 | { |
18237 | if (is_auto (DECL_SAVED_AUTO_RETURN_TYPE (fndecl)) |
18238 | && !current_function_returns_value |
18239 | && !current_function_returns_null) |
18240 | { |
18241 | /* We haven't applied return type deduction because we haven't |
18242 | seen any return statements. Do that now. */ |
18243 | tree node = type_uses_auto (DECL_SAVED_AUTO_RETURN_TYPE (fndecl)); |
18244 | do_auto_deduction (DECL_SAVED_AUTO_RETURN_TYPE (fndecl), |
18245 | void_node, node, tf_warning_or_error, |
18246 | adc_return_type); |
18247 | |
18248 | apply_deduced_return_type (fndecl, void_type_node); |
18249 | fntype = TREE_TYPE (fndecl); |
18250 | } |
18251 | else if (!current_function_returns_value |
18252 | && !current_function_returns_null) |
18253 | { |
18254 | error ("no return statements in function returning %qT" , |
18255 | DECL_SAVED_AUTO_RETURN_TYPE (fndecl)); |
18256 | inform (input_location, "only plain %<auto%> return type can be " |
18257 | "deduced to %<void%>" ); |
18258 | } |
18259 | } |
18260 | |
18261 | /* Remember that we were in class scope. */ |
18262 | if (current_class_name) |
18263 | ctype = current_class_type; |
18264 | |
18265 | if (DECL_DELETED_FN (fndecl)) |
18266 | { |
18267 | DECL_INITIAL (fndecl) = error_mark_node; |
18268 | DECL_SAVED_TREE (fndecl) = NULL_TREE; |
18269 | goto cleanup; |
18270 | } |
18271 | |
18272 | // If this is a concept, check that the definition is reasonable. |
18273 | if (DECL_DECLARED_CONCEPT_P (fndecl)) |
18274 | check_function_concept (fndecl); |
18275 | |
18276 | if (flag_openmp) |
18277 | if (tree attr = lookup_attribute (attr_name: "omp declare variant base" , |
18278 | DECL_ATTRIBUTES (fndecl))) |
18279 | omp_declare_variant_finalize (decl: fndecl, attr); |
18280 | |
18281 | /* Complain if there's just no return statement. */ |
18282 | if ((warn_return_type |
18283 | || (cxx_dialect >= cxx14 |
18284 | && DECL_DECLARED_CONSTEXPR_P (fndecl))) |
18285 | && !VOID_TYPE_P (TREE_TYPE (fntype)) |
18286 | && !dependent_type_p (TREE_TYPE (fntype)) |
18287 | && !current_function_returns_value && !current_function_returns_null |
18288 | /* Don't complain if we abort or throw. */ |
18289 | && !current_function_returns_abnormally |
18290 | /* Don't complain if there's an infinite loop. */ |
18291 | && !current_function_infinite_loop |
18292 | /* Don't complain if we are declared noreturn. */ |
18293 | && !TREE_THIS_VOLATILE (fndecl) |
18294 | && !DECL_NAME (DECL_RESULT (fndecl)) |
18295 | && !warning_suppressed_p (fndecl, OPT_Wreturn_type) |
18296 | /* Structor return values (if any) are set by the compiler. */ |
18297 | && !DECL_CONSTRUCTOR_P (fndecl) |
18298 | && !DECL_DESTRUCTOR_P (fndecl) |
18299 | && targetm.warn_func_return (fndecl)) |
18300 | { |
18301 | gcc_rich_location richloc (input_location); |
18302 | /* Potentially add a "return *this;" fix-it hint for |
18303 | assignment operators. */ |
18304 | if (IDENTIFIER_ASSIGN_OP_P (DECL_NAME (fndecl))) |
18305 | { |
18306 | tree valtype = TREE_TYPE (DECL_RESULT (fndecl)); |
18307 | if (TREE_CODE (valtype) == REFERENCE_TYPE |
18308 | && current_class_ref |
18309 | && same_type_ignoring_top_level_qualifiers_p |
18310 | (TREE_TYPE (valtype), TREE_TYPE (current_class_ref)) |
18311 | && global_dc->m_option_enabled (OPT_Wreturn_type, |
18312 | global_dc->m_lang_mask, |
18313 | global_dc->m_option_state)) |
18314 | add_return_star_this_fixit (richloc: &richloc, fndecl); |
18315 | } |
18316 | if (cxx_dialect >= cxx14 |
18317 | && DECL_DECLARED_CONSTEXPR_P (fndecl)) |
18318 | error_at (&richloc, "no return statement in %<constexpr%> function " |
18319 | "returning non-void" ); |
18320 | else if (warning_at (&richloc, OPT_Wreturn_type, |
18321 | "no return statement in function returning " |
18322 | "non-void" )) |
18323 | suppress_warning (fndecl, OPT_Wreturn_type); |
18324 | } |
18325 | |
18326 | /* Lambda closure members are implicitly constexpr if possible. */ |
18327 | if (cxx_dialect >= cxx17 |
18328 | && LAMBDA_TYPE_P (CP_DECL_CONTEXT (fndecl))) |
18329 | DECL_DECLARED_CONSTEXPR_P (fndecl) |
18330 | = ((processing_template_decl |
18331 | || is_valid_constexpr_fn (fndecl, /*complain*/false)) |
18332 | && potential_constant_expression (DECL_SAVED_TREE (fndecl))); |
18333 | |
18334 | /* Save constexpr function body before it gets munged by |
18335 | the NRV transformation. */ |
18336 | maybe_save_constexpr_fundef (fndecl); |
18337 | |
18338 | /* Invoke the pre-genericize plugin before we start munging things. */ |
18339 | if (!processing_template_decl) |
18340 | invoke_plugin_callbacks (event: PLUGIN_PRE_GENERICIZE, gcc_data: fndecl); |
18341 | |
18342 | /* Perform delayed folding before NRV transformation. */ |
18343 | if (!processing_template_decl |
18344 | && !DECL_IMMEDIATE_FUNCTION_P (fndecl) |
18345 | && !DECL_OMP_DECLARE_REDUCTION_P (fndecl)) |
18346 | cp_fold_function (fndecl); |
18347 | |
18348 | /* Set up the named return value optimization, if we can. Candidate |
18349 | variables are selected in check_return_expr. */ |
18350 | if (tree r = current_function_return_value) |
18351 | { |
18352 | if (r != error_mark_node) |
18353 | finalize_nrv (fndecl, r); |
18354 | current_function_return_value = NULL_TREE; |
18355 | } |
18356 | |
18357 | /* Must mark the RESULT_DECL as being in this function. */ |
18358 | DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl; |
18359 | |
18360 | /* Set the BLOCK_SUPERCONTEXT of the outermost function scope to point |
18361 | to the FUNCTION_DECL node itself. */ |
18362 | BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl; |
18363 | |
18364 | /* Store the end of the function, so that we get good line number |
18365 | info for the epilogue. */ |
18366 | cfun->function_end_locus = input_location; |
18367 | |
18368 | /* Complain about parameters that are only set, but never otherwise used. */ |
18369 | if (warn_unused_but_set_parameter |
18370 | && !processing_template_decl |
18371 | && errorcount == unused_but_set_errorcount |
18372 | && !DECL_CLONED_FUNCTION_P (fndecl)) |
18373 | { |
18374 | tree decl; |
18375 | |
18376 | for (decl = DECL_ARGUMENTS (fndecl); |
18377 | decl; |
18378 | decl = DECL_CHAIN (decl)) |
18379 | if (TREE_USED (decl) |
18380 | && TREE_CODE (decl) == PARM_DECL |
18381 | && !DECL_READ_P (decl) |
18382 | && DECL_NAME (decl) |
18383 | && !DECL_ARTIFICIAL (decl) |
18384 | && !warning_suppressed_p (decl,OPT_Wunused_but_set_parameter) |
18385 | && !DECL_IN_SYSTEM_HEADER (decl) |
18386 | && TREE_TYPE (decl) != error_mark_node |
18387 | && !TYPE_REF_P (TREE_TYPE (decl)) |
18388 | && (!CLASS_TYPE_P (TREE_TYPE (decl)) |
18389 | || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))) |
18390 | warning_at (DECL_SOURCE_LOCATION (decl), |
18391 | OPT_Wunused_but_set_parameter, |
18392 | "parameter %qD set but not used" , decl); |
18393 | unused_but_set_errorcount = errorcount; |
18394 | } |
18395 | |
18396 | /* Complain about locally defined typedefs that are not used in this |
18397 | function. */ |
18398 | maybe_warn_unused_local_typedefs (); |
18399 | |
18400 | /* Possibly warn about unused parameters. */ |
18401 | if (warn_unused_parameter |
18402 | && !processing_template_decl |
18403 | && !DECL_CLONED_FUNCTION_P (fndecl)) |
18404 | do_warn_unused_parameter (fndecl); |
18405 | |
18406 | /* Genericize before inlining. */ |
18407 | if (!processing_template_decl |
18408 | && !DECL_IMMEDIATE_FUNCTION_P (fndecl) |
18409 | && !DECL_OMP_DECLARE_REDUCTION_P (fndecl)) |
18410 | cp_genericize (fndecl); |
18411 | |
18412 | /* If this function can't throw any exceptions, remember that. */ |
18413 | if (!processing_template_decl |
18414 | && !cp_function_chain->can_throw |
18415 | && !flag_non_call_exceptions |
18416 | && !decl_replaceable_p (fndecl, |
18417 | opt_for_fn (fndecl, flag_semantic_interposition))) |
18418 | TREE_NOTHROW (fndecl) = 1; |
18419 | |
18420 | /* Emit the resumer and destroyer functions now, providing that we have |
18421 | not encountered some fatal error. */ |
18422 | if (coro_emit_helpers) |
18423 | { |
18424 | emit_coro_helper (helper: resumer); |
18425 | emit_coro_helper (helper: destroyer); |
18426 | } |
18427 | |
18428 | cleanup: |
18429 | /* We're leaving the context of this function, so zap cfun. It's still in |
18430 | DECL_STRUCT_FUNCTION, and we'll restore it in tree_rest_of_compilation. */ |
18431 | set_cfun (NULL); |
18432 | current_function_decl = NULL; |
18433 | |
18434 | /* If this is an in-class inline definition, we may have to pop the |
18435 | bindings for the template parameters that we added in |
18436 | maybe_begin_member_template_processing when start_function was |
18437 | called. */ |
18438 | if (inline_p) |
18439 | maybe_end_member_template_processing (); |
18440 | |
18441 | /* Leave the scope of the class. */ |
18442 | if (ctype) |
18443 | pop_nested_class (); |
18444 | |
18445 | --function_depth; |
18446 | |
18447 | /* Clean up. */ |
18448 | current_function_decl = NULL_TREE; |
18449 | |
18450 | invoke_plugin_callbacks (event: PLUGIN_FINISH_PARSE_FUNCTION, gcc_data: fndecl); |
18451 | |
18452 | finish_function_contracts (fndecl); |
18453 | |
18454 | return fndecl; |
18455 | } |
18456 | |
18457 | /* Create the FUNCTION_DECL for a function definition. |
18458 | DECLSPECS and DECLARATOR are the parts of the declaration; |
18459 | they describe the return type and the name of the function, |
18460 | but twisted together in a fashion that parallels the syntax of C. |
18461 | |
18462 | This function creates a binding context for the function body |
18463 | as well as setting up the FUNCTION_DECL in current_function_decl. |
18464 | |
18465 | Returns a FUNCTION_DECL on success. |
18466 | |
18467 | If the DECLARATOR is not suitable for a function (it defines a datum |
18468 | instead), we return 0, which tells yyparse to report a parse error. |
18469 | |
18470 | May return void_type_node indicating that this method is actually |
18471 | a friend. See grokfield for more details. |
18472 | |
18473 | Came here with a `.pushlevel' . |
18474 | |
18475 | DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING |
18476 | CHANGES TO CODE IN `grokfield'. */ |
18477 | |
18478 | tree |
18479 | grokmethod (cp_decl_specifier_seq *declspecs, |
18480 | const cp_declarator *declarator, tree attrlist) |
18481 | { |
18482 | tree fndecl = grokdeclarator (declarator, declspecs, decl_context: MEMFUNCDEF, initialized: 0, |
18483 | attrlist: &attrlist); |
18484 | |
18485 | if (fndecl == error_mark_node) |
18486 | return error_mark_node; |
18487 | |
18488 | if (attrlist) |
18489 | cplus_decl_attributes (&fndecl, attrlist, 0); |
18490 | |
18491 | /* Pass friends other than inline friend functions back. */ |
18492 | if (fndecl == void_type_node) |
18493 | return fndecl; |
18494 | |
18495 | if (DECL_IN_AGGR_P (fndecl)) |
18496 | { |
18497 | if (DECL_CLASS_SCOPE_P (fndecl)) |
18498 | error ("%qD is already defined in class %qT" , fndecl, |
18499 | DECL_CONTEXT (fndecl)); |
18500 | return error_mark_node; |
18501 | } |
18502 | |
18503 | check_template_shadow (fndecl); |
18504 | |
18505 | /* p1779 ABI-Isolation makes inline not a default for in-class |
18506 | definitions in named module purview. If the user explicitly |
18507 | made it inline, grokdeclarator will already have done the right |
18508 | things. */ |
18509 | if ((!named_module_purview_p () |
18510 | || flag_module_implicit_inline |
18511 | /* Lambda's operator function remains inline. */ |
18512 | || LAMBDA_TYPE_P (DECL_CONTEXT (fndecl))) |
18513 | /* If the user explicitly asked for this to be inline, we don't |
18514 | need to do more, but more importantly we want to warn if we |
18515 | can't inline it. */ |
18516 | && !DECL_DECLARED_INLINE_P (fndecl)) |
18517 | { |
18518 | if (TREE_PUBLIC (fndecl)) |
18519 | DECL_COMDAT (fndecl) = 1; |
18520 | DECL_DECLARED_INLINE_P (fndecl) = 1; |
18521 | /* It's ok if we can't inline this. */ |
18522 | DECL_NO_INLINE_WARNING_P (fndecl) = 1; |
18523 | } |
18524 | |
18525 | /* We process method specializations in finish_struct_1. */ |
18526 | if (processing_template_decl && !DECL_TEMPLATE_SPECIALIZATION (fndecl)) |
18527 | { |
18528 | /* Avoid calling decl_spec_seq... until we have to. */ |
18529 | bool friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend); |
18530 | fndecl = push_template_decl (fndecl, is_friend: friendp); |
18531 | if (fndecl == error_mark_node) |
18532 | return fndecl; |
18533 | } |
18534 | |
18535 | if (DECL_CHAIN (fndecl) && !decl_spec_seq_has_spec_p (declspecs, ds_friend)) |
18536 | { |
18537 | fndecl = copy_node (fndecl); |
18538 | TREE_CHAIN (fndecl) = NULL_TREE; |
18539 | } |
18540 | |
18541 | cp_finish_decl (decl: fndecl, NULL_TREE, init_const_expr_p: false, NULL_TREE, flags: 0); |
18542 | |
18543 | DECL_IN_AGGR_P (fndecl) = 1; |
18544 | return fndecl; |
18545 | } |
18546 | |
18547 | |
18548 | /* VAR is a VAR_DECL. If its type is incomplete, remember VAR so that |
18549 | we can lay it out later, when and if its type becomes complete. |
18550 | |
18551 | Also handle constexpr variables where the initializer involves |
18552 | an unlowered PTRMEM_CST because the class isn't complete yet. */ |
18553 | |
18554 | void |
18555 | maybe_register_incomplete_var (tree var) |
18556 | { |
18557 | gcc_assert (VAR_P (var)); |
18558 | |
18559 | /* Keep track of variables with incomplete types. */ |
18560 | if (!processing_template_decl && TREE_TYPE (var) != error_mark_node |
18561 | && DECL_EXTERNAL (var)) |
18562 | { |
18563 | tree inner_type = TREE_TYPE (var); |
18564 | |
18565 | while (TREE_CODE (inner_type) == ARRAY_TYPE) |
18566 | inner_type = TREE_TYPE (inner_type); |
18567 | inner_type = TYPE_MAIN_VARIANT (inner_type); |
18568 | |
18569 | if ((!COMPLETE_TYPE_P (inner_type) && CLASS_TYPE_P (inner_type)) |
18570 | /* RTTI TD entries are created while defining the type_info. */ |
18571 | || (TYPE_LANG_SPECIFIC (inner_type) |
18572 | && TYPE_BEING_DEFINED (inner_type))) |
18573 | { |
18574 | incomplete_var iv = {.decl: var, .incomplete_type: inner_type}; |
18575 | vec_safe_push (v&: incomplete_vars, obj: iv); |
18576 | } |
18577 | else if (!(DECL_LANG_SPECIFIC (var) && DECL_TEMPLATE_INFO (var)) |
18578 | && decl_constant_var_p (var) |
18579 | && (TYPE_PTRMEM_P (inner_type) || CLASS_TYPE_P (inner_type))) |
18580 | { |
18581 | /* When the outermost open class is complete we can resolve any |
18582 | pointers-to-members. */ |
18583 | tree context = outermost_open_class (); |
18584 | incomplete_var iv = {.decl: var, .incomplete_type: context}; |
18585 | vec_safe_push (v&: incomplete_vars, obj: iv); |
18586 | } |
18587 | } |
18588 | } |
18589 | |
18590 | /* Called when a class type (given by TYPE) is defined. If there are |
18591 | any existing VAR_DECLs whose type has been completed by this |
18592 | declaration, update them now. */ |
18593 | |
18594 | void |
18595 | complete_vars (tree type) |
18596 | { |
18597 | unsigned ix; |
18598 | incomplete_var *iv; |
18599 | |
18600 | for (ix = 0; vec_safe_iterate (v: incomplete_vars, ix, ptr: &iv); ) |
18601 | { |
18602 | if (same_type_p (type, iv->incomplete_type)) |
18603 | { |
18604 | tree var = iv->decl; |
18605 | tree type = TREE_TYPE (var); |
18606 | |
18607 | if (type != error_mark_node |
18608 | && (TYPE_MAIN_VARIANT (strip_array_types (type)) |
18609 | == iv->incomplete_type)) |
18610 | { |
18611 | /* Complete the type of the variable. */ |
18612 | complete_type (type); |
18613 | cp_apply_type_quals_to_decl (cp_type_quals (type), var); |
18614 | if (COMPLETE_TYPE_P (type)) |
18615 | layout_var_decl (decl: var); |
18616 | } |
18617 | |
18618 | /* Remove this entry from the list. */ |
18619 | incomplete_vars->unordered_remove (ix); |
18620 | } |
18621 | else |
18622 | ix++; |
18623 | } |
18624 | } |
18625 | |
18626 | /* If DECL is of a type which needs a cleanup, build and return an |
18627 | expression to perform that cleanup here. Return NULL_TREE if no |
18628 | cleanup need be done. DECL can also be a _REF when called from |
18629 | split_nonconstant_init_1. */ |
18630 | |
18631 | tree |
18632 | cxx_maybe_build_cleanup (tree decl, tsubst_flags_t complain) |
18633 | { |
18634 | tree type; |
18635 | tree attr; |
18636 | tree cleanup; |
18637 | |
18638 | /* Assume no cleanup is required. */ |
18639 | cleanup = NULL_TREE; |
18640 | |
18641 | if (error_operand_p (t: decl)) |
18642 | return cleanup; |
18643 | |
18644 | /* Handle "__attribute__((cleanup))". We run the cleanup function |
18645 | before the destructor since the destructor is what actually |
18646 | terminates the lifetime of the object. */ |
18647 | if (DECL_P (decl)) |
18648 | attr = lookup_attribute (attr_name: "cleanup" , DECL_ATTRIBUTES (decl)); |
18649 | else |
18650 | attr = NULL_TREE; |
18651 | if (attr) |
18652 | { |
18653 | tree id; |
18654 | tree fn; |
18655 | tree arg; |
18656 | |
18657 | /* Get the name specified by the user for the cleanup function. */ |
18658 | id = TREE_VALUE (TREE_VALUE (attr)); |
18659 | /* Look up the name to find the cleanup function to call. It is |
18660 | important to use lookup_name here because that is what is |
18661 | used in c-common.cc:handle_cleanup_attribute when performing |
18662 | initial checks on the attribute. Note that those checks |
18663 | include ensuring that the function found is not an overloaded |
18664 | function, or an object with an overloaded call operator, |
18665 | etc.; we can rely on the fact that the function found is an |
18666 | ordinary FUNCTION_DECL. */ |
18667 | fn = lookup_name (name: id); |
18668 | arg = build_address (decl); |
18669 | if (!mark_used (decl, complain) && !(complain & tf_error)) |
18670 | return error_mark_node; |
18671 | cleanup = cp_build_function_call_nary (fn, complain, arg, NULL_TREE); |
18672 | if (cleanup == error_mark_node) |
18673 | return error_mark_node; |
18674 | } |
18675 | /* Handle ordinary C++ destructors. */ |
18676 | type = TREE_TYPE (decl); |
18677 | if (type_build_dtor_call (type)) |
18678 | { |
18679 | int flags = LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR; |
18680 | tree addr; |
18681 | tree call; |
18682 | |
18683 | if (TREE_CODE (type) == ARRAY_TYPE) |
18684 | addr = decl; |
18685 | else |
18686 | addr = build_address (decl); |
18687 | |
18688 | call = build_delete (input_location, TREE_TYPE (addr), addr, |
18689 | sfk_complete_destructor, flags, 0, complain); |
18690 | if (call == error_mark_node) |
18691 | cleanup = error_mark_node; |
18692 | else if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type)) |
18693 | /* Discard the call. */; |
18694 | else if (decl_maybe_constant_destruction (decl, type) |
18695 | && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl)) |
18696 | cxx_constant_dtor (call, decl); |
18697 | else if (cleanup) |
18698 | cleanup = cp_build_compound_expr (cleanup, call, complain); |
18699 | else |
18700 | cleanup = call; |
18701 | } |
18702 | |
18703 | /* build_delete sets the location of the destructor call to the |
18704 | current location, even though the destructor is going to be |
18705 | called later, at the end of the current scope. This can lead to |
18706 | a "jumpy" behavior for users of debuggers when they step around |
18707 | the end of the block. So let's unset the location of the |
18708 | destructor call instead. */ |
18709 | protected_set_expr_location (cleanup, UNKNOWN_LOCATION); |
18710 | if (cleanup && CONVERT_EXPR_P (cleanup)) |
18711 | protected_set_expr_location (TREE_OPERAND (cleanup, 0), UNKNOWN_LOCATION); |
18712 | |
18713 | if (cleanup |
18714 | && DECL_P (decl) |
18715 | && !lookup_attribute (attr_name: "warn_unused" , TYPE_ATTRIBUTES (TREE_TYPE (decl))) |
18716 | /* Treat objects with destructors as used; the destructor may do |
18717 | something substantive. */ |
18718 | && !mark_used (decl, complain) && !(complain & tf_error)) |
18719 | return error_mark_node; |
18720 | |
18721 | if (cleanup && cfun && !processing_template_decl |
18722 | && !expr_noexcept_p (cleanup, tf_none)) |
18723 | cp_function_chain->throwing_cleanup = true; |
18724 | |
18725 | return cleanup; |
18726 | } |
18727 | |
18728 | |
18729 | /* Return the FUNCTION_TYPE that corresponds to MEMFNTYPE, which can be a |
18730 | FUNCTION_DECL, METHOD_TYPE, FUNCTION_TYPE, pointer or reference to |
18731 | METHOD_TYPE or FUNCTION_TYPE, or pointer to member function. */ |
18732 | |
18733 | tree |
18734 | static_fn_type (tree memfntype) |
18735 | { |
18736 | tree fntype; |
18737 | tree args; |
18738 | |
18739 | if (TYPE_PTRMEMFUNC_P (memfntype)) |
18740 | memfntype = TYPE_PTRMEMFUNC_FN_TYPE (memfntype); |
18741 | if (INDIRECT_TYPE_P (memfntype) |
18742 | || TREE_CODE (memfntype) == FUNCTION_DECL) |
18743 | memfntype = TREE_TYPE (memfntype); |
18744 | if (TREE_CODE (memfntype) == FUNCTION_TYPE) |
18745 | return memfntype; |
18746 | gcc_assert (TREE_CODE (memfntype) == METHOD_TYPE); |
18747 | args = TYPE_ARG_TYPES (memfntype); |
18748 | fntype = build_function_type (TREE_TYPE (memfntype), TREE_CHAIN (args)); |
18749 | fntype = apply_memfn_quals (fntype, type_memfn_quals (memfntype)); |
18750 | fntype = (cp_build_type_attribute_variant |
18751 | (fntype, TYPE_ATTRIBUTES (memfntype))); |
18752 | fntype = cxx_copy_lang_qualifiers (fntype, memfntype); |
18753 | return fntype; |
18754 | } |
18755 | |
18756 | /* DECL was originally constructed as a non-static member function, |
18757 | but turned out to be static. Update it accordingly. */ |
18758 | |
18759 | void |
18760 | revert_static_member_fn (tree decl) |
18761 | { |
18762 | tree stype = static_fn_type (memfntype: decl); |
18763 | cp_cv_quals quals = type_memfn_quals (stype); |
18764 | cp_ref_qualifier rqual = type_memfn_rqual (stype); |
18765 | |
18766 | if (quals != TYPE_UNQUALIFIED || rqual != REF_QUAL_NONE) |
18767 | stype = apply_memfn_quals (stype, TYPE_UNQUALIFIED, REF_QUAL_NONE); |
18768 | |
18769 | TREE_TYPE (decl) = stype; |
18770 | |
18771 | if (DECL_ARGUMENTS (decl)) |
18772 | DECL_ARGUMENTS (decl) = DECL_CHAIN (DECL_ARGUMENTS (decl)); |
18773 | DECL_STATIC_FUNCTION_P (decl) = 1; |
18774 | } |
18775 | |
18776 | /* Return which tree structure is used by T, or TS_CP_GENERIC if T is |
18777 | one of the language-independent trees. */ |
18778 | |
18779 | enum cp_tree_node_structure_enum |
18780 | cp_tree_node_structure (union lang_tree_node * t) |
18781 | { |
18782 | switch (TREE_CODE (&t->generic)) |
18783 | { |
18784 | case ARGUMENT_PACK_SELECT: return TS_CP_ARGUMENT_PACK_SELECT; |
18785 | case BASELINK: return TS_CP_BASELINK; |
18786 | case CONSTRAINT_INFO: return TS_CP_CONSTRAINT_INFO; |
18787 | case DEFERRED_NOEXCEPT: return TS_CP_DEFERRED_NOEXCEPT; |
18788 | case DEFERRED_PARSE: return TS_CP_DEFERRED_PARSE; |
18789 | case IDENTIFIER_NODE: return TS_CP_IDENTIFIER; |
18790 | case LAMBDA_EXPR: return TS_CP_LAMBDA_EXPR; |
18791 | case BINDING_VECTOR: return TS_CP_BINDING_VECTOR; |
18792 | case OVERLOAD: return TS_CP_OVERLOAD; |
18793 | case PTRMEM_CST: return TS_CP_PTRMEM; |
18794 | case STATIC_ASSERT: return TS_CP_STATIC_ASSERT; |
18795 | case TEMPLATE_DECL: return TS_CP_TEMPLATE_DECL; |
18796 | case TEMPLATE_INFO: return TS_CP_TEMPLATE_INFO; |
18797 | case TEMPLATE_PARM_INDEX: return TS_CP_TPI; |
18798 | case TRAIT_EXPR: return TS_CP_TRAIT_EXPR; |
18799 | case USERDEF_LITERAL: return TS_CP_USERDEF_LITERAL; |
18800 | default: return TS_CP_GENERIC; |
18801 | } |
18802 | } |
18803 | |
18804 | bool |
18805 | cp_missing_noreturn_ok_p (tree decl) |
18806 | { |
18807 | /* A missing noreturn is ok for the `main' function. */ |
18808 | return DECL_MAIN_P (decl); |
18809 | } |
18810 | |
18811 | /* Return the decl used to identify the COMDAT group into which DECL should |
18812 | be placed. */ |
18813 | |
18814 | tree |
18815 | cxx_comdat_group (tree decl) |
18816 | { |
18817 | /* Virtual tables, construction virtual tables, and virtual table |
18818 | tables all go in a single COMDAT group, named after the primary |
18819 | virtual table. */ |
18820 | if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl)) |
18821 | decl = CLASSTYPE_VTABLES (DECL_CONTEXT (decl)); |
18822 | /* For all other DECLs, the COMDAT group is the mangled name of the |
18823 | declaration itself. */ |
18824 | else |
18825 | { |
18826 | while (DECL_THUNK_P (decl)) |
18827 | { |
18828 | /* If TARGET_USE_LOCAL_THUNK_ALIAS_P, use_thunk puts the thunk |
18829 | into the same section as the target function. In that case |
18830 | we must return target's name. */ |
18831 | tree target = THUNK_TARGET (decl); |
18832 | if (TARGET_USE_LOCAL_THUNK_ALIAS_P (target) |
18833 | && DECL_SECTION_NAME (target) != NULL |
18834 | && DECL_ONE_ONLY (target)) |
18835 | decl = target; |
18836 | else |
18837 | break; |
18838 | } |
18839 | } |
18840 | |
18841 | return decl; |
18842 | } |
18843 | |
18844 | /* Returns the return type for FN as written by the user, which may include |
18845 | a placeholder for a deduced return type. */ |
18846 | |
18847 | tree |
18848 | fndecl_declared_return_type (tree fn) |
18849 | { |
18850 | fn = STRIP_TEMPLATE (fn); |
18851 | if (FNDECL_USED_AUTO (fn)) |
18852 | return DECL_SAVED_AUTO_RETURN_TYPE (fn); |
18853 | |
18854 | return TREE_TYPE (TREE_TYPE (fn)); |
18855 | } |
18856 | |
18857 | /* Returns true iff DECL is a variable or function declared with an auto type |
18858 | that has not yet been deduced to a real type. */ |
18859 | |
18860 | bool |
18861 | undeduced_auto_decl (tree decl) |
18862 | { |
18863 | if (cxx_dialect < cxx11) |
18864 | return false; |
18865 | STRIP_ANY_LOCATION_WRAPPER (decl); |
18866 | return ((VAR_OR_FUNCTION_DECL_P (decl) |
18867 | || TREE_CODE (decl) == TEMPLATE_DECL) |
18868 | && type_uses_auto (TREE_TYPE (decl))); |
18869 | } |
18870 | |
18871 | /* Complain if DECL has an undeduced return type. */ |
18872 | |
18873 | bool |
18874 | require_deduced_type (tree decl, tsubst_flags_t complain) |
18875 | { |
18876 | if (undeduced_auto_decl (decl)) |
18877 | { |
18878 | if (TREE_CODE (decl) == FUNCTION_DECL |
18879 | && fndecl_built_in_p (node: decl, klass: BUILT_IN_FRONTEND) |
18880 | && DECL_FE_FUNCTION_CODE (decl) == CP_BUILT_IN_SOURCE_LOCATION) |
18881 | { |
18882 | /* Set the return type of __builtin_source_location. */ |
18883 | tree type = get_source_location_impl_type (); |
18884 | if (type == error_mark_node) |
18885 | { |
18886 | inform (input_location, "using %qs" , "__builtin_source_location" ); |
18887 | return false; |
18888 | } |
18889 | type = cp_build_qualified_type (type, TYPE_QUAL_CONST); |
18890 | type = build_pointer_type (type); |
18891 | apply_deduced_return_type (decl, type); |
18892 | return true; |
18893 | } |
18894 | |
18895 | if (warning_suppressed_p (decl) && seen_error ()) |
18896 | /* We probably already complained about deduction failure. */; |
18897 | else if (complain & tf_error) |
18898 | error ("use of %qD before deduction of %<auto%>" , decl); |
18899 | note_failed_type_completion_for_satisfaction (decl); |
18900 | return false; |
18901 | } |
18902 | return true; |
18903 | } |
18904 | |
18905 | /* Create a representation of the explicit-specifier with |
18906 | constant-expression of EXPR. COMPLAIN is as for tsubst. */ |
18907 | |
18908 | tree |
18909 | build_explicit_specifier (tree expr, tsubst_flags_t complain) |
18910 | { |
18911 | if (check_for_bare_parameter_packs (expr)) |
18912 | return error_mark_node; |
18913 | |
18914 | if (instantiation_dependent_expression_p (expr)) |
18915 | /* Wait for instantiation, tsubst_function_decl will handle it. */ |
18916 | return expr; |
18917 | |
18918 | expr = build_converted_constant_bool_expr (expr, complain); |
18919 | expr = instantiate_non_dependent_expr (expr, complain); |
18920 | expr = cxx_constant_value (t: expr, complain); |
18921 | return expr; |
18922 | } |
18923 | |
18924 | #include "gt-cp-decl.h" |
18925 | |