Make more use of subreg_size_lowpart_offset
[official-gcc.git] / gcc / cp / decl.c
bloba3cc80cf7a30f5e95e5cde876a114131da3d6511
1 /* Process declarations and variables for C++ compiler.
2 Copyright (C) 1988-2017 Free Software Foundation, Inc.
3 Contributed by Michael Tiemann (tiemann@cygnus.com)
5 This file is part of GCC.
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.
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.
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/>. */
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. */
26 /* ??? not all decl nodes are given the most useful possible
27 line numbers. For example, the CONST_DECLs for enum values. */
29 #include "config.h"
30 #include "system.h"
31 #include "coretypes.h"
32 #include "target.h"
33 #include "c-family/c-target.h"
34 #include "cp-tree.h"
35 #include "timevar.h"
36 #include "stringpool.h"
37 #include "cgraph.h"
38 #include "stor-layout.h"
39 #include "varasm.h"
40 #include "attribs.h"
41 #include "flags.h"
42 #include "tree-iterator.h"
43 #include "decl.h"
44 #include "intl.h"
45 #include "toplev.h"
46 #include "c-family/c-objc.h"
47 #include "c-family/c-pragma.h"
48 #include "c-family/c-ubsan.h"
49 #include "debug.h"
50 #include "plugin.h"
51 #include "cilk.h"
52 #include "builtins.h"
53 #include "gimplify.h"
54 #include "asan.h"
56 /* Possible cases of bad specifiers type used by bad_specifiers. */
57 enum bad_spec_place {
58 BSP_VAR, /* variable */
59 BSP_PARM, /* parameter */
60 BSP_TYPE, /* type */
61 BSP_FIELD /* field */
64 static const char *redeclaration_error_message (tree, tree);
66 static int decl_jump_unsafe (tree);
67 static void require_complete_types_for_parms (tree);
68 static bool ambi_op_p (enum tree_code);
69 static bool unary_op_p (enum tree_code);
70 static void push_local_name (tree);
71 static tree grok_reference_init (tree, tree, tree, int);
72 static tree grokvardecl (tree, tree, tree, const cp_decl_specifier_seq *,
73 int, int, int, bool, int, tree);
74 static int check_static_variable_definition (tree, tree);
75 static void record_unknown_type (tree, const char *);
76 static tree builtin_function_1 (tree, tree, bool);
77 static int member_function_or_else (tree, tree, enum overload_flags);
78 static void check_for_uninitialized_const_var (tree);
79 static tree local_variable_p_walkfn (tree *, int *, void *);
80 static const char *tag_name (enum tag_types);
81 static tree lookup_and_check_tag (enum tag_types, tree, tag_scope, bool);
82 static void maybe_deduce_size_from_array_init (tree, tree);
83 static void layout_var_decl (tree);
84 static tree check_initializer (tree, tree, int, vec<tree, va_gc> **);
85 static void make_rtl_for_nonlocal_decl (tree, tree, const char *);
86 static void save_function_data (tree);
87 static void copy_type_enum (tree , tree);
88 static void check_function_type (tree, tree);
89 static void finish_constructor_body (void);
90 static void begin_destructor_body (void);
91 static void finish_destructor_body (void);
92 static void record_key_method_defined (tree);
93 static tree create_array_type_for_decl (tree, tree, tree);
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);
108 /* The following symbols are subsumed in the cp_global_trees array, and
109 listed here individually for documentation purposes.
111 C++ extensions
112 tree wchar_decl_node;
114 tree vtable_entry_type;
115 tree delta_type_node;
116 tree __t_desc_type_node;
118 tree class_type_node;
119 tree unknown_type_node;
121 Array type `vtable_entry_type[]'
123 tree vtbl_type_node;
124 tree vtbl_ptr_type_node;
126 Namespaces,
128 tree std_node;
129 tree abi_node;
131 A FUNCTION_DECL which can call `abort'. Not necessarily the
132 one that the user will declare, but sufficient to be called
133 by routines that want to abort the program.
135 tree abort_fndecl;
137 Used by RTTI
138 tree type_info_type_node, tinfo_decl_id, tinfo_decl_type;
139 tree tinfo_var_id; */
141 tree cp_global_trees[CPTI_MAX];
143 #define local_names cp_function_chain->x_local_names
145 /* A list of objects which have constructors or destructors
146 which reside in the global scope. The decl is stored in
147 the TREE_VALUE slot and the initializer is stored
148 in the TREE_PURPOSE slot. */
149 tree static_aggregates;
151 /* Like static_aggregates, but for thread_local variables. */
152 tree tls_aggregates;
154 /* -- end of C++ */
156 /* A node for the integer constant 2. */
158 tree integer_two_node;
160 /* vector of static decls. */
161 vec<tree, va_gc> *static_decls;
163 /* vector of keyed classes. */
164 vec<tree, va_gc> *keyed_classes;
166 /* Used only for jumps to as-yet undefined labels, since jumps to
167 defined labels can have their validity checked immediately. */
169 struct GTY((chain_next ("%h.next"))) named_label_use_entry {
170 struct named_label_use_entry *next;
171 /* The binding level to which this entry is *currently* attached.
172 This is initially the binding level in which the goto appeared,
173 but is modified as scopes are closed. */
174 cp_binding_level *binding_level;
175 /* The head of the names list that was current when the goto appeared,
176 or the inner scope popped. These are the decls that will *not* be
177 skipped when jumping to the label. */
178 tree names_in_scope;
179 /* The location of the goto, for error reporting. */
180 location_t o_goto_locus;
181 /* True if an OpenMP structured block scope has been closed since
182 the goto appeared. This means that the branch from the label will
183 illegally exit an OpenMP scope. */
184 bool in_omp_scope;
187 /* A list of all LABEL_DECLs in the function that have names. Here so
188 we can clear out their names' definitions at the end of the
189 function, and so we can check the validity of jumps to these labels. */
191 struct GTY((for_user)) named_label_entry {
192 /* The decl itself. */
193 tree label_decl;
195 /* The binding level to which the label is *currently* attached.
196 This is initially set to the binding level in which the label
197 is defined, but is modified as scopes are closed. */
198 cp_binding_level *binding_level;
199 /* The head of the names list that was current when the label was
200 defined, or the inner scope popped. These are the decls that will
201 be skipped when jumping to the label. */
202 tree names_in_scope;
203 /* A vector of all decls from all binding levels that would be
204 crossed by a backward branch to the label. */
205 vec<tree, va_gc> *bad_decls;
207 /* A list of uses of the label, before the label is defined. */
208 struct named_label_use_entry *uses;
210 /* The following bits are set after the label is defined, and are
211 updated as scopes are popped. They indicate that a backward jump
212 to the label will illegally enter a scope of the given flavor. */
213 bool in_try_scope;
214 bool in_catch_scope;
215 bool in_omp_scope;
216 bool in_transaction_scope;
217 bool in_constexpr_if;
220 #define named_labels cp_function_chain->x_named_labels
222 /* The number of function bodies which we are currently processing.
223 (Zero if we are at namespace scope, one inside the body of a
224 function, two inside the body of a function in a local class, etc.) */
225 int function_depth;
227 /* Whether the exception-specifier is part of a function type (i.e. C++17). */
228 bool flag_noexcept_type;
230 /* States indicating how grokdeclarator() should handle declspecs marked
231 with __attribute__((deprecated)). An object declared as
232 __attribute__((deprecated)) suppresses warnings of uses of other
233 deprecated items. */
234 enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
237 /* A list of VAR_DECLs whose type was incomplete at the time the
238 variable was declared. */
240 struct GTY(()) incomplete_var {
241 tree decl;
242 tree incomplete_type;
246 static GTY(()) vec<incomplete_var, va_gc> *incomplete_vars;
248 /* Returns the kind of template specialization we are currently
249 processing, given that it's declaration contained N_CLASS_SCOPES
250 explicit scope qualifications. */
252 tmpl_spec_kind
253 current_tmpl_spec_kind (int n_class_scopes)
255 int n_template_parm_scopes = 0;
256 int seen_specialization_p = 0;
257 int innermost_specialization_p = 0;
258 cp_binding_level *b;
260 /* Scan through the template parameter scopes. */
261 for (b = current_binding_level;
262 b->kind == sk_template_parms;
263 b = b->level_chain)
265 /* If we see a specialization scope inside a parameter scope,
266 then something is wrong. That corresponds to a declaration
267 like:
269 template <class T> template <> ...
271 which is always invalid since [temp.expl.spec] forbids the
272 specialization of a class member template if the enclosing
273 class templates are not explicitly specialized as well. */
274 if (b->explicit_spec_p)
276 if (n_template_parm_scopes == 0)
277 innermost_specialization_p = 1;
278 else
279 seen_specialization_p = 1;
281 else if (seen_specialization_p == 1)
282 return tsk_invalid_member_spec;
284 ++n_template_parm_scopes;
287 /* Handle explicit instantiations. */
288 if (processing_explicit_instantiation)
290 if (n_template_parm_scopes != 0)
291 /* We've seen a template parameter list during an explicit
292 instantiation. For example:
294 template <class T> template void f(int);
296 This is erroneous. */
297 return tsk_invalid_expl_inst;
298 else
299 return tsk_expl_inst;
302 if (n_template_parm_scopes < n_class_scopes)
303 /* We've not seen enough template headers to match all the
304 specialized classes present. For example:
306 template <class T> void R<T>::S<T>::f(int);
308 This is invalid; there needs to be one set of template
309 parameters for each class. */
310 return tsk_insufficient_parms;
311 else if (n_template_parm_scopes == n_class_scopes)
312 /* We're processing a non-template declaration (even though it may
313 be a member of a template class.) For example:
315 template <class T> void S<T>::f(int);
317 The `class T' matches the `S<T>', leaving no template headers
318 corresponding to the `f'. */
319 return tsk_none;
320 else if (n_template_parm_scopes > n_class_scopes + 1)
321 /* We've got too many template headers. For example:
323 template <> template <class T> void f (T);
325 There need to be more enclosing classes. */
326 return tsk_excessive_parms;
327 else
328 /* This must be a template. It's of the form:
330 template <class T> template <class U> void S<T>::f(U);
332 This is a specialization if the innermost level was a
333 specialization; otherwise it's just a definition of the
334 template. */
335 return innermost_specialization_p ? tsk_expl_spec : tsk_template;
338 /* Exit the current scope. */
340 void
341 finish_scope (void)
343 poplevel (0, 0, 0);
346 /* When a label goes out of scope, check to see if that label was used
347 in a valid manner, and issue any appropriate warnings or errors. */
349 static void
350 pop_label (tree label, tree old_value)
352 if (!processing_template_decl)
354 if (DECL_INITIAL (label) == NULL_TREE)
356 location_t location;
358 error ("label %q+D used but not defined", label);
359 location = input_location;
360 /* FIXME want (LOCATION_FILE (input_location), (line)0) */
361 /* Avoid crashing later. */
362 define_label (location, DECL_NAME (label));
364 else
365 warn_for_unused_label (label);
368 SET_IDENTIFIER_LABEL_VALUE (DECL_NAME (label), old_value);
371 /* Push all named labels into a vector, so that we can sort it on DECL_UID
372 to avoid code generation differences. */
375 note_label (named_label_entry **slot, vec<named_label_entry **> &labels)
377 labels.quick_push (slot);
378 return 1;
381 /* Helper function to sort named label entries in a vector by DECL_UID. */
383 static int
384 sort_labels (const void *a, const void *b)
386 named_label_entry **slot1 = *(named_label_entry **const *) a;
387 named_label_entry **slot2 = *(named_label_entry **const *) b;
388 if (DECL_UID ((*slot1)->label_decl) < DECL_UID ((*slot2)->label_decl))
389 return -1;
390 if (DECL_UID ((*slot1)->label_decl) > DECL_UID ((*slot2)->label_decl))
391 return 1;
392 return 0;
395 /* At the end of a function, all labels declared within the function
396 go out of scope. BLOCK is the top-level block for the
397 function. */
399 static void
400 pop_labels (tree block)
402 if (named_labels)
404 auto_vec<named_label_entry **, 32> labels;
405 named_label_entry **slot;
406 unsigned int i;
408 /* Push all the labels into a vector and sort them by DECL_UID,
409 so that gaps between DECL_UIDs don't affect code generation. */
410 labels.reserve_exact (named_labels->elements ());
411 named_labels->traverse<vec<named_label_entry **> &, note_label> (labels);
412 labels.qsort (sort_labels);
413 FOR_EACH_VEC_ELT (labels, i, slot)
415 struct named_label_entry *ent = *slot;
417 pop_label (ent->label_decl, NULL_TREE);
419 /* Put the labels into the "variables" of the top-level block,
420 so debugger can see them. */
421 DECL_CHAIN (ent->label_decl) = BLOCK_VARS (block);
422 BLOCK_VARS (block) = ent->label_decl;
424 named_labels->clear_slot (slot);
426 named_labels = NULL;
430 /* At the end of a block with local labels, restore the outer definition. */
432 static void
433 pop_local_label (tree label, tree old_value)
435 struct named_label_entry dummy;
437 pop_label (label, old_value);
439 dummy.label_decl = label;
440 named_label_entry **slot = named_labels->find_slot (&dummy, NO_INSERT);
441 named_labels->clear_slot (slot);
444 /* The following two routines are used to interface to Objective-C++.
445 The binding level is purposely treated as an opaque type. */
447 void *
448 objc_get_current_scope (void)
450 return current_binding_level;
453 /* The following routine is used by the NeXT-style SJLJ exceptions;
454 variables get marked 'volatile' so as to not be clobbered by
455 _setjmp()/_longjmp() calls. All variables in the current scope,
456 as well as parent scopes up to (but not including) ENCLOSING_BLK
457 shall be thusly marked. */
459 void
460 objc_mark_locals_volatile (void *enclosing_blk)
462 cp_binding_level *scope;
464 for (scope = current_binding_level;
465 scope && scope != enclosing_blk;
466 scope = scope->level_chain)
468 tree decl;
470 for (decl = scope->names; decl; decl = TREE_CHAIN (decl))
471 objc_volatilize_decl (decl);
473 /* Do not climb up past the current function. */
474 if (scope->kind == sk_function_parms)
475 break;
479 /* True if B is the level for the condition of a constexpr if. */
481 static bool
482 level_for_constexpr_if (cp_binding_level *b)
484 return (b->kind == sk_cond && b->this_entity
485 && TREE_CODE (b->this_entity) == IF_STMT
486 && IF_STMT_CONSTEXPR_P (b->this_entity));
489 /* Update data for defined and undefined labels when leaving a scope. */
492 poplevel_named_label_1 (named_label_entry **slot, cp_binding_level *bl)
494 named_label_entry *ent = *slot;
495 cp_binding_level *obl = bl->level_chain;
497 if (ent->binding_level == bl)
499 tree decl;
501 /* ENT->NAMES_IN_SCOPE may contain a mixture of DECLs and
502 TREE_LISTs representing OVERLOADs, so be careful. */
503 for (decl = ent->names_in_scope; decl; decl = (DECL_P (decl)
504 ? DECL_CHAIN (decl)
505 : TREE_CHAIN (decl)))
506 if (decl_jump_unsafe (decl))
507 vec_safe_push (ent->bad_decls, decl);
509 ent->binding_level = obl;
510 ent->names_in_scope = obl->names;
511 switch (bl->kind)
513 case sk_try:
514 ent->in_try_scope = true;
515 break;
516 case sk_catch:
517 ent->in_catch_scope = true;
518 break;
519 case sk_omp:
520 ent->in_omp_scope = true;
521 break;
522 case sk_transaction:
523 ent->in_transaction_scope = true;
524 break;
525 case sk_block:
526 if (level_for_constexpr_if (bl->level_chain))
527 ent->in_constexpr_if = true;
528 break;
529 default:
530 break;
533 else if (ent->uses)
535 struct named_label_use_entry *use;
537 for (use = ent->uses; use ; use = use->next)
538 if (use->binding_level == bl)
540 use->binding_level = obl;
541 use->names_in_scope = obl->names;
542 if (bl->kind == sk_omp)
543 use->in_omp_scope = true;
547 return 1;
550 /* Saved errorcount to avoid -Wunused-but-set-{parameter,variable} warnings
551 when errors were reported, except for -Werror-unused-but-set-*. */
552 static int unused_but_set_errorcount;
554 /* Exit a binding level.
555 Pop the level off, and restore the state of the identifier-decl mappings
556 that were in effect when this level was entered.
558 If KEEP == 1, this level had explicit declarations, so
559 and create a "block" (a BLOCK node) for the level
560 to record its declarations and subblocks for symbol table output.
562 If FUNCTIONBODY is nonzero, this level is the body of a function,
563 so create a block as if KEEP were set and also clear out all
564 label names.
566 If REVERSE is nonzero, reverse the order of decls before putting
567 them into the BLOCK. */
569 tree
570 poplevel (int keep, int reverse, int functionbody)
572 tree link;
573 /* The chain of decls was accumulated in reverse order.
574 Put it into forward order, just for cleanliness. */
575 tree decls;
576 tree subblocks;
577 tree block;
578 tree decl;
579 int leaving_for_scope;
580 scope_kind kind;
581 unsigned ix;
582 cp_label_binding *label_bind;
584 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
585 restart:
587 block = NULL_TREE;
589 gcc_assert (current_binding_level->kind != sk_class
590 && current_binding_level->kind != sk_namespace);
592 if (current_binding_level->kind == sk_cleanup)
593 functionbody = 0;
594 subblocks = functionbody >= 0 ? current_binding_level->blocks : 0;
596 gcc_assert (!vec_safe_length (current_binding_level->class_shadowed));
598 /* We used to use KEEP == 2 to indicate that the new block should go
599 at the beginning of the list of blocks at this binding level,
600 rather than the end. This hack is no longer used. */
601 gcc_assert (keep == 0 || keep == 1);
603 if (current_binding_level->keep)
604 keep = 1;
606 /* Any uses of undefined labels, and any defined labels, now operate
607 under constraints of next binding contour. */
608 if (cfun && !functionbody && named_labels)
609 named_labels->traverse<cp_binding_level *, poplevel_named_label_1>
610 (current_binding_level);
612 /* Get the decls in the order they were written.
613 Usually current_binding_level->names is in reverse order.
614 But parameter decls were previously put in forward order. */
616 if (reverse)
617 current_binding_level->names
618 = decls = nreverse (current_binding_level->names);
619 else
620 decls = current_binding_level->names;
622 /* If there were any declarations or structure tags in that level,
623 or if this level is a function body,
624 create a BLOCK to record them for the life of this function. */
625 block = NULL_TREE;
626 /* Avoid function body block if possible. */
627 if (functionbody && subblocks && BLOCK_CHAIN (subblocks) == NULL_TREE)
628 keep = 0;
629 else if (keep == 1 || functionbody)
630 block = make_node (BLOCK);
631 if (block != NULL_TREE)
633 BLOCK_VARS (block) = decls;
634 BLOCK_SUBBLOCKS (block) = subblocks;
637 /* In each subblock, record that this is its superior. */
638 if (keep >= 0)
639 for (link = subblocks; link; link = BLOCK_CHAIN (link))
640 BLOCK_SUPERCONTEXT (link) = block;
642 /* We still support the old for-scope rules, whereby the variables
643 in a init statement were in scope after the for-statement ended.
644 We only use the new rules if flag_new_for_scope is nonzero. */
645 leaving_for_scope
646 = current_binding_level->kind == sk_for && flag_new_for_scope == 1;
648 /* Before we remove the declarations first check for unused variables. */
649 if ((warn_unused_variable || warn_unused_but_set_variable)
650 && current_binding_level->kind != sk_template_parms
651 && !processing_template_decl)
652 for (tree d = get_local_decls (); d; d = TREE_CHAIN (d))
654 /* There are cases where D itself is a TREE_LIST. See in
655 push_local_binding where the list of decls returned by
656 getdecls is built. */
657 decl = TREE_CODE (d) == TREE_LIST ? TREE_VALUE (d) : d;
659 tree type = TREE_TYPE (decl);
660 if (VAR_P (decl)
661 && (! TREE_USED (decl) || !DECL_READ_P (decl))
662 && ! DECL_IN_SYSTEM_HEADER (decl)
663 /* For structured bindings, consider only real variables, not
664 subobjects. */
665 && (DECL_DECOMPOSITION_P (decl) ? !DECL_DECOMP_BASE (decl)
666 : (DECL_NAME (decl) && !DECL_ARTIFICIAL (decl)))
667 && type != error_mark_node
668 && (!CLASS_TYPE_P (type)
669 || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
670 || lookup_attribute ("warn_unused",
671 TYPE_ATTRIBUTES (TREE_TYPE (decl)))))
673 if (! TREE_USED (decl))
675 if (!DECL_NAME (decl) && DECL_DECOMPOSITION_P (decl))
676 warning_at (DECL_SOURCE_LOCATION (decl),
677 OPT_Wunused_variable,
678 "unused structured binding declaration");
679 else
680 warning_at (DECL_SOURCE_LOCATION (decl),
681 OPT_Wunused_variable, "unused variable %qD", decl);
683 else if (DECL_CONTEXT (decl) == current_function_decl
684 // For -Wunused-but-set-variable leave references alone.
685 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
686 && errorcount == unused_but_set_errorcount)
688 if (!DECL_NAME (decl) && DECL_DECOMPOSITION_P (decl))
689 warning_at (DECL_SOURCE_LOCATION (decl),
690 OPT_Wunused_but_set_variable, "structured "
691 "binding declaration set but not used");
692 else
693 warning_at (DECL_SOURCE_LOCATION (decl),
694 OPT_Wunused_but_set_variable,
695 "variable %qD set but not used", decl);
696 unused_but_set_errorcount = errorcount;
701 /* Remove declarations for all the DECLs in this level. */
702 for (link = decls; link; link = TREE_CHAIN (link))
704 decl = TREE_CODE (link) == TREE_LIST ? TREE_VALUE (link) : link;
705 tree name = OVL_NAME (decl);
707 if (leaving_for_scope && VAR_P (decl)
708 /* It's hard to make this ARM compatibility hack play nicely with
709 lambdas, and it really isn't necessary in C++11 mode. */
710 && cxx_dialect < cxx11
711 && name)
713 cxx_binding *ob = outer_binding (name,
714 IDENTIFIER_BINDING (name),
715 /*class_p=*/true);
716 tree ns_binding = NULL_TREE;
717 if (!ob)
718 ns_binding = get_namespace_binding (current_namespace, name);
720 if (ob && ob->scope == current_binding_level->level_chain)
721 /* We have something like:
723 int i;
724 for (int i; ;);
726 and we are leaving the `for' scope. There's no reason to
727 keep the binding of the inner `i' in this case. */
729 else if ((ob && (TREE_CODE (ob->value) == TYPE_DECL))
730 || (ns_binding && TREE_CODE (ns_binding) == TYPE_DECL))
731 /* Here, we have something like:
733 typedef int I;
735 void f () {
736 for (int I; ;);
739 We must pop the for-scope binding so we know what's a
740 type and what isn't. */
742 else
744 /* Mark this VAR_DECL as dead so that we can tell we left it
745 there only for backward compatibility. */
746 DECL_DEAD_FOR_LOCAL (link) = 1;
748 /* Keep track of what should have happened when we
749 popped the binding. */
750 if (ob && ob->value)
752 SET_DECL_SHADOWED_FOR_VAR (link, ob->value);
753 DECL_HAS_SHADOWED_FOR_VAR_P (link) = 1;
756 /* Add it to the list of dead variables in the next
757 outermost binding to that we can remove these when we
758 leave that binding. */
759 vec_safe_push (
760 current_binding_level->level_chain->dead_vars_from_for,
761 link);
763 /* Although we don't pop the cxx_binding, we do clear
764 its SCOPE since the scope is going away now. */
765 IDENTIFIER_BINDING (name)->scope
766 = current_binding_level->level_chain;
768 /* Don't remove the binding. */
769 name = NULL_TREE;
772 /* Remove the binding. */
773 pop_local_binding (name, decl);
776 /* Remove declarations for any `for' variables from inner scopes
777 that we kept around. */
778 FOR_EACH_VEC_SAFE_ELT_REVERSE (current_binding_level->dead_vars_from_for,
779 ix, decl)
780 pop_local_binding (DECL_NAME (decl), decl);
782 /* Restore the IDENTIFIER_TYPE_VALUEs. */
783 for (link = current_binding_level->type_shadowed;
784 link; link = TREE_CHAIN (link))
785 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link), TREE_VALUE (link));
787 /* Restore the IDENTIFIER_LABEL_VALUEs for local labels. */
788 FOR_EACH_VEC_SAFE_ELT_REVERSE (current_binding_level->shadowed_labels,
789 ix, label_bind)
790 pop_local_label (label_bind->label, label_bind->prev_value);
792 /* There may be OVERLOADs (wrapped in TREE_LISTs) on the BLOCK_VARs
793 list if a `using' declaration put them there. The debugging
794 back ends won't understand OVERLOAD, so we remove them here.
795 Because the BLOCK_VARS are (temporarily) shared with
796 CURRENT_BINDING_LEVEL->NAMES we must do this fixup after we have
797 popped all the bindings. Also remove undeduced 'auto' decls,
798 which LTO doesn't understand, and can't have been used by anything. */
799 if (block)
801 tree* d;
803 for (d = &BLOCK_VARS (block); *d; )
805 if (TREE_CODE (*d) == TREE_LIST
806 || (!processing_template_decl
807 && undeduced_auto_decl (*d)))
808 *d = TREE_CHAIN (*d);
809 else
810 d = &DECL_CHAIN (*d);
814 /* If the level being exited is the top level of a function,
815 check over all the labels. */
816 if (functionbody)
818 if (block)
820 /* Since this is the top level block of a function, the vars are
821 the function's parameters. Don't leave them in the BLOCK
822 because they are found in the FUNCTION_DECL instead. */
823 BLOCK_VARS (block) = 0;
824 pop_labels (block);
826 else
827 pop_labels (subblocks);
830 kind = current_binding_level->kind;
831 if (kind == sk_cleanup)
833 tree stmt;
835 /* If this is a temporary binding created for a cleanup, then we'll
836 have pushed a statement list level. Pop that, create a new
837 BIND_EXPR for the block, and insert it into the stream. */
838 stmt = pop_stmt_list (current_binding_level->statement_list);
839 stmt = c_build_bind_expr (input_location, block, stmt);
840 add_stmt (stmt);
843 leave_scope ();
844 if (functionbody)
846 /* The current function is being defined, so its DECL_INITIAL
847 should be error_mark_node. */
848 gcc_assert (DECL_INITIAL (current_function_decl) == error_mark_node);
849 DECL_INITIAL (current_function_decl) = block ? block : subblocks;
850 if (subblocks)
852 if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
854 if (BLOCK_SUBBLOCKS (subblocks))
855 BLOCK_OUTER_CURLY_BRACE_P (BLOCK_SUBBLOCKS (subblocks)) = 1;
857 else
858 BLOCK_OUTER_CURLY_BRACE_P (subblocks) = 1;
861 else if (block)
862 current_binding_level->blocks
863 = block_chainon (current_binding_level->blocks, block);
865 /* If we did not make a block for the level just exited,
866 any blocks made for inner levels
867 (since they cannot be recorded as subblocks in that level)
868 must be carried forward so they will later become subblocks
869 of something else. */
870 else if (subblocks)
871 current_binding_level->blocks
872 = block_chainon (current_binding_level->blocks, subblocks);
874 /* Each and every BLOCK node created here in `poplevel' is important
875 (e.g. for proper debugging information) so if we created one
876 earlier, mark it as "used". */
877 if (block)
878 TREE_USED (block) = 1;
880 /* All temporary bindings created for cleanups are popped silently. */
881 if (kind == sk_cleanup)
882 goto restart;
884 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
885 return block;
888 /* Call wrapup_globals_declarations for the globals in NAMESPACE. */
889 /* Diagnose odr-used extern inline variables without definitions
890 in the current TU. */
893 wrapup_namespace_globals ()
895 if (vec<tree, va_gc> *statics = static_decls)
897 tree decl;
898 unsigned int i;
899 FOR_EACH_VEC_ELT (*statics, i, decl)
901 if (warn_unused_function
902 && TREE_CODE (decl) == FUNCTION_DECL
903 && DECL_INITIAL (decl) == 0
904 && DECL_EXTERNAL (decl)
905 && !TREE_PUBLIC (decl)
906 && !DECL_ARTIFICIAL (decl)
907 && !DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
908 && !TREE_NO_WARNING (decl))
909 warning_at (DECL_SOURCE_LOCATION (decl),
910 OPT_Wunused_function,
911 "%qF declared %<static%> but never defined", decl);
913 if (VAR_P (decl)
914 && DECL_EXTERNAL (decl)
915 && DECL_INLINE_VAR_P (decl)
916 && DECL_ODR_USED (decl))
917 error_at (DECL_SOURCE_LOCATION (decl),
918 "odr-used inline variable %qD is not defined", decl);
921 /* Clear out the list, so we don't rescan next time. */
922 static_decls = NULL;
924 /* Write out any globals that need to be output. */
925 return wrapup_global_declarations (statics->address (),
926 statics->length ());
928 return 0;
931 /* In C++, you don't have to write `struct S' to refer to `S'; you
932 can just use `S'. We accomplish this by creating a TYPE_DECL as
933 if the user had written `typedef struct S S'. Create and return
934 the TYPE_DECL for TYPE. */
936 tree
937 create_implicit_typedef (tree name, tree type)
939 tree decl;
941 decl = build_decl (input_location, TYPE_DECL, name, type);
942 DECL_ARTIFICIAL (decl) = 1;
943 /* There are other implicit type declarations, like the one *within*
944 a class that allows you to write `S::S'. We must distinguish
945 amongst these. */
946 SET_DECL_IMPLICIT_TYPEDEF_P (decl);
947 TYPE_NAME (type) = decl;
948 TYPE_STUB_DECL (type) = decl;
950 return decl;
953 /* Remember a local name for name-mangling purposes. */
955 static void
956 push_local_name (tree decl)
958 size_t i, nelts;
959 tree t, name;
961 timevar_start (TV_NAME_LOOKUP);
963 name = DECL_NAME (decl);
965 nelts = vec_safe_length (local_names);
966 for (i = 0; i < nelts; i++)
968 t = (*local_names)[i];
969 if (DECL_NAME (t) == name)
971 retrofit_lang_decl (decl);
972 DECL_LANG_SPECIFIC (decl)->u.base.u2sel = 1;
973 if (DECL_DISCRIMINATOR_SET_P (t))
974 DECL_DISCRIMINATOR (decl) = DECL_DISCRIMINATOR (t) + 1;
975 else
976 DECL_DISCRIMINATOR (decl) = 1;
978 (*local_names)[i] = decl;
979 timevar_stop (TV_NAME_LOOKUP);
980 return;
984 vec_safe_push (local_names, decl);
985 timevar_stop (TV_NAME_LOOKUP);
988 /* Subroutine of duplicate_decls: return truthvalue of whether
989 or not types of these decls match.
991 For C++, we must compare the parameter list so that `int' can match
992 `int&' in a parameter position, but `int&' is not confused with
993 `const int&'. */
996 decls_match (tree newdecl, tree olddecl)
998 int types_match;
1000 if (newdecl == olddecl)
1001 return 1;
1003 if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
1004 /* If the two DECLs are not even the same kind of thing, we're not
1005 interested in their types. */
1006 return 0;
1008 gcc_assert (DECL_P (newdecl));
1010 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1012 tree f1 = TREE_TYPE (newdecl);
1013 tree f2 = TREE_TYPE (olddecl);
1014 tree p1 = TYPE_ARG_TYPES (f1);
1015 tree p2 = TYPE_ARG_TYPES (f2);
1016 tree r2;
1018 /* Specializations of different templates are different functions
1019 even if they have the same type. */
1020 tree t1 = (DECL_USE_TEMPLATE (newdecl)
1021 ? DECL_TI_TEMPLATE (newdecl)
1022 : NULL_TREE);
1023 tree t2 = (DECL_USE_TEMPLATE (olddecl)
1024 ? DECL_TI_TEMPLATE (olddecl)
1025 : NULL_TREE);
1026 if (t1 != t2)
1027 return 0;
1029 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
1030 && ! (DECL_EXTERN_C_P (newdecl)
1031 && DECL_EXTERN_C_P (olddecl)))
1032 return 0;
1034 /* A new declaration doesn't match a built-in one unless it
1035 is also extern "C". */
1036 if (DECL_IS_BUILTIN (olddecl)
1037 && DECL_EXTERN_C_P (olddecl) && !DECL_EXTERN_C_P (newdecl))
1038 return 0;
1040 if (TREE_CODE (f1) != TREE_CODE (f2))
1041 return 0;
1043 /* A declaration with deduced return type should use its pre-deduction
1044 type for declaration matching. */
1045 r2 = fndecl_declared_return_type (olddecl);
1047 if (same_type_p (TREE_TYPE (f1), r2))
1049 if (!prototype_p (f2) && DECL_EXTERN_C_P (olddecl)
1050 && (DECL_BUILT_IN (olddecl)
1051 #ifndef NO_IMPLICIT_EXTERN_C
1052 || (DECL_IN_SYSTEM_HEADER (newdecl) && !DECL_CLASS_SCOPE_P (newdecl))
1053 || (DECL_IN_SYSTEM_HEADER (olddecl) && !DECL_CLASS_SCOPE_P (olddecl))
1054 #endif
1057 types_match = self_promoting_args_p (p1);
1058 if (p1 == void_list_node)
1059 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
1061 #ifndef NO_IMPLICIT_EXTERN_C
1062 else if (!prototype_p (f1)
1063 && (DECL_EXTERN_C_P (olddecl)
1064 && DECL_IN_SYSTEM_HEADER (olddecl)
1065 && !DECL_CLASS_SCOPE_P (olddecl))
1066 && (DECL_EXTERN_C_P (newdecl)
1067 && DECL_IN_SYSTEM_HEADER (newdecl)
1068 && !DECL_CLASS_SCOPE_P (newdecl)))
1070 types_match = self_promoting_args_p (p2);
1071 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
1073 #endif
1074 else
1075 types_match =
1076 compparms (p1, p2)
1077 && type_memfn_rqual (f1) == type_memfn_rqual (f2)
1078 && (TYPE_ATTRIBUTES (TREE_TYPE (newdecl)) == NULL_TREE
1079 || comp_type_attributes (TREE_TYPE (newdecl),
1080 TREE_TYPE (olddecl)) != 0);
1082 else
1083 types_match = 0;
1085 /* The decls dont match if they correspond to two different versions
1086 of the same function. Disallow extern "C" functions to be
1087 versions for now. */
1088 if (types_match
1089 && !DECL_EXTERN_C_P (newdecl)
1090 && !DECL_EXTERN_C_P (olddecl)
1091 && maybe_version_functions (newdecl, olddecl))
1092 return 0;
1094 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1096 tree oldres = DECL_TEMPLATE_RESULT (olddecl);
1097 tree newres = DECL_TEMPLATE_RESULT (newdecl);
1099 if (TREE_CODE (newres) != TREE_CODE (oldres))
1100 return 0;
1102 if (!comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
1103 DECL_TEMPLATE_PARMS (olddecl)))
1104 return 0;
1106 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
1107 types_match = (same_type_p (TREE_TYPE (oldres), TREE_TYPE (newres))
1108 && equivalently_constrained (olddecl, newdecl));
1109 else
1110 // We don't need to check equivalently_constrained for variable and
1111 // function templates because we check it on the results.
1112 types_match = decls_match (oldres, newres);
1114 else
1116 /* Need to check scope for variable declaration (VAR_DECL).
1117 For typedef (TYPE_DECL), scope is ignored. */
1118 if (VAR_P (newdecl)
1119 && CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
1120 /* [dcl.link]
1121 Two declarations for an object with C language linkage
1122 with the same name (ignoring the namespace that qualify
1123 it) that appear in different namespace scopes refer to
1124 the same object. */
1125 && !(DECL_EXTERN_C_P (olddecl) && DECL_EXTERN_C_P (newdecl)))
1126 return 0;
1128 if (TREE_TYPE (newdecl) == error_mark_node)
1129 types_match = TREE_TYPE (olddecl) == error_mark_node;
1130 else if (TREE_TYPE (olddecl) == NULL_TREE)
1131 types_match = TREE_TYPE (newdecl) == NULL_TREE;
1132 else if (TREE_TYPE (newdecl) == NULL_TREE)
1133 types_match = 0;
1134 else
1135 types_match = comptypes (TREE_TYPE (newdecl),
1136 TREE_TYPE (olddecl),
1137 COMPARE_REDECLARATION);
1140 // Normal functions can be constrained, as can variable partial
1141 // specializations.
1142 if (types_match && VAR_OR_FUNCTION_DECL_P (newdecl))
1143 types_match = equivalently_constrained (newdecl, olddecl);
1145 return types_match;
1148 /* NEWDECL and OLDDECL have identical signatures. If they are
1149 different versions adjust them and return true. */
1151 bool
1152 maybe_version_functions (tree newdecl, tree olddecl)
1154 if (!targetm.target_option.function_versions (newdecl, olddecl))
1155 return false;
1157 bool record = false;
1159 if (!DECL_FUNCTION_VERSIONED (olddecl))
1161 record = true;
1162 DECL_FUNCTION_VERSIONED (olddecl) = 1;
1163 if (DECL_ASSEMBLER_NAME_SET_P (olddecl))
1164 mangle_decl (olddecl);
1167 if (!DECL_FUNCTION_VERSIONED (newdecl))
1169 record = true;
1170 DECL_FUNCTION_VERSIONED (newdecl) = 1;
1171 if (DECL_ASSEMBLER_NAME_SET_P (newdecl))
1172 mangle_decl (newdecl);
1175 /* Only record if at least one was not already versions. */
1176 if (record)
1177 cgraph_node::record_function_versions (olddecl, newdecl);
1179 return true;
1182 /* If NEWDECL is `static' and an `extern' was seen previously,
1183 warn about it. OLDDECL is the previous declaration.
1185 Note that this does not apply to the C++ case of declaring
1186 a variable `extern const' and then later `const'.
1188 Don't complain about built-in functions, since they are beyond
1189 the user's control. */
1191 void
1192 warn_extern_redeclared_static (tree newdecl, tree olddecl)
1194 if (TREE_CODE (newdecl) == TYPE_DECL
1195 || TREE_CODE (newdecl) == TEMPLATE_DECL
1196 || TREE_CODE (newdecl) == CONST_DECL
1197 || TREE_CODE (newdecl) == NAMESPACE_DECL)
1198 return;
1200 /* Don't get confused by static member functions; that's a different
1201 use of `static'. */
1202 if (TREE_CODE (newdecl) == FUNCTION_DECL
1203 && DECL_STATIC_FUNCTION_P (newdecl))
1204 return;
1206 /* If the old declaration was `static', or the new one isn't, then
1207 everything is OK. */
1208 if (DECL_THIS_STATIC (olddecl) || !DECL_THIS_STATIC (newdecl))
1209 return;
1211 /* It's OK to declare a builtin function as `static'. */
1212 if (TREE_CODE (olddecl) == FUNCTION_DECL
1213 && DECL_ARTIFICIAL (olddecl))
1214 return;
1216 if (permerror (DECL_SOURCE_LOCATION (newdecl),
1217 "%qD was declared %<extern%> and later %<static%>", newdecl))
1218 inform (DECL_SOURCE_LOCATION (olddecl),
1219 "previous declaration of %qD", olddecl);
1222 /* NEW_DECL is a redeclaration of OLD_DECL; both are functions or
1223 function templates. If their exception specifications do not
1224 match, issue a diagnostic. */
1226 static void
1227 check_redeclaration_exception_specification (tree new_decl,
1228 tree old_decl)
1230 tree new_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (new_decl));
1231 tree old_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old_decl));
1233 /* Two default specs are equivalent, don't force evaluation. */
1234 if (UNEVALUATED_NOEXCEPT_SPEC_P (new_exceptions)
1235 && UNEVALUATED_NOEXCEPT_SPEC_P (old_exceptions))
1236 return;
1238 maybe_instantiate_noexcept (new_decl);
1239 maybe_instantiate_noexcept (old_decl);
1240 new_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (new_decl));
1241 old_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old_decl));
1243 /* [except.spec]
1245 If any declaration of a function has an exception-specification,
1246 all declarations, including the definition and an explicit
1247 specialization, of that function shall have an
1248 exception-specification with the same set of type-ids. */
1249 if (! DECL_IS_BUILTIN (old_decl)
1250 && !comp_except_specs (new_exceptions, old_exceptions, ce_normal))
1252 const char *const msg
1253 = G_("declaration of %qF has a different exception specifier");
1254 bool complained = true;
1255 location_t new_loc = DECL_SOURCE_LOCATION (new_decl);
1256 if (DECL_IN_SYSTEM_HEADER (old_decl))
1257 complained = pedwarn (new_loc, OPT_Wsystem_headers, msg, new_decl);
1258 else if (!flag_exceptions)
1259 /* We used to silently permit mismatched eh specs with
1260 -fno-exceptions, so make them a pedwarn now. */
1261 complained = pedwarn (new_loc, OPT_Wpedantic, msg, new_decl);
1262 else
1263 error_at (new_loc, msg, new_decl);
1264 if (complained)
1265 inform (DECL_SOURCE_LOCATION (old_decl),
1266 "from previous declaration %qF", old_decl);
1270 /* Return true if OLD_DECL and NEW_DECL agree on constexprness.
1271 Otherwise issue diagnostics. */
1273 static bool
1274 validate_constexpr_redeclaration (tree old_decl, tree new_decl)
1276 old_decl = STRIP_TEMPLATE (old_decl);
1277 new_decl = STRIP_TEMPLATE (new_decl);
1278 if (!VAR_OR_FUNCTION_DECL_P (old_decl)
1279 || !VAR_OR_FUNCTION_DECL_P (new_decl))
1280 return true;
1281 if (DECL_DECLARED_CONSTEXPR_P (old_decl)
1282 == DECL_DECLARED_CONSTEXPR_P (new_decl))
1283 return true;
1284 if (TREE_CODE (old_decl) == FUNCTION_DECL)
1286 if (DECL_BUILT_IN (old_decl))
1288 /* Hide a built-in declaration. */
1289 DECL_DECLARED_CONSTEXPR_P (old_decl)
1290 = DECL_DECLARED_CONSTEXPR_P (new_decl);
1291 return true;
1293 /* 7.1.5 [dcl.constexpr]
1294 Note: An explicit specialization can differ from the template
1295 declaration with respect to the constexpr specifier. */
1296 if (! DECL_TEMPLATE_SPECIALIZATION (old_decl)
1297 && DECL_TEMPLATE_SPECIALIZATION (new_decl))
1298 return true;
1300 error_at (DECL_SOURCE_LOCATION (new_decl),
1301 "redeclaration %qD differs in %<constexpr%> "
1302 "from previous declaration", new_decl);
1303 inform (DECL_SOURCE_LOCATION (old_decl),
1304 "previous declaration %qD", old_decl);
1305 return false;
1307 return true;
1310 // If OLDDECL and NEWDECL are concept declarations with the same type
1311 // (i.e., and template parameters), but different requirements,
1312 // emit diagnostics and return true. Otherwise, return false.
1313 static inline bool
1314 check_concept_refinement (tree olddecl, tree newdecl)
1316 if (!DECL_DECLARED_CONCEPT_P (olddecl) || !DECL_DECLARED_CONCEPT_P (newdecl))
1317 return false;
1319 tree d1 = DECL_TEMPLATE_RESULT (olddecl);
1320 tree d2 = DECL_TEMPLATE_RESULT (newdecl);
1321 if (TREE_CODE (d1) != TREE_CODE (d2))
1322 return false;
1324 tree t1 = TREE_TYPE (d1);
1325 tree t2 = TREE_TYPE (d2);
1326 if (TREE_CODE (d1) == FUNCTION_DECL)
1328 if (compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2))
1329 && comp_template_parms (DECL_TEMPLATE_PARMS (olddecl),
1330 DECL_TEMPLATE_PARMS (newdecl))
1331 && !equivalently_constrained (olddecl, newdecl))
1333 error ("cannot specialize concept %q#D", olddecl);
1334 return true;
1337 return false;
1340 /* DECL is a redeclaration of a function or function template. If
1341 it does have default arguments issue a diagnostic. Note: this
1342 function is used to enforce the requirements in C++11 8.3.6 about
1343 no default arguments in redeclarations. */
1345 static void
1346 check_redeclaration_no_default_args (tree decl)
1348 gcc_assert (DECL_DECLARES_FUNCTION_P (decl));
1350 for (tree t = FUNCTION_FIRST_USER_PARMTYPE (decl);
1351 t && t != void_list_node; t = TREE_CHAIN (t))
1352 if (TREE_PURPOSE (t))
1354 permerror (DECL_SOURCE_LOCATION (decl),
1355 "redeclaration of %q#D may not have default "
1356 "arguments", decl);
1357 return;
1361 #define GNU_INLINE_P(fn) (DECL_DECLARED_INLINE_P (fn) \
1362 && lookup_attribute ("gnu_inline", \
1363 DECL_ATTRIBUTES (fn)))
1365 /* If NEWDECL is a redeclaration of OLDDECL, merge the declarations.
1366 If the redeclaration is invalid, a diagnostic is issued, and the
1367 error_mark_node is returned. Otherwise, OLDDECL is returned.
1369 If NEWDECL is not a redeclaration of OLDDECL, NULL_TREE is
1370 returned.
1372 NEWDECL_IS_FRIEND is true if NEWDECL was declared as a friend. */
1374 tree
1375 duplicate_decls (tree newdecl, tree olddecl, bool newdecl_is_friend)
1377 unsigned olddecl_uid = DECL_UID (olddecl);
1378 int olddecl_friend = 0, types_match = 0, hidden_friend = 0;
1379 int new_defines_function = 0;
1380 tree new_template_info;
1382 if (newdecl == olddecl)
1383 return olddecl;
1385 types_match = decls_match (newdecl, olddecl);
1387 /* If either the type of the new decl or the type of the old decl is an
1388 error_mark_node, then that implies that we have already issued an
1389 error (earlier) for some bogus type specification, and in that case,
1390 it is rather pointless to harass the user with yet more error message
1391 about the same declaration, so just pretend the types match here. */
1392 if (TREE_TYPE (newdecl) == error_mark_node
1393 || TREE_TYPE (olddecl) == error_mark_node)
1394 return error_mark_node;
1396 if (UDLIT_OPER_P (DECL_NAME (newdecl))
1397 && UDLIT_OPER_P (DECL_NAME (olddecl)))
1399 if (TREE_CODE (newdecl) == TEMPLATE_DECL
1400 && TREE_CODE (olddecl) != TEMPLATE_DECL
1401 && check_raw_literal_operator (olddecl))
1402 error ("literal operator template %q+D conflicts with"
1403 " raw literal operator %qD", newdecl, olddecl);
1404 else if (TREE_CODE (newdecl) != TEMPLATE_DECL
1405 && TREE_CODE (olddecl) == TEMPLATE_DECL
1406 && check_raw_literal_operator (newdecl))
1407 error ("raw literal operator %q+D conflicts with"
1408 " literal operator template %qD", newdecl, olddecl);
1411 if (DECL_P (olddecl)
1412 && TREE_CODE (newdecl) == FUNCTION_DECL
1413 && TREE_CODE (olddecl) == FUNCTION_DECL
1414 && diagnose_mismatched_attributes (olddecl, newdecl))
1416 if (DECL_INITIAL (olddecl))
1417 inform (DECL_SOURCE_LOCATION (olddecl),
1418 "previous definition of %qD was here", olddecl);
1419 else
1420 inform (DECL_SOURCE_LOCATION (olddecl),
1421 "previous declaration of %qD was here", olddecl);
1424 /* Check for redeclaration and other discrepancies. */
1425 if (TREE_CODE (olddecl) == FUNCTION_DECL
1426 && DECL_ARTIFICIAL (olddecl))
1428 gcc_assert (!DECL_HIDDEN_FRIEND_P (olddecl));
1429 if (TREE_CODE (newdecl) != FUNCTION_DECL)
1431 /* Avoid warnings redeclaring built-ins which have not been
1432 explicitly declared. */
1433 if (DECL_ANTICIPATED (olddecl))
1434 return NULL_TREE;
1436 /* If you declare a built-in or predefined function name as static,
1437 the old definition is overridden, but optionally warn this was a
1438 bad choice of name. */
1439 if (! TREE_PUBLIC (newdecl))
1441 warning (OPT_Wshadow,
1442 DECL_BUILT_IN (olddecl)
1443 ? G_("shadowing built-in function %q#D")
1444 : G_("shadowing library function %q#D"), olddecl);
1445 /* Discard the old built-in function. */
1446 return NULL_TREE;
1448 /* If the built-in is not ansi, then programs can override
1449 it even globally without an error. */
1450 else if (! DECL_BUILT_IN (olddecl))
1451 warning_at (DECL_SOURCE_LOCATION (newdecl), 0,
1452 "library function %q#D redeclared as non-function %q#D",
1453 olddecl, newdecl);
1454 else
1455 error ("declaration of %q+#D conflicts with built-in "
1456 "declaration %q#D", newdecl, olddecl);
1457 return NULL_TREE;
1459 else if (DECL_OMP_DECLARE_REDUCTION_P (olddecl))
1461 gcc_assert (DECL_OMP_DECLARE_REDUCTION_P (newdecl));
1462 error_at (DECL_SOURCE_LOCATION (newdecl),
1463 "redeclaration of %<pragma omp declare reduction%>");
1464 inform (DECL_SOURCE_LOCATION (olddecl),
1465 "previous %<pragma omp declare reduction%> declaration");
1466 return error_mark_node;
1468 else if (!types_match)
1470 /* Avoid warnings redeclaring built-ins which have not been
1471 explicitly declared. */
1472 if (DECL_ANTICIPATED (olddecl))
1474 tree t1, t2;
1476 /* A new declaration doesn't match a built-in one unless it
1477 is also extern "C". */
1478 gcc_assert (DECL_IS_BUILTIN (olddecl));
1479 gcc_assert (DECL_EXTERN_C_P (olddecl));
1480 if (!DECL_EXTERN_C_P (newdecl))
1481 return NULL_TREE;
1483 for (t1 = TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1484 t2 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1485 t1 || t2;
1486 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1488 if (!t1 || !t2)
1489 break;
1490 /* FILE, tm types are not known at the time
1491 we create the builtins. */
1492 for (unsigned i = 0;
1493 i < sizeof (builtin_structptr_types)
1494 / sizeof (builtin_structptr_type);
1495 ++i)
1496 if (TREE_VALUE (t2) == builtin_structptr_types[i].node)
1498 tree t = TREE_VALUE (t1);
1500 if (TYPE_PTR_P (t)
1501 && TYPE_IDENTIFIER (TREE_TYPE (t))
1502 == get_identifier (builtin_structptr_types[i].str)
1503 && compparms (TREE_CHAIN (t1), TREE_CHAIN (t2)))
1505 tree oldargs = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1507 TYPE_ARG_TYPES (TREE_TYPE (olddecl))
1508 = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
1509 types_match = decls_match (newdecl, olddecl);
1510 if (types_match)
1511 return duplicate_decls (newdecl, olddecl,
1512 newdecl_is_friend);
1513 TYPE_ARG_TYPES (TREE_TYPE (olddecl)) = oldargs;
1515 goto next_arg;
1518 if (! same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1519 break;
1520 next_arg:;
1523 warning_at (DECL_SOURCE_LOCATION (newdecl),
1524 OPT_Wbuiltin_declaration_mismatch,
1525 "declaration of %q+#D conflicts with built-in "
1526 "declaration %q#D", newdecl, olddecl);
1528 else if ((DECL_EXTERN_C_P (newdecl)
1529 && DECL_EXTERN_C_P (olddecl))
1530 || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1531 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
1533 /* A near match; override the builtin. */
1535 if (TREE_PUBLIC (newdecl))
1536 warning_at (DECL_SOURCE_LOCATION (newdecl),
1537 OPT_Wbuiltin_declaration_mismatch,
1538 "new declaration %q#D ambiguates built-in "
1539 "declaration %q#D", newdecl, olddecl);
1540 else
1541 warning (OPT_Wshadow,
1542 DECL_BUILT_IN (olddecl)
1543 ? G_("shadowing built-in function %q#D")
1544 : G_("shadowing library function %q#D"), olddecl);
1546 else
1547 /* Discard the old built-in function. */
1548 return NULL_TREE;
1550 /* Replace the old RTL to avoid problems with inlining. */
1551 COPY_DECL_RTL (newdecl, olddecl);
1553 /* Even if the types match, prefer the new declarations type for
1554 built-ins which have not been explicitly declared, for
1555 exception lists, etc... */
1556 else if (DECL_IS_BUILTIN (olddecl))
1558 tree type = TREE_TYPE (newdecl);
1559 tree attribs = (*targetm.merge_type_attributes)
1560 (TREE_TYPE (olddecl), type);
1562 type = cp_build_type_attribute_variant (type, attribs);
1563 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = type;
1566 /* If a function is explicitly declared "throw ()", propagate that to
1567 the corresponding builtin. */
1568 if (DECL_BUILT_IN_CLASS (olddecl) == BUILT_IN_NORMAL
1569 && DECL_ANTICIPATED (olddecl)
1570 && TREE_NOTHROW (newdecl)
1571 && !TREE_NOTHROW (olddecl))
1573 enum built_in_function fncode = DECL_FUNCTION_CODE (olddecl);
1574 tree tmpdecl = builtin_decl_explicit (fncode);
1575 if (tmpdecl && tmpdecl != olddecl && types_match)
1576 TREE_NOTHROW (tmpdecl) = 1;
1579 /* Whether or not the builtin can throw exceptions has no
1580 bearing on this declarator. */
1581 TREE_NOTHROW (olddecl) = 0;
1583 if (DECL_THIS_STATIC (newdecl) && !DECL_THIS_STATIC (olddecl))
1585 /* If a builtin function is redeclared as `static', merge
1586 the declarations, but make the original one static. */
1587 DECL_THIS_STATIC (olddecl) = 1;
1588 TREE_PUBLIC (olddecl) = 0;
1590 /* Make the old declaration consistent with the new one so
1591 that all remnants of the builtin-ness of this function
1592 will be banished. */
1593 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
1594 COPY_DECL_RTL (newdecl, olddecl);
1597 else if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1599 /* C++ Standard, 3.3, clause 4:
1600 "[Note: a namespace name or a class template name must be unique
1601 in its declarative region (7.3.2, clause 14). ]" */
1602 if (TREE_CODE (olddecl) != NAMESPACE_DECL
1603 && TREE_CODE (newdecl) != NAMESPACE_DECL
1604 && (TREE_CODE (olddecl) != TEMPLATE_DECL
1605 || TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) != TYPE_DECL)
1606 && (TREE_CODE (newdecl) != TEMPLATE_DECL
1607 || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) != TYPE_DECL))
1609 if ((TREE_CODE (olddecl) == TYPE_DECL && DECL_ARTIFICIAL (olddecl)
1610 && TREE_CODE (newdecl) != TYPE_DECL)
1611 || (TREE_CODE (newdecl) == TYPE_DECL && DECL_ARTIFICIAL (newdecl)
1612 && TREE_CODE (olddecl) != TYPE_DECL))
1614 /* We do nothing special here, because C++ does such nasty
1615 things with TYPE_DECLs. Instead, just let the TYPE_DECL
1616 get shadowed, and know that if we need to find a TYPE_DECL
1617 for a given name, we can look in the IDENTIFIER_TYPE_VALUE
1618 slot of the identifier. */
1619 return NULL_TREE;
1622 if ((TREE_CODE (newdecl) == FUNCTION_DECL
1623 && DECL_FUNCTION_TEMPLATE_P (olddecl))
1624 || (TREE_CODE (olddecl) == FUNCTION_DECL
1625 && DECL_FUNCTION_TEMPLATE_P (newdecl)))
1626 return NULL_TREE;
1629 error ("%q#D redeclared as different kind of symbol", newdecl);
1630 if (TREE_CODE (olddecl) == TREE_LIST)
1631 olddecl = TREE_VALUE (olddecl);
1632 inform (DECL_SOURCE_LOCATION (olddecl),
1633 "previous declaration %q#D", olddecl);
1635 return error_mark_node;
1637 else if (!types_match)
1639 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl))
1640 /* These are certainly not duplicate declarations; they're
1641 from different scopes. */
1642 return NULL_TREE;
1644 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1646 /* The name of a class template may not be declared to refer to
1647 any other template, class, function, object, namespace, value,
1648 or type in the same scope. */
1649 if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == TYPE_DECL
1650 || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
1652 error ("conflicting declaration of template %q+#D", newdecl);
1653 inform (DECL_SOURCE_LOCATION (olddecl),
1654 "previous declaration %q#D", olddecl);
1655 return error_mark_node;
1657 else if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == FUNCTION_DECL
1658 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL
1659 && compparms (TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl))),
1660 TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl))))
1661 && comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
1662 DECL_TEMPLATE_PARMS (olddecl))
1663 /* Template functions can be disambiguated by
1664 return type. */
1665 && same_type_p (TREE_TYPE (TREE_TYPE (newdecl)),
1666 TREE_TYPE (TREE_TYPE (olddecl)))
1667 // Template functions can also be disambiguated by
1668 // constraints.
1669 && equivalently_constrained (olddecl, newdecl))
1671 error ("ambiguating new declaration %q+#D", newdecl);
1672 inform (DECL_SOURCE_LOCATION (olddecl),
1673 "old declaration %q#D", olddecl);
1675 else if (check_concept_refinement (olddecl, newdecl))
1676 return error_mark_node;
1677 return NULL_TREE;
1679 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1681 if (DECL_EXTERN_C_P (newdecl) && DECL_EXTERN_C_P (olddecl))
1683 error ("conflicting declaration of C function %q+#D",
1684 newdecl);
1685 inform (DECL_SOURCE_LOCATION (olddecl),
1686 "previous declaration %q#D", olddecl);
1687 return NULL_TREE;
1689 /* For function versions, params and types match, but they
1690 are not ambiguous. */
1691 else if ((!DECL_FUNCTION_VERSIONED (newdecl)
1692 && !DECL_FUNCTION_VERSIONED (olddecl))
1693 // The functions have the same parameter types.
1694 && compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1695 TYPE_ARG_TYPES (TREE_TYPE (olddecl)))
1696 // And the same constraints.
1697 && equivalently_constrained (newdecl, olddecl))
1699 error ("ambiguating new declaration of %q+#D", newdecl);
1700 inform (DECL_SOURCE_LOCATION (olddecl),
1701 "old declaration %q#D", olddecl);
1702 return error_mark_node;
1704 else
1705 return NULL_TREE;
1707 else
1709 error ("conflicting declaration %q+#D", newdecl);
1710 inform (DECL_SOURCE_LOCATION (olddecl),
1711 "previous declaration as %q#D", olddecl);
1712 return error_mark_node;
1715 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1716 && ((DECL_TEMPLATE_SPECIALIZATION (olddecl)
1717 && (!DECL_TEMPLATE_INFO (newdecl)
1718 || (DECL_TI_TEMPLATE (newdecl)
1719 != DECL_TI_TEMPLATE (olddecl))))
1720 || (DECL_TEMPLATE_SPECIALIZATION (newdecl)
1721 && (!DECL_TEMPLATE_INFO (olddecl)
1722 || (DECL_TI_TEMPLATE (olddecl)
1723 != DECL_TI_TEMPLATE (newdecl))))))
1724 /* It's OK to have a template specialization and a non-template
1725 with the same type, or to have specializations of two
1726 different templates with the same type. Note that if one is a
1727 specialization, and the other is an instantiation of the same
1728 template, that we do not exit at this point. That situation
1729 can occur if we instantiate a template class, and then
1730 specialize one of its methods. This situation is valid, but
1731 the declarations must be merged in the usual way. */
1732 return NULL_TREE;
1733 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1734 && ((DECL_TEMPLATE_INSTANTIATION (olddecl)
1735 && !DECL_USE_TEMPLATE (newdecl))
1736 || (DECL_TEMPLATE_INSTANTIATION (newdecl)
1737 && !DECL_USE_TEMPLATE (olddecl))))
1738 /* One of the declarations is a template instantiation, and the
1739 other is not a template at all. That's OK. */
1740 return NULL_TREE;
1741 else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
1743 /* In [namespace.alias] we have:
1745 In a declarative region, a namespace-alias-definition can be
1746 used to redefine a namespace-alias declared in that declarative
1747 region to refer only to the namespace to which it already
1748 refers.
1750 Therefore, if we encounter a second alias directive for the same
1751 alias, we can just ignore the second directive. */
1752 if (DECL_NAMESPACE_ALIAS (newdecl)
1753 && (DECL_NAMESPACE_ALIAS (newdecl)
1754 == DECL_NAMESPACE_ALIAS (olddecl)))
1755 return olddecl;
1757 /* Leave it to update_binding to merge or report error. */
1758 return NULL_TREE;
1760 else
1762 const char *errmsg = redeclaration_error_message (newdecl, olddecl);
1763 if (errmsg)
1765 error_at (DECL_SOURCE_LOCATION (newdecl), errmsg, newdecl);
1766 if (DECL_NAME (olddecl) != NULL_TREE)
1767 inform (DECL_SOURCE_LOCATION (olddecl),
1768 (DECL_INITIAL (olddecl) && namespace_bindings_p ())
1769 ? G_("%q#D previously defined here")
1770 : G_("%q#D previously declared here"), olddecl);
1771 return error_mark_node;
1773 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1774 && DECL_INITIAL (olddecl) != NULL_TREE
1775 && !prototype_p (TREE_TYPE (olddecl))
1776 && prototype_p (TREE_TYPE (newdecl)))
1778 /* Prototype decl follows defn w/o prototype. */
1779 if (warning_at (DECL_SOURCE_LOCATION (newdecl), 0,
1780 "prototype specified for %q#D", newdecl))
1781 inform (DECL_SOURCE_LOCATION (olddecl),
1782 "previous non-prototype definition here");
1784 else if (VAR_OR_FUNCTION_DECL_P (olddecl)
1785 && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl))
1787 /* [dcl.link]
1788 If two declarations of the same function or object
1789 specify different linkage-specifications ..., the program
1790 is ill-formed.... Except for functions with C++ linkage,
1791 a function declaration without a linkage specification
1792 shall not precede the first linkage specification for
1793 that function. A function can be declared without a
1794 linkage specification after an explicit linkage
1795 specification has been seen; the linkage explicitly
1796 specified in the earlier declaration is not affected by
1797 such a function declaration.
1799 DR 563 raises the question why the restrictions on
1800 functions should not also apply to objects. Older
1801 versions of G++ silently ignore the linkage-specification
1802 for this example:
1804 namespace N {
1805 extern int i;
1806 extern "C" int i;
1809 which is clearly wrong. Therefore, we now treat objects
1810 like functions. */
1811 if (current_lang_depth () == 0)
1813 /* There is no explicit linkage-specification, so we use
1814 the linkage from the previous declaration. */
1815 retrofit_lang_decl (newdecl);
1816 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
1818 else
1820 error ("conflicting declaration of %q+#D with %qL linkage",
1821 newdecl, DECL_LANGUAGE (newdecl));
1822 inform (DECL_SOURCE_LOCATION (olddecl),
1823 "previous declaration with %qL linkage",
1824 DECL_LANGUAGE (olddecl));
1828 if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl))
1830 else if (TREE_CODE (olddecl) == FUNCTION_DECL)
1832 /* Note: free functions, as TEMPLATE_DECLs, are handled below. */
1833 if (DECL_FUNCTION_MEMBER_P (olddecl)
1834 && (/* grokfndecl passes member function templates too
1835 as FUNCTION_DECLs. */
1836 DECL_TEMPLATE_INFO (olddecl)
1837 /* C++11 8.3.6/6.
1838 Default arguments for a member function of a class
1839 template shall be specified on the initial declaration
1840 of the member function within the class template. */
1841 || CLASSTYPE_TEMPLATE_INFO (CP_DECL_CONTEXT (olddecl))))
1842 check_redeclaration_no_default_args (newdecl);
1843 else
1845 tree t1 = FUNCTION_FIRST_USER_PARMTYPE (olddecl);
1846 tree t2 = FUNCTION_FIRST_USER_PARMTYPE (newdecl);
1847 int i = 1;
1849 for (; t1 && t1 != void_list_node;
1850 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++)
1851 if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
1853 if (1 == simple_cst_equal (TREE_PURPOSE (t1),
1854 TREE_PURPOSE (t2)))
1856 if (permerror (input_location,
1857 "default argument given for parameter "
1858 "%d of %q#D", i, newdecl))
1859 inform (DECL_SOURCE_LOCATION (olddecl),
1860 "previous specification in %q#D here",
1861 olddecl);
1863 else
1865 error ("default argument given for parameter %d "
1866 "of %q#D", i, newdecl);
1867 inform (DECL_SOURCE_LOCATION (olddecl),
1868 "previous specification in %q#D here",
1869 olddecl);
1876 /* Do not merge an implicit typedef with an explicit one. In:
1878 class A;
1880 typedef class A A __attribute__ ((foo));
1882 the attribute should apply only to the typedef. */
1883 if (TREE_CODE (olddecl) == TYPE_DECL
1884 && (DECL_IMPLICIT_TYPEDEF_P (olddecl)
1885 || DECL_IMPLICIT_TYPEDEF_P (newdecl)))
1886 return NULL_TREE;
1888 /* If new decl is `static' and an `extern' was seen previously,
1889 warn about it. */
1890 warn_extern_redeclared_static (newdecl, olddecl);
1892 if (!validate_constexpr_redeclaration (olddecl, newdecl))
1893 return error_mark_node;
1895 /* We have committed to returning 1 at this point. */
1896 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1898 /* Now that functions must hold information normally held
1899 by field decls, there is extra work to do so that
1900 declaration information does not get destroyed during
1901 definition. */
1902 if (DECL_VINDEX (olddecl))
1903 DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl);
1904 if (DECL_CONTEXT (olddecl))
1905 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1906 DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl);
1907 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1908 DECL_PURE_VIRTUAL_P (newdecl) |= DECL_PURE_VIRTUAL_P (olddecl);
1909 DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl);
1910 DECL_INVALID_OVERRIDER_P (newdecl) |= DECL_INVALID_OVERRIDER_P (olddecl);
1911 DECL_FINAL_P (newdecl) |= DECL_FINAL_P (olddecl);
1912 DECL_OVERRIDE_P (newdecl) |= DECL_OVERRIDE_P (olddecl);
1913 DECL_THIS_STATIC (newdecl) |= DECL_THIS_STATIC (olddecl);
1914 if (DECL_OVERLOADED_OPERATOR_P (olddecl) != ERROR_MARK)
1915 SET_OVERLOADED_OPERATOR_CODE
1916 (newdecl, DECL_OVERLOADED_OPERATOR_P (olddecl));
1917 new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE;
1919 /* Optionally warn about more than one declaration for the same
1920 name, but don't warn about a function declaration followed by a
1921 definition. */
1922 if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl)
1923 && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE)
1924 /* Don't warn about extern decl followed by definition. */
1925 && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl))
1926 /* Don't warn about friends, let add_friend take care of it. */
1927 && ! (newdecl_is_friend || DECL_FRIEND_P (olddecl))
1928 /* Don't warn about declaration followed by specialization. */
1929 && (! DECL_TEMPLATE_SPECIALIZATION (newdecl)
1930 || DECL_TEMPLATE_SPECIALIZATION (olddecl)))
1932 if (warning_at (DECL_SOURCE_LOCATION (newdecl),
1933 OPT_Wredundant_decls,
1934 "redundant redeclaration of %qD in same scope",
1935 newdecl))
1936 inform (DECL_SOURCE_LOCATION (olddecl),
1937 "previous declaration of %qD", olddecl);
1940 if (!(DECL_TEMPLATE_INSTANTIATION (olddecl)
1941 && DECL_TEMPLATE_SPECIALIZATION (newdecl)))
1943 if (DECL_DELETED_FN (newdecl))
1945 error ("deleted definition of %q+D", newdecl);
1946 inform (DECL_SOURCE_LOCATION (olddecl),
1947 "previous declaration of %qD", olddecl);
1949 DECL_DELETED_FN (newdecl) |= DECL_DELETED_FN (olddecl);
1953 /* Deal with C++: must preserve virtual function table size. */
1954 if (TREE_CODE (olddecl) == TYPE_DECL)
1956 tree newtype = TREE_TYPE (newdecl);
1957 tree oldtype = TREE_TYPE (olddecl);
1959 if (newtype != error_mark_node && oldtype != error_mark_node
1960 && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype))
1961 CLASSTYPE_FRIEND_CLASSES (newtype)
1962 = CLASSTYPE_FRIEND_CLASSES (oldtype);
1964 DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl);
1967 /* Copy all the DECL_... slots specified in the new decl
1968 except for any that we copy here from the old type. */
1969 DECL_ATTRIBUTES (newdecl)
1970 = (*targetm.merge_decl_attributes) (olddecl, newdecl);
1972 if (DECL_DECLARES_FUNCTION_P (olddecl) && DECL_DECLARES_FUNCTION_P (newdecl))
1974 olddecl_friend = DECL_FRIEND_P (olddecl);
1975 hidden_friend = (DECL_ANTICIPATED (olddecl)
1976 && DECL_HIDDEN_FRIEND_P (olddecl)
1977 && newdecl_is_friend);
1978 if (!hidden_friend)
1980 DECL_ANTICIPATED (olddecl) = 0;
1981 DECL_HIDDEN_FRIEND_P (olddecl) = 0;
1985 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1987 tree old_result;
1988 tree new_result;
1989 old_result = DECL_TEMPLATE_RESULT (olddecl);
1990 new_result = DECL_TEMPLATE_RESULT (newdecl);
1991 TREE_TYPE (olddecl) = TREE_TYPE (old_result);
1992 DECL_TEMPLATE_SPECIALIZATIONS (olddecl)
1993 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (olddecl),
1994 DECL_TEMPLATE_SPECIALIZATIONS (newdecl));
1996 DECL_ATTRIBUTES (old_result)
1997 = (*targetm.merge_decl_attributes) (old_result, new_result);
1999 if (DECL_FUNCTION_TEMPLATE_P (newdecl))
2001 /* Per C++11 8.3.6/4, default arguments cannot be added in later
2002 declarations of a function template. */
2003 if (DECL_SOURCE_LOCATION (newdecl)
2004 != DECL_SOURCE_LOCATION (olddecl))
2005 check_redeclaration_no_default_args (newdecl);
2007 check_default_args (newdecl);
2009 if (GNU_INLINE_P (old_result) != GNU_INLINE_P (new_result)
2010 && DECL_INITIAL (new_result))
2012 if (DECL_INITIAL (old_result))
2013 DECL_UNINLINABLE (old_result) = 1;
2014 else
2015 DECL_UNINLINABLE (old_result) = DECL_UNINLINABLE (new_result);
2016 DECL_EXTERNAL (old_result) = DECL_EXTERNAL (new_result);
2017 DECL_NOT_REALLY_EXTERN (old_result)
2018 = DECL_NOT_REALLY_EXTERN (new_result);
2019 DECL_INTERFACE_KNOWN (old_result)
2020 = DECL_INTERFACE_KNOWN (new_result);
2021 DECL_DECLARED_INLINE_P (old_result)
2022 = DECL_DECLARED_INLINE_P (new_result);
2023 DECL_DISREGARD_INLINE_LIMITS (old_result)
2024 |= DECL_DISREGARD_INLINE_LIMITS (new_result);
2027 else
2029 DECL_DECLARED_INLINE_P (old_result)
2030 |= DECL_DECLARED_INLINE_P (new_result);
2031 DECL_DISREGARD_INLINE_LIMITS (old_result)
2032 |= DECL_DISREGARD_INLINE_LIMITS (new_result);
2033 check_redeclaration_exception_specification (newdecl, olddecl);
2037 /* If the new declaration is a definition, update the file and
2038 line information on the declaration, and also make
2039 the old declaration the same definition. */
2040 if (DECL_INITIAL (new_result) != NULL_TREE)
2042 DECL_SOURCE_LOCATION (olddecl)
2043 = DECL_SOURCE_LOCATION (old_result)
2044 = DECL_SOURCE_LOCATION (newdecl);
2045 DECL_INITIAL (old_result) = DECL_INITIAL (new_result);
2046 if (DECL_FUNCTION_TEMPLATE_P (newdecl))
2048 tree parm;
2049 DECL_ARGUMENTS (old_result)
2050 = DECL_ARGUMENTS (new_result);
2051 for (parm = DECL_ARGUMENTS (old_result); parm;
2052 parm = DECL_CHAIN (parm))
2053 DECL_CONTEXT (parm) = old_result;
2057 return olddecl;
2060 if (types_match)
2062 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2063 check_redeclaration_exception_specification (newdecl, olddecl);
2065 /* Automatically handles default parameters. */
2066 tree oldtype = TREE_TYPE (olddecl);
2067 tree newtype;
2069 /* For typedefs use the old type, as the new type's DECL_NAME points
2070 at newdecl, which will be ggc_freed. */
2071 if (TREE_CODE (newdecl) == TYPE_DECL)
2073 /* But NEWTYPE might have an attribute, honor that. */
2074 tree tem = TREE_TYPE (newdecl);
2075 newtype = oldtype;
2077 if (TYPE_USER_ALIGN (tem))
2079 if (TYPE_ALIGN (tem) > TYPE_ALIGN (newtype))
2080 SET_TYPE_ALIGN (newtype, TYPE_ALIGN (tem));
2081 TYPE_USER_ALIGN (newtype) = true;
2084 /* And remove the new type from the variants list. */
2085 if (TYPE_NAME (TREE_TYPE (newdecl)) == newdecl)
2087 tree remove = TREE_TYPE (newdecl);
2088 for (tree t = TYPE_MAIN_VARIANT (remove); ;
2089 t = TYPE_NEXT_VARIANT (t))
2090 if (TYPE_NEXT_VARIANT (t) == remove)
2092 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (remove);
2093 break;
2097 else
2098 /* Merge the data types specified in the two decls. */
2099 newtype = merge_types (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
2101 if (VAR_P (newdecl))
2103 DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl);
2104 /* For already initialized vars, TREE_READONLY could have been
2105 cleared in cp_finish_decl, because the var needs runtime
2106 initialization or destruction. Make sure not to set
2107 TREE_READONLY on it again. */
2108 if (DECL_INITIALIZED_P (olddecl)
2109 && !DECL_EXTERNAL (olddecl)
2110 && !TREE_READONLY (olddecl))
2111 TREE_READONLY (newdecl) = 0;
2112 DECL_INITIALIZED_P (newdecl) |= DECL_INITIALIZED_P (olddecl);
2113 DECL_NONTRIVIALLY_INITIALIZED_P (newdecl)
2114 |= DECL_NONTRIVIALLY_INITIALIZED_P (olddecl);
2115 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (newdecl)
2116 |= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (olddecl);
2117 if (DECL_CLASS_SCOPE_P (olddecl))
2118 DECL_DECLARED_CONSTEXPR_P (newdecl)
2119 |= DECL_DECLARED_CONSTEXPR_P (olddecl);
2121 /* Merge the threadprivate attribute from OLDDECL into NEWDECL. */
2122 if (DECL_LANG_SPECIFIC (olddecl)
2123 && CP_DECL_THREADPRIVATE_P (olddecl))
2125 /* Allocate a LANG_SPECIFIC structure for NEWDECL, if needed. */
2126 retrofit_lang_decl (newdecl);
2127 CP_DECL_THREADPRIVATE_P (newdecl) = 1;
2131 /* An explicit specialization of a function template or of a member
2132 function of a class template can be declared transaction_safe
2133 independently of whether the corresponding template entity is declared
2134 transaction_safe. */
2135 if (flag_tm && TREE_CODE (newdecl) == FUNCTION_DECL
2136 && DECL_TEMPLATE_INSTANTIATION (olddecl)
2137 && DECL_TEMPLATE_SPECIALIZATION (newdecl)
2138 && tx_safe_fn_type_p (newtype)
2139 && !tx_safe_fn_type_p (TREE_TYPE (newdecl)))
2140 newtype = tx_unsafe_fn_variant (newtype);
2142 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype;
2144 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2145 check_default_args (newdecl);
2147 /* Lay the type out, unless already done. */
2148 if (! same_type_p (newtype, oldtype)
2149 && TREE_TYPE (newdecl) != error_mark_node
2150 && !(processing_template_decl && uses_template_parms (newdecl)))
2151 layout_type (TREE_TYPE (newdecl));
2153 if ((VAR_P (newdecl)
2154 || TREE_CODE (newdecl) == PARM_DECL
2155 || TREE_CODE (newdecl) == RESULT_DECL
2156 || TREE_CODE (newdecl) == FIELD_DECL
2157 || TREE_CODE (newdecl) == TYPE_DECL)
2158 && !(processing_template_decl && uses_template_parms (newdecl)))
2159 layout_decl (newdecl, 0);
2161 /* Merge the type qualifiers. */
2162 if (TREE_READONLY (newdecl))
2163 TREE_READONLY (olddecl) = 1;
2164 if (TREE_THIS_VOLATILE (newdecl))
2165 TREE_THIS_VOLATILE (olddecl) = 1;
2166 if (TREE_NOTHROW (newdecl))
2167 TREE_NOTHROW (olddecl) = 1;
2169 /* Merge deprecatedness. */
2170 if (TREE_DEPRECATED (newdecl))
2171 TREE_DEPRECATED (olddecl) = 1;
2173 /* Preserve function specific target and optimization options */
2174 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2176 if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
2177 && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
2178 DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
2179 = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
2181 if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
2182 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
2183 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
2184 = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
2187 /* Merge the initialization information. */
2188 if (DECL_INITIAL (newdecl) == NULL_TREE
2189 && DECL_INITIAL (olddecl) != NULL_TREE)
2191 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2192 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2193 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2195 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
2196 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
2200 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2202 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
2203 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
2204 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
2205 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
2206 TREE_NOTHROW (newdecl) |= TREE_NOTHROW (olddecl);
2207 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
2208 DECL_IS_OPERATOR_NEW (newdecl) |= DECL_IS_OPERATOR_NEW (olddecl);
2209 DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
2210 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
2211 DECL_LOOPING_CONST_OR_PURE_P (newdecl)
2212 |= DECL_LOOPING_CONST_OR_PURE_P (olddecl);
2213 /* Keep the old RTL. */
2214 COPY_DECL_RTL (olddecl, newdecl);
2216 else if (VAR_P (newdecl)
2217 && (DECL_SIZE (olddecl) || !DECL_SIZE (newdecl)))
2219 /* Keep the old RTL. We cannot keep the old RTL if the old
2220 declaration was for an incomplete object and the new
2221 declaration is not since many attributes of the RTL will
2222 change. */
2223 COPY_DECL_RTL (olddecl, newdecl);
2226 /* If cannot merge, then use the new type and qualifiers,
2227 and don't preserve the old rtl. */
2228 else
2230 /* Clean out any memory we had of the old declaration. */
2231 tree oldstatic = value_member (olddecl, static_aggregates);
2232 if (oldstatic)
2233 TREE_VALUE (oldstatic) = error_mark_node;
2235 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
2236 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
2237 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
2238 TREE_NOTHROW (olddecl) = TREE_NOTHROW (newdecl);
2239 TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
2242 /* Merge the storage class information. */
2243 merge_weak (newdecl, olddecl);
2245 DECL_DEFER_OUTPUT (newdecl) |= DECL_DEFER_OUTPUT (olddecl);
2246 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
2247 TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl);
2248 if (! DECL_EXTERNAL (olddecl))
2249 DECL_EXTERNAL (newdecl) = 0;
2250 if (! DECL_COMDAT (olddecl))
2251 DECL_COMDAT (newdecl) = 0;
2253 new_template_info = NULL_TREE;
2254 if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl))
2256 bool new_redefines_gnu_inline = false;
2258 if (new_defines_function
2259 && ((DECL_INTERFACE_KNOWN (olddecl)
2260 && TREE_CODE (olddecl) == FUNCTION_DECL)
2261 || (TREE_CODE (olddecl) == TEMPLATE_DECL
2262 && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl))
2263 == FUNCTION_DECL))))
2265 tree fn = olddecl;
2267 if (TREE_CODE (fn) == TEMPLATE_DECL)
2268 fn = DECL_TEMPLATE_RESULT (olddecl);
2270 new_redefines_gnu_inline = GNU_INLINE_P (fn) && DECL_INITIAL (fn);
2273 if (!new_redefines_gnu_inline)
2275 DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl);
2276 DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl);
2277 DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl);
2279 DECL_TEMPLATE_INSTANTIATED (newdecl)
2280 |= DECL_TEMPLATE_INSTANTIATED (olddecl);
2281 DECL_ODR_USED (newdecl) |= DECL_ODR_USED (olddecl);
2283 /* If the OLDDECL is an instantiation and/or specialization,
2284 then the NEWDECL must be too. But, it may not yet be marked
2285 as such if the caller has created NEWDECL, but has not yet
2286 figured out that it is a redeclaration. */
2287 if (!DECL_USE_TEMPLATE (newdecl))
2288 DECL_USE_TEMPLATE (newdecl) = DECL_USE_TEMPLATE (olddecl);
2290 /* Don't really know how much of the language-specific
2291 values we should copy from old to new. */
2292 DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl);
2293 DECL_REPO_AVAILABLE_P (newdecl) = DECL_REPO_AVAILABLE_P (olddecl);
2294 DECL_INITIALIZED_IN_CLASS_P (newdecl)
2295 |= DECL_INITIALIZED_IN_CLASS_P (olddecl);
2297 if (LANG_DECL_HAS_MIN (newdecl))
2299 DECL_LANG_SPECIFIC (newdecl)->u.min.u2 =
2300 DECL_LANG_SPECIFIC (olddecl)->u.min.u2;
2301 if (DECL_TEMPLATE_INFO (newdecl))
2303 new_template_info = DECL_TEMPLATE_INFO (newdecl);
2304 if (DECL_TEMPLATE_INSTANTIATION (olddecl)
2305 && DECL_TEMPLATE_SPECIALIZATION (newdecl))
2306 /* Remember the presence of explicit specialization args. */
2307 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (olddecl))
2308 = TINFO_USED_TEMPLATE_ID (new_template_info);
2310 DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl);
2312 /* Only functions have these fields. */
2313 if (DECL_DECLARES_FUNCTION_P (newdecl))
2315 DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl);
2316 DECL_BEFRIENDING_CLASSES (newdecl)
2317 = chainon (DECL_BEFRIENDING_CLASSES (newdecl),
2318 DECL_BEFRIENDING_CLASSES (olddecl));
2319 /* DECL_THUNKS is only valid for virtual functions,
2320 otherwise it is a DECL_FRIEND_CONTEXT. */
2321 if (DECL_VIRTUAL_P (newdecl))
2322 SET_DECL_THUNKS (newdecl, DECL_THUNKS (olddecl));
2324 /* Only variables have this field. */
2325 else if (VAR_P (newdecl)
2326 && VAR_HAD_UNKNOWN_BOUND (olddecl))
2327 SET_VAR_HAD_UNKNOWN_BOUND (newdecl);
2330 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2332 tree parm;
2334 /* Merge parameter attributes. */
2335 tree oldarg, newarg;
2336 for (oldarg = DECL_ARGUMENTS(olddecl),
2337 newarg = DECL_ARGUMENTS(newdecl);
2338 oldarg && newarg;
2339 oldarg = DECL_CHAIN(oldarg), newarg = DECL_CHAIN(newarg)) {
2340 DECL_ATTRIBUTES (newarg)
2341 = (*targetm.merge_decl_attributes) (oldarg, newarg);
2342 DECL_ATTRIBUTES (oldarg) = DECL_ATTRIBUTES (newarg);
2345 if (DECL_TEMPLATE_INSTANTIATION (olddecl)
2346 && !DECL_TEMPLATE_INSTANTIATION (newdecl))
2348 /* If newdecl is not a specialization, then it is not a
2349 template-related function at all. And that means that we
2350 should have exited above, returning 0. */
2351 gcc_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl));
2353 if (DECL_ODR_USED (olddecl))
2354 /* From [temp.expl.spec]:
2356 If a template, a member template or the member of a class
2357 template is explicitly specialized then that
2358 specialization shall be declared before the first use of
2359 that specialization that would cause an implicit
2360 instantiation to take place, in every translation unit in
2361 which such a use occurs. */
2362 error ("explicit specialization of %qD after first use",
2363 olddecl);
2365 SET_DECL_TEMPLATE_SPECIALIZATION (olddecl);
2366 DECL_COMDAT (newdecl) = (TREE_PUBLIC (newdecl)
2367 && DECL_DECLARED_INLINE_P (newdecl));
2369 /* Don't propagate visibility from the template to the
2370 specialization here. We'll do that in determine_visibility if
2371 appropriate. */
2372 DECL_VISIBILITY_SPECIFIED (olddecl) = 0;
2374 /* [temp.expl.spec/14] We don't inline explicit specialization
2375 just because the primary template says so. */
2377 /* But still keep DECL_DISREGARD_INLINE_LIMITS in sync with
2378 the always_inline attribute. */
2379 if (DECL_DISREGARD_INLINE_LIMITS (olddecl)
2380 && !DECL_DISREGARD_INLINE_LIMITS (newdecl))
2382 if (DECL_DECLARED_INLINE_P (newdecl))
2383 DECL_DISREGARD_INLINE_LIMITS (newdecl) = true;
2384 else
2385 DECL_ATTRIBUTES (newdecl)
2386 = remove_attribute ("always_inline",
2387 DECL_ATTRIBUTES (newdecl));
2390 else if (new_defines_function && DECL_INITIAL (olddecl))
2392 /* Never inline re-defined extern inline functions.
2393 FIXME: this could be better handled by keeping both
2394 function as separate declarations. */
2395 DECL_UNINLINABLE (newdecl) = 1;
2397 else
2399 if (DECL_PENDING_INLINE_P (olddecl))
2401 DECL_PENDING_INLINE_P (newdecl) = 1;
2402 DECL_PENDING_INLINE_INFO (newdecl)
2403 = DECL_PENDING_INLINE_INFO (olddecl);
2405 else if (DECL_PENDING_INLINE_P (newdecl))
2407 else if (DECL_SAVED_FUNCTION_DATA (newdecl) == NULL)
2408 DECL_SAVED_FUNCTION_DATA (newdecl)
2409 = DECL_SAVED_FUNCTION_DATA (olddecl);
2411 DECL_DECLARED_INLINE_P (newdecl) |= DECL_DECLARED_INLINE_P (olddecl);
2413 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
2414 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
2416 DECL_DISREGARD_INLINE_LIMITS (newdecl)
2417 = DECL_DISREGARD_INLINE_LIMITS (olddecl)
2418 = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
2419 || DECL_DISREGARD_INLINE_LIMITS (olddecl));
2422 /* Preserve abstractness on cloned [cd]tors. */
2423 DECL_ABSTRACT_P (newdecl) = DECL_ABSTRACT_P (olddecl);
2425 /* Update newdecl's parms to point at olddecl. */
2426 for (parm = DECL_ARGUMENTS (newdecl); parm;
2427 parm = DECL_CHAIN (parm))
2428 DECL_CONTEXT (parm) = olddecl;
2430 if (! types_match)
2432 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
2433 COPY_DECL_ASSEMBLER_NAME (newdecl, olddecl);
2434 COPY_DECL_RTL (newdecl, olddecl);
2436 if (! types_match || new_defines_function)
2438 /* These need to be copied so that the names are available.
2439 Note that if the types do match, we'll preserve inline
2440 info and other bits, but if not, we won't. */
2441 DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl);
2442 DECL_RESULT (olddecl) = DECL_RESULT (newdecl);
2444 /* If redeclaring a builtin function, it stays built in
2445 if newdecl is a gnu_inline definition, or if newdecl is just
2446 a declaration. */
2447 if (DECL_BUILT_IN (olddecl)
2448 && (new_defines_function ? GNU_INLINE_P (newdecl) : types_match))
2450 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
2451 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
2452 /* If we're keeping the built-in definition, keep the rtl,
2453 regardless of declaration matches. */
2454 COPY_DECL_RTL (olddecl, newdecl);
2455 if (DECL_BUILT_IN_CLASS (newdecl) == BUILT_IN_NORMAL)
2457 enum built_in_function fncode = DECL_FUNCTION_CODE (newdecl);
2458 switch (fncode)
2460 /* If a compatible prototype of these builtin functions
2461 is seen, assume the runtime implements it with the
2462 expected semantics. */
2463 case BUILT_IN_STPCPY:
2464 if (builtin_decl_explicit_p (fncode))
2465 set_builtin_decl_implicit_p (fncode, true);
2466 break;
2467 default:
2468 if (builtin_decl_explicit_p (fncode))
2469 set_builtin_decl_declared_p (fncode, true);
2470 break;
2474 if (new_defines_function)
2475 /* If defining a function declared with other language
2476 linkage, use the previously declared language linkage. */
2477 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
2478 else if (types_match)
2480 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
2481 /* Don't clear out the arguments if we're just redeclaring a
2482 function. */
2483 if (DECL_ARGUMENTS (olddecl))
2484 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
2487 else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
2488 NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl);
2490 /* Now preserve various other info from the definition. */
2491 TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl);
2492 TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl);
2493 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
2494 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
2496 /* Warn about conflicting visibility specifications. */
2497 if (DECL_VISIBILITY_SPECIFIED (olddecl)
2498 && DECL_VISIBILITY_SPECIFIED (newdecl)
2499 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
2501 if (warning_at (DECL_SOURCE_LOCATION (newdecl), OPT_Wattributes,
2502 "%qD: visibility attribute ignored because it "
2503 "conflicts with previous declaration", newdecl))
2504 inform (DECL_SOURCE_LOCATION (olddecl),
2505 "previous declaration of %qD", olddecl);
2507 /* Choose the declaration which specified visibility. */
2508 if (DECL_VISIBILITY_SPECIFIED (olddecl))
2510 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
2511 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
2513 /* Init priority used to be merged from newdecl to olddecl by the memcpy,
2514 so keep this behavior. */
2515 if (VAR_P (newdecl) && DECL_HAS_INIT_PRIORITY_P (newdecl))
2517 SET_DECL_INIT_PRIORITY (olddecl, DECL_INIT_PRIORITY (newdecl));
2518 DECL_HAS_INIT_PRIORITY_P (olddecl) = 1;
2520 /* Likewise for DECL_ALIGN, DECL_USER_ALIGN and DECL_PACKED. */
2521 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
2523 SET_DECL_ALIGN (newdecl, DECL_ALIGN (olddecl));
2524 DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
2526 DECL_USER_ALIGN (olddecl) = DECL_USER_ALIGN (newdecl);
2527 if (DECL_WARN_IF_NOT_ALIGN (olddecl)
2528 > DECL_WARN_IF_NOT_ALIGN (newdecl))
2529 SET_DECL_WARN_IF_NOT_ALIGN (newdecl,
2530 DECL_WARN_IF_NOT_ALIGN (olddecl));
2531 if (TREE_CODE (newdecl) == FIELD_DECL)
2532 DECL_PACKED (olddecl) = DECL_PACKED (newdecl);
2534 /* The DECL_LANG_SPECIFIC information in OLDDECL will be replaced
2535 with that from NEWDECL below. */
2536 if (DECL_LANG_SPECIFIC (olddecl))
2538 gcc_assert (DECL_LANG_SPECIFIC (olddecl)
2539 != DECL_LANG_SPECIFIC (newdecl));
2540 ggc_free (DECL_LANG_SPECIFIC (olddecl));
2543 /* Merge the USED information. */
2544 if (TREE_USED (olddecl))
2545 TREE_USED (newdecl) = 1;
2546 else if (TREE_USED (newdecl))
2547 TREE_USED (olddecl) = 1;
2548 if (VAR_P (newdecl))
2550 if (DECL_READ_P (olddecl))
2551 DECL_READ_P (newdecl) = 1;
2552 else if (DECL_READ_P (newdecl))
2553 DECL_READ_P (olddecl) = 1;
2555 if (DECL_PRESERVE_P (olddecl))
2556 DECL_PRESERVE_P (newdecl) = 1;
2557 else if (DECL_PRESERVE_P (newdecl))
2558 DECL_PRESERVE_P (olddecl) = 1;
2560 /* Merge the DECL_FUNCTION_VERSIONED information. newdecl will be copied
2561 to olddecl and deleted. */
2562 if (TREE_CODE (newdecl) == FUNCTION_DECL
2563 && DECL_FUNCTION_VERSIONED (olddecl))
2565 /* Set the flag for newdecl so that it gets copied to olddecl. */
2566 DECL_FUNCTION_VERSIONED (newdecl) = 1;
2567 /* newdecl will be purged after copying to olddecl and is no longer
2568 a version. */
2569 cgraph_node::delete_function_version_by_decl (newdecl);
2572 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2574 int function_size;
2575 struct symtab_node *snode = symtab_node::get (olddecl);
2577 function_size = sizeof (struct tree_decl_common);
2579 memcpy ((char *) olddecl + sizeof (struct tree_common),
2580 (char *) newdecl + sizeof (struct tree_common),
2581 function_size - sizeof (struct tree_common));
2583 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2584 (char *) newdecl + sizeof (struct tree_decl_common),
2585 sizeof (struct tree_function_decl) - sizeof (struct tree_decl_common));
2587 /* Preserve symtab node mapping. */
2588 olddecl->decl_with_vis.symtab_node = snode;
2590 if (new_template_info)
2591 /* If newdecl is a template instantiation, it is possible that
2592 the following sequence of events has occurred:
2594 o A friend function was declared in a class template. The
2595 class template was instantiated.
2597 o The instantiation of the friend declaration was
2598 recorded on the instantiation list, and is newdecl.
2600 o Later, however, instantiate_class_template called pushdecl
2601 on the newdecl to perform name injection. But, pushdecl in
2602 turn called duplicate_decls when it discovered that another
2603 declaration of a global function with the same name already
2604 existed.
2606 o Here, in duplicate_decls, we decided to clobber newdecl.
2608 If we're going to do that, we'd better make sure that
2609 olddecl, and not newdecl, is on the list of
2610 instantiations so that if we try to do the instantiation
2611 again we won't get the clobbered declaration. */
2612 reregister_specialization (newdecl,
2613 new_template_info,
2614 olddecl);
2616 else
2618 size_t size = tree_code_size (TREE_CODE (newdecl));
2620 memcpy ((char *) olddecl + sizeof (struct tree_common),
2621 (char *) newdecl + sizeof (struct tree_common),
2622 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
2623 switch (TREE_CODE (newdecl))
2625 case LABEL_DECL:
2626 case VAR_DECL:
2627 case RESULT_DECL:
2628 case PARM_DECL:
2629 case FIELD_DECL:
2630 case TYPE_DECL:
2631 case CONST_DECL:
2633 struct symtab_node *snode = NULL;
2635 if (VAR_P (newdecl)
2636 && (TREE_STATIC (olddecl) || TREE_PUBLIC (olddecl)
2637 || DECL_EXTERNAL (olddecl)))
2638 snode = symtab_node::get (olddecl);
2639 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2640 (char *) newdecl + sizeof (struct tree_decl_common),
2641 size - sizeof (struct tree_decl_common)
2642 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
2643 if (VAR_P (newdecl))
2644 olddecl->decl_with_vis.symtab_node = snode;
2646 break;
2647 default:
2648 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2649 (char *) newdecl + sizeof (struct tree_decl_common),
2650 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common)
2651 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
2652 break;
2656 if (VAR_OR_FUNCTION_DECL_P (newdecl))
2658 if (DECL_EXTERNAL (olddecl)
2659 || TREE_PUBLIC (olddecl)
2660 || TREE_STATIC (olddecl))
2662 /* Merge the section attribute.
2663 We want to issue an error if the sections conflict but that must be
2664 done later in decl_attributes since we are called before attributes
2665 are assigned. */
2666 if (DECL_SECTION_NAME (newdecl) != NULL)
2667 set_decl_section_name (olddecl, DECL_SECTION_NAME (newdecl));
2669 if (DECL_ONE_ONLY (newdecl))
2671 struct symtab_node *oldsym, *newsym;
2672 if (TREE_CODE (olddecl) == FUNCTION_DECL)
2673 oldsym = cgraph_node::get_create (olddecl);
2674 else
2675 oldsym = varpool_node::get_create (olddecl);
2676 newsym = symtab_node::get (newdecl);
2677 oldsym->set_comdat_group (newsym->get_comdat_group ());
2681 if (VAR_P (newdecl)
2682 && CP_DECL_THREAD_LOCAL_P (newdecl))
2684 CP_DECL_THREAD_LOCAL_P (olddecl) = true;
2685 if (!processing_template_decl)
2686 set_decl_tls_model (olddecl, DECL_TLS_MODEL (newdecl));
2690 DECL_UID (olddecl) = olddecl_uid;
2691 if (olddecl_friend)
2692 DECL_FRIEND_P (olddecl) = 1;
2693 if (hidden_friend)
2695 DECL_ANTICIPATED (olddecl) = 1;
2696 DECL_HIDDEN_FRIEND_P (olddecl) = 1;
2699 /* NEWDECL contains the merged attribute lists.
2700 Update OLDDECL to be the same. */
2701 DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
2703 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
2704 so that encode_section_info has a chance to look at the new decl
2705 flags and attributes. */
2706 if (DECL_RTL_SET_P (olddecl)
2707 && (TREE_CODE (olddecl) == FUNCTION_DECL
2708 || (VAR_P (olddecl)
2709 && TREE_STATIC (olddecl))))
2710 make_decl_rtl (olddecl);
2712 /* The NEWDECL will no longer be needed. Because every out-of-class
2713 declaration of a member results in a call to duplicate_decls,
2714 freeing these nodes represents in a significant savings.
2716 Before releasing the node, be sore to remove function from symbol
2717 table that might have been inserted there to record comdat group.
2718 Be sure to however do not free DECL_STRUCT_FUNCTION because this
2719 structure is shared in between newdecl and oldecl. */
2720 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2721 DECL_STRUCT_FUNCTION (newdecl) = NULL;
2722 if (VAR_OR_FUNCTION_DECL_P (newdecl))
2724 struct symtab_node *snode = symtab_node::get (newdecl);
2725 if (snode)
2726 snode->remove ();
2729 /* Remove the associated constraints for newdecl, if any, before
2730 reclaiming memory. */
2731 if (flag_concepts)
2732 remove_constraints (newdecl);
2734 ggc_free (newdecl);
2736 return olddecl;
2739 /* Return zero if the declaration NEWDECL is valid
2740 when the declaration OLDDECL (assumed to be for the same name)
2741 has already been seen.
2742 Otherwise return an error message format string with a %s
2743 where the identifier should go. */
2745 static const char *
2746 redeclaration_error_message (tree newdecl, tree olddecl)
2748 if (TREE_CODE (newdecl) == TYPE_DECL)
2750 /* Because C++ can put things into name space for free,
2751 constructs like "typedef struct foo { ... } foo"
2752 would look like an erroneous redeclaration. */
2753 if (same_type_p (TREE_TYPE (newdecl), TREE_TYPE (olddecl)))
2754 return NULL;
2755 else
2756 return G_("redefinition of %q#D");
2758 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
2760 /* If this is a pure function, its olddecl will actually be
2761 the original initialization to `0' (which we force to call
2762 abort()). Don't complain about redefinition in this case. */
2763 if (DECL_LANG_SPECIFIC (olddecl) && DECL_PURE_VIRTUAL_P (olddecl)
2764 && DECL_INITIAL (olddecl) == NULL_TREE)
2765 return NULL;
2767 /* If both functions come from different namespaces, this is not
2768 a redeclaration - this is a conflict with a used function. */
2769 if (DECL_NAMESPACE_SCOPE_P (olddecl)
2770 && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl)
2771 && ! decls_match (olddecl, newdecl))
2772 return G_("%qD conflicts with used function");
2774 /* We'll complain about linkage mismatches in
2775 warn_extern_redeclared_static. */
2777 /* Defining the same name twice is no good. */
2778 if (decl_defined_p (olddecl)
2779 && decl_defined_p (newdecl))
2781 if (DECL_NAME (olddecl) == NULL_TREE)
2782 return G_("%q#D not declared in class");
2783 else if (!GNU_INLINE_P (olddecl)
2784 || GNU_INLINE_P (newdecl))
2785 return G_("redefinition of %q#D");
2788 if (DECL_DECLARED_INLINE_P (olddecl) && DECL_DECLARED_INLINE_P (newdecl))
2790 bool olda = GNU_INLINE_P (olddecl);
2791 bool newa = GNU_INLINE_P (newdecl);
2793 if (olda != newa)
2795 if (newa)
2796 return G_("%q+D redeclared inline with "
2797 "%<gnu_inline%> attribute");
2798 else
2799 return G_("%q+D redeclared inline without "
2800 "%<gnu_inline%> attribute");
2804 check_abi_tag_redeclaration
2805 (olddecl, lookup_attribute ("abi_tag", DECL_ATTRIBUTES (olddecl)),
2806 lookup_attribute ("abi_tag", DECL_ATTRIBUTES (newdecl)));
2808 return NULL;
2810 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2812 tree nt, ot;
2814 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
2816 if (COMPLETE_TYPE_P (TREE_TYPE (newdecl))
2817 && COMPLETE_TYPE_P (TREE_TYPE (olddecl)))
2818 return G_("redefinition of %q#D");
2819 return NULL;
2822 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) != FUNCTION_DECL
2823 || (DECL_TEMPLATE_RESULT (newdecl)
2824 == DECL_TEMPLATE_RESULT (olddecl)))
2825 return NULL;
2827 nt = DECL_TEMPLATE_RESULT (newdecl);
2828 if (DECL_TEMPLATE_INFO (nt))
2829 nt = DECL_TEMPLATE_RESULT (template_for_substitution (nt));
2830 ot = DECL_TEMPLATE_RESULT (olddecl);
2831 if (DECL_TEMPLATE_INFO (ot))
2832 ot = DECL_TEMPLATE_RESULT (template_for_substitution (ot));
2833 if (DECL_INITIAL (nt) && DECL_INITIAL (ot)
2834 && (!GNU_INLINE_P (ot) || GNU_INLINE_P (nt)))
2835 return G_("redefinition of %q#D");
2837 if (DECL_DECLARED_INLINE_P (ot) && DECL_DECLARED_INLINE_P (nt))
2839 bool olda = GNU_INLINE_P (ot);
2840 bool newa = GNU_INLINE_P (nt);
2842 if (olda != newa)
2844 if (newa)
2845 return G_("%q+D redeclared inline with "
2846 "%<gnu_inline%> attribute");
2847 else
2848 return G_("%q+D redeclared inline without "
2849 "%<gnu_inline%> attribute");
2853 /* Core issue #226 (C++0x):
2855 If a friend function template declaration specifies a
2856 default template-argument, that declaration shall be a
2857 definition and shall be the only declaration of the
2858 function template in the translation unit. */
2859 if ((cxx_dialect != cxx98)
2860 && TREE_CODE (ot) == FUNCTION_DECL && DECL_FRIEND_P (ot)
2861 && !check_default_tmpl_args (nt, DECL_TEMPLATE_PARMS (newdecl),
2862 /*is_primary=*/true,
2863 /*is_partial=*/false,
2864 /*is_friend_decl=*/2))
2865 return G_("redeclaration of friend %q#D "
2866 "may not have default template arguments");
2868 return NULL;
2870 else if (VAR_P (newdecl)
2871 && CP_DECL_THREAD_LOCAL_P (newdecl) != CP_DECL_THREAD_LOCAL_P (olddecl)
2872 && (! DECL_LANG_SPECIFIC (olddecl)
2873 || ! CP_DECL_THREADPRIVATE_P (olddecl)
2874 || CP_DECL_THREAD_LOCAL_P (newdecl)))
2876 /* Only variables can be thread-local, and all declarations must
2877 agree on this property. */
2878 if (CP_DECL_THREAD_LOCAL_P (newdecl))
2879 return G_("thread-local declaration of %q#D follows "
2880 "non-thread-local declaration");
2881 else
2882 return G_("non-thread-local declaration of %q#D follows "
2883 "thread-local declaration");
2885 else if (toplevel_bindings_p () || DECL_NAMESPACE_SCOPE_P (newdecl))
2887 /* The objects have been declared at namespace scope. If either
2888 is a member of an anonymous union, then this is an invalid
2889 redeclaration. For example:
2891 int i;
2892 union { int i; };
2894 is invalid. */
2895 if ((VAR_P (newdecl) && DECL_ANON_UNION_VAR_P (newdecl))
2896 || (VAR_P (olddecl) && DECL_ANON_UNION_VAR_P (olddecl)))
2897 return G_("redeclaration of %q#D");
2898 /* If at least one declaration is a reference, there is no
2899 conflict. For example:
2901 int i = 3;
2902 extern int i;
2904 is valid. */
2905 if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
2906 return NULL;
2908 /* Static data member declared outside a class definition
2909 if the variable is defined within the class with constexpr
2910 specifier is declaration rather than definition (and
2911 deprecated). */
2912 if (cxx_dialect >= cxx17
2913 && DECL_CLASS_SCOPE_P (olddecl)
2914 && DECL_DECLARED_CONSTEXPR_P (olddecl)
2915 && !DECL_INITIAL (newdecl))
2917 DECL_EXTERNAL (newdecl) = 1;
2918 /* For now, only warn with explicit -Wdeprecated. */
2919 if (global_options_set.x_warn_deprecated
2920 && warning_at (DECL_SOURCE_LOCATION (newdecl), OPT_Wdeprecated,
2921 "redundant redeclaration of %<constexpr%> static "
2922 "data member %qD", newdecl))
2923 inform (DECL_SOURCE_LOCATION (olddecl),
2924 "previous declaration of %qD", olddecl);
2925 return NULL;
2928 /* Reject two definitions. */
2929 return G_("redefinition of %q#D");
2931 else
2933 /* Objects declared with block scope: */
2934 /* Reject two definitions, and reject a definition
2935 together with an external reference. */
2936 if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl)))
2937 return G_("redeclaration of %q#D");
2938 return NULL;
2942 /* Hash and equality functions for the named_label table. */
2944 hashval_t
2945 named_label_hasher::hash (named_label_entry *ent)
2947 return DECL_UID (ent->label_decl);
2950 bool
2951 named_label_hasher::equal (named_label_entry *a, named_label_entry *b)
2953 return a->label_decl == b->label_decl;
2956 /* Create a new label, named ID. */
2958 static tree
2959 make_label_decl (tree id, int local_p)
2961 struct named_label_entry *ent;
2962 tree decl;
2964 decl = build_decl (input_location, LABEL_DECL, id, void_type_node);
2966 DECL_CONTEXT (decl) = current_function_decl;
2967 SET_DECL_MODE (decl, VOIDmode);
2968 C_DECLARED_LABEL_FLAG (decl) = local_p;
2970 /* Say where one reference is to the label, for the sake of the
2971 error if it is not defined. */
2972 DECL_SOURCE_LOCATION (decl) = input_location;
2974 /* Record the fact that this identifier is bound to this label. */
2975 SET_IDENTIFIER_LABEL_VALUE (id, decl);
2977 /* Create the label htab for the function on demand. */
2978 if (!named_labels)
2979 named_labels = hash_table<named_label_hasher>::create_ggc (13);
2981 /* Record this label on the list of labels used in this function.
2982 We do this before calling make_label_decl so that we get the
2983 IDENTIFIER_LABEL_VALUE before the new label is declared. */
2984 ent = ggc_cleared_alloc<named_label_entry> ();
2985 ent->label_decl = decl;
2987 named_label_entry **slot = named_labels->find_slot (ent, INSERT);
2988 gcc_assert (*slot == NULL);
2989 *slot = ent;
2991 return decl;
2994 /* Look for a label named ID in the current function. If one cannot
2995 be found, create one. (We keep track of used, but undefined,
2996 labels, and complain about them at the end of a function.) */
2998 static tree
2999 lookup_label_1 (tree id)
3001 tree decl;
3003 /* You can't use labels at global scope. */
3004 if (current_function_decl == NULL_TREE)
3006 error ("label %qE referenced outside of any function", id);
3007 return NULL_TREE;
3010 /* See if we've already got this label. */
3011 decl = IDENTIFIER_LABEL_VALUE (id);
3012 if (decl != NULL_TREE && DECL_CONTEXT (decl) == current_function_decl)
3013 return decl;
3015 decl = make_label_decl (id, /*local_p=*/0);
3016 return decl;
3019 /* Wrapper for lookup_label_1. */
3021 tree
3022 lookup_label (tree id)
3024 tree ret;
3025 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3026 ret = lookup_label_1 (id);
3027 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3028 return ret;
3031 /* Declare a local label named ID. */
3033 tree
3034 declare_local_label (tree id)
3036 tree decl;
3037 cp_label_binding bind;
3039 /* Add a new entry to the SHADOWED_LABELS list so that when we leave
3040 this scope we can restore the old value of IDENTIFIER_TYPE_VALUE. */
3041 bind.prev_value = IDENTIFIER_LABEL_VALUE (id);
3043 decl = make_label_decl (id, /*local_p=*/1);
3044 bind.label = decl;
3045 vec_safe_push (current_binding_level->shadowed_labels, bind);
3047 return decl;
3050 /* Returns nonzero if it is ill-formed to jump past the declaration of
3051 DECL. Returns 2 if it's also a real problem. */
3053 static int
3054 decl_jump_unsafe (tree decl)
3056 /* [stmt.dcl]/3: A program that jumps from a point where a local variable
3057 with automatic storage duration is not in scope to a point where it is
3058 in scope is ill-formed unless the variable has scalar type, class type
3059 with a trivial default constructor and a trivial destructor, a
3060 cv-qualified version of one of these types, or an array of one of the
3061 preceding types and is declared without an initializer (8.5). */
3062 tree type = TREE_TYPE (decl);
3064 if (!VAR_P (decl) || TREE_STATIC (decl)
3065 || type == error_mark_node)
3066 return 0;
3068 if (DECL_NONTRIVIALLY_INITIALIZED_P (decl)
3069 || variably_modified_type_p (type, NULL_TREE))
3070 return 2;
3072 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3073 return 1;
3075 return 0;
3078 /* A subroutine of check_previous_goto_1 and check_goto to identify a branch
3079 to the user. */
3081 static bool
3082 identify_goto (tree decl, location_t loc, const location_t *locus,
3083 diagnostic_t diag_kind)
3085 bool complained
3086 = (decl ? emit_diagnostic (diag_kind, loc, 0, "jump to label %qD", decl)
3087 : emit_diagnostic (diag_kind, loc, 0, "jump to case label"));
3088 if (complained && locus)
3089 inform (*locus, " from here");
3090 return complained;
3093 /* Check that a single previously seen jump to a newly defined label
3094 is OK. DECL is the LABEL_DECL or 0; LEVEL is the binding_level for
3095 the jump context; NAMES are the names in scope in LEVEL at the jump
3096 context; LOCUS is the source position of the jump or 0. Returns
3097 true if all is well. */
3099 static bool
3100 check_previous_goto_1 (tree decl, cp_binding_level* level, tree names,
3101 bool exited_omp, const location_t *locus)
3103 cp_binding_level *b;
3104 bool complained = false;
3105 int identified = 0;
3106 bool saw_eh = false, saw_omp = false, saw_tm = false, saw_cxif = false;
3108 if (exited_omp)
3110 complained = identify_goto (decl, input_location, locus, DK_ERROR);
3111 if (complained)
3112 inform (input_location, " exits OpenMP structured block");
3113 saw_omp = true;
3114 identified = 2;
3117 for (b = current_binding_level; b ; b = b->level_chain)
3119 tree new_decls, old_decls = (b == level ? names : NULL_TREE);
3121 for (new_decls = b->names; new_decls != old_decls;
3122 new_decls = (DECL_P (new_decls) ? DECL_CHAIN (new_decls)
3123 : TREE_CHAIN (new_decls)))
3125 int problem = decl_jump_unsafe (new_decls);
3126 if (! problem)
3127 continue;
3129 if (!identified)
3131 complained = identify_goto (decl, input_location, locus,
3132 DK_PERMERROR);
3133 identified = 1;
3135 if (complained)
3137 if (problem > 1)
3138 inform (DECL_SOURCE_LOCATION (new_decls),
3139 " crosses initialization of %q#D", new_decls);
3140 else
3141 inform (DECL_SOURCE_LOCATION (new_decls),
3142 " enters scope of %q#D which has "
3143 "non-trivial destructor", new_decls);
3147 if (b == level)
3148 break;
3149 if ((b->kind == sk_try || b->kind == sk_catch) && !saw_eh)
3151 if (identified < 2)
3153 complained = identify_goto (decl, input_location, locus,
3154 DK_ERROR);
3155 identified = 2;
3157 if (complained)
3159 if (b->kind == sk_try)
3160 inform (input_location, " enters try block");
3161 else
3162 inform (input_location, " enters catch block");
3164 saw_eh = true;
3166 if (b->kind == sk_omp && !saw_omp)
3168 if (identified < 2)
3170 complained = identify_goto (decl, input_location, locus,
3171 DK_ERROR);
3172 identified = 2;
3174 if (complained)
3175 inform (input_location, " enters OpenMP structured block");
3176 saw_omp = true;
3178 if (b->kind == sk_transaction && !saw_tm)
3180 if (identified < 2)
3182 complained = identify_goto (decl, input_location, locus,
3183 DK_ERROR);
3184 identified = 2;
3186 if (complained)
3187 inform (input_location,
3188 " enters synchronized or atomic statement");
3189 saw_tm = true;
3191 if (!saw_cxif && b->kind == sk_block
3192 && level_for_constexpr_if (b->level_chain))
3194 if (identified < 2)
3196 complained = identify_goto (decl, input_location, locus,
3197 DK_ERROR);
3198 identified = 2;
3200 if (complained)
3201 inform (EXPR_LOCATION (b->level_chain->this_entity),
3202 " enters constexpr if statement");
3203 saw_cxif = true;
3207 return !identified;
3210 static void
3211 check_previous_goto (tree decl, struct named_label_use_entry *use)
3213 check_previous_goto_1 (decl, use->binding_level,
3214 use->names_in_scope, use->in_omp_scope,
3215 &use->o_goto_locus);
3218 static bool
3219 check_switch_goto (cp_binding_level* level)
3221 return check_previous_goto_1 (NULL_TREE, level, level->names, false, NULL);
3224 /* Check that a new jump to a label DECL is OK. Called by
3225 finish_goto_stmt. */
3227 void
3228 check_goto (tree decl)
3230 struct named_label_entry *ent, dummy;
3231 bool saw_catch = false, complained = false;
3232 int identified = 0;
3233 tree bad;
3234 unsigned ix;
3236 /* We can't know where a computed goto is jumping.
3237 So we assume that it's OK. */
3238 if (TREE_CODE (decl) != LABEL_DECL)
3239 return;
3241 /* We didn't record any information about this label when we created it,
3242 and there's not much point since it's trivial to analyze as a return. */
3243 if (decl == cdtor_label)
3244 return;
3246 dummy.label_decl = decl;
3247 ent = named_labels->find (&dummy);
3248 gcc_assert (ent != NULL);
3250 /* If the label hasn't been defined yet, defer checking. */
3251 if (! DECL_INITIAL (decl))
3253 struct named_label_use_entry *new_use;
3255 /* Don't bother creating another use if the last goto had the
3256 same data, and will therefore create the same set of errors. */
3257 if (ent->uses
3258 && ent->uses->names_in_scope == current_binding_level->names)
3259 return;
3261 new_use = ggc_alloc<named_label_use_entry> ();
3262 new_use->binding_level = current_binding_level;
3263 new_use->names_in_scope = current_binding_level->names;
3264 new_use->o_goto_locus = input_location;
3265 new_use->in_omp_scope = false;
3267 new_use->next = ent->uses;
3268 ent->uses = new_use;
3269 return;
3272 if (ent->in_try_scope || ent->in_catch_scope || ent->in_transaction_scope
3273 || ent->in_constexpr_if
3274 || ent->in_omp_scope || !vec_safe_is_empty (ent->bad_decls))
3276 diagnostic_t diag_kind = DK_PERMERROR;
3277 if (ent->in_try_scope || ent->in_catch_scope || ent->in_constexpr_if
3278 || ent->in_transaction_scope || ent->in_omp_scope)
3279 diag_kind = DK_ERROR;
3280 complained = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
3281 &input_location, diag_kind);
3282 identified = 1 + (diag_kind == DK_ERROR);
3285 FOR_EACH_VEC_SAFE_ELT (ent->bad_decls, ix, bad)
3287 int u = decl_jump_unsafe (bad);
3289 if (u > 1 && DECL_ARTIFICIAL (bad))
3291 /* Can't skip init of __exception_info. */
3292 if (identified == 1)
3294 complained = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
3295 &input_location, DK_ERROR);
3296 identified = 2;
3298 if (complained)
3299 inform (DECL_SOURCE_LOCATION (bad), " enters catch block");
3300 saw_catch = true;
3302 else if (complained)
3304 if (u > 1)
3305 inform (DECL_SOURCE_LOCATION (bad),
3306 " skips initialization of %q#D", bad);
3307 else
3308 inform (DECL_SOURCE_LOCATION (bad),
3309 " enters scope of %q#D which has "
3310 "non-trivial destructor", bad);
3314 if (complained)
3316 if (ent->in_try_scope)
3317 inform (input_location, " enters try block");
3318 else if (ent->in_catch_scope && !saw_catch)
3319 inform (input_location, " enters catch block");
3320 else if (ent->in_transaction_scope)
3321 inform (input_location, " enters synchronized or atomic statement");
3322 else if (ent->in_constexpr_if)
3323 inform (input_location, " enters constexpr if statement");
3326 if (ent->in_omp_scope)
3328 if (complained)
3329 inform (input_location, " enters OpenMP structured block");
3331 else if (flag_openmp)
3333 cp_binding_level *b;
3334 for (b = current_binding_level; b ; b = b->level_chain)
3336 if (b == ent->binding_level)
3337 break;
3338 if (b->kind == sk_omp)
3340 if (identified < 2)
3342 complained = identify_goto (decl,
3343 DECL_SOURCE_LOCATION (decl),
3344 &input_location, DK_ERROR);
3345 identified = 2;
3347 if (complained)
3348 inform (input_location, " exits OpenMP structured block");
3349 break;
3355 /* Check that a return is ok wrt OpenMP structured blocks.
3356 Called by finish_return_stmt. Returns true if all is well. */
3358 bool
3359 check_omp_return (void)
3361 cp_binding_level *b;
3362 for (b = current_binding_level; b ; b = b->level_chain)
3363 if (b->kind == sk_omp)
3365 error ("invalid exit from OpenMP structured block");
3366 return false;
3368 else if (b->kind == sk_function_parms)
3369 break;
3370 return true;
3373 /* Define a label, specifying the location in the source file.
3374 Return the LABEL_DECL node for the label. */
3376 static tree
3377 define_label_1 (location_t location, tree name)
3379 struct named_label_entry *ent, dummy;
3380 cp_binding_level *p;
3381 tree decl;
3383 decl = lookup_label (name);
3385 dummy.label_decl = decl;
3386 ent = named_labels->find (&dummy);
3387 gcc_assert (ent != NULL);
3389 /* After labels, make any new cleanups in the function go into their
3390 own new (temporary) binding contour. */
3391 for (p = current_binding_level;
3392 p->kind != sk_function_parms;
3393 p = p->level_chain)
3394 p->more_cleanups_ok = 0;
3396 if (name == get_identifier ("wchar_t"))
3397 permerror (input_location, "label named wchar_t");
3399 if (DECL_INITIAL (decl) != NULL_TREE)
3401 error ("duplicate label %qD", decl);
3402 return error_mark_node;
3404 else
3406 struct named_label_use_entry *use;
3408 /* Mark label as having been defined. */
3409 DECL_INITIAL (decl) = error_mark_node;
3410 /* Say where in the source. */
3411 DECL_SOURCE_LOCATION (decl) = location;
3413 ent->binding_level = current_binding_level;
3414 ent->names_in_scope = current_binding_level->names;
3416 for (use = ent->uses; use ; use = use->next)
3417 check_previous_goto (decl, use);
3418 ent->uses = NULL;
3421 return decl;
3424 /* Wrapper for define_label_1. */
3426 tree
3427 define_label (location_t location, tree name)
3429 tree ret;
3430 bool running = timevar_cond_start (TV_NAME_LOOKUP);
3431 ret = define_label_1 (location, name);
3432 timevar_cond_stop (TV_NAME_LOOKUP, running);
3433 return ret;
3437 struct cp_switch
3439 cp_binding_level *level;
3440 struct cp_switch *next;
3441 /* The SWITCH_STMT being built. */
3442 tree switch_stmt;
3443 /* A splay-tree mapping the low element of a case range to the high
3444 element, or NULL_TREE if there is no high element. Used to
3445 determine whether or not a new case label duplicates an old case
3446 label. We need a tree, rather than simply a hash table, because
3447 of the GNU case range extension. */
3448 splay_tree cases;
3449 /* Remember whether there was a case value that is outside the
3450 range of the original type of the controlling expression. */
3451 bool outside_range_p;
3454 /* A stack of the currently active switch statements. The innermost
3455 switch statement is on the top of the stack. There is no need to
3456 mark the stack for garbage collection because it is only active
3457 during the processing of the body of a function, and we never
3458 collect at that point. */
3460 static struct cp_switch *switch_stack;
3462 /* Called right after a switch-statement condition is parsed.
3463 SWITCH_STMT is the switch statement being parsed. */
3465 void
3466 push_switch (tree switch_stmt)
3468 struct cp_switch *p = XNEW (struct cp_switch);
3469 p->level = current_binding_level;
3470 p->next = switch_stack;
3471 p->switch_stmt = switch_stmt;
3472 p->cases = splay_tree_new (case_compare, NULL, NULL);
3473 p->outside_range_p = false;
3474 switch_stack = p;
3477 void
3478 pop_switch (void)
3480 struct cp_switch *cs = switch_stack;
3481 location_t switch_location;
3483 /* Emit warnings as needed. */
3484 switch_location = EXPR_LOC_OR_LOC (cs->switch_stmt, input_location);
3485 const bool bool_cond_p
3486 = (SWITCH_STMT_TYPE (cs->switch_stmt)
3487 && TREE_CODE (SWITCH_STMT_TYPE (cs->switch_stmt)) == BOOLEAN_TYPE);
3488 if (!processing_template_decl)
3489 c_do_switch_warnings (cs->cases, switch_location,
3490 SWITCH_STMT_TYPE (cs->switch_stmt),
3491 SWITCH_STMT_COND (cs->switch_stmt),
3492 bool_cond_p, cs->outside_range_p);
3494 splay_tree_delete (cs->cases);
3495 switch_stack = switch_stack->next;
3496 free (cs);
3499 /* Convert a case constant VALUE in a switch to the type TYPE of the switch
3500 condition. Note that if TYPE and VALUE are already integral we don't
3501 really do the conversion because the language-independent
3502 warning/optimization code will work better that way. */
3504 static tree
3505 case_conversion (tree type, tree value)
3507 if (value == NULL_TREE)
3508 return value;
3510 if (cxx_dialect >= cxx11
3511 && (SCOPED_ENUM_P (type)
3512 || !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (value))))
3514 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
3515 type = type_promotes_to (type);
3516 value = (perform_implicit_conversion_flags
3517 (type, value, tf_warning_or_error,
3518 LOOKUP_IMPLICIT | LOOKUP_NO_NON_INTEGRAL));
3520 return cxx_constant_value (value);
3523 /* Note that we've seen a definition of a case label, and complain if this
3524 is a bad place for one. */
3526 tree
3527 finish_case_label (location_t loc, tree low_value, tree high_value)
3529 tree cond, r;
3530 cp_binding_level *p;
3531 tree type;
3533 if (processing_template_decl)
3535 tree label;
3537 /* For templates, just add the case label; we'll do semantic
3538 analysis at instantiation-time. */
3539 label = build_decl (loc, LABEL_DECL, NULL_TREE, NULL_TREE);
3540 return add_stmt (build_case_label (low_value, high_value, label));
3543 /* Find the condition on which this switch statement depends. */
3544 cond = SWITCH_STMT_COND (switch_stack->switch_stmt);
3545 if (cond && TREE_CODE (cond) == TREE_LIST)
3546 cond = TREE_VALUE (cond);
3548 if (!check_switch_goto (switch_stack->level))
3549 return error_mark_node;
3551 type = SWITCH_STMT_TYPE (switch_stack->switch_stmt);
3553 low_value = case_conversion (type, low_value);
3554 high_value = case_conversion (type, high_value);
3556 r = c_add_case_label (loc, switch_stack->cases, cond, type,
3557 low_value, high_value,
3558 &switch_stack->outside_range_p);
3560 /* After labels, make any new cleanups in the function go into their
3561 own new (temporary) binding contour. */
3562 for (p = current_binding_level;
3563 p->kind != sk_function_parms;
3564 p = p->level_chain)
3565 p->more_cleanups_ok = 0;
3567 return r;
3570 struct typename_info {
3571 tree scope;
3572 tree name;
3573 tree template_id;
3574 bool enum_p;
3575 bool class_p;
3578 struct typename_hasher : ggc_ptr_hash<tree_node>
3580 typedef typename_info *compare_type;
3582 /* Hash a TYPENAME_TYPE. */
3584 static hashval_t
3585 hash (tree t)
3587 hashval_t hash;
3589 hash = (htab_hash_pointer (TYPE_CONTEXT (t))
3590 ^ htab_hash_pointer (TYPE_IDENTIFIER (t)));
3592 return hash;
3595 /* Compare two TYPENAME_TYPEs. */
3597 static bool
3598 equal (tree t1, const typename_info *t2)
3600 return (TYPE_IDENTIFIER (t1) == t2->name
3601 && TYPE_CONTEXT (t1) == t2->scope
3602 && TYPENAME_TYPE_FULLNAME (t1) == t2->template_id
3603 && TYPENAME_IS_ENUM_P (t1) == t2->enum_p
3604 && TYPENAME_IS_CLASS_P (t1) == t2->class_p);
3608 /* Build a TYPENAME_TYPE. If the type is `typename T::t', CONTEXT is
3609 the type of `T', NAME is the IDENTIFIER_NODE for `t'.
3611 Returns the new TYPENAME_TYPE. */
3613 static GTY (()) hash_table<typename_hasher> *typename_htab;
3615 tree
3616 build_typename_type (tree context, tree name, tree fullname,
3617 enum tag_types tag_type)
3619 tree t;
3620 tree d;
3621 typename_info ti;
3622 tree *e;
3623 hashval_t hash;
3625 if (typename_htab == NULL)
3626 typename_htab = hash_table<typename_hasher>::create_ggc (61);
3628 ti.scope = FROB_CONTEXT (context);
3629 ti.name = name;
3630 ti.template_id = fullname;
3631 ti.enum_p = tag_type == enum_type;
3632 ti.class_p = (tag_type == class_type
3633 || tag_type == record_type
3634 || tag_type == union_type);
3635 hash = (htab_hash_pointer (ti.scope)
3636 ^ htab_hash_pointer (ti.name));
3638 /* See if we already have this type. */
3639 e = typename_htab->find_slot_with_hash (&ti, hash, INSERT);
3640 if (*e)
3641 t = *e;
3642 else
3644 /* Build the TYPENAME_TYPE. */
3645 t = cxx_make_type (TYPENAME_TYPE);
3646 TYPE_CONTEXT (t) = ti.scope;
3647 TYPENAME_TYPE_FULLNAME (t) = ti.template_id;
3648 TYPENAME_IS_ENUM_P (t) = ti.enum_p;
3649 TYPENAME_IS_CLASS_P (t) = ti.class_p;
3651 /* Build the corresponding TYPE_DECL. */
3652 d = build_decl (input_location, TYPE_DECL, name, t);
3653 TYPE_NAME (TREE_TYPE (d)) = d;
3654 TYPE_STUB_DECL (TREE_TYPE (d)) = d;
3655 DECL_CONTEXT (d) = FROB_CONTEXT (context);
3656 DECL_ARTIFICIAL (d) = 1;
3658 /* Store it in the hash table. */
3659 *e = t;
3661 /* TYPENAME_TYPEs must always be compared structurally, because
3662 they may or may not resolve down to another type depending on
3663 the currently open classes. */
3664 SET_TYPE_STRUCTURAL_EQUALITY (t);
3667 return t;
3670 /* Resolve `typename CONTEXT::NAME'. TAG_TYPE indicates the tag
3671 provided to name the type. Returns an appropriate type, unless an
3672 error occurs, in which case error_mark_node is returned. If we
3673 locate a non-artificial TYPE_DECL and TF_KEEP_TYPE_DECL is set, we
3674 return that, rather than the _TYPE it corresponds to, in other
3675 cases we look through the type decl. If TF_ERROR is set, complain
3676 about errors, otherwise be quiet. */
3678 tree
3679 make_typename_type (tree context, tree name, enum tag_types tag_type,
3680 tsubst_flags_t complain)
3682 tree fullname;
3683 tree t;
3684 bool want_template;
3686 if (name == error_mark_node
3687 || context == NULL_TREE
3688 || context == error_mark_node)
3689 return error_mark_node;
3691 if (TYPE_P (name))
3693 if (!(TYPE_LANG_SPECIFIC (name)
3694 && (CLASSTYPE_IS_TEMPLATE (name)
3695 || CLASSTYPE_USE_TEMPLATE (name))))
3696 name = TYPE_IDENTIFIER (name);
3697 else
3698 /* Create a TEMPLATE_ID_EXPR for the type. */
3699 name = build_nt (TEMPLATE_ID_EXPR,
3700 CLASSTYPE_TI_TEMPLATE (name),
3701 CLASSTYPE_TI_ARGS (name));
3703 else if (TREE_CODE (name) == TYPE_DECL)
3704 name = DECL_NAME (name);
3706 fullname = name;
3708 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3710 name = TREE_OPERAND (name, 0);
3711 if (DECL_TYPE_TEMPLATE_P (name))
3712 name = TREE_OPERAND (fullname, 0) = DECL_NAME (name);
3713 if (TREE_CODE (name) != IDENTIFIER_NODE)
3715 if (complain & tf_error)
3716 error ("%qD is not a type", name);
3717 return error_mark_node;
3720 if (TREE_CODE (name) == TEMPLATE_DECL)
3722 if (complain & tf_error)
3723 error ("%qD used without template parameters", name);
3724 return error_mark_node;
3726 gcc_assert (identifier_p (name));
3727 gcc_assert (TYPE_P (context));
3729 if (!MAYBE_CLASS_TYPE_P (context))
3731 if (complain & tf_error)
3732 error ("%q#T is not a class", context);
3733 return error_mark_node;
3736 /* When the CONTEXT is a dependent type, NAME could refer to a
3737 dependent base class of CONTEXT. But look inside it anyway
3738 if CONTEXT is a currently open scope, in case it refers to a
3739 member of the current instantiation or a non-dependent base;
3740 lookup will stop when we hit a dependent base. */
3741 if (!dependent_scope_p (context))
3742 /* We should only set WANT_TYPE when we're a nested typename type.
3743 Then we can give better diagnostics if we find a non-type. */
3744 t = lookup_field (context, name, 2, /*want_type=*/true);
3745 else
3746 t = NULL_TREE;
3748 if ((!t || TREE_CODE (t) == TREE_LIST) && dependent_type_p (context))
3749 return build_typename_type (context, name, fullname, tag_type);
3751 want_template = TREE_CODE (fullname) == TEMPLATE_ID_EXPR;
3753 if (!t)
3755 if (complain & tf_error)
3757 if (!COMPLETE_TYPE_P (context))
3758 cxx_incomplete_type_error (NULL_TREE, context);
3759 else
3760 error (want_template ? G_("no class template named %q#T in %q#T")
3761 : G_("no type named %q#T in %q#T"), name, context);
3763 return error_mark_node;
3766 /* Pull out the template from an injected-class-name (or multiple). */
3767 if (want_template)
3768 t = maybe_get_template_decl_from_type_decl (t);
3770 if (TREE_CODE (t) == TREE_LIST)
3772 if (complain & tf_error)
3774 error ("lookup of %qT in %qT is ambiguous", name, context);
3775 print_candidates (t);
3777 return error_mark_node;
3780 if (want_template && !DECL_TYPE_TEMPLATE_P (t))
3782 if (complain & tf_error)
3783 error ("%<typename %T::%D%> names %q#T, which is not a class template",
3784 context, name, t);
3785 return error_mark_node;
3787 if (!want_template && TREE_CODE (t) != TYPE_DECL)
3789 if (complain & tf_error)
3790 error ("%<typename %T::%D%> names %q#T, which is not a type",
3791 context, name, t);
3792 return error_mark_node;
3795 if (!perform_or_defer_access_check (TYPE_BINFO (context), t, t, complain))
3796 return error_mark_node;
3798 /* If we are currently parsing a template and if T is a typedef accessed
3799 through CONTEXT then we need to remember and check access of T at
3800 template instantiation time. */
3801 add_typedef_to_current_template_for_access_check (t, context, input_location);
3803 if (want_template)
3804 return lookup_template_class (t, TREE_OPERAND (fullname, 1),
3805 NULL_TREE, context,
3806 /*entering_scope=*/0,
3807 complain | tf_user);
3809 if (DECL_ARTIFICIAL (t) || !(complain & tf_keep_type_decl))
3810 t = TREE_TYPE (t);
3812 maybe_record_typedef_use (t);
3814 return t;
3817 /* Resolve `CONTEXT::template NAME'. Returns a TEMPLATE_DECL if the name
3818 can be resolved or an UNBOUND_CLASS_TEMPLATE, unless an error occurs,
3819 in which case error_mark_node is returned.
3821 If PARM_LIST is non-NULL, also make sure that the template parameter
3822 list of TEMPLATE_DECL matches.
3824 If COMPLAIN zero, don't complain about any errors that occur. */
3826 tree
3827 make_unbound_class_template (tree context, tree name, tree parm_list,
3828 tsubst_flags_t complain)
3830 tree t;
3831 tree d;
3833 if (TYPE_P (name))
3834 name = TYPE_IDENTIFIER (name);
3835 else if (DECL_P (name))
3836 name = DECL_NAME (name);
3837 gcc_assert (identifier_p (name));
3839 if (!dependent_type_p (context)
3840 || currently_open_class (context))
3842 tree tmpl = NULL_TREE;
3844 if (MAYBE_CLASS_TYPE_P (context))
3845 tmpl = lookup_field (context, name, 0, false);
3847 if (tmpl && TREE_CODE (tmpl) == TYPE_DECL)
3848 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
3850 if (!tmpl || !DECL_TYPE_TEMPLATE_P (tmpl))
3852 if (complain & tf_error)
3853 error ("no class template named %q#T in %q#T", name, context);
3854 return error_mark_node;
3857 if (parm_list
3858 && !comp_template_parms (DECL_TEMPLATE_PARMS (tmpl), parm_list))
3860 if (complain & tf_error)
3862 error ("template parameters do not match template %qD", tmpl);
3863 inform (DECL_SOURCE_LOCATION (tmpl),
3864 "%qD declared here", tmpl);
3866 return error_mark_node;
3869 if (!perform_or_defer_access_check (TYPE_BINFO (context), tmpl, tmpl,
3870 complain))
3871 return error_mark_node;
3873 return tmpl;
3876 /* Build the UNBOUND_CLASS_TEMPLATE. */
3877 t = cxx_make_type (UNBOUND_CLASS_TEMPLATE);
3878 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
3879 TREE_TYPE (t) = NULL_TREE;
3880 SET_TYPE_STRUCTURAL_EQUALITY (t);
3882 /* Build the corresponding TEMPLATE_DECL. */
3883 d = build_decl (input_location, TEMPLATE_DECL, name, t);
3884 TYPE_NAME (TREE_TYPE (d)) = d;
3885 TYPE_STUB_DECL (TREE_TYPE (d)) = d;
3886 DECL_CONTEXT (d) = FROB_CONTEXT (context);
3887 DECL_ARTIFICIAL (d) = 1;
3888 DECL_TEMPLATE_PARMS (d) = parm_list;
3890 return t;
3895 /* Push the declarations of builtin types into the global namespace.
3896 RID_INDEX is the index of the builtin type in the array
3897 RID_POINTERS. NAME is the name used when looking up the builtin
3898 type. TYPE is the _TYPE node for the builtin type.
3900 The calls to set_global_binding below should be
3901 eliminated. Built-in types should not be looked up name; their
3902 names are keywords that the parser can recognize. However, there
3903 is code in c-common.c that uses identifier_global_value to look up
3904 built-in types by name. */
3906 void
3907 record_builtin_type (enum rid rid_index,
3908 const char* name,
3909 tree type)
3911 tree decl = NULL_TREE;
3913 if (name)
3915 tree tname = get_identifier (name);
3916 tree tdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, tname, type);
3917 DECL_ARTIFICIAL (tdecl) = 1;
3918 set_global_binding (tdecl);
3919 decl = tdecl;
3922 if ((int) rid_index < (int) RID_MAX)
3923 if (tree rname = ridpointers[(int) rid_index])
3924 if (!decl || DECL_NAME (decl) != rname)
3926 tree rdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, rname, type);
3927 DECL_ARTIFICIAL (rdecl) = 1;
3928 set_global_binding (rdecl);
3929 if (!decl)
3930 decl = rdecl;
3933 if (decl)
3935 if (!TYPE_NAME (type))
3936 TYPE_NAME (type) = decl;
3937 debug_hooks->type_decl (decl, 0);
3941 /* Push a type into the namespace so that the back ends ignore it. */
3943 static void
3944 record_unknown_type (tree type, const char* name)
3946 tree decl = pushdecl (build_decl (UNKNOWN_LOCATION,
3947 TYPE_DECL, get_identifier (name), type));
3948 /* Make sure the "unknown type" typedecl gets ignored for debug info. */
3949 DECL_IGNORED_P (decl) = 1;
3950 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
3951 TYPE_SIZE (type) = TYPE_SIZE (void_type_node);
3952 SET_TYPE_ALIGN (type, 1);
3953 TYPE_USER_ALIGN (type) = 0;
3954 SET_TYPE_MODE (type, TYPE_MODE (void_type_node));
3957 /* Create all the predefined identifiers. */
3959 static void
3960 initialize_predefined_identifiers (void)
3962 struct predefined_identifier
3964 const char *name; /* Name. */
3965 tree *node; /* Node to store it in. */
3966 cp_identifier_kind kind; /* Kind of identifier. */
3969 /* A table of identifiers to create at startup. */
3970 static const predefined_identifier predefined_identifiers[] = {
3971 {"C++", &lang_name_cplusplus, cik_normal},
3972 {"C", &lang_name_c, cik_normal},
3973 /* Some of these names have a trailing space so that it is
3974 impossible for them to conflict with names written by users. */
3975 {"__ct ", &ctor_identifier, cik_ctor},
3976 {"__ct_base ", &base_ctor_identifier, cik_ctor},
3977 {"__ct_comp ", &complete_ctor_identifier, cik_ctor},
3978 {"__dt ", &dtor_identifier, cik_dtor},
3979 {"__dt_base ", &base_dtor_identifier, cik_dtor},
3980 {"__dt_comp ", &complete_dtor_identifier, cik_dtor},
3981 {"__dt_del ", &deleting_dtor_identifier, cik_dtor},
3982 {"__conv_op ", &conv_op_identifier, cik_conv_op},
3983 {"__in_chrg", &in_charge_identifier, cik_normal},
3984 {"this", &this_identifier, cik_normal},
3985 {"__delta", &delta_identifier, cik_normal},
3986 {"__pfn", &pfn_identifier, cik_normal},
3987 {"_vptr", &vptr_identifier, cik_normal},
3988 {"__vtt_parm", &vtt_parm_identifier, cik_normal},
3989 {"::", &global_identifier, cik_normal},
3990 {"std", &std_identifier, cik_normal},
3991 /* The demangler expects anonymous namespaces to be called
3992 something starting with '_GLOBAL__N_'. It no longer needs
3993 to be unique to the TU. */
3994 {"_GLOBAL__N_1", &anon_identifier, cik_normal},
3995 {"auto", &auto_identifier, cik_normal},
3996 {"decltype(auto)", &decltype_auto_identifier, cik_normal},
3997 {"initializer_list", &init_list_identifier, cik_normal},
3998 {NULL, NULL, cik_normal}
4001 for (const predefined_identifier *pid = predefined_identifiers;
4002 pid->name; ++pid)
4004 *pid->node = get_identifier (pid->name);
4005 /* Some of these identifiers already have a special kind. */
4006 if (pid->kind != cik_normal)
4007 set_identifier_kind (*pid->node, pid->kind);
4011 /* Create the predefined scalar types of C,
4012 and some nodes representing standard constants (0, 1, (void *)0).
4013 Initialize the global binding level.
4014 Make definitions for built-in primitive functions. */
4016 void
4017 cxx_init_decl_processing (void)
4019 tree void_ftype;
4020 tree void_ftype_ptr;
4022 /* Create all the identifiers we need. */
4023 initialize_predefined_identifiers ();
4025 /* Create the global variables. */
4026 push_to_top_level ();
4028 current_function_decl = NULL_TREE;
4029 current_binding_level = NULL;
4030 /* Enter the global namespace. */
4031 gcc_assert (global_namespace == NULL_TREE);
4032 global_namespace = build_lang_decl (NAMESPACE_DECL, global_identifier,
4033 void_type_node);
4034 TREE_PUBLIC (global_namespace) = 1;
4035 DECL_CONTEXT (global_namespace)
4036 = build_translation_unit_decl (get_identifier (main_input_filename));
4037 debug_hooks->register_main_translation_unit
4038 (DECL_CONTEXT (global_namespace));
4039 begin_scope (sk_namespace, global_namespace);
4040 current_namespace = global_namespace;
4042 if (flag_visibility_ms_compat)
4043 default_visibility = VISIBILITY_HIDDEN;
4045 /* Initially, C. */
4046 current_lang_name = lang_name_c;
4048 /* Create the `std' namespace. */
4049 push_namespace (std_identifier);
4050 std_node = current_namespace;
4051 pop_namespace ();
4053 flag_noexcept_type = (cxx_dialect >= cxx17);
4055 c_common_nodes_and_builtins ();
4057 integer_two_node = build_int_cst (NULL_TREE, 2);
4059 /* Guess at the initial static decls size. */
4060 vec_alloc (static_decls, 500);
4062 /* ... and keyed classes. */
4063 vec_alloc (keyed_classes, 100);
4065 record_builtin_type (RID_BOOL, "bool", boolean_type_node);
4066 truthvalue_type_node = boolean_type_node;
4067 truthvalue_false_node = boolean_false_node;
4068 truthvalue_true_node = boolean_true_node;
4070 empty_except_spec = build_tree_list (NULL_TREE, NULL_TREE);
4071 noexcept_true_spec = build_tree_list (boolean_true_node, NULL_TREE);
4072 noexcept_false_spec = build_tree_list (boolean_false_node, NULL_TREE);
4073 noexcept_deferred_spec = build_tree_list (make_node (DEFERRED_NOEXCEPT),
4074 NULL_TREE);
4076 #if 0
4077 record_builtin_type (RID_MAX, NULL, string_type_node);
4078 #endif
4080 delta_type_node = ptrdiff_type_node;
4081 vtable_index_type = ptrdiff_type_node;
4083 vtt_parm_type = build_pointer_type (const_ptr_type_node);
4084 void_ftype = build_function_type_list (void_type_node, NULL_TREE);
4085 void_ftype_ptr = build_function_type_list (void_type_node,
4086 ptr_type_node, NULL_TREE);
4087 void_ftype_ptr
4088 = build_exception_variant (void_ftype_ptr, empty_except_spec);
4090 /* Create the conversion operator marker. This operator's DECL_NAME
4091 is in the identifier table, so we can use identifier equality to
4092 find it. */
4093 conv_op_marker = build_lang_decl (FUNCTION_DECL, conv_op_identifier,
4094 void_ftype);
4096 /* C++ extensions */
4098 unknown_type_node = make_node (LANG_TYPE);
4099 record_unknown_type (unknown_type_node, "unknown type");
4101 /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node. */
4102 TREE_TYPE (unknown_type_node) = unknown_type_node;
4104 /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same
4105 result. */
4106 TYPE_POINTER_TO (unknown_type_node) = unknown_type_node;
4107 TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node;
4109 init_list_type_node = make_node (LANG_TYPE);
4110 record_unknown_type (init_list_type_node, "init list");
4113 /* Make sure we get a unique function type, so we can give
4114 its pointer type a name. (This wins for gdb.) */
4115 tree vfunc_type = make_node (FUNCTION_TYPE);
4116 TREE_TYPE (vfunc_type) = integer_type_node;
4117 TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
4118 layout_type (vfunc_type);
4120 vtable_entry_type = build_pointer_type (vfunc_type);
4122 record_builtin_type (RID_MAX, "__vtbl_ptr_type", vtable_entry_type);
4124 vtbl_type_node
4125 = build_cplus_array_type (vtable_entry_type, NULL_TREE);
4126 layout_type (vtbl_type_node);
4127 vtbl_type_node = cp_build_qualified_type (vtbl_type_node, TYPE_QUAL_CONST);
4128 record_builtin_type (RID_MAX, NULL, vtbl_type_node);
4129 vtbl_ptr_type_node = build_pointer_type (vtable_entry_type);
4130 layout_type (vtbl_ptr_type_node);
4131 record_builtin_type (RID_MAX, NULL, vtbl_ptr_type_node);
4133 push_namespace (get_identifier ("__cxxabiv1"));
4134 abi_node = current_namespace;
4135 pop_namespace ();
4137 global_type_node = make_node (LANG_TYPE);
4138 record_unknown_type (global_type_node, "global type");
4140 any_targ_node = make_node (LANG_TYPE);
4141 record_unknown_type (any_targ_node, "any type");
4143 /* Now, C++. */
4144 current_lang_name = lang_name_cplusplus;
4146 if (aligned_new_threshold > 1
4147 && !pow2p_hwi (aligned_new_threshold))
4149 error ("-faligned-new=%d is not a power of two", aligned_new_threshold);
4150 aligned_new_threshold = 1;
4152 if (aligned_new_threshold == -1)
4153 aligned_new_threshold = (cxx_dialect >= cxx17) ? 1 : 0;
4154 if (aligned_new_threshold == 1)
4155 aligned_new_threshold = malloc_alignment () / BITS_PER_UNIT;
4158 tree newattrs, extvisattr;
4159 tree newtype, deltype;
4160 tree ptr_ftype_sizetype;
4161 tree new_eh_spec;
4163 ptr_ftype_sizetype
4164 = build_function_type_list (ptr_type_node, size_type_node, NULL_TREE);
4165 if (cxx_dialect == cxx98)
4167 tree bad_alloc_id;
4168 tree bad_alloc_type_node;
4169 tree bad_alloc_decl;
4171 push_namespace (std_identifier);
4172 bad_alloc_id = get_identifier ("bad_alloc");
4173 bad_alloc_type_node = make_class_type (RECORD_TYPE);
4174 TYPE_CONTEXT (bad_alloc_type_node) = current_namespace;
4175 bad_alloc_decl
4176 = create_implicit_typedef (bad_alloc_id, bad_alloc_type_node);
4177 DECL_CONTEXT (bad_alloc_decl) = current_namespace;
4178 pop_namespace ();
4180 new_eh_spec
4181 = add_exception_specifier (NULL_TREE, bad_alloc_type_node, -1);
4183 else
4184 new_eh_spec = noexcept_false_spec;
4186 /* Ensure attribs.c is initialized. */
4187 init_attributes ();
4189 /* Ensure constraint.cc is initialized. */
4190 init_constraint_processing ();
4192 extvisattr = build_tree_list (get_identifier ("externally_visible"),
4193 NULL_TREE);
4194 newattrs = tree_cons (get_identifier ("alloc_size"),
4195 build_tree_list (NULL_TREE, integer_one_node),
4196 extvisattr);
4197 newtype = cp_build_type_attribute_variant (ptr_ftype_sizetype, newattrs);
4198 newtype = build_exception_variant (newtype, new_eh_spec);
4199 deltype = cp_build_type_attribute_variant (void_ftype_ptr, extvisattr);
4200 deltype = build_exception_variant (deltype, empty_except_spec);
4201 tree opnew = push_cp_library_fn (NEW_EXPR, newtype, 0);
4202 DECL_IS_MALLOC (opnew) = 1;
4203 DECL_IS_OPERATOR_NEW (opnew) = 1;
4204 opnew = push_cp_library_fn (VEC_NEW_EXPR, newtype, 0);
4205 DECL_IS_MALLOC (opnew) = 1;
4206 DECL_IS_OPERATOR_NEW (opnew) = 1;
4207 push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
4208 push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
4209 if (flag_sized_deallocation)
4211 /* Also push the sized deallocation variants:
4212 void operator delete(void*, std::size_t) throw();
4213 void operator delete[](void*, std::size_t) throw(); */
4214 tree void_ftype_ptr_size
4215 = build_function_type_list (void_type_node, ptr_type_node,
4216 size_type_node, NULL_TREE);
4217 deltype = cp_build_type_attribute_variant (void_ftype_ptr_size,
4218 extvisattr);
4219 deltype = build_exception_variant (deltype, empty_except_spec);
4220 push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
4221 push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
4224 if (aligned_new_threshold)
4226 push_namespace (std_identifier);
4227 tree align_id = get_identifier ("align_val_t");
4228 align_type_node = start_enum (align_id, NULL_TREE, size_type_node,
4229 NULL_TREE, /*scoped*/true, NULL);
4230 pop_namespace ();
4232 /* operator new (size_t, align_val_t); */
4233 newtype = build_function_type_list (ptr_type_node, size_type_node,
4234 align_type_node, NULL_TREE);
4235 newtype = cp_build_type_attribute_variant (newtype, newattrs);
4236 newtype = build_exception_variant (newtype, new_eh_spec);
4237 opnew = push_cp_library_fn (NEW_EXPR, newtype, 0);
4238 DECL_IS_MALLOC (opnew) = 1;
4239 DECL_IS_OPERATOR_NEW (opnew) = 1;
4240 opnew = push_cp_library_fn (VEC_NEW_EXPR, newtype, 0);
4241 DECL_IS_MALLOC (opnew) = 1;
4242 DECL_IS_OPERATOR_NEW (opnew) = 1;
4244 /* operator delete (void *, align_val_t); */
4245 deltype = build_function_type_list (void_type_node, ptr_type_node,
4246 align_type_node, NULL_TREE);
4247 deltype = cp_build_type_attribute_variant (deltype, extvisattr);
4248 deltype = build_exception_variant (deltype, empty_except_spec);
4249 push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
4250 push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
4252 if (flag_sized_deallocation)
4254 /* operator delete (void *, size_t, align_val_t); */
4255 deltype = build_function_type_list (void_type_node, ptr_type_node,
4256 size_type_node, align_type_node,
4257 NULL_TREE);
4258 deltype = cp_build_type_attribute_variant (deltype, extvisattr);
4259 deltype = build_exception_variant (deltype, empty_except_spec);
4260 push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
4261 push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
4265 nullptr_type_node = make_node (NULLPTR_TYPE);
4266 TYPE_SIZE (nullptr_type_node) = bitsize_int (GET_MODE_BITSIZE (ptr_mode));
4267 TYPE_SIZE_UNIT (nullptr_type_node) = size_int (GET_MODE_SIZE (ptr_mode));
4268 TYPE_UNSIGNED (nullptr_type_node) = 1;
4269 TYPE_PRECISION (nullptr_type_node) = GET_MODE_BITSIZE (ptr_mode);
4270 if (abi_version_at_least (9))
4271 SET_TYPE_ALIGN (nullptr_type_node, GET_MODE_ALIGNMENT (ptr_mode));
4272 SET_TYPE_MODE (nullptr_type_node, ptr_mode);
4273 record_builtin_type (RID_MAX, "decltype(nullptr)", nullptr_type_node);
4274 nullptr_node = build_int_cst (nullptr_type_node, 0);
4277 abort_fndecl
4278 = build_library_fn_ptr ("__cxa_pure_virtual", void_ftype,
4279 ECF_NORETURN | ECF_NOTHROW | ECF_COLD);
4281 /* Perform other language dependent initializations. */
4282 init_class_processing ();
4283 init_rtti_processing ();
4284 init_template_processing ();
4286 if (flag_exceptions)
4287 init_exception_processing ();
4289 if (! supports_one_only ())
4290 flag_weak = 0;
4292 make_fname_decl = cp_make_fname_decl;
4293 start_fname_decls ();
4295 /* Show we use EH for cleanups. */
4296 if (flag_exceptions)
4297 using_eh_for_cleanups ();
4300 /* Generate an initializer for a function naming variable from
4301 NAME. NAME may be NULL, to indicate a dependent name. TYPE_P is
4302 filled in with the type of the init. */
4304 tree
4305 cp_fname_init (const char* name, tree *type_p)
4307 tree domain = NULL_TREE;
4308 tree type;
4309 tree init = NULL_TREE;
4310 size_t length = 0;
4312 if (name)
4314 length = strlen (name);
4315 domain = build_index_type (size_int (length));
4316 init = build_string (length + 1, name);
4319 type = cp_build_qualified_type (char_type_node, TYPE_QUAL_CONST);
4320 type = build_cplus_array_type (type, domain);
4322 *type_p = type;
4324 if (init)
4325 TREE_TYPE (init) = type;
4326 else
4327 init = error_mark_node;
4329 return init;
4332 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give
4333 the decl, LOC is the location to give the decl, NAME is the
4334 initialization string and TYPE_DEP indicates whether NAME depended
4335 on the type of the function. We make use of that to detect
4336 __PRETTY_FUNCTION__ inside a template fn. This is being done lazily
4337 at the point of first use, so we mustn't push the decl now. */
4339 static tree
4340 cp_make_fname_decl (location_t loc, tree id, int type_dep)
4342 const char *const name = (type_dep && processing_template_decl
4343 ? NULL : fname_as_string (type_dep));
4344 tree type;
4345 tree init = cp_fname_init (name, &type);
4346 tree decl = build_decl (loc, VAR_DECL, id, type);
4348 if (name)
4349 free (CONST_CAST (char *, name));
4351 TREE_STATIC (decl) = 1;
4352 TREE_READONLY (decl) = 1;
4353 DECL_ARTIFICIAL (decl) = 1;
4355 TREE_USED (decl) = 1;
4357 if (current_function_decl)
4359 DECL_CONTEXT (decl) = current_function_decl;
4360 decl = pushdecl_outermost_localscope (decl);
4361 cp_finish_decl (decl, init, /*init_const_expr_p=*/false, NULL_TREE,
4362 LOOKUP_ONLYCONVERTING);
4364 else
4366 DECL_THIS_STATIC (decl) = true;
4367 pushdecl_top_level_and_finish (decl, init);
4370 return decl;
4373 static tree
4374 builtin_function_1 (tree decl, tree context, bool is_global)
4376 tree id = DECL_NAME (decl);
4377 const char *name = IDENTIFIER_POINTER (id);
4379 retrofit_lang_decl (decl);
4381 DECL_ARTIFICIAL (decl) = 1;
4382 SET_OVERLOADED_OPERATOR_CODE (decl, ERROR_MARK);
4383 SET_DECL_LANGUAGE (decl, lang_c);
4384 /* Runtime library routines are, by definition, available in an
4385 external shared object. */
4386 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
4387 DECL_VISIBILITY_SPECIFIED (decl) = 1;
4389 DECL_CONTEXT (decl) = context;
4391 /* A function in the user's namespace should have an explicit
4392 declaration before it is used. Mark the built-in function as
4393 anticipated but not actually declared. */
4394 if (name[0] != '_' || name[1] != '_')
4395 DECL_ANTICIPATED (decl) = 1;
4396 else if (strncmp (name + 2, "builtin_", strlen ("builtin_")) != 0)
4398 size_t len = strlen (name);
4400 /* Treat __*_chk fortification functions as anticipated as well,
4401 unless they are __builtin_*. */
4402 if (len > strlen ("___chk")
4403 && memcmp (name + len - strlen ("_chk"),
4404 "_chk", strlen ("_chk") + 1) == 0)
4405 DECL_ANTICIPATED (decl) = 1;
4408 if (is_global)
4409 pushdecl_top_level (decl);
4410 else
4411 pushdecl (decl);
4413 return decl;
4416 tree
4417 cxx_builtin_function (tree decl)
4419 tree id = DECL_NAME (decl);
4420 const char *name = IDENTIFIER_POINTER (id);
4421 /* All builtins that don't begin with an '_' should additionally
4422 go in the 'std' namespace. */
4423 if (name[0] != '_')
4425 tree decl2 = copy_node(decl);
4426 push_namespace (std_identifier);
4427 builtin_function_1 (decl2, std_node, false);
4428 pop_namespace ();
4431 return builtin_function_1 (decl, NULL_TREE, false);
4434 /* Like cxx_builtin_function, but guarantee the function is added to the global
4435 scope. This is to allow function specific options to add new machine
4436 dependent builtins when the target ISA changes via attribute((target(...)))
4437 which saves space on program startup if the program does not use non-generic
4438 ISAs. */
4440 tree
4441 cxx_builtin_function_ext_scope (tree decl)
4444 tree id = DECL_NAME (decl);
4445 const char *name = IDENTIFIER_POINTER (id);
4446 /* All builtins that don't begin with an '_' should additionally
4447 go in the 'std' namespace. */
4448 if (name[0] != '_')
4450 tree decl2 = copy_node(decl);
4451 push_namespace (std_identifier);
4452 builtin_function_1 (decl2, std_node, true);
4453 pop_namespace ();
4456 return builtin_function_1 (decl, NULL_TREE, true);
4459 /* Generate a FUNCTION_DECL with the typical flags for a runtime library
4460 function. Not called directly. */
4462 static tree
4463 build_library_fn (tree name, enum tree_code operator_code, tree type,
4464 int ecf_flags)
4466 tree fn = build_lang_decl (FUNCTION_DECL, name, type);
4467 DECL_EXTERNAL (fn) = 1;
4468 TREE_PUBLIC (fn) = 1;
4469 DECL_ARTIFICIAL (fn) = 1;
4470 SET_OVERLOADED_OPERATOR_CODE (fn, operator_code);
4471 SET_DECL_LANGUAGE (fn, lang_c);
4472 /* Runtime library routines are, by definition, available in an
4473 external shared object. */
4474 DECL_VISIBILITY (fn) = VISIBILITY_DEFAULT;
4475 DECL_VISIBILITY_SPECIFIED (fn) = 1;
4476 set_call_expr_flags (fn, ecf_flags);
4477 return fn;
4480 /* Returns the _DECL for a library function with C++ linkage. */
4482 static tree
4483 build_cp_library_fn (tree name, enum tree_code operator_code, tree type,
4484 int ecf_flags)
4486 tree fn = build_library_fn (name, operator_code, type, ecf_flags);
4487 DECL_CONTEXT (fn) = FROB_CONTEXT (current_namespace);
4488 SET_DECL_LANGUAGE (fn, lang_cplusplus);
4489 return fn;
4492 /* Like build_library_fn, but takes a C string instead of an
4493 IDENTIFIER_NODE. */
4495 tree
4496 build_library_fn_ptr (const char* name, tree type, int ecf_flags)
4498 return build_library_fn (get_identifier (name), ERROR_MARK, type, ecf_flags);
4501 /* Like build_cp_library_fn, but takes a C string instead of an
4502 IDENTIFIER_NODE. */
4504 tree
4505 build_cp_library_fn_ptr (const char* name, tree type, int ecf_flags)
4507 return build_cp_library_fn (get_identifier (name), ERROR_MARK, type,
4508 ecf_flags);
4511 /* Like build_library_fn, but also pushes the function so that we will
4512 be able to find it via get_global_binding. Also, the function
4513 may throw exceptions listed in RAISES. */
4515 tree
4516 push_library_fn (tree name, tree type, tree raises, int ecf_flags)
4518 tree fn;
4520 if (raises)
4521 type = build_exception_variant (type, raises);
4523 fn = build_library_fn (name, ERROR_MARK, type, ecf_flags);
4524 pushdecl_top_level (fn);
4525 return fn;
4528 /* Like build_cp_library_fn, but also pushes the function so that it
4529 will be found by normal lookup. */
4531 static tree
4532 push_cp_library_fn (enum tree_code operator_code, tree type,
4533 int ecf_flags)
4535 tree fn = build_cp_library_fn (cp_operator_id (operator_code),
4536 operator_code,
4537 type, ecf_flags);
4538 pushdecl (fn);
4539 if (flag_tm)
4540 apply_tm_attr (fn, get_identifier ("transaction_safe"));
4541 return fn;
4544 /* Like push_library_fn, but takes a TREE_LIST of parm types rather than
4545 a FUNCTION_TYPE. */
4547 tree
4548 push_void_library_fn (tree name, tree parmtypes, int ecf_flags)
4550 tree type = build_function_type (void_type_node, parmtypes);
4551 return push_library_fn (name, type, NULL_TREE, ecf_flags);
4554 /* Like push_library_fn, but also note that this function throws
4555 and does not return. Used for __throw_foo and the like. */
4557 tree
4558 push_throw_library_fn (tree name, tree type)
4560 tree fn = push_library_fn (name, type, NULL_TREE, ECF_NORETURN | ECF_COLD);
4561 return fn;
4564 /* When we call finish_struct for an anonymous union, we create
4565 default copy constructors and such. But, an anonymous union
4566 shouldn't have such things; this function undoes the damage to the
4567 anonymous union type T.
4569 (The reason that we create the synthesized methods is that we don't
4570 distinguish `union { int i; }' from `typedef union { int i; } U'.
4571 The first is an anonymous union; the second is just an ordinary
4572 union type.) */
4574 void
4575 fixup_anonymous_aggr (tree t)
4577 /* Wipe out memory of synthesized methods. */
4578 TYPE_HAS_USER_CONSTRUCTOR (t) = 0;
4579 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0;
4580 TYPE_HAS_COPY_CTOR (t) = 0;
4581 TYPE_HAS_CONST_COPY_CTOR (t) = 0;
4582 TYPE_HAS_COPY_ASSIGN (t) = 0;
4583 TYPE_HAS_CONST_COPY_ASSIGN (t) = 0;
4585 /* Splice the implicitly generated functions out of TYPE_FIELDS. */
4586 for (tree probe, *prev_p = &TYPE_FIELDS (t); (probe = *prev_p);)
4587 if (TREE_CODE (probe) == FUNCTION_DECL && DECL_ARTIFICIAL (probe))
4588 *prev_p = DECL_CHAIN (probe);
4589 else
4590 prev_p = &DECL_CHAIN (probe);
4592 /* Anonymous aggregates cannot have fields with ctors, dtors or complex
4593 assignment operators (because they cannot have these methods themselves).
4594 For anonymous unions this is already checked because they are not allowed
4595 in any union, otherwise we have to check it. */
4596 if (TREE_CODE (t) != UNION_TYPE)
4598 tree field, type;
4600 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
4601 if (TREE_CODE (field) == FIELD_DECL)
4603 type = TREE_TYPE (field);
4604 if (CLASS_TYPE_P (type))
4606 if (TYPE_NEEDS_CONSTRUCTING (type))
4607 error ("member %q+#D with constructor not allowed "
4608 "in anonymous aggregate", field);
4609 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
4610 error ("member %q+#D with destructor not allowed "
4611 "in anonymous aggregate", field);
4612 if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type))
4613 error ("member %q+#D with copy assignment operator "
4614 "not allowed in anonymous aggregate", field);
4620 /* Warn for an attribute located at LOCATION that appertains to the
4621 class type CLASS_TYPE that has not been properly placed after its
4622 class-key, in it class-specifier. */
4624 void
4625 warn_misplaced_attr_for_class_type (source_location location,
4626 tree class_type)
4628 gcc_assert (OVERLOAD_TYPE_P (class_type));
4630 if (warning_at (location, OPT_Wattributes,
4631 "attribute ignored in declaration "
4632 "of %q#T", class_type))
4633 inform (location,
4634 "attribute for %q#T must follow the %qs keyword",
4635 class_type, class_key_or_enum_as_string (class_type));
4638 /* Make sure that a declaration with no declarator is well-formed, i.e.
4639 just declares a tagged type or anonymous union.
4641 Returns the type declared; or NULL_TREE if none. */
4643 tree
4644 check_tag_decl (cp_decl_specifier_seq *declspecs,
4645 bool explicit_type_instantiation_p)
4647 int saw_friend = decl_spec_seq_has_spec_p (declspecs, ds_friend);
4648 int saw_typedef = decl_spec_seq_has_spec_p (declspecs, ds_typedef);
4649 /* If a class, struct, or enum type is declared by the DECLSPECS
4650 (i.e, if a class-specifier, enum-specifier, or non-typename
4651 elaborated-type-specifier appears in the DECLSPECS),
4652 DECLARED_TYPE is set to the corresponding type. */
4653 tree declared_type = NULL_TREE;
4654 bool error_p = false;
4656 if (declspecs->multiple_types_p)
4657 error ("multiple types in one declaration");
4658 else if (declspecs->redefined_builtin_type)
4660 if (!in_system_header_at (input_location))
4661 permerror (declspecs->locations[ds_redefined_builtin_type_spec],
4662 "redeclaration of C++ built-in type %qT",
4663 declspecs->redefined_builtin_type);
4664 return NULL_TREE;
4667 if (declspecs->type
4668 && TYPE_P (declspecs->type)
4669 && ((TREE_CODE (declspecs->type) != TYPENAME_TYPE
4670 && MAYBE_CLASS_TYPE_P (declspecs->type))
4671 || TREE_CODE (declspecs->type) == ENUMERAL_TYPE))
4672 declared_type = declspecs->type;
4673 else if (declspecs->type == error_mark_node)
4674 error_p = true;
4675 if (declared_type == NULL_TREE && ! saw_friend && !error_p)
4676 permerror (input_location, "declaration does not declare anything");
4677 else if (declared_type != NULL_TREE && type_uses_auto (declared_type))
4679 error_at (declspecs->locations[ds_type_spec],
4680 "%<auto%> can only be specified for variables "
4681 "or function declarations");
4682 return error_mark_node;
4684 /* Check for an anonymous union. */
4685 else if (declared_type && RECORD_OR_UNION_CODE_P (TREE_CODE (declared_type))
4686 && TYPE_UNNAMED_P (declared_type))
4688 /* 7/3 In a simple-declaration, the optional init-declarator-list
4689 can be omitted only when declaring a class (clause 9) or
4690 enumeration (7.2), that is, when the decl-specifier-seq contains
4691 either a class-specifier, an elaborated-type-specifier with
4692 a class-key (9.1), or an enum-specifier. In these cases and
4693 whenever a class-specifier or enum-specifier is present in the
4694 decl-specifier-seq, the identifiers in these specifiers are among
4695 the names being declared by the declaration (as class-name,
4696 enum-names, or enumerators, depending on the syntax). In such
4697 cases, and except for the declaration of an unnamed bit-field (9.6),
4698 the decl-specifier-seq shall introduce one or more names into the
4699 program, or shall redeclare a name introduced by a previous
4700 declaration. [Example:
4701 enum { }; // ill-formed
4702 typedef class { }; // ill-formed
4703 --end example] */
4704 if (saw_typedef)
4706 error ("missing type-name in typedef-declaration");
4707 return NULL_TREE;
4709 /* Anonymous unions are objects, so they can have specifiers. */;
4710 SET_ANON_AGGR_TYPE_P (declared_type);
4712 if (TREE_CODE (declared_type) != UNION_TYPE
4713 && !in_system_header_at (input_location))
4714 pedwarn (input_location, OPT_Wpedantic, "ISO C++ prohibits anonymous structs");
4717 else
4719 if (decl_spec_seq_has_spec_p (declspecs, ds_inline))
4720 error_at (declspecs->locations[ds_inline],
4721 "%<inline%> can only be specified for functions");
4722 else if (decl_spec_seq_has_spec_p (declspecs, ds_virtual))
4723 error_at (declspecs->locations[ds_virtual],
4724 "%<virtual%> can only be specified for functions");
4725 else if (saw_friend
4726 && (!current_class_type
4727 || current_scope () != current_class_type))
4728 error_at (declspecs->locations[ds_friend],
4729 "%<friend%> can only be specified inside a class");
4730 else if (decl_spec_seq_has_spec_p (declspecs, ds_explicit))
4731 error_at (declspecs->locations[ds_explicit],
4732 "%<explicit%> can only be specified for constructors");
4733 else if (declspecs->storage_class)
4734 error_at (declspecs->locations[ds_storage_class],
4735 "a storage class can only be specified for objects "
4736 "and functions");
4737 else if (decl_spec_seq_has_spec_p (declspecs, ds_const))
4738 error_at (declspecs->locations[ds_const],
4739 "%<const%> can only be specified for objects and "
4740 "functions");
4741 else if (decl_spec_seq_has_spec_p (declspecs, ds_volatile))
4742 error_at (declspecs->locations[ds_volatile],
4743 "%<volatile%> can only be specified for objects and "
4744 "functions");
4745 else if (decl_spec_seq_has_spec_p (declspecs, ds_restrict))
4746 error_at (declspecs->locations[ds_restrict],
4747 "%<__restrict%> can only be specified for objects and "
4748 "functions");
4749 else if (decl_spec_seq_has_spec_p (declspecs, ds_thread))
4750 error_at (declspecs->locations[ds_thread],
4751 "%<__thread%> can only be specified for objects "
4752 "and functions");
4753 else if (saw_typedef)
4754 warning_at (declspecs->locations[ds_typedef], 0,
4755 "%<typedef%> was ignored in this declaration");
4756 else if (decl_spec_seq_has_spec_p (declspecs, ds_constexpr))
4757 error_at (declspecs->locations[ds_constexpr],
4758 "%<constexpr%> cannot be used for type declarations");
4761 if (declspecs->attributes && warn_attributes && declared_type)
4763 location_t loc;
4764 if (!CLASS_TYPE_P (declared_type)
4765 || !CLASSTYPE_TEMPLATE_INSTANTIATION (declared_type))
4766 /* For a non-template class, use the name location. */
4767 loc = location_of (declared_type);
4768 else
4769 /* For a template class (an explicit instantiation), use the
4770 current location. */
4771 loc = input_location;
4773 if (explicit_type_instantiation_p)
4774 /* [dcl.attr.grammar]/4:
4776 No attribute-specifier-seq shall appertain to an explicit
4777 instantiation. */
4779 if (warning_at (loc, OPT_Wattributes,
4780 "attribute ignored in explicit instantiation %q#T",
4781 declared_type))
4782 inform (loc,
4783 "no attribute can be applied to "
4784 "an explicit instantiation");
4786 else
4787 warn_misplaced_attr_for_class_type (loc, declared_type);
4790 return declared_type;
4793 /* Called when a declaration is seen that contains no names to declare.
4794 If its type is a reference to a structure, union or enum inherited
4795 from a containing scope, shadow that tag name for the current scope
4796 with a forward reference.
4797 If its type defines a new named structure or union
4798 or defines an enum, it is valid but we need not do anything here.
4799 Otherwise, it is an error.
4801 C++: may have to grok the declspecs to learn about static,
4802 complain for anonymous unions.
4804 Returns the TYPE declared -- or NULL_TREE if none. */
4806 tree
4807 shadow_tag (cp_decl_specifier_seq *declspecs)
4809 tree t = check_tag_decl (declspecs,
4810 /*explicit_type_instantiation_p=*/false);
4812 if (!t)
4813 return NULL_TREE;
4815 if (maybe_process_partial_specialization (t) == error_mark_node)
4816 return NULL_TREE;
4818 /* This is where the variables in an anonymous union are
4819 declared. An anonymous union declaration looks like:
4820 union { ... } ;
4821 because there is no declarator after the union, the parser
4822 sends that declaration here. */
4823 if (ANON_AGGR_TYPE_P (t))
4825 fixup_anonymous_aggr (t);
4827 if (TYPE_FIELDS (t))
4829 tree decl = grokdeclarator (/*declarator=*/NULL,
4830 declspecs, NORMAL, 0, NULL);
4831 finish_anon_union (decl);
4835 return t;
4838 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
4840 tree
4841 groktypename (cp_decl_specifier_seq *type_specifiers,
4842 const cp_declarator *declarator,
4843 bool is_template_arg)
4845 tree attrs;
4846 tree type;
4847 enum decl_context context
4848 = is_template_arg ? TEMPLATE_TYPE_ARG : TYPENAME;
4849 attrs = type_specifiers->attributes;
4850 type_specifiers->attributes = NULL_TREE;
4851 type = grokdeclarator (declarator, type_specifiers, context, 0, &attrs);
4852 if (attrs && type != error_mark_node)
4854 if (CLASS_TYPE_P (type))
4855 warning (OPT_Wattributes, "ignoring attributes applied to class type %qT "
4856 "outside of definition", type);
4857 else if (MAYBE_CLASS_TYPE_P (type))
4858 /* A template type parameter or other dependent type. */
4859 warning (OPT_Wattributes, "ignoring attributes applied to dependent "
4860 "type %qT without an associated declaration", type);
4861 else
4862 cplus_decl_attributes (&type, attrs, 0);
4864 return type;
4867 /* Process a DECLARATOR for a function-scope variable declaration,
4868 namespace-scope variable declaration, or function declaration.
4869 (Function definitions go through start_function; class member
4870 declarations appearing in the body of the class go through
4871 grokfield.) The DECL corresponding to the DECLARATOR is returned.
4872 If an error occurs, the error_mark_node is returned instead.
4874 DECLSPECS are the decl-specifiers for the declaration. INITIALIZED is
4875 SD_INITIALIZED if an explicit initializer is present, or SD_DEFAULTED
4876 for an explicitly defaulted function, or SD_DELETED for an explicitly
4877 deleted function, but 0 (SD_UNINITIALIZED) if this is a variable
4878 implicitly initialized via a default constructor. ATTRIBUTES and
4879 PREFIX_ATTRIBUTES are GNU attributes associated with this declaration.
4881 The scope represented by the context of the returned DECL is pushed
4882 (if it is not the global namespace) and is assigned to
4883 *PUSHED_SCOPE_P. The caller is then responsible for calling
4884 pop_scope on *PUSHED_SCOPE_P if it is set. */
4886 tree
4887 start_decl (const cp_declarator *declarator,
4888 cp_decl_specifier_seq *declspecs,
4889 int initialized,
4890 tree attributes,
4891 tree prefix_attributes,
4892 tree *pushed_scope_p)
4894 tree decl;
4895 tree context;
4896 bool was_public;
4897 int flags;
4898 bool alias;
4900 *pushed_scope_p = NULL_TREE;
4902 /* An object declared as __attribute__((deprecated)) suppresses
4903 warnings of uses of other deprecated items. */
4904 if (lookup_attribute ("deprecated", attributes))
4905 deprecated_state = DEPRECATED_SUPPRESS;
4907 attributes = chainon (attributes, prefix_attributes);
4909 decl = grokdeclarator (declarator, declspecs, NORMAL, initialized,
4910 &attributes);
4912 deprecated_state = DEPRECATED_NORMAL;
4914 if (decl == NULL_TREE || VOID_TYPE_P (decl)
4915 || decl == error_mark_node)
4916 return error_mark_node;
4918 context = CP_DECL_CONTEXT (decl);
4919 if (context != global_namespace)
4920 *pushed_scope_p = push_scope (context);
4922 /* Is it valid for this decl to have an initializer at all?
4923 If not, set INITIALIZED to zero, which will indirectly
4924 tell `cp_finish_decl' to ignore the initializer once it is parsed. */
4925 if (initialized
4926 && TREE_CODE (decl) == TYPE_DECL)
4928 error ("typedef %qD is initialized (use decltype instead)", decl);
4929 return error_mark_node;
4932 if (initialized)
4934 if (! toplevel_bindings_p ()
4935 && DECL_EXTERNAL (decl))
4936 warning (0, "declaration of %q#D has %<extern%> and is initialized",
4937 decl);
4938 DECL_EXTERNAL (decl) = 0;
4939 if (toplevel_bindings_p ())
4940 TREE_STATIC (decl) = 1;
4942 alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl)) != 0;
4944 if (alias && TREE_CODE (decl) == FUNCTION_DECL)
4945 record_key_method_defined (decl);
4947 /* If this is a typedef that names the class for linkage purposes
4948 (7.1.3p8), apply any attributes directly to the type. */
4949 if (TREE_CODE (decl) == TYPE_DECL
4950 && OVERLOAD_TYPE_P (TREE_TYPE (decl))
4951 && decl == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (decl))))
4952 flags = ATTR_FLAG_TYPE_IN_PLACE;
4953 else
4954 flags = 0;
4956 /* Set attributes here so if duplicate decl, will have proper attributes. */
4957 cplus_decl_attributes (&decl, attributes, flags);
4959 /* Dllimported symbols cannot be defined. Static data members (which
4960 can be initialized in-class and dllimported) go through grokfield,
4961 not here, so we don't need to exclude those decls when checking for
4962 a definition. */
4963 if (initialized && DECL_DLLIMPORT_P (decl))
4965 error ("definition of %q#D is marked %<dllimport%>", decl);
4966 DECL_DLLIMPORT_P (decl) = 0;
4969 /* If #pragma weak was used, mark the decl weak now. */
4970 if (!processing_template_decl)
4971 maybe_apply_pragma_weak (decl);
4973 if (TREE_CODE (decl) == FUNCTION_DECL
4974 && DECL_DECLARED_INLINE_P (decl)
4975 && DECL_UNINLINABLE (decl)
4976 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
4977 warning_at (DECL_SOURCE_LOCATION (decl), 0,
4978 "inline function %qD given attribute noinline", decl);
4980 if (TYPE_P (context) && COMPLETE_TYPE_P (complete_type (context)))
4982 bool this_tmpl = (processing_template_decl
4983 > template_class_depth (context));
4984 if (VAR_P (decl))
4986 tree field = lookup_field (context, DECL_NAME (decl), 0, false);
4987 if (field == NULL_TREE
4988 || !(VAR_P (field) || variable_template_p (field)))
4989 error ("%q+#D is not a static data member of %q#T", decl, context);
4990 else if (variable_template_p (field) && !this_tmpl)
4992 if (DECL_LANG_SPECIFIC (decl)
4993 && DECL_TEMPLATE_SPECIALIZATION (decl))
4994 /* OK, specialization was already checked. */;
4995 else
4997 error_at (DECL_SOURCE_LOCATION (decl),
4998 "non-member-template declaration of %qD", decl);
4999 inform (DECL_SOURCE_LOCATION (field), "does not match "
5000 "member template declaration here");
5001 return error_mark_node;
5004 else
5006 if (variable_template_p (field))
5007 field = DECL_TEMPLATE_RESULT (field);
5009 if (DECL_CONTEXT (field) != context)
5011 if (!same_type_p (DECL_CONTEXT (field), context))
5012 permerror (input_location, "ISO C++ does not permit %<%T::%D%> "
5013 "to be defined as %<%T::%D%>",
5014 DECL_CONTEXT (field), DECL_NAME (decl),
5015 context, DECL_NAME (decl));
5016 DECL_CONTEXT (decl) = DECL_CONTEXT (field);
5018 /* Static data member are tricky; an in-class initialization
5019 still doesn't provide a definition, so the in-class
5020 declaration will have DECL_EXTERNAL set, but will have an
5021 initialization. Thus, duplicate_decls won't warn
5022 about this situation, and so we check here. */
5023 if (initialized && DECL_INITIALIZED_IN_CLASS_P (field))
5024 error ("duplicate initialization of %qD", decl);
5025 field = duplicate_decls (decl, field,
5026 /*newdecl_is_friend=*/false);
5027 if (field == error_mark_node)
5028 return error_mark_node;
5029 else if (field)
5030 decl = field;
5033 else
5035 tree field = check_classfn (context, decl,
5036 this_tmpl
5037 ? current_template_parms
5038 : NULL_TREE);
5039 if (field && field != error_mark_node
5040 && duplicate_decls (decl, field,
5041 /*newdecl_is_friend=*/false))
5042 decl = field;
5045 /* cp_finish_decl sets DECL_EXTERNAL if DECL_IN_AGGR_P is set. */
5046 DECL_IN_AGGR_P (decl) = 0;
5047 /* Do not mark DECL as an explicit specialization if it was not
5048 already marked as an instantiation; a declaration should
5049 never be marked as a specialization unless we know what
5050 template is being specialized. */
5051 if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
5053 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
5054 if (TREE_CODE (decl) == FUNCTION_DECL)
5055 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
5056 && DECL_DECLARED_INLINE_P (decl));
5057 else
5058 DECL_COMDAT (decl) = false;
5060 /* [temp.expl.spec] An explicit specialization of a static data
5061 member of a template is a definition if the declaration
5062 includes an initializer; otherwise, it is a declaration.
5064 We check for processing_specialization so this only applies
5065 to the new specialization syntax. */
5066 if (!initialized && processing_specialization)
5067 DECL_EXTERNAL (decl) = 1;
5070 if (DECL_EXTERNAL (decl) && ! DECL_TEMPLATE_SPECIALIZATION (decl)
5071 /* Aliases are definitions. */
5072 && !alias)
5073 permerror (input_location, "declaration of %q#D outside of class is not definition",
5074 decl);
5077 was_public = TREE_PUBLIC (decl);
5079 /* Enter this declaration into the symbol table. Don't push the plain
5080 VAR_DECL for a variable template. */
5081 if (!template_parm_scope_p ()
5082 || !VAR_P (decl))
5083 decl = maybe_push_decl (decl);
5085 if (processing_template_decl)
5086 decl = push_template_decl (decl);
5087 if (decl == error_mark_node)
5088 return error_mark_node;
5090 if (VAR_P (decl)
5091 && DECL_NAMESPACE_SCOPE_P (decl) && !TREE_PUBLIC (decl) && !was_public
5092 && !DECL_THIS_STATIC (decl) && !DECL_ARTIFICIAL (decl))
5094 /* This is a const variable with implicit 'static'. Set
5095 DECL_THIS_STATIC so we can tell it from variables that are
5096 !TREE_PUBLIC because of the anonymous namespace. */
5097 gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (decl)) || errorcount);
5098 DECL_THIS_STATIC (decl) = 1;
5101 if (current_function_decl && VAR_P (decl)
5102 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
5104 bool ok = false;
5105 if (CP_DECL_THREAD_LOCAL_P (decl))
5106 error ("%qD declared %<thread_local%> in %<constexpr%> function",
5107 decl);
5108 else if (TREE_STATIC (decl))
5109 error ("%qD declared %<static%> in %<constexpr%> function", decl);
5110 else
5111 ok = true;
5112 if (!ok)
5113 cp_function_chain->invalid_constexpr = true;
5116 if (!processing_template_decl && VAR_P (decl))
5117 start_decl_1 (decl, initialized);
5119 return decl;
5122 /* Process the declaration of a variable DECL. INITIALIZED is true
5123 iff DECL is explicitly initialized. (INITIALIZED is false if the
5124 variable is initialized via an implicitly-called constructor.)
5125 This function must be called for ordinary variables (including, for
5126 example, implicit instantiations of templates), but must not be
5127 called for template declarations. */
5129 void
5130 start_decl_1 (tree decl, bool initialized)
5132 tree type;
5133 bool complete_p;
5134 bool aggregate_definition_p;
5136 gcc_assert (!processing_template_decl);
5138 if (error_operand_p (decl))
5139 return;
5141 gcc_assert (VAR_P (decl));
5143 type = TREE_TYPE (decl);
5144 complete_p = COMPLETE_TYPE_P (type);
5145 aggregate_definition_p = MAYBE_CLASS_TYPE_P (type) && !DECL_EXTERNAL (decl);
5147 /* If an explicit initializer is present, or if this is a definition
5148 of an aggregate, then we need a complete type at this point.
5149 (Scalars are always complete types, so there is nothing to
5150 check.) This code just sets COMPLETE_P; errors (if necessary)
5151 are issued below. */
5152 if ((initialized || aggregate_definition_p)
5153 && !complete_p
5154 && COMPLETE_TYPE_P (complete_type (type)))
5156 complete_p = true;
5157 /* We will not yet have set TREE_READONLY on DECL if the type
5158 was "const", but incomplete, before this point. But, now, we
5159 have a complete type, so we can try again. */
5160 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
5163 if (initialized)
5164 /* Is it valid for this decl to have an initializer at all? */
5166 /* Don't allow initializations for incomplete types except for
5167 arrays which might be completed by the initialization. */
5168 if (complete_p)
5169 ; /* A complete type is ok. */
5170 else if (type_uses_auto (type))
5171 ; /* An auto type is ok. */
5172 else if (TREE_CODE (type) != ARRAY_TYPE)
5174 error ("variable %q#D has initializer but incomplete type", decl);
5175 type = TREE_TYPE (decl) = error_mark_node;
5177 else if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
5179 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
5180 error ("elements of array %q#D have incomplete type", decl);
5181 /* else we already gave an error in start_decl. */
5184 else if (aggregate_definition_p && !complete_p)
5186 if (type_uses_auto (type))
5187 gcc_assert (CLASS_PLACEHOLDER_TEMPLATE (type));
5188 else
5190 error ("aggregate %q#D has incomplete type and cannot be defined",
5191 decl);
5192 /* Change the type so that assemble_variable will give
5193 DECL an rtl we can live with: (mem (const_int 0)). */
5194 type = TREE_TYPE (decl) = error_mark_node;
5198 /* Create a new scope to hold this declaration if necessary.
5199 Whether or not a new scope is necessary cannot be determined
5200 until after the type has been completed; if the type is a
5201 specialization of a class template it is not until after
5202 instantiation has occurred that TYPE_HAS_NONTRIVIAL_DESTRUCTOR
5203 will be set correctly. */
5204 maybe_push_cleanup_level (type);
5207 /* Handle initialization of references. DECL, TYPE, and INIT have the
5208 same meaning as in cp_finish_decl. *CLEANUP must be NULL on entry,
5209 but will be set to a new CLEANUP_STMT if a temporary is created
5210 that must be destroyed subsequently.
5212 Returns an initializer expression to use to initialize DECL, or
5213 NULL if the initialization can be performed statically.
5215 Quotes on semantics can be found in ARM 8.4.3. */
5217 static tree
5218 grok_reference_init (tree decl, tree type, tree init, int flags)
5220 if (init == NULL_TREE)
5222 if ((DECL_LANG_SPECIFIC (decl) == 0
5223 || DECL_IN_AGGR_P (decl) == 0)
5224 && ! DECL_THIS_EXTERN (decl))
5225 error ("%qD declared as reference but not initialized", decl);
5226 return NULL_TREE;
5229 if (TREE_CODE (init) == TREE_LIST)
5230 init = build_x_compound_expr_from_list (init, ELK_INIT,
5231 tf_warning_or_error);
5233 tree ttype = TREE_TYPE (type);
5234 if (TREE_CODE (ttype) != ARRAY_TYPE
5235 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
5236 /* Note: default conversion is only called in very special cases. */
5237 init = decay_conversion (init, tf_warning_or_error);
5239 /* check_initializer handles this for non-reference variables, but for
5240 references we need to do it here or the initializer will get the
5241 incomplete array type and confuse later calls to
5242 cp_complete_array_type. */
5243 if (TREE_CODE (ttype) == ARRAY_TYPE
5244 && TYPE_DOMAIN (ttype) == NULL_TREE
5245 && (BRACE_ENCLOSED_INITIALIZER_P (init)
5246 || TREE_CODE (init) == STRING_CST))
5248 cp_complete_array_type (&ttype, init, false);
5249 if (ttype != TREE_TYPE (type))
5250 type = cp_build_reference_type (ttype, TYPE_REF_IS_RVALUE (type));
5253 /* Convert INIT to the reference type TYPE. This may involve the
5254 creation of a temporary, whose lifetime must be the same as that
5255 of the reference. If so, a DECL_EXPR for the temporary will be
5256 added just after the DECL_EXPR for DECL. That's why we don't set
5257 DECL_INITIAL for local references (instead assigning to them
5258 explicitly); we need to allow the temporary to be initialized
5259 first. */
5260 return initialize_reference (type, init, flags,
5261 tf_warning_or_error);
5264 /* Designated initializers in arrays are not supported in GNU C++.
5265 The parser cannot detect this error since it does not know whether
5266 a given brace-enclosed initializer is for a class type or for an
5267 array. This function checks that CE does not use a designated
5268 initializer. If it does, an error is issued. Returns true if CE
5269 is valid, i.e., does not have a designated initializer. */
5271 static bool
5272 check_array_designated_initializer (constructor_elt *ce,
5273 unsigned HOST_WIDE_INT index)
5275 /* Designated initializers for array elements are not supported. */
5276 if (ce->index)
5278 /* The parser only allows identifiers as designated
5279 initializers. */
5280 if (ce->index == error_mark_node)
5282 error ("name used in a GNU-style designated "
5283 "initializer for an array");
5284 return false;
5286 else if (identifier_p (ce->index))
5288 error ("name %qD used in a GNU-style designated "
5289 "initializer for an array", ce->index);
5290 return false;
5293 tree ce_index = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5294 ce->index, true);
5295 if (ce_index
5296 && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (ce_index))
5297 && (TREE_CODE (ce_index = maybe_constant_value (ce_index))
5298 == INTEGER_CST))
5300 /* A C99 designator is OK if it matches the current index. */
5301 if (wi::to_wide (ce_index) == index)
5302 return true;
5303 else
5304 sorry ("non-trivial designated initializers not supported");
5306 else
5307 error ("C99 designator %qE is not an integral constant-expression",
5308 ce->index);
5310 return false;
5313 return true;
5316 /* When parsing `int a[] = {1, 2};' we don't know the size of the
5317 array until we finish parsing the initializer. If that's the
5318 situation we're in, update DECL accordingly. */
5320 static void
5321 maybe_deduce_size_from_array_init (tree decl, tree init)
5323 tree type = TREE_TYPE (decl);
5325 if (TREE_CODE (type) == ARRAY_TYPE
5326 && TYPE_DOMAIN (type) == NULL_TREE
5327 && TREE_CODE (decl) != TYPE_DECL)
5329 /* do_default is really a C-ism to deal with tentative definitions.
5330 But let's leave it here to ease the eventual merge. */
5331 int do_default = !DECL_EXTERNAL (decl);
5332 tree initializer = init ? init : DECL_INITIAL (decl);
5333 int failure = 0;
5335 /* Check that there are no designated initializers in INIT, as
5336 those are not supported in GNU C++, and as the middle-end
5337 will crash if presented with a non-numeric designated
5338 initializer. */
5339 if (initializer && BRACE_ENCLOSED_INITIALIZER_P (initializer))
5341 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initializer);
5342 constructor_elt *ce;
5343 HOST_WIDE_INT i;
5344 FOR_EACH_VEC_SAFE_ELT (v, i, ce)
5345 if (!check_array_designated_initializer (ce, i))
5346 failure = 1;
5349 if (!failure)
5351 failure = cp_complete_array_type (&TREE_TYPE (decl), initializer,
5352 do_default);
5353 if (failure == 1)
5355 error_at (EXPR_LOC_OR_LOC (initializer,
5356 DECL_SOURCE_LOCATION (decl)),
5357 "initializer fails to determine size of %qD", decl);
5359 else if (failure == 2)
5361 if (do_default)
5363 error_at (DECL_SOURCE_LOCATION (decl),
5364 "array size missing in %qD", decl);
5366 /* If a `static' var's size isn't known, make it extern as
5367 well as static, so it does not get allocated. If it's not
5368 `static', then don't mark it extern; finish_incomplete_decl
5369 will give it a default size and it will get allocated. */
5370 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
5371 DECL_EXTERNAL (decl) = 1;
5373 else if (failure == 3)
5375 error_at (DECL_SOURCE_LOCATION (decl),
5376 "zero-size array %qD", decl);
5380 cp_apply_type_quals_to_decl (cp_type_quals (TREE_TYPE (decl)), decl);
5382 relayout_decl (decl);
5386 /* Set DECL_SIZE, DECL_ALIGN, etc. for DECL (a VAR_DECL), and issue
5387 any appropriate error messages regarding the layout. */
5389 static void
5390 layout_var_decl (tree decl)
5392 tree type;
5394 type = TREE_TYPE (decl);
5395 if (type == error_mark_node)
5396 return;
5398 /* If we haven't already laid out this declaration, do so now.
5399 Note that we must not call complete type for an external object
5400 because it's type might involve templates that we are not
5401 supposed to instantiate yet. (And it's perfectly valid to say
5402 `extern X x' for some incomplete type `X'.) */
5403 if (!DECL_EXTERNAL (decl))
5404 complete_type (type);
5405 if (!DECL_SIZE (decl)
5406 && TREE_TYPE (decl) != error_mark_node
5407 && complete_or_array_type_p (type))
5408 layout_decl (decl, 0);
5410 if (!DECL_EXTERNAL (decl) && DECL_SIZE (decl) == NULL_TREE)
5412 /* An automatic variable with an incomplete type: that is an error.
5413 Don't talk about array types here, since we took care of that
5414 message in grokdeclarator. */
5415 error_at (DECL_SOURCE_LOCATION (decl),
5416 "storage size of %qD isn%'t known", decl);
5417 TREE_TYPE (decl) = error_mark_node;
5419 #if 0
5420 /* Keep this code around in case we later want to control debug info
5421 based on whether a type is "used". (jason 1999-11-11) */
5423 else if (!DECL_EXTERNAL (decl) && MAYBE_CLASS_TYPE_P (ttype))
5424 /* Let debugger know it should output info for this type. */
5425 note_debug_info_needed (ttype);
5427 if (TREE_STATIC (decl) && DECL_CLASS_SCOPE_P (decl))
5428 note_debug_info_needed (DECL_CONTEXT (decl));
5429 #endif
5431 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
5432 && DECL_SIZE (decl) != NULL_TREE
5433 && ! TREE_CONSTANT (DECL_SIZE (decl)))
5435 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
5436 constant_expression_warning (DECL_SIZE (decl));
5437 else
5439 error_at (DECL_SOURCE_LOCATION (decl),
5440 "storage size of %qD isn%'t constant", decl);
5441 TREE_TYPE (decl) = error_mark_node;
5446 /* If a local static variable is declared in an inline function, or if
5447 we have a weak definition, we must endeavor to create only one
5448 instance of the variable at link-time. */
5450 void
5451 maybe_commonize_var (tree decl)
5453 /* Static data in a function with comdat linkage also has comdat
5454 linkage. */
5455 if ((TREE_STATIC (decl)
5456 /* Don't mess with __FUNCTION__. */
5457 && ! DECL_ARTIFICIAL (decl)
5458 && DECL_FUNCTION_SCOPE_P (decl)
5459 && vague_linkage_p (DECL_CONTEXT (decl)))
5460 || (TREE_PUBLIC (decl) && DECL_INLINE_VAR_P (decl)))
5462 if (flag_weak)
5464 /* With weak symbols, we simply make the variable COMDAT;
5465 that will cause copies in multiple translations units to
5466 be merged. */
5467 comdat_linkage (decl);
5469 else
5471 if (DECL_INITIAL (decl) == NULL_TREE
5472 || DECL_INITIAL (decl) == error_mark_node)
5474 /* Without weak symbols, we can use COMMON to merge
5475 uninitialized variables. */
5476 TREE_PUBLIC (decl) = 1;
5477 DECL_COMMON (decl) = 1;
5479 else
5481 /* While for initialized variables, we must use internal
5482 linkage -- which means that multiple copies will not
5483 be merged. */
5484 TREE_PUBLIC (decl) = 0;
5485 DECL_COMMON (decl) = 0;
5486 const char *msg;
5487 if (DECL_INLINE_VAR_P (decl))
5488 msg = G_("sorry: semantics of inline variable "
5489 "%q#D are wrong (you%'ll wind up with "
5490 "multiple copies)");
5491 else
5492 msg = G_("sorry: semantics of inline function "
5493 "static data %q#D are wrong (you%'ll wind "
5494 "up with multiple copies)");
5495 if (warning_at (DECL_SOURCE_LOCATION (decl), 0,
5496 msg, decl))
5497 inform (DECL_SOURCE_LOCATION (decl),
5498 "you can work around this by removing the initializer");
5504 /* Issue an error message if DECL is an uninitialized const variable. */
5506 static void
5507 check_for_uninitialized_const_var (tree decl)
5509 tree type = strip_array_types (TREE_TYPE (decl));
5511 /* ``Unless explicitly declared extern, a const object does not have
5512 external linkage and must be initialized. ($8.4; $12.1)'' ARM
5513 7.1.6 */
5514 if (VAR_P (decl)
5515 && TREE_CODE (type) != REFERENCE_TYPE
5516 && (CP_TYPE_CONST_P (type) || var_in_constexpr_fn (decl))
5517 && !DECL_INITIAL (decl))
5519 tree field = default_init_uninitialized_part (type);
5520 if (!field)
5521 return;
5523 if (CP_TYPE_CONST_P (type))
5524 permerror (DECL_SOURCE_LOCATION (decl),
5525 "uninitialized const %qD", decl);
5526 else
5528 if (!is_instantiation_of_constexpr (current_function_decl))
5529 error_at (DECL_SOURCE_LOCATION (decl),
5530 "uninitialized variable %qD in %<constexpr%> function",
5531 decl);
5532 cp_function_chain->invalid_constexpr = true;
5535 if (CLASS_TYPE_P (type))
5537 tree defaulted_ctor;
5539 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
5540 "%q#T has no user-provided default constructor", type);
5541 defaulted_ctor = in_class_defaulted_default_constructor (type);
5542 if (defaulted_ctor)
5543 inform (DECL_SOURCE_LOCATION (defaulted_ctor),
5544 "constructor is not user-provided because it is "
5545 "explicitly defaulted in the class body");
5546 inform (DECL_SOURCE_LOCATION (field),
5547 "and the implicitly-defined constructor does not "
5548 "initialize %q#D", field);
5553 /* Structure holding the current initializer being processed by reshape_init.
5554 CUR is a pointer to the current element being processed, END is a pointer
5555 after the last element present in the initializer. */
5556 struct reshape_iter
5558 constructor_elt *cur;
5559 constructor_elt *end;
5562 static tree reshape_init_r (tree, reshape_iter *, bool, tsubst_flags_t);
5564 /* FIELD is a FIELD_DECL or NULL. In the former case, the value
5565 returned is the next FIELD_DECL (possibly FIELD itself) that can be
5566 initialized. If there are no more such fields, the return value
5567 will be NULL. */
5569 tree
5570 next_initializable_field (tree field)
5572 while (field
5573 && (TREE_CODE (field) != FIELD_DECL
5574 || (DECL_C_BIT_FIELD (field) && !DECL_NAME (field))
5575 || (DECL_ARTIFICIAL (field)
5576 && !(cxx_dialect >= cxx17 && DECL_FIELD_IS_BASE (field)))))
5577 field = DECL_CHAIN (field);
5579 return field;
5582 /* Return true for [dcl.init.list] direct-list-initialization from
5583 single element of enumeration with a fixed underlying type. */
5585 bool
5586 is_direct_enum_init (tree type, tree init)
5588 if (cxx_dialect >= cxx17
5589 && TREE_CODE (type) == ENUMERAL_TYPE
5590 && ENUM_FIXED_UNDERLYING_TYPE_P (type)
5591 && TREE_CODE (init) == CONSTRUCTOR
5592 && CONSTRUCTOR_IS_DIRECT_INIT (init)
5593 && CONSTRUCTOR_NELTS (init) == 1)
5594 return true;
5595 return false;
5598 /* Subroutine of reshape_init_array and reshape_init_vector, which does
5599 the actual work. ELT_TYPE is the element type of the array. MAX_INDEX is an
5600 INTEGER_CST representing the size of the array minus one (the maximum index),
5601 or NULL_TREE if the array was declared without specifying the size. D is
5602 the iterator within the constructor. */
5604 static tree
5605 reshape_init_array_1 (tree elt_type, tree max_index, reshape_iter *d,
5606 tsubst_flags_t complain)
5608 tree new_init;
5609 bool sized_array_p = (max_index && TREE_CONSTANT (max_index));
5610 unsigned HOST_WIDE_INT max_index_cst = 0;
5611 unsigned HOST_WIDE_INT index;
5613 /* The initializer for an array is always a CONSTRUCTOR. */
5614 new_init = build_constructor (init_list_type_node, NULL);
5616 if (sized_array_p)
5618 /* Minus 1 is used for zero sized arrays. */
5619 if (integer_all_onesp (max_index))
5620 return new_init;
5622 if (tree_fits_uhwi_p (max_index))
5623 max_index_cst = tree_to_uhwi (max_index);
5624 /* sizetype is sign extended, not zero extended. */
5625 else
5626 max_index_cst = tree_to_uhwi (fold_convert (size_type_node, max_index));
5629 /* Loop until there are no more initializers. */
5630 for (index = 0;
5631 d->cur != d->end && (!sized_array_p || index <= max_index_cst);
5632 ++index)
5634 tree elt_init;
5635 constructor_elt *old_cur = d->cur;
5637 check_array_designated_initializer (d->cur, index);
5638 elt_init = reshape_init_r (elt_type, d, /*first_initializer_p=*/false,
5639 complain);
5640 if (elt_init == error_mark_node)
5641 return error_mark_node;
5642 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init),
5643 size_int (index), elt_init);
5644 if (!TREE_CONSTANT (elt_init))
5645 TREE_CONSTANT (new_init) = false;
5647 /* This can happen with an invalid initializer (c++/54501). */
5648 if (d->cur == old_cur && !sized_array_p)
5649 break;
5652 return new_init;
5655 /* Subroutine of reshape_init_r, processes the initializers for arrays.
5656 Parameters are the same of reshape_init_r. */
5658 static tree
5659 reshape_init_array (tree type, reshape_iter *d, tsubst_flags_t complain)
5661 tree max_index = NULL_TREE;
5663 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
5665 if (TYPE_DOMAIN (type))
5666 max_index = array_type_nelts (type);
5668 return reshape_init_array_1 (TREE_TYPE (type), max_index, d, complain);
5671 /* Subroutine of reshape_init_r, processes the initializers for vectors.
5672 Parameters are the same of reshape_init_r. */
5674 static tree
5675 reshape_init_vector (tree type, reshape_iter *d, tsubst_flags_t complain)
5677 tree max_index = NULL_TREE;
5679 gcc_assert (VECTOR_TYPE_P (type));
5681 if (COMPOUND_LITERAL_P (d->cur->value))
5683 tree value = d->cur->value;
5684 if (!same_type_p (TREE_TYPE (value), type))
5686 if (complain & tf_error)
5687 error ("invalid type %qT as initializer for a vector of type %qT",
5688 TREE_TYPE (d->cur->value), type);
5689 value = error_mark_node;
5691 ++d->cur;
5692 return value;
5695 /* For a vector, we initialize it as an array of the appropriate size. */
5696 if (VECTOR_TYPE_P (type))
5697 max_index = size_int (TYPE_VECTOR_SUBPARTS (type) - 1);
5699 return reshape_init_array_1 (TREE_TYPE (type), max_index, d, complain);
5702 /* Subroutine of reshape_init_r, processes the initializers for classes
5703 or union. Parameters are the same of reshape_init_r. */
5705 static tree
5706 reshape_init_class (tree type, reshape_iter *d, bool first_initializer_p,
5707 tsubst_flags_t complain)
5709 tree field;
5710 tree new_init;
5712 gcc_assert (CLASS_TYPE_P (type));
5714 /* The initializer for a class is always a CONSTRUCTOR. */
5715 new_init = build_constructor (init_list_type_node, NULL);
5716 field = next_initializable_field (TYPE_FIELDS (type));
5718 if (!field)
5720 /* [dcl.init.aggr]
5722 An initializer for an aggregate member that is an
5723 empty class shall have the form of an empty
5724 initializer-list {}. */
5725 if (!first_initializer_p)
5727 if (complain & tf_error)
5728 error ("initializer for %qT must be brace-enclosed", type);
5729 return error_mark_node;
5731 return new_init;
5734 /* Loop through the initializable fields, gathering initializers. */
5735 while (d->cur != d->end)
5737 tree field_init;
5738 constructor_elt *old_cur = d->cur;
5740 /* Handle designated initializers, as an extension. */
5741 if (d->cur->index)
5743 if (d->cur->index == error_mark_node)
5744 return error_mark_node;
5746 if (TREE_CODE (d->cur->index) == FIELD_DECL)
5747 /* We already reshaped this. */
5748 gcc_assert (d->cur->index == field);
5749 else if (TREE_CODE (d->cur->index) == IDENTIFIER_NODE)
5750 field = get_class_binding (type, d->cur->index, false);
5751 else
5753 if (complain & tf_error)
5754 error ("%<[%E] =%> used in a GNU-style designated initializer"
5755 " for class %qT", d->cur->index, type);
5756 return error_mark_node;
5759 if (!field || TREE_CODE (field) != FIELD_DECL)
5761 if (complain & tf_error)
5762 error ("%qT has no non-static data member named %qD", type,
5763 d->cur->index);
5764 return error_mark_node;
5768 /* If we processed all the member of the class, we are done. */
5769 if (!field)
5770 break;
5772 field_init = reshape_init_r (TREE_TYPE (field), d,
5773 /*first_initializer_p=*/false, complain);
5774 if (field_init == error_mark_node)
5775 return error_mark_node;
5777 if (d->cur == old_cur && d->cur->index)
5779 /* This can happen with an invalid initializer for a flexible
5780 array member (c++/54441). */
5781 if (complain & tf_error)
5782 error ("invalid initializer for %q#D", field);
5783 return error_mark_node;
5786 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init), field, field_init);
5788 /* [dcl.init.aggr]
5790 When a union is initialized with a brace-enclosed
5791 initializer, the braces shall only contain an
5792 initializer for the first member of the union. */
5793 if (TREE_CODE (type) == UNION_TYPE)
5794 break;
5796 field = next_initializable_field (DECL_CHAIN (field));
5799 return new_init;
5802 /* Subroutine of reshape_init_r. We're in a context where C99 initializer
5803 designators are not valid; either complain or return true to indicate
5804 that reshape_init_r should return error_mark_node. */
5806 static bool
5807 has_designator_problem (reshape_iter *d, tsubst_flags_t complain)
5809 if (d->cur->index)
5811 if (complain & tf_error)
5812 error ("C99 designator %qE outside aggregate initializer",
5813 d->cur->index);
5814 else
5815 return true;
5817 return false;
5820 /* Subroutine of reshape_init, which processes a single initializer (part of
5821 a CONSTRUCTOR). TYPE is the type of the variable being initialized, D is the
5822 iterator within the CONSTRUCTOR which points to the initializer to process.
5823 FIRST_INITIALIZER_P is true if this is the first initializer of the
5824 outermost CONSTRUCTOR node. */
5826 static tree
5827 reshape_init_r (tree type, reshape_iter *d, bool first_initializer_p,
5828 tsubst_flags_t complain)
5830 tree init = d->cur->value;
5832 if (error_operand_p (init))
5833 return error_mark_node;
5835 if (first_initializer_p && !CP_AGGREGATE_TYPE_P (type)
5836 && has_designator_problem (d, complain))
5837 return error_mark_node;
5839 if (TREE_CODE (type) == COMPLEX_TYPE)
5841 /* A complex type can be initialized from one or two initializers,
5842 but braces are not elided. */
5843 d->cur++;
5844 if (BRACE_ENCLOSED_INITIALIZER_P (init))
5846 if (CONSTRUCTOR_NELTS (init) > 2)
5848 if (complain & tf_error)
5849 error ("too many initializers for %qT", type);
5850 else
5851 return error_mark_node;
5854 else if (first_initializer_p && d->cur != d->end)
5856 vec<constructor_elt, va_gc> *v = 0;
5857 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, init);
5858 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, d->cur->value);
5859 if (has_designator_problem (d, complain))
5860 return error_mark_node;
5861 d->cur++;
5862 init = build_constructor (init_list_type_node, v);
5864 return init;
5867 /* A non-aggregate type is always initialized with a single
5868 initializer. */
5869 if (!CP_AGGREGATE_TYPE_P (type))
5871 /* It is invalid to initialize a non-aggregate type with a
5872 brace-enclosed initializer before C++0x.
5873 We need to check for BRACE_ENCLOSED_INITIALIZER_P here because
5874 of g++.old-deja/g++.mike/p7626.C: a pointer-to-member constant is
5875 a CONSTRUCTOR (with a record type). */
5876 if (TREE_CODE (init) == CONSTRUCTOR
5877 /* Don't complain about a capture-init. */
5878 && !CONSTRUCTOR_IS_DIRECT_INIT (init)
5879 && BRACE_ENCLOSED_INITIALIZER_P (init)) /* p7626.C */
5881 if (SCALAR_TYPE_P (type))
5883 if (cxx_dialect < cxx11
5884 /* Isn't value-initialization. */
5885 || CONSTRUCTOR_NELTS (init) > 0)
5887 if (complain & tf_error)
5888 error ("braces around scalar initializer for type %qT",
5889 type);
5890 init = error_mark_node;
5893 else
5894 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5897 d->cur++;
5898 return init;
5901 /* "If T is a class type and the initializer list has a single element of
5902 type cv U, where U is T or a class derived from T, the object is
5903 initialized from that element." Even if T is an aggregate. */
5904 if (cxx_dialect >= cxx11 && (CLASS_TYPE_P (type) || VECTOR_TYPE_P (type))
5905 && first_initializer_p
5906 && d->end - d->cur == 1
5907 && reference_related_p (type, TREE_TYPE (init)))
5909 d->cur++;
5910 return init;
5913 /* [dcl.init.aggr]
5915 All implicit type conversions (clause _conv_) are considered when
5916 initializing the aggregate member with an initializer from an
5917 initializer-list. If the initializer can initialize a member,
5918 the member is initialized. Otherwise, if the member is itself a
5919 non-empty subaggregate, brace elision is assumed and the
5920 initializer is considered for the initialization of the first
5921 member of the subaggregate. */
5922 if (TREE_CODE (init) != CONSTRUCTOR
5923 /* But don't try this for the first initializer, since that would be
5924 looking through the outermost braces; A a2 = { a1 }; is not a
5925 valid aggregate initialization. */
5926 && !first_initializer_p
5927 && (same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (init))
5928 || can_convert_arg (type, TREE_TYPE (init), init, LOOKUP_NORMAL,
5929 complain)))
5931 d->cur++;
5932 return init;
5935 /* [dcl.init.string]
5937 A char array (whether plain char, signed char, or unsigned char)
5938 can be initialized by a string-literal (optionally enclosed in
5939 braces); a wchar_t array can be initialized by a wide
5940 string-literal (optionally enclosed in braces). */
5941 if (TREE_CODE (type) == ARRAY_TYPE
5942 && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type))))
5944 tree str_init = init;
5946 /* Strip one level of braces if and only if they enclose a single
5947 element (as allowed by [dcl.init.string]). */
5948 if (!first_initializer_p
5949 && TREE_CODE (str_init) == CONSTRUCTOR
5950 && CONSTRUCTOR_NELTS (str_init) == 1)
5952 str_init = (*CONSTRUCTOR_ELTS (str_init))[0].value;
5955 /* If it's a string literal, then it's the initializer for the array
5956 as a whole. Otherwise, continue with normal initialization for
5957 array types (one value per array element). */
5958 if (TREE_CODE (str_init) == STRING_CST)
5960 if (has_designator_problem (d, complain))
5961 return error_mark_node;
5962 d->cur++;
5963 return str_init;
5967 /* The following cases are about aggregates. If we are not within a full
5968 initializer already, and there is not a CONSTRUCTOR, it means that there
5969 is a missing set of braces (that is, we are processing the case for
5970 which reshape_init exists). */
5971 if (!first_initializer_p)
5973 if (TREE_CODE (init) == CONSTRUCTOR)
5975 if (TREE_TYPE (init) && TYPE_PTRMEMFUNC_P (TREE_TYPE (init)))
5976 /* There is no need to reshape pointer-to-member function
5977 initializers, as they are always constructed correctly
5978 by the front end. */
5980 else if (COMPOUND_LITERAL_P (init))
5981 /* For a nested compound literal, there is no need to reshape since
5982 brace elision is not allowed. Even if we decided to allow it,
5983 we should add a call to reshape_init in finish_compound_literal,
5984 before calling digest_init, so changing this code would still
5985 not be necessary. */
5986 gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (init));
5987 else
5989 ++d->cur;
5990 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
5991 return reshape_init (type, init, complain);
5995 if (complain & tf_warning)
5996 warning (OPT_Wmissing_braces,
5997 "missing braces around initializer for %qT",
5998 type);
6001 /* Dispatch to specialized routines. */
6002 if (CLASS_TYPE_P (type))
6003 return reshape_init_class (type, d, first_initializer_p, complain);
6004 else if (TREE_CODE (type) == ARRAY_TYPE)
6005 return reshape_init_array (type, d, complain);
6006 else if (VECTOR_TYPE_P (type))
6007 return reshape_init_vector (type, d, complain);
6008 else
6009 gcc_unreachable();
6012 /* Undo the brace-elision allowed by [dcl.init.aggr] in a
6013 brace-enclosed aggregate initializer.
6015 INIT is the CONSTRUCTOR containing the list of initializers describing
6016 a brace-enclosed initializer for an entity of the indicated aggregate TYPE.
6017 It may not presently match the shape of the TYPE; for example:
6019 struct S { int a; int b; };
6020 struct S a[] = { 1, 2, 3, 4 };
6022 Here INIT will hold a vector of four elements, rather than a
6023 vector of two elements, each itself a vector of two elements. This
6024 routine transforms INIT from the former form into the latter. The
6025 revised CONSTRUCTOR node is returned. */
6027 tree
6028 reshape_init (tree type, tree init, tsubst_flags_t complain)
6030 vec<constructor_elt, va_gc> *v;
6031 reshape_iter d;
6032 tree new_init;
6034 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
6036 v = CONSTRUCTOR_ELTS (init);
6038 /* An empty constructor does not need reshaping, and it is always a valid
6039 initializer. */
6040 if (vec_safe_is_empty (v))
6041 return init;
6043 /* Handle [dcl.init.list] direct-list-initialization from
6044 single element of enumeration with a fixed underlying type. */
6045 if (is_direct_enum_init (type, init))
6047 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
6048 type = cv_unqualified (type);
6049 if (check_narrowing (ENUM_UNDERLYING_TYPE (type), elt, complain))
6051 warning_sentinel w (warn_useless_cast);
6052 return cp_build_c_cast (type, elt, tf_warning_or_error);
6054 else
6055 return error_mark_node;
6058 /* Recurse on this CONSTRUCTOR. */
6059 d.cur = &(*v)[0];
6060 d.end = d.cur + v->length ();
6062 new_init = reshape_init_r (type, &d, true, complain);
6063 if (new_init == error_mark_node)
6064 return error_mark_node;
6066 /* Make sure all the element of the constructor were used. Otherwise,
6067 issue an error about exceeding initializers. */
6068 if (d.cur != d.end)
6070 if (complain & tf_error)
6071 error ("too many initializers for %qT", type);
6072 return error_mark_node;
6075 if (CONSTRUCTOR_IS_DIRECT_INIT (init)
6076 && BRACE_ENCLOSED_INITIALIZER_P (new_init))
6077 CONSTRUCTOR_IS_DIRECT_INIT (new_init) = true;
6079 return new_init;
6082 /* Verify array initializer. Returns true if errors have been reported. */
6084 bool
6085 check_array_initializer (tree decl, tree type, tree init)
6087 tree element_type = TREE_TYPE (type);
6089 /* The array type itself need not be complete, because the
6090 initializer may tell us how many elements are in the array.
6091 But, the elements of the array must be complete. */
6092 if (!COMPLETE_TYPE_P (complete_type (element_type)))
6094 if (decl)
6095 error_at (DECL_SOURCE_LOCATION (decl),
6096 "elements of array %q#D have incomplete type", decl);
6097 else
6098 error ("elements of array %q#T have incomplete type", type);
6099 return true;
6101 /* A compound literal can't have variable size. */
6102 if (init && !decl
6103 && ((COMPLETE_TYPE_P (type) && !TREE_CONSTANT (TYPE_SIZE (type)))
6104 || !TREE_CONSTANT (TYPE_SIZE (element_type))))
6106 error ("variable-sized compound literal");
6107 return true;
6109 return false;
6112 /* Subroutine of check_initializer; args are passed down from that function.
6113 Set stmts_are_full_exprs_p to 1 across a call to build_aggr_init. */
6115 static tree
6116 build_aggr_init_full_exprs (tree decl, tree init, int flags)
6119 gcc_assert (stmts_are_full_exprs_p ());
6120 return build_aggr_init (decl, init, flags, tf_warning_or_error);
6123 /* Verify INIT (the initializer for DECL), and record the
6124 initialization in DECL_INITIAL, if appropriate. CLEANUP is as for
6125 grok_reference_init.
6127 If the return value is non-NULL, it is an expression that must be
6128 evaluated dynamically to initialize DECL. */
6130 static tree
6131 check_initializer (tree decl, tree init, int flags, vec<tree, va_gc> **cleanups)
6133 tree type = TREE_TYPE (decl);
6134 tree init_code = NULL;
6135 tree core_type;
6137 /* Things that are going to be initialized need to have complete
6138 type. */
6139 TREE_TYPE (decl) = type = complete_type (TREE_TYPE (decl));
6141 if (DECL_HAS_VALUE_EXPR_P (decl))
6143 /* A variable with DECL_HAS_VALUE_EXPR_P set is just a placeholder,
6144 it doesn't have storage to be initialized. */
6145 gcc_assert (init == NULL_TREE);
6146 return NULL_TREE;
6149 if (type == error_mark_node)
6150 /* We will have already complained. */
6151 return NULL_TREE;
6153 if (TREE_CODE (type) == ARRAY_TYPE)
6155 if (check_array_initializer (decl, type, init))
6156 return NULL_TREE;
6158 else if (!COMPLETE_TYPE_P (type))
6160 error_at (DECL_SOURCE_LOCATION (decl),
6161 "%q#D has incomplete type", decl);
6162 TREE_TYPE (decl) = error_mark_node;
6163 return NULL_TREE;
6165 else
6166 /* There is no way to make a variable-sized class type in GNU C++. */
6167 gcc_assert (TREE_CONSTANT (TYPE_SIZE (type)));
6169 if (init && BRACE_ENCLOSED_INITIALIZER_P (init))
6171 int init_len = CONSTRUCTOR_NELTS (init);
6172 if (SCALAR_TYPE_P (type))
6174 if (init_len == 0)
6176 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6177 init = build_zero_init (type, NULL_TREE, false);
6179 else if (init_len != 1 && TREE_CODE (type) != COMPLEX_TYPE)
6181 error_at (EXPR_LOC_OR_LOC (init, DECL_SOURCE_LOCATION (decl)),
6182 "scalar object %qD requires one element in "
6183 "initializer", decl);
6184 TREE_TYPE (decl) = error_mark_node;
6185 return NULL_TREE;
6190 if (TREE_CODE (decl) == CONST_DECL)
6192 gcc_assert (TREE_CODE (type) != REFERENCE_TYPE);
6194 DECL_INITIAL (decl) = init;
6196 gcc_assert (init != NULL_TREE);
6197 init = NULL_TREE;
6199 else if (!init && DECL_REALLY_EXTERN (decl))
6201 else if (init || type_build_ctor_call (type)
6202 || TREE_CODE (type) == REFERENCE_TYPE)
6204 if (TREE_CODE (type) == REFERENCE_TYPE)
6206 init = grok_reference_init (decl, type, init, flags);
6207 flags |= LOOKUP_ALREADY_DIGESTED;
6209 else if (!init)
6210 check_for_uninitialized_const_var (decl);
6211 /* Do not reshape constructors of vectors (they don't need to be
6212 reshaped. */
6213 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
6215 if (is_std_init_list (type))
6217 init = perform_implicit_conversion (type, init,
6218 tf_warning_or_error);
6219 flags |= LOOKUP_ALREADY_DIGESTED;
6221 else if (TYPE_NON_AGGREGATE_CLASS (type))
6223 /* Don't reshape if the class has constructors. */
6224 if (cxx_dialect == cxx98)
6225 error_at (EXPR_LOC_OR_LOC (init, DECL_SOURCE_LOCATION (decl)),
6226 "in C++98 %qD must be initialized by "
6227 "constructor, not by %<{...}%>",
6228 decl);
6230 else if (VECTOR_TYPE_P (type) && TYPE_VECTOR_OPAQUE (type))
6232 error ("opaque vector types cannot be initialized");
6233 init = error_mark_node;
6235 else
6237 init = reshape_init (type, init, tf_warning_or_error);
6238 flags |= LOOKUP_NO_NARROWING;
6241 else if (TREE_CODE (init) == TREE_LIST
6242 && TREE_TYPE (init) != unknown_type_node
6243 && !MAYBE_CLASS_TYPE_P (type))
6245 gcc_assert (TREE_CODE (decl) != RESULT_DECL);
6247 /* We get here with code like `int a (2);' */
6248 init = build_x_compound_expr_from_list (init, ELK_INIT,
6249 tf_warning_or_error);
6252 /* If DECL has an array type without a specific bound, deduce the
6253 array size from the initializer. */
6254 maybe_deduce_size_from_array_init (decl, init);
6255 type = TREE_TYPE (decl);
6256 if (type == error_mark_node)
6257 return NULL_TREE;
6259 if (((type_build_ctor_call (type) || CLASS_TYPE_P (type))
6260 && !(flags & LOOKUP_ALREADY_DIGESTED)
6261 && !(init && BRACE_ENCLOSED_INITIALIZER_P (init)
6262 && CP_AGGREGATE_TYPE_P (type)
6263 && (CLASS_TYPE_P (type)
6264 || !TYPE_NEEDS_CONSTRUCTING (type)
6265 || type_has_extended_temps (type))))
6266 || (DECL_DECOMPOSITION_P (decl) && TREE_CODE (type) == ARRAY_TYPE))
6268 init_code = build_aggr_init_full_exprs (decl, init, flags);
6270 /* A constructor call is a non-trivial initializer even if
6271 it isn't explicitly written. */
6272 if (TREE_SIDE_EFFECTS (init_code))
6273 DECL_NONTRIVIALLY_INITIALIZED_P (decl) = true;
6275 /* If this is a constexpr initializer, expand_default_init will
6276 have returned an INIT_EXPR rather than a CALL_EXPR. In that
6277 case, pull the initializer back out and pass it down into
6278 store_init_value. */
6279 while (TREE_CODE (init_code) == EXPR_STMT
6280 || TREE_CODE (init_code) == CONVERT_EXPR)
6281 init_code = TREE_OPERAND (init_code, 0);
6282 if (TREE_CODE (init_code) == INIT_EXPR)
6284 init = TREE_OPERAND (init_code, 1);
6285 init_code = NULL_TREE;
6286 /* Don't call digest_init; it's unnecessary and will complain
6287 about aggregate initialization of non-aggregate classes. */
6288 flags |= LOOKUP_ALREADY_DIGESTED;
6290 else if (DECL_DECLARED_CONSTEXPR_P (decl))
6292 /* Declared constexpr, but no suitable initializer; massage
6293 init appropriately so we can pass it into store_init_value
6294 for the error. */
6295 if (CLASS_TYPE_P (type)
6296 && (!init || TREE_CODE (init) == TREE_LIST))
6298 init = build_functional_cast (type, init, tf_none);
6299 if (TREE_CODE (init) == TARGET_EXPR)
6300 TARGET_EXPR_DIRECT_INIT_P (init) = true;
6302 init_code = NULL_TREE;
6304 else
6305 init = NULL_TREE;
6308 if (init && TREE_CODE (init) != TREE_VEC)
6310 /* In aggregate initialization of a variable, each element
6311 initialization is a full-expression because there is no
6312 enclosing expression. */
6313 gcc_assert (stmts_are_full_exprs_p ());
6315 init_code = store_init_value (decl, init, cleanups, flags);
6317 if (pedantic && TREE_CODE (type) == ARRAY_TYPE
6318 && DECL_INITIAL (decl)
6319 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
6320 && PAREN_STRING_LITERAL_P (DECL_INITIAL (decl)))
6321 warning_at (EXPR_LOC_OR_LOC (DECL_INITIAL (decl),
6322 DECL_SOURCE_LOCATION (decl)),
6323 0, "array %qD initialized by parenthesized "
6324 "string literal %qE",
6325 decl, DECL_INITIAL (decl));
6326 init = NULL;
6329 else
6331 if (CLASS_TYPE_P (core_type = strip_array_types (type))
6332 && (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type)
6333 || CLASSTYPE_REF_FIELDS_NEED_INIT (core_type)))
6334 diagnose_uninitialized_cst_or_ref_member (core_type, /*using_new=*/false,
6335 /*complain=*/true);
6337 check_for_uninitialized_const_var (decl);
6340 if (init && init != error_mark_node)
6341 init_code = build2 (INIT_EXPR, type, decl, init);
6343 if (init_code)
6345 /* We might have set these in cp_finish_decl. */
6346 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = false;
6347 TREE_CONSTANT (decl) = false;
6350 if (init_code
6351 && (DECL_IN_AGGR_P (decl) && !DECL_VAR_DECLARED_INLINE_P (decl)))
6353 static int explained = 0;
6355 if (cxx_dialect < cxx11)
6356 error ("initializer invalid for static member with constructor");
6357 else if (cxx_dialect < cxx17)
6358 error ("non-constant in-class initialization invalid for static "
6359 "member %qD", decl);
6360 else
6361 error ("non-constant in-class initialization invalid for non-inline "
6362 "static member %qD", decl);
6363 if (!explained)
6365 inform (input_location,
6366 "(an out of class initialization is required)");
6367 explained = 1;
6369 return NULL_TREE;
6372 return init_code;
6375 /* If DECL is not a local variable, give it RTL. */
6377 static void
6378 make_rtl_for_nonlocal_decl (tree decl, tree init, const char* asmspec)
6380 int toplev = toplevel_bindings_p ();
6381 int defer_p;
6383 /* Set the DECL_ASSEMBLER_NAME for the object. */
6384 if (asmspec)
6386 /* The `register' keyword, when used together with an
6387 asm-specification, indicates that the variable should be
6388 placed in a particular register. */
6389 if (VAR_P (decl) && DECL_REGISTER (decl))
6391 set_user_assembler_name (decl, asmspec);
6392 DECL_HARD_REGISTER (decl) = 1;
6394 else
6396 if (TREE_CODE (decl) == FUNCTION_DECL
6397 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
6398 set_builtin_user_assembler_name (decl, asmspec);
6399 set_user_assembler_name (decl, asmspec);
6403 /* Handle non-variables up front. */
6404 if (!VAR_P (decl))
6406 rest_of_decl_compilation (decl, toplev, at_eof);
6407 return;
6410 /* If we see a class member here, it should be a static data
6411 member. */
6412 if (DECL_LANG_SPECIFIC (decl) && DECL_IN_AGGR_P (decl))
6414 gcc_assert (TREE_STATIC (decl));
6415 /* An in-class declaration of a static data member should be
6416 external; it is only a declaration, and not a definition. */
6417 if (init == NULL_TREE)
6418 gcc_assert (DECL_EXTERNAL (decl)
6419 || !TREE_PUBLIC (decl)
6420 || DECL_INLINE_VAR_P (decl));
6423 /* We don't create any RTL for local variables. */
6424 if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
6425 return;
6427 /* We defer emission of local statics until the corresponding
6428 DECL_EXPR is expanded. But with constexpr its function might never
6429 be expanded, so go ahead and tell cgraph about the variable now. */
6430 defer_p = ((DECL_FUNCTION_SCOPE_P (decl)
6431 && !var_in_maybe_constexpr_fn (decl))
6432 || DECL_VIRTUAL_P (decl));
6434 /* Defer template instantiations. */
6435 if (DECL_LANG_SPECIFIC (decl)
6436 && DECL_IMPLICIT_INSTANTIATION (decl))
6437 defer_p = 1;
6439 /* If we're not deferring, go ahead and assemble the variable. */
6440 if (!defer_p)
6441 rest_of_decl_compilation (decl, toplev, at_eof);
6444 /* walk_tree helper for wrap_temporary_cleanups, below. */
6446 static tree
6447 wrap_cleanups_r (tree *stmt_p, int *walk_subtrees, void *data)
6449 /* Stop at types or full-expression boundaries. */
6450 if (TYPE_P (*stmt_p)
6451 || TREE_CODE (*stmt_p) == CLEANUP_POINT_EXPR)
6453 *walk_subtrees = 0;
6454 return NULL_TREE;
6457 if (TREE_CODE (*stmt_p) == TARGET_EXPR)
6459 tree guard = (tree)data;
6460 tree tcleanup = TARGET_EXPR_CLEANUP (*stmt_p);
6462 tcleanup = build2 (TRY_CATCH_EXPR, void_type_node, tcleanup, guard);
6463 /* Tell honor_protect_cleanup_actions to handle this as a separate
6464 cleanup. */
6465 TRY_CATCH_IS_CLEANUP (tcleanup) = 1;
6467 TARGET_EXPR_CLEANUP (*stmt_p) = tcleanup;
6470 return NULL_TREE;
6473 /* We're initializing a local variable which has a cleanup GUARD. If there
6474 are any temporaries used in the initializer INIT of this variable, we
6475 need to wrap their cleanups with TRY_CATCH_EXPR (, GUARD) so that the
6476 variable will be cleaned up properly if one of them throws.
6478 Unfortunately, there's no way to express this properly in terms of
6479 nesting, as the regions for the temporaries overlap the region for the
6480 variable itself; if there are two temporaries, the variable needs to be
6481 the first thing destroyed if either of them throws. However, we only
6482 want to run the variable's cleanup if it actually got constructed. So
6483 we need to guard the temporary cleanups with the variable's cleanup if
6484 they are run on the normal path, but not if they are run on the
6485 exceptional path. We implement this by telling
6486 honor_protect_cleanup_actions to strip the variable cleanup from the
6487 exceptional path. */
6489 static void
6490 wrap_temporary_cleanups (tree init, tree guard)
6492 cp_walk_tree_without_duplicates (&init, wrap_cleanups_r, (void *)guard);
6495 /* Generate code to initialize DECL (a local variable). */
6497 static void
6498 initialize_local_var (tree decl, tree init)
6500 tree type = TREE_TYPE (decl);
6501 tree cleanup;
6502 int already_used;
6504 gcc_assert (VAR_P (decl)
6505 || TREE_CODE (decl) == RESULT_DECL);
6506 gcc_assert (!TREE_STATIC (decl));
6508 if (DECL_SIZE (decl) == NULL_TREE)
6510 /* If we used it already as memory, it must stay in memory. */
6511 DECL_INITIAL (decl) = NULL_TREE;
6512 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
6513 return;
6516 if (type == error_mark_node)
6517 return;
6519 /* Compute and store the initial value. */
6520 already_used = TREE_USED (decl) || TREE_USED (type);
6521 if (TREE_USED (type))
6522 DECL_READ_P (decl) = 1;
6524 /* Generate a cleanup, if necessary. */
6525 cleanup = cxx_maybe_build_cleanup (decl, tf_warning_or_error);
6527 /* Perform the initialization. */
6528 if (init)
6530 tree rinit = (TREE_CODE (init) == INIT_EXPR
6531 ? TREE_OPERAND (init, 1) : NULL_TREE);
6532 if (rinit && !TREE_SIDE_EFFECTS (rinit))
6534 /* Stick simple initializers in DECL_INITIAL so that
6535 -Wno-init-self works (c++/34772). */
6536 gcc_assert (TREE_OPERAND (init, 0) == decl);
6537 DECL_INITIAL (decl) = rinit;
6539 if (warn_init_self && TREE_CODE (type) == REFERENCE_TYPE)
6541 STRIP_NOPS (rinit);
6542 if (rinit == decl)
6543 warning_at (DECL_SOURCE_LOCATION (decl),
6544 OPT_Winit_self,
6545 "reference %qD is initialized with itself", decl);
6548 else
6550 int saved_stmts_are_full_exprs_p;
6552 /* If we're only initializing a single object, guard the
6553 destructors of any temporaries used in its initializer with
6554 its destructor. This isn't right for arrays because each
6555 element initialization is a full-expression. */
6556 if (cleanup && TREE_CODE (type) != ARRAY_TYPE)
6557 wrap_temporary_cleanups (init, cleanup);
6559 gcc_assert (building_stmt_list_p ());
6560 saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
6561 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
6562 finish_expr_stmt (init);
6563 current_stmt_tree ()->stmts_are_full_exprs_p =
6564 saved_stmts_are_full_exprs_p;
6568 /* Set this to 0 so we can tell whether an aggregate which was
6569 initialized was ever used. Don't do this if it has a
6570 destructor, so we don't complain about the 'resource
6571 allocation is initialization' idiom. Now set
6572 attribute((unused)) on types so decls of that type will be
6573 marked used. (see TREE_USED, above.) */
6574 if (TYPE_NEEDS_CONSTRUCTING (type)
6575 && ! already_used
6576 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type)
6577 && DECL_NAME (decl))
6578 TREE_USED (decl) = 0;
6579 else if (already_used)
6580 TREE_USED (decl) = 1;
6582 if (cleanup)
6583 finish_decl_cleanup (decl, cleanup);
6586 /* DECL is a VAR_DECL for a compiler-generated variable with static
6587 storage duration (like a virtual table) whose initializer is a
6588 compile-time constant. Initialize the variable and provide it to the
6589 back end. */
6591 void
6592 initialize_artificial_var (tree decl, vec<constructor_elt, va_gc> *v)
6594 tree init;
6595 gcc_assert (DECL_ARTIFICIAL (decl));
6596 init = build_constructor (TREE_TYPE (decl), v);
6597 gcc_assert (TREE_CODE (init) == CONSTRUCTOR);
6598 DECL_INITIAL (decl) = init;
6599 DECL_INITIALIZED_P (decl) = 1;
6600 determine_visibility (decl);
6601 layout_var_decl (decl);
6602 maybe_commonize_var (decl);
6603 make_rtl_for_nonlocal_decl (decl, init, /*asmspec=*/NULL);
6606 /* INIT is the initializer for a variable, as represented by the
6607 parser. Returns true iff INIT is type-dependent. */
6609 static bool
6610 type_dependent_init_p (tree init)
6612 if (TREE_CODE (init) == TREE_LIST)
6613 /* A parenthesized initializer, e.g.: int i (3, 2); ? */
6614 return any_type_dependent_elements_p (init);
6615 else if (TREE_CODE (init) == CONSTRUCTOR)
6616 /* A brace-enclosed initializer, e.g.: int i = { 3 }; ? */
6618 if (dependent_type_p (TREE_TYPE (init)))
6619 return true;
6621 vec<constructor_elt, va_gc> *elts;
6622 size_t nelts;
6623 size_t i;
6625 elts = CONSTRUCTOR_ELTS (init);
6626 nelts = vec_safe_length (elts);
6627 for (i = 0; i < nelts; ++i)
6628 if (type_dependent_init_p ((*elts)[i].value))
6629 return true;
6631 else
6632 /* It must be a simple expression, e.g., int i = 3; */
6633 return type_dependent_expression_p (init);
6635 return false;
6638 /* INIT is the initializer for a variable, as represented by the
6639 parser. Returns true iff INIT is value-dependent. */
6641 static bool
6642 value_dependent_init_p (tree init)
6644 if (TREE_CODE (init) == TREE_LIST)
6645 /* A parenthesized initializer, e.g.: int i (3, 2); ? */
6646 return any_value_dependent_elements_p (init);
6647 else if (TREE_CODE (init) == CONSTRUCTOR)
6648 /* A brace-enclosed initializer, e.g.: int i = { 3 }; ? */
6650 vec<constructor_elt, va_gc> *elts;
6651 size_t nelts;
6652 size_t i;
6654 elts = CONSTRUCTOR_ELTS (init);
6655 nelts = vec_safe_length (elts);
6656 for (i = 0; i < nelts; ++i)
6657 if (value_dependent_init_p ((*elts)[i].value))
6658 return true;
6660 else
6661 /* It must be a simple expression, e.g., int i = 3; */
6662 return value_dependent_expression_p (init);
6664 return false;
6667 // Returns true if a DECL is VAR_DECL with the concept specifier.
6668 static inline bool
6669 is_concept_var (tree decl)
6671 return (VAR_P (decl)
6672 // Not all variables have DECL_LANG_SPECIFIC.
6673 && DECL_LANG_SPECIFIC (decl)
6674 && DECL_DECLARED_CONCEPT_P (decl));
6677 /* A helper function to be called via walk_tree. If any label exists
6678 under *TP, it is (going to be) forced. Set has_forced_label_in_static. */
6680 static tree
6681 notice_forced_label_r (tree *tp, int *walk_subtrees, void *)
6683 if (TYPE_P (*tp))
6684 *walk_subtrees = 0;
6685 if (TREE_CODE (*tp) == LABEL_DECL)
6686 cfun->has_forced_label_in_static = 1;
6687 return NULL_TREE;
6690 /* Finish processing of a declaration;
6691 install its line number and initial value.
6692 If the length of an array type is not known before,
6693 it must be determined now, from the initial value, or it is an error.
6695 INIT is the initializer (if any) for DECL. If INIT_CONST_EXPR_P is
6696 true, then INIT is an integral constant expression.
6698 FLAGS is LOOKUP_ONLYCONVERTING if the = init syntax was used, else 0
6699 if the (init) syntax was used. */
6701 void
6702 cp_finish_decl (tree decl, tree init, bool init_const_expr_p,
6703 tree asmspec_tree, int flags)
6705 tree type;
6706 vec<tree, va_gc> *cleanups = NULL;
6707 const char *asmspec = NULL;
6708 int was_readonly = 0;
6709 bool var_definition_p = false;
6710 tree auto_node;
6712 if (decl == error_mark_node)
6713 return;
6714 else if (! decl)
6716 if (init)
6717 error ("assignment (not initialization) in declaration");
6718 return;
6721 gcc_assert (TREE_CODE (decl) != RESULT_DECL);
6722 /* Parameters are handled by store_parm_decls, not cp_finish_decl. */
6723 gcc_assert (TREE_CODE (decl) != PARM_DECL);
6725 type = TREE_TYPE (decl);
6726 if (type == error_mark_node)
6727 return;
6729 /* Warn about register storage specifiers except when in GNU global
6730 or local register variable extension. */
6731 if (VAR_P (decl) && DECL_REGISTER (decl) && asmspec_tree == NULL_TREE)
6733 if (cxx_dialect >= cxx17)
6734 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
6735 "ISO C++17 does not allow %<register%> storage "
6736 "class specifier");
6737 else
6738 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
6739 "%<register%> storage class specifier used");
6742 /* If a name was specified, get the string. */
6743 if (at_namespace_scope_p ())
6744 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
6745 if (asmspec_tree && asmspec_tree != error_mark_node)
6746 asmspec = TREE_STRING_POINTER (asmspec_tree);
6748 if (current_class_type
6749 && CP_DECL_CONTEXT (decl) == current_class_type
6750 && TYPE_BEING_DEFINED (current_class_type)
6751 && !CLASSTYPE_TEMPLATE_INSTANTIATION (current_class_type)
6752 && (DECL_INITIAL (decl) || init))
6753 DECL_INITIALIZED_IN_CLASS_P (decl) = 1;
6755 if (TREE_CODE (decl) != FUNCTION_DECL
6756 && (auto_node = type_uses_auto (type)))
6758 tree d_init;
6759 if (init == NULL_TREE)
6761 if (DECL_LANG_SPECIFIC (decl)
6762 && DECL_TEMPLATE_INSTANTIATION (decl)
6763 && !DECL_TEMPLATE_INSTANTIATED (decl))
6765 /* init is null because we're deferring instantiating the
6766 initializer until we need it. Well, we need it now. */
6767 instantiate_decl (decl, /*defer_ok*/true, /*expl*/false);
6768 return;
6771 gcc_assert (CLASS_PLACEHOLDER_TEMPLATE (auto_node));
6773 d_init = init;
6774 if (d_init)
6776 if (TREE_CODE (d_init) == TREE_LIST
6777 && !CLASS_PLACEHOLDER_TEMPLATE (auto_node))
6778 d_init = build_x_compound_expr_from_list (d_init, ELK_INIT,
6779 tf_warning_or_error);
6780 d_init = resolve_nondeduced_context (d_init, tf_warning_or_error);
6782 enum auto_deduction_context adc = adc_variable_type;
6783 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
6784 adc = adc_decomp_type;
6785 type = TREE_TYPE (decl) = do_auto_deduction (type, d_init, auto_node,
6786 tf_warning_or_error, adc,
6787 NULL_TREE, flags);
6788 if (type == error_mark_node)
6789 return;
6790 if (TREE_CODE (type) == FUNCTION_TYPE)
6792 error ("initializer for %<decltype(auto) %D%> has function type "
6793 "(did you forget the %<()%> ?)", decl);
6794 TREE_TYPE (decl) = error_mark_node;
6795 return;
6797 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
6800 if (!ensure_literal_type_for_constexpr_object (decl))
6801 DECL_DECLARED_CONSTEXPR_P (decl) = 0;
6803 if (VAR_P (decl)
6804 && DECL_CLASS_SCOPE_P (decl)
6805 && DECL_INITIALIZED_IN_CLASS_P (decl))
6806 check_static_variable_definition (decl, type);
6808 if (init && TREE_CODE (decl) == FUNCTION_DECL)
6810 tree clone;
6811 if (init == ridpointers[(int)RID_DELETE])
6813 /* FIXME check this is 1st decl. */
6814 DECL_DELETED_FN (decl) = 1;
6815 DECL_DECLARED_INLINE_P (decl) = 1;
6816 DECL_INITIAL (decl) = error_mark_node;
6817 FOR_EACH_CLONE (clone, decl)
6819 DECL_DELETED_FN (clone) = 1;
6820 DECL_DECLARED_INLINE_P (clone) = 1;
6821 DECL_INITIAL (clone) = error_mark_node;
6823 init = NULL_TREE;
6825 else if (init == ridpointers[(int)RID_DEFAULT])
6827 if (defaultable_fn_check (decl))
6828 DECL_DEFAULTED_FN (decl) = 1;
6829 else
6830 DECL_INITIAL (decl) = NULL_TREE;
6834 if (init && VAR_P (decl))
6836 DECL_NONTRIVIALLY_INITIALIZED_P (decl) = 1;
6837 /* If DECL is a reference, then we want to know whether init is a
6838 reference constant; init_const_expr_p as passed tells us whether
6839 it's an rvalue constant. */
6840 if (TREE_CODE (type) == REFERENCE_TYPE)
6841 init_const_expr_p = potential_constant_expression (init);
6842 if (init_const_expr_p)
6844 /* Set these flags now for templates. We'll update the flags in
6845 store_init_value for instantiations. */
6846 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
6847 if (decl_maybe_constant_var_p (decl)
6848 /* FIXME setting TREE_CONSTANT on refs breaks the back end. */
6849 && TREE_CODE (type) != REFERENCE_TYPE)
6850 TREE_CONSTANT (decl) = 1;
6854 if (processing_template_decl)
6856 bool type_dependent_p;
6858 /* Add this declaration to the statement-tree. */
6859 if (at_function_scope_p ())
6860 add_decl_expr (decl);
6862 type_dependent_p = dependent_type_p (type);
6864 if (check_for_bare_parameter_packs (init))
6866 init = NULL_TREE;
6867 DECL_INITIAL (decl) = NULL_TREE;
6870 init = do_dependent_capture (init);
6872 /* Generally, initializers in templates are expanded when the
6873 template is instantiated. But, if DECL is a variable constant
6874 then it can be used in future constant expressions, so its value
6875 must be available. */
6877 if (!VAR_P (decl) || type_dependent_p)
6878 /* We can't do anything if the decl has dependent type. */;
6879 else if (init
6880 && init_const_expr_p
6881 && TREE_CODE (type) != REFERENCE_TYPE
6882 && decl_maybe_constant_var_p (decl)
6883 && !type_dependent_init_p (init)
6884 && !value_dependent_init_p (init))
6886 /* This variable seems to be a non-dependent constant, so process
6887 its initializer. If check_initializer returns non-null the
6888 initialization wasn't constant after all. */
6889 tree init_code;
6890 cleanups = make_tree_vector ();
6891 init_code = check_initializer (decl, init, flags, &cleanups);
6892 if (init_code == NULL_TREE)
6893 init = NULL_TREE;
6894 release_tree_vector (cleanups);
6896 else if (!init && is_concept_var (decl))
6897 error ("variable concept has no initializer");
6898 else if (!DECL_PRETTY_FUNCTION_P (decl))
6900 /* Deduce array size even if the initializer is dependent. */
6901 maybe_deduce_size_from_array_init (decl, init);
6902 /* And complain about multiple initializers. */
6903 if (init && TREE_CODE (init) == TREE_LIST && TREE_CHAIN (init)
6904 && !MAYBE_CLASS_TYPE_P (type))
6905 init = build_x_compound_expr_from_list (init, ELK_INIT,
6906 tf_warning_or_error);
6909 if (init)
6910 DECL_INITIAL (decl) = init;
6911 return;
6914 /* Just store non-static data member initializers for later. */
6915 if (init && TREE_CODE (decl) == FIELD_DECL)
6916 DECL_INITIAL (decl) = init;
6918 /* Take care of TYPE_DECLs up front. */
6919 if (TREE_CODE (decl) == TYPE_DECL)
6921 if (type != error_mark_node
6922 && MAYBE_CLASS_TYPE_P (type) && DECL_NAME (decl))
6924 if (TREE_TYPE (DECL_NAME (decl)) && TREE_TYPE (decl) != type)
6925 warning (0, "shadowing previous type declaration of %q#D", decl);
6926 set_identifier_type_value (DECL_NAME (decl), decl);
6929 /* If we have installed this as the canonical typedef for this
6930 type, and that type has not been defined yet, delay emitting
6931 the debug information for it, as we will emit it later. */
6932 if (TYPE_MAIN_DECL (TREE_TYPE (decl)) == decl
6933 && !COMPLETE_TYPE_P (TREE_TYPE (decl)))
6934 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
6936 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl),
6937 at_eof);
6938 return;
6941 /* A reference will be modified here, as it is initialized. */
6942 if (! DECL_EXTERNAL (decl)
6943 && TREE_READONLY (decl)
6944 && TREE_CODE (type) == REFERENCE_TYPE)
6946 was_readonly = 1;
6947 TREE_READONLY (decl) = 0;
6950 if (VAR_P (decl))
6952 /* If this is a local variable that will need a mangled name,
6953 register it now. We must do this before processing the
6954 initializer for the variable, since the initialization might
6955 require a guard variable, and since the mangled name of the
6956 guard variable will depend on the mangled name of this
6957 variable. */
6958 if (DECL_FUNCTION_SCOPE_P (decl)
6959 && TREE_STATIC (decl)
6960 && !DECL_ARTIFICIAL (decl))
6962 push_local_name (decl);
6963 /* Normally has_forced_label_in_static is set during GIMPLE
6964 lowering, but [cd]tors are never actually compiled directly.
6965 We need to set this early so we can deal with the label
6966 address extension. */
6967 if ((DECL_CONSTRUCTOR_P (current_function_decl)
6968 || DECL_DESTRUCTOR_P (current_function_decl))
6969 && init)
6971 walk_tree (&init, notice_forced_label_r, NULL, NULL);
6972 add_local_decl (cfun, decl);
6974 /* And make sure it's in the symbol table for
6975 c_parse_final_cleanups to find. */
6976 varpool_node::get_create (decl);
6979 /* Convert the initializer to the type of DECL, if we have not
6980 already initialized DECL. */
6981 if (!DECL_INITIALIZED_P (decl)
6982 /* If !DECL_EXTERNAL then DECL is being defined. In the
6983 case of a static data member initialized inside the
6984 class-specifier, there can be an initializer even if DECL
6985 is *not* defined. */
6986 && (!DECL_EXTERNAL (decl) || init))
6988 cleanups = make_tree_vector ();
6989 init = check_initializer (decl, init, flags, &cleanups);
6991 /* Handle:
6993 [dcl.init]
6995 The memory occupied by any object of static storage
6996 duration is zero-initialized at program startup before
6997 any other initialization takes place.
6999 We cannot create an appropriate initializer until after
7000 the type of DECL is finalized. If DECL_INITIAL is set,
7001 then the DECL is statically initialized, and any
7002 necessary zero-initialization has already been performed. */
7003 if (TREE_STATIC (decl) && !DECL_INITIAL (decl))
7004 DECL_INITIAL (decl) = build_zero_init (TREE_TYPE (decl),
7005 /*nelts=*/NULL_TREE,
7006 /*static_storage_p=*/true);
7007 /* Remember that the initialization for this variable has
7008 taken place. */
7009 DECL_INITIALIZED_P (decl) = 1;
7010 /* This declaration is the definition of this variable,
7011 unless we are initializing a static data member within
7012 the class specifier. */
7013 if (!DECL_EXTERNAL (decl))
7014 var_definition_p = true;
7016 /* If the variable has an array type, lay out the type, even if
7017 there is no initializer. It is valid to index through the
7018 array, and we must get TYPE_ALIGN set correctly on the array
7019 type. */
7020 else if (TREE_CODE (type) == ARRAY_TYPE)
7021 layout_type (type);
7023 if (TREE_STATIC (decl)
7024 && !at_function_scope_p ()
7025 && current_function_decl == NULL)
7026 /* So decl is a global variable or a static member of a
7027 non local class. Record the types it uses
7028 so that we can decide later to emit debug info for them. */
7029 record_types_used_by_current_var_decl (decl);
7032 /* Add this declaration to the statement-tree. This needs to happen
7033 after the call to check_initializer so that the DECL_EXPR for a
7034 reference temp is added before the DECL_EXPR for the reference itself. */
7035 if (DECL_FUNCTION_SCOPE_P (decl))
7037 /* If we're building a variable sized type, and we might be
7038 reachable other than via the top of the current binding
7039 level, then create a new BIND_EXPR so that we deallocate
7040 the object at the right time. */
7041 if (VAR_P (decl)
7042 && DECL_SIZE (decl)
7043 && !TREE_CONSTANT (DECL_SIZE (decl))
7044 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
7046 tree bind;
7047 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
7048 TREE_SIDE_EFFECTS (bind) = 1;
7049 add_stmt (bind);
7050 BIND_EXPR_BODY (bind) = push_stmt_list ();
7052 add_decl_expr (decl);
7055 /* Let the middle end know about variables and functions -- but not
7056 static data members in uninstantiated class templates. */
7057 if (VAR_OR_FUNCTION_DECL_P (decl))
7059 if (VAR_P (decl))
7061 layout_var_decl (decl);
7062 maybe_commonize_var (decl);
7065 /* This needs to happen after the linkage is set. */
7066 determine_visibility (decl);
7068 if (var_definition_p && TREE_STATIC (decl))
7070 /* If a TREE_READONLY variable needs initialization
7071 at runtime, it is no longer readonly and we need to
7072 avoid MEM_READONLY_P being set on RTL created for it. */
7073 if (init)
7075 if (TREE_READONLY (decl))
7076 TREE_READONLY (decl) = 0;
7077 was_readonly = 0;
7079 else if (was_readonly)
7080 TREE_READONLY (decl) = 1;
7082 /* Likewise if it needs destruction. */
7083 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
7084 TREE_READONLY (decl) = 0;
7087 make_rtl_for_nonlocal_decl (decl, init, asmspec);
7089 /* Check for abstractness of the type. Notice that there is no
7090 need to strip array types here since the check for those types
7091 is already done within create_array_type_for_decl. */
7092 abstract_virtuals_error (decl, type);
7094 if (TREE_TYPE (decl) == error_mark_node)
7095 /* No initialization required. */
7097 else if (TREE_CODE (decl) == FUNCTION_DECL)
7099 if (init)
7101 if (init == ridpointers[(int)RID_DEFAULT])
7103 /* An out-of-class default definition is defined at
7104 the point where it is explicitly defaulted. */
7105 if (DECL_DELETED_FN (decl))
7106 maybe_explain_implicit_delete (decl);
7107 else if (DECL_INITIAL (decl) == error_mark_node)
7108 synthesize_method (decl);
7110 else
7111 error ("function %q#D is initialized like a variable", decl);
7113 /* else no initialization required. */
7115 else if (DECL_EXTERNAL (decl)
7116 && ! (DECL_LANG_SPECIFIC (decl)
7117 && DECL_NOT_REALLY_EXTERN (decl)))
7119 if (init)
7120 DECL_INITIAL (decl) = init;
7122 /* A variable definition. */
7123 else if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
7124 /* Initialize the local variable. */
7125 initialize_local_var (decl, init);
7127 /* If a variable is defined, and then a subsequent
7128 definition with external linkage is encountered, we will
7129 get here twice for the same variable. We want to avoid
7130 calling expand_static_init more than once. For variables
7131 that are not static data members, we can call
7132 expand_static_init only when we actually process the
7133 initializer. It is not legal to redeclare a static data
7134 member, so this issue does not arise in that case. */
7135 else if (var_definition_p && TREE_STATIC (decl))
7136 expand_static_init (decl, init);
7139 /* If a CLEANUP_STMT was created to destroy a temporary bound to a
7140 reference, insert it in the statement-tree now. */
7141 if (cleanups)
7143 unsigned i; tree t;
7144 FOR_EACH_VEC_ELT (*cleanups, i, t)
7145 push_cleanup (decl, t, false);
7146 release_tree_vector (cleanups);
7149 if (was_readonly)
7150 TREE_READONLY (decl) = 1;
7152 invoke_plugin_callbacks (PLUGIN_FINISH_DECL, decl);
7155 /* For class TYPE return itself or some its bases that contain
7156 any direct non-static data members. Return error_mark_node if an
7157 error has been diagnosed. */
7159 static tree
7160 find_decomp_class_base (location_t loc, tree type, tree ret)
7162 bool member_seen = false;
7163 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
7164 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
7165 continue;
7166 else if (ret)
7167 return type;
7168 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
7170 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE)
7171 error_at (loc, "cannot decompose class type %qT because it has an "
7172 "anonymous struct member", type);
7173 else
7174 error_at (loc, "cannot decompose class type %qT because it has an "
7175 "anonymous union member", type);
7176 inform (DECL_SOURCE_LOCATION (field), "declared here");
7177 return error_mark_node;
7179 else if (TREE_PRIVATE (field) || TREE_PROTECTED (field))
7181 error_at (loc, "cannot decompose non-public member %qD of %qT",
7182 field, type);
7183 inform (DECL_SOURCE_LOCATION (field),
7184 TREE_PRIVATE (field)
7185 ? G_("declared private here")
7186 : G_("declared protected here"));
7187 return error_mark_node;
7189 else
7190 member_seen = true;
7192 tree base_binfo, binfo;
7193 tree orig_ret = ret;
7194 int i;
7195 if (member_seen)
7196 ret = type;
7197 for (binfo = TYPE_BINFO (type), i = 0;
7198 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
7200 tree t = find_decomp_class_base (loc, TREE_TYPE (base_binfo), ret);
7201 if (t == error_mark_node)
7202 return error_mark_node;
7203 if (t != NULL_TREE)
7205 if (ret == type)
7207 error_at (loc, "cannot decompose class type %qT: both it and "
7208 "its base class %qT have non-static data members",
7209 type, t);
7210 return error_mark_node;
7212 else if (orig_ret != NULL_TREE)
7213 return t;
7214 else if (ret == t)
7215 /* OK, found the same base along another path. We'll complain
7216 in convert_to_base if it's ambiguous. */;
7217 else if (ret != NULL_TREE)
7219 error_at (loc, "cannot decompose class type %qT: its base "
7220 "classes %qT and %qT have non-static data "
7221 "members", type, ret, t);
7222 return error_mark_node;
7224 else
7225 ret = t;
7228 return ret;
7231 /* Return std::tuple_size<TYPE>::value. */
7233 static tree
7234 get_tuple_size (tree type)
7236 tree args = make_tree_vec (1);
7237 TREE_VEC_ELT (args, 0) = type;
7238 tree inst = lookup_template_class (get_identifier ("tuple_size"), args,
7239 /*in_decl*/NULL_TREE,
7240 /*context*/std_node,
7241 /*entering_scope*/false, tf_none);
7242 inst = complete_type (inst);
7243 if (inst == error_mark_node || !COMPLETE_TYPE_P (inst))
7244 return NULL_TREE;
7245 tree val = lookup_qualified_name (inst, get_identifier ("value"),
7246 /*type*/false, /*complain*/false);
7247 if (TREE_CODE (val) == VAR_DECL || TREE_CODE (val) == CONST_DECL)
7248 val = maybe_constant_value (val);
7249 if (TREE_CODE (val) == INTEGER_CST)
7250 return val;
7251 else
7252 return error_mark_node;
7255 /* Return std::tuple_element<I,TYPE>::type. */
7257 static tree
7258 get_tuple_element_type (tree type, unsigned i)
7260 tree args = make_tree_vec (2);
7261 TREE_VEC_ELT (args, 0) = build_int_cst (integer_type_node, i);
7262 TREE_VEC_ELT (args, 1) = type;
7263 tree inst = lookup_template_class (get_identifier ("tuple_element"), args,
7264 /*in_decl*/NULL_TREE,
7265 /*context*/std_node,
7266 /*entering_scope*/false,
7267 tf_warning_or_error);
7268 return make_typename_type (inst, get_identifier ("type"),
7269 none_type, tf_warning_or_error);
7272 /* Return e.get<i>() or get<i>(e). */
7274 static tree
7275 get_tuple_decomp_init (tree decl, unsigned i)
7277 tree get_id = get_identifier ("get");
7278 tree targs = make_tree_vec (1);
7279 TREE_VEC_ELT (targs, 0) = build_int_cst (integer_type_node, i);
7281 tree etype = TREE_TYPE (decl);
7282 tree e = convert_from_reference (decl);
7284 /* [The id-expression] e is an lvalue if the type of the entity e is an
7285 lvalue reference and an xvalue otherwise. */
7286 if (TREE_CODE (etype) != REFERENCE_TYPE
7287 || TYPE_REF_IS_RVALUE (etype))
7288 e = move (e);
7290 tree fns = lookup_qualified_name (TREE_TYPE (e), get_id,
7291 /*type*/false, /*complain*/false);
7292 if (fns != error_mark_node)
7294 fns = lookup_template_function (fns, targs);
7295 return build_new_method_call (e, fns, /*args*/NULL,
7296 /*path*/NULL_TREE, LOOKUP_NORMAL,
7297 /*fn_p*/NULL, tf_warning_or_error);
7299 else
7301 vec<tree,va_gc> *args = make_tree_vector_single (e);
7302 fns = lookup_template_function (get_id, targs);
7303 fns = perform_koenig_lookup (fns, args, tf_warning_or_error);
7304 return finish_call_expr (fns, &args, /*novirt*/false,
7305 /*koenig*/true, tf_warning_or_error);
7309 /* It's impossible to recover the decltype of a tuple decomposition variable
7310 based on the actual type of the variable, so store it in a hash table. */
7311 static GTY(()) hash_map<tree,tree> *decomp_type_table;
7312 static void
7313 store_decomp_type (tree v, tree t)
7315 if (!decomp_type_table)
7316 decomp_type_table = hash_map<tree,tree>::create_ggc (13);
7317 decomp_type_table->put (v, t);
7320 tree
7321 lookup_decomp_type (tree v)
7323 return *decomp_type_table->get (v);
7326 /* Finish a decomposition declaration. DECL is the underlying declaration
7327 "e", FIRST is the head of a chain of decls for the individual identifiers
7328 chained through DECL_CHAIN in reverse order and COUNT is the number of
7329 those decls. */
7331 void
7332 cp_finish_decomp (tree decl, tree first, unsigned int count)
7334 if (error_operand_p (decl))
7336 error_out:
7337 while (count--)
7339 TREE_TYPE (first) = error_mark_node;
7340 if (DECL_HAS_VALUE_EXPR_P (first))
7342 SET_DECL_VALUE_EXPR (first, NULL_TREE);
7343 DECL_HAS_VALUE_EXPR_P (first) = 0;
7345 first = DECL_CHAIN (first);
7347 if (DECL_P (decl) && DECL_NAMESPACE_SCOPE_P (decl))
7348 SET_DECL_ASSEMBLER_NAME (decl, get_identifier ("<decomp>"));
7349 return;
7352 location_t loc = DECL_SOURCE_LOCATION (decl);
7353 if (type_dependent_expression_p (decl)
7354 /* This happens for range for when not in templates.
7355 Still add the DECL_VALUE_EXPRs for later processing. */
7356 || (!processing_template_decl
7357 && type_uses_auto (TREE_TYPE (decl))))
7359 for (unsigned int i = 0; i < count; i++)
7361 if (!DECL_HAS_VALUE_EXPR_P (first))
7363 tree v = build_nt (ARRAY_REF, decl,
7364 size_int (count - i - 1),
7365 NULL_TREE, NULL_TREE);
7366 SET_DECL_VALUE_EXPR (first, v);
7367 DECL_HAS_VALUE_EXPR_P (first) = 1;
7369 if (processing_template_decl)
7370 fit_decomposition_lang_decl (first, decl);
7371 first = DECL_CHAIN (first);
7373 return;
7376 auto_vec<tree, 16> v;
7377 v.safe_grow (count);
7378 tree d = first;
7379 for (unsigned int i = 0; i < count; i++, d = DECL_CHAIN (d))
7381 v[count - i - 1] = d;
7382 fit_decomposition_lang_decl (d, decl);
7385 tree type = TREE_TYPE (decl);
7386 tree dexp = decl;
7388 if (TREE_CODE (type) == REFERENCE_TYPE)
7390 dexp = convert_from_reference (dexp);
7391 type = TREE_TYPE (type);
7394 tree eltype = NULL_TREE;
7395 unsigned HOST_WIDE_INT eltscnt = 0;
7396 if (TREE_CODE (type) == ARRAY_TYPE)
7398 tree nelts;
7399 nelts = array_type_nelts_top (type);
7400 if (nelts == error_mark_node)
7401 goto error_out;
7402 if (!tree_fits_uhwi_p (nelts))
7404 error_at (loc, "cannot decompose variable length array %qT", type);
7405 goto error_out;
7407 eltscnt = tree_to_uhwi (nelts);
7408 if (count != eltscnt)
7410 cnt_mismatch:
7411 if (count > eltscnt)
7412 error_at (loc, "%u names provided while %qT decomposes into "
7413 "%wu elements", count, type, eltscnt);
7414 else
7415 error_at (loc, "only %u names provided while %qT decomposes into "
7416 "%wu elements", count, type, eltscnt);
7417 goto error_out;
7419 eltype = TREE_TYPE (type);
7420 for (unsigned int i = 0; i < count; i++)
7422 TREE_TYPE (v[i]) = eltype;
7423 layout_decl (v[i], 0);
7424 if (processing_template_decl)
7425 continue;
7426 tree t = unshare_expr (dexp);
7427 t = build4_loc (DECL_SOURCE_LOCATION (v[i]), ARRAY_REF,
7428 eltype, t, size_int (i), NULL_TREE,
7429 NULL_TREE);
7430 SET_DECL_VALUE_EXPR (v[i], t);
7431 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
7434 /* 2 GNU extensions. */
7435 else if (TREE_CODE (type) == COMPLEX_TYPE)
7437 eltscnt = 2;
7438 if (count != eltscnt)
7439 goto cnt_mismatch;
7440 eltype = cp_build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
7441 for (unsigned int i = 0; i < count; i++)
7443 TREE_TYPE (v[i]) = eltype;
7444 layout_decl (v[i], 0);
7445 if (processing_template_decl)
7446 continue;
7447 tree t = unshare_expr (dexp);
7448 t = build1_loc (DECL_SOURCE_LOCATION (v[i]),
7449 i ? IMAGPART_EXPR : REALPART_EXPR, eltype,
7451 SET_DECL_VALUE_EXPR (v[i], t);
7452 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
7455 else if (TREE_CODE (type) == VECTOR_TYPE)
7457 eltscnt = TYPE_VECTOR_SUBPARTS (type);
7458 if (count != eltscnt)
7459 goto cnt_mismatch;
7460 eltype = cp_build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
7461 for (unsigned int i = 0; i < count; i++)
7463 TREE_TYPE (v[i]) = eltype;
7464 layout_decl (v[i], 0);
7465 if (processing_template_decl)
7466 continue;
7467 tree t = unshare_expr (dexp);
7468 convert_vector_to_array_for_subscript (DECL_SOURCE_LOCATION (v[i]),
7469 &t, size_int (i));
7470 t = build4_loc (DECL_SOURCE_LOCATION (v[i]), ARRAY_REF,
7471 eltype, t, size_int (i), NULL_TREE,
7472 NULL_TREE);
7473 SET_DECL_VALUE_EXPR (v[i], t);
7474 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
7477 else if (tree tsize = get_tuple_size (type))
7479 if (tsize == error_mark_node)
7481 error_at (loc, "%<std::tuple_size<%T>::value%> is not an integral "
7482 "constant expression", type);
7483 goto error_out;
7485 eltscnt = tree_to_uhwi (tsize);
7486 if (count != eltscnt)
7487 goto cnt_mismatch;
7488 int save_read = DECL_READ_P (decl);
7489 for (unsigned i = 0; i < count; ++i)
7491 location_t sloc = input_location;
7492 location_t dloc = DECL_SOURCE_LOCATION (v[i]);
7494 input_location = dloc;
7495 tree init = get_tuple_decomp_init (decl, i);
7496 tree eltype = (init == error_mark_node ? error_mark_node
7497 : get_tuple_element_type (type, i));
7498 input_location = sloc;
7500 if (init == error_mark_node || eltype == error_mark_node)
7502 inform (dloc, "in initialization of structured binding "
7503 "variable %qD", v[i]);
7504 goto error_out;
7506 /* Save the decltype away before reference collapse. */
7507 store_decomp_type (v[i], eltype);
7508 eltype = cp_build_reference_type (eltype, !lvalue_p (init));
7509 TREE_TYPE (v[i]) = eltype;
7510 layout_decl (v[i], 0);
7511 if (DECL_HAS_VALUE_EXPR_P (v[i]))
7513 /* In this case the names are variables, not just proxies. */
7514 SET_DECL_VALUE_EXPR (v[i], NULL_TREE);
7515 DECL_HAS_VALUE_EXPR_P (v[i]) = 0;
7517 if (!processing_template_decl)
7518 cp_finish_decl (v[i], init, /*constexpr*/false,
7519 /*asm*/NULL_TREE, LOOKUP_NORMAL);
7521 /* Ignore reads from the underlying decl performed during initialization
7522 of the individual variables. If those will be read, we'll mark
7523 the underlying decl as read at that point. */
7524 DECL_READ_P (decl) = save_read;
7526 else if (TREE_CODE (type) == UNION_TYPE)
7528 error_at (loc, "cannot decompose union type %qT", type);
7529 goto error_out;
7531 else if (!CLASS_TYPE_P (type))
7533 error_at (loc, "cannot decompose non-array non-class type %qT", type);
7534 goto error_out;
7536 else if (LAMBDA_TYPE_P (type))
7538 error_at (loc, "cannot decompose lambda closure type %qT", type);
7539 goto error_out;
7541 else
7543 tree btype = find_decomp_class_base (loc, type, NULL_TREE);
7544 if (btype == error_mark_node)
7545 goto error_out;
7546 else if (btype == NULL_TREE)
7548 error_at (loc, "cannot decompose class type %qT without non-static "
7549 "data members", type);
7550 goto error_out;
7552 for (tree field = TYPE_FIELDS (btype); field; field = TREE_CHAIN (field))
7553 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
7554 continue;
7555 else
7556 eltscnt++;
7557 if (count != eltscnt)
7558 goto cnt_mismatch;
7559 tree t = dexp;
7560 if (type != btype)
7562 t = convert_to_base (t, btype, /*check_access*/true,
7563 /*nonnull*/false, tf_warning_or_error);
7564 type = btype;
7566 unsigned int i = 0;
7567 for (tree field = TYPE_FIELDS (btype); field; field = TREE_CHAIN (field))
7568 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
7569 continue;
7570 else
7572 tree tt = finish_non_static_data_member (field, unshare_expr (t),
7573 NULL_TREE);
7574 if (REFERENCE_REF_P (tt))
7575 tt = TREE_OPERAND (tt, 0);
7576 TREE_TYPE (v[i]) = TREE_TYPE (tt);
7577 layout_decl (v[i], 0);
7578 if (!processing_template_decl)
7580 SET_DECL_VALUE_EXPR (v[i], tt);
7581 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
7583 i++;
7586 if (processing_template_decl)
7588 for (unsigned int i = 0; i < count; i++)
7589 if (!DECL_HAS_VALUE_EXPR_P (v[i]))
7591 tree a = build_nt (ARRAY_REF, decl, size_int (i),
7592 NULL_TREE, NULL_TREE);
7593 SET_DECL_VALUE_EXPR (v[i], a);
7594 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
7597 else if (DECL_NAMESPACE_SCOPE_P (decl))
7598 SET_DECL_ASSEMBLER_NAME (decl, mangle_decomp (decl, v));
7601 /* Returns a declaration for a VAR_DECL as if:
7603 extern "C" TYPE NAME;
7605 had been seen. Used to create compiler-generated global
7606 variables. */
7608 static tree
7609 declare_global_var (tree name, tree type)
7611 tree decl;
7613 push_to_top_level ();
7614 decl = build_decl (input_location, VAR_DECL, name, type);
7615 TREE_PUBLIC (decl) = 1;
7616 DECL_EXTERNAL (decl) = 1;
7617 DECL_ARTIFICIAL (decl) = 1;
7618 DECL_CONTEXT (decl) = FROB_CONTEXT (global_namespace);
7619 /* If the user has explicitly declared this variable (perhaps
7620 because the code we are compiling is part of a low-level runtime
7621 library), then it is possible that our declaration will be merged
7622 with theirs by pushdecl. */
7623 decl = pushdecl (decl);
7624 cp_finish_decl (decl, NULL_TREE, false, NULL_TREE, 0);
7625 pop_from_top_level ();
7627 return decl;
7630 /* Returns the type for the argument to "__cxa_atexit" (or "atexit",
7631 if "__cxa_atexit" is not being used) corresponding to the function
7632 to be called when the program exits. */
7634 static tree
7635 get_atexit_fn_ptr_type (void)
7637 tree fn_type;
7639 if (!atexit_fn_ptr_type_node)
7641 tree arg_type;
7642 if (flag_use_cxa_atexit
7643 && !targetm.cxx.use_atexit_for_cxa_atexit ())
7644 /* The parameter to "__cxa_atexit" is "void (*)(void *)". */
7645 arg_type = ptr_type_node;
7646 else
7647 /* The parameter to "atexit" is "void (*)(void)". */
7648 arg_type = NULL_TREE;
7650 fn_type = build_function_type_list (void_type_node,
7651 arg_type, NULL_TREE);
7652 atexit_fn_ptr_type_node = build_pointer_type (fn_type);
7655 return atexit_fn_ptr_type_node;
7658 /* Returns a pointer to the `atexit' function. Note that if
7659 FLAG_USE_CXA_ATEXIT is nonzero, then this will actually be the new
7660 `__cxa_atexit' function specified in the IA64 C++ ABI. */
7662 static tree
7663 get_atexit_node (void)
7665 tree atexit_fndecl;
7666 tree fn_type;
7667 tree fn_ptr_type;
7668 const char *name;
7669 bool use_aeabi_atexit;
7671 if (atexit_node)
7672 return atexit_node;
7674 if (flag_use_cxa_atexit && !targetm.cxx.use_atexit_for_cxa_atexit ())
7676 /* The declaration for `__cxa_atexit' is:
7678 int __cxa_atexit (void (*)(void *), void *, void *)
7680 We build up the argument types and then the function type
7681 itself. */
7682 tree argtype0, argtype1, argtype2;
7684 use_aeabi_atexit = targetm.cxx.use_aeabi_atexit ();
7685 /* First, build the pointer-to-function type for the first
7686 argument. */
7687 fn_ptr_type = get_atexit_fn_ptr_type ();
7688 /* Then, build the rest of the argument types. */
7689 argtype2 = ptr_type_node;
7690 if (use_aeabi_atexit)
7692 argtype1 = fn_ptr_type;
7693 argtype0 = ptr_type_node;
7695 else
7697 argtype1 = ptr_type_node;
7698 argtype0 = fn_ptr_type;
7700 /* And the final __cxa_atexit type. */
7701 fn_type = build_function_type_list (integer_type_node,
7702 argtype0, argtype1, argtype2,
7703 NULL_TREE);
7704 if (use_aeabi_atexit)
7705 name = "__aeabi_atexit";
7706 else
7707 name = "__cxa_atexit";
7709 else
7711 /* The declaration for `atexit' is:
7713 int atexit (void (*)());
7715 We build up the argument types and then the function type
7716 itself. */
7717 fn_ptr_type = get_atexit_fn_ptr_type ();
7718 /* Build the final atexit type. */
7719 fn_type = build_function_type_list (integer_type_node,
7720 fn_ptr_type, NULL_TREE);
7721 name = "atexit";
7724 /* Now, build the function declaration. */
7725 push_lang_context (lang_name_c);
7726 atexit_fndecl = build_library_fn_ptr (name, fn_type, ECF_LEAF | ECF_NOTHROW);
7727 mark_used (atexit_fndecl);
7728 pop_lang_context ();
7729 atexit_node = decay_conversion (atexit_fndecl, tf_warning_or_error);
7731 return atexit_node;
7734 /* Like get_atexit_node, but for thread-local cleanups. */
7736 static tree
7737 get_thread_atexit_node (void)
7739 /* The declaration for `__cxa_thread_atexit' is:
7741 int __cxa_thread_atexit (void (*)(void *), void *, void *) */
7742 tree fn_type = build_function_type_list (integer_type_node,
7743 get_atexit_fn_ptr_type (),
7744 ptr_type_node, ptr_type_node,
7745 NULL_TREE);
7747 /* Now, build the function declaration. */
7748 tree atexit_fndecl = build_library_fn_ptr ("__cxa_thread_atexit", fn_type,
7749 ECF_LEAF | ECF_NOTHROW);
7750 return decay_conversion (atexit_fndecl, tf_warning_or_error);
7753 /* Returns the __dso_handle VAR_DECL. */
7755 static tree
7756 get_dso_handle_node (void)
7758 if (dso_handle_node)
7759 return dso_handle_node;
7761 /* Declare the variable. */
7762 dso_handle_node = declare_global_var (get_identifier ("__dso_handle"),
7763 ptr_type_node);
7765 #ifdef HAVE_GAS_HIDDEN
7766 if (dso_handle_node != error_mark_node)
7768 DECL_VISIBILITY (dso_handle_node) = VISIBILITY_HIDDEN;
7769 DECL_VISIBILITY_SPECIFIED (dso_handle_node) = 1;
7771 #endif
7773 return dso_handle_node;
7776 /* Begin a new function with internal linkage whose job will be simply
7777 to destroy some particular variable. */
7779 static GTY(()) int start_cleanup_cnt;
7781 static tree
7782 start_cleanup_fn (void)
7784 char name[32];
7785 tree fntype;
7786 tree fndecl;
7787 bool use_cxa_atexit = flag_use_cxa_atexit
7788 && !targetm.cxx.use_atexit_for_cxa_atexit ();
7790 push_to_top_level ();
7792 /* No need to mangle this. */
7793 push_lang_context (lang_name_c);
7795 /* Build the name of the function. */
7796 sprintf (name, "__tcf_%d", start_cleanup_cnt++);
7797 /* Build the function declaration. */
7798 fntype = TREE_TYPE (get_atexit_fn_ptr_type ());
7799 fndecl = build_lang_decl (FUNCTION_DECL, get_identifier (name), fntype);
7800 /* It's a function with internal linkage, generated by the
7801 compiler. */
7802 TREE_PUBLIC (fndecl) = 0;
7803 DECL_ARTIFICIAL (fndecl) = 1;
7804 /* Make the function `inline' so that it is only emitted if it is
7805 actually needed. It is unlikely that it will be inlined, since
7806 it is only called via a function pointer, but we avoid unnecessary
7807 emissions this way. */
7808 DECL_DECLARED_INLINE_P (fndecl) = 1;
7809 DECL_INTERFACE_KNOWN (fndecl) = 1;
7810 /* Build the parameter. */
7811 if (use_cxa_atexit)
7813 tree parmdecl = cp_build_parm_decl (fndecl, NULL_TREE, ptr_type_node);
7814 TREE_USED (parmdecl) = 1;
7815 DECL_READ_P (parmdecl) = 1;
7816 DECL_ARGUMENTS (fndecl) = parmdecl;
7819 pushdecl (fndecl);
7820 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
7822 pop_lang_context ();
7824 return current_function_decl;
7827 /* Finish the cleanup function begun by start_cleanup_fn. */
7829 static void
7830 end_cleanup_fn (void)
7832 expand_or_defer_fn (finish_function (/*inline_p=*/false));
7834 pop_from_top_level ();
7837 /* Generate code to handle the destruction of DECL, an object with
7838 static storage duration. */
7840 tree
7841 register_dtor_fn (tree decl)
7843 tree cleanup;
7844 tree addr;
7845 tree compound_stmt;
7846 tree fcall;
7847 tree type;
7848 bool ob_parm, dso_parm, use_dtor;
7849 tree arg0, arg1, arg2;
7850 tree atex_node;
7852 type = TREE_TYPE (decl);
7853 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
7854 return void_node;
7856 /* If we're using "__cxa_atexit" (or "__cxa_thread_atexit" or
7857 "__aeabi_atexit"), and DECL is a class object, we can just pass the
7858 destructor to "__cxa_atexit"; we don't have to build a temporary
7859 function to do the cleanup. */
7860 dso_parm = (flag_use_cxa_atexit
7861 && !targetm.cxx.use_atexit_for_cxa_atexit ());
7862 ob_parm = (CP_DECL_THREAD_LOCAL_P (decl) || dso_parm);
7863 use_dtor = ob_parm && CLASS_TYPE_P (type);
7864 if (use_dtor)
7866 cleanup = get_class_binding (type, complete_dtor_identifier);
7868 /* Make sure it is accessible. */
7869 perform_or_defer_access_check (TYPE_BINFO (type), cleanup, cleanup,
7870 tf_warning_or_error);
7872 else
7874 /* Call build_cleanup before we enter the anonymous function so
7875 that any access checks will be done relative to the current
7876 scope, rather than the scope of the anonymous function. */
7877 build_cleanup (decl);
7879 /* Now start the function. */
7880 cleanup = start_cleanup_fn ();
7882 /* Now, recompute the cleanup. It may contain SAVE_EXPRs that refer
7883 to the original function, rather than the anonymous one. That
7884 will make the back end think that nested functions are in use,
7885 which causes confusion. */
7886 push_deferring_access_checks (dk_no_check);
7887 fcall = build_cleanup (decl);
7888 pop_deferring_access_checks ();
7890 /* Create the body of the anonymous function. */
7891 compound_stmt = begin_compound_stmt (BCS_FN_BODY);
7892 finish_expr_stmt (fcall);
7893 finish_compound_stmt (compound_stmt);
7894 end_cleanup_fn ();
7897 /* Call atexit with the cleanup function. */
7898 mark_used (cleanup);
7899 cleanup = build_address (cleanup);
7901 if (CP_DECL_THREAD_LOCAL_P (decl))
7902 atex_node = get_thread_atexit_node ();
7903 else
7904 atex_node = get_atexit_node ();
7906 if (use_dtor)
7908 /* We must convert CLEANUP to the type that "__cxa_atexit"
7909 expects. */
7910 cleanup = build_nop (get_atexit_fn_ptr_type (), cleanup);
7911 /* "__cxa_atexit" will pass the address of DECL to the
7912 cleanup function. */
7913 mark_used (decl);
7914 addr = build_address (decl);
7915 /* The declared type of the parameter to "__cxa_atexit" is
7916 "void *". For plain "T*", we could just let the
7917 machinery in cp_build_function_call convert it -- but if the
7918 type is "cv-qualified T *", then we need to convert it
7919 before passing it in, to avoid spurious errors. */
7920 addr = build_nop (ptr_type_node, addr);
7922 else
7923 /* Since the cleanup functions we build ignore the address
7924 they're given, there's no reason to pass the actual address
7925 in, and, in general, it's cheaper to pass NULL than any
7926 other value. */
7927 addr = null_pointer_node;
7929 if (dso_parm)
7930 arg2 = cp_build_addr_expr (get_dso_handle_node (),
7931 tf_warning_or_error);
7932 else if (ob_parm)
7933 /* Just pass NULL to the dso handle parm if we don't actually
7934 have a DSO handle on this target. */
7935 arg2 = null_pointer_node;
7936 else
7937 arg2 = NULL_TREE;
7939 if (ob_parm)
7941 if (!CP_DECL_THREAD_LOCAL_P (decl)
7942 && targetm.cxx.use_aeabi_atexit ())
7944 arg1 = cleanup;
7945 arg0 = addr;
7947 else
7949 arg1 = addr;
7950 arg0 = cleanup;
7953 else
7955 arg0 = cleanup;
7956 arg1 = NULL_TREE;
7958 return cp_build_function_call_nary (atex_node, tf_warning_or_error,
7959 arg0, arg1, arg2, NULL_TREE);
7962 /* DECL is a VAR_DECL with static storage duration. INIT, if present,
7963 is its initializer. Generate code to handle the construction
7964 and destruction of DECL. */
7966 static void
7967 expand_static_init (tree decl, tree init)
7969 gcc_assert (VAR_P (decl));
7970 gcc_assert (TREE_STATIC (decl));
7972 /* Some variables require no dynamic initialization. */
7973 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
7975 /* Make sure the destructor is callable. */
7976 cxx_maybe_build_cleanup (decl, tf_warning_or_error);
7977 if (!init)
7978 return;
7981 if (CP_DECL_THREAD_LOCAL_P (decl) && DECL_GNU_TLS_P (decl)
7982 && !DECL_FUNCTION_SCOPE_P (decl))
7984 if (init)
7985 error ("non-local variable %qD declared %<__thread%> "
7986 "needs dynamic initialization", decl);
7987 else
7988 error ("non-local variable %qD declared %<__thread%> "
7989 "has a non-trivial destructor", decl);
7990 static bool informed;
7991 if (!informed)
7993 inform (DECL_SOURCE_LOCATION (decl),
7994 "C++11 %<thread_local%> allows dynamic initialization "
7995 "and destruction");
7996 informed = true;
7998 return;
8001 if (DECL_FUNCTION_SCOPE_P (decl))
8003 /* Emit code to perform this initialization but once. */
8004 tree if_stmt = NULL_TREE, inner_if_stmt = NULL_TREE;
8005 tree then_clause = NULL_TREE, inner_then_clause = NULL_TREE;
8006 tree guard, guard_addr;
8007 tree flag, begin;
8008 /* We don't need thread-safety code for thread-local vars. */
8009 bool thread_guard = (flag_threadsafe_statics
8010 && !CP_DECL_THREAD_LOCAL_P (decl));
8012 /* Emit code to perform this initialization but once. This code
8013 looks like:
8015 static <type> guard;
8016 if (!__atomic_load (guard.first_byte)) {
8017 if (__cxa_guard_acquire (&guard)) {
8018 bool flag = false;
8019 try {
8020 // Do initialization.
8021 flag = true; __cxa_guard_release (&guard);
8022 // Register variable for destruction at end of program.
8023 } catch {
8024 if (!flag) __cxa_guard_abort (&guard);
8029 Note that the `flag' variable is only set to 1 *after* the
8030 initialization is complete. This ensures that an exception,
8031 thrown during the construction, will cause the variable to
8032 reinitialized when we pass through this code again, as per:
8034 [stmt.dcl]
8036 If the initialization exits by throwing an exception, the
8037 initialization is not complete, so it will be tried again
8038 the next time control enters the declaration.
8040 This process should be thread-safe, too; multiple threads
8041 should not be able to initialize the variable more than
8042 once. */
8044 /* Create the guard variable. */
8045 guard = get_guard (decl);
8047 /* Begin the conditional initialization. */
8048 if_stmt = begin_if_stmt ();
8050 finish_if_stmt_cond (get_guard_cond (guard, thread_guard), if_stmt);
8051 then_clause = begin_compound_stmt (BCS_NO_SCOPE);
8053 if (thread_guard)
8055 tree vfntype = NULL_TREE;
8056 tree acquire_name, release_name, abort_name;
8057 tree acquire_fn, release_fn, abort_fn;
8058 guard_addr = build_address (guard);
8060 acquire_name = get_identifier ("__cxa_guard_acquire");
8061 release_name = get_identifier ("__cxa_guard_release");
8062 abort_name = get_identifier ("__cxa_guard_abort");
8063 acquire_fn = get_global_binding (acquire_name);
8064 release_fn = get_global_binding (release_name);
8065 abort_fn = get_global_binding (abort_name);
8066 if (!acquire_fn)
8067 acquire_fn = push_library_fn
8068 (acquire_name, build_function_type_list (integer_type_node,
8069 TREE_TYPE (guard_addr),
8070 NULL_TREE),
8071 NULL_TREE, ECF_NOTHROW | ECF_LEAF);
8072 if (!release_fn || !abort_fn)
8073 vfntype = build_function_type_list (void_type_node,
8074 TREE_TYPE (guard_addr),
8075 NULL_TREE);
8076 if (!release_fn)
8077 release_fn = push_library_fn (release_name, vfntype, NULL_TREE,
8078 ECF_NOTHROW | ECF_LEAF);
8079 if (!abort_fn)
8080 abort_fn = push_library_fn (abort_name, vfntype, NULL_TREE,
8081 ECF_NOTHROW | ECF_LEAF);
8083 inner_if_stmt = begin_if_stmt ();
8084 finish_if_stmt_cond (build_call_n (acquire_fn, 1, guard_addr),
8085 inner_if_stmt);
8087 inner_then_clause = begin_compound_stmt (BCS_NO_SCOPE);
8088 begin = get_target_expr (boolean_false_node);
8089 flag = TARGET_EXPR_SLOT (begin);
8091 TARGET_EXPR_CLEANUP (begin)
8092 = build3 (COND_EXPR, void_type_node, flag,
8093 void_node,
8094 build_call_n (abort_fn, 1, guard_addr));
8095 CLEANUP_EH_ONLY (begin) = 1;
8097 /* Do the initialization itself. */
8098 init = add_stmt_to_compound (begin, init);
8099 init = add_stmt_to_compound
8100 (init, build2 (MODIFY_EXPR, void_type_node, flag, boolean_true_node));
8101 init = add_stmt_to_compound
8102 (init, build_call_n (release_fn, 1, guard_addr));
8104 else
8105 init = add_stmt_to_compound (init, set_guard (guard));
8107 /* Use atexit to register a function for destroying this static
8108 variable. */
8109 init = add_stmt_to_compound (init, register_dtor_fn (decl));
8111 finish_expr_stmt (init);
8113 if (thread_guard)
8115 finish_compound_stmt (inner_then_clause);
8116 finish_then_clause (inner_if_stmt);
8117 finish_if_stmt (inner_if_stmt);
8120 finish_compound_stmt (then_clause);
8121 finish_then_clause (if_stmt);
8122 finish_if_stmt (if_stmt);
8124 else if (CP_DECL_THREAD_LOCAL_P (decl))
8125 tls_aggregates = tree_cons (init, decl, tls_aggregates);
8126 else
8127 static_aggregates = tree_cons (init, decl, static_aggregates);
8131 /* Make TYPE a complete type based on INITIAL_VALUE.
8132 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
8133 2 if there was no information (in which case assume 0 if DO_DEFAULT),
8134 3 if the initializer list is empty (in pedantic mode). */
8137 cp_complete_array_type (tree *ptype, tree initial_value, bool do_default)
8139 int failure;
8140 tree type, elt_type;
8142 /* Don't get confused by a CONSTRUCTOR for some other type. */
8143 if (initial_value && TREE_CODE (initial_value) == CONSTRUCTOR
8144 && !BRACE_ENCLOSED_INITIALIZER_P (initial_value)
8145 && TREE_CODE (TREE_TYPE (initial_value)) != ARRAY_TYPE)
8146 return 1;
8148 if (initial_value)
8150 unsigned HOST_WIDE_INT i;
8151 tree value;
8153 /* An array of character type can be initialized from a
8154 brace-enclosed string constant.
8156 FIXME: this code is duplicated from reshape_init. Probably
8157 we should just call reshape_init here? */
8158 if (char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (*ptype)))
8159 && TREE_CODE (initial_value) == CONSTRUCTOR
8160 && !vec_safe_is_empty (CONSTRUCTOR_ELTS (initial_value)))
8162 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value);
8163 tree value = (*v)[0].value;
8165 if (TREE_CODE (value) == STRING_CST
8166 && v->length () == 1)
8167 initial_value = value;
8170 /* If any of the elements are parameter packs, we can't actually
8171 complete this type now because the array size is dependent. */
8172 if (TREE_CODE (initial_value) == CONSTRUCTOR)
8174 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (initial_value),
8175 i, value)
8177 if (PACK_EXPANSION_P (value))
8178 return 0;
8183 failure = complete_array_type (ptype, initial_value, do_default);
8185 /* We can create the array before the element type is complete, which
8186 means that we didn't have these two bits set in the original type
8187 either. In completing the type, we are expected to propagate these
8188 bits. See also complete_type which does the same thing for arrays
8189 of fixed size. */
8190 type = *ptype;
8191 if (TYPE_DOMAIN (type))
8193 elt_type = TREE_TYPE (type);
8194 TYPE_NEEDS_CONSTRUCTING (type) = TYPE_NEEDS_CONSTRUCTING (elt_type);
8195 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
8196 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (elt_type);
8199 return failure;
8202 /* As above, but either give an error or reject zero-size arrays, depending
8203 on COMPLAIN. */
8206 cp_complete_array_type_or_error (tree *ptype, tree initial_value,
8207 bool do_default, tsubst_flags_t complain)
8209 int failure;
8210 bool sfinae = !(complain & tf_error);
8211 /* In SFINAE context we can't be lenient about zero-size arrays. */
8212 if (sfinae)
8213 ++pedantic;
8214 failure = cp_complete_array_type (ptype, initial_value, do_default);
8215 if (sfinae)
8216 --pedantic;
8217 if (failure)
8219 if (sfinae)
8220 /* Not an error. */;
8221 else if (failure == 1)
8222 error ("initializer fails to determine size of %qT", *ptype);
8223 else if (failure == 2)
8225 if (do_default)
8226 error ("array size missing in %qT", *ptype);
8228 else if (failure == 3)
8229 error ("zero-size array %qT", *ptype);
8230 *ptype = error_mark_node;
8232 return failure;
8235 /* Return zero if something is declared to be a member of type
8236 CTYPE when in the context of CUR_TYPE. STRING is the error
8237 message to print in that case. Otherwise, quietly return 1. */
8239 static int
8240 member_function_or_else (tree ctype, tree cur_type, enum overload_flags flags)
8242 if (ctype && ctype != cur_type)
8244 if (flags == DTOR_FLAG)
8245 error ("destructor for alien class %qT cannot be a member", ctype);
8246 else
8247 error ("constructor for alien class %qT cannot be a member", ctype);
8248 return 0;
8250 return 1;
8253 /* Subroutine of `grokdeclarator'. */
8255 /* Generate errors possibly applicable for a given set of specifiers.
8256 This is for ARM $7.1.2. */
8258 static void
8259 bad_specifiers (tree object,
8260 enum bad_spec_place type,
8261 int virtualp,
8262 int quals,
8263 int inlinep,
8264 int friendp,
8265 int raises)
8267 switch (type)
8269 case BSP_VAR:
8270 if (virtualp)
8271 error ("%qD declared as a %<virtual%> variable", object);
8272 if (quals)
8273 error ("%<const%> and %<volatile%> function specifiers on "
8274 "%qD invalid in variable declaration", object);
8275 break;
8276 case BSP_PARM:
8277 if (virtualp)
8278 error ("%qD declared as a %<virtual%> parameter", object);
8279 if (inlinep)
8280 error ("%qD declared as an %<inline%> parameter", object);
8281 if (quals)
8282 error ("%<const%> and %<volatile%> function specifiers on "
8283 "%qD invalid in parameter declaration", object);
8284 break;
8285 case BSP_TYPE:
8286 if (virtualp)
8287 error ("%qD declared as a %<virtual%> type", object);
8288 if (inlinep)
8289 error ("%qD declared as an %<inline%> type", object);
8290 if (quals)
8291 error ("%<const%> and %<volatile%> function specifiers on "
8292 "%qD invalid in type declaration", object);
8293 break;
8294 case BSP_FIELD:
8295 if (virtualp)
8296 error ("%qD declared as a %<virtual%> field", object);
8297 if (inlinep)
8298 error ("%qD declared as an %<inline%> field", object);
8299 if (quals)
8300 error ("%<const%> and %<volatile%> function specifiers on "
8301 "%qD invalid in field declaration", object);
8302 break;
8303 default:
8304 gcc_unreachable();
8306 if (friendp)
8307 error ("%q+D declared as a friend", object);
8308 if (raises
8309 && !flag_noexcept_type
8310 && (TREE_CODE (object) == TYPE_DECL
8311 || (!TYPE_PTRFN_P (TREE_TYPE (object))
8312 && !TYPE_REFFN_P (TREE_TYPE (object))
8313 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (object)))))
8314 error ("%q+D declared with an exception specification", object);
8317 /* DECL is a member function or static data member and is presently
8318 being defined. Check that the definition is taking place in a
8319 valid namespace. */
8321 static void
8322 check_class_member_definition_namespace (tree decl)
8324 /* These checks only apply to member functions and static data
8325 members. */
8326 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
8327 /* We check for problems with specializations in pt.c in
8328 check_specialization_namespace, where we can issue better
8329 diagnostics. */
8330 if (processing_specialization)
8331 return;
8332 /* We check this in check_explicit_instantiation_namespace. */
8333 if (processing_explicit_instantiation)
8334 return;
8335 /* [class.mfct]
8337 A member function definition that appears outside of the
8338 class definition shall appear in a namespace scope enclosing
8339 the class definition.
8341 [class.static.data]
8343 The definition for a static data member shall appear in a
8344 namespace scope enclosing the member's class definition. */
8345 if (!is_ancestor (current_namespace, DECL_CONTEXT (decl)))
8346 permerror (input_location, "definition of %qD is not in namespace enclosing %qT",
8347 decl, DECL_CONTEXT (decl));
8350 /* Build a PARM_DECL for the "this" parameter of FN. TYPE is the
8351 METHOD_TYPE for a non-static member function; QUALS are the
8352 cv-qualifiers that apply to the function. */
8354 tree
8355 build_this_parm (tree fn, tree type, cp_cv_quals quals)
8357 tree this_type;
8358 tree qual_type;
8359 tree parm;
8360 cp_cv_quals this_quals;
8362 if (CLASS_TYPE_P (type))
8364 this_type
8365 = cp_build_qualified_type (type, quals & ~TYPE_QUAL_RESTRICT);
8366 this_type = build_pointer_type (this_type);
8368 else
8369 this_type = type_of_this_parm (type);
8370 /* The `this' parameter is implicitly `const'; it cannot be
8371 assigned to. */
8372 this_quals = (quals & TYPE_QUAL_RESTRICT) | TYPE_QUAL_CONST;
8373 qual_type = cp_build_qualified_type (this_type, this_quals);
8374 parm = build_artificial_parm (fn, this_identifier, qual_type);
8375 cp_apply_type_quals_to_decl (this_quals, parm);
8376 return parm;
8379 /* DECL is a static member function. Complain if it was declared
8380 with function-cv-quals. */
8382 static void
8383 check_static_quals (tree decl, cp_cv_quals quals)
8385 if (quals != TYPE_UNQUALIFIED)
8386 error ("static member function %q#D declared with type qualifiers",
8387 decl);
8390 // Check that FN takes no arguments and returns bool.
8391 static void
8392 check_concept_fn (tree fn)
8394 // A constraint is nullary.
8395 if (DECL_ARGUMENTS (fn))
8396 error ("concept %q#D declared with function parameters", fn);
8398 // The declared return type of the concept shall be bool, and
8399 // it shall not be deduced from it definition.
8400 tree type = TREE_TYPE (TREE_TYPE (fn));
8401 if (is_auto (type))
8402 error ("concept %q#D declared with a deduced return type", fn);
8403 else if (type != boolean_type_node)
8404 error ("concept %q#D with non-%<bool%> return type %qT", fn, type);
8407 /* Helper function. Replace the temporary this parameter injected
8408 during cp_finish_omp_declare_simd with the real this parameter. */
8410 static tree
8411 declare_simd_adjust_this (tree *tp, int *walk_subtrees, void *data)
8413 tree this_parm = (tree) data;
8414 if (TREE_CODE (*tp) == PARM_DECL
8415 && DECL_NAME (*tp) == this_identifier
8416 && *tp != this_parm)
8417 *tp = this_parm;
8418 else if (TYPE_P (*tp))
8419 *walk_subtrees = 0;
8420 return NULL_TREE;
8423 /* CTYPE is class type, or null if non-class.
8424 TYPE is type this FUNCTION_DECL should have, either FUNCTION_TYPE
8425 or METHOD_TYPE.
8426 DECLARATOR is the function's name.
8427 PARMS is a chain of PARM_DECLs for the function.
8428 VIRTUALP is truthvalue of whether the function is virtual or not.
8429 FLAGS are to be passed through to `grokclassfn'.
8430 QUALS are qualifiers indicating whether the function is `const'
8431 or `volatile'.
8432 RAISES is a list of exceptions that this function can raise.
8433 CHECK is 1 if we must find this method in CTYPE, 0 if we should
8434 not look, and -1 if we should not call `grokclassfn' at all.
8436 SFK is the kind of special function (if any) for the new function.
8438 Returns `NULL_TREE' if something goes wrong, after issuing
8439 applicable error messages. */
8441 static tree
8442 grokfndecl (tree ctype,
8443 tree type,
8444 tree declarator,
8445 tree parms,
8446 tree orig_declarator,
8447 tree decl_reqs,
8448 int virtualp,
8449 enum overload_flags flags,
8450 cp_cv_quals quals,
8451 cp_ref_qualifier rqual,
8452 tree raises,
8453 int check,
8454 int friendp,
8455 int publicp,
8456 int inlinep,
8457 bool deletedp,
8458 special_function_kind sfk,
8459 bool funcdef_flag,
8460 int template_count,
8461 tree in_namespace,
8462 tree* attrlist,
8463 location_t location)
8465 tree decl;
8466 int staticp = ctype && TREE_CODE (type) == FUNCTION_TYPE;
8467 tree t;
8469 // Was the concept specifier present?
8470 bool concept_p = inlinep & 4;
8472 // Concept declarations must have a corresponding definition.
8473 if (concept_p && !funcdef_flag)
8475 error ("concept %qD has no definition", declarator);
8476 return NULL_TREE;
8479 if (rqual)
8480 type = build_ref_qualified_type (type, rqual);
8481 if (raises)
8482 type = build_exception_variant (type, raises);
8484 decl = build_lang_decl (FUNCTION_DECL, declarator, type);
8486 /* Set the constraints on the declaration. */
8487 if (flag_concepts)
8489 tree tmpl_reqs = NULL_TREE;
8490 if (processing_template_decl > template_class_depth (ctype))
8491 tmpl_reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
8493 /* Adjust the required expression into a constraint. */
8494 if (decl_reqs)
8495 decl_reqs = normalize_expression (decl_reqs);
8497 tree ci = build_constraints (tmpl_reqs, decl_reqs);
8498 set_constraints (decl, ci);
8501 /* If we have an explicit location, use it, otherwise use whatever
8502 build_lang_decl used (probably input_location). */
8503 if (location != UNKNOWN_LOCATION)
8504 DECL_SOURCE_LOCATION (decl) = location;
8506 if (TREE_CODE (type) == METHOD_TYPE)
8508 tree parm = build_this_parm (decl, type, quals);
8509 DECL_CHAIN (parm) = parms;
8510 parms = parm;
8512 /* Allocate space to hold the vptr bit if needed. */
8513 SET_DECL_ALIGN (decl, MINIMUM_METHOD_BOUNDARY);
8516 DECL_ARGUMENTS (decl) = parms;
8517 for (t = parms; t; t = DECL_CHAIN (t))
8518 DECL_CONTEXT (t) = decl;
8520 /* Propagate volatile out from type to decl. */
8521 if (TYPE_VOLATILE (type))
8522 TREE_THIS_VOLATILE (decl) = 1;
8524 /* Setup decl according to sfk. */
8525 switch (sfk)
8527 case sfk_constructor:
8528 case sfk_copy_constructor:
8529 case sfk_move_constructor:
8530 DECL_CXX_CONSTRUCTOR_P (decl) = 1;
8531 DECL_NAME (decl) = ctor_identifier;
8532 break;
8533 case sfk_destructor:
8534 DECL_CXX_DESTRUCTOR_P (decl) = 1;
8535 DECL_NAME (decl) = dtor_identifier;
8536 break;
8537 default:
8538 break;
8541 if (friendp && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)
8543 if (funcdef_flag)
8544 error ("defining explicit specialization %qD in friend declaration",
8545 orig_declarator);
8546 else
8548 tree fns = TREE_OPERAND (orig_declarator, 0);
8549 tree args = TREE_OPERAND (orig_declarator, 1);
8551 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8553 /* Something like `template <class T> friend void f<T>()'. */
8554 error ("invalid use of template-id %qD in declaration "
8555 "of primary template",
8556 orig_declarator);
8557 return NULL_TREE;
8561 /* A friend declaration of the form friend void f<>(). Record
8562 the information in the TEMPLATE_ID_EXPR. */
8563 SET_DECL_IMPLICIT_INSTANTIATION (decl);
8565 gcc_assert (identifier_p (fns) || TREE_CODE (fns) == OVERLOAD);
8566 DECL_TEMPLATE_INFO (decl) = build_template_info (fns, args);
8568 for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t))
8569 if (TREE_PURPOSE (t)
8570 && TREE_CODE (TREE_PURPOSE (t)) == DEFAULT_ARG)
8572 error ("default arguments are not allowed in declaration "
8573 "of friend template specialization %qD",
8574 decl);
8575 return NULL_TREE;
8578 if (inlinep & 1)
8580 error ("%<inline%> is not allowed in declaration of friend "
8581 "template specialization %qD",
8582 decl);
8583 return NULL_TREE;
8588 /* If this decl has namespace scope, set that up. */
8589 if (in_namespace)
8590 set_decl_namespace (decl, in_namespace, friendp);
8591 else if (!ctype)
8592 DECL_CONTEXT (decl) = FROB_CONTEXT (current_decl_namespace ());
8594 /* `main' and builtins have implicit 'C' linkage. */
8595 if (ctype == NULL_TREE
8596 && DECL_FILE_SCOPE_P (decl)
8597 && current_lang_name == lang_name_cplusplus
8598 && (MAIN_NAME_P (declarator)
8599 || (IDENTIFIER_LENGTH (declarator) > 10
8600 && IDENTIFIER_POINTER (declarator)[0] == '_'
8601 && IDENTIFIER_POINTER (declarator)[1] == '_'
8602 && strncmp (IDENTIFIER_POINTER (declarator)+2,
8603 "builtin_", 8) == 0)
8604 || (targetcm.cxx_implicit_extern_c
8605 && (targetcm.cxx_implicit_extern_c
8606 (IDENTIFIER_POINTER (declarator))))))
8607 SET_DECL_LANGUAGE (decl, lang_c);
8609 /* Should probably propagate const out from type to decl I bet (mrs). */
8610 if (staticp)
8612 DECL_STATIC_FUNCTION_P (decl) = 1;
8613 DECL_CONTEXT (decl) = ctype;
8616 if (deletedp)
8617 DECL_DELETED_FN (decl) = 1;
8619 if (ctype)
8621 DECL_CONTEXT (decl) = ctype;
8622 if (funcdef_flag)
8623 check_class_member_definition_namespace (decl);
8626 if (ctype == NULL_TREE && DECL_MAIN_P (decl))
8628 if (PROCESSING_REAL_TEMPLATE_DECL_P())
8629 error ("cannot declare %<::main%> to be a template");
8630 if (inlinep & 1)
8631 error ("cannot declare %<::main%> to be inline");
8632 if (inlinep & 2)
8633 error ("cannot declare %<::main%> to be constexpr");
8634 if (!publicp)
8635 error ("cannot declare %<::main%> to be static");
8636 inlinep = 0;
8637 publicp = 1;
8640 /* Members of anonymous types and local classes have no linkage; make
8641 them internal. If a typedef is made later, this will be changed. */
8642 if (ctype && (!TREE_PUBLIC (TYPE_MAIN_DECL (ctype))
8643 || decl_function_context (TYPE_MAIN_DECL (ctype))))
8644 publicp = 0;
8646 if (publicp && cxx_dialect == cxx98)
8648 /* [basic.link]: A name with no linkage (notably, the name of a class
8649 or enumeration declared in a local scope) shall not be used to
8650 declare an entity with linkage.
8652 DR 757 relaxes this restriction for C++0x. */
8653 no_linkage_error (decl);
8656 TREE_PUBLIC (decl) = publicp;
8657 if (! publicp)
8659 DECL_INTERFACE_KNOWN (decl) = 1;
8660 DECL_NOT_REALLY_EXTERN (decl) = 1;
8663 /* If the declaration was declared inline, mark it as such. */
8664 if (inlinep)
8666 DECL_DECLARED_INLINE_P (decl) = 1;
8667 if (publicp)
8668 DECL_COMDAT (decl) = 1;
8670 if (inlinep & 2)
8671 DECL_DECLARED_CONSTEXPR_P (decl) = true;
8673 // If the concept declaration specifier was found, check
8674 // that the declaration satisfies the necessary requirements.
8675 if (concept_p)
8677 DECL_DECLARED_CONCEPT_P (decl) = true;
8678 check_concept_fn (decl);
8681 DECL_EXTERNAL (decl) = 1;
8682 if (TREE_CODE (type) == FUNCTION_TYPE)
8684 if (quals || rqual)
8685 TREE_TYPE (decl) = apply_memfn_quals (TREE_TYPE (decl),
8686 TYPE_UNQUALIFIED,
8687 REF_QUAL_NONE);
8689 if (quals)
8691 error (ctype
8692 ? G_("static member function %qD cannot have cv-qualifier")
8693 : G_("non-member function %qD cannot have cv-qualifier"),
8694 decl);
8695 quals = TYPE_UNQUALIFIED;
8698 if (rqual)
8700 error (ctype
8701 ? G_("static member function %qD cannot have ref-qualifier")
8702 : G_("non-member function %qD cannot have ref-qualifier"),
8703 decl);
8704 rqual = REF_QUAL_NONE;
8708 if (deduction_guide_p (decl))
8710 if (!DECL_NAMESPACE_SCOPE_P (decl))
8712 error_at (location, "deduction guide %qD must be declared at "
8713 "namespace scope", decl);
8714 return NULL_TREE;
8716 if (funcdef_flag)
8717 error_at (location,
8718 "deduction guide %qD must not have a function body", decl);
8720 else if (IDENTIFIER_ANY_OP_P (DECL_NAME (decl))
8721 && !grok_op_properties (decl, /*complain=*/true))
8722 return NULL_TREE;
8723 else if (UDLIT_OPER_P (DECL_NAME (decl)))
8725 bool long_long_unsigned_p;
8726 bool long_double_p;
8727 const char *suffix = NULL;
8728 /* [over.literal]/6: Literal operators shall not have C linkage. */
8729 if (DECL_LANGUAGE (decl) == lang_c)
8731 error ("literal operator with C linkage");
8732 maybe_show_extern_c_location ();
8733 return NULL_TREE;
8736 if (DECL_NAMESPACE_SCOPE_P (decl))
8738 if (!check_literal_operator_args (decl, &long_long_unsigned_p,
8739 &long_double_p))
8741 error ("%qD has invalid argument list", decl);
8742 return NULL_TREE;
8745 suffix = UDLIT_OP_SUFFIX (DECL_NAME (decl));
8746 if (long_long_unsigned_p)
8748 if (cpp_interpret_int_suffix (parse_in, suffix, strlen (suffix)))
8749 warning (0, "integer suffix %qs"
8750 " shadowed by implementation", suffix);
8752 else if (long_double_p)
8754 if (cpp_interpret_float_suffix (parse_in, suffix, strlen (suffix)))
8755 warning (0, "floating point suffix %qs"
8756 " shadowed by implementation", suffix);
8759 else
8761 error ("%qD must be a non-member function", decl);
8762 return NULL_TREE;
8766 if (funcdef_flag)
8767 /* Make the init_value nonzero so pushdecl knows this is not
8768 tentative. error_mark_node is replaced later with the BLOCK. */
8769 DECL_INITIAL (decl) = error_mark_node;
8771 if (TYPE_NOTHROW_P (type) || nothrow_libfn_p (decl))
8772 TREE_NOTHROW (decl) = 1;
8774 if (flag_openmp || flag_openmp_simd || flag_cilkplus)
8776 /* Adjust "omp declare simd" attributes. */
8777 tree ods = lookup_attribute ("omp declare simd", *attrlist);
8778 if (ods)
8780 tree attr;
8781 for (attr = ods; attr;
8782 attr = lookup_attribute ("omp declare simd", TREE_CHAIN (attr)))
8784 if (TREE_CODE (type) == METHOD_TYPE)
8785 walk_tree (&TREE_VALUE (attr), declare_simd_adjust_this,
8786 DECL_ARGUMENTS (decl), NULL);
8787 if (TREE_VALUE (attr) != NULL_TREE)
8789 tree cl = TREE_VALUE (TREE_VALUE (attr));
8790 cl = c_omp_declare_simd_clauses_to_numbers
8791 (DECL_ARGUMENTS (decl), cl);
8792 if (cl)
8793 TREE_VALUE (TREE_VALUE (attr)) = cl;
8794 else
8795 TREE_VALUE (attr) = NULL_TREE;
8801 /* Caller will do the rest of this. */
8802 if (check < 0)
8803 return decl;
8805 if (ctype != NULL_TREE)
8806 grokclassfn (ctype, decl, flags);
8808 /* 12.4/3 */
8809 if (cxx_dialect >= cxx11
8810 && DECL_DESTRUCTOR_P (decl)
8811 && !TYPE_BEING_DEFINED (DECL_CONTEXT (decl))
8812 && !processing_template_decl)
8813 deduce_noexcept_on_destructor (decl);
8815 decl = check_explicit_specialization (orig_declarator, decl,
8816 template_count,
8817 2 * funcdef_flag +
8818 4 * (friendp != 0) +
8819 8 * concept_p);
8820 if (decl == error_mark_node)
8821 return NULL_TREE;
8823 if (DECL_STATIC_FUNCTION_P (decl))
8824 check_static_quals (decl, quals);
8826 if (attrlist)
8828 cplus_decl_attributes (&decl, *attrlist, 0);
8829 *attrlist = NULL_TREE;
8832 /* Check main's type after attributes have been applied. */
8833 if (ctype == NULL_TREE && DECL_MAIN_P (decl))
8835 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
8836 integer_type_node))
8838 tree oldtypeargs = TYPE_ARG_TYPES (TREE_TYPE (decl));
8839 tree newtype;
8840 error ("%<::main%> must return %<int%>");
8841 newtype = build_function_type (integer_type_node, oldtypeargs);
8842 TREE_TYPE (decl) = newtype;
8844 if (warn_main)
8845 check_main_parameter_types (decl);
8848 if (ctype != NULL_TREE && check)
8850 tree old_decl = check_classfn (ctype, decl,
8851 (processing_template_decl
8852 > template_class_depth (ctype))
8853 ? current_template_parms
8854 : NULL_TREE);
8856 if (old_decl == error_mark_node)
8857 return NULL_TREE;
8859 if (old_decl)
8861 tree ok;
8862 tree pushed_scope;
8864 if (TREE_CODE (old_decl) == TEMPLATE_DECL)
8865 /* Because grokfndecl is always supposed to return a
8866 FUNCTION_DECL, we pull out the DECL_TEMPLATE_RESULT
8867 here. We depend on our callers to figure out that its
8868 really a template that's being returned. */
8869 old_decl = DECL_TEMPLATE_RESULT (old_decl);
8871 if (DECL_STATIC_FUNCTION_P (old_decl)
8872 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
8874 /* Remove the `this' parm added by grokclassfn. */
8875 revert_static_member_fn (decl);
8876 check_static_quals (decl, quals);
8878 if (DECL_ARTIFICIAL (old_decl))
8880 error ("definition of implicitly-declared %qD", old_decl);
8881 return NULL_TREE;
8883 else if (DECL_DEFAULTED_FN (old_decl))
8885 error ("definition of explicitly-defaulted %q+D", decl);
8886 inform (DECL_SOURCE_LOCATION (old_decl),
8887 "%q#D explicitly defaulted here", old_decl);
8888 return NULL_TREE;
8891 /* Since we've smashed OLD_DECL to its
8892 DECL_TEMPLATE_RESULT, we must do the same to DECL. */
8893 if (TREE_CODE (decl) == TEMPLATE_DECL)
8894 decl = DECL_TEMPLATE_RESULT (decl);
8896 /* Attempt to merge the declarations. This can fail, in
8897 the case of some invalid specialization declarations. */
8898 pushed_scope = push_scope (ctype);
8899 ok = duplicate_decls (decl, old_decl, friendp);
8900 if (pushed_scope)
8901 pop_scope (pushed_scope);
8902 if (!ok)
8904 error ("no %q#D member function declared in class %qT",
8905 decl, ctype);
8906 return NULL_TREE;
8908 if (ok == error_mark_node)
8909 return NULL_TREE;
8910 return old_decl;
8914 if (DECL_CONSTRUCTOR_P (decl) && !grok_ctor_properties (ctype, decl))
8915 return NULL_TREE;
8917 if (ctype == NULL_TREE || check)
8918 return decl;
8920 if (virtualp)
8921 DECL_VIRTUAL_P (decl) = 1;
8923 return decl;
8926 /* decl is a FUNCTION_DECL.
8927 specifiers are the parsed virt-specifiers.
8929 Set flags to reflect the virt-specifiers.
8931 Returns decl. */
8933 static tree
8934 set_virt_specifiers (tree decl, cp_virt_specifiers specifiers)
8936 if (decl == NULL_TREE)
8937 return decl;
8938 if (specifiers & VIRT_SPEC_OVERRIDE)
8939 DECL_OVERRIDE_P (decl) = 1;
8940 if (specifiers & VIRT_SPEC_FINAL)
8941 DECL_FINAL_P (decl) = 1;
8942 return decl;
8945 /* DECL is a VAR_DECL for a static data member. Set flags to reflect
8946 the linkage that DECL will receive in the object file. */
8948 static void
8949 set_linkage_for_static_data_member (tree decl)
8951 /* A static data member always has static storage duration and
8952 external linkage. Note that static data members are forbidden in
8953 local classes -- the only situation in which a class has
8954 non-external linkage. */
8955 TREE_PUBLIC (decl) = 1;
8956 TREE_STATIC (decl) = 1;
8957 /* For non-template classes, static data members are always put
8958 out in exactly those files where they are defined, just as
8959 with ordinary namespace-scope variables. */
8960 if (!processing_template_decl)
8961 DECL_INTERFACE_KNOWN (decl) = 1;
8964 /* Create a VAR_DECL named NAME with the indicated TYPE.
8966 If SCOPE is non-NULL, it is the class type or namespace containing
8967 the variable. If SCOPE is NULL, the variable should is created in
8968 the innermost enclosing scope. */
8970 static tree
8971 grokvardecl (tree type,
8972 tree name,
8973 tree orig_declarator,
8974 const cp_decl_specifier_seq *declspecs,
8975 int initialized,
8976 int type_quals,
8977 int inlinep,
8978 bool conceptp,
8979 int template_count,
8980 tree scope)
8982 tree decl;
8983 tree explicit_scope;
8985 gcc_assert (!name || identifier_p (name));
8987 bool constp = (type_quals & TYPE_QUAL_CONST) != 0;
8988 bool volatilep = (type_quals & TYPE_QUAL_VOLATILE) != 0;
8990 /* Compute the scope in which to place the variable, but remember
8991 whether or not that scope was explicitly specified by the user. */
8992 explicit_scope = scope;
8993 if (!scope)
8995 /* An explicit "extern" specifier indicates a namespace-scope
8996 variable. */
8997 if (declspecs->storage_class == sc_extern)
8998 scope = current_decl_namespace ();
8999 else if (!at_function_scope_p ())
9000 scope = current_scope ();
9003 if (scope
9004 && (/* If the variable is a namespace-scope variable declared in a
9005 template, we need DECL_LANG_SPECIFIC. */
9006 (TREE_CODE (scope) == NAMESPACE_DECL && processing_template_decl)
9007 /* Similarly for namespace-scope variables with language linkage
9008 other than C++. */
9009 || (TREE_CODE (scope) == NAMESPACE_DECL
9010 && current_lang_name != lang_name_cplusplus)
9011 /* Similarly for static data members. */
9012 || TYPE_P (scope)
9013 /* Similarly for explicit specializations. */
9014 || (orig_declarator
9015 && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)))
9016 decl = build_lang_decl (VAR_DECL, name, type);
9017 else
9018 decl = build_decl (input_location, VAR_DECL, name, type);
9020 if (explicit_scope && TREE_CODE (explicit_scope) == NAMESPACE_DECL)
9021 set_decl_namespace (decl, explicit_scope, 0);
9022 else
9023 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
9025 if (declspecs->storage_class == sc_extern)
9027 DECL_THIS_EXTERN (decl) = 1;
9028 DECL_EXTERNAL (decl) = !initialized;
9031 if (DECL_CLASS_SCOPE_P (decl))
9033 set_linkage_for_static_data_member (decl);
9034 /* This function is only called with out-of-class definitions. */
9035 DECL_EXTERNAL (decl) = 0;
9036 check_class_member_definition_namespace (decl);
9038 /* At top level, either `static' or no s.c. makes a definition
9039 (perhaps tentative), and absence of `static' makes it public. */
9040 else if (toplevel_bindings_p ())
9042 TREE_PUBLIC (decl) = (declspecs->storage_class != sc_static
9043 && (DECL_THIS_EXTERN (decl)
9044 || ! constp
9045 || volatilep
9046 || inlinep));
9047 TREE_STATIC (decl) = ! DECL_EXTERNAL (decl);
9049 /* Not at top level, only `static' makes a static definition. */
9050 else
9052 TREE_STATIC (decl) = declspecs->storage_class == sc_static;
9053 TREE_PUBLIC (decl) = DECL_EXTERNAL (decl);
9056 if (decl_spec_seq_has_spec_p (declspecs, ds_thread))
9058 if (DECL_EXTERNAL (decl) || TREE_STATIC (decl))
9060 CP_DECL_THREAD_LOCAL_P (decl) = true;
9061 if (!processing_template_decl)
9062 set_decl_tls_model (decl, decl_default_tls_model (decl));
9064 if (declspecs->gnu_thread_keyword_p)
9065 SET_DECL_GNU_TLS_P (decl);
9068 /* If the type of the decl has no linkage, make sure that we'll
9069 notice that in mark_used. */
9070 if (cxx_dialect > cxx98
9071 && decl_linkage (decl) != lk_none
9072 && DECL_LANG_SPECIFIC (decl) == NULL
9073 && !DECL_EXTERN_C_P (decl)
9074 && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
9075 retrofit_lang_decl (decl);
9077 if (TREE_PUBLIC (decl))
9079 /* [basic.link]: A name with no linkage (notably, the name of a class
9080 or enumeration declared in a local scope) shall not be used to
9081 declare an entity with linkage.
9083 DR 757 relaxes this restriction for C++0x. */
9084 if (cxx_dialect < cxx11)
9085 no_linkage_error (decl);
9087 else
9088 DECL_INTERFACE_KNOWN (decl) = 1;
9090 if (DECL_NAME (decl)
9091 && MAIN_NAME_P (DECL_NAME (decl))
9092 && scope == global_namespace)
9093 error ("cannot declare %<::main%> to be a global variable");
9095 /* Check that the variable can be safely declared as a concept.
9096 Note that this also forbids explicit specializations. */
9097 if (conceptp)
9099 if (!processing_template_decl)
9101 error ("a non-template variable cannot be %<concept%>");
9102 return NULL_TREE;
9104 else
9105 DECL_DECLARED_CONCEPT_P (decl) = true;
9106 if (!same_type_ignoring_top_level_qualifiers_p (type, boolean_type_node))
9107 error_at (declspecs->locations[ds_type_spec],
9108 "concept must have type %<bool%>");
9110 else if (flag_concepts
9111 && processing_template_decl > template_class_depth (scope))
9113 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
9114 tree ci = build_constraints (reqs, NULL_TREE);
9115 set_constraints (decl, ci);
9118 // Handle explicit specializations and instantiations of variable templates.
9119 if (orig_declarator)
9120 decl = check_explicit_specialization (orig_declarator, decl,
9121 template_count, conceptp * 8);
9123 return decl != error_mark_node ? decl : NULL_TREE;
9126 /* Create and return a canonical pointer to member function type, for
9127 TYPE, which is a POINTER_TYPE to a METHOD_TYPE. */
9129 tree
9130 build_ptrmemfunc_type (tree type)
9132 tree field, fields;
9133 tree t;
9135 if (type == error_mark_node)
9136 return type;
9138 /* Make sure that we always have the unqualified pointer-to-member
9139 type first. */
9140 if (cp_cv_quals quals = cp_type_quals (type))
9142 tree unqual = build_ptrmemfunc_type (TYPE_MAIN_VARIANT (type));
9143 return cp_build_qualified_type (unqual, quals);
9146 /* If a canonical type already exists for this type, use it. We use
9147 this method instead of type_hash_canon, because it only does a
9148 simple equality check on the list of field members. */
9150 t = TYPE_PTRMEMFUNC_TYPE (type);
9151 if (t)
9152 return t;
9154 t = make_node (RECORD_TYPE);
9156 /* Let the front end know this is a pointer to member function. */
9157 TYPE_PTRMEMFUNC_FLAG (t) = 1;
9159 field = build_decl (input_location, FIELD_DECL, pfn_identifier, type);
9160 fields = field;
9162 field = build_decl (input_location, FIELD_DECL, delta_identifier,
9163 delta_type_node);
9164 DECL_CHAIN (field) = fields;
9165 fields = field;
9167 finish_builtin_struct (t, "__ptrmemfunc_type", fields, ptr_type_node);
9169 /* Zap out the name so that the back end will give us the debugging
9170 information for this anonymous RECORD_TYPE. */
9171 TYPE_NAME (t) = NULL_TREE;
9173 /* Cache this pointer-to-member type so that we can find it again
9174 later. */
9175 TYPE_PTRMEMFUNC_TYPE (type) = t;
9177 if (TYPE_STRUCTURAL_EQUALITY_P (type))
9178 SET_TYPE_STRUCTURAL_EQUALITY (t);
9179 else if (TYPE_CANONICAL (type) != type)
9180 TYPE_CANONICAL (t) = build_ptrmemfunc_type (TYPE_CANONICAL (type));
9182 return t;
9185 /* Create and return a pointer to data member type. */
9187 tree
9188 build_ptrmem_type (tree class_type, tree member_type)
9190 if (TREE_CODE (member_type) == METHOD_TYPE)
9192 cp_cv_quals quals = type_memfn_quals (member_type);
9193 cp_ref_qualifier rqual = type_memfn_rqual (member_type);
9194 member_type = build_memfn_type (member_type, class_type, quals, rqual);
9195 return build_ptrmemfunc_type (build_pointer_type (member_type));
9197 else
9199 gcc_assert (TREE_CODE (member_type) != FUNCTION_TYPE);
9200 return build_offset_type (class_type, member_type);
9204 /* DECL is a VAR_DECL defined in-class, whose TYPE is also given.
9205 Check to see that the definition is valid. Issue appropriate error
9206 messages. Return 1 if the definition is particularly bad, or 0
9207 otherwise. */
9209 static int
9210 check_static_variable_definition (tree decl, tree type)
9212 /* Avoid redundant diagnostics on out-of-class definitions. */
9213 if (!current_class_type || !TYPE_BEING_DEFINED (current_class_type))
9214 return 0;
9215 /* Can't check yet if we don't know the type. */
9216 if (dependent_type_p (type))
9217 return 0;
9218 /* If DECL is declared constexpr, we'll do the appropriate checks
9219 in check_initializer. Similarly for inline static data members. */
9220 if (DECL_P (decl)
9221 && (DECL_DECLARED_CONSTEXPR_P (decl)
9222 || DECL_VAR_DECLARED_INLINE_P (decl)))
9223 return 0;
9224 else if (cxx_dialect >= cxx11 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
9226 if (!COMPLETE_TYPE_P (type))
9227 error_at (DECL_SOURCE_LOCATION (decl),
9228 "in-class initialization of static data member %q#D of "
9229 "incomplete type", decl);
9230 else if (literal_type_p (type))
9231 permerror (DECL_SOURCE_LOCATION (decl),
9232 "%<constexpr%> needed for in-class initialization of "
9233 "static data member %q#D of non-integral type", decl);
9234 else
9235 error_at (DECL_SOURCE_LOCATION (decl),
9236 "in-class initialization of static data member %q#D of "
9237 "non-literal type", decl);
9238 return 1;
9241 /* Motion 10 at San Diego: If a static const integral data member is
9242 initialized with an integral constant expression, the initializer
9243 may appear either in the declaration (within the class), or in
9244 the definition, but not both. If it appears in the class, the
9245 member is a member constant. The file-scope definition is always
9246 required. */
9247 if (!ARITHMETIC_TYPE_P (type) && TREE_CODE (type) != ENUMERAL_TYPE)
9249 error_at (DECL_SOURCE_LOCATION (decl),
9250 "invalid in-class initialization of static data member "
9251 "of non-integral type %qT",
9252 type);
9253 return 1;
9255 else if (!CP_TYPE_CONST_P (type))
9256 error_at (DECL_SOURCE_LOCATION (decl),
9257 "ISO C++ forbids in-class initialization of non-const "
9258 "static member %qD",
9259 decl);
9260 else if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type))
9261 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
9262 "ISO C++ forbids initialization of member constant "
9263 "%qD of non-integral type %qT", decl, type);
9265 return 0;
9268 /* *expr_p is part of the TYPE_SIZE of a variably-sized array. If any
9269 SAVE_EXPRs in *expr_p wrap expressions with side-effects, break those
9270 expressions out into temporary variables so that walk_tree doesn't
9271 step into them (c++/15764). */
9273 static tree
9274 stabilize_save_expr_r (tree *expr_p, int *walk_subtrees, void *data)
9276 hash_set<tree> *pset = (hash_set<tree> *)data;
9277 tree expr = *expr_p;
9278 if (TREE_CODE (expr) == SAVE_EXPR)
9280 tree op = TREE_OPERAND (expr, 0);
9281 cp_walk_tree (&op, stabilize_save_expr_r, data, pset);
9282 if (TREE_SIDE_EFFECTS (op))
9283 TREE_OPERAND (expr, 0) = get_temp_regvar (TREE_TYPE (op), op);
9284 *walk_subtrees = 0;
9286 else if (!EXPR_P (expr) || !TREE_SIDE_EFFECTS (expr))
9287 *walk_subtrees = 0;
9288 return NULL;
9291 /* Entry point for the above. */
9293 static void
9294 stabilize_vla_size (tree size)
9296 hash_set<tree> pset;
9297 /* Break out any function calls into temporary variables. */
9298 cp_walk_tree (&size, stabilize_save_expr_r, &pset, &pset);
9301 /* Reduce a SIZEOF_EXPR to its value. */
9303 tree
9304 fold_sizeof_expr (tree t)
9306 tree r;
9307 if (SIZEOF_EXPR_TYPE_P (t))
9308 r = cxx_sizeof_or_alignof_type (TREE_TYPE (TREE_OPERAND (t, 0)),
9309 SIZEOF_EXPR, false);
9310 else if (TYPE_P (TREE_OPERAND (t, 0)))
9311 r = cxx_sizeof_or_alignof_type (TREE_OPERAND (t, 0), SIZEOF_EXPR,
9312 false);
9313 else
9314 r = cxx_sizeof_or_alignof_expr (TREE_OPERAND (t, 0), SIZEOF_EXPR,
9315 false);
9316 if (r == error_mark_node)
9317 r = size_one_node;
9318 return r;
9321 /* Given the SIZE (i.e., number of elements) in an array, compute
9322 an appropriate index type for the array. If non-NULL, NAME is
9323 the name of the entity being declared. */
9325 tree
9326 compute_array_index_type (tree name, tree size, tsubst_flags_t complain)
9328 tree itype;
9329 tree osize = size;
9331 if (error_operand_p (size))
9332 return error_mark_node;
9334 if (!type_dependent_expression_p (size))
9336 tree type = TREE_TYPE (size);
9338 size = mark_rvalue_use (size);
9340 if (cxx_dialect < cxx11 && TREE_CODE (size) == NOP_EXPR
9341 && TREE_SIDE_EFFECTS (size))
9342 /* In C++98, we mark a non-constant array bound with a magic
9343 NOP_EXPR with TREE_SIDE_EFFECTS; don't fold in that case. */;
9344 else
9346 size = instantiate_non_dependent_expr_sfinae (size, complain);
9348 if (CLASS_TYPE_P (type)
9349 && CLASSTYPE_LITERAL_P (type))
9351 size = build_expr_type_conversion (WANT_INT, size, true);
9352 if (!size)
9354 if (!(complain & tf_error))
9355 return error_mark_node;
9356 if (name)
9357 error ("size of array %qD has non-integral type %qT",
9358 name, type);
9359 else
9360 error ("size of array has non-integral type %qT", type);
9361 size = integer_one_node;
9363 if (size == error_mark_node)
9364 return error_mark_node;
9365 type = TREE_TYPE (size);
9368 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
9369 size = maybe_constant_value (size);
9371 if (!TREE_CONSTANT (size))
9372 size = osize;
9375 if (error_operand_p (size))
9376 return error_mark_node;
9378 /* The array bound must be an integer type. */
9379 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
9381 if (!(complain & tf_error))
9382 return error_mark_node;
9383 if (name)
9384 error ("size of array %qD has non-integral type %qT", name, type);
9385 else
9386 error ("size of array has non-integral type %qT", type);
9387 size = integer_one_node;
9388 type = TREE_TYPE (size);
9392 /* A type is dependent if it is...an array type constructed from any
9393 dependent type or whose size is specified by a constant expression
9394 that is value-dependent. */
9395 /* We can only call value_dependent_expression_p on integral constant
9396 expressions; treat non-constant expressions as dependent, too. */
9397 if (processing_template_decl
9398 && (type_dependent_expression_p (size)
9399 || !TREE_CONSTANT (size) || value_dependent_expression_p (size)))
9401 /* We cannot do any checking for a SIZE that isn't known to be
9402 constant. Just build the index type and mark that it requires
9403 structural equality checks. */
9404 itype = build_index_type (build_min (MINUS_EXPR, sizetype,
9405 size, size_one_node));
9406 TYPE_DEPENDENT_P (itype) = 1;
9407 TYPE_DEPENDENT_P_VALID (itype) = 1;
9408 SET_TYPE_STRUCTURAL_EQUALITY (itype);
9409 return itype;
9412 if (TREE_CODE (size) != INTEGER_CST)
9414 tree folded = cp_fully_fold (size);
9415 if (TREE_CODE (folded) == INTEGER_CST)
9416 pedwarn (location_of (size), OPT_Wpedantic,
9417 "size of array is not an integral constant-expression");
9418 /* Use the folded result for VLAs, too; it will have resolved
9419 SIZEOF_EXPR. */
9420 size = folded;
9423 /* Normally, the array-bound will be a constant. */
9424 if (TREE_CODE (size) == INTEGER_CST)
9426 /* Check to see if the array bound overflowed. Make that an
9427 error, no matter how generous we're being. */
9428 constant_expression_error (size);
9430 /* An array must have a positive number of elements. */
9431 if (tree_int_cst_lt (size, integer_zero_node))
9433 if (!(complain & tf_error))
9434 return error_mark_node;
9435 if (name)
9436 error ("size of array %qD is negative", name);
9437 else
9438 error ("size of array is negative");
9439 size = integer_one_node;
9441 /* As an extension we allow zero-sized arrays. */
9442 else if (integer_zerop (size))
9444 if (!(complain & tf_error))
9445 /* We must fail if performing argument deduction (as
9446 indicated by the state of complain), so that
9447 another substitution can be found. */
9448 return error_mark_node;
9449 else if (in_system_header_at (input_location))
9450 /* Allow them in system headers because glibc uses them. */;
9451 else if (name)
9452 pedwarn (input_location, OPT_Wpedantic, "ISO C++ forbids zero-size array %qD", name);
9453 else
9454 pedwarn (input_location, OPT_Wpedantic, "ISO C++ forbids zero-size array");
9457 else if (TREE_CONSTANT (size)
9458 /* We don't allow VLAs at non-function scopes, or during
9459 tentative template substitution. */
9460 || !at_function_scope_p ()
9461 || !(complain & tf_error))
9463 if (!(complain & tf_error))
9464 return error_mark_node;
9465 /* `(int) &fn' is not a valid array bound. */
9466 if (name)
9467 error ("size of array %qD is not an integral constant-expression",
9468 name);
9469 else
9470 error ("size of array is not an integral constant-expression");
9471 size = integer_one_node;
9473 else if (pedantic && warn_vla != 0)
9475 if (name)
9476 pedwarn (input_location, OPT_Wvla, "ISO C++ forbids variable length array %qD", name);
9477 else
9478 pedwarn (input_location, OPT_Wvla, "ISO C++ forbids variable length array");
9480 else if (warn_vla > 0)
9482 if (name)
9483 warning (OPT_Wvla,
9484 "variable length array %qD is used", name);
9485 else
9486 warning (OPT_Wvla,
9487 "variable length array is used");
9490 if (processing_template_decl && !TREE_CONSTANT (size))
9491 /* A variable sized array. */
9492 itype = build_min (MINUS_EXPR, sizetype, size, integer_one_node);
9493 else
9495 HOST_WIDE_INT saved_processing_template_decl;
9497 /* Compute the index of the largest element in the array. It is
9498 one less than the number of elements in the array. We save
9499 and restore PROCESSING_TEMPLATE_DECL so that computations in
9500 cp_build_binary_op will be appropriately folded. */
9501 saved_processing_template_decl = processing_template_decl;
9502 processing_template_decl = 0;
9503 itype = cp_build_binary_op (input_location,
9504 MINUS_EXPR,
9505 cp_convert (ssizetype, size, complain),
9506 cp_convert (ssizetype, integer_one_node,
9507 complain),
9508 complain);
9509 itype = maybe_constant_value (itype);
9510 processing_template_decl = saved_processing_template_decl;
9512 if (!TREE_CONSTANT (itype))
9514 /* A variable sized array. */
9515 itype = variable_size (itype);
9517 stabilize_vla_size (itype);
9519 if (sanitize_flags_p (SANITIZE_VLA)
9520 && current_function_decl != NULL_TREE)
9522 /* We have to add 1 -- in the ubsan routine we generate
9523 LE_EXPR rather than LT_EXPR. */
9524 tree t = fold_build2 (PLUS_EXPR, TREE_TYPE (itype), itype,
9525 build_one_cst (TREE_TYPE (itype)));
9526 t = ubsan_instrument_vla (input_location, t);
9527 finish_expr_stmt (t);
9530 /* Make sure that there was no overflow when creating to a signed
9531 index type. (For example, on a 32-bit machine, an array with
9532 size 2^32 - 1 is too big.) */
9533 else if (TREE_CODE (itype) == INTEGER_CST
9534 && TREE_OVERFLOW (itype))
9536 if (!(complain & tf_error))
9537 return error_mark_node;
9538 error ("overflow in array dimension");
9539 TREE_OVERFLOW (itype) = 0;
9543 /* Create and return the appropriate index type. */
9544 itype = build_index_type (itype);
9546 /* If the index type were dependent, we would have returned early, so
9547 remember that it isn't. */
9548 TYPE_DEPENDENT_P (itype) = 0;
9549 TYPE_DEPENDENT_P_VALID (itype) = 1;
9550 return itype;
9553 /* Returns the scope (if any) in which the entity declared by
9554 DECLARATOR will be located. If the entity was declared with an
9555 unqualified name, NULL_TREE is returned. */
9557 tree
9558 get_scope_of_declarator (const cp_declarator *declarator)
9560 while (declarator && declarator->kind != cdk_id)
9561 declarator = declarator->declarator;
9563 /* If the declarator-id is a SCOPE_REF, the scope in which the
9564 declaration occurs is the first operand. */
9565 if (declarator
9566 && declarator->u.id.qualifying_scope)
9567 return declarator->u.id.qualifying_scope;
9569 /* Otherwise, the declarator is not a qualified name; the entity will
9570 be declared in the current scope. */
9571 return NULL_TREE;
9574 /* Returns an ARRAY_TYPE for an array with SIZE elements of the
9575 indicated TYPE. If non-NULL, NAME is the NAME of the declaration
9576 with this type. */
9578 static tree
9579 create_array_type_for_decl (tree name, tree type, tree size)
9581 tree itype = NULL_TREE;
9583 /* If things have already gone awry, bail now. */
9584 if (type == error_mark_node || size == error_mark_node)
9585 return error_mark_node;
9587 /* 8.3.4/1: If the type of the identifier of D contains the auto
9588 type-specifier, the program is ill-formed. */
9589 if (type_uses_auto (type))
9591 error ("%qD declared as array of %qT", name, type);
9592 return error_mark_node;
9595 /* If there are some types which cannot be array elements,
9596 issue an error-message and return. */
9597 switch (TREE_CODE (type))
9599 case VOID_TYPE:
9600 if (name)
9601 error ("declaration of %qD as array of void", name);
9602 else
9603 error ("creating array of void");
9604 return error_mark_node;
9606 case FUNCTION_TYPE:
9607 if (name)
9608 error ("declaration of %qD as array of functions", name);
9609 else
9610 error ("creating array of functions");
9611 return error_mark_node;
9613 case REFERENCE_TYPE:
9614 if (name)
9615 error ("declaration of %qD as array of references", name);
9616 else
9617 error ("creating array of references");
9618 return error_mark_node;
9620 case METHOD_TYPE:
9621 if (name)
9622 error ("declaration of %qD as array of function members", name);
9623 else
9624 error ("creating array of function members");
9625 return error_mark_node;
9627 default:
9628 break;
9631 /* [dcl.array]
9633 The constant expressions that specify the bounds of the arrays
9634 can be omitted only for the first member of the sequence. */
9635 if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type))
9637 if (name)
9638 error ("declaration of %qD as multidimensional array must "
9639 "have bounds for all dimensions except the first",
9640 name);
9641 else
9642 error ("multidimensional array must have bounds for all "
9643 "dimensions except the first");
9645 return error_mark_node;
9648 /* Figure out the index type for the array. */
9649 if (size)
9650 itype = compute_array_index_type (name, size, tf_warning_or_error);
9652 /* [dcl.array]
9653 T is called the array element type; this type shall not be [...] an
9654 abstract class type. */
9655 abstract_virtuals_error (name, type);
9657 return build_cplus_array_type (type, itype);
9660 /* Returns the smallest location != UNKNOWN_LOCATION among the
9661 three stored in LOCATIONS[ds_const], LOCATIONS[ds_volatile],
9662 and LOCATIONS[ds_restrict]. */
9664 static location_t
9665 smallest_type_quals_location (int type_quals, const location_t* locations)
9667 location_t loc = UNKNOWN_LOCATION;
9669 if (type_quals & TYPE_QUAL_CONST)
9670 loc = locations[ds_const];
9672 if ((type_quals & TYPE_QUAL_VOLATILE)
9673 && (loc == UNKNOWN_LOCATION || locations[ds_volatile] < loc))
9674 loc = locations[ds_volatile];
9676 if ((type_quals & TYPE_QUAL_RESTRICT)
9677 && (loc == UNKNOWN_LOCATION || locations[ds_restrict] < loc))
9678 loc = locations[ds_restrict];
9680 return loc;
9683 /* Check that it's OK to declare a function with the indicated TYPE
9684 and TYPE_QUALS. SFK indicates the kind of special function (if any)
9685 that this function is. OPTYPE is the type given in a conversion
9686 operator declaration, or the class type for a constructor/destructor.
9687 Returns the actual return type of the function; that may be different
9688 than TYPE if an error occurs, or for certain special functions. */
9690 static tree
9691 check_special_function_return_type (special_function_kind sfk,
9692 tree type,
9693 tree optype,
9694 int type_quals,
9695 const location_t* locations)
9697 switch (sfk)
9699 case sfk_constructor:
9700 if (type)
9701 error ("return type specification for constructor invalid");
9702 else if (type_quals != TYPE_UNQUALIFIED)
9703 error_at (smallest_type_quals_location (type_quals, locations),
9704 "qualifiers are not allowed on constructor declaration");
9706 if (targetm.cxx.cdtor_returns_this ())
9707 type = build_pointer_type (optype);
9708 else
9709 type = void_type_node;
9710 break;
9712 case sfk_destructor:
9713 if (type)
9714 error ("return type specification for destructor invalid");
9715 else if (type_quals != TYPE_UNQUALIFIED)
9716 error_at (smallest_type_quals_location (type_quals, locations),
9717 "qualifiers are not allowed on destructor declaration");
9719 /* We can't use the proper return type here because we run into
9720 problems with ambiguous bases and covariant returns. */
9721 if (targetm.cxx.cdtor_returns_this ())
9722 type = build_pointer_type (void_type_node);
9723 else
9724 type = void_type_node;
9725 break;
9727 case sfk_conversion:
9728 if (type)
9729 error ("return type specified for %<operator %T%>", optype);
9730 else if (type_quals != TYPE_UNQUALIFIED)
9731 error_at (smallest_type_quals_location (type_quals, locations),
9732 "qualifiers are not allowed on declaration of "
9733 "%<operator %T%>", optype);
9735 type = optype;
9736 break;
9738 case sfk_deduction_guide:
9739 if (type)
9740 error ("return type specified for deduction guide");
9741 else if (type_quals != TYPE_UNQUALIFIED)
9742 error_at (smallest_type_quals_location (type_quals, locations),
9743 "qualifiers are not allowed on declaration of "
9744 "deduction guide");
9745 type = make_template_placeholder (CLASSTYPE_TI_TEMPLATE (optype));
9746 for (int i = 0; i < ds_last; ++i)
9747 if (i != ds_explicit && locations[i])
9748 error_at (locations[i],
9749 "decl-specifier in declaration of deduction guide");
9750 break;
9752 default:
9753 gcc_unreachable ();
9756 return type;
9759 /* A variable or data member (whose unqualified name is IDENTIFIER)
9760 has been declared with the indicated TYPE. If the TYPE is not
9761 acceptable, issue an error message and return a type to use for
9762 error-recovery purposes. */
9764 tree
9765 check_var_type (tree identifier, tree type)
9767 if (VOID_TYPE_P (type))
9769 if (!identifier)
9770 error ("unnamed variable or field declared void");
9771 else if (identifier_p (identifier))
9773 gcc_assert (!IDENTIFIER_ANY_OP_P (identifier));
9774 error ("variable or field %qE declared void", identifier);
9776 else
9777 error ("variable or field declared void");
9778 type = error_mark_node;
9781 return type;
9784 /* Handle declaring DECL as an inline variable. */
9786 static void
9787 mark_inline_variable (tree decl)
9789 bool inlinep = true;
9790 if (! toplevel_bindings_p ())
9792 error ("%<inline%> specifier invalid for variable "
9793 "%qD declared at block scope", decl);
9794 inlinep = false;
9796 else if (cxx_dialect < cxx17)
9797 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
9798 "inline variables are only available "
9799 "with -std=c++17 or -std=gnu++17");
9800 if (inlinep)
9802 retrofit_lang_decl (decl);
9803 SET_DECL_VAR_DECLARED_INLINE_P (decl);
9808 /* Assign a typedef-given name to a class or enumeration type declared
9809 as anonymous at first. This was split out of grokdeclarator
9810 because it is also used in libcc1. */
9812 void
9813 name_unnamed_type (tree type, tree decl)
9815 gcc_assert (TYPE_UNNAMED_P (type));
9817 /* Replace the anonymous name with the real name everywhere. */
9818 for (tree t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
9820 if (anon_aggrname_p (TYPE_IDENTIFIER (t)))
9821 /* We do not rename the debug info representing the
9822 unnamed tagged type because the standard says in
9823 [dcl.typedef] that the naming applies only for
9824 linkage purposes. */
9825 /*debug_hooks->set_name (t, decl);*/
9826 TYPE_NAME (t) = decl;
9829 if (TYPE_LANG_SPECIFIC (type))
9830 TYPE_WAS_UNNAMED (type) = 1;
9832 /* If this is a typedef within a template class, the nested
9833 type is a (non-primary) template. The name for the
9834 template needs updating as well. */
9835 if (TYPE_LANG_SPECIFIC (type) && CLASSTYPE_TEMPLATE_INFO (type))
9836 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type))
9837 = TYPE_IDENTIFIER (type);
9839 /* Adjust linkage now that we aren't unnamed anymore. */
9840 reset_type_linkage (type);
9842 /* FIXME remangle member functions; member functions of a
9843 type with external linkage have external linkage. */
9845 /* Check that our job is done, and that it would fail if we
9846 attempted to do it again. */
9847 gcc_assert (!TYPE_UNNAMED_P (type));
9850 /* Given declspecs and a declarator (abstract or otherwise), determine
9851 the name and type of the object declared and construct a DECL node
9852 for it.
9854 DECLSPECS points to the representation of declaration-specifier
9855 sequence that precedes declarator.
9857 DECL_CONTEXT says which syntactic context this declaration is in:
9858 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
9859 FUNCDEF for a function definition. Like NORMAL but a few different
9860 error messages in each case. Return value may be zero meaning
9861 this definition is too screwy to try to parse.
9862 MEMFUNCDEF for a function definition. Like FUNCDEF but prepares to
9863 handle member functions (which have FIELD context).
9864 Return value may be zero meaning this definition is too screwy to
9865 try to parse.
9866 PARM for a parameter declaration (either within a function prototype
9867 or before a function body). Make a PARM_DECL, or return void_type_node.
9868 TPARM for a template parameter declaration.
9869 CATCHPARM for a parameter declaration before a catch clause.
9870 TYPENAME if for a typename (in a cast or sizeof).
9871 Don't make a DECL node; just return the ..._TYPE node.
9872 FIELD for a struct or union field; make a FIELD_DECL.
9873 BITFIELD for a field with specified width.
9875 INITIALIZED is as for start_decl.
9877 ATTRLIST is a pointer to the list of attributes, which may be NULL
9878 if there are none; *ATTRLIST may be modified if attributes from inside
9879 the declarator should be applied to the declaration.
9881 When this function is called, scoping variables (such as
9882 CURRENT_CLASS_TYPE) should reflect the scope in which the
9883 declaration occurs, not the scope in which the new declaration will
9884 be placed. For example, on:
9886 void S::f() { ... }
9888 when grokdeclarator is called for `S::f', the CURRENT_CLASS_TYPE
9889 should not be `S'.
9891 Returns a DECL (if a declarator is present), a TYPE (if there is no
9892 declarator, in cases like "struct S;"), or the ERROR_MARK_NODE if an
9893 error occurs. */
9895 tree
9896 grokdeclarator (const cp_declarator *declarator,
9897 cp_decl_specifier_seq *declspecs,
9898 enum decl_context decl_context,
9899 int initialized,
9900 tree* attrlist)
9902 tree type = NULL_TREE;
9903 int longlong = 0;
9904 int explicit_intN = 0;
9905 int virtualp, explicitp, friendp, inlinep, staticp;
9906 int explicit_int = 0;
9907 int explicit_char = 0;
9908 int defaulted_int = 0;
9910 tree typedef_decl = NULL_TREE;
9911 const char *name = NULL;
9912 tree typedef_type = NULL_TREE;
9913 /* True if this declarator is a function definition. */
9914 bool funcdef_flag = false;
9915 cp_declarator_kind innermost_code = cdk_error;
9916 int bitfield = 0;
9917 #if 0
9918 /* See the code below that used this. */
9919 tree decl_attr = NULL_TREE;
9920 #endif
9922 /* Keep track of what sort of function is being processed
9923 so that we can warn about default return values, or explicit
9924 return values which do not match prescribed defaults. */
9925 special_function_kind sfk = sfk_none;
9927 tree dname = NULL_TREE;
9928 tree ctor_return_type = NULL_TREE;
9929 enum overload_flags flags = NO_SPECIAL;
9930 /* cv-qualifiers that apply to the declarator, for a declaration of
9931 a member function. */
9932 cp_cv_quals memfn_quals = TYPE_UNQUALIFIED;
9933 /* virt-specifiers that apply to the declarator, for a declaration of
9934 a member function. */
9935 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
9936 /* ref-qualifier that applies to the declarator, for a declaration of
9937 a member function. */
9938 cp_ref_qualifier rqual = REF_QUAL_NONE;
9939 /* cv-qualifiers that apply to the type specified by the DECLSPECS. */
9940 int type_quals = TYPE_UNQUALIFIED;
9941 tree raises = NULL_TREE;
9942 int template_count = 0;
9943 tree returned_attrs = NULL_TREE;
9944 tree parms = NULL_TREE;
9945 const cp_declarator *id_declarator;
9946 /* The unqualified name of the declarator; either an
9947 IDENTIFIER_NODE, BIT_NOT_EXPR, or TEMPLATE_ID_EXPR. */
9948 tree unqualified_id;
9949 /* The class type, if any, in which this entity is located,
9950 or NULL_TREE if none. Note that this value may be different from
9951 the current class type; for example if an attempt is made to declare
9952 "A::f" inside "B", this value will be "A". */
9953 tree ctype = current_class_type;
9954 /* The NAMESPACE_DECL for the namespace in which this entity is
9955 located. If an unqualified name is used to declare the entity,
9956 this value will be NULL_TREE, even if the entity is located at
9957 namespace scope. */
9958 tree in_namespace = NULL_TREE;
9959 cp_storage_class storage_class;
9960 bool unsigned_p, signed_p, short_p, long_p, thread_p;
9961 bool type_was_error_mark_node = false;
9962 bool parameter_pack_p = declarator ? declarator->parameter_pack_p : false;
9963 bool template_type_arg = false;
9964 bool template_parm_flag = false;
9965 bool typedef_p = decl_spec_seq_has_spec_p (declspecs, ds_typedef);
9966 bool constexpr_p = decl_spec_seq_has_spec_p (declspecs, ds_constexpr);
9967 bool late_return_type_p = false;
9968 bool array_parameter_p = false;
9969 source_location saved_loc = input_location;
9970 tree reqs = NULL_TREE;
9972 signed_p = decl_spec_seq_has_spec_p (declspecs, ds_signed);
9973 unsigned_p = decl_spec_seq_has_spec_p (declspecs, ds_unsigned);
9974 short_p = decl_spec_seq_has_spec_p (declspecs, ds_short);
9975 long_p = decl_spec_seq_has_spec_p (declspecs, ds_long);
9976 longlong = decl_spec_seq_has_spec_p (declspecs, ds_long_long);
9977 explicit_intN = declspecs->explicit_intN_p;
9978 thread_p = decl_spec_seq_has_spec_p (declspecs, ds_thread);
9980 // Was concept_p specified? Note that ds_concept
9981 // implies ds_constexpr!
9982 bool concept_p = decl_spec_seq_has_spec_p (declspecs, ds_concept);
9983 if (concept_p)
9984 constexpr_p = true;
9986 if (decl_spec_seq_has_spec_p (declspecs, ds_const))
9987 type_quals |= TYPE_QUAL_CONST;
9988 if (decl_spec_seq_has_spec_p (declspecs, ds_volatile))
9989 type_quals |= TYPE_QUAL_VOLATILE;
9990 if (decl_spec_seq_has_spec_p (declspecs, ds_restrict))
9991 type_quals |= TYPE_QUAL_RESTRICT;
9993 if (decl_context == FUNCDEF)
9994 funcdef_flag = true, decl_context = NORMAL;
9995 else if (decl_context == MEMFUNCDEF)
9996 funcdef_flag = true, decl_context = FIELD;
9997 else if (decl_context == BITFIELD)
9998 bitfield = 1, decl_context = FIELD;
9999 else if (decl_context == TEMPLATE_TYPE_ARG)
10000 template_type_arg = true, decl_context = TYPENAME;
10001 else if (decl_context == TPARM)
10002 template_parm_flag = true, decl_context = PARM;
10004 if (initialized > 1)
10005 funcdef_flag = true;
10007 location_t typespec_loc = smallest_type_quals_location (type_quals,
10008 declspecs->locations);
10009 if (typespec_loc == UNKNOWN_LOCATION)
10010 typespec_loc = declspecs->locations[ds_type_spec];
10011 if (typespec_loc == UNKNOWN_LOCATION)
10012 typespec_loc = input_location;
10014 /* Look inside a declarator for the name being declared
10015 and get it as a string, for an error message. */
10016 for (id_declarator = declarator;
10017 id_declarator;
10018 id_declarator = id_declarator->declarator)
10020 if (id_declarator->kind != cdk_id)
10021 innermost_code = id_declarator->kind;
10023 switch (id_declarator->kind)
10025 case cdk_function:
10026 if (id_declarator->declarator
10027 && id_declarator->declarator->kind == cdk_id)
10029 sfk = id_declarator->declarator->u.id.sfk;
10030 if (sfk == sfk_destructor)
10031 flags = DTOR_FLAG;
10033 break;
10035 case cdk_id:
10037 tree qualifying_scope = id_declarator->u.id.qualifying_scope;
10038 tree decl = id_declarator->u.id.unqualified_name;
10039 if (!decl)
10040 break;
10041 if (qualifying_scope)
10043 if (at_function_scope_p ())
10045 /* [dcl.meaning]
10047 A declarator-id shall not be qualified except
10048 for ...
10050 None of the cases are permitted in block
10051 scope. */
10052 if (qualifying_scope == global_namespace)
10053 error ("invalid use of qualified-name %<::%D%>",
10054 decl);
10055 else if (TYPE_P (qualifying_scope))
10056 error ("invalid use of qualified-name %<%T::%D%>",
10057 qualifying_scope, decl);
10058 else
10059 error ("invalid use of qualified-name %<%D::%D%>",
10060 qualifying_scope, decl);
10061 return error_mark_node;
10063 else if (TYPE_P (qualifying_scope))
10065 ctype = qualifying_scope;
10066 if (!MAYBE_CLASS_TYPE_P (ctype))
10068 error ("%q#T is not a class or a namespace", ctype);
10069 ctype = NULL_TREE;
10071 else if (innermost_code != cdk_function
10072 && current_class_type
10073 && !uniquely_derived_from_p (ctype,
10074 current_class_type))
10076 error ("invalid use of qualified-name %<%T::%D%>",
10077 qualifying_scope, decl);
10078 return error_mark_node;
10081 else if (TREE_CODE (qualifying_scope) == NAMESPACE_DECL)
10082 in_namespace = qualifying_scope;
10084 switch (TREE_CODE (decl))
10086 case BIT_NOT_EXPR:
10088 if (innermost_code != cdk_function)
10090 error ("declaration of %qD as non-function", decl);
10091 return error_mark_node;
10093 else if (!qualifying_scope
10094 && !(current_class_type && at_class_scope_p ()))
10096 error ("declaration of %qD as non-member", decl);
10097 return error_mark_node;
10100 tree type = TREE_OPERAND (decl, 0);
10101 if (TYPE_P (type))
10102 type = constructor_name (type);
10103 name = identifier_to_locale (IDENTIFIER_POINTER (type));
10104 dname = decl;
10106 break;
10108 case TEMPLATE_ID_EXPR:
10110 tree fns = TREE_OPERAND (decl, 0);
10112 dname = fns;
10113 if (!identifier_p (dname))
10114 dname = OVL_NAME (dname);
10116 /* Fall through. */
10118 case IDENTIFIER_NODE:
10119 if (identifier_p (decl))
10120 dname = decl;
10122 if (IDENTIFIER_KEYWORD_P (dname))
10124 error ("declarator-id missing; using reserved word %qD",
10125 dname);
10126 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
10128 else if (!IDENTIFIER_CONV_OP_P (dname))
10129 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
10130 else
10132 gcc_assert (flags == NO_SPECIAL);
10133 flags = TYPENAME_FLAG;
10134 sfk = sfk_conversion;
10135 tree glob = get_global_binding (dname);
10136 if (glob && TREE_CODE (glob) == TYPE_DECL)
10137 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
10138 else
10139 name = "<invalid operator>";
10141 break;
10143 default:
10144 gcc_unreachable ();
10146 break;
10149 case cdk_array:
10150 case cdk_pointer:
10151 case cdk_reference:
10152 case cdk_ptrmem:
10153 break;
10155 case cdk_decomp:
10156 name = "structured binding";
10157 break;
10159 case cdk_error:
10160 return error_mark_node;
10162 default:
10163 gcc_unreachable ();
10165 if (id_declarator->kind == cdk_id)
10166 break;
10169 /* [dcl.fct.edf]
10171 The declarator in a function-definition shall have the form
10172 D1 ( parameter-declaration-clause) ... */
10173 if (funcdef_flag && innermost_code != cdk_function)
10175 error ("function definition does not declare parameters");
10176 return error_mark_node;
10179 if (flags == TYPENAME_FLAG
10180 && innermost_code != cdk_function
10181 && ! (ctype && !declspecs->any_specifiers_p))
10183 error ("declaration of %qD as non-function", dname);
10184 return error_mark_node;
10187 if (dname && identifier_p (dname))
10189 if (UDLIT_OPER_P (dname)
10190 && innermost_code != cdk_function)
10192 error ("declaration of %qD as non-function", dname);
10193 return error_mark_node;
10196 if (IDENTIFIER_ANY_OP_P (dname))
10198 if (typedef_p)
10200 error ("declaration of %qD as %<typedef%>", dname);
10201 return error_mark_node;
10203 else if (decl_context == PARM || decl_context == CATCHPARM)
10205 error ("declaration of %qD as parameter", dname);
10206 return error_mark_node;
10211 /* Anything declared one level down from the top level
10212 must be one of the parameters of a function
10213 (because the body is at least two levels down). */
10215 /* This heuristic cannot be applied to C++ nodes! Fixed, however,
10216 by not allowing C++ class definitions to specify their parameters
10217 with xdecls (must be spec.d in the parmlist).
10219 Since we now wait to push a class scope until we are sure that
10220 we are in a legitimate method context, we must set oldcname
10221 explicitly (since current_class_name is not yet alive).
10223 We also want to avoid calling this a PARM if it is in a namespace. */
10225 if (decl_context == NORMAL && !toplevel_bindings_p ())
10227 cp_binding_level *b = current_binding_level;
10228 current_binding_level = b->level_chain;
10229 if (current_binding_level != 0 && toplevel_bindings_p ())
10230 decl_context = PARM;
10231 current_binding_level = b;
10234 if (name == NULL)
10235 name = decl_context == PARM ? "parameter" : "type name";
10237 if (concept_p && typedef_p)
10239 error ("%<concept%> cannot appear in a typedef declaration");
10240 return error_mark_node;
10243 if (constexpr_p && typedef_p)
10245 error ("%<constexpr%> cannot appear in a typedef declaration");
10246 return error_mark_node;
10249 /* If there were multiple types specified in the decl-specifier-seq,
10250 issue an error message. */
10251 if (declspecs->multiple_types_p)
10253 error ("two or more data types in declaration of %qs", name);
10254 return error_mark_node;
10257 if (declspecs->conflicting_specifiers_p)
10259 error ("conflicting specifiers in declaration of %qs", name);
10260 return error_mark_node;
10263 /* Extract the basic type from the decl-specifier-seq. */
10264 type = declspecs->type;
10265 if (type == error_mark_node)
10267 type = NULL_TREE;
10268 type_was_error_mark_node = true;
10270 /* If the entire declaration is itself tagged as deprecated then
10271 suppress reports of deprecated items. */
10272 if (type && TREE_DEPRECATED (type)
10273 && deprecated_state != DEPRECATED_SUPPRESS)
10274 warn_deprecated_use (type, NULL_TREE);
10275 if (type && TREE_CODE (type) == TYPE_DECL)
10277 typedef_decl = type;
10278 type = TREE_TYPE (typedef_decl);
10279 if (TREE_DEPRECATED (type)
10280 && DECL_ARTIFICIAL (typedef_decl)
10281 && deprecated_state != DEPRECATED_SUPPRESS)
10282 warn_deprecated_use (type, NULL_TREE);
10284 /* No type at all: default to `int', and set DEFAULTED_INT
10285 because it was not a user-defined typedef. */
10286 if (type == NULL_TREE)
10288 if (signed_p || unsigned_p || long_p || short_p)
10290 /* These imply 'int'. */
10291 type = integer_type_node;
10292 defaulted_int = 1;
10294 /* If we just have "complex", it is equivalent to "complex double". */
10295 else if (!longlong && !explicit_intN
10296 && decl_spec_seq_has_spec_p (declspecs, ds_complex))
10298 type = double_type_node;
10299 pedwarn (declspecs->locations[ds_complex], OPT_Wpedantic,
10300 "ISO C++ does not support plain %<complex%> meaning "
10301 "%<double complex%>");
10304 /* Gather flags. */
10305 explicit_int = declspecs->explicit_int_p;
10306 explicit_char = declspecs->explicit_char_p;
10308 #if 0
10309 /* See the code below that used this. */
10310 if (typedef_decl)
10311 decl_attr = DECL_ATTRIBUTES (typedef_decl);
10312 #endif
10313 typedef_type = type;
10315 if (sfk == sfk_conversion || sfk == sfk_deduction_guide)
10316 ctor_return_type = TREE_TYPE (dname);
10317 else
10318 ctor_return_type = ctype;
10320 if (sfk != sfk_none)
10322 type = check_special_function_return_type (sfk, type,
10323 ctor_return_type,
10324 type_quals,
10325 declspecs->locations);
10326 type_quals = TYPE_UNQUALIFIED;
10328 else if (type == NULL_TREE)
10330 int is_main;
10332 explicit_int = -1;
10334 /* We handle `main' specially here, because 'main () { }' is so
10335 common. With no options, it is allowed. With -Wreturn-type,
10336 it is a warning. It is only an error with -pedantic-errors. */
10337 is_main = (funcdef_flag
10338 && dname && identifier_p (dname)
10339 && MAIN_NAME_P (dname)
10340 && ctype == NULL_TREE
10341 && in_namespace == NULL_TREE
10342 && current_namespace == global_namespace);
10344 if (type_was_error_mark_node)
10345 /* We've already issued an error, don't complain more. */;
10346 else if (in_system_header_at (input_location) || flag_ms_extensions)
10347 /* Allow it, sigh. */;
10348 else if (! is_main)
10349 permerror (input_location, "ISO C++ forbids declaration of %qs with no type", name);
10350 else if (pedantic)
10351 pedwarn (input_location, OPT_Wpedantic,
10352 "ISO C++ forbids declaration of %qs with no type", name);
10353 else
10354 warning (OPT_Wreturn_type,
10355 "ISO C++ forbids declaration of %qs with no type", name);
10357 if (type_was_error_mark_node && template_parm_flag)
10358 /* FIXME we should be able to propagate the error_mark_node as is
10359 for other contexts too. */
10360 type = error_mark_node;
10361 else
10362 type = integer_type_node;
10365 ctype = NULL_TREE;
10367 if (explicit_intN)
10369 if (! int_n_enabled_p[declspecs->int_n_idx])
10371 error ("%<__int%d%> is not supported by this target",
10372 int_n_data[declspecs->int_n_idx].bitsize);
10373 explicit_intN = false;
10375 else if (pedantic && ! in_system_header_at (input_location))
10376 pedwarn (input_location, OPT_Wpedantic,
10377 "ISO C++ does not support %<__int%d%> for %qs",
10378 int_n_data[declspecs->int_n_idx].bitsize, name);
10381 /* Now process the modifiers that were specified
10382 and check for invalid combinations. */
10384 /* Long double is a special combination. */
10385 if (long_p && !longlong && TYPE_MAIN_VARIANT (type) == double_type_node)
10387 long_p = false;
10388 type = cp_build_qualified_type (long_double_type_node,
10389 cp_type_quals (type));
10392 /* Check all other uses of type modifiers. */
10394 if (unsigned_p || signed_p || long_p || short_p)
10396 int ok = 0;
10398 if ((signed_p || unsigned_p) && TREE_CODE (type) != INTEGER_TYPE)
10399 error ("%<signed%> or %<unsigned%> invalid for %qs", name);
10400 else if (signed_p && unsigned_p)
10401 error ("%<signed%> and %<unsigned%> specified together for %qs", name);
10402 else if (longlong && TREE_CODE (type) != INTEGER_TYPE)
10403 error ("%<long long%> invalid for %qs", name);
10404 else if (long_p && TREE_CODE (type) == REAL_TYPE)
10405 error ("%<long%> invalid for %qs", name);
10406 else if (short_p && TREE_CODE (type) == REAL_TYPE)
10407 error ("%<short%> invalid for %qs", name);
10408 else if ((long_p || short_p) && TREE_CODE (type) != INTEGER_TYPE)
10409 error ("%<long%> or %<short%> invalid for %qs", name);
10410 else if ((long_p || short_p || explicit_char || explicit_int) && explicit_intN)
10411 error ("%<long%>, %<int%>, %<short%>, or %<char%> invalid for %qs", name);
10412 else if ((long_p || short_p) && explicit_char)
10413 error ("%<long%> or %<short%> specified with char for %qs", name);
10414 else if (long_p && short_p)
10415 error ("%<long%> and %<short%> specified together for %qs", name);
10416 else if (type == char16_type_node || type == char32_type_node)
10418 if (signed_p || unsigned_p)
10419 error ("%<signed%> or %<unsigned%> invalid for %qs", name);
10420 else if (short_p || long_p)
10421 error ("%<short%> or %<long%> invalid for %qs", name);
10423 else
10425 ok = 1;
10426 if (!explicit_int && !defaulted_int && !explicit_char && !explicit_intN && pedantic)
10428 pedwarn (input_location, OPT_Wpedantic,
10429 "long, short, signed or unsigned used invalidly for %qs",
10430 name);
10431 if (flag_pedantic_errors)
10432 ok = 0;
10436 /* Discard the type modifiers if they are invalid. */
10437 if (! ok)
10439 unsigned_p = false;
10440 signed_p = false;
10441 long_p = false;
10442 short_p = false;
10443 longlong = 0;
10447 /* Decide whether an integer type is signed or not.
10448 Optionally treat bitfields as signed by default. */
10449 if (unsigned_p
10450 /* [class.bit]
10452 It is implementation-defined whether a plain (neither
10453 explicitly signed or unsigned) char, short, int, or long
10454 bit-field is signed or unsigned.
10456 Naturally, we extend this to long long as well. Note that
10457 this does not include wchar_t. */
10458 || (bitfield && !flag_signed_bitfields
10459 && !signed_p
10460 /* A typedef for plain `int' without `signed' can be
10461 controlled just like plain `int', but a typedef for
10462 `signed int' cannot be so controlled. */
10463 && !(typedef_decl
10464 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl))
10465 && TREE_CODE (type) == INTEGER_TYPE
10466 && !same_type_p (TYPE_MAIN_VARIANT (type), wchar_type_node)))
10468 if (explicit_intN)
10469 type = int_n_trees[declspecs->int_n_idx].unsigned_type;
10470 else if (longlong)
10471 type = long_long_unsigned_type_node;
10472 else if (long_p)
10473 type = long_unsigned_type_node;
10474 else if (short_p)
10475 type = short_unsigned_type_node;
10476 else if (type == char_type_node)
10477 type = unsigned_char_type_node;
10478 else if (typedef_decl)
10479 type = unsigned_type_for (type);
10480 else
10481 type = unsigned_type_node;
10483 else if (signed_p && type == char_type_node)
10484 type = signed_char_type_node;
10485 else if (explicit_intN)
10486 type = int_n_trees[declspecs->int_n_idx].signed_type;
10487 else if (longlong)
10488 type = long_long_integer_type_node;
10489 else if (long_p)
10490 type = long_integer_type_node;
10491 else if (short_p)
10492 type = short_integer_type_node;
10494 if (decl_spec_seq_has_spec_p (declspecs, ds_complex))
10496 if (TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
10497 error ("complex invalid for %qs", name);
10498 /* If a modifier is specified, the resulting complex is the complex
10499 form of TYPE. E.g, "complex short" is "complex short int". */
10500 else if (type == integer_type_node)
10501 type = complex_integer_type_node;
10502 else if (type == float_type_node)
10503 type = complex_float_type_node;
10504 else if (type == double_type_node)
10505 type = complex_double_type_node;
10506 else if (type == long_double_type_node)
10507 type = complex_long_double_type_node;
10508 else
10509 type = build_complex_type (type);
10512 /* If we're using the injected-class-name to form a compound type or a
10513 declaration, replace it with the underlying class so we don't get
10514 redundant typedefs in the debug output. But if we are returning the
10515 type unchanged, leave it alone so that it's available to
10516 maybe_get_template_decl_from_type_decl. */
10517 if (CLASS_TYPE_P (type)
10518 && DECL_SELF_REFERENCE_P (TYPE_NAME (type))
10519 && type == TREE_TYPE (TYPE_NAME (type))
10520 && (declarator || type_quals))
10521 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
10523 type_quals |= cp_type_quals (type);
10524 type = cp_build_qualified_type_real
10525 (type, type_quals, ((((typedef_decl && !DECL_ARTIFICIAL (typedef_decl))
10526 || declspecs->decltype_p)
10527 ? tf_ignore_bad_quals : 0) | tf_warning_or_error));
10528 /* We might have ignored or rejected some of the qualifiers. */
10529 type_quals = cp_type_quals (type);
10531 if (cxx_dialect >= cxx17 && type && is_auto (type)
10532 && innermost_code != cdk_function
10533 && id_declarator && declarator != id_declarator)
10534 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (type))
10536 error_at (typespec_loc, "template placeholder type %qT must be followed "
10537 "by a simple declarator-id", type);
10538 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here", tmpl);
10541 staticp = 0;
10542 inlinep = decl_spec_seq_has_spec_p (declspecs, ds_inline);
10543 virtualp = decl_spec_seq_has_spec_p (declspecs, ds_virtual);
10544 explicitp = decl_spec_seq_has_spec_p (declspecs, ds_explicit);
10546 storage_class = declspecs->storage_class;
10547 if (storage_class == sc_static)
10548 staticp = 1 + (decl_context == FIELD);
10550 if (virtualp)
10552 if (staticp == 2)
10554 error ("member %qD cannot be declared both %<virtual%> "
10555 "and %<static%>", dname);
10556 storage_class = sc_none;
10557 staticp = 0;
10559 if (constexpr_p)
10560 error ("member %qD cannot be declared both %<virtual%> "
10561 "and %<constexpr%>", dname);
10563 friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
10565 /* Issue errors about use of storage classes for parameters. */
10566 if (decl_context == PARM)
10568 if (typedef_p)
10570 error ("typedef declaration invalid in parameter declaration");
10571 return error_mark_node;
10573 else if (template_parm_flag && storage_class != sc_none)
10575 error ("storage class specified for template parameter %qs", name);
10576 return error_mark_node;
10578 else if (storage_class == sc_static
10579 || storage_class == sc_extern
10580 || thread_p)
10581 error ("storage class specifiers invalid in parameter declarations");
10583 /* Function parameters cannot be concept. */
10584 if (concept_p)
10585 error ("a parameter cannot be declared %<concept%>");
10586 /* Function parameters cannot be constexpr. If we saw one, moan
10587 and pretend it wasn't there. */
10588 else if (constexpr_p)
10590 error ("a parameter cannot be declared %<constexpr%>");
10591 constexpr_p = 0;
10595 /* Give error if `virtual' is used outside of class declaration. */
10596 if (virtualp
10597 && (current_class_name == NULL_TREE || decl_context != FIELD))
10599 error_at (declspecs->locations[ds_virtual],
10600 "%<virtual%> outside class declaration");
10601 virtualp = 0;
10604 if (innermost_code == cdk_decomp)
10606 location_t loc = (declarator->kind == cdk_reference
10607 ? declarator->declarator->id_loc : declarator->id_loc);
10608 if (inlinep)
10609 error_at (declspecs->locations[ds_inline],
10610 "structured binding declaration cannot be %<inline%>");
10611 if (typedef_p)
10612 error_at (declspecs->locations[ds_typedef],
10613 "structured binding declaration cannot be %<typedef%>");
10614 if (constexpr_p)
10615 error_at (declspecs->locations[ds_constexpr], "structured "
10616 "binding declaration cannot be %<constexpr%>");
10617 if (thread_p)
10618 error_at (declspecs->locations[ds_thread],
10619 "structured binding declaration cannot be %qs",
10620 declspecs->gnu_thread_keyword_p
10621 ? "__thread" : "thread_local");
10622 if (concept_p)
10623 error_at (declspecs->locations[ds_concept],
10624 "structured binding declaration cannot be %<concept%>");
10625 switch (storage_class)
10627 case sc_none:
10628 break;
10629 case sc_register:
10630 error_at (loc, "structured binding declaration cannot be "
10631 "%<register%>");
10632 break;
10633 case sc_static:
10634 error_at (loc, "structured binding declaration cannot be "
10635 "%<static%>");
10636 break;
10637 case sc_extern:
10638 error_at (loc, "structured binding declaration cannot be "
10639 "%<extern%>");
10640 break;
10641 case sc_mutable:
10642 error_at (loc, "structured binding declaration cannot be "
10643 "%<mutable%>");
10644 break;
10645 case sc_auto:
10646 error_at (loc, "structured binding declaration cannot be "
10647 "C++98 %<auto%>");
10648 break;
10649 default:
10650 gcc_unreachable ();
10652 if (TREE_CODE (type) != TEMPLATE_TYPE_PARM
10653 || TYPE_IDENTIFIER (type) != auto_identifier)
10655 if (type != error_mark_node)
10657 error_at (loc, "structured binding declaration cannot have "
10658 "type %qT", type);
10659 inform (loc,
10660 "type must be cv-qualified %<auto%> or reference to "
10661 "cv-qualified %<auto%>");
10663 type = build_qualified_type (make_auto (), type_quals);
10664 declspecs->type = type;
10666 inlinep = 0;
10667 typedef_p = 0;
10668 constexpr_p = 0;
10669 thread_p = 0;
10670 concept_p = 0;
10671 storage_class = sc_none;
10672 staticp = 0;
10673 declspecs->storage_class = sc_none;
10674 declspecs->locations[ds_thread] = UNKNOWN_LOCATION;
10677 /* Static anonymous unions are dealt with here. */
10678 if (staticp && decl_context == TYPENAME
10679 && declspecs->type
10680 && ANON_AGGR_TYPE_P (declspecs->type))
10681 decl_context = FIELD;
10683 /* Warn about storage classes that are invalid for certain
10684 kinds of declarations (parameters, typenames, etc.). */
10685 if (thread_p
10686 && ((storage_class
10687 && storage_class != sc_extern
10688 && storage_class != sc_static)
10689 || typedef_p))
10691 error ("multiple storage classes in declaration of %qs", name);
10692 thread_p = false;
10694 if (decl_context != NORMAL
10695 && ((storage_class != sc_none
10696 && storage_class != sc_mutable)
10697 || thread_p))
10699 if ((decl_context == PARM || decl_context == CATCHPARM)
10700 && (storage_class == sc_register
10701 || storage_class == sc_auto))
10703 else if (typedef_p)
10705 else if (decl_context == FIELD
10706 /* C++ allows static class elements. */
10707 && storage_class == sc_static)
10708 /* C++ also allows inlines and signed and unsigned elements,
10709 but in those cases we don't come in here. */
10711 else
10713 if (decl_context == FIELD)
10714 error ("storage class specified for %qs", name);
10715 else
10717 if (decl_context == PARM || decl_context == CATCHPARM)
10718 error ("storage class specified for parameter %qs", name);
10719 else
10720 error ("storage class specified for typename");
10722 if (storage_class == sc_register
10723 || storage_class == sc_auto
10724 || storage_class == sc_extern
10725 || thread_p)
10726 storage_class = sc_none;
10729 else if (storage_class == sc_extern && funcdef_flag
10730 && ! toplevel_bindings_p ())
10731 error ("nested function %qs declared %<extern%>", name);
10732 else if (toplevel_bindings_p ())
10734 if (storage_class == sc_auto)
10735 error ("top-level declaration of %qs specifies %<auto%>", name);
10737 else if (thread_p
10738 && storage_class != sc_extern
10739 && storage_class != sc_static)
10741 if (declspecs->gnu_thread_keyword_p)
10742 pedwarn (input_location, 0, "function-scope %qs implicitly auto and "
10743 "declared %<__thread%>", name);
10745 /* When thread_local is applied to a variable of block scope the
10746 storage-class-specifier static is implied if it does not appear
10747 explicitly. */
10748 storage_class = declspecs->storage_class = sc_static;
10749 staticp = 1;
10752 if (storage_class && friendp)
10754 error ("storage class specifiers invalid in friend function declarations");
10755 storage_class = sc_none;
10756 staticp = 0;
10759 if (!id_declarator)
10760 unqualified_id = NULL_TREE;
10761 else
10763 unqualified_id = id_declarator->u.id.unqualified_name;
10764 switch (TREE_CODE (unqualified_id))
10766 case BIT_NOT_EXPR:
10767 unqualified_id = TREE_OPERAND (unqualified_id, 0);
10768 if (TYPE_P (unqualified_id))
10769 unqualified_id = constructor_name (unqualified_id);
10770 break;
10772 case IDENTIFIER_NODE:
10773 case TEMPLATE_ID_EXPR:
10774 break;
10776 default:
10777 gcc_unreachable ();
10781 if (declspecs->std_attributes)
10783 /* Apply the c++11 attributes to the type preceding them. */
10784 input_location = declspecs->locations[ds_std_attribute];
10785 decl_attributes (&type, declspecs->std_attributes, 0);
10786 input_location = saved_loc;
10789 /* Determine the type of the entity declared by recurring on the
10790 declarator. */
10791 for (; declarator; declarator = declarator->declarator)
10793 const cp_declarator *inner_declarator;
10794 tree attrs;
10796 if (type == error_mark_node)
10797 return error_mark_node;
10799 attrs = declarator->attributes;
10800 if (attrs)
10802 int attr_flags;
10804 attr_flags = 0;
10805 if (declarator == NULL || declarator->kind == cdk_id)
10806 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
10807 if (declarator->kind == cdk_function)
10808 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
10809 if (declarator->kind == cdk_array)
10810 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
10811 returned_attrs = decl_attributes (&type,
10812 chainon (returned_attrs, attrs),
10813 attr_flags);
10816 /* We don't want to warn in parmeter context because we don't
10817 yet know if the parse will succeed, and this might turn out
10818 to be a constructor call. */
10819 if (decl_context != PARM
10820 && declarator->parenthesized != UNKNOWN_LOCATION)
10821 warning_at (declarator->parenthesized, OPT_Wparentheses,
10822 "unnecessary parentheses in declaration of %qs", name);
10823 if (declarator->kind == cdk_id || declarator->kind == cdk_decomp)
10824 break;
10826 inner_declarator = declarator->declarator;
10828 switch (declarator->kind)
10830 case cdk_array:
10831 type = create_array_type_for_decl (dname, type,
10832 declarator->u.array.bounds);
10833 if (!valid_array_size_p (input_location, type, dname))
10834 type = error_mark_node;
10836 if (declarator->std_attributes)
10837 /* [dcl.array]/1:
10839 The optional attribute-specifier-seq appertains to the
10840 array. */
10841 returned_attrs = chainon (returned_attrs,
10842 declarator->std_attributes);
10843 break;
10845 case cdk_function:
10847 tree arg_types;
10848 int funcdecl_p;
10850 /* Declaring a function type. */
10852 input_location = declspecs->locations[ds_type_spec];
10853 abstract_virtuals_error (ACU_RETURN, type);
10854 input_location = saved_loc;
10856 /* Pick up type qualifiers which should be applied to `this'. */
10857 memfn_quals = declarator->u.function.qualifiers;
10858 /* Pick up virt-specifiers. */
10859 virt_specifiers = declarator->u.function.virt_specifiers;
10860 /* And ref-qualifier, too */
10861 rqual = declarator->u.function.ref_qualifier;
10862 /* And tx-qualifier. */
10863 tree tx_qual = declarator->u.function.tx_qualifier;
10864 /* Pick up the exception specifications. */
10865 raises = declarator->u.function.exception_specification;
10866 /* If the exception-specification is ill-formed, let's pretend
10867 there wasn't one. */
10868 if (raises == error_mark_node)
10869 raises = NULL_TREE;
10871 if (reqs)
10872 error_at (location_of (reqs), "requires-clause on return type");
10873 reqs = declarator->u.function.requires_clause;
10875 /* Say it's a definition only for the CALL_EXPR
10876 closest to the identifier. */
10877 funcdecl_p = inner_declarator && inner_declarator->kind == cdk_id;
10879 /* Handle a late-specified return type. */
10880 tree late_return_type = declarator->u.function.late_return_type;
10881 if (funcdecl_p)
10883 if (tree auto_node = type_uses_auto (type))
10885 if (!late_return_type)
10887 if (current_class_type
10888 && LAMBDA_TYPE_P (current_class_type))
10889 /* OK for C++11 lambdas. */;
10890 else if (cxx_dialect < cxx14)
10892 error ("%qs function uses "
10893 "%<auto%> type specifier without trailing "
10894 "return type", name);
10895 inform (input_location, "deduced return type "
10896 "only available with -std=c++14 or "
10897 "-std=gnu++14");
10899 else if (virtualp)
10901 error ("virtual function cannot "
10902 "have deduced return type");
10903 virtualp = false;
10906 else if (!is_auto (type) && sfk != sfk_conversion)
10908 error ("%qs function with trailing return type has"
10909 " %qT as its type rather than plain %<auto%>",
10910 name, type);
10911 return error_mark_node;
10913 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
10915 if (!late_return_type)
10917 if (dguide_name_p (unqualified_id))
10918 error_at (declarator->id_loc, "deduction guide "
10919 "for %qT must have trailing return "
10920 "type", TREE_TYPE (tmpl));
10921 else
10922 error_at (declarator->id_loc, "deduced class "
10923 "type %qT in function return type",
10924 type);
10925 inform (DECL_SOURCE_LOCATION (tmpl),
10926 "%qD declared here", tmpl);
10928 else if (CLASS_TYPE_P (late_return_type)
10929 && CLASSTYPE_TEMPLATE_INFO (late_return_type)
10930 && (CLASSTYPE_TI_TEMPLATE (late_return_type)
10931 == tmpl))
10932 /* OK */;
10933 else
10934 error ("trailing return type %qT of deduction guide "
10935 "is not a specialization of %qT",
10936 late_return_type, TREE_TYPE (tmpl));
10939 else if (late_return_type
10940 && sfk != sfk_conversion)
10942 if (cxx_dialect < cxx11)
10943 /* Not using maybe_warn_cpp0x because this should
10944 always be an error. */
10945 error ("trailing return type only available with "
10946 "-std=c++11 or -std=gnu++11");
10947 else
10948 error ("%qs function with trailing return type not "
10949 "declared with %<auto%> type specifier", name);
10950 return error_mark_node;
10953 type = splice_late_return_type (type, late_return_type);
10954 if (type == error_mark_node)
10955 return error_mark_node;
10957 if (late_return_type)
10959 late_return_type_p = true;
10960 type_quals = cp_type_quals (type);
10963 if (type_quals != TYPE_UNQUALIFIED)
10965 if (SCALAR_TYPE_P (type) || VOID_TYPE_P (type))
10966 warning_at (typespec_loc, OPT_Wignored_qualifiers, "type "
10967 "qualifiers ignored on function return type");
10968 /* We now know that the TYPE_QUALS don't apply to the
10969 decl, but to its return type. */
10970 type_quals = TYPE_UNQUALIFIED;
10973 /* Error about some types functions can't return. */
10975 if (TREE_CODE (type) == FUNCTION_TYPE)
10977 error_at (typespec_loc, "%qs declared as function returning "
10978 "a function", name);
10979 return error_mark_node;
10981 if (TREE_CODE (type) == ARRAY_TYPE)
10983 error_at (typespec_loc, "%qs declared as function returning "
10984 "an array", name);
10985 return error_mark_node;
10988 if (ctype == NULL_TREE
10989 && decl_context == FIELD
10990 && funcdecl_p
10991 && friendp == 0)
10992 ctype = current_class_type;
10994 if (ctype && (sfk == sfk_constructor
10995 || sfk == sfk_destructor))
10997 /* We are within a class's scope. If our declarator name
10998 is the same as the class name, and we are defining
10999 a function, then it is a constructor/destructor, and
11000 therefore returns a void type. */
11002 /* ISO C++ 12.4/2. A destructor may not be declared
11003 const or volatile. A destructor may not be static.
11004 A destructor may not be declared with ref-qualifier.
11006 ISO C++ 12.1. A constructor may not be declared
11007 const or volatile. A constructor may not be
11008 virtual. A constructor may not be static.
11009 A constructor may not be declared with ref-qualifier. */
11010 if (staticp == 2)
11011 error ((flags == DTOR_FLAG)
11012 ? G_("destructor cannot be static member function")
11013 : G_("constructor cannot be static member function"));
11014 if (memfn_quals)
11016 error ((flags == DTOR_FLAG)
11017 ? G_("destructors may not be cv-qualified")
11018 : G_("constructors may not be cv-qualified"));
11019 memfn_quals = TYPE_UNQUALIFIED;
11022 if (rqual)
11024 maybe_warn_cpp0x (CPP0X_REF_QUALIFIER);
11025 error ((flags == DTOR_FLAG)
11026 ? G_("destructors may not be ref-qualified")
11027 : G_("constructors may not be ref-qualified"));
11028 rqual = REF_QUAL_NONE;
11031 if (decl_context == FIELD
11032 && !member_function_or_else (ctype,
11033 current_class_type,
11034 flags))
11035 return error_mark_node;
11037 if (flags != DTOR_FLAG)
11039 /* It's a constructor. */
11040 if (explicitp == 1)
11041 explicitp = 2;
11042 if (virtualp)
11044 permerror (input_location,
11045 "constructors cannot be declared %<virtual%>");
11046 virtualp = 0;
11048 if (decl_context == FIELD
11049 && sfk != sfk_constructor)
11050 return error_mark_node;
11052 if (decl_context == FIELD)
11053 staticp = 0;
11055 else if (friendp)
11057 if (virtualp)
11059 /* Cannot be both friend and virtual. */
11060 error ("virtual functions cannot be friends");
11061 friendp = 0;
11063 if (decl_context == NORMAL)
11064 error ("friend declaration not in class definition");
11065 if (current_function_decl && funcdef_flag)
11066 error ("can%'t define friend function %qs in a local "
11067 "class definition",
11068 name);
11070 else if (ctype && sfk == sfk_conversion)
11072 if (explicitp == 1)
11074 maybe_warn_cpp0x (CPP0X_EXPLICIT_CONVERSION);
11075 explicitp = 2;
11077 if (late_return_type_p)
11078 error ("a conversion function cannot have a trailing return type");
11080 else if (sfk == sfk_deduction_guide)
11082 if (explicitp == 1)
11083 explicitp = 2;
11086 arg_types = grokparms (declarator->u.function.parameters,
11087 &parms);
11089 if (inner_declarator
11090 && inner_declarator->kind == cdk_id
11091 && inner_declarator->u.id.sfk == sfk_destructor
11092 && arg_types != void_list_node)
11094 error ("destructors may not have parameters");
11095 arg_types = void_list_node;
11096 parms = NULL_TREE;
11099 type = build_function_type (type, arg_types);
11101 tree attrs = declarator->std_attributes;
11102 if (tx_qual)
11104 tree att = build_tree_list (tx_qual, NULL_TREE);
11105 /* transaction_safe applies to the type, but
11106 transaction_safe_dynamic applies to the function. */
11107 if (is_attribute_p ("transaction_safe", tx_qual))
11108 attrs = chainon (attrs, att);
11109 else
11110 returned_attrs = chainon (returned_attrs, att);
11112 if (attrs)
11113 /* [dcl.fct]/2:
11115 The optional attribute-specifier-seq appertains to
11116 the function type. */
11117 decl_attributes (&type, attrs, 0);
11119 if (raises)
11120 type = build_exception_variant (type, raises);
11122 break;
11124 case cdk_pointer:
11125 case cdk_reference:
11126 case cdk_ptrmem:
11127 /* Filter out pointers-to-references and references-to-references.
11128 We can get these if a TYPE_DECL is used. */
11130 if (TREE_CODE (type) == REFERENCE_TYPE)
11132 if (declarator->kind != cdk_reference)
11134 error ("cannot declare pointer to %q#T", type);
11135 type = TREE_TYPE (type);
11138 /* In C++0x, we allow reference to reference declarations
11139 that occur indirectly through typedefs [7.1.3/8 dcl.typedef]
11140 and template type arguments [14.3.1/4 temp.arg.type]. The
11141 check for direct reference to reference declarations, which
11142 are still forbidden, occurs below. Reasoning behind the change
11143 can be found in DR106, DR540, and the rvalue reference
11144 proposals. */
11145 else if (cxx_dialect == cxx98)
11147 error ("cannot declare reference to %q#T", type);
11148 type = TREE_TYPE (type);
11151 else if (VOID_TYPE_P (type))
11153 if (declarator->kind == cdk_reference)
11154 error ("cannot declare reference to %q#T", type);
11155 else if (declarator->kind == cdk_ptrmem)
11156 error ("cannot declare pointer to %q#T member", type);
11159 /* We now know that the TYPE_QUALS don't apply to the decl,
11160 but to the target of the pointer. */
11161 type_quals = TYPE_UNQUALIFIED;
11163 /* This code used to handle METHOD_TYPE, but I don't think it's
11164 possible to get it here anymore. */
11165 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
11166 if (declarator->kind == cdk_ptrmem
11167 && TREE_CODE (type) == FUNCTION_TYPE)
11169 memfn_quals |= type_memfn_quals (type);
11170 type = build_memfn_type (type,
11171 declarator->u.pointer.class_type,
11172 memfn_quals,
11173 rqual);
11174 if (type == error_mark_node)
11175 return error_mark_node;
11177 rqual = REF_QUAL_NONE;
11178 memfn_quals = TYPE_UNQUALIFIED;
11181 if (TREE_CODE (type) == FUNCTION_TYPE
11182 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
11183 || type_memfn_rqual (type) != REF_QUAL_NONE))
11184 error (declarator->kind == cdk_reference
11185 ? G_("cannot declare reference to qualified function type %qT")
11186 : G_("cannot declare pointer to qualified function type %qT"),
11187 type);
11189 /* When the pointed-to type involves components of variable size,
11190 care must be taken to ensure that the size evaluation code is
11191 emitted early enough to dominate all the possible later uses
11192 and late enough for the variables on which it depends to have
11193 been assigned.
11195 This is expected to happen automatically when the pointed-to
11196 type has a name/declaration of it's own, but special attention
11197 is required if the type is anonymous.
11199 We handle the NORMAL and FIELD contexts here by inserting a
11200 dummy statement that just evaluates the size at a safe point
11201 and ensures it is not deferred until e.g. within a deeper
11202 conditional context (c++/43555).
11204 We expect nothing to be needed here for PARM or TYPENAME.
11205 Evaluating the size at this point for TYPENAME would
11206 actually be incorrect, as we might be in the middle of an
11207 expression with side effects on the pointed-to type size
11208 "arguments" prior to the pointer declaration point and the
11209 size evaluation could end up prior to the side effects. */
11211 if (!TYPE_NAME (type)
11212 && (decl_context == NORMAL || decl_context == FIELD)
11213 && at_function_scope_p ()
11214 && variably_modified_type_p (type, NULL_TREE))
11216 TYPE_NAME (type) = build_decl (UNKNOWN_LOCATION, TYPE_DECL,
11217 NULL_TREE, type);
11218 add_decl_expr (TYPE_NAME (type));
11221 if (declarator->kind == cdk_reference)
11223 /* In C++0x, the type we are creating a reference to might be
11224 a typedef which is itself a reference type. In that case,
11225 we follow the reference collapsing rules in
11226 [7.1.3/8 dcl.typedef] to create the final reference type:
11228 "If a typedef TD names a type that is a reference to a type
11229 T, an attempt to create the type 'lvalue reference to cv TD'
11230 creates the type 'lvalue reference to T,' while an attempt
11231 to create the type "rvalue reference to cv TD' creates the
11232 type TD."
11234 if (VOID_TYPE_P (type))
11235 /* We already gave an error. */;
11236 else if (TREE_CODE (type) == REFERENCE_TYPE)
11238 if (declarator->u.reference.rvalue_ref)
11239 /* Leave type alone. */;
11240 else
11241 type = cp_build_reference_type (TREE_TYPE (type), false);
11243 else
11244 type = cp_build_reference_type
11245 (type, declarator->u.reference.rvalue_ref);
11247 /* In C++0x, we need this check for direct reference to
11248 reference declarations, which are forbidden by
11249 [8.3.2/5 dcl.ref]. Reference to reference declarations
11250 are only allowed indirectly through typedefs and template
11251 type arguments. Example:
11253 void foo(int & &); // invalid ref-to-ref decl
11255 typedef int & int_ref;
11256 void foo(int_ref &); // valid ref-to-ref decl
11258 if (inner_declarator && inner_declarator->kind == cdk_reference)
11259 error ("cannot declare reference to %q#T, which is not "
11260 "a typedef or a template type argument", type);
11262 else if (TREE_CODE (type) == METHOD_TYPE)
11263 type = build_ptrmemfunc_type (build_pointer_type (type));
11264 else if (declarator->kind == cdk_ptrmem)
11266 gcc_assert (TREE_CODE (declarator->u.pointer.class_type)
11267 != NAMESPACE_DECL);
11268 if (declarator->u.pointer.class_type == error_mark_node)
11269 /* We will already have complained. */
11270 type = error_mark_node;
11271 else
11272 type = build_ptrmem_type (declarator->u.pointer.class_type,
11273 type);
11275 else
11276 type = build_pointer_type (type);
11278 /* Process a list of type modifier keywords (such as
11279 const or volatile) that were given inside the `*' or `&'. */
11281 if (declarator->u.pointer.qualifiers)
11283 type
11284 = cp_build_qualified_type (type,
11285 declarator->u.pointer.qualifiers);
11286 type_quals = cp_type_quals (type);
11289 /* Apply C++11 attributes to the pointer, and not to the
11290 type pointed to. This is unlike what is done for GNU
11291 attributes above. It is to comply with [dcl.ptr]/1:
11293 [the optional attribute-specifier-seq (7.6.1) appertains
11294 to the pointer and not to the object pointed to]. */
11295 if (declarator->std_attributes)
11296 decl_attributes (&type, declarator->std_attributes,
11299 ctype = NULL_TREE;
11300 break;
11302 case cdk_error:
11303 break;
11305 default:
11306 gcc_unreachable ();
11310 /* A `constexpr' specifier used in an object declaration declares
11311 the object as `const'. */
11312 if (constexpr_p && innermost_code != cdk_function)
11314 /* DR1688 says that a `constexpr' specifier in combination with
11315 `volatile' is valid. */
11317 if (TREE_CODE (type) != REFERENCE_TYPE)
11319 type_quals |= TYPE_QUAL_CONST;
11320 type = cp_build_qualified_type (type, type_quals);
11324 if (unqualified_id && TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR
11325 && TREE_CODE (type) != FUNCTION_TYPE
11326 && TREE_CODE (type) != METHOD_TYPE
11327 && !variable_template_p (TREE_OPERAND (unqualified_id, 0)))
11329 error ("template-id %qD used as a declarator",
11330 unqualified_id);
11331 unqualified_id = dname;
11334 /* If TYPE is a FUNCTION_TYPE, but the function name was explicitly
11335 qualified with a class-name, turn it into a METHOD_TYPE, unless
11336 we know that the function is static. We take advantage of this
11337 opportunity to do other processing that pertains to entities
11338 explicitly declared to be class members. Note that if DECLARATOR
11339 is non-NULL, we know it is a cdk_id declarator; otherwise, we
11340 would not have exited the loop above. */
11341 if (declarator
11342 && declarator->kind == cdk_id
11343 && declarator->u.id.qualifying_scope
11344 && MAYBE_CLASS_TYPE_P (declarator->u.id.qualifying_scope))
11346 ctype = declarator->u.id.qualifying_scope;
11347 ctype = TYPE_MAIN_VARIANT (ctype);
11348 template_count = num_template_headers_for_class (ctype);
11350 if (ctype == current_class_type)
11352 if (friendp)
11354 permerror (input_location, "member functions are implicitly "
11355 "friends of their class");
11356 friendp = 0;
11358 else
11359 permerror (declarator->id_loc,
11360 "extra qualification %<%T::%> on member %qs",
11361 ctype, name);
11363 else if (/* If the qualifying type is already complete, then we
11364 can skip the following checks. */
11365 !COMPLETE_TYPE_P (ctype)
11366 && (/* If the function is being defined, then
11367 qualifying type must certainly be complete. */
11368 funcdef_flag
11369 /* A friend declaration of "T::f" is OK, even if
11370 "T" is a template parameter. But, if this
11371 function is not a friend, the qualifying type
11372 must be a class. */
11373 || (!friendp && !CLASS_TYPE_P (ctype))
11374 /* For a declaration, the type need not be
11375 complete, if either it is dependent (since there
11376 is no meaningful definition of complete in that
11377 case) or the qualifying class is currently being
11378 defined. */
11379 || !(dependent_type_p (ctype)
11380 || currently_open_class (ctype)))
11381 /* Check that the qualifying type is complete. */
11382 && !complete_type_or_else (ctype, NULL_TREE))
11383 return error_mark_node;
11384 else if (TREE_CODE (type) == FUNCTION_TYPE)
11386 if (current_class_type
11387 && (!friendp || funcdef_flag || initialized))
11389 error (funcdef_flag || initialized
11390 ? G_("cannot define member function %<%T::%s%> "
11391 "within %qT")
11392 : G_("cannot declare member function %<%T::%s%> "
11393 "within %qT"),
11394 ctype, name, current_class_type);
11395 return error_mark_node;
11398 else if (typedef_p && current_class_type)
11400 error ("cannot declare member %<%T::%s%> within %qT",
11401 ctype, name, current_class_type);
11402 return error_mark_node;
11406 if (ctype == NULL_TREE && decl_context == FIELD && friendp == 0)
11407 ctype = current_class_type;
11409 /* Now TYPE has the actual type. */
11411 if (returned_attrs)
11413 if (attrlist)
11414 *attrlist = chainon (returned_attrs, *attrlist);
11415 else
11416 attrlist = &returned_attrs;
11419 if (declarator
11420 && declarator->kind == cdk_id
11421 && declarator->std_attributes
11422 && attrlist != NULL)
11423 /* [dcl.meaning]/1: The optional attribute-specifier-seq following
11424 a declarator-id appertains to the entity that is declared. */
11425 *attrlist = chainon (*attrlist, declarator->std_attributes);
11427 /* Handle parameter packs. */
11428 if (parameter_pack_p)
11430 if (decl_context == PARM)
11431 /* Turn the type into a pack expansion.*/
11432 type = make_pack_expansion (type);
11433 else
11434 error ("non-parameter %qs cannot be a parameter pack", name);
11437 if ((decl_context == FIELD || decl_context == PARM)
11438 && !processing_template_decl
11439 && variably_modified_type_p (type, NULL_TREE))
11441 if (decl_context == FIELD)
11442 error ("data member may not have variably modified type %qT", type);
11443 else
11444 error ("parameter may not have variably modified type %qT", type);
11445 type = error_mark_node;
11448 if (explicitp == 1 || (explicitp && friendp))
11450 /* [dcl.fct.spec] (C++11) The explicit specifier shall be used only
11451 in the declaration of a constructor or conversion function within
11452 a class definition. */
11453 if (!current_class_type)
11454 error_at (declspecs->locations[ds_explicit],
11455 "%<explicit%> outside class declaration");
11456 else if (friendp)
11457 error_at (declspecs->locations[ds_explicit],
11458 "%<explicit%> in friend declaration");
11459 else
11460 error_at (declspecs->locations[ds_explicit],
11461 "only declarations of constructors and conversion operators "
11462 "can be %<explicit%>");
11463 explicitp = 0;
11466 if (storage_class == sc_mutable)
11468 if (decl_context != FIELD || friendp)
11470 error ("non-member %qs cannot be declared %<mutable%>", name);
11471 storage_class = sc_none;
11473 else if (decl_context == TYPENAME || typedef_p)
11475 error ("non-object member %qs cannot be declared %<mutable%>", name);
11476 storage_class = sc_none;
11478 else if (TREE_CODE (type) == FUNCTION_TYPE
11479 || TREE_CODE (type) == METHOD_TYPE)
11481 error ("function %qs cannot be declared %<mutable%>", name);
11482 storage_class = sc_none;
11484 else if (staticp)
11486 error ("static %qs cannot be declared %<mutable%>", name);
11487 storage_class = sc_none;
11489 else if (type_quals & TYPE_QUAL_CONST)
11491 error ("const %qs cannot be declared %<mutable%>", name);
11492 storage_class = sc_none;
11494 else if (TREE_CODE (type) == REFERENCE_TYPE)
11496 permerror (input_location, "reference %qs cannot be declared "
11497 "%<mutable%>", name);
11498 storage_class = sc_none;
11502 /* If this is declaring a typedef name, return a TYPE_DECL. */
11503 if (typedef_p && decl_context != TYPENAME)
11505 tree decl;
11507 /* This declaration:
11509 typedef void f(int) const;
11511 declares a function type which is not a member of any
11512 particular class, but which is cv-qualified; for
11513 example "f S::*" declares a pointer to a const-qualified
11514 member function of S. We record the cv-qualification in the
11515 function type. */
11516 if ((rqual || memfn_quals) && TREE_CODE (type) == FUNCTION_TYPE)
11518 type = apply_memfn_quals (type, memfn_quals, rqual);
11520 /* We have now dealt with these qualifiers. */
11521 memfn_quals = TYPE_UNQUALIFIED;
11522 rqual = REF_QUAL_NONE;
11525 if (type_uses_auto (type))
11527 error ("typedef declared %<auto%>");
11528 type = error_mark_node;
11531 if (reqs)
11532 error_at (location_of (reqs), "requires-clause on typedef");
11534 if (decl_context == FIELD)
11535 decl = build_lang_decl (TYPE_DECL, unqualified_id, type);
11536 else
11537 decl = build_decl (input_location, TYPE_DECL, unqualified_id, type);
11538 if (id_declarator && declarator->u.id.qualifying_scope) {
11539 error_at (DECL_SOURCE_LOCATION (decl),
11540 "typedef name may not be a nested-name-specifier");
11541 TREE_TYPE (decl) = error_mark_node;
11544 if (decl_context != FIELD)
11546 if (!current_function_decl)
11547 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
11548 else if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (current_function_decl)
11549 || (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P
11550 (current_function_decl)))
11551 /* The TYPE_DECL is "abstract" because there will be
11552 clones of this constructor/destructor, and there will
11553 be copies of this TYPE_DECL generated in those
11554 clones. The decloning optimization (for space) may
11555 revert this subsequently if it determines that
11556 the clones should share a common implementation. */
11557 DECL_ABSTRACT_P (decl) = true;
11559 else if (current_class_type
11560 && constructor_name_p (unqualified_id, current_class_type))
11561 permerror (input_location, "ISO C++ forbids nested type %qD with same name "
11562 "as enclosing class",
11563 unqualified_id);
11565 /* If the user declares "typedef struct {...} foo" then the
11566 struct will have an anonymous name. Fill that name in now.
11567 Nothing can refer to it, so nothing needs know about the name
11568 change. */
11569 if (type != error_mark_node
11570 && unqualified_id
11571 && TYPE_NAME (type)
11572 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
11573 && TYPE_UNNAMED_P (type)
11574 && declspecs->type_definition_p
11575 && attributes_naming_typedef_ok (*attrlist)
11576 && cp_type_quals (type) == TYPE_UNQUALIFIED)
11577 name_unnamed_type (type, decl);
11579 if (signed_p
11580 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
11581 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
11583 bad_specifiers (decl, BSP_TYPE, virtualp,
11584 memfn_quals != TYPE_UNQUALIFIED,
11585 inlinep, friendp, raises != NULL_TREE);
11587 if (decl_spec_seq_has_spec_p (declspecs, ds_alias))
11588 /* Acknowledge that this was written:
11589 `using analias = atype;'. */
11590 TYPE_DECL_ALIAS_P (decl) = 1;
11592 return decl;
11595 /* Detect the case of an array type of unspecified size
11596 which came, as such, direct from a typedef name.
11597 We must copy the type, so that the array's domain can be
11598 individually set by the object's initializer. */
11600 if (type && typedef_type
11601 && TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
11602 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
11603 type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
11605 /* Detect where we're using a typedef of function type to declare a
11606 function. PARMS will not be set, so we must create it now. */
11608 if (type == typedef_type && TREE_CODE (type) == FUNCTION_TYPE)
11610 tree decls = NULL_TREE;
11611 tree args;
11613 for (args = TYPE_ARG_TYPES (type);
11614 args && args != void_list_node;
11615 args = TREE_CHAIN (args))
11617 tree decl = cp_build_parm_decl (NULL_TREE, NULL_TREE,
11618 TREE_VALUE (args));
11620 DECL_CHAIN (decl) = decls;
11621 decls = decl;
11624 parms = nreverse (decls);
11626 if (decl_context != TYPENAME)
11628 /* The qualifiers on the function type become the qualifiers on
11629 the non-static member function. */
11630 memfn_quals |= type_memfn_quals (type);
11631 rqual = type_memfn_rqual (type);
11632 type_quals = TYPE_UNQUALIFIED;
11636 /* If this is a type name (such as, in a cast or sizeof),
11637 compute the type and return it now. */
11639 if (decl_context == TYPENAME)
11641 /* Note that here we don't care about type_quals. */
11643 /* Special case: "friend class foo" looks like a TYPENAME context. */
11644 if (friendp)
11646 if (inlinep)
11648 error ("%<inline%> specified for friend class declaration");
11649 inlinep = 0;
11652 if (!current_aggr)
11654 /* Don't allow friend declaration without a class-key. */
11655 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
11656 permerror (input_location, "template parameters cannot be friends");
11657 else if (TREE_CODE (type) == TYPENAME_TYPE)
11658 permerror (input_location, "friend declaration requires class-key, "
11659 "i.e. %<friend class %T::%D%>",
11660 TYPE_CONTEXT (type), TYPENAME_TYPE_FULLNAME (type));
11661 else
11662 permerror (input_location, "friend declaration requires class-key, "
11663 "i.e. %<friend %#T%>",
11664 type);
11667 /* Only try to do this stuff if we didn't already give up. */
11668 if (type != integer_type_node)
11670 /* A friendly class? */
11671 if (current_class_type)
11672 make_friend_class (current_class_type, TYPE_MAIN_VARIANT (type),
11673 /*complain=*/true);
11674 else
11675 error ("trying to make class %qT a friend of global scope",
11676 type);
11678 type = void_type_node;
11681 else if (memfn_quals || rqual)
11683 if (ctype == NULL_TREE
11684 && TREE_CODE (type) == METHOD_TYPE)
11685 ctype = TYPE_METHOD_BASETYPE (type);
11687 if (ctype)
11688 type = build_memfn_type (type, ctype, memfn_quals, rqual);
11689 /* Core issue #547: need to allow this in template type args.
11690 Allow it in general in C++11 for alias-declarations. */
11691 else if ((template_type_arg || cxx_dialect >= cxx11)
11692 && TREE_CODE (type) == FUNCTION_TYPE)
11693 type = apply_memfn_quals (type, memfn_quals, rqual);
11694 else
11695 error ("invalid qualifiers on non-member function type");
11698 if (reqs)
11699 error_at (location_of (reqs), "requires-clause on type-id");
11701 return type;
11703 else if (unqualified_id == NULL_TREE && decl_context != PARM
11704 && decl_context != CATCHPARM
11705 && TREE_CODE (type) != UNION_TYPE
11706 && ! bitfield
11707 && innermost_code != cdk_decomp)
11709 error ("abstract declarator %qT used as declaration", type);
11710 return error_mark_node;
11713 if (!FUNC_OR_METHOD_TYPE_P (type))
11715 /* Only functions may be declared using an operator-function-id. */
11716 if (dname && IDENTIFIER_ANY_OP_P (dname))
11718 error ("declaration of %qD as non-function", dname);
11719 return error_mark_node;
11722 if (reqs)
11723 error_at (location_of (reqs),
11724 "requires-clause on declaration of non-function type %qT",
11725 type);
11728 /* We don't check parameter types here because we can emit a better
11729 error message later. */
11730 if (decl_context != PARM)
11732 type = check_var_type (unqualified_id, type);
11733 if (type == error_mark_node)
11734 return error_mark_node;
11737 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
11738 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
11740 if (decl_context == PARM || decl_context == CATCHPARM)
11742 if (ctype || in_namespace)
11743 error ("cannot use %<::%> in parameter declaration");
11745 if (type_uses_auto (type)
11746 && !(cxx_dialect >= cxx17 && template_parm_flag))
11748 if (cxx_dialect >= cxx14)
11749 error ("%<auto%> parameter not permitted in this context");
11750 else
11751 error ("parameter declared %<auto%>");
11752 type = error_mark_node;
11755 /* A parameter declared as an array of T is really a pointer to T.
11756 One declared as a function is really a pointer to a function.
11757 One declared as a member is really a pointer to member. */
11759 if (TREE_CODE (type) == ARRAY_TYPE)
11761 /* Transfer const-ness of array into that of type pointed to. */
11762 type = build_pointer_type (TREE_TYPE (type));
11763 type_quals = TYPE_UNQUALIFIED;
11764 array_parameter_p = true;
11766 else if (TREE_CODE (type) == FUNCTION_TYPE)
11767 type = build_pointer_type (type);
11770 if (ctype && TREE_CODE (type) == FUNCTION_TYPE && staticp < 2
11771 && !(identifier_p (unqualified_id)
11772 && IDENTIFIER_NEWDEL_OP_P (unqualified_id)))
11774 cp_cv_quals real_quals = memfn_quals;
11775 if (cxx_dialect < cxx14 && constexpr_p
11776 && sfk != sfk_constructor && sfk != sfk_destructor)
11777 real_quals |= TYPE_QUAL_CONST;
11778 type = build_memfn_type (type, ctype, real_quals, rqual);
11782 tree decl = NULL_TREE;
11784 if (decl_context == PARM)
11786 decl = cp_build_parm_decl (NULL_TREE, unqualified_id, type);
11787 DECL_ARRAY_PARAMETER_P (decl) = array_parameter_p;
11789 bad_specifiers (decl, BSP_PARM, virtualp,
11790 memfn_quals != TYPE_UNQUALIFIED,
11791 inlinep, friendp, raises != NULL_TREE);
11793 else if (decl_context == FIELD)
11795 if (!staticp && !friendp && TREE_CODE (type) != METHOD_TYPE)
11796 if (tree auto_node = type_uses_auto (type))
11798 location_t loc = declspecs->locations[ds_type_spec];
11799 if (CLASS_PLACEHOLDER_TEMPLATE (auto_node))
11800 error_at (loc, "invalid use of template-name %qE without an "
11801 "argument list",
11802 CLASS_PLACEHOLDER_TEMPLATE (auto_node));
11803 else
11804 error_at (loc, "non-static data member declared with "
11805 "placeholder %qT", auto_node);
11806 type = error_mark_node;
11809 /* The C99 flexible array extension. */
11810 if (!staticp && TREE_CODE (type) == ARRAY_TYPE
11811 && TYPE_DOMAIN (type) == NULL_TREE)
11813 if (ctype
11814 && (TREE_CODE (ctype) == UNION_TYPE
11815 || TREE_CODE (ctype) == QUAL_UNION_TYPE))
11817 error ("flexible array member in union");
11818 type = error_mark_node;
11820 else
11822 /* Array is a flexible member. */
11823 if (in_system_header_at (input_location))
11824 /* Do not warn on flexible array members in system
11825 headers because glibc uses them. */;
11826 else if (name)
11827 pedwarn (input_location, OPT_Wpedantic,
11828 "ISO C++ forbids flexible array member %qs", name);
11829 else
11830 pedwarn (input_location, OPT_Wpedantic,
11831 "ISO C++ forbids flexible array members");
11833 /* Flexible array member has a null domain. */
11834 type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
11838 if (type == error_mark_node)
11840 /* Happens when declaring arrays of sizes which
11841 are error_mark_node, for example. */
11842 decl = NULL_TREE;
11844 else if (in_namespace && !friendp)
11846 /* Something like struct S { int N::j; }; */
11847 error ("invalid use of %<::%>");
11848 return error_mark_node;
11850 else if (TREE_CODE (type) == FUNCTION_TYPE
11851 || TREE_CODE (type) == METHOD_TYPE)
11853 int publicp = 0;
11854 tree function_context;
11856 if (friendp == 0)
11858 /* This should never happen in pure C++ (the check
11859 could be an assert). It could happen in
11860 Objective-C++ if someone writes invalid code that
11861 uses a function declaration for an instance
11862 variable or property (instance variables and
11863 properties are parsed as FIELD_DECLs, but they are
11864 part of an Objective-C class, not a C++ class).
11865 That code is invalid and is caught by this
11866 check. */
11867 if (!ctype)
11869 error ("declaration of function %qD in invalid context",
11870 unqualified_id);
11871 return error_mark_node;
11874 /* ``A union may [ ... ] not [ have ] virtual functions.''
11875 ARM 9.5 */
11876 if (virtualp && TREE_CODE (ctype) == UNION_TYPE)
11878 error ("function %qD declared %<virtual%> inside a union",
11879 unqualified_id);
11880 return error_mark_node;
11883 if (virtualp
11884 && identifier_p (unqualified_id)
11885 && IDENTIFIER_NEWDEL_OP_P (unqualified_id))
11887 error ("%qD cannot be declared %<virtual%>, since it "
11888 "is always static", unqualified_id);
11889 virtualp = 0;
11893 /* Check that the name used for a destructor makes sense. */
11894 if (sfk == sfk_destructor)
11896 tree uqname = id_declarator->u.id.unqualified_name;
11898 if (!ctype)
11900 gcc_assert (friendp);
11901 error ("expected qualified name in friend declaration "
11902 "for destructor %qD", uqname);
11903 return error_mark_node;
11906 if (!check_dtor_name (ctype, TREE_OPERAND (uqname, 0)))
11908 error ("declaration of %qD as member of %qT",
11909 uqname, ctype);
11910 return error_mark_node;
11912 if (concept_p)
11914 error ("a destructor cannot be %<concept%>");
11915 return error_mark_node;
11917 if (constexpr_p)
11919 error ("a destructor cannot be %<constexpr%>");
11920 return error_mark_node;
11923 else if (sfk == sfk_constructor && friendp && !ctype)
11925 error ("expected qualified name in friend declaration "
11926 "for constructor %qD",
11927 id_declarator->u.id.unqualified_name);
11928 return error_mark_node;
11930 if (sfk == sfk_constructor)
11931 if (concept_p)
11933 error ("a constructor cannot be %<concept%>");
11934 return error_mark_node;
11936 if (concept_p)
11938 error ("a concept cannot be a member function");
11939 concept_p = false;
11942 if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
11944 tree tmpl = TREE_OPERAND (unqualified_id, 0);
11945 if (variable_template_p (tmpl))
11947 error ("specialization of variable template %qD "
11948 "declared as function", tmpl);
11949 inform (DECL_SOURCE_LOCATION (tmpl),
11950 "variable template declared here");
11951 return error_mark_node;
11955 /* Tell grokfndecl if it needs to set TREE_PUBLIC on the node. */
11956 function_context = (ctype != NULL_TREE) ?
11957 decl_function_context (TYPE_MAIN_DECL (ctype)) : NULL_TREE;
11958 publicp = (! friendp || ! staticp)
11959 && function_context == NULL_TREE;
11961 if (late_return_type_p)
11962 TYPE_HAS_LATE_RETURN_TYPE (type) = 1;
11964 decl = grokfndecl (ctype, type,
11965 TREE_CODE (unqualified_id) != TEMPLATE_ID_EXPR
11966 ? unqualified_id : dname,
11967 parms,
11968 unqualified_id,
11969 reqs,
11970 virtualp, flags, memfn_quals, rqual, raises,
11971 friendp ? -1 : 0, friendp, publicp,
11972 inlinep | (2 * constexpr_p) | (4 * concept_p),
11973 initialized == SD_DELETED, sfk,
11974 funcdef_flag, template_count, in_namespace,
11975 attrlist, declarator->id_loc);
11976 decl = set_virt_specifiers (decl, virt_specifiers);
11977 if (decl == NULL_TREE)
11978 return error_mark_node;
11979 #if 0
11980 /* This clobbers the attrs stored in `decl' from `attrlist'. */
11981 /* The decl and setting of decl_attr is also turned off. */
11982 decl = build_decl_attribute_variant (decl, decl_attr);
11983 #endif
11985 /* [class.conv.ctor]
11987 A constructor declared without the function-specifier
11988 explicit that can be called with a single parameter
11989 specifies a conversion from the type of its first
11990 parameter to the type of its class. Such a constructor
11991 is called a converting constructor. */
11992 if (explicitp == 2)
11993 DECL_NONCONVERTING_P (decl) = 1;
11995 else if (!staticp && !dependent_type_p (type)
11996 && !COMPLETE_TYPE_P (complete_type (type))
11997 && (!complete_or_array_type_p (type)
11998 || initialized == 0))
12000 if (TREE_CODE (type) != ARRAY_TYPE
12001 || !COMPLETE_TYPE_P (TREE_TYPE (type)))
12003 if (unqualified_id)
12005 error ("field %qD has incomplete type %qT",
12006 unqualified_id, type);
12007 cxx_incomplete_type_inform (strip_array_types (type));
12009 else
12010 error ("name %qT has incomplete type", type);
12012 type = error_mark_node;
12013 decl = NULL_TREE;
12016 else
12018 if (friendp)
12020 error ("%qE is neither function nor member function; "
12021 "cannot be declared friend", unqualified_id);
12022 friendp = 0;
12024 decl = NULL_TREE;
12027 if (friendp)
12029 /* Friends are treated specially. */
12030 if (ctype == current_class_type)
12031 ; /* We already issued a permerror. */
12032 else if (decl && DECL_NAME (decl))
12034 if (template_class_depth (current_class_type) == 0)
12036 decl = check_explicit_specialization
12037 (unqualified_id, decl, template_count,
12038 2 * funcdef_flag + 4);
12039 if (decl == error_mark_node)
12040 return error_mark_node;
12043 decl = do_friend (ctype, unqualified_id, decl,
12044 *attrlist, flags,
12045 funcdef_flag);
12046 return decl;
12048 else
12049 return error_mark_node;
12052 /* Structure field. It may not be a function, except for C++. */
12054 if (decl == NULL_TREE)
12056 if (staticp)
12058 /* C++ allows static class members. All other work
12059 for this is done by grokfield. */
12060 decl = build_lang_decl_loc (declarator
12061 ? declarator->id_loc
12062 : input_location,
12063 VAR_DECL, unqualified_id, type);
12064 set_linkage_for_static_data_member (decl);
12065 if (concept_p)
12066 error ("static data member %qE declared %<concept%>",
12067 unqualified_id);
12068 else if (constexpr_p && !initialized)
12070 error ("constexpr static data member %qD must have an "
12071 "initializer", decl);
12072 constexpr_p = false;
12075 if (inlinep)
12076 mark_inline_variable (decl);
12078 if (!DECL_VAR_DECLARED_INLINE_P (decl)
12079 && !(cxx_dialect >= cxx17 && constexpr_p))
12080 /* Even if there is an in-class initialization, DECL
12081 is considered undefined until an out-of-class
12082 definition is provided, unless this is an inline
12083 variable. */
12084 DECL_EXTERNAL (decl) = 1;
12086 if (thread_p)
12088 CP_DECL_THREAD_LOCAL_P (decl) = true;
12089 if (!processing_template_decl)
12090 set_decl_tls_model (decl, decl_default_tls_model (decl));
12091 if (declspecs->gnu_thread_keyword_p)
12092 SET_DECL_GNU_TLS_P (decl);
12095 else
12097 if (concept_p)
12098 error ("non-static data member %qE declared %<concept%>",
12099 unqualified_id);
12100 else if (constexpr_p)
12102 error ("non-static data member %qE declared %<constexpr%>",
12103 unqualified_id);
12104 constexpr_p = false;
12106 decl = build_decl (input_location,
12107 FIELD_DECL, unqualified_id, type);
12108 DECL_NONADDRESSABLE_P (decl) = bitfield;
12109 if (bitfield && !unqualified_id)
12110 TREE_NO_WARNING (decl) = 1;
12112 if (storage_class == sc_mutable)
12114 DECL_MUTABLE_P (decl) = 1;
12115 storage_class = sc_none;
12118 if (initialized)
12120 /* An attempt is being made to initialize a non-static
12121 member. This is new in C++11. */
12122 maybe_warn_cpp0x (CPP0X_NSDMI);
12124 /* If this has been parsed with static storage class, but
12125 errors forced staticp to be cleared, ensure NSDMI is
12126 not present. */
12127 if (declspecs->storage_class == sc_static)
12128 DECL_INITIAL (decl) = error_mark_node;
12132 bad_specifiers (decl, BSP_FIELD, virtualp,
12133 memfn_quals != TYPE_UNQUALIFIED,
12134 staticp ? false : inlinep, friendp,
12135 raises != NULL_TREE);
12138 else if (TREE_CODE (type) == FUNCTION_TYPE
12139 || TREE_CODE (type) == METHOD_TYPE)
12141 tree original_name;
12142 int publicp = 0;
12144 if (!unqualified_id)
12145 return error_mark_node;
12147 if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
12148 original_name = dname;
12149 else
12150 original_name = unqualified_id;
12151 // FIXME:gcc_assert (original_name == dname);
12153 if (storage_class == sc_auto)
12154 error ("storage class %<auto%> invalid for function %qs", name);
12155 else if (storage_class == sc_register)
12156 error ("storage class %<register%> invalid for function %qs", name);
12157 else if (thread_p)
12159 if (declspecs->gnu_thread_keyword_p)
12160 error ("storage class %<__thread%> invalid for function %qs",
12161 name);
12162 else
12163 error ("storage class %<thread_local%> invalid for function %qs",
12164 name);
12167 if (virt_specifiers)
12168 error ("virt-specifiers in %qs not allowed outside a class definition", name);
12169 /* Function declaration not at top level.
12170 Storage classes other than `extern' are not allowed
12171 and `extern' makes no difference. */
12172 if (! toplevel_bindings_p ()
12173 && (storage_class == sc_static
12174 || decl_spec_seq_has_spec_p (declspecs, ds_inline))
12175 && pedantic)
12177 if (storage_class == sc_static)
12178 pedwarn (input_location, OPT_Wpedantic,
12179 "%<static%> specifier invalid for function %qs "
12180 "declared out of global scope", name);
12181 else
12182 pedwarn (input_location, OPT_Wpedantic,
12183 "%<inline%> specifier invalid for function %qs "
12184 "declared out of global scope", name);
12187 if (ctype == NULL_TREE)
12189 if (virtualp)
12191 error ("virtual non-class function %qs", name);
12192 virtualp = 0;
12194 else if (sfk == sfk_constructor
12195 || sfk == sfk_destructor)
12197 error (funcdef_flag
12198 ? G_("%qs defined in a non-class scope")
12199 : G_("%qs declared in a non-class scope"), name);
12200 sfk = sfk_none;
12204 /* Record whether the function is public. */
12205 publicp = (ctype != NULL_TREE
12206 || storage_class != sc_static);
12208 if (late_return_type_p)
12209 TYPE_HAS_LATE_RETURN_TYPE (type) = 1;
12211 decl = grokfndecl (ctype, type, original_name, parms, unqualified_id,
12212 reqs, virtualp, flags, memfn_quals, rqual, raises,
12213 1, friendp,
12214 publicp,
12215 inlinep | (2 * constexpr_p) | (4 * concept_p),
12216 initialized == SD_DELETED,
12217 sfk,
12218 funcdef_flag,
12219 template_count, in_namespace, attrlist,
12220 declarator->id_loc);
12221 if (decl == NULL_TREE)
12222 return error_mark_node;
12224 if (explicitp == 2)
12225 DECL_NONCONVERTING_P (decl) = 1;
12226 if (staticp == 1)
12228 int invalid_static = 0;
12230 /* Don't allow a static member function in a class, and forbid
12231 declaring main to be static. */
12232 if (TREE_CODE (type) == METHOD_TYPE)
12234 permerror (input_location, "cannot declare member function %qD to have "
12235 "static linkage", decl);
12236 invalid_static = 1;
12238 else if (current_function_decl)
12240 /* 7.1.1: There can be no static function declarations within a
12241 block. */
12242 error ("cannot declare static function inside another function");
12243 invalid_static = 1;
12246 if (invalid_static)
12248 staticp = 0;
12249 storage_class = sc_none;
12253 else
12255 /* It's a variable. */
12257 /* An uninitialized decl with `extern' is a reference. */
12258 decl = grokvardecl (type, dname, unqualified_id,
12259 declspecs,
12260 initialized,
12261 type_quals,
12262 inlinep,
12263 concept_p,
12264 template_count,
12265 ctype ? ctype : in_namespace);
12266 if (decl == NULL_TREE)
12267 return error_mark_node;
12269 bad_specifiers (decl, BSP_VAR, virtualp,
12270 memfn_quals != TYPE_UNQUALIFIED,
12271 inlinep, friendp, raises != NULL_TREE);
12273 if (ctype)
12275 DECL_CONTEXT (decl) = ctype;
12276 if (staticp == 1)
12278 permerror (input_location, "%<static%> may not be used when defining "
12279 "(as opposed to declaring) a static data member");
12280 staticp = 0;
12281 storage_class = sc_none;
12283 if (storage_class == sc_register && TREE_STATIC (decl))
12285 error ("static member %qD declared %<register%>", decl);
12286 storage_class = sc_none;
12288 if (storage_class == sc_extern && pedantic)
12290 pedwarn (input_location, OPT_Wpedantic,
12291 "cannot explicitly declare member %q#D to have "
12292 "extern linkage", decl);
12293 storage_class = sc_none;
12296 else if (constexpr_p && DECL_EXTERNAL (decl))
12298 error ("declaration of constexpr variable %qD is not a definition",
12299 decl);
12300 constexpr_p = false;
12303 if (inlinep)
12304 mark_inline_variable (decl);
12305 if (innermost_code == cdk_decomp)
12307 gcc_assert (declarator && declarator->kind == cdk_decomp);
12308 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
12309 DECL_ARTIFICIAL (decl) = 1;
12310 fit_decomposition_lang_decl (decl, NULL_TREE);
12314 if (VAR_P (decl) && !initialized)
12315 if (tree auto_node = type_uses_auto (type))
12316 if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node))
12318 location_t loc = declspecs->locations[ds_type_spec];
12319 error_at (loc, "declaration of %q#D has no initializer", decl);
12320 TREE_TYPE (decl) = error_mark_node;
12323 if (storage_class == sc_extern && initialized && !funcdef_flag)
12325 if (toplevel_bindings_p ())
12327 /* It's common practice (and completely valid) to have a const
12328 be initialized and declared extern. */
12329 if (!(type_quals & TYPE_QUAL_CONST))
12330 warning (0, "%qs initialized and declared %<extern%>", name);
12332 else
12334 error ("%qs has both %<extern%> and initializer", name);
12335 return error_mark_node;
12339 /* Record `register' declaration for warnings on &
12340 and in case doing stupid register allocation. */
12342 if (storage_class == sc_register)
12344 DECL_REGISTER (decl) = 1;
12345 /* Warn about register storage specifiers on PARM_DECLs. */
12346 if (TREE_CODE (decl) == PARM_DECL)
12348 if (cxx_dialect >= cxx17)
12349 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
12350 "ISO C++17 does not allow %<register%> storage "
12351 "class specifier");
12352 else
12353 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
12354 "%<register%> storage class specifier used");
12357 else if (storage_class == sc_extern)
12358 DECL_THIS_EXTERN (decl) = 1;
12359 else if (storage_class == sc_static)
12360 DECL_THIS_STATIC (decl) = 1;
12362 /* Set constexpr flag on vars (functions got it in grokfndecl). */
12363 if (constexpr_p && VAR_P (decl))
12364 DECL_DECLARED_CONSTEXPR_P (decl) = true;
12366 /* Record constancy and volatility on the DECL itself . There's
12367 no need to do this when processing a template; we'll do this
12368 for the instantiated declaration based on the type of DECL. */
12369 if (!processing_template_decl)
12370 cp_apply_type_quals_to_decl (type_quals, decl);
12372 return decl;
12376 /* Subroutine of start_function. Ensure that each of the parameter
12377 types (as listed in PARMS) is complete, as is required for a
12378 function definition. */
12380 static void
12381 require_complete_types_for_parms (tree parms)
12383 for (; parms; parms = DECL_CHAIN (parms))
12385 if (dependent_type_p (TREE_TYPE (parms)))
12386 continue;
12387 if (!VOID_TYPE_P (TREE_TYPE (parms))
12388 && complete_type_or_else (TREE_TYPE (parms), parms))
12390 relayout_decl (parms);
12391 DECL_ARG_TYPE (parms) = type_passed_as (TREE_TYPE (parms));
12393 maybe_warn_parm_abi (TREE_TYPE (parms),
12394 DECL_SOURCE_LOCATION (parms));
12396 else
12397 /* grokparms or complete_type_or_else will have already issued
12398 an error. */
12399 TREE_TYPE (parms) = error_mark_node;
12403 /* Returns nonzero if T is a local variable. */
12406 local_variable_p (const_tree t)
12408 if ((VAR_P (t)
12409 /* A VAR_DECL with a context that is a _TYPE is a static data
12410 member. */
12411 && !TYPE_P (CP_DECL_CONTEXT (t))
12412 /* Any other non-local variable must be at namespace scope. */
12413 && !DECL_NAMESPACE_SCOPE_P (t))
12414 || (TREE_CODE (t) == PARM_DECL))
12415 return 1;
12417 return 0;
12420 /* Like local_variable_p, but suitable for use as a tree-walking
12421 function. */
12423 static tree
12424 local_variable_p_walkfn (tree *tp, int *walk_subtrees,
12425 void * /*data*/)
12427 if (local_variable_p (*tp)
12428 && (!DECL_ARTIFICIAL (*tp) || DECL_NAME (*tp) == this_identifier))
12429 return *tp;
12430 else if (TYPE_P (*tp))
12431 *walk_subtrees = 0;
12433 return NULL_TREE;
12436 /* Check that ARG, which is a default-argument expression for a
12437 parameter DECL, is valid. Returns ARG, or ERROR_MARK_NODE, if
12438 something goes wrong. DECL may also be a _TYPE node, rather than a
12439 DECL, if there is no DECL available. */
12441 tree
12442 check_default_argument (tree decl, tree arg, tsubst_flags_t complain)
12444 tree var;
12445 tree decl_type;
12447 if (TREE_CODE (arg) == DEFAULT_ARG)
12448 /* We get a DEFAULT_ARG when looking at an in-class declaration
12449 with a default argument. Ignore the argument for now; we'll
12450 deal with it after the class is complete. */
12451 return arg;
12453 if (TYPE_P (decl))
12455 decl_type = decl;
12456 decl = NULL_TREE;
12458 else
12459 decl_type = TREE_TYPE (decl);
12461 if (arg == error_mark_node
12462 || decl == error_mark_node
12463 || TREE_TYPE (arg) == error_mark_node
12464 || decl_type == error_mark_node)
12465 /* Something already went wrong. There's no need to check
12466 further. */
12467 return error_mark_node;
12469 /* [dcl.fct.default]
12471 A default argument expression is implicitly converted to the
12472 parameter type. */
12473 ++cp_unevaluated_operand;
12474 perform_implicit_conversion_flags (decl_type, arg, complain,
12475 LOOKUP_IMPLICIT);
12476 --cp_unevaluated_operand;
12478 /* Avoid redundant -Wzero-as-null-pointer-constant warnings at
12479 the call sites. */
12480 if (TYPE_PTR_OR_PTRMEM_P (decl_type)
12481 && null_ptr_cst_p (arg))
12482 return nullptr_node;
12484 /* [dcl.fct.default]
12486 Local variables shall not be used in default argument
12487 expressions.
12489 The keyword `this' shall not be used in a default argument of a
12490 member function. */
12491 var = cp_walk_tree_without_duplicates (&arg, local_variable_p_walkfn, NULL);
12492 if (var)
12494 if (complain & tf_warning_or_error)
12496 if (DECL_NAME (var) == this_identifier)
12497 permerror (input_location, "default argument %qE uses %qD",
12498 arg, var);
12499 else
12500 error ("default argument %qE uses local variable %qD", arg, var);
12502 return error_mark_node;
12505 /* All is well. */
12506 return arg;
12509 /* Returns a deprecated type used within TYPE, or NULL_TREE if none. */
12511 static tree
12512 type_is_deprecated (tree type)
12514 enum tree_code code;
12515 if (TREE_DEPRECATED (type))
12516 return type;
12517 if (TYPE_NAME (type))
12519 if (TREE_DEPRECATED (TYPE_NAME (type)))
12520 return type;
12521 else
12522 return NULL_TREE;
12525 /* Do warn about using typedefs to a deprecated class. */
12526 if (OVERLOAD_TYPE_P (type) && type != TYPE_MAIN_VARIANT (type))
12527 return type_is_deprecated (TYPE_MAIN_VARIANT (type));
12529 code = TREE_CODE (type);
12531 if (code == POINTER_TYPE || code == REFERENCE_TYPE
12532 || code == OFFSET_TYPE || code == FUNCTION_TYPE
12533 || code == METHOD_TYPE || code == ARRAY_TYPE)
12534 return type_is_deprecated (TREE_TYPE (type));
12536 if (TYPE_PTRMEMFUNC_P (type))
12537 return type_is_deprecated
12538 (TREE_TYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type))));
12540 return NULL_TREE;
12543 /* Decode the list of parameter types for a function type.
12544 Given the list of things declared inside the parens,
12545 return a list of types.
12547 If this parameter does not end with an ellipsis, we append
12548 void_list_node.
12550 *PARMS is set to the chain of PARM_DECLs created. */
12552 tree
12553 grokparms (tree parmlist, tree *parms)
12555 tree result = NULL_TREE;
12556 tree decls = NULL_TREE;
12557 tree parm;
12558 int any_error = 0;
12560 for (parm = parmlist; parm != NULL_TREE; parm = TREE_CHAIN (parm))
12562 tree type = NULL_TREE;
12563 tree init = TREE_PURPOSE (parm);
12564 tree decl = TREE_VALUE (parm);
12566 if (parm == void_list_node)
12567 break;
12569 if (! decl || TREE_TYPE (decl) == error_mark_node)
12570 continue;
12572 type = TREE_TYPE (decl);
12573 if (VOID_TYPE_P (type))
12575 if (same_type_p (type, void_type_node)
12576 && !init
12577 && !DECL_NAME (decl) && !result
12578 && TREE_CHAIN (parm) == void_list_node)
12579 /* DR 577: A parameter list consisting of a single
12580 unnamed parameter of non-dependent type 'void'. */
12581 break;
12582 else if (cv_qualified_p (type))
12583 error_at (DECL_SOURCE_LOCATION (decl),
12584 "invalid use of cv-qualified type %qT in "
12585 "parameter declaration", type);
12586 else
12587 error_at (DECL_SOURCE_LOCATION (decl),
12588 "invalid use of type %<void%> in parameter "
12589 "declaration");
12590 /* It's not a good idea to actually create parameters of
12591 type `void'; other parts of the compiler assume that a
12592 void type terminates the parameter list. */
12593 type = error_mark_node;
12594 TREE_TYPE (decl) = error_mark_node;
12597 if (type != error_mark_node)
12599 if (deprecated_state != DEPRECATED_SUPPRESS)
12601 tree deptype = type_is_deprecated (type);
12602 if (deptype)
12603 warn_deprecated_use (deptype, NULL_TREE);
12606 /* Top-level qualifiers on the parameters are
12607 ignored for function types. */
12608 type = cp_build_qualified_type (type, 0);
12609 if (TREE_CODE (type) == METHOD_TYPE)
12611 error ("parameter %qD invalidly declared method type", decl);
12612 type = build_pointer_type (type);
12613 TREE_TYPE (decl) = type;
12615 else if (abstract_virtuals_error (decl, type))
12616 any_error = 1; /* Seems like a good idea. */
12617 else if (cxx_dialect < cxx17 && POINTER_TYPE_P (type))
12619 /* Before C++17 DR 393:
12620 [dcl.fct]/6, parameter types cannot contain pointers
12621 (references) to arrays of unknown bound. */
12622 tree t = TREE_TYPE (type);
12623 int ptr = TYPE_PTR_P (type);
12625 while (1)
12627 if (TYPE_PTR_P (t))
12628 ptr = 1;
12629 else if (TREE_CODE (t) != ARRAY_TYPE)
12630 break;
12631 else if (!TYPE_DOMAIN (t))
12632 break;
12633 t = TREE_TYPE (t);
12635 if (TREE_CODE (t) == ARRAY_TYPE)
12636 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
12638 ? G_("parameter %qD includes pointer to array of "
12639 "unknown bound %qT")
12640 : G_("parameter %qD includes reference to array of "
12641 "unknown bound %qT"),
12642 decl, t);
12645 if (any_error)
12646 init = NULL_TREE;
12647 else if (init && !processing_template_decl)
12648 init = check_default_argument (decl, init, tf_warning_or_error);
12651 DECL_CHAIN (decl) = decls;
12652 decls = decl;
12653 result = tree_cons (init, type, result);
12655 decls = nreverse (decls);
12656 result = nreverse (result);
12657 if (parm)
12658 result = chainon (result, void_list_node);
12659 *parms = decls;
12661 return result;
12665 /* D is a constructor or overloaded `operator='.
12667 Let T be the class in which D is declared. Then, this function
12668 returns:
12670 -1 if D's is an ill-formed constructor or copy assignment operator
12671 whose first parameter is of type `T'.
12672 0 if D is not a copy constructor or copy assignment
12673 operator.
12674 1 if D is a copy constructor or copy assignment operator whose
12675 first parameter is a reference to non-const qualified T.
12676 2 if D is a copy constructor or copy assignment operator whose
12677 first parameter is a reference to const qualified T.
12679 This function can be used as a predicate. Positive values indicate
12680 a copy constructor and nonzero values indicate a copy assignment
12681 operator. */
12684 copy_fn_p (const_tree d)
12686 tree args;
12687 tree arg_type;
12688 int result = 1;
12690 gcc_assert (DECL_FUNCTION_MEMBER_P (d));
12692 if (TREE_CODE (d) == TEMPLATE_DECL
12693 || (DECL_TEMPLATE_INFO (d)
12694 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d))))
12695 /* Instantiations of template member functions are never copy
12696 functions. Note that member functions of templated classes are
12697 represented as template functions internally, and we must
12698 accept those as copy functions. */
12699 return 0;
12701 args = FUNCTION_FIRST_USER_PARMTYPE (d);
12702 if (!args)
12703 return 0;
12705 arg_type = TREE_VALUE (args);
12706 if (arg_type == error_mark_node)
12707 return 0;
12709 if (TYPE_MAIN_VARIANT (arg_type) == DECL_CONTEXT (d))
12711 /* Pass by value copy assignment operator. */
12712 result = -1;
12714 else if (TREE_CODE (arg_type) == REFERENCE_TYPE
12715 && !TYPE_REF_IS_RVALUE (arg_type)
12716 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)) == DECL_CONTEXT (d))
12718 if (CP_TYPE_CONST_P (TREE_TYPE (arg_type)))
12719 result = 2;
12721 else
12722 return 0;
12724 args = TREE_CHAIN (args);
12726 if (args && args != void_list_node && !TREE_PURPOSE (args))
12727 /* There are more non-optional args. */
12728 return 0;
12730 return result;
12733 /* D is a constructor or overloaded `operator='.
12735 Let T be the class in which D is declared. Then, this function
12736 returns true when D is a move constructor or move assignment
12737 operator, false otherwise. */
12739 bool
12740 move_fn_p (const_tree d)
12742 gcc_assert (DECL_FUNCTION_MEMBER_P (d));
12744 if (cxx_dialect == cxx98)
12745 /* There are no move constructors if we are in C++98 mode. */
12746 return false;
12748 if (TREE_CODE (d) == TEMPLATE_DECL
12749 || (DECL_TEMPLATE_INFO (d)
12750 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d))))
12751 /* Instantiations of template member functions are never move
12752 functions. Note that member functions of templated classes are
12753 represented as template functions internally, and we must
12754 accept those as move functions. */
12755 return 0;
12757 return move_signature_fn_p (d);
12760 /* D is a constructor or overloaded `operator='.
12762 Then, this function returns true when D has the same signature as a move
12763 constructor or move assignment operator (because either it is such a
12764 ctor/op= or it is a template specialization with the same signature),
12765 false otherwise. */
12767 bool
12768 move_signature_fn_p (const_tree d)
12770 tree args;
12771 tree arg_type;
12772 bool result = false;
12774 args = FUNCTION_FIRST_USER_PARMTYPE (d);
12775 if (!args)
12776 return 0;
12778 arg_type = TREE_VALUE (args);
12779 if (arg_type == error_mark_node)
12780 return 0;
12782 if (TREE_CODE (arg_type) == REFERENCE_TYPE
12783 && TYPE_REF_IS_RVALUE (arg_type)
12784 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)),
12785 DECL_CONTEXT (d)))
12786 result = true;
12788 args = TREE_CHAIN (args);
12790 if (args && args != void_list_node && !TREE_PURPOSE (args))
12791 /* There are more non-optional args. */
12792 return false;
12794 return result;
12797 /* Remember any special properties of member function DECL. */
12799 void
12800 grok_special_member_properties (tree decl)
12802 tree class_type;
12804 if (!DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
12805 return;
12807 class_type = DECL_CONTEXT (decl);
12808 if (IDENTIFIER_CTOR_P (DECL_NAME (decl)))
12810 int ctor = copy_fn_p (decl);
12812 if (!DECL_ARTIFICIAL (decl))
12813 TYPE_HAS_USER_CONSTRUCTOR (class_type) = 1;
12815 if (ctor > 0)
12817 /* [class.copy]
12819 A non-template constructor for class X is a copy
12820 constructor if its first parameter is of type X&, const
12821 X&, volatile X& or const volatile X&, and either there
12822 are no other parameters or else all other parameters have
12823 default arguments. */
12824 TYPE_HAS_COPY_CTOR (class_type) = 1;
12825 if (user_provided_p (decl))
12826 TYPE_HAS_COMPLEX_COPY_CTOR (class_type) = 1;
12827 if (ctor > 1)
12828 TYPE_HAS_CONST_COPY_CTOR (class_type) = 1;
12830 else if (sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (decl)))
12831 TYPE_HAS_DEFAULT_CONSTRUCTOR (class_type) = 1;
12832 else if (move_fn_p (decl) && user_provided_p (decl))
12833 TYPE_HAS_COMPLEX_MOVE_CTOR (class_type) = 1;
12834 else if (is_list_ctor (decl))
12835 TYPE_HAS_LIST_CTOR (class_type) = 1;
12837 if (DECL_DECLARED_CONSTEXPR_P (decl)
12838 && !ctor && !move_fn_p (decl))
12839 TYPE_HAS_CONSTEXPR_CTOR (class_type) = 1;
12841 else if (DECL_NAME (decl) == cp_assignment_operator_id (NOP_EXPR))
12843 /* [class.copy]
12845 A non-template assignment operator for class X is a copy
12846 assignment operator if its parameter is of type X, X&, const
12847 X&, volatile X& or const volatile X&. */
12849 int assop = copy_fn_p (decl);
12851 if (assop)
12853 TYPE_HAS_COPY_ASSIGN (class_type) = 1;
12854 if (user_provided_p (decl))
12855 TYPE_HAS_COMPLEX_COPY_ASSIGN (class_type) = 1;
12856 if (assop != 1)
12857 TYPE_HAS_CONST_COPY_ASSIGN (class_type) = 1;
12859 else if (move_fn_p (decl) && user_provided_p (decl))
12860 TYPE_HAS_COMPLEX_MOVE_ASSIGN (class_type) = 1;
12862 else if (IDENTIFIER_CONV_OP_P (DECL_NAME (decl)))
12863 TYPE_HAS_CONVERSION (class_type) = true;
12865 /* Destructors are handled in check_methods. */
12868 /* Check a constructor DECL has the correct form. Complains
12869 if the class has a constructor of the form X(X). */
12871 bool
12872 grok_ctor_properties (const_tree ctype, const_tree decl)
12874 int ctor_parm = copy_fn_p (decl);
12876 if (ctor_parm < 0)
12878 /* [class.copy]
12880 A declaration of a constructor for a class X is ill-formed if
12881 its first parameter is of type (optionally cv-qualified) X
12882 and either there are no other parameters or else all other
12883 parameters have default arguments.
12885 We *don't* complain about member template instantiations that
12886 have this form, though; they can occur as we try to decide
12887 what constructor to use during overload resolution. Since
12888 overload resolution will never prefer such a constructor to
12889 the non-template copy constructor (which is either explicitly
12890 or implicitly defined), there's no need to worry about their
12891 existence. Theoretically, they should never even be
12892 instantiated, but that's hard to forestall. */
12893 error ("invalid constructor; you probably meant %<%T (const %T&)%>",
12894 ctype, ctype);
12895 return false;
12898 return true;
12901 /* An operator with this code is unary, but can also be binary. */
12903 static bool
12904 ambi_op_p (enum tree_code code)
12906 return (code == INDIRECT_REF
12907 || code == ADDR_EXPR
12908 || code == UNARY_PLUS_EXPR
12909 || code == NEGATE_EXPR
12910 || code == PREINCREMENT_EXPR
12911 || code == PREDECREMENT_EXPR);
12914 /* An operator with this name can only be unary. */
12916 static bool
12917 unary_op_p (enum tree_code code)
12919 return (code == TRUTH_NOT_EXPR
12920 || code == BIT_NOT_EXPR
12921 || code == COMPONENT_REF
12922 || code == TYPE_EXPR);
12925 /* DECL is a declaration for an overloaded operator. If COMPLAIN is true,
12926 errors are issued for invalid declarations. */
12928 bool
12929 grok_op_properties (tree decl, bool complain)
12931 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
12932 tree argtype;
12933 int methodp = (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE);
12934 tree name = DECL_NAME (decl);
12935 enum tree_code operator_code;
12936 int arity;
12937 bool ellipsis_p;
12938 tree class_type;
12940 /* Count the number of arguments and check for ellipsis. */
12941 for (argtype = argtypes, arity = 0;
12942 argtype && argtype != void_list_node;
12943 argtype = TREE_CHAIN (argtype))
12944 ++arity;
12945 ellipsis_p = !argtype;
12947 class_type = DECL_CONTEXT (decl);
12948 if (class_type && !CLASS_TYPE_P (class_type))
12949 class_type = NULL_TREE;
12951 if (IDENTIFIER_CONV_OP_P (name))
12952 operator_code = TYPE_EXPR;
12953 else
12955 /* It'd be nice to hang something else of the identifier to
12956 find CODE more directly. */
12957 bool assign_op = IDENTIFIER_ASSIGN_OP_P (name);
12958 const operator_name_info_t *oni
12959 = (assign_op ? assignment_operator_name_info : operator_name_info);
12961 if (false)
12963 #define DEF_OPERATOR(NAME, CODE, MANGLING, ARITY, KIND) \
12964 else if (assign_op == (KIND == cik_assign_op) \
12965 && oni[int (CODE)].identifier == name) \
12966 operator_code = (CODE);
12967 #include "operators.def"
12968 #undef DEF_OPERATOR
12969 else
12970 gcc_unreachable ();
12972 while (0);
12973 gcc_assert (operator_code != MAX_TREE_CODES);
12974 SET_OVERLOADED_OPERATOR_CODE (decl, operator_code);
12976 if (class_type)
12977 switch (operator_code)
12979 case NEW_EXPR:
12980 TYPE_HAS_NEW_OPERATOR (class_type) = 1;
12981 break;
12983 case DELETE_EXPR:
12984 TYPE_GETS_DELETE (class_type) |= 1;
12985 break;
12987 case VEC_NEW_EXPR:
12988 TYPE_HAS_ARRAY_NEW_OPERATOR (class_type) = 1;
12989 break;
12991 case VEC_DELETE_EXPR:
12992 TYPE_GETS_DELETE (class_type) |= 2;
12993 break;
12995 default:
12996 break;
12999 /* [basic.std.dynamic.allocation]/1:
13001 A program is ill-formed if an allocation function is declared
13002 in a namespace scope other than global scope or declared static
13003 in global scope.
13005 The same also holds true for deallocation functions. */
13006 if (operator_code == NEW_EXPR || operator_code == VEC_NEW_EXPR
13007 || operator_code == DELETE_EXPR || operator_code == VEC_DELETE_EXPR)
13009 if (DECL_NAMESPACE_SCOPE_P (decl))
13011 if (CP_DECL_CONTEXT (decl) != global_namespace)
13013 error ("%qD may not be declared within a namespace", decl);
13014 return false;
13016 else if (!TREE_PUBLIC (decl))
13018 error ("%qD may not be declared as static", decl);
13019 return false;
13024 if (operator_code == NEW_EXPR || operator_code == VEC_NEW_EXPR)
13026 TREE_TYPE (decl) = coerce_new_type (TREE_TYPE (decl));
13027 DECL_IS_OPERATOR_NEW (decl) = 1;
13029 else if (operator_code == DELETE_EXPR || operator_code == VEC_DELETE_EXPR)
13030 TREE_TYPE (decl) = coerce_delete_type (TREE_TYPE (decl));
13031 else
13033 /* An operator function must either be a non-static member function
13034 or have at least one parameter of a class, a reference to a class,
13035 an enumeration, or a reference to an enumeration. 13.4.0.6 */
13036 if (! methodp || DECL_STATIC_FUNCTION_P (decl))
13038 if (operator_code == TYPE_EXPR
13039 || operator_code == CALL_EXPR
13040 || operator_code == COMPONENT_REF
13041 || operator_code == ARRAY_REF
13042 || operator_code == NOP_EXPR)
13044 error ("%qD must be a nonstatic member function", decl);
13045 return false;
13047 else
13049 tree p;
13051 if (DECL_STATIC_FUNCTION_P (decl))
13053 error ("%qD must be either a non-static member "
13054 "function or a non-member function", decl);
13055 return false;
13058 for (p = argtypes; p && p != void_list_node; p = TREE_CHAIN (p))
13060 tree arg = non_reference (TREE_VALUE (p));
13061 if (arg == error_mark_node)
13062 return false;
13064 /* MAYBE_CLASS_TYPE_P, rather than CLASS_TYPE_P, is used
13065 because these checks are performed even on
13066 template functions. */
13067 if (MAYBE_CLASS_TYPE_P (arg)
13068 || TREE_CODE (arg) == ENUMERAL_TYPE)
13069 break;
13072 if (!p || p == void_list_node)
13074 if (complain)
13075 error ("%qD must have an argument of class or "
13076 "enumerated type", decl);
13077 return false;
13082 /* There are no restrictions on the arguments to an overloaded
13083 "operator ()". */
13084 if (operator_code == CALL_EXPR)
13085 return true;
13087 /* Warn about conversion operators that will never be used. */
13088 if (IDENTIFIER_CONV_OP_P (name)
13089 && ! DECL_TEMPLATE_INFO (decl)
13090 && warn_conversion
13091 /* Warn only declaring the function; there is no need to
13092 warn again about out-of-class definitions. */
13093 && class_type == current_class_type)
13095 tree t = TREE_TYPE (name);
13096 int ref = (TREE_CODE (t) == REFERENCE_TYPE);
13098 if (ref)
13099 t = TYPE_MAIN_VARIANT (TREE_TYPE (t));
13101 if (VOID_TYPE_P (t))
13102 warning (OPT_Wconversion,
13104 ? G_("conversion to a reference to void "
13105 "will never use a type conversion operator")
13106 : G_("conversion to void "
13107 "will never use a type conversion operator"));
13108 else if (class_type)
13110 if (t == class_type)
13111 warning (OPT_Wconversion,
13113 ? G_("conversion to a reference to the same type "
13114 "will never use a type conversion operator")
13115 : G_("conversion to the same type "
13116 "will never use a type conversion operator"));
13117 /* Don't force t to be complete here. */
13118 else if (MAYBE_CLASS_TYPE_P (t)
13119 && COMPLETE_TYPE_P (t)
13120 && DERIVED_FROM_P (t, class_type))
13121 warning (OPT_Wconversion,
13123 ? G_("conversion to a reference to a base class "
13124 "will never use a type conversion operator")
13125 : G_("conversion to a base class "
13126 "will never use a type conversion operator"));
13131 if (operator_code == COND_EXPR)
13133 /* 13.4.0.3 */
13134 error ("ISO C++ prohibits overloading operator ?:");
13135 return false;
13137 else if (ellipsis_p)
13139 error ("%qD must not have variable number of arguments", decl);
13140 return false;
13142 else if (ambi_op_p (operator_code))
13144 if (arity == 1)
13145 /* We pick the one-argument operator codes by default, so
13146 we don't have to change anything. */
13148 else if (arity == 2)
13150 /* If we thought this was a unary operator, we now know
13151 it to be a binary operator. */
13152 switch (operator_code)
13154 case INDIRECT_REF:
13155 operator_code = MULT_EXPR;
13156 break;
13158 case ADDR_EXPR:
13159 operator_code = BIT_AND_EXPR;
13160 break;
13162 case UNARY_PLUS_EXPR:
13163 operator_code = PLUS_EXPR;
13164 break;
13166 case NEGATE_EXPR:
13167 operator_code = MINUS_EXPR;
13168 break;
13170 case PREINCREMENT_EXPR:
13171 operator_code = POSTINCREMENT_EXPR;
13172 break;
13174 case PREDECREMENT_EXPR:
13175 operator_code = POSTDECREMENT_EXPR;
13176 break;
13178 default:
13179 gcc_unreachable ();
13182 SET_OVERLOADED_OPERATOR_CODE (decl, operator_code);
13184 if ((operator_code == POSTINCREMENT_EXPR
13185 || operator_code == POSTDECREMENT_EXPR)
13186 && ! processing_template_decl
13187 && ! same_type_p (TREE_VALUE (TREE_CHAIN (argtypes)), integer_type_node))
13189 if (methodp)
13190 error ("postfix %qD must take %<int%> as its argument",
13191 decl);
13192 else
13193 error ("postfix %qD must take %<int%> as its second "
13194 "argument", decl);
13195 return false;
13198 else
13200 if (methodp)
13201 error ("%qD must take either zero or one argument", decl);
13202 else
13203 error ("%qD must take either one or two arguments", decl);
13204 return false;
13207 /* More Effective C++ rule 6. */
13208 if (warn_ecpp
13209 && (operator_code == POSTINCREMENT_EXPR
13210 || operator_code == POSTDECREMENT_EXPR
13211 || operator_code == PREINCREMENT_EXPR
13212 || operator_code == PREDECREMENT_EXPR))
13214 tree arg = TREE_VALUE (argtypes);
13215 tree ret = TREE_TYPE (TREE_TYPE (decl));
13216 if (methodp || TREE_CODE (arg) == REFERENCE_TYPE)
13217 arg = TREE_TYPE (arg);
13218 arg = TYPE_MAIN_VARIANT (arg);
13219 if (operator_code == PREINCREMENT_EXPR
13220 || operator_code == PREDECREMENT_EXPR)
13222 if (TREE_CODE (ret) != REFERENCE_TYPE
13223 || !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (ret)),
13224 arg))
13225 warning (OPT_Weffc__, "prefix %qD should return %qT", decl,
13226 build_reference_type (arg));
13228 else
13230 if (!same_type_p (TYPE_MAIN_VARIANT (ret), arg))
13231 warning (OPT_Weffc__, "postfix %qD should return %qT", decl, arg);
13235 else if (unary_op_p (operator_code))
13237 if (arity != 1)
13239 if (methodp)
13240 error ("%qD must take %<void%>", decl);
13241 else
13242 error ("%qD must take exactly one argument", decl);
13243 return false;
13246 else /* if (binary_op_p (operator_code)) */
13248 if (arity != 2)
13250 if (methodp)
13251 error ("%qD must take exactly one argument", decl);
13252 else
13253 error ("%qD must take exactly two arguments", decl);
13254 return false;
13257 /* More Effective C++ rule 7. */
13258 if (warn_ecpp
13259 && (operator_code == TRUTH_ANDIF_EXPR
13260 || operator_code == TRUTH_ORIF_EXPR
13261 || operator_code == COMPOUND_EXPR))
13262 warning (OPT_Weffc__, "user-defined %qD always evaluates both arguments",
13263 decl);
13266 /* Effective C++ rule 23. */
13267 if (warn_ecpp
13268 && arity == 2
13269 && !DECL_ASSIGNMENT_OPERATOR_P (decl)
13270 && (operator_code == PLUS_EXPR
13271 || operator_code == MINUS_EXPR
13272 || operator_code == TRUNC_DIV_EXPR
13273 || operator_code == MULT_EXPR
13274 || operator_code == TRUNC_MOD_EXPR)
13275 && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == REFERENCE_TYPE)
13276 warning (OPT_Weffc__, "%qD should return by value", decl);
13278 /* [over.oper]/8 */
13279 for (; argtypes && argtypes != void_list_node;
13280 argtypes = TREE_CHAIN (argtypes))
13281 if (TREE_PURPOSE (argtypes))
13283 TREE_PURPOSE (argtypes) = NULL_TREE;
13284 if (operator_code == POSTINCREMENT_EXPR
13285 || operator_code == POSTDECREMENT_EXPR)
13287 pedwarn (input_location, OPT_Wpedantic, "%qD cannot have default arguments",
13288 decl);
13290 else
13292 error ("%qD cannot have default arguments", decl);
13293 return false;
13297 return true;
13300 /* Return a string giving the keyword associate with CODE. */
13302 static const char *
13303 tag_name (enum tag_types code)
13305 switch (code)
13307 case record_type:
13308 return "struct";
13309 case class_type:
13310 return "class";
13311 case union_type:
13312 return "union";
13313 case enum_type:
13314 return "enum";
13315 case typename_type:
13316 return "typename";
13317 default:
13318 gcc_unreachable ();
13322 /* Name lookup in an elaborated-type-specifier (after the keyword
13323 indicated by TAG_CODE) has found the TYPE_DECL DECL. If the
13324 elaborated-type-specifier is invalid, issue a diagnostic and return
13325 error_mark_node; otherwise, return the *_TYPE to which it referred.
13326 If ALLOW_TEMPLATE_P is true, TYPE may be a class template. */
13328 tree
13329 check_elaborated_type_specifier (enum tag_types tag_code,
13330 tree decl,
13331 bool allow_template_p)
13333 tree type;
13335 /* In the case of:
13337 struct S { struct S *p; };
13339 name lookup will find the TYPE_DECL for the implicit "S::S"
13340 typedef. Adjust for that here. */
13341 if (DECL_SELF_REFERENCE_P (decl))
13342 decl = TYPE_NAME (TREE_TYPE (decl));
13344 type = TREE_TYPE (decl);
13346 /* Check TEMPLATE_TYPE_PARM first because DECL_IMPLICIT_TYPEDEF_P
13347 is false for this case as well. */
13348 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
13350 error ("using template type parameter %qT after %qs",
13351 type, tag_name (tag_code));
13352 return error_mark_node;
13354 /* Accept template template parameters. */
13355 else if (allow_template_p
13356 && (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM
13357 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM))
13359 /* [dcl.type.elab]
13361 If the identifier resolves to a typedef-name or the
13362 simple-template-id resolves to an alias template
13363 specialization, the elaborated-type-specifier is ill-formed.
13365 In other words, the only legitimate declaration to use in the
13366 elaborated type specifier is the implicit typedef created when
13367 the type is declared. */
13368 else if (!DECL_IMPLICIT_TYPEDEF_P (decl)
13369 && !DECL_SELF_REFERENCE_P (decl)
13370 && tag_code != typename_type)
13372 if (alias_template_specialization_p (type))
13373 error ("using alias template specialization %qT after %qs",
13374 type, tag_name (tag_code));
13375 else
13376 error ("using typedef-name %qD after %qs", decl, tag_name (tag_code));
13377 inform (DECL_SOURCE_LOCATION (decl),
13378 "%qD has a previous declaration here", decl);
13379 return error_mark_node;
13381 else if (TREE_CODE (type) != RECORD_TYPE
13382 && TREE_CODE (type) != UNION_TYPE
13383 && tag_code != enum_type
13384 && tag_code != typename_type)
13386 error ("%qT referred to as %qs", type, tag_name (tag_code));
13387 inform (location_of (type), "%qT has a previous declaration here", type);
13388 return error_mark_node;
13390 else if (TREE_CODE (type) != ENUMERAL_TYPE
13391 && tag_code == enum_type)
13393 error ("%qT referred to as enum", type);
13394 inform (location_of (type), "%qT has a previous declaration here", type);
13395 return error_mark_node;
13397 else if (!allow_template_p
13398 && TREE_CODE (type) == RECORD_TYPE
13399 && CLASSTYPE_IS_TEMPLATE (type))
13401 /* If a class template appears as elaborated type specifier
13402 without a template header such as:
13404 template <class T> class C {};
13405 void f(class C); // No template header here
13407 then the required template argument is missing. */
13408 error ("template argument required for %<%s %T%>",
13409 tag_name (tag_code),
13410 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)));
13411 return error_mark_node;
13414 return type;
13417 /* Lookup NAME in elaborate type specifier in scope according to
13418 SCOPE and issue diagnostics if necessary.
13419 Return *_TYPE node upon success, NULL_TREE when the NAME is not
13420 found, and ERROR_MARK_NODE for type error. */
13422 static tree
13423 lookup_and_check_tag (enum tag_types tag_code, tree name,
13424 tag_scope scope, bool template_header_p)
13426 tree t;
13427 tree decl;
13428 if (scope == ts_global)
13430 /* First try ordinary name lookup, ignoring hidden class name
13431 injected via friend declaration. */
13432 decl = lookup_name_prefer_type (name, 2);
13433 decl = strip_using_decl (decl);
13434 /* If that fails, the name will be placed in the smallest
13435 non-class, non-function-prototype scope according to 3.3.1/5.
13436 We may already have a hidden name declared as friend in this
13437 scope. So lookup again but not ignoring hidden names.
13438 If we find one, that name will be made visible rather than
13439 creating a new tag. */
13440 if (!decl)
13441 decl = lookup_type_scope (name, ts_within_enclosing_non_class);
13443 else
13444 decl = lookup_type_scope (name, scope);
13446 if (decl
13447 && (DECL_CLASS_TEMPLATE_P (decl)
13448 /* If scope is ts_current we're defining a class, so ignore a
13449 template template parameter. */
13450 || (scope != ts_current
13451 && DECL_TEMPLATE_TEMPLATE_PARM_P (decl))))
13452 decl = DECL_TEMPLATE_RESULT (decl);
13454 if (decl && TREE_CODE (decl) == TYPE_DECL)
13456 /* Look for invalid nested type:
13457 class C {
13458 class C {};
13459 }; */
13460 if (scope == ts_current && DECL_SELF_REFERENCE_P (decl))
13462 error ("%qD has the same name as the class in which it is "
13463 "declared",
13464 decl);
13465 return error_mark_node;
13468 /* Two cases we need to consider when deciding if a class
13469 template is allowed as an elaborated type specifier:
13470 1. It is a self reference to its own class.
13471 2. It comes with a template header.
13473 For example:
13475 template <class T> class C {
13476 class C *c1; // DECL_SELF_REFERENCE_P is true
13477 class D;
13479 template <class U> class C; // template_header_p is true
13480 template <class T> class C<T>::D {
13481 class C *c2; // DECL_SELF_REFERENCE_P is true
13482 }; */
13484 t = check_elaborated_type_specifier (tag_code,
13485 decl,
13486 template_header_p
13487 | DECL_SELF_REFERENCE_P (decl));
13488 if (template_header_p && t && CLASS_TYPE_P (t)
13489 && (!CLASSTYPE_TEMPLATE_INFO (t)
13490 || (!PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)))))
13492 error ("%qT is not a template", t);
13493 inform (location_of (t), "previous declaration here");
13494 if (TYPE_CLASS_SCOPE_P (t)
13495 && CLASSTYPE_TEMPLATE_INFO (TYPE_CONTEXT (t)))
13496 inform (input_location,
13497 "perhaps you want to explicitly add %<%T::%>",
13498 TYPE_CONTEXT (t));
13499 t = error_mark_node;
13502 return t;
13504 else if (decl && TREE_CODE (decl) == TREE_LIST)
13506 error ("reference to %qD is ambiguous", name);
13507 print_candidates (decl);
13508 return error_mark_node;
13510 else
13511 return NULL_TREE;
13514 /* Get the struct, enum or union (TAG_CODE says which) with tag NAME.
13515 Define the tag as a forward-reference if it is not defined.
13517 If a declaration is given, process it here, and report an error if
13518 multiple declarations are not identical.
13520 SCOPE is TS_CURRENT when this is also a definition. Only look in
13521 the current frame for the name (since C++ allows new names in any
13522 scope.) It is TS_WITHIN_ENCLOSING_NON_CLASS if this is a friend
13523 declaration. Only look beginning from the current scope outward up
13524 till the nearest non-class scope. Otherwise it is TS_GLOBAL.
13526 TEMPLATE_HEADER_P is true when this declaration is preceded by
13527 a set of template parameters. */
13529 static tree
13530 xref_tag_1 (enum tag_types tag_code, tree name,
13531 tag_scope scope, bool template_header_p)
13533 enum tree_code code;
13534 tree context = NULL_TREE;
13536 gcc_assert (identifier_p (name));
13538 switch (tag_code)
13540 case record_type:
13541 case class_type:
13542 code = RECORD_TYPE;
13543 break;
13544 case union_type:
13545 code = UNION_TYPE;
13546 break;
13547 case enum_type:
13548 code = ENUMERAL_TYPE;
13549 break;
13550 default:
13551 gcc_unreachable ();
13554 /* In case of anonymous name, xref_tag is only called to
13555 make type node and push name. Name lookup is not required. */
13556 tree t = NULL_TREE;
13557 if (scope != ts_lambda && !anon_aggrname_p (name))
13558 t = lookup_and_check_tag (tag_code, name, scope, template_header_p);
13560 if (t == error_mark_node)
13561 return error_mark_node;
13563 if (scope != ts_current && t && current_class_type
13564 && template_class_depth (current_class_type)
13565 && template_header_p)
13567 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
13568 return t;
13570 /* Since SCOPE is not TS_CURRENT, we are not looking at a
13571 definition of this tag. Since, in addition, we are currently
13572 processing a (member) template declaration of a template
13573 class, we must be very careful; consider:
13575 template <class X>
13576 struct S1
13578 template <class U>
13579 struct S2
13580 { template <class V>
13581 friend struct S1; };
13583 Here, the S2::S1 declaration should not be confused with the
13584 outer declaration. In particular, the inner version should
13585 have a template parameter of level 2, not level 1. This
13586 would be particularly important if the member declaration
13587 were instead:
13589 template <class V = U> friend struct S1;
13591 say, when we should tsubst into `U' when instantiating
13592 S2. On the other hand, when presented with:
13594 template <class T>
13595 struct S1 {
13596 template <class U>
13597 struct S2 {};
13598 template <class U>
13599 friend struct S2;
13602 we must find the inner binding eventually. We
13603 accomplish this by making sure that the new type we
13604 create to represent this declaration has the right
13605 TYPE_CONTEXT. */
13606 context = TYPE_CONTEXT (t);
13607 t = NULL_TREE;
13610 if (! t)
13612 /* If no such tag is yet defined, create a forward-reference node
13613 and record it as the "definition".
13614 When a real declaration of this type is found,
13615 the forward-reference will be altered into a real type. */
13616 if (code == ENUMERAL_TYPE)
13618 error ("use of enum %q#D without previous declaration", name);
13619 return error_mark_node;
13621 else
13623 t = make_class_type (code);
13624 TYPE_CONTEXT (t) = context;
13625 if (scope == ts_lambda)
13626 /* Mark it as a lambda type. */
13627 CLASSTYPE_LAMBDA_EXPR (t) = error_mark_node;
13628 t = pushtag (name, t, scope);
13631 else
13633 if (template_header_p && MAYBE_CLASS_TYPE_P (t))
13635 /* Check that we aren't trying to overload a class with different
13636 constraints. */
13637 tree constr = NULL_TREE;
13638 if (current_template_parms)
13640 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
13641 constr = build_constraints (reqs, NULL_TREE);
13643 if (!redeclare_class_template (t, current_template_parms, constr))
13644 return error_mark_node;
13646 else if (!processing_template_decl
13647 && CLASS_TYPE_P (t)
13648 && CLASSTYPE_IS_TEMPLATE (t))
13650 error ("redeclaration of %qT as a non-template", t);
13651 inform (location_of (t), "previous declaration %qD", t);
13652 return error_mark_node;
13655 /* Make injected friend class visible. */
13656 if (scope != ts_within_enclosing_non_class && TYPE_HIDDEN_P (t))
13658 tree decl = TYPE_NAME (t);
13660 DECL_ANTICIPATED (decl) = false;
13661 DECL_FRIEND_P (decl) = false;
13663 if (TYPE_TEMPLATE_INFO (t))
13665 tree tmpl = TYPE_TI_TEMPLATE (t);
13666 DECL_ANTICIPATED (tmpl) = false;
13667 DECL_FRIEND_P (tmpl) = false;
13672 return t;
13675 /* Wrapper for xref_tag_1. */
13677 tree
13678 xref_tag (enum tag_types tag_code, tree name,
13679 tag_scope scope, bool template_header_p)
13681 tree ret;
13682 bool subtime;
13683 subtime = timevar_cond_start (TV_NAME_LOOKUP);
13684 ret = xref_tag_1 (tag_code, name, scope, template_header_p);
13685 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
13686 return ret;
13690 tree
13691 xref_tag_from_type (tree old, tree id, tag_scope scope)
13693 enum tag_types tag_kind;
13695 if (TREE_CODE (old) == RECORD_TYPE)
13696 tag_kind = (CLASSTYPE_DECLARED_CLASS (old) ? class_type : record_type);
13697 else
13698 tag_kind = union_type;
13700 if (id == NULL_TREE)
13701 id = TYPE_IDENTIFIER (old);
13703 return xref_tag (tag_kind, id, scope, false);
13706 /* Create the binfo hierarchy for REF with (possibly NULL) base list
13707 BASE_LIST. For each element on BASE_LIST the TREE_PURPOSE is an
13708 access_* node, and the TREE_VALUE is the type of the base-class.
13709 Non-NULL TREE_TYPE indicates virtual inheritance. */
13711 void
13712 xref_basetypes (tree ref, tree base_list)
13714 tree *basep;
13715 tree binfo, base_binfo;
13716 unsigned max_vbases = 0; /* Maximum direct & indirect virtual bases. */
13717 unsigned max_bases = 0; /* Maximum direct bases. */
13718 unsigned max_dvbases = 0; /* Maximum direct virtual bases. */
13719 int i;
13720 tree default_access;
13721 tree igo_prev; /* Track Inheritance Graph Order. */
13723 if (ref == error_mark_node)
13724 return;
13726 /* The base of a derived class is private by default, all others are
13727 public. */
13728 default_access = (TREE_CODE (ref) == RECORD_TYPE
13729 && CLASSTYPE_DECLARED_CLASS (ref)
13730 ? access_private_node : access_public_node);
13732 /* First, make sure that any templates in base-classes are
13733 instantiated. This ensures that if we call ourselves recursively
13734 we do not get confused about which classes are marked and which
13735 are not. */
13736 basep = &base_list;
13737 while (*basep)
13739 tree basetype = TREE_VALUE (*basep);
13741 /* The dependent_type_p call below should really be dependent_scope_p
13742 so that we give a hard error about using an incomplete type as a
13743 base, but we allow it with a pedwarn for backward
13744 compatibility. */
13745 if (processing_template_decl
13746 && CLASS_TYPE_P (basetype) && TYPE_BEING_DEFINED (basetype))
13747 cxx_incomplete_type_diagnostic (NULL_TREE, basetype, DK_PEDWARN);
13748 if (!dependent_type_p (basetype)
13749 && !complete_type_or_else (basetype, NULL))
13750 /* An incomplete type. Remove it from the list. */
13751 *basep = TREE_CHAIN (*basep);
13752 else
13754 max_bases++;
13755 if (TREE_TYPE (*basep))
13756 max_dvbases++;
13757 if (CLASS_TYPE_P (basetype))
13758 max_vbases += vec_safe_length (CLASSTYPE_VBASECLASSES (basetype));
13759 basep = &TREE_CHAIN (*basep);
13762 max_vbases += max_dvbases;
13764 TYPE_MARKED_P (ref) = 1;
13766 /* The binfo slot should be empty, unless this is an (ill-formed)
13767 redefinition. */
13768 gcc_assert (!TYPE_BINFO (ref) || TYPE_SIZE (ref));
13770 gcc_assert (TYPE_MAIN_VARIANT (ref) == ref);
13772 binfo = make_tree_binfo (max_bases);
13774 TYPE_BINFO (ref) = binfo;
13775 BINFO_OFFSET (binfo) = size_zero_node;
13776 BINFO_TYPE (binfo) = ref;
13778 /* Apply base-class info set up to the variants of this type. */
13779 fixup_type_variants (ref);
13781 if (max_bases)
13783 vec_alloc (BINFO_BASE_ACCESSES (binfo), max_bases);
13784 /* A C++98 POD cannot have base classes. */
13785 CLASSTYPE_NON_LAYOUT_POD_P (ref) = true;
13787 if (TREE_CODE (ref) == UNION_TYPE)
13788 error ("derived union %qT invalid", ref);
13791 if (max_bases > 1)
13792 warning (OPT_Wmultiple_inheritance,
13793 "%qT defined with multiple direct bases", ref);
13795 if (max_vbases)
13797 /* An aggregate can't have virtual base classes. */
13798 CLASSTYPE_NON_AGGREGATE (ref) = true;
13800 vec_alloc (CLASSTYPE_VBASECLASSES (ref), max_vbases);
13802 if (max_dvbases)
13803 warning (OPT_Wvirtual_inheritance,
13804 "%qT defined with direct virtual base", ref);
13807 for (igo_prev = binfo; base_list; base_list = TREE_CHAIN (base_list))
13809 tree access = TREE_PURPOSE (base_list);
13810 int via_virtual = TREE_TYPE (base_list) != NULL_TREE;
13811 tree basetype = TREE_VALUE (base_list);
13813 if (access == access_default_node)
13814 access = default_access;
13816 /* Before C++17, an aggregate cannot have base classes. In C++17, an
13817 aggregate can't have virtual, private, or protected base classes. */
13818 if (cxx_dialect < cxx17
13819 || access != access_public_node
13820 || via_virtual)
13821 CLASSTYPE_NON_AGGREGATE (ref) = true;
13823 if (PACK_EXPANSION_P (basetype))
13824 basetype = PACK_EXPANSION_PATTERN (basetype);
13825 if (TREE_CODE (basetype) == TYPE_DECL)
13826 basetype = TREE_TYPE (basetype);
13827 if (!MAYBE_CLASS_TYPE_P (basetype) || TREE_CODE (basetype) == UNION_TYPE)
13829 error ("base type %qT fails to be a struct or class type",
13830 basetype);
13831 goto dropped_base;
13834 base_binfo = NULL_TREE;
13835 if (CLASS_TYPE_P (basetype) && !dependent_scope_p (basetype))
13837 base_binfo = TYPE_BINFO (basetype);
13838 /* The original basetype could have been a typedef'd type. */
13839 basetype = BINFO_TYPE (base_binfo);
13841 /* Inherit flags from the base. */
13842 TYPE_HAS_NEW_OPERATOR (ref)
13843 |= TYPE_HAS_NEW_OPERATOR (basetype);
13844 TYPE_HAS_ARRAY_NEW_OPERATOR (ref)
13845 |= TYPE_HAS_ARRAY_NEW_OPERATOR (basetype);
13846 TYPE_GETS_DELETE (ref) |= TYPE_GETS_DELETE (basetype);
13847 TYPE_HAS_CONVERSION (ref) |= TYPE_HAS_CONVERSION (basetype);
13848 CLASSTYPE_DIAMOND_SHAPED_P (ref)
13849 |= CLASSTYPE_DIAMOND_SHAPED_P (basetype);
13850 CLASSTYPE_REPEATED_BASE_P (ref)
13851 |= CLASSTYPE_REPEATED_BASE_P (basetype);
13854 /* We must do this test after we've seen through a typedef
13855 type. */
13856 if (TYPE_MARKED_P (basetype))
13858 if (basetype == ref)
13859 error ("recursive type %qT undefined", basetype);
13860 else
13861 error ("duplicate base type %qT invalid", basetype);
13862 goto dropped_base;
13865 if (PACK_EXPANSION_P (TREE_VALUE (base_list)))
13866 /* Regenerate the pack expansion for the bases. */
13867 basetype = make_pack_expansion (basetype);
13869 TYPE_MARKED_P (basetype) = 1;
13871 base_binfo = copy_binfo (base_binfo, basetype, ref,
13872 &igo_prev, via_virtual);
13873 if (!BINFO_INHERITANCE_CHAIN (base_binfo))
13874 BINFO_INHERITANCE_CHAIN (base_binfo) = binfo;
13876 BINFO_BASE_APPEND (binfo, base_binfo);
13877 BINFO_BASE_ACCESS_APPEND (binfo, access);
13878 continue;
13880 dropped_base:
13881 /* Update max_vbases to reflect the reality that we are dropping
13882 this base: if it reaches zero we want to undo the vec_alloc
13883 above to avoid inconsistencies during error-recovery: eg, in
13884 build_special_member_call, CLASSTYPE_VBASECLASSES non null
13885 and vtt null (c++/27952). */
13886 if (via_virtual)
13887 max_vbases--;
13888 if (CLASS_TYPE_P (basetype))
13889 max_vbases
13890 -= vec_safe_length (CLASSTYPE_VBASECLASSES (basetype));
13893 if (CLASSTYPE_VBASECLASSES (ref)
13894 && max_vbases == 0)
13895 vec_free (CLASSTYPE_VBASECLASSES (ref));
13897 if (vec_safe_length (CLASSTYPE_VBASECLASSES (ref)) < max_vbases)
13898 /* If we didn't get max_vbases vbases, we must have shared at
13899 least one of them, and are therefore diamond shaped. */
13900 CLASSTYPE_DIAMOND_SHAPED_P (ref) = 1;
13902 /* Unmark all the types. */
13903 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
13904 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
13905 TYPE_MARKED_P (ref) = 0;
13907 /* Now see if we have a repeated base type. */
13908 if (!CLASSTYPE_REPEATED_BASE_P (ref))
13910 for (base_binfo = binfo; base_binfo;
13911 base_binfo = TREE_CHAIN (base_binfo))
13913 if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
13915 CLASSTYPE_REPEATED_BASE_P (ref) = 1;
13916 break;
13918 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 1;
13920 for (base_binfo = binfo; base_binfo;
13921 base_binfo = TREE_CHAIN (base_binfo))
13922 if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
13923 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
13924 else
13925 break;
13930 /* Copies the enum-related properties from type SRC to type DST.
13931 Used with the underlying type of an enum and the enum itself. */
13932 static void
13933 copy_type_enum (tree dst, tree src)
13935 tree t;
13936 for (t = dst; t; t = TYPE_NEXT_VARIANT (t))
13938 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (src);
13939 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (src);
13940 TYPE_SIZE (t) = TYPE_SIZE (src);
13941 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (src);
13942 SET_TYPE_MODE (dst, TYPE_MODE (src));
13943 TYPE_PRECISION (t) = TYPE_PRECISION (src);
13944 unsigned valign = TYPE_ALIGN (src);
13945 if (TYPE_USER_ALIGN (t))
13946 valign = MAX (valign, TYPE_ALIGN (t));
13947 else
13948 TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (src);
13949 SET_TYPE_ALIGN (t, valign);
13950 TYPE_UNSIGNED (t) = TYPE_UNSIGNED (src);
13954 /* Begin compiling the definition of an enumeration type.
13955 NAME is its name,
13957 if ENUMTYPE is not NULL_TREE then the type has alredy been found.
13959 UNDERLYING_TYPE is the type that will be used as the storage for
13960 the enumeration type. This should be NULL_TREE if no storage type
13961 was specified.
13963 ATTRIBUTES are any attributes specified after the enum-key.
13965 SCOPED_ENUM_P is true if this is a scoped enumeration type.
13967 if IS_NEW is not NULL, gets TRUE iff a new type is created.
13969 Returns the type object, as yet incomplete.
13970 Also records info about it so that build_enumerator
13971 may be used to declare the individual values as they are read. */
13973 tree
13974 start_enum (tree name, tree enumtype, tree underlying_type,
13975 tree attributes, bool scoped_enum_p, bool *is_new)
13977 tree prevtype = NULL_TREE;
13978 gcc_assert (identifier_p (name));
13980 if (is_new)
13981 *is_new = false;
13982 /* [C++0x dcl.enum]p5:
13984 If not explicitly specified, the underlying type of a scoped
13985 enumeration type is int. */
13986 if (!underlying_type && scoped_enum_p)
13987 underlying_type = integer_type_node;
13989 if (underlying_type)
13990 underlying_type = cv_unqualified (underlying_type);
13992 /* If this is the real definition for a previous forward reference,
13993 fill in the contents in the same object that used to be the
13994 forward reference. */
13995 if (!enumtype)
13996 enumtype = lookup_and_check_tag (enum_type, name,
13997 /*tag_scope=*/ts_current,
13998 /*template_header_p=*/false);
14000 /* In case of a template_decl, the only check that should be deferred
14001 to instantiation time is the comparison of underlying types. */
14002 if (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE)
14004 if (scoped_enum_p != SCOPED_ENUM_P (enumtype))
14006 error_at (input_location, "scoped/unscoped mismatch "
14007 "in enum %q#T", enumtype);
14008 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
14009 "previous definition here");
14010 enumtype = error_mark_node;
14012 else if (ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) != !! underlying_type)
14014 error_at (input_location, "underlying type mismatch "
14015 "in enum %q#T", enumtype);
14016 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
14017 "previous definition here");
14018 enumtype = error_mark_node;
14020 else if (underlying_type && ENUM_UNDERLYING_TYPE (enumtype)
14021 && !dependent_type_p (underlying_type)
14022 && !dependent_type_p (ENUM_UNDERLYING_TYPE (enumtype))
14023 && !same_type_p (underlying_type,
14024 ENUM_UNDERLYING_TYPE (enumtype)))
14026 error_at (input_location, "different underlying type "
14027 "in enum %q#T", enumtype);
14028 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
14029 "previous definition here");
14030 underlying_type = NULL_TREE;
14034 if (!enumtype || TREE_CODE (enumtype) != ENUMERAL_TYPE
14035 || processing_template_decl)
14037 /* In case of error, make a dummy enum to allow parsing to
14038 continue. */
14039 if (enumtype == error_mark_node)
14041 name = make_anon_name ();
14042 enumtype = NULL_TREE;
14045 /* enumtype may be an ENUMERAL_TYPE if this is a redefinition
14046 of an opaque enum, or an opaque enum of an already defined
14047 enumeration (C++0x only).
14048 In any other case, it'll be NULL_TREE. */
14049 if (!enumtype)
14051 if (is_new)
14052 *is_new = true;
14054 prevtype = enumtype;
14056 /* Do not push the decl more than once, unless we need to
14057 compare underlying types at instantiation time */
14058 if (!enumtype
14059 || TREE_CODE (enumtype) != ENUMERAL_TYPE
14060 || (underlying_type
14061 && dependent_type_p (underlying_type))
14062 || (ENUM_UNDERLYING_TYPE (enumtype)
14063 && dependent_type_p (ENUM_UNDERLYING_TYPE (enumtype))))
14065 enumtype = cxx_make_type (ENUMERAL_TYPE);
14066 enumtype = pushtag (name, enumtype, /*tag_scope=*/ts_current);
14068 /* std::byte aliases anything. */
14069 if (enumtype != error_mark_node
14070 && TYPE_CONTEXT (enumtype) == std_node
14071 && !strcmp ("byte", TYPE_NAME_STRING (enumtype)))
14072 TYPE_ALIAS_SET (enumtype) = 0;
14074 else
14075 enumtype = xref_tag (enum_type, name, /*tag_scope=*/ts_current,
14076 false);
14078 if (enumtype == error_mark_node)
14079 return error_mark_node;
14081 /* The enum is considered opaque until the opening '{' of the
14082 enumerator list. */
14083 SET_OPAQUE_ENUM_P (enumtype, true);
14084 ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) = !! underlying_type;
14087 SET_SCOPED_ENUM_P (enumtype, scoped_enum_p);
14089 cplus_decl_attributes (&enumtype, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
14091 if (underlying_type)
14093 if (ENUM_UNDERLYING_TYPE (enumtype))
14094 /* We already checked that it matches, don't change it to a different
14095 typedef variant. */;
14096 else if (CP_INTEGRAL_TYPE_P (underlying_type))
14098 copy_type_enum (enumtype, underlying_type);
14099 ENUM_UNDERLYING_TYPE (enumtype) = underlying_type;
14101 else if (dependent_type_p (underlying_type))
14102 ENUM_UNDERLYING_TYPE (enumtype) = underlying_type;
14103 else
14104 error ("underlying type %qT of %qT must be an integral type",
14105 underlying_type, enumtype);
14108 /* If into a template class, the returned enum is always the first
14109 declaration (opaque or not) seen. This way all the references to
14110 this type will be to the same declaration. The following ones are used
14111 only to check for definition errors. */
14112 if (prevtype && processing_template_decl)
14113 return prevtype;
14114 else
14115 return enumtype;
14118 /* After processing and defining all the values of an enumeration type,
14119 install their decls in the enumeration type.
14120 ENUMTYPE is the type object. */
14122 void
14123 finish_enum_value_list (tree enumtype)
14125 tree values;
14126 tree underlying_type;
14127 tree decl;
14128 tree value;
14129 tree minnode, maxnode;
14130 tree t;
14132 bool fixed_underlying_type_p
14133 = ENUM_UNDERLYING_TYPE (enumtype) != NULL_TREE;
14135 /* We built up the VALUES in reverse order. */
14136 TYPE_VALUES (enumtype) = nreverse (TYPE_VALUES (enumtype));
14138 /* For an enum defined in a template, just set the type of the values;
14139 all further processing is postponed until the template is
14140 instantiated. We need to set the type so that tsubst of a CONST_DECL
14141 works. */
14142 if (processing_template_decl)
14144 for (values = TYPE_VALUES (enumtype);
14145 values;
14146 values = TREE_CHAIN (values))
14147 TREE_TYPE (TREE_VALUE (values)) = enumtype;
14148 return;
14151 /* Determine the minimum and maximum values of the enumerators. */
14152 if (TYPE_VALUES (enumtype))
14154 minnode = maxnode = NULL_TREE;
14156 for (values = TYPE_VALUES (enumtype);
14157 values;
14158 values = TREE_CHAIN (values))
14160 decl = TREE_VALUE (values);
14162 /* [dcl.enum]: Following the closing brace of an enum-specifier,
14163 each enumerator has the type of its enumeration. Prior to the
14164 closing brace, the type of each enumerator is the type of its
14165 initializing value. */
14166 TREE_TYPE (decl) = enumtype;
14168 /* Update the minimum and maximum values, if appropriate. */
14169 value = DECL_INITIAL (decl);
14170 if (value == error_mark_node)
14171 value = integer_zero_node;
14172 /* Figure out what the minimum and maximum values of the
14173 enumerators are. */
14174 if (!minnode)
14175 minnode = maxnode = value;
14176 else if (tree_int_cst_lt (maxnode, value))
14177 maxnode = value;
14178 else if (tree_int_cst_lt (value, minnode))
14179 minnode = value;
14182 else
14183 /* [dcl.enum]
14185 If the enumerator-list is empty, the underlying type is as if
14186 the enumeration had a single enumerator with value 0. */
14187 minnode = maxnode = integer_zero_node;
14189 if (!fixed_underlying_type_p)
14191 /* Compute the number of bits require to represent all values of the
14192 enumeration. We must do this before the type of MINNODE and
14193 MAXNODE are transformed, since tree_int_cst_min_precision relies
14194 on the TREE_TYPE of the value it is passed. */
14195 signop sgn = tree_int_cst_sgn (minnode) >= 0 ? UNSIGNED : SIGNED;
14196 int lowprec = tree_int_cst_min_precision (minnode, sgn);
14197 int highprec = tree_int_cst_min_precision (maxnode, sgn);
14198 int precision = MAX (lowprec, highprec);
14199 unsigned int itk;
14200 bool use_short_enum;
14202 /* Determine the underlying type of the enumeration.
14204 [dcl.enum]
14206 The underlying type of an enumeration is an integral type that
14207 can represent all the enumerator values defined in the
14208 enumeration. It is implementation-defined which integral type is
14209 used as the underlying type for an enumeration except that the
14210 underlying type shall not be larger than int unless the value of
14211 an enumerator cannot fit in an int or unsigned int.
14213 We use "int" or an "unsigned int" as the underlying type, even if
14214 a smaller integral type would work, unless the user has
14215 explicitly requested that we use the smallest possible type. The
14216 user can request that for all enumerations with a command line
14217 flag, or for just one enumeration with an attribute. */
14219 use_short_enum = flag_short_enums
14220 || lookup_attribute ("packed", TYPE_ATTRIBUTES (enumtype));
14222 /* If the precision of the type was specified with an attribute and it
14223 was too small, give an error. Otherwise, use it. */
14224 if (TYPE_PRECISION (enumtype))
14226 if (precision > TYPE_PRECISION (enumtype))
14227 error ("specified mode too small for enumeral values");
14228 else
14230 use_short_enum = true;
14231 precision = TYPE_PRECISION (enumtype);
14235 for (itk = (use_short_enum ? itk_char : itk_int);
14236 itk != itk_none;
14237 itk++)
14239 underlying_type = integer_types[itk];
14240 if (underlying_type != NULL_TREE
14241 && TYPE_PRECISION (underlying_type) >= precision
14242 && TYPE_SIGN (underlying_type) == sgn)
14243 break;
14245 if (itk == itk_none)
14247 /* DR 377
14249 IF no integral type can represent all the enumerator values, the
14250 enumeration is ill-formed. */
14251 error ("no integral type can represent all of the enumerator values "
14252 "for %qT", enumtype);
14253 precision = TYPE_PRECISION (long_long_integer_type_node);
14254 underlying_type = integer_types[itk_unsigned_long_long];
14257 /* [dcl.enum]
14259 The value of sizeof() applied to an enumeration type, an object
14260 of an enumeration type, or an enumerator, is the value of sizeof()
14261 applied to the underlying type. */
14262 copy_type_enum (enumtype, underlying_type);
14264 /* Compute the minimum and maximum values for the type.
14266 [dcl.enum]
14268 For an enumeration where emin is the smallest enumerator and emax
14269 is the largest, the values of the enumeration are the values of the
14270 underlying type in the range bmin to bmax, where bmin and bmax are,
14271 respectively, the smallest and largest values of the smallest bit-
14272 field that can store emin and emax. */
14274 /* The middle-end currently assumes that types with TYPE_PRECISION
14275 narrower than their underlying type are suitably zero or sign
14276 extended to fill their mode. Similarly, it assumes that the front
14277 end assures that a value of a particular type must be within
14278 TYPE_MIN_VALUE and TYPE_MAX_VALUE.
14280 We used to set these fields based on bmin and bmax, but that led
14281 to invalid assumptions like optimizing away bounds checking. So
14282 now we just set the TYPE_PRECISION, TYPE_MIN_VALUE, and
14283 TYPE_MAX_VALUE to the values for the mode above and only restrict
14284 the ENUM_UNDERLYING_TYPE for the benefit of diagnostics. */
14285 ENUM_UNDERLYING_TYPE (enumtype)
14286 = build_distinct_type_copy (underlying_type);
14287 TYPE_PRECISION (ENUM_UNDERLYING_TYPE (enumtype)) = precision;
14288 set_min_and_max_values_for_integral_type
14289 (ENUM_UNDERLYING_TYPE (enumtype), precision, sgn);
14291 /* If -fstrict-enums, still constrain TYPE_MIN/MAX_VALUE. */
14292 if (flag_strict_enums)
14293 set_min_and_max_values_for_integral_type (enumtype, precision, sgn);
14295 else
14296 underlying_type = ENUM_UNDERLYING_TYPE (enumtype);
14298 /* Convert each of the enumerators to the type of the underlying
14299 type of the enumeration. */
14300 for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values))
14302 location_t saved_location;
14304 decl = TREE_VALUE (values);
14305 saved_location = input_location;
14306 input_location = DECL_SOURCE_LOCATION (decl);
14307 if (fixed_underlying_type_p)
14308 /* If the enumeration type has a fixed underlying type, we
14309 already checked all of the enumerator values. */
14310 value = DECL_INITIAL (decl);
14311 else
14312 value = perform_implicit_conversion (underlying_type,
14313 DECL_INITIAL (decl),
14314 tf_warning_or_error);
14315 input_location = saved_location;
14317 /* Do not clobber shared ints. */
14318 if (value != error_mark_node)
14320 value = copy_node (value);
14322 TREE_TYPE (value) = enumtype;
14324 DECL_INITIAL (decl) = value;
14327 /* Fix up all variant types of this enum type. */
14328 for (t = TYPE_MAIN_VARIANT (enumtype); t; t = TYPE_NEXT_VARIANT (t))
14329 TYPE_VALUES (t) = TYPE_VALUES (enumtype);
14331 if (at_class_scope_p ()
14332 && COMPLETE_TYPE_P (current_class_type)
14333 && UNSCOPED_ENUM_P (enumtype))
14335 insert_late_enum_def_bindings (current_class_type, enumtype);
14336 /* TYPE_FIELDS needs fixup. */
14337 fixup_type_variants (current_class_type);
14340 /* Finish debugging output for this type. */
14341 rest_of_type_compilation (enumtype, namespace_bindings_p ());
14343 /* Each enumerator now has the type of its enumeration. Clear the cache
14344 so that this change in types doesn't confuse us later on. */
14345 clear_cv_and_fold_caches ();
14348 /* Finishes the enum type. This is called only the first time an
14349 enumeration is seen, be it opaque or odinary.
14350 ENUMTYPE is the type object. */
14352 void
14353 finish_enum (tree enumtype)
14355 if (processing_template_decl)
14357 if (at_function_scope_p ())
14358 add_stmt (build_min (TAG_DEFN, enumtype));
14359 return;
14362 /* If this is a forward declaration, there should not be any variants,
14363 though we can get a variant in the middle of an enum-specifier with
14364 wacky code like 'enum E { e = sizeof(const E*) };' */
14365 gcc_assert (enumtype == TYPE_MAIN_VARIANT (enumtype)
14366 && (TYPE_VALUES (enumtype)
14367 || !TYPE_NEXT_VARIANT (enumtype)));
14370 /* Build and install a CONST_DECL for an enumeration constant of the
14371 enumeration type ENUMTYPE whose NAME and VALUE (if any) are provided.
14372 Apply ATTRIBUTES if available. LOC is the location of NAME.
14373 Assignment of sequential values by default is handled here. */
14375 void
14376 build_enumerator (tree name, tree value, tree enumtype, tree attributes,
14377 location_t loc)
14379 tree decl;
14380 tree context;
14381 tree type;
14383 /* scalar_constant_value will pull out this expression, so make sure
14384 it's folded as appropriate. */
14385 if (processing_template_decl)
14386 value = fold_non_dependent_expr (value);
14388 /* If the VALUE was erroneous, pretend it wasn't there; that will
14389 result in the enum being assigned the next value in sequence. */
14390 if (value == error_mark_node)
14391 value = NULL_TREE;
14393 /* Remove no-op casts from the value. */
14394 if (value)
14395 STRIP_TYPE_NOPS (value);
14397 if (! processing_template_decl)
14399 /* Validate and default VALUE. */
14400 if (value != NULL_TREE)
14402 if (!ENUM_UNDERLYING_TYPE (enumtype))
14404 tree tmp_value = build_expr_type_conversion (WANT_INT | WANT_ENUM,
14405 value, true);
14406 if (tmp_value)
14407 value = tmp_value;
14409 else if (! INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P
14410 (TREE_TYPE (value)))
14411 value = perform_implicit_conversion_flags
14412 (ENUM_UNDERLYING_TYPE (enumtype), value, tf_warning_or_error,
14413 LOOKUP_IMPLICIT | LOOKUP_NO_NARROWING);
14415 if (value == error_mark_node)
14416 value = NULL_TREE;
14418 if (value != NULL_TREE)
14420 if (! INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P
14421 (TREE_TYPE (value)))
14423 error ("enumerator value for %qD must have integral or "
14424 "unscoped enumeration type", name);
14425 value = NULL_TREE;
14427 else
14429 value = cxx_constant_value (value);
14431 if (TREE_CODE (value) != INTEGER_CST)
14433 error ("enumerator value for %qD is not an integer "
14434 "constant", name);
14435 value = NULL_TREE;
14441 /* Default based on previous value. */
14442 if (value == NULL_TREE)
14444 if (TYPE_VALUES (enumtype))
14446 tree prev_value;
14447 bool overflowed;
14449 /* C++03 7.2/4: If no initializer is specified for the first
14450 enumerator, the type is an unspecified integral
14451 type. Otherwise the type is the same as the type of the
14452 initializing value of the preceding enumerator unless the
14453 incremented value is not representable in that type, in
14454 which case the type is an unspecified integral type
14455 sufficient to contain the incremented value. */
14456 prev_value = DECL_INITIAL (TREE_VALUE (TYPE_VALUES (enumtype)));
14457 if (error_operand_p (prev_value))
14458 value = error_mark_node;
14459 else
14461 tree type = TREE_TYPE (prev_value);
14462 signop sgn = TYPE_SIGN (type);
14463 widest_int wi = wi::add (wi::to_widest (prev_value), 1, sgn,
14464 &overflowed);
14465 if (!overflowed)
14467 bool pos = !wi::neg_p (wi, sgn);
14468 if (!wi::fits_to_tree_p (wi, type))
14470 unsigned int itk;
14471 for (itk = itk_int; itk != itk_none; itk++)
14473 type = integer_types[itk];
14474 if (type != NULL_TREE
14475 && (pos || !TYPE_UNSIGNED (type))
14476 && wi::fits_to_tree_p (wi, type))
14477 break;
14479 if (type && cxx_dialect < cxx11
14480 && itk > itk_unsigned_long)
14481 pedwarn (input_location, OPT_Wlong_long,
14482 pos ? G_("\
14483 incremented enumerator value is too large for %<unsigned long%>") : G_("\
14484 incremented enumerator value is too large for %<long%>"));
14486 if (type == NULL_TREE)
14487 overflowed = true;
14488 else
14489 value = wide_int_to_tree (type, wi);
14492 if (overflowed)
14494 error ("overflow in enumeration values at %qD", name);
14495 value = error_mark_node;
14499 else
14500 value = integer_zero_node;
14503 /* Remove no-op casts from the value. */
14504 STRIP_TYPE_NOPS (value);
14506 /* If the underlying type of the enum is fixed, check whether
14507 the enumerator values fits in the underlying type. If it
14508 does not fit, the program is ill-formed [C++0x dcl.enum]. */
14509 if (ENUM_UNDERLYING_TYPE (enumtype)
14510 && value
14511 && TREE_CODE (value) == INTEGER_CST)
14513 if (!int_fits_type_p (value, ENUM_UNDERLYING_TYPE (enumtype)))
14514 error ("enumerator value %qE is outside the range of underlying "
14515 "type %qT", value, ENUM_UNDERLYING_TYPE (enumtype));
14517 /* Convert the value to the appropriate type. */
14518 value = fold_convert (ENUM_UNDERLYING_TYPE (enumtype), value);
14522 /* C++ associates enums with global, function, or class declarations. */
14523 context = current_scope ();
14525 /* Build the actual enumeration constant. Note that the enumeration
14526 constants have the underlying type of the enum (if it is fixed)
14527 or the type of their initializer (if the underlying type of the
14528 enum is not fixed):
14530 [ C++0x dcl.enum ]
14532 If the underlying type is fixed, the type of each enumerator
14533 prior to the closing brace is the underlying type; if the
14534 initializing value of an enumerator cannot be represented by
14535 the underlying type, the program is ill-formed. If the
14536 underlying type is not fixed, the type of each enumerator is
14537 the type of its initializing value.
14539 If the underlying type is not fixed, it will be computed by
14540 finish_enum and we will reset the type of this enumerator. Of
14541 course, if we're processing a template, there may be no value. */
14542 type = value ? TREE_TYPE (value) : NULL_TREE;
14544 decl = build_decl (loc, CONST_DECL, name, type);
14546 DECL_CONTEXT (decl) = enumtype;
14547 TREE_CONSTANT (decl) = 1;
14548 TREE_READONLY (decl) = 1;
14549 DECL_INITIAL (decl) = value;
14551 if (attributes)
14552 cplus_decl_attributes (&decl, attributes, 0);
14554 if (context && context == current_class_type && !SCOPED_ENUM_P (enumtype))
14556 /* In something like `struct S { enum E { i = 7 }; };' we put `i'
14557 on the TYPE_FIELDS list for `S'. (That's so that you can say
14558 things like `S::i' later.) */
14560 /* The enumerator may be getting declared outside of its enclosing
14561 class, like so:
14563 class S { public: enum E : int; }; enum S::E : int { i = 7; };
14565 For which case we need to make sure that the access of `S::i'
14566 matches the access of `S::E'. */
14567 tree saved_cas = current_access_specifier;
14568 if (TREE_PRIVATE (TYPE_NAME (enumtype)))
14569 current_access_specifier = access_private_node;
14570 else if (TREE_PROTECTED (TYPE_NAME (enumtype)))
14571 current_access_specifier = access_protected_node;
14572 else
14573 current_access_specifier = access_public_node;
14575 finish_member_declaration (decl);
14577 current_access_specifier = saved_cas;
14579 else
14580 pushdecl (decl);
14582 /* Add this enumeration constant to the list for this type. */
14583 TYPE_VALUES (enumtype) = tree_cons (name, decl, TYPE_VALUES (enumtype));
14586 /* Look for an enumerator with the given NAME within the enumeration
14587 type ENUMTYPE. This routine is used primarily for qualified name
14588 lookup into an enumerator in C++0x, e.g.,
14590 enum class Color { Red, Green, Blue };
14592 Color color = Color::Red;
14594 Returns the value corresponding to the enumerator, or
14595 NULL_TREE if no such enumerator was found. */
14596 tree
14597 lookup_enumerator (tree enumtype, tree name)
14599 tree e;
14600 gcc_assert (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE);
14602 e = purpose_member (name, TYPE_VALUES (enumtype));
14603 return e? TREE_VALUE (e) : NULL_TREE;
14607 /* We're defining DECL. Make sure that its type is OK. */
14609 static void
14610 check_function_type (tree decl, tree current_function_parms)
14612 tree fntype = TREE_TYPE (decl);
14613 tree return_type = complete_type (TREE_TYPE (fntype));
14615 /* In a function definition, arg types must be complete. */
14616 require_complete_types_for_parms (current_function_parms);
14618 if (dependent_type_p (return_type)
14619 || type_uses_auto (return_type))
14620 return;
14621 if (!COMPLETE_OR_VOID_TYPE_P (return_type))
14623 tree args = TYPE_ARG_TYPES (fntype);
14625 error ("return type %q#T is incomplete", return_type);
14627 /* Make it return void instead. */
14628 if (TREE_CODE (fntype) == METHOD_TYPE)
14629 fntype = build_method_type_directly (TREE_TYPE (TREE_VALUE (args)),
14630 void_type_node,
14631 TREE_CHAIN (args));
14632 else
14633 fntype = build_function_type (void_type_node, args);
14634 fntype
14635 = build_exception_variant (fntype,
14636 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl)));
14637 fntype = (cp_build_type_attribute_variant
14638 (fntype, TYPE_ATTRIBUTES (TREE_TYPE (decl))));
14639 TREE_TYPE (decl) = fntype;
14641 else
14643 abstract_virtuals_error (decl, TREE_TYPE (fntype));
14644 maybe_warn_parm_abi (TREE_TYPE (fntype),
14645 DECL_SOURCE_LOCATION (decl));
14649 /* True iff FN is an implicitly-defined default constructor. */
14651 static bool
14652 implicit_default_ctor_p (tree fn)
14654 return (DECL_CONSTRUCTOR_P (fn)
14655 && !user_provided_p (fn)
14656 && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn)));
14659 /* Clobber the contents of *this to let the back end know that the object
14660 storage is dead when we enter the constructor or leave the destructor. */
14662 static tree
14663 build_clobber_this ()
14665 /* Clobbering an empty base is pointless, and harmful if its one byte
14666 TYPE_SIZE overlays real data. */
14667 if (is_empty_class (current_class_type))
14668 return void_node;
14670 /* If we have virtual bases, clobber the whole object, but only if we're in
14671 charge. If we don't have virtual bases, clobber the as-base type so we
14672 don't mess with tail padding. */
14673 bool vbases = CLASSTYPE_VBASECLASSES (current_class_type);
14675 tree ctype = current_class_type;
14676 if (!vbases)
14677 ctype = CLASSTYPE_AS_BASE (ctype);
14679 tree clobber = build_constructor (ctype, NULL);
14680 TREE_THIS_VOLATILE (clobber) = true;
14682 tree thisref = current_class_ref;
14683 if (ctype != current_class_type)
14685 thisref = build_nop (build_reference_type (ctype), current_class_ptr);
14686 thisref = convert_from_reference (thisref);
14689 tree exprstmt = build2 (MODIFY_EXPR, void_type_node, thisref, clobber);
14690 if (vbases)
14691 exprstmt = build_if_in_charge (exprstmt);
14693 return exprstmt;
14696 /* Create the FUNCTION_DECL for a function definition.
14697 DECLSPECS and DECLARATOR are the parts of the declaration;
14698 they describe the function's name and the type it returns,
14699 but twisted together in a fashion that parallels the syntax of C.
14701 FLAGS is a bitwise or of SF_PRE_PARSED (indicating that the
14702 DECLARATOR is really the DECL for the function we are about to
14703 process and that DECLSPECS should be ignored), SF_INCLASS_INLINE
14704 indicating that the function is an inline defined in-class.
14706 This function creates a binding context for the function body
14707 as well as setting up the FUNCTION_DECL in current_function_decl.
14709 For C++, we must first check whether that datum makes any sense.
14710 For example, "class A local_a(1,2);" means that variable local_a
14711 is an aggregate of type A, which should have a constructor
14712 applied to it with the argument list [1, 2].
14714 On entry, DECL_INITIAL (decl1) should be NULL_TREE or error_mark_node,
14715 or may be a BLOCK if the function has been defined previously
14716 in this translation unit. On exit, DECL_INITIAL (decl1) will be
14717 error_mark_node if the function has never been defined, or
14718 a BLOCK if the function has been defined somewhere. */
14720 bool
14721 start_preparsed_function (tree decl1, tree attrs, int flags)
14723 tree ctype = NULL_TREE;
14724 tree fntype;
14725 tree restype;
14726 int doing_friend = 0;
14727 cp_binding_level *bl;
14728 tree current_function_parms;
14729 struct c_fileinfo *finfo
14730 = get_fileinfo (LOCATION_FILE (DECL_SOURCE_LOCATION (decl1)));
14731 bool honor_interface;
14733 /* Sanity check. */
14734 gcc_assert (VOID_TYPE_P (TREE_VALUE (void_list_node)));
14735 gcc_assert (TREE_CHAIN (void_list_node) == NULL_TREE);
14737 fntype = TREE_TYPE (decl1);
14738 if (TREE_CODE (fntype) == METHOD_TYPE)
14739 ctype = TYPE_METHOD_BASETYPE (fntype);
14741 /* ISO C++ 11.4/5. A friend function defined in a class is in
14742 the (lexical) scope of the class in which it is defined. */
14743 if (!ctype && DECL_FRIEND_P (decl1))
14745 ctype = DECL_FRIEND_CONTEXT (decl1);
14747 /* CTYPE could be null here if we're dealing with a template;
14748 for example, `inline friend float foo()' inside a template
14749 will have no CTYPE set. */
14750 if (ctype && TREE_CODE (ctype) != RECORD_TYPE)
14751 ctype = NULL_TREE;
14752 else
14753 doing_friend = 1;
14756 if (DECL_DECLARED_INLINE_P (decl1)
14757 && lookup_attribute ("noinline", attrs))
14758 warning_at (DECL_SOURCE_LOCATION (decl1), 0,
14759 "inline function %qD given attribute noinline", decl1);
14761 /* Handle gnu_inline attribute. */
14762 if (GNU_INLINE_P (decl1))
14764 DECL_EXTERNAL (decl1) = 1;
14765 DECL_NOT_REALLY_EXTERN (decl1) = 0;
14766 DECL_INTERFACE_KNOWN (decl1) = 1;
14767 DECL_DISREGARD_INLINE_LIMITS (decl1) = 1;
14770 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl1))
14771 /* This is a constructor, we must ensure that any default args
14772 introduced by this definition are propagated to the clones
14773 now. The clones are used directly in overload resolution. */
14774 adjust_clone_args (decl1);
14776 /* Sometimes we don't notice that a function is a static member, and
14777 build a METHOD_TYPE for it. Fix that up now. */
14778 gcc_assert (!(ctype != NULL_TREE && DECL_STATIC_FUNCTION_P (decl1)
14779 && TREE_CODE (TREE_TYPE (decl1)) == METHOD_TYPE));
14781 /* Set up current_class_type, and enter the scope of the class, if
14782 appropriate. */
14783 if (ctype)
14784 push_nested_class (ctype);
14785 else if (DECL_STATIC_FUNCTION_P (decl1))
14786 push_nested_class (DECL_CONTEXT (decl1));
14788 /* Now that we have entered the scope of the class, we must restore
14789 the bindings for any template parameters surrounding DECL1, if it
14790 is an inline member template. (Order is important; consider the
14791 case where a template parameter has the same name as a field of
14792 the class.) It is not until after this point that
14793 PROCESSING_TEMPLATE_DECL is guaranteed to be set up correctly. */
14794 if (flags & SF_INCLASS_INLINE)
14795 maybe_begin_member_template_processing (decl1);
14797 /* Effective C++ rule 15. */
14798 if (warn_ecpp
14799 && DECL_OVERLOADED_OPERATOR_P (decl1) == NOP_EXPR
14800 && VOID_TYPE_P (TREE_TYPE (fntype)))
14801 warning (OPT_Weffc__, "%<operator=%> should return a reference to %<*this%>");
14803 /* Make the init_value nonzero so pushdecl knows this is not tentative.
14804 error_mark_node is replaced below (in poplevel) with the BLOCK. */
14805 if (!DECL_INITIAL (decl1))
14806 DECL_INITIAL (decl1) = error_mark_node;
14808 /* This function exists in static storage.
14809 (This does not mean `static' in the C sense!) */
14810 TREE_STATIC (decl1) = 1;
14812 /* We must call push_template_decl after current_class_type is set
14813 up. (If we are processing inline definitions after exiting a
14814 class scope, current_class_type will be NULL_TREE until set above
14815 by push_nested_class.) */
14816 if (processing_template_decl)
14818 tree newdecl1 = push_template_decl (decl1);
14819 if (newdecl1 == error_mark_node)
14821 if (ctype || DECL_STATIC_FUNCTION_P (decl1))
14822 pop_nested_class ();
14823 return false;
14825 decl1 = newdecl1;
14828 /* Make sure the parameter and return types are reasonable. When
14829 you declare a function, these types can be incomplete, but they
14830 must be complete when you define the function. */
14831 check_function_type (decl1, DECL_ARGUMENTS (decl1));
14833 /* Build the return declaration for the function. */
14834 restype = TREE_TYPE (fntype);
14836 if (DECL_RESULT (decl1) == NULL_TREE)
14838 tree resdecl;
14840 resdecl = build_decl (input_location, RESULT_DECL, 0, restype);
14841 DECL_ARTIFICIAL (resdecl) = 1;
14842 DECL_IGNORED_P (resdecl) = 1;
14843 DECL_RESULT (decl1) = resdecl;
14845 cp_apply_type_quals_to_decl (cp_type_quals (restype), resdecl);
14848 /* Record the decl so that the function name is defined.
14849 If we already have a decl for this name, and it is a FUNCTION_DECL,
14850 use the old decl. */
14851 if (!processing_template_decl && !(flags & SF_PRE_PARSED))
14853 /* A specialization is not used to guide overload resolution. */
14854 if (!DECL_FUNCTION_MEMBER_P (decl1)
14855 && !(DECL_USE_TEMPLATE (decl1) &&
14856 PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl1))))
14858 tree olddecl = pushdecl (decl1);
14860 if (olddecl == error_mark_node)
14861 /* If something went wrong when registering the declaration,
14862 use DECL1; we have to have a FUNCTION_DECL to use when
14863 parsing the body of the function. */
14865 else
14867 /* Otherwise, OLDDECL is either a previous declaration
14868 of the same function or DECL1 itself. */
14870 if (warn_missing_declarations
14871 && olddecl == decl1
14872 && !DECL_MAIN_P (decl1)
14873 && TREE_PUBLIC (decl1)
14874 && !DECL_DECLARED_INLINE_P (decl1))
14876 tree context;
14878 /* Check whether DECL1 is in an anonymous
14879 namespace. */
14880 for (context = DECL_CONTEXT (decl1);
14881 context;
14882 context = DECL_CONTEXT (context))
14884 if (TREE_CODE (context) == NAMESPACE_DECL
14885 && DECL_NAME (context) == NULL_TREE)
14886 break;
14889 if (context == NULL)
14890 warning_at (DECL_SOURCE_LOCATION (decl1),
14891 OPT_Wmissing_declarations,
14892 "no previous declaration for %qD", decl1);
14895 decl1 = olddecl;
14898 else
14900 /* We need to set the DECL_CONTEXT. */
14901 if (!DECL_CONTEXT (decl1) && DECL_TEMPLATE_INFO (decl1))
14902 DECL_CONTEXT (decl1) = DECL_CONTEXT (DECL_TI_TEMPLATE (decl1));
14904 fntype = TREE_TYPE (decl1);
14905 restype = TREE_TYPE (fntype);
14907 /* If #pragma weak applies, mark the decl appropriately now.
14908 The pragma only applies to global functions. Because
14909 determining whether or not the #pragma applies involves
14910 computing the mangled name for the declaration, we cannot
14911 apply the pragma until after we have merged this declaration
14912 with any previous declarations; if the original declaration
14913 has a linkage specification, that specification applies to
14914 the definition as well, and may affect the mangled name. */
14915 if (DECL_FILE_SCOPE_P (decl1))
14916 maybe_apply_pragma_weak (decl1);
14919 /* We are now in the scope of the function being defined. */
14920 current_function_decl = decl1;
14922 /* Save the parm names or decls from this function's declarator
14923 where store_parm_decls will find them. */
14924 current_function_parms = DECL_ARGUMENTS (decl1);
14926 /* Let the user know we're compiling this function. */
14927 announce_function (decl1);
14929 gcc_assert (DECL_INITIAL (decl1));
14931 /* This function may already have been parsed, in which case just
14932 return; our caller will skip over the body without parsing. */
14933 if (DECL_INITIAL (decl1) != error_mark_node)
14934 return true;
14936 /* Initialize RTL machinery. We cannot do this until
14937 CURRENT_FUNCTION_DECL and DECL_RESULT are set up. We do this
14938 even when processing a template; this is how we get
14939 CFUN set up, and our per-function variables initialized.
14940 FIXME factor out the non-RTL stuff. */
14941 bl = current_binding_level;
14942 allocate_struct_function (decl1, processing_template_decl);
14944 /* Initialize the language data structures. Whenever we start
14945 a new function, we destroy temporaries in the usual way. */
14946 cfun->language = ggc_cleared_alloc<language_function> ();
14947 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
14948 current_binding_level = bl;
14950 if (!processing_template_decl && type_uses_auto (restype))
14952 FNDECL_USED_AUTO (decl1) = true;
14953 current_function_auto_return_pattern = restype;
14956 /* Start the statement-tree, start the tree now. */
14957 DECL_SAVED_TREE (decl1) = push_stmt_list ();
14959 /* If we are (erroneously) defining a function that we have already
14960 defined before, wipe out what we knew before. */
14961 if (!DECL_PENDING_INLINE_P (decl1))
14962 DECL_SAVED_FUNCTION_DATA (decl1) = NULL;
14964 if (ctype && !doing_friend && !DECL_STATIC_FUNCTION_P (decl1))
14966 /* We know that this was set up by `grokclassfn'. We do not
14967 wait until `store_parm_decls', since evil parse errors may
14968 never get us to that point. Here we keep the consistency
14969 between `current_class_type' and `current_class_ptr'. */
14970 tree t = DECL_ARGUMENTS (decl1);
14972 gcc_assert (t != NULL_TREE && TREE_CODE (t) == PARM_DECL);
14973 gcc_assert (TYPE_PTR_P (TREE_TYPE (t)));
14975 cp_function_chain->x_current_class_ref
14976 = cp_build_indirect_ref (t, RO_NULL, tf_warning_or_error);
14977 /* Set this second to avoid shortcut in cp_build_indirect_ref. */
14978 cp_function_chain->x_current_class_ptr = t;
14980 /* Constructors and destructors need to know whether they're "in
14981 charge" of initializing virtual base classes. */
14982 t = DECL_CHAIN (t);
14983 if (DECL_HAS_IN_CHARGE_PARM_P (decl1))
14985 current_in_charge_parm = t;
14986 t = DECL_CHAIN (t);
14988 if (DECL_HAS_VTT_PARM_P (decl1))
14990 gcc_assert (DECL_NAME (t) == vtt_parm_identifier);
14991 current_vtt_parm = t;
14995 honor_interface = (!DECL_TEMPLATE_INSTANTIATION (decl1)
14996 /* Implicitly-defined methods (like the
14997 destructor for a class in which no destructor
14998 is explicitly declared) must not be defined
14999 until their definition is needed. So, we
15000 ignore interface specifications for
15001 compiler-generated functions. */
15002 && !DECL_ARTIFICIAL (decl1));
15004 if (processing_template_decl)
15005 /* Don't mess with interface flags. */;
15006 else if (DECL_INTERFACE_KNOWN (decl1))
15008 tree ctx = decl_function_context (decl1);
15010 if (DECL_NOT_REALLY_EXTERN (decl1))
15011 DECL_EXTERNAL (decl1) = 0;
15013 if (ctx != NULL_TREE && vague_linkage_p (ctx))
15014 /* This is a function in a local class in an extern inline
15015 or template function. */
15016 comdat_linkage (decl1);
15018 /* If this function belongs to an interface, it is public.
15019 If it belongs to someone else's interface, it is also external.
15020 This only affects inlines and template instantiations. */
15021 else if (!finfo->interface_unknown && honor_interface)
15023 if (DECL_DECLARED_INLINE_P (decl1)
15024 || DECL_TEMPLATE_INSTANTIATION (decl1))
15026 DECL_EXTERNAL (decl1)
15027 = (finfo->interface_only
15028 || (DECL_DECLARED_INLINE_P (decl1)
15029 && ! flag_implement_inlines
15030 && !DECL_VINDEX (decl1)));
15032 /* For WIN32 we also want to put these in linkonce sections. */
15033 maybe_make_one_only (decl1);
15035 else
15036 DECL_EXTERNAL (decl1) = 0;
15037 DECL_INTERFACE_KNOWN (decl1) = 1;
15038 /* If this function is in an interface implemented in this file,
15039 make sure that the back end knows to emit this function
15040 here. */
15041 if (!DECL_EXTERNAL (decl1))
15042 mark_needed (decl1);
15044 else if (finfo->interface_unknown && finfo->interface_only
15045 && honor_interface)
15047 /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma
15048 interface, we will have both finfo->interface_unknown and
15049 finfo->interface_only set. In that case, we don't want to
15050 use the normal heuristics because someone will supply a
15051 #pragma implementation elsewhere, and deducing it here would
15052 produce a conflict. */
15053 comdat_linkage (decl1);
15054 DECL_EXTERNAL (decl1) = 0;
15055 DECL_INTERFACE_KNOWN (decl1) = 1;
15056 DECL_DEFER_OUTPUT (decl1) = 1;
15058 else
15060 /* This is a definition, not a reference.
15061 So clear DECL_EXTERNAL, unless this is a GNU extern inline. */
15062 if (!GNU_INLINE_P (decl1))
15063 DECL_EXTERNAL (decl1) = 0;
15065 if ((DECL_DECLARED_INLINE_P (decl1)
15066 || DECL_TEMPLATE_INSTANTIATION (decl1))
15067 && ! DECL_INTERFACE_KNOWN (decl1))
15068 DECL_DEFER_OUTPUT (decl1) = 1;
15069 else
15070 DECL_INTERFACE_KNOWN (decl1) = 1;
15073 /* Determine the ELF visibility attribute for the function. We must not
15074 do this before calling "pushdecl", as we must allow "duplicate_decls"
15075 to merge any attributes appropriately. We also need to wait until
15076 linkage is set. */
15077 if (!DECL_CLONED_FUNCTION_P (decl1))
15078 determine_visibility (decl1);
15080 if (!processing_template_decl)
15081 maybe_instantiate_noexcept (decl1);
15083 begin_scope (sk_function_parms, decl1);
15085 ++function_depth;
15087 if (DECL_DESTRUCTOR_P (decl1)
15088 || (DECL_CONSTRUCTOR_P (decl1)
15089 && targetm.cxx.cdtor_returns_this ()))
15091 cdtor_label = create_artificial_label (input_location);
15092 LABEL_DECL_CDTOR (cdtor_label) = true;
15095 start_fname_decls ();
15097 store_parm_decls (current_function_parms);
15099 if (!processing_template_decl
15100 && (flag_lifetime_dse > 1)
15101 && DECL_CONSTRUCTOR_P (decl1)
15102 && !DECL_CLONED_FUNCTION_P (decl1)
15103 /* Clobbering an empty base is harmful if it overlays real data. */
15104 && !is_empty_class (current_class_type)
15105 /* We can't clobber safely for an implicitly-defined default constructor
15106 because part of the initialization might happen before we enter the
15107 constructor, via AGGR_INIT_ZERO_FIRST (c++/68006). */
15108 && !implicit_default_ctor_p (decl1))
15109 finish_expr_stmt (build_clobber_this ());
15111 if (!processing_template_decl
15112 && DECL_CONSTRUCTOR_P (decl1)
15113 && sanitize_flags_p (SANITIZE_VPTR)
15114 && !DECL_CLONED_FUNCTION_P (decl1)
15115 && !implicit_default_ctor_p (decl1))
15116 cp_ubsan_maybe_initialize_vtbl_ptrs (current_class_ptr);
15118 start_lambda_scope (decl1);
15120 return true;
15124 /* Like start_preparsed_function, except that instead of a
15125 FUNCTION_DECL, this function takes DECLSPECS and DECLARATOR.
15127 Returns true on success. If the DECLARATOR is not suitable
15128 for a function, we return false, which tells the parser to
15129 skip the entire function. */
15131 bool
15132 start_function (cp_decl_specifier_seq *declspecs,
15133 const cp_declarator *declarator,
15134 tree attrs)
15136 tree decl1;
15138 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, &attrs);
15139 invoke_plugin_callbacks (PLUGIN_START_PARSE_FUNCTION, decl1);
15140 if (decl1 == error_mark_node)
15141 return false;
15142 /* If the declarator is not suitable for a function definition,
15143 cause a syntax error. */
15144 if (decl1 == NULL_TREE || TREE_CODE (decl1) != FUNCTION_DECL)
15146 error ("invalid function declaration");
15147 return false;
15150 if (DECL_MAIN_P (decl1))
15151 /* main must return int. grokfndecl should have corrected it
15152 (and issued a diagnostic) if the user got it wrong. */
15153 gcc_assert (same_type_p (TREE_TYPE (TREE_TYPE (decl1)),
15154 integer_type_node));
15156 return start_preparsed_function (decl1, attrs, /*flags=*/SF_DEFAULT);
15159 /* Returns true iff an EH_SPEC_BLOCK should be created in the body of
15160 FN. */
15162 static bool
15163 use_eh_spec_block (tree fn)
15165 return (flag_exceptions && flag_enforce_eh_specs
15166 && !processing_template_decl
15167 && !type_throw_all_p (TREE_TYPE (fn))
15168 /* We insert the EH_SPEC_BLOCK only in the original
15169 function; then, it is copied automatically to the
15170 clones. */
15171 && !DECL_CLONED_FUNCTION_P (fn)
15172 /* Implicitly-generated constructors and destructors have
15173 exception specifications. However, those specifications
15174 are the union of the possible exceptions specified by the
15175 constructors/destructors for bases and members, so no
15176 unallowed exception will ever reach this function. By
15177 not creating the EH_SPEC_BLOCK we save a little memory,
15178 and we avoid spurious warnings about unreachable
15179 code. */
15180 && !DECL_DEFAULTED_FN (fn));
15183 /* Store the parameter declarations into the current function declaration.
15184 This is called after parsing the parameter declarations, before
15185 digesting the body of the function.
15187 Also install to binding contour return value identifier, if any. */
15189 static void
15190 store_parm_decls (tree current_function_parms)
15192 tree fndecl = current_function_decl;
15193 tree parm;
15195 /* This is a chain of any other decls that came in among the parm
15196 declarations. If a parm is declared with enum {foo, bar} x;
15197 then CONST_DECLs for foo and bar are put here. */
15198 tree nonparms = NULL_TREE;
15200 if (current_function_parms)
15202 /* This case is when the function was defined with an ANSI prototype.
15203 The parms already have decls, so we need not do anything here
15204 except record them as in effect
15205 and complain if any redundant old-style parm decls were written. */
15207 tree specparms = current_function_parms;
15208 tree next;
15210 /* Must clear this because it might contain TYPE_DECLs declared
15211 at class level. */
15212 current_binding_level->names = NULL;
15214 /* If we're doing semantic analysis, then we'll call pushdecl
15215 for each of these. We must do them in reverse order so that
15216 they end in the correct forward order. */
15217 specparms = nreverse (specparms);
15219 for (parm = specparms; parm; parm = next)
15221 next = DECL_CHAIN (parm);
15222 if (TREE_CODE (parm) == PARM_DECL)
15223 pushdecl (parm);
15224 else
15226 /* If we find an enum constant or a type tag,
15227 put it aside for the moment. */
15228 TREE_CHAIN (parm) = NULL_TREE;
15229 nonparms = chainon (nonparms, parm);
15233 /* Get the decls in their original chain order and record in the
15234 function. This is all and only the PARM_DECLs that were
15235 pushed into scope by the loop above. */
15236 DECL_ARGUMENTS (fndecl) = get_local_decls ();
15238 else
15239 DECL_ARGUMENTS (fndecl) = NULL_TREE;
15241 /* Now store the final chain of decls for the arguments
15242 as the decl-chain of the current lexical scope.
15243 Put the enumerators in as well, at the front so that
15244 DECL_ARGUMENTS is not modified. */
15245 current_binding_level->names = chainon (nonparms, DECL_ARGUMENTS (fndecl));
15247 if (use_eh_spec_block (current_function_decl))
15248 current_eh_spec_block = begin_eh_spec_block ();
15252 /* We have finished doing semantic analysis on DECL, but have not yet
15253 generated RTL for its body. Save away our current state, so that
15254 when we want to generate RTL later we know what to do. */
15256 static void
15257 save_function_data (tree decl)
15259 struct language_function *f;
15261 /* Save the language-specific per-function data so that we can
15262 get it back when we really expand this function. */
15263 gcc_assert (!DECL_PENDING_INLINE_P (decl));
15265 /* Make a copy. */
15266 f = ggc_alloc<language_function> ();
15267 memcpy (f, cp_function_chain, sizeof (struct language_function));
15268 DECL_SAVED_FUNCTION_DATA (decl) = f;
15270 /* Clear out the bits we don't need. */
15271 f->base.x_stmt_tree.x_cur_stmt_list = NULL;
15272 f->bindings = NULL;
15273 f->x_local_names = NULL;
15274 f->base.local_typedefs = NULL;
15278 /* Set the return value of the constructor (if present). */
15280 static void
15281 finish_constructor_body (void)
15283 tree val;
15284 tree exprstmt;
15286 if (targetm.cxx.cdtor_returns_this ())
15288 /* Any return from a constructor will end up here. */
15289 add_stmt (build_stmt (input_location, LABEL_EXPR, cdtor_label));
15291 val = DECL_ARGUMENTS (current_function_decl);
15292 val = build2 (MODIFY_EXPR, TREE_TYPE (val),
15293 DECL_RESULT (current_function_decl), val);
15294 /* Return the address of the object. */
15295 exprstmt = build_stmt (input_location, RETURN_EXPR, val);
15296 add_stmt (exprstmt);
15300 /* Do all the processing for the beginning of a destructor; set up the
15301 vtable pointers and cleanups for bases and members. */
15303 static void
15304 begin_destructor_body (void)
15306 tree compound_stmt;
15308 /* If the CURRENT_CLASS_TYPE is incomplete, we will have already
15309 issued an error message. We still want to try to process the
15310 body of the function, but initialize_vtbl_ptrs will crash if
15311 TYPE_BINFO is NULL. */
15312 if (COMPLETE_TYPE_P (current_class_type))
15314 compound_stmt = begin_compound_stmt (0);
15315 /* Make all virtual function table pointers in non-virtual base
15316 classes point to CURRENT_CLASS_TYPE's virtual function
15317 tables. */
15318 initialize_vtbl_ptrs (current_class_ptr);
15319 finish_compound_stmt (compound_stmt);
15321 if (flag_lifetime_dse
15322 /* Clobbering an empty base is harmful if it overlays real data. */
15323 && !is_empty_class (current_class_type))
15324 finish_decl_cleanup (NULL_TREE, build_clobber_this ());
15326 /* And insert cleanups for our bases and members so that they
15327 will be properly destroyed if we throw. */
15328 push_base_cleanups ();
15332 /* At the end of every destructor we generate code to delete the object if
15333 necessary. Do that now. */
15335 static void
15336 finish_destructor_body (void)
15338 tree exprstmt;
15340 /* Any return from a destructor will end up here; that way all base
15341 and member cleanups will be run when the function returns. */
15342 add_stmt (build_stmt (input_location, LABEL_EXPR, cdtor_label));
15344 if (targetm.cxx.cdtor_returns_this ())
15346 tree val;
15348 val = DECL_ARGUMENTS (current_function_decl);
15349 val = build2 (MODIFY_EXPR, TREE_TYPE (val),
15350 DECL_RESULT (current_function_decl), val);
15351 /* Return the address of the object. */
15352 exprstmt = build_stmt (input_location, RETURN_EXPR, val);
15353 add_stmt (exprstmt);
15357 /* Do the necessary processing for the beginning of a function body, which
15358 in this case includes member-initializers, but not the catch clauses of
15359 a function-try-block. Currently, this means opening a binding level
15360 for the member-initializers (in a ctor), member cleanups (in a dtor),
15361 and capture proxies (in a lambda operator()). */
15363 tree
15364 begin_function_body (void)
15366 tree stmt;
15368 if (! FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
15369 return NULL_TREE;
15371 if (processing_template_decl)
15372 /* Do nothing now. */;
15373 else
15374 /* Always keep the BLOCK node associated with the outermost pair of
15375 curly braces of a function. These are needed for correct
15376 operation of dwarfout.c. */
15377 keep_next_level (true);
15379 stmt = begin_compound_stmt (BCS_FN_BODY);
15381 if (processing_template_decl)
15382 /* Do nothing now. */;
15383 else if (DECL_DESTRUCTOR_P (current_function_decl))
15384 begin_destructor_body ();
15386 return stmt;
15389 /* Do the processing for the end of a function body. Currently, this means
15390 closing out the cleanups for fully-constructed bases and members, and in
15391 the case of the destructor, deleting the object if desired. Again, this
15392 is only meaningful for [cd]tors, since they are the only functions where
15393 there is a significant distinction between the main body and any
15394 function catch clauses. Handling, say, main() return semantics here
15395 would be wrong, as flowing off the end of a function catch clause for
15396 main() would also need to return 0. */
15398 void
15399 finish_function_body (tree compstmt)
15401 if (compstmt == NULL_TREE)
15402 return;
15404 /* Close the block. */
15405 finish_compound_stmt (compstmt);
15407 if (processing_template_decl)
15408 /* Do nothing now. */;
15409 else if (DECL_CONSTRUCTOR_P (current_function_decl))
15410 finish_constructor_body ();
15411 else if (DECL_DESTRUCTOR_P (current_function_decl))
15412 finish_destructor_body ();
15415 /* Given a function, returns the BLOCK corresponding to the outermost level
15416 of curly braces, skipping the artificial block created for constructor
15417 initializers. */
15419 tree
15420 outer_curly_brace_block (tree fndecl)
15422 tree block = DECL_INITIAL (fndecl);
15423 if (BLOCK_OUTER_CURLY_BRACE_P (block))
15424 return block;
15425 block = BLOCK_SUBBLOCKS (block);
15426 if (BLOCK_OUTER_CURLY_BRACE_P (block))
15427 return block;
15428 block = BLOCK_SUBBLOCKS (block);
15429 gcc_assert (BLOCK_OUTER_CURLY_BRACE_P (block));
15430 return block;
15433 /* If FNDECL is a class's key method, add the class to the list of
15434 keyed classes that should be emitted. */
15436 static void
15437 record_key_method_defined (tree fndecl)
15439 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fndecl)
15440 && DECL_VIRTUAL_P (fndecl)
15441 && !processing_template_decl)
15443 tree fnclass = DECL_CONTEXT (fndecl);
15444 if (fndecl == CLASSTYPE_KEY_METHOD (fnclass))
15445 vec_safe_push (keyed_classes, fnclass);
15449 /* Subroutine of finish_function.
15450 Save the body of constexpr functions for possible
15451 future compile time evaluation. */
15453 static void
15454 maybe_save_function_definition (tree fun)
15456 if (!processing_template_decl
15457 && DECL_DECLARED_CONSTEXPR_P (fun)
15458 && !cp_function_chain->invalid_constexpr
15459 && !DECL_CLONED_FUNCTION_P (fun))
15460 register_constexpr_fundef (fun, DECL_SAVED_TREE (fun));
15463 /* Finish up a function declaration and compile that function
15464 all the way to assembler language output. The free the storage
15465 for the function definition. INLINE_P is TRUE if we just
15466 finished processing the body of an in-class inline function
15467 definition. (This processing will have taken place after the
15468 class definition is complete.) */
15470 tree
15471 finish_function (bool inline_p)
15473 tree fndecl = current_function_decl;
15474 tree fntype, ctype = NULL_TREE;
15476 /* When we get some parse errors, we can end up without a
15477 current_function_decl, so cope. */
15478 if (fndecl == NULL_TREE)
15479 return error_mark_node;
15481 finish_lambda_scope ();
15483 if (c_dialect_objc ())
15484 objc_finish_function ();
15486 record_key_method_defined (fndecl);
15488 fntype = TREE_TYPE (fndecl);
15490 /* TREE_READONLY (fndecl) = 1;
15491 This caused &foo to be of type ptr-to-const-function
15492 which then got a warning when stored in a ptr-to-function variable. */
15494 gcc_assert (building_stmt_list_p ());
15495 /* The current function is being defined, so its DECL_INITIAL should
15496 be set, and unless there's a multiple definition, it should be
15497 error_mark_node. */
15498 gcc_assert (DECL_INITIAL (fndecl) == error_mark_node);
15500 /* For a cloned function, we've already got all the code we need;
15501 there's no need to add any extra bits. */
15502 if (!DECL_CLONED_FUNCTION_P (fndecl))
15504 /* Make it so that `main' always returns 0 by default. */
15505 if (DECL_MAIN_P (current_function_decl))
15506 finish_return_stmt (integer_zero_node);
15508 if (use_eh_spec_block (current_function_decl))
15509 finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
15510 (TREE_TYPE (current_function_decl)),
15511 current_eh_spec_block);
15514 /* If we're saving up tree structure, tie off the function now. */
15515 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
15517 if (fn_contains_cilk_spawn_p (cfun) && !processing_template_decl)
15518 cfun->cilk_frame_decl = insert_cilk_frame (fndecl);
15520 finish_fname_decls ();
15522 /* If this function can't throw any exceptions, remember that. */
15523 if (!processing_template_decl
15524 && !cp_function_chain->can_throw
15525 && !flag_non_call_exceptions
15526 && !decl_replaceable_p (fndecl))
15527 TREE_NOTHROW (fndecl) = 1;
15529 /* This must come after expand_function_end because cleanups might
15530 have declarations (from inline functions) that need to go into
15531 this function's blocks. */
15533 /* If the current binding level isn't the outermost binding level
15534 for this function, either there is a bug, or we have experienced
15535 syntax errors and the statement tree is malformed. */
15536 if (current_binding_level->kind != sk_function_parms)
15538 /* Make sure we have already experienced errors. */
15539 gcc_assert (errorcount);
15541 /* Throw away the broken statement tree and extra binding
15542 levels. */
15543 DECL_SAVED_TREE (fndecl) = alloc_stmt_list ();
15545 while (current_binding_level->kind != sk_function_parms)
15547 if (current_binding_level->kind == sk_class)
15548 pop_nested_class ();
15549 else
15550 poplevel (0, 0, 0);
15553 poplevel (1, 0, 1);
15555 /* Statements should always be full-expressions at the outermost set
15556 of curly braces for a function. */
15557 gcc_assert (stmts_are_full_exprs_p ());
15559 /* If there are no return statements in a function with auto return type,
15560 the return type is void. But if the declared type is something like
15561 auto*, this is an error. */
15562 if (!processing_template_decl && FNDECL_USED_AUTO (fndecl)
15563 && TREE_TYPE (fntype) == current_function_auto_return_pattern)
15565 if (is_auto (current_function_auto_return_pattern))
15567 apply_deduced_return_type (fndecl, void_type_node);
15568 fntype = TREE_TYPE (fndecl);
15570 else if (!current_function_returns_value
15571 && !current_function_returns_null)
15573 error ("no return statements in function returning %qT",
15574 current_function_auto_return_pattern);
15575 inform (input_location, "only plain %<auto%> return type can be "
15576 "deduced to %<void%>");
15580 // If this is a concept, check that the definition is reasonable.
15581 if (DECL_DECLARED_CONCEPT_P (fndecl))
15582 check_function_concept (fndecl);
15584 /* Lambda closure members are implicitly constexpr if possible. */
15585 if (cxx_dialect >= cxx17
15586 && LAMBDA_TYPE_P (CP_DECL_CONTEXT (fndecl)))
15587 DECL_DECLARED_CONSTEXPR_P (fndecl)
15588 = ((processing_template_decl
15589 || is_valid_constexpr_fn (fndecl, /*complain*/false))
15590 && potential_constant_expression (DECL_SAVED_TREE (fndecl)));
15592 /* Save constexpr function body before it gets munged by
15593 the NRV transformation. */
15594 maybe_save_function_definition (fndecl);
15596 /* Invoke the pre-genericize plugin before we start munging things. */
15597 if (!processing_template_decl)
15598 invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE, fndecl);
15600 /* Perform delayed folding before NRV transformation. */
15601 if (!processing_template_decl)
15602 cp_fold_function (fndecl);
15604 /* Set up the named return value optimization, if we can. Candidate
15605 variables are selected in check_return_expr. */
15606 if (current_function_return_value)
15608 tree r = current_function_return_value;
15609 tree outer;
15611 if (r != error_mark_node
15612 /* This is only worth doing for fns that return in memory--and
15613 simpler, since we don't have to worry about promoted modes. */
15614 && aggregate_value_p (TREE_TYPE (TREE_TYPE (fndecl)), fndecl)
15615 /* Only allow this for variables declared in the outer scope of
15616 the function so we know that their lifetime always ends with a
15617 return; see g++.dg/opt/nrv6.C. We could be more flexible if
15618 we were to do this optimization in tree-ssa. */
15619 && (outer = outer_curly_brace_block (fndecl))
15620 && chain_member (r, BLOCK_VARS (outer)))
15621 finalize_nrv (&DECL_SAVED_TREE (fndecl), r, DECL_RESULT (fndecl));
15623 current_function_return_value = NULL_TREE;
15626 /* Remember that we were in class scope. */
15627 if (current_class_name)
15628 ctype = current_class_type;
15630 /* Must mark the RESULT_DECL as being in this function. */
15631 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
15633 /* Set the BLOCK_SUPERCONTEXT of the outermost function scope to point
15634 to the FUNCTION_DECL node itself. */
15635 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
15637 /* Save away current state, if appropriate. */
15638 if (!processing_template_decl)
15639 save_function_data (fndecl);
15641 /* Complain if there's just no return statement. */
15642 if (warn_return_type
15643 && !VOID_TYPE_P (TREE_TYPE (fntype))
15644 && !dependent_type_p (TREE_TYPE (fntype))
15645 && !current_function_returns_value && !current_function_returns_null
15646 /* Don't complain if we abort or throw. */
15647 && !current_function_returns_abnormally
15648 /* Don't complain if there's an infinite loop. */
15649 && !current_function_infinite_loop
15650 /* Don't complain if we are declared noreturn. */
15651 && !TREE_THIS_VOLATILE (fndecl)
15652 && !DECL_NAME (DECL_RESULT (fndecl))
15653 && !TREE_NO_WARNING (fndecl)
15654 /* Structor return values (if any) are set by the compiler. */
15655 && !DECL_CONSTRUCTOR_P (fndecl)
15656 && !DECL_DESTRUCTOR_P (fndecl)
15657 && targetm.warn_func_return (fndecl))
15659 warning (OPT_Wreturn_type,
15660 "no return statement in function returning non-void");
15661 TREE_NO_WARNING (fndecl) = 1;
15664 /* Store the end of the function, so that we get good line number
15665 info for the epilogue. */
15666 cfun->function_end_locus = input_location;
15668 /* Complain about parameters that are only set, but never otherwise used. */
15669 if (warn_unused_but_set_parameter
15670 && !processing_template_decl
15671 && errorcount == unused_but_set_errorcount
15672 && !DECL_CLONED_FUNCTION_P (fndecl))
15674 tree decl;
15676 for (decl = DECL_ARGUMENTS (fndecl);
15677 decl;
15678 decl = DECL_CHAIN (decl))
15679 if (TREE_USED (decl)
15680 && TREE_CODE (decl) == PARM_DECL
15681 && !DECL_READ_P (decl)
15682 && DECL_NAME (decl)
15683 && !DECL_ARTIFICIAL (decl)
15684 && !TREE_NO_WARNING (decl)
15685 && !DECL_IN_SYSTEM_HEADER (decl)
15686 && TREE_TYPE (decl) != error_mark_node
15687 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
15688 && (!CLASS_TYPE_P (TREE_TYPE (decl))
15689 || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl))))
15690 warning_at (DECL_SOURCE_LOCATION (decl),
15691 OPT_Wunused_but_set_parameter,
15692 "parameter %qD set but not used", decl);
15693 unused_but_set_errorcount = errorcount;
15696 /* Complain about locally defined typedefs that are not used in this
15697 function. */
15698 maybe_warn_unused_local_typedefs ();
15700 /* Possibly warn about unused parameters. */
15701 if (warn_unused_parameter
15702 && !processing_template_decl
15703 && !DECL_CLONED_FUNCTION_P (fndecl))
15704 do_warn_unused_parameter (fndecl);
15706 /* Genericize before inlining. */
15707 if (!processing_template_decl)
15709 struct language_function *f = DECL_SAVED_FUNCTION_DATA (fndecl);
15710 cp_genericize (fndecl);
15711 /* Clear out the bits we don't need. */
15712 f->x_current_class_ptr = NULL;
15713 f->x_current_class_ref = NULL;
15714 f->x_eh_spec_block = NULL;
15715 f->x_in_charge_parm = NULL;
15716 f->x_vtt_parm = NULL;
15717 f->x_return_value = NULL;
15718 f->bindings = NULL;
15719 f->extern_decl_map = NULL;
15720 f->infinite_loops = NULL;
15722 /* Clear out the bits we don't need. */
15723 local_names = NULL;
15725 /* We're leaving the context of this function, so zap cfun. It's still in
15726 DECL_STRUCT_FUNCTION, and we'll restore it in tree_rest_of_compilation. */
15727 set_cfun (NULL);
15728 current_function_decl = NULL;
15730 /* If this is an in-class inline definition, we may have to pop the
15731 bindings for the template parameters that we added in
15732 maybe_begin_member_template_processing when start_function was
15733 called. */
15734 if (inline_p)
15735 maybe_end_member_template_processing ();
15737 /* Leave the scope of the class. */
15738 if (ctype)
15739 pop_nested_class ();
15741 --function_depth;
15743 /* Clean up. */
15744 current_function_decl = NULL_TREE;
15746 invoke_plugin_callbacks (PLUGIN_FINISH_PARSE_FUNCTION, fndecl);
15747 return fndecl;
15750 /* Create the FUNCTION_DECL for a function definition.
15751 DECLSPECS and DECLARATOR are the parts of the declaration;
15752 they describe the return type and the name of the function,
15753 but twisted together in a fashion that parallels the syntax of C.
15755 This function creates a binding context for the function body
15756 as well as setting up the FUNCTION_DECL in current_function_decl.
15758 Returns a FUNCTION_DECL on success.
15760 If the DECLARATOR is not suitable for a function (it defines a datum
15761 instead), we return 0, which tells yyparse to report a parse error.
15763 May return void_type_node indicating that this method is actually
15764 a friend. See grokfield for more details.
15766 Came here with a `.pushlevel' .
15768 DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
15769 CHANGES TO CODE IN `grokfield'. */
15771 tree
15772 grokmethod (cp_decl_specifier_seq *declspecs,
15773 const cp_declarator *declarator, tree attrlist)
15775 tree fndecl = grokdeclarator (declarator, declspecs, MEMFUNCDEF, 0,
15776 &attrlist);
15778 if (fndecl == error_mark_node)
15779 return error_mark_node;
15781 if (fndecl == NULL || TREE_CODE (fndecl) != FUNCTION_DECL)
15783 error ("invalid member function declaration");
15784 return error_mark_node;
15787 if (attrlist)
15788 cplus_decl_attributes (&fndecl, attrlist, 0);
15790 /* Pass friends other than inline friend functions back. */
15791 if (fndecl == void_type_node)
15792 return fndecl;
15794 if (DECL_IN_AGGR_P (fndecl))
15796 if (DECL_CLASS_SCOPE_P (fndecl))
15797 error ("%qD is already defined in class %qT", fndecl,
15798 DECL_CONTEXT (fndecl));
15799 return error_mark_node;
15802 check_template_shadow (fndecl);
15804 if (TREE_PUBLIC (fndecl))
15805 DECL_COMDAT (fndecl) = 1;
15806 DECL_DECLARED_INLINE_P (fndecl) = 1;
15807 DECL_NO_INLINE_WARNING_P (fndecl) = 1;
15809 /* We process method specializations in finish_struct_1. */
15810 if (processing_template_decl && !DECL_TEMPLATE_SPECIALIZATION (fndecl))
15812 fndecl = push_template_decl (fndecl);
15813 if (fndecl == error_mark_node)
15814 return fndecl;
15817 if (! DECL_FRIEND_P (fndecl))
15819 if (DECL_CHAIN (fndecl))
15821 fndecl = copy_node (fndecl);
15822 TREE_CHAIN (fndecl) = NULL_TREE;
15826 cp_finish_decl (fndecl, NULL_TREE, false, NULL_TREE, 0);
15828 DECL_IN_AGGR_P (fndecl) = 1;
15829 return fndecl;
15833 /* VAR is a VAR_DECL. If its type is incomplete, remember VAR so that
15834 we can lay it out later, when and if its type becomes complete.
15836 Also handle constexpr variables where the initializer involves
15837 an unlowered PTRMEM_CST because the class isn't complete yet. */
15839 void
15840 maybe_register_incomplete_var (tree var)
15842 gcc_assert (VAR_P (var));
15844 /* Keep track of variables with incomplete types. */
15845 if (!processing_template_decl && TREE_TYPE (var) != error_mark_node
15846 && DECL_EXTERNAL (var))
15848 tree inner_type = TREE_TYPE (var);
15850 while (TREE_CODE (inner_type) == ARRAY_TYPE)
15851 inner_type = TREE_TYPE (inner_type);
15852 inner_type = TYPE_MAIN_VARIANT (inner_type);
15854 if ((!COMPLETE_TYPE_P (inner_type) && CLASS_TYPE_P (inner_type))
15855 /* RTTI TD entries are created while defining the type_info. */
15856 || (TYPE_LANG_SPECIFIC (inner_type)
15857 && TYPE_BEING_DEFINED (inner_type)))
15859 incomplete_var iv = {var, inner_type};
15860 vec_safe_push (incomplete_vars, iv);
15862 else if (!(DECL_LANG_SPECIFIC (var) && DECL_TEMPLATE_INFO (var))
15863 && decl_constant_var_p (var)
15864 && (TYPE_PTRMEM_P (inner_type) || CLASS_TYPE_P (inner_type)))
15866 /* When the outermost open class is complete we can resolve any
15867 pointers-to-members. */
15868 tree context = outermost_open_class ();
15869 incomplete_var iv = {var, context};
15870 vec_safe_push (incomplete_vars, iv);
15875 /* Called when a class type (given by TYPE) is defined. If there are
15876 any existing VAR_DECLs whose type has been completed by this
15877 declaration, update them now. */
15879 void
15880 complete_vars (tree type)
15882 unsigned ix;
15883 incomplete_var *iv;
15885 for (ix = 0; vec_safe_iterate (incomplete_vars, ix, &iv); )
15887 if (same_type_p (type, iv->incomplete_type))
15889 tree var = iv->decl;
15890 tree type = TREE_TYPE (var);
15892 if (type != error_mark_node
15893 && (TYPE_MAIN_VARIANT (strip_array_types (type))
15894 == iv->incomplete_type))
15896 /* Complete the type of the variable. The VAR_DECL itself
15897 will be laid out in expand_expr. */
15898 complete_type (type);
15899 cp_apply_type_quals_to_decl (cp_type_quals (type), var);
15902 /* Remove this entry from the list. */
15903 incomplete_vars->unordered_remove (ix);
15905 else
15906 ix++;
15909 /* Check for pending declarations which may have abstract type. */
15910 complete_type_check_abstract (type);
15913 /* If DECL is of a type which needs a cleanup, build and return an
15914 expression to perform that cleanup here. Return NULL_TREE if no
15915 cleanup need be done. DECL can also be a _REF when called from
15916 split_nonconstant_init_1. */
15918 tree
15919 cxx_maybe_build_cleanup (tree decl, tsubst_flags_t complain)
15921 tree type;
15922 tree attr;
15923 tree cleanup;
15925 /* Assume no cleanup is required. */
15926 cleanup = NULL_TREE;
15928 if (error_operand_p (decl))
15929 return cleanup;
15931 /* Handle "__attribute__((cleanup))". We run the cleanup function
15932 before the destructor since the destructor is what actually
15933 terminates the lifetime of the object. */
15934 if (DECL_P (decl))
15935 attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
15936 else
15937 attr = NULL_TREE;
15938 if (attr)
15940 tree id;
15941 tree fn;
15942 tree arg;
15944 /* Get the name specified by the user for the cleanup function. */
15945 id = TREE_VALUE (TREE_VALUE (attr));
15946 /* Look up the name to find the cleanup function to call. It is
15947 important to use lookup_name here because that is what is
15948 used in c-common.c:handle_cleanup_attribute when performing
15949 initial checks on the attribute. Note that those checks
15950 include ensuring that the function found is not an overloaded
15951 function, or an object with an overloaded call operator,
15952 etc.; we can rely on the fact that the function found is an
15953 ordinary FUNCTION_DECL. */
15954 fn = lookup_name (id);
15955 arg = build_address (decl);
15956 if (!mark_used (decl, complain) && !(complain & tf_error))
15957 return error_mark_node;
15958 cleanup = cp_build_function_call_nary (fn, complain, arg, NULL_TREE);
15959 if (cleanup == error_mark_node)
15960 return error_mark_node;
15962 /* Handle ordinary C++ destructors. */
15963 type = TREE_TYPE (decl);
15964 if (type_build_dtor_call (type))
15966 int flags = LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR;
15967 tree addr;
15968 tree call;
15970 if (TREE_CODE (type) == ARRAY_TYPE)
15971 addr = decl;
15972 else
15973 addr = build_address (decl);
15975 call = build_delete (TREE_TYPE (addr), addr,
15976 sfk_complete_destructor, flags, 0, complain);
15977 if (call == error_mark_node)
15978 cleanup = error_mark_node;
15979 else if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
15980 /* Discard the call. */;
15981 else if (cleanup)
15982 cleanup = cp_build_compound_expr (cleanup, call, complain);
15983 else
15984 cleanup = call;
15987 /* build_delete sets the location of the destructor call to the
15988 current location, even though the destructor is going to be
15989 called later, at the end of the current scope. This can lead to
15990 a "jumpy" behavior for users of debuggers when they step around
15991 the end of the block. So let's unset the location of the
15992 destructor call instead. */
15993 protected_set_expr_location (cleanup, UNKNOWN_LOCATION);
15995 if (cleanup
15996 && DECL_P (decl)
15997 && !lookup_attribute ("warn_unused", TYPE_ATTRIBUTES (TREE_TYPE (decl)))
15998 /* Treat objects with destructors as used; the destructor may do
15999 something substantive. */
16000 && !mark_used (decl, complain) && !(complain & tf_error))
16001 return error_mark_node;
16003 return cleanup;
16007 /* Return the FUNCTION_TYPE that corresponds to MEMFNTYPE, which can be a
16008 FUNCTION_DECL, METHOD_TYPE, FUNCTION_TYPE, pointer or reference to
16009 METHOD_TYPE or FUNCTION_TYPE, or pointer to member function. */
16011 tree
16012 static_fn_type (tree memfntype)
16014 tree fntype;
16015 tree args;
16017 if (TYPE_PTRMEMFUNC_P (memfntype))
16018 memfntype = TYPE_PTRMEMFUNC_FN_TYPE (memfntype);
16019 if (POINTER_TYPE_P (memfntype)
16020 || TREE_CODE (memfntype) == FUNCTION_DECL)
16021 memfntype = TREE_TYPE (memfntype);
16022 if (TREE_CODE (memfntype) == FUNCTION_TYPE)
16023 return memfntype;
16024 gcc_assert (TREE_CODE (memfntype) == METHOD_TYPE);
16025 args = TYPE_ARG_TYPES (memfntype);
16026 cp_ref_qualifier rqual = type_memfn_rqual (memfntype);
16027 fntype = build_function_type (TREE_TYPE (memfntype), TREE_CHAIN (args));
16028 fntype = apply_memfn_quals (fntype, type_memfn_quals (memfntype), rqual);
16029 fntype = (cp_build_type_attribute_variant
16030 (fntype, TYPE_ATTRIBUTES (memfntype)));
16031 fntype = (build_exception_variant
16032 (fntype, TYPE_RAISES_EXCEPTIONS (memfntype)));
16033 if (TYPE_HAS_LATE_RETURN_TYPE (memfntype))
16034 TYPE_HAS_LATE_RETURN_TYPE (fntype) = 1;
16035 return fntype;
16038 /* DECL was originally constructed as a non-static member function,
16039 but turned out to be static. Update it accordingly. */
16041 void
16042 revert_static_member_fn (tree decl)
16044 tree stype = static_fn_type (decl);
16045 cp_cv_quals quals = type_memfn_quals (stype);
16046 cp_ref_qualifier rqual = type_memfn_rqual (stype);
16048 if (quals != TYPE_UNQUALIFIED || rqual != REF_QUAL_NONE)
16049 stype = apply_memfn_quals (stype, TYPE_UNQUALIFIED, REF_QUAL_NONE);
16051 TREE_TYPE (decl) = stype;
16053 if (DECL_ARGUMENTS (decl))
16054 DECL_ARGUMENTS (decl) = DECL_CHAIN (DECL_ARGUMENTS (decl));
16055 DECL_STATIC_FUNCTION_P (decl) = 1;
16058 /* Return which tree structure is used by T, or TS_CP_GENERIC if T is
16059 one of the language-independent trees. */
16061 enum cp_tree_node_structure_enum
16062 cp_tree_node_structure (union lang_tree_node * t)
16064 switch (TREE_CODE (&t->generic))
16066 case DEFAULT_ARG: return TS_CP_DEFAULT_ARG;
16067 case DEFERRED_NOEXCEPT: return TS_CP_DEFERRED_NOEXCEPT;
16068 case IDENTIFIER_NODE: return TS_CP_IDENTIFIER;
16069 case OVERLOAD: return TS_CP_OVERLOAD;
16070 case TEMPLATE_PARM_INDEX: return TS_CP_TPI;
16071 case PTRMEM_CST: return TS_CP_PTRMEM;
16072 case BASELINK: return TS_CP_BASELINK;
16073 case TEMPLATE_DECL: return TS_CP_TEMPLATE_DECL;
16074 case STATIC_ASSERT: return TS_CP_STATIC_ASSERT;
16075 case ARGUMENT_PACK_SELECT: return TS_CP_ARGUMENT_PACK_SELECT;
16076 case TRAIT_EXPR: return TS_CP_TRAIT_EXPR;
16077 case LAMBDA_EXPR: return TS_CP_LAMBDA_EXPR;
16078 case TEMPLATE_INFO: return TS_CP_TEMPLATE_INFO;
16079 case CONSTRAINT_INFO: return TS_CP_CONSTRAINT_INFO;
16080 case USERDEF_LITERAL: return TS_CP_USERDEF_LITERAL;
16081 default: return TS_CP_GENERIC;
16085 /* Build the void_list_node (void_type_node having been created). */
16086 tree
16087 build_void_list_node (void)
16089 tree t = build_tree_list (NULL_TREE, void_type_node);
16090 return t;
16093 bool
16094 cp_missing_noreturn_ok_p (tree decl)
16096 /* A missing noreturn is ok for the `main' function. */
16097 return DECL_MAIN_P (decl);
16100 /* Return the decl used to identify the COMDAT group into which DECL should
16101 be placed. */
16103 tree
16104 cxx_comdat_group (tree decl)
16106 /* Virtual tables, construction virtual tables, and virtual table
16107 tables all go in a single COMDAT group, named after the primary
16108 virtual table. */
16109 if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl))
16110 decl = CLASSTYPE_VTABLES (DECL_CONTEXT (decl));
16111 /* For all other DECLs, the COMDAT group is the mangled name of the
16112 declaration itself. */
16113 else
16115 while (DECL_THUNK_P (decl))
16117 /* If TARGET_USE_LOCAL_THUNK_ALIAS_P, use_thunk puts the thunk
16118 into the same section as the target function. In that case
16119 we must return target's name. */
16120 tree target = THUNK_TARGET (decl);
16121 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (target)
16122 && DECL_SECTION_NAME (target) != NULL
16123 && DECL_ONE_ONLY (target))
16124 decl = target;
16125 else
16126 break;
16130 return decl;
16133 /* Returns the return type for FN as written by the user, which may include
16134 a placeholder for a deduced return type. */
16136 tree
16137 fndecl_declared_return_type (tree fn)
16139 fn = STRIP_TEMPLATE (fn);
16140 if (FNDECL_USED_AUTO (fn))
16142 struct language_function *f = NULL;
16143 if (DECL_STRUCT_FUNCTION (fn))
16144 f = DECL_STRUCT_FUNCTION (fn)->language;
16145 if (f == NULL)
16146 f = DECL_SAVED_FUNCTION_DATA (fn);
16147 return f->x_auto_return_pattern;
16149 return TREE_TYPE (TREE_TYPE (fn));
16152 /* Returns true iff DECL was declared with an auto type and it has
16153 not yet been deduced to a real type. */
16155 bool
16156 undeduced_auto_decl (tree decl)
16158 if (cxx_dialect < cxx11)
16159 return false;
16160 return type_uses_auto (TREE_TYPE (decl));
16163 /* Complain if DECL has an undeduced return type. */
16165 bool
16166 require_deduced_type (tree decl, tsubst_flags_t complain)
16168 if (undeduced_auto_decl (decl))
16170 if (complain & tf_error)
16171 error ("use of %qD before deduction of %<auto%>", decl);
16172 return false;
16174 return true;
16177 #include "gt-cp-decl.h"