* doc/invoke.texi: Document -std=c++17 and -std=gnu++17 and document
[official-gcc.git] / gcc / cp / decl.c
blob858747eecfc35c24e8de80f9bb2a947d1032f1f6
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 (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 void
3901 record_builtin_type (enum rid rid_index,
3902 const char* name,
3903 tree type)
3905 tree rname = NULL_TREE, tname = NULL_TREE;
3906 tree tdecl = NULL_TREE;
3908 if ((int) rid_index < (int) RID_MAX)
3909 rname = ridpointers[(int) rid_index];
3910 if (name)
3911 tname = get_identifier (name);
3913 /* The calls to SET_IDENTIFIER_GLOBAL_VALUE below should be
3914 eliminated. Built-in types should not be looked up name; their
3915 names are keywords that the parser can recognize. However, there
3916 is code in c-common.c that uses identifier_global_value to look
3917 up built-in types by name. */
3918 if (tname)
3920 tdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, tname, type);
3921 DECL_ARTIFICIAL (tdecl) = 1;
3922 SET_IDENTIFIER_GLOBAL_VALUE (tname, tdecl);
3924 if (rname)
3926 if (!tdecl)
3928 tdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, rname, type);
3929 DECL_ARTIFICIAL (tdecl) = 1;
3931 SET_IDENTIFIER_GLOBAL_VALUE (rname, tdecl);
3934 if (!TYPE_NAME (type))
3935 TYPE_NAME (type) = tdecl;
3937 if (tdecl)
3938 debug_hooks->type_decl (tdecl, 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 IDENTIFIER_GLOBAL_VALUE. 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::eq_p (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))
6050 return cp_build_c_cast (type, elt, tf_warning_or_error);
6051 else
6052 return error_mark_node;
6055 /* Recurse on this CONSTRUCTOR. */
6056 d.cur = &(*v)[0];
6057 d.end = d.cur + v->length ();
6059 new_init = reshape_init_r (type, &d, true, complain);
6060 if (new_init == error_mark_node)
6061 return error_mark_node;
6063 /* Make sure all the element of the constructor were used. Otherwise,
6064 issue an error about exceeding initializers. */
6065 if (d.cur != d.end)
6067 if (complain & tf_error)
6068 error ("too many initializers for %qT", type);
6069 return error_mark_node;
6072 if (CONSTRUCTOR_IS_DIRECT_INIT (init)
6073 && BRACE_ENCLOSED_INITIALIZER_P (new_init))
6074 CONSTRUCTOR_IS_DIRECT_INIT (new_init) = true;
6076 return new_init;
6079 /* Verify array initializer. Returns true if errors have been reported. */
6081 bool
6082 check_array_initializer (tree decl, tree type, tree init)
6084 tree element_type = TREE_TYPE (type);
6086 /* The array type itself need not be complete, because the
6087 initializer may tell us how many elements are in the array.
6088 But, the elements of the array must be complete. */
6089 if (!COMPLETE_TYPE_P (complete_type (element_type)))
6091 if (decl)
6092 error_at (DECL_SOURCE_LOCATION (decl),
6093 "elements of array %q#D have incomplete type", decl);
6094 else
6095 error ("elements of array %q#T have incomplete type", type);
6096 return true;
6098 /* A compound literal can't have variable size. */
6099 if (init && !decl
6100 && ((COMPLETE_TYPE_P (type) && !TREE_CONSTANT (TYPE_SIZE (type)))
6101 || !TREE_CONSTANT (TYPE_SIZE (element_type))))
6103 error ("variable-sized compound literal");
6104 return true;
6106 return false;
6109 /* Subroutine of check_initializer; args are passed down from that function.
6110 Set stmts_are_full_exprs_p to 1 across a call to build_aggr_init. */
6112 static tree
6113 build_aggr_init_full_exprs (tree decl, tree init, int flags)
6116 gcc_assert (stmts_are_full_exprs_p ());
6117 return build_aggr_init (decl, init, flags, tf_warning_or_error);
6120 /* Verify INIT (the initializer for DECL), and record the
6121 initialization in DECL_INITIAL, if appropriate. CLEANUP is as for
6122 grok_reference_init.
6124 If the return value is non-NULL, it is an expression that must be
6125 evaluated dynamically to initialize DECL. */
6127 static tree
6128 check_initializer (tree decl, tree init, int flags, vec<tree, va_gc> **cleanups)
6130 tree type = TREE_TYPE (decl);
6131 tree init_code = NULL;
6132 tree core_type;
6134 /* Things that are going to be initialized need to have complete
6135 type. */
6136 TREE_TYPE (decl) = type = complete_type (TREE_TYPE (decl));
6138 if (DECL_HAS_VALUE_EXPR_P (decl))
6140 /* A variable with DECL_HAS_VALUE_EXPR_P set is just a placeholder,
6141 it doesn't have storage to be initialized. */
6142 gcc_assert (init == NULL_TREE);
6143 return NULL_TREE;
6146 if (type == error_mark_node)
6147 /* We will have already complained. */
6148 return NULL_TREE;
6150 if (TREE_CODE (type) == ARRAY_TYPE)
6152 if (check_array_initializer (decl, type, init))
6153 return NULL_TREE;
6155 else if (!COMPLETE_TYPE_P (type))
6157 error_at (DECL_SOURCE_LOCATION (decl),
6158 "%q#D has incomplete type", decl);
6159 TREE_TYPE (decl) = error_mark_node;
6160 return NULL_TREE;
6162 else
6163 /* There is no way to make a variable-sized class type in GNU C++. */
6164 gcc_assert (TREE_CONSTANT (TYPE_SIZE (type)));
6166 if (init && BRACE_ENCLOSED_INITIALIZER_P (init))
6168 int init_len = CONSTRUCTOR_NELTS (init);
6169 if (SCALAR_TYPE_P (type))
6171 if (init_len == 0)
6173 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6174 init = build_zero_init (type, NULL_TREE, false);
6176 else if (init_len != 1 && TREE_CODE (type) != COMPLEX_TYPE)
6178 error_at (EXPR_LOC_OR_LOC (init, DECL_SOURCE_LOCATION (decl)),
6179 "scalar object %qD requires one element in "
6180 "initializer", decl);
6181 TREE_TYPE (decl) = error_mark_node;
6182 return NULL_TREE;
6187 if (TREE_CODE (decl) == CONST_DECL)
6189 gcc_assert (TREE_CODE (type) != REFERENCE_TYPE);
6191 DECL_INITIAL (decl) = init;
6193 gcc_assert (init != NULL_TREE);
6194 init = NULL_TREE;
6196 else if (!init && DECL_REALLY_EXTERN (decl))
6198 else if (init || type_build_ctor_call (type)
6199 || TREE_CODE (type) == REFERENCE_TYPE)
6201 if (TREE_CODE (type) == REFERENCE_TYPE)
6203 init = grok_reference_init (decl, type, init, flags);
6204 flags |= LOOKUP_ALREADY_DIGESTED;
6206 else if (!init)
6207 check_for_uninitialized_const_var (decl);
6208 /* Do not reshape constructors of vectors (they don't need to be
6209 reshaped. */
6210 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
6212 if (is_std_init_list (type))
6214 init = perform_implicit_conversion (type, init,
6215 tf_warning_or_error);
6216 flags |= LOOKUP_ALREADY_DIGESTED;
6218 else if (TYPE_NON_AGGREGATE_CLASS (type))
6220 /* Don't reshape if the class has constructors. */
6221 if (cxx_dialect == cxx98)
6222 error_at (EXPR_LOC_OR_LOC (init, DECL_SOURCE_LOCATION (decl)),
6223 "in C++98 %qD must be initialized by "
6224 "constructor, not by %<{...}%>",
6225 decl);
6227 else if (VECTOR_TYPE_P (type) && TYPE_VECTOR_OPAQUE (type))
6229 error ("opaque vector types cannot be initialized");
6230 init = error_mark_node;
6232 else
6234 init = reshape_init (type, init, tf_warning_or_error);
6235 flags |= LOOKUP_NO_NARROWING;
6238 else if (TREE_CODE (init) == TREE_LIST
6239 && TREE_TYPE (init) != unknown_type_node
6240 && !MAYBE_CLASS_TYPE_P (type))
6242 gcc_assert (TREE_CODE (decl) != RESULT_DECL);
6244 /* We get here with code like `int a (2);' */
6245 init = build_x_compound_expr_from_list (init, ELK_INIT,
6246 tf_warning_or_error);
6249 /* If DECL has an array type without a specific bound, deduce the
6250 array size from the initializer. */
6251 maybe_deduce_size_from_array_init (decl, init);
6252 type = TREE_TYPE (decl);
6253 if (type == error_mark_node)
6254 return NULL_TREE;
6256 if (((type_build_ctor_call (type) || CLASS_TYPE_P (type))
6257 && !(flags & LOOKUP_ALREADY_DIGESTED)
6258 && !(init && BRACE_ENCLOSED_INITIALIZER_P (init)
6259 && CP_AGGREGATE_TYPE_P (type)
6260 && (CLASS_TYPE_P (type)
6261 || !TYPE_NEEDS_CONSTRUCTING (type)
6262 || type_has_extended_temps (type))))
6263 || (DECL_DECOMPOSITION_P (decl) && TREE_CODE (type) == ARRAY_TYPE))
6265 init_code = build_aggr_init_full_exprs (decl, init, flags);
6267 /* A constructor call is a non-trivial initializer even if
6268 it isn't explicitly written. */
6269 if (TREE_SIDE_EFFECTS (init_code))
6270 DECL_NONTRIVIALLY_INITIALIZED_P (decl) = true;
6272 /* If this is a constexpr initializer, expand_default_init will
6273 have returned an INIT_EXPR rather than a CALL_EXPR. In that
6274 case, pull the initializer back out and pass it down into
6275 store_init_value. */
6276 while (TREE_CODE (init_code) == EXPR_STMT
6277 || TREE_CODE (init_code) == CONVERT_EXPR)
6278 init_code = TREE_OPERAND (init_code, 0);
6279 if (TREE_CODE (init_code) == INIT_EXPR)
6281 init = TREE_OPERAND (init_code, 1);
6282 init_code = NULL_TREE;
6283 /* Don't call digest_init; it's unnecessary and will complain
6284 about aggregate initialization of non-aggregate classes. */
6285 flags |= LOOKUP_ALREADY_DIGESTED;
6287 else if (DECL_DECLARED_CONSTEXPR_P (decl))
6289 /* Declared constexpr, but no suitable initializer; massage
6290 init appropriately so we can pass it into store_init_value
6291 for the error. */
6292 if (CLASS_TYPE_P (type)
6293 && (!init || TREE_CODE (init) == TREE_LIST))
6295 init = build_functional_cast (type, init, tf_none);
6296 if (TREE_CODE (init) == TARGET_EXPR)
6297 TARGET_EXPR_DIRECT_INIT_P (init) = true;
6299 init_code = NULL_TREE;
6301 else
6302 init = NULL_TREE;
6305 if (init && TREE_CODE (init) != TREE_VEC)
6307 /* In aggregate initialization of a variable, each element
6308 initialization is a full-expression because there is no
6309 enclosing expression. */
6310 gcc_assert (stmts_are_full_exprs_p ());
6312 init_code = store_init_value (decl, init, cleanups, flags);
6314 if (pedantic && TREE_CODE (type) == ARRAY_TYPE
6315 && DECL_INITIAL (decl)
6316 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
6317 && PAREN_STRING_LITERAL_P (DECL_INITIAL (decl)))
6318 warning_at (EXPR_LOC_OR_LOC (DECL_INITIAL (decl),
6319 DECL_SOURCE_LOCATION (decl)),
6320 0, "array %qD initialized by parenthesized "
6321 "string literal %qE",
6322 decl, DECL_INITIAL (decl));
6323 init = NULL;
6326 else
6328 if (CLASS_TYPE_P (core_type = strip_array_types (type))
6329 && (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type)
6330 || CLASSTYPE_REF_FIELDS_NEED_INIT (core_type)))
6331 diagnose_uninitialized_cst_or_ref_member (core_type, /*using_new=*/false,
6332 /*complain=*/true);
6334 check_for_uninitialized_const_var (decl);
6337 if (init && init != error_mark_node)
6338 init_code = build2 (INIT_EXPR, type, decl, init);
6340 if (init_code)
6342 /* We might have set these in cp_finish_decl. */
6343 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = false;
6344 TREE_CONSTANT (decl) = false;
6347 if (init_code
6348 && (DECL_IN_AGGR_P (decl) && !DECL_VAR_DECLARED_INLINE_P (decl)))
6350 static int explained = 0;
6352 if (cxx_dialect < cxx11)
6353 error ("initializer invalid for static member with constructor");
6354 else if (cxx_dialect < cxx17)
6355 error ("non-constant in-class initialization invalid for static "
6356 "member %qD", decl);
6357 else
6358 error ("non-constant in-class initialization invalid for non-inline "
6359 "static member %qD", decl);
6360 if (!explained)
6362 inform (input_location,
6363 "(an out of class initialization is required)");
6364 explained = 1;
6366 return NULL_TREE;
6369 return init_code;
6372 /* If DECL is not a local variable, give it RTL. */
6374 static void
6375 make_rtl_for_nonlocal_decl (tree decl, tree init, const char* asmspec)
6377 int toplev = toplevel_bindings_p ();
6378 int defer_p;
6380 /* Set the DECL_ASSEMBLER_NAME for the object. */
6381 if (asmspec)
6383 /* The `register' keyword, when used together with an
6384 asm-specification, indicates that the variable should be
6385 placed in a particular register. */
6386 if (VAR_P (decl) && DECL_REGISTER (decl))
6388 set_user_assembler_name (decl, asmspec);
6389 DECL_HARD_REGISTER (decl) = 1;
6391 else
6393 if (TREE_CODE (decl) == FUNCTION_DECL
6394 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
6395 set_builtin_user_assembler_name (decl, asmspec);
6396 set_user_assembler_name (decl, asmspec);
6400 /* Handle non-variables up front. */
6401 if (!VAR_P (decl))
6403 rest_of_decl_compilation (decl, toplev, at_eof);
6404 return;
6407 /* If we see a class member here, it should be a static data
6408 member. */
6409 if (DECL_LANG_SPECIFIC (decl) && DECL_IN_AGGR_P (decl))
6411 gcc_assert (TREE_STATIC (decl));
6412 /* An in-class declaration of a static data member should be
6413 external; it is only a declaration, and not a definition. */
6414 if (init == NULL_TREE)
6415 gcc_assert (DECL_EXTERNAL (decl)
6416 || !TREE_PUBLIC (decl)
6417 || DECL_INLINE_VAR_P (decl));
6420 /* We don't create any RTL for local variables. */
6421 if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
6422 return;
6424 /* We defer emission of local statics until the corresponding
6425 DECL_EXPR is expanded. But with constexpr its function might never
6426 be expanded, so go ahead and tell cgraph about the variable now. */
6427 defer_p = ((DECL_FUNCTION_SCOPE_P (decl)
6428 && !var_in_maybe_constexpr_fn (decl))
6429 || DECL_VIRTUAL_P (decl));
6431 /* Defer template instantiations. */
6432 if (DECL_LANG_SPECIFIC (decl)
6433 && DECL_IMPLICIT_INSTANTIATION (decl))
6434 defer_p = 1;
6436 /* If we're not deferring, go ahead and assemble the variable. */
6437 if (!defer_p)
6438 rest_of_decl_compilation (decl, toplev, at_eof);
6441 /* walk_tree helper for wrap_temporary_cleanups, below. */
6443 static tree
6444 wrap_cleanups_r (tree *stmt_p, int *walk_subtrees, void *data)
6446 /* Stop at types or full-expression boundaries. */
6447 if (TYPE_P (*stmt_p)
6448 || TREE_CODE (*stmt_p) == CLEANUP_POINT_EXPR)
6450 *walk_subtrees = 0;
6451 return NULL_TREE;
6454 if (TREE_CODE (*stmt_p) == TARGET_EXPR)
6456 tree guard = (tree)data;
6457 tree tcleanup = TARGET_EXPR_CLEANUP (*stmt_p);
6459 tcleanup = build2 (TRY_CATCH_EXPR, void_type_node, tcleanup, guard);
6460 /* Tell honor_protect_cleanup_actions to handle this as a separate
6461 cleanup. */
6462 TRY_CATCH_IS_CLEANUP (tcleanup) = 1;
6464 TARGET_EXPR_CLEANUP (*stmt_p) = tcleanup;
6467 return NULL_TREE;
6470 /* We're initializing a local variable which has a cleanup GUARD. If there
6471 are any temporaries used in the initializer INIT of this variable, we
6472 need to wrap their cleanups with TRY_CATCH_EXPR (, GUARD) so that the
6473 variable will be cleaned up properly if one of them throws.
6475 Unfortunately, there's no way to express this properly in terms of
6476 nesting, as the regions for the temporaries overlap the region for the
6477 variable itself; if there are two temporaries, the variable needs to be
6478 the first thing destroyed if either of them throws. However, we only
6479 want to run the variable's cleanup if it actually got constructed. So
6480 we need to guard the temporary cleanups with the variable's cleanup if
6481 they are run on the normal path, but not if they are run on the
6482 exceptional path. We implement this by telling
6483 honor_protect_cleanup_actions to strip the variable cleanup from the
6484 exceptional path. */
6486 static void
6487 wrap_temporary_cleanups (tree init, tree guard)
6489 cp_walk_tree_without_duplicates (&init, wrap_cleanups_r, (void *)guard);
6492 /* Generate code to initialize DECL (a local variable). */
6494 static void
6495 initialize_local_var (tree decl, tree init)
6497 tree type = TREE_TYPE (decl);
6498 tree cleanup;
6499 int already_used;
6501 gcc_assert (VAR_P (decl)
6502 || TREE_CODE (decl) == RESULT_DECL);
6503 gcc_assert (!TREE_STATIC (decl));
6505 if (DECL_SIZE (decl) == NULL_TREE)
6507 /* If we used it already as memory, it must stay in memory. */
6508 DECL_INITIAL (decl) = NULL_TREE;
6509 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
6510 return;
6513 if (type == error_mark_node)
6514 return;
6516 /* Compute and store the initial value. */
6517 already_used = TREE_USED (decl) || TREE_USED (type);
6518 if (TREE_USED (type))
6519 DECL_READ_P (decl) = 1;
6521 /* Generate a cleanup, if necessary. */
6522 cleanup = cxx_maybe_build_cleanup (decl, tf_warning_or_error);
6524 /* Perform the initialization. */
6525 if (init)
6527 tree rinit = (TREE_CODE (init) == INIT_EXPR
6528 ? TREE_OPERAND (init, 1) : NULL_TREE);
6529 if (rinit && !TREE_SIDE_EFFECTS (rinit))
6531 /* Stick simple initializers in DECL_INITIAL so that
6532 -Wno-init-self works (c++/34772). */
6533 gcc_assert (TREE_OPERAND (init, 0) == decl);
6534 DECL_INITIAL (decl) = rinit;
6536 if (warn_init_self && TREE_CODE (type) == REFERENCE_TYPE)
6538 STRIP_NOPS (rinit);
6539 if (rinit == decl)
6540 warning_at (DECL_SOURCE_LOCATION (decl),
6541 OPT_Winit_self,
6542 "reference %qD is initialized with itself", decl);
6545 else
6547 int saved_stmts_are_full_exprs_p;
6549 /* If we're only initializing a single object, guard the
6550 destructors of any temporaries used in its initializer with
6551 its destructor. This isn't right for arrays because each
6552 element initialization is a full-expression. */
6553 if (cleanup && TREE_CODE (type) != ARRAY_TYPE)
6554 wrap_temporary_cleanups (init, cleanup);
6556 gcc_assert (building_stmt_list_p ());
6557 saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
6558 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
6559 finish_expr_stmt (init);
6560 current_stmt_tree ()->stmts_are_full_exprs_p =
6561 saved_stmts_are_full_exprs_p;
6565 /* Set this to 0 so we can tell whether an aggregate which was
6566 initialized was ever used. Don't do this if it has a
6567 destructor, so we don't complain about the 'resource
6568 allocation is initialization' idiom. Now set
6569 attribute((unused)) on types so decls of that type will be
6570 marked used. (see TREE_USED, above.) */
6571 if (TYPE_NEEDS_CONSTRUCTING (type)
6572 && ! already_used
6573 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type)
6574 && DECL_NAME (decl))
6575 TREE_USED (decl) = 0;
6576 else if (already_used)
6577 TREE_USED (decl) = 1;
6579 if (cleanup)
6580 finish_decl_cleanup (decl, cleanup);
6583 /* DECL is a VAR_DECL for a compiler-generated variable with static
6584 storage duration (like a virtual table) whose initializer is a
6585 compile-time constant. Initialize the variable and provide it to the
6586 back end. */
6588 void
6589 initialize_artificial_var (tree decl, vec<constructor_elt, va_gc> *v)
6591 tree init;
6592 gcc_assert (DECL_ARTIFICIAL (decl));
6593 init = build_constructor (TREE_TYPE (decl), v);
6594 gcc_assert (TREE_CODE (init) == CONSTRUCTOR);
6595 DECL_INITIAL (decl) = init;
6596 DECL_INITIALIZED_P (decl) = 1;
6597 determine_visibility (decl);
6598 layout_var_decl (decl);
6599 maybe_commonize_var (decl);
6600 make_rtl_for_nonlocal_decl (decl, init, /*asmspec=*/NULL);
6603 /* INIT is the initializer for a variable, as represented by the
6604 parser. Returns true iff INIT is type-dependent. */
6606 static bool
6607 type_dependent_init_p (tree init)
6609 if (TREE_CODE (init) == TREE_LIST)
6610 /* A parenthesized initializer, e.g.: int i (3, 2); ? */
6611 return any_type_dependent_elements_p (init);
6612 else if (TREE_CODE (init) == CONSTRUCTOR)
6613 /* A brace-enclosed initializer, e.g.: int i = { 3 }; ? */
6615 if (dependent_type_p (TREE_TYPE (init)))
6616 return true;
6618 vec<constructor_elt, va_gc> *elts;
6619 size_t nelts;
6620 size_t i;
6622 elts = CONSTRUCTOR_ELTS (init);
6623 nelts = vec_safe_length (elts);
6624 for (i = 0; i < nelts; ++i)
6625 if (type_dependent_init_p ((*elts)[i].value))
6626 return true;
6628 else
6629 /* It must be a simple expression, e.g., int i = 3; */
6630 return type_dependent_expression_p (init);
6632 return false;
6635 /* INIT is the initializer for a variable, as represented by the
6636 parser. Returns true iff INIT is value-dependent. */
6638 static bool
6639 value_dependent_init_p (tree init)
6641 if (TREE_CODE (init) == TREE_LIST)
6642 /* A parenthesized initializer, e.g.: int i (3, 2); ? */
6643 return any_value_dependent_elements_p (init);
6644 else if (TREE_CODE (init) == CONSTRUCTOR)
6645 /* A brace-enclosed initializer, e.g.: int i = { 3 }; ? */
6647 vec<constructor_elt, va_gc> *elts;
6648 size_t nelts;
6649 size_t i;
6651 elts = CONSTRUCTOR_ELTS (init);
6652 nelts = vec_safe_length (elts);
6653 for (i = 0; i < nelts; ++i)
6654 if (value_dependent_init_p ((*elts)[i].value))
6655 return true;
6657 else
6658 /* It must be a simple expression, e.g., int i = 3; */
6659 return value_dependent_expression_p (init);
6661 return false;
6664 // Returns true if a DECL is VAR_DECL with the concept specifier.
6665 static inline bool
6666 is_concept_var (tree decl)
6668 return (VAR_P (decl)
6669 // Not all variables have DECL_LANG_SPECIFIC.
6670 && DECL_LANG_SPECIFIC (decl)
6671 && DECL_DECLARED_CONCEPT_P (decl));
6674 /* A helper function to be called via walk_tree. If any label exists
6675 under *TP, it is (going to be) forced. Set has_forced_label_in_static. */
6677 static tree
6678 notice_forced_label_r (tree *tp, int *walk_subtrees, void *)
6680 if (TYPE_P (*tp))
6681 *walk_subtrees = 0;
6682 if (TREE_CODE (*tp) == LABEL_DECL)
6683 cfun->has_forced_label_in_static = 1;
6684 return NULL_TREE;
6687 /* Finish processing of a declaration;
6688 install its line number and initial value.
6689 If the length of an array type is not known before,
6690 it must be determined now, from the initial value, or it is an error.
6692 INIT is the initializer (if any) for DECL. If INIT_CONST_EXPR_P is
6693 true, then INIT is an integral constant expression.
6695 FLAGS is LOOKUP_ONLYCONVERTING if the = init syntax was used, else 0
6696 if the (init) syntax was used. */
6698 void
6699 cp_finish_decl (tree decl, tree init, bool init_const_expr_p,
6700 tree asmspec_tree, int flags)
6702 tree type;
6703 vec<tree, va_gc> *cleanups = NULL;
6704 const char *asmspec = NULL;
6705 int was_readonly = 0;
6706 bool var_definition_p = false;
6707 tree auto_node;
6709 if (decl == error_mark_node)
6710 return;
6711 else if (! decl)
6713 if (init)
6714 error ("assignment (not initialization) in declaration");
6715 return;
6718 gcc_assert (TREE_CODE (decl) != RESULT_DECL);
6719 /* Parameters are handled by store_parm_decls, not cp_finish_decl. */
6720 gcc_assert (TREE_CODE (decl) != PARM_DECL);
6722 type = TREE_TYPE (decl);
6723 if (type == error_mark_node)
6724 return;
6726 /* Warn about register storage specifiers except when in GNU global
6727 or local register variable extension. */
6728 if (VAR_P (decl) && DECL_REGISTER (decl) && asmspec_tree == NULL_TREE)
6730 if (cxx_dialect >= cxx17)
6731 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
6732 "ISO C++17 does not allow %<register%> storage "
6733 "class specifier");
6734 else
6735 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
6736 "%<register%> storage class specifier used");
6739 /* If a name was specified, get the string. */
6740 if (at_namespace_scope_p ())
6741 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
6742 if (asmspec_tree && asmspec_tree != error_mark_node)
6743 asmspec = TREE_STRING_POINTER (asmspec_tree);
6745 if (current_class_type
6746 && CP_DECL_CONTEXT (decl) == current_class_type
6747 && TYPE_BEING_DEFINED (current_class_type)
6748 && !CLASSTYPE_TEMPLATE_INSTANTIATION (current_class_type)
6749 && (DECL_INITIAL (decl) || init))
6750 DECL_INITIALIZED_IN_CLASS_P (decl) = 1;
6752 if (TREE_CODE (decl) != FUNCTION_DECL
6753 && (auto_node = type_uses_auto (type)))
6755 tree d_init;
6756 if (init == NULL_TREE)
6758 if (DECL_LANG_SPECIFIC (decl)
6759 && DECL_TEMPLATE_INSTANTIATION (decl)
6760 && !DECL_TEMPLATE_INSTANTIATED (decl))
6762 /* init is null because we're deferring instantiating the
6763 initializer until we need it. Well, we need it now. */
6764 instantiate_decl (decl, /*defer_ok*/true, /*expl*/false);
6765 return;
6768 gcc_assert (CLASS_PLACEHOLDER_TEMPLATE (auto_node));
6770 d_init = init;
6771 if (d_init)
6773 if (TREE_CODE (d_init) == TREE_LIST
6774 && !CLASS_PLACEHOLDER_TEMPLATE (auto_node))
6775 d_init = build_x_compound_expr_from_list (d_init, ELK_INIT,
6776 tf_warning_or_error);
6777 d_init = resolve_nondeduced_context (d_init, tf_warning_or_error);
6779 enum auto_deduction_context adc = adc_variable_type;
6780 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
6781 adc = adc_decomp_type;
6782 type = TREE_TYPE (decl) = do_auto_deduction (type, d_init, auto_node,
6783 tf_warning_or_error, adc,
6784 NULL_TREE, flags);
6785 if (type == error_mark_node)
6786 return;
6787 if (TREE_CODE (type) == FUNCTION_TYPE)
6789 error ("initializer for %<decltype(auto) %D%> has function type "
6790 "(did you forget the %<()%> ?)", decl);
6791 TREE_TYPE (decl) = error_mark_node;
6792 return;
6794 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
6797 if (!ensure_literal_type_for_constexpr_object (decl))
6798 DECL_DECLARED_CONSTEXPR_P (decl) = 0;
6800 if (VAR_P (decl)
6801 && DECL_CLASS_SCOPE_P (decl)
6802 && DECL_INITIALIZED_IN_CLASS_P (decl))
6803 check_static_variable_definition (decl, type);
6805 if (init && TREE_CODE (decl) == FUNCTION_DECL)
6807 tree clone;
6808 if (init == ridpointers[(int)RID_DELETE])
6810 /* FIXME check this is 1st decl. */
6811 DECL_DELETED_FN (decl) = 1;
6812 DECL_DECLARED_INLINE_P (decl) = 1;
6813 DECL_INITIAL (decl) = error_mark_node;
6814 FOR_EACH_CLONE (clone, decl)
6816 DECL_DELETED_FN (clone) = 1;
6817 DECL_DECLARED_INLINE_P (clone) = 1;
6818 DECL_INITIAL (clone) = error_mark_node;
6820 init = NULL_TREE;
6822 else if (init == ridpointers[(int)RID_DEFAULT])
6824 if (defaultable_fn_check (decl))
6825 DECL_DEFAULTED_FN (decl) = 1;
6826 else
6827 DECL_INITIAL (decl) = NULL_TREE;
6831 if (init && VAR_P (decl))
6833 DECL_NONTRIVIALLY_INITIALIZED_P (decl) = 1;
6834 /* If DECL is a reference, then we want to know whether init is a
6835 reference constant; init_const_expr_p as passed tells us whether
6836 it's an rvalue constant. */
6837 if (TREE_CODE (type) == REFERENCE_TYPE)
6838 init_const_expr_p = potential_constant_expression (init);
6839 if (init_const_expr_p)
6841 /* Set these flags now for templates. We'll update the flags in
6842 store_init_value for instantiations. */
6843 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
6844 if (decl_maybe_constant_var_p (decl)
6845 /* FIXME setting TREE_CONSTANT on refs breaks the back end. */
6846 && TREE_CODE (type) != REFERENCE_TYPE)
6847 TREE_CONSTANT (decl) = 1;
6851 if (processing_template_decl)
6853 bool type_dependent_p;
6855 /* Add this declaration to the statement-tree. */
6856 if (at_function_scope_p ())
6857 add_decl_expr (decl);
6859 type_dependent_p = dependent_type_p (type);
6861 if (check_for_bare_parameter_packs (init))
6863 init = NULL_TREE;
6864 DECL_INITIAL (decl) = NULL_TREE;
6867 /* Generally, initializers in templates are expanded when the
6868 template is instantiated. But, if DECL is a variable constant
6869 then it can be used in future constant expressions, so its value
6870 must be available. */
6872 if (!VAR_P (decl) || type_dependent_p)
6873 /* We can't do anything if the decl has dependent type. */;
6874 else if (init
6875 && init_const_expr_p
6876 && TREE_CODE (type) != REFERENCE_TYPE
6877 && decl_maybe_constant_var_p (decl)
6878 && !type_dependent_init_p (init)
6879 && !value_dependent_init_p (init))
6881 /* This variable seems to be a non-dependent constant, so process
6882 its initializer. If check_initializer returns non-null the
6883 initialization wasn't constant after all. */
6884 tree init_code;
6885 cleanups = make_tree_vector ();
6886 init_code = check_initializer (decl, init, flags, &cleanups);
6887 if (init_code == NULL_TREE)
6888 init = NULL_TREE;
6889 release_tree_vector (cleanups);
6891 else if (!init && is_concept_var (decl))
6892 error ("variable concept has no initializer");
6893 else if (!DECL_PRETTY_FUNCTION_P (decl))
6895 /* Deduce array size even if the initializer is dependent. */
6896 maybe_deduce_size_from_array_init (decl, init);
6897 /* And complain about multiple initializers. */
6898 if (init && TREE_CODE (init) == TREE_LIST && TREE_CHAIN (init)
6899 && !MAYBE_CLASS_TYPE_P (type))
6900 init = build_x_compound_expr_from_list (init, ELK_INIT,
6901 tf_warning_or_error);
6904 if (init)
6905 DECL_INITIAL (decl) = init;
6906 return;
6909 /* Just store non-static data member initializers for later. */
6910 if (init && TREE_CODE (decl) == FIELD_DECL)
6911 DECL_INITIAL (decl) = init;
6913 /* Take care of TYPE_DECLs up front. */
6914 if (TREE_CODE (decl) == TYPE_DECL)
6916 if (type != error_mark_node
6917 && MAYBE_CLASS_TYPE_P (type) && DECL_NAME (decl))
6919 if (TREE_TYPE (DECL_NAME (decl)) && TREE_TYPE (decl) != type)
6920 warning (0, "shadowing previous type declaration of %q#D", decl);
6921 set_identifier_type_value (DECL_NAME (decl), decl);
6924 /* If we have installed this as the canonical typedef for this
6925 type, and that type has not been defined yet, delay emitting
6926 the debug information for it, as we will emit it later. */
6927 if (TYPE_MAIN_DECL (TREE_TYPE (decl)) == decl
6928 && !COMPLETE_TYPE_P (TREE_TYPE (decl)))
6929 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
6931 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl),
6932 at_eof);
6933 return;
6936 /* A reference will be modified here, as it is initialized. */
6937 if (! DECL_EXTERNAL (decl)
6938 && TREE_READONLY (decl)
6939 && TREE_CODE (type) == REFERENCE_TYPE)
6941 was_readonly = 1;
6942 TREE_READONLY (decl) = 0;
6945 if (VAR_P (decl))
6947 /* If this is a local variable that will need a mangled name,
6948 register it now. We must do this before processing the
6949 initializer for the variable, since the initialization might
6950 require a guard variable, and since the mangled name of the
6951 guard variable will depend on the mangled name of this
6952 variable. */
6953 if (DECL_FUNCTION_SCOPE_P (decl)
6954 && TREE_STATIC (decl)
6955 && !DECL_ARTIFICIAL (decl))
6957 push_local_name (decl);
6958 /* Normally has_forced_label_in_static is set during GIMPLE
6959 lowering, but [cd]tors are never actually compiled directly.
6960 We need to set this early so we can deal with the label
6961 address extension. */
6962 if ((DECL_CONSTRUCTOR_P (current_function_decl)
6963 || DECL_DESTRUCTOR_P (current_function_decl))
6964 && init)
6966 walk_tree (&init, notice_forced_label_r, NULL, NULL);
6967 add_local_decl (cfun, decl);
6969 /* And make sure it's in the symbol table for
6970 c_parse_final_cleanups to find. */
6971 varpool_node::get_create (decl);
6974 /* Convert the initializer to the type of DECL, if we have not
6975 already initialized DECL. */
6976 if (!DECL_INITIALIZED_P (decl)
6977 /* If !DECL_EXTERNAL then DECL is being defined. In the
6978 case of a static data member initialized inside the
6979 class-specifier, there can be an initializer even if DECL
6980 is *not* defined. */
6981 && (!DECL_EXTERNAL (decl) || init))
6983 cleanups = make_tree_vector ();
6984 init = check_initializer (decl, init, flags, &cleanups);
6986 /* Handle:
6988 [dcl.init]
6990 The memory occupied by any object of static storage
6991 duration is zero-initialized at program startup before
6992 any other initialization takes place.
6994 We cannot create an appropriate initializer until after
6995 the type of DECL is finalized. If DECL_INITIAL is set,
6996 then the DECL is statically initialized, and any
6997 necessary zero-initialization has already been performed. */
6998 if (TREE_STATIC (decl) && !DECL_INITIAL (decl))
6999 DECL_INITIAL (decl) = build_zero_init (TREE_TYPE (decl),
7000 /*nelts=*/NULL_TREE,
7001 /*static_storage_p=*/true);
7002 /* Remember that the initialization for this variable has
7003 taken place. */
7004 DECL_INITIALIZED_P (decl) = 1;
7005 /* This declaration is the definition of this variable,
7006 unless we are initializing a static data member within
7007 the class specifier. */
7008 if (!DECL_EXTERNAL (decl))
7009 var_definition_p = true;
7011 /* If the variable has an array type, lay out the type, even if
7012 there is no initializer. It is valid to index through the
7013 array, and we must get TYPE_ALIGN set correctly on the array
7014 type. */
7015 else if (TREE_CODE (type) == ARRAY_TYPE)
7016 layout_type (type);
7018 if (TREE_STATIC (decl)
7019 && !at_function_scope_p ()
7020 && current_function_decl == NULL)
7021 /* So decl is a global variable or a static member of a
7022 non local class. Record the types it uses
7023 so that we can decide later to emit debug info for them. */
7024 record_types_used_by_current_var_decl (decl);
7027 /* Add this declaration to the statement-tree. This needs to happen
7028 after the call to check_initializer so that the DECL_EXPR for a
7029 reference temp is added before the DECL_EXPR for the reference itself. */
7030 if (DECL_FUNCTION_SCOPE_P (decl))
7032 /* If we're building a variable sized type, and we might be
7033 reachable other than via the top of the current binding
7034 level, then create a new BIND_EXPR so that we deallocate
7035 the object at the right time. */
7036 if (VAR_P (decl)
7037 && DECL_SIZE (decl)
7038 && !TREE_CONSTANT (DECL_SIZE (decl))
7039 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
7041 tree bind;
7042 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
7043 TREE_SIDE_EFFECTS (bind) = 1;
7044 add_stmt (bind);
7045 BIND_EXPR_BODY (bind) = push_stmt_list ();
7047 add_decl_expr (decl);
7050 /* Let the middle end know about variables and functions -- but not
7051 static data members in uninstantiated class templates. */
7052 if (VAR_OR_FUNCTION_DECL_P (decl))
7054 if (VAR_P (decl))
7056 layout_var_decl (decl);
7057 maybe_commonize_var (decl);
7060 /* This needs to happen after the linkage is set. */
7061 determine_visibility (decl);
7063 if (var_definition_p && TREE_STATIC (decl))
7065 /* If a TREE_READONLY variable needs initialization
7066 at runtime, it is no longer readonly and we need to
7067 avoid MEM_READONLY_P being set on RTL created for it. */
7068 if (init)
7070 if (TREE_READONLY (decl))
7071 TREE_READONLY (decl) = 0;
7072 was_readonly = 0;
7074 else if (was_readonly)
7075 TREE_READONLY (decl) = 1;
7077 /* Likewise if it needs destruction. */
7078 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
7079 TREE_READONLY (decl) = 0;
7082 make_rtl_for_nonlocal_decl (decl, init, asmspec);
7084 /* Check for abstractness of the type. Notice that there is no
7085 need to strip array types here since the check for those types
7086 is already done within create_array_type_for_decl. */
7087 abstract_virtuals_error (decl, type);
7089 if (TREE_TYPE (decl) == error_mark_node)
7090 /* No initialization required. */
7092 else if (TREE_CODE (decl) == FUNCTION_DECL)
7094 if (init)
7096 if (init == ridpointers[(int)RID_DEFAULT])
7098 /* An out-of-class default definition is defined at
7099 the point where it is explicitly defaulted. */
7100 if (DECL_DELETED_FN (decl))
7101 maybe_explain_implicit_delete (decl);
7102 else if (DECL_INITIAL (decl) == error_mark_node)
7103 synthesize_method (decl);
7105 else
7106 error ("function %q#D is initialized like a variable", decl);
7108 /* else no initialization required. */
7110 else if (DECL_EXTERNAL (decl)
7111 && ! (DECL_LANG_SPECIFIC (decl)
7112 && DECL_NOT_REALLY_EXTERN (decl)))
7114 if (init)
7115 DECL_INITIAL (decl) = init;
7117 /* A variable definition. */
7118 else if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
7119 /* Initialize the local variable. */
7120 initialize_local_var (decl, init);
7122 /* If a variable is defined, and then a subsequent
7123 definition with external linkage is encountered, we will
7124 get here twice for the same variable. We want to avoid
7125 calling expand_static_init more than once. For variables
7126 that are not static data members, we can call
7127 expand_static_init only when we actually process the
7128 initializer. It is not legal to redeclare a static data
7129 member, so this issue does not arise in that case. */
7130 else if (var_definition_p && TREE_STATIC (decl))
7131 expand_static_init (decl, init);
7134 /* If a CLEANUP_STMT was created to destroy a temporary bound to a
7135 reference, insert it in the statement-tree now. */
7136 if (cleanups)
7138 unsigned i; tree t;
7139 FOR_EACH_VEC_ELT (*cleanups, i, t)
7140 push_cleanup (decl, t, false);
7141 release_tree_vector (cleanups);
7144 if (was_readonly)
7145 TREE_READONLY (decl) = 1;
7147 invoke_plugin_callbacks (PLUGIN_FINISH_DECL, decl);
7150 /* For class TYPE return itself or some its bases that contain
7151 any direct non-static data members. Return error_mark_node if an
7152 error has been diagnosed. */
7154 static tree
7155 find_decomp_class_base (location_t loc, tree type, tree ret)
7157 bool member_seen = false;
7158 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
7159 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
7160 continue;
7161 else if (ret)
7162 return type;
7163 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
7165 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE)
7166 error_at (loc, "cannot decompose class type %qT because it has an "
7167 "anonymous struct member", type);
7168 else
7169 error_at (loc, "cannot decompose class type %qT because it has an "
7170 "anonymous union member", type);
7171 inform (DECL_SOURCE_LOCATION (field), "declared here");
7172 return error_mark_node;
7174 else if (TREE_PRIVATE (field) || TREE_PROTECTED (field))
7176 error_at (loc, "cannot decompose non-public member %qD of %qT",
7177 field, type);
7178 inform (DECL_SOURCE_LOCATION (field),
7179 TREE_PRIVATE (field)
7180 ? G_("declared private here")
7181 : G_("declared protected here"));
7182 return error_mark_node;
7184 else
7185 member_seen = true;
7187 tree base_binfo, binfo;
7188 tree orig_ret = ret;
7189 int i;
7190 if (member_seen)
7191 ret = type;
7192 for (binfo = TYPE_BINFO (type), i = 0;
7193 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
7195 tree t = find_decomp_class_base (loc, TREE_TYPE (base_binfo), ret);
7196 if (t == error_mark_node)
7197 return error_mark_node;
7198 if (t != NULL_TREE)
7200 if (ret == type)
7202 error_at (loc, "cannot decompose class type %qT: both it and "
7203 "its base class %qT have non-static data members",
7204 type, t);
7205 return error_mark_node;
7207 else if (orig_ret != NULL_TREE)
7208 return t;
7209 else if (ret == t)
7210 /* OK, found the same base along another path. We'll complain
7211 in convert_to_base if it's ambiguous. */;
7212 else if (ret != NULL_TREE)
7214 error_at (loc, "cannot decompose class type %qT: its base "
7215 "classes %qT and %qT have non-static data "
7216 "members", type, ret, t);
7217 return error_mark_node;
7219 else
7220 ret = t;
7223 return ret;
7226 /* Return std::tuple_size<TYPE>::value. */
7228 static tree
7229 get_tuple_size (tree type)
7231 tree args = make_tree_vec (1);
7232 TREE_VEC_ELT (args, 0) = type;
7233 tree inst = lookup_template_class (get_identifier ("tuple_size"), args,
7234 /*in_decl*/NULL_TREE,
7235 /*context*/std_node,
7236 /*entering_scope*/false, tf_none);
7237 inst = complete_type (inst);
7238 if (inst == error_mark_node || !COMPLETE_TYPE_P (inst))
7239 return NULL_TREE;
7240 tree val = lookup_qualified_name (inst, get_identifier ("value"),
7241 /*type*/false, /*complain*/false);
7242 if (TREE_CODE (val) == VAR_DECL || TREE_CODE (val) == CONST_DECL)
7243 val = maybe_constant_value (val);
7244 if (TREE_CODE (val) == INTEGER_CST)
7245 return val;
7246 else
7247 return error_mark_node;
7250 /* Return std::tuple_element<I,TYPE>::type. */
7252 static tree
7253 get_tuple_element_type (tree type, unsigned i)
7255 tree args = make_tree_vec (2);
7256 TREE_VEC_ELT (args, 0) = build_int_cst (integer_type_node, i);
7257 TREE_VEC_ELT (args, 1) = type;
7258 tree inst = lookup_template_class (get_identifier ("tuple_element"), args,
7259 /*in_decl*/NULL_TREE,
7260 /*context*/std_node,
7261 /*entering_scope*/false,
7262 tf_warning_or_error);
7263 return make_typename_type (inst, get_identifier ("type"),
7264 none_type, tf_warning_or_error);
7267 /* Return e.get<i>() or get<i>(e). */
7269 static tree
7270 get_tuple_decomp_init (tree decl, unsigned i)
7272 tree get_id = get_identifier ("get");
7273 tree targs = make_tree_vec (1);
7274 TREE_VEC_ELT (targs, 0) = build_int_cst (integer_type_node, i);
7276 tree etype = TREE_TYPE (decl);
7277 tree e = convert_from_reference (decl);
7279 /* [The id-expression] e is an lvalue if the type of the entity e is an
7280 lvalue reference and an xvalue otherwise. */
7281 if (TREE_CODE (etype) != REFERENCE_TYPE
7282 || TYPE_REF_IS_RVALUE (etype))
7283 e = move (e);
7285 tree fns = lookup_qualified_name (TREE_TYPE (e), get_id,
7286 /*type*/false, /*complain*/false);
7287 if (fns != error_mark_node)
7289 fns = lookup_template_function (fns, targs);
7290 return build_new_method_call (e, fns, /*args*/NULL,
7291 /*path*/NULL_TREE, LOOKUP_NORMAL,
7292 /*fn_p*/NULL, tf_warning_or_error);
7294 else
7296 vec<tree,va_gc> *args = make_tree_vector_single (e);
7297 fns = lookup_template_function (get_id, targs);
7298 fns = perform_koenig_lookup (fns, args, tf_warning_or_error);
7299 return finish_call_expr (fns, &args, /*novirt*/false,
7300 /*koenig*/true, tf_warning_or_error);
7304 /* It's impossible to recover the decltype of a tuple decomposition variable
7305 based on the actual type of the variable, so store it in a hash table. */
7306 static GTY(()) hash_map<tree,tree> *decomp_type_table;
7307 static void
7308 store_decomp_type (tree v, tree t)
7310 if (!decomp_type_table)
7311 decomp_type_table = hash_map<tree,tree>::create_ggc (13);
7312 decomp_type_table->put (v, t);
7315 tree
7316 lookup_decomp_type (tree v)
7318 return *decomp_type_table->get (v);
7321 /* Finish a decomposition declaration. DECL is the underlying declaration
7322 "e", FIRST is the head of a chain of decls for the individual identifiers
7323 chained through DECL_CHAIN in reverse order and COUNT is the number of
7324 those decls. */
7326 void
7327 cp_finish_decomp (tree decl, tree first, unsigned int count)
7329 if (error_operand_p (decl))
7331 error_out:
7332 while (count--)
7334 TREE_TYPE (first) = error_mark_node;
7335 if (DECL_HAS_VALUE_EXPR_P (first))
7337 SET_DECL_VALUE_EXPR (first, NULL_TREE);
7338 DECL_HAS_VALUE_EXPR_P (first) = 0;
7340 first = DECL_CHAIN (first);
7342 if (DECL_P (decl) && DECL_NAMESPACE_SCOPE_P (decl))
7343 SET_DECL_ASSEMBLER_NAME (decl, get_identifier ("<decomp>"));
7344 return;
7347 location_t loc = DECL_SOURCE_LOCATION (decl);
7348 if (type_dependent_expression_p (decl)
7349 /* This happens for range for when not in templates.
7350 Still add the DECL_VALUE_EXPRs for later processing. */
7351 || (!processing_template_decl
7352 && type_uses_auto (TREE_TYPE (decl))))
7354 for (unsigned int i = 0; i < count; i++)
7356 if (!DECL_HAS_VALUE_EXPR_P (first))
7358 tree v = build_nt (ARRAY_REF, decl,
7359 size_int (count - i - 1),
7360 NULL_TREE, NULL_TREE);
7361 SET_DECL_VALUE_EXPR (first, v);
7362 DECL_HAS_VALUE_EXPR_P (first) = 1;
7364 if (processing_template_decl)
7365 fit_decomposition_lang_decl (first, decl);
7366 first = DECL_CHAIN (first);
7368 return;
7371 auto_vec<tree, 16> v;
7372 v.safe_grow (count);
7373 tree d = first;
7374 for (unsigned int i = 0; i < count; i++, d = DECL_CHAIN (d))
7376 v[count - i - 1] = d;
7377 fit_decomposition_lang_decl (d, decl);
7380 tree type = TREE_TYPE (decl);
7381 tree dexp = decl;
7383 if (TREE_CODE (type) == REFERENCE_TYPE)
7385 dexp = convert_from_reference (dexp);
7386 type = TREE_TYPE (type);
7389 tree eltype = NULL_TREE;
7390 unsigned HOST_WIDE_INT eltscnt = 0;
7391 if (TREE_CODE (type) == ARRAY_TYPE)
7393 tree nelts;
7394 nelts = array_type_nelts_top (type);
7395 if (nelts == error_mark_node)
7396 goto error_out;
7397 if (!tree_fits_uhwi_p (nelts))
7399 error_at (loc, "cannot decompose variable length array %qT", type);
7400 goto error_out;
7402 eltscnt = tree_to_uhwi (nelts);
7403 if (count != eltscnt)
7405 cnt_mismatch:
7406 if (count > eltscnt)
7407 error_at (loc, "%u names provided while %qT decomposes into "
7408 "%wu elements", count, type, eltscnt);
7409 else
7410 error_at (loc, "only %u names provided while %qT decomposes into "
7411 "%wu elements", count, type, eltscnt);
7412 goto error_out;
7414 eltype = TREE_TYPE (type);
7415 for (unsigned int i = 0; i < count; i++)
7417 TREE_TYPE (v[i]) = eltype;
7418 layout_decl (v[i], 0);
7419 if (processing_template_decl)
7420 continue;
7421 tree t = unshare_expr (dexp);
7422 t = build4_loc (DECL_SOURCE_LOCATION (v[i]), ARRAY_REF,
7423 eltype, t, size_int (i), NULL_TREE,
7424 NULL_TREE);
7425 SET_DECL_VALUE_EXPR (v[i], t);
7426 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
7429 /* 2 GNU extensions. */
7430 else if (TREE_CODE (type) == COMPLEX_TYPE)
7432 eltscnt = 2;
7433 if (count != eltscnt)
7434 goto cnt_mismatch;
7435 eltype = cp_build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
7436 for (unsigned int i = 0; i < count; i++)
7438 TREE_TYPE (v[i]) = eltype;
7439 layout_decl (v[i], 0);
7440 if (processing_template_decl)
7441 continue;
7442 tree t = unshare_expr (dexp);
7443 t = build1_loc (DECL_SOURCE_LOCATION (v[i]),
7444 i ? IMAGPART_EXPR : REALPART_EXPR, eltype,
7446 SET_DECL_VALUE_EXPR (v[i], t);
7447 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
7450 else if (TREE_CODE (type) == VECTOR_TYPE)
7452 eltscnt = TYPE_VECTOR_SUBPARTS (type);
7453 if (count != eltscnt)
7454 goto cnt_mismatch;
7455 eltype = cp_build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
7456 for (unsigned int i = 0; i < count; i++)
7458 TREE_TYPE (v[i]) = eltype;
7459 layout_decl (v[i], 0);
7460 if (processing_template_decl)
7461 continue;
7462 tree t = unshare_expr (dexp);
7463 convert_vector_to_array_for_subscript (DECL_SOURCE_LOCATION (v[i]),
7464 &t, size_int (i));
7465 t = build4_loc (DECL_SOURCE_LOCATION (v[i]), ARRAY_REF,
7466 eltype, t, size_int (i), NULL_TREE,
7467 NULL_TREE);
7468 SET_DECL_VALUE_EXPR (v[i], t);
7469 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
7472 else if (tree tsize = get_tuple_size (type))
7474 if (tsize == error_mark_node)
7476 error_at (loc, "%<std::tuple_size<%T>::value%> is not an integral "
7477 "constant expression", type);
7478 goto error_out;
7480 eltscnt = tree_to_uhwi (tsize);
7481 if (count != eltscnt)
7482 goto cnt_mismatch;
7483 int save_read = DECL_READ_P (decl);
7484 for (unsigned i = 0; i < count; ++i)
7486 location_t sloc = input_location;
7487 location_t dloc = DECL_SOURCE_LOCATION (v[i]);
7489 input_location = dloc;
7490 tree init = get_tuple_decomp_init (decl, i);
7491 tree eltype = (init == error_mark_node ? error_mark_node
7492 : get_tuple_element_type (type, i));
7493 input_location = sloc;
7495 if (init == error_mark_node || eltype == error_mark_node)
7497 inform (dloc, "in initialization of structured binding "
7498 "variable %qD", v[i]);
7499 goto error_out;
7501 /* Save the decltype away before reference collapse. */
7502 store_decomp_type (v[i], eltype);
7503 eltype = cp_build_reference_type (eltype, !lvalue_p (init));
7504 TREE_TYPE (v[i]) = eltype;
7505 layout_decl (v[i], 0);
7506 if (DECL_HAS_VALUE_EXPR_P (v[i]))
7508 /* In this case the names are variables, not just proxies. */
7509 SET_DECL_VALUE_EXPR (v[i], NULL_TREE);
7510 DECL_HAS_VALUE_EXPR_P (v[i]) = 0;
7512 if (!processing_template_decl)
7513 cp_finish_decl (v[i], init, /*constexpr*/false,
7514 /*asm*/NULL_TREE, LOOKUP_NORMAL);
7516 /* Ignore reads from the underlying decl performed during initialization
7517 of the individual variables. If those will be read, we'll mark
7518 the underlying decl as read at that point. */
7519 DECL_READ_P (decl) = save_read;
7521 else if (TREE_CODE (type) == UNION_TYPE)
7523 error_at (loc, "cannot decompose union type %qT", type);
7524 goto error_out;
7526 else if (!CLASS_TYPE_P (type))
7528 error_at (loc, "cannot decompose non-array non-class type %qT", type);
7529 goto error_out;
7531 else if (LAMBDA_TYPE_P (type))
7533 error_at (loc, "cannot decompose lambda closure type %qT", type);
7534 goto error_out;
7536 else
7538 tree btype = find_decomp_class_base (loc, type, NULL_TREE);
7539 if (btype == error_mark_node)
7540 goto error_out;
7541 else if (btype == NULL_TREE)
7543 error_at (loc, "cannot decompose class type %qT without non-static "
7544 "data members", type);
7545 goto error_out;
7547 for (tree field = TYPE_FIELDS (btype); field; field = TREE_CHAIN (field))
7548 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
7549 continue;
7550 else
7551 eltscnt++;
7552 if (count != eltscnt)
7553 goto cnt_mismatch;
7554 tree t = dexp;
7555 if (type != btype)
7557 t = convert_to_base (t, btype, /*check_access*/true,
7558 /*nonnull*/false, tf_warning_or_error);
7559 type = btype;
7561 unsigned int i = 0;
7562 for (tree field = TYPE_FIELDS (btype); field; field = TREE_CHAIN (field))
7563 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
7564 continue;
7565 else
7567 tree tt = finish_non_static_data_member (field, unshare_expr (t),
7568 NULL_TREE);
7569 if (REFERENCE_REF_P (tt))
7570 tt = TREE_OPERAND (tt, 0);
7571 TREE_TYPE (v[i]) = TREE_TYPE (tt);
7572 layout_decl (v[i], 0);
7573 if (!processing_template_decl)
7575 SET_DECL_VALUE_EXPR (v[i], tt);
7576 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
7578 i++;
7581 if (processing_template_decl)
7583 for (unsigned int i = 0; i < count; i++)
7584 if (!DECL_HAS_VALUE_EXPR_P (v[i]))
7586 tree a = build_nt (ARRAY_REF, decl, size_int (i),
7587 NULL_TREE, NULL_TREE);
7588 SET_DECL_VALUE_EXPR (v[i], a);
7589 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
7592 else if (DECL_NAMESPACE_SCOPE_P (decl))
7593 SET_DECL_ASSEMBLER_NAME (decl, mangle_decomp (decl, v));
7596 /* Returns a declaration for a VAR_DECL as if:
7598 extern "C" TYPE NAME;
7600 had been seen. Used to create compiler-generated global
7601 variables. */
7603 static tree
7604 declare_global_var (tree name, tree type)
7606 tree decl;
7608 push_to_top_level ();
7609 decl = build_decl (input_location, VAR_DECL, name, type);
7610 TREE_PUBLIC (decl) = 1;
7611 DECL_EXTERNAL (decl) = 1;
7612 DECL_ARTIFICIAL (decl) = 1;
7613 DECL_CONTEXT (decl) = FROB_CONTEXT (global_namespace);
7614 /* If the user has explicitly declared this variable (perhaps
7615 because the code we are compiling is part of a low-level runtime
7616 library), then it is possible that our declaration will be merged
7617 with theirs by pushdecl. */
7618 decl = pushdecl (decl);
7619 cp_finish_decl (decl, NULL_TREE, false, NULL_TREE, 0);
7620 pop_from_top_level ();
7622 return decl;
7625 /* Returns the type for the argument to "__cxa_atexit" (or "atexit",
7626 if "__cxa_atexit" is not being used) corresponding to the function
7627 to be called when the program exits. */
7629 static tree
7630 get_atexit_fn_ptr_type (void)
7632 tree fn_type;
7634 if (!atexit_fn_ptr_type_node)
7636 tree arg_type;
7637 if (flag_use_cxa_atexit
7638 && !targetm.cxx.use_atexit_for_cxa_atexit ())
7639 /* The parameter to "__cxa_atexit" is "void (*)(void *)". */
7640 arg_type = ptr_type_node;
7641 else
7642 /* The parameter to "atexit" is "void (*)(void)". */
7643 arg_type = NULL_TREE;
7645 fn_type = build_function_type_list (void_type_node,
7646 arg_type, NULL_TREE);
7647 atexit_fn_ptr_type_node = build_pointer_type (fn_type);
7650 return atexit_fn_ptr_type_node;
7653 /* Returns a pointer to the `atexit' function. Note that if
7654 FLAG_USE_CXA_ATEXIT is nonzero, then this will actually be the new
7655 `__cxa_atexit' function specified in the IA64 C++ ABI. */
7657 static tree
7658 get_atexit_node (void)
7660 tree atexit_fndecl;
7661 tree fn_type;
7662 tree fn_ptr_type;
7663 const char *name;
7664 bool use_aeabi_atexit;
7666 if (atexit_node)
7667 return atexit_node;
7669 if (flag_use_cxa_atexit && !targetm.cxx.use_atexit_for_cxa_atexit ())
7671 /* The declaration for `__cxa_atexit' is:
7673 int __cxa_atexit (void (*)(void *), void *, void *)
7675 We build up the argument types and then the function type
7676 itself. */
7677 tree argtype0, argtype1, argtype2;
7679 use_aeabi_atexit = targetm.cxx.use_aeabi_atexit ();
7680 /* First, build the pointer-to-function type for the first
7681 argument. */
7682 fn_ptr_type = get_atexit_fn_ptr_type ();
7683 /* Then, build the rest of the argument types. */
7684 argtype2 = ptr_type_node;
7685 if (use_aeabi_atexit)
7687 argtype1 = fn_ptr_type;
7688 argtype0 = ptr_type_node;
7690 else
7692 argtype1 = ptr_type_node;
7693 argtype0 = fn_ptr_type;
7695 /* And the final __cxa_atexit type. */
7696 fn_type = build_function_type_list (integer_type_node,
7697 argtype0, argtype1, argtype2,
7698 NULL_TREE);
7699 if (use_aeabi_atexit)
7700 name = "__aeabi_atexit";
7701 else
7702 name = "__cxa_atexit";
7704 else
7706 /* The declaration for `atexit' is:
7708 int atexit (void (*)());
7710 We build up the argument types and then the function type
7711 itself. */
7712 fn_ptr_type = get_atexit_fn_ptr_type ();
7713 /* Build the final atexit type. */
7714 fn_type = build_function_type_list (integer_type_node,
7715 fn_ptr_type, NULL_TREE);
7716 name = "atexit";
7719 /* Now, build the function declaration. */
7720 push_lang_context (lang_name_c);
7721 atexit_fndecl = build_library_fn_ptr (name, fn_type, ECF_LEAF | ECF_NOTHROW);
7722 mark_used (atexit_fndecl);
7723 pop_lang_context ();
7724 atexit_node = decay_conversion (atexit_fndecl, tf_warning_or_error);
7726 return atexit_node;
7729 /* Like get_atexit_node, but for thread-local cleanups. */
7731 static tree
7732 get_thread_atexit_node (void)
7734 /* The declaration for `__cxa_thread_atexit' is:
7736 int __cxa_thread_atexit (void (*)(void *), void *, void *) */
7737 tree fn_type = build_function_type_list (integer_type_node,
7738 get_atexit_fn_ptr_type (),
7739 ptr_type_node, ptr_type_node,
7740 NULL_TREE);
7742 /* Now, build the function declaration. */
7743 tree atexit_fndecl = build_library_fn_ptr ("__cxa_thread_atexit", fn_type,
7744 ECF_LEAF | ECF_NOTHROW);
7745 return decay_conversion (atexit_fndecl, tf_warning_or_error);
7748 /* Returns the __dso_handle VAR_DECL. */
7750 static tree
7751 get_dso_handle_node (void)
7753 if (dso_handle_node)
7754 return dso_handle_node;
7756 /* Declare the variable. */
7757 dso_handle_node = declare_global_var (get_identifier ("__dso_handle"),
7758 ptr_type_node);
7760 #ifdef HAVE_GAS_HIDDEN
7761 if (dso_handle_node != error_mark_node)
7763 DECL_VISIBILITY (dso_handle_node) = VISIBILITY_HIDDEN;
7764 DECL_VISIBILITY_SPECIFIED (dso_handle_node) = 1;
7766 #endif
7768 return dso_handle_node;
7771 /* Begin a new function with internal linkage whose job will be simply
7772 to destroy some particular variable. */
7774 static GTY(()) int start_cleanup_cnt;
7776 static tree
7777 start_cleanup_fn (void)
7779 char name[32];
7780 tree fntype;
7781 tree fndecl;
7782 bool use_cxa_atexit = flag_use_cxa_atexit
7783 && !targetm.cxx.use_atexit_for_cxa_atexit ();
7785 push_to_top_level ();
7787 /* No need to mangle this. */
7788 push_lang_context (lang_name_c);
7790 /* Build the name of the function. */
7791 sprintf (name, "__tcf_%d", start_cleanup_cnt++);
7792 /* Build the function declaration. */
7793 fntype = TREE_TYPE (get_atexit_fn_ptr_type ());
7794 fndecl = build_lang_decl (FUNCTION_DECL, get_identifier (name), fntype);
7795 /* It's a function with internal linkage, generated by the
7796 compiler. */
7797 TREE_PUBLIC (fndecl) = 0;
7798 DECL_ARTIFICIAL (fndecl) = 1;
7799 /* Make the function `inline' so that it is only emitted if it is
7800 actually needed. It is unlikely that it will be inlined, since
7801 it is only called via a function pointer, but we avoid unnecessary
7802 emissions this way. */
7803 DECL_DECLARED_INLINE_P (fndecl) = 1;
7804 DECL_INTERFACE_KNOWN (fndecl) = 1;
7805 /* Build the parameter. */
7806 if (use_cxa_atexit)
7808 tree parmdecl = cp_build_parm_decl (fndecl, NULL_TREE, ptr_type_node);
7809 TREE_USED (parmdecl) = 1;
7810 DECL_READ_P (parmdecl) = 1;
7811 DECL_ARGUMENTS (fndecl) = parmdecl;
7814 pushdecl (fndecl);
7815 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
7817 pop_lang_context ();
7819 return current_function_decl;
7822 /* Finish the cleanup function begun by start_cleanup_fn. */
7824 static void
7825 end_cleanup_fn (void)
7827 expand_or_defer_fn (finish_function (0));
7829 pop_from_top_level ();
7832 /* Generate code to handle the destruction of DECL, an object with
7833 static storage duration. */
7835 tree
7836 register_dtor_fn (tree decl)
7838 tree cleanup;
7839 tree addr;
7840 tree compound_stmt;
7841 tree fcall;
7842 tree type;
7843 bool ob_parm, dso_parm, use_dtor;
7844 tree arg0, arg1, arg2;
7845 tree atex_node;
7847 type = TREE_TYPE (decl);
7848 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
7849 return void_node;
7851 /* If we're using "__cxa_atexit" (or "__cxa_thread_atexit" or
7852 "__aeabi_atexit"), and DECL is a class object, we can just pass the
7853 destructor to "__cxa_atexit"; we don't have to build a temporary
7854 function to do the cleanup. */
7855 dso_parm = (flag_use_cxa_atexit
7856 && !targetm.cxx.use_atexit_for_cxa_atexit ());
7857 ob_parm = (CP_DECL_THREAD_LOCAL_P (decl) || dso_parm);
7858 use_dtor = ob_parm && CLASS_TYPE_P (type);
7859 if (use_dtor)
7861 cleanup = get_class_binding (type, complete_dtor_identifier);
7863 /* Make sure it is accessible. */
7864 perform_or_defer_access_check (TYPE_BINFO (type), cleanup, cleanup,
7865 tf_warning_or_error);
7867 else
7869 /* Call build_cleanup before we enter the anonymous function so
7870 that any access checks will be done relative to the current
7871 scope, rather than the scope of the anonymous function. */
7872 build_cleanup (decl);
7874 /* Now start the function. */
7875 cleanup = start_cleanup_fn ();
7877 /* Now, recompute the cleanup. It may contain SAVE_EXPRs that refer
7878 to the original function, rather than the anonymous one. That
7879 will make the back end think that nested functions are in use,
7880 which causes confusion. */
7881 push_deferring_access_checks (dk_no_check);
7882 fcall = build_cleanup (decl);
7883 pop_deferring_access_checks ();
7885 /* Create the body of the anonymous function. */
7886 compound_stmt = begin_compound_stmt (BCS_FN_BODY);
7887 finish_expr_stmt (fcall);
7888 finish_compound_stmt (compound_stmt);
7889 end_cleanup_fn ();
7892 /* Call atexit with the cleanup function. */
7893 mark_used (cleanup);
7894 cleanup = build_address (cleanup);
7896 if (CP_DECL_THREAD_LOCAL_P (decl))
7897 atex_node = get_thread_atexit_node ();
7898 else
7899 atex_node = get_atexit_node ();
7901 if (use_dtor)
7903 /* We must convert CLEANUP to the type that "__cxa_atexit"
7904 expects. */
7905 cleanup = build_nop (get_atexit_fn_ptr_type (), cleanup);
7906 /* "__cxa_atexit" will pass the address of DECL to the
7907 cleanup function. */
7908 mark_used (decl);
7909 addr = build_address (decl);
7910 /* The declared type of the parameter to "__cxa_atexit" is
7911 "void *". For plain "T*", we could just let the
7912 machinery in cp_build_function_call convert it -- but if the
7913 type is "cv-qualified T *", then we need to convert it
7914 before passing it in, to avoid spurious errors. */
7915 addr = build_nop (ptr_type_node, addr);
7917 else
7918 /* Since the cleanup functions we build ignore the address
7919 they're given, there's no reason to pass the actual address
7920 in, and, in general, it's cheaper to pass NULL than any
7921 other value. */
7922 addr = null_pointer_node;
7924 if (dso_parm)
7925 arg2 = cp_build_addr_expr (get_dso_handle_node (),
7926 tf_warning_or_error);
7927 else if (ob_parm)
7928 /* Just pass NULL to the dso handle parm if we don't actually
7929 have a DSO handle on this target. */
7930 arg2 = null_pointer_node;
7931 else
7932 arg2 = NULL_TREE;
7934 if (ob_parm)
7936 if (!CP_DECL_THREAD_LOCAL_P (decl)
7937 && targetm.cxx.use_aeabi_atexit ())
7939 arg1 = cleanup;
7940 arg0 = addr;
7942 else
7944 arg1 = addr;
7945 arg0 = cleanup;
7948 else
7950 arg0 = cleanup;
7951 arg1 = NULL_TREE;
7953 return cp_build_function_call_nary (atex_node, tf_warning_or_error,
7954 arg0, arg1, arg2, NULL_TREE);
7957 /* DECL is a VAR_DECL with static storage duration. INIT, if present,
7958 is its initializer. Generate code to handle the construction
7959 and destruction of DECL. */
7961 static void
7962 expand_static_init (tree decl, tree init)
7964 gcc_assert (VAR_P (decl));
7965 gcc_assert (TREE_STATIC (decl));
7967 /* Some variables require no dynamic initialization. */
7968 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
7970 /* Make sure the destructor is callable. */
7971 cxx_maybe_build_cleanup (decl, tf_warning_or_error);
7972 if (!init)
7973 return;
7976 if (CP_DECL_THREAD_LOCAL_P (decl) && DECL_GNU_TLS_P (decl)
7977 && !DECL_FUNCTION_SCOPE_P (decl))
7979 if (init)
7980 error ("non-local variable %qD declared %<__thread%> "
7981 "needs dynamic initialization", decl);
7982 else
7983 error ("non-local variable %qD declared %<__thread%> "
7984 "has a non-trivial destructor", decl);
7985 static bool informed;
7986 if (!informed)
7988 inform (DECL_SOURCE_LOCATION (decl),
7989 "C++11 %<thread_local%> allows dynamic initialization "
7990 "and destruction");
7991 informed = true;
7993 return;
7996 if (DECL_FUNCTION_SCOPE_P (decl))
7998 /* Emit code to perform this initialization but once. */
7999 tree if_stmt = NULL_TREE, inner_if_stmt = NULL_TREE;
8000 tree then_clause = NULL_TREE, inner_then_clause = NULL_TREE;
8001 tree guard, guard_addr;
8002 tree flag, begin;
8003 /* We don't need thread-safety code for thread-local vars. */
8004 bool thread_guard = (flag_threadsafe_statics
8005 && !CP_DECL_THREAD_LOCAL_P (decl));
8007 /* Emit code to perform this initialization but once. This code
8008 looks like:
8010 static <type> guard;
8011 if (!__atomic_load (guard.first_byte)) {
8012 if (__cxa_guard_acquire (&guard)) {
8013 bool flag = false;
8014 try {
8015 // Do initialization.
8016 flag = true; __cxa_guard_release (&guard);
8017 // Register variable for destruction at end of program.
8018 } catch {
8019 if (!flag) __cxa_guard_abort (&guard);
8024 Note that the `flag' variable is only set to 1 *after* the
8025 initialization is complete. This ensures that an exception,
8026 thrown during the construction, will cause the variable to
8027 reinitialized when we pass through this code again, as per:
8029 [stmt.dcl]
8031 If the initialization exits by throwing an exception, the
8032 initialization is not complete, so it will be tried again
8033 the next time control enters the declaration.
8035 This process should be thread-safe, too; multiple threads
8036 should not be able to initialize the variable more than
8037 once. */
8039 /* Create the guard variable. */
8040 guard = get_guard (decl);
8042 /* Begin the conditional initialization. */
8043 if_stmt = begin_if_stmt ();
8045 finish_if_stmt_cond (get_guard_cond (guard, thread_guard), if_stmt);
8046 then_clause = begin_compound_stmt (BCS_NO_SCOPE);
8048 if (thread_guard)
8050 tree vfntype = NULL_TREE;
8051 tree acquire_name, release_name, abort_name;
8052 tree acquire_fn, release_fn, abort_fn;
8053 guard_addr = build_address (guard);
8055 acquire_name = get_identifier ("__cxa_guard_acquire");
8056 release_name = get_identifier ("__cxa_guard_release");
8057 abort_name = get_identifier ("__cxa_guard_abort");
8058 acquire_fn = identifier_global_value (acquire_name);
8059 release_fn = identifier_global_value (release_name);
8060 abort_fn = identifier_global_value (abort_name);
8061 if (!acquire_fn)
8062 acquire_fn = push_library_fn
8063 (acquire_name, build_function_type_list (integer_type_node,
8064 TREE_TYPE (guard_addr),
8065 NULL_TREE),
8066 NULL_TREE, ECF_NOTHROW | ECF_LEAF);
8067 if (!release_fn || !abort_fn)
8068 vfntype = build_function_type_list (void_type_node,
8069 TREE_TYPE (guard_addr),
8070 NULL_TREE);
8071 if (!release_fn)
8072 release_fn = push_library_fn (release_name, vfntype, NULL_TREE,
8073 ECF_NOTHROW | ECF_LEAF);
8074 if (!abort_fn)
8075 abort_fn = push_library_fn (abort_name, vfntype, NULL_TREE,
8076 ECF_NOTHROW | ECF_LEAF);
8078 inner_if_stmt = begin_if_stmt ();
8079 finish_if_stmt_cond (build_call_n (acquire_fn, 1, guard_addr),
8080 inner_if_stmt);
8082 inner_then_clause = begin_compound_stmt (BCS_NO_SCOPE);
8083 begin = get_target_expr (boolean_false_node);
8084 flag = TARGET_EXPR_SLOT (begin);
8086 TARGET_EXPR_CLEANUP (begin)
8087 = build3 (COND_EXPR, void_type_node, flag,
8088 void_node,
8089 build_call_n (abort_fn, 1, guard_addr));
8090 CLEANUP_EH_ONLY (begin) = 1;
8092 /* Do the initialization itself. */
8093 init = add_stmt_to_compound (begin, init);
8094 init = add_stmt_to_compound
8095 (init, build2 (MODIFY_EXPR, void_type_node, flag, boolean_true_node));
8096 init = add_stmt_to_compound
8097 (init, build_call_n (release_fn, 1, guard_addr));
8099 else
8100 init = add_stmt_to_compound (init, set_guard (guard));
8102 /* Use atexit to register a function for destroying this static
8103 variable. */
8104 init = add_stmt_to_compound (init, register_dtor_fn (decl));
8106 finish_expr_stmt (init);
8108 if (thread_guard)
8110 finish_compound_stmt (inner_then_clause);
8111 finish_then_clause (inner_if_stmt);
8112 finish_if_stmt (inner_if_stmt);
8115 finish_compound_stmt (then_clause);
8116 finish_then_clause (if_stmt);
8117 finish_if_stmt (if_stmt);
8119 else if (CP_DECL_THREAD_LOCAL_P (decl))
8120 tls_aggregates = tree_cons (init, decl, tls_aggregates);
8121 else
8122 static_aggregates = tree_cons (init, decl, static_aggregates);
8126 /* Make TYPE a complete type based on INITIAL_VALUE.
8127 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
8128 2 if there was no information (in which case assume 0 if DO_DEFAULT),
8129 3 if the initializer list is empty (in pedantic mode). */
8132 cp_complete_array_type (tree *ptype, tree initial_value, bool do_default)
8134 int failure;
8135 tree type, elt_type;
8137 /* Don't get confused by a CONSTRUCTOR for some other type. */
8138 if (initial_value && TREE_CODE (initial_value) == CONSTRUCTOR
8139 && !BRACE_ENCLOSED_INITIALIZER_P (initial_value)
8140 && TREE_CODE (TREE_TYPE (initial_value)) != ARRAY_TYPE)
8141 return 1;
8143 if (initial_value)
8145 unsigned HOST_WIDE_INT i;
8146 tree value;
8148 /* An array of character type can be initialized from a
8149 brace-enclosed string constant.
8151 FIXME: this code is duplicated from reshape_init. Probably
8152 we should just call reshape_init here? */
8153 if (char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (*ptype)))
8154 && TREE_CODE (initial_value) == CONSTRUCTOR
8155 && !vec_safe_is_empty (CONSTRUCTOR_ELTS (initial_value)))
8157 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value);
8158 tree value = (*v)[0].value;
8160 if (TREE_CODE (value) == STRING_CST
8161 && v->length () == 1)
8162 initial_value = value;
8165 /* If any of the elements are parameter packs, we can't actually
8166 complete this type now because the array size is dependent. */
8167 if (TREE_CODE (initial_value) == CONSTRUCTOR)
8169 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (initial_value),
8170 i, value)
8172 if (PACK_EXPANSION_P (value))
8173 return 0;
8178 failure = complete_array_type (ptype, initial_value, do_default);
8180 /* We can create the array before the element type is complete, which
8181 means that we didn't have these two bits set in the original type
8182 either. In completing the type, we are expected to propagate these
8183 bits. See also complete_type which does the same thing for arrays
8184 of fixed size. */
8185 type = *ptype;
8186 if (TYPE_DOMAIN (type))
8188 elt_type = TREE_TYPE (type);
8189 TYPE_NEEDS_CONSTRUCTING (type) = TYPE_NEEDS_CONSTRUCTING (elt_type);
8190 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
8191 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (elt_type);
8194 return failure;
8197 /* As above, but either give an error or reject zero-size arrays, depending
8198 on COMPLAIN. */
8201 cp_complete_array_type_or_error (tree *ptype, tree initial_value,
8202 bool do_default, tsubst_flags_t complain)
8204 int failure;
8205 bool sfinae = !(complain & tf_error);
8206 /* In SFINAE context we can't be lenient about zero-size arrays. */
8207 if (sfinae)
8208 ++pedantic;
8209 failure = cp_complete_array_type (ptype, initial_value, do_default);
8210 if (sfinae)
8211 --pedantic;
8212 if (failure)
8214 if (sfinae)
8215 /* Not an error. */;
8216 else if (failure == 1)
8217 error ("initializer fails to determine size of %qT", *ptype);
8218 else if (failure == 2)
8220 if (do_default)
8221 error ("array size missing in %qT", *ptype);
8223 else if (failure == 3)
8224 error ("zero-size array %qT", *ptype);
8225 *ptype = error_mark_node;
8227 return failure;
8230 /* Return zero if something is declared to be a member of type
8231 CTYPE when in the context of CUR_TYPE. STRING is the error
8232 message to print in that case. Otherwise, quietly return 1. */
8234 static int
8235 member_function_or_else (tree ctype, tree cur_type, enum overload_flags flags)
8237 if (ctype && ctype != cur_type)
8239 if (flags == DTOR_FLAG)
8240 error ("destructor for alien class %qT cannot be a member", ctype);
8241 else
8242 error ("constructor for alien class %qT cannot be a member", ctype);
8243 return 0;
8245 return 1;
8248 /* Subroutine of `grokdeclarator'. */
8250 /* Generate errors possibly applicable for a given set of specifiers.
8251 This is for ARM $7.1.2. */
8253 static void
8254 bad_specifiers (tree object,
8255 enum bad_spec_place type,
8256 int virtualp,
8257 int quals,
8258 int inlinep,
8259 int friendp,
8260 int raises)
8262 switch (type)
8264 case BSP_VAR:
8265 if (virtualp)
8266 error ("%qD declared as a %<virtual%> variable", object);
8267 if (quals)
8268 error ("%<const%> and %<volatile%> function specifiers on "
8269 "%qD invalid in variable declaration", object);
8270 break;
8271 case BSP_PARM:
8272 if (virtualp)
8273 error ("%qD declared as a %<virtual%> parameter", object);
8274 if (inlinep)
8275 error ("%qD declared as an %<inline%> parameter", object);
8276 if (quals)
8277 error ("%<const%> and %<volatile%> function specifiers on "
8278 "%qD invalid in parameter declaration", object);
8279 break;
8280 case BSP_TYPE:
8281 if (virtualp)
8282 error ("%qD declared as a %<virtual%> type", object);
8283 if (inlinep)
8284 error ("%qD declared as an %<inline%> type", object);
8285 if (quals)
8286 error ("%<const%> and %<volatile%> function specifiers on "
8287 "%qD invalid in type declaration", object);
8288 break;
8289 case BSP_FIELD:
8290 if (virtualp)
8291 error ("%qD declared as a %<virtual%> field", object);
8292 if (inlinep)
8293 error ("%qD declared as an %<inline%> field", object);
8294 if (quals)
8295 error ("%<const%> and %<volatile%> function specifiers on "
8296 "%qD invalid in field declaration", object);
8297 break;
8298 default:
8299 gcc_unreachable();
8301 if (friendp)
8302 error ("%q+D declared as a friend", object);
8303 if (raises
8304 && !flag_noexcept_type
8305 && (TREE_CODE (object) == TYPE_DECL
8306 || (!TYPE_PTRFN_P (TREE_TYPE (object))
8307 && !TYPE_REFFN_P (TREE_TYPE (object))
8308 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (object)))))
8309 error ("%q+D declared with an exception specification", object);
8312 /* DECL is a member function or static data member and is presently
8313 being defined. Check that the definition is taking place in a
8314 valid namespace. */
8316 static void
8317 check_class_member_definition_namespace (tree decl)
8319 /* These checks only apply to member functions and static data
8320 members. */
8321 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
8322 /* We check for problems with specializations in pt.c in
8323 check_specialization_namespace, where we can issue better
8324 diagnostics. */
8325 if (processing_specialization)
8326 return;
8327 /* We check this in check_explicit_instantiation_namespace. */
8328 if (processing_explicit_instantiation)
8329 return;
8330 /* [class.mfct]
8332 A member function definition that appears outside of the
8333 class definition shall appear in a namespace scope enclosing
8334 the class definition.
8336 [class.static.data]
8338 The definition for a static data member shall appear in a
8339 namespace scope enclosing the member's class definition. */
8340 if (!is_ancestor (current_namespace, DECL_CONTEXT (decl)))
8341 permerror (input_location, "definition of %qD is not in namespace enclosing %qT",
8342 decl, DECL_CONTEXT (decl));
8345 /* Build a PARM_DECL for the "this" parameter of FN. TYPE is the
8346 METHOD_TYPE for a non-static member function; QUALS are the
8347 cv-qualifiers that apply to the function. */
8349 tree
8350 build_this_parm (tree fn, tree type, cp_cv_quals quals)
8352 tree this_type;
8353 tree qual_type;
8354 tree parm;
8355 cp_cv_quals this_quals;
8357 if (CLASS_TYPE_P (type))
8359 this_type
8360 = cp_build_qualified_type (type, quals & ~TYPE_QUAL_RESTRICT);
8361 this_type = build_pointer_type (this_type);
8363 else
8364 this_type = type_of_this_parm (type);
8365 /* The `this' parameter is implicitly `const'; it cannot be
8366 assigned to. */
8367 this_quals = (quals & TYPE_QUAL_RESTRICT) | TYPE_QUAL_CONST;
8368 qual_type = cp_build_qualified_type (this_type, this_quals);
8369 parm = build_artificial_parm (fn, this_identifier, qual_type);
8370 cp_apply_type_quals_to_decl (this_quals, parm);
8371 return parm;
8374 /* DECL is a static member function. Complain if it was declared
8375 with function-cv-quals. */
8377 static void
8378 check_static_quals (tree decl, cp_cv_quals quals)
8380 if (quals != TYPE_UNQUALIFIED)
8381 error ("static member function %q#D declared with type qualifiers",
8382 decl);
8385 // Check that FN takes no arguments and returns bool.
8386 static void
8387 check_concept_fn (tree fn)
8389 // A constraint is nullary.
8390 if (DECL_ARGUMENTS (fn))
8391 error ("concept %q#D declared with function parameters", fn);
8393 // The declared return type of the concept shall be bool, and
8394 // it shall not be deduced from it definition.
8395 tree type = TREE_TYPE (TREE_TYPE (fn));
8396 if (is_auto (type))
8397 error ("concept %q#D declared with a deduced return type", fn);
8398 else if (type != boolean_type_node)
8399 error ("concept %q#D with non-%<bool%> return type %qT", fn, type);
8402 /* Helper function. Replace the temporary this parameter injected
8403 during cp_finish_omp_declare_simd with the real this parameter. */
8405 static tree
8406 declare_simd_adjust_this (tree *tp, int *walk_subtrees, void *data)
8408 tree this_parm = (tree) data;
8409 if (TREE_CODE (*tp) == PARM_DECL
8410 && DECL_NAME (*tp) == this_identifier
8411 && *tp != this_parm)
8412 *tp = this_parm;
8413 else if (TYPE_P (*tp))
8414 *walk_subtrees = 0;
8415 return NULL_TREE;
8418 /* CTYPE is class type, or null if non-class.
8419 TYPE is type this FUNCTION_DECL should have, either FUNCTION_TYPE
8420 or METHOD_TYPE.
8421 DECLARATOR is the function's name.
8422 PARMS is a chain of PARM_DECLs for the function.
8423 VIRTUALP is truthvalue of whether the function is virtual or not.
8424 FLAGS are to be passed through to `grokclassfn'.
8425 QUALS are qualifiers indicating whether the function is `const'
8426 or `volatile'.
8427 RAISES is a list of exceptions that this function can raise.
8428 CHECK is 1 if we must find this method in CTYPE, 0 if we should
8429 not look, and -1 if we should not call `grokclassfn' at all.
8431 SFK is the kind of special function (if any) for the new function.
8433 Returns `NULL_TREE' if something goes wrong, after issuing
8434 applicable error messages. */
8436 static tree
8437 grokfndecl (tree ctype,
8438 tree type,
8439 tree declarator,
8440 tree parms,
8441 tree orig_declarator,
8442 tree decl_reqs,
8443 int virtualp,
8444 enum overload_flags flags,
8445 cp_cv_quals quals,
8446 cp_ref_qualifier rqual,
8447 tree raises,
8448 int check,
8449 int friendp,
8450 int publicp,
8451 int inlinep,
8452 bool deletedp,
8453 special_function_kind sfk,
8454 bool funcdef_flag,
8455 int template_count,
8456 tree in_namespace,
8457 tree* attrlist,
8458 location_t location)
8460 tree decl;
8461 int staticp = ctype && TREE_CODE (type) == FUNCTION_TYPE;
8462 tree t;
8464 // Was the concept specifier present?
8465 bool concept_p = inlinep & 4;
8467 // Concept declarations must have a corresponding definition.
8468 if (concept_p && !funcdef_flag)
8470 error ("concept %qD has no definition", declarator);
8471 return NULL_TREE;
8474 if (rqual)
8475 type = build_ref_qualified_type (type, rqual);
8476 if (raises)
8477 type = build_exception_variant (type, raises);
8479 decl = build_lang_decl (FUNCTION_DECL, declarator, type);
8481 /* Set the constraints on the declaration. */
8482 if (flag_concepts)
8484 tree tmpl_reqs = NULL_TREE;
8485 if (processing_template_decl > template_class_depth (ctype))
8486 tmpl_reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
8488 /* Adjust the required expression into a constraint. */
8489 if (decl_reqs)
8490 decl_reqs = normalize_expression (decl_reqs);
8492 tree ci = build_constraints (tmpl_reqs, decl_reqs);
8493 set_constraints (decl, ci);
8496 /* If we have an explicit location, use it, otherwise use whatever
8497 build_lang_decl used (probably input_location). */
8498 if (location != UNKNOWN_LOCATION)
8499 DECL_SOURCE_LOCATION (decl) = location;
8501 if (TREE_CODE (type) == METHOD_TYPE)
8503 tree parm = build_this_parm (decl, type, quals);
8504 DECL_CHAIN (parm) = parms;
8505 parms = parm;
8507 /* Allocate space to hold the vptr bit if needed. */
8508 SET_DECL_ALIGN (decl, MINIMUM_METHOD_BOUNDARY);
8511 DECL_ARGUMENTS (decl) = parms;
8512 for (t = parms; t; t = DECL_CHAIN (t))
8513 DECL_CONTEXT (t) = decl;
8515 /* Propagate volatile out from type to decl. */
8516 if (TYPE_VOLATILE (type))
8517 TREE_THIS_VOLATILE (decl) = 1;
8519 /* Setup decl according to sfk. */
8520 switch (sfk)
8522 case sfk_constructor:
8523 case sfk_copy_constructor:
8524 case sfk_move_constructor:
8525 DECL_CXX_CONSTRUCTOR_P (decl) = 1;
8526 DECL_NAME (decl) = ctor_identifier;
8527 break;
8528 case sfk_destructor:
8529 DECL_CXX_DESTRUCTOR_P (decl) = 1;
8530 DECL_NAME (decl) = dtor_identifier;
8531 break;
8532 default:
8533 break;
8536 if (friendp && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)
8538 if (funcdef_flag)
8539 error ("defining explicit specialization %qD in friend declaration",
8540 orig_declarator);
8541 else
8543 tree fns = TREE_OPERAND (orig_declarator, 0);
8544 tree args = TREE_OPERAND (orig_declarator, 1);
8546 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8548 /* Something like `template <class T> friend void f<T>()'. */
8549 error ("invalid use of template-id %qD in declaration "
8550 "of primary template",
8551 orig_declarator);
8552 return NULL_TREE;
8556 /* A friend declaration of the form friend void f<>(). Record
8557 the information in the TEMPLATE_ID_EXPR. */
8558 SET_DECL_IMPLICIT_INSTANTIATION (decl);
8560 gcc_assert (identifier_p (fns) || TREE_CODE (fns) == OVERLOAD);
8561 DECL_TEMPLATE_INFO (decl) = build_template_info (fns, args);
8563 for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t))
8564 if (TREE_PURPOSE (t)
8565 && TREE_CODE (TREE_PURPOSE (t)) == DEFAULT_ARG)
8567 error ("default arguments are not allowed in declaration "
8568 "of friend template specialization %qD",
8569 decl);
8570 return NULL_TREE;
8573 if (inlinep & 1)
8575 error ("%<inline%> is not allowed in declaration of friend "
8576 "template specialization %qD",
8577 decl);
8578 return NULL_TREE;
8583 /* If this decl has namespace scope, set that up. */
8584 if (in_namespace)
8585 set_decl_namespace (decl, in_namespace, friendp);
8586 else if (!ctype)
8587 DECL_CONTEXT (decl) = FROB_CONTEXT (current_decl_namespace ());
8589 /* `main' and builtins have implicit 'C' linkage. */
8590 if (ctype == NULL_TREE
8591 && DECL_FILE_SCOPE_P (decl)
8592 && current_lang_name == lang_name_cplusplus
8593 && (MAIN_NAME_P (declarator)
8594 || (IDENTIFIER_LENGTH (declarator) > 10
8595 && IDENTIFIER_POINTER (declarator)[0] == '_'
8596 && IDENTIFIER_POINTER (declarator)[1] == '_'
8597 && strncmp (IDENTIFIER_POINTER (declarator)+2,
8598 "builtin_", 8) == 0)
8599 || (targetcm.cxx_implicit_extern_c
8600 && (targetcm.cxx_implicit_extern_c
8601 (IDENTIFIER_POINTER (declarator))))))
8602 SET_DECL_LANGUAGE (decl, lang_c);
8604 /* Should probably propagate const out from type to decl I bet (mrs). */
8605 if (staticp)
8607 DECL_STATIC_FUNCTION_P (decl) = 1;
8608 DECL_CONTEXT (decl) = ctype;
8611 if (deletedp)
8612 DECL_DELETED_FN (decl) = 1;
8614 if (ctype)
8616 DECL_CONTEXT (decl) = ctype;
8617 if (funcdef_flag)
8618 check_class_member_definition_namespace (decl);
8621 if (ctype == NULL_TREE && DECL_MAIN_P (decl))
8623 if (PROCESSING_REAL_TEMPLATE_DECL_P())
8624 error ("cannot declare %<::main%> to be a template");
8625 if (inlinep & 1)
8626 error ("cannot declare %<::main%> to be inline");
8627 if (inlinep & 2)
8628 error ("cannot declare %<::main%> to be constexpr");
8629 if (!publicp)
8630 error ("cannot declare %<::main%> to be static");
8631 inlinep = 0;
8632 publicp = 1;
8635 /* Members of anonymous types and local classes have no linkage; make
8636 them internal. If a typedef is made later, this will be changed. */
8637 if (ctype && (!TREE_PUBLIC (TYPE_MAIN_DECL (ctype))
8638 || decl_function_context (TYPE_MAIN_DECL (ctype))))
8639 publicp = 0;
8641 if (publicp && cxx_dialect == cxx98)
8643 /* [basic.link]: A name with no linkage (notably, the name of a class
8644 or enumeration declared in a local scope) shall not be used to
8645 declare an entity with linkage.
8647 DR 757 relaxes this restriction for C++0x. */
8648 no_linkage_error (decl);
8651 TREE_PUBLIC (decl) = publicp;
8652 if (! publicp)
8654 DECL_INTERFACE_KNOWN (decl) = 1;
8655 DECL_NOT_REALLY_EXTERN (decl) = 1;
8658 /* If the declaration was declared inline, mark it as such. */
8659 if (inlinep)
8661 DECL_DECLARED_INLINE_P (decl) = 1;
8662 if (publicp)
8663 DECL_COMDAT (decl) = 1;
8665 if (inlinep & 2)
8666 DECL_DECLARED_CONSTEXPR_P (decl) = true;
8668 // If the concept declaration specifier was found, check
8669 // that the declaration satisfies the necessary requirements.
8670 if (concept_p)
8672 DECL_DECLARED_CONCEPT_P (decl) = true;
8673 check_concept_fn (decl);
8676 DECL_EXTERNAL (decl) = 1;
8677 if (TREE_CODE (type) == FUNCTION_TYPE)
8679 if (quals || rqual)
8680 TREE_TYPE (decl) = apply_memfn_quals (TREE_TYPE (decl),
8681 TYPE_UNQUALIFIED,
8682 REF_QUAL_NONE);
8684 if (quals)
8686 error (ctype
8687 ? G_("static member function %qD cannot have cv-qualifier")
8688 : G_("non-member function %qD cannot have cv-qualifier"),
8689 decl);
8690 quals = TYPE_UNQUALIFIED;
8693 if (rqual)
8695 error (ctype
8696 ? G_("static member function %qD cannot have ref-qualifier")
8697 : G_("non-member function %qD cannot have ref-qualifier"),
8698 decl);
8699 rqual = REF_QUAL_NONE;
8703 if (deduction_guide_p (decl))
8705 if (!DECL_NAMESPACE_SCOPE_P (decl))
8707 error_at (location, "deduction guide %qD must be declared at "
8708 "namespace scope", decl);
8709 return NULL_TREE;
8711 if (funcdef_flag)
8712 error_at (location,
8713 "deduction guide %qD must not have a function body", decl);
8715 else if (IDENTIFIER_ANY_OP_P (DECL_NAME (decl))
8716 && !grok_op_properties (decl, /*complain=*/true))
8717 return NULL_TREE;
8718 else if (UDLIT_OPER_P (DECL_NAME (decl)))
8720 bool long_long_unsigned_p;
8721 bool long_double_p;
8722 const char *suffix = NULL;
8723 /* [over.literal]/6: Literal operators shall not have C linkage. */
8724 if (DECL_LANGUAGE (decl) == lang_c)
8726 error ("literal operator with C linkage");
8727 return NULL_TREE;
8730 if (DECL_NAMESPACE_SCOPE_P (decl))
8732 if (!check_literal_operator_args (decl, &long_long_unsigned_p,
8733 &long_double_p))
8735 error ("%qD has invalid argument list", decl);
8736 return NULL_TREE;
8739 suffix = UDLIT_OP_SUFFIX (DECL_NAME (decl));
8740 if (long_long_unsigned_p)
8742 if (cpp_interpret_int_suffix (parse_in, suffix, strlen (suffix)))
8743 warning (0, "integer suffix %qs"
8744 " shadowed by implementation", suffix);
8746 else if (long_double_p)
8748 if (cpp_interpret_float_suffix (parse_in, suffix, strlen (suffix)))
8749 warning (0, "floating point suffix %qs"
8750 " shadowed by implementation", suffix);
8753 else
8755 error ("%qD must be a non-member function", decl);
8756 return NULL_TREE;
8760 if (funcdef_flag)
8761 /* Make the init_value nonzero so pushdecl knows this is not
8762 tentative. error_mark_node is replaced later with the BLOCK. */
8763 DECL_INITIAL (decl) = error_mark_node;
8765 if (TYPE_NOTHROW_P (type) || nothrow_libfn_p (decl))
8766 TREE_NOTHROW (decl) = 1;
8768 if (flag_openmp || flag_openmp_simd || flag_cilkplus)
8770 /* Adjust "omp declare simd" attributes. */
8771 tree ods = lookup_attribute ("omp declare simd", *attrlist);
8772 if (ods)
8774 tree attr;
8775 for (attr = ods; attr;
8776 attr = lookup_attribute ("omp declare simd", TREE_CHAIN (attr)))
8778 if (TREE_CODE (type) == METHOD_TYPE)
8779 walk_tree (&TREE_VALUE (attr), declare_simd_adjust_this,
8780 DECL_ARGUMENTS (decl), NULL);
8781 if (TREE_VALUE (attr) != NULL_TREE)
8783 tree cl = TREE_VALUE (TREE_VALUE (attr));
8784 cl = c_omp_declare_simd_clauses_to_numbers
8785 (DECL_ARGUMENTS (decl), cl);
8786 if (cl)
8787 TREE_VALUE (TREE_VALUE (attr)) = cl;
8788 else
8789 TREE_VALUE (attr) = NULL_TREE;
8795 /* Caller will do the rest of this. */
8796 if (check < 0)
8797 return decl;
8799 if (ctype != NULL_TREE)
8800 grokclassfn (ctype, decl, flags);
8802 /* 12.4/3 */
8803 if (cxx_dialect >= cxx11
8804 && DECL_DESTRUCTOR_P (decl)
8805 && !TYPE_BEING_DEFINED (DECL_CONTEXT (decl))
8806 && !processing_template_decl)
8807 deduce_noexcept_on_destructor (decl);
8809 decl = check_explicit_specialization (orig_declarator, decl,
8810 template_count,
8811 2 * funcdef_flag +
8812 4 * (friendp != 0) +
8813 8 * concept_p);
8814 if (decl == error_mark_node)
8815 return NULL_TREE;
8817 if (DECL_STATIC_FUNCTION_P (decl))
8818 check_static_quals (decl, quals);
8820 if (attrlist)
8822 cplus_decl_attributes (&decl, *attrlist, 0);
8823 *attrlist = NULL_TREE;
8826 /* Check main's type after attributes have been applied. */
8827 if (ctype == NULL_TREE && DECL_MAIN_P (decl))
8829 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
8830 integer_type_node))
8832 tree oldtypeargs = TYPE_ARG_TYPES (TREE_TYPE (decl));
8833 tree newtype;
8834 error ("%<::main%> must return %<int%>");
8835 newtype = build_function_type (integer_type_node, oldtypeargs);
8836 TREE_TYPE (decl) = newtype;
8838 if (warn_main)
8839 check_main_parameter_types (decl);
8842 if (ctype != NULL_TREE && check)
8844 tree old_decl = check_classfn (ctype, decl,
8845 (processing_template_decl
8846 > template_class_depth (ctype))
8847 ? current_template_parms
8848 : NULL_TREE);
8850 if (old_decl == error_mark_node)
8851 return NULL_TREE;
8853 if (old_decl)
8855 tree ok;
8856 tree pushed_scope;
8858 if (TREE_CODE (old_decl) == TEMPLATE_DECL)
8859 /* Because grokfndecl is always supposed to return a
8860 FUNCTION_DECL, we pull out the DECL_TEMPLATE_RESULT
8861 here. We depend on our callers to figure out that its
8862 really a template that's being returned. */
8863 old_decl = DECL_TEMPLATE_RESULT (old_decl);
8865 if (DECL_STATIC_FUNCTION_P (old_decl)
8866 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
8868 /* Remove the `this' parm added by grokclassfn. */
8869 revert_static_member_fn (decl);
8870 check_static_quals (decl, quals);
8872 if (DECL_ARTIFICIAL (old_decl))
8874 error ("definition of implicitly-declared %qD", old_decl);
8875 return NULL_TREE;
8877 else if (DECL_DEFAULTED_FN (old_decl))
8879 error ("definition of explicitly-defaulted %q+D", decl);
8880 inform (DECL_SOURCE_LOCATION (old_decl),
8881 "%q#D explicitly defaulted here", old_decl);
8882 return NULL_TREE;
8885 /* Since we've smashed OLD_DECL to its
8886 DECL_TEMPLATE_RESULT, we must do the same to DECL. */
8887 if (TREE_CODE (decl) == TEMPLATE_DECL)
8888 decl = DECL_TEMPLATE_RESULT (decl);
8890 /* Attempt to merge the declarations. This can fail, in
8891 the case of some invalid specialization declarations. */
8892 pushed_scope = push_scope (ctype);
8893 ok = duplicate_decls (decl, old_decl, friendp);
8894 if (pushed_scope)
8895 pop_scope (pushed_scope);
8896 if (!ok)
8898 error ("no %q#D member function declared in class %qT",
8899 decl, ctype);
8900 return NULL_TREE;
8902 if (ok == error_mark_node)
8903 return NULL_TREE;
8904 return old_decl;
8908 if (DECL_CONSTRUCTOR_P (decl) && !grok_ctor_properties (ctype, decl))
8909 return NULL_TREE;
8911 if (ctype == NULL_TREE || check)
8912 return decl;
8914 if (virtualp)
8915 DECL_VIRTUAL_P (decl) = 1;
8917 return decl;
8920 /* decl is a FUNCTION_DECL.
8921 specifiers are the parsed virt-specifiers.
8923 Set flags to reflect the virt-specifiers.
8925 Returns decl. */
8927 static tree
8928 set_virt_specifiers (tree decl, cp_virt_specifiers specifiers)
8930 if (decl == NULL_TREE)
8931 return decl;
8932 if (specifiers & VIRT_SPEC_OVERRIDE)
8933 DECL_OVERRIDE_P (decl) = 1;
8934 if (specifiers & VIRT_SPEC_FINAL)
8935 DECL_FINAL_P (decl) = 1;
8936 return decl;
8939 /* DECL is a VAR_DECL for a static data member. Set flags to reflect
8940 the linkage that DECL will receive in the object file. */
8942 static void
8943 set_linkage_for_static_data_member (tree decl)
8945 /* A static data member always has static storage duration and
8946 external linkage. Note that static data members are forbidden in
8947 local classes -- the only situation in which a class has
8948 non-external linkage. */
8949 TREE_PUBLIC (decl) = 1;
8950 TREE_STATIC (decl) = 1;
8951 /* For non-template classes, static data members are always put
8952 out in exactly those files where they are defined, just as
8953 with ordinary namespace-scope variables. */
8954 if (!processing_template_decl)
8955 DECL_INTERFACE_KNOWN (decl) = 1;
8958 /* Create a VAR_DECL named NAME with the indicated TYPE.
8960 If SCOPE is non-NULL, it is the class type or namespace containing
8961 the variable. If SCOPE is NULL, the variable should is created in
8962 the innermost enclosing scope. */
8964 static tree
8965 grokvardecl (tree type,
8966 tree name,
8967 tree orig_declarator,
8968 const cp_decl_specifier_seq *declspecs,
8969 int initialized,
8970 int type_quals,
8971 int inlinep,
8972 bool conceptp,
8973 int template_count,
8974 tree scope)
8976 tree decl;
8977 tree explicit_scope;
8979 gcc_assert (!name || identifier_p (name));
8981 bool constp = (type_quals & TYPE_QUAL_CONST) != 0;
8982 bool volatilep = (type_quals & TYPE_QUAL_VOLATILE) != 0;
8984 /* Compute the scope in which to place the variable, but remember
8985 whether or not that scope was explicitly specified by the user. */
8986 explicit_scope = scope;
8987 if (!scope)
8989 /* An explicit "extern" specifier indicates a namespace-scope
8990 variable. */
8991 if (declspecs->storage_class == sc_extern)
8992 scope = current_decl_namespace ();
8993 else if (!at_function_scope_p ())
8994 scope = current_scope ();
8997 if (scope
8998 && (/* If the variable is a namespace-scope variable declared in a
8999 template, we need DECL_LANG_SPECIFIC. */
9000 (TREE_CODE (scope) == NAMESPACE_DECL && processing_template_decl)
9001 /* Similarly for namespace-scope variables with language linkage
9002 other than C++. */
9003 || (TREE_CODE (scope) == NAMESPACE_DECL
9004 && current_lang_name != lang_name_cplusplus)
9005 /* Similarly for static data members. */
9006 || TYPE_P (scope)
9007 /* Similarly for explicit specializations. */
9008 || (orig_declarator
9009 && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)))
9010 decl = build_lang_decl (VAR_DECL, name, type);
9011 else
9012 decl = build_decl (input_location, VAR_DECL, name, type);
9014 if (explicit_scope && TREE_CODE (explicit_scope) == NAMESPACE_DECL)
9015 set_decl_namespace (decl, explicit_scope, 0);
9016 else
9017 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
9019 if (declspecs->storage_class == sc_extern)
9021 DECL_THIS_EXTERN (decl) = 1;
9022 DECL_EXTERNAL (decl) = !initialized;
9025 if (DECL_CLASS_SCOPE_P (decl))
9027 set_linkage_for_static_data_member (decl);
9028 /* This function is only called with out-of-class definitions. */
9029 DECL_EXTERNAL (decl) = 0;
9030 check_class_member_definition_namespace (decl);
9032 /* At top level, either `static' or no s.c. makes a definition
9033 (perhaps tentative), and absence of `static' makes it public. */
9034 else if (toplevel_bindings_p ())
9036 TREE_PUBLIC (decl) = (declspecs->storage_class != sc_static
9037 && (DECL_THIS_EXTERN (decl)
9038 || ! constp
9039 || volatilep
9040 || inlinep));
9041 TREE_STATIC (decl) = ! DECL_EXTERNAL (decl);
9043 /* Not at top level, only `static' makes a static definition. */
9044 else
9046 TREE_STATIC (decl) = declspecs->storage_class == sc_static;
9047 TREE_PUBLIC (decl) = DECL_EXTERNAL (decl);
9050 if (decl_spec_seq_has_spec_p (declspecs, ds_thread))
9052 if (DECL_EXTERNAL (decl) || TREE_STATIC (decl))
9054 CP_DECL_THREAD_LOCAL_P (decl) = true;
9055 if (!processing_template_decl)
9056 set_decl_tls_model (decl, decl_default_tls_model (decl));
9058 if (declspecs->gnu_thread_keyword_p)
9059 SET_DECL_GNU_TLS_P (decl);
9062 /* If the type of the decl has no linkage, make sure that we'll
9063 notice that in mark_used. */
9064 if (cxx_dialect > cxx98
9065 && decl_linkage (decl) != lk_none
9066 && DECL_LANG_SPECIFIC (decl) == NULL
9067 && !DECL_EXTERN_C_P (decl)
9068 && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
9069 retrofit_lang_decl (decl);
9071 if (TREE_PUBLIC (decl))
9073 /* [basic.link]: A name with no linkage (notably, the name of a class
9074 or enumeration declared in a local scope) shall not be used to
9075 declare an entity with linkage.
9077 DR 757 relaxes this restriction for C++0x. */
9078 if (cxx_dialect < cxx11)
9079 no_linkage_error (decl);
9081 else
9082 DECL_INTERFACE_KNOWN (decl) = 1;
9084 if (DECL_NAME (decl)
9085 && MAIN_NAME_P (DECL_NAME (decl))
9086 && scope == global_namespace)
9087 error ("cannot declare %<::main%> to be a global variable");
9089 /* Check that the variable can be safely declared as a concept.
9090 Note that this also forbids explicit specializations. */
9091 if (conceptp)
9093 if (!processing_template_decl)
9095 error ("a non-template variable cannot be %<concept%>");
9096 return NULL_TREE;
9098 else
9099 DECL_DECLARED_CONCEPT_P (decl) = true;
9100 if (!same_type_ignoring_top_level_qualifiers_p (type, boolean_type_node))
9101 error_at (declspecs->locations[ds_type_spec],
9102 "concept must have type %<bool%>");
9104 else if (flag_concepts
9105 && processing_template_decl > template_class_depth (scope))
9107 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
9108 tree ci = build_constraints (reqs, NULL_TREE);
9109 set_constraints (decl, ci);
9112 // Handle explicit specializations and instantiations of variable templates.
9113 if (orig_declarator)
9114 decl = check_explicit_specialization (orig_declarator, decl,
9115 template_count, conceptp * 8);
9117 return decl != error_mark_node ? decl : NULL_TREE;
9120 /* Create and return a canonical pointer to member function type, for
9121 TYPE, which is a POINTER_TYPE to a METHOD_TYPE. */
9123 tree
9124 build_ptrmemfunc_type (tree type)
9126 tree field, fields;
9127 tree t;
9129 if (type == error_mark_node)
9130 return type;
9132 /* Make sure that we always have the unqualified pointer-to-member
9133 type first. */
9134 if (cp_cv_quals quals = cp_type_quals (type))
9136 tree unqual = build_ptrmemfunc_type (TYPE_MAIN_VARIANT (type));
9137 return cp_build_qualified_type (unqual, quals);
9140 /* If a canonical type already exists for this type, use it. We use
9141 this method instead of type_hash_canon, because it only does a
9142 simple equality check on the list of field members. */
9144 t = TYPE_PTRMEMFUNC_TYPE (type);
9145 if (t)
9146 return t;
9148 t = make_node (RECORD_TYPE);
9150 /* Let the front end know this is a pointer to member function. */
9151 TYPE_PTRMEMFUNC_FLAG (t) = 1;
9153 field = build_decl (input_location, FIELD_DECL, pfn_identifier, type);
9154 fields = field;
9156 field = build_decl (input_location, FIELD_DECL, delta_identifier,
9157 delta_type_node);
9158 DECL_CHAIN (field) = fields;
9159 fields = field;
9161 finish_builtin_struct (t, "__ptrmemfunc_type", fields, ptr_type_node);
9163 /* Zap out the name so that the back end will give us the debugging
9164 information for this anonymous RECORD_TYPE. */
9165 TYPE_NAME (t) = NULL_TREE;
9167 /* Cache this pointer-to-member type so that we can find it again
9168 later. */
9169 TYPE_PTRMEMFUNC_TYPE (type) = t;
9171 if (TYPE_STRUCTURAL_EQUALITY_P (type))
9172 SET_TYPE_STRUCTURAL_EQUALITY (t);
9173 else if (TYPE_CANONICAL (type) != type)
9174 TYPE_CANONICAL (t) = build_ptrmemfunc_type (TYPE_CANONICAL (type));
9176 return t;
9179 /* Create and return a pointer to data member type. */
9181 tree
9182 build_ptrmem_type (tree class_type, tree member_type)
9184 if (TREE_CODE (member_type) == METHOD_TYPE)
9186 cp_cv_quals quals = type_memfn_quals (member_type);
9187 cp_ref_qualifier rqual = type_memfn_rqual (member_type);
9188 member_type = build_memfn_type (member_type, class_type, quals, rqual);
9189 return build_ptrmemfunc_type (build_pointer_type (member_type));
9191 else
9193 gcc_assert (TREE_CODE (member_type) != FUNCTION_TYPE);
9194 return build_offset_type (class_type, member_type);
9198 /* DECL is a VAR_DECL defined in-class, whose TYPE is also given.
9199 Check to see that the definition is valid. Issue appropriate error
9200 messages. Return 1 if the definition is particularly bad, or 0
9201 otherwise. */
9203 static int
9204 check_static_variable_definition (tree decl, tree type)
9206 /* Avoid redundant diagnostics on out-of-class definitions. */
9207 if (!current_class_type || !TYPE_BEING_DEFINED (current_class_type))
9208 return 0;
9209 /* Can't check yet if we don't know the type. */
9210 if (dependent_type_p (type))
9211 return 0;
9212 /* If DECL is declared constexpr, we'll do the appropriate checks
9213 in check_initializer. Similarly for inline static data members. */
9214 if (DECL_P (decl)
9215 && (DECL_DECLARED_CONSTEXPR_P (decl)
9216 || DECL_VAR_DECLARED_INLINE_P (decl)))
9217 return 0;
9218 else if (cxx_dialect >= cxx11 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
9220 if (!COMPLETE_TYPE_P (type))
9221 error_at (DECL_SOURCE_LOCATION (decl),
9222 "in-class initialization of static data member %q#D of "
9223 "incomplete type", decl);
9224 else if (literal_type_p (type))
9225 permerror (DECL_SOURCE_LOCATION (decl),
9226 "%<constexpr%> needed for in-class initialization of "
9227 "static data member %q#D of non-integral type", decl);
9228 else
9229 error_at (DECL_SOURCE_LOCATION (decl),
9230 "in-class initialization of static data member %q#D of "
9231 "non-literal type", decl);
9232 return 1;
9235 /* Motion 10 at San Diego: If a static const integral data member is
9236 initialized with an integral constant expression, the initializer
9237 may appear either in the declaration (within the class), or in
9238 the definition, but not both. If it appears in the class, the
9239 member is a member constant. The file-scope definition is always
9240 required. */
9241 if (!ARITHMETIC_TYPE_P (type) && TREE_CODE (type) != ENUMERAL_TYPE)
9243 error_at (DECL_SOURCE_LOCATION (decl),
9244 "invalid in-class initialization of static data member "
9245 "of non-integral type %qT",
9246 type);
9247 return 1;
9249 else if (!CP_TYPE_CONST_P (type))
9250 error_at (DECL_SOURCE_LOCATION (decl),
9251 "ISO C++ forbids in-class initialization of non-const "
9252 "static member %qD",
9253 decl);
9254 else if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type))
9255 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
9256 "ISO C++ forbids initialization of member constant "
9257 "%qD of non-integral type %qT", decl, type);
9259 return 0;
9262 /* *expr_p is part of the TYPE_SIZE of a variably-sized array. If any
9263 SAVE_EXPRs in *expr_p wrap expressions with side-effects, break those
9264 expressions out into temporary variables so that walk_tree doesn't
9265 step into them (c++/15764). */
9267 static tree
9268 stabilize_save_expr_r (tree *expr_p, int *walk_subtrees, void *data)
9270 hash_set<tree> *pset = (hash_set<tree> *)data;
9271 tree expr = *expr_p;
9272 if (TREE_CODE (expr) == SAVE_EXPR)
9274 tree op = TREE_OPERAND (expr, 0);
9275 cp_walk_tree (&op, stabilize_save_expr_r, data, pset);
9276 if (TREE_SIDE_EFFECTS (op))
9277 TREE_OPERAND (expr, 0) = get_temp_regvar (TREE_TYPE (op), op);
9278 *walk_subtrees = 0;
9280 else if (!EXPR_P (expr) || !TREE_SIDE_EFFECTS (expr))
9281 *walk_subtrees = 0;
9282 return NULL;
9285 /* Entry point for the above. */
9287 static void
9288 stabilize_vla_size (tree size)
9290 hash_set<tree> pset;
9291 /* Break out any function calls into temporary variables. */
9292 cp_walk_tree (&size, stabilize_save_expr_r, &pset, &pset);
9295 /* Reduce a SIZEOF_EXPR to its value. */
9297 tree
9298 fold_sizeof_expr (tree t)
9300 tree r;
9301 if (SIZEOF_EXPR_TYPE_P (t))
9302 r = cxx_sizeof_or_alignof_type (TREE_TYPE (TREE_OPERAND (t, 0)),
9303 SIZEOF_EXPR, false);
9304 else if (TYPE_P (TREE_OPERAND (t, 0)))
9305 r = cxx_sizeof_or_alignof_type (TREE_OPERAND (t, 0), SIZEOF_EXPR,
9306 false);
9307 else
9308 r = cxx_sizeof_or_alignof_expr (TREE_OPERAND (t, 0), SIZEOF_EXPR,
9309 false);
9310 if (r == error_mark_node)
9311 r = size_one_node;
9312 return r;
9315 /* Given the SIZE (i.e., number of elements) in an array, compute
9316 an appropriate index type for the array. If non-NULL, NAME is
9317 the name of the entity being declared. */
9319 tree
9320 compute_array_index_type (tree name, tree size, tsubst_flags_t complain)
9322 tree itype;
9323 tree osize = size;
9325 if (error_operand_p (size))
9326 return error_mark_node;
9328 if (!type_dependent_expression_p (size))
9330 tree type = TREE_TYPE (size);
9332 mark_rvalue_use (size);
9334 if (cxx_dialect < cxx11 && TREE_CODE (size) == NOP_EXPR
9335 && TREE_SIDE_EFFECTS (size))
9336 /* In C++98, we mark a non-constant array bound with a magic
9337 NOP_EXPR with TREE_SIDE_EFFECTS; don't fold in that case. */;
9338 else
9340 size = instantiate_non_dependent_expr_sfinae (size, complain);
9342 if (CLASS_TYPE_P (type)
9343 && CLASSTYPE_LITERAL_P (type))
9345 size = build_expr_type_conversion (WANT_INT, size, true);
9346 if (!size)
9348 if (!(complain & tf_error))
9349 return error_mark_node;
9350 if (name)
9351 error ("size of array %qD has non-integral type %qT",
9352 name, type);
9353 else
9354 error ("size of array has non-integral type %qT", type);
9355 size = integer_one_node;
9357 if (size == error_mark_node)
9358 return error_mark_node;
9359 type = TREE_TYPE (size);
9362 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
9363 size = maybe_constant_value (size);
9365 if (!TREE_CONSTANT (size))
9366 size = osize;
9369 if (error_operand_p (size))
9370 return error_mark_node;
9372 /* The array bound must be an integer type. */
9373 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
9375 if (!(complain & tf_error))
9376 return error_mark_node;
9377 if (name)
9378 error ("size of array %qD has non-integral type %qT", name, type);
9379 else
9380 error ("size of array has non-integral type %qT", type);
9381 size = integer_one_node;
9382 type = TREE_TYPE (size);
9386 /* A type is dependent if it is...an array type constructed from any
9387 dependent type or whose size is specified by a constant expression
9388 that is value-dependent. */
9389 /* We can only call value_dependent_expression_p on integral constant
9390 expressions; treat non-constant expressions as dependent, too. */
9391 if (processing_template_decl
9392 && (type_dependent_expression_p (size)
9393 || !TREE_CONSTANT (size) || value_dependent_expression_p (size)))
9395 /* We cannot do any checking for a SIZE that isn't known to be
9396 constant. Just build the index type and mark that it requires
9397 structural equality checks. */
9398 itype = build_index_type (build_min (MINUS_EXPR, sizetype,
9399 size, size_one_node));
9400 TYPE_DEPENDENT_P (itype) = 1;
9401 TYPE_DEPENDENT_P_VALID (itype) = 1;
9402 SET_TYPE_STRUCTURAL_EQUALITY (itype);
9403 return itype;
9406 if (TREE_CODE (size) != INTEGER_CST)
9408 tree folded = cp_fully_fold (size);
9409 if (TREE_CODE (folded) == INTEGER_CST)
9410 pedwarn (location_of (size), OPT_Wpedantic,
9411 "size of array is not an integral constant-expression");
9412 /* Use the folded result for VLAs, too; it will have resolved
9413 SIZEOF_EXPR. */
9414 size = folded;
9417 /* Normally, the array-bound will be a constant. */
9418 if (TREE_CODE (size) == INTEGER_CST)
9420 /* Check to see if the array bound overflowed. Make that an
9421 error, no matter how generous we're being. */
9422 constant_expression_error (size);
9424 /* An array must have a positive number of elements. */
9425 if (tree_int_cst_lt (size, integer_zero_node))
9427 if (!(complain & tf_error))
9428 return error_mark_node;
9429 if (name)
9430 error ("size of array %qD is negative", name);
9431 else
9432 error ("size of array is negative");
9433 size = integer_one_node;
9435 /* As an extension we allow zero-sized arrays. */
9436 else if (integer_zerop (size))
9438 if (!(complain & tf_error))
9439 /* We must fail if performing argument deduction (as
9440 indicated by the state of complain), so that
9441 another substitution can be found. */
9442 return error_mark_node;
9443 else if (in_system_header_at (input_location))
9444 /* Allow them in system headers because glibc uses them. */;
9445 else if (name)
9446 pedwarn (input_location, OPT_Wpedantic, "ISO C++ forbids zero-size array %qD", name);
9447 else
9448 pedwarn (input_location, OPT_Wpedantic, "ISO C++ forbids zero-size array");
9451 else if (TREE_CONSTANT (size)
9452 /* We don't allow VLAs at non-function scopes, or during
9453 tentative template substitution. */
9454 || !at_function_scope_p ()
9455 || !(complain & tf_error))
9457 if (!(complain & tf_error))
9458 return error_mark_node;
9459 /* `(int) &fn' is not a valid array bound. */
9460 if (name)
9461 error ("size of array %qD is not an integral constant-expression",
9462 name);
9463 else
9464 error ("size of array is not an integral constant-expression");
9465 size = integer_one_node;
9467 else if (pedantic && warn_vla != 0)
9469 if (name)
9470 pedwarn (input_location, OPT_Wvla, "ISO C++ forbids variable length array %qD", name);
9471 else
9472 pedwarn (input_location, OPT_Wvla, "ISO C++ forbids variable length array");
9474 else if (warn_vla > 0)
9476 if (name)
9477 warning (OPT_Wvla,
9478 "variable length array %qD is used", name);
9479 else
9480 warning (OPT_Wvla,
9481 "variable length array is used");
9484 if (processing_template_decl && !TREE_CONSTANT (size))
9485 /* A variable sized array. */
9486 itype = build_min (MINUS_EXPR, sizetype, size, integer_one_node);
9487 else
9489 HOST_WIDE_INT saved_processing_template_decl;
9491 /* Compute the index of the largest element in the array. It is
9492 one less than the number of elements in the array. We save
9493 and restore PROCESSING_TEMPLATE_DECL so that computations in
9494 cp_build_binary_op will be appropriately folded. */
9495 saved_processing_template_decl = processing_template_decl;
9496 processing_template_decl = 0;
9497 itype = cp_build_binary_op (input_location,
9498 MINUS_EXPR,
9499 cp_convert (ssizetype, size, complain),
9500 cp_convert (ssizetype, integer_one_node,
9501 complain),
9502 complain);
9503 itype = maybe_constant_value (itype);
9504 processing_template_decl = saved_processing_template_decl;
9506 if (!TREE_CONSTANT (itype))
9508 /* A variable sized array. */
9509 itype = variable_size (itype);
9511 stabilize_vla_size (itype);
9513 if (sanitize_flags_p (SANITIZE_VLA)
9514 && current_function_decl != NULL_TREE)
9516 /* We have to add 1 -- in the ubsan routine we generate
9517 LE_EXPR rather than LT_EXPR. */
9518 tree t = fold_build2 (PLUS_EXPR, TREE_TYPE (itype), itype,
9519 build_one_cst (TREE_TYPE (itype)));
9520 t = ubsan_instrument_vla (input_location, t);
9521 finish_expr_stmt (t);
9524 /* Make sure that there was no overflow when creating to a signed
9525 index type. (For example, on a 32-bit machine, an array with
9526 size 2^32 - 1 is too big.) */
9527 else if (TREE_CODE (itype) == INTEGER_CST
9528 && TREE_OVERFLOW (itype))
9530 if (!(complain & tf_error))
9531 return error_mark_node;
9532 error ("overflow in array dimension");
9533 TREE_OVERFLOW (itype) = 0;
9537 /* Create and return the appropriate index type. */
9538 itype = build_index_type (itype);
9540 /* If the index type were dependent, we would have returned early, so
9541 remember that it isn't. */
9542 TYPE_DEPENDENT_P (itype) = 0;
9543 TYPE_DEPENDENT_P_VALID (itype) = 1;
9544 return itype;
9547 /* Returns the scope (if any) in which the entity declared by
9548 DECLARATOR will be located. If the entity was declared with an
9549 unqualified name, NULL_TREE is returned. */
9551 tree
9552 get_scope_of_declarator (const cp_declarator *declarator)
9554 while (declarator && declarator->kind != cdk_id)
9555 declarator = declarator->declarator;
9557 /* If the declarator-id is a SCOPE_REF, the scope in which the
9558 declaration occurs is the first operand. */
9559 if (declarator
9560 && declarator->u.id.qualifying_scope)
9561 return declarator->u.id.qualifying_scope;
9563 /* Otherwise, the declarator is not a qualified name; the entity will
9564 be declared in the current scope. */
9565 return NULL_TREE;
9568 /* Returns an ARRAY_TYPE for an array with SIZE elements of the
9569 indicated TYPE. If non-NULL, NAME is the NAME of the declaration
9570 with this type. */
9572 static tree
9573 create_array_type_for_decl (tree name, tree type, tree size)
9575 tree itype = NULL_TREE;
9577 /* If things have already gone awry, bail now. */
9578 if (type == error_mark_node || size == error_mark_node)
9579 return error_mark_node;
9581 /* 8.3.4/1: If the type of the identifier of D contains the auto
9582 type-specifier, the program is ill-formed. */
9583 if (type_uses_auto (type))
9585 error ("%qD declared as array of %qT", name, type);
9586 return error_mark_node;
9589 /* If there are some types which cannot be array elements,
9590 issue an error-message and return. */
9591 switch (TREE_CODE (type))
9593 case VOID_TYPE:
9594 if (name)
9595 error ("declaration of %qD as array of void", name);
9596 else
9597 error ("creating array of void");
9598 return error_mark_node;
9600 case FUNCTION_TYPE:
9601 if (name)
9602 error ("declaration of %qD as array of functions", name);
9603 else
9604 error ("creating array of functions");
9605 return error_mark_node;
9607 case REFERENCE_TYPE:
9608 if (name)
9609 error ("declaration of %qD as array of references", name);
9610 else
9611 error ("creating array of references");
9612 return error_mark_node;
9614 case METHOD_TYPE:
9615 if (name)
9616 error ("declaration of %qD as array of function members", name);
9617 else
9618 error ("creating array of function members");
9619 return error_mark_node;
9621 default:
9622 break;
9625 /* [dcl.array]
9627 The constant expressions that specify the bounds of the arrays
9628 can be omitted only for the first member of the sequence. */
9629 if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type))
9631 if (name)
9632 error ("declaration of %qD as multidimensional array must "
9633 "have bounds for all dimensions except the first",
9634 name);
9635 else
9636 error ("multidimensional array must have bounds for all "
9637 "dimensions except the first");
9639 return error_mark_node;
9642 /* Figure out the index type for the array. */
9643 if (size)
9644 itype = compute_array_index_type (name, size, tf_warning_or_error);
9646 /* [dcl.array]
9647 T is called the array element type; this type shall not be [...] an
9648 abstract class type. */
9649 abstract_virtuals_error (name, type);
9651 return build_cplus_array_type (type, itype);
9654 /* Returns the smallest location != UNKNOWN_LOCATION among the
9655 three stored in LOCATIONS[ds_const], LOCATIONS[ds_volatile],
9656 and LOCATIONS[ds_restrict]. */
9658 static location_t
9659 smallest_type_quals_location (int type_quals, const location_t* locations)
9661 location_t loc = UNKNOWN_LOCATION;
9663 if (type_quals & TYPE_QUAL_CONST)
9664 loc = locations[ds_const];
9666 if ((type_quals & TYPE_QUAL_VOLATILE)
9667 && (loc == UNKNOWN_LOCATION || locations[ds_volatile] < loc))
9668 loc = locations[ds_volatile];
9670 if ((type_quals & TYPE_QUAL_RESTRICT)
9671 && (loc == UNKNOWN_LOCATION || locations[ds_restrict] < loc))
9672 loc = locations[ds_restrict];
9674 return loc;
9677 /* Check that it's OK to declare a function with the indicated TYPE
9678 and TYPE_QUALS. SFK indicates the kind of special function (if any)
9679 that this function is. OPTYPE is the type given in a conversion
9680 operator declaration, or the class type for a constructor/destructor.
9681 Returns the actual return type of the function; that may be different
9682 than TYPE if an error occurs, or for certain special functions. */
9684 static tree
9685 check_special_function_return_type (special_function_kind sfk,
9686 tree type,
9687 tree optype,
9688 int type_quals,
9689 const location_t* locations)
9691 switch (sfk)
9693 case sfk_constructor:
9694 if (type)
9695 error ("return type specification for constructor invalid");
9696 else if (type_quals != TYPE_UNQUALIFIED)
9697 error_at (smallest_type_quals_location (type_quals, locations),
9698 "qualifiers are not allowed on constructor declaration");
9700 if (targetm.cxx.cdtor_returns_this ())
9701 type = build_pointer_type (optype);
9702 else
9703 type = void_type_node;
9704 break;
9706 case sfk_destructor:
9707 if (type)
9708 error ("return type specification for destructor invalid");
9709 else if (type_quals != TYPE_UNQUALIFIED)
9710 error_at (smallest_type_quals_location (type_quals, locations),
9711 "qualifiers are not allowed on destructor declaration");
9713 /* We can't use the proper return type here because we run into
9714 problems with ambiguous bases and covariant returns. */
9715 if (targetm.cxx.cdtor_returns_this ())
9716 type = build_pointer_type (void_type_node);
9717 else
9718 type = void_type_node;
9719 break;
9721 case sfk_conversion:
9722 if (type)
9723 error ("return type specified for %<operator %T%>", optype);
9724 else if (type_quals != TYPE_UNQUALIFIED)
9725 error_at (smallest_type_quals_location (type_quals, locations),
9726 "qualifiers are not allowed on declaration of "
9727 "%<operator %T%>", optype);
9729 type = optype;
9730 break;
9732 case sfk_deduction_guide:
9733 if (type)
9734 error ("return type specified for deduction guide");
9735 else if (type_quals != TYPE_UNQUALIFIED)
9736 error_at (smallest_type_quals_location (type_quals, locations),
9737 "qualifiers are not allowed on declaration of "
9738 "deduction guide");
9739 type = make_template_placeholder (CLASSTYPE_TI_TEMPLATE (optype));
9740 for (int i = 0; i < ds_last; ++i)
9741 if (i != ds_explicit && locations[i])
9742 error_at (locations[i],
9743 "decl-specifier in declaration of deduction guide");
9744 break;
9746 default:
9747 gcc_unreachable ();
9750 return type;
9753 /* A variable or data member (whose unqualified name is IDENTIFIER)
9754 has been declared with the indicated TYPE. If the TYPE is not
9755 acceptable, issue an error message and return a type to use for
9756 error-recovery purposes. */
9758 tree
9759 check_var_type (tree identifier, tree type)
9761 if (VOID_TYPE_P (type))
9763 if (!identifier)
9764 error ("unnamed variable or field declared void");
9765 else if (identifier_p (identifier))
9767 gcc_assert (!IDENTIFIER_ANY_OP_P (identifier));
9768 error ("variable or field %qE declared void", identifier);
9770 else
9771 error ("variable or field declared void");
9772 type = error_mark_node;
9775 return type;
9778 /* Handle declaring DECL as an inline variable. */
9780 static void
9781 mark_inline_variable (tree decl)
9783 bool inlinep = true;
9784 if (! toplevel_bindings_p ())
9786 error ("%<inline%> specifier invalid for variable "
9787 "%qD declared at block scope", decl);
9788 inlinep = false;
9790 else if (cxx_dialect < cxx17)
9791 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
9792 "inline variables are only available "
9793 "with -std=c++17 or -std=gnu++17");
9794 if (inlinep)
9796 retrofit_lang_decl (decl);
9797 SET_DECL_VAR_DECLARED_INLINE_P (decl);
9802 /* Assign a typedef-given name to a class or enumeration type declared
9803 as anonymous at first. This was split out of grokdeclarator
9804 because it is also used in libcc1. */
9806 void
9807 name_unnamed_type (tree type, tree decl)
9809 gcc_assert (TYPE_UNNAMED_P (type));
9811 /* Replace the anonymous name with the real name everywhere. */
9812 for (tree t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
9814 if (anon_aggrname_p (TYPE_IDENTIFIER (t)))
9815 /* We do not rename the debug info representing the
9816 unnamed tagged type because the standard says in
9817 [dcl.typedef] that the naming applies only for
9818 linkage purposes. */
9819 /*debug_hooks->set_name (t, decl);*/
9820 TYPE_NAME (t) = decl;
9823 if (TYPE_LANG_SPECIFIC (type))
9824 TYPE_WAS_UNNAMED (type) = 1;
9826 /* If this is a typedef within a template class, the nested
9827 type is a (non-primary) template. The name for the
9828 template needs updating as well. */
9829 if (TYPE_LANG_SPECIFIC (type) && CLASSTYPE_TEMPLATE_INFO (type))
9830 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type))
9831 = TYPE_IDENTIFIER (type);
9833 /* Adjust linkage now that we aren't unnamed anymore. */
9834 reset_type_linkage (type);
9836 /* FIXME remangle member functions; member functions of a
9837 type with external linkage have external linkage. */
9839 /* Check that our job is done, and that it would fail if we
9840 attempted to do it again. */
9841 gcc_assert (!TYPE_UNNAMED_P (type));
9844 /* Given declspecs and a declarator (abstract or otherwise), determine
9845 the name and type of the object declared and construct a DECL node
9846 for it.
9848 DECLSPECS points to the representation of declaration-specifier
9849 sequence that precedes declarator.
9851 DECL_CONTEXT says which syntactic context this declaration is in:
9852 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
9853 FUNCDEF for a function definition. Like NORMAL but a few different
9854 error messages in each case. Return value may be zero meaning
9855 this definition is too screwy to try to parse.
9856 MEMFUNCDEF for a function definition. Like FUNCDEF but prepares to
9857 handle member functions (which have FIELD context).
9858 Return value may be zero meaning this definition is too screwy to
9859 try to parse.
9860 PARM for a parameter declaration (either within a function prototype
9861 or before a function body). Make a PARM_DECL, or return void_type_node.
9862 TPARM for a template parameter declaration.
9863 CATCHPARM for a parameter declaration before a catch clause.
9864 TYPENAME if for a typename (in a cast or sizeof).
9865 Don't make a DECL node; just return the ..._TYPE node.
9866 FIELD for a struct or union field; make a FIELD_DECL.
9867 BITFIELD for a field with specified width.
9869 INITIALIZED is as for start_decl.
9871 ATTRLIST is a pointer to the list of attributes, which may be NULL
9872 if there are none; *ATTRLIST may be modified if attributes from inside
9873 the declarator should be applied to the declaration.
9875 When this function is called, scoping variables (such as
9876 CURRENT_CLASS_TYPE) should reflect the scope in which the
9877 declaration occurs, not the scope in which the new declaration will
9878 be placed. For example, on:
9880 void S::f() { ... }
9882 when grokdeclarator is called for `S::f', the CURRENT_CLASS_TYPE
9883 should not be `S'.
9885 Returns a DECL (if a declarator is present), a TYPE (if there is no
9886 declarator, in cases like "struct S;"), or the ERROR_MARK_NODE if an
9887 error occurs. */
9889 tree
9890 grokdeclarator (const cp_declarator *declarator,
9891 cp_decl_specifier_seq *declspecs,
9892 enum decl_context decl_context,
9893 int initialized,
9894 tree* attrlist)
9896 tree type = NULL_TREE;
9897 int longlong = 0;
9898 int explicit_intN = 0;
9899 int virtualp, explicitp, friendp, inlinep, staticp;
9900 int explicit_int = 0;
9901 int explicit_char = 0;
9902 int defaulted_int = 0;
9904 tree typedef_decl = NULL_TREE;
9905 const char *name = NULL;
9906 tree typedef_type = NULL_TREE;
9907 /* True if this declarator is a function definition. */
9908 bool funcdef_flag = false;
9909 cp_declarator_kind innermost_code = cdk_error;
9910 int bitfield = 0;
9911 #if 0
9912 /* See the code below that used this. */
9913 tree decl_attr = NULL_TREE;
9914 #endif
9916 /* Keep track of what sort of function is being processed
9917 so that we can warn about default return values, or explicit
9918 return values which do not match prescribed defaults. */
9919 special_function_kind sfk = sfk_none;
9921 tree dname = NULL_TREE;
9922 tree ctor_return_type = NULL_TREE;
9923 enum overload_flags flags = NO_SPECIAL;
9924 /* cv-qualifiers that apply to the declarator, for a declaration of
9925 a member function. */
9926 cp_cv_quals memfn_quals = TYPE_UNQUALIFIED;
9927 /* virt-specifiers that apply to the declarator, for a declaration of
9928 a member function. */
9929 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
9930 /* ref-qualifier that applies to the declarator, for a declaration of
9931 a member function. */
9932 cp_ref_qualifier rqual = REF_QUAL_NONE;
9933 /* cv-qualifiers that apply to the type specified by the DECLSPECS. */
9934 int type_quals = TYPE_UNQUALIFIED;
9935 tree raises = NULL_TREE;
9936 int template_count = 0;
9937 tree returned_attrs = NULL_TREE;
9938 tree parms = NULL_TREE;
9939 const cp_declarator *id_declarator;
9940 /* The unqualified name of the declarator; either an
9941 IDENTIFIER_NODE, BIT_NOT_EXPR, or TEMPLATE_ID_EXPR. */
9942 tree unqualified_id;
9943 /* The class type, if any, in which this entity is located,
9944 or NULL_TREE if none. Note that this value may be different from
9945 the current class type; for example if an attempt is made to declare
9946 "A::f" inside "B", this value will be "A". */
9947 tree ctype = current_class_type;
9948 /* The NAMESPACE_DECL for the namespace in which this entity is
9949 located. If an unqualified name is used to declare the entity,
9950 this value will be NULL_TREE, even if the entity is located at
9951 namespace scope. */
9952 tree in_namespace = NULL_TREE;
9953 cp_storage_class storage_class;
9954 bool unsigned_p, signed_p, short_p, long_p, thread_p;
9955 bool type_was_error_mark_node = false;
9956 bool parameter_pack_p = declarator ? declarator->parameter_pack_p : false;
9957 bool template_type_arg = false;
9958 bool template_parm_flag = false;
9959 bool typedef_p = decl_spec_seq_has_spec_p (declspecs, ds_typedef);
9960 bool constexpr_p = decl_spec_seq_has_spec_p (declspecs, ds_constexpr);
9961 bool late_return_type_p = false;
9962 bool array_parameter_p = false;
9963 source_location saved_loc = input_location;
9964 tree reqs = NULL_TREE;
9966 signed_p = decl_spec_seq_has_spec_p (declspecs, ds_signed);
9967 unsigned_p = decl_spec_seq_has_spec_p (declspecs, ds_unsigned);
9968 short_p = decl_spec_seq_has_spec_p (declspecs, ds_short);
9969 long_p = decl_spec_seq_has_spec_p (declspecs, ds_long);
9970 longlong = decl_spec_seq_has_spec_p (declspecs, ds_long_long);
9971 explicit_intN = declspecs->explicit_intN_p;
9972 thread_p = decl_spec_seq_has_spec_p (declspecs, ds_thread);
9974 // Was concept_p specified? Note that ds_concept
9975 // implies ds_constexpr!
9976 bool concept_p = decl_spec_seq_has_spec_p (declspecs, ds_concept);
9977 if (concept_p)
9978 constexpr_p = true;
9980 if (decl_spec_seq_has_spec_p (declspecs, ds_const))
9981 type_quals |= TYPE_QUAL_CONST;
9982 if (decl_spec_seq_has_spec_p (declspecs, ds_volatile))
9983 type_quals |= TYPE_QUAL_VOLATILE;
9984 if (decl_spec_seq_has_spec_p (declspecs, ds_restrict))
9985 type_quals |= TYPE_QUAL_RESTRICT;
9987 if (decl_context == FUNCDEF)
9988 funcdef_flag = true, decl_context = NORMAL;
9989 else if (decl_context == MEMFUNCDEF)
9990 funcdef_flag = true, decl_context = FIELD;
9991 else if (decl_context == BITFIELD)
9992 bitfield = 1, decl_context = FIELD;
9993 else if (decl_context == TEMPLATE_TYPE_ARG)
9994 template_type_arg = true, decl_context = TYPENAME;
9995 else if (decl_context == TPARM)
9996 template_parm_flag = true, decl_context = PARM;
9998 if (initialized > 1)
9999 funcdef_flag = true;
10001 location_t typespec_loc = smallest_type_quals_location (type_quals,
10002 declspecs->locations);
10003 if (typespec_loc == UNKNOWN_LOCATION)
10004 typespec_loc = declspecs->locations[ds_type_spec];
10005 if (typespec_loc == UNKNOWN_LOCATION)
10006 typespec_loc = input_location;
10008 /* Look inside a declarator for the name being declared
10009 and get it as a string, for an error message. */
10010 for (id_declarator = declarator;
10011 id_declarator;
10012 id_declarator = id_declarator->declarator)
10014 if (id_declarator->kind != cdk_id)
10015 innermost_code = id_declarator->kind;
10017 switch (id_declarator->kind)
10019 case cdk_function:
10020 if (id_declarator->declarator
10021 && id_declarator->declarator->kind == cdk_id)
10023 sfk = id_declarator->declarator->u.id.sfk;
10024 if (sfk == sfk_destructor)
10025 flags = DTOR_FLAG;
10027 break;
10029 case cdk_id:
10031 tree qualifying_scope = id_declarator->u.id.qualifying_scope;
10032 tree decl = id_declarator->u.id.unqualified_name;
10033 if (!decl)
10034 break;
10035 if (qualifying_scope)
10037 if (at_function_scope_p ())
10039 /* [dcl.meaning]
10041 A declarator-id shall not be qualified except
10042 for ...
10044 None of the cases are permitted in block
10045 scope. */
10046 if (qualifying_scope == global_namespace)
10047 error ("invalid use of qualified-name %<::%D%>",
10048 decl);
10049 else if (TYPE_P (qualifying_scope))
10050 error ("invalid use of qualified-name %<%T::%D%>",
10051 qualifying_scope, decl);
10052 else
10053 error ("invalid use of qualified-name %<%D::%D%>",
10054 qualifying_scope, decl);
10055 return error_mark_node;
10057 else if (TYPE_P (qualifying_scope))
10059 ctype = qualifying_scope;
10060 if (!MAYBE_CLASS_TYPE_P (ctype))
10062 error ("%q#T is not a class or a namespace", ctype);
10063 ctype = NULL_TREE;
10065 else if (innermost_code != cdk_function
10066 && current_class_type
10067 && !uniquely_derived_from_p (ctype,
10068 current_class_type))
10070 error ("invalid use of qualified-name %<%T::%D%>",
10071 qualifying_scope, decl);
10072 return error_mark_node;
10075 else if (TREE_CODE (qualifying_scope) == NAMESPACE_DECL)
10076 in_namespace = qualifying_scope;
10078 switch (TREE_CODE (decl))
10080 case BIT_NOT_EXPR:
10082 if (innermost_code != cdk_function)
10084 error ("declaration of %qD as non-function", decl);
10085 return error_mark_node;
10087 else if (!qualifying_scope
10088 && !(current_class_type && at_class_scope_p ()))
10090 error ("declaration of %qD as non-member", decl);
10091 return error_mark_node;
10094 tree type = TREE_OPERAND (decl, 0);
10095 if (TYPE_P (type))
10096 type = constructor_name (type);
10097 name = identifier_to_locale (IDENTIFIER_POINTER (type));
10098 dname = decl;
10100 break;
10102 case TEMPLATE_ID_EXPR:
10104 tree fns = TREE_OPERAND (decl, 0);
10106 dname = fns;
10107 if (!identifier_p (dname))
10108 dname = OVL_NAME (dname);
10110 /* Fall through. */
10112 case IDENTIFIER_NODE:
10113 if (identifier_p (decl))
10114 dname = decl;
10116 if (IDENTIFIER_KEYWORD_P (dname))
10118 error ("declarator-id missing; using reserved word %qD",
10119 dname);
10120 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
10122 else if (!IDENTIFIER_CONV_OP_P (dname))
10123 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
10124 else
10126 gcc_assert (flags == NO_SPECIAL);
10127 flags = TYPENAME_FLAG;
10128 sfk = sfk_conversion;
10129 tree glob = IDENTIFIER_GLOBAL_VALUE (dname);
10130 if (glob && TREE_CODE (glob) == TYPE_DECL)
10131 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
10132 else
10133 name = "<invalid operator>";
10135 break;
10137 default:
10138 gcc_unreachable ();
10140 break;
10143 case cdk_array:
10144 case cdk_pointer:
10145 case cdk_reference:
10146 case cdk_ptrmem:
10147 break;
10149 case cdk_decomp:
10150 name = "structured binding";
10151 break;
10153 case cdk_error:
10154 return error_mark_node;
10156 default:
10157 gcc_unreachable ();
10159 if (id_declarator->kind == cdk_id)
10160 break;
10163 /* [dcl.fct.edf]
10165 The declarator in a function-definition shall have the form
10166 D1 ( parameter-declaration-clause) ... */
10167 if (funcdef_flag && innermost_code != cdk_function)
10169 error ("function definition does not declare parameters");
10170 return error_mark_node;
10173 if (flags == TYPENAME_FLAG
10174 && innermost_code != cdk_function
10175 && ! (ctype && !declspecs->any_specifiers_p))
10177 error ("declaration of %qD as non-function", dname);
10178 return error_mark_node;
10181 if (dname && identifier_p (dname))
10183 if (UDLIT_OPER_P (dname)
10184 && innermost_code != cdk_function)
10186 error ("declaration of %qD as non-function", dname);
10187 return error_mark_node;
10190 if (IDENTIFIER_ANY_OP_P (dname))
10192 if (typedef_p)
10194 error ("declaration of %qD as %<typedef%>", dname);
10195 return error_mark_node;
10197 else if (decl_context == PARM || decl_context == CATCHPARM)
10199 error ("declaration of %qD as parameter", dname);
10200 return error_mark_node;
10205 /* Anything declared one level down from the top level
10206 must be one of the parameters of a function
10207 (because the body is at least two levels down). */
10209 /* This heuristic cannot be applied to C++ nodes! Fixed, however,
10210 by not allowing C++ class definitions to specify their parameters
10211 with xdecls (must be spec.d in the parmlist).
10213 Since we now wait to push a class scope until we are sure that
10214 we are in a legitimate method context, we must set oldcname
10215 explicitly (since current_class_name is not yet alive).
10217 We also want to avoid calling this a PARM if it is in a namespace. */
10219 if (decl_context == NORMAL && !toplevel_bindings_p ())
10221 cp_binding_level *b = current_binding_level;
10222 current_binding_level = b->level_chain;
10223 if (current_binding_level != 0 && toplevel_bindings_p ())
10224 decl_context = PARM;
10225 current_binding_level = b;
10228 if (name == NULL)
10229 name = decl_context == PARM ? "parameter" : "type name";
10231 if (concept_p && typedef_p)
10233 error ("%<concept%> cannot appear in a typedef declaration");
10234 return error_mark_node;
10237 if (constexpr_p && typedef_p)
10239 error ("%<constexpr%> cannot appear in a typedef declaration");
10240 return error_mark_node;
10243 /* If there were multiple types specified in the decl-specifier-seq,
10244 issue an error message. */
10245 if (declspecs->multiple_types_p)
10247 error ("two or more data types in declaration of %qs", name);
10248 return error_mark_node;
10251 if (declspecs->conflicting_specifiers_p)
10253 error ("conflicting specifiers in declaration of %qs", name);
10254 return error_mark_node;
10257 /* Extract the basic type from the decl-specifier-seq. */
10258 type = declspecs->type;
10259 if (type == error_mark_node)
10261 type = NULL_TREE;
10262 type_was_error_mark_node = true;
10264 /* If the entire declaration is itself tagged as deprecated then
10265 suppress reports of deprecated items. */
10266 if (type && TREE_DEPRECATED (type)
10267 && deprecated_state != DEPRECATED_SUPPRESS)
10268 warn_deprecated_use (type, NULL_TREE);
10269 if (type && TREE_CODE (type) == TYPE_DECL)
10271 typedef_decl = type;
10272 type = TREE_TYPE (typedef_decl);
10273 if (TREE_DEPRECATED (type)
10274 && DECL_ARTIFICIAL (typedef_decl)
10275 && deprecated_state != DEPRECATED_SUPPRESS)
10276 warn_deprecated_use (type, NULL_TREE);
10278 /* No type at all: default to `int', and set DEFAULTED_INT
10279 because it was not a user-defined typedef. */
10280 if (type == NULL_TREE)
10282 if (signed_p || unsigned_p || long_p || short_p)
10284 /* These imply 'int'. */
10285 type = integer_type_node;
10286 defaulted_int = 1;
10288 /* If we just have "complex", it is equivalent to "complex double". */
10289 else if (!longlong && !explicit_intN
10290 && decl_spec_seq_has_spec_p (declspecs, ds_complex))
10292 type = double_type_node;
10293 pedwarn (declspecs->locations[ds_complex], OPT_Wpedantic,
10294 "ISO C++ does not support plain %<complex%> meaning "
10295 "%<double complex%>");
10298 /* Gather flags. */
10299 explicit_int = declspecs->explicit_int_p;
10300 explicit_char = declspecs->explicit_char_p;
10302 #if 0
10303 /* See the code below that used this. */
10304 if (typedef_decl)
10305 decl_attr = DECL_ATTRIBUTES (typedef_decl);
10306 #endif
10307 typedef_type = type;
10309 if (sfk == sfk_conversion || sfk == sfk_deduction_guide)
10310 ctor_return_type = TREE_TYPE (dname);
10311 else
10312 ctor_return_type = ctype;
10314 if (sfk != sfk_none)
10316 type = check_special_function_return_type (sfk, type,
10317 ctor_return_type,
10318 type_quals,
10319 declspecs->locations);
10320 type_quals = TYPE_UNQUALIFIED;
10322 else if (type == NULL_TREE)
10324 int is_main;
10326 explicit_int = -1;
10328 /* We handle `main' specially here, because 'main () { }' is so
10329 common. With no options, it is allowed. With -Wreturn-type,
10330 it is a warning. It is only an error with -pedantic-errors. */
10331 is_main = (funcdef_flag
10332 && dname && identifier_p (dname)
10333 && MAIN_NAME_P (dname)
10334 && ctype == NULL_TREE
10335 && in_namespace == NULL_TREE
10336 && current_namespace == global_namespace);
10338 if (type_was_error_mark_node)
10339 /* We've already issued an error, don't complain more. */;
10340 else if (in_system_header_at (input_location) || flag_ms_extensions)
10341 /* Allow it, sigh. */;
10342 else if (! is_main)
10343 permerror (input_location, "ISO C++ forbids declaration of %qs with no type", name);
10344 else if (pedantic)
10345 pedwarn (input_location, OPT_Wpedantic,
10346 "ISO C++ forbids declaration of %qs with no type", name);
10347 else
10348 warning (OPT_Wreturn_type,
10349 "ISO C++ forbids declaration of %qs with no type", name);
10351 if (type_was_error_mark_node && template_parm_flag)
10352 /* FIXME we should be able to propagate the error_mark_node as is
10353 for other contexts too. */
10354 type = error_mark_node;
10355 else
10356 type = integer_type_node;
10359 ctype = NULL_TREE;
10361 if (explicit_intN)
10363 if (! int_n_enabled_p[declspecs->int_n_idx])
10365 error ("%<__int%d%> is not supported by this target",
10366 int_n_data[declspecs->int_n_idx].bitsize);
10367 explicit_intN = false;
10369 else if (pedantic && ! in_system_header_at (input_location))
10370 pedwarn (input_location, OPT_Wpedantic,
10371 "ISO C++ does not support %<__int%d%> for %qs",
10372 int_n_data[declspecs->int_n_idx].bitsize, name);
10375 /* Now process the modifiers that were specified
10376 and check for invalid combinations. */
10378 /* Long double is a special combination. */
10379 if (long_p && !longlong && TYPE_MAIN_VARIANT (type) == double_type_node)
10381 long_p = false;
10382 type = cp_build_qualified_type (long_double_type_node,
10383 cp_type_quals (type));
10386 /* Check all other uses of type modifiers. */
10388 if (unsigned_p || signed_p || long_p || short_p)
10390 int ok = 0;
10392 if ((signed_p || unsigned_p) && TREE_CODE (type) != INTEGER_TYPE)
10393 error ("%<signed%> or %<unsigned%> invalid for %qs", name);
10394 else if (signed_p && unsigned_p)
10395 error ("%<signed%> and %<unsigned%> specified together for %qs", name);
10396 else if (longlong && TREE_CODE (type) != INTEGER_TYPE)
10397 error ("%<long long%> invalid for %qs", name);
10398 else if (long_p && TREE_CODE (type) == REAL_TYPE)
10399 error ("%<long%> invalid for %qs", name);
10400 else if (short_p && TREE_CODE (type) == REAL_TYPE)
10401 error ("%<short%> invalid for %qs", name);
10402 else if ((long_p || short_p) && TREE_CODE (type) != INTEGER_TYPE)
10403 error ("%<long%> or %<short%> invalid for %qs", name);
10404 else if ((long_p || short_p || explicit_char || explicit_int) && explicit_intN)
10405 error ("%<long%>, %<int%>, %<short%>, or %<char%> invalid for %qs", name);
10406 else if ((long_p || short_p) && explicit_char)
10407 error ("%<long%> or %<short%> specified with char for %qs", name);
10408 else if (long_p && short_p)
10409 error ("%<long%> and %<short%> specified together for %qs", name);
10410 else if (type == char16_type_node || type == char32_type_node)
10412 if (signed_p || unsigned_p)
10413 error ("%<signed%> or %<unsigned%> invalid for %qs", name);
10414 else if (short_p || long_p)
10415 error ("%<short%> or %<long%> invalid for %qs", name);
10417 else
10419 ok = 1;
10420 if (!explicit_int && !defaulted_int && !explicit_char && !explicit_intN && pedantic)
10422 pedwarn (input_location, OPT_Wpedantic,
10423 "long, short, signed or unsigned used invalidly for %qs",
10424 name);
10425 if (flag_pedantic_errors)
10426 ok = 0;
10430 /* Discard the type modifiers if they are invalid. */
10431 if (! ok)
10433 unsigned_p = false;
10434 signed_p = false;
10435 long_p = false;
10436 short_p = false;
10437 longlong = 0;
10441 /* Decide whether an integer type is signed or not.
10442 Optionally treat bitfields as signed by default. */
10443 if (unsigned_p
10444 /* [class.bit]
10446 It is implementation-defined whether a plain (neither
10447 explicitly signed or unsigned) char, short, int, or long
10448 bit-field is signed or unsigned.
10450 Naturally, we extend this to long long as well. Note that
10451 this does not include wchar_t. */
10452 || (bitfield && !flag_signed_bitfields
10453 && !signed_p
10454 /* A typedef for plain `int' without `signed' can be
10455 controlled just like plain `int', but a typedef for
10456 `signed int' cannot be so controlled. */
10457 && !(typedef_decl
10458 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl))
10459 && TREE_CODE (type) == INTEGER_TYPE
10460 && !same_type_p (TYPE_MAIN_VARIANT (type), wchar_type_node)))
10462 if (explicit_intN)
10463 type = int_n_trees[declspecs->int_n_idx].unsigned_type;
10464 else if (longlong)
10465 type = long_long_unsigned_type_node;
10466 else if (long_p)
10467 type = long_unsigned_type_node;
10468 else if (short_p)
10469 type = short_unsigned_type_node;
10470 else if (type == char_type_node)
10471 type = unsigned_char_type_node;
10472 else if (typedef_decl)
10473 type = unsigned_type_for (type);
10474 else
10475 type = unsigned_type_node;
10477 else if (signed_p && type == char_type_node)
10478 type = signed_char_type_node;
10479 else if (explicit_intN)
10480 type = int_n_trees[declspecs->int_n_idx].signed_type;
10481 else if (longlong)
10482 type = long_long_integer_type_node;
10483 else if (long_p)
10484 type = long_integer_type_node;
10485 else if (short_p)
10486 type = short_integer_type_node;
10488 if (decl_spec_seq_has_spec_p (declspecs, ds_complex))
10490 if (TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
10491 error ("complex invalid for %qs", name);
10492 /* If a modifier is specified, the resulting complex is the complex
10493 form of TYPE. E.g, "complex short" is "complex short int". */
10494 else if (type == integer_type_node)
10495 type = complex_integer_type_node;
10496 else if (type == float_type_node)
10497 type = complex_float_type_node;
10498 else if (type == double_type_node)
10499 type = complex_double_type_node;
10500 else if (type == long_double_type_node)
10501 type = complex_long_double_type_node;
10502 else
10503 type = build_complex_type (type);
10506 /* If we're using the injected-class-name to form a compound type or a
10507 declaration, replace it with the underlying class so we don't get
10508 redundant typedefs in the debug output. But if we are returning the
10509 type unchanged, leave it alone so that it's available to
10510 maybe_get_template_decl_from_type_decl. */
10511 if (CLASS_TYPE_P (type)
10512 && DECL_SELF_REFERENCE_P (TYPE_NAME (type))
10513 && type == TREE_TYPE (TYPE_NAME (type))
10514 && (declarator || type_quals))
10515 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
10517 type_quals |= cp_type_quals (type);
10518 type = cp_build_qualified_type_real
10519 (type, type_quals, ((((typedef_decl && !DECL_ARTIFICIAL (typedef_decl))
10520 || declspecs->decltype_p)
10521 ? tf_ignore_bad_quals : 0) | tf_warning_or_error));
10522 /* We might have ignored or rejected some of the qualifiers. */
10523 type_quals = cp_type_quals (type);
10525 if (cxx_dialect >= cxx17 && type && is_auto (type)
10526 && innermost_code != cdk_function
10527 && id_declarator && declarator != id_declarator)
10528 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (type))
10530 error_at (typespec_loc, "template placeholder type %qT must be followed "
10531 "by a simple declarator-id", type);
10532 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here", tmpl);
10535 staticp = 0;
10536 inlinep = decl_spec_seq_has_spec_p (declspecs, ds_inline);
10537 virtualp = decl_spec_seq_has_spec_p (declspecs, ds_virtual);
10538 explicitp = decl_spec_seq_has_spec_p (declspecs, ds_explicit);
10540 storage_class = declspecs->storage_class;
10541 if (storage_class == sc_static)
10542 staticp = 1 + (decl_context == FIELD);
10544 if (virtualp)
10546 if (staticp == 2)
10548 error ("member %qD cannot be declared both %<virtual%> "
10549 "and %<static%>", dname);
10550 storage_class = sc_none;
10551 staticp = 0;
10553 if (constexpr_p)
10554 error ("member %qD cannot be declared both %<virtual%> "
10555 "and %<constexpr%>", dname);
10557 friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
10559 /* Issue errors about use of storage classes for parameters. */
10560 if (decl_context == PARM)
10562 if (typedef_p)
10564 error ("typedef declaration invalid in parameter declaration");
10565 return error_mark_node;
10567 else if (template_parm_flag && storage_class != sc_none)
10569 error ("storage class specified for template parameter %qs", name);
10570 return error_mark_node;
10572 else if (storage_class == sc_static
10573 || storage_class == sc_extern
10574 || thread_p)
10575 error ("storage class specifiers invalid in parameter declarations");
10577 /* Function parameters cannot be concept. */
10578 if (concept_p)
10579 error ("a parameter cannot be declared %<concept%>");
10580 /* Function parameters cannot be constexpr. If we saw one, moan
10581 and pretend it wasn't there. */
10582 else if (constexpr_p)
10584 error ("a parameter cannot be declared %<constexpr%>");
10585 constexpr_p = 0;
10589 /* Give error if `virtual' is used outside of class declaration. */
10590 if (virtualp
10591 && (current_class_name == NULL_TREE || decl_context != FIELD))
10593 error_at (declspecs->locations[ds_virtual],
10594 "%<virtual%> outside class declaration");
10595 virtualp = 0;
10598 if (innermost_code == cdk_decomp)
10600 location_t loc = (declarator->kind == cdk_reference
10601 ? declarator->declarator->id_loc : declarator->id_loc);
10602 if (inlinep)
10603 error_at (declspecs->locations[ds_inline],
10604 "structured binding declaration cannot be %<inline%>");
10605 if (typedef_p)
10606 error_at (declspecs->locations[ds_typedef],
10607 "structured binding declaration cannot be %<typedef%>");
10608 if (constexpr_p)
10609 error_at (declspecs->locations[ds_constexpr], "structured "
10610 "binding declaration cannot be %<constexpr%>");
10611 if (thread_p)
10612 error_at (declspecs->locations[ds_thread],
10613 "structured binding declaration cannot be %qs",
10614 declspecs->gnu_thread_keyword_p
10615 ? "__thread" : "thread_local");
10616 if (concept_p)
10617 error_at (declspecs->locations[ds_concept],
10618 "structured binding declaration cannot be %<concept%>");
10619 switch (storage_class)
10621 case sc_none:
10622 break;
10623 case sc_register:
10624 error_at (loc, "structured binding declaration cannot be "
10625 "%<register%>");
10626 break;
10627 case sc_static:
10628 error_at (loc, "structured binding declaration cannot be "
10629 "%<static%>");
10630 break;
10631 case sc_extern:
10632 error_at (loc, "structured binding declaration cannot be "
10633 "%<extern%>");
10634 break;
10635 case sc_mutable:
10636 error_at (loc, "structured binding declaration cannot be "
10637 "%<mutable%>");
10638 break;
10639 case sc_auto:
10640 error_at (loc, "structured binding declaration cannot be "
10641 "C++98 %<auto%>");
10642 break;
10643 default:
10644 gcc_unreachable ();
10646 if (TREE_CODE (type) != TEMPLATE_TYPE_PARM
10647 || TYPE_IDENTIFIER (type) != auto_identifier)
10649 if (type != error_mark_node)
10651 error_at (loc, "structured binding declaration cannot have "
10652 "type %qT", type);
10653 inform (loc,
10654 "type must be cv-qualified %<auto%> or reference to "
10655 "cv-qualified %<auto%>");
10657 type = build_qualified_type (make_auto (), type_quals);
10658 declspecs->type = type;
10660 inlinep = 0;
10661 typedef_p = 0;
10662 constexpr_p = 0;
10663 thread_p = 0;
10664 concept_p = 0;
10665 storage_class = sc_none;
10666 staticp = 0;
10667 declspecs->storage_class = sc_none;
10668 declspecs->locations[ds_thread] = UNKNOWN_LOCATION;
10671 /* Static anonymous unions are dealt with here. */
10672 if (staticp && decl_context == TYPENAME
10673 && declspecs->type
10674 && ANON_AGGR_TYPE_P (declspecs->type))
10675 decl_context = FIELD;
10677 /* Warn about storage classes that are invalid for certain
10678 kinds of declarations (parameters, typenames, etc.). */
10679 if (thread_p
10680 && ((storage_class
10681 && storage_class != sc_extern
10682 && storage_class != sc_static)
10683 || typedef_p))
10685 error ("multiple storage classes in declaration of %qs", name);
10686 thread_p = false;
10688 if (decl_context != NORMAL
10689 && ((storage_class != sc_none
10690 && storage_class != sc_mutable)
10691 || thread_p))
10693 if ((decl_context == PARM || decl_context == CATCHPARM)
10694 && (storage_class == sc_register
10695 || storage_class == sc_auto))
10697 else if (typedef_p)
10699 else if (decl_context == FIELD
10700 /* C++ allows static class elements. */
10701 && storage_class == sc_static)
10702 /* C++ also allows inlines and signed and unsigned elements,
10703 but in those cases we don't come in here. */
10705 else
10707 if (decl_context == FIELD)
10708 error ("storage class specified for %qs", name);
10709 else
10711 if (decl_context == PARM || decl_context == CATCHPARM)
10712 error ("storage class specified for parameter %qs", name);
10713 else
10714 error ("storage class specified for typename");
10716 if (storage_class == sc_register
10717 || storage_class == sc_auto
10718 || storage_class == sc_extern
10719 || thread_p)
10720 storage_class = sc_none;
10723 else if (storage_class == sc_extern && funcdef_flag
10724 && ! toplevel_bindings_p ())
10725 error ("nested function %qs declared %<extern%>", name);
10726 else if (toplevel_bindings_p ())
10728 if (storage_class == sc_auto)
10729 error ("top-level declaration of %qs specifies %<auto%>", name);
10731 else if (thread_p
10732 && storage_class != sc_extern
10733 && storage_class != sc_static)
10735 if (declspecs->gnu_thread_keyword_p)
10736 pedwarn (input_location, 0, "function-scope %qs implicitly auto and "
10737 "declared %<__thread%>", name);
10739 /* When thread_local is applied to a variable of block scope the
10740 storage-class-specifier static is implied if it does not appear
10741 explicitly. */
10742 storage_class = declspecs->storage_class = sc_static;
10743 staticp = 1;
10746 if (storage_class && friendp)
10748 error ("storage class specifiers invalid in friend function declarations");
10749 storage_class = sc_none;
10750 staticp = 0;
10753 if (!id_declarator)
10754 unqualified_id = NULL_TREE;
10755 else
10757 unqualified_id = id_declarator->u.id.unqualified_name;
10758 switch (TREE_CODE (unqualified_id))
10760 case BIT_NOT_EXPR:
10761 unqualified_id = TREE_OPERAND (unqualified_id, 0);
10762 if (TYPE_P (unqualified_id))
10763 unqualified_id = constructor_name (unqualified_id);
10764 break;
10766 case IDENTIFIER_NODE:
10767 case TEMPLATE_ID_EXPR:
10768 break;
10770 default:
10771 gcc_unreachable ();
10775 if (declspecs->std_attributes)
10777 /* Apply the c++11 attributes to the type preceding them. */
10778 input_location = declspecs->locations[ds_std_attribute];
10779 decl_attributes (&type, declspecs->std_attributes, 0);
10780 input_location = saved_loc;
10783 /* Determine the type of the entity declared by recurring on the
10784 declarator. */
10785 for (; declarator; declarator = declarator->declarator)
10787 const cp_declarator *inner_declarator;
10788 tree attrs;
10790 if (type == error_mark_node)
10791 return error_mark_node;
10793 attrs = declarator->attributes;
10794 if (attrs)
10796 int attr_flags;
10798 attr_flags = 0;
10799 if (declarator == NULL || declarator->kind == cdk_id)
10800 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
10801 if (declarator->kind == cdk_function)
10802 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
10803 if (declarator->kind == cdk_array)
10804 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
10805 returned_attrs = decl_attributes (&type,
10806 chainon (returned_attrs, attrs),
10807 attr_flags);
10810 if (declarator->kind == cdk_id || declarator->kind == cdk_decomp)
10811 break;
10813 inner_declarator = declarator->declarator;
10815 switch (declarator->kind)
10817 case cdk_array:
10818 type = create_array_type_for_decl (dname, type,
10819 declarator->u.array.bounds);
10820 if (!valid_array_size_p (input_location, type, dname))
10821 type = error_mark_node;
10823 if (declarator->std_attributes)
10824 /* [dcl.array]/1:
10826 The optional attribute-specifier-seq appertains to the
10827 array. */
10828 returned_attrs = chainon (returned_attrs,
10829 declarator->std_attributes);
10830 break;
10832 case cdk_function:
10834 tree arg_types;
10835 int funcdecl_p;
10837 /* Declaring a function type. */
10839 input_location = declspecs->locations[ds_type_spec];
10840 abstract_virtuals_error (ACU_RETURN, type);
10841 input_location = saved_loc;
10843 /* Pick up type qualifiers which should be applied to `this'. */
10844 memfn_quals = declarator->u.function.qualifiers;
10845 /* Pick up virt-specifiers. */
10846 virt_specifiers = declarator->u.function.virt_specifiers;
10847 /* And ref-qualifier, too */
10848 rqual = declarator->u.function.ref_qualifier;
10849 /* And tx-qualifier. */
10850 tree tx_qual = declarator->u.function.tx_qualifier;
10851 /* Pick up the exception specifications. */
10852 raises = declarator->u.function.exception_specification;
10853 /* If the exception-specification is ill-formed, let's pretend
10854 there wasn't one. */
10855 if (raises == error_mark_node)
10856 raises = NULL_TREE;
10858 if (reqs)
10859 error_at (location_of (reqs), "requires-clause on return type");
10860 reqs = declarator->u.function.requires_clause;
10862 /* Say it's a definition only for the CALL_EXPR
10863 closest to the identifier. */
10864 funcdecl_p = inner_declarator && inner_declarator->kind == cdk_id;
10866 /* Handle a late-specified return type. */
10867 tree late_return_type = declarator->u.function.late_return_type;
10868 if (funcdecl_p)
10870 if (tree auto_node = type_uses_auto (type))
10872 if (!late_return_type)
10874 if (current_class_type
10875 && LAMBDA_TYPE_P (current_class_type))
10876 /* OK for C++11 lambdas. */;
10877 else if (cxx_dialect < cxx14)
10879 error ("%qs function uses "
10880 "%<auto%> type specifier without trailing "
10881 "return type", name);
10882 inform (input_location, "deduced return type "
10883 "only available with -std=c++14 or "
10884 "-std=gnu++14");
10886 else if (virtualp)
10888 error ("virtual function cannot "
10889 "have deduced return type");
10890 virtualp = false;
10893 else if (!is_auto (type) && sfk != sfk_conversion)
10895 error ("%qs function with trailing return type has"
10896 " %qT as its type rather than plain %<auto%>",
10897 name, type);
10898 return error_mark_node;
10900 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
10902 if (!late_return_type)
10904 if (dguide_name_p (unqualified_id))
10905 error_at (declarator->id_loc, "deduction guide "
10906 "for %qT must have trailing return "
10907 "type", TREE_TYPE (tmpl));
10908 else
10909 error_at (declarator->id_loc, "deduced class "
10910 "type %qT in function return type",
10911 type);
10912 inform (DECL_SOURCE_LOCATION (tmpl),
10913 "%qD declared here", tmpl);
10915 else if (CLASS_TYPE_P (late_return_type)
10916 && CLASSTYPE_TEMPLATE_INFO (late_return_type)
10917 && (CLASSTYPE_TI_TEMPLATE (late_return_type)
10918 == tmpl))
10919 /* OK */;
10920 else
10921 error ("trailing return type %qT of deduction guide "
10922 "is not a specialization of %qT",
10923 late_return_type, TREE_TYPE (tmpl));
10926 else if (late_return_type
10927 && sfk != sfk_conversion)
10929 if (cxx_dialect < cxx11)
10930 /* Not using maybe_warn_cpp0x because this should
10931 always be an error. */
10932 error ("trailing return type only available with "
10933 "-std=c++11 or -std=gnu++11");
10934 else
10935 error ("%qs function with trailing return type not "
10936 "declared with %<auto%> type specifier", name);
10937 return error_mark_node;
10940 type = splice_late_return_type (type, late_return_type);
10941 if (type == error_mark_node)
10942 return error_mark_node;
10944 if (late_return_type)
10946 late_return_type_p = true;
10947 type_quals = cp_type_quals (type);
10950 if (type_quals != TYPE_UNQUALIFIED)
10952 if (SCALAR_TYPE_P (type) || VOID_TYPE_P (type))
10953 warning_at (typespec_loc, OPT_Wignored_qualifiers, "type "
10954 "qualifiers ignored on function return type");
10955 /* We now know that the TYPE_QUALS don't apply to the
10956 decl, but to its return type. */
10957 type_quals = TYPE_UNQUALIFIED;
10960 /* Error about some types functions can't return. */
10962 if (TREE_CODE (type) == FUNCTION_TYPE)
10964 error_at (typespec_loc, "%qs declared as function returning "
10965 "a function", name);
10966 return error_mark_node;
10968 if (TREE_CODE (type) == ARRAY_TYPE)
10970 error_at (typespec_loc, "%qs declared as function returning "
10971 "an array", name);
10972 return error_mark_node;
10975 if (ctype == NULL_TREE
10976 && decl_context == FIELD
10977 && funcdecl_p
10978 && friendp == 0)
10979 ctype = current_class_type;
10981 if (ctype && (sfk == sfk_constructor
10982 || sfk == sfk_destructor))
10984 /* We are within a class's scope. If our declarator name
10985 is the same as the class name, and we are defining
10986 a function, then it is a constructor/destructor, and
10987 therefore returns a void type. */
10989 /* ISO C++ 12.4/2. A destructor may not be declared
10990 const or volatile. A destructor may not be static.
10991 A destructor may not be declared with ref-qualifier.
10993 ISO C++ 12.1. A constructor may not be declared
10994 const or volatile. A constructor may not be
10995 virtual. A constructor may not be static.
10996 A constructor may not be declared with ref-qualifier. */
10997 if (staticp == 2)
10998 error ((flags == DTOR_FLAG)
10999 ? G_("destructor cannot be static member function")
11000 : G_("constructor cannot be static member function"));
11001 if (memfn_quals)
11003 error ((flags == DTOR_FLAG)
11004 ? G_("destructors may not be cv-qualified")
11005 : G_("constructors may not be cv-qualified"));
11006 memfn_quals = TYPE_UNQUALIFIED;
11009 if (rqual)
11011 maybe_warn_cpp0x (CPP0X_REF_QUALIFIER);
11012 error ((flags == DTOR_FLAG)
11013 ? G_("destructors may not be ref-qualified")
11014 : G_("constructors may not be ref-qualified"));
11015 rqual = REF_QUAL_NONE;
11018 if (decl_context == FIELD
11019 && !member_function_or_else (ctype,
11020 current_class_type,
11021 flags))
11022 return error_mark_node;
11024 if (flags != DTOR_FLAG)
11026 /* It's a constructor. */
11027 if (explicitp == 1)
11028 explicitp = 2;
11029 if (virtualp)
11031 permerror (input_location,
11032 "constructors cannot be declared %<virtual%>");
11033 virtualp = 0;
11035 if (decl_context == FIELD
11036 && sfk != sfk_constructor)
11037 return error_mark_node;
11039 if (decl_context == FIELD)
11040 staticp = 0;
11042 else if (friendp)
11044 if (virtualp)
11046 /* Cannot be both friend and virtual. */
11047 error ("virtual functions cannot be friends");
11048 friendp = 0;
11050 if (decl_context == NORMAL)
11051 error ("friend declaration not in class definition");
11052 if (current_function_decl && funcdef_flag)
11053 error ("can%'t define friend function %qs in a local "
11054 "class definition",
11055 name);
11057 else if (ctype && sfk == sfk_conversion)
11059 if (explicitp == 1)
11061 maybe_warn_cpp0x (CPP0X_EXPLICIT_CONVERSION);
11062 explicitp = 2;
11064 if (late_return_type_p)
11065 error ("a conversion function cannot have a trailing return type");
11067 else if (sfk == sfk_deduction_guide)
11069 if (explicitp == 1)
11070 explicitp = 2;
11073 arg_types = grokparms (declarator->u.function.parameters,
11074 &parms);
11076 if (inner_declarator
11077 && inner_declarator->kind == cdk_id
11078 && inner_declarator->u.id.sfk == sfk_destructor
11079 && arg_types != void_list_node)
11081 error ("destructors may not have parameters");
11082 arg_types = void_list_node;
11083 parms = NULL_TREE;
11086 type = build_function_type (type, arg_types);
11088 tree attrs = declarator->std_attributes;
11089 if (tx_qual)
11091 tree att = build_tree_list (tx_qual, NULL_TREE);
11092 /* transaction_safe applies to the type, but
11093 transaction_safe_dynamic applies to the function. */
11094 if (is_attribute_p ("transaction_safe", tx_qual))
11095 attrs = chainon (attrs, att);
11096 else
11097 returned_attrs = chainon (returned_attrs, att);
11099 if (attrs)
11100 /* [dcl.fct]/2:
11102 The optional attribute-specifier-seq appertains to
11103 the function type. */
11104 decl_attributes (&type, attrs, 0);
11106 if (raises)
11107 type = build_exception_variant (type, raises);
11109 break;
11111 case cdk_pointer:
11112 case cdk_reference:
11113 case cdk_ptrmem:
11114 /* Filter out pointers-to-references and references-to-references.
11115 We can get these if a TYPE_DECL is used. */
11117 if (TREE_CODE (type) == REFERENCE_TYPE)
11119 if (declarator->kind != cdk_reference)
11121 error ("cannot declare pointer to %q#T", type);
11122 type = TREE_TYPE (type);
11125 /* In C++0x, we allow reference to reference declarations
11126 that occur indirectly through typedefs [7.1.3/8 dcl.typedef]
11127 and template type arguments [14.3.1/4 temp.arg.type]. The
11128 check for direct reference to reference declarations, which
11129 are still forbidden, occurs below. Reasoning behind the change
11130 can be found in DR106, DR540, and the rvalue reference
11131 proposals. */
11132 else if (cxx_dialect == cxx98)
11134 error ("cannot declare reference to %q#T", type);
11135 type = TREE_TYPE (type);
11138 else if (VOID_TYPE_P (type))
11140 if (declarator->kind == cdk_reference)
11141 error ("cannot declare reference to %q#T", type);
11142 else if (declarator->kind == cdk_ptrmem)
11143 error ("cannot declare pointer to %q#T member", type);
11146 /* We now know that the TYPE_QUALS don't apply to the decl,
11147 but to the target of the pointer. */
11148 type_quals = TYPE_UNQUALIFIED;
11150 /* This code used to handle METHOD_TYPE, but I don't think it's
11151 possible to get it here anymore. */
11152 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
11153 if (declarator->kind == cdk_ptrmem
11154 && TREE_CODE (type) == FUNCTION_TYPE)
11156 memfn_quals |= type_memfn_quals (type);
11157 type = build_memfn_type (type,
11158 declarator->u.pointer.class_type,
11159 memfn_quals,
11160 rqual);
11161 if (type == error_mark_node)
11162 return error_mark_node;
11164 rqual = REF_QUAL_NONE;
11165 memfn_quals = TYPE_UNQUALIFIED;
11168 if (TREE_CODE (type) == FUNCTION_TYPE
11169 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
11170 || type_memfn_rqual (type) != REF_QUAL_NONE))
11171 error (declarator->kind == cdk_reference
11172 ? G_("cannot declare reference to qualified function type %qT")
11173 : G_("cannot declare pointer to qualified function type %qT"),
11174 type);
11176 /* When the pointed-to type involves components of variable size,
11177 care must be taken to ensure that the size evaluation code is
11178 emitted early enough to dominate all the possible later uses
11179 and late enough for the variables on which it depends to have
11180 been assigned.
11182 This is expected to happen automatically when the pointed-to
11183 type has a name/declaration of it's own, but special attention
11184 is required if the type is anonymous.
11186 We handle the NORMAL and FIELD contexts here by inserting a
11187 dummy statement that just evaluates the size at a safe point
11188 and ensures it is not deferred until e.g. within a deeper
11189 conditional context (c++/43555).
11191 We expect nothing to be needed here for PARM or TYPENAME.
11192 Evaluating the size at this point for TYPENAME would
11193 actually be incorrect, as we might be in the middle of an
11194 expression with side effects on the pointed-to type size
11195 "arguments" prior to the pointer declaration point and the
11196 size evaluation could end up prior to the side effects. */
11198 if (!TYPE_NAME (type)
11199 && (decl_context == NORMAL || decl_context == FIELD)
11200 && at_function_scope_p ()
11201 && variably_modified_type_p (type, NULL_TREE))
11203 TYPE_NAME (type) = build_decl (UNKNOWN_LOCATION, TYPE_DECL,
11204 NULL_TREE, type);
11205 add_decl_expr (TYPE_NAME (type));
11208 if (declarator->kind == cdk_reference)
11210 /* In C++0x, the type we are creating a reference to might be
11211 a typedef which is itself a reference type. In that case,
11212 we follow the reference collapsing rules in
11213 [7.1.3/8 dcl.typedef] to create the final reference type:
11215 "If a typedef TD names a type that is a reference to a type
11216 T, an attempt to create the type 'lvalue reference to cv TD'
11217 creates the type 'lvalue reference to T,' while an attempt
11218 to create the type "rvalue reference to cv TD' creates the
11219 type TD."
11221 if (VOID_TYPE_P (type))
11222 /* We already gave an error. */;
11223 else if (TREE_CODE (type) == REFERENCE_TYPE)
11225 if (declarator->u.reference.rvalue_ref)
11226 /* Leave type alone. */;
11227 else
11228 type = cp_build_reference_type (TREE_TYPE (type), false);
11230 else
11231 type = cp_build_reference_type
11232 (type, declarator->u.reference.rvalue_ref);
11234 /* In C++0x, we need this check for direct reference to
11235 reference declarations, which are forbidden by
11236 [8.3.2/5 dcl.ref]. Reference to reference declarations
11237 are only allowed indirectly through typedefs and template
11238 type arguments. Example:
11240 void foo(int & &); // invalid ref-to-ref decl
11242 typedef int & int_ref;
11243 void foo(int_ref &); // valid ref-to-ref decl
11245 if (inner_declarator && inner_declarator->kind == cdk_reference)
11246 error ("cannot declare reference to %q#T, which is not "
11247 "a typedef or a template type argument", type);
11249 else if (TREE_CODE (type) == METHOD_TYPE)
11250 type = build_ptrmemfunc_type (build_pointer_type (type));
11251 else if (declarator->kind == cdk_ptrmem)
11253 gcc_assert (TREE_CODE (declarator->u.pointer.class_type)
11254 != NAMESPACE_DECL);
11255 if (declarator->u.pointer.class_type == error_mark_node)
11256 /* We will already have complained. */
11257 type = error_mark_node;
11258 else
11259 type = build_ptrmem_type (declarator->u.pointer.class_type,
11260 type);
11262 else
11263 type = build_pointer_type (type);
11265 /* Process a list of type modifier keywords (such as
11266 const or volatile) that were given inside the `*' or `&'. */
11268 if (declarator->u.pointer.qualifiers)
11270 type
11271 = cp_build_qualified_type (type,
11272 declarator->u.pointer.qualifiers);
11273 type_quals = cp_type_quals (type);
11276 /* Apply C++11 attributes to the pointer, and not to the
11277 type pointed to. This is unlike what is done for GNU
11278 attributes above. It is to comply with [dcl.ptr]/1:
11280 [the optional attribute-specifier-seq (7.6.1) appertains
11281 to the pointer and not to the object pointed to]. */
11282 if (declarator->std_attributes)
11283 decl_attributes (&type, declarator->std_attributes,
11286 ctype = NULL_TREE;
11287 break;
11289 case cdk_error:
11290 break;
11292 default:
11293 gcc_unreachable ();
11297 /* A `constexpr' specifier used in an object declaration declares
11298 the object as `const'. */
11299 if (constexpr_p && innermost_code != cdk_function)
11301 /* DR1688 says that a `constexpr' specifier in combination with
11302 `volatile' is valid. */
11304 if (TREE_CODE (type) != REFERENCE_TYPE)
11306 type_quals |= TYPE_QUAL_CONST;
11307 type = cp_build_qualified_type (type, type_quals);
11311 if (unqualified_id && TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR
11312 && TREE_CODE (type) != FUNCTION_TYPE
11313 && TREE_CODE (type) != METHOD_TYPE
11314 && !variable_template_p (TREE_OPERAND (unqualified_id, 0)))
11316 error ("template-id %qD used as a declarator",
11317 unqualified_id);
11318 unqualified_id = dname;
11321 /* If TYPE is a FUNCTION_TYPE, but the function name was explicitly
11322 qualified with a class-name, turn it into a METHOD_TYPE, unless
11323 we know that the function is static. We take advantage of this
11324 opportunity to do other processing that pertains to entities
11325 explicitly declared to be class members. Note that if DECLARATOR
11326 is non-NULL, we know it is a cdk_id declarator; otherwise, we
11327 would not have exited the loop above. */
11328 if (declarator
11329 && declarator->kind == cdk_id
11330 && declarator->u.id.qualifying_scope
11331 && MAYBE_CLASS_TYPE_P (declarator->u.id.qualifying_scope))
11333 ctype = declarator->u.id.qualifying_scope;
11334 ctype = TYPE_MAIN_VARIANT (ctype);
11335 template_count = num_template_headers_for_class (ctype);
11337 if (ctype == current_class_type)
11339 if (friendp)
11341 permerror (input_location, "member functions are implicitly "
11342 "friends of their class");
11343 friendp = 0;
11345 else
11346 permerror (declarator->id_loc,
11347 "extra qualification %<%T::%> on member %qs",
11348 ctype, name);
11350 else if (/* If the qualifying type is already complete, then we
11351 can skip the following checks. */
11352 !COMPLETE_TYPE_P (ctype)
11353 && (/* If the function is being defined, then
11354 qualifying type must certainly be complete. */
11355 funcdef_flag
11356 /* A friend declaration of "T::f" is OK, even if
11357 "T" is a template parameter. But, if this
11358 function is not a friend, the qualifying type
11359 must be a class. */
11360 || (!friendp && !CLASS_TYPE_P (ctype))
11361 /* For a declaration, the type need not be
11362 complete, if either it is dependent (since there
11363 is no meaningful definition of complete in that
11364 case) or the qualifying class is currently being
11365 defined. */
11366 || !(dependent_type_p (ctype)
11367 || currently_open_class (ctype)))
11368 /* Check that the qualifying type is complete. */
11369 && !complete_type_or_else (ctype, NULL_TREE))
11370 return error_mark_node;
11371 else if (TREE_CODE (type) == FUNCTION_TYPE)
11373 if (current_class_type
11374 && (!friendp || funcdef_flag || initialized))
11376 error (funcdef_flag || initialized
11377 ? G_("cannot define member function %<%T::%s%> "
11378 "within %qT")
11379 : G_("cannot declare member function %<%T::%s%> "
11380 "within %qT"),
11381 ctype, name, current_class_type);
11382 return error_mark_node;
11385 else if (typedef_p && current_class_type)
11387 error ("cannot declare member %<%T::%s%> within %qT",
11388 ctype, name, current_class_type);
11389 return error_mark_node;
11393 if (ctype == NULL_TREE && decl_context == FIELD && friendp == 0)
11394 ctype = current_class_type;
11396 /* Now TYPE has the actual type. */
11398 if (returned_attrs)
11400 if (attrlist)
11401 *attrlist = chainon (returned_attrs, *attrlist);
11402 else
11403 attrlist = &returned_attrs;
11406 if (declarator
11407 && declarator->kind == cdk_id
11408 && declarator->std_attributes
11409 && attrlist != NULL)
11410 /* [dcl.meaning]/1: The optional attribute-specifier-seq following
11411 a declarator-id appertains to the entity that is declared. */
11412 *attrlist = chainon (*attrlist, declarator->std_attributes);
11414 /* Handle parameter packs. */
11415 if (parameter_pack_p)
11417 if (decl_context == PARM)
11418 /* Turn the type into a pack expansion.*/
11419 type = make_pack_expansion (type);
11420 else
11421 error ("non-parameter %qs cannot be a parameter pack", name);
11424 if ((decl_context == FIELD || decl_context == PARM)
11425 && !processing_template_decl
11426 && variably_modified_type_p (type, NULL_TREE))
11428 if (decl_context == FIELD)
11429 error ("data member may not have variably modified type %qT", type);
11430 else
11431 error ("parameter may not have variably modified type %qT", type);
11432 type = error_mark_node;
11435 if (explicitp == 1 || (explicitp && friendp))
11437 /* [dcl.fct.spec] (C++11) The explicit specifier shall be used only
11438 in the declaration of a constructor or conversion function within
11439 a class definition. */
11440 if (!current_class_type)
11441 error_at (declspecs->locations[ds_explicit],
11442 "%<explicit%> outside class declaration");
11443 else if (friendp)
11444 error_at (declspecs->locations[ds_explicit],
11445 "%<explicit%> in friend declaration");
11446 else
11447 error_at (declspecs->locations[ds_explicit],
11448 "only declarations of constructors and conversion operators "
11449 "can be %<explicit%>");
11450 explicitp = 0;
11453 if (storage_class == sc_mutable)
11455 if (decl_context != FIELD || friendp)
11457 error ("non-member %qs cannot be declared %<mutable%>", name);
11458 storage_class = sc_none;
11460 else if (decl_context == TYPENAME || typedef_p)
11462 error ("non-object member %qs cannot be declared %<mutable%>", name);
11463 storage_class = sc_none;
11465 else if (TREE_CODE (type) == FUNCTION_TYPE
11466 || TREE_CODE (type) == METHOD_TYPE)
11468 error ("function %qs cannot be declared %<mutable%>", name);
11469 storage_class = sc_none;
11471 else if (staticp)
11473 error ("static %qs cannot be declared %<mutable%>", name);
11474 storage_class = sc_none;
11476 else if (type_quals & TYPE_QUAL_CONST)
11478 error ("const %qs cannot be declared %<mutable%>", name);
11479 storage_class = sc_none;
11481 else if (TREE_CODE (type) == REFERENCE_TYPE)
11483 permerror (input_location, "reference %qs cannot be declared "
11484 "%<mutable%>", name);
11485 storage_class = sc_none;
11489 /* If this is declaring a typedef name, return a TYPE_DECL. */
11490 if (typedef_p && decl_context != TYPENAME)
11492 tree decl;
11494 /* This declaration:
11496 typedef void f(int) const;
11498 declares a function type which is not a member of any
11499 particular class, but which is cv-qualified; for
11500 example "f S::*" declares a pointer to a const-qualified
11501 member function of S. We record the cv-qualification in the
11502 function type. */
11503 if ((rqual || memfn_quals) && TREE_CODE (type) == FUNCTION_TYPE)
11505 type = apply_memfn_quals (type, memfn_quals, rqual);
11507 /* We have now dealt with these qualifiers. */
11508 memfn_quals = TYPE_UNQUALIFIED;
11509 rqual = REF_QUAL_NONE;
11512 if (type_uses_auto (type))
11514 error ("typedef declared %<auto%>");
11515 type = error_mark_node;
11518 if (reqs)
11519 error_at (location_of (reqs), "requires-clause on typedef");
11521 if (decl_context == FIELD)
11522 decl = build_lang_decl (TYPE_DECL, unqualified_id, type);
11523 else
11524 decl = build_decl (input_location, TYPE_DECL, unqualified_id, type);
11525 if (id_declarator && declarator->u.id.qualifying_scope) {
11526 error_at (DECL_SOURCE_LOCATION (decl),
11527 "typedef name may not be a nested-name-specifier");
11528 TREE_TYPE (decl) = error_mark_node;
11531 if (decl_context != FIELD)
11533 if (!current_function_decl)
11534 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
11535 else if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (current_function_decl)
11536 || (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P
11537 (current_function_decl)))
11538 /* The TYPE_DECL is "abstract" because there will be
11539 clones of this constructor/destructor, and there will
11540 be copies of this TYPE_DECL generated in those
11541 clones. The decloning optimization (for space) may
11542 revert this subsequently if it determines that
11543 the clones should share a common implementation. */
11544 DECL_ABSTRACT_P (decl) = true;
11546 else if (current_class_type
11547 && constructor_name_p (unqualified_id, current_class_type))
11548 permerror (input_location, "ISO C++ forbids nested type %qD with same name "
11549 "as enclosing class",
11550 unqualified_id);
11552 /* If the user declares "typedef struct {...} foo" then the
11553 struct will have an anonymous name. Fill that name in now.
11554 Nothing can refer to it, so nothing needs know about the name
11555 change. */
11556 if (type != error_mark_node
11557 && unqualified_id
11558 && TYPE_NAME (type)
11559 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
11560 && TYPE_UNNAMED_P (type)
11561 && declspecs->type_definition_p
11562 && attributes_naming_typedef_ok (*attrlist)
11563 && cp_type_quals (type) == TYPE_UNQUALIFIED)
11564 name_unnamed_type (type, decl);
11566 if (signed_p
11567 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
11568 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
11570 bad_specifiers (decl, BSP_TYPE, virtualp,
11571 memfn_quals != TYPE_UNQUALIFIED,
11572 inlinep, friendp, raises != NULL_TREE);
11574 if (decl_spec_seq_has_spec_p (declspecs, ds_alias))
11575 /* Acknowledge that this was written:
11576 `using analias = atype;'. */
11577 TYPE_DECL_ALIAS_P (decl) = 1;
11579 return decl;
11582 /* Detect the case of an array type of unspecified size
11583 which came, as such, direct from a typedef name.
11584 We must copy the type, so that the array's domain can be
11585 individually set by the object's initializer. */
11587 if (type && typedef_type
11588 && TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
11589 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
11590 type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
11592 /* Detect where we're using a typedef of function type to declare a
11593 function. PARMS will not be set, so we must create it now. */
11595 if (type == typedef_type && TREE_CODE (type) == FUNCTION_TYPE)
11597 tree decls = NULL_TREE;
11598 tree args;
11600 for (args = TYPE_ARG_TYPES (type);
11601 args && args != void_list_node;
11602 args = TREE_CHAIN (args))
11604 tree decl = cp_build_parm_decl (NULL_TREE, NULL_TREE,
11605 TREE_VALUE (args));
11607 DECL_CHAIN (decl) = decls;
11608 decls = decl;
11611 parms = nreverse (decls);
11613 if (decl_context != TYPENAME)
11615 /* The qualifiers on the function type become the qualifiers on
11616 the non-static member function. */
11617 memfn_quals |= type_memfn_quals (type);
11618 rqual = type_memfn_rqual (type);
11619 type_quals = TYPE_UNQUALIFIED;
11623 /* If this is a type name (such as, in a cast or sizeof),
11624 compute the type and return it now. */
11626 if (decl_context == TYPENAME)
11628 /* Note that here we don't care about type_quals. */
11630 /* Special case: "friend class foo" looks like a TYPENAME context. */
11631 if (friendp)
11633 if (inlinep)
11635 error ("%<inline%> specified for friend class declaration");
11636 inlinep = 0;
11639 if (!current_aggr)
11641 /* Don't allow friend declaration without a class-key. */
11642 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
11643 permerror (input_location, "template parameters cannot be friends");
11644 else if (TREE_CODE (type) == TYPENAME_TYPE)
11645 permerror (input_location, "friend declaration requires class-key, "
11646 "i.e. %<friend class %T::%D%>",
11647 TYPE_CONTEXT (type), TYPENAME_TYPE_FULLNAME (type));
11648 else
11649 permerror (input_location, "friend declaration requires class-key, "
11650 "i.e. %<friend %#T%>",
11651 type);
11654 /* Only try to do this stuff if we didn't already give up. */
11655 if (type != integer_type_node)
11657 /* A friendly class? */
11658 if (current_class_type)
11659 make_friend_class (current_class_type, TYPE_MAIN_VARIANT (type),
11660 /*complain=*/true);
11661 else
11662 error ("trying to make class %qT a friend of global scope",
11663 type);
11665 type = void_type_node;
11668 else if (memfn_quals || rqual)
11670 if (ctype == NULL_TREE
11671 && TREE_CODE (type) == METHOD_TYPE)
11672 ctype = TYPE_METHOD_BASETYPE (type);
11674 if (ctype)
11675 type = build_memfn_type (type, ctype, memfn_quals, rqual);
11676 /* Core issue #547: need to allow this in template type args.
11677 Allow it in general in C++11 for alias-declarations. */
11678 else if ((template_type_arg || cxx_dialect >= cxx11)
11679 && TREE_CODE (type) == FUNCTION_TYPE)
11680 type = apply_memfn_quals (type, memfn_quals, rqual);
11681 else
11682 error ("invalid qualifiers on non-member function type");
11685 if (reqs)
11686 error_at (location_of (reqs), "requires-clause on type-id");
11688 return type;
11690 else if (unqualified_id == NULL_TREE && decl_context != PARM
11691 && decl_context != CATCHPARM
11692 && TREE_CODE (type) != UNION_TYPE
11693 && ! bitfield
11694 && innermost_code != cdk_decomp)
11696 error ("abstract declarator %qT used as declaration", type);
11697 return error_mark_node;
11700 if (!FUNC_OR_METHOD_TYPE_P (type))
11702 /* Only functions may be declared using an operator-function-id. */
11703 if (dname && IDENTIFIER_ANY_OP_P (dname))
11705 error ("declaration of %qD as non-function", dname);
11706 return error_mark_node;
11709 if (reqs)
11710 error_at (location_of (reqs),
11711 "requires-clause on declaration of non-function type %qT",
11712 type);
11715 /* We don't check parameter types here because we can emit a better
11716 error message later. */
11717 if (decl_context != PARM)
11719 type = check_var_type (unqualified_id, type);
11720 if (type == error_mark_node)
11721 return error_mark_node;
11724 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
11725 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
11727 if (decl_context == PARM || decl_context == CATCHPARM)
11729 if (ctype || in_namespace)
11730 error ("cannot use %<::%> in parameter declaration");
11732 if (type_uses_auto (type)
11733 && !(cxx_dialect >= cxx17 && template_parm_flag))
11735 if (cxx_dialect >= cxx14)
11736 error ("%<auto%> parameter not permitted in this context");
11737 else
11738 error ("parameter declared %<auto%>");
11739 type = error_mark_node;
11742 /* A parameter declared as an array of T is really a pointer to T.
11743 One declared as a function is really a pointer to a function.
11744 One declared as a member is really a pointer to member. */
11746 if (TREE_CODE (type) == ARRAY_TYPE)
11748 /* Transfer const-ness of array into that of type pointed to. */
11749 type = build_pointer_type (TREE_TYPE (type));
11750 type_quals = TYPE_UNQUALIFIED;
11751 array_parameter_p = true;
11753 else if (TREE_CODE (type) == FUNCTION_TYPE)
11754 type = build_pointer_type (type);
11757 if (ctype && TREE_CODE (type) == FUNCTION_TYPE && staticp < 2
11758 && !(identifier_p (unqualified_id)
11759 && IDENTIFIER_NEWDEL_OP_P (unqualified_id)))
11761 cp_cv_quals real_quals = memfn_quals;
11762 if (cxx_dialect < cxx14 && constexpr_p
11763 && sfk != sfk_constructor && sfk != sfk_destructor)
11764 real_quals |= TYPE_QUAL_CONST;
11765 type = build_memfn_type (type, ctype, real_quals, rqual);
11769 tree decl = NULL_TREE;
11771 if (decl_context == PARM)
11773 decl = cp_build_parm_decl (NULL_TREE, unqualified_id, type);
11774 DECL_ARRAY_PARAMETER_P (decl) = array_parameter_p;
11776 bad_specifiers (decl, BSP_PARM, virtualp,
11777 memfn_quals != TYPE_UNQUALIFIED,
11778 inlinep, friendp, raises != NULL_TREE);
11780 else if (decl_context == FIELD)
11782 if (!staticp && !friendp && TREE_CODE (type) != METHOD_TYPE)
11783 if (tree auto_node = type_uses_auto (type))
11785 location_t loc = declspecs->locations[ds_type_spec];
11786 if (CLASS_PLACEHOLDER_TEMPLATE (auto_node))
11787 error_at (loc, "invalid use of template-name %qE without an "
11788 "argument list",
11789 CLASS_PLACEHOLDER_TEMPLATE (auto_node));
11790 else
11791 error_at (loc, "non-static data member declared with "
11792 "placeholder %qT", auto_node);
11793 type = error_mark_node;
11796 /* The C99 flexible array extension. */
11797 if (!staticp && TREE_CODE (type) == ARRAY_TYPE
11798 && TYPE_DOMAIN (type) == NULL_TREE)
11800 if (ctype
11801 && (TREE_CODE (ctype) == UNION_TYPE
11802 || TREE_CODE (ctype) == QUAL_UNION_TYPE))
11804 error ("flexible array member in union");
11805 type = error_mark_node;
11807 else
11809 /* Array is a flexible member. */
11810 if (in_system_header_at (input_location))
11811 /* Do not warn on flexible array members in system
11812 headers because glibc uses them. */;
11813 else if (name)
11814 pedwarn (input_location, OPT_Wpedantic,
11815 "ISO C++ forbids flexible array member %qs", name);
11816 else
11817 pedwarn (input_location, OPT_Wpedantic,
11818 "ISO C++ forbids flexible array members");
11820 /* Flexible array member has a null domain. */
11821 type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
11825 if (type == error_mark_node)
11827 /* Happens when declaring arrays of sizes which
11828 are error_mark_node, for example. */
11829 decl = NULL_TREE;
11831 else if (in_namespace && !friendp)
11833 /* Something like struct S { int N::j; }; */
11834 error ("invalid use of %<::%>");
11835 return error_mark_node;
11837 else if (TREE_CODE (type) == FUNCTION_TYPE
11838 || TREE_CODE (type) == METHOD_TYPE)
11840 int publicp = 0;
11841 tree function_context;
11843 if (friendp == 0)
11845 /* This should never happen in pure C++ (the check
11846 could be an assert). It could happen in
11847 Objective-C++ if someone writes invalid code that
11848 uses a function declaration for an instance
11849 variable or property (instance variables and
11850 properties are parsed as FIELD_DECLs, but they are
11851 part of an Objective-C class, not a C++ class).
11852 That code is invalid and is caught by this
11853 check. */
11854 if (!ctype)
11856 error ("declaration of function %qD in invalid context",
11857 unqualified_id);
11858 return error_mark_node;
11861 /* ``A union may [ ... ] not [ have ] virtual functions.''
11862 ARM 9.5 */
11863 if (virtualp && TREE_CODE (ctype) == UNION_TYPE)
11865 error ("function %qD declared %<virtual%> inside a union",
11866 unqualified_id);
11867 return error_mark_node;
11870 if (virtualp
11871 && identifier_p (unqualified_id)
11872 && IDENTIFIER_NEWDEL_OP_P (unqualified_id))
11874 error ("%qD cannot be declared %<virtual%>, since it "
11875 "is always static", unqualified_id);
11876 virtualp = 0;
11880 /* Check that the name used for a destructor makes sense. */
11881 if (sfk == sfk_destructor)
11883 tree uqname = id_declarator->u.id.unqualified_name;
11885 if (!ctype)
11887 gcc_assert (friendp);
11888 error ("expected qualified name in friend declaration "
11889 "for destructor %qD", uqname);
11890 return error_mark_node;
11893 if (!check_dtor_name (ctype, TREE_OPERAND (uqname, 0)))
11895 error ("declaration of %qD as member of %qT",
11896 uqname, ctype);
11897 return error_mark_node;
11899 if (concept_p)
11901 error ("a destructor cannot be %<concept%>");
11902 return error_mark_node;
11904 if (constexpr_p)
11906 error ("a destructor cannot be %<constexpr%>");
11907 return error_mark_node;
11910 else if (sfk == sfk_constructor && friendp && !ctype)
11912 error ("expected qualified name in friend declaration "
11913 "for constructor %qD",
11914 id_declarator->u.id.unqualified_name);
11915 return error_mark_node;
11917 if (sfk == sfk_constructor)
11918 if (concept_p)
11920 error ("a constructor cannot be %<concept%>");
11921 return error_mark_node;
11923 if (concept_p)
11925 error ("a concept cannot be a member function");
11926 concept_p = false;
11929 if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
11931 tree tmpl = TREE_OPERAND (unqualified_id, 0);
11932 if (variable_template_p (tmpl))
11934 error ("specialization of variable template %qD "
11935 "declared as function", tmpl);
11936 inform (DECL_SOURCE_LOCATION (tmpl),
11937 "variable template declared here");
11938 return error_mark_node;
11942 /* Tell grokfndecl if it needs to set TREE_PUBLIC on the node. */
11943 function_context = (ctype != NULL_TREE) ?
11944 decl_function_context (TYPE_MAIN_DECL (ctype)) : NULL_TREE;
11945 publicp = (! friendp || ! staticp)
11946 && function_context == NULL_TREE;
11948 if (late_return_type_p)
11949 TYPE_HAS_LATE_RETURN_TYPE (type) = 1;
11951 decl = grokfndecl (ctype, type,
11952 TREE_CODE (unqualified_id) != TEMPLATE_ID_EXPR
11953 ? unqualified_id : dname,
11954 parms,
11955 unqualified_id,
11956 reqs,
11957 virtualp, flags, memfn_quals, rqual, raises,
11958 friendp ? -1 : 0, friendp, publicp,
11959 inlinep | (2 * constexpr_p) | (4 * concept_p),
11960 initialized == SD_DELETED, sfk,
11961 funcdef_flag, template_count, in_namespace,
11962 attrlist, declarator->id_loc);
11963 decl = set_virt_specifiers (decl, virt_specifiers);
11964 if (decl == NULL_TREE)
11965 return error_mark_node;
11966 #if 0
11967 /* This clobbers the attrs stored in `decl' from `attrlist'. */
11968 /* The decl and setting of decl_attr is also turned off. */
11969 decl = build_decl_attribute_variant (decl, decl_attr);
11970 #endif
11972 /* [class.conv.ctor]
11974 A constructor declared without the function-specifier
11975 explicit that can be called with a single parameter
11976 specifies a conversion from the type of its first
11977 parameter to the type of its class. Such a constructor
11978 is called a converting constructor. */
11979 if (explicitp == 2)
11980 DECL_NONCONVERTING_P (decl) = 1;
11982 else if (!staticp && !dependent_type_p (type)
11983 && !COMPLETE_TYPE_P (complete_type (type))
11984 && (!complete_or_array_type_p (type)
11985 || initialized == 0))
11987 if (TREE_CODE (type) != ARRAY_TYPE
11988 || !COMPLETE_TYPE_P (TREE_TYPE (type)))
11990 if (unqualified_id)
11992 error ("field %qD has incomplete type %qT",
11993 unqualified_id, type);
11994 cxx_incomplete_type_inform (strip_array_types (type));
11996 else
11997 error ("name %qT has incomplete type", type);
11999 type = error_mark_node;
12000 decl = NULL_TREE;
12003 else
12005 if (friendp)
12007 error ("%qE is neither function nor member function; "
12008 "cannot be declared friend", unqualified_id);
12009 friendp = 0;
12011 decl = NULL_TREE;
12014 if (friendp)
12016 /* Friends are treated specially. */
12017 if (ctype == current_class_type)
12018 ; /* We already issued a permerror. */
12019 else if (decl && DECL_NAME (decl))
12021 if (template_class_depth (current_class_type) == 0)
12023 decl = check_explicit_specialization
12024 (unqualified_id, decl, template_count,
12025 2 * funcdef_flag + 4);
12026 if (decl == error_mark_node)
12027 return error_mark_node;
12030 decl = do_friend (ctype, unqualified_id, decl,
12031 *attrlist, flags,
12032 funcdef_flag);
12033 return decl;
12035 else
12036 return error_mark_node;
12039 /* Structure field. It may not be a function, except for C++. */
12041 if (decl == NULL_TREE)
12043 if (staticp)
12045 /* C++ allows static class members. All other work
12046 for this is done by grokfield. */
12047 decl = build_lang_decl_loc (declarator
12048 ? declarator->id_loc
12049 : input_location,
12050 VAR_DECL, unqualified_id, type);
12051 set_linkage_for_static_data_member (decl);
12052 if (concept_p)
12053 error ("static data member %qE declared %<concept%>",
12054 unqualified_id);
12055 else if (constexpr_p && !initialized)
12057 error ("constexpr static data member %qD must have an "
12058 "initializer", decl);
12059 constexpr_p = false;
12062 if (inlinep)
12063 mark_inline_variable (decl);
12065 if (!DECL_VAR_DECLARED_INLINE_P (decl)
12066 && !(cxx_dialect >= cxx17 && constexpr_p))
12067 /* Even if there is an in-class initialization, DECL
12068 is considered undefined until an out-of-class
12069 definition is provided, unless this is an inline
12070 variable. */
12071 DECL_EXTERNAL (decl) = 1;
12073 if (thread_p)
12075 CP_DECL_THREAD_LOCAL_P (decl) = true;
12076 if (!processing_template_decl)
12077 set_decl_tls_model (decl, decl_default_tls_model (decl));
12078 if (declspecs->gnu_thread_keyword_p)
12079 SET_DECL_GNU_TLS_P (decl);
12082 else
12084 if (concept_p)
12085 error ("non-static data member %qE declared %<concept%>",
12086 unqualified_id);
12087 else if (constexpr_p)
12089 error ("non-static data member %qE declared %<constexpr%>",
12090 unqualified_id);
12091 constexpr_p = false;
12093 decl = build_decl (input_location,
12094 FIELD_DECL, unqualified_id, type);
12095 DECL_NONADDRESSABLE_P (decl) = bitfield;
12096 if (bitfield && !unqualified_id)
12097 TREE_NO_WARNING (decl) = 1;
12099 if (storage_class == sc_mutable)
12101 DECL_MUTABLE_P (decl) = 1;
12102 storage_class = sc_none;
12105 if (initialized)
12107 /* An attempt is being made to initialize a non-static
12108 member. This is new in C++11. */
12109 maybe_warn_cpp0x (CPP0X_NSDMI);
12111 /* If this has been parsed with static storage class, but
12112 errors forced staticp to be cleared, ensure NSDMI is
12113 not present. */
12114 if (declspecs->storage_class == sc_static)
12115 DECL_INITIAL (decl) = error_mark_node;
12119 bad_specifiers (decl, BSP_FIELD, virtualp,
12120 memfn_quals != TYPE_UNQUALIFIED,
12121 staticp ? false : inlinep, friendp,
12122 raises != NULL_TREE);
12125 else if (TREE_CODE (type) == FUNCTION_TYPE
12126 || TREE_CODE (type) == METHOD_TYPE)
12128 tree original_name;
12129 int publicp = 0;
12131 if (!unqualified_id)
12132 return error_mark_node;
12134 if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
12135 original_name = dname;
12136 else
12137 original_name = unqualified_id;
12138 // FIXME:gcc_assert (original_name == dname);
12140 if (storage_class == sc_auto)
12141 error ("storage class %<auto%> invalid for function %qs", name);
12142 else if (storage_class == sc_register)
12143 error ("storage class %<register%> invalid for function %qs", name);
12144 else if (thread_p)
12146 if (declspecs->gnu_thread_keyword_p)
12147 error ("storage class %<__thread%> invalid for function %qs",
12148 name);
12149 else
12150 error ("storage class %<thread_local%> invalid for function %qs",
12151 name);
12154 if (virt_specifiers)
12155 error ("virt-specifiers in %qs not allowed outside a class definition", name);
12156 /* Function declaration not at top level.
12157 Storage classes other than `extern' are not allowed
12158 and `extern' makes no difference. */
12159 if (! toplevel_bindings_p ()
12160 && (storage_class == sc_static
12161 || decl_spec_seq_has_spec_p (declspecs, ds_inline))
12162 && pedantic)
12164 if (storage_class == sc_static)
12165 pedwarn (input_location, OPT_Wpedantic,
12166 "%<static%> specifier invalid for function %qs "
12167 "declared out of global scope", name);
12168 else
12169 pedwarn (input_location, OPT_Wpedantic,
12170 "%<inline%> specifier invalid for function %qs "
12171 "declared out of global scope", name);
12174 if (ctype == NULL_TREE)
12176 if (virtualp)
12178 error ("virtual non-class function %qs", name);
12179 virtualp = 0;
12181 else if (sfk == sfk_constructor
12182 || sfk == sfk_destructor)
12184 error (funcdef_flag
12185 ? G_("%qs defined in a non-class scope")
12186 : G_("%qs declared in a non-class scope"), name);
12187 sfk = sfk_none;
12191 /* Record whether the function is public. */
12192 publicp = (ctype != NULL_TREE
12193 || storage_class != sc_static);
12195 if (late_return_type_p)
12196 TYPE_HAS_LATE_RETURN_TYPE (type) = 1;
12198 decl = grokfndecl (ctype, type, original_name, parms, unqualified_id,
12199 reqs, virtualp, flags, memfn_quals, rqual, raises,
12200 1, friendp,
12201 publicp,
12202 inlinep | (2 * constexpr_p) | (4 * concept_p),
12203 initialized == SD_DELETED,
12204 sfk,
12205 funcdef_flag,
12206 template_count, in_namespace, attrlist,
12207 declarator->id_loc);
12208 if (decl == NULL_TREE)
12209 return error_mark_node;
12211 if (explicitp == 2)
12212 DECL_NONCONVERTING_P (decl) = 1;
12213 if (staticp == 1)
12215 int invalid_static = 0;
12217 /* Don't allow a static member function in a class, and forbid
12218 declaring main to be static. */
12219 if (TREE_CODE (type) == METHOD_TYPE)
12221 permerror (input_location, "cannot declare member function %qD to have "
12222 "static linkage", decl);
12223 invalid_static = 1;
12225 else if (current_function_decl)
12227 /* 7.1.1: There can be no static function declarations within a
12228 block. */
12229 error ("cannot declare static function inside another function");
12230 invalid_static = 1;
12233 if (invalid_static)
12235 staticp = 0;
12236 storage_class = sc_none;
12240 else
12242 /* It's a variable. */
12244 /* An uninitialized decl with `extern' is a reference. */
12245 decl = grokvardecl (type, dname, unqualified_id,
12246 declspecs,
12247 initialized,
12248 type_quals,
12249 inlinep,
12250 concept_p,
12251 template_count,
12252 ctype ? ctype : in_namespace);
12253 if (decl == NULL_TREE)
12254 return error_mark_node;
12256 bad_specifiers (decl, BSP_VAR, virtualp,
12257 memfn_quals != TYPE_UNQUALIFIED,
12258 inlinep, friendp, raises != NULL_TREE);
12260 if (ctype)
12262 DECL_CONTEXT (decl) = ctype;
12263 if (staticp == 1)
12265 permerror (input_location, "%<static%> may not be used when defining "
12266 "(as opposed to declaring) a static data member");
12267 staticp = 0;
12268 storage_class = sc_none;
12270 if (storage_class == sc_register && TREE_STATIC (decl))
12272 error ("static member %qD declared %<register%>", decl);
12273 storage_class = sc_none;
12275 if (storage_class == sc_extern && pedantic)
12277 pedwarn (input_location, OPT_Wpedantic,
12278 "cannot explicitly declare member %q#D to have "
12279 "extern linkage", decl);
12280 storage_class = sc_none;
12283 else if (constexpr_p && DECL_EXTERNAL (decl))
12285 error ("declaration of constexpr variable %qD is not a definition",
12286 decl);
12287 constexpr_p = false;
12290 if (inlinep)
12291 mark_inline_variable (decl);
12292 if (innermost_code == cdk_decomp)
12294 gcc_assert (declarator && declarator->kind == cdk_decomp);
12295 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
12296 DECL_ARTIFICIAL (decl) = 1;
12297 fit_decomposition_lang_decl (decl, NULL_TREE);
12301 if (VAR_P (decl) && !initialized)
12302 if (tree auto_node = type_uses_auto (type))
12303 if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node))
12305 location_t loc = declspecs->locations[ds_type_spec];
12306 error_at (loc, "declaration of %q#D has no initializer", decl);
12307 TREE_TYPE (decl) = error_mark_node;
12310 if (storage_class == sc_extern && initialized && !funcdef_flag)
12312 if (toplevel_bindings_p ())
12314 /* It's common practice (and completely valid) to have a const
12315 be initialized and declared extern. */
12316 if (!(type_quals & TYPE_QUAL_CONST))
12317 warning (0, "%qs initialized and declared %<extern%>", name);
12319 else
12321 error ("%qs has both %<extern%> and initializer", name);
12322 return error_mark_node;
12326 /* Record `register' declaration for warnings on &
12327 and in case doing stupid register allocation. */
12329 if (storage_class == sc_register)
12331 DECL_REGISTER (decl) = 1;
12332 /* Warn about register storage specifiers on PARM_DECLs. */
12333 if (TREE_CODE (decl) == PARM_DECL)
12335 if (cxx_dialect >= cxx17)
12336 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
12337 "ISO C++17 does not allow %<register%> storage "
12338 "class specifier");
12339 else
12340 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
12341 "%<register%> storage class specifier used");
12344 else if (storage_class == sc_extern)
12345 DECL_THIS_EXTERN (decl) = 1;
12346 else if (storage_class == sc_static)
12347 DECL_THIS_STATIC (decl) = 1;
12349 /* Set constexpr flag on vars (functions got it in grokfndecl). */
12350 if (constexpr_p && VAR_P (decl))
12351 DECL_DECLARED_CONSTEXPR_P (decl) = true;
12353 /* Record constancy and volatility on the DECL itself . There's
12354 no need to do this when processing a template; we'll do this
12355 for the instantiated declaration based on the type of DECL. */
12356 if (!processing_template_decl)
12357 cp_apply_type_quals_to_decl (type_quals, decl);
12359 return decl;
12363 /* Subroutine of start_function. Ensure that each of the parameter
12364 types (as listed in PARMS) is complete, as is required for a
12365 function definition. */
12367 static void
12368 require_complete_types_for_parms (tree parms)
12370 for (; parms; parms = DECL_CHAIN (parms))
12372 if (dependent_type_p (TREE_TYPE (parms)))
12373 continue;
12374 if (!VOID_TYPE_P (TREE_TYPE (parms))
12375 && complete_type_or_else (TREE_TYPE (parms), parms))
12377 relayout_decl (parms);
12378 DECL_ARG_TYPE (parms) = type_passed_as (TREE_TYPE (parms));
12380 maybe_warn_parm_abi (TREE_TYPE (parms),
12381 DECL_SOURCE_LOCATION (parms));
12383 else
12384 /* grokparms or complete_type_or_else will have already issued
12385 an error. */
12386 TREE_TYPE (parms) = error_mark_node;
12390 /* Returns nonzero if T is a local variable. */
12393 local_variable_p (const_tree t)
12395 if ((VAR_P (t)
12396 /* A VAR_DECL with a context that is a _TYPE is a static data
12397 member. */
12398 && !TYPE_P (CP_DECL_CONTEXT (t))
12399 /* Any other non-local variable must be at namespace scope. */
12400 && !DECL_NAMESPACE_SCOPE_P (t))
12401 || (TREE_CODE (t) == PARM_DECL))
12402 return 1;
12404 return 0;
12407 /* Like local_variable_p, but suitable for use as a tree-walking
12408 function. */
12410 static tree
12411 local_variable_p_walkfn (tree *tp, int *walk_subtrees,
12412 void * /*data*/)
12414 if (local_variable_p (*tp)
12415 && (!DECL_ARTIFICIAL (*tp) || DECL_NAME (*tp) == this_identifier))
12416 return *tp;
12417 else if (TYPE_P (*tp))
12418 *walk_subtrees = 0;
12420 return NULL_TREE;
12423 /* Check that ARG, which is a default-argument expression for a
12424 parameter DECL, is valid. Returns ARG, or ERROR_MARK_NODE, if
12425 something goes wrong. DECL may also be a _TYPE node, rather than a
12426 DECL, if there is no DECL available. */
12428 tree
12429 check_default_argument (tree decl, tree arg, tsubst_flags_t complain)
12431 tree var;
12432 tree decl_type;
12434 if (TREE_CODE (arg) == DEFAULT_ARG)
12435 /* We get a DEFAULT_ARG when looking at an in-class declaration
12436 with a default argument. Ignore the argument for now; we'll
12437 deal with it after the class is complete. */
12438 return arg;
12440 if (TYPE_P (decl))
12442 decl_type = decl;
12443 decl = NULL_TREE;
12445 else
12446 decl_type = TREE_TYPE (decl);
12448 if (arg == error_mark_node
12449 || decl == error_mark_node
12450 || TREE_TYPE (arg) == error_mark_node
12451 || decl_type == error_mark_node)
12452 /* Something already went wrong. There's no need to check
12453 further. */
12454 return error_mark_node;
12456 /* [dcl.fct.default]
12458 A default argument expression is implicitly converted to the
12459 parameter type. */
12460 ++cp_unevaluated_operand;
12461 perform_implicit_conversion_flags (decl_type, arg, complain,
12462 LOOKUP_IMPLICIT);
12463 --cp_unevaluated_operand;
12465 /* Avoid redundant -Wzero-as-null-pointer-constant warnings at
12466 the call sites. */
12467 if (TYPE_PTR_OR_PTRMEM_P (decl_type)
12468 && null_ptr_cst_p (arg))
12469 return nullptr_node;
12471 /* [dcl.fct.default]
12473 Local variables shall not be used in default argument
12474 expressions.
12476 The keyword `this' shall not be used in a default argument of a
12477 member function. */
12478 var = cp_walk_tree_without_duplicates (&arg, local_variable_p_walkfn, NULL);
12479 if (var)
12481 if (complain & tf_warning_or_error)
12483 if (DECL_NAME (var) == this_identifier)
12484 permerror (input_location, "default argument %qE uses %qD",
12485 arg, var);
12486 else
12487 error ("default argument %qE uses local variable %qD", arg, var);
12489 return error_mark_node;
12492 /* All is well. */
12493 return arg;
12496 /* Returns a deprecated type used within TYPE, or NULL_TREE if none. */
12498 static tree
12499 type_is_deprecated (tree type)
12501 enum tree_code code;
12502 if (TREE_DEPRECATED (type))
12503 return type;
12504 if (TYPE_NAME (type))
12506 if (TREE_DEPRECATED (TYPE_NAME (type)))
12507 return type;
12508 else
12509 return NULL_TREE;
12512 /* Do warn about using typedefs to a deprecated class. */
12513 if (OVERLOAD_TYPE_P (type) && type != TYPE_MAIN_VARIANT (type))
12514 return type_is_deprecated (TYPE_MAIN_VARIANT (type));
12516 code = TREE_CODE (type);
12518 if (code == POINTER_TYPE || code == REFERENCE_TYPE
12519 || code == OFFSET_TYPE || code == FUNCTION_TYPE
12520 || code == METHOD_TYPE || code == ARRAY_TYPE)
12521 return type_is_deprecated (TREE_TYPE (type));
12523 if (TYPE_PTRMEMFUNC_P (type))
12524 return type_is_deprecated
12525 (TREE_TYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type))));
12527 return NULL_TREE;
12530 /* Decode the list of parameter types for a function type.
12531 Given the list of things declared inside the parens,
12532 return a list of types.
12534 If this parameter does not end with an ellipsis, we append
12535 void_list_node.
12537 *PARMS is set to the chain of PARM_DECLs created. */
12539 tree
12540 grokparms (tree parmlist, tree *parms)
12542 tree result = NULL_TREE;
12543 tree decls = NULL_TREE;
12544 tree parm;
12545 int any_error = 0;
12547 for (parm = parmlist; parm != NULL_TREE; parm = TREE_CHAIN (parm))
12549 tree type = NULL_TREE;
12550 tree init = TREE_PURPOSE (parm);
12551 tree decl = TREE_VALUE (parm);
12553 if (parm == void_list_node)
12554 break;
12556 if (! decl || TREE_TYPE (decl) == error_mark_node)
12557 continue;
12559 type = TREE_TYPE (decl);
12560 if (VOID_TYPE_P (type))
12562 if (same_type_p (type, void_type_node)
12563 && !init
12564 && !DECL_NAME (decl) && !result
12565 && TREE_CHAIN (parm) == void_list_node)
12566 /* DR 577: A parameter list consisting of a single
12567 unnamed parameter of non-dependent type 'void'. */
12568 break;
12569 else if (cv_qualified_p (type))
12570 error_at (DECL_SOURCE_LOCATION (decl),
12571 "invalid use of cv-qualified type %qT in "
12572 "parameter declaration", type);
12573 else
12574 error_at (DECL_SOURCE_LOCATION (decl),
12575 "invalid use of type %<void%> in parameter "
12576 "declaration");
12577 /* It's not a good idea to actually create parameters of
12578 type `void'; other parts of the compiler assume that a
12579 void type terminates the parameter list. */
12580 type = error_mark_node;
12581 TREE_TYPE (decl) = error_mark_node;
12584 if (type != error_mark_node)
12586 if (deprecated_state != DEPRECATED_SUPPRESS)
12588 tree deptype = type_is_deprecated (type);
12589 if (deptype)
12590 warn_deprecated_use (deptype, NULL_TREE);
12593 /* Top-level qualifiers on the parameters are
12594 ignored for function types. */
12595 type = cp_build_qualified_type (type, 0);
12596 if (TREE_CODE (type) == METHOD_TYPE)
12598 error ("parameter %qD invalidly declared method type", decl);
12599 type = build_pointer_type (type);
12600 TREE_TYPE (decl) = type;
12602 else if (abstract_virtuals_error (decl, type))
12603 any_error = 1; /* Seems like a good idea. */
12604 else if (cxx_dialect < cxx17 && POINTER_TYPE_P (type))
12606 /* Before C++17 DR 393:
12607 [dcl.fct]/6, parameter types cannot contain pointers
12608 (references) to arrays of unknown bound. */
12609 tree t = TREE_TYPE (type);
12610 int ptr = TYPE_PTR_P (type);
12612 while (1)
12614 if (TYPE_PTR_P (t))
12615 ptr = 1;
12616 else if (TREE_CODE (t) != ARRAY_TYPE)
12617 break;
12618 else if (!TYPE_DOMAIN (t))
12619 break;
12620 t = TREE_TYPE (t);
12622 if (TREE_CODE (t) == ARRAY_TYPE)
12623 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
12625 ? G_("parameter %qD includes pointer to array of "
12626 "unknown bound %qT")
12627 : G_("parameter %qD includes reference to array of "
12628 "unknown bound %qT"),
12629 decl, t);
12632 if (any_error)
12633 init = NULL_TREE;
12634 else if (init && !processing_template_decl)
12635 init = check_default_argument (decl, init, tf_warning_or_error);
12638 DECL_CHAIN (decl) = decls;
12639 decls = decl;
12640 result = tree_cons (init, type, result);
12642 decls = nreverse (decls);
12643 result = nreverse (result);
12644 if (parm)
12645 result = chainon (result, void_list_node);
12646 *parms = decls;
12648 return result;
12652 /* D is a constructor or overloaded `operator='.
12654 Let T be the class in which D is declared. Then, this function
12655 returns:
12657 -1 if D's is an ill-formed constructor or copy assignment operator
12658 whose first parameter is of type `T'.
12659 0 if D is not a copy constructor or copy assignment
12660 operator.
12661 1 if D is a copy constructor or copy assignment operator whose
12662 first parameter is a reference to non-const qualified T.
12663 2 if D is a copy constructor or copy assignment operator whose
12664 first parameter is a reference to const qualified T.
12666 This function can be used as a predicate. Positive values indicate
12667 a copy constructor and nonzero values indicate a copy assignment
12668 operator. */
12671 copy_fn_p (const_tree d)
12673 tree args;
12674 tree arg_type;
12675 int result = 1;
12677 gcc_assert (DECL_FUNCTION_MEMBER_P (d));
12679 if (TREE_CODE (d) == TEMPLATE_DECL
12680 || (DECL_TEMPLATE_INFO (d)
12681 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d))))
12682 /* Instantiations of template member functions are never copy
12683 functions. Note that member functions of templated classes are
12684 represented as template functions internally, and we must
12685 accept those as copy functions. */
12686 return 0;
12688 args = FUNCTION_FIRST_USER_PARMTYPE (d);
12689 if (!args)
12690 return 0;
12692 arg_type = TREE_VALUE (args);
12693 if (arg_type == error_mark_node)
12694 return 0;
12696 if (TYPE_MAIN_VARIANT (arg_type) == DECL_CONTEXT (d))
12698 /* Pass by value copy assignment operator. */
12699 result = -1;
12701 else if (TREE_CODE (arg_type) == REFERENCE_TYPE
12702 && !TYPE_REF_IS_RVALUE (arg_type)
12703 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)) == DECL_CONTEXT (d))
12705 if (CP_TYPE_CONST_P (TREE_TYPE (arg_type)))
12706 result = 2;
12708 else
12709 return 0;
12711 args = TREE_CHAIN (args);
12713 if (args && args != void_list_node && !TREE_PURPOSE (args))
12714 /* There are more non-optional args. */
12715 return 0;
12717 return result;
12720 /* D is a constructor or overloaded `operator='.
12722 Let T be the class in which D is declared. Then, this function
12723 returns true when D is a move constructor or move assignment
12724 operator, false otherwise. */
12726 bool
12727 move_fn_p (const_tree d)
12729 gcc_assert (DECL_FUNCTION_MEMBER_P (d));
12731 if (cxx_dialect == cxx98)
12732 /* There are no move constructors if we are in C++98 mode. */
12733 return false;
12735 if (TREE_CODE (d) == TEMPLATE_DECL
12736 || (DECL_TEMPLATE_INFO (d)
12737 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d))))
12738 /* Instantiations of template member functions are never move
12739 functions. Note that member functions of templated classes are
12740 represented as template functions internally, and we must
12741 accept those as move functions. */
12742 return 0;
12744 return move_signature_fn_p (d);
12747 /* D is a constructor or overloaded `operator='.
12749 Then, this function returns true when D has the same signature as a move
12750 constructor or move assignment operator (because either it is such a
12751 ctor/op= or it is a template specialization with the same signature),
12752 false otherwise. */
12754 bool
12755 move_signature_fn_p (const_tree d)
12757 tree args;
12758 tree arg_type;
12759 bool result = false;
12761 args = FUNCTION_FIRST_USER_PARMTYPE (d);
12762 if (!args)
12763 return 0;
12765 arg_type = TREE_VALUE (args);
12766 if (arg_type == error_mark_node)
12767 return 0;
12769 if (TREE_CODE (arg_type) == REFERENCE_TYPE
12770 && TYPE_REF_IS_RVALUE (arg_type)
12771 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)),
12772 DECL_CONTEXT (d)))
12773 result = true;
12775 args = TREE_CHAIN (args);
12777 if (args && args != void_list_node && !TREE_PURPOSE (args))
12778 /* There are more non-optional args. */
12779 return false;
12781 return result;
12784 /* Remember any special properties of member function DECL. */
12786 void
12787 grok_special_member_properties (tree decl)
12789 tree class_type;
12791 if (!DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
12792 return;
12794 class_type = DECL_CONTEXT (decl);
12795 if (IDENTIFIER_CTOR_P (DECL_NAME (decl)))
12797 int ctor = copy_fn_p (decl);
12799 if (!DECL_ARTIFICIAL (decl))
12800 TYPE_HAS_USER_CONSTRUCTOR (class_type) = 1;
12802 if (ctor > 0)
12804 /* [class.copy]
12806 A non-template constructor for class X is a copy
12807 constructor if its first parameter is of type X&, const
12808 X&, volatile X& or const volatile X&, and either there
12809 are no other parameters or else all other parameters have
12810 default arguments. */
12811 TYPE_HAS_COPY_CTOR (class_type) = 1;
12812 if (user_provided_p (decl))
12813 TYPE_HAS_COMPLEX_COPY_CTOR (class_type) = 1;
12814 if (ctor > 1)
12815 TYPE_HAS_CONST_COPY_CTOR (class_type) = 1;
12817 else if (sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (decl)))
12818 TYPE_HAS_DEFAULT_CONSTRUCTOR (class_type) = 1;
12819 else if (move_fn_p (decl) && user_provided_p (decl))
12820 TYPE_HAS_COMPLEX_MOVE_CTOR (class_type) = 1;
12821 else if (is_list_ctor (decl))
12822 TYPE_HAS_LIST_CTOR (class_type) = 1;
12824 if (DECL_DECLARED_CONSTEXPR_P (decl)
12825 && !ctor && !move_fn_p (decl))
12826 TYPE_HAS_CONSTEXPR_CTOR (class_type) = 1;
12828 else if (DECL_NAME (decl) == cp_assignment_operator_id (NOP_EXPR))
12830 /* [class.copy]
12832 A non-template assignment operator for class X is a copy
12833 assignment operator if its parameter is of type X, X&, const
12834 X&, volatile X& or const volatile X&. */
12836 int assop = copy_fn_p (decl);
12838 if (assop)
12840 TYPE_HAS_COPY_ASSIGN (class_type) = 1;
12841 if (user_provided_p (decl))
12842 TYPE_HAS_COMPLEX_COPY_ASSIGN (class_type) = 1;
12843 if (assop != 1)
12844 TYPE_HAS_CONST_COPY_ASSIGN (class_type) = 1;
12846 else if (move_fn_p (decl) && user_provided_p (decl))
12847 TYPE_HAS_COMPLEX_MOVE_ASSIGN (class_type) = 1;
12849 else if (IDENTIFIER_CONV_OP_P (DECL_NAME (decl)))
12850 TYPE_HAS_CONVERSION (class_type) = true;
12852 /* Destructors are handled in check_methods. */
12855 /* Check a constructor DECL has the correct form. Complains
12856 if the class has a constructor of the form X(X). */
12858 bool
12859 grok_ctor_properties (const_tree ctype, const_tree decl)
12861 int ctor_parm = copy_fn_p (decl);
12863 if (ctor_parm < 0)
12865 /* [class.copy]
12867 A declaration of a constructor for a class X is ill-formed if
12868 its first parameter is of type (optionally cv-qualified) X
12869 and either there are no other parameters or else all other
12870 parameters have default arguments.
12872 We *don't* complain about member template instantiations that
12873 have this form, though; they can occur as we try to decide
12874 what constructor to use during overload resolution. Since
12875 overload resolution will never prefer such a constructor to
12876 the non-template copy constructor (which is either explicitly
12877 or implicitly defined), there's no need to worry about their
12878 existence. Theoretically, they should never even be
12879 instantiated, but that's hard to forestall. */
12880 error ("invalid constructor; you probably meant %<%T (const %T&)%>",
12881 ctype, ctype);
12882 return false;
12885 return true;
12888 /* An operator with this code is unary, but can also be binary. */
12890 static bool
12891 ambi_op_p (enum tree_code code)
12893 return (code == INDIRECT_REF
12894 || code == ADDR_EXPR
12895 || code == UNARY_PLUS_EXPR
12896 || code == NEGATE_EXPR
12897 || code == PREINCREMENT_EXPR
12898 || code == PREDECREMENT_EXPR);
12901 /* An operator with this name can only be unary. */
12903 static bool
12904 unary_op_p (enum tree_code code)
12906 return (code == TRUTH_NOT_EXPR
12907 || code == BIT_NOT_EXPR
12908 || code == COMPONENT_REF
12909 || code == TYPE_EXPR);
12912 /* DECL is a declaration for an overloaded operator. If COMPLAIN is true,
12913 errors are issued for invalid declarations. */
12915 bool
12916 grok_op_properties (tree decl, bool complain)
12918 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
12919 tree argtype;
12920 int methodp = (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE);
12921 tree name = DECL_NAME (decl);
12922 enum tree_code operator_code;
12923 int arity;
12924 bool ellipsis_p;
12925 tree class_type;
12927 /* Count the number of arguments and check for ellipsis. */
12928 for (argtype = argtypes, arity = 0;
12929 argtype && argtype != void_list_node;
12930 argtype = TREE_CHAIN (argtype))
12931 ++arity;
12932 ellipsis_p = !argtype;
12934 class_type = DECL_CONTEXT (decl);
12935 if (class_type && !CLASS_TYPE_P (class_type))
12936 class_type = NULL_TREE;
12938 if (IDENTIFIER_CONV_OP_P (name))
12939 operator_code = TYPE_EXPR;
12940 else
12942 /* It'd be nice to hang something else of the identifier to
12943 find CODE more directly. */
12944 bool assign_op = IDENTIFIER_ASSIGN_OP_P (name);
12945 const operator_name_info_t *oni
12946 = (assign_op ? assignment_operator_name_info : operator_name_info);
12948 if (false)
12950 #define DEF_OPERATOR(NAME, CODE, MANGLING, ARITY, KIND) \
12951 else if (assign_op == (KIND == cik_assign_op) \
12952 && oni[int (CODE)].identifier == name) \
12953 operator_code = (CODE);
12954 #include "operators.def"
12955 #undef DEF_OPERATOR
12956 else
12957 gcc_unreachable ();
12959 while (0);
12960 gcc_assert (operator_code != MAX_TREE_CODES);
12961 SET_OVERLOADED_OPERATOR_CODE (decl, operator_code);
12963 if (class_type)
12964 switch (operator_code)
12966 case NEW_EXPR:
12967 TYPE_HAS_NEW_OPERATOR (class_type) = 1;
12968 break;
12970 case DELETE_EXPR:
12971 TYPE_GETS_DELETE (class_type) |= 1;
12972 break;
12974 case VEC_NEW_EXPR:
12975 TYPE_HAS_ARRAY_NEW_OPERATOR (class_type) = 1;
12976 break;
12978 case VEC_DELETE_EXPR:
12979 TYPE_GETS_DELETE (class_type) |= 2;
12980 break;
12982 default:
12983 break;
12986 /* [basic.std.dynamic.allocation]/1:
12988 A program is ill-formed if an allocation function is declared
12989 in a namespace scope other than global scope or declared static
12990 in global scope.
12992 The same also holds true for deallocation functions. */
12993 if (operator_code == NEW_EXPR || operator_code == VEC_NEW_EXPR
12994 || operator_code == DELETE_EXPR || operator_code == VEC_DELETE_EXPR)
12996 if (DECL_NAMESPACE_SCOPE_P (decl))
12998 if (CP_DECL_CONTEXT (decl) != global_namespace)
13000 error ("%qD may not be declared within a namespace", decl);
13001 return false;
13003 else if (!TREE_PUBLIC (decl))
13005 error ("%qD may not be declared as static", decl);
13006 return false;
13011 if (operator_code == NEW_EXPR || operator_code == VEC_NEW_EXPR)
13013 TREE_TYPE (decl) = coerce_new_type (TREE_TYPE (decl));
13014 DECL_IS_OPERATOR_NEW (decl) = 1;
13016 else if (operator_code == DELETE_EXPR || operator_code == VEC_DELETE_EXPR)
13017 TREE_TYPE (decl) = coerce_delete_type (TREE_TYPE (decl));
13018 else
13020 /* An operator function must either be a non-static member function
13021 or have at least one parameter of a class, a reference to a class,
13022 an enumeration, or a reference to an enumeration. 13.4.0.6 */
13023 if (! methodp || DECL_STATIC_FUNCTION_P (decl))
13025 if (operator_code == TYPE_EXPR
13026 || operator_code == CALL_EXPR
13027 || operator_code == COMPONENT_REF
13028 || operator_code == ARRAY_REF
13029 || operator_code == NOP_EXPR)
13031 error ("%qD must be a nonstatic member function", decl);
13032 return false;
13034 else
13036 tree p;
13038 if (DECL_STATIC_FUNCTION_P (decl))
13040 error ("%qD must be either a non-static member "
13041 "function or a non-member function", decl);
13042 return false;
13045 for (p = argtypes; p && p != void_list_node; p = TREE_CHAIN (p))
13047 tree arg = non_reference (TREE_VALUE (p));
13048 if (arg == error_mark_node)
13049 return false;
13051 /* MAYBE_CLASS_TYPE_P, rather than CLASS_TYPE_P, is used
13052 because these checks are performed even on
13053 template functions. */
13054 if (MAYBE_CLASS_TYPE_P (arg)
13055 || TREE_CODE (arg) == ENUMERAL_TYPE)
13056 break;
13059 if (!p || p == void_list_node)
13061 if (complain)
13062 error ("%qD must have an argument of class or "
13063 "enumerated type", decl);
13064 return false;
13069 /* There are no restrictions on the arguments to an overloaded
13070 "operator ()". */
13071 if (operator_code == CALL_EXPR)
13072 return true;
13074 /* Warn about conversion operators that will never be used. */
13075 if (IDENTIFIER_CONV_OP_P (name)
13076 && ! DECL_TEMPLATE_INFO (decl)
13077 && warn_conversion
13078 /* Warn only declaring the function; there is no need to
13079 warn again about out-of-class definitions. */
13080 && class_type == current_class_type)
13082 tree t = TREE_TYPE (name);
13083 int ref = (TREE_CODE (t) == REFERENCE_TYPE);
13085 if (ref)
13086 t = TYPE_MAIN_VARIANT (TREE_TYPE (t));
13088 if (VOID_TYPE_P (t))
13089 warning (OPT_Wconversion,
13091 ? G_("conversion to a reference to void "
13092 "will never use a type conversion operator")
13093 : G_("conversion to void "
13094 "will never use a type conversion operator"));
13095 else if (class_type)
13097 if (t == class_type)
13098 warning (OPT_Wconversion,
13100 ? G_("conversion to a reference to the same type "
13101 "will never use a type conversion operator")
13102 : G_("conversion to the same type "
13103 "will never use a type conversion operator"));
13104 /* Don't force t to be complete here. */
13105 else if (MAYBE_CLASS_TYPE_P (t)
13106 && COMPLETE_TYPE_P (t)
13107 && DERIVED_FROM_P (t, class_type))
13108 warning (OPT_Wconversion,
13110 ? G_("conversion to a reference to a base class "
13111 "will never use a type conversion operator")
13112 : G_("conversion to a base class "
13113 "will never use a type conversion operator"));
13118 if (operator_code == COND_EXPR)
13120 /* 13.4.0.3 */
13121 error ("ISO C++ prohibits overloading operator ?:");
13122 return false;
13124 else if (ellipsis_p)
13126 error ("%qD must not have variable number of arguments", decl);
13127 return false;
13129 else if (ambi_op_p (operator_code))
13131 if (arity == 1)
13132 /* We pick the one-argument operator codes by default, so
13133 we don't have to change anything. */
13135 else if (arity == 2)
13137 /* If we thought this was a unary operator, we now know
13138 it to be a binary operator. */
13139 switch (operator_code)
13141 case INDIRECT_REF:
13142 operator_code = MULT_EXPR;
13143 break;
13145 case ADDR_EXPR:
13146 operator_code = BIT_AND_EXPR;
13147 break;
13149 case UNARY_PLUS_EXPR:
13150 operator_code = PLUS_EXPR;
13151 break;
13153 case NEGATE_EXPR:
13154 operator_code = MINUS_EXPR;
13155 break;
13157 case PREINCREMENT_EXPR:
13158 operator_code = POSTINCREMENT_EXPR;
13159 break;
13161 case PREDECREMENT_EXPR:
13162 operator_code = POSTDECREMENT_EXPR;
13163 break;
13165 default:
13166 gcc_unreachable ();
13169 SET_OVERLOADED_OPERATOR_CODE (decl, operator_code);
13171 if ((operator_code == POSTINCREMENT_EXPR
13172 || operator_code == POSTDECREMENT_EXPR)
13173 && ! processing_template_decl
13174 && ! same_type_p (TREE_VALUE (TREE_CHAIN (argtypes)), integer_type_node))
13176 if (methodp)
13177 error ("postfix %qD must take %<int%> as its argument",
13178 decl);
13179 else
13180 error ("postfix %qD must take %<int%> as its second "
13181 "argument", decl);
13182 return false;
13185 else
13187 if (methodp)
13188 error ("%qD must take either zero or one argument", decl);
13189 else
13190 error ("%qD must take either one or two arguments", decl);
13191 return false;
13194 /* More Effective C++ rule 6. */
13195 if (warn_ecpp
13196 && (operator_code == POSTINCREMENT_EXPR
13197 || operator_code == POSTDECREMENT_EXPR
13198 || operator_code == PREINCREMENT_EXPR
13199 || operator_code == PREDECREMENT_EXPR))
13201 tree arg = TREE_VALUE (argtypes);
13202 tree ret = TREE_TYPE (TREE_TYPE (decl));
13203 if (methodp || TREE_CODE (arg) == REFERENCE_TYPE)
13204 arg = TREE_TYPE (arg);
13205 arg = TYPE_MAIN_VARIANT (arg);
13206 if (operator_code == PREINCREMENT_EXPR
13207 || operator_code == PREDECREMENT_EXPR)
13209 if (TREE_CODE (ret) != REFERENCE_TYPE
13210 || !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (ret)),
13211 arg))
13212 warning (OPT_Weffc__, "prefix %qD should return %qT", decl,
13213 build_reference_type (arg));
13215 else
13217 if (!same_type_p (TYPE_MAIN_VARIANT (ret), arg))
13218 warning (OPT_Weffc__, "postfix %qD should return %qT", decl, arg);
13222 else if (unary_op_p (operator_code))
13224 if (arity != 1)
13226 if (methodp)
13227 error ("%qD must take %<void%>", decl);
13228 else
13229 error ("%qD must take exactly one argument", decl);
13230 return false;
13233 else /* if (binary_op_p (operator_code)) */
13235 if (arity != 2)
13237 if (methodp)
13238 error ("%qD must take exactly one argument", decl);
13239 else
13240 error ("%qD must take exactly two arguments", decl);
13241 return false;
13244 /* More Effective C++ rule 7. */
13245 if (warn_ecpp
13246 && (operator_code == TRUTH_ANDIF_EXPR
13247 || operator_code == TRUTH_ORIF_EXPR
13248 || operator_code == COMPOUND_EXPR))
13249 warning (OPT_Weffc__, "user-defined %qD always evaluates both arguments",
13250 decl);
13253 /* Effective C++ rule 23. */
13254 if (warn_ecpp
13255 && arity == 2
13256 && !DECL_ASSIGNMENT_OPERATOR_P (decl)
13257 && (operator_code == PLUS_EXPR
13258 || operator_code == MINUS_EXPR
13259 || operator_code == TRUNC_DIV_EXPR
13260 || operator_code == MULT_EXPR
13261 || operator_code == TRUNC_MOD_EXPR)
13262 && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == REFERENCE_TYPE)
13263 warning (OPT_Weffc__, "%qD should return by value", decl);
13265 /* [over.oper]/8 */
13266 for (; argtypes && argtypes != void_list_node;
13267 argtypes = TREE_CHAIN (argtypes))
13268 if (TREE_PURPOSE (argtypes))
13270 TREE_PURPOSE (argtypes) = NULL_TREE;
13271 if (operator_code == POSTINCREMENT_EXPR
13272 || operator_code == POSTDECREMENT_EXPR)
13274 pedwarn (input_location, OPT_Wpedantic, "%qD cannot have default arguments",
13275 decl);
13277 else
13279 error ("%qD cannot have default arguments", decl);
13280 return false;
13284 return true;
13287 /* Return a string giving the keyword associate with CODE. */
13289 static const char *
13290 tag_name (enum tag_types code)
13292 switch (code)
13294 case record_type:
13295 return "struct";
13296 case class_type:
13297 return "class";
13298 case union_type:
13299 return "union";
13300 case enum_type:
13301 return "enum";
13302 case typename_type:
13303 return "typename";
13304 default:
13305 gcc_unreachable ();
13309 /* Name lookup in an elaborated-type-specifier (after the keyword
13310 indicated by TAG_CODE) has found the TYPE_DECL DECL. If the
13311 elaborated-type-specifier is invalid, issue a diagnostic and return
13312 error_mark_node; otherwise, return the *_TYPE to which it referred.
13313 If ALLOW_TEMPLATE_P is true, TYPE may be a class template. */
13315 tree
13316 check_elaborated_type_specifier (enum tag_types tag_code,
13317 tree decl,
13318 bool allow_template_p)
13320 tree type;
13322 /* In the case of:
13324 struct S { struct S *p; };
13326 name lookup will find the TYPE_DECL for the implicit "S::S"
13327 typedef. Adjust for that here. */
13328 if (DECL_SELF_REFERENCE_P (decl))
13329 decl = TYPE_NAME (TREE_TYPE (decl));
13331 type = TREE_TYPE (decl);
13333 /* Check TEMPLATE_TYPE_PARM first because DECL_IMPLICIT_TYPEDEF_P
13334 is false for this case as well. */
13335 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
13337 error ("using template type parameter %qT after %qs",
13338 type, tag_name (tag_code));
13339 return error_mark_node;
13341 /* Accept template template parameters. */
13342 else if (allow_template_p
13343 && (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM
13344 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM))
13346 /* [dcl.type.elab]
13348 If the identifier resolves to a typedef-name or the
13349 simple-template-id resolves to an alias template
13350 specialization, the elaborated-type-specifier is ill-formed.
13352 In other words, the only legitimate declaration to use in the
13353 elaborated type specifier is the implicit typedef created when
13354 the type is declared. */
13355 else if (!DECL_IMPLICIT_TYPEDEF_P (decl)
13356 && !DECL_SELF_REFERENCE_P (decl)
13357 && tag_code != typename_type)
13359 if (alias_template_specialization_p (type))
13360 error ("using alias template specialization %qT after %qs",
13361 type, tag_name (tag_code));
13362 else
13363 error ("using typedef-name %qD after %qs", decl, tag_name (tag_code));
13364 inform (DECL_SOURCE_LOCATION (decl),
13365 "%qD has a previous declaration here", decl);
13366 return error_mark_node;
13368 else if (TREE_CODE (type) != RECORD_TYPE
13369 && TREE_CODE (type) != UNION_TYPE
13370 && tag_code != enum_type
13371 && tag_code != typename_type)
13373 error ("%qT referred to as %qs", type, tag_name (tag_code));
13374 inform (location_of (type), "%qT has a previous declaration here", type);
13375 return error_mark_node;
13377 else if (TREE_CODE (type) != ENUMERAL_TYPE
13378 && tag_code == enum_type)
13380 error ("%qT referred to as enum", type);
13381 inform (location_of (type), "%qT has a previous declaration here", type);
13382 return error_mark_node;
13384 else if (!allow_template_p
13385 && TREE_CODE (type) == RECORD_TYPE
13386 && CLASSTYPE_IS_TEMPLATE (type))
13388 /* If a class template appears as elaborated type specifier
13389 without a template header such as:
13391 template <class T> class C {};
13392 void f(class C); // No template header here
13394 then the required template argument is missing. */
13395 error ("template argument required for %<%s %T%>",
13396 tag_name (tag_code),
13397 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)));
13398 return error_mark_node;
13401 return type;
13404 /* Lookup NAME in elaborate type specifier in scope according to
13405 SCOPE and issue diagnostics if necessary.
13406 Return *_TYPE node upon success, NULL_TREE when the NAME is not
13407 found, and ERROR_MARK_NODE for type error. */
13409 static tree
13410 lookup_and_check_tag (enum tag_types tag_code, tree name,
13411 tag_scope scope, bool template_header_p)
13413 tree t;
13414 tree decl;
13415 if (scope == ts_global)
13417 /* First try ordinary name lookup, ignoring hidden class name
13418 injected via friend declaration. */
13419 decl = lookup_name_prefer_type (name, 2);
13420 decl = strip_using_decl (decl);
13421 /* If that fails, the name will be placed in the smallest
13422 non-class, non-function-prototype scope according to 3.3.1/5.
13423 We may already have a hidden name declared as friend in this
13424 scope. So lookup again but not ignoring hidden names.
13425 If we find one, that name will be made visible rather than
13426 creating a new tag. */
13427 if (!decl)
13428 decl = lookup_type_scope (name, ts_within_enclosing_non_class);
13430 else
13431 decl = lookup_type_scope (name, scope);
13433 if (decl
13434 && (DECL_CLASS_TEMPLATE_P (decl)
13435 /* If scope is ts_current we're defining a class, so ignore a
13436 template template parameter. */
13437 || (scope != ts_current
13438 && DECL_TEMPLATE_TEMPLATE_PARM_P (decl))))
13439 decl = DECL_TEMPLATE_RESULT (decl);
13441 if (decl && TREE_CODE (decl) == TYPE_DECL)
13443 /* Look for invalid nested type:
13444 class C {
13445 class C {};
13446 }; */
13447 if (scope == ts_current && DECL_SELF_REFERENCE_P (decl))
13449 error ("%qD has the same name as the class in which it is "
13450 "declared",
13451 decl);
13452 return error_mark_node;
13455 /* Two cases we need to consider when deciding if a class
13456 template is allowed as an elaborated type specifier:
13457 1. It is a self reference to its own class.
13458 2. It comes with a template header.
13460 For example:
13462 template <class T> class C {
13463 class C *c1; // DECL_SELF_REFERENCE_P is true
13464 class D;
13466 template <class U> class C; // template_header_p is true
13467 template <class T> class C<T>::D {
13468 class C *c2; // DECL_SELF_REFERENCE_P is true
13469 }; */
13471 t = check_elaborated_type_specifier (tag_code,
13472 decl,
13473 template_header_p
13474 | DECL_SELF_REFERENCE_P (decl));
13475 if (template_header_p && t && CLASS_TYPE_P (t)
13476 && (!CLASSTYPE_TEMPLATE_INFO (t)
13477 || (!PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)))))
13479 error ("%qT is not a template", t);
13480 inform (location_of (t), "previous declaration here");
13481 if (TYPE_CLASS_SCOPE_P (t)
13482 && CLASSTYPE_TEMPLATE_INFO (TYPE_CONTEXT (t)))
13483 inform (input_location,
13484 "perhaps you want to explicitly add %<%T::%>",
13485 TYPE_CONTEXT (t));
13486 t = error_mark_node;
13489 return t;
13491 else if (decl && TREE_CODE (decl) == TREE_LIST)
13493 error ("reference to %qD is ambiguous", name);
13494 print_candidates (decl);
13495 return error_mark_node;
13497 else
13498 return NULL_TREE;
13501 /* Get the struct, enum or union (TAG_CODE says which) with tag NAME.
13502 Define the tag as a forward-reference if it is not defined.
13504 If a declaration is given, process it here, and report an error if
13505 multiple declarations are not identical.
13507 SCOPE is TS_CURRENT when this is also a definition. Only look in
13508 the current frame for the name (since C++ allows new names in any
13509 scope.) It is TS_WITHIN_ENCLOSING_NON_CLASS if this is a friend
13510 declaration. Only look beginning from the current scope outward up
13511 till the nearest non-class scope. Otherwise it is TS_GLOBAL.
13513 TEMPLATE_HEADER_P is true when this declaration is preceded by
13514 a set of template parameters. */
13516 static tree
13517 xref_tag_1 (enum tag_types tag_code, tree name,
13518 tag_scope scope, bool template_header_p)
13520 enum tree_code code;
13521 tree context = NULL_TREE;
13523 gcc_assert (identifier_p (name));
13525 switch (tag_code)
13527 case record_type:
13528 case class_type:
13529 code = RECORD_TYPE;
13530 break;
13531 case union_type:
13532 code = UNION_TYPE;
13533 break;
13534 case enum_type:
13535 code = ENUMERAL_TYPE;
13536 break;
13537 default:
13538 gcc_unreachable ();
13541 /* In case of anonymous name, xref_tag is only called to
13542 make type node and push name. Name lookup is not required. */
13543 tree t = NULL_TREE;
13544 if (scope != ts_lambda && !anon_aggrname_p (name))
13545 t = lookup_and_check_tag (tag_code, name, scope, template_header_p);
13547 if (t == error_mark_node)
13548 return error_mark_node;
13550 if (scope != ts_current && t && current_class_type
13551 && template_class_depth (current_class_type)
13552 && template_header_p)
13554 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
13555 return t;
13557 /* Since SCOPE is not TS_CURRENT, we are not looking at a
13558 definition of this tag. Since, in addition, we are currently
13559 processing a (member) template declaration of a template
13560 class, we must be very careful; consider:
13562 template <class X>
13563 struct S1
13565 template <class U>
13566 struct S2
13567 { template <class V>
13568 friend struct S1; };
13570 Here, the S2::S1 declaration should not be confused with the
13571 outer declaration. In particular, the inner version should
13572 have a template parameter of level 2, not level 1. This
13573 would be particularly important if the member declaration
13574 were instead:
13576 template <class V = U> friend struct S1;
13578 say, when we should tsubst into `U' when instantiating
13579 S2. On the other hand, when presented with:
13581 template <class T>
13582 struct S1 {
13583 template <class U>
13584 struct S2 {};
13585 template <class U>
13586 friend struct S2;
13589 we must find the inner binding eventually. We
13590 accomplish this by making sure that the new type we
13591 create to represent this declaration has the right
13592 TYPE_CONTEXT. */
13593 context = TYPE_CONTEXT (t);
13594 t = NULL_TREE;
13597 if (! t)
13599 /* If no such tag is yet defined, create a forward-reference node
13600 and record it as the "definition".
13601 When a real declaration of this type is found,
13602 the forward-reference will be altered into a real type. */
13603 if (code == ENUMERAL_TYPE)
13605 error ("use of enum %q#D without previous declaration", name);
13606 return error_mark_node;
13608 else
13610 t = make_class_type (code);
13611 TYPE_CONTEXT (t) = context;
13612 if (scope == ts_lambda)
13613 /* Mark it as a lambda type. */
13614 CLASSTYPE_LAMBDA_EXPR (t) = error_mark_node;
13615 t = pushtag (name, t, scope);
13618 else
13620 if (template_header_p && MAYBE_CLASS_TYPE_P (t))
13622 /* Check that we aren't trying to overload a class with different
13623 constraints. */
13624 tree constr = NULL_TREE;
13625 if (current_template_parms)
13627 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
13628 constr = build_constraints (reqs, NULL_TREE);
13630 if (!redeclare_class_template (t, current_template_parms, constr))
13631 return error_mark_node;
13633 else if (!processing_template_decl
13634 && CLASS_TYPE_P (t)
13635 && CLASSTYPE_IS_TEMPLATE (t))
13637 error ("redeclaration of %qT as a non-template", t);
13638 inform (location_of (t), "previous declaration %qD", t);
13639 return error_mark_node;
13642 /* Make injected friend class visible. */
13643 if (scope != ts_within_enclosing_non_class && TYPE_HIDDEN_P (t))
13645 tree decl = TYPE_NAME (t);
13647 DECL_ANTICIPATED (decl) = false;
13648 DECL_FRIEND_P (decl) = false;
13650 if (TYPE_TEMPLATE_INFO (t))
13652 tree tmpl = TYPE_TI_TEMPLATE (t);
13653 DECL_ANTICIPATED (tmpl) = false;
13654 DECL_FRIEND_P (tmpl) = false;
13659 return t;
13662 /* Wrapper for xref_tag_1. */
13664 tree
13665 xref_tag (enum tag_types tag_code, tree name,
13666 tag_scope scope, bool template_header_p)
13668 tree ret;
13669 bool subtime;
13670 subtime = timevar_cond_start (TV_NAME_LOOKUP);
13671 ret = xref_tag_1 (tag_code, name, scope, template_header_p);
13672 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
13673 return ret;
13677 tree
13678 xref_tag_from_type (tree old, tree id, tag_scope scope)
13680 enum tag_types tag_kind;
13682 if (TREE_CODE (old) == RECORD_TYPE)
13683 tag_kind = (CLASSTYPE_DECLARED_CLASS (old) ? class_type : record_type);
13684 else
13685 tag_kind = union_type;
13687 if (id == NULL_TREE)
13688 id = TYPE_IDENTIFIER (old);
13690 return xref_tag (tag_kind, id, scope, false);
13693 /* Create the binfo hierarchy for REF with (possibly NULL) base list
13694 BASE_LIST. For each element on BASE_LIST the TREE_PURPOSE is an
13695 access_* node, and the TREE_VALUE is the type of the base-class.
13696 Non-NULL TREE_TYPE indicates virtual inheritance. */
13698 void
13699 xref_basetypes (tree ref, tree base_list)
13701 tree *basep;
13702 tree binfo, base_binfo;
13703 unsigned max_vbases = 0; /* Maximum direct & indirect virtual bases. */
13704 unsigned max_bases = 0; /* Maximum direct bases. */
13705 unsigned max_dvbases = 0; /* Maximum direct virtual bases. */
13706 int i;
13707 tree default_access;
13708 tree igo_prev; /* Track Inheritance Graph Order. */
13710 if (ref == error_mark_node)
13711 return;
13713 /* The base of a derived class is private by default, all others are
13714 public. */
13715 default_access = (TREE_CODE (ref) == RECORD_TYPE
13716 && CLASSTYPE_DECLARED_CLASS (ref)
13717 ? access_private_node : access_public_node);
13719 /* First, make sure that any templates in base-classes are
13720 instantiated. This ensures that if we call ourselves recursively
13721 we do not get confused about which classes are marked and which
13722 are not. */
13723 basep = &base_list;
13724 while (*basep)
13726 tree basetype = TREE_VALUE (*basep);
13728 /* The dependent_type_p call below should really be dependent_scope_p
13729 so that we give a hard error about using an incomplete type as a
13730 base, but we allow it with a pedwarn for backward
13731 compatibility. */
13732 if (processing_template_decl
13733 && CLASS_TYPE_P (basetype) && TYPE_BEING_DEFINED (basetype))
13734 cxx_incomplete_type_diagnostic (NULL_TREE, basetype, DK_PEDWARN);
13735 if (!dependent_type_p (basetype)
13736 && !complete_type_or_else (basetype, NULL))
13737 /* An incomplete type. Remove it from the list. */
13738 *basep = TREE_CHAIN (*basep);
13739 else
13741 max_bases++;
13742 if (TREE_TYPE (*basep))
13743 max_dvbases++;
13744 if (CLASS_TYPE_P (basetype))
13745 max_vbases += vec_safe_length (CLASSTYPE_VBASECLASSES (basetype));
13746 basep = &TREE_CHAIN (*basep);
13749 max_vbases += max_dvbases;
13751 TYPE_MARKED_P (ref) = 1;
13753 /* The binfo slot should be empty, unless this is an (ill-formed)
13754 redefinition. */
13755 gcc_assert (!TYPE_BINFO (ref) || TYPE_SIZE (ref));
13757 gcc_assert (TYPE_MAIN_VARIANT (ref) == ref);
13759 binfo = make_tree_binfo (max_bases);
13761 TYPE_BINFO (ref) = binfo;
13762 BINFO_OFFSET (binfo) = size_zero_node;
13763 BINFO_TYPE (binfo) = ref;
13765 /* Apply base-class info set up to the variants of this type. */
13766 fixup_type_variants (ref);
13768 if (max_bases)
13770 vec_alloc (BINFO_BASE_ACCESSES (binfo), max_bases);
13771 /* A C++98 POD cannot have base classes. */
13772 CLASSTYPE_NON_LAYOUT_POD_P (ref) = true;
13774 if (TREE_CODE (ref) == UNION_TYPE)
13775 error ("derived union %qT invalid", ref);
13778 if (max_bases > 1)
13779 warning (OPT_Wmultiple_inheritance,
13780 "%qT defined with multiple direct bases", ref);
13782 if (max_vbases)
13784 /* An aggregate can't have virtual base classes. */
13785 CLASSTYPE_NON_AGGREGATE (ref) = true;
13787 vec_alloc (CLASSTYPE_VBASECLASSES (ref), max_vbases);
13789 if (max_dvbases)
13790 warning (OPT_Wvirtual_inheritance,
13791 "%qT defined with direct virtual base", ref);
13794 for (igo_prev = binfo; base_list; base_list = TREE_CHAIN (base_list))
13796 tree access = TREE_PURPOSE (base_list);
13797 int via_virtual = TREE_TYPE (base_list) != NULL_TREE;
13798 tree basetype = TREE_VALUE (base_list);
13800 if (access == access_default_node)
13801 access = default_access;
13803 /* Before C++17, an aggregate cannot have base classes. In C++17, an
13804 aggregate can't have virtual, private, or protected base classes. */
13805 if (cxx_dialect < cxx17
13806 || access != access_public_node
13807 || via_virtual)
13808 CLASSTYPE_NON_AGGREGATE (ref) = true;
13810 if (PACK_EXPANSION_P (basetype))
13811 basetype = PACK_EXPANSION_PATTERN (basetype);
13812 if (TREE_CODE (basetype) == TYPE_DECL)
13813 basetype = TREE_TYPE (basetype);
13814 if (!MAYBE_CLASS_TYPE_P (basetype) || TREE_CODE (basetype) == UNION_TYPE)
13816 error ("base type %qT fails to be a struct or class type",
13817 basetype);
13818 goto dropped_base;
13821 base_binfo = NULL_TREE;
13822 if (CLASS_TYPE_P (basetype) && !dependent_scope_p (basetype))
13824 base_binfo = TYPE_BINFO (basetype);
13825 /* The original basetype could have been a typedef'd type. */
13826 basetype = BINFO_TYPE (base_binfo);
13828 /* Inherit flags from the base. */
13829 TYPE_HAS_NEW_OPERATOR (ref)
13830 |= TYPE_HAS_NEW_OPERATOR (basetype);
13831 TYPE_HAS_ARRAY_NEW_OPERATOR (ref)
13832 |= TYPE_HAS_ARRAY_NEW_OPERATOR (basetype);
13833 TYPE_GETS_DELETE (ref) |= TYPE_GETS_DELETE (basetype);
13834 TYPE_HAS_CONVERSION (ref) |= TYPE_HAS_CONVERSION (basetype);
13835 CLASSTYPE_DIAMOND_SHAPED_P (ref)
13836 |= CLASSTYPE_DIAMOND_SHAPED_P (basetype);
13837 CLASSTYPE_REPEATED_BASE_P (ref)
13838 |= CLASSTYPE_REPEATED_BASE_P (basetype);
13841 /* We must do this test after we've seen through a typedef
13842 type. */
13843 if (TYPE_MARKED_P (basetype))
13845 if (basetype == ref)
13846 error ("recursive type %qT undefined", basetype);
13847 else
13848 error ("duplicate base type %qT invalid", basetype);
13849 goto dropped_base;
13852 if (PACK_EXPANSION_P (TREE_VALUE (base_list)))
13853 /* Regenerate the pack expansion for the bases. */
13854 basetype = make_pack_expansion (basetype);
13856 TYPE_MARKED_P (basetype) = 1;
13858 base_binfo = copy_binfo (base_binfo, basetype, ref,
13859 &igo_prev, via_virtual);
13860 if (!BINFO_INHERITANCE_CHAIN (base_binfo))
13861 BINFO_INHERITANCE_CHAIN (base_binfo) = binfo;
13863 BINFO_BASE_APPEND (binfo, base_binfo);
13864 BINFO_BASE_ACCESS_APPEND (binfo, access);
13865 continue;
13867 dropped_base:
13868 /* Update max_vbases to reflect the reality that we are dropping
13869 this base: if it reaches zero we want to undo the vec_alloc
13870 above to avoid inconsistencies during error-recovery: eg, in
13871 build_special_member_call, CLASSTYPE_VBASECLASSES non null
13872 and vtt null (c++/27952). */
13873 if (via_virtual)
13874 max_vbases--;
13875 if (CLASS_TYPE_P (basetype))
13876 max_vbases
13877 -= vec_safe_length (CLASSTYPE_VBASECLASSES (basetype));
13880 if (CLASSTYPE_VBASECLASSES (ref)
13881 && max_vbases == 0)
13882 vec_free (CLASSTYPE_VBASECLASSES (ref));
13884 if (vec_safe_length (CLASSTYPE_VBASECLASSES (ref)) < max_vbases)
13885 /* If we didn't get max_vbases vbases, we must have shared at
13886 least one of them, and are therefore diamond shaped. */
13887 CLASSTYPE_DIAMOND_SHAPED_P (ref) = 1;
13889 /* Unmark all the types. */
13890 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
13891 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
13892 TYPE_MARKED_P (ref) = 0;
13894 /* Now see if we have a repeated base type. */
13895 if (!CLASSTYPE_REPEATED_BASE_P (ref))
13897 for (base_binfo = binfo; base_binfo;
13898 base_binfo = TREE_CHAIN (base_binfo))
13900 if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
13902 CLASSTYPE_REPEATED_BASE_P (ref) = 1;
13903 break;
13905 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 1;
13907 for (base_binfo = binfo; base_binfo;
13908 base_binfo = TREE_CHAIN (base_binfo))
13909 if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
13910 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
13911 else
13912 break;
13917 /* Copies the enum-related properties from type SRC to type DST.
13918 Used with the underlying type of an enum and the enum itself. */
13919 static void
13920 copy_type_enum (tree dst, tree src)
13922 tree t;
13923 for (t = dst; t; t = TYPE_NEXT_VARIANT (t))
13925 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (src);
13926 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (src);
13927 TYPE_SIZE (t) = TYPE_SIZE (src);
13928 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (src);
13929 SET_TYPE_MODE (dst, TYPE_MODE (src));
13930 TYPE_PRECISION (t) = TYPE_PRECISION (src);
13931 unsigned valign = TYPE_ALIGN (src);
13932 if (TYPE_USER_ALIGN (t))
13933 valign = MAX (valign, TYPE_ALIGN (t));
13934 else
13935 TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (src);
13936 SET_TYPE_ALIGN (t, valign);
13937 TYPE_UNSIGNED (t) = TYPE_UNSIGNED (src);
13941 /* Begin compiling the definition of an enumeration type.
13942 NAME is its name,
13944 if ENUMTYPE is not NULL_TREE then the type has alredy been found.
13946 UNDERLYING_TYPE is the type that will be used as the storage for
13947 the enumeration type. This should be NULL_TREE if no storage type
13948 was specified.
13950 ATTRIBUTES are any attributes specified after the enum-key.
13952 SCOPED_ENUM_P is true if this is a scoped enumeration type.
13954 if IS_NEW is not NULL, gets TRUE iff a new type is created.
13956 Returns the type object, as yet incomplete.
13957 Also records info about it so that build_enumerator
13958 may be used to declare the individual values as they are read. */
13960 tree
13961 start_enum (tree name, tree enumtype, tree underlying_type,
13962 tree attributes, bool scoped_enum_p, bool *is_new)
13964 tree prevtype = NULL_TREE;
13965 gcc_assert (identifier_p (name));
13967 if (is_new)
13968 *is_new = false;
13969 /* [C++0x dcl.enum]p5:
13971 If not explicitly specified, the underlying type of a scoped
13972 enumeration type is int. */
13973 if (!underlying_type && scoped_enum_p)
13974 underlying_type = integer_type_node;
13976 if (underlying_type)
13977 underlying_type = cv_unqualified (underlying_type);
13979 /* If this is the real definition for a previous forward reference,
13980 fill in the contents in the same object that used to be the
13981 forward reference. */
13982 if (!enumtype)
13983 enumtype = lookup_and_check_tag (enum_type, name,
13984 /*tag_scope=*/ts_current,
13985 /*template_header_p=*/false);
13987 /* In case of a template_decl, the only check that should be deferred
13988 to instantiation time is the comparison of underlying types. */
13989 if (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE)
13991 if (scoped_enum_p != SCOPED_ENUM_P (enumtype))
13993 error_at (input_location, "scoped/unscoped mismatch "
13994 "in enum %q#T", enumtype);
13995 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
13996 "previous definition here");
13997 enumtype = error_mark_node;
13999 else if (ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) != !! underlying_type)
14001 error_at (input_location, "underlying type mismatch "
14002 "in enum %q#T", enumtype);
14003 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
14004 "previous definition here");
14005 enumtype = error_mark_node;
14007 else if (underlying_type && ENUM_UNDERLYING_TYPE (enumtype)
14008 && !dependent_type_p (underlying_type)
14009 && !dependent_type_p (ENUM_UNDERLYING_TYPE (enumtype))
14010 && !same_type_p (underlying_type,
14011 ENUM_UNDERLYING_TYPE (enumtype)))
14013 error_at (input_location, "different underlying type "
14014 "in enum %q#T", enumtype);
14015 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
14016 "previous definition here");
14017 underlying_type = NULL_TREE;
14021 if (!enumtype || TREE_CODE (enumtype) != ENUMERAL_TYPE
14022 || processing_template_decl)
14024 /* In case of error, make a dummy enum to allow parsing to
14025 continue. */
14026 if (enumtype == error_mark_node)
14028 name = make_anon_name ();
14029 enumtype = NULL_TREE;
14032 /* enumtype may be an ENUMERAL_TYPE if this is a redefinition
14033 of an opaque enum, or an opaque enum of an already defined
14034 enumeration (C++0x only).
14035 In any other case, it'll be NULL_TREE. */
14036 if (!enumtype)
14038 if (is_new)
14039 *is_new = true;
14041 prevtype = enumtype;
14043 /* Do not push the decl more than once, unless we need to
14044 compare underlying types at instantiation time */
14045 if (!enumtype
14046 || TREE_CODE (enumtype) != ENUMERAL_TYPE
14047 || (underlying_type
14048 && dependent_type_p (underlying_type))
14049 || (ENUM_UNDERLYING_TYPE (enumtype)
14050 && dependent_type_p (ENUM_UNDERLYING_TYPE (enumtype))))
14052 enumtype = cxx_make_type (ENUMERAL_TYPE);
14053 enumtype = pushtag (name, enumtype, /*tag_scope=*/ts_current);
14055 /* std::byte aliases anything. */
14056 if (enumtype != error_mark_node
14057 && TYPE_CONTEXT (enumtype) == std_node
14058 && !strcmp ("byte", TYPE_NAME_STRING (enumtype)))
14059 TYPE_ALIAS_SET (enumtype) = 0;
14061 else
14062 enumtype = xref_tag (enum_type, name, /*tag_scope=*/ts_current,
14063 false);
14065 if (enumtype == error_mark_node)
14066 return error_mark_node;
14068 /* The enum is considered opaque until the opening '{' of the
14069 enumerator list. */
14070 SET_OPAQUE_ENUM_P (enumtype, true);
14071 ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) = !! underlying_type;
14074 SET_SCOPED_ENUM_P (enumtype, scoped_enum_p);
14076 cplus_decl_attributes (&enumtype, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
14078 if (underlying_type)
14080 if (ENUM_UNDERLYING_TYPE (enumtype))
14081 /* We already checked that it matches, don't change it to a different
14082 typedef variant. */;
14083 else if (CP_INTEGRAL_TYPE_P (underlying_type))
14085 copy_type_enum (enumtype, underlying_type);
14086 ENUM_UNDERLYING_TYPE (enumtype) = underlying_type;
14088 else if (dependent_type_p (underlying_type))
14089 ENUM_UNDERLYING_TYPE (enumtype) = underlying_type;
14090 else
14091 error ("underlying type %qT of %qT must be an integral type",
14092 underlying_type, enumtype);
14095 /* If into a template class, the returned enum is always the first
14096 declaration (opaque or not) seen. This way all the references to
14097 this type will be to the same declaration. The following ones are used
14098 only to check for definition errors. */
14099 if (prevtype && processing_template_decl)
14100 return prevtype;
14101 else
14102 return enumtype;
14105 /* After processing and defining all the values of an enumeration type,
14106 install their decls in the enumeration type.
14107 ENUMTYPE is the type object. */
14109 void
14110 finish_enum_value_list (tree enumtype)
14112 tree values;
14113 tree underlying_type;
14114 tree decl;
14115 tree value;
14116 tree minnode, maxnode;
14117 tree t;
14119 bool fixed_underlying_type_p
14120 = ENUM_UNDERLYING_TYPE (enumtype) != NULL_TREE;
14122 /* We built up the VALUES in reverse order. */
14123 TYPE_VALUES (enumtype) = nreverse (TYPE_VALUES (enumtype));
14125 /* For an enum defined in a template, just set the type of the values;
14126 all further processing is postponed until the template is
14127 instantiated. We need to set the type so that tsubst of a CONST_DECL
14128 works. */
14129 if (processing_template_decl)
14131 for (values = TYPE_VALUES (enumtype);
14132 values;
14133 values = TREE_CHAIN (values))
14134 TREE_TYPE (TREE_VALUE (values)) = enumtype;
14135 return;
14138 /* Determine the minimum and maximum values of the enumerators. */
14139 if (TYPE_VALUES (enumtype))
14141 minnode = maxnode = NULL_TREE;
14143 for (values = TYPE_VALUES (enumtype);
14144 values;
14145 values = TREE_CHAIN (values))
14147 decl = TREE_VALUE (values);
14149 /* [dcl.enum]: Following the closing brace of an enum-specifier,
14150 each enumerator has the type of its enumeration. Prior to the
14151 closing brace, the type of each enumerator is the type of its
14152 initializing value. */
14153 TREE_TYPE (decl) = enumtype;
14155 /* Update the minimum and maximum values, if appropriate. */
14156 value = DECL_INITIAL (decl);
14157 if (value == error_mark_node)
14158 value = integer_zero_node;
14159 /* Figure out what the minimum and maximum values of the
14160 enumerators are. */
14161 if (!minnode)
14162 minnode = maxnode = value;
14163 else if (tree_int_cst_lt (maxnode, value))
14164 maxnode = value;
14165 else if (tree_int_cst_lt (value, minnode))
14166 minnode = value;
14169 else
14170 /* [dcl.enum]
14172 If the enumerator-list is empty, the underlying type is as if
14173 the enumeration had a single enumerator with value 0. */
14174 minnode = maxnode = integer_zero_node;
14176 if (!fixed_underlying_type_p)
14178 /* Compute the number of bits require to represent all values of the
14179 enumeration. We must do this before the type of MINNODE and
14180 MAXNODE are transformed, since tree_int_cst_min_precision relies
14181 on the TREE_TYPE of the value it is passed. */
14182 signop sgn = tree_int_cst_sgn (minnode) >= 0 ? UNSIGNED : SIGNED;
14183 int lowprec = tree_int_cst_min_precision (minnode, sgn);
14184 int highprec = tree_int_cst_min_precision (maxnode, sgn);
14185 int precision = MAX (lowprec, highprec);
14186 unsigned int itk;
14187 bool use_short_enum;
14189 /* Determine the underlying type of the enumeration.
14191 [dcl.enum]
14193 The underlying type of an enumeration is an integral type that
14194 can represent all the enumerator values defined in the
14195 enumeration. It is implementation-defined which integral type is
14196 used as the underlying type for an enumeration except that the
14197 underlying type shall not be larger than int unless the value of
14198 an enumerator cannot fit in an int or unsigned int.
14200 We use "int" or an "unsigned int" as the underlying type, even if
14201 a smaller integral type would work, unless the user has
14202 explicitly requested that we use the smallest possible type. The
14203 user can request that for all enumerations with a command line
14204 flag, or for just one enumeration with an attribute. */
14206 use_short_enum = flag_short_enums
14207 || lookup_attribute ("packed", TYPE_ATTRIBUTES (enumtype));
14209 /* If the precision of the type was specified with an attribute and it
14210 was too small, give an error. Otherwise, use it. */
14211 if (TYPE_PRECISION (enumtype))
14213 if (precision > TYPE_PRECISION (enumtype))
14214 error ("specified mode too small for enumeral values");
14215 else
14217 use_short_enum = true;
14218 precision = TYPE_PRECISION (enumtype);
14222 for (itk = (use_short_enum ? itk_char : itk_int);
14223 itk != itk_none;
14224 itk++)
14226 underlying_type = integer_types[itk];
14227 if (underlying_type != NULL_TREE
14228 && TYPE_PRECISION (underlying_type) >= precision
14229 && TYPE_SIGN (underlying_type) == sgn)
14230 break;
14232 if (itk == itk_none)
14234 /* DR 377
14236 IF no integral type can represent all the enumerator values, the
14237 enumeration is ill-formed. */
14238 error ("no integral type can represent all of the enumerator values "
14239 "for %qT", enumtype);
14240 precision = TYPE_PRECISION (long_long_integer_type_node);
14241 underlying_type = integer_types[itk_unsigned_long_long];
14244 /* [dcl.enum]
14246 The value of sizeof() applied to an enumeration type, an object
14247 of an enumeration type, or an enumerator, is the value of sizeof()
14248 applied to the underlying type. */
14249 copy_type_enum (enumtype, underlying_type);
14251 /* Compute the minimum and maximum values for the type.
14253 [dcl.enum]
14255 For an enumeration where emin is the smallest enumerator and emax
14256 is the largest, the values of the enumeration are the values of the
14257 underlying type in the range bmin to bmax, where bmin and bmax are,
14258 respectively, the smallest and largest values of the smallest bit-
14259 field that can store emin and emax. */
14261 /* The middle-end currently assumes that types with TYPE_PRECISION
14262 narrower than their underlying type are suitably zero or sign
14263 extended to fill their mode. Similarly, it assumes that the front
14264 end assures that a value of a particular type must be within
14265 TYPE_MIN_VALUE and TYPE_MAX_VALUE.
14267 We used to set these fields based on bmin and bmax, but that led
14268 to invalid assumptions like optimizing away bounds checking. So
14269 now we just set the TYPE_PRECISION, TYPE_MIN_VALUE, and
14270 TYPE_MAX_VALUE to the values for the mode above and only restrict
14271 the ENUM_UNDERLYING_TYPE for the benefit of diagnostics. */
14272 ENUM_UNDERLYING_TYPE (enumtype)
14273 = build_distinct_type_copy (underlying_type);
14274 TYPE_PRECISION (ENUM_UNDERLYING_TYPE (enumtype)) = precision;
14275 set_min_and_max_values_for_integral_type
14276 (ENUM_UNDERLYING_TYPE (enumtype), precision, sgn);
14278 /* If -fstrict-enums, still constrain TYPE_MIN/MAX_VALUE. */
14279 if (flag_strict_enums)
14280 set_min_and_max_values_for_integral_type (enumtype, precision, sgn);
14282 else
14283 underlying_type = ENUM_UNDERLYING_TYPE (enumtype);
14285 /* Convert each of the enumerators to the type of the underlying
14286 type of the enumeration. */
14287 for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values))
14289 location_t saved_location;
14291 decl = TREE_VALUE (values);
14292 saved_location = input_location;
14293 input_location = DECL_SOURCE_LOCATION (decl);
14294 if (fixed_underlying_type_p)
14295 /* If the enumeration type has a fixed underlying type, we
14296 already checked all of the enumerator values. */
14297 value = DECL_INITIAL (decl);
14298 else
14299 value = perform_implicit_conversion (underlying_type,
14300 DECL_INITIAL (decl),
14301 tf_warning_or_error);
14302 input_location = saved_location;
14304 /* Do not clobber shared ints. */
14305 if (value != error_mark_node)
14307 value = copy_node (value);
14309 TREE_TYPE (value) = enumtype;
14311 DECL_INITIAL (decl) = value;
14314 /* Fix up all variant types of this enum type. */
14315 for (t = TYPE_MAIN_VARIANT (enumtype); t; t = TYPE_NEXT_VARIANT (t))
14316 TYPE_VALUES (t) = TYPE_VALUES (enumtype);
14318 if (at_class_scope_p ()
14319 && COMPLETE_TYPE_P (current_class_type)
14320 && UNSCOPED_ENUM_P (enumtype))
14322 insert_late_enum_def_bindings (current_class_type, enumtype);
14323 /* TYPE_FIELDS needs fixup. */
14324 fixup_type_variants (current_class_type);
14327 /* Finish debugging output for this type. */
14328 rest_of_type_compilation (enumtype, namespace_bindings_p ());
14330 /* Each enumerator now has the type of its enumeration. Clear the cache
14331 so that this change in types doesn't confuse us later on. */
14332 clear_cv_and_fold_caches ();
14335 /* Finishes the enum type. This is called only the first time an
14336 enumeration is seen, be it opaque or odinary.
14337 ENUMTYPE is the type object. */
14339 void
14340 finish_enum (tree enumtype)
14342 if (processing_template_decl)
14344 if (at_function_scope_p ())
14345 add_stmt (build_min (TAG_DEFN, enumtype));
14346 return;
14349 /* If this is a forward declaration, there should not be any variants,
14350 though we can get a variant in the middle of an enum-specifier with
14351 wacky code like 'enum E { e = sizeof(const E*) };' */
14352 gcc_assert (enumtype == TYPE_MAIN_VARIANT (enumtype)
14353 && (TYPE_VALUES (enumtype)
14354 || !TYPE_NEXT_VARIANT (enumtype)));
14357 /* Build and install a CONST_DECL for an enumeration constant of the
14358 enumeration type ENUMTYPE whose NAME and VALUE (if any) are provided.
14359 Apply ATTRIBUTES if available. LOC is the location of NAME.
14360 Assignment of sequential values by default is handled here. */
14362 void
14363 build_enumerator (tree name, tree value, tree enumtype, tree attributes,
14364 location_t loc)
14366 tree decl;
14367 tree context;
14368 tree type;
14370 /* scalar_constant_value will pull out this expression, so make sure
14371 it's folded as appropriate. */
14372 if (processing_template_decl)
14373 value = fold_non_dependent_expr (value);
14375 /* If the VALUE was erroneous, pretend it wasn't there; that will
14376 result in the enum being assigned the next value in sequence. */
14377 if (value == error_mark_node)
14378 value = NULL_TREE;
14380 /* Remove no-op casts from the value. */
14381 if (value)
14382 STRIP_TYPE_NOPS (value);
14384 if (! processing_template_decl)
14386 /* Validate and default VALUE. */
14387 if (value != NULL_TREE)
14389 if (!ENUM_UNDERLYING_TYPE (enumtype))
14391 tree tmp_value = build_expr_type_conversion (WANT_INT | WANT_ENUM,
14392 value, true);
14393 if (tmp_value)
14394 value = tmp_value;
14396 else if (! INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P
14397 (TREE_TYPE (value)))
14398 value = perform_implicit_conversion_flags
14399 (ENUM_UNDERLYING_TYPE (enumtype), value, tf_warning_or_error,
14400 LOOKUP_IMPLICIT | LOOKUP_NO_NARROWING);
14402 if (value == error_mark_node)
14403 value = NULL_TREE;
14405 if (value != NULL_TREE)
14407 if (! INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P
14408 (TREE_TYPE (value)))
14410 error ("enumerator value for %qD must have integral or "
14411 "unscoped enumeration type", name);
14412 value = NULL_TREE;
14414 else
14416 value = cxx_constant_value (value);
14418 if (TREE_CODE (value) != INTEGER_CST)
14420 error ("enumerator value for %qD is not an integer "
14421 "constant", name);
14422 value = NULL_TREE;
14428 /* Default based on previous value. */
14429 if (value == NULL_TREE)
14431 if (TYPE_VALUES (enumtype))
14433 tree prev_value;
14434 bool overflowed;
14436 /* C++03 7.2/4: If no initializer is specified for the first
14437 enumerator, the type is an unspecified integral
14438 type. Otherwise the type is the same as the type of the
14439 initializing value of the preceding enumerator unless the
14440 incremented value is not representable in that type, in
14441 which case the type is an unspecified integral type
14442 sufficient to contain the incremented value. */
14443 prev_value = DECL_INITIAL (TREE_VALUE (TYPE_VALUES (enumtype)));
14444 if (error_operand_p (prev_value))
14445 value = error_mark_node;
14446 else
14448 tree type = TREE_TYPE (prev_value);
14449 signop sgn = TYPE_SIGN (type);
14450 widest_int wi = wi::add (wi::to_widest (prev_value), 1, sgn,
14451 &overflowed);
14452 if (!overflowed)
14454 bool pos = !wi::neg_p (wi, sgn);
14455 if (!wi::fits_to_tree_p (wi, type))
14457 unsigned int itk;
14458 for (itk = itk_int; itk != itk_none; itk++)
14460 type = integer_types[itk];
14461 if (type != NULL_TREE
14462 && (pos || !TYPE_UNSIGNED (type))
14463 && wi::fits_to_tree_p (wi, type))
14464 break;
14466 if (type && cxx_dialect < cxx11
14467 && itk > itk_unsigned_long)
14468 pedwarn (input_location, OPT_Wlong_long,
14469 pos ? G_("\
14470 incremented enumerator value is too large for %<unsigned long%>") : G_("\
14471 incremented enumerator value is too large for %<long%>"));
14473 if (type == NULL_TREE)
14474 overflowed = true;
14475 else
14476 value = wide_int_to_tree (type, wi);
14479 if (overflowed)
14481 error ("overflow in enumeration values at %qD", name);
14482 value = error_mark_node;
14486 else
14487 value = integer_zero_node;
14490 /* Remove no-op casts from the value. */
14491 STRIP_TYPE_NOPS (value);
14493 /* If the underlying type of the enum is fixed, check whether
14494 the enumerator values fits in the underlying type. If it
14495 does not fit, the program is ill-formed [C++0x dcl.enum]. */
14496 if (ENUM_UNDERLYING_TYPE (enumtype)
14497 && value
14498 && TREE_CODE (value) == INTEGER_CST)
14500 if (!int_fits_type_p (value, ENUM_UNDERLYING_TYPE (enumtype)))
14501 error ("enumerator value %qE is outside the range of underlying "
14502 "type %qT", value, ENUM_UNDERLYING_TYPE (enumtype));
14504 /* Convert the value to the appropriate type. */
14505 value = fold_convert (ENUM_UNDERLYING_TYPE (enumtype), value);
14509 /* C++ associates enums with global, function, or class declarations. */
14510 context = current_scope ();
14512 /* Build the actual enumeration constant. Note that the enumeration
14513 constants have the underlying type of the enum (if it is fixed)
14514 or the type of their initializer (if the underlying type of the
14515 enum is not fixed):
14517 [ C++0x dcl.enum ]
14519 If the underlying type is fixed, the type of each enumerator
14520 prior to the closing brace is the underlying type; if the
14521 initializing value of an enumerator cannot be represented by
14522 the underlying type, the program is ill-formed. If the
14523 underlying type is not fixed, the type of each enumerator is
14524 the type of its initializing value.
14526 If the underlying type is not fixed, it will be computed by
14527 finish_enum and we will reset the type of this enumerator. Of
14528 course, if we're processing a template, there may be no value. */
14529 type = value ? TREE_TYPE (value) : NULL_TREE;
14531 decl = build_decl (loc, CONST_DECL, name, type);
14533 DECL_CONTEXT (decl) = enumtype;
14534 TREE_CONSTANT (decl) = 1;
14535 TREE_READONLY (decl) = 1;
14536 DECL_INITIAL (decl) = value;
14538 if (attributes)
14539 cplus_decl_attributes (&decl, attributes, 0);
14541 if (context && context == current_class_type && !SCOPED_ENUM_P (enumtype))
14543 /* In something like `struct S { enum E { i = 7 }; };' we put `i'
14544 on the TYPE_FIELDS list for `S'. (That's so that you can say
14545 things like `S::i' later.) */
14547 /* The enumerator may be getting declared outside of its enclosing
14548 class, like so:
14550 class S { public: enum E : int; }; enum S::E : int { i = 7; };
14552 For which case we need to make sure that the access of `S::i'
14553 matches the access of `S::E'. */
14554 tree saved_cas = current_access_specifier;
14555 if (TREE_PRIVATE (TYPE_NAME (enumtype)))
14556 current_access_specifier = access_private_node;
14557 else if (TREE_PROTECTED (TYPE_NAME (enumtype)))
14558 current_access_specifier = access_protected_node;
14559 else
14560 current_access_specifier = access_public_node;
14562 finish_member_declaration (decl);
14564 current_access_specifier = saved_cas;
14566 else
14567 pushdecl (decl);
14569 /* Add this enumeration constant to the list for this type. */
14570 TYPE_VALUES (enumtype) = tree_cons (name, decl, TYPE_VALUES (enumtype));
14573 /* Look for an enumerator with the given NAME within the enumeration
14574 type ENUMTYPE. This routine is used primarily for qualified name
14575 lookup into an enumerator in C++0x, e.g.,
14577 enum class Color { Red, Green, Blue };
14579 Color color = Color::Red;
14581 Returns the value corresponding to the enumerator, or
14582 NULL_TREE if no such enumerator was found. */
14583 tree
14584 lookup_enumerator (tree enumtype, tree name)
14586 tree e;
14587 gcc_assert (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE);
14589 e = purpose_member (name, TYPE_VALUES (enumtype));
14590 return e? TREE_VALUE (e) : NULL_TREE;
14594 /* We're defining DECL. Make sure that its type is OK. */
14596 static void
14597 check_function_type (tree decl, tree current_function_parms)
14599 tree fntype = TREE_TYPE (decl);
14600 tree return_type = complete_type (TREE_TYPE (fntype));
14602 /* In a function definition, arg types must be complete. */
14603 require_complete_types_for_parms (current_function_parms);
14605 if (dependent_type_p (return_type)
14606 || type_uses_auto (return_type))
14607 return;
14608 if (!COMPLETE_OR_VOID_TYPE_P (return_type))
14610 tree args = TYPE_ARG_TYPES (fntype);
14612 error ("return type %q#T is incomplete", return_type);
14614 /* Make it return void instead. */
14615 if (TREE_CODE (fntype) == METHOD_TYPE)
14616 fntype = build_method_type_directly (TREE_TYPE (TREE_VALUE (args)),
14617 void_type_node,
14618 TREE_CHAIN (args));
14619 else
14620 fntype = build_function_type (void_type_node, args);
14621 fntype
14622 = build_exception_variant (fntype,
14623 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl)));
14624 fntype = (cp_build_type_attribute_variant
14625 (fntype, TYPE_ATTRIBUTES (TREE_TYPE (decl))));
14626 TREE_TYPE (decl) = fntype;
14628 else
14630 abstract_virtuals_error (decl, TREE_TYPE (fntype));
14631 maybe_warn_parm_abi (TREE_TYPE (fntype),
14632 DECL_SOURCE_LOCATION (decl));
14636 /* True iff FN is an implicitly-defined default constructor. */
14638 static bool
14639 implicit_default_ctor_p (tree fn)
14641 return (DECL_CONSTRUCTOR_P (fn)
14642 && !user_provided_p (fn)
14643 && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn)));
14646 /* Clobber the contents of *this to let the back end know that the object
14647 storage is dead when we enter the constructor or leave the destructor. */
14649 static tree
14650 build_clobber_this ()
14652 /* Clobbering an empty base is pointless, and harmful if its one byte
14653 TYPE_SIZE overlays real data. */
14654 if (is_empty_class (current_class_type))
14655 return void_node;
14657 /* If we have virtual bases, clobber the whole object, but only if we're in
14658 charge. If we don't have virtual bases, clobber the as-base type so we
14659 don't mess with tail padding. */
14660 bool vbases = CLASSTYPE_VBASECLASSES (current_class_type);
14662 tree ctype = current_class_type;
14663 if (!vbases)
14664 ctype = CLASSTYPE_AS_BASE (ctype);
14666 tree clobber = build_constructor (ctype, NULL);
14667 TREE_THIS_VOLATILE (clobber) = true;
14669 tree thisref = current_class_ref;
14670 if (ctype != current_class_type)
14672 thisref = build_nop (build_reference_type (ctype), current_class_ptr);
14673 thisref = convert_from_reference (thisref);
14676 tree exprstmt = build2 (MODIFY_EXPR, void_type_node, thisref, clobber);
14677 if (vbases)
14678 exprstmt = build_if_in_charge (exprstmt);
14680 return exprstmt;
14683 /* Create the FUNCTION_DECL for a function definition.
14684 DECLSPECS and DECLARATOR are the parts of the declaration;
14685 they describe the function's name and the type it returns,
14686 but twisted together in a fashion that parallels the syntax of C.
14688 FLAGS is a bitwise or of SF_PRE_PARSED (indicating that the
14689 DECLARATOR is really the DECL for the function we are about to
14690 process and that DECLSPECS should be ignored), SF_INCLASS_INLINE
14691 indicating that the function is an inline defined in-class.
14693 This function creates a binding context for the function body
14694 as well as setting up the FUNCTION_DECL in current_function_decl.
14696 For C++, we must first check whether that datum makes any sense.
14697 For example, "class A local_a(1,2);" means that variable local_a
14698 is an aggregate of type A, which should have a constructor
14699 applied to it with the argument list [1, 2].
14701 On entry, DECL_INITIAL (decl1) should be NULL_TREE or error_mark_node,
14702 or may be a BLOCK if the function has been defined previously
14703 in this translation unit. On exit, DECL_INITIAL (decl1) will be
14704 error_mark_node if the function has never been defined, or
14705 a BLOCK if the function has been defined somewhere. */
14707 bool
14708 start_preparsed_function (tree decl1, tree attrs, int flags)
14710 tree ctype = NULL_TREE;
14711 tree fntype;
14712 tree restype;
14713 int doing_friend = 0;
14714 cp_binding_level *bl;
14715 tree current_function_parms;
14716 struct c_fileinfo *finfo
14717 = get_fileinfo (LOCATION_FILE (DECL_SOURCE_LOCATION (decl1)));
14718 bool honor_interface;
14720 /* Sanity check. */
14721 gcc_assert (VOID_TYPE_P (TREE_VALUE (void_list_node)));
14722 gcc_assert (TREE_CHAIN (void_list_node) == NULL_TREE);
14724 fntype = TREE_TYPE (decl1);
14725 if (TREE_CODE (fntype) == METHOD_TYPE)
14726 ctype = TYPE_METHOD_BASETYPE (fntype);
14728 /* ISO C++ 11.4/5. A friend function defined in a class is in
14729 the (lexical) scope of the class in which it is defined. */
14730 if (!ctype && DECL_FRIEND_P (decl1))
14732 ctype = DECL_FRIEND_CONTEXT (decl1);
14734 /* CTYPE could be null here if we're dealing with a template;
14735 for example, `inline friend float foo()' inside a template
14736 will have no CTYPE set. */
14737 if (ctype && TREE_CODE (ctype) != RECORD_TYPE)
14738 ctype = NULL_TREE;
14739 else
14740 doing_friend = 1;
14743 if (DECL_DECLARED_INLINE_P (decl1)
14744 && lookup_attribute ("noinline", attrs))
14745 warning_at (DECL_SOURCE_LOCATION (decl1), 0,
14746 "inline function %qD given attribute noinline", decl1);
14748 /* Handle gnu_inline attribute. */
14749 if (GNU_INLINE_P (decl1))
14751 DECL_EXTERNAL (decl1) = 1;
14752 DECL_NOT_REALLY_EXTERN (decl1) = 0;
14753 DECL_INTERFACE_KNOWN (decl1) = 1;
14754 DECL_DISREGARD_INLINE_LIMITS (decl1) = 1;
14757 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl1))
14758 /* This is a constructor, we must ensure that any default args
14759 introduced by this definition are propagated to the clones
14760 now. The clones are used directly in overload resolution. */
14761 adjust_clone_args (decl1);
14763 /* Sometimes we don't notice that a function is a static member, and
14764 build a METHOD_TYPE for it. Fix that up now. */
14765 gcc_assert (!(ctype != NULL_TREE && DECL_STATIC_FUNCTION_P (decl1)
14766 && TREE_CODE (TREE_TYPE (decl1)) == METHOD_TYPE));
14768 /* Set up current_class_type, and enter the scope of the class, if
14769 appropriate. */
14770 if (ctype)
14771 push_nested_class (ctype);
14772 else if (DECL_STATIC_FUNCTION_P (decl1))
14773 push_nested_class (DECL_CONTEXT (decl1));
14775 /* Now that we have entered the scope of the class, we must restore
14776 the bindings for any template parameters surrounding DECL1, if it
14777 is an inline member template. (Order is important; consider the
14778 case where a template parameter has the same name as a field of
14779 the class.) It is not until after this point that
14780 PROCESSING_TEMPLATE_DECL is guaranteed to be set up correctly. */
14781 if (flags & SF_INCLASS_INLINE)
14782 maybe_begin_member_template_processing (decl1);
14784 /* Effective C++ rule 15. */
14785 if (warn_ecpp
14786 && DECL_OVERLOADED_OPERATOR_P (decl1) == NOP_EXPR
14787 && VOID_TYPE_P (TREE_TYPE (fntype)))
14788 warning (OPT_Weffc__, "%<operator=%> should return a reference to %<*this%>");
14790 /* Make the init_value nonzero so pushdecl knows this is not tentative.
14791 error_mark_node is replaced below (in poplevel) with the BLOCK. */
14792 if (!DECL_INITIAL (decl1))
14793 DECL_INITIAL (decl1) = error_mark_node;
14795 /* This function exists in static storage.
14796 (This does not mean `static' in the C sense!) */
14797 TREE_STATIC (decl1) = 1;
14799 /* We must call push_template_decl after current_class_type is set
14800 up. (If we are processing inline definitions after exiting a
14801 class scope, current_class_type will be NULL_TREE until set above
14802 by push_nested_class.) */
14803 if (processing_template_decl)
14805 tree newdecl1 = push_template_decl (decl1);
14806 if (newdecl1 == error_mark_node)
14808 if (ctype || DECL_STATIC_FUNCTION_P (decl1))
14809 pop_nested_class ();
14810 return false;
14812 decl1 = newdecl1;
14815 /* Make sure the parameter and return types are reasonable. When
14816 you declare a function, these types can be incomplete, but they
14817 must be complete when you define the function. */
14818 check_function_type (decl1, DECL_ARGUMENTS (decl1));
14820 /* Build the return declaration for the function. */
14821 restype = TREE_TYPE (fntype);
14823 if (DECL_RESULT (decl1) == NULL_TREE)
14825 tree resdecl;
14827 resdecl = build_decl (input_location, RESULT_DECL, 0, restype);
14828 DECL_ARTIFICIAL (resdecl) = 1;
14829 DECL_IGNORED_P (resdecl) = 1;
14830 DECL_RESULT (decl1) = resdecl;
14832 cp_apply_type_quals_to_decl (cp_type_quals (restype), resdecl);
14835 /* Record the decl so that the function name is defined.
14836 If we already have a decl for this name, and it is a FUNCTION_DECL,
14837 use the old decl. */
14838 if (!processing_template_decl && !(flags & SF_PRE_PARSED))
14840 /* A specialization is not used to guide overload resolution. */
14841 if (!DECL_FUNCTION_MEMBER_P (decl1)
14842 && !(DECL_USE_TEMPLATE (decl1) &&
14843 PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl1))))
14845 tree olddecl = pushdecl (decl1);
14847 if (olddecl == error_mark_node)
14848 /* If something went wrong when registering the declaration,
14849 use DECL1; we have to have a FUNCTION_DECL to use when
14850 parsing the body of the function. */
14852 else
14854 /* Otherwise, OLDDECL is either a previous declaration
14855 of the same function or DECL1 itself. */
14857 if (warn_missing_declarations
14858 && olddecl == decl1
14859 && !DECL_MAIN_P (decl1)
14860 && TREE_PUBLIC (decl1)
14861 && !DECL_DECLARED_INLINE_P (decl1))
14863 tree context;
14865 /* Check whether DECL1 is in an anonymous
14866 namespace. */
14867 for (context = DECL_CONTEXT (decl1);
14868 context;
14869 context = DECL_CONTEXT (context))
14871 if (TREE_CODE (context) == NAMESPACE_DECL
14872 && DECL_NAME (context) == NULL_TREE)
14873 break;
14876 if (context == NULL)
14877 warning_at (DECL_SOURCE_LOCATION (decl1),
14878 OPT_Wmissing_declarations,
14879 "no previous declaration for %qD", decl1);
14882 decl1 = olddecl;
14885 else
14887 /* We need to set the DECL_CONTEXT. */
14888 if (!DECL_CONTEXT (decl1) && DECL_TEMPLATE_INFO (decl1))
14889 DECL_CONTEXT (decl1) = DECL_CONTEXT (DECL_TI_TEMPLATE (decl1));
14891 fntype = TREE_TYPE (decl1);
14892 restype = TREE_TYPE (fntype);
14894 /* If #pragma weak applies, mark the decl appropriately now.
14895 The pragma only applies to global functions. Because
14896 determining whether or not the #pragma applies involves
14897 computing the mangled name for the declaration, we cannot
14898 apply the pragma until after we have merged this declaration
14899 with any previous declarations; if the original declaration
14900 has a linkage specification, that specification applies to
14901 the definition as well, and may affect the mangled name. */
14902 if (DECL_FILE_SCOPE_P (decl1))
14903 maybe_apply_pragma_weak (decl1);
14906 /* We are now in the scope of the function being defined. */
14907 current_function_decl = decl1;
14909 /* Save the parm names or decls from this function's declarator
14910 where store_parm_decls will find them. */
14911 current_function_parms = DECL_ARGUMENTS (decl1);
14913 /* Let the user know we're compiling this function. */
14914 announce_function (decl1);
14916 gcc_assert (DECL_INITIAL (decl1));
14918 /* This function may already have been parsed, in which case just
14919 return; our caller will skip over the body without parsing. */
14920 if (DECL_INITIAL (decl1) != error_mark_node)
14921 return true;
14923 /* Initialize RTL machinery. We cannot do this until
14924 CURRENT_FUNCTION_DECL and DECL_RESULT are set up. We do this
14925 even when processing a template; this is how we get
14926 CFUN set up, and our per-function variables initialized.
14927 FIXME factor out the non-RTL stuff. */
14928 bl = current_binding_level;
14929 allocate_struct_function (decl1, processing_template_decl);
14931 /* Initialize the language data structures. Whenever we start
14932 a new function, we destroy temporaries in the usual way. */
14933 cfun->language = ggc_cleared_alloc<language_function> ();
14934 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
14935 current_binding_level = bl;
14937 if (!processing_template_decl && type_uses_auto (restype))
14939 FNDECL_USED_AUTO (decl1) = true;
14940 current_function_auto_return_pattern = restype;
14943 /* Start the statement-tree, start the tree now. */
14944 DECL_SAVED_TREE (decl1) = push_stmt_list ();
14946 /* If we are (erroneously) defining a function that we have already
14947 defined before, wipe out what we knew before. */
14948 if (!DECL_PENDING_INLINE_P (decl1))
14949 DECL_SAVED_FUNCTION_DATA (decl1) = NULL;
14951 if (ctype && !doing_friend && !DECL_STATIC_FUNCTION_P (decl1))
14953 /* We know that this was set up by `grokclassfn'. We do not
14954 wait until `store_parm_decls', since evil parse errors may
14955 never get us to that point. Here we keep the consistency
14956 between `current_class_type' and `current_class_ptr'. */
14957 tree t = DECL_ARGUMENTS (decl1);
14959 gcc_assert (t != NULL_TREE && TREE_CODE (t) == PARM_DECL);
14960 gcc_assert (TYPE_PTR_P (TREE_TYPE (t)));
14962 cp_function_chain->x_current_class_ref
14963 = cp_build_indirect_ref (t, RO_NULL, tf_warning_or_error);
14964 /* Set this second to avoid shortcut in cp_build_indirect_ref. */
14965 cp_function_chain->x_current_class_ptr = t;
14967 /* Constructors and destructors need to know whether they're "in
14968 charge" of initializing virtual base classes. */
14969 t = DECL_CHAIN (t);
14970 if (DECL_HAS_IN_CHARGE_PARM_P (decl1))
14972 current_in_charge_parm = t;
14973 t = DECL_CHAIN (t);
14975 if (DECL_HAS_VTT_PARM_P (decl1))
14977 gcc_assert (DECL_NAME (t) == vtt_parm_identifier);
14978 current_vtt_parm = t;
14982 honor_interface = (!DECL_TEMPLATE_INSTANTIATION (decl1)
14983 /* Implicitly-defined methods (like the
14984 destructor for a class in which no destructor
14985 is explicitly declared) must not be defined
14986 until their definition is needed. So, we
14987 ignore interface specifications for
14988 compiler-generated functions. */
14989 && !DECL_ARTIFICIAL (decl1));
14991 if (processing_template_decl)
14992 /* Don't mess with interface flags. */;
14993 else if (DECL_INTERFACE_KNOWN (decl1))
14995 tree ctx = decl_function_context (decl1);
14997 if (DECL_NOT_REALLY_EXTERN (decl1))
14998 DECL_EXTERNAL (decl1) = 0;
15000 if (ctx != NULL_TREE && vague_linkage_p (ctx))
15001 /* This is a function in a local class in an extern inline
15002 or template function. */
15003 comdat_linkage (decl1);
15005 /* If this function belongs to an interface, it is public.
15006 If it belongs to someone else's interface, it is also external.
15007 This only affects inlines and template instantiations. */
15008 else if (!finfo->interface_unknown && honor_interface)
15010 if (DECL_DECLARED_INLINE_P (decl1)
15011 || DECL_TEMPLATE_INSTANTIATION (decl1))
15013 DECL_EXTERNAL (decl1)
15014 = (finfo->interface_only
15015 || (DECL_DECLARED_INLINE_P (decl1)
15016 && ! flag_implement_inlines
15017 && !DECL_VINDEX (decl1)));
15019 /* For WIN32 we also want to put these in linkonce sections. */
15020 maybe_make_one_only (decl1);
15022 else
15023 DECL_EXTERNAL (decl1) = 0;
15024 DECL_INTERFACE_KNOWN (decl1) = 1;
15025 /* If this function is in an interface implemented in this file,
15026 make sure that the back end knows to emit this function
15027 here. */
15028 if (!DECL_EXTERNAL (decl1))
15029 mark_needed (decl1);
15031 else if (finfo->interface_unknown && finfo->interface_only
15032 && honor_interface)
15034 /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma
15035 interface, we will have both finfo->interface_unknown and
15036 finfo->interface_only set. In that case, we don't want to
15037 use the normal heuristics because someone will supply a
15038 #pragma implementation elsewhere, and deducing it here would
15039 produce a conflict. */
15040 comdat_linkage (decl1);
15041 DECL_EXTERNAL (decl1) = 0;
15042 DECL_INTERFACE_KNOWN (decl1) = 1;
15043 DECL_DEFER_OUTPUT (decl1) = 1;
15045 else
15047 /* This is a definition, not a reference.
15048 So clear DECL_EXTERNAL, unless this is a GNU extern inline. */
15049 if (!GNU_INLINE_P (decl1))
15050 DECL_EXTERNAL (decl1) = 0;
15052 if ((DECL_DECLARED_INLINE_P (decl1)
15053 || DECL_TEMPLATE_INSTANTIATION (decl1))
15054 && ! DECL_INTERFACE_KNOWN (decl1))
15055 DECL_DEFER_OUTPUT (decl1) = 1;
15056 else
15057 DECL_INTERFACE_KNOWN (decl1) = 1;
15060 /* Determine the ELF visibility attribute for the function. We must not
15061 do this before calling "pushdecl", as we must allow "duplicate_decls"
15062 to merge any attributes appropriately. We also need to wait until
15063 linkage is set. */
15064 if (!DECL_CLONED_FUNCTION_P (decl1))
15065 determine_visibility (decl1);
15067 if (!processing_template_decl)
15068 maybe_instantiate_noexcept (decl1);
15070 begin_scope (sk_function_parms, decl1);
15072 ++function_depth;
15074 if (DECL_DESTRUCTOR_P (decl1)
15075 || (DECL_CONSTRUCTOR_P (decl1)
15076 && targetm.cxx.cdtor_returns_this ()))
15078 cdtor_label = create_artificial_label (input_location);
15079 LABEL_DECL_CDTOR (cdtor_label) = true;
15082 start_fname_decls ();
15084 store_parm_decls (current_function_parms);
15086 if (!processing_template_decl
15087 && (flag_lifetime_dse > 1)
15088 && DECL_CONSTRUCTOR_P (decl1)
15089 && !DECL_CLONED_FUNCTION_P (decl1)
15090 /* Clobbering an empty base is harmful if it overlays real data. */
15091 && !is_empty_class (current_class_type)
15092 /* We can't clobber safely for an implicitly-defined default constructor
15093 because part of the initialization might happen before we enter the
15094 constructor, via AGGR_INIT_ZERO_FIRST (c++/68006). */
15095 && !implicit_default_ctor_p (decl1))
15096 finish_expr_stmt (build_clobber_this ());
15098 if (!processing_template_decl
15099 && DECL_CONSTRUCTOR_P (decl1)
15100 && sanitize_flags_p (SANITIZE_VPTR)
15101 && !DECL_CLONED_FUNCTION_P (decl1)
15102 && !implicit_default_ctor_p (decl1))
15103 cp_ubsan_maybe_initialize_vtbl_ptrs (current_class_ptr);
15105 start_lambda_scope (decl1);
15107 return true;
15111 /* Like start_preparsed_function, except that instead of a
15112 FUNCTION_DECL, this function takes DECLSPECS and DECLARATOR.
15114 Returns true on success. If the DECLARATOR is not suitable
15115 for a function, we return false, which tells the parser to
15116 skip the entire function. */
15118 bool
15119 start_function (cp_decl_specifier_seq *declspecs,
15120 const cp_declarator *declarator,
15121 tree attrs)
15123 tree decl1;
15125 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, &attrs);
15126 invoke_plugin_callbacks (PLUGIN_START_PARSE_FUNCTION, decl1);
15127 if (decl1 == error_mark_node)
15128 return false;
15129 /* If the declarator is not suitable for a function definition,
15130 cause a syntax error. */
15131 if (decl1 == NULL_TREE || TREE_CODE (decl1) != FUNCTION_DECL)
15133 error ("invalid function declaration");
15134 return false;
15137 if (DECL_MAIN_P (decl1))
15138 /* main must return int. grokfndecl should have corrected it
15139 (and issued a diagnostic) if the user got it wrong. */
15140 gcc_assert (same_type_p (TREE_TYPE (TREE_TYPE (decl1)),
15141 integer_type_node));
15143 return start_preparsed_function (decl1, attrs, /*flags=*/SF_DEFAULT);
15146 /* Returns true iff an EH_SPEC_BLOCK should be created in the body of
15147 FN. */
15149 static bool
15150 use_eh_spec_block (tree fn)
15152 return (flag_exceptions && flag_enforce_eh_specs
15153 && !processing_template_decl
15154 && !type_throw_all_p (TREE_TYPE (fn))
15155 /* We insert the EH_SPEC_BLOCK only in the original
15156 function; then, it is copied automatically to the
15157 clones. */
15158 && !DECL_CLONED_FUNCTION_P (fn)
15159 /* Implicitly-generated constructors and destructors have
15160 exception specifications. However, those specifications
15161 are the union of the possible exceptions specified by the
15162 constructors/destructors for bases and members, so no
15163 unallowed exception will ever reach this function. By
15164 not creating the EH_SPEC_BLOCK we save a little memory,
15165 and we avoid spurious warnings about unreachable
15166 code. */
15167 && !DECL_DEFAULTED_FN (fn));
15170 /* Store the parameter declarations into the current function declaration.
15171 This is called after parsing the parameter declarations, before
15172 digesting the body of the function.
15174 Also install to binding contour return value identifier, if any. */
15176 static void
15177 store_parm_decls (tree current_function_parms)
15179 tree fndecl = current_function_decl;
15180 tree parm;
15182 /* This is a chain of any other decls that came in among the parm
15183 declarations. If a parm is declared with enum {foo, bar} x;
15184 then CONST_DECLs for foo and bar are put here. */
15185 tree nonparms = NULL_TREE;
15187 if (current_function_parms)
15189 /* This case is when the function was defined with an ANSI prototype.
15190 The parms already have decls, so we need not do anything here
15191 except record them as in effect
15192 and complain if any redundant old-style parm decls were written. */
15194 tree specparms = current_function_parms;
15195 tree next;
15197 /* Must clear this because it might contain TYPE_DECLs declared
15198 at class level. */
15199 current_binding_level->names = NULL;
15201 /* If we're doing semantic analysis, then we'll call pushdecl
15202 for each of these. We must do them in reverse order so that
15203 they end in the correct forward order. */
15204 specparms = nreverse (specparms);
15206 for (parm = specparms; parm; parm = next)
15208 next = DECL_CHAIN (parm);
15209 if (TREE_CODE (parm) == PARM_DECL)
15210 pushdecl (parm);
15211 else
15213 /* If we find an enum constant or a type tag,
15214 put it aside for the moment. */
15215 TREE_CHAIN (parm) = NULL_TREE;
15216 nonparms = chainon (nonparms, parm);
15220 /* Get the decls in their original chain order and record in the
15221 function. This is all and only the PARM_DECLs that were
15222 pushed into scope by the loop above. */
15223 DECL_ARGUMENTS (fndecl) = get_local_decls ();
15225 else
15226 DECL_ARGUMENTS (fndecl) = NULL_TREE;
15228 /* Now store the final chain of decls for the arguments
15229 as the decl-chain of the current lexical scope.
15230 Put the enumerators in as well, at the front so that
15231 DECL_ARGUMENTS is not modified. */
15232 current_binding_level->names = chainon (nonparms, DECL_ARGUMENTS (fndecl));
15234 if (use_eh_spec_block (current_function_decl))
15235 current_eh_spec_block = begin_eh_spec_block ();
15239 /* We have finished doing semantic analysis on DECL, but have not yet
15240 generated RTL for its body. Save away our current state, so that
15241 when we want to generate RTL later we know what to do. */
15243 static void
15244 save_function_data (tree decl)
15246 struct language_function *f;
15248 /* Save the language-specific per-function data so that we can
15249 get it back when we really expand this function. */
15250 gcc_assert (!DECL_PENDING_INLINE_P (decl));
15252 /* Make a copy. */
15253 f = ggc_alloc<language_function> ();
15254 memcpy (f, cp_function_chain, sizeof (struct language_function));
15255 DECL_SAVED_FUNCTION_DATA (decl) = f;
15257 /* Clear out the bits we don't need. */
15258 f->base.x_stmt_tree.x_cur_stmt_list = NULL;
15259 f->bindings = NULL;
15260 f->x_local_names = NULL;
15261 f->base.local_typedefs = NULL;
15265 /* Set the return value of the constructor (if present). */
15267 static void
15268 finish_constructor_body (void)
15270 tree val;
15271 tree exprstmt;
15273 if (targetm.cxx.cdtor_returns_this ())
15275 /* Any return from a constructor will end up here. */
15276 add_stmt (build_stmt (input_location, LABEL_EXPR, cdtor_label));
15278 val = DECL_ARGUMENTS (current_function_decl);
15279 val = build2 (MODIFY_EXPR, TREE_TYPE (val),
15280 DECL_RESULT (current_function_decl), val);
15281 /* Return the address of the object. */
15282 exprstmt = build_stmt (input_location, RETURN_EXPR, val);
15283 add_stmt (exprstmt);
15287 /* Do all the processing for the beginning of a destructor; set up the
15288 vtable pointers and cleanups for bases and members. */
15290 static void
15291 begin_destructor_body (void)
15293 tree compound_stmt;
15295 /* If the CURRENT_CLASS_TYPE is incomplete, we will have already
15296 issued an error message. We still want to try to process the
15297 body of the function, but initialize_vtbl_ptrs will crash if
15298 TYPE_BINFO is NULL. */
15299 if (COMPLETE_TYPE_P (current_class_type))
15301 compound_stmt = begin_compound_stmt (0);
15302 /* Make all virtual function table pointers in non-virtual base
15303 classes point to CURRENT_CLASS_TYPE's virtual function
15304 tables. */
15305 initialize_vtbl_ptrs (current_class_ptr);
15306 finish_compound_stmt (compound_stmt);
15308 if (flag_lifetime_dse
15309 /* Clobbering an empty base is harmful if it overlays real data. */
15310 && !is_empty_class (current_class_type))
15311 finish_decl_cleanup (NULL_TREE, build_clobber_this ());
15313 /* And insert cleanups for our bases and members so that they
15314 will be properly destroyed if we throw. */
15315 push_base_cleanups ();
15319 /* At the end of every destructor we generate code to delete the object if
15320 necessary. Do that now. */
15322 static void
15323 finish_destructor_body (void)
15325 tree exprstmt;
15327 /* Any return from a destructor will end up here; that way all base
15328 and member cleanups will be run when the function returns. */
15329 add_stmt (build_stmt (input_location, LABEL_EXPR, cdtor_label));
15331 if (targetm.cxx.cdtor_returns_this ())
15333 tree val;
15335 val = DECL_ARGUMENTS (current_function_decl);
15336 val = build2 (MODIFY_EXPR, TREE_TYPE (val),
15337 DECL_RESULT (current_function_decl), val);
15338 /* Return the address of the object. */
15339 exprstmt = build_stmt (input_location, RETURN_EXPR, val);
15340 add_stmt (exprstmt);
15344 /* Do the necessary processing for the beginning of a function body, which
15345 in this case includes member-initializers, but not the catch clauses of
15346 a function-try-block. Currently, this means opening a binding level
15347 for the member-initializers (in a ctor), member cleanups (in a dtor),
15348 and capture proxies (in a lambda operator()). */
15350 tree
15351 begin_function_body (void)
15353 tree stmt;
15355 if (! FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
15356 return NULL_TREE;
15358 if (processing_template_decl)
15359 /* Do nothing now. */;
15360 else
15361 /* Always keep the BLOCK node associated with the outermost pair of
15362 curly braces of a function. These are needed for correct
15363 operation of dwarfout.c. */
15364 keep_next_level (true);
15366 stmt = begin_compound_stmt (BCS_FN_BODY);
15368 if (processing_template_decl)
15369 /* Do nothing now. */;
15370 else if (DECL_DESTRUCTOR_P (current_function_decl))
15371 begin_destructor_body ();
15373 return stmt;
15376 /* Do the processing for the end of a function body. Currently, this means
15377 closing out the cleanups for fully-constructed bases and members, and in
15378 the case of the destructor, deleting the object if desired. Again, this
15379 is only meaningful for [cd]tors, since they are the only functions where
15380 there is a significant distinction between the main body and any
15381 function catch clauses. Handling, say, main() return semantics here
15382 would be wrong, as flowing off the end of a function catch clause for
15383 main() would also need to return 0. */
15385 void
15386 finish_function_body (tree compstmt)
15388 if (compstmt == NULL_TREE)
15389 return;
15391 /* Close the block. */
15392 finish_compound_stmt (compstmt);
15394 if (processing_template_decl)
15395 /* Do nothing now. */;
15396 else if (DECL_CONSTRUCTOR_P (current_function_decl))
15397 finish_constructor_body ();
15398 else if (DECL_DESTRUCTOR_P (current_function_decl))
15399 finish_destructor_body ();
15402 /* Given a function, returns the BLOCK corresponding to the outermost level
15403 of curly braces, skipping the artificial block created for constructor
15404 initializers. */
15406 tree
15407 outer_curly_brace_block (tree fndecl)
15409 tree block = DECL_INITIAL (fndecl);
15410 if (BLOCK_OUTER_CURLY_BRACE_P (block))
15411 return block;
15412 block = BLOCK_SUBBLOCKS (block);
15413 if (BLOCK_OUTER_CURLY_BRACE_P (block))
15414 return block;
15415 block = BLOCK_SUBBLOCKS (block);
15416 gcc_assert (BLOCK_OUTER_CURLY_BRACE_P (block));
15417 return block;
15420 /* If FNDECL is a class's key method, add the class to the list of
15421 keyed classes that should be emitted. */
15423 static void
15424 record_key_method_defined (tree fndecl)
15426 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fndecl)
15427 && DECL_VIRTUAL_P (fndecl)
15428 && !processing_template_decl)
15430 tree fnclass = DECL_CONTEXT (fndecl);
15431 if (fndecl == CLASSTYPE_KEY_METHOD (fnclass))
15432 vec_safe_push (keyed_classes, fnclass);
15436 /* Subroutine of finish_function.
15437 Save the body of constexpr functions for possible
15438 future compile time evaluation. */
15440 static void
15441 maybe_save_function_definition (tree fun)
15443 if (!processing_template_decl
15444 && DECL_DECLARED_CONSTEXPR_P (fun)
15445 && !cp_function_chain->invalid_constexpr
15446 && !DECL_CLONED_FUNCTION_P (fun))
15447 register_constexpr_fundef (fun, DECL_SAVED_TREE (fun));
15450 /* Finish up a function declaration and compile that function
15451 all the way to assembler language output. The free the storage
15452 for the function definition.
15454 FLAGS is a bitwise or of the following values:
15455 2 - INCLASS_INLINE
15456 We just finished processing the body of an in-class inline
15457 function definition. (This processing will have taken place
15458 after the class definition is complete.) */
15460 tree
15461 finish_function (int flags)
15463 tree fndecl = current_function_decl;
15464 tree fntype, ctype = NULL_TREE;
15465 int inclass_inline = (flags & 2) != 0;
15467 /* When we get some parse errors, we can end up without a
15468 current_function_decl, so cope. */
15469 if (fndecl == NULL_TREE)
15470 return error_mark_node;
15472 finish_lambda_scope ();
15474 if (c_dialect_objc ())
15475 objc_finish_function ();
15477 record_key_method_defined (fndecl);
15479 fntype = TREE_TYPE (fndecl);
15481 /* TREE_READONLY (fndecl) = 1;
15482 This caused &foo to be of type ptr-to-const-function
15483 which then got a warning when stored in a ptr-to-function variable. */
15485 gcc_assert (building_stmt_list_p ());
15486 /* The current function is being defined, so its DECL_INITIAL should
15487 be set, and unless there's a multiple definition, it should be
15488 error_mark_node. */
15489 gcc_assert (DECL_INITIAL (fndecl) == error_mark_node);
15491 /* For a cloned function, we've already got all the code we need;
15492 there's no need to add any extra bits. */
15493 if (!DECL_CLONED_FUNCTION_P (fndecl))
15495 /* Make it so that `main' always returns 0 by default. */
15496 if (DECL_MAIN_P (current_function_decl))
15497 finish_return_stmt (integer_zero_node);
15499 if (use_eh_spec_block (current_function_decl))
15500 finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
15501 (TREE_TYPE (current_function_decl)),
15502 current_eh_spec_block);
15505 /* If we're saving up tree structure, tie off the function now. */
15506 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
15508 if (fn_contains_cilk_spawn_p (cfun) && !processing_template_decl)
15509 cfun->cilk_frame_decl = insert_cilk_frame (fndecl);
15511 finish_fname_decls ();
15513 /* If this function can't throw any exceptions, remember that. */
15514 if (!processing_template_decl
15515 && !cp_function_chain->can_throw
15516 && !flag_non_call_exceptions
15517 && !decl_replaceable_p (fndecl))
15518 TREE_NOTHROW (fndecl) = 1;
15520 /* This must come after expand_function_end because cleanups might
15521 have declarations (from inline functions) that need to go into
15522 this function's blocks. */
15524 /* If the current binding level isn't the outermost binding level
15525 for this function, either there is a bug, or we have experienced
15526 syntax errors and the statement tree is malformed. */
15527 if (current_binding_level->kind != sk_function_parms)
15529 /* Make sure we have already experienced errors. */
15530 gcc_assert (errorcount);
15532 /* Throw away the broken statement tree and extra binding
15533 levels. */
15534 DECL_SAVED_TREE (fndecl) = alloc_stmt_list ();
15536 while (current_binding_level->kind != sk_function_parms)
15538 if (current_binding_level->kind == sk_class)
15539 pop_nested_class ();
15540 else
15541 poplevel (0, 0, 0);
15544 poplevel (1, 0, 1);
15546 /* Statements should always be full-expressions at the outermost set
15547 of curly braces for a function. */
15548 gcc_assert (stmts_are_full_exprs_p ());
15550 /* If there are no return statements in a function with auto return type,
15551 the return type is void. But if the declared type is something like
15552 auto*, this is an error. */
15553 if (!processing_template_decl && FNDECL_USED_AUTO (fndecl)
15554 && TREE_TYPE (fntype) == current_function_auto_return_pattern)
15556 if (is_auto (current_function_auto_return_pattern))
15558 apply_deduced_return_type (fndecl, void_type_node);
15559 fntype = TREE_TYPE (fndecl);
15561 else if (!current_function_returns_value
15562 && !current_function_returns_null)
15564 error ("no return statements in function returning %qT",
15565 current_function_auto_return_pattern);
15566 inform (input_location, "only plain %<auto%> return type can be "
15567 "deduced to %<void%>");
15571 // If this is a concept, check that the definition is reasonable.
15572 if (DECL_DECLARED_CONCEPT_P (fndecl))
15573 check_function_concept (fndecl);
15575 /* Lambda closure members are implicitly constexpr if possible. */
15576 if (cxx_dialect >= cxx17
15577 && LAMBDA_TYPE_P (CP_DECL_CONTEXT (fndecl)))
15578 DECL_DECLARED_CONSTEXPR_P (fndecl)
15579 = ((processing_template_decl
15580 || is_valid_constexpr_fn (fndecl, /*complain*/false))
15581 && potential_constant_expression (DECL_SAVED_TREE (fndecl)));
15583 /* Save constexpr function body before it gets munged by
15584 the NRV transformation. */
15585 maybe_save_function_definition (fndecl);
15587 /* Invoke the pre-genericize plugin before we start munging things. */
15588 if (!processing_template_decl)
15589 invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE, fndecl);
15591 /* Perform delayed folding before NRV transformation. */
15592 if (!processing_template_decl)
15593 cp_fold_function (fndecl);
15595 /* Set up the named return value optimization, if we can. Candidate
15596 variables are selected in check_return_expr. */
15597 if (current_function_return_value)
15599 tree r = current_function_return_value;
15600 tree outer;
15602 if (r != error_mark_node
15603 /* This is only worth doing for fns that return in memory--and
15604 simpler, since we don't have to worry about promoted modes. */
15605 && aggregate_value_p (TREE_TYPE (TREE_TYPE (fndecl)), fndecl)
15606 /* Only allow this for variables declared in the outer scope of
15607 the function so we know that their lifetime always ends with a
15608 return; see g++.dg/opt/nrv6.C. We could be more flexible if
15609 we were to do this optimization in tree-ssa. */
15610 && (outer = outer_curly_brace_block (fndecl))
15611 && chain_member (r, BLOCK_VARS (outer)))
15612 finalize_nrv (&DECL_SAVED_TREE (fndecl), r, DECL_RESULT (fndecl));
15614 current_function_return_value = NULL_TREE;
15617 /* Remember that we were in class scope. */
15618 if (current_class_name)
15619 ctype = current_class_type;
15621 /* Must mark the RESULT_DECL as being in this function. */
15622 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
15624 /* Set the BLOCK_SUPERCONTEXT of the outermost function scope to point
15625 to the FUNCTION_DECL node itself. */
15626 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
15628 /* Save away current state, if appropriate. */
15629 if (!processing_template_decl)
15630 save_function_data (fndecl);
15632 /* Complain if there's just no return statement. */
15633 if (warn_return_type
15634 && !VOID_TYPE_P (TREE_TYPE (fntype))
15635 && !dependent_type_p (TREE_TYPE (fntype))
15636 && !current_function_returns_value && !current_function_returns_null
15637 /* Don't complain if we abort or throw. */
15638 && !current_function_returns_abnormally
15639 /* Don't complain if there's an infinite loop. */
15640 && !current_function_infinite_loop
15641 /* Don't complain if we are declared noreturn. */
15642 && !TREE_THIS_VOLATILE (fndecl)
15643 && !DECL_NAME (DECL_RESULT (fndecl))
15644 && !TREE_NO_WARNING (fndecl)
15645 /* Structor return values (if any) are set by the compiler. */
15646 && !DECL_CONSTRUCTOR_P (fndecl)
15647 && !DECL_DESTRUCTOR_P (fndecl)
15648 && targetm.warn_func_return (fndecl))
15650 warning (OPT_Wreturn_type,
15651 "no return statement in function returning non-void");
15652 TREE_NO_WARNING (fndecl) = 1;
15655 /* Store the end of the function, so that we get good line number
15656 info for the epilogue. */
15657 cfun->function_end_locus = input_location;
15659 /* Complain about parameters that are only set, but never otherwise used. */
15660 if (warn_unused_but_set_parameter
15661 && !processing_template_decl
15662 && errorcount == unused_but_set_errorcount
15663 && !DECL_CLONED_FUNCTION_P (fndecl))
15665 tree decl;
15667 for (decl = DECL_ARGUMENTS (fndecl);
15668 decl;
15669 decl = DECL_CHAIN (decl))
15670 if (TREE_USED (decl)
15671 && TREE_CODE (decl) == PARM_DECL
15672 && !DECL_READ_P (decl)
15673 && DECL_NAME (decl)
15674 && !DECL_ARTIFICIAL (decl)
15675 && !TREE_NO_WARNING (decl)
15676 && !DECL_IN_SYSTEM_HEADER (decl)
15677 && TREE_TYPE (decl) != error_mark_node
15678 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
15679 && (!CLASS_TYPE_P (TREE_TYPE (decl))
15680 || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl))))
15681 warning_at (DECL_SOURCE_LOCATION (decl),
15682 OPT_Wunused_but_set_parameter,
15683 "parameter %qD set but not used", decl);
15684 unused_but_set_errorcount = errorcount;
15687 /* Complain about locally defined typedefs that are not used in this
15688 function. */
15689 maybe_warn_unused_local_typedefs ();
15691 /* Possibly warn about unused parameters. */
15692 if (warn_unused_parameter
15693 && !processing_template_decl
15694 && !DECL_CLONED_FUNCTION_P (fndecl))
15695 do_warn_unused_parameter (fndecl);
15697 /* Genericize before inlining. */
15698 if (!processing_template_decl)
15700 struct language_function *f = DECL_SAVED_FUNCTION_DATA (fndecl);
15701 cp_genericize (fndecl);
15702 /* Clear out the bits we don't need. */
15703 f->x_current_class_ptr = NULL;
15704 f->x_current_class_ref = NULL;
15705 f->x_eh_spec_block = NULL;
15706 f->x_in_charge_parm = NULL;
15707 f->x_vtt_parm = NULL;
15708 f->x_return_value = NULL;
15709 f->bindings = NULL;
15710 f->extern_decl_map = NULL;
15711 f->infinite_loops = NULL;
15713 /* Clear out the bits we don't need. */
15714 local_names = NULL;
15716 /* We're leaving the context of this function, so zap cfun. It's still in
15717 DECL_STRUCT_FUNCTION, and we'll restore it in tree_rest_of_compilation. */
15718 set_cfun (NULL);
15719 current_function_decl = NULL;
15721 /* If this is an in-class inline definition, we may have to pop the
15722 bindings for the template parameters that we added in
15723 maybe_begin_member_template_processing when start_function was
15724 called. */
15725 if (inclass_inline)
15726 maybe_end_member_template_processing ();
15728 /* Leave the scope of the class. */
15729 if (ctype)
15730 pop_nested_class ();
15732 --function_depth;
15734 /* Clean up. */
15735 current_function_decl = NULL_TREE;
15737 invoke_plugin_callbacks (PLUGIN_FINISH_PARSE_FUNCTION, fndecl);
15738 return fndecl;
15741 /* Create the FUNCTION_DECL for a function definition.
15742 DECLSPECS and DECLARATOR are the parts of the declaration;
15743 they describe the return type and the name of the function,
15744 but twisted together in a fashion that parallels the syntax of C.
15746 This function creates a binding context for the function body
15747 as well as setting up the FUNCTION_DECL in current_function_decl.
15749 Returns a FUNCTION_DECL on success.
15751 If the DECLARATOR is not suitable for a function (it defines a datum
15752 instead), we return 0, which tells yyparse to report a parse error.
15754 May return void_type_node indicating that this method is actually
15755 a friend. See grokfield for more details.
15757 Came here with a `.pushlevel' .
15759 DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
15760 CHANGES TO CODE IN `grokfield'. */
15762 tree
15763 grokmethod (cp_decl_specifier_seq *declspecs,
15764 const cp_declarator *declarator, tree attrlist)
15766 tree fndecl = grokdeclarator (declarator, declspecs, MEMFUNCDEF, 0,
15767 &attrlist);
15769 if (fndecl == error_mark_node)
15770 return error_mark_node;
15772 if (fndecl == NULL || TREE_CODE (fndecl) != FUNCTION_DECL)
15774 error ("invalid member function declaration");
15775 return error_mark_node;
15778 if (attrlist)
15779 cplus_decl_attributes (&fndecl, attrlist, 0);
15781 /* Pass friends other than inline friend functions back. */
15782 if (fndecl == void_type_node)
15783 return fndecl;
15785 if (DECL_IN_AGGR_P (fndecl))
15787 if (DECL_CLASS_SCOPE_P (fndecl))
15788 error ("%qD is already defined in class %qT", fndecl,
15789 DECL_CONTEXT (fndecl));
15790 return error_mark_node;
15793 check_template_shadow (fndecl);
15795 if (TREE_PUBLIC (fndecl))
15796 DECL_COMDAT (fndecl) = 1;
15797 DECL_DECLARED_INLINE_P (fndecl) = 1;
15798 DECL_NO_INLINE_WARNING_P (fndecl) = 1;
15800 /* We process method specializations in finish_struct_1. */
15801 if (processing_template_decl && !DECL_TEMPLATE_SPECIALIZATION (fndecl))
15803 fndecl = push_template_decl (fndecl);
15804 if (fndecl == error_mark_node)
15805 return fndecl;
15808 if (! DECL_FRIEND_P (fndecl))
15810 if (DECL_CHAIN (fndecl))
15812 fndecl = copy_node (fndecl);
15813 TREE_CHAIN (fndecl) = NULL_TREE;
15817 cp_finish_decl (fndecl, NULL_TREE, false, NULL_TREE, 0);
15819 DECL_IN_AGGR_P (fndecl) = 1;
15820 return fndecl;
15824 /* VAR is a VAR_DECL. If its type is incomplete, remember VAR so that
15825 we can lay it out later, when and if its type becomes complete.
15827 Also handle constexpr variables where the initializer involves
15828 an unlowered PTRMEM_CST because the class isn't complete yet. */
15830 void
15831 maybe_register_incomplete_var (tree var)
15833 gcc_assert (VAR_P (var));
15835 /* Keep track of variables with incomplete types. */
15836 if (!processing_template_decl && TREE_TYPE (var) != error_mark_node
15837 && DECL_EXTERNAL (var))
15839 tree inner_type = TREE_TYPE (var);
15841 while (TREE_CODE (inner_type) == ARRAY_TYPE)
15842 inner_type = TREE_TYPE (inner_type);
15843 inner_type = TYPE_MAIN_VARIANT (inner_type);
15845 if ((!COMPLETE_TYPE_P (inner_type) && CLASS_TYPE_P (inner_type))
15846 /* RTTI TD entries are created while defining the type_info. */
15847 || (TYPE_LANG_SPECIFIC (inner_type)
15848 && TYPE_BEING_DEFINED (inner_type)))
15850 incomplete_var iv = {var, inner_type};
15851 vec_safe_push (incomplete_vars, iv);
15853 else if (!(DECL_LANG_SPECIFIC (var) && DECL_TEMPLATE_INFO (var))
15854 && decl_constant_var_p (var)
15855 && (TYPE_PTRMEM_P (inner_type) || CLASS_TYPE_P (inner_type)))
15857 /* When the outermost open class is complete we can resolve any
15858 pointers-to-members. */
15859 tree context = outermost_open_class ();
15860 incomplete_var iv = {var, context};
15861 vec_safe_push (incomplete_vars, iv);
15866 /* Called when a class type (given by TYPE) is defined. If there are
15867 any existing VAR_DECLs whose type has been completed by this
15868 declaration, update them now. */
15870 void
15871 complete_vars (tree type)
15873 unsigned ix;
15874 incomplete_var *iv;
15876 for (ix = 0; vec_safe_iterate (incomplete_vars, ix, &iv); )
15878 if (same_type_p (type, iv->incomplete_type))
15880 tree var = iv->decl;
15881 tree type = TREE_TYPE (var);
15883 if (type != error_mark_node
15884 && (TYPE_MAIN_VARIANT (strip_array_types (type))
15885 == iv->incomplete_type))
15887 /* Complete the type of the variable. The VAR_DECL itself
15888 will be laid out in expand_expr. */
15889 complete_type (type);
15890 cp_apply_type_quals_to_decl (cp_type_quals (type), var);
15893 /* Remove this entry from the list. */
15894 incomplete_vars->unordered_remove (ix);
15896 else
15897 ix++;
15900 /* Check for pending declarations which may have abstract type. */
15901 complete_type_check_abstract (type);
15904 /* If DECL is of a type which needs a cleanup, build and return an
15905 expression to perform that cleanup here. Return NULL_TREE if no
15906 cleanup need be done. DECL can also be a _REF when called from
15907 split_nonconstant_init_1. */
15909 tree
15910 cxx_maybe_build_cleanup (tree decl, tsubst_flags_t complain)
15912 tree type;
15913 tree attr;
15914 tree cleanup;
15916 /* Assume no cleanup is required. */
15917 cleanup = NULL_TREE;
15919 if (error_operand_p (decl))
15920 return cleanup;
15922 /* Handle "__attribute__((cleanup))". We run the cleanup function
15923 before the destructor since the destructor is what actually
15924 terminates the lifetime of the object. */
15925 if (DECL_P (decl))
15926 attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
15927 else
15928 attr = NULL_TREE;
15929 if (attr)
15931 tree id;
15932 tree fn;
15933 tree arg;
15935 /* Get the name specified by the user for the cleanup function. */
15936 id = TREE_VALUE (TREE_VALUE (attr));
15937 /* Look up the name to find the cleanup function to call. It is
15938 important to use lookup_name here because that is what is
15939 used in c-common.c:handle_cleanup_attribute when performing
15940 initial checks on the attribute. Note that those checks
15941 include ensuring that the function found is not an overloaded
15942 function, or an object with an overloaded call operator,
15943 etc.; we can rely on the fact that the function found is an
15944 ordinary FUNCTION_DECL. */
15945 fn = lookup_name (id);
15946 arg = build_address (decl);
15947 if (!mark_used (decl, complain) && !(complain & tf_error))
15948 return error_mark_node;
15949 cleanup = cp_build_function_call_nary (fn, complain, arg, NULL_TREE);
15950 if (cleanup == error_mark_node)
15951 return error_mark_node;
15953 /* Handle ordinary C++ destructors. */
15954 type = TREE_TYPE (decl);
15955 if (type_build_dtor_call (type))
15957 int flags = LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR;
15958 tree addr;
15959 tree call;
15961 if (TREE_CODE (type) == ARRAY_TYPE)
15962 addr = decl;
15963 else
15964 addr = build_address (decl);
15966 call = build_delete (TREE_TYPE (addr), addr,
15967 sfk_complete_destructor, flags, 0, complain);
15968 if (call == error_mark_node)
15969 cleanup = error_mark_node;
15970 else if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
15971 /* Discard the call. */;
15972 else if (cleanup)
15973 cleanup = cp_build_compound_expr (cleanup, call, complain);
15974 else
15975 cleanup = call;
15978 /* build_delete sets the location of the destructor call to the
15979 current location, even though the destructor is going to be
15980 called later, at the end of the current scope. This can lead to
15981 a "jumpy" behavior for users of debuggers when they step around
15982 the end of the block. So let's unset the location of the
15983 destructor call instead. */
15984 protected_set_expr_location (cleanup, UNKNOWN_LOCATION);
15986 if (cleanup
15987 && DECL_P (decl)
15988 && !lookup_attribute ("warn_unused", TYPE_ATTRIBUTES (TREE_TYPE (decl)))
15989 /* Treat objects with destructors as used; the destructor may do
15990 something substantive. */
15991 && !mark_used (decl, complain) && !(complain & tf_error))
15992 return error_mark_node;
15994 return cleanup;
15998 /* Return the FUNCTION_TYPE that corresponds to MEMFNTYPE, which can be a
15999 FUNCTION_DECL, METHOD_TYPE, FUNCTION_TYPE, pointer or reference to
16000 METHOD_TYPE or FUNCTION_TYPE, or pointer to member function. */
16002 tree
16003 static_fn_type (tree memfntype)
16005 tree fntype;
16006 tree args;
16008 if (TYPE_PTRMEMFUNC_P (memfntype))
16009 memfntype = TYPE_PTRMEMFUNC_FN_TYPE (memfntype);
16010 if (POINTER_TYPE_P (memfntype)
16011 || TREE_CODE (memfntype) == FUNCTION_DECL)
16012 memfntype = TREE_TYPE (memfntype);
16013 if (TREE_CODE (memfntype) == FUNCTION_TYPE)
16014 return memfntype;
16015 gcc_assert (TREE_CODE (memfntype) == METHOD_TYPE);
16016 args = TYPE_ARG_TYPES (memfntype);
16017 cp_ref_qualifier rqual = type_memfn_rqual (memfntype);
16018 fntype = build_function_type (TREE_TYPE (memfntype), TREE_CHAIN (args));
16019 fntype = apply_memfn_quals (fntype, type_memfn_quals (memfntype), rqual);
16020 fntype = (cp_build_type_attribute_variant
16021 (fntype, TYPE_ATTRIBUTES (memfntype)));
16022 fntype = (build_exception_variant
16023 (fntype, TYPE_RAISES_EXCEPTIONS (memfntype)));
16024 if (TYPE_HAS_LATE_RETURN_TYPE (memfntype))
16025 TYPE_HAS_LATE_RETURN_TYPE (fntype) = 1;
16026 return fntype;
16029 /* DECL was originally constructed as a non-static member function,
16030 but turned out to be static. Update it accordingly. */
16032 void
16033 revert_static_member_fn (tree decl)
16035 tree stype = static_fn_type (decl);
16036 cp_cv_quals quals = type_memfn_quals (stype);
16037 cp_ref_qualifier rqual = type_memfn_rqual (stype);
16039 if (quals != TYPE_UNQUALIFIED || rqual != REF_QUAL_NONE)
16040 stype = apply_memfn_quals (stype, TYPE_UNQUALIFIED, REF_QUAL_NONE);
16042 TREE_TYPE (decl) = stype;
16044 if (DECL_ARGUMENTS (decl))
16045 DECL_ARGUMENTS (decl) = DECL_CHAIN (DECL_ARGUMENTS (decl));
16046 DECL_STATIC_FUNCTION_P (decl) = 1;
16049 /* Return which tree structure is used by T, or TS_CP_GENERIC if T is
16050 one of the language-independent trees. */
16052 enum cp_tree_node_structure_enum
16053 cp_tree_node_structure (union lang_tree_node * t)
16055 switch (TREE_CODE (&t->generic))
16057 case DEFAULT_ARG: return TS_CP_DEFAULT_ARG;
16058 case DEFERRED_NOEXCEPT: return TS_CP_DEFERRED_NOEXCEPT;
16059 case IDENTIFIER_NODE: return TS_CP_IDENTIFIER;
16060 case OVERLOAD: return TS_CP_OVERLOAD;
16061 case TEMPLATE_PARM_INDEX: return TS_CP_TPI;
16062 case PTRMEM_CST: return TS_CP_PTRMEM;
16063 case BASELINK: return TS_CP_BASELINK;
16064 case TEMPLATE_DECL: return TS_CP_TEMPLATE_DECL;
16065 case STATIC_ASSERT: return TS_CP_STATIC_ASSERT;
16066 case ARGUMENT_PACK_SELECT: return TS_CP_ARGUMENT_PACK_SELECT;
16067 case TRAIT_EXPR: return TS_CP_TRAIT_EXPR;
16068 case LAMBDA_EXPR: return TS_CP_LAMBDA_EXPR;
16069 case TEMPLATE_INFO: return TS_CP_TEMPLATE_INFO;
16070 case CONSTRAINT_INFO: return TS_CP_CONSTRAINT_INFO;
16071 case USERDEF_LITERAL: return TS_CP_USERDEF_LITERAL;
16072 default: return TS_CP_GENERIC;
16076 /* Build the void_list_node (void_type_node having been created). */
16077 tree
16078 build_void_list_node (void)
16080 tree t = build_tree_list (NULL_TREE, void_type_node);
16081 return t;
16084 bool
16085 cp_missing_noreturn_ok_p (tree decl)
16087 /* A missing noreturn is ok for the `main' function. */
16088 return DECL_MAIN_P (decl);
16091 /* Return the decl used to identify the COMDAT group into which DECL should
16092 be placed. */
16094 tree
16095 cxx_comdat_group (tree decl)
16097 /* Virtual tables, construction virtual tables, and virtual table
16098 tables all go in a single COMDAT group, named after the primary
16099 virtual table. */
16100 if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl))
16101 decl = CLASSTYPE_VTABLES (DECL_CONTEXT (decl));
16102 /* For all other DECLs, the COMDAT group is the mangled name of the
16103 declaration itself. */
16104 else
16106 while (DECL_THUNK_P (decl))
16108 /* If TARGET_USE_LOCAL_THUNK_ALIAS_P, use_thunk puts the thunk
16109 into the same section as the target function. In that case
16110 we must return target's name. */
16111 tree target = THUNK_TARGET (decl);
16112 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (target)
16113 && DECL_SECTION_NAME (target) != NULL
16114 && DECL_ONE_ONLY (target))
16115 decl = target;
16116 else
16117 break;
16121 return decl;
16124 /* Returns the return type for FN as written by the user, which may include
16125 a placeholder for a deduced return type. */
16127 tree
16128 fndecl_declared_return_type (tree fn)
16130 fn = STRIP_TEMPLATE (fn);
16131 if (FNDECL_USED_AUTO (fn))
16133 struct language_function *f = NULL;
16134 if (DECL_STRUCT_FUNCTION (fn))
16135 f = DECL_STRUCT_FUNCTION (fn)->language;
16136 if (f == NULL)
16137 f = DECL_SAVED_FUNCTION_DATA (fn);
16138 return f->x_auto_return_pattern;
16140 return TREE_TYPE (TREE_TYPE (fn));
16143 /* Returns true iff DECL was declared with an auto type and it has
16144 not yet been deduced to a real type. */
16146 bool
16147 undeduced_auto_decl (tree decl)
16149 if (cxx_dialect < cxx11)
16150 return false;
16151 return type_uses_auto (TREE_TYPE (decl));
16154 /* Complain if DECL has an undeduced return type. */
16156 bool
16157 require_deduced_type (tree decl, tsubst_flags_t complain)
16159 if (undeduced_auto_decl (decl))
16161 if (complain & tf_error)
16162 error ("use of %qD before deduction of %<auto%>", decl);
16163 return false;
16165 return true;
16168 #include "gt-cp-decl.h"