gcc/cp/ChangeLog:
[official-gcc.git] / gcc / cp / decl.c
blob44aefd82b73eeaf12bd5cb91695692843632a96e
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"
54 /* Possible cases of bad specifiers type used by bad_specifiers. */
55 enum bad_spec_place {
56 BSP_VAR, /* variable */
57 BSP_PARM, /* parameter */
58 BSP_TYPE, /* type */
59 BSP_FIELD /* field */
62 static const char *redeclaration_error_message (tree, tree);
64 static int decl_jump_unsafe (tree);
65 static void require_complete_types_for_parms (tree);
66 static int ambi_op_p (enum tree_code);
67 static int unary_op_p (enum tree_code);
68 static void push_local_name (tree);
69 static tree grok_reference_init (tree, tree, tree, int);
70 static tree grokvardecl (tree, tree, tree, const cp_decl_specifier_seq *,
71 int, int, int, bool, int, tree);
72 static int check_static_variable_definition (tree, tree);
73 static void record_unknown_type (tree, const char *);
74 static tree builtin_function_1 (tree, tree, bool);
75 static int member_function_or_else (tree, tree, enum overload_flags);
76 static void check_for_uninitialized_const_var (tree);
77 static tree local_variable_p_walkfn (tree *, int *, void *);
78 static const char *tag_name (enum tag_types);
79 static tree lookup_and_check_tag (enum tag_types, tree, tag_scope, bool);
80 static int walk_namespaces_r (tree, walk_namespaces_fn, void *);
81 static void maybe_deduce_size_from_array_init (tree, tree);
82 static void layout_var_decl (tree);
83 static tree check_initializer (tree, tree, int, vec<tree, va_gc> **);
84 static void make_rtl_for_nonlocal_decl (tree, tree, const char *);
85 static void save_function_data (tree);
86 static void copy_type_enum (tree , tree);
87 static void check_function_type (tree, tree);
88 static void finish_constructor_body (void);
89 static void begin_destructor_body (void);
90 static void finish_destructor_body (void);
91 static void record_key_method_defined (tree);
92 static tree create_array_type_for_decl (tree, tree, tree);
93 static tree get_atexit_node (void);
94 static tree get_dso_handle_node (void);
95 static tree start_cleanup_fn (void);
96 static void end_cleanup_fn (void);
97 static tree cp_make_fname_decl (location_t, tree, int);
98 static void initialize_predefined_identifiers (void);
99 static tree check_special_function_return_type
100 (special_function_kind, tree, tree, int, const location_t*);
101 static tree push_cp_library_fn (enum tree_code, tree, int);
102 static tree build_cp_library_fn (tree, enum tree_code, tree, int);
103 static void store_parm_decls (tree);
104 static void initialize_local_var (tree, tree);
105 static void expand_static_init (tree, tree);
107 /* The following symbols are subsumed in the cp_global_trees array, and
108 listed here individually for documentation purposes.
110 C++ extensions
111 tree wchar_decl_node;
113 tree vtable_entry_type;
114 tree delta_type_node;
115 tree __t_desc_type_node;
117 tree class_type_node;
118 tree unknown_type_node;
120 Array type `vtable_entry_type[]'
122 tree vtbl_type_node;
123 tree vtbl_ptr_type_node;
125 Namespaces,
127 tree std_node;
128 tree abi_node;
130 A FUNCTION_DECL which can call `abort'. Not necessarily the
131 one that the user will declare, but sufficient to be called
132 by routines that want to abort the program.
134 tree abort_fndecl;
136 Used by RTTI
137 tree type_info_type_node, tinfo_decl_id, tinfo_decl_type;
138 tree tinfo_var_id; */
140 tree cp_global_trees[CPTI_MAX];
142 /* Indicates that there is a type value in some namespace, although
143 that is not necessarily in scope at the moment. */
145 tree global_type_node;
147 /* The node that holds the "name" of the global scope. */
148 tree global_scope_name;
150 #define local_names cp_function_chain->x_local_names
152 /* A list of objects which have constructors or destructors
153 which reside in the global scope. The decl is stored in
154 the TREE_VALUE slot and the initializer is stored
155 in the TREE_PURPOSE slot. */
156 tree static_aggregates;
158 /* Like static_aggregates, but for thread_local variables. */
159 tree tls_aggregates;
161 /* -- end of C++ */
163 /* A node for the integer constant 2. */
165 tree integer_two_node;
167 /* Used only for jumps to as-yet undefined labels, since jumps to
168 defined labels can have their validity checked immediately. */
170 struct GTY((chain_next ("%h.next"))) named_label_use_entry {
171 struct named_label_use_entry *next;
172 /* The binding level to which this entry is *currently* attached.
173 This is initially the binding level in which the goto appeared,
174 but is modified as scopes are closed. */
175 cp_binding_level *binding_level;
176 /* The head of the names list that was current when the goto appeared,
177 or the inner scope popped. These are the decls that will *not* be
178 skipped when jumping to the label. */
179 tree names_in_scope;
180 /* The location of the goto, for error reporting. */
181 location_t o_goto_locus;
182 /* True if an OpenMP structured block scope has been closed since
183 the goto appeared. This means that the branch from the label will
184 illegally exit an OpenMP scope. */
185 bool in_omp_scope;
188 /* A list of all LABEL_DECLs in the function that have names. Here so
189 we can clear out their names' definitions at the end of the
190 function, and so we can check the validity of jumps to these labels. */
192 struct GTY((for_user)) named_label_entry {
193 /* The decl itself. */
194 tree label_decl;
196 /* The binding level to which the label is *currently* attached.
197 This is initially set to the binding level in which the label
198 is defined, but is modified as scopes are closed. */
199 cp_binding_level *binding_level;
200 /* The head of the names list that was current when the label was
201 defined, or the inner scope popped. These are the decls that will
202 be skipped when jumping to the label. */
203 tree names_in_scope;
204 /* A vector of all decls from all binding levels that would be
205 crossed by a backward branch to the label. */
206 vec<tree, va_gc> *bad_decls;
208 /* A list of uses of the label, before the label is defined. */
209 struct named_label_use_entry *uses;
211 /* The following bits are set after the label is defined, and are
212 updated as scopes are popped. They indicate that a backward jump
213 to the label will illegally enter a scope of the given flavor. */
214 bool in_try_scope;
215 bool in_catch_scope;
216 bool in_omp_scope;
217 bool in_transaction_scope;
218 bool in_constexpr_if;
221 #define named_labels cp_function_chain->x_named_labels
223 /* The number of function bodies which we are currently processing.
224 (Zero if we are at namespace scope, one inside the body of a
225 function, two inside the body of a function in a local class, etc.) */
226 int function_depth;
228 /* Whether the exception-specifier is part of a function type (i.e. C++17). */
229 bool flag_noexcept_type;
231 /* States indicating how grokdeclarator() should handle declspecs marked
232 with __attribute__((deprecated)). An object declared as
233 __attribute__((deprecated)) suppresses warnings of uses of other
234 deprecated items. */
235 enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
238 /* A list of VAR_DECLs whose type was incomplete at the time the
239 variable was declared. */
241 struct GTY(()) incomplete_var {
242 tree decl;
243 tree incomplete_type;
247 static GTY(()) vec<incomplete_var, va_gc> *incomplete_vars;
249 /* Returns the kind of template specialization we are currently
250 processing, given that it's declaration contained N_CLASS_SCOPES
251 explicit scope qualifications. */
253 tmpl_spec_kind
254 current_tmpl_spec_kind (int n_class_scopes)
256 int n_template_parm_scopes = 0;
257 int seen_specialization_p = 0;
258 int innermost_specialization_p = 0;
259 cp_binding_level *b;
261 /* Scan through the template parameter scopes. */
262 for (b = current_binding_level;
263 b->kind == sk_template_parms;
264 b = b->level_chain)
266 /* If we see a specialization scope inside a parameter scope,
267 then something is wrong. That corresponds to a declaration
268 like:
270 template <class T> template <> ...
272 which is always invalid since [temp.expl.spec] forbids the
273 specialization of a class member template if the enclosing
274 class templates are not explicitly specialized as well. */
275 if (b->explicit_spec_p)
277 if (n_template_parm_scopes == 0)
278 innermost_specialization_p = 1;
279 else
280 seen_specialization_p = 1;
282 else if (seen_specialization_p == 1)
283 return tsk_invalid_member_spec;
285 ++n_template_parm_scopes;
288 /* Handle explicit instantiations. */
289 if (processing_explicit_instantiation)
291 if (n_template_parm_scopes != 0)
292 /* We've seen a template parameter list during an explicit
293 instantiation. For example:
295 template <class T> template void f(int);
297 This is erroneous. */
298 return tsk_invalid_expl_inst;
299 else
300 return tsk_expl_inst;
303 if (n_template_parm_scopes < n_class_scopes)
304 /* We've not seen enough template headers to match all the
305 specialized classes present. For example:
307 template <class T> void R<T>::S<T>::f(int);
309 This is invalid; there needs to be one set of template
310 parameters for each class. */
311 return tsk_insufficient_parms;
312 else if (n_template_parm_scopes == n_class_scopes)
313 /* We're processing a non-template declaration (even though it may
314 be a member of a template class.) For example:
316 template <class T> void S<T>::f(int);
318 The `class T' matches the `S<T>', leaving no template headers
319 corresponding to the `f'. */
320 return tsk_none;
321 else if (n_template_parm_scopes > n_class_scopes + 1)
322 /* We've got too many template headers. For example:
324 template <> template <class T> void f (T);
326 There need to be more enclosing classes. */
327 return tsk_excessive_parms;
328 else
329 /* This must be a template. It's of the form:
331 template <class T> template <class U> void S<T>::f(U);
333 This is a specialization if the innermost level was a
334 specialization; otherwise it's just a definition of the
335 template. */
336 return innermost_specialization_p ? tsk_expl_spec : tsk_template;
339 /* Exit the current scope. */
341 void
342 finish_scope (void)
344 poplevel (0, 0, 0);
347 /* When a label goes out of scope, check to see if that label was used
348 in a valid manner, and issue any appropriate warnings or errors. */
350 static void
351 pop_label (tree label, tree old_value)
353 if (!processing_template_decl)
355 if (DECL_INITIAL (label) == NULL_TREE)
357 location_t location;
359 error ("label %q+D used but not defined", label);
360 location = input_location;
361 /* FIXME want (LOCATION_FILE (input_location), (line)0) */
362 /* Avoid crashing later. */
363 define_label (location, DECL_NAME (label));
365 else
366 warn_for_unused_label (label);
369 SET_IDENTIFIER_LABEL_VALUE (DECL_NAME (label), old_value);
372 /* Push all named labels into a vector, so that we can sort it on DECL_UID
373 to avoid code generation differences. */
376 note_label (named_label_entry **slot, vec<named_label_entry **> &labels)
378 labels.quick_push (slot);
379 return 1;
382 /* Helper function to sort named label entries in a vector by DECL_UID. */
384 static int
385 sort_labels (const void *a, const void *b)
387 named_label_entry **slot1 = *(named_label_entry **const *) a;
388 named_label_entry **slot2 = *(named_label_entry **const *) b;
389 if (DECL_UID ((*slot1)->label_decl) < DECL_UID ((*slot2)->label_decl))
390 return -1;
391 if (DECL_UID ((*slot1)->label_decl) > DECL_UID ((*slot2)->label_decl))
392 return 1;
393 return 0;
396 /* At the end of a function, all labels declared within the function
397 go out of scope. BLOCK is the top-level block for the
398 function. */
400 static void
401 pop_labels (tree block)
403 if (named_labels)
405 auto_vec<named_label_entry **, 32> labels;
406 named_label_entry **slot;
407 unsigned int i;
409 /* Push all the labels into a vector and sort them by DECL_UID,
410 so that gaps between DECL_UIDs don't affect code generation. */
411 labels.reserve_exact (named_labels->elements ());
412 named_labels->traverse<vec<named_label_entry **> &, note_label> (labels);
413 labels.qsort (sort_labels);
414 FOR_EACH_VEC_ELT (labels, i, slot)
416 struct named_label_entry *ent = *slot;
418 pop_label (ent->label_decl, NULL_TREE);
420 /* Put the labels into the "variables" of the top-level block,
421 so debugger can see them. */
422 DECL_CHAIN (ent->label_decl) = BLOCK_VARS (block);
423 BLOCK_VARS (block) = ent->label_decl;
425 named_labels->clear_slot (slot);
427 named_labels = NULL;
431 /* At the end of a block with local labels, restore the outer definition. */
433 static void
434 pop_local_label (tree label, tree old_value)
436 struct named_label_entry dummy;
438 pop_label (label, old_value);
440 dummy.label_decl = label;
441 named_label_entry **slot = named_labels->find_slot (&dummy, NO_INSERT);
442 named_labels->clear_slot (slot);
445 /* The following two routines are used to interface to Objective-C++.
446 The binding level is purposely treated as an opaque type. */
448 void *
449 objc_get_current_scope (void)
451 return current_binding_level;
454 /* The following routine is used by the NeXT-style SJLJ exceptions;
455 variables get marked 'volatile' so as to not be clobbered by
456 _setjmp()/_longjmp() calls. All variables in the current scope,
457 as well as parent scopes up to (but not including) ENCLOSING_BLK
458 shall be thusly marked. */
460 void
461 objc_mark_locals_volatile (void *enclosing_blk)
463 cp_binding_level *scope;
465 for (scope = current_binding_level;
466 scope && scope != enclosing_blk;
467 scope = scope->level_chain)
469 tree decl;
471 for (decl = scope->names; decl; decl = TREE_CHAIN (decl))
472 objc_volatilize_decl (decl);
474 /* Do not climb up past the current function. */
475 if (scope->kind == sk_function_parms)
476 break;
480 /* True if B is the level for the condition of a constexpr if. */
482 static bool
483 level_for_constexpr_if (cp_binding_level *b)
485 return (b->kind == sk_cond && b->this_entity
486 && TREE_CODE (b->this_entity) == IF_STMT
487 && IF_STMT_CONSTEXPR_P (b->this_entity));
490 /* Update data for defined and undefined labels when leaving a scope. */
493 poplevel_named_label_1 (named_label_entry **slot, cp_binding_level *bl)
495 named_label_entry *ent = *slot;
496 cp_binding_level *obl = bl->level_chain;
498 if (ent->binding_level == bl)
500 tree decl;
502 /* ENT->NAMES_IN_SCOPE may contain a mixture of DECLs and
503 TREE_LISTs representing OVERLOADs, so be careful. */
504 for (decl = ent->names_in_scope; decl; decl = (DECL_P (decl)
505 ? DECL_CHAIN (decl)
506 : TREE_CHAIN (decl)))
507 if (decl_jump_unsafe (decl))
508 vec_safe_push (ent->bad_decls, decl);
510 ent->binding_level = obl;
511 ent->names_in_scope = obl->names;
512 switch (bl->kind)
514 case sk_try:
515 ent->in_try_scope = true;
516 break;
517 case sk_catch:
518 ent->in_catch_scope = true;
519 break;
520 case sk_omp:
521 ent->in_omp_scope = true;
522 break;
523 case sk_transaction:
524 ent->in_transaction_scope = true;
525 break;
526 case sk_block:
527 if (level_for_constexpr_if (bl->level_chain))
528 ent->in_constexpr_if = true;
529 break;
530 default:
531 break;
534 else if (ent->uses)
536 struct named_label_use_entry *use;
538 for (use = ent->uses; use ; use = use->next)
539 if (use->binding_level == bl)
541 use->binding_level = obl;
542 use->names_in_scope = obl->names;
543 if (bl->kind == sk_omp)
544 use->in_omp_scope = true;
548 return 1;
551 /* Saved errorcount to avoid -Wunused-but-set-{parameter,variable} warnings
552 when errors were reported, except for -Werror-unused-but-set-*. */
553 static int unused_but_set_errorcount;
555 /* Exit a binding level.
556 Pop the level off, and restore the state of the identifier-decl mappings
557 that were in effect when this level was entered.
559 If KEEP == 1, this level had explicit declarations, so
560 and create a "block" (a BLOCK node) for the level
561 to record its declarations and subblocks for symbol table output.
563 If FUNCTIONBODY is nonzero, this level is the body of a function,
564 so create a block as if KEEP were set and also clear out all
565 label names.
567 If REVERSE is nonzero, reverse the order of decls before putting
568 them into the BLOCK. */
570 tree
571 poplevel (int keep, int reverse, int functionbody)
573 tree link;
574 /* The chain of decls was accumulated in reverse order.
575 Put it into forward order, just for cleanliness. */
576 tree decls;
577 tree subblocks;
578 tree block;
579 tree decl;
580 int leaving_for_scope;
581 scope_kind kind;
582 unsigned ix;
583 cp_label_binding *label_bind;
585 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
586 restart:
588 block = NULL_TREE;
590 gcc_assert (current_binding_level->kind != sk_class);
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 = getdecls (); 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;
658 tree type = TREE_TYPE (decl);
659 if (VAR_P (decl)
660 && (! TREE_USED (decl) || !DECL_READ_P (decl))
661 && ! DECL_IN_SYSTEM_HEADER (decl)
662 && DECL_NAME (decl) && ! DECL_ARTIFICIAL (decl)
663 && type != error_mark_node
664 && (!CLASS_TYPE_P (type)
665 || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
666 || lookup_attribute ("warn_unused",
667 TYPE_ATTRIBUTES (TREE_TYPE (decl)))))
669 if (! TREE_USED (decl))
670 warning_at (DECL_SOURCE_LOCATION (decl),
671 OPT_Wunused_variable, "unused variable %qD", decl);
672 else if (DECL_CONTEXT (decl) == current_function_decl
673 // For -Wunused-but-set-variable leave references alone.
674 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
675 && errorcount == unused_but_set_errorcount)
677 warning_at (DECL_SOURCE_LOCATION (decl),
678 OPT_Wunused_but_set_variable,
679 "variable %qD set but not used", decl);
680 unused_but_set_errorcount = errorcount;
685 /* Remove declarations for all the DECLs in this level. */
686 for (link = decls; link; link = TREE_CHAIN (link))
688 if (leaving_for_scope && VAR_P (link)
689 /* It's hard to make this ARM compatibility hack play nicely with
690 lambdas, and it really isn't necessary in C++11 mode. */
691 && cxx_dialect < cxx11
692 && DECL_NAME (link))
694 tree name = DECL_NAME (link);
695 cxx_binding *ob;
696 tree ns_binding;
698 ob = outer_binding (name,
699 IDENTIFIER_BINDING (name),
700 /*class_p=*/true);
701 if (!ob)
702 ns_binding = IDENTIFIER_NAMESPACE_VALUE (name);
703 else
704 ns_binding = NULL_TREE;
706 if (ob && ob->scope == current_binding_level->level_chain)
707 /* We have something like:
709 int i;
710 for (int i; ;);
712 and we are leaving the `for' scope. There's no reason to
713 keep the binding of the inner `i' in this case. */
714 pop_binding (name, link);
715 else if ((ob && (TREE_CODE (ob->value) == TYPE_DECL))
716 || (ns_binding && TREE_CODE (ns_binding) == TYPE_DECL))
717 /* Here, we have something like:
719 typedef int I;
721 void f () {
722 for (int I; ;);
725 We must pop the for-scope binding so we know what's a
726 type and what isn't. */
727 pop_binding (name, link);
728 else
730 /* Mark this VAR_DECL as dead so that we can tell we left it
731 there only for backward compatibility. */
732 DECL_DEAD_FOR_LOCAL (link) = 1;
734 /* Keep track of what should have happened when we
735 popped the binding. */
736 if (ob && ob->value)
738 SET_DECL_SHADOWED_FOR_VAR (link, ob->value);
739 DECL_HAS_SHADOWED_FOR_VAR_P (link) = 1;
742 /* Add it to the list of dead variables in the next
743 outermost binding to that we can remove these when we
744 leave that binding. */
745 vec_safe_push (
746 current_binding_level->level_chain->dead_vars_from_for,
747 link);
749 /* Although we don't pop the cxx_binding, we do clear
750 its SCOPE since the scope is going away now. */
751 IDENTIFIER_BINDING (name)->scope
752 = current_binding_level->level_chain;
755 else
757 tree name;
759 /* Remove the binding. */
760 decl = link;
762 if (TREE_CODE (decl) == TREE_LIST)
763 decl = TREE_VALUE (decl);
764 name = decl;
766 if (TREE_CODE (name) == OVERLOAD)
767 name = OVL_FUNCTION (name);
769 gcc_assert (DECL_P (name));
770 pop_binding (DECL_NAME (name), decl);
774 /* Remove declarations for any `for' variables from inner scopes
775 that we kept around. */
776 FOR_EACH_VEC_SAFE_ELT_REVERSE (current_binding_level->dead_vars_from_for,
777 ix, decl)
778 pop_binding (DECL_NAME (decl), decl);
780 /* Restore the IDENTIFIER_TYPE_VALUEs. */
781 for (link = current_binding_level->type_shadowed;
782 link; link = TREE_CHAIN (link))
783 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link), TREE_VALUE (link));
785 /* Restore the IDENTIFIER_LABEL_VALUEs for local labels. */
786 FOR_EACH_VEC_SAFE_ELT_REVERSE (current_binding_level->shadowed_labels,
787 ix, label_bind)
788 pop_local_label (label_bind->label, label_bind->prev_value);
790 /* There may be OVERLOADs (wrapped in TREE_LISTs) on the BLOCK_VARs
791 list if a `using' declaration put them there. The debugging
792 back ends won't understand OVERLOAD, so we remove them here.
793 Because the BLOCK_VARS are (temporarily) shared with
794 CURRENT_BINDING_LEVEL->NAMES we must do this fixup after we have
795 popped all the bindings. */
796 if (block)
798 tree* d;
800 for (d = &BLOCK_VARS (block); *d; )
802 if (TREE_CODE (*d) == TREE_LIST)
803 *d = TREE_CHAIN (*d);
804 else
805 d = &DECL_CHAIN (*d);
809 /* If the level being exited is the top level of a function,
810 check over all the labels. */
811 if (functionbody)
813 if (block)
815 /* Since this is the top level block of a function, the vars are
816 the function's parameters. Don't leave them in the BLOCK
817 because they are found in the FUNCTION_DECL instead. */
818 BLOCK_VARS (block) = 0;
819 pop_labels (block);
821 else
822 pop_labels (subblocks);
825 kind = current_binding_level->kind;
826 if (kind == sk_cleanup)
828 tree stmt;
830 /* If this is a temporary binding created for a cleanup, then we'll
831 have pushed a statement list level. Pop that, create a new
832 BIND_EXPR for the block, and insert it into the stream. */
833 stmt = pop_stmt_list (current_binding_level->statement_list);
834 stmt = c_build_bind_expr (input_location, block, stmt);
835 add_stmt (stmt);
838 leave_scope ();
839 if (functionbody)
841 /* The current function is being defined, so its DECL_INITIAL
842 should be error_mark_node. */
843 gcc_assert (DECL_INITIAL (current_function_decl) == error_mark_node);
844 DECL_INITIAL (current_function_decl) = block ? block : subblocks;
845 if (subblocks)
847 if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
849 if (BLOCK_SUBBLOCKS (subblocks))
850 BLOCK_OUTER_CURLY_BRACE_P (BLOCK_SUBBLOCKS (subblocks)) = 1;
852 else
853 BLOCK_OUTER_CURLY_BRACE_P (subblocks) = 1;
856 else if (block)
857 current_binding_level->blocks
858 = block_chainon (current_binding_level->blocks, block);
860 /* If we did not make a block for the level just exited,
861 any blocks made for inner levels
862 (since they cannot be recorded as subblocks in that level)
863 must be carried forward so they will later become subblocks
864 of something else. */
865 else if (subblocks)
866 current_binding_level->blocks
867 = block_chainon (current_binding_level->blocks, subblocks);
869 /* Each and every BLOCK node created here in `poplevel' is important
870 (e.g. for proper debugging information) so if we created one
871 earlier, mark it as "used". */
872 if (block)
873 TREE_USED (block) = 1;
875 /* All temporary bindings created for cleanups are popped silently. */
876 if (kind == sk_cleanup)
877 goto restart;
879 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
880 return block;
883 /* Walk all the namespaces contained NAMESPACE, including NAMESPACE
884 itself, calling F for each. The DATA is passed to F as well. */
886 static int
887 walk_namespaces_r (tree name_space, walk_namespaces_fn f, void* data)
889 int result = 0;
890 tree current = NAMESPACE_LEVEL (name_space)->namespaces;
892 result |= (*f) (name_space, data);
894 for (; current; current = DECL_CHAIN (current))
895 result |= walk_namespaces_r (current, f, data);
897 return result;
900 /* Walk all the namespaces, calling F for each. The DATA is passed to
901 F as well. */
904 walk_namespaces (walk_namespaces_fn f, void* data)
906 return walk_namespaces_r (global_namespace, f, data);
909 /* Call wrapup_globals_declarations for the globals in NAMESPACE. */
912 wrapup_globals_for_namespace (tree name_space, void* data ATTRIBUTE_UNUSED)
914 cp_binding_level *level = NAMESPACE_LEVEL (name_space);
915 vec<tree, va_gc> *statics = level->static_decls;
916 tree *vec = statics->address ();
917 int len = statics->length ();
919 if (warn_unused_function)
921 tree decl;
922 unsigned int i;
923 FOR_EACH_VEC_SAFE_ELT (statics, i, decl)
924 if (TREE_CODE (decl) == FUNCTION_DECL
925 && DECL_INITIAL (decl) == 0
926 && DECL_EXTERNAL (decl)
927 && !TREE_PUBLIC (decl)
928 && !DECL_ARTIFICIAL (decl)
929 && !DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
930 && !TREE_NO_WARNING (decl))
932 warning_at (DECL_SOURCE_LOCATION (decl),
933 OPT_Wunused_function,
934 "%qF declared %<static%> but never defined", decl);
935 TREE_NO_WARNING (decl) = 1;
939 /* Write out any globals that need to be output. */
940 return wrapup_global_declarations (vec, len);
943 /* Diagnose odr-used extern inline variables without definitions
944 in the current TU. */
946 diagnose_inline_vars_for_namespace (tree name_space, void *)
948 cp_binding_level *level = NAMESPACE_LEVEL (name_space);
949 vec<tree, va_gc> *statics = level->static_decls;
950 tree decl;
951 unsigned int i;
953 FOR_EACH_VEC_SAFE_ELT (statics, i, decl)
954 if (VAR_P (decl)
955 && DECL_EXTERNAL (decl)
956 && DECL_INLINE_VAR_P (decl)
957 && DECL_ODR_USED (decl))
958 error_at (DECL_SOURCE_LOCATION (decl),
959 "odr-used inline variable %qD is not defined", decl);
961 return 0;
964 /* In C++, you don't have to write `struct S' to refer to `S'; you
965 can just use `S'. We accomplish this by creating a TYPE_DECL as
966 if the user had written `typedef struct S S'. Create and return
967 the TYPE_DECL for TYPE. */
969 tree
970 create_implicit_typedef (tree name, tree type)
972 tree decl;
974 decl = build_decl (input_location, TYPE_DECL, name, type);
975 DECL_ARTIFICIAL (decl) = 1;
976 /* There are other implicit type declarations, like the one *within*
977 a class that allows you to write `S::S'. We must distinguish
978 amongst these. */
979 SET_DECL_IMPLICIT_TYPEDEF_P (decl);
980 TYPE_NAME (type) = decl;
981 TYPE_STUB_DECL (type) = decl;
983 return decl;
986 /* Remember a local name for name-mangling purposes. */
988 static void
989 push_local_name (tree decl)
991 size_t i, nelts;
992 tree t, name;
994 timevar_start (TV_NAME_LOOKUP);
996 name = DECL_NAME (decl);
998 nelts = vec_safe_length (local_names);
999 for (i = 0; i < nelts; i++)
1001 t = (*local_names)[i];
1002 if (DECL_NAME (t) == name)
1004 if (!DECL_LANG_SPECIFIC (decl))
1005 retrofit_lang_decl (decl);
1006 DECL_LANG_SPECIFIC (decl)->u.base.u2sel = 1;
1007 if (DECL_DISCRIMINATOR_SET_P (t))
1008 DECL_DISCRIMINATOR (decl) = DECL_DISCRIMINATOR (t) + 1;
1009 else
1010 DECL_DISCRIMINATOR (decl) = 1;
1012 (*local_names)[i] = decl;
1013 timevar_stop (TV_NAME_LOOKUP);
1014 return;
1018 vec_safe_push (local_names, decl);
1019 timevar_stop (TV_NAME_LOOKUP);
1022 /* Subroutine of duplicate_decls: return truthvalue of whether
1023 or not types of these decls match.
1025 For C++, we must compare the parameter list so that `int' can match
1026 `int&' in a parameter position, but `int&' is not confused with
1027 `const int&'. */
1030 decls_match (tree newdecl, tree olddecl)
1032 int types_match;
1034 if (newdecl == olddecl)
1035 return 1;
1037 if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
1038 /* If the two DECLs are not even the same kind of thing, we're not
1039 interested in their types. */
1040 return 0;
1042 gcc_assert (DECL_P (newdecl));
1044 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1046 tree f1 = TREE_TYPE (newdecl);
1047 tree f2 = TREE_TYPE (olddecl);
1048 tree p1 = TYPE_ARG_TYPES (f1);
1049 tree p2 = TYPE_ARG_TYPES (f2);
1050 tree r2;
1052 /* Specializations of different templates are different functions
1053 even if they have the same type. */
1054 tree t1 = (DECL_USE_TEMPLATE (newdecl)
1055 ? DECL_TI_TEMPLATE (newdecl)
1056 : NULL_TREE);
1057 tree t2 = (DECL_USE_TEMPLATE (olddecl)
1058 ? DECL_TI_TEMPLATE (olddecl)
1059 : NULL_TREE);
1060 if (t1 != t2)
1061 return 0;
1063 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
1064 && ! (DECL_EXTERN_C_P (newdecl)
1065 && DECL_EXTERN_C_P (olddecl)))
1066 return 0;
1068 /* A new declaration doesn't match a built-in one unless it
1069 is also extern "C". */
1070 if (DECL_IS_BUILTIN (olddecl)
1071 && DECL_EXTERN_C_P (olddecl) && !DECL_EXTERN_C_P (newdecl))
1072 return 0;
1074 if (TREE_CODE (f1) != TREE_CODE (f2))
1075 return 0;
1077 /* A declaration with deduced return type should use its pre-deduction
1078 type for declaration matching. */
1079 r2 = fndecl_declared_return_type (olddecl);
1081 if (same_type_p (TREE_TYPE (f1), r2))
1083 if (!prototype_p (f2) && DECL_EXTERN_C_P (olddecl)
1084 && (DECL_BUILT_IN (olddecl)
1085 #ifndef NO_IMPLICIT_EXTERN_C
1086 || (DECL_IN_SYSTEM_HEADER (newdecl) && !DECL_CLASS_SCOPE_P (newdecl))
1087 || (DECL_IN_SYSTEM_HEADER (olddecl) && !DECL_CLASS_SCOPE_P (olddecl))
1088 #endif
1091 types_match = self_promoting_args_p (p1);
1092 if (p1 == void_list_node)
1093 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
1095 #ifndef NO_IMPLICIT_EXTERN_C
1096 else if (!prototype_p (f1)
1097 && (DECL_EXTERN_C_P (olddecl)
1098 && DECL_IN_SYSTEM_HEADER (olddecl)
1099 && !DECL_CLASS_SCOPE_P (olddecl))
1100 && (DECL_EXTERN_C_P (newdecl)
1101 && DECL_IN_SYSTEM_HEADER (newdecl)
1102 && !DECL_CLASS_SCOPE_P (newdecl)))
1104 types_match = self_promoting_args_p (p2);
1105 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
1107 #endif
1108 else
1109 types_match =
1110 compparms (p1, p2)
1111 && type_memfn_rqual (f1) == type_memfn_rqual (f2)
1112 && (TYPE_ATTRIBUTES (TREE_TYPE (newdecl)) == NULL_TREE
1113 || comp_type_attributes (TREE_TYPE (newdecl),
1114 TREE_TYPE (olddecl)) != 0);
1116 else
1117 types_match = 0;
1119 /* The decls dont match if they correspond to two different versions
1120 of the same function. Disallow extern "C" functions to be
1121 versions for now. */
1122 if (types_match
1123 && !DECL_EXTERN_C_P (newdecl)
1124 && !DECL_EXTERN_C_P (olddecl)
1125 && targetm.target_option.function_versions (newdecl, olddecl))
1127 /* Mark functions as versions if necessary. Modify the mangled decl
1128 name if necessary. */
1129 if (DECL_FUNCTION_VERSIONED (newdecl)
1130 && DECL_FUNCTION_VERSIONED (olddecl))
1131 return 0;
1132 if (!DECL_FUNCTION_VERSIONED (newdecl))
1134 DECL_FUNCTION_VERSIONED (newdecl) = 1;
1135 if (DECL_ASSEMBLER_NAME_SET_P (newdecl))
1136 mangle_decl (newdecl);
1138 if (!DECL_FUNCTION_VERSIONED (olddecl))
1140 DECL_FUNCTION_VERSIONED (olddecl) = 1;
1141 if (DECL_ASSEMBLER_NAME_SET_P (olddecl))
1142 mangle_decl (olddecl);
1144 cgraph_node::record_function_versions (olddecl, newdecl);
1145 return 0;
1148 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1150 tree oldres = DECL_TEMPLATE_RESULT (olddecl);
1151 tree newres = DECL_TEMPLATE_RESULT (newdecl);
1153 if (TREE_CODE (newres) != TREE_CODE (oldres))
1154 return 0;
1156 if (!comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
1157 DECL_TEMPLATE_PARMS (olddecl)))
1158 return 0;
1160 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
1161 types_match = (same_type_p (TREE_TYPE (oldres), TREE_TYPE (newres))
1162 && equivalently_constrained (olddecl, newdecl));
1163 else
1164 // We don't need to check equivalently_constrained for variable and
1165 // function templates because we check it on the results.
1166 types_match = decls_match (oldres, newres);
1168 else
1170 /* Need to check scope for variable declaration (VAR_DECL).
1171 For typedef (TYPE_DECL), scope is ignored. */
1172 if (VAR_P (newdecl)
1173 && CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
1174 /* [dcl.link]
1175 Two declarations for an object with C language linkage
1176 with the same name (ignoring the namespace that qualify
1177 it) that appear in different namespace scopes refer to
1178 the same object. */
1179 && !(DECL_EXTERN_C_P (olddecl) && DECL_EXTERN_C_P (newdecl)))
1180 return 0;
1182 if (TREE_TYPE (newdecl) == error_mark_node)
1183 types_match = TREE_TYPE (olddecl) == error_mark_node;
1184 else if (TREE_TYPE (olddecl) == NULL_TREE)
1185 types_match = TREE_TYPE (newdecl) == NULL_TREE;
1186 else if (TREE_TYPE (newdecl) == NULL_TREE)
1187 types_match = 0;
1188 else
1189 types_match = comptypes (TREE_TYPE (newdecl),
1190 TREE_TYPE (olddecl),
1191 COMPARE_REDECLARATION);
1194 // Normal functions can be constrained, as can variable partial
1195 // specializations.
1196 if (types_match && VAR_OR_FUNCTION_DECL_P (newdecl))
1197 types_match = equivalently_constrained (newdecl, olddecl);
1199 return types_match;
1202 /* If NEWDECL is `static' and an `extern' was seen previously,
1203 warn about it. OLDDECL is the previous declaration.
1205 Note that this does not apply to the C++ case of declaring
1206 a variable `extern const' and then later `const'.
1208 Don't complain about built-in functions, since they are beyond
1209 the user's control. */
1211 void
1212 warn_extern_redeclared_static (tree newdecl, tree olddecl)
1214 if (TREE_CODE (newdecl) == TYPE_DECL
1215 || TREE_CODE (newdecl) == TEMPLATE_DECL
1216 || TREE_CODE (newdecl) == CONST_DECL
1217 || TREE_CODE (newdecl) == NAMESPACE_DECL)
1218 return;
1220 /* Don't get confused by static member functions; that's a different
1221 use of `static'. */
1222 if (TREE_CODE (newdecl) == FUNCTION_DECL
1223 && DECL_STATIC_FUNCTION_P (newdecl))
1224 return;
1226 /* If the old declaration was `static', or the new one isn't, then
1227 everything is OK. */
1228 if (DECL_THIS_STATIC (olddecl) || !DECL_THIS_STATIC (newdecl))
1229 return;
1231 /* It's OK to declare a builtin function as `static'. */
1232 if (TREE_CODE (olddecl) == FUNCTION_DECL
1233 && DECL_ARTIFICIAL (olddecl))
1234 return;
1236 if (permerror (DECL_SOURCE_LOCATION (newdecl),
1237 "%qD was declared %<extern%> and later %<static%>", newdecl))
1238 inform (DECL_SOURCE_LOCATION (olddecl),
1239 "previous declaration of %qD", olddecl);
1242 /* NEW_DECL is a redeclaration of OLD_DECL; both are functions or
1243 function templates. If their exception specifications do not
1244 match, issue a diagnostic. */
1246 static void
1247 check_redeclaration_exception_specification (tree new_decl,
1248 tree old_decl)
1250 tree new_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (new_decl));
1251 tree old_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old_decl));
1253 /* Two default specs are equivalent, don't force evaluation. */
1254 if (UNEVALUATED_NOEXCEPT_SPEC_P (new_exceptions)
1255 && UNEVALUATED_NOEXCEPT_SPEC_P (old_exceptions))
1256 return;
1258 maybe_instantiate_noexcept (new_decl);
1259 maybe_instantiate_noexcept (old_decl);
1260 new_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (new_decl));
1261 old_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old_decl));
1263 /* [except.spec]
1265 If any declaration of a function has an exception-specification,
1266 all declarations, including the definition and an explicit
1267 specialization, of that function shall have an
1268 exception-specification with the same set of type-ids. */
1269 if (! DECL_IS_BUILTIN (old_decl)
1270 && !comp_except_specs (new_exceptions, old_exceptions, ce_normal))
1272 const char *msg
1273 = "declaration of %qF has a different exception specifier";
1274 bool complained = true;
1275 location_t new_loc = DECL_SOURCE_LOCATION (new_decl);
1276 if (DECL_IN_SYSTEM_HEADER (old_decl))
1277 complained = pedwarn (new_loc, OPT_Wsystem_headers, msg, new_decl);
1278 else if (!flag_exceptions)
1279 /* We used to silently permit mismatched eh specs with
1280 -fno-exceptions, so make them a pedwarn now. */
1281 complained = pedwarn (new_loc, OPT_Wpedantic, msg, new_decl);
1282 else
1283 error_at (new_loc, msg, new_decl);
1284 if (complained)
1285 inform (DECL_SOURCE_LOCATION (old_decl),
1286 "from previous declaration %qF", old_decl);
1290 /* Return true if OLD_DECL and NEW_DECL agree on constexprness.
1291 Otherwise issue diagnostics. */
1293 static bool
1294 validate_constexpr_redeclaration (tree old_decl, tree new_decl)
1296 old_decl = STRIP_TEMPLATE (old_decl);
1297 new_decl = STRIP_TEMPLATE (new_decl);
1298 if (!VAR_OR_FUNCTION_DECL_P (old_decl)
1299 || !VAR_OR_FUNCTION_DECL_P (new_decl))
1300 return true;
1301 if (DECL_DECLARED_CONSTEXPR_P (old_decl)
1302 == DECL_DECLARED_CONSTEXPR_P (new_decl))
1303 return true;
1304 if (TREE_CODE (old_decl) == FUNCTION_DECL)
1306 if (DECL_BUILT_IN (old_decl))
1308 /* Hide a built-in declaration. */
1309 DECL_DECLARED_CONSTEXPR_P (old_decl)
1310 = DECL_DECLARED_CONSTEXPR_P (new_decl);
1311 return true;
1313 /* 7.1.5 [dcl.constexpr]
1314 Note: An explicit specialization can differ from the template
1315 declaration with respect to the constexpr specifier. */
1316 if (! DECL_TEMPLATE_SPECIALIZATION (old_decl)
1317 && DECL_TEMPLATE_SPECIALIZATION (new_decl))
1318 return true;
1320 error_at (DECL_SOURCE_LOCATION (new_decl),
1321 "redeclaration %qD differs in %<constexpr%> "
1322 "from previous declaration", new_decl);
1323 inform (DECL_SOURCE_LOCATION (old_decl),
1324 "previous declaration %qD", old_decl);
1325 return false;
1327 return true;
1330 // If OLDDECL and NEWDECL are concept declarations with the same type
1331 // (i.e., and template parameters), but different requirements,
1332 // emit diagnostics and return true. Otherwise, return false.
1333 static inline bool
1334 check_concept_refinement (tree olddecl, tree newdecl)
1336 if (!DECL_DECLARED_CONCEPT_P (olddecl) || !DECL_DECLARED_CONCEPT_P (newdecl))
1337 return false;
1339 tree d1 = DECL_TEMPLATE_RESULT (olddecl);
1340 tree d2 = DECL_TEMPLATE_RESULT (newdecl);
1341 if (TREE_CODE (d1) != TREE_CODE (d2))
1342 return false;
1344 tree t1 = TREE_TYPE (d1);
1345 tree t2 = TREE_TYPE (d2);
1346 if (TREE_CODE (d1) == FUNCTION_DECL)
1348 if (compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2))
1349 && comp_template_parms (DECL_TEMPLATE_PARMS (olddecl),
1350 DECL_TEMPLATE_PARMS (newdecl))
1351 && !equivalently_constrained (olddecl, newdecl))
1353 error ("cannot specialize concept %q#D", olddecl);
1354 return true;
1357 return false;
1360 /* DECL is a redeclaration of a function or function template. If
1361 it does have default arguments issue a diagnostic. Note: this
1362 function is used to enforce the requirements in C++11 8.3.6 about
1363 no default arguments in redeclarations. */
1365 static void
1366 check_redeclaration_no_default_args (tree decl)
1368 gcc_assert (DECL_DECLARES_FUNCTION_P (decl));
1370 for (tree t = FUNCTION_FIRST_USER_PARMTYPE (decl);
1371 t && t != void_list_node; t = TREE_CHAIN (t))
1372 if (TREE_PURPOSE (t))
1374 permerror (DECL_SOURCE_LOCATION (decl),
1375 "redeclaration of %q#D may not have default "
1376 "arguments", decl);
1377 return;
1381 #define GNU_INLINE_P(fn) (DECL_DECLARED_INLINE_P (fn) \
1382 && lookup_attribute ("gnu_inline", \
1383 DECL_ATTRIBUTES (fn)))
1385 /* If NEWDECL is a redeclaration of OLDDECL, merge the declarations.
1386 If the redeclaration is invalid, a diagnostic is issued, and the
1387 error_mark_node is returned. Otherwise, OLDDECL is returned.
1389 If NEWDECL is not a redeclaration of OLDDECL, NULL_TREE is
1390 returned.
1392 NEWDECL_IS_FRIEND is true if NEWDECL was declared as a friend. */
1394 tree
1395 duplicate_decls (tree newdecl, tree olddecl, bool newdecl_is_friend)
1397 unsigned olddecl_uid = DECL_UID (olddecl);
1398 int olddecl_friend = 0, types_match = 0, hidden_friend = 0;
1399 int new_defines_function = 0;
1400 tree new_template_info;
1402 if (newdecl == olddecl)
1403 return olddecl;
1405 types_match = decls_match (newdecl, olddecl);
1407 /* If either the type of the new decl or the type of the old decl is an
1408 error_mark_node, then that implies that we have already issued an
1409 error (earlier) for some bogus type specification, and in that case,
1410 it is rather pointless to harass the user with yet more error message
1411 about the same declaration, so just pretend the types match here. */
1412 if (TREE_TYPE (newdecl) == error_mark_node
1413 || TREE_TYPE (olddecl) == error_mark_node)
1414 return error_mark_node;
1416 if (UDLIT_OPER_P (DECL_NAME (newdecl))
1417 && UDLIT_OPER_P (DECL_NAME (olddecl)))
1419 if (TREE_CODE (newdecl) == TEMPLATE_DECL
1420 && TREE_CODE (olddecl) != TEMPLATE_DECL
1421 && check_raw_literal_operator (olddecl))
1422 error ("literal operator template %q+D conflicts with"
1423 " raw literal operator %qD", newdecl, olddecl);
1424 else if (TREE_CODE (newdecl) != TEMPLATE_DECL
1425 && TREE_CODE (olddecl) == TEMPLATE_DECL
1426 && check_raw_literal_operator (newdecl))
1427 error ("raw literal operator %q+D conflicts with"
1428 " literal operator template %qD", newdecl, olddecl);
1431 if (DECL_P (olddecl)
1432 && TREE_CODE (newdecl) == FUNCTION_DECL
1433 && TREE_CODE (olddecl) == FUNCTION_DECL
1434 && diagnose_mismatched_attributes (olddecl, newdecl))
1436 if (DECL_INITIAL (olddecl))
1437 inform (DECL_SOURCE_LOCATION (olddecl),
1438 "previous definition of %qD was here", olddecl);
1439 else
1440 inform (DECL_SOURCE_LOCATION (olddecl),
1441 "previous declaration of %qD was here", olddecl);
1444 /* Check for redeclaration and other discrepancies. */
1445 if (TREE_CODE (olddecl) == FUNCTION_DECL
1446 && DECL_ARTIFICIAL (olddecl))
1448 gcc_assert (!DECL_HIDDEN_FRIEND_P (olddecl));
1449 if (TREE_CODE (newdecl) != FUNCTION_DECL)
1451 /* Avoid warnings redeclaring built-ins which have not been
1452 explicitly declared. */
1453 if (DECL_ANTICIPATED (olddecl))
1454 return NULL_TREE;
1456 /* If you declare a built-in or predefined function name as static,
1457 the old definition is overridden, but optionally warn this was a
1458 bad choice of name. */
1459 if (! TREE_PUBLIC (newdecl))
1461 warning (OPT_Wshadow,
1462 DECL_BUILT_IN (olddecl)
1463 ? G_("shadowing built-in function %q#D")
1464 : G_("shadowing library function %q#D"), olddecl);
1465 /* Discard the old built-in function. */
1466 return NULL_TREE;
1468 /* If the built-in is not ansi, then programs can override
1469 it even globally without an error. */
1470 else if (! DECL_BUILT_IN (olddecl))
1471 warning_at (DECL_SOURCE_LOCATION (newdecl), 0,
1472 "library function %q#D redeclared as non-function %q#D",
1473 olddecl, newdecl);
1474 else
1475 error ("declaration of %q+#D conflicts with built-in "
1476 "declaration %q#D", newdecl, olddecl);
1477 return NULL_TREE;
1479 else if (DECL_OMP_DECLARE_REDUCTION_P (olddecl))
1481 gcc_assert (DECL_OMP_DECLARE_REDUCTION_P (newdecl));
1482 error_at (DECL_SOURCE_LOCATION (newdecl),
1483 "redeclaration of %<pragma omp declare reduction%>");
1484 inform (DECL_SOURCE_LOCATION (olddecl),
1485 "previous %<pragma omp declare reduction%> declaration");
1486 return error_mark_node;
1488 else if (!types_match)
1490 /* Avoid warnings redeclaring built-ins which have not been
1491 explicitly declared. */
1492 if (DECL_ANTICIPATED (olddecl))
1494 tree t1, t2;
1496 /* A new declaration doesn't match a built-in one unless it
1497 is also extern "C". */
1498 gcc_assert (DECL_IS_BUILTIN (olddecl));
1499 gcc_assert (DECL_EXTERN_C_P (olddecl));
1500 if (!DECL_EXTERN_C_P (newdecl))
1501 return NULL_TREE;
1503 for (t1 = TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1504 t2 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1505 t1 || t2;
1506 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1507 if (!t1 || !t2)
1508 break;
1509 /* Deal with fileptr_type_node. FILE type is not known
1510 at the time we create the builtins. */
1511 else if (TREE_VALUE (t2) == fileptr_type_node)
1513 tree t = TREE_VALUE (t1);
1515 if (TYPE_PTR_P (t)
1516 && TYPE_IDENTIFIER (TREE_TYPE (t))
1517 == get_identifier ("FILE")
1518 && compparms (TREE_CHAIN (t1), TREE_CHAIN (t2)))
1520 tree oldargs = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1522 TYPE_ARG_TYPES (TREE_TYPE (olddecl))
1523 = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
1524 types_match = decls_match (newdecl, olddecl);
1525 if (types_match)
1526 return duplicate_decls (newdecl, olddecl,
1527 newdecl_is_friend);
1528 TYPE_ARG_TYPES (TREE_TYPE (olddecl)) = oldargs;
1531 /* Likewise for const struct tm*. */
1532 else if (TREE_VALUE (t2) == const_tm_ptr_type_node)
1534 tree t = TREE_VALUE (t1);
1536 if (TYPE_PTR_P (t)
1537 && TYPE_IDENTIFIER (TREE_TYPE (t))
1538 == get_identifier ("tm")
1539 && compparms (TREE_CHAIN (t1), TREE_CHAIN (t2)))
1541 tree oldargs = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1543 TYPE_ARG_TYPES (TREE_TYPE (olddecl))
1544 = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
1545 types_match = decls_match (newdecl, olddecl);
1546 if (types_match)
1547 return duplicate_decls (newdecl, olddecl,
1548 newdecl_is_friend);
1549 TYPE_ARG_TYPES (TREE_TYPE (olddecl)) = oldargs;
1552 else if (! same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1553 break;
1555 warning_at (DECL_SOURCE_LOCATION (newdecl),
1556 OPT_Wbuiltin_declaration_mismatch,
1557 "declaration of %q+#D conflicts with built-in "
1558 "declaration %q#D", newdecl, olddecl);
1560 else if ((DECL_EXTERN_C_P (newdecl)
1561 && DECL_EXTERN_C_P (olddecl))
1562 || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1563 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
1565 /* A near match; override the builtin. */
1567 if (TREE_PUBLIC (newdecl))
1568 warning_at (DECL_SOURCE_LOCATION (newdecl),
1569 OPT_Wbuiltin_declaration_mismatch,
1570 "new declaration %q#D ambiguates built-in "
1571 "declaration %q#D", newdecl, olddecl);
1572 else
1573 warning (OPT_Wshadow,
1574 DECL_BUILT_IN (olddecl)
1575 ? G_("shadowing built-in function %q#D")
1576 : G_("shadowing library function %q#D"), olddecl);
1578 else
1579 /* Discard the old built-in function. */
1580 return NULL_TREE;
1582 /* Replace the old RTL to avoid problems with inlining. */
1583 COPY_DECL_RTL (newdecl, olddecl);
1585 /* Even if the types match, prefer the new declarations type for
1586 built-ins which have not been explicitly declared, for
1587 exception lists, etc... */
1588 else if (DECL_IS_BUILTIN (olddecl))
1590 tree type = TREE_TYPE (newdecl);
1591 tree attribs = (*targetm.merge_type_attributes)
1592 (TREE_TYPE (olddecl), type);
1594 type = cp_build_type_attribute_variant (type, attribs);
1595 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = type;
1598 /* If a function is explicitly declared "throw ()", propagate that to
1599 the corresponding builtin. */
1600 if (DECL_BUILT_IN_CLASS (olddecl) == BUILT_IN_NORMAL
1601 && DECL_ANTICIPATED (olddecl)
1602 && TREE_NOTHROW (newdecl)
1603 && !TREE_NOTHROW (olddecl))
1605 enum built_in_function fncode = DECL_FUNCTION_CODE (olddecl);
1606 tree tmpdecl = builtin_decl_explicit (fncode);
1607 if (tmpdecl && tmpdecl != olddecl && types_match)
1608 TREE_NOTHROW (tmpdecl) = 1;
1611 /* Whether or not the builtin can throw exceptions has no
1612 bearing on this declarator. */
1613 TREE_NOTHROW (olddecl) = 0;
1615 if (DECL_THIS_STATIC (newdecl) && !DECL_THIS_STATIC (olddecl))
1617 /* If a builtin function is redeclared as `static', merge
1618 the declarations, but make the original one static. */
1619 DECL_THIS_STATIC (olddecl) = 1;
1620 TREE_PUBLIC (olddecl) = 0;
1622 /* Make the old declaration consistent with the new one so
1623 that all remnants of the builtin-ness of this function
1624 will be banished. */
1625 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
1626 COPY_DECL_RTL (newdecl, olddecl);
1629 else if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1631 /* C++ Standard, 3.3, clause 4:
1632 "[Note: a namespace name or a class template name must be unique
1633 in its declarative region (7.3.2, clause 14). ]" */
1634 if (TREE_CODE (olddecl) != NAMESPACE_DECL
1635 && TREE_CODE (newdecl) != NAMESPACE_DECL
1636 && (TREE_CODE (olddecl) != TEMPLATE_DECL
1637 || TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) != TYPE_DECL)
1638 && (TREE_CODE (newdecl) != TEMPLATE_DECL
1639 || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) != TYPE_DECL))
1641 if ((TREE_CODE (olddecl) == TYPE_DECL && DECL_ARTIFICIAL (olddecl)
1642 && TREE_CODE (newdecl) != TYPE_DECL)
1643 || (TREE_CODE (newdecl) == TYPE_DECL && DECL_ARTIFICIAL (newdecl)
1644 && TREE_CODE (olddecl) != TYPE_DECL))
1646 /* We do nothing special here, because C++ does such nasty
1647 things with TYPE_DECLs. Instead, just let the TYPE_DECL
1648 get shadowed, and know that if we need to find a TYPE_DECL
1649 for a given name, we can look in the IDENTIFIER_TYPE_VALUE
1650 slot of the identifier. */
1651 return NULL_TREE;
1654 if ((TREE_CODE (newdecl) == FUNCTION_DECL
1655 && DECL_FUNCTION_TEMPLATE_P (olddecl))
1656 || (TREE_CODE (olddecl) == FUNCTION_DECL
1657 && DECL_FUNCTION_TEMPLATE_P (newdecl)))
1658 return NULL_TREE;
1661 error ("%q#D redeclared as different kind of symbol", newdecl);
1662 if (TREE_CODE (olddecl) == TREE_LIST)
1663 olddecl = TREE_VALUE (olddecl);
1664 inform (DECL_SOURCE_LOCATION (olddecl),
1665 "previous declaration %q#D", olddecl);
1667 return error_mark_node;
1669 else if (!types_match)
1671 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl))
1672 /* These are certainly not duplicate declarations; they're
1673 from different scopes. */
1674 return NULL_TREE;
1676 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1678 /* The name of a class template may not be declared to refer to
1679 any other template, class, function, object, namespace, value,
1680 or type in the same scope. */
1681 if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == TYPE_DECL
1682 || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
1684 error ("conflicting declaration of template %q+#D", newdecl);
1685 inform (DECL_SOURCE_LOCATION (olddecl),
1686 "previous declaration %q#D", olddecl);
1687 return error_mark_node;
1689 else if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == FUNCTION_DECL
1690 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL
1691 && compparms (TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl))),
1692 TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl))))
1693 && comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
1694 DECL_TEMPLATE_PARMS (olddecl))
1695 /* Template functions can be disambiguated by
1696 return type. */
1697 && same_type_p (TREE_TYPE (TREE_TYPE (newdecl)),
1698 TREE_TYPE (TREE_TYPE (olddecl)))
1699 // Template functions can also be disambiguated by
1700 // constraints.
1701 && equivalently_constrained (olddecl, newdecl))
1703 error ("ambiguating new declaration %q+#D", newdecl);
1704 inform (DECL_SOURCE_LOCATION (olddecl),
1705 "old declaration %q#D", olddecl);
1707 else if (check_concept_refinement (olddecl, newdecl))
1708 return error_mark_node;
1709 return NULL_TREE;
1711 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1713 if (DECL_EXTERN_C_P (newdecl) && DECL_EXTERN_C_P (olddecl))
1715 error ("conflicting declaration of C function %q+#D",
1716 newdecl);
1717 inform (DECL_SOURCE_LOCATION (olddecl),
1718 "previous declaration %q#D", olddecl);
1719 return NULL_TREE;
1721 /* For function versions, params and types match, but they
1722 are not ambiguous. */
1723 else if ((!DECL_FUNCTION_VERSIONED (newdecl)
1724 && !DECL_FUNCTION_VERSIONED (olddecl))
1725 // The functions have the same parameter types.
1726 && compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1727 TYPE_ARG_TYPES (TREE_TYPE (olddecl)))
1728 // And the same constraints.
1729 && equivalently_constrained (newdecl, olddecl))
1731 error ("ambiguating new declaration of %q+#D", newdecl);
1732 inform (DECL_SOURCE_LOCATION (olddecl),
1733 "old declaration %q#D", olddecl);
1734 return error_mark_node;
1736 else
1737 return NULL_TREE;
1739 else
1741 error ("conflicting declaration %q+#D", newdecl);
1742 inform (DECL_SOURCE_LOCATION (olddecl),
1743 "previous declaration as %q#D", olddecl);
1744 return error_mark_node;
1747 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1748 && ((DECL_TEMPLATE_SPECIALIZATION (olddecl)
1749 && (!DECL_TEMPLATE_INFO (newdecl)
1750 || (DECL_TI_TEMPLATE (newdecl)
1751 != DECL_TI_TEMPLATE (olddecl))))
1752 || (DECL_TEMPLATE_SPECIALIZATION (newdecl)
1753 && (!DECL_TEMPLATE_INFO (olddecl)
1754 || (DECL_TI_TEMPLATE (olddecl)
1755 != DECL_TI_TEMPLATE (newdecl))))))
1756 /* It's OK to have a template specialization and a non-template
1757 with the same type, or to have specializations of two
1758 different templates with the same type. Note that if one is a
1759 specialization, and the other is an instantiation of the same
1760 template, that we do not exit at this point. That situation
1761 can occur if we instantiate a template class, and then
1762 specialize one of its methods. This situation is valid, but
1763 the declarations must be merged in the usual way. */
1764 return NULL_TREE;
1765 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1766 && ((DECL_TEMPLATE_INSTANTIATION (olddecl)
1767 && !DECL_USE_TEMPLATE (newdecl))
1768 || (DECL_TEMPLATE_INSTANTIATION (newdecl)
1769 && !DECL_USE_TEMPLATE (olddecl))))
1770 /* One of the declarations is a template instantiation, and the
1771 other is not a template at all. That's OK. */
1772 return NULL_TREE;
1773 else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
1775 /* In [namespace.alias] we have:
1777 In a declarative region, a namespace-alias-definition can be
1778 used to redefine a namespace-alias declared in that declarative
1779 region to refer only to the namespace to which it already
1780 refers.
1782 Therefore, if we encounter a second alias directive for the same
1783 alias, we can just ignore the second directive. */
1784 if (DECL_NAMESPACE_ALIAS (newdecl)
1785 && (DECL_NAMESPACE_ALIAS (newdecl)
1786 == DECL_NAMESPACE_ALIAS (olddecl)))
1787 return olddecl;
1788 /* [namespace.alias]
1790 A namespace-name or namespace-alias shall not be declared as
1791 the name of any other entity in the same declarative region.
1792 A namespace-name defined at global scope shall not be
1793 declared as the name of any other entity in any global scope
1794 of the program. */
1795 error ("conflicting declaration of namespace %q+D", newdecl);
1796 inform (DECL_SOURCE_LOCATION (olddecl),
1797 "previous declaration of namespace %qD here", olddecl);
1798 return error_mark_node;
1800 else
1802 const char *errmsg = redeclaration_error_message (newdecl, olddecl);
1803 if (errmsg)
1805 error_at (DECL_SOURCE_LOCATION (newdecl), errmsg, newdecl);
1806 if (DECL_NAME (olddecl) != NULL_TREE)
1807 inform (DECL_SOURCE_LOCATION (olddecl),
1808 (DECL_INITIAL (olddecl) && namespace_bindings_p ())
1809 ? G_("%q#D previously defined here")
1810 : G_("%q#D previously declared here"), olddecl);
1811 return error_mark_node;
1813 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1814 && DECL_INITIAL (olddecl) != NULL_TREE
1815 && !prototype_p (TREE_TYPE (olddecl))
1816 && prototype_p (TREE_TYPE (newdecl)))
1818 /* Prototype decl follows defn w/o prototype. */
1819 if (warning_at (DECL_SOURCE_LOCATION (newdecl), 0,
1820 "prototype specified for %q#D", newdecl))
1821 inform (DECL_SOURCE_LOCATION (olddecl),
1822 "previous non-prototype definition here");
1824 else if (VAR_OR_FUNCTION_DECL_P (olddecl)
1825 && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl))
1827 /* [dcl.link]
1828 If two declarations of the same function or object
1829 specify different linkage-specifications ..., the program
1830 is ill-formed.... Except for functions with C++ linkage,
1831 a function declaration without a linkage specification
1832 shall not precede the first linkage specification for
1833 that function. A function can be declared without a
1834 linkage specification after an explicit linkage
1835 specification has been seen; the linkage explicitly
1836 specified in the earlier declaration is not affected by
1837 such a function declaration.
1839 DR 563 raises the question why the restrictions on
1840 functions should not also apply to objects. Older
1841 versions of G++ silently ignore the linkage-specification
1842 for this example:
1844 namespace N {
1845 extern int i;
1846 extern "C" int i;
1849 which is clearly wrong. Therefore, we now treat objects
1850 like functions. */
1851 if (current_lang_depth () == 0)
1853 /* There is no explicit linkage-specification, so we use
1854 the linkage from the previous declaration. */
1855 if (!DECL_LANG_SPECIFIC (newdecl))
1856 retrofit_lang_decl (newdecl);
1857 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
1859 else
1861 error ("conflicting declaration of %q+#D with %qL linkage",
1862 newdecl, DECL_LANGUAGE (newdecl));
1863 inform (DECL_SOURCE_LOCATION (olddecl),
1864 "previous declaration with %qL linkage",
1865 DECL_LANGUAGE (olddecl));
1869 if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl))
1871 else if (TREE_CODE (olddecl) == FUNCTION_DECL)
1873 /* Note: free functions, as TEMPLATE_DECLs, are handled below. */
1874 if (DECL_FUNCTION_MEMBER_P (olddecl)
1875 && (/* grokfndecl passes member function templates too
1876 as FUNCTION_DECLs. */
1877 DECL_TEMPLATE_INFO (olddecl)
1878 /* C++11 8.3.6/6.
1879 Default arguments for a member function of a class
1880 template shall be specified on the initial declaration
1881 of the member function within the class template. */
1882 || CLASSTYPE_TEMPLATE_INFO (CP_DECL_CONTEXT (olddecl))))
1883 check_redeclaration_no_default_args (newdecl);
1884 else
1886 tree t1 = FUNCTION_FIRST_USER_PARMTYPE (olddecl);
1887 tree t2 = FUNCTION_FIRST_USER_PARMTYPE (newdecl);
1888 int i = 1;
1890 for (; t1 && t1 != void_list_node;
1891 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++)
1892 if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
1894 if (1 == simple_cst_equal (TREE_PURPOSE (t1),
1895 TREE_PURPOSE (t2)))
1897 if (permerror (input_location,
1898 "default argument given for parameter "
1899 "%d of %q#D", i, newdecl))
1900 inform (DECL_SOURCE_LOCATION (olddecl),
1901 "previous specification in %q#D here",
1902 olddecl);
1904 else
1906 error ("default argument given for parameter %d "
1907 "of %q#D", i, newdecl);
1908 inform (DECL_SOURCE_LOCATION (olddecl),
1909 "previous specification in %q#D here",
1910 olddecl);
1917 /* Do not merge an implicit typedef with an explicit one. In:
1919 class A;
1921 typedef class A A __attribute__ ((foo));
1923 the attribute should apply only to the typedef. */
1924 if (TREE_CODE (olddecl) == TYPE_DECL
1925 && (DECL_IMPLICIT_TYPEDEF_P (olddecl)
1926 || DECL_IMPLICIT_TYPEDEF_P (newdecl)))
1927 return NULL_TREE;
1929 /* If new decl is `static' and an `extern' was seen previously,
1930 warn about it. */
1931 warn_extern_redeclared_static (newdecl, olddecl);
1933 if (!validate_constexpr_redeclaration (olddecl, newdecl))
1934 return error_mark_node;
1936 /* We have committed to returning 1 at this point. */
1937 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1939 /* Now that functions must hold information normally held
1940 by field decls, there is extra work to do so that
1941 declaration information does not get destroyed during
1942 definition. */
1943 if (DECL_VINDEX (olddecl))
1944 DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl);
1945 if (DECL_CONTEXT (olddecl))
1946 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1947 DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl);
1948 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1949 DECL_PURE_VIRTUAL_P (newdecl) |= DECL_PURE_VIRTUAL_P (olddecl);
1950 DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl);
1951 DECL_INVALID_OVERRIDER_P (newdecl) |= DECL_INVALID_OVERRIDER_P (olddecl);
1952 DECL_FINAL_P (newdecl) |= DECL_FINAL_P (olddecl);
1953 DECL_OVERRIDE_P (newdecl) |= DECL_OVERRIDE_P (olddecl);
1954 DECL_THIS_STATIC (newdecl) |= DECL_THIS_STATIC (olddecl);
1955 if (DECL_OVERLOADED_OPERATOR_P (olddecl) != ERROR_MARK)
1956 SET_OVERLOADED_OPERATOR_CODE
1957 (newdecl, DECL_OVERLOADED_OPERATOR_P (olddecl));
1958 new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE;
1960 /* Optionally warn about more than one declaration for the same
1961 name, but don't warn about a function declaration followed by a
1962 definition. */
1963 if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl)
1964 && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE)
1965 /* Don't warn about extern decl followed by definition. */
1966 && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl))
1967 /* Don't warn about friends, let add_friend take care of it. */
1968 && ! (newdecl_is_friend || DECL_FRIEND_P (olddecl))
1969 /* Don't warn about declaration followed by specialization. */
1970 && (! DECL_TEMPLATE_SPECIALIZATION (newdecl)
1971 || DECL_TEMPLATE_SPECIALIZATION (olddecl)))
1973 if (warning_at (DECL_SOURCE_LOCATION (newdecl),
1974 OPT_Wredundant_decls,
1975 "redundant redeclaration of %qD in same scope",
1976 newdecl))
1977 inform (DECL_SOURCE_LOCATION (olddecl),
1978 "previous declaration of %qD", olddecl);
1981 if (!(DECL_TEMPLATE_INSTANTIATION (olddecl)
1982 && DECL_TEMPLATE_SPECIALIZATION (newdecl)))
1984 if (DECL_DELETED_FN (newdecl))
1986 error ("deleted definition of %q+D", newdecl);
1987 inform (DECL_SOURCE_LOCATION (olddecl),
1988 "previous declaration of %qD", olddecl);
1990 DECL_DELETED_FN (newdecl) |= DECL_DELETED_FN (olddecl);
1994 /* Deal with C++: must preserve virtual function table size. */
1995 if (TREE_CODE (olddecl) == TYPE_DECL)
1997 tree newtype = TREE_TYPE (newdecl);
1998 tree oldtype = TREE_TYPE (olddecl);
2000 if (newtype != error_mark_node && oldtype != error_mark_node
2001 && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype))
2002 CLASSTYPE_FRIEND_CLASSES (newtype)
2003 = CLASSTYPE_FRIEND_CLASSES (oldtype);
2005 DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl);
2008 /* Copy all the DECL_... slots specified in the new decl
2009 except for any that we copy here from the old type. */
2010 DECL_ATTRIBUTES (newdecl)
2011 = (*targetm.merge_decl_attributes) (olddecl, newdecl);
2013 if (DECL_DECLARES_FUNCTION_P (olddecl) && DECL_DECLARES_FUNCTION_P (newdecl))
2015 olddecl_friend = DECL_FRIEND_P (olddecl);
2016 hidden_friend = (DECL_ANTICIPATED (olddecl)
2017 && DECL_HIDDEN_FRIEND_P (olddecl)
2018 && newdecl_is_friend);
2019 if (!hidden_friend)
2021 DECL_ANTICIPATED (olddecl) = 0;
2022 DECL_HIDDEN_FRIEND_P (olddecl) = 0;
2026 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2028 tree old_result;
2029 tree new_result;
2030 old_result = DECL_TEMPLATE_RESULT (olddecl);
2031 new_result = DECL_TEMPLATE_RESULT (newdecl);
2032 TREE_TYPE (olddecl) = TREE_TYPE (old_result);
2033 DECL_TEMPLATE_SPECIALIZATIONS (olddecl)
2034 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (olddecl),
2035 DECL_TEMPLATE_SPECIALIZATIONS (newdecl));
2037 DECL_ATTRIBUTES (old_result)
2038 = (*targetm.merge_decl_attributes) (old_result, new_result);
2040 if (DECL_FUNCTION_TEMPLATE_P (newdecl))
2042 /* Per C++11 8.3.6/4, default arguments cannot be added in later
2043 declarations of a function template. */
2044 if (DECL_SOURCE_LOCATION (newdecl)
2045 != DECL_SOURCE_LOCATION (olddecl))
2046 check_redeclaration_no_default_args (newdecl);
2048 check_default_args (newdecl);
2050 if (GNU_INLINE_P (old_result) != GNU_INLINE_P (new_result)
2051 && DECL_INITIAL (new_result))
2053 if (DECL_INITIAL (old_result))
2054 DECL_UNINLINABLE (old_result) = 1;
2055 else
2056 DECL_UNINLINABLE (old_result) = DECL_UNINLINABLE (new_result);
2057 DECL_EXTERNAL (old_result) = DECL_EXTERNAL (new_result);
2058 DECL_NOT_REALLY_EXTERN (old_result)
2059 = DECL_NOT_REALLY_EXTERN (new_result);
2060 DECL_INTERFACE_KNOWN (old_result)
2061 = DECL_INTERFACE_KNOWN (new_result);
2062 DECL_DECLARED_INLINE_P (old_result)
2063 = DECL_DECLARED_INLINE_P (new_result);
2064 DECL_DISREGARD_INLINE_LIMITS (old_result)
2065 |= DECL_DISREGARD_INLINE_LIMITS (new_result);
2068 else
2070 DECL_DECLARED_INLINE_P (old_result)
2071 |= DECL_DECLARED_INLINE_P (new_result);
2072 DECL_DISREGARD_INLINE_LIMITS (old_result)
2073 |= DECL_DISREGARD_INLINE_LIMITS (new_result);
2074 check_redeclaration_exception_specification (newdecl, olddecl);
2078 /* If the new declaration is a definition, update the file and
2079 line information on the declaration, and also make
2080 the old declaration the same definition. */
2081 if (DECL_INITIAL (new_result) != NULL_TREE)
2083 DECL_SOURCE_LOCATION (olddecl)
2084 = DECL_SOURCE_LOCATION (old_result)
2085 = DECL_SOURCE_LOCATION (newdecl);
2086 DECL_INITIAL (old_result) = DECL_INITIAL (new_result);
2087 if (DECL_FUNCTION_TEMPLATE_P (newdecl))
2089 tree parm;
2090 DECL_ARGUMENTS (old_result)
2091 = DECL_ARGUMENTS (new_result);
2092 for (parm = DECL_ARGUMENTS (old_result); parm;
2093 parm = DECL_CHAIN (parm))
2094 DECL_CONTEXT (parm) = old_result;
2098 return olddecl;
2101 if (types_match)
2103 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2104 check_redeclaration_exception_specification (newdecl, olddecl);
2106 /* Automatically handles default parameters. */
2107 tree oldtype = TREE_TYPE (olddecl);
2108 tree newtype;
2110 /* For typedefs use the old type, as the new type's DECL_NAME points
2111 at newdecl, which will be ggc_freed. */
2112 if (TREE_CODE (newdecl) == TYPE_DECL)
2114 /* But NEWTYPE might have an attribute, honor that. */
2115 tree tem = TREE_TYPE (newdecl);
2116 newtype = oldtype;
2118 if (TYPE_USER_ALIGN (tem))
2120 if (TYPE_ALIGN (tem) > TYPE_ALIGN (newtype))
2121 SET_TYPE_ALIGN (newtype, TYPE_ALIGN (tem));
2122 TYPE_USER_ALIGN (newtype) = true;
2125 /* And remove the new type from the variants list. */
2126 if (TYPE_NAME (TREE_TYPE (newdecl)) == newdecl)
2128 tree remove = TREE_TYPE (newdecl);
2129 for (tree t = TYPE_MAIN_VARIANT (remove); ;
2130 t = TYPE_NEXT_VARIANT (t))
2131 if (TYPE_NEXT_VARIANT (t) == remove)
2133 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (remove);
2134 break;
2138 else
2139 /* Merge the data types specified in the two decls. */
2140 newtype = merge_types (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
2142 if (VAR_P (newdecl))
2144 DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl);
2145 /* For already initialized vars, TREE_READONLY could have been
2146 cleared in cp_finish_decl, because the var needs runtime
2147 initialization or destruction. Make sure not to set
2148 TREE_READONLY on it again. */
2149 if (DECL_INITIALIZED_P (olddecl)
2150 && !DECL_EXTERNAL (olddecl)
2151 && !TREE_READONLY (olddecl))
2152 TREE_READONLY (newdecl) = 0;
2153 DECL_INITIALIZED_P (newdecl) |= DECL_INITIALIZED_P (olddecl);
2154 DECL_NONTRIVIALLY_INITIALIZED_P (newdecl)
2155 |= DECL_NONTRIVIALLY_INITIALIZED_P (olddecl);
2156 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (newdecl)
2157 |= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (olddecl);
2158 if (DECL_CLASS_SCOPE_P (olddecl))
2159 DECL_DECLARED_CONSTEXPR_P (newdecl)
2160 |= DECL_DECLARED_CONSTEXPR_P (olddecl);
2162 /* Merge the threadprivate attribute from OLDDECL into NEWDECL. */
2163 if (DECL_LANG_SPECIFIC (olddecl)
2164 && CP_DECL_THREADPRIVATE_P (olddecl))
2166 /* Allocate a LANG_SPECIFIC structure for NEWDECL, if needed. */
2167 if (!DECL_LANG_SPECIFIC (newdecl))
2168 retrofit_lang_decl (newdecl);
2170 CP_DECL_THREADPRIVATE_P (newdecl) = 1;
2174 /* An explicit specialization of a function template or of a member
2175 function of a class template can be declared transaction_safe
2176 independently of whether the corresponding template entity is declared
2177 transaction_safe. */
2178 if (flag_tm && TREE_CODE (newdecl) == FUNCTION_DECL
2179 && DECL_TEMPLATE_INSTANTIATION (olddecl)
2180 && DECL_TEMPLATE_SPECIALIZATION (newdecl)
2181 && tx_safe_fn_type_p (newtype)
2182 && !tx_safe_fn_type_p (TREE_TYPE (newdecl)))
2183 newtype = tx_unsafe_fn_variant (newtype);
2185 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype;
2187 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2188 check_default_args (newdecl);
2190 /* Lay the type out, unless already done. */
2191 if (! same_type_p (newtype, oldtype)
2192 && TREE_TYPE (newdecl) != error_mark_node
2193 && !(processing_template_decl && uses_template_parms (newdecl)))
2194 layout_type (TREE_TYPE (newdecl));
2196 if ((VAR_P (newdecl)
2197 || TREE_CODE (newdecl) == PARM_DECL
2198 || TREE_CODE (newdecl) == RESULT_DECL
2199 || TREE_CODE (newdecl) == FIELD_DECL
2200 || TREE_CODE (newdecl) == TYPE_DECL)
2201 && !(processing_template_decl && uses_template_parms (newdecl)))
2202 layout_decl (newdecl, 0);
2204 /* Merge the type qualifiers. */
2205 if (TREE_READONLY (newdecl))
2206 TREE_READONLY (olddecl) = 1;
2207 if (TREE_THIS_VOLATILE (newdecl))
2208 TREE_THIS_VOLATILE (olddecl) = 1;
2209 if (TREE_NOTHROW (newdecl))
2210 TREE_NOTHROW (olddecl) = 1;
2212 /* Merge deprecatedness. */
2213 if (TREE_DEPRECATED (newdecl))
2214 TREE_DEPRECATED (olddecl) = 1;
2216 /* Preserve function specific target and optimization options */
2217 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2219 if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
2220 && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
2221 DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
2222 = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
2224 if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
2225 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
2226 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
2227 = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
2230 /* Merge the initialization information. */
2231 if (DECL_INITIAL (newdecl) == NULL_TREE
2232 && DECL_INITIAL (olddecl) != NULL_TREE)
2234 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2235 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2236 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2238 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
2239 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
2243 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2245 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
2246 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
2247 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
2248 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
2249 TREE_NOTHROW (newdecl) |= TREE_NOTHROW (olddecl);
2250 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
2251 DECL_IS_OPERATOR_NEW (newdecl) |= DECL_IS_OPERATOR_NEW (olddecl);
2252 DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
2253 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
2254 DECL_LOOPING_CONST_OR_PURE_P (newdecl)
2255 |= DECL_LOOPING_CONST_OR_PURE_P (olddecl);
2256 /* Keep the old RTL. */
2257 COPY_DECL_RTL (olddecl, newdecl);
2259 else if (VAR_P (newdecl)
2260 && (DECL_SIZE (olddecl) || !DECL_SIZE (newdecl)))
2262 /* Keep the old RTL. We cannot keep the old RTL if the old
2263 declaration was for an incomplete object and the new
2264 declaration is not since many attributes of the RTL will
2265 change. */
2266 COPY_DECL_RTL (olddecl, newdecl);
2269 /* If cannot merge, then use the new type and qualifiers,
2270 and don't preserve the old rtl. */
2271 else
2273 /* Clean out any memory we had of the old declaration. */
2274 tree oldstatic = value_member (olddecl, static_aggregates);
2275 if (oldstatic)
2276 TREE_VALUE (oldstatic) = error_mark_node;
2278 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
2279 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
2280 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
2281 TREE_NOTHROW (olddecl) = TREE_NOTHROW (newdecl);
2282 TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
2285 /* Merge the storage class information. */
2286 merge_weak (newdecl, olddecl);
2288 DECL_DEFER_OUTPUT (newdecl) |= DECL_DEFER_OUTPUT (olddecl);
2289 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
2290 TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl);
2291 if (! DECL_EXTERNAL (olddecl))
2292 DECL_EXTERNAL (newdecl) = 0;
2293 if (! DECL_COMDAT (olddecl))
2294 DECL_COMDAT (newdecl) = 0;
2296 new_template_info = NULL_TREE;
2297 if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl))
2299 bool new_redefines_gnu_inline = false;
2301 if (new_defines_function
2302 && ((DECL_INTERFACE_KNOWN (olddecl)
2303 && TREE_CODE (olddecl) == FUNCTION_DECL)
2304 || (TREE_CODE (olddecl) == TEMPLATE_DECL
2305 && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl))
2306 == FUNCTION_DECL))))
2308 tree fn = olddecl;
2310 if (TREE_CODE (fn) == TEMPLATE_DECL)
2311 fn = DECL_TEMPLATE_RESULT (olddecl);
2313 new_redefines_gnu_inline = GNU_INLINE_P (fn) && DECL_INITIAL (fn);
2316 if (!new_redefines_gnu_inline)
2318 DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl);
2319 DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl);
2320 DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl);
2322 DECL_TEMPLATE_INSTANTIATED (newdecl)
2323 |= DECL_TEMPLATE_INSTANTIATED (olddecl);
2324 DECL_ODR_USED (newdecl) |= DECL_ODR_USED (olddecl);
2326 /* If the OLDDECL is an instantiation and/or specialization,
2327 then the NEWDECL must be too. But, it may not yet be marked
2328 as such if the caller has created NEWDECL, but has not yet
2329 figured out that it is a redeclaration. */
2330 if (!DECL_USE_TEMPLATE (newdecl))
2331 DECL_USE_TEMPLATE (newdecl) = DECL_USE_TEMPLATE (olddecl);
2333 /* Don't really know how much of the language-specific
2334 values we should copy from old to new. */
2335 DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl);
2336 DECL_REPO_AVAILABLE_P (newdecl) = DECL_REPO_AVAILABLE_P (olddecl);
2337 DECL_INITIALIZED_IN_CLASS_P (newdecl)
2338 |= DECL_INITIALIZED_IN_CLASS_P (olddecl);
2340 if (LANG_DECL_HAS_MIN (newdecl))
2342 DECL_LANG_SPECIFIC (newdecl)->u.min.u2 =
2343 DECL_LANG_SPECIFIC (olddecl)->u.min.u2;
2344 if (DECL_TEMPLATE_INFO (newdecl))
2346 new_template_info = DECL_TEMPLATE_INFO (newdecl);
2347 if (DECL_TEMPLATE_INSTANTIATION (olddecl)
2348 && DECL_TEMPLATE_SPECIALIZATION (newdecl))
2349 /* Remember the presence of explicit specialization args. */
2350 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (olddecl))
2351 = TINFO_USED_TEMPLATE_ID (new_template_info);
2353 DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl);
2355 /* Only functions have these fields. */
2356 if (DECL_DECLARES_FUNCTION_P (newdecl))
2358 DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl);
2359 DECL_BEFRIENDING_CLASSES (newdecl)
2360 = chainon (DECL_BEFRIENDING_CLASSES (newdecl),
2361 DECL_BEFRIENDING_CLASSES (olddecl));
2362 /* DECL_THUNKS is only valid for virtual functions,
2363 otherwise it is a DECL_FRIEND_CONTEXT. */
2364 if (DECL_VIRTUAL_P (newdecl))
2365 SET_DECL_THUNKS (newdecl, DECL_THUNKS (olddecl));
2367 /* Only variables have this field. */
2368 else if (VAR_P (newdecl)
2369 && VAR_HAD_UNKNOWN_BOUND (olddecl))
2370 SET_VAR_HAD_UNKNOWN_BOUND (newdecl);
2373 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2375 tree parm;
2377 /* Merge parameter attributes. */
2378 tree oldarg, newarg;
2379 for (oldarg = DECL_ARGUMENTS(olddecl),
2380 newarg = DECL_ARGUMENTS(newdecl);
2381 oldarg && newarg;
2382 oldarg = DECL_CHAIN(oldarg), newarg = DECL_CHAIN(newarg)) {
2383 DECL_ATTRIBUTES (newarg)
2384 = (*targetm.merge_decl_attributes) (oldarg, newarg);
2385 DECL_ATTRIBUTES (oldarg) = DECL_ATTRIBUTES (newarg);
2388 if (DECL_TEMPLATE_INSTANTIATION (olddecl)
2389 && !DECL_TEMPLATE_INSTANTIATION (newdecl))
2391 /* If newdecl is not a specialization, then it is not a
2392 template-related function at all. And that means that we
2393 should have exited above, returning 0. */
2394 gcc_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl));
2396 if (DECL_ODR_USED (olddecl))
2397 /* From [temp.expl.spec]:
2399 If a template, a member template or the member of a class
2400 template is explicitly specialized then that
2401 specialization shall be declared before the first use of
2402 that specialization that would cause an implicit
2403 instantiation to take place, in every translation unit in
2404 which such a use occurs. */
2405 error ("explicit specialization of %qD after first use",
2406 olddecl);
2408 SET_DECL_TEMPLATE_SPECIALIZATION (olddecl);
2409 DECL_COMDAT (newdecl) = (TREE_PUBLIC (newdecl)
2410 && DECL_DECLARED_INLINE_P (newdecl));
2412 /* Don't propagate visibility from the template to the
2413 specialization here. We'll do that in determine_visibility if
2414 appropriate. */
2415 DECL_VISIBILITY_SPECIFIED (olddecl) = 0;
2417 /* [temp.expl.spec/14] We don't inline explicit specialization
2418 just because the primary template says so. */
2420 /* But still keep DECL_DISREGARD_INLINE_LIMITS in sync with
2421 the always_inline attribute. */
2422 if (DECL_DISREGARD_INLINE_LIMITS (olddecl)
2423 && !DECL_DISREGARD_INLINE_LIMITS (newdecl))
2425 if (DECL_DECLARED_INLINE_P (newdecl))
2426 DECL_DISREGARD_INLINE_LIMITS (newdecl) = true;
2427 else
2428 DECL_ATTRIBUTES (newdecl)
2429 = remove_attribute ("always_inline",
2430 DECL_ATTRIBUTES (newdecl));
2433 else if (new_defines_function && DECL_INITIAL (olddecl))
2435 /* Never inline re-defined extern inline functions.
2436 FIXME: this could be better handled by keeping both
2437 function as separate declarations. */
2438 DECL_UNINLINABLE (newdecl) = 1;
2440 else
2442 if (DECL_PENDING_INLINE_P (olddecl))
2444 DECL_PENDING_INLINE_P (newdecl) = 1;
2445 DECL_PENDING_INLINE_INFO (newdecl)
2446 = DECL_PENDING_INLINE_INFO (olddecl);
2448 else if (DECL_PENDING_INLINE_P (newdecl))
2450 else if (DECL_SAVED_FUNCTION_DATA (newdecl) == NULL)
2451 DECL_SAVED_FUNCTION_DATA (newdecl)
2452 = DECL_SAVED_FUNCTION_DATA (olddecl);
2454 DECL_DECLARED_INLINE_P (newdecl) |= DECL_DECLARED_INLINE_P (olddecl);
2456 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
2457 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
2459 DECL_DISREGARD_INLINE_LIMITS (newdecl)
2460 = DECL_DISREGARD_INLINE_LIMITS (olddecl)
2461 = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
2462 || DECL_DISREGARD_INLINE_LIMITS (olddecl));
2465 /* Preserve abstractness on cloned [cd]tors. */
2466 DECL_ABSTRACT_P (newdecl) = DECL_ABSTRACT_P (olddecl);
2468 /* Update newdecl's parms to point at olddecl. */
2469 for (parm = DECL_ARGUMENTS (newdecl); parm;
2470 parm = DECL_CHAIN (parm))
2471 DECL_CONTEXT (parm) = olddecl;
2473 if (! types_match)
2475 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
2476 COPY_DECL_ASSEMBLER_NAME (newdecl, olddecl);
2477 COPY_DECL_RTL (newdecl, olddecl);
2479 if (! types_match || new_defines_function)
2481 /* These need to be copied so that the names are available.
2482 Note that if the types do match, we'll preserve inline
2483 info and other bits, but if not, we won't. */
2484 DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl);
2485 DECL_RESULT (olddecl) = DECL_RESULT (newdecl);
2487 /* If redeclaring a builtin function, it stays built in
2488 if newdecl is a gnu_inline definition, or if newdecl is just
2489 a declaration. */
2490 if (DECL_BUILT_IN (olddecl)
2491 && (new_defines_function ? GNU_INLINE_P (newdecl) : types_match))
2493 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
2494 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
2495 /* If we're keeping the built-in definition, keep the rtl,
2496 regardless of declaration matches. */
2497 COPY_DECL_RTL (olddecl, newdecl);
2498 if (DECL_BUILT_IN_CLASS (newdecl) == BUILT_IN_NORMAL)
2500 enum built_in_function fncode = DECL_FUNCTION_CODE (newdecl);
2501 switch (fncode)
2503 /* If a compatible prototype of these builtin functions
2504 is seen, assume the runtime implements it with the
2505 expected semantics. */
2506 case BUILT_IN_STPCPY:
2507 if (builtin_decl_explicit_p (fncode))
2508 set_builtin_decl_implicit_p (fncode, true);
2509 break;
2510 default:
2511 if (builtin_decl_explicit_p (fncode))
2512 set_builtin_decl_declared_p (fncode, true);
2513 break;
2517 if (new_defines_function)
2518 /* If defining a function declared with other language
2519 linkage, use the previously declared language linkage. */
2520 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
2521 else if (types_match)
2523 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
2524 /* Don't clear out the arguments if we're just redeclaring a
2525 function. */
2526 if (DECL_ARGUMENTS (olddecl))
2527 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
2530 else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
2531 NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl);
2533 /* Now preserve various other info from the definition. */
2534 TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl);
2535 TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl);
2536 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
2537 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
2539 /* Warn about conflicting visibility specifications. */
2540 if (DECL_VISIBILITY_SPECIFIED (olddecl)
2541 && DECL_VISIBILITY_SPECIFIED (newdecl)
2542 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
2544 if (warning_at (DECL_SOURCE_LOCATION (newdecl), OPT_Wattributes,
2545 "%qD: visibility attribute ignored because it "
2546 "conflicts with previous declaration", newdecl))
2547 inform (DECL_SOURCE_LOCATION (olddecl),
2548 "previous declaration of %qD", olddecl);
2550 /* Choose the declaration which specified visibility. */
2551 if (DECL_VISIBILITY_SPECIFIED (olddecl))
2553 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
2554 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
2556 /* Init priority used to be merged from newdecl to olddecl by the memcpy,
2557 so keep this behavior. */
2558 if (VAR_P (newdecl) && DECL_HAS_INIT_PRIORITY_P (newdecl))
2560 SET_DECL_INIT_PRIORITY (olddecl, DECL_INIT_PRIORITY (newdecl));
2561 DECL_HAS_INIT_PRIORITY_P (olddecl) = 1;
2563 /* Likewise for DECL_ALIGN, DECL_USER_ALIGN and DECL_PACKED. */
2564 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
2566 SET_DECL_ALIGN (newdecl, DECL_ALIGN (olddecl));
2567 DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
2569 DECL_USER_ALIGN (olddecl) = DECL_USER_ALIGN (newdecl);
2570 if (TREE_CODE (newdecl) == FIELD_DECL)
2571 DECL_PACKED (olddecl) = DECL_PACKED (newdecl);
2573 /* The DECL_LANG_SPECIFIC information in OLDDECL will be replaced
2574 with that from NEWDECL below. */
2575 if (DECL_LANG_SPECIFIC (olddecl))
2577 gcc_assert (DECL_LANG_SPECIFIC (olddecl)
2578 != DECL_LANG_SPECIFIC (newdecl));
2579 ggc_free (DECL_LANG_SPECIFIC (olddecl));
2582 /* Merge the USED information. */
2583 if (TREE_USED (olddecl))
2584 TREE_USED (newdecl) = 1;
2585 else if (TREE_USED (newdecl))
2586 TREE_USED (olddecl) = 1;
2587 if (VAR_P (newdecl))
2589 if (DECL_READ_P (olddecl))
2590 DECL_READ_P (newdecl) = 1;
2591 else if (DECL_READ_P (newdecl))
2592 DECL_READ_P (olddecl) = 1;
2594 if (DECL_PRESERVE_P (olddecl))
2595 DECL_PRESERVE_P (newdecl) = 1;
2596 else if (DECL_PRESERVE_P (newdecl))
2597 DECL_PRESERVE_P (olddecl) = 1;
2599 /* Merge the DECL_FUNCTION_VERSIONED information. newdecl will be copied
2600 to olddecl and deleted. */
2601 if (TREE_CODE (newdecl) == FUNCTION_DECL
2602 && DECL_FUNCTION_VERSIONED (olddecl))
2604 /* Set the flag for newdecl so that it gets copied to olddecl. */
2605 DECL_FUNCTION_VERSIONED (newdecl) = 1;
2606 /* newdecl will be purged after copying to olddecl and is no longer
2607 a version. */
2608 cgraph_node::delete_function_version (newdecl);
2611 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2613 int function_size;
2614 struct symtab_node *snode = symtab_node::get (olddecl);
2616 function_size = sizeof (struct tree_decl_common);
2618 memcpy ((char *) olddecl + sizeof (struct tree_common),
2619 (char *) newdecl + sizeof (struct tree_common),
2620 function_size - sizeof (struct tree_common));
2622 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2623 (char *) newdecl + sizeof (struct tree_decl_common),
2624 sizeof (struct tree_function_decl) - sizeof (struct tree_decl_common));
2626 /* Preserve symtab node mapping. */
2627 olddecl->decl_with_vis.symtab_node = snode;
2629 if (new_template_info)
2630 /* If newdecl is a template instantiation, it is possible that
2631 the following sequence of events has occurred:
2633 o A friend function was declared in a class template. The
2634 class template was instantiated.
2636 o The instantiation of the friend declaration was
2637 recorded on the instantiation list, and is newdecl.
2639 o Later, however, instantiate_class_template called pushdecl
2640 on the newdecl to perform name injection. But, pushdecl in
2641 turn called duplicate_decls when it discovered that another
2642 declaration of a global function with the same name already
2643 existed.
2645 o Here, in duplicate_decls, we decided to clobber newdecl.
2647 If we're going to do that, we'd better make sure that
2648 olddecl, and not newdecl, is on the list of
2649 instantiations so that if we try to do the instantiation
2650 again we won't get the clobbered declaration. */
2651 reregister_specialization (newdecl,
2652 new_template_info,
2653 olddecl);
2655 else
2657 size_t size = tree_code_size (TREE_CODE (newdecl));
2659 memcpy ((char *) olddecl + sizeof (struct tree_common),
2660 (char *) newdecl + sizeof (struct tree_common),
2661 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
2662 switch (TREE_CODE (newdecl))
2664 case LABEL_DECL:
2665 case VAR_DECL:
2666 case RESULT_DECL:
2667 case PARM_DECL:
2668 case FIELD_DECL:
2669 case TYPE_DECL:
2670 case CONST_DECL:
2672 struct symtab_node *snode = NULL;
2674 if (VAR_P (newdecl)
2675 && (TREE_STATIC (olddecl) || TREE_PUBLIC (olddecl)
2676 || DECL_EXTERNAL (olddecl)))
2677 snode = symtab_node::get (olddecl);
2678 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2679 (char *) newdecl + sizeof (struct tree_decl_common),
2680 size - sizeof (struct tree_decl_common)
2681 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
2682 if (VAR_P (newdecl))
2683 olddecl->decl_with_vis.symtab_node = snode;
2685 break;
2686 default:
2687 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2688 (char *) newdecl + sizeof (struct tree_decl_common),
2689 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common)
2690 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
2691 break;
2695 if (VAR_OR_FUNCTION_DECL_P (newdecl))
2697 if (DECL_EXTERNAL (olddecl)
2698 || TREE_PUBLIC (olddecl)
2699 || TREE_STATIC (olddecl))
2701 /* Merge the section attribute.
2702 We want to issue an error if the sections conflict but that must be
2703 done later in decl_attributes since we are called before attributes
2704 are assigned. */
2705 if (DECL_SECTION_NAME (newdecl) != NULL)
2706 set_decl_section_name (olddecl, DECL_SECTION_NAME (newdecl));
2708 if (DECL_ONE_ONLY (newdecl))
2710 struct symtab_node *oldsym, *newsym;
2711 if (TREE_CODE (olddecl) == FUNCTION_DECL)
2712 oldsym = cgraph_node::get_create (olddecl);
2713 else
2714 oldsym = varpool_node::get_create (olddecl);
2715 newsym = symtab_node::get (newdecl);
2716 oldsym->set_comdat_group (newsym->get_comdat_group ());
2720 if (VAR_P (newdecl)
2721 && CP_DECL_THREAD_LOCAL_P (newdecl))
2723 CP_DECL_THREAD_LOCAL_P (olddecl) = true;
2724 if (!processing_template_decl)
2725 set_decl_tls_model (olddecl, DECL_TLS_MODEL (newdecl));
2729 DECL_UID (olddecl) = olddecl_uid;
2730 if (olddecl_friend)
2731 DECL_FRIEND_P (olddecl) = 1;
2732 if (hidden_friend)
2734 DECL_ANTICIPATED (olddecl) = 1;
2735 DECL_HIDDEN_FRIEND_P (olddecl) = 1;
2738 /* NEWDECL contains the merged attribute lists.
2739 Update OLDDECL to be the same. */
2740 DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
2742 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
2743 so that encode_section_info has a chance to look at the new decl
2744 flags and attributes. */
2745 if (DECL_RTL_SET_P (olddecl)
2746 && (TREE_CODE (olddecl) == FUNCTION_DECL
2747 || (VAR_P (olddecl)
2748 && TREE_STATIC (olddecl))))
2749 make_decl_rtl (olddecl);
2751 /* The NEWDECL will no longer be needed. Because every out-of-class
2752 declaration of a member results in a call to duplicate_decls,
2753 freeing these nodes represents in a significant savings.
2755 Before releasing the node, be sore to remove function from symbol
2756 table that might have been inserted there to record comdat group.
2757 Be sure to however do not free DECL_STRUCT_FUNCTION because this
2758 structure is shared in between newdecl and oldecl. */
2759 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2760 DECL_STRUCT_FUNCTION (newdecl) = NULL;
2761 if (VAR_OR_FUNCTION_DECL_P (newdecl))
2763 struct symtab_node *snode = symtab_node::get (newdecl);
2764 if (snode)
2765 snode->remove ();
2768 /* Remove the associated constraints for newdecl, if any, before
2769 reclaiming memory. */
2770 if (flag_concepts)
2771 remove_constraints (newdecl);
2773 ggc_free (newdecl);
2775 return olddecl;
2778 /* Return zero if the declaration NEWDECL is valid
2779 when the declaration OLDDECL (assumed to be for the same name)
2780 has already been seen.
2781 Otherwise return an error message format string with a %s
2782 where the identifier should go. */
2784 static const char *
2785 redeclaration_error_message (tree newdecl, tree olddecl)
2787 if (TREE_CODE (newdecl) == TYPE_DECL)
2789 /* Because C++ can put things into name space for free,
2790 constructs like "typedef struct foo { ... } foo"
2791 would look like an erroneous redeclaration. */
2792 if (same_type_p (TREE_TYPE (newdecl), TREE_TYPE (olddecl)))
2793 return NULL;
2794 else
2795 return G_("redefinition of %q#D");
2797 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
2799 /* If this is a pure function, its olddecl will actually be
2800 the original initialization to `0' (which we force to call
2801 abort()). Don't complain about redefinition in this case. */
2802 if (DECL_LANG_SPECIFIC (olddecl) && DECL_PURE_VIRTUAL_P (olddecl)
2803 && DECL_INITIAL (olddecl) == NULL_TREE)
2804 return NULL;
2806 /* If both functions come from different namespaces, this is not
2807 a redeclaration - this is a conflict with a used function. */
2808 if (DECL_NAMESPACE_SCOPE_P (olddecl)
2809 && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl)
2810 && ! decls_match (olddecl, newdecl))
2811 return G_("%qD conflicts with used function");
2813 /* We'll complain about linkage mismatches in
2814 warn_extern_redeclared_static. */
2816 /* Defining the same name twice is no good. */
2817 if (decl_defined_p (olddecl)
2818 && decl_defined_p (newdecl))
2820 if (DECL_NAME (olddecl) == NULL_TREE)
2821 return G_("%q#D not declared in class");
2822 else if (!GNU_INLINE_P (olddecl)
2823 || GNU_INLINE_P (newdecl))
2824 return G_("redefinition of %q#D");
2827 if (DECL_DECLARED_INLINE_P (olddecl) && DECL_DECLARED_INLINE_P (newdecl))
2829 bool olda = GNU_INLINE_P (olddecl);
2830 bool newa = GNU_INLINE_P (newdecl);
2832 if (olda != newa)
2834 if (newa)
2835 return G_("%q+D redeclared inline with "
2836 "%<gnu_inline%> attribute");
2837 else
2838 return G_("%q+D redeclared inline without "
2839 "%<gnu_inline%> attribute");
2843 check_abi_tag_redeclaration
2844 (olddecl, lookup_attribute ("abi_tag", DECL_ATTRIBUTES (olddecl)),
2845 lookup_attribute ("abi_tag", DECL_ATTRIBUTES (newdecl)));
2847 return NULL;
2849 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2851 tree nt, ot;
2853 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
2855 if (COMPLETE_TYPE_P (TREE_TYPE (newdecl))
2856 && COMPLETE_TYPE_P (TREE_TYPE (olddecl)))
2857 return G_("redefinition of %q#D");
2858 return NULL;
2861 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) != FUNCTION_DECL
2862 || (DECL_TEMPLATE_RESULT (newdecl)
2863 == DECL_TEMPLATE_RESULT (olddecl)))
2864 return NULL;
2866 nt = DECL_TEMPLATE_RESULT (newdecl);
2867 if (DECL_TEMPLATE_INFO (nt))
2868 nt = DECL_TEMPLATE_RESULT (template_for_substitution (nt));
2869 ot = DECL_TEMPLATE_RESULT (olddecl);
2870 if (DECL_TEMPLATE_INFO (ot))
2871 ot = DECL_TEMPLATE_RESULT (template_for_substitution (ot));
2872 if (DECL_INITIAL (nt) && DECL_INITIAL (ot)
2873 && (!GNU_INLINE_P (ot) || GNU_INLINE_P (nt)))
2874 return G_("redefinition of %q#D");
2876 if (DECL_DECLARED_INLINE_P (ot) && DECL_DECLARED_INLINE_P (nt))
2878 bool olda = GNU_INLINE_P (ot);
2879 bool newa = GNU_INLINE_P (nt);
2881 if (olda != newa)
2883 if (newa)
2884 return G_("%q+D redeclared inline with "
2885 "%<gnu_inline%> attribute");
2886 else
2887 return G_("%q+D redeclared inline without "
2888 "%<gnu_inline%> attribute");
2892 /* Core issue #226 (C++0x):
2894 If a friend function template declaration specifies a
2895 default template-argument, that declaration shall be a
2896 definition and shall be the only declaration of the
2897 function template in the translation unit. */
2898 if ((cxx_dialect != cxx98)
2899 && TREE_CODE (ot) == FUNCTION_DECL && DECL_FRIEND_P (ot)
2900 && !check_default_tmpl_args (nt, DECL_TEMPLATE_PARMS (newdecl),
2901 /*is_primary=*/true,
2902 /*is_partial=*/false,
2903 /*is_friend_decl=*/2))
2904 return G_("redeclaration of friend %q#D "
2905 "may not have default template arguments");
2907 return NULL;
2909 else if (VAR_P (newdecl)
2910 && CP_DECL_THREAD_LOCAL_P (newdecl) != CP_DECL_THREAD_LOCAL_P (olddecl)
2911 && (! DECL_LANG_SPECIFIC (olddecl)
2912 || ! CP_DECL_THREADPRIVATE_P (olddecl)
2913 || CP_DECL_THREAD_LOCAL_P (newdecl)))
2915 /* Only variables can be thread-local, and all declarations must
2916 agree on this property. */
2917 if (CP_DECL_THREAD_LOCAL_P (newdecl))
2918 return G_("thread-local declaration of %q#D follows "
2919 "non-thread-local declaration");
2920 else
2921 return G_("non-thread-local declaration of %q#D follows "
2922 "thread-local declaration");
2924 else if (toplevel_bindings_p () || DECL_NAMESPACE_SCOPE_P (newdecl))
2926 /* The objects have been declared at namespace scope. If either
2927 is a member of an anonymous union, then this is an invalid
2928 redeclaration. For example:
2930 int i;
2931 union { int i; };
2933 is invalid. */
2934 if ((VAR_P (newdecl) && DECL_ANON_UNION_VAR_P (newdecl))
2935 || (VAR_P (olddecl) && DECL_ANON_UNION_VAR_P (olddecl)))
2936 return G_("redeclaration of %q#D");
2937 /* If at least one declaration is a reference, there is no
2938 conflict. For example:
2940 int i = 3;
2941 extern int i;
2943 is valid. */
2944 if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
2945 return NULL;
2947 /* Static data member declared outside a class definition
2948 if the variable is defined within the class with constexpr
2949 specifier is declaration rather than definition (and
2950 deprecated). */
2951 if (cxx_dialect >= cxx1z
2952 && DECL_CLASS_SCOPE_P (olddecl)
2953 && DECL_DECLARED_CONSTEXPR_P (olddecl)
2954 && !DECL_INITIAL (newdecl))
2956 DECL_EXTERNAL (newdecl) = 1;
2957 /* For now, only warn with explicit -Wdeprecated. */
2958 if (global_options_set.x_warn_deprecated
2959 && warning_at (DECL_SOURCE_LOCATION (newdecl), OPT_Wdeprecated,
2960 "redundant redeclaration of %<constexpr%> static "
2961 "data member %qD", newdecl))
2962 inform (DECL_SOURCE_LOCATION (olddecl),
2963 "previous declaration of %qD", olddecl);
2964 return NULL;
2967 /* Reject two definitions. */
2968 return G_("redefinition of %q#D");
2970 else
2972 /* Objects declared with block scope: */
2973 /* Reject two definitions, and reject a definition
2974 together with an external reference. */
2975 if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl)))
2976 return G_("redeclaration of %q#D");
2977 return NULL;
2981 /* Hash and equality functions for the named_label table. */
2983 hashval_t
2984 named_label_hasher::hash (named_label_entry *ent)
2986 return DECL_UID (ent->label_decl);
2989 bool
2990 named_label_hasher::equal (named_label_entry *a, named_label_entry *b)
2992 return a->label_decl == b->label_decl;
2995 /* Create a new label, named ID. */
2997 static tree
2998 make_label_decl (tree id, int local_p)
3000 struct named_label_entry *ent;
3001 tree decl;
3003 decl = build_decl (input_location, LABEL_DECL, id, void_type_node);
3005 DECL_CONTEXT (decl) = current_function_decl;
3006 SET_DECL_MODE (decl, VOIDmode);
3007 C_DECLARED_LABEL_FLAG (decl) = local_p;
3009 /* Say where one reference is to the label, for the sake of the
3010 error if it is not defined. */
3011 DECL_SOURCE_LOCATION (decl) = input_location;
3013 /* Record the fact that this identifier is bound to this label. */
3014 SET_IDENTIFIER_LABEL_VALUE (id, decl);
3016 /* Create the label htab for the function on demand. */
3017 if (!named_labels)
3018 named_labels = hash_table<named_label_hasher>::create_ggc (13);
3020 /* Record this label on the list of labels used in this function.
3021 We do this before calling make_label_decl so that we get the
3022 IDENTIFIER_LABEL_VALUE before the new label is declared. */
3023 ent = ggc_cleared_alloc<named_label_entry> ();
3024 ent->label_decl = decl;
3026 named_label_entry **slot = named_labels->find_slot (ent, INSERT);
3027 gcc_assert (*slot == NULL);
3028 *slot = ent;
3030 return decl;
3033 /* Look for a label named ID in the current function. If one cannot
3034 be found, create one. (We keep track of used, but undefined,
3035 labels, and complain about them at the end of a function.) */
3037 static tree
3038 lookup_label_1 (tree id)
3040 tree decl;
3042 /* You can't use labels at global scope. */
3043 if (current_function_decl == NULL_TREE)
3045 error ("label %qE referenced outside of any function", id);
3046 return NULL_TREE;
3049 /* See if we've already got this label. */
3050 decl = IDENTIFIER_LABEL_VALUE (id);
3051 if (decl != NULL_TREE && DECL_CONTEXT (decl) == current_function_decl)
3052 return decl;
3054 decl = make_label_decl (id, /*local_p=*/0);
3055 return decl;
3058 /* Wrapper for lookup_label_1. */
3060 tree
3061 lookup_label (tree id)
3063 tree ret;
3064 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3065 ret = lookup_label_1 (id);
3066 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3067 return ret;
3070 /* Declare a local label named ID. */
3072 tree
3073 declare_local_label (tree id)
3075 tree decl;
3076 cp_label_binding bind;
3078 /* Add a new entry to the SHADOWED_LABELS list so that when we leave
3079 this scope we can restore the old value of IDENTIFIER_TYPE_VALUE. */
3080 bind.prev_value = IDENTIFIER_LABEL_VALUE (id);
3082 decl = make_label_decl (id, /*local_p=*/1);
3083 bind.label = decl;
3084 vec_safe_push (current_binding_level->shadowed_labels, bind);
3086 return decl;
3089 /* Returns nonzero if it is ill-formed to jump past the declaration of
3090 DECL. Returns 2 if it's also a real problem. */
3092 static int
3093 decl_jump_unsafe (tree decl)
3095 /* [stmt.dcl]/3: A program that jumps from a point where a local variable
3096 with automatic storage duration is not in scope to a point where it is
3097 in scope is ill-formed unless the variable has scalar type, class type
3098 with a trivial default constructor and a trivial destructor, a
3099 cv-qualified version of one of these types, or an array of one of the
3100 preceding types and is declared without an initializer (8.5). */
3101 tree type = TREE_TYPE (decl);
3103 if (!VAR_P (decl) || TREE_STATIC (decl)
3104 || type == error_mark_node)
3105 return 0;
3107 if (DECL_NONTRIVIALLY_INITIALIZED_P (decl)
3108 || variably_modified_type_p (type, NULL_TREE))
3109 return 2;
3111 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3112 return 1;
3114 return 0;
3117 /* A subroutine of check_previous_goto_1 and check_goto to identify a branch
3118 to the user. */
3120 static bool
3121 identify_goto (tree decl, location_t loc, const location_t *locus,
3122 diagnostic_t diag_kind)
3124 bool complained
3125 = (decl ? emit_diagnostic (diag_kind, loc, 0, "jump to label %qD", decl)
3126 : emit_diagnostic (diag_kind, loc, 0, "jump to case label"));
3127 if (complained && locus)
3128 inform (*locus, " from here");
3129 return complained;
3132 /* Check that a single previously seen jump to a newly defined label
3133 is OK. DECL is the LABEL_DECL or 0; LEVEL is the binding_level for
3134 the jump context; NAMES are the names in scope in LEVEL at the jump
3135 context; LOCUS is the source position of the jump or 0. Returns
3136 true if all is well. */
3138 static bool
3139 check_previous_goto_1 (tree decl, cp_binding_level* level, tree names,
3140 bool exited_omp, const location_t *locus)
3142 cp_binding_level *b;
3143 bool complained = false;
3144 int identified = 0;
3145 bool saw_eh = false, saw_omp = false, saw_tm = false, saw_cxif = false;
3147 if (exited_omp)
3149 complained = identify_goto (decl, input_location, locus, DK_ERROR);
3150 if (complained)
3151 inform (input_location, " exits OpenMP structured block");
3152 saw_omp = true;
3153 identified = 2;
3156 for (b = current_binding_level; b ; b = b->level_chain)
3158 tree new_decls, old_decls = (b == level ? names : NULL_TREE);
3160 for (new_decls = b->names; new_decls != old_decls;
3161 new_decls = (DECL_P (new_decls) ? DECL_CHAIN (new_decls)
3162 : TREE_CHAIN (new_decls)))
3164 int problem = decl_jump_unsafe (new_decls);
3165 if (! problem)
3166 continue;
3168 if (!identified)
3170 complained = identify_goto (decl, input_location, locus,
3171 DK_PERMERROR);
3172 identified = 1;
3174 if (complained)
3176 if (problem > 1)
3177 inform (DECL_SOURCE_LOCATION (new_decls),
3178 " crosses initialization of %q#D", new_decls);
3179 else
3180 inform (DECL_SOURCE_LOCATION (new_decls),
3181 " enters scope of %q#D which has "
3182 "non-trivial destructor", new_decls);
3186 if (b == level)
3187 break;
3188 if ((b->kind == sk_try || b->kind == sk_catch) && !saw_eh)
3190 if (identified < 2)
3192 complained = identify_goto (decl, input_location, locus,
3193 DK_ERROR);
3194 identified = 2;
3196 if (complained)
3198 if (b->kind == sk_try)
3199 inform (input_location, " enters try block");
3200 else
3201 inform (input_location, " enters catch block");
3203 saw_eh = true;
3205 if (b->kind == sk_omp && !saw_omp)
3207 if (identified < 2)
3209 complained = identify_goto (decl, input_location, locus,
3210 DK_ERROR);
3211 identified = 2;
3213 if (complained)
3214 inform (input_location, " enters OpenMP structured block");
3215 saw_omp = true;
3217 if (b->kind == sk_transaction && !saw_tm)
3219 if (identified < 2)
3221 complained = identify_goto (decl, input_location, locus,
3222 DK_ERROR);
3223 identified = 2;
3225 if (complained)
3226 inform (input_location,
3227 " enters synchronized or atomic statement");
3228 saw_tm = true;
3230 if (!saw_cxif && b->kind == sk_block
3231 && level_for_constexpr_if (b->level_chain))
3233 if (identified < 2)
3235 complained = identify_goto (decl, input_location, locus,
3236 DK_ERROR);
3237 identified = 2;
3239 if (complained)
3240 inform (EXPR_LOCATION (b->level_chain->this_entity),
3241 " enters constexpr if statement");
3242 saw_cxif = true;
3246 return !identified;
3249 static void
3250 check_previous_goto (tree decl, struct named_label_use_entry *use)
3252 check_previous_goto_1 (decl, use->binding_level,
3253 use->names_in_scope, use->in_omp_scope,
3254 &use->o_goto_locus);
3257 static bool
3258 check_switch_goto (cp_binding_level* level)
3260 return check_previous_goto_1 (NULL_TREE, level, level->names, false, NULL);
3263 /* Check that a new jump to a label DECL is OK. Called by
3264 finish_goto_stmt. */
3266 void
3267 check_goto (tree decl)
3269 struct named_label_entry *ent, dummy;
3270 bool saw_catch = false, complained = false;
3271 int identified = 0;
3272 tree bad;
3273 unsigned ix;
3275 /* We can't know where a computed goto is jumping.
3276 So we assume that it's OK. */
3277 if (TREE_CODE (decl) != LABEL_DECL)
3278 return;
3280 /* We didn't record any information about this label when we created it,
3281 and there's not much point since it's trivial to analyze as a return. */
3282 if (decl == cdtor_label)
3283 return;
3285 dummy.label_decl = decl;
3286 ent = named_labels->find (&dummy);
3287 gcc_assert (ent != NULL);
3289 /* If the label hasn't been defined yet, defer checking. */
3290 if (! DECL_INITIAL (decl))
3292 struct named_label_use_entry *new_use;
3294 /* Don't bother creating another use if the last goto had the
3295 same data, and will therefore create the same set of errors. */
3296 if (ent->uses
3297 && ent->uses->names_in_scope == current_binding_level->names)
3298 return;
3300 new_use = ggc_alloc<named_label_use_entry> ();
3301 new_use->binding_level = current_binding_level;
3302 new_use->names_in_scope = current_binding_level->names;
3303 new_use->o_goto_locus = input_location;
3304 new_use->in_omp_scope = false;
3306 new_use->next = ent->uses;
3307 ent->uses = new_use;
3308 return;
3311 if (ent->in_try_scope || ent->in_catch_scope || ent->in_transaction_scope
3312 || ent->in_constexpr_if
3313 || ent->in_omp_scope || !vec_safe_is_empty (ent->bad_decls))
3315 diagnostic_t diag_kind = DK_PERMERROR;
3316 if (ent->in_try_scope || ent->in_catch_scope || ent->in_constexpr_if
3317 || ent->in_transaction_scope || ent->in_omp_scope)
3318 diag_kind = DK_ERROR;
3319 complained = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
3320 &input_location, diag_kind);
3321 identified = 1 + (diag_kind == DK_ERROR);
3324 FOR_EACH_VEC_SAFE_ELT (ent->bad_decls, ix, bad)
3326 int u = decl_jump_unsafe (bad);
3328 if (u > 1 && DECL_ARTIFICIAL (bad))
3330 /* Can't skip init of __exception_info. */
3331 if (identified == 1)
3333 complained = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
3334 &input_location, DK_ERROR);
3335 identified = 2;
3337 if (complained)
3338 inform (DECL_SOURCE_LOCATION (bad), " enters catch block");
3339 saw_catch = true;
3341 else if (complained)
3343 if (u > 1)
3344 inform (DECL_SOURCE_LOCATION (bad),
3345 " skips initialization of %q#D", bad);
3346 else
3347 inform (DECL_SOURCE_LOCATION (bad),
3348 " enters scope of %q#D which has "
3349 "non-trivial destructor", bad);
3353 if (complained)
3355 if (ent->in_try_scope)
3356 inform (input_location, " enters try block");
3357 else if (ent->in_catch_scope && !saw_catch)
3358 inform (input_location, " enters catch block");
3359 else if (ent->in_transaction_scope)
3360 inform (input_location, " enters synchronized or atomic statement");
3361 else if (ent->in_constexpr_if)
3362 inform (input_location, " enters constexpr if statement");
3365 if (ent->in_omp_scope)
3367 if (complained)
3368 inform (input_location, " enters OpenMP structured block");
3370 else if (flag_openmp)
3372 cp_binding_level *b;
3373 for (b = current_binding_level; b ; b = b->level_chain)
3375 if (b == ent->binding_level)
3376 break;
3377 if (b->kind == sk_omp)
3379 if (identified < 2)
3381 complained = identify_goto (decl,
3382 DECL_SOURCE_LOCATION (decl),
3383 &input_location, DK_ERROR);
3384 identified = 2;
3386 if (complained)
3387 inform (input_location, " exits OpenMP structured block");
3388 break;
3394 /* Check that a return is ok wrt OpenMP structured blocks.
3395 Called by finish_return_stmt. Returns true if all is well. */
3397 bool
3398 check_omp_return (void)
3400 cp_binding_level *b;
3401 for (b = current_binding_level; b ; b = b->level_chain)
3402 if (b->kind == sk_omp)
3404 error ("invalid exit from OpenMP structured block");
3405 return false;
3407 else if (b->kind == sk_function_parms)
3408 break;
3409 return true;
3412 /* Define a label, specifying the location in the source file.
3413 Return the LABEL_DECL node for the label. */
3415 static tree
3416 define_label_1 (location_t location, tree name)
3418 struct named_label_entry *ent, dummy;
3419 cp_binding_level *p;
3420 tree decl;
3422 decl = lookup_label (name);
3424 dummy.label_decl = decl;
3425 ent = named_labels->find (&dummy);
3426 gcc_assert (ent != NULL);
3428 /* After labels, make any new cleanups in the function go into their
3429 own new (temporary) binding contour. */
3430 for (p = current_binding_level;
3431 p->kind != sk_function_parms;
3432 p = p->level_chain)
3433 p->more_cleanups_ok = 0;
3435 if (name == get_identifier ("wchar_t"))
3436 permerror (input_location, "label named wchar_t");
3438 if (DECL_INITIAL (decl) != NULL_TREE)
3440 error ("duplicate label %qD", decl);
3441 return error_mark_node;
3443 else
3445 struct named_label_use_entry *use;
3447 /* Mark label as having been defined. */
3448 DECL_INITIAL (decl) = error_mark_node;
3449 /* Say where in the source. */
3450 DECL_SOURCE_LOCATION (decl) = location;
3452 ent->binding_level = current_binding_level;
3453 ent->names_in_scope = current_binding_level->names;
3455 for (use = ent->uses; use ; use = use->next)
3456 check_previous_goto (decl, use);
3457 ent->uses = NULL;
3460 return decl;
3463 /* Wrapper for define_label_1. */
3465 tree
3466 define_label (location_t location, tree name)
3468 tree ret;
3469 bool running = timevar_cond_start (TV_NAME_LOOKUP);
3470 ret = define_label_1 (location, name);
3471 timevar_cond_stop (TV_NAME_LOOKUP, running);
3472 return ret;
3476 struct cp_switch
3478 cp_binding_level *level;
3479 struct cp_switch *next;
3480 /* The SWITCH_STMT being built. */
3481 tree switch_stmt;
3482 /* A splay-tree mapping the low element of a case range to the high
3483 element, or NULL_TREE if there is no high element. Used to
3484 determine whether or not a new case label duplicates an old case
3485 label. We need a tree, rather than simply a hash table, because
3486 of the GNU case range extension. */
3487 splay_tree cases;
3488 /* Remember whether there was a case value that is outside the
3489 range of the original type of the controlling expression. */
3490 bool outside_range_p;
3493 /* A stack of the currently active switch statements. The innermost
3494 switch statement is on the top of the stack. There is no need to
3495 mark the stack for garbage collection because it is only active
3496 during the processing of the body of a function, and we never
3497 collect at that point. */
3499 static struct cp_switch *switch_stack;
3501 /* Called right after a switch-statement condition is parsed.
3502 SWITCH_STMT is the switch statement being parsed. */
3504 void
3505 push_switch (tree switch_stmt)
3507 struct cp_switch *p = XNEW (struct cp_switch);
3508 p->level = current_binding_level;
3509 p->next = switch_stack;
3510 p->switch_stmt = switch_stmt;
3511 p->cases = splay_tree_new (case_compare, NULL, NULL);
3512 p->outside_range_p = false;
3513 switch_stack = p;
3516 void
3517 pop_switch (void)
3519 struct cp_switch *cs = switch_stack;
3520 location_t switch_location;
3522 /* Emit warnings as needed. */
3523 switch_location = EXPR_LOC_OR_LOC (cs->switch_stmt, input_location);
3524 const bool bool_cond_p
3525 = (SWITCH_STMT_TYPE (cs->switch_stmt)
3526 && TREE_CODE (SWITCH_STMT_TYPE (cs->switch_stmt)) == BOOLEAN_TYPE);
3527 if (!processing_template_decl)
3528 c_do_switch_warnings (cs->cases, switch_location,
3529 SWITCH_STMT_TYPE (cs->switch_stmt),
3530 SWITCH_STMT_COND (cs->switch_stmt),
3531 bool_cond_p, cs->outside_range_p);
3533 splay_tree_delete (cs->cases);
3534 switch_stack = switch_stack->next;
3535 free (cs);
3538 /* Convert a case constant VALUE in a switch to the type TYPE of the switch
3539 condition. Note that if TYPE and VALUE are already integral we don't
3540 really do the conversion because the language-independent
3541 warning/optimization code will work better that way. */
3543 static tree
3544 case_conversion (tree type, tree value)
3546 if (value == NULL_TREE)
3547 return value;
3549 if (cxx_dialect >= cxx11
3550 && (SCOPED_ENUM_P (type)
3551 || !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (value))))
3553 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
3554 type = type_promotes_to (type);
3555 value = (perform_implicit_conversion_flags
3556 (type, value, tf_warning_or_error,
3557 LOOKUP_IMPLICIT | LOOKUP_NO_NON_INTEGRAL));
3559 return cxx_constant_value (value);
3562 /* Note that we've seen a definition of a case label, and complain if this
3563 is a bad place for one. */
3565 tree
3566 finish_case_label (location_t loc, tree low_value, tree high_value)
3568 tree cond, r;
3569 cp_binding_level *p;
3570 tree type;
3572 if (processing_template_decl)
3574 tree label;
3576 /* For templates, just add the case label; we'll do semantic
3577 analysis at instantiation-time. */
3578 label = build_decl (loc, LABEL_DECL, NULL_TREE, NULL_TREE);
3579 return add_stmt (build_case_label (low_value, high_value, label));
3582 /* Find the condition on which this switch statement depends. */
3583 cond = SWITCH_STMT_COND (switch_stack->switch_stmt);
3584 if (cond && TREE_CODE (cond) == TREE_LIST)
3585 cond = TREE_VALUE (cond);
3587 if (!check_switch_goto (switch_stack->level))
3588 return error_mark_node;
3590 type = SWITCH_STMT_TYPE (switch_stack->switch_stmt);
3592 low_value = case_conversion (type, low_value);
3593 high_value = case_conversion (type, high_value);
3595 r = c_add_case_label (loc, switch_stack->cases, cond, type,
3596 low_value, high_value,
3597 &switch_stack->outside_range_p);
3599 /* After labels, make any new cleanups in the function go into their
3600 own new (temporary) binding contour. */
3601 for (p = current_binding_level;
3602 p->kind != sk_function_parms;
3603 p = p->level_chain)
3604 p->more_cleanups_ok = 0;
3606 return r;
3609 struct typename_info {
3610 tree scope;
3611 tree name;
3612 tree template_id;
3613 bool enum_p;
3614 bool class_p;
3617 struct typename_hasher : ggc_ptr_hash<tree_node>
3619 typedef typename_info *compare_type;
3621 /* Hash a TYPENAME_TYPE. */
3623 static hashval_t
3624 hash (tree t)
3626 hashval_t hash;
3628 hash = (htab_hash_pointer (TYPE_CONTEXT (t))
3629 ^ htab_hash_pointer (TYPE_IDENTIFIER (t)));
3631 return hash;
3634 /* Compare two TYPENAME_TYPEs. */
3636 static bool
3637 equal (tree t1, const typename_info *t2)
3639 return (TYPE_IDENTIFIER (t1) == t2->name
3640 && TYPE_CONTEXT (t1) == t2->scope
3641 && TYPENAME_TYPE_FULLNAME (t1) == t2->template_id
3642 && TYPENAME_IS_ENUM_P (t1) == t2->enum_p
3643 && TYPENAME_IS_CLASS_P (t1) == t2->class_p);
3647 /* Build a TYPENAME_TYPE. If the type is `typename T::t', CONTEXT is
3648 the type of `T', NAME is the IDENTIFIER_NODE for `t'.
3650 Returns the new TYPENAME_TYPE. */
3652 static GTY (()) hash_table<typename_hasher> *typename_htab;
3654 static tree
3655 build_typename_type (tree context, tree name, tree fullname,
3656 enum tag_types tag_type)
3658 tree t;
3659 tree d;
3660 typename_info ti;
3661 tree *e;
3662 hashval_t hash;
3664 if (typename_htab == NULL)
3665 typename_htab = hash_table<typename_hasher>::create_ggc (61);
3667 ti.scope = FROB_CONTEXT (context);
3668 ti.name = name;
3669 ti.template_id = fullname;
3670 ti.enum_p = tag_type == enum_type;
3671 ti.class_p = (tag_type == class_type
3672 || tag_type == record_type
3673 || tag_type == union_type);
3674 hash = (htab_hash_pointer (ti.scope)
3675 ^ htab_hash_pointer (ti.name));
3677 /* See if we already have this type. */
3678 e = typename_htab->find_slot_with_hash (&ti, hash, INSERT);
3679 if (*e)
3680 t = *e;
3681 else
3683 /* Build the TYPENAME_TYPE. */
3684 t = cxx_make_type (TYPENAME_TYPE);
3685 TYPE_CONTEXT (t) = ti.scope;
3686 TYPENAME_TYPE_FULLNAME (t) = ti.template_id;
3687 TYPENAME_IS_ENUM_P (t) = ti.enum_p;
3688 TYPENAME_IS_CLASS_P (t) = ti.class_p;
3690 /* Build the corresponding TYPE_DECL. */
3691 d = build_decl (input_location, TYPE_DECL, name, t);
3692 TYPE_NAME (TREE_TYPE (d)) = d;
3693 TYPE_STUB_DECL (TREE_TYPE (d)) = d;
3694 DECL_CONTEXT (d) = FROB_CONTEXT (context);
3695 DECL_ARTIFICIAL (d) = 1;
3697 /* Store it in the hash table. */
3698 *e = t;
3700 /* TYPENAME_TYPEs must always be compared structurally, because
3701 they may or may not resolve down to another type depending on
3702 the currently open classes. */
3703 SET_TYPE_STRUCTURAL_EQUALITY (t);
3706 return t;
3709 /* Resolve `typename CONTEXT::NAME'. TAG_TYPE indicates the tag
3710 provided to name the type. Returns an appropriate type, unless an
3711 error occurs, in which case error_mark_node is returned. If we
3712 locate a non-artificial TYPE_DECL and TF_KEEP_TYPE_DECL is set, we
3713 return that, rather than the _TYPE it corresponds to, in other
3714 cases we look through the type decl. If TF_ERROR is set, complain
3715 about errors, otherwise be quiet. */
3717 tree
3718 make_typename_type (tree context, tree name, enum tag_types tag_type,
3719 tsubst_flags_t complain)
3721 tree fullname;
3722 tree t;
3723 bool want_template;
3725 if (name == error_mark_node
3726 || context == NULL_TREE
3727 || context == error_mark_node)
3728 return error_mark_node;
3730 if (TYPE_P (name))
3732 if (!(TYPE_LANG_SPECIFIC (name)
3733 && (CLASSTYPE_IS_TEMPLATE (name)
3734 || CLASSTYPE_USE_TEMPLATE (name))))
3735 name = TYPE_IDENTIFIER (name);
3736 else
3737 /* Create a TEMPLATE_ID_EXPR for the type. */
3738 name = build_nt (TEMPLATE_ID_EXPR,
3739 CLASSTYPE_TI_TEMPLATE (name),
3740 CLASSTYPE_TI_ARGS (name));
3742 else if (TREE_CODE (name) == TYPE_DECL)
3743 name = DECL_NAME (name);
3745 fullname = name;
3747 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3749 name = TREE_OPERAND (name, 0);
3750 if (DECL_TYPE_TEMPLATE_P (name))
3751 name = TREE_OPERAND (fullname, 0) = DECL_NAME (name);
3752 if (TREE_CODE (name) != IDENTIFIER_NODE)
3754 if (complain & tf_error)
3755 error ("%qD is not a type", name);
3756 return error_mark_node;
3759 if (TREE_CODE (name) == TEMPLATE_DECL)
3761 if (complain & tf_error)
3762 error ("%qD used without template parameters", name);
3763 return error_mark_node;
3765 gcc_assert (identifier_p (name));
3766 gcc_assert (TYPE_P (context));
3768 if (!MAYBE_CLASS_TYPE_P (context))
3770 if (complain & tf_error)
3771 error ("%q#T is not a class", context);
3772 return error_mark_node;
3775 /* When the CONTEXT is a dependent type, NAME could refer to a
3776 dependent base class of CONTEXT. But look inside it anyway
3777 if CONTEXT is a currently open scope, in case it refers to a
3778 member of the current instantiation or a non-dependent base;
3779 lookup will stop when we hit a dependent base. */
3780 if (!dependent_scope_p (context))
3781 /* We should only set WANT_TYPE when we're a nested typename type.
3782 Then we can give better diagnostics if we find a non-type. */
3783 t = lookup_field (context, name, 2, /*want_type=*/true);
3784 else
3785 t = NULL_TREE;
3787 if ((!t || TREE_CODE (t) == TREE_LIST) && dependent_type_p (context))
3788 return build_typename_type (context, name, fullname, tag_type);
3790 want_template = TREE_CODE (fullname) == TEMPLATE_ID_EXPR;
3792 if (!t)
3794 if (complain & tf_error)
3796 if (!COMPLETE_TYPE_P (context))
3797 cxx_incomplete_type_error (NULL_TREE, context);
3798 else
3799 error (want_template ? G_("no class template named %q#T in %q#T")
3800 : G_("no type named %q#T in %q#T"), name, context);
3802 return error_mark_node;
3805 /* Pull out the template from an injected-class-name (or multiple). */
3806 if (want_template)
3807 t = maybe_get_template_decl_from_type_decl (t);
3809 if (TREE_CODE (t) == TREE_LIST)
3811 if (complain & tf_error)
3813 error ("lookup of %qT in %qT is ambiguous", name, context);
3814 print_candidates (t);
3816 return error_mark_node;
3819 if (want_template && !DECL_TYPE_TEMPLATE_P (t))
3821 if (complain & tf_error)
3822 error ("%<typename %T::%D%> names %q#T, which is not a class template",
3823 context, name, t);
3824 return error_mark_node;
3826 if (!want_template && TREE_CODE (t) != TYPE_DECL)
3828 if (complain & tf_error)
3829 error ("%<typename %T::%D%> names %q#T, which is not a type",
3830 context, name, t);
3831 return error_mark_node;
3834 if (!perform_or_defer_access_check (TYPE_BINFO (context), t, t, complain))
3835 return error_mark_node;
3837 /* If we are currently parsing a template and if T is a typedef accessed
3838 through CONTEXT then we need to remember and check access of T at
3839 template instantiation time. */
3840 add_typedef_to_current_template_for_access_check (t, context, input_location);
3842 if (want_template)
3843 return lookup_template_class (t, TREE_OPERAND (fullname, 1),
3844 NULL_TREE, context,
3845 /*entering_scope=*/0,
3846 complain | tf_user);
3848 if (DECL_ARTIFICIAL (t) || !(complain & tf_keep_type_decl))
3849 t = TREE_TYPE (t);
3851 maybe_record_typedef_use (t);
3853 return t;
3856 /* Resolve `CONTEXT::template NAME'. Returns a TEMPLATE_DECL if the name
3857 can be resolved or an UNBOUND_CLASS_TEMPLATE, unless an error occurs,
3858 in which case error_mark_node is returned.
3860 If PARM_LIST is non-NULL, also make sure that the template parameter
3861 list of TEMPLATE_DECL matches.
3863 If COMPLAIN zero, don't complain about any errors that occur. */
3865 tree
3866 make_unbound_class_template (tree context, tree name, tree parm_list,
3867 tsubst_flags_t complain)
3869 tree t;
3870 tree d;
3872 if (TYPE_P (name))
3873 name = TYPE_IDENTIFIER (name);
3874 else if (DECL_P (name))
3875 name = DECL_NAME (name);
3876 gcc_assert (identifier_p (name));
3878 if (!dependent_type_p (context)
3879 || currently_open_class (context))
3881 tree tmpl = NULL_TREE;
3883 if (MAYBE_CLASS_TYPE_P (context))
3884 tmpl = lookup_field (context, name, 0, false);
3886 if (tmpl && TREE_CODE (tmpl) == TYPE_DECL)
3887 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
3889 if (!tmpl || !DECL_TYPE_TEMPLATE_P (tmpl))
3891 if (complain & tf_error)
3892 error ("no class template named %q#T in %q#T", name, context);
3893 return error_mark_node;
3896 if (parm_list
3897 && !comp_template_parms (DECL_TEMPLATE_PARMS (tmpl), parm_list))
3899 if (complain & tf_error)
3901 error ("template parameters do not match template %qD", tmpl);
3902 inform (DECL_SOURCE_LOCATION (tmpl),
3903 "%qD declared here", tmpl);
3905 return error_mark_node;
3908 if (!perform_or_defer_access_check (TYPE_BINFO (context), tmpl, tmpl,
3909 complain))
3910 return error_mark_node;
3912 return tmpl;
3915 /* Build the UNBOUND_CLASS_TEMPLATE. */
3916 t = cxx_make_type (UNBOUND_CLASS_TEMPLATE);
3917 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
3918 TREE_TYPE (t) = NULL_TREE;
3919 SET_TYPE_STRUCTURAL_EQUALITY (t);
3921 /* Build the corresponding TEMPLATE_DECL. */
3922 d = build_decl (input_location, TEMPLATE_DECL, name, t);
3923 TYPE_NAME (TREE_TYPE (d)) = d;
3924 TYPE_STUB_DECL (TREE_TYPE (d)) = d;
3925 DECL_CONTEXT (d) = FROB_CONTEXT (context);
3926 DECL_ARTIFICIAL (d) = 1;
3927 DECL_TEMPLATE_PARMS (d) = parm_list;
3929 return t;
3934 /* Push the declarations of builtin types into the namespace.
3935 RID_INDEX is the index of the builtin type in the array
3936 RID_POINTERS. NAME is the name used when looking up the builtin
3937 type. TYPE is the _TYPE node for the builtin type. */
3939 void
3940 record_builtin_type (enum rid rid_index,
3941 const char* name,
3942 tree type)
3944 tree rname = NULL_TREE, tname = NULL_TREE;
3945 tree tdecl = NULL_TREE;
3947 if ((int) rid_index < (int) RID_MAX)
3948 rname = ridpointers[(int) rid_index];
3949 if (name)
3950 tname = get_identifier (name);
3952 /* The calls to SET_IDENTIFIER_GLOBAL_VALUE below should be
3953 eliminated. Built-in types should not be looked up name; their
3954 names are keywords that the parser can recognize. However, there
3955 is code in c-common.c that uses identifier_global_value to look
3956 up built-in types by name. */
3957 if (tname)
3959 tdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, tname, type);
3960 DECL_ARTIFICIAL (tdecl) = 1;
3961 SET_IDENTIFIER_GLOBAL_VALUE (tname, tdecl);
3963 if (rname)
3965 if (!tdecl)
3967 tdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, rname, type);
3968 DECL_ARTIFICIAL (tdecl) = 1;
3970 SET_IDENTIFIER_GLOBAL_VALUE (rname, tdecl);
3973 if (!TYPE_NAME (type))
3974 TYPE_NAME (type) = tdecl;
3976 if (tdecl)
3977 debug_hooks->type_decl (tdecl, 0);
3980 /* Push a type into the namespace so that the back ends ignore it. */
3982 static void
3983 record_unknown_type (tree type, const char* name)
3985 tree decl = pushdecl (build_decl (UNKNOWN_LOCATION,
3986 TYPE_DECL, get_identifier (name), type));
3987 /* Make sure the "unknown type" typedecl gets ignored for debug info. */
3988 DECL_IGNORED_P (decl) = 1;
3989 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
3990 TYPE_SIZE (type) = TYPE_SIZE (void_type_node);
3991 SET_TYPE_ALIGN (type, 1);
3992 TYPE_USER_ALIGN (type) = 0;
3993 SET_TYPE_MODE (type, TYPE_MODE (void_type_node));
3996 /* A string for which we should create an IDENTIFIER_NODE at
3997 startup. */
3999 struct predefined_identifier
4001 /* The name of the identifier. */
4002 const char *const name;
4003 /* The place where the IDENTIFIER_NODE should be stored. */
4004 tree *const node;
4005 /* Nonzero if this is the name of a constructor or destructor. */
4006 const int ctor_or_dtor_p;
4009 /* Create all the predefined identifiers. */
4011 static void
4012 initialize_predefined_identifiers (void)
4014 const predefined_identifier *pid;
4016 /* A table of identifiers to create at startup. */
4017 static const predefined_identifier predefined_identifiers[] = {
4018 { "C++", &lang_name_cplusplus, 0 },
4019 { "C", &lang_name_c, 0 },
4020 /* Some of these names have a trailing space so that it is
4021 impossible for them to conflict with names written by users. */
4022 { "__ct ", &ctor_identifier, 1 },
4023 { "__base_ctor ", &base_ctor_identifier, 1 },
4024 { "__comp_ctor ", &complete_ctor_identifier, 1 },
4025 { "__dt ", &dtor_identifier, 1 },
4026 { "__comp_dtor ", &complete_dtor_identifier, 1 },
4027 { "__base_dtor ", &base_dtor_identifier, 1 },
4028 { "__deleting_dtor ", &deleting_dtor_identifier, 1 },
4029 { IN_CHARGE_NAME, &in_charge_identifier, 0 },
4030 { "nelts", &nelts_identifier, 0 },
4031 { THIS_NAME, &this_identifier, 0 },
4032 { VTABLE_DELTA_NAME, &delta_identifier, 0 },
4033 { VTABLE_PFN_NAME, &pfn_identifier, 0 },
4034 { "_vptr", &vptr_identifier, 0 },
4035 { "__vtt_parm", &vtt_parm_identifier, 0 },
4036 { "::", &global_scope_name, 0 },
4037 { "std", &std_identifier, 0 },
4038 { "auto", &auto_identifier, 0 },
4039 { "decltype(auto)", &decltype_auto_identifier, 0 },
4040 { NULL, NULL, 0 }
4043 for (pid = predefined_identifiers; pid->name; ++pid)
4045 *pid->node = get_identifier (pid->name);
4046 if (pid->ctor_or_dtor_p)
4047 IDENTIFIER_CTOR_OR_DTOR_P (*pid->node) = 1;
4051 /* Create the predefined scalar types of C,
4052 and some nodes representing standard constants (0, 1, (void *)0).
4053 Initialize the global binding level.
4054 Make definitions for built-in primitive functions. */
4056 void
4057 cxx_init_decl_processing (void)
4059 tree void_ftype;
4060 tree void_ftype_ptr;
4062 /* Create all the identifiers we need. */
4063 initialize_predefined_identifiers ();
4065 /* Create the global variables. */
4066 push_to_top_level ();
4068 current_function_decl = NULL_TREE;
4069 current_binding_level = NULL;
4070 /* Enter the global namespace. */
4071 gcc_assert (global_namespace == NULL_TREE);
4072 global_namespace = build_lang_decl (NAMESPACE_DECL, global_scope_name,
4073 void_type_node);
4074 DECL_CONTEXT (global_namespace)
4075 = build_translation_unit_decl (get_identifier (main_input_filename));
4076 debug_hooks->register_main_translation_unit
4077 (DECL_CONTEXT (global_namespace));
4078 TREE_PUBLIC (global_namespace) = 1;
4079 begin_scope (sk_namespace, global_namespace);
4081 if (flag_visibility_ms_compat)
4082 default_visibility = VISIBILITY_HIDDEN;
4084 /* Initially, C. */
4085 current_lang_name = lang_name_c;
4087 /* Create the `std' namespace. */
4088 push_namespace (std_identifier);
4089 std_node = current_namespace;
4090 pop_namespace ();
4092 flag_noexcept_type = (cxx_dialect >= cxx1z);
4094 c_common_nodes_and_builtins ();
4096 integer_two_node = build_int_cst (NULL_TREE, 2);
4098 record_builtin_type (RID_BOOL, "bool", boolean_type_node);
4099 truthvalue_type_node = boolean_type_node;
4100 truthvalue_false_node = boolean_false_node;
4101 truthvalue_true_node = boolean_true_node;
4103 empty_except_spec = build_tree_list (NULL_TREE, NULL_TREE);
4104 noexcept_true_spec = build_tree_list (boolean_true_node, NULL_TREE);
4105 noexcept_false_spec = build_tree_list (boolean_false_node, NULL_TREE);
4107 #if 0
4108 record_builtin_type (RID_MAX, NULL, string_type_node);
4109 #endif
4111 delta_type_node = ptrdiff_type_node;
4112 vtable_index_type = ptrdiff_type_node;
4114 vtt_parm_type = build_pointer_type (const_ptr_type_node);
4115 void_ftype = build_function_type_list (void_type_node, NULL_TREE);
4116 void_ftype_ptr = build_function_type_list (void_type_node,
4117 ptr_type_node, NULL_TREE);
4118 void_ftype_ptr
4119 = build_exception_variant (void_ftype_ptr, empty_except_spec);
4121 /* C++ extensions */
4123 unknown_type_node = make_node (LANG_TYPE);
4124 record_unknown_type (unknown_type_node, "unknown type");
4126 /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node. */
4127 TREE_TYPE (unknown_type_node) = unknown_type_node;
4129 /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same
4130 result. */
4131 TYPE_POINTER_TO (unknown_type_node) = unknown_type_node;
4132 TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node;
4134 init_list_type_node = make_node (LANG_TYPE);
4135 record_unknown_type (init_list_type_node, "init list");
4138 /* Make sure we get a unique function type, so we can give
4139 its pointer type a name. (This wins for gdb.) */
4140 tree vfunc_type = make_node (FUNCTION_TYPE);
4141 TREE_TYPE (vfunc_type) = integer_type_node;
4142 TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
4143 layout_type (vfunc_type);
4145 vtable_entry_type = build_pointer_type (vfunc_type);
4147 record_builtin_type (RID_MAX, VTBL_PTR_TYPE, vtable_entry_type);
4149 vtbl_type_node
4150 = build_cplus_array_type (vtable_entry_type, NULL_TREE);
4151 layout_type (vtbl_type_node);
4152 vtbl_type_node = cp_build_qualified_type (vtbl_type_node, TYPE_QUAL_CONST);
4153 record_builtin_type (RID_MAX, NULL, vtbl_type_node);
4154 vtbl_ptr_type_node = build_pointer_type (vtable_entry_type);
4155 layout_type (vtbl_ptr_type_node);
4156 record_builtin_type (RID_MAX, NULL, vtbl_ptr_type_node);
4158 push_namespace (get_identifier ("__cxxabiv1"));
4159 abi_node = current_namespace;
4160 pop_namespace ();
4162 global_type_node = make_node (LANG_TYPE);
4163 record_unknown_type (global_type_node, "global type");
4165 any_targ_node = make_node (LANG_TYPE);
4166 record_unknown_type (any_targ_node, "any type");
4168 /* Now, C++. */
4169 current_lang_name = lang_name_cplusplus;
4171 if (aligned_new_threshold > 1
4172 && !pow2p_hwi (aligned_new_threshold))
4174 error ("-faligned-new=%d is not a power of two", aligned_new_threshold);
4175 aligned_new_threshold = 1;
4177 if (aligned_new_threshold == -1)
4178 aligned_new_threshold = (cxx_dialect >= cxx1z) ? 1 : 0;
4179 if (aligned_new_threshold == 1)
4180 aligned_new_threshold = malloc_alignment () / BITS_PER_UNIT;
4183 tree newattrs, extvisattr;
4184 tree newtype, deltype;
4185 tree ptr_ftype_sizetype;
4186 tree new_eh_spec;
4188 ptr_ftype_sizetype
4189 = build_function_type_list (ptr_type_node, size_type_node, NULL_TREE);
4190 if (cxx_dialect == cxx98)
4192 tree bad_alloc_id;
4193 tree bad_alloc_type_node;
4194 tree bad_alloc_decl;
4196 push_namespace (std_identifier);
4197 bad_alloc_id = get_identifier ("bad_alloc");
4198 bad_alloc_type_node = make_class_type (RECORD_TYPE);
4199 TYPE_CONTEXT (bad_alloc_type_node) = current_namespace;
4200 bad_alloc_decl
4201 = create_implicit_typedef (bad_alloc_id, bad_alloc_type_node);
4202 DECL_CONTEXT (bad_alloc_decl) = current_namespace;
4203 pop_namespace ();
4205 new_eh_spec
4206 = add_exception_specifier (NULL_TREE, bad_alloc_type_node, -1);
4208 else
4209 new_eh_spec = noexcept_false_spec;
4211 /* Ensure attribs.c is initialized. */
4212 init_attributes ();
4214 /* Ensure constraint.cc is initialized. */
4215 init_constraint_processing ();
4217 extvisattr = build_tree_list (get_identifier ("externally_visible"),
4218 NULL_TREE);
4219 newattrs = tree_cons (get_identifier ("alloc_size"),
4220 build_tree_list (NULL_TREE, integer_one_node),
4221 extvisattr);
4222 newtype = cp_build_type_attribute_variant (ptr_ftype_sizetype, newattrs);
4223 newtype = build_exception_variant (newtype, new_eh_spec);
4224 deltype = cp_build_type_attribute_variant (void_ftype_ptr, extvisattr);
4225 deltype = build_exception_variant (deltype, empty_except_spec);
4226 tree opnew = push_cp_library_fn (NEW_EXPR, newtype, 0);
4227 DECL_IS_MALLOC (opnew) = 1;
4228 DECL_IS_OPERATOR_NEW (opnew) = 1;
4229 opnew = push_cp_library_fn (VEC_NEW_EXPR, newtype, 0);
4230 DECL_IS_MALLOC (opnew) = 1;
4231 DECL_IS_OPERATOR_NEW (opnew) = 1;
4232 push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
4233 push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
4234 if (flag_sized_deallocation)
4236 /* Also push the sized deallocation variants:
4237 void operator delete(void*, std::size_t) throw();
4238 void operator delete[](void*, std::size_t) throw(); */
4239 tree void_ftype_ptr_size
4240 = build_function_type_list (void_type_node, ptr_type_node,
4241 size_type_node, NULL_TREE);
4242 deltype = cp_build_type_attribute_variant (void_ftype_ptr_size,
4243 extvisattr);
4244 deltype = build_exception_variant (deltype, empty_except_spec);
4245 push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
4246 push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
4249 if (aligned_new_threshold)
4251 push_namespace (std_identifier);
4252 tree align_id = get_identifier ("align_val_t");
4253 align_type_node = start_enum (align_id, NULL_TREE, size_type_node,
4254 NULL_TREE, /*scoped*/true, NULL);
4255 pop_namespace ();
4257 /* operator new (size_t, align_val_t); */
4258 newtype = build_function_type_list (ptr_type_node, size_type_node,
4259 align_type_node, NULL_TREE);
4260 newtype = cp_build_type_attribute_variant (newtype, newattrs);
4261 newtype = build_exception_variant (newtype, new_eh_spec);
4262 opnew = push_cp_library_fn (NEW_EXPR, newtype, 0);
4263 DECL_IS_MALLOC (opnew) = 1;
4264 DECL_IS_OPERATOR_NEW (opnew) = 1;
4265 opnew = push_cp_library_fn (VEC_NEW_EXPR, newtype, 0);
4266 DECL_IS_MALLOC (opnew) = 1;
4267 DECL_IS_OPERATOR_NEW (opnew) = 1;
4269 /* operator delete (void *, align_val_t); */
4270 deltype = build_function_type_list (void_type_node, ptr_type_node,
4271 align_type_node, NULL_TREE);
4272 deltype = cp_build_type_attribute_variant (deltype, extvisattr);
4273 deltype = build_exception_variant (deltype, empty_except_spec);
4274 push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
4275 push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
4277 if (flag_sized_deallocation)
4279 /* operator delete (void *, size_t, align_val_t); */
4280 deltype = build_function_type_list (void_type_node, ptr_type_node,
4281 size_type_node, align_type_node,
4282 NULL_TREE);
4283 deltype = cp_build_type_attribute_variant (deltype, extvisattr);
4284 deltype = build_exception_variant (deltype, empty_except_spec);
4285 push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
4286 push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
4290 nullptr_type_node = make_node (NULLPTR_TYPE);
4291 TYPE_SIZE (nullptr_type_node) = bitsize_int (GET_MODE_BITSIZE (ptr_mode));
4292 TYPE_SIZE_UNIT (nullptr_type_node) = size_int (GET_MODE_SIZE (ptr_mode));
4293 TYPE_UNSIGNED (nullptr_type_node) = 1;
4294 TYPE_PRECISION (nullptr_type_node) = GET_MODE_BITSIZE (ptr_mode);
4295 if (abi_version_at_least (9))
4296 SET_TYPE_ALIGN (nullptr_type_node, GET_MODE_ALIGNMENT (ptr_mode));
4297 SET_TYPE_MODE (nullptr_type_node, ptr_mode);
4298 record_builtin_type (RID_MAX, "decltype(nullptr)", nullptr_type_node);
4299 nullptr_node = build_int_cst (nullptr_type_node, 0);
4302 abort_fndecl
4303 = build_library_fn_ptr ("__cxa_pure_virtual", void_ftype,
4304 ECF_NORETURN | ECF_NOTHROW);
4306 /* Perform other language dependent initializations. */
4307 init_class_processing ();
4308 init_rtti_processing ();
4309 init_template_processing ();
4311 if (flag_exceptions)
4312 init_exception_processing ();
4314 if (! supports_one_only ())
4315 flag_weak = 0;
4317 make_fname_decl = cp_make_fname_decl;
4318 start_fname_decls ();
4320 /* Show we use EH for cleanups. */
4321 if (flag_exceptions)
4322 using_eh_for_cleanups ();
4325 /* Generate an initializer for a function naming variable from
4326 NAME. NAME may be NULL, to indicate a dependent name. TYPE_P is
4327 filled in with the type of the init. */
4329 tree
4330 cp_fname_init (const char* name, tree *type_p)
4332 tree domain = NULL_TREE;
4333 tree type;
4334 tree init = NULL_TREE;
4335 size_t length = 0;
4337 if (name)
4339 length = strlen (name);
4340 domain = build_index_type (size_int (length));
4341 init = build_string (length + 1, name);
4344 type = cp_build_qualified_type (char_type_node, TYPE_QUAL_CONST);
4345 type = build_cplus_array_type (type, domain);
4347 *type_p = type;
4349 if (init)
4350 TREE_TYPE (init) = type;
4351 else
4352 init = error_mark_node;
4354 return init;
4357 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give
4358 the decl, LOC is the location to give the decl, NAME is the
4359 initialization string and TYPE_DEP indicates whether NAME depended
4360 on the type of the function. We make use of that to detect
4361 __PRETTY_FUNCTION__ inside a template fn. This is being done lazily
4362 at the point of first use, so we mustn't push the decl now. */
4364 static tree
4365 cp_make_fname_decl (location_t loc, tree id, int type_dep)
4367 const char *const name = (type_dep && processing_template_decl
4368 ? NULL : fname_as_string (type_dep));
4369 tree type;
4370 tree init = cp_fname_init (name, &type);
4371 tree decl = build_decl (loc, VAR_DECL, id, type);
4373 if (name)
4374 free (CONST_CAST (char *, name));
4376 /* As we're using pushdecl_with_scope, we must set the context. */
4377 DECL_CONTEXT (decl) = current_function_decl;
4379 TREE_STATIC (decl) = 1;
4380 TREE_READONLY (decl) = 1;
4381 DECL_ARTIFICIAL (decl) = 1;
4383 TREE_USED (decl) = 1;
4385 if (current_function_decl)
4387 cp_binding_level *b = current_binding_level;
4388 if (b->kind == sk_function_parms)
4389 return error_mark_node;
4390 while (b->level_chain->kind != sk_function_parms)
4391 b = b->level_chain;
4392 pushdecl_with_scope (decl, b, /*is_friend=*/false);
4393 cp_finish_decl (decl, init, /*init_const_expr_p=*/false, NULL_TREE,
4394 LOOKUP_ONLYCONVERTING);
4396 else
4398 DECL_THIS_STATIC (decl) = true;
4399 pushdecl_top_level_and_finish (decl, init);
4402 return decl;
4405 static tree
4406 builtin_function_1 (tree decl, tree context, bool is_global)
4408 tree id = DECL_NAME (decl);
4409 const char *name = IDENTIFIER_POINTER (id);
4411 retrofit_lang_decl (decl);
4413 DECL_ARTIFICIAL (decl) = 1;
4414 SET_OVERLOADED_OPERATOR_CODE (decl, ERROR_MARK);
4415 SET_DECL_LANGUAGE (decl, lang_c);
4416 /* Runtime library routines are, by definition, available in an
4417 external shared object. */
4418 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
4419 DECL_VISIBILITY_SPECIFIED (decl) = 1;
4421 DECL_CONTEXT (decl) = context;
4423 if (is_global)
4424 pushdecl_top_level (decl);
4425 else
4426 pushdecl (decl);
4428 /* A function in the user's namespace should have an explicit
4429 declaration before it is used. Mark the built-in function as
4430 anticipated but not actually declared. */
4431 if (name[0] != '_' || name[1] != '_')
4432 DECL_ANTICIPATED (decl) = 1;
4433 else if (strncmp (name + 2, "builtin_", strlen ("builtin_")) != 0)
4435 size_t len = strlen (name);
4437 /* Treat __*_chk fortification functions as anticipated as well,
4438 unless they are __builtin_*. */
4439 if (len > strlen ("___chk")
4440 && memcmp (name + len - strlen ("_chk"),
4441 "_chk", strlen ("_chk") + 1) == 0)
4442 DECL_ANTICIPATED (decl) = 1;
4445 return decl;
4448 tree
4449 cxx_builtin_function (tree decl)
4451 tree id = DECL_NAME (decl);
4452 const char *name = IDENTIFIER_POINTER (id);
4453 /* All builtins that don't begin with an '_' should additionally
4454 go in the 'std' namespace. */
4455 if (name[0] != '_')
4457 tree decl2 = copy_node(decl);
4458 push_namespace (std_identifier);
4459 builtin_function_1 (decl2, std_node, false);
4460 pop_namespace ();
4463 return builtin_function_1 (decl, NULL_TREE, false);
4466 /* Like cxx_builtin_function, but guarantee the function is added to the global
4467 scope. This is to allow function specific options to add new machine
4468 dependent builtins when the target ISA changes via attribute((target(...)))
4469 which saves space on program startup if the program does not use non-generic
4470 ISAs. */
4472 tree
4473 cxx_builtin_function_ext_scope (tree decl)
4476 tree id = DECL_NAME (decl);
4477 const char *name = IDENTIFIER_POINTER (id);
4478 /* All builtins that don't begin with an '_' should additionally
4479 go in the 'std' namespace. */
4480 if (name[0] != '_')
4482 tree decl2 = copy_node(decl);
4483 push_namespace (std_identifier);
4484 builtin_function_1 (decl2, std_node, true);
4485 pop_namespace ();
4488 return builtin_function_1 (decl, NULL_TREE, true);
4491 /* Generate a FUNCTION_DECL with the typical flags for a runtime library
4492 function. Not called directly. */
4494 static tree
4495 build_library_fn (tree name, enum tree_code operator_code, tree type,
4496 int ecf_flags)
4498 tree fn = build_lang_decl (FUNCTION_DECL, name, type);
4499 DECL_EXTERNAL (fn) = 1;
4500 TREE_PUBLIC (fn) = 1;
4501 DECL_ARTIFICIAL (fn) = 1;
4502 SET_OVERLOADED_OPERATOR_CODE (fn, operator_code);
4503 SET_DECL_LANGUAGE (fn, lang_c);
4504 /* Runtime library routines are, by definition, available in an
4505 external shared object. */
4506 DECL_VISIBILITY (fn) = VISIBILITY_DEFAULT;
4507 DECL_VISIBILITY_SPECIFIED (fn) = 1;
4508 set_call_expr_flags (fn, ecf_flags);
4509 return fn;
4512 /* Returns the _DECL for a library function with C++ linkage. */
4514 static tree
4515 build_cp_library_fn (tree name, enum tree_code operator_code, tree type,
4516 int ecf_flags)
4518 tree fn = build_library_fn (name, operator_code, type, ecf_flags);
4519 DECL_CONTEXT (fn) = FROB_CONTEXT (current_namespace);
4520 SET_DECL_LANGUAGE (fn, lang_cplusplus);
4521 return fn;
4524 /* Like build_library_fn, but takes a C string instead of an
4525 IDENTIFIER_NODE. */
4527 tree
4528 build_library_fn_ptr (const char* name, tree type, int ecf_flags)
4530 return build_library_fn (get_identifier (name), ERROR_MARK, type, ecf_flags);
4533 /* Like build_cp_library_fn, but takes a C string instead of an
4534 IDENTIFIER_NODE. */
4536 tree
4537 build_cp_library_fn_ptr (const char* name, tree type, int ecf_flags)
4539 return build_cp_library_fn (get_identifier (name), ERROR_MARK, type,
4540 ecf_flags);
4543 /* Like build_library_fn, but also pushes the function so that we will
4544 be able to find it via IDENTIFIER_GLOBAL_VALUE. Also, the function
4545 may throw exceptions listed in RAISES. */
4547 tree
4548 push_library_fn (tree name, tree type, tree raises, int ecf_flags)
4550 tree fn;
4552 if (raises)
4553 type = build_exception_variant (type, raises);
4555 fn = build_library_fn (name, ERROR_MARK, type, ecf_flags);
4556 pushdecl_top_level (fn);
4557 return fn;
4560 /* Like build_cp_library_fn, but also pushes the function so that it
4561 will be found by normal lookup. */
4563 static tree
4564 push_cp_library_fn (enum tree_code operator_code, tree type,
4565 int ecf_flags)
4567 tree fn = build_cp_library_fn (ansi_opname (operator_code),
4568 operator_code,
4569 type, ecf_flags);
4570 pushdecl (fn);
4571 if (flag_tm)
4572 apply_tm_attr (fn, get_identifier ("transaction_safe"));
4573 return fn;
4576 /* Like push_library_fn, but takes a TREE_LIST of parm types rather than
4577 a FUNCTION_TYPE. */
4579 tree
4580 push_void_library_fn (tree name, tree parmtypes, int ecf_flags)
4582 tree type = build_function_type (void_type_node, parmtypes);
4583 return push_library_fn (name, type, NULL_TREE, ecf_flags);
4586 /* Like push_library_fn, but also note that this function throws
4587 and does not return. Used for __throw_foo and the like. */
4589 tree
4590 push_throw_library_fn (tree name, tree type)
4592 tree fn = push_library_fn (name, type, NULL_TREE, ECF_NORETURN);
4593 return fn;
4596 /* When we call finish_struct for an anonymous union, we create
4597 default copy constructors and such. But, an anonymous union
4598 shouldn't have such things; this function undoes the damage to the
4599 anonymous union type T.
4601 (The reason that we create the synthesized methods is that we don't
4602 distinguish `union { int i; }' from `typedef union { int i; } U'.
4603 The first is an anonymous union; the second is just an ordinary
4604 union type.) */
4606 void
4607 fixup_anonymous_aggr (tree t)
4609 tree *q;
4611 /* Wipe out memory of synthesized methods. */
4612 TYPE_HAS_USER_CONSTRUCTOR (t) = 0;
4613 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0;
4614 TYPE_HAS_COPY_CTOR (t) = 0;
4615 TYPE_HAS_CONST_COPY_CTOR (t) = 0;
4616 TYPE_HAS_COPY_ASSIGN (t) = 0;
4617 TYPE_HAS_CONST_COPY_ASSIGN (t) = 0;
4619 /* Splice the implicitly generated functions out of the TYPE_METHODS
4620 list. */
4621 q = &TYPE_METHODS (t);
4622 while (*q)
4624 if (DECL_ARTIFICIAL (*q))
4625 *q = TREE_CHAIN (*q);
4626 else
4627 q = &DECL_CHAIN (*q);
4630 /* ISO C++ 9.5.3. Anonymous unions may not have function members. */
4631 if (TYPE_METHODS (t))
4633 tree decl = TYPE_MAIN_DECL (t);
4635 if (TREE_CODE (t) != UNION_TYPE)
4636 error_at (DECL_SOURCE_LOCATION (decl),
4637 "an anonymous struct cannot have function members");
4638 else
4639 error_at (DECL_SOURCE_LOCATION (decl),
4640 "an anonymous union cannot have function members");
4643 /* Anonymous aggregates cannot have fields with ctors, dtors or complex
4644 assignment operators (because they cannot have these methods themselves).
4645 For anonymous unions this is already checked because they are not allowed
4646 in any union, otherwise we have to check it. */
4647 if (TREE_CODE (t) != UNION_TYPE)
4649 tree field, type;
4651 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
4652 if (TREE_CODE (field) == FIELD_DECL)
4654 type = TREE_TYPE (field);
4655 if (CLASS_TYPE_P (type))
4657 if (TYPE_NEEDS_CONSTRUCTING (type))
4658 error ("member %q+#D with constructor not allowed "
4659 "in anonymous aggregate", field);
4660 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
4661 error ("member %q+#D with destructor not allowed "
4662 "in anonymous aggregate", field);
4663 if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type))
4664 error ("member %q+#D with copy assignment operator "
4665 "not allowed in anonymous aggregate", field);
4671 /* Warn for an attribute located at LOCATION that appertains to the
4672 class type CLASS_TYPE that has not been properly placed after its
4673 class-key, in it class-specifier. */
4675 void
4676 warn_misplaced_attr_for_class_type (source_location location,
4677 tree class_type)
4679 gcc_assert (OVERLOAD_TYPE_P (class_type));
4681 if (warning_at (location, OPT_Wattributes,
4682 "attribute ignored in declaration "
4683 "of %q#T", class_type))
4684 inform (location,
4685 "attribute for %q#T must follow the %qs keyword",
4686 class_type, class_key_or_enum_as_string (class_type));
4689 /* Make sure that a declaration with no declarator is well-formed, i.e.
4690 just declares a tagged type or anonymous union.
4692 Returns the type declared; or NULL_TREE if none. */
4694 tree
4695 check_tag_decl (cp_decl_specifier_seq *declspecs,
4696 bool explicit_type_instantiation_p)
4698 int saw_friend = decl_spec_seq_has_spec_p (declspecs, ds_friend);
4699 int saw_typedef = decl_spec_seq_has_spec_p (declspecs, ds_typedef);
4700 /* If a class, struct, or enum type is declared by the DECLSPECS
4701 (i.e, if a class-specifier, enum-specifier, or non-typename
4702 elaborated-type-specifier appears in the DECLSPECS),
4703 DECLARED_TYPE is set to the corresponding type. */
4704 tree declared_type = NULL_TREE;
4705 bool error_p = false;
4707 if (declspecs->multiple_types_p)
4708 error ("multiple types in one declaration");
4709 else if (declspecs->redefined_builtin_type)
4711 if (!in_system_header_at (input_location))
4712 permerror (declspecs->locations[ds_redefined_builtin_type_spec],
4713 "redeclaration of C++ built-in type %qT",
4714 declspecs->redefined_builtin_type);
4715 return NULL_TREE;
4718 if (declspecs->type
4719 && TYPE_P (declspecs->type)
4720 && ((TREE_CODE (declspecs->type) != TYPENAME_TYPE
4721 && MAYBE_CLASS_TYPE_P (declspecs->type))
4722 || TREE_CODE (declspecs->type) == ENUMERAL_TYPE))
4723 declared_type = declspecs->type;
4724 else if (declspecs->type == error_mark_node)
4725 error_p = true;
4726 if (declared_type == NULL_TREE && ! saw_friend && !error_p)
4727 permerror (input_location, "declaration does not declare anything");
4728 else if (declared_type != NULL_TREE && type_uses_auto (declared_type))
4730 error_at (declspecs->locations[ds_type_spec],
4731 "%<auto%> can only be specified for variables "
4732 "or function declarations");
4733 return error_mark_node;
4735 /* Check for an anonymous union. */
4736 else if (declared_type && RECORD_OR_UNION_CODE_P (TREE_CODE (declared_type))
4737 && TYPE_UNNAMED_P (declared_type))
4739 /* 7/3 In a simple-declaration, the optional init-declarator-list
4740 can be omitted only when declaring a class (clause 9) or
4741 enumeration (7.2), that is, when the decl-specifier-seq contains
4742 either a class-specifier, an elaborated-type-specifier with
4743 a class-key (9.1), or an enum-specifier. In these cases and
4744 whenever a class-specifier or enum-specifier is present in the
4745 decl-specifier-seq, the identifiers in these specifiers are among
4746 the names being declared by the declaration (as class-name,
4747 enum-names, or enumerators, depending on the syntax). In such
4748 cases, and except for the declaration of an unnamed bit-field (9.6),
4749 the decl-specifier-seq shall introduce one or more names into the
4750 program, or shall redeclare a name introduced by a previous
4751 declaration. [Example:
4752 enum { }; // ill-formed
4753 typedef class { }; // ill-formed
4754 --end example] */
4755 if (saw_typedef)
4757 error ("missing type-name in typedef-declaration");
4758 return NULL_TREE;
4760 /* Anonymous unions are objects, so they can have specifiers. */;
4761 SET_ANON_AGGR_TYPE_P (declared_type);
4763 if (TREE_CODE (declared_type) != UNION_TYPE
4764 && !in_system_header_at (input_location))
4765 pedwarn (input_location, OPT_Wpedantic, "ISO C++ prohibits anonymous structs");
4768 else
4770 if (decl_spec_seq_has_spec_p (declspecs, ds_inline))
4771 error_at (declspecs->locations[ds_inline],
4772 "%<inline%> can only be specified for functions");
4773 else if (decl_spec_seq_has_spec_p (declspecs, ds_virtual))
4774 error_at (declspecs->locations[ds_virtual],
4775 "%<virtual%> can only be specified for functions");
4776 else if (saw_friend
4777 && (!current_class_type
4778 || current_scope () != current_class_type))
4779 error_at (declspecs->locations[ds_friend],
4780 "%<friend%> can only be specified inside a class");
4781 else if (decl_spec_seq_has_spec_p (declspecs, ds_explicit))
4782 error_at (declspecs->locations[ds_explicit],
4783 "%<explicit%> can only be specified for constructors");
4784 else if (declspecs->storage_class)
4785 error_at (declspecs->locations[ds_storage_class],
4786 "a storage class can only be specified for objects "
4787 "and functions");
4788 else if (decl_spec_seq_has_spec_p (declspecs, ds_const))
4789 error_at (declspecs->locations[ds_const],
4790 "%<const%> can only be specified for objects and "
4791 "functions");
4792 else if (decl_spec_seq_has_spec_p (declspecs, ds_volatile))
4793 error_at (declspecs->locations[ds_volatile],
4794 "%<volatile%> can only be specified for objects and "
4795 "functions");
4796 else if (decl_spec_seq_has_spec_p (declspecs, ds_restrict))
4797 error_at (declspecs->locations[ds_restrict],
4798 "%<__restrict%> can only be specified for objects and "
4799 "functions");
4800 else if (decl_spec_seq_has_spec_p (declspecs, ds_thread))
4801 error_at (declspecs->locations[ds_thread],
4802 "%<__thread%> can only be specified for objects "
4803 "and functions");
4804 else if (saw_typedef)
4805 warning_at (declspecs->locations[ds_typedef], 0,
4806 "%<typedef%> was ignored in this declaration");
4807 else if (decl_spec_seq_has_spec_p (declspecs, ds_constexpr))
4808 error_at (declspecs->locations[ds_constexpr],
4809 "%<constexpr%> cannot be used for type declarations");
4812 if (declspecs->attributes && warn_attributes && declared_type)
4814 location_t loc;
4815 if (!CLASS_TYPE_P (declared_type)
4816 || !CLASSTYPE_TEMPLATE_INSTANTIATION (declared_type))
4817 /* For a non-template class, use the name location. */
4818 loc = location_of (declared_type);
4819 else
4820 /* For a template class (an explicit instantiation), use the
4821 current location. */
4822 loc = input_location;
4824 if (explicit_type_instantiation_p)
4825 /* [dcl.attr.grammar]/4:
4827 No attribute-specifier-seq shall appertain to an explicit
4828 instantiation. */
4830 if (warning_at (loc, OPT_Wattributes,
4831 "attribute ignored in explicit instantiation %q#T",
4832 declared_type))
4833 inform (loc,
4834 "no attribute can be applied to "
4835 "an explicit instantiation");
4837 else
4838 warn_misplaced_attr_for_class_type (loc, declared_type);
4841 return declared_type;
4844 /* Called when a declaration is seen that contains no names to declare.
4845 If its type is a reference to a structure, union or enum inherited
4846 from a containing scope, shadow that tag name for the current scope
4847 with a forward reference.
4848 If its type defines a new named structure or union
4849 or defines an enum, it is valid but we need not do anything here.
4850 Otherwise, it is an error.
4852 C++: may have to grok the declspecs to learn about static,
4853 complain for anonymous unions.
4855 Returns the TYPE declared -- or NULL_TREE if none. */
4857 tree
4858 shadow_tag (cp_decl_specifier_seq *declspecs)
4860 tree t = check_tag_decl (declspecs,
4861 /*explicit_type_instantiation_p=*/false);
4863 if (!t)
4864 return NULL_TREE;
4866 if (maybe_process_partial_specialization (t) == error_mark_node)
4867 return NULL_TREE;
4869 /* This is where the variables in an anonymous union are
4870 declared. An anonymous union declaration looks like:
4871 union { ... } ;
4872 because there is no declarator after the union, the parser
4873 sends that declaration here. */
4874 if (ANON_AGGR_TYPE_P (t))
4876 fixup_anonymous_aggr (t);
4878 if (TYPE_FIELDS (t))
4880 tree decl = grokdeclarator (/*declarator=*/NULL,
4881 declspecs, NORMAL, 0, NULL);
4882 finish_anon_union (decl);
4886 return t;
4889 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
4891 tree
4892 groktypename (cp_decl_specifier_seq *type_specifiers,
4893 const cp_declarator *declarator,
4894 bool is_template_arg)
4896 tree attrs;
4897 tree type;
4898 enum decl_context context
4899 = is_template_arg ? TEMPLATE_TYPE_ARG : TYPENAME;
4900 attrs = type_specifiers->attributes;
4901 type_specifiers->attributes = NULL_TREE;
4902 type = grokdeclarator (declarator, type_specifiers, context, 0, &attrs);
4903 if (attrs && type != error_mark_node)
4905 if (CLASS_TYPE_P (type))
4906 warning (OPT_Wattributes, "ignoring attributes applied to class type %qT "
4907 "outside of definition", type);
4908 else if (MAYBE_CLASS_TYPE_P (type))
4909 /* A template type parameter or other dependent type. */
4910 warning (OPT_Wattributes, "ignoring attributes applied to dependent "
4911 "type %qT without an associated declaration", type);
4912 else
4913 cplus_decl_attributes (&type, attrs, 0);
4915 return type;
4918 /* Process a DECLARATOR for a function-scope variable declaration,
4919 namespace-scope variable declaration, or function declaration.
4920 (Function definitions go through start_function; class member
4921 declarations appearing in the body of the class go through
4922 grokfield.) The DECL corresponding to the DECLARATOR is returned.
4923 If an error occurs, the error_mark_node is returned instead.
4925 DECLSPECS are the decl-specifiers for the declaration. INITIALIZED is
4926 SD_INITIALIZED if an explicit initializer is present, or SD_DEFAULTED
4927 for an explicitly defaulted function, or SD_DELETED for an explicitly
4928 deleted function, but 0 (SD_UNINITIALIZED) if this is a variable
4929 implicitly initialized via a default constructor. ATTRIBUTES and
4930 PREFIX_ATTRIBUTES are GNU attributes associated with this declaration.
4932 The scope represented by the context of the returned DECL is pushed
4933 (if it is not the global namespace) and is assigned to
4934 *PUSHED_SCOPE_P. The caller is then responsible for calling
4935 pop_scope on *PUSHED_SCOPE_P if it is set. */
4937 tree
4938 start_decl (const cp_declarator *declarator,
4939 cp_decl_specifier_seq *declspecs,
4940 int initialized,
4941 tree attributes,
4942 tree prefix_attributes,
4943 tree *pushed_scope_p)
4945 tree decl;
4946 tree context;
4947 bool was_public;
4948 int flags;
4949 bool alias;
4951 *pushed_scope_p = NULL_TREE;
4953 /* An object declared as __attribute__((deprecated)) suppresses
4954 warnings of uses of other deprecated items. */
4955 if (lookup_attribute ("deprecated", attributes))
4956 deprecated_state = DEPRECATED_SUPPRESS;
4958 attributes = chainon (attributes, prefix_attributes);
4960 decl = grokdeclarator (declarator, declspecs, NORMAL, initialized,
4961 &attributes);
4963 deprecated_state = DEPRECATED_NORMAL;
4965 if (decl == NULL_TREE || VOID_TYPE_P (decl)
4966 || decl == error_mark_node)
4967 return error_mark_node;
4969 context = CP_DECL_CONTEXT (decl);
4970 if (context != global_namespace)
4971 *pushed_scope_p = push_scope (context);
4973 /* Is it valid for this decl to have an initializer at all?
4974 If not, set INITIALIZED to zero, which will indirectly
4975 tell `cp_finish_decl' to ignore the initializer once it is parsed. */
4976 if (initialized
4977 && TREE_CODE (decl) == TYPE_DECL)
4979 error ("typedef %qD is initialized (use decltype instead)", decl);
4980 return error_mark_node;
4983 if (initialized)
4985 if (! toplevel_bindings_p ()
4986 && DECL_EXTERNAL (decl))
4987 warning (0, "declaration of %q#D has %<extern%> and is initialized",
4988 decl);
4989 DECL_EXTERNAL (decl) = 0;
4990 if (toplevel_bindings_p ())
4991 TREE_STATIC (decl) = 1;
4993 alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl)) != 0;
4995 if (alias && TREE_CODE (decl) == FUNCTION_DECL)
4996 record_key_method_defined (decl);
4998 /* If this is a typedef that names the class for linkage purposes
4999 (7.1.3p8), apply any attributes directly to the type. */
5000 if (TREE_CODE (decl) == TYPE_DECL
5001 && OVERLOAD_TYPE_P (TREE_TYPE (decl))
5002 && decl == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (decl))))
5003 flags = ATTR_FLAG_TYPE_IN_PLACE;
5004 else
5005 flags = 0;
5007 /* Set attributes here so if duplicate decl, will have proper attributes. */
5008 cplus_decl_attributes (&decl, attributes, flags);
5010 /* Dllimported symbols cannot be defined. Static data members (which
5011 can be initialized in-class and dllimported) go through grokfield,
5012 not here, so we don't need to exclude those decls when checking for
5013 a definition. */
5014 if (initialized && DECL_DLLIMPORT_P (decl))
5016 error ("definition of %q#D is marked %<dllimport%>", decl);
5017 DECL_DLLIMPORT_P (decl) = 0;
5020 /* If #pragma weak was used, mark the decl weak now. */
5021 if (!processing_template_decl)
5022 maybe_apply_pragma_weak (decl);
5024 if (TREE_CODE (decl) == FUNCTION_DECL
5025 && DECL_DECLARED_INLINE_P (decl)
5026 && DECL_UNINLINABLE (decl)
5027 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
5028 warning_at (DECL_SOURCE_LOCATION (decl), 0,
5029 "inline function %qD given attribute noinline", decl);
5031 if (TYPE_P (context) && COMPLETE_TYPE_P (complete_type (context)))
5033 bool this_tmpl = (processing_template_decl
5034 > template_class_depth (context));
5035 if (VAR_P (decl))
5037 tree field = lookup_field (context, DECL_NAME (decl), 0, false);
5038 if (field == NULL_TREE
5039 || !(VAR_P (field) || variable_template_p (field)))
5040 error ("%q+#D is not a static data member of %q#T", decl, context);
5041 else if (variable_template_p (field) && !this_tmpl)
5043 if (DECL_LANG_SPECIFIC (decl)
5044 && DECL_TEMPLATE_SPECIALIZATION (decl))
5045 /* OK, specialization was already checked. */;
5046 else
5048 error_at (DECL_SOURCE_LOCATION (decl),
5049 "non-member-template declaration of %qD", decl);
5050 inform (DECL_SOURCE_LOCATION (field), "does not match "
5051 "member template declaration here");
5052 return error_mark_node;
5055 else
5057 if (variable_template_p (field))
5058 field = DECL_TEMPLATE_RESULT (field);
5060 if (DECL_CONTEXT (field) != context)
5062 if (!same_type_p (DECL_CONTEXT (field), context))
5063 permerror (input_location, "ISO C++ does not permit %<%T::%D%> "
5064 "to be defined as %<%T::%D%>",
5065 DECL_CONTEXT (field), DECL_NAME (decl),
5066 context, DECL_NAME (decl));
5067 DECL_CONTEXT (decl) = DECL_CONTEXT (field);
5069 /* Static data member are tricky; an in-class initialization
5070 still doesn't provide a definition, so the in-class
5071 declaration will have DECL_EXTERNAL set, but will have an
5072 initialization. Thus, duplicate_decls won't warn
5073 about this situation, and so we check here. */
5074 if (initialized && DECL_INITIALIZED_IN_CLASS_P (field))
5075 error ("duplicate initialization of %qD", decl);
5076 if (duplicate_decls (decl, field, /*newdecl_is_friend=*/false))
5077 decl = field;
5078 if (decl_spec_seq_has_spec_p (declspecs, ds_constexpr)
5079 && !DECL_DECLARED_CONSTEXPR_P (field))
5080 error ("%qD declared %<constexpr%> outside its class", field);
5083 else
5085 tree field = check_classfn (context, decl,
5086 this_tmpl
5087 ? current_template_parms
5088 : NULL_TREE);
5089 if (field && field != error_mark_node
5090 && duplicate_decls (decl, field,
5091 /*newdecl_is_friend=*/false))
5092 decl = field;
5095 /* cp_finish_decl sets DECL_EXTERNAL if DECL_IN_AGGR_P is set. */
5096 DECL_IN_AGGR_P (decl) = 0;
5097 /* Do not mark DECL as an explicit specialization if it was not
5098 already marked as an instantiation; a declaration should
5099 never be marked as a specialization unless we know what
5100 template is being specialized. */
5101 if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
5103 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
5104 if (TREE_CODE (decl) == FUNCTION_DECL)
5105 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
5106 && DECL_DECLARED_INLINE_P (decl));
5107 else
5108 DECL_COMDAT (decl) = false;
5110 /* [temp.expl.spec] An explicit specialization of a static data
5111 member of a template is a definition if the declaration
5112 includes an initializer; otherwise, it is a declaration.
5114 We check for processing_specialization so this only applies
5115 to the new specialization syntax. */
5116 if (!initialized && processing_specialization)
5117 DECL_EXTERNAL (decl) = 1;
5120 if (DECL_EXTERNAL (decl) && ! DECL_TEMPLATE_SPECIALIZATION (decl)
5121 /* Aliases are definitions. */
5122 && !alias)
5123 permerror (input_location, "declaration of %q#D outside of class is not definition",
5124 decl);
5127 was_public = TREE_PUBLIC (decl);
5129 /* Enter this declaration into the symbol table. Don't push the plain
5130 VAR_DECL for a variable template. */
5131 if (!template_parm_scope_p ()
5132 || !VAR_P (decl))
5133 decl = maybe_push_decl (decl);
5135 if (processing_template_decl)
5136 decl = push_template_decl (decl);
5137 if (decl == error_mark_node)
5138 return error_mark_node;
5140 if (VAR_P (decl)
5141 && DECL_NAMESPACE_SCOPE_P (decl) && !TREE_PUBLIC (decl) && !was_public
5142 && !DECL_THIS_STATIC (decl) && !DECL_ARTIFICIAL (decl))
5144 /* This is a const variable with implicit 'static'. Set
5145 DECL_THIS_STATIC so we can tell it from variables that are
5146 !TREE_PUBLIC because of the anonymous namespace. */
5147 gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (decl)) || errorcount);
5148 DECL_THIS_STATIC (decl) = 1;
5151 if (current_function_decl && VAR_P (decl)
5152 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
5154 bool ok = false;
5155 if (CP_DECL_THREAD_LOCAL_P (decl))
5156 error ("%qD declared %<thread_local%> in %<constexpr%> function",
5157 decl);
5158 else if (TREE_STATIC (decl))
5159 error ("%qD declared %<static%> in %<constexpr%> function", decl);
5160 else
5161 ok = true;
5162 if (!ok)
5163 cp_function_chain->invalid_constexpr = true;
5166 if (!processing_template_decl && VAR_P (decl))
5167 start_decl_1 (decl, initialized);
5169 return decl;
5172 /* Process the declaration of a variable DECL. INITIALIZED is true
5173 iff DECL is explicitly initialized. (INITIALIZED is false if the
5174 variable is initialized via an implicitly-called constructor.)
5175 This function must be called for ordinary variables (including, for
5176 example, implicit instantiations of templates), but must not be
5177 called for template declarations. */
5179 void
5180 start_decl_1 (tree decl, bool initialized)
5182 tree type;
5183 bool complete_p;
5184 bool aggregate_definition_p;
5186 gcc_assert (!processing_template_decl);
5188 if (error_operand_p (decl))
5189 return;
5191 gcc_assert (VAR_P (decl));
5193 type = TREE_TYPE (decl);
5194 complete_p = COMPLETE_TYPE_P (type);
5195 aggregate_definition_p = MAYBE_CLASS_TYPE_P (type) && !DECL_EXTERNAL (decl);
5197 /* If an explicit initializer is present, or if this is a definition
5198 of an aggregate, then we need a complete type at this point.
5199 (Scalars are always complete types, so there is nothing to
5200 check.) This code just sets COMPLETE_P; errors (if necessary)
5201 are issued below. */
5202 if ((initialized || aggregate_definition_p)
5203 && !complete_p
5204 && COMPLETE_TYPE_P (complete_type (type)))
5206 complete_p = true;
5207 /* We will not yet have set TREE_READONLY on DECL if the type
5208 was "const", but incomplete, before this point. But, now, we
5209 have a complete type, so we can try again. */
5210 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
5213 if (initialized)
5214 /* Is it valid for this decl to have an initializer at all? */
5216 /* Don't allow initializations for incomplete types except for
5217 arrays which might be completed by the initialization. */
5218 if (complete_p)
5219 ; /* A complete type is ok. */
5220 else if (type_uses_auto (type))
5221 ; /* An auto type is ok. */
5222 else if (TREE_CODE (type) != ARRAY_TYPE)
5224 error ("variable %q#D has initializer but incomplete type", decl);
5225 type = TREE_TYPE (decl) = error_mark_node;
5227 else if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
5229 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
5230 error ("elements of array %q#D have incomplete type", decl);
5231 /* else we already gave an error in start_decl. */
5234 else if (aggregate_definition_p && !complete_p)
5236 if (type_uses_auto (type))
5237 gcc_unreachable ();
5238 else
5239 error ("aggregate %q#D has incomplete type and cannot be defined",
5240 decl);
5241 /* Change the type so that assemble_variable will give
5242 DECL an rtl we can live with: (mem (const_int 0)). */
5243 type = TREE_TYPE (decl) = error_mark_node;
5246 /* Create a new scope to hold this declaration if necessary.
5247 Whether or not a new scope is necessary cannot be determined
5248 until after the type has been completed; if the type is a
5249 specialization of a class template it is not until after
5250 instantiation has occurred that TYPE_HAS_NONTRIVIAL_DESTRUCTOR
5251 will be set correctly. */
5252 maybe_push_cleanup_level (type);
5255 /* Handle initialization of references. DECL, TYPE, and INIT have the
5256 same meaning as in cp_finish_decl. *CLEANUP must be NULL on entry,
5257 but will be set to a new CLEANUP_STMT if a temporary is created
5258 that must be destroyed subsequently.
5260 Returns an initializer expression to use to initialize DECL, or
5261 NULL if the initialization can be performed statically.
5263 Quotes on semantics can be found in ARM 8.4.3. */
5265 static tree
5266 grok_reference_init (tree decl, tree type, tree init, int flags)
5268 if (init == NULL_TREE)
5270 if ((DECL_LANG_SPECIFIC (decl) == 0
5271 || DECL_IN_AGGR_P (decl) == 0)
5272 && ! DECL_THIS_EXTERN (decl))
5273 error ("%qD declared as reference but not initialized", decl);
5274 return NULL_TREE;
5277 if (TREE_CODE (init) == TREE_LIST)
5278 init = build_x_compound_expr_from_list (init, ELK_INIT,
5279 tf_warning_or_error);
5281 tree ttype = TREE_TYPE (type);
5282 if (TREE_CODE (ttype) != ARRAY_TYPE
5283 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
5284 /* Note: default conversion is only called in very special cases. */
5285 init = decay_conversion (init, tf_warning_or_error);
5287 /* check_initializer handles this for non-reference variables, but for
5288 references we need to do it here or the initializer will get the
5289 incomplete array type and confuse later calls to
5290 cp_complete_array_type. */
5291 if (TREE_CODE (ttype) == ARRAY_TYPE
5292 && TYPE_DOMAIN (ttype) == NULL_TREE
5293 && (BRACE_ENCLOSED_INITIALIZER_P (init)
5294 || TREE_CODE (init) == STRING_CST))
5296 cp_complete_array_type (&ttype, init, false);
5297 if (ttype != TREE_TYPE (type))
5298 type = cp_build_reference_type (ttype, TYPE_REF_IS_RVALUE (type));
5301 /* Convert INIT to the reference type TYPE. This may involve the
5302 creation of a temporary, whose lifetime must be the same as that
5303 of the reference. If so, a DECL_EXPR for the temporary will be
5304 added just after the DECL_EXPR for DECL. That's why we don't set
5305 DECL_INITIAL for local references (instead assigning to them
5306 explicitly); we need to allow the temporary to be initialized
5307 first. */
5308 return initialize_reference (type, init, flags,
5309 tf_warning_or_error);
5312 /* Designated initializers in arrays are not supported in GNU C++.
5313 The parser cannot detect this error since it does not know whether
5314 a given brace-enclosed initializer is for a class type or for an
5315 array. This function checks that CE does not use a designated
5316 initializer. If it does, an error is issued. Returns true if CE
5317 is valid, i.e., does not have a designated initializer. */
5319 static bool
5320 check_array_designated_initializer (constructor_elt *ce,
5321 unsigned HOST_WIDE_INT index)
5323 /* Designated initializers for array elements are not supported. */
5324 if (ce->index)
5326 /* The parser only allows identifiers as designated
5327 initializers. */
5328 if (ce->index == error_mark_node)
5330 error ("name used in a GNU-style designated "
5331 "initializer for an array");
5332 return false;
5334 else if (identifier_p (ce->index))
5336 error ("name %qD used in a GNU-style designated "
5337 "initializer for an array", ce->index);
5338 return false;
5341 tree ce_index = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5342 ce->index, true);
5343 if (ce_index
5344 && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (ce_index))
5345 && (TREE_CODE (ce_index = maybe_constant_value (ce_index))
5346 == INTEGER_CST))
5348 /* A C99 designator is OK if it matches the current index. */
5349 if (wi::eq_p (ce_index, index))
5350 return true;
5351 else
5352 sorry ("non-trivial designated initializers not supported");
5354 else
5355 error ("C99 designator %qE is not an integral constant-expression",
5356 ce->index);
5358 return false;
5361 return true;
5364 /* When parsing `int a[] = {1, 2};' we don't know the size of the
5365 array until we finish parsing the initializer. If that's the
5366 situation we're in, update DECL accordingly. */
5368 static void
5369 maybe_deduce_size_from_array_init (tree decl, tree init)
5371 tree type = TREE_TYPE (decl);
5373 if (TREE_CODE (type) == ARRAY_TYPE
5374 && TYPE_DOMAIN (type) == NULL_TREE
5375 && TREE_CODE (decl) != TYPE_DECL)
5377 /* do_default is really a C-ism to deal with tentative definitions.
5378 But let's leave it here to ease the eventual merge. */
5379 int do_default = !DECL_EXTERNAL (decl);
5380 tree initializer = init ? init : DECL_INITIAL (decl);
5381 int failure = 0;
5383 /* Check that there are no designated initializers in INIT, as
5384 those are not supported in GNU C++, and as the middle-end
5385 will crash if presented with a non-numeric designated
5386 initializer. */
5387 if (initializer && BRACE_ENCLOSED_INITIALIZER_P (initializer))
5389 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initializer);
5390 constructor_elt *ce;
5391 HOST_WIDE_INT i;
5392 FOR_EACH_VEC_SAFE_ELT (v, i, ce)
5393 if (!check_array_designated_initializer (ce, i))
5394 failure = 1;
5397 if (!failure)
5399 failure = cp_complete_array_type (&TREE_TYPE (decl), initializer,
5400 do_default);
5401 if (failure == 1)
5403 error_at (EXPR_LOC_OR_LOC (initializer,
5404 DECL_SOURCE_LOCATION (decl)),
5405 "initializer fails to determine size of %qD", decl);
5407 else if (failure == 2)
5409 if (do_default)
5411 error_at (DECL_SOURCE_LOCATION (decl),
5412 "array size missing in %qD", decl);
5414 /* If a `static' var's size isn't known, make it extern as
5415 well as static, so it does not get allocated. If it's not
5416 `static', then don't mark it extern; finish_incomplete_decl
5417 will give it a default size and it will get allocated. */
5418 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
5419 DECL_EXTERNAL (decl) = 1;
5421 else if (failure == 3)
5423 error_at (DECL_SOURCE_LOCATION (decl),
5424 "zero-size array %qD", decl);
5428 cp_apply_type_quals_to_decl (cp_type_quals (TREE_TYPE (decl)), decl);
5430 relayout_decl (decl);
5434 /* Set DECL_SIZE, DECL_ALIGN, etc. for DECL (a VAR_DECL), and issue
5435 any appropriate error messages regarding the layout. */
5437 static void
5438 layout_var_decl (tree decl)
5440 tree type;
5442 type = TREE_TYPE (decl);
5443 if (type == error_mark_node)
5444 return;
5446 /* If we haven't already laid out this declaration, do so now.
5447 Note that we must not call complete type for an external object
5448 because it's type might involve templates that we are not
5449 supposed to instantiate yet. (And it's perfectly valid to say
5450 `extern X x' for some incomplete type `X'.) */
5451 if (!DECL_EXTERNAL (decl))
5452 complete_type (type);
5453 if (!DECL_SIZE (decl)
5454 && TREE_TYPE (decl) != error_mark_node
5455 && complete_or_array_type_p (type))
5456 layout_decl (decl, 0);
5458 if (!DECL_EXTERNAL (decl) && DECL_SIZE (decl) == NULL_TREE)
5460 /* An automatic variable with an incomplete type: that is an error.
5461 Don't talk about array types here, since we took care of that
5462 message in grokdeclarator. */
5463 error_at (DECL_SOURCE_LOCATION (decl),
5464 "storage size of %qD isn%'t known", decl);
5465 TREE_TYPE (decl) = error_mark_node;
5467 #if 0
5468 /* Keep this code around in case we later want to control debug info
5469 based on whether a type is "used". (jason 1999-11-11) */
5471 else if (!DECL_EXTERNAL (decl) && MAYBE_CLASS_TYPE_P (ttype))
5472 /* Let debugger know it should output info for this type. */
5473 note_debug_info_needed (ttype);
5475 if (TREE_STATIC (decl) && DECL_CLASS_SCOPE_P (decl))
5476 note_debug_info_needed (DECL_CONTEXT (decl));
5477 #endif
5479 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
5480 && DECL_SIZE (decl) != NULL_TREE
5481 && ! TREE_CONSTANT (DECL_SIZE (decl)))
5483 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
5484 constant_expression_warning (DECL_SIZE (decl));
5485 else
5487 error_at (DECL_SOURCE_LOCATION (decl),
5488 "storage size of %qD isn%'t constant", decl);
5489 TREE_TYPE (decl) = error_mark_node;
5494 /* If a local static variable is declared in an inline function, or if
5495 we have a weak definition, we must endeavor to create only one
5496 instance of the variable at link-time. */
5498 void
5499 maybe_commonize_var (tree decl)
5501 /* Static data in a function with comdat linkage also has comdat
5502 linkage. */
5503 if ((TREE_STATIC (decl)
5504 /* Don't mess with __FUNCTION__. */
5505 && ! DECL_ARTIFICIAL (decl)
5506 && DECL_FUNCTION_SCOPE_P (decl)
5507 && vague_linkage_p (DECL_CONTEXT (decl)))
5508 || (TREE_PUBLIC (decl) && DECL_INLINE_VAR_P (decl)))
5510 if (flag_weak)
5512 /* With weak symbols, we simply make the variable COMDAT;
5513 that will cause copies in multiple translations units to
5514 be merged. */
5515 comdat_linkage (decl);
5517 else
5519 if (DECL_INITIAL (decl) == NULL_TREE
5520 || DECL_INITIAL (decl) == error_mark_node)
5522 /* Without weak symbols, we can use COMMON to merge
5523 uninitialized variables. */
5524 TREE_PUBLIC (decl) = 1;
5525 DECL_COMMON (decl) = 1;
5527 else
5529 /* While for initialized variables, we must use internal
5530 linkage -- which means that multiple copies will not
5531 be merged. */
5532 TREE_PUBLIC (decl) = 0;
5533 DECL_COMMON (decl) = 0;
5534 const char *msg;
5535 if (DECL_INLINE_VAR_P (decl))
5536 msg = G_("sorry: semantics of inline variable "
5537 "%q#D are wrong (you%'ll wind up with "
5538 "multiple copies)");
5539 else
5540 msg = G_("sorry: semantics of inline function "
5541 "static data %q#D are wrong (you%'ll wind "
5542 "up with multiple copies)");
5543 if (warning_at (DECL_SOURCE_LOCATION (decl), 0,
5544 msg, decl))
5545 inform (DECL_SOURCE_LOCATION (decl),
5546 "you can work around this by removing the initializer");
5552 /* Issue an error message if DECL is an uninitialized const variable. */
5554 static void
5555 check_for_uninitialized_const_var (tree decl)
5557 tree type = strip_array_types (TREE_TYPE (decl));
5559 /* ``Unless explicitly declared extern, a const object does not have
5560 external linkage and must be initialized. ($8.4; $12.1)'' ARM
5561 7.1.6 */
5562 if (VAR_P (decl)
5563 && TREE_CODE (type) != REFERENCE_TYPE
5564 && (CP_TYPE_CONST_P (type) || var_in_constexpr_fn (decl))
5565 && !DECL_INITIAL (decl))
5567 tree field = default_init_uninitialized_part (type);
5568 if (!field)
5569 return;
5571 if (CP_TYPE_CONST_P (type))
5572 permerror (DECL_SOURCE_LOCATION (decl),
5573 "uninitialized const %qD", decl);
5574 else
5576 error_at (DECL_SOURCE_LOCATION (decl),
5577 "uninitialized variable %qD in %<constexpr%> function",
5578 decl);
5579 cp_function_chain->invalid_constexpr = true;
5582 if (CLASS_TYPE_P (type))
5584 tree defaulted_ctor;
5586 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
5587 "%q#T has no user-provided default constructor", type);
5588 defaulted_ctor = in_class_defaulted_default_constructor (type);
5589 if (defaulted_ctor)
5590 inform (DECL_SOURCE_LOCATION (defaulted_ctor),
5591 "constructor is not user-provided because it is "
5592 "explicitly defaulted in the class body");
5593 inform (DECL_SOURCE_LOCATION (field),
5594 "and the implicitly-defined constructor does not "
5595 "initialize %q#D", field);
5600 /* Structure holding the current initializer being processed by reshape_init.
5601 CUR is a pointer to the current element being processed, END is a pointer
5602 after the last element present in the initializer. */
5603 struct reshape_iter
5605 constructor_elt *cur;
5606 constructor_elt *end;
5609 static tree reshape_init_r (tree, reshape_iter *, bool, tsubst_flags_t);
5611 /* FIELD is a FIELD_DECL or NULL. In the former case, the value
5612 returned is the next FIELD_DECL (possibly FIELD itself) that can be
5613 initialized. If there are no more such fields, the return value
5614 will be NULL. */
5616 tree
5617 next_initializable_field (tree field)
5619 while (field
5620 && (TREE_CODE (field) != FIELD_DECL
5621 || (DECL_C_BIT_FIELD (field) && !DECL_NAME (field))
5622 || (DECL_ARTIFICIAL (field)
5623 && !(cxx_dialect >= cxx1z && DECL_FIELD_IS_BASE (field)))))
5624 field = DECL_CHAIN (field);
5626 return field;
5629 /* Return true for [dcl.init.list] direct-list-initialization from
5630 single element of enumeration with a fixed underlying type. */
5632 bool
5633 is_direct_enum_init (tree type, tree init)
5635 if (cxx_dialect >= cxx1z
5636 && TREE_CODE (type) == ENUMERAL_TYPE
5637 && ENUM_FIXED_UNDERLYING_TYPE_P (type)
5638 && TREE_CODE (init) == CONSTRUCTOR
5639 && CONSTRUCTOR_IS_DIRECT_INIT (init)
5640 && CONSTRUCTOR_NELTS (init) == 1)
5641 return true;
5642 return false;
5645 /* Subroutine of reshape_init_array and reshape_init_vector, which does
5646 the actual work. ELT_TYPE is the element type of the array. MAX_INDEX is an
5647 INTEGER_CST representing the size of the array minus one (the maximum index),
5648 or NULL_TREE if the array was declared without specifying the size. D is
5649 the iterator within the constructor. */
5651 static tree
5652 reshape_init_array_1 (tree elt_type, tree max_index, reshape_iter *d,
5653 tsubst_flags_t complain)
5655 tree new_init;
5656 bool sized_array_p = (max_index && TREE_CONSTANT (max_index));
5657 unsigned HOST_WIDE_INT max_index_cst = 0;
5658 unsigned HOST_WIDE_INT index;
5660 /* The initializer for an array is always a CONSTRUCTOR. */
5661 new_init = build_constructor (init_list_type_node, NULL);
5663 if (sized_array_p)
5665 /* Minus 1 is used for zero sized arrays. */
5666 if (integer_all_onesp (max_index))
5667 return new_init;
5669 if (tree_fits_uhwi_p (max_index))
5670 max_index_cst = tree_to_uhwi (max_index);
5671 /* sizetype is sign extended, not zero extended. */
5672 else
5673 max_index_cst = tree_to_uhwi (fold_convert (size_type_node, max_index));
5676 /* Loop until there are no more initializers. */
5677 for (index = 0;
5678 d->cur != d->end && (!sized_array_p || index <= max_index_cst);
5679 ++index)
5681 tree elt_init;
5682 constructor_elt *old_cur = d->cur;
5684 check_array_designated_initializer (d->cur, index);
5685 elt_init = reshape_init_r (elt_type, d, /*first_initializer_p=*/false,
5686 complain);
5687 if (elt_init == error_mark_node)
5688 return error_mark_node;
5689 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init),
5690 size_int (index), elt_init);
5691 if (!TREE_CONSTANT (elt_init))
5692 TREE_CONSTANT (new_init) = false;
5694 /* This can happen with an invalid initializer (c++/54501). */
5695 if (d->cur == old_cur && !sized_array_p)
5696 break;
5699 return new_init;
5702 /* Subroutine of reshape_init_r, processes the initializers for arrays.
5703 Parameters are the same of reshape_init_r. */
5705 static tree
5706 reshape_init_array (tree type, reshape_iter *d, tsubst_flags_t complain)
5708 tree max_index = NULL_TREE;
5710 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
5712 if (TYPE_DOMAIN (type))
5713 max_index = array_type_nelts (type);
5715 return reshape_init_array_1 (TREE_TYPE (type), max_index, d, complain);
5718 /* Subroutine of reshape_init_r, processes the initializers for vectors.
5719 Parameters are the same of reshape_init_r. */
5721 static tree
5722 reshape_init_vector (tree type, reshape_iter *d, tsubst_flags_t complain)
5724 tree max_index = NULL_TREE;
5726 gcc_assert (VECTOR_TYPE_P (type));
5728 if (COMPOUND_LITERAL_P (d->cur->value))
5730 tree value = d->cur->value;
5731 if (!same_type_p (TREE_TYPE (value), type))
5733 if (complain & tf_error)
5734 error ("invalid type %qT as initializer for a vector of type %qT",
5735 TREE_TYPE (d->cur->value), type);
5736 value = error_mark_node;
5738 ++d->cur;
5739 return value;
5742 /* For a vector, we initialize it as an array of the appropriate size. */
5743 if (VECTOR_TYPE_P (type))
5744 max_index = size_int (TYPE_VECTOR_SUBPARTS (type) - 1);
5746 return reshape_init_array_1 (TREE_TYPE (type), max_index, d, complain);
5749 /* Subroutine of reshape_init_r, processes the initializers for classes
5750 or union. Parameters are the same of reshape_init_r. */
5752 static tree
5753 reshape_init_class (tree type, reshape_iter *d, bool first_initializer_p,
5754 tsubst_flags_t complain)
5756 tree field;
5757 tree new_init;
5759 gcc_assert (CLASS_TYPE_P (type));
5761 /* The initializer for a class is always a CONSTRUCTOR. */
5762 new_init = build_constructor (init_list_type_node, NULL);
5763 field = next_initializable_field (TYPE_FIELDS (type));
5765 if (!field)
5767 /* [dcl.init.aggr]
5769 An initializer for an aggregate member that is an
5770 empty class shall have the form of an empty
5771 initializer-list {}. */
5772 if (!first_initializer_p)
5774 if (complain & tf_error)
5775 error ("initializer for %qT must be brace-enclosed", type);
5776 return error_mark_node;
5778 return new_init;
5781 /* Loop through the initializable fields, gathering initializers. */
5782 while (d->cur != d->end)
5784 tree field_init;
5785 constructor_elt *old_cur = d->cur;
5787 /* Handle designated initializers, as an extension. */
5788 if (d->cur->index)
5790 if (d->cur->index == error_mark_node)
5791 return error_mark_node;
5793 if (TREE_CODE (d->cur->index) == FIELD_DECL)
5794 /* We already reshaped this. */
5795 gcc_assert (d->cur->index == field);
5796 else if (TREE_CODE (d->cur->index) == IDENTIFIER_NODE)
5797 field = lookup_field_1 (type, d->cur->index, /*want_type=*/false);
5798 else
5800 if (complain & tf_error)
5801 error ("%<[%E] =%> used in a GNU-style designated initializer"
5802 " for class %qT", d->cur->index, type);
5803 return error_mark_node;
5806 if (!field || TREE_CODE (field) != FIELD_DECL)
5808 if (complain & tf_error)
5809 error ("%qT has no non-static data member named %qD", type,
5810 d->cur->index);
5811 return error_mark_node;
5815 /* If we processed all the member of the class, we are done. */
5816 if (!field)
5817 break;
5819 field_init = reshape_init_r (TREE_TYPE (field), d,
5820 /*first_initializer_p=*/false, complain);
5821 if (field_init == error_mark_node)
5822 return error_mark_node;
5824 if (d->cur == old_cur && d->cur->index)
5826 /* This can happen with an invalid initializer for a flexible
5827 array member (c++/54441). */
5828 if (complain & tf_error)
5829 error ("invalid initializer for %q#D", field);
5830 return error_mark_node;
5833 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init), field, field_init);
5835 /* [dcl.init.aggr]
5837 When a union is initialized with a brace-enclosed
5838 initializer, the braces shall only contain an
5839 initializer for the first member of the union. */
5840 if (TREE_CODE (type) == UNION_TYPE)
5841 break;
5843 field = next_initializable_field (DECL_CHAIN (field));
5846 return new_init;
5849 /* Subroutine of reshape_init_r. We're in a context where C99 initializer
5850 designators are not valid; either complain or return true to indicate
5851 that reshape_init_r should return error_mark_node. */
5853 static bool
5854 has_designator_problem (reshape_iter *d, tsubst_flags_t complain)
5856 if (d->cur->index)
5858 if (complain & tf_error)
5859 error ("C99 designator %qE outside aggregate initializer",
5860 d->cur->index);
5861 else
5862 return true;
5864 return false;
5867 /* Subroutine of reshape_init, which processes a single initializer (part of
5868 a CONSTRUCTOR). TYPE is the type of the variable being initialized, D is the
5869 iterator within the CONSTRUCTOR which points to the initializer to process.
5870 FIRST_INITIALIZER_P is true if this is the first initializer of the
5871 outermost CONSTRUCTOR node. */
5873 static tree
5874 reshape_init_r (tree type, reshape_iter *d, bool first_initializer_p,
5875 tsubst_flags_t complain)
5877 tree init = d->cur->value;
5879 if (error_operand_p (init))
5880 return error_mark_node;
5882 if (first_initializer_p && !CP_AGGREGATE_TYPE_P (type)
5883 && has_designator_problem (d, complain))
5884 return error_mark_node;
5886 if (TREE_CODE (type) == COMPLEX_TYPE)
5888 /* A complex type can be initialized from one or two initializers,
5889 but braces are not elided. */
5890 d->cur++;
5891 if (BRACE_ENCLOSED_INITIALIZER_P (init))
5893 if (CONSTRUCTOR_NELTS (init) > 2)
5895 if (complain & tf_error)
5896 error ("too many initializers for %qT", type);
5897 else
5898 return error_mark_node;
5901 else if (first_initializer_p && d->cur != d->end)
5903 vec<constructor_elt, va_gc> *v = 0;
5904 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, init);
5905 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, d->cur->value);
5906 if (has_designator_problem (d, complain))
5907 return error_mark_node;
5908 d->cur++;
5909 init = build_constructor (init_list_type_node, v);
5911 return init;
5914 /* A non-aggregate type is always initialized with a single
5915 initializer. */
5916 if (!CP_AGGREGATE_TYPE_P (type))
5918 /* It is invalid to initialize a non-aggregate type with a
5919 brace-enclosed initializer before C++0x.
5920 We need to check for BRACE_ENCLOSED_INITIALIZER_P here because
5921 of g++.old-deja/g++.mike/p7626.C: a pointer-to-member constant is
5922 a CONSTRUCTOR (with a record type). */
5923 if (TREE_CODE (init) == CONSTRUCTOR
5924 /* Don't complain about a capture-init. */
5925 && !CONSTRUCTOR_IS_DIRECT_INIT (init)
5926 && BRACE_ENCLOSED_INITIALIZER_P (init)) /* p7626.C */
5928 if (SCALAR_TYPE_P (type))
5930 if (cxx_dialect < cxx11
5931 /* Isn't value-initialization. */
5932 || CONSTRUCTOR_NELTS (init) > 0)
5934 if (complain & tf_error)
5935 error ("braces around scalar initializer for type %qT",
5936 type);
5937 init = error_mark_node;
5940 else
5941 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5944 d->cur++;
5945 return init;
5948 /* "If T is a class type and the initializer list has a single element of
5949 type cv U, where U is T or a class derived from T, the object is
5950 initialized from that element." Even if T is an aggregate. */
5951 if (cxx_dialect >= cxx11 && (CLASS_TYPE_P (type) || VECTOR_TYPE_P (type))
5952 && first_initializer_p
5953 && d->end - d->cur == 1
5954 && reference_related_p (type, TREE_TYPE (init)))
5956 d->cur++;
5957 return init;
5960 /* [dcl.init.aggr]
5962 All implicit type conversions (clause _conv_) are considered when
5963 initializing the aggregate member with an initializer from an
5964 initializer-list. If the initializer can initialize a member,
5965 the member is initialized. Otherwise, if the member is itself a
5966 non-empty subaggregate, brace elision is assumed and the
5967 initializer is considered for the initialization of the first
5968 member of the subaggregate. */
5969 if (TREE_CODE (init) != CONSTRUCTOR
5970 /* But don't try this for the first initializer, since that would be
5971 looking through the outermost braces; A a2 = { a1 }; is not a
5972 valid aggregate initialization. */
5973 && !first_initializer_p
5974 && (same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (init))
5975 || can_convert_arg (type, TREE_TYPE (init), init, LOOKUP_NORMAL,
5976 complain)))
5978 d->cur++;
5979 return init;
5982 /* [dcl.init.string]
5984 A char array (whether plain char, signed char, or unsigned char)
5985 can be initialized by a string-literal (optionally enclosed in
5986 braces); a wchar_t array can be initialized by a wide
5987 string-literal (optionally enclosed in braces). */
5988 if (TREE_CODE (type) == ARRAY_TYPE
5989 && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type))))
5991 tree str_init = init;
5993 /* Strip one level of braces if and only if they enclose a single
5994 element (as allowed by [dcl.init.string]). */
5995 if (!first_initializer_p
5996 && TREE_CODE (str_init) == CONSTRUCTOR
5997 && CONSTRUCTOR_NELTS (str_init) == 1)
5999 str_init = (*CONSTRUCTOR_ELTS (str_init))[0].value;
6002 /* If it's a string literal, then it's the initializer for the array
6003 as a whole. Otherwise, continue with normal initialization for
6004 array types (one value per array element). */
6005 if (TREE_CODE (str_init) == STRING_CST)
6007 if (has_designator_problem (d, complain))
6008 return error_mark_node;
6009 d->cur++;
6010 return str_init;
6014 /* The following cases are about aggregates. If we are not within a full
6015 initializer already, and there is not a CONSTRUCTOR, it means that there
6016 is a missing set of braces (that is, we are processing the case for
6017 which reshape_init exists). */
6018 if (!first_initializer_p)
6020 if (TREE_CODE (init) == CONSTRUCTOR)
6022 if (TREE_TYPE (init) && TYPE_PTRMEMFUNC_P (TREE_TYPE (init)))
6023 /* There is no need to reshape pointer-to-member function
6024 initializers, as they are always constructed correctly
6025 by the front end. */
6027 else if (COMPOUND_LITERAL_P (init))
6028 /* For a nested compound literal, there is no need to reshape since
6029 brace elision is not allowed. Even if we decided to allow it,
6030 we should add a call to reshape_init in finish_compound_literal,
6031 before calling digest_init, so changing this code would still
6032 not be necessary. */
6033 gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (init));
6034 else
6036 ++d->cur;
6037 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
6038 return reshape_init (type, init, complain);
6042 if (complain & tf_warning)
6043 warning (OPT_Wmissing_braces,
6044 "missing braces around initializer for %qT",
6045 type);
6048 /* Dispatch to specialized routines. */
6049 if (CLASS_TYPE_P (type))
6050 return reshape_init_class (type, d, first_initializer_p, complain);
6051 else if (TREE_CODE (type) == ARRAY_TYPE)
6052 return reshape_init_array (type, d, complain);
6053 else if (VECTOR_TYPE_P (type))
6054 return reshape_init_vector (type, d, complain);
6055 else
6056 gcc_unreachable();
6059 /* Undo the brace-elision allowed by [dcl.init.aggr] in a
6060 brace-enclosed aggregate initializer.
6062 INIT is the CONSTRUCTOR containing the list of initializers describing
6063 a brace-enclosed initializer for an entity of the indicated aggregate TYPE.
6064 It may not presently match the shape of the TYPE; for example:
6066 struct S { int a; int b; };
6067 struct S a[] = { 1, 2, 3, 4 };
6069 Here INIT will hold a vector of four elements, rather than a
6070 vector of two elements, each itself a vector of two elements. This
6071 routine transforms INIT from the former form into the latter. The
6072 revised CONSTRUCTOR node is returned. */
6074 tree
6075 reshape_init (tree type, tree init, tsubst_flags_t complain)
6077 vec<constructor_elt, va_gc> *v;
6078 reshape_iter d;
6079 tree new_init;
6081 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
6083 v = CONSTRUCTOR_ELTS (init);
6085 /* An empty constructor does not need reshaping, and it is always a valid
6086 initializer. */
6087 if (vec_safe_is_empty (v))
6088 return init;
6090 /* Handle [dcl.init.list] direct-list-initialization from
6091 single element of enumeration with a fixed underlying type. */
6092 if (is_direct_enum_init (type, init))
6094 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
6095 if (check_narrowing (ENUM_UNDERLYING_TYPE (type), elt, complain))
6096 return cp_build_c_cast (type, elt, tf_warning_or_error);
6097 else
6098 return error_mark_node;
6101 /* Recurse on this CONSTRUCTOR. */
6102 d.cur = &(*v)[0];
6103 d.end = d.cur + v->length ();
6105 new_init = reshape_init_r (type, &d, true, complain);
6106 if (new_init == error_mark_node)
6107 return error_mark_node;
6109 /* Make sure all the element of the constructor were used. Otherwise,
6110 issue an error about exceeding initializers. */
6111 if (d.cur != d.end)
6113 if (complain & tf_error)
6114 error ("too many initializers for %qT", type);
6115 else
6116 return error_mark_node;
6119 if (CONSTRUCTOR_IS_DIRECT_INIT (init)
6120 && BRACE_ENCLOSED_INITIALIZER_P (new_init))
6121 CONSTRUCTOR_IS_DIRECT_INIT (new_init) = true;
6123 return new_init;
6126 /* Verify array initializer. Returns true if errors have been reported. */
6128 bool
6129 check_array_initializer (tree decl, tree type, tree init)
6131 tree element_type = TREE_TYPE (type);
6133 /* The array type itself need not be complete, because the
6134 initializer may tell us how many elements are in the array.
6135 But, the elements of the array must be complete. */
6136 if (!COMPLETE_TYPE_P (complete_type (element_type)))
6138 if (decl)
6139 error_at (DECL_SOURCE_LOCATION (decl),
6140 "elements of array %q#D have incomplete type", decl);
6141 else
6142 error ("elements of array %q#T have incomplete type", type);
6143 return true;
6145 /* A compound literal can't have variable size. */
6146 if (init && !decl
6147 && ((COMPLETE_TYPE_P (type) && !TREE_CONSTANT (TYPE_SIZE (type)))
6148 || !TREE_CONSTANT (TYPE_SIZE (element_type))))
6150 error ("variable-sized compound literal");
6151 return true;
6153 return false;
6156 /* Subroutine of check_initializer; args are passed down from that function.
6157 Set stmts_are_full_exprs_p to 1 across a call to build_aggr_init. */
6159 static tree
6160 build_aggr_init_full_exprs (tree decl, tree init, int flags)
6163 gcc_assert (stmts_are_full_exprs_p ());
6164 return build_aggr_init (decl, init, flags, tf_warning_or_error);
6167 /* Verify INIT (the initializer for DECL), and record the
6168 initialization in DECL_INITIAL, if appropriate. CLEANUP is as for
6169 grok_reference_init.
6171 If the return value is non-NULL, it is an expression that must be
6172 evaluated dynamically to initialize DECL. */
6174 static tree
6175 check_initializer (tree decl, tree init, int flags, vec<tree, va_gc> **cleanups)
6177 tree type = TREE_TYPE (decl);
6178 tree init_code = NULL;
6179 tree core_type;
6181 /* Things that are going to be initialized need to have complete
6182 type. */
6183 TREE_TYPE (decl) = type = complete_type (TREE_TYPE (decl));
6185 if (DECL_HAS_VALUE_EXPR_P (decl))
6187 /* A variable with DECL_HAS_VALUE_EXPR_P set is just a placeholder,
6188 it doesn't have storage to be initialized. */
6189 gcc_assert (init == NULL_TREE);
6190 return NULL_TREE;
6193 if (type == error_mark_node)
6194 /* We will have already complained. */
6195 return NULL_TREE;
6197 if (TREE_CODE (type) == ARRAY_TYPE)
6199 if (check_array_initializer (decl, type, init))
6200 return NULL_TREE;
6202 else if (!COMPLETE_TYPE_P (type))
6204 error_at (DECL_SOURCE_LOCATION (decl),
6205 "%q#D has incomplete type", decl);
6206 TREE_TYPE (decl) = error_mark_node;
6207 return NULL_TREE;
6209 else
6210 /* There is no way to make a variable-sized class type in GNU C++. */
6211 gcc_assert (TREE_CONSTANT (TYPE_SIZE (type)));
6213 if (init && BRACE_ENCLOSED_INITIALIZER_P (init))
6215 int init_len = CONSTRUCTOR_NELTS (init);
6216 if (SCALAR_TYPE_P (type))
6218 if (init_len == 0)
6220 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6221 init = build_zero_init (type, NULL_TREE, false);
6223 else if (init_len != 1 && TREE_CODE (type) != COMPLEX_TYPE)
6225 error_at (EXPR_LOC_OR_LOC (init, DECL_SOURCE_LOCATION (decl)),
6226 "scalar object %qD requires one element in "
6227 "initializer", decl);
6228 TREE_TYPE (decl) = error_mark_node;
6229 return NULL_TREE;
6234 if (TREE_CODE (decl) == CONST_DECL)
6236 gcc_assert (TREE_CODE (type) != REFERENCE_TYPE);
6238 DECL_INITIAL (decl) = init;
6240 gcc_assert (init != NULL_TREE);
6241 init = NULL_TREE;
6243 else if (!init && DECL_REALLY_EXTERN (decl))
6245 else if (init || type_build_ctor_call (type)
6246 || TREE_CODE (type) == REFERENCE_TYPE)
6248 if (TREE_CODE (type) == REFERENCE_TYPE)
6250 init = grok_reference_init (decl, type, init, flags);
6251 flags |= LOOKUP_ALREADY_DIGESTED;
6253 else if (!init)
6254 check_for_uninitialized_const_var (decl);
6255 /* Do not reshape constructors of vectors (they don't need to be
6256 reshaped. */
6257 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
6259 if (is_std_init_list (type))
6261 init = perform_implicit_conversion (type, init,
6262 tf_warning_or_error);
6263 flags |= LOOKUP_ALREADY_DIGESTED;
6265 else if (TYPE_NON_AGGREGATE_CLASS (type))
6267 /* Don't reshape if the class has constructors. */
6268 if (cxx_dialect == cxx98)
6269 error_at (EXPR_LOC_OR_LOC (init, DECL_SOURCE_LOCATION (decl)),
6270 "in C++98 %qD must be initialized by "
6271 "constructor, not by %<{...}%>",
6272 decl);
6274 else if (VECTOR_TYPE_P (type) && TYPE_VECTOR_OPAQUE (type))
6276 error ("opaque vector types cannot be initialized");
6277 init = error_mark_node;
6279 else
6281 init = reshape_init (type, init, tf_warning_or_error);
6282 flags |= LOOKUP_NO_NARROWING;
6285 else if (TREE_CODE (init) == TREE_LIST
6286 && TREE_TYPE (init) != unknown_type_node
6287 && !MAYBE_CLASS_TYPE_P (type))
6289 gcc_assert (TREE_CODE (decl) != RESULT_DECL);
6291 /* We get here with code like `int a (2);' */
6292 init = build_x_compound_expr_from_list (init, ELK_INIT,
6293 tf_warning_or_error);
6296 /* If DECL has an array type without a specific bound, deduce the
6297 array size from the initializer. */
6298 maybe_deduce_size_from_array_init (decl, init);
6299 type = TREE_TYPE (decl);
6300 if (type == error_mark_node)
6301 return NULL_TREE;
6303 if (((type_build_ctor_call (type) || CLASS_TYPE_P (type))
6304 && !(flags & LOOKUP_ALREADY_DIGESTED)
6305 && !(init && BRACE_ENCLOSED_INITIALIZER_P (init)
6306 && CP_AGGREGATE_TYPE_P (type)
6307 && (CLASS_TYPE_P (type)
6308 || !TYPE_NEEDS_CONSTRUCTING (type)
6309 || type_has_extended_temps (type))))
6310 || (DECL_DECOMPOSITION_P (decl) && TREE_CODE (type) == ARRAY_TYPE))
6312 init_code = build_aggr_init_full_exprs (decl, init, flags);
6314 /* A constructor call is a non-trivial initializer even if
6315 it isn't explicitly written. */
6316 if (TREE_SIDE_EFFECTS (init_code))
6317 DECL_NONTRIVIALLY_INITIALIZED_P (decl) = true;
6319 /* If this is a constexpr initializer, expand_default_init will
6320 have returned an INIT_EXPR rather than a CALL_EXPR. In that
6321 case, pull the initializer back out and pass it down into
6322 store_init_value. */
6323 while (TREE_CODE (init_code) == EXPR_STMT
6324 || TREE_CODE (init_code) == CONVERT_EXPR)
6325 init_code = TREE_OPERAND (init_code, 0);
6326 if (TREE_CODE (init_code) == INIT_EXPR)
6328 init = TREE_OPERAND (init_code, 1);
6329 init_code = NULL_TREE;
6330 /* Don't call digest_init; it's unnecessary and will complain
6331 about aggregate initialization of non-aggregate classes. */
6332 flags |= LOOKUP_ALREADY_DIGESTED;
6334 else if (DECL_DECLARED_CONSTEXPR_P (decl))
6336 /* Declared constexpr, but no suitable initializer; massage
6337 init appropriately so we can pass it into store_init_value
6338 for the error. */
6339 if (CLASS_TYPE_P (type)
6340 && (!init || TREE_CODE (init) == TREE_LIST))
6342 init = build_functional_cast (type, init, tf_none);
6343 if (TREE_CODE (init) == TARGET_EXPR)
6344 TARGET_EXPR_DIRECT_INIT_P (init) = true;
6346 init_code = NULL_TREE;
6348 else
6349 init = NULL_TREE;
6352 if (init && TREE_CODE (init) != TREE_VEC)
6354 /* In aggregate initialization of a variable, each element
6355 initialization is a full-expression because there is no
6356 enclosing expression. */
6357 gcc_assert (stmts_are_full_exprs_p ());
6359 init_code = store_init_value (decl, init, cleanups, flags);
6361 if (pedantic && TREE_CODE (type) == ARRAY_TYPE
6362 && DECL_INITIAL (decl)
6363 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
6364 && PAREN_STRING_LITERAL_P (DECL_INITIAL (decl)))
6365 warning_at (EXPR_LOC_OR_LOC (DECL_INITIAL (decl),
6366 DECL_SOURCE_LOCATION (decl)),
6367 0, "array %qD initialized by parenthesized "
6368 "string literal %qE",
6369 decl, DECL_INITIAL (decl));
6370 init = NULL;
6373 else
6375 if (CLASS_TYPE_P (core_type = strip_array_types (type))
6376 && (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type)
6377 || CLASSTYPE_REF_FIELDS_NEED_INIT (core_type)))
6378 diagnose_uninitialized_cst_or_ref_member (core_type, /*using_new=*/false,
6379 /*complain=*/true);
6381 check_for_uninitialized_const_var (decl);
6384 if (init && init != error_mark_node)
6385 init_code = build2 (INIT_EXPR, type, decl, init);
6387 if (init_code)
6389 /* We might have set these in cp_finish_decl. */
6390 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = false;
6391 TREE_CONSTANT (decl) = false;
6394 if (init_code
6395 && (DECL_IN_AGGR_P (decl) && !DECL_VAR_DECLARED_INLINE_P (decl)))
6397 static int explained = 0;
6399 if (cxx_dialect < cxx11)
6400 error ("initializer invalid for static member with constructor");
6401 else if (cxx_dialect < cxx1z)
6402 error ("non-constant in-class initialization invalid for static "
6403 "member %qD", decl);
6404 else
6405 error ("non-constant in-class initialization invalid for non-inline "
6406 "static member %qD", decl);
6407 if (!explained)
6409 inform (input_location,
6410 "(an out of class initialization is required)");
6411 explained = 1;
6413 return NULL_TREE;
6416 return init_code;
6419 /* If DECL is not a local variable, give it RTL. */
6421 static void
6422 make_rtl_for_nonlocal_decl (tree decl, tree init, const char* asmspec)
6424 int toplev = toplevel_bindings_p ();
6425 int defer_p;
6427 /* Set the DECL_ASSEMBLER_NAME for the object. */
6428 if (asmspec)
6430 /* The `register' keyword, when used together with an
6431 asm-specification, indicates that the variable should be
6432 placed in a particular register. */
6433 if (VAR_P (decl) && DECL_REGISTER (decl))
6435 set_user_assembler_name (decl, asmspec);
6436 DECL_HARD_REGISTER (decl) = 1;
6438 else
6440 if (TREE_CODE (decl) == FUNCTION_DECL
6441 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
6442 set_builtin_user_assembler_name (decl, asmspec);
6443 set_user_assembler_name (decl, asmspec);
6447 /* Handle non-variables up front. */
6448 if (!VAR_P (decl))
6450 rest_of_decl_compilation (decl, toplev, at_eof);
6451 return;
6454 /* If we see a class member here, it should be a static data
6455 member. */
6456 if (DECL_LANG_SPECIFIC (decl) && DECL_IN_AGGR_P (decl))
6458 gcc_assert (TREE_STATIC (decl));
6459 /* An in-class declaration of a static data member should be
6460 external; it is only a declaration, and not a definition. */
6461 if (init == NULL_TREE)
6462 gcc_assert (DECL_EXTERNAL (decl)
6463 || !TREE_PUBLIC (decl)
6464 || DECL_INLINE_VAR_P (decl));
6467 /* We don't create any RTL for local variables. */
6468 if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
6469 return;
6471 /* We defer emission of local statics until the corresponding
6472 DECL_EXPR is expanded. But with constexpr its function might never
6473 be expanded, so go ahead and tell cgraph about the variable now. */
6474 defer_p = ((DECL_FUNCTION_SCOPE_P (decl)
6475 && !var_in_maybe_constexpr_fn (decl))
6476 || DECL_VIRTUAL_P (decl));
6478 /* Defer template instantiations. */
6479 if (DECL_LANG_SPECIFIC (decl)
6480 && DECL_IMPLICIT_INSTANTIATION (decl))
6481 defer_p = 1;
6483 /* If we're not deferring, go ahead and assemble the variable. */
6484 if (!defer_p)
6485 rest_of_decl_compilation (decl, toplev, at_eof);
6488 /* walk_tree helper for wrap_temporary_cleanups, below. */
6490 static tree
6491 wrap_cleanups_r (tree *stmt_p, int *walk_subtrees, void *data)
6493 /* Stop at types or full-expression boundaries. */
6494 if (TYPE_P (*stmt_p)
6495 || TREE_CODE (*stmt_p) == CLEANUP_POINT_EXPR)
6497 *walk_subtrees = 0;
6498 return NULL_TREE;
6501 if (TREE_CODE (*stmt_p) == TARGET_EXPR)
6503 tree guard = (tree)data;
6504 tree tcleanup = TARGET_EXPR_CLEANUP (*stmt_p);
6506 tcleanup = build2 (TRY_CATCH_EXPR, void_type_node, tcleanup, guard);
6507 /* Tell honor_protect_cleanup_actions to handle this as a separate
6508 cleanup. */
6509 TRY_CATCH_IS_CLEANUP (tcleanup) = 1;
6511 TARGET_EXPR_CLEANUP (*stmt_p) = tcleanup;
6514 return NULL_TREE;
6517 /* We're initializing a local variable which has a cleanup GUARD. If there
6518 are any temporaries used in the initializer INIT of this variable, we
6519 need to wrap their cleanups with TRY_CATCH_EXPR (, GUARD) so that the
6520 variable will be cleaned up properly if one of them throws.
6522 Unfortunately, there's no way to express this properly in terms of
6523 nesting, as the regions for the temporaries overlap the region for the
6524 variable itself; if there are two temporaries, the variable needs to be
6525 the first thing destroyed if either of them throws. However, we only
6526 want to run the variable's cleanup if it actually got constructed. So
6527 we need to guard the temporary cleanups with the variable's cleanup if
6528 they are run on the normal path, but not if they are run on the
6529 exceptional path. We implement this by telling
6530 honor_protect_cleanup_actions to strip the variable cleanup from the
6531 exceptional path. */
6533 static void
6534 wrap_temporary_cleanups (tree init, tree guard)
6536 cp_walk_tree_without_duplicates (&init, wrap_cleanups_r, (void *)guard);
6539 /* Generate code to initialize DECL (a local variable). */
6541 static void
6542 initialize_local_var (tree decl, tree init)
6544 tree type = TREE_TYPE (decl);
6545 tree cleanup;
6546 int already_used;
6548 gcc_assert (VAR_P (decl)
6549 || TREE_CODE (decl) == RESULT_DECL);
6550 gcc_assert (!TREE_STATIC (decl));
6552 if (DECL_SIZE (decl) == NULL_TREE)
6554 /* If we used it already as memory, it must stay in memory. */
6555 DECL_INITIAL (decl) = NULL_TREE;
6556 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
6557 return;
6560 if (type == error_mark_node)
6561 return;
6563 /* Compute and store the initial value. */
6564 already_used = TREE_USED (decl) || TREE_USED (type);
6565 if (TREE_USED (type))
6566 DECL_READ_P (decl) = 1;
6568 /* Generate a cleanup, if necessary. */
6569 cleanup = cxx_maybe_build_cleanup (decl, tf_warning_or_error);
6571 /* Perform the initialization. */
6572 if (init)
6574 tree rinit = (TREE_CODE (init) == INIT_EXPR
6575 ? TREE_OPERAND (init, 1) : NULL_TREE);
6576 if (rinit && !TREE_SIDE_EFFECTS (rinit))
6578 /* Stick simple initializers in DECL_INITIAL so that
6579 -Wno-init-self works (c++/34772). */
6580 gcc_assert (TREE_OPERAND (init, 0) == decl);
6581 DECL_INITIAL (decl) = rinit;
6583 if (warn_init_self && TREE_CODE (type) == REFERENCE_TYPE)
6585 STRIP_NOPS (rinit);
6586 if (rinit == decl)
6587 warning_at (DECL_SOURCE_LOCATION (decl),
6588 OPT_Winit_self,
6589 "reference %qD is initialized with itself", decl);
6592 else
6594 int saved_stmts_are_full_exprs_p;
6596 /* If we're only initializing a single object, guard the
6597 destructors of any temporaries used in its initializer with
6598 its destructor. This isn't right for arrays because each
6599 element initialization is a full-expression. */
6600 if (cleanup && TREE_CODE (type) != ARRAY_TYPE)
6601 wrap_temporary_cleanups (init, cleanup);
6603 gcc_assert (building_stmt_list_p ());
6604 saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
6605 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
6606 finish_expr_stmt (init);
6607 current_stmt_tree ()->stmts_are_full_exprs_p =
6608 saved_stmts_are_full_exprs_p;
6612 /* Set this to 0 so we can tell whether an aggregate which was
6613 initialized was ever used. Don't do this if it has a
6614 destructor, so we don't complain about the 'resource
6615 allocation is initialization' idiom. Now set
6616 attribute((unused)) on types so decls of that type will be
6617 marked used. (see TREE_USED, above.) */
6618 if (TYPE_NEEDS_CONSTRUCTING (type)
6619 && ! already_used
6620 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type)
6621 && DECL_NAME (decl))
6622 TREE_USED (decl) = 0;
6623 else if (already_used)
6624 TREE_USED (decl) = 1;
6626 if (cleanup)
6627 finish_decl_cleanup (decl, cleanup);
6630 /* DECL is a VAR_DECL for a compiler-generated variable with static
6631 storage duration (like a virtual table) whose initializer is a
6632 compile-time constant. Initialize the variable and provide it to the
6633 back end. */
6635 void
6636 initialize_artificial_var (tree decl, vec<constructor_elt, va_gc> *v)
6638 tree init;
6639 gcc_assert (DECL_ARTIFICIAL (decl));
6640 init = build_constructor (TREE_TYPE (decl), v);
6641 gcc_assert (TREE_CODE (init) == CONSTRUCTOR);
6642 DECL_INITIAL (decl) = init;
6643 DECL_INITIALIZED_P (decl) = 1;
6644 determine_visibility (decl);
6645 layout_var_decl (decl);
6646 maybe_commonize_var (decl);
6647 make_rtl_for_nonlocal_decl (decl, init, /*asmspec=*/NULL);
6650 /* INIT is the initializer for a variable, as represented by the
6651 parser. Returns true iff INIT is type-dependent. */
6653 static bool
6654 type_dependent_init_p (tree init)
6656 if (TREE_CODE (init) == TREE_LIST)
6657 /* A parenthesized initializer, e.g.: int i (3, 2); ? */
6658 return any_type_dependent_elements_p (init);
6659 else if (TREE_CODE (init) == CONSTRUCTOR)
6660 /* A brace-enclosed initializer, e.g.: int i = { 3 }; ? */
6662 vec<constructor_elt, va_gc> *elts;
6663 size_t nelts;
6664 size_t i;
6666 elts = CONSTRUCTOR_ELTS (init);
6667 nelts = vec_safe_length (elts);
6668 for (i = 0; i < nelts; ++i)
6669 if (type_dependent_init_p ((*elts)[i].value))
6670 return true;
6672 else
6673 /* It must be a simple expression, e.g., int i = 3; */
6674 return type_dependent_expression_p (init);
6676 return false;
6679 /* INIT is the initializer for a variable, as represented by the
6680 parser. Returns true iff INIT is value-dependent. */
6682 static bool
6683 value_dependent_init_p (tree init)
6685 if (TREE_CODE (init) == TREE_LIST)
6686 /* A parenthesized initializer, e.g.: int i (3, 2); ? */
6687 return any_value_dependent_elements_p (init);
6688 else if (TREE_CODE (init) == CONSTRUCTOR)
6689 /* A brace-enclosed initializer, e.g.: int i = { 3 }; ? */
6691 vec<constructor_elt, va_gc> *elts;
6692 size_t nelts;
6693 size_t i;
6695 elts = CONSTRUCTOR_ELTS (init);
6696 nelts = vec_safe_length (elts);
6697 for (i = 0; i < nelts; ++i)
6698 if (value_dependent_init_p ((*elts)[i].value))
6699 return true;
6701 else
6702 /* It must be a simple expression, e.g., int i = 3; */
6703 return value_dependent_expression_p (init);
6705 return false;
6708 // Returns true if a DECL is VAR_DECL with the concept specifier.
6709 static inline bool
6710 is_concept_var (tree decl)
6712 return (VAR_P (decl)
6713 // Not all variables have DECL_LANG_SPECIFIC.
6714 && DECL_LANG_SPECIFIC (decl)
6715 && DECL_DECLARED_CONCEPT_P (decl));
6718 /* A helper function to be called via walk_tree. If any label exists
6719 under *TP, it is (going to be) forced. Set has_forced_label_in_static. */
6721 static tree
6722 notice_forced_label_r (tree *tp, int *walk_subtrees, void *)
6724 if (TYPE_P (*tp))
6725 *walk_subtrees = 0;
6726 if (TREE_CODE (*tp) == LABEL_DECL)
6727 cfun->has_forced_label_in_static = 1;
6728 return NULL_TREE;
6731 /* Finish processing of a declaration;
6732 install its line number and initial value.
6733 If the length of an array type is not known before,
6734 it must be determined now, from the initial value, or it is an error.
6736 INIT is the initializer (if any) for DECL. If INIT_CONST_EXPR_P is
6737 true, then INIT is an integral constant expression.
6739 FLAGS is LOOKUP_ONLYCONVERTING if the = init syntax was used, else 0
6740 if the (init) syntax was used. */
6742 void
6743 cp_finish_decl (tree decl, tree init, bool init_const_expr_p,
6744 tree asmspec_tree, int flags)
6746 tree type;
6747 vec<tree, va_gc> *cleanups = NULL;
6748 const char *asmspec = NULL;
6749 int was_readonly = 0;
6750 bool var_definition_p = false;
6751 tree auto_node;
6753 if (decl == error_mark_node)
6754 return;
6755 else if (! decl)
6757 if (init)
6758 error ("assignment (not initialization) in declaration");
6759 return;
6762 gcc_assert (TREE_CODE (decl) != RESULT_DECL);
6763 /* Parameters are handled by store_parm_decls, not cp_finish_decl. */
6764 gcc_assert (TREE_CODE (decl) != PARM_DECL);
6766 type = TREE_TYPE (decl);
6767 if (type == error_mark_node)
6768 return;
6770 /* Warn about register storage specifiers except when in GNU global
6771 or local register variable extension. */
6772 if (VAR_P (decl) && DECL_REGISTER (decl) && asmspec_tree == NULL_TREE)
6774 if (cxx_dialect >= cxx1z)
6775 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
6776 "ISO C++1z does not allow %<register%> storage "
6777 "class specifier");
6778 else
6779 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
6780 "%<register%> storage class specifier used");
6783 /* If a name was specified, get the string. */
6784 if (at_namespace_scope_p ())
6785 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
6786 if (asmspec_tree && asmspec_tree != error_mark_node)
6787 asmspec = TREE_STRING_POINTER (asmspec_tree);
6789 if (current_class_type
6790 && CP_DECL_CONTEXT (decl) == current_class_type
6791 && TYPE_BEING_DEFINED (current_class_type)
6792 && !CLASSTYPE_TEMPLATE_INSTANTIATION (current_class_type)
6793 && (DECL_INITIAL (decl) || init))
6794 DECL_INITIALIZED_IN_CLASS_P (decl) = 1;
6796 if (TREE_CODE (decl) != FUNCTION_DECL
6797 && (auto_node = type_uses_auto (type)))
6799 tree d_init;
6800 if (init == NULL_TREE)
6802 if (DECL_LANG_SPECIFIC (decl)
6803 && DECL_TEMPLATE_INSTANTIATION (decl)
6804 && !DECL_TEMPLATE_INSTANTIATED (decl))
6806 /* init is null because we're deferring instantiating the
6807 initializer until we need it. Well, we need it now. */
6808 instantiate_decl (decl, /*defer_ok*/true, /*expl*/false);
6809 return;
6812 gcc_unreachable ();
6814 d_init = init;
6815 if (TREE_CODE (d_init) == TREE_LIST
6816 && !CLASS_PLACEHOLDER_TEMPLATE (auto_node))
6817 d_init = build_x_compound_expr_from_list (d_init, ELK_INIT,
6818 tf_warning_or_error);
6819 d_init = resolve_nondeduced_context (d_init, tf_warning_or_error);
6820 enum auto_deduction_context adc = adc_variable_type;
6821 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
6822 adc = adc_decomp_type;
6823 type = TREE_TYPE (decl) = do_auto_deduction (type, d_init, auto_node,
6824 tf_warning_or_error, adc);
6825 if (type == error_mark_node)
6826 return;
6827 if (TREE_CODE (type) == FUNCTION_TYPE)
6829 error ("initializer for %<decltype(auto) %D%> has function type "
6830 "(did you forget the %<()%> ?)", decl);
6831 TREE_TYPE (decl) = error_mark_node;
6832 return;
6834 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
6837 if (!ensure_literal_type_for_constexpr_object (decl))
6838 DECL_DECLARED_CONSTEXPR_P (decl) = 0;
6840 if (VAR_P (decl)
6841 && DECL_CLASS_SCOPE_P (decl)
6842 && DECL_INITIALIZED_IN_CLASS_P (decl))
6843 check_static_variable_definition (decl, type);
6845 if (init && TREE_CODE (decl) == FUNCTION_DECL)
6847 tree clone;
6848 if (init == ridpointers[(int)RID_DELETE])
6850 /* FIXME check this is 1st decl. */
6851 DECL_DELETED_FN (decl) = 1;
6852 DECL_DECLARED_INLINE_P (decl) = 1;
6853 DECL_INITIAL (decl) = error_mark_node;
6854 FOR_EACH_CLONE (clone, decl)
6856 DECL_DELETED_FN (clone) = 1;
6857 DECL_DECLARED_INLINE_P (clone) = 1;
6858 DECL_INITIAL (clone) = error_mark_node;
6860 init = NULL_TREE;
6862 else if (init == ridpointers[(int)RID_DEFAULT])
6864 if (defaultable_fn_check (decl))
6865 DECL_DEFAULTED_FN (decl) = 1;
6866 else
6867 DECL_INITIAL (decl) = NULL_TREE;
6871 if (init && VAR_P (decl))
6873 DECL_NONTRIVIALLY_INITIALIZED_P (decl) = 1;
6874 /* If DECL is a reference, then we want to know whether init is a
6875 reference constant; init_const_expr_p as passed tells us whether
6876 it's an rvalue constant. */
6877 if (TREE_CODE (type) == REFERENCE_TYPE)
6878 init_const_expr_p = potential_constant_expression (init);
6879 if (init_const_expr_p)
6881 /* Set these flags now for templates. We'll update the flags in
6882 store_init_value for instantiations. */
6883 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
6884 if (decl_maybe_constant_var_p (decl)
6885 /* FIXME setting TREE_CONSTANT on refs breaks the back end. */
6886 && TREE_CODE (type) != REFERENCE_TYPE)
6887 TREE_CONSTANT (decl) = 1;
6891 if (processing_template_decl)
6893 bool type_dependent_p;
6895 /* Add this declaration to the statement-tree. */
6896 if (at_function_scope_p ())
6897 add_decl_expr (decl);
6899 type_dependent_p = dependent_type_p (type);
6901 if (check_for_bare_parameter_packs (init))
6903 init = NULL_TREE;
6904 DECL_INITIAL (decl) = NULL_TREE;
6907 /* Generally, initializers in templates are expanded when the
6908 template is instantiated. But, if DECL is a variable constant
6909 then it can be used in future constant expressions, so its value
6910 must be available. */
6912 if (!VAR_P (decl) || type_dependent_p)
6913 /* We can't do anything if the decl has dependent type. */;
6914 else if (init
6915 && init_const_expr_p
6916 && TREE_CODE (type) != REFERENCE_TYPE
6917 && decl_maybe_constant_var_p (decl)
6918 && !type_dependent_init_p (init)
6919 && !value_dependent_init_p (init))
6921 /* This variable seems to be a non-dependent constant, so process
6922 its initializer. If check_initializer returns non-null the
6923 initialization wasn't constant after all. */
6924 tree init_code;
6925 cleanups = make_tree_vector ();
6926 init_code = check_initializer (decl, init, flags, &cleanups);
6927 if (init_code == NULL_TREE)
6928 init = NULL_TREE;
6929 release_tree_vector (cleanups);
6931 else if (!init && is_concept_var (decl))
6932 error ("variable concept has no initializer");
6933 else if (!DECL_PRETTY_FUNCTION_P (decl))
6935 /* Deduce array size even if the initializer is dependent. */
6936 maybe_deduce_size_from_array_init (decl, init);
6937 /* And complain about multiple initializers. */
6938 if (init && TREE_CODE (init) == TREE_LIST && TREE_CHAIN (init)
6939 && !MAYBE_CLASS_TYPE_P (type))
6940 init = build_x_compound_expr_from_list (init, ELK_INIT,
6941 tf_warning_or_error);
6944 if (init)
6945 DECL_INITIAL (decl) = init;
6946 return;
6949 /* Just store non-static data member initializers for later. */
6950 if (init && TREE_CODE (decl) == FIELD_DECL)
6951 DECL_INITIAL (decl) = init;
6953 /* Take care of TYPE_DECLs up front. */
6954 if (TREE_CODE (decl) == TYPE_DECL)
6956 if (type != error_mark_node
6957 && MAYBE_CLASS_TYPE_P (type) && DECL_NAME (decl))
6959 if (TREE_TYPE (DECL_NAME (decl)) && TREE_TYPE (decl) != type)
6960 warning (0, "shadowing previous type declaration of %q#D", decl);
6961 set_identifier_type_value (DECL_NAME (decl), decl);
6964 /* If we have installed this as the canonical typedef for this
6965 type, and that type has not been defined yet, delay emitting
6966 the debug information for it, as we will emit it later. */
6967 if (TYPE_MAIN_DECL (TREE_TYPE (decl)) == decl
6968 && !COMPLETE_TYPE_P (TREE_TYPE (decl)))
6969 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
6971 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl),
6972 at_eof);
6973 return;
6976 /* A reference will be modified here, as it is initialized. */
6977 if (! DECL_EXTERNAL (decl)
6978 && TREE_READONLY (decl)
6979 && TREE_CODE (type) == REFERENCE_TYPE)
6981 was_readonly = 1;
6982 TREE_READONLY (decl) = 0;
6985 if (VAR_P (decl))
6987 /* If this is a local variable that will need a mangled name,
6988 register it now. We must do this before processing the
6989 initializer for the variable, since the initialization might
6990 require a guard variable, and since the mangled name of the
6991 guard variable will depend on the mangled name of this
6992 variable. */
6993 if (DECL_FUNCTION_SCOPE_P (decl)
6994 && TREE_STATIC (decl)
6995 && !DECL_ARTIFICIAL (decl))
6997 push_local_name (decl);
6998 /* Normally has_forced_label_in_static is set during GIMPLE
6999 lowering, but [cd]tors are never actually compiled directly.
7000 We need to set this early so we can deal with the label
7001 address extension. */
7002 if ((DECL_CONSTRUCTOR_P (current_function_decl)
7003 || DECL_DESTRUCTOR_P (current_function_decl))
7004 && init)
7006 walk_tree (&init, notice_forced_label_r, NULL, NULL);
7007 add_local_decl (cfun, decl);
7009 /* And make sure it's in the symbol table for
7010 c_parse_final_cleanups to find. */
7011 varpool_node::get_create (decl);
7014 /* Convert the initializer to the type of DECL, if we have not
7015 already initialized DECL. */
7016 if (!DECL_INITIALIZED_P (decl)
7017 /* If !DECL_EXTERNAL then DECL is being defined. In the
7018 case of a static data member initialized inside the
7019 class-specifier, there can be an initializer even if DECL
7020 is *not* defined. */
7021 && (!DECL_EXTERNAL (decl) || init))
7023 cleanups = make_tree_vector ();
7024 init = check_initializer (decl, init, flags, &cleanups);
7026 /* Handle:
7028 [dcl.init]
7030 The memory occupied by any object of static storage
7031 duration is zero-initialized at program startup before
7032 any other initialization takes place.
7034 We cannot create an appropriate initializer until after
7035 the type of DECL is finalized. If DECL_INITIAL is set,
7036 then the DECL is statically initialized, and any
7037 necessary zero-initialization has already been performed. */
7038 if (TREE_STATIC (decl) && !DECL_INITIAL (decl))
7039 DECL_INITIAL (decl) = build_zero_init (TREE_TYPE (decl),
7040 /*nelts=*/NULL_TREE,
7041 /*static_storage_p=*/true);
7042 /* Remember that the initialization for this variable has
7043 taken place. */
7044 DECL_INITIALIZED_P (decl) = 1;
7045 /* This declaration is the definition of this variable,
7046 unless we are initializing a static data member within
7047 the class specifier. */
7048 if (!DECL_EXTERNAL (decl))
7049 var_definition_p = true;
7051 /* If the variable has an array type, lay out the type, even if
7052 there is no initializer. It is valid to index through the
7053 array, and we must get TYPE_ALIGN set correctly on the array
7054 type. */
7055 else if (TREE_CODE (type) == ARRAY_TYPE)
7056 layout_type (type);
7058 if (TREE_STATIC (decl)
7059 && !at_function_scope_p ()
7060 && current_function_decl == NULL)
7061 /* So decl is a global variable or a static member of a
7062 non local class. Record the types it uses
7063 so that we can decide later to emit debug info for them. */
7064 record_types_used_by_current_var_decl (decl);
7067 /* Add this declaration to the statement-tree. This needs to happen
7068 after the call to check_initializer so that the DECL_EXPR for a
7069 reference temp is added before the DECL_EXPR for the reference itself. */
7070 if (DECL_FUNCTION_SCOPE_P (decl))
7072 /* If we're building a variable sized type, and we might be
7073 reachable other than via the top of the current binding
7074 level, then create a new BIND_EXPR so that we deallocate
7075 the object at the right time. */
7076 if (VAR_P (decl)
7077 && DECL_SIZE (decl)
7078 && !TREE_CONSTANT (DECL_SIZE (decl))
7079 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
7081 tree bind;
7082 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
7083 TREE_SIDE_EFFECTS (bind) = 1;
7084 add_stmt (bind);
7085 BIND_EXPR_BODY (bind) = push_stmt_list ();
7087 add_decl_expr (decl);
7090 /* Let the middle end know about variables and functions -- but not
7091 static data members in uninstantiated class templates. */
7092 if (VAR_OR_FUNCTION_DECL_P (decl))
7094 if (VAR_P (decl))
7096 layout_var_decl (decl);
7097 maybe_commonize_var (decl);
7100 /* This needs to happen after the linkage is set. */
7101 determine_visibility (decl);
7103 if (var_definition_p && TREE_STATIC (decl))
7105 /* If a TREE_READONLY variable needs initialization
7106 at runtime, it is no longer readonly and we need to
7107 avoid MEM_READONLY_P being set on RTL created for it. */
7108 if (init)
7110 if (TREE_READONLY (decl))
7111 TREE_READONLY (decl) = 0;
7112 was_readonly = 0;
7114 else if (was_readonly)
7115 TREE_READONLY (decl) = 1;
7117 /* Likewise if it needs destruction. */
7118 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
7119 TREE_READONLY (decl) = 0;
7122 make_rtl_for_nonlocal_decl (decl, init, asmspec);
7124 /* Check for abstractness of the type. Notice that there is no
7125 need to strip array types here since the check for those types
7126 is already done within create_array_type_for_decl. */
7127 abstract_virtuals_error (decl, type);
7129 if (TREE_TYPE (decl) == error_mark_node)
7130 /* No initialization required. */
7132 else if (TREE_CODE (decl) == FUNCTION_DECL)
7134 if (init)
7136 if (init == ridpointers[(int)RID_DEFAULT])
7138 /* An out-of-class default definition is defined at
7139 the point where it is explicitly defaulted. */
7140 if (DECL_DELETED_FN (decl))
7141 maybe_explain_implicit_delete (decl);
7142 else if (DECL_INITIAL (decl) == error_mark_node)
7143 synthesize_method (decl);
7145 else
7146 error ("function %q#D is initialized like a variable", decl);
7148 /* else no initialization required. */
7150 else if (DECL_EXTERNAL (decl)
7151 && ! (DECL_LANG_SPECIFIC (decl)
7152 && DECL_NOT_REALLY_EXTERN (decl)))
7154 if (init)
7155 DECL_INITIAL (decl) = init;
7157 /* A variable definition. */
7158 else if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
7159 /* Initialize the local variable. */
7160 initialize_local_var (decl, init);
7162 /* If a variable is defined, and then a subsequent
7163 definition with external linkage is encountered, we will
7164 get here twice for the same variable. We want to avoid
7165 calling expand_static_init more than once. For variables
7166 that are not static data members, we can call
7167 expand_static_init only when we actually process the
7168 initializer. It is not legal to redeclare a static data
7169 member, so this issue does not arise in that case. */
7170 else if (var_definition_p && TREE_STATIC (decl))
7171 expand_static_init (decl, init);
7174 /* If a CLEANUP_STMT was created to destroy a temporary bound to a
7175 reference, insert it in the statement-tree now. */
7176 if (cleanups)
7178 unsigned i; tree t;
7179 FOR_EACH_VEC_ELT (*cleanups, i, t)
7180 push_cleanup (decl, t, false);
7181 release_tree_vector (cleanups);
7184 if (was_readonly)
7185 TREE_READONLY (decl) = 1;
7187 invoke_plugin_callbacks (PLUGIN_FINISH_DECL, decl);
7190 /* For class TYPE return itself or some its bases that contain
7191 any direct non-static data members. Return error_mark_node if an
7192 error has been diagnosed. */
7194 static tree
7195 find_decomp_class_base (location_t loc, tree type, tree ret)
7197 bool member_seen = false;
7198 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
7199 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
7200 continue;
7201 else if (ret)
7202 return type;
7203 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
7205 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE)
7206 error_at (loc, "cannot decompose class type %qT because it has an "
7207 "anonymous struct member", type);
7208 else
7209 error_at (loc, "cannot decompose class type %qT because it has an "
7210 "anonymous union member", type);
7211 inform (DECL_SOURCE_LOCATION (field), "declared here");
7212 return error_mark_node;
7214 else if (TREE_PRIVATE (field) || TREE_PROTECTED (field))
7216 error_at (loc, "cannot decompose non-public member %qD of %qT",
7217 field, type);
7218 inform (DECL_SOURCE_LOCATION (field),
7219 TREE_PRIVATE (field) ? "declared private here"
7220 : "declared protected here");
7221 return error_mark_node;
7223 else
7224 member_seen = true;
7226 tree base_binfo, binfo;
7227 tree orig_ret = ret;
7228 int i;
7229 if (member_seen)
7230 ret = type;
7231 for (binfo = TYPE_BINFO (type), i = 0;
7232 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
7234 tree t = find_decomp_class_base (loc, TREE_TYPE (base_binfo), ret);
7235 if (t == error_mark_node)
7236 return error_mark_node;
7237 if (t != NULL_TREE)
7239 if (ret == type)
7241 error_at (loc, "cannot decompose class type %qT: both it and "
7242 "its base class %qT have non-static data members",
7243 type, t);
7244 return error_mark_node;
7246 else if (orig_ret != NULL_TREE)
7247 return t;
7248 else if (ret == t)
7249 /* OK, found the same base along another path. We'll complain
7250 in convert_to_base if it's ambiguous. */;
7251 else if (ret != NULL_TREE)
7253 error_at (loc, "cannot decompose class type %qT: its base "
7254 "classes %qT and %qT have non-static data "
7255 "members", type, ret, t);
7256 return error_mark_node;
7258 else
7259 ret = t;
7262 return ret;
7265 /* Return std::tuple_size<TYPE>::value. */
7267 static tree
7268 get_tuple_size (tree type)
7270 tree args = make_tree_vec (1);
7271 TREE_VEC_ELT (args, 0) = type;
7272 tree inst = lookup_template_class (get_identifier ("tuple_size"), args,
7273 /*in_decl*/NULL_TREE,
7274 /*context*/std_node,
7275 /*entering_scope*/false, tf_none);
7276 inst = complete_type (inst);
7277 if (inst == error_mark_node || !COMPLETE_TYPE_P (inst))
7278 return NULL_TREE;
7279 tree val = lookup_qualified_name (inst, get_identifier ("value"),
7280 /*type*/false, /*complain*/false);
7281 if (TREE_CODE (val) == VAR_DECL || TREE_CODE (val) == CONST_DECL)
7282 val = maybe_constant_value (val);
7283 if (TREE_CODE (val) == INTEGER_CST)
7284 return val;
7285 else
7286 return error_mark_node;
7289 /* Return std::tuple_element<I,TYPE>::type. */
7291 static tree
7292 get_tuple_element_type (tree type, unsigned i)
7294 tree args = make_tree_vec (2);
7295 TREE_VEC_ELT (args, 0) = build_int_cst (integer_type_node, i);
7296 TREE_VEC_ELT (args, 1) = type;
7297 tree inst = lookup_template_class (get_identifier ("tuple_element"), args,
7298 /*in_decl*/NULL_TREE,
7299 /*context*/std_node,
7300 /*entering_scope*/false,
7301 tf_warning_or_error);
7302 return make_typename_type (inst, get_identifier ("type"),
7303 none_type, tf_warning_or_error);
7306 /* Return e.get<i>() or get<i>(e). */
7308 static tree
7309 get_tuple_decomp_init (tree decl, unsigned i)
7311 tree get_id = get_identifier ("get");
7312 tree targs = make_tree_vec (1);
7313 TREE_VEC_ELT (targs, 0) = build_int_cst (integer_type_node, i);
7315 tree etype = TREE_TYPE (decl);
7316 tree e = convert_from_reference (decl);
7318 /* [The id-expression] e is an lvalue if the type of the entity e is an
7319 lvalue reference and an xvalue otherwise. */
7320 if (TREE_CODE (etype) != REFERENCE_TYPE
7321 || TYPE_REF_IS_RVALUE (etype))
7322 e = move (e);
7324 tree fns = lookup_qualified_name (TREE_TYPE (e), get_id,
7325 /*type*/false, /*complain*/false);
7326 if (fns != error_mark_node)
7328 fns = lookup_template_function (fns, targs);
7329 return build_new_method_call (e, fns, /*args*/NULL,
7330 /*path*/NULL_TREE, LOOKUP_NORMAL,
7331 /*fn_p*/NULL, tf_warning_or_error);
7333 else
7335 vec<tree,va_gc> *args = make_tree_vector_single (e);
7336 fns = lookup_template_function (get_id, targs);
7337 fns = perform_koenig_lookup (fns, args, tf_warning_or_error);
7338 return finish_call_expr (fns, &args, /*novirt*/false,
7339 /*koenig*/true, tf_warning_or_error);
7343 /* It's impossible to recover the decltype of a tuple decomposition variable
7344 based on the actual type of the variable, so store it in a hash table. */
7345 static GTY(()) hash_map<tree,tree> *decomp_type_table;
7346 static void
7347 store_decomp_type (tree v, tree t)
7349 if (!decomp_type_table)
7350 decomp_type_table = hash_map<tree,tree>::create_ggc (13);
7351 decomp_type_table->put (v, t);
7354 tree
7355 lookup_decomp_type (tree v)
7357 return *decomp_type_table->get (v);
7360 /* Finish a decomposition declaration. DECL is the underlying declaration
7361 "e", FIRST is the head of a chain of decls for the individual identifiers
7362 chained through DECL_CHAIN in reverse order and COUNT is the number of
7363 those decls. */
7365 void
7366 cp_finish_decomp (tree decl, tree first, unsigned int count)
7368 if (error_operand_p (decl))
7370 error_out:
7371 while (count--)
7373 TREE_TYPE (first) = error_mark_node;
7374 if (DECL_HAS_VALUE_EXPR_P (first))
7376 SET_DECL_VALUE_EXPR (first, NULL_TREE);
7377 DECL_HAS_VALUE_EXPR_P (first) = 0;
7379 first = DECL_CHAIN (first);
7381 if (DECL_P (decl) && DECL_NAMESPACE_SCOPE_P (decl))
7382 SET_DECL_ASSEMBLER_NAME (decl, get_identifier ("<decomp>"));
7383 return;
7386 location_t loc = DECL_SOURCE_LOCATION (decl);
7387 if (type_dependent_expression_p (decl)
7388 /* This happens for range for when not in templates.
7389 Still add the DECL_VALUE_EXPRs for later processing. */
7390 || (!processing_template_decl
7391 && type_uses_auto (TREE_TYPE (decl))))
7393 for (unsigned int i = 0; i < count; i++)
7395 if (!DECL_HAS_VALUE_EXPR_P (first))
7397 tree v = build_nt (ARRAY_REF, decl,
7398 size_int (count - i - 1),
7399 NULL_TREE, NULL_TREE);
7400 SET_DECL_VALUE_EXPR (first, v);
7401 DECL_HAS_VALUE_EXPR_P (first) = 1;
7403 if (processing_template_decl)
7405 retrofit_lang_decl (first);
7406 SET_DECL_DECOMPOSITION_P (first);
7408 first = DECL_CHAIN (first);
7410 return;
7413 auto_vec<tree, 16> v;
7414 v.safe_grow (count);
7415 tree d = first;
7416 for (unsigned int i = 0; i < count; i++, d = DECL_CHAIN (d))
7418 v[count - i - 1] = d;
7419 retrofit_lang_decl (d);
7420 SET_DECL_DECOMPOSITION_P (d);
7423 tree type = TREE_TYPE (decl);
7424 tree dexp = decl;
7426 if (TREE_CODE (type) == REFERENCE_TYPE)
7428 /* If e is a constant reference, use the referent directly. */
7429 if (DECL_INITIAL (decl))
7430 dexp = DECL_INITIAL (decl);
7431 dexp = convert_from_reference (dexp);
7432 type = TREE_TYPE (type);
7435 tree eltype = NULL_TREE;
7436 unsigned HOST_WIDE_INT eltscnt = 0;
7437 if (TREE_CODE (type) == ARRAY_TYPE)
7439 tree nelts;
7440 nelts = array_type_nelts_top (type);
7441 if (nelts == error_mark_node)
7442 goto error_out;
7443 if (!tree_fits_uhwi_p (nelts))
7445 error_at (loc, "cannot decompose variable length array %qT", type);
7446 goto error_out;
7448 eltscnt = tree_to_uhwi (nelts);
7449 if (count != eltscnt)
7451 cnt_mismatch:
7452 if (count > eltscnt)
7453 error_at (loc, "%u names provided while %qT decomposes into "
7454 "%wu elements", count, type, eltscnt);
7455 else
7456 error_at (loc, "only %u names provided while %qT decomposes into "
7457 "%wu elements", count, type, eltscnt);
7458 goto error_out;
7460 eltype = TREE_TYPE (type);
7461 for (unsigned int i = 0; i < count; i++)
7463 TREE_TYPE (v[i]) = eltype;
7464 layout_decl (v[i], 0);
7465 tree t = dexp;
7466 t = build4_loc (DECL_SOURCE_LOCATION (v[i]), ARRAY_REF,
7467 eltype, t, size_int (i), NULL_TREE,
7468 NULL_TREE);
7469 SET_DECL_VALUE_EXPR (v[i], t);
7470 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
7473 /* 2 GNU extensions. */
7474 else if (TREE_CODE (type) == COMPLEX_TYPE)
7476 eltscnt = 2;
7477 if (count != eltscnt)
7478 goto cnt_mismatch;
7479 eltype = cp_build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
7480 for (unsigned int i = 0; i < count; i++)
7482 TREE_TYPE (v[i]) = eltype;
7483 layout_decl (v[i], 0);
7484 tree t = dexp;
7485 t = build1_loc (DECL_SOURCE_LOCATION (v[i]),
7486 i ? IMAGPART_EXPR : REALPART_EXPR, eltype,
7488 SET_DECL_VALUE_EXPR (v[i], t);
7489 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
7492 else if (TREE_CODE (type) == VECTOR_TYPE)
7494 eltscnt = TYPE_VECTOR_SUBPARTS (type);
7495 if (count != eltscnt)
7496 goto cnt_mismatch;
7497 eltype = cp_build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
7498 for (unsigned int i = 0; i < count; i++)
7500 TREE_TYPE (v[i]) = eltype;
7501 layout_decl (v[i], 0);
7502 tree t = dexp;
7503 convert_vector_to_array_for_subscript (DECL_SOURCE_LOCATION (v[i]),
7504 &t, size_int (i));
7505 t = build4_loc (DECL_SOURCE_LOCATION (v[i]), ARRAY_REF,
7506 eltype, t, size_int (i), NULL_TREE,
7507 NULL_TREE);
7508 SET_DECL_VALUE_EXPR (v[i], t);
7509 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
7512 else if (tree tsize = get_tuple_size (type))
7514 if (tsize == error_mark_node)
7516 error_at (loc, "%<std::tuple_size<%T>::value%> is not an integral "
7517 "constant expression", type);
7518 goto error_out;
7520 eltscnt = tree_to_uhwi (tsize);
7521 if (count != eltscnt)
7522 goto cnt_mismatch;
7523 for (unsigned i = 0; i < count; ++i)
7525 location_t sloc = input_location;
7526 location_t dloc = DECL_SOURCE_LOCATION (v[i]);
7528 input_location = dloc;
7529 tree init = get_tuple_decomp_init (decl, i);
7530 tree eltype = (init == error_mark_node ? error_mark_node
7531 : get_tuple_element_type (type, i));
7532 input_location = sloc;
7534 if (init == error_mark_node || eltype == error_mark_node)
7536 inform (dloc, "in initialization of decomposition variable %qD",
7537 v[i]);
7538 goto error_out;
7540 /* Save the decltype away before reference collapse. */
7541 store_decomp_type (v[i], eltype);
7542 eltype = cp_build_reference_type (eltype, !lvalue_p (init));
7543 TREE_TYPE (v[i]) = eltype;
7544 layout_decl (v[i], 0);
7545 if (DECL_HAS_VALUE_EXPR_P (v[i]))
7547 /* In this case the names are variables, not just proxies. */
7548 SET_DECL_VALUE_EXPR (v[i], NULL_TREE);
7549 DECL_HAS_VALUE_EXPR_P (v[i]) = 0;
7551 cp_finish_decl (v[i], init, /*constexpr*/false,
7552 /*asm*/NULL_TREE, LOOKUP_NORMAL);
7555 else if (TREE_CODE (type) == UNION_TYPE)
7557 error_at (loc, "cannot decompose union type %qT", type);
7558 goto error_out;
7560 else if (!CLASS_TYPE_P (type))
7562 error_at (loc, "cannot decompose non-array non-class type %qT", type);
7563 goto error_out;
7565 else if (LAMBDA_TYPE_P (type))
7567 error_at (loc, "cannot decompose lambda closure type %qT", type);
7568 goto error_out;
7570 else
7572 tree btype = find_decomp_class_base (loc, type, NULL_TREE);
7573 if (btype == error_mark_node)
7574 goto error_out;
7575 else if (btype == NULL_TREE)
7577 error_at (loc, "cannot decompose class type %qT without non-static "
7578 "data members", type);
7579 goto error_out;
7581 for (tree field = TYPE_FIELDS (btype); field; field = TREE_CHAIN (field))
7582 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
7583 continue;
7584 else
7585 eltscnt++;
7586 if (count != eltscnt)
7587 goto cnt_mismatch;
7588 tree t = dexp;
7589 if (type != btype)
7591 t = convert_to_base (t, btype, /*check_access*/true,
7592 /*nonnull*/false, tf_warning_or_error);
7593 type = btype;
7595 unsigned int i = 0;
7596 for (tree field = TYPE_FIELDS (btype); field; field = TREE_CHAIN (field))
7597 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
7598 continue;
7599 else
7601 tree tt = finish_non_static_data_member (field, t, NULL_TREE);
7602 if (REFERENCE_REF_P (tt))
7603 tt = TREE_OPERAND (tt, 0);
7604 TREE_TYPE (v[i]) = TREE_TYPE (tt);
7605 layout_decl (v[i], 0);
7606 SET_DECL_VALUE_EXPR (v[i], tt);
7607 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
7608 i++;
7611 if (DECL_NAMESPACE_SCOPE_P (decl))
7612 SET_DECL_ASSEMBLER_NAME (decl, mangle_decomp (decl, v));
7615 /* Returns a declaration for a VAR_DECL as if:
7617 extern "C" TYPE NAME;
7619 had been seen. Used to create compiler-generated global
7620 variables. */
7622 static tree
7623 declare_global_var (tree name, tree type)
7625 tree decl;
7627 push_to_top_level ();
7628 decl = build_decl (input_location, VAR_DECL, name, type);
7629 TREE_PUBLIC (decl) = 1;
7630 DECL_EXTERNAL (decl) = 1;
7631 DECL_ARTIFICIAL (decl) = 1;
7632 /* If the user has explicitly declared this variable (perhaps
7633 because the code we are compiling is part of a low-level runtime
7634 library), then it is possible that our declaration will be merged
7635 with theirs by pushdecl. */
7636 decl = pushdecl (decl);
7637 cp_finish_decl (decl, NULL_TREE, false, NULL_TREE, 0);
7638 pop_from_top_level ();
7640 return decl;
7643 /* Returns the type for the argument to "__cxa_atexit" (or "atexit",
7644 if "__cxa_atexit" is not being used) corresponding to the function
7645 to be called when the program exits. */
7647 static tree
7648 get_atexit_fn_ptr_type (void)
7650 tree fn_type;
7652 if (!atexit_fn_ptr_type_node)
7654 tree arg_type;
7655 if (flag_use_cxa_atexit
7656 && !targetm.cxx.use_atexit_for_cxa_atexit ())
7657 /* The parameter to "__cxa_atexit" is "void (*)(void *)". */
7658 arg_type = ptr_type_node;
7659 else
7660 /* The parameter to "atexit" is "void (*)(void)". */
7661 arg_type = NULL_TREE;
7663 fn_type = build_function_type_list (void_type_node,
7664 arg_type, NULL_TREE);
7665 atexit_fn_ptr_type_node = build_pointer_type (fn_type);
7668 return atexit_fn_ptr_type_node;
7671 /* Returns a pointer to the `atexit' function. Note that if
7672 FLAG_USE_CXA_ATEXIT is nonzero, then this will actually be the new
7673 `__cxa_atexit' function specified in the IA64 C++ ABI. */
7675 static tree
7676 get_atexit_node (void)
7678 tree atexit_fndecl;
7679 tree fn_type;
7680 tree fn_ptr_type;
7681 const char *name;
7682 bool use_aeabi_atexit;
7684 if (atexit_node)
7685 return atexit_node;
7687 if (flag_use_cxa_atexit && !targetm.cxx.use_atexit_for_cxa_atexit ())
7689 /* The declaration for `__cxa_atexit' is:
7691 int __cxa_atexit (void (*)(void *), void *, void *)
7693 We build up the argument types and then the function type
7694 itself. */
7695 tree argtype0, argtype1, argtype2;
7697 use_aeabi_atexit = targetm.cxx.use_aeabi_atexit ();
7698 /* First, build the pointer-to-function type for the first
7699 argument. */
7700 fn_ptr_type = get_atexit_fn_ptr_type ();
7701 /* Then, build the rest of the argument types. */
7702 argtype2 = ptr_type_node;
7703 if (use_aeabi_atexit)
7705 argtype1 = fn_ptr_type;
7706 argtype0 = ptr_type_node;
7708 else
7710 argtype1 = ptr_type_node;
7711 argtype0 = fn_ptr_type;
7713 /* And the final __cxa_atexit type. */
7714 fn_type = build_function_type_list (integer_type_node,
7715 argtype0, argtype1, argtype2,
7716 NULL_TREE);
7717 if (use_aeabi_atexit)
7718 name = "__aeabi_atexit";
7719 else
7720 name = "__cxa_atexit";
7722 else
7724 /* The declaration for `atexit' is:
7726 int atexit (void (*)());
7728 We build up the argument types and then the function type
7729 itself. */
7730 fn_ptr_type = get_atexit_fn_ptr_type ();
7731 /* Build the final atexit type. */
7732 fn_type = build_function_type_list (integer_type_node,
7733 fn_ptr_type, NULL_TREE);
7734 name = "atexit";
7737 /* Now, build the function declaration. */
7738 push_lang_context (lang_name_c);
7739 atexit_fndecl = build_library_fn_ptr (name, fn_type, ECF_LEAF | ECF_NOTHROW);
7740 mark_used (atexit_fndecl);
7741 pop_lang_context ();
7742 atexit_node = decay_conversion (atexit_fndecl, tf_warning_or_error);
7744 return atexit_node;
7747 /* Like get_atexit_node, but for thread-local cleanups. */
7749 static tree
7750 get_thread_atexit_node (void)
7752 /* The declaration for `__cxa_thread_atexit' is:
7754 int __cxa_thread_atexit (void (*)(void *), void *, void *) */
7755 tree fn_type = build_function_type_list (integer_type_node,
7756 get_atexit_fn_ptr_type (),
7757 ptr_type_node, ptr_type_node,
7758 NULL_TREE);
7760 /* Now, build the function declaration. */
7761 tree atexit_fndecl = build_library_fn_ptr ("__cxa_thread_atexit", fn_type,
7762 ECF_LEAF | ECF_NOTHROW);
7763 return decay_conversion (atexit_fndecl, tf_warning_or_error);
7766 /* Returns the __dso_handle VAR_DECL. */
7768 static tree
7769 get_dso_handle_node (void)
7771 if (dso_handle_node)
7772 return dso_handle_node;
7774 /* Declare the variable. */
7775 dso_handle_node = declare_global_var (get_identifier ("__dso_handle"),
7776 ptr_type_node);
7778 #ifdef HAVE_GAS_HIDDEN
7779 if (dso_handle_node != error_mark_node)
7781 DECL_VISIBILITY (dso_handle_node) = VISIBILITY_HIDDEN;
7782 DECL_VISIBILITY_SPECIFIED (dso_handle_node) = 1;
7784 #endif
7786 return dso_handle_node;
7789 /* Begin a new function with internal linkage whose job will be simply
7790 to destroy some particular variable. */
7792 static GTY(()) int start_cleanup_cnt;
7794 static tree
7795 start_cleanup_fn (void)
7797 char name[32];
7798 tree fntype;
7799 tree fndecl;
7800 bool use_cxa_atexit = flag_use_cxa_atexit
7801 && !targetm.cxx.use_atexit_for_cxa_atexit ();
7803 push_to_top_level ();
7805 /* No need to mangle this. */
7806 push_lang_context (lang_name_c);
7808 /* Build the name of the function. */
7809 sprintf (name, "__tcf_%d", start_cleanup_cnt++);
7810 /* Build the function declaration. */
7811 fntype = TREE_TYPE (get_atexit_fn_ptr_type ());
7812 fndecl = build_lang_decl (FUNCTION_DECL, get_identifier (name), fntype);
7813 /* It's a function with internal linkage, generated by the
7814 compiler. */
7815 TREE_PUBLIC (fndecl) = 0;
7816 DECL_ARTIFICIAL (fndecl) = 1;
7817 /* Make the function `inline' so that it is only emitted if it is
7818 actually needed. It is unlikely that it will be inlined, since
7819 it is only called via a function pointer, but we avoid unnecessary
7820 emissions this way. */
7821 DECL_DECLARED_INLINE_P (fndecl) = 1;
7822 DECL_INTERFACE_KNOWN (fndecl) = 1;
7823 /* Build the parameter. */
7824 if (use_cxa_atexit)
7826 tree parmdecl;
7828 parmdecl = cp_build_parm_decl (NULL_TREE, ptr_type_node);
7829 DECL_CONTEXT (parmdecl) = fndecl;
7830 TREE_USED (parmdecl) = 1;
7831 DECL_READ_P (parmdecl) = 1;
7832 DECL_ARGUMENTS (fndecl) = parmdecl;
7835 pushdecl (fndecl);
7836 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
7838 pop_lang_context ();
7840 return current_function_decl;
7843 /* Finish the cleanup function begun by start_cleanup_fn. */
7845 static void
7846 end_cleanup_fn (void)
7848 expand_or_defer_fn (finish_function (0));
7850 pop_from_top_level ();
7853 /* Generate code to handle the destruction of DECL, an object with
7854 static storage duration. */
7856 tree
7857 register_dtor_fn (tree decl)
7859 tree cleanup;
7860 tree addr;
7861 tree compound_stmt;
7862 tree fcall;
7863 tree type;
7864 bool ob_parm, dso_parm, use_dtor;
7865 tree arg0, arg1, arg2;
7866 tree atex_node;
7868 type = TREE_TYPE (decl);
7869 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
7870 return void_node;
7872 /* If we're using "__cxa_atexit" (or "__cxa_thread_atexit" or
7873 "__aeabi_atexit"), and DECL is a class object, we can just pass the
7874 destructor to "__cxa_atexit"; we don't have to build a temporary
7875 function to do the cleanup. */
7876 dso_parm = (flag_use_cxa_atexit
7877 && !targetm.cxx.use_atexit_for_cxa_atexit ());
7878 ob_parm = (CP_DECL_THREAD_LOCAL_P (decl) || dso_parm);
7879 use_dtor = ob_parm && CLASS_TYPE_P (type);
7880 if (use_dtor)
7882 int idx;
7884 /* Find the destructor. */
7885 idx = lookup_fnfields_1 (type, complete_dtor_identifier);
7886 gcc_assert (idx >= 0);
7887 cleanup = (*CLASSTYPE_METHOD_VEC (type))[idx];
7888 /* Make sure it is accessible. */
7889 perform_or_defer_access_check (TYPE_BINFO (type), cleanup, cleanup,
7890 tf_warning_or_error);
7892 else
7894 /* Call build_cleanup before we enter the anonymous function so
7895 that any access checks will be done relative to the current
7896 scope, rather than the scope of the anonymous function. */
7897 build_cleanup (decl);
7899 /* Now start the function. */
7900 cleanup = start_cleanup_fn ();
7902 /* Now, recompute the cleanup. It may contain SAVE_EXPRs that refer
7903 to the original function, rather than the anonymous one. That
7904 will make the back end think that nested functions are in use,
7905 which causes confusion. */
7906 push_deferring_access_checks (dk_no_check);
7907 fcall = build_cleanup (decl);
7908 pop_deferring_access_checks ();
7910 /* Create the body of the anonymous function. */
7911 compound_stmt = begin_compound_stmt (BCS_FN_BODY);
7912 finish_expr_stmt (fcall);
7913 finish_compound_stmt (compound_stmt);
7914 end_cleanup_fn ();
7917 /* Call atexit with the cleanup function. */
7918 mark_used (cleanup);
7919 cleanup = build_address (cleanup);
7921 if (CP_DECL_THREAD_LOCAL_P (decl))
7922 atex_node = get_thread_atexit_node ();
7923 else
7924 atex_node = get_atexit_node ();
7926 if (use_dtor)
7928 /* We must convert CLEANUP to the type that "__cxa_atexit"
7929 expects. */
7930 cleanup = build_nop (get_atexit_fn_ptr_type (), cleanup);
7931 /* "__cxa_atexit" will pass the address of DECL to the
7932 cleanup function. */
7933 mark_used (decl);
7934 addr = build_address (decl);
7935 /* The declared type of the parameter to "__cxa_atexit" is
7936 "void *". For plain "T*", we could just let the
7937 machinery in cp_build_function_call convert it -- but if the
7938 type is "cv-qualified T *", then we need to convert it
7939 before passing it in, to avoid spurious errors. */
7940 addr = build_nop (ptr_type_node, addr);
7942 else
7943 /* Since the cleanup functions we build ignore the address
7944 they're given, there's no reason to pass the actual address
7945 in, and, in general, it's cheaper to pass NULL than any
7946 other value. */
7947 addr = null_pointer_node;
7949 if (dso_parm)
7950 arg2 = cp_build_addr_expr (get_dso_handle_node (),
7951 tf_warning_or_error);
7952 else if (ob_parm)
7953 /* Just pass NULL to the dso handle parm if we don't actually
7954 have a DSO handle on this target. */
7955 arg2 = null_pointer_node;
7956 else
7957 arg2 = NULL_TREE;
7959 if (ob_parm)
7961 if (!CP_DECL_THREAD_LOCAL_P (decl)
7962 && targetm.cxx.use_aeabi_atexit ())
7964 arg1 = cleanup;
7965 arg0 = addr;
7967 else
7969 arg1 = addr;
7970 arg0 = cleanup;
7973 else
7975 arg0 = cleanup;
7976 arg1 = NULL_TREE;
7978 return cp_build_function_call_nary (atex_node, tf_warning_or_error,
7979 arg0, arg1, arg2, NULL_TREE);
7982 /* DECL is a VAR_DECL with static storage duration. INIT, if present,
7983 is its initializer. Generate code to handle the construction
7984 and destruction of DECL. */
7986 static void
7987 expand_static_init (tree decl, tree init)
7989 gcc_assert (VAR_P (decl));
7990 gcc_assert (TREE_STATIC (decl));
7992 /* Some variables require no dynamic initialization. */
7993 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
7995 /* Make sure the destructor is callable. */
7996 cxx_maybe_build_cleanup (decl, tf_warning_or_error);
7997 if (!init)
7998 return;
8001 if (CP_DECL_THREAD_LOCAL_P (decl) && DECL_GNU_TLS_P (decl)
8002 && !DECL_FUNCTION_SCOPE_P (decl))
8004 if (init)
8005 error ("non-local variable %qD declared %<__thread%> "
8006 "needs dynamic initialization", decl);
8007 else
8008 error ("non-local variable %qD declared %<__thread%> "
8009 "has a non-trivial destructor", decl);
8010 static bool informed;
8011 if (!informed)
8013 inform (DECL_SOURCE_LOCATION (decl),
8014 "C++11 %<thread_local%> allows dynamic initialization "
8015 "and destruction");
8016 informed = true;
8018 return;
8021 if (DECL_FUNCTION_SCOPE_P (decl))
8023 /* Emit code to perform this initialization but once. */
8024 tree if_stmt = NULL_TREE, inner_if_stmt = NULL_TREE;
8025 tree then_clause = NULL_TREE, inner_then_clause = NULL_TREE;
8026 tree guard, guard_addr;
8027 tree flag, begin;
8028 /* We don't need thread-safety code for thread-local vars. */
8029 bool thread_guard = (flag_threadsafe_statics
8030 && !CP_DECL_THREAD_LOCAL_P (decl));
8032 /* Emit code to perform this initialization but once. This code
8033 looks like:
8035 static <type> guard;
8036 if (!__atomic_load (guard.first_byte)) {
8037 if (__cxa_guard_acquire (&guard)) {
8038 bool flag = false;
8039 try {
8040 // Do initialization.
8041 flag = true; __cxa_guard_release (&guard);
8042 // Register variable for destruction at end of program.
8043 } catch {
8044 if (!flag) __cxa_guard_abort (&guard);
8048 Note that the `flag' variable is only set to 1 *after* the
8049 initialization is complete. This ensures that an exception,
8050 thrown during the construction, will cause the variable to
8051 reinitialized when we pass through this code again, as per:
8053 [stmt.dcl]
8055 If the initialization exits by throwing an exception, the
8056 initialization is not complete, so it will be tried again
8057 the next time control enters the declaration.
8059 This process should be thread-safe, too; multiple threads
8060 should not be able to initialize the variable more than
8061 once. */
8063 /* Create the guard variable. */
8064 guard = get_guard (decl);
8066 /* Begin the conditional initialization. */
8067 if_stmt = begin_if_stmt ();
8069 finish_if_stmt_cond (get_guard_cond (guard, thread_guard), if_stmt);
8070 then_clause = begin_compound_stmt (BCS_NO_SCOPE);
8072 if (thread_guard)
8074 tree vfntype = NULL_TREE;
8075 tree acquire_name, release_name, abort_name;
8076 tree acquire_fn, release_fn, abort_fn;
8077 guard_addr = build_address (guard);
8079 acquire_name = get_identifier ("__cxa_guard_acquire");
8080 release_name = get_identifier ("__cxa_guard_release");
8081 abort_name = get_identifier ("__cxa_guard_abort");
8082 acquire_fn = identifier_global_value (acquire_name);
8083 release_fn = identifier_global_value (release_name);
8084 abort_fn = identifier_global_value (abort_name);
8085 if (!acquire_fn)
8086 acquire_fn = push_library_fn
8087 (acquire_name, build_function_type_list (integer_type_node,
8088 TREE_TYPE (guard_addr),
8089 NULL_TREE),
8090 NULL_TREE, ECF_NOTHROW | ECF_LEAF);
8091 if (!release_fn || !abort_fn)
8092 vfntype = build_function_type_list (void_type_node,
8093 TREE_TYPE (guard_addr),
8094 NULL_TREE);
8095 if (!release_fn)
8096 release_fn = push_library_fn (release_name, vfntype, NULL_TREE,
8097 ECF_NOTHROW | ECF_LEAF);
8098 if (!abort_fn)
8099 abort_fn = push_library_fn (abort_name, vfntype, NULL_TREE,
8100 ECF_NOTHROW | ECF_LEAF);
8102 inner_if_stmt = begin_if_stmt ();
8103 finish_if_stmt_cond (build_call_n (acquire_fn, 1, guard_addr),
8104 inner_if_stmt);
8106 inner_then_clause = begin_compound_stmt (BCS_NO_SCOPE);
8107 begin = get_target_expr (boolean_false_node);
8108 flag = TARGET_EXPR_SLOT (begin);
8110 TARGET_EXPR_CLEANUP (begin)
8111 = build3 (COND_EXPR, void_type_node, flag,
8112 void_node,
8113 build_call_n (abort_fn, 1, guard_addr));
8114 CLEANUP_EH_ONLY (begin) = 1;
8116 /* Do the initialization itself. */
8117 init = add_stmt_to_compound (begin, init);
8118 init = add_stmt_to_compound
8119 (init, build2 (MODIFY_EXPR, void_type_node, flag, boolean_true_node));
8120 init = add_stmt_to_compound
8121 (init, build_call_n (release_fn, 1, guard_addr));
8123 else
8124 init = add_stmt_to_compound (init, set_guard (guard));
8126 /* Use atexit to register a function for destroying this static
8127 variable. */
8128 init = add_stmt_to_compound (init, register_dtor_fn (decl));
8130 finish_expr_stmt (init);
8132 if (thread_guard)
8134 finish_compound_stmt (inner_then_clause);
8135 finish_then_clause (inner_if_stmt);
8136 finish_if_stmt (inner_if_stmt);
8139 finish_compound_stmt (then_clause);
8140 finish_then_clause (if_stmt);
8141 finish_if_stmt (if_stmt);
8143 else if (CP_DECL_THREAD_LOCAL_P (decl))
8144 tls_aggregates = tree_cons (init, decl, tls_aggregates);
8145 else
8146 static_aggregates = tree_cons (init, decl, static_aggregates);
8150 /* Make TYPE a complete type based on INITIAL_VALUE.
8151 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
8152 2 if there was no information (in which case assume 0 if DO_DEFAULT),
8153 3 if the initializer list is empty (in pedantic mode). */
8156 cp_complete_array_type (tree *ptype, tree initial_value, bool do_default)
8158 int failure;
8159 tree type, elt_type;
8161 /* Don't get confused by a CONSTRUCTOR for some other type. */
8162 if (initial_value && TREE_CODE (initial_value) == CONSTRUCTOR
8163 && !BRACE_ENCLOSED_INITIALIZER_P (initial_value)
8164 && TREE_CODE (TREE_TYPE (initial_value)) != ARRAY_TYPE)
8165 return 1;
8167 if (initial_value)
8169 unsigned HOST_WIDE_INT i;
8170 tree value;
8172 /* An array of character type can be initialized from a
8173 brace-enclosed string constant.
8175 FIXME: this code is duplicated from reshape_init. Probably
8176 we should just call reshape_init here? */
8177 if (char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (*ptype)))
8178 && TREE_CODE (initial_value) == CONSTRUCTOR
8179 && !vec_safe_is_empty (CONSTRUCTOR_ELTS (initial_value)))
8181 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value);
8182 tree value = (*v)[0].value;
8184 if (TREE_CODE (value) == STRING_CST
8185 && v->length () == 1)
8186 initial_value = value;
8189 /* If any of the elements are parameter packs, we can't actually
8190 complete this type now because the array size is dependent. */
8191 if (TREE_CODE (initial_value) == CONSTRUCTOR)
8193 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (initial_value),
8194 i, value)
8196 if (PACK_EXPANSION_P (value))
8197 return 0;
8202 failure = complete_array_type (ptype, initial_value, do_default);
8204 /* We can create the array before the element type is complete, which
8205 means that we didn't have these two bits set in the original type
8206 either. In completing the type, we are expected to propagate these
8207 bits. See also complete_type which does the same thing for arrays
8208 of fixed size. */
8209 type = *ptype;
8210 if (TYPE_DOMAIN (type))
8212 elt_type = TREE_TYPE (type);
8213 TYPE_NEEDS_CONSTRUCTING (type) = TYPE_NEEDS_CONSTRUCTING (elt_type);
8214 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
8215 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (elt_type);
8218 return failure;
8221 /* As above, but either give an error or reject zero-size arrays, depending
8222 on COMPLAIN. */
8225 cp_complete_array_type_or_error (tree *ptype, tree initial_value,
8226 bool do_default, tsubst_flags_t complain)
8228 int failure;
8229 bool sfinae = !(complain & tf_error);
8230 /* In SFINAE context we can't be lenient about zero-size arrays. */
8231 if (sfinae)
8232 ++pedantic;
8233 failure = cp_complete_array_type (ptype, initial_value, do_default);
8234 if (sfinae)
8235 --pedantic;
8236 if (failure)
8238 if (sfinae)
8239 /* Not an error. */;
8240 else if (failure == 1)
8241 error ("initializer fails to determine size of %qT", *ptype);
8242 else if (failure == 2)
8244 if (do_default)
8245 error ("array size missing in %qT", *ptype);
8247 else if (failure == 3)
8248 error ("zero-size array %qT", *ptype);
8249 *ptype = error_mark_node;
8251 return failure;
8254 /* Return zero if something is declared to be a member of type
8255 CTYPE when in the context of CUR_TYPE. STRING is the error
8256 message to print in that case. Otherwise, quietly return 1. */
8258 static int
8259 member_function_or_else (tree ctype, tree cur_type, enum overload_flags flags)
8261 if (ctype && ctype != cur_type)
8263 if (flags == DTOR_FLAG)
8264 error ("destructor for alien class %qT cannot be a member", ctype);
8265 else
8266 error ("constructor for alien class %qT cannot be a member", ctype);
8267 return 0;
8269 return 1;
8272 /* Subroutine of `grokdeclarator'. */
8274 /* Generate errors possibly applicable for a given set of specifiers.
8275 This is for ARM $7.1.2. */
8277 static void
8278 bad_specifiers (tree object,
8279 enum bad_spec_place type,
8280 int virtualp,
8281 int quals,
8282 int inlinep,
8283 int friendp,
8284 int raises)
8286 switch (type)
8288 case BSP_VAR:
8289 if (virtualp)
8290 error ("%qD declared as a %<virtual%> variable", object);
8291 if (quals)
8292 error ("%<const%> and %<volatile%> function specifiers on "
8293 "%qD invalid in variable declaration", object);
8294 break;
8295 case BSP_PARM:
8296 if (virtualp)
8297 error ("%qD declared as a %<virtual%> parameter", object);
8298 if (inlinep)
8299 error ("%qD declared as an %<inline%> parameter", object);
8300 if (quals)
8301 error ("%<const%> and %<volatile%> function specifiers on "
8302 "%qD invalid in parameter declaration", object);
8303 break;
8304 case BSP_TYPE:
8305 if (virtualp)
8306 error ("%qD declared as a %<virtual%> type", object);
8307 if (inlinep)
8308 error ("%qD declared as an %<inline%> type", object);
8309 if (quals)
8310 error ("%<const%> and %<volatile%> function specifiers on "
8311 "%qD invalid in type declaration", object);
8312 break;
8313 case BSP_FIELD:
8314 if (virtualp)
8315 error ("%qD declared as a %<virtual%> field", object);
8316 if (inlinep)
8317 error ("%qD declared as an %<inline%> field", object);
8318 if (quals)
8319 error ("%<const%> and %<volatile%> function specifiers on "
8320 "%qD invalid in field declaration", object);
8321 break;
8322 default:
8323 gcc_unreachable();
8325 if (friendp)
8326 error ("%q+D declared as a friend", object);
8327 if (raises
8328 && !flag_noexcept_type
8329 && (TREE_CODE (object) == TYPE_DECL
8330 || (!TYPE_PTRFN_P (TREE_TYPE (object))
8331 && !TYPE_REFFN_P (TREE_TYPE (object))
8332 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (object)))))
8333 error ("%q+D declared with an exception specification", object);
8336 /* DECL is a member function or static data member and is presently
8337 being defined. Check that the definition is taking place in a
8338 valid namespace. */
8340 static void
8341 check_class_member_definition_namespace (tree decl)
8343 /* These checks only apply to member functions and static data
8344 members. */
8345 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
8346 /* We check for problems with specializations in pt.c in
8347 check_specialization_namespace, where we can issue better
8348 diagnostics. */
8349 if (processing_specialization)
8350 return;
8351 /* We check this in check_explicit_instantiation_namespace. */
8352 if (processing_explicit_instantiation)
8353 return;
8354 /* [class.mfct]
8356 A member function definition that appears outside of the
8357 class definition shall appear in a namespace scope enclosing
8358 the class definition.
8360 [class.static.data]
8362 The definition for a static data member shall appear in a
8363 namespace scope enclosing the member's class definition. */
8364 if (!is_ancestor (current_namespace, DECL_CONTEXT (decl)))
8365 permerror (input_location, "definition of %qD is not in namespace enclosing %qT",
8366 decl, DECL_CONTEXT (decl));
8369 /* Build a PARM_DECL for the "this" parameter. TYPE is the
8370 METHOD_TYPE for a non-static member function; QUALS are the
8371 cv-qualifiers that apply to the function. */
8373 tree
8374 build_this_parm (tree type, cp_cv_quals quals)
8376 tree this_type;
8377 tree qual_type;
8378 tree parm;
8379 cp_cv_quals this_quals;
8381 if (CLASS_TYPE_P (type))
8383 this_type
8384 = cp_build_qualified_type (type, quals & ~TYPE_QUAL_RESTRICT);
8385 this_type = build_pointer_type (this_type);
8387 else
8388 this_type = type_of_this_parm (type);
8389 /* The `this' parameter is implicitly `const'; it cannot be
8390 assigned to. */
8391 this_quals = (quals & TYPE_QUAL_RESTRICT) | TYPE_QUAL_CONST;
8392 qual_type = cp_build_qualified_type (this_type, this_quals);
8393 parm = build_artificial_parm (this_identifier, qual_type);
8394 cp_apply_type_quals_to_decl (this_quals, parm);
8395 return parm;
8398 /* DECL is a static member function. Complain if it was declared
8399 with function-cv-quals. */
8401 static void
8402 check_static_quals (tree decl, cp_cv_quals quals)
8404 if (quals != TYPE_UNQUALIFIED)
8405 error ("static member function %q#D declared with type qualifiers",
8406 decl);
8409 // Check that FN takes no arguments and returns bool.
8410 static void
8411 check_concept_fn (tree fn)
8413 // A constraint is nullary.
8414 if (DECL_ARGUMENTS (fn))
8415 error ("concept %q#D declared with function parameters", fn);
8417 // The declared return type of the concept shall be bool, and
8418 // it shall not be deduced from it definition.
8419 tree type = TREE_TYPE (TREE_TYPE (fn));
8420 if (is_auto (type))
8421 error ("concept %q#D declared with a deduced return type", fn);
8422 else if (type != boolean_type_node)
8423 error ("concept %q#D with non-%<bool%> return type %qT", fn, type);
8426 /* Helper function. Replace the temporary this parameter injected
8427 during cp_finish_omp_declare_simd with the real this parameter. */
8429 static tree
8430 declare_simd_adjust_this (tree *tp, int *walk_subtrees, void *data)
8432 tree this_parm = (tree) data;
8433 if (TREE_CODE (*tp) == PARM_DECL
8434 && DECL_NAME (*tp) == this_identifier
8435 && *tp != this_parm)
8436 *tp = this_parm;
8437 else if (TYPE_P (*tp))
8438 *walk_subtrees = 0;
8439 return NULL_TREE;
8442 /* CTYPE is class type, or null if non-class.
8443 TYPE is type this FUNCTION_DECL should have, either FUNCTION_TYPE
8444 or METHOD_TYPE.
8445 DECLARATOR is the function's name.
8446 PARMS is a chain of PARM_DECLs for the function.
8447 VIRTUALP is truthvalue of whether the function is virtual or not.
8448 FLAGS are to be passed through to `grokclassfn'.
8449 QUALS are qualifiers indicating whether the function is `const'
8450 or `volatile'.
8451 RAISES is a list of exceptions that this function can raise.
8452 CHECK is 1 if we must find this method in CTYPE, 0 if we should
8453 not look, and -1 if we should not call `grokclassfn' at all.
8455 SFK is the kind of special function (if any) for the new function.
8457 Returns `NULL_TREE' if something goes wrong, after issuing
8458 applicable error messages. */
8460 static tree
8461 grokfndecl (tree ctype,
8462 tree type,
8463 tree declarator,
8464 tree parms,
8465 tree orig_declarator,
8466 tree decl_reqs,
8467 int virtualp,
8468 enum overload_flags flags,
8469 cp_cv_quals quals,
8470 cp_ref_qualifier rqual,
8471 tree raises,
8472 int check,
8473 int friendp,
8474 int publicp,
8475 int inlinep,
8476 bool deletedp,
8477 special_function_kind sfk,
8478 bool funcdef_flag,
8479 int template_count,
8480 tree in_namespace,
8481 tree* attrlist,
8482 location_t location)
8484 tree decl;
8485 int staticp = ctype && TREE_CODE (type) == FUNCTION_TYPE;
8486 tree t;
8488 // Was the concept specifier present?
8489 bool concept_p = inlinep & 4;
8491 // Concept declarations must have a corresponding definition.
8492 if (concept_p && !funcdef_flag)
8494 error ("concept %qD has no definition", declarator);
8495 return NULL_TREE;
8498 if (rqual)
8499 type = build_ref_qualified_type (type, rqual);
8500 if (raises)
8501 type = build_exception_variant (type, raises);
8503 decl = build_lang_decl (FUNCTION_DECL, declarator, type);
8505 /* Set the constraints on the declaration. */
8506 if (flag_concepts)
8508 tree tmpl_reqs = NULL_TREE;
8509 if (processing_template_decl > template_class_depth (ctype))
8510 tmpl_reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
8512 /* Adjust the required expression into a constraint. */
8513 if (decl_reqs)
8514 decl_reqs = normalize_expression (decl_reqs);
8516 tree ci = build_constraints (tmpl_reqs, decl_reqs);
8517 set_constraints (decl, ci);
8520 /* If we have an explicit location, use it, otherwise use whatever
8521 build_lang_decl used (probably input_location). */
8522 if (location != UNKNOWN_LOCATION)
8523 DECL_SOURCE_LOCATION (decl) = location;
8525 if (TREE_CODE (type) == METHOD_TYPE)
8527 tree parm;
8528 parm = build_this_parm (type, quals);
8529 DECL_CHAIN (parm) = parms;
8530 parms = parm;
8532 /* Allocate space to hold the vptr bit if needed. */
8533 SET_DECL_ALIGN (decl, MINIMUM_METHOD_BOUNDARY);
8535 DECL_ARGUMENTS (decl) = parms;
8536 for (t = parms; t; t = DECL_CHAIN (t))
8537 DECL_CONTEXT (t) = decl;
8538 /* Propagate volatile out from type to decl. */
8539 if (TYPE_VOLATILE (type))
8540 TREE_THIS_VOLATILE (decl) = 1;
8542 /* Setup decl according to sfk. */
8543 switch (sfk)
8545 case sfk_constructor:
8546 case sfk_copy_constructor:
8547 case sfk_move_constructor:
8548 DECL_CONSTRUCTOR_P (decl) = 1;
8549 break;
8550 case sfk_destructor:
8551 DECL_DESTRUCTOR_P (decl) = 1;
8552 break;
8553 default:
8554 break;
8557 if (friendp
8558 && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)
8560 if (funcdef_flag)
8561 error
8562 ("defining explicit specialization %qD in friend declaration",
8563 orig_declarator);
8564 else
8566 tree fns = TREE_OPERAND (orig_declarator, 0);
8567 tree args = TREE_OPERAND (orig_declarator, 1);
8569 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8571 /* Something like `template <class T> friend void f<T>()'. */
8572 error ("invalid use of template-id %qD in declaration "
8573 "of primary template",
8574 orig_declarator);
8575 return NULL_TREE;
8579 /* A friend declaration of the form friend void f<>(). Record
8580 the information in the TEMPLATE_ID_EXPR. */
8581 SET_DECL_IMPLICIT_INSTANTIATION (decl);
8583 gcc_assert (identifier_p (fns) || TREE_CODE (fns) == OVERLOAD);
8584 DECL_TEMPLATE_INFO (decl) = build_template_info (fns, args);
8586 for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t))
8587 if (TREE_PURPOSE (t)
8588 && TREE_CODE (TREE_PURPOSE (t)) == DEFAULT_ARG)
8590 error ("default arguments are not allowed in declaration "
8591 "of friend template specialization %qD",
8592 decl);
8593 return NULL_TREE;
8596 if (inlinep & 1)
8598 error ("%<inline%> is not allowed in declaration of friend "
8599 "template specialization %qD",
8600 decl);
8601 return NULL_TREE;
8606 /* If this decl has namespace scope, set that up. */
8607 if (in_namespace)
8608 set_decl_namespace (decl, in_namespace, friendp);
8609 else if (!ctype)
8610 DECL_CONTEXT (decl) = FROB_CONTEXT (current_decl_namespace ());
8612 /* `main' and builtins have implicit 'C' linkage. */
8613 if ((MAIN_NAME_P (declarator)
8614 || (IDENTIFIER_LENGTH (declarator) > 10
8615 && IDENTIFIER_POINTER (declarator)[0] == '_'
8616 && IDENTIFIER_POINTER (declarator)[1] == '_'
8617 && strncmp (IDENTIFIER_POINTER (declarator)+2, "builtin_", 8) == 0)
8618 || (targetcm.cxx_implicit_extern_c
8619 && targetcm.cxx_implicit_extern_c(IDENTIFIER_POINTER (declarator))))
8620 && current_lang_name == lang_name_cplusplus
8621 && ctype == NULL_TREE
8622 && DECL_FILE_SCOPE_P (decl))
8623 SET_DECL_LANGUAGE (decl, lang_c);
8625 /* Should probably propagate const out from type to decl I bet (mrs). */
8626 if (staticp)
8628 DECL_STATIC_FUNCTION_P (decl) = 1;
8629 DECL_CONTEXT (decl) = ctype;
8632 if (deletedp)
8633 DECL_DELETED_FN (decl) = 1;
8635 if (ctype)
8637 DECL_CONTEXT (decl) = ctype;
8638 if (funcdef_flag)
8639 check_class_member_definition_namespace (decl);
8642 if (ctype == NULL_TREE && DECL_MAIN_P (decl))
8644 if (PROCESSING_REAL_TEMPLATE_DECL_P())
8645 error ("cannot declare %<::main%> to be a template");
8646 if (inlinep & 1)
8647 error ("cannot declare %<::main%> to be inline");
8648 if (inlinep & 2)
8649 error ("cannot declare %<::main%> to be constexpr");
8650 if (!publicp)
8651 error ("cannot declare %<::main%> to be static");
8652 inlinep = 0;
8653 publicp = 1;
8656 /* Members of anonymous types and local classes have no linkage; make
8657 them internal. If a typedef is made later, this will be changed. */
8658 if (ctype && (!TREE_PUBLIC (TYPE_MAIN_DECL (ctype))
8659 || decl_function_context (TYPE_MAIN_DECL (ctype))))
8660 publicp = 0;
8662 if (publicp && cxx_dialect == cxx98)
8664 /* [basic.link]: A name with no linkage (notably, the name of a class
8665 or enumeration declared in a local scope) shall not be used to
8666 declare an entity with linkage.
8668 DR 757 relaxes this restriction for C++0x. */
8669 no_linkage_error (decl);
8672 TREE_PUBLIC (decl) = publicp;
8673 if (! publicp)
8675 DECL_INTERFACE_KNOWN (decl) = 1;
8676 DECL_NOT_REALLY_EXTERN (decl) = 1;
8679 /* If the declaration was declared inline, mark it as such. */
8680 if (inlinep)
8682 DECL_DECLARED_INLINE_P (decl) = 1;
8683 if (publicp)
8684 DECL_COMDAT (decl) = 1;
8686 if (inlinep & 2)
8687 DECL_DECLARED_CONSTEXPR_P (decl) = true;
8689 // If the concept declaration specifier was found, check
8690 // that the declaration satisfies the necessary requirements.
8691 if (concept_p)
8693 DECL_DECLARED_CONCEPT_P (decl) = true;
8694 check_concept_fn (decl);
8697 DECL_EXTERNAL (decl) = 1;
8698 if (TREE_CODE (type) == FUNCTION_TYPE)
8700 if (quals || rqual)
8701 TREE_TYPE (decl) = apply_memfn_quals (TREE_TYPE (decl),
8702 TYPE_UNQUALIFIED,
8703 REF_QUAL_NONE);
8705 if (quals)
8707 error (ctype
8708 ? G_("static member function %qD cannot have cv-qualifier")
8709 : G_("non-member function %qD cannot have cv-qualifier"),
8710 decl);
8711 quals = TYPE_UNQUALIFIED;
8714 if (rqual)
8716 error (ctype
8717 ? G_("static member function %qD cannot have ref-qualifier")
8718 : G_("non-member function %qD cannot have ref-qualifier"),
8719 decl);
8720 rqual = REF_QUAL_NONE;
8724 if (deduction_guide_p (decl))
8726 if (!DECL_NAMESPACE_SCOPE_P (decl))
8728 error_at (location, "deduction guide %qD must be declared at "
8729 "namespace scope", decl);
8730 return NULL_TREE;
8732 tree type = TREE_TYPE (DECL_NAME (decl));
8733 if (CP_DECL_CONTEXT (decl) != CP_TYPE_CONTEXT (type))
8735 error_at (location, "deduction guide %qD must be declared in the "
8736 "same scope as %qT", decl, type);
8737 inform (location_of (type), " declared here");
8738 return NULL_TREE;
8740 if (funcdef_flag)
8741 error_at (location,
8742 "deduction guide %qD must not have a function body", decl);
8744 else if (IDENTIFIER_OPNAME_P (DECL_NAME (decl))
8745 && !grok_op_properties (decl, /*complain=*/true))
8746 return NULL_TREE;
8747 else if (UDLIT_OPER_P (DECL_NAME (decl)))
8749 bool long_long_unsigned_p;
8750 bool long_double_p;
8751 const char *suffix = NULL;
8752 /* [over.literal]/6: Literal operators shall not have C linkage. */
8753 if (DECL_LANGUAGE (decl) == lang_c)
8755 error ("literal operator with C linkage");
8756 return NULL_TREE;
8759 if (DECL_NAMESPACE_SCOPE_P (decl))
8761 if (!check_literal_operator_args (decl, &long_long_unsigned_p,
8762 &long_double_p))
8764 error ("%qD has invalid argument list", decl);
8765 return NULL_TREE;
8768 suffix = UDLIT_OP_SUFFIX (DECL_NAME (decl));
8769 if (long_long_unsigned_p)
8771 if (cpp_interpret_int_suffix (parse_in, suffix, strlen (suffix)))
8772 warning (0, "integer suffix %<%s%>"
8773 " shadowed by implementation", suffix);
8775 else if (long_double_p)
8777 if (cpp_interpret_float_suffix (parse_in, suffix, strlen (suffix)))
8778 warning (0, "floating point suffix %<%s%>"
8779 " shadowed by implementation", suffix);
8782 else
8784 error ("%qD must be a non-member function", decl);
8785 return NULL_TREE;
8789 if (funcdef_flag)
8790 /* Make the init_value nonzero so pushdecl knows this is not
8791 tentative. error_mark_node is replaced later with the BLOCK. */
8792 DECL_INITIAL (decl) = error_mark_node;
8794 if (TYPE_NOTHROW_P (type) || nothrow_libfn_p (decl))
8795 TREE_NOTHROW (decl) = 1;
8797 if (flag_openmp || flag_openmp_simd || flag_cilkplus)
8799 /* Adjust "omp declare simd" attributes. */
8800 tree ods = lookup_attribute ("omp declare simd", *attrlist);
8801 if (ods)
8803 tree attr;
8804 for (attr = ods; attr;
8805 attr = lookup_attribute ("omp declare simd", TREE_CHAIN (attr)))
8807 if (TREE_CODE (type) == METHOD_TYPE)
8808 walk_tree (&TREE_VALUE (attr), declare_simd_adjust_this,
8809 DECL_ARGUMENTS (decl), NULL);
8810 if (TREE_VALUE (attr) != NULL_TREE)
8812 tree cl = TREE_VALUE (TREE_VALUE (attr));
8813 cl = c_omp_declare_simd_clauses_to_numbers
8814 (DECL_ARGUMENTS (decl), cl);
8815 if (cl)
8816 TREE_VALUE (TREE_VALUE (attr)) = cl;
8817 else
8818 TREE_VALUE (attr) = NULL_TREE;
8824 /* Caller will do the rest of this. */
8825 if (check < 0)
8826 return decl;
8828 if (ctype != NULL_TREE)
8829 grokclassfn (ctype, decl, flags);
8831 /* 12.4/3 */
8832 if (cxx_dialect >= cxx11
8833 && DECL_DESTRUCTOR_P (decl)
8834 && !TYPE_BEING_DEFINED (DECL_CONTEXT (decl))
8835 && !processing_template_decl)
8836 deduce_noexcept_on_destructor (decl);
8838 decl = check_explicit_specialization (orig_declarator, decl,
8839 template_count,
8840 2 * funcdef_flag +
8841 4 * (friendp != 0) +
8842 8 * concept_p);
8843 if (decl == error_mark_node)
8844 return NULL_TREE;
8846 if (DECL_STATIC_FUNCTION_P (decl))
8847 check_static_quals (decl, quals);
8849 if (attrlist)
8851 cplus_decl_attributes (&decl, *attrlist, 0);
8852 *attrlist = NULL_TREE;
8855 /* Check main's type after attributes have been applied. */
8856 if (ctype == NULL_TREE && DECL_MAIN_P (decl))
8858 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
8859 integer_type_node))
8861 tree oldtypeargs = TYPE_ARG_TYPES (TREE_TYPE (decl));
8862 tree newtype;
8863 error ("%<::main%> must return %<int%>");
8864 newtype = build_function_type (integer_type_node, oldtypeargs);
8865 TREE_TYPE (decl) = newtype;
8867 if (warn_main)
8868 check_main_parameter_types (decl);
8871 if (ctype != NULL_TREE && check)
8873 tree old_decl = check_classfn (ctype, decl,
8874 (processing_template_decl
8875 > template_class_depth (ctype))
8876 ? current_template_parms
8877 : NULL_TREE);
8879 if (old_decl == error_mark_node)
8880 return NULL_TREE;
8882 if (old_decl)
8884 tree ok;
8885 tree pushed_scope;
8887 if (TREE_CODE (old_decl) == TEMPLATE_DECL)
8888 /* Because grokfndecl is always supposed to return a
8889 FUNCTION_DECL, we pull out the DECL_TEMPLATE_RESULT
8890 here. We depend on our callers to figure out that its
8891 really a template that's being returned. */
8892 old_decl = DECL_TEMPLATE_RESULT (old_decl);
8894 if (DECL_STATIC_FUNCTION_P (old_decl)
8895 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
8897 /* Remove the `this' parm added by grokclassfn. */
8898 revert_static_member_fn (decl);
8899 check_static_quals (decl, quals);
8901 if (DECL_ARTIFICIAL (old_decl))
8903 error ("definition of implicitly-declared %qD", old_decl);
8904 return NULL_TREE;
8906 else if (DECL_DEFAULTED_FN (old_decl))
8908 error ("definition of explicitly-defaulted %q+D", decl);
8909 inform (DECL_SOURCE_LOCATION (old_decl),
8910 "%q#D explicitly defaulted here", old_decl);
8911 return NULL_TREE;
8914 /* Since we've smashed OLD_DECL to its
8915 DECL_TEMPLATE_RESULT, we must do the same to DECL. */
8916 if (TREE_CODE (decl) == TEMPLATE_DECL)
8917 decl = DECL_TEMPLATE_RESULT (decl);
8919 /* Attempt to merge the declarations. This can fail, in
8920 the case of some invalid specialization declarations. */
8921 pushed_scope = push_scope (ctype);
8922 ok = duplicate_decls (decl, old_decl, friendp);
8923 if (pushed_scope)
8924 pop_scope (pushed_scope);
8925 if (!ok)
8927 error ("no %q#D member function declared in class %qT",
8928 decl, ctype);
8929 return NULL_TREE;
8931 if (ok == error_mark_node)
8932 return NULL_TREE;
8933 return old_decl;
8937 if (DECL_CONSTRUCTOR_P (decl) && !grok_ctor_properties (ctype, decl))
8938 return NULL_TREE;
8940 if (ctype == NULL_TREE || check)
8941 return decl;
8943 if (virtualp)
8944 DECL_VIRTUAL_P (decl) = 1;
8946 return decl;
8949 /* decl is a FUNCTION_DECL.
8950 specifiers are the parsed virt-specifiers.
8952 Set flags to reflect the virt-specifiers.
8954 Returns decl. */
8956 static tree
8957 set_virt_specifiers (tree decl, cp_virt_specifiers specifiers)
8959 if (decl == NULL_TREE)
8960 return decl;
8961 if (specifiers & VIRT_SPEC_OVERRIDE)
8962 DECL_OVERRIDE_P (decl) = 1;
8963 if (specifiers & VIRT_SPEC_FINAL)
8964 DECL_FINAL_P (decl) = 1;
8965 return decl;
8968 /* DECL is a VAR_DECL for a static data member. Set flags to reflect
8969 the linkage that DECL will receive in the object file. */
8971 static void
8972 set_linkage_for_static_data_member (tree decl)
8974 /* A static data member always has static storage duration and
8975 external linkage. Note that static data members are forbidden in
8976 local classes -- the only situation in which a class has
8977 non-external linkage. */
8978 TREE_PUBLIC (decl) = 1;
8979 TREE_STATIC (decl) = 1;
8980 /* For non-template classes, static data members are always put
8981 out in exactly those files where they are defined, just as
8982 with ordinary namespace-scope variables. */
8983 if (!processing_template_decl)
8984 DECL_INTERFACE_KNOWN (decl) = 1;
8987 /* Create a VAR_DECL named NAME with the indicated TYPE.
8989 If SCOPE is non-NULL, it is the class type or namespace containing
8990 the variable. If SCOPE is NULL, the variable should is created in
8991 the innermost enclosing scope. */
8993 static tree
8994 grokvardecl (tree type,
8995 tree name,
8996 tree orig_declarator,
8997 const cp_decl_specifier_seq *declspecs,
8998 int initialized,
8999 int type_quals,
9000 int inlinep,
9001 bool conceptp,
9002 int template_count,
9003 tree scope)
9005 tree decl;
9006 tree explicit_scope;
9008 gcc_assert (!name || identifier_p (name));
9010 bool constp = (type_quals & TYPE_QUAL_CONST) != 0;
9011 bool volatilep = (type_quals & TYPE_QUAL_VOLATILE) != 0;
9013 /* Compute the scope in which to place the variable, but remember
9014 whether or not that scope was explicitly specified by the user. */
9015 explicit_scope = scope;
9016 if (!scope)
9018 /* An explicit "extern" specifier indicates a namespace-scope
9019 variable. */
9020 if (declspecs->storage_class == sc_extern)
9021 scope = current_decl_namespace ();
9022 else if (!at_function_scope_p ())
9023 scope = current_scope ();
9026 if (scope
9027 && (/* If the variable is a namespace-scope variable declared in a
9028 template, we need DECL_LANG_SPECIFIC. */
9029 (TREE_CODE (scope) == NAMESPACE_DECL && processing_template_decl)
9030 /* Similarly for namespace-scope variables with language linkage
9031 other than C++. */
9032 || (TREE_CODE (scope) == NAMESPACE_DECL
9033 && current_lang_name != lang_name_cplusplus)
9034 /* Similarly for static data members. */
9035 || TYPE_P (scope)
9036 /* Similarly for explicit specializations. */
9037 || (orig_declarator
9038 && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)))
9039 decl = build_lang_decl (VAR_DECL, name, type);
9040 else
9041 decl = build_decl (input_location, VAR_DECL, name, type);
9043 if (explicit_scope && TREE_CODE (explicit_scope) == NAMESPACE_DECL)
9044 set_decl_namespace (decl, explicit_scope, 0);
9045 else
9046 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
9048 if (declspecs->storage_class == sc_extern)
9050 DECL_THIS_EXTERN (decl) = 1;
9051 DECL_EXTERNAL (decl) = !initialized;
9054 if (DECL_CLASS_SCOPE_P (decl))
9056 set_linkage_for_static_data_member (decl);
9057 /* This function is only called with out-of-class definitions. */
9058 DECL_EXTERNAL (decl) = 0;
9059 check_class_member_definition_namespace (decl);
9061 /* At top level, either `static' or no s.c. makes a definition
9062 (perhaps tentative), and absence of `static' makes it public. */
9063 else if (toplevel_bindings_p ())
9065 TREE_PUBLIC (decl) = (declspecs->storage_class != sc_static
9066 && (DECL_THIS_EXTERN (decl)
9067 || ! constp
9068 || volatilep
9069 || inlinep));
9070 TREE_STATIC (decl) = ! DECL_EXTERNAL (decl);
9072 /* Not at top level, only `static' makes a static definition. */
9073 else
9075 TREE_STATIC (decl) = declspecs->storage_class == sc_static;
9076 TREE_PUBLIC (decl) = DECL_EXTERNAL (decl);
9079 if (decl_spec_seq_has_spec_p (declspecs, ds_thread))
9081 if (DECL_EXTERNAL (decl) || TREE_STATIC (decl))
9083 CP_DECL_THREAD_LOCAL_P (decl) = true;
9084 if (!processing_template_decl)
9085 set_decl_tls_model (decl, decl_default_tls_model (decl));
9087 if (declspecs->gnu_thread_keyword_p)
9088 SET_DECL_GNU_TLS_P (decl);
9091 /* If the type of the decl has no linkage, make sure that we'll
9092 notice that in mark_used. */
9093 if (cxx_dialect > cxx98
9094 && decl_linkage (decl) != lk_none
9095 && DECL_LANG_SPECIFIC (decl) == NULL
9096 && !DECL_EXTERN_C_P (decl)
9097 && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
9098 retrofit_lang_decl (decl);
9100 if (TREE_PUBLIC (decl))
9102 /* [basic.link]: A name with no linkage (notably, the name of a class
9103 or enumeration declared in a local scope) shall not be used to
9104 declare an entity with linkage.
9106 DR 757 relaxes this restriction for C++0x. */
9107 if (cxx_dialect < cxx11)
9108 no_linkage_error (decl);
9110 else
9111 DECL_INTERFACE_KNOWN (decl) = 1;
9113 if (DECL_NAME (decl)
9114 && MAIN_NAME_P (DECL_NAME (decl))
9115 && scope == global_namespace)
9116 error ("cannot declare %<::main%> to be a global variable");
9118 /* Check that the variable can be safely declared as a concept.
9119 Note that this also forbids explicit specializations. */
9120 if (conceptp)
9122 if (!processing_template_decl)
9124 error ("a non-template variable cannot be %<concept%>");
9125 return NULL_TREE;
9127 else
9128 DECL_DECLARED_CONCEPT_P (decl) = true;
9129 if (!same_type_ignoring_top_level_qualifiers_p (type, boolean_type_node))
9130 error_at (declspecs->locations[ds_type_spec],
9131 "concept must have type %<bool%>");
9133 else if (flag_concepts
9134 && processing_template_decl > template_class_depth (scope))
9136 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
9137 tree ci = build_constraints (reqs, NULL_TREE);
9138 set_constraints (decl, ci);
9141 // Handle explicit specializations and instantiations of variable templates.
9142 if (orig_declarator)
9143 decl = check_explicit_specialization (orig_declarator, decl,
9144 template_count, conceptp * 8);
9146 return decl != error_mark_node ? decl : NULL_TREE;
9149 /* Create and return a canonical pointer to member function type, for
9150 TYPE, which is a POINTER_TYPE to a METHOD_TYPE. */
9152 tree
9153 build_ptrmemfunc_type (tree type)
9155 tree field, fields;
9156 tree t;
9158 if (type == error_mark_node)
9159 return type;
9161 /* Make sure that we always have the unqualified pointer-to-member
9162 type first. */
9163 if (cp_cv_quals quals = cp_type_quals (type))
9165 tree unqual = build_ptrmemfunc_type (TYPE_MAIN_VARIANT (type));
9166 return cp_build_qualified_type (unqual, quals);
9169 /* If a canonical type already exists for this type, use it. We use
9170 this method instead of type_hash_canon, because it only does a
9171 simple equality check on the list of field members. */
9173 if ((t = TYPE_GET_PTRMEMFUNC_TYPE (type)))
9174 return t;
9176 t = make_node (RECORD_TYPE);
9178 /* Let the front end know this is a pointer to member function. */
9179 TYPE_PTRMEMFUNC_FLAG (t) = 1;
9181 field = build_decl (input_location, FIELD_DECL, pfn_identifier, type);
9182 fields = field;
9184 field = build_decl (input_location, FIELD_DECL, delta_identifier,
9185 delta_type_node);
9186 DECL_CHAIN (field) = fields;
9187 fields = field;
9189 finish_builtin_struct (t, "__ptrmemfunc_type", fields, ptr_type_node);
9191 /* Zap out the name so that the back end will give us the debugging
9192 information for this anonymous RECORD_TYPE. */
9193 TYPE_NAME (t) = NULL_TREE;
9195 /* Cache this pointer-to-member type so that we can find it again
9196 later. */
9197 TYPE_SET_PTRMEMFUNC_TYPE (type, t);
9199 if (TYPE_STRUCTURAL_EQUALITY_P (type))
9200 SET_TYPE_STRUCTURAL_EQUALITY (t);
9201 else if (TYPE_CANONICAL (type) != type)
9202 TYPE_CANONICAL (t) = build_ptrmemfunc_type (TYPE_CANONICAL (type));
9204 return t;
9207 /* Create and return a pointer to data member type. */
9209 tree
9210 build_ptrmem_type (tree class_type, tree member_type)
9212 if (TREE_CODE (member_type) == METHOD_TYPE)
9214 cp_cv_quals quals = type_memfn_quals (member_type);
9215 cp_ref_qualifier rqual = type_memfn_rqual (member_type);
9216 member_type = build_memfn_type (member_type, class_type, quals, rqual);
9217 return build_ptrmemfunc_type (build_pointer_type (member_type));
9219 else
9221 gcc_assert (TREE_CODE (member_type) != FUNCTION_TYPE);
9222 return build_offset_type (class_type, member_type);
9226 /* DECL is a VAR_DECL defined in-class, whose TYPE is also given.
9227 Check to see that the definition is valid. Issue appropriate error
9228 messages. Return 1 if the definition is particularly bad, or 0
9229 otherwise. */
9231 static int
9232 check_static_variable_definition (tree decl, tree type)
9234 /* Avoid redundant diagnostics on out-of-class definitions. */
9235 if (!current_class_type || !TYPE_BEING_DEFINED (current_class_type))
9236 return 0;
9237 /* Can't check yet if we don't know the type. */
9238 if (dependent_type_p (type))
9239 return 0;
9240 /* If DECL is declared constexpr, we'll do the appropriate checks
9241 in check_initializer. Similarly for inline static data members. */
9242 if (DECL_P (decl)
9243 && (DECL_DECLARED_CONSTEXPR_P (decl)
9244 || DECL_VAR_DECLARED_INLINE_P (decl)))
9245 return 0;
9246 else if (cxx_dialect >= cxx11 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
9248 if (!COMPLETE_TYPE_P (type))
9249 error_at (DECL_SOURCE_LOCATION (decl),
9250 "in-class initialization of static data member %q#D of "
9251 "incomplete type", decl);
9252 else if (literal_type_p (type))
9253 permerror (DECL_SOURCE_LOCATION (decl),
9254 "%<constexpr%> needed for in-class initialization of "
9255 "static data member %q#D of non-integral type", decl);
9256 else
9257 error_at (DECL_SOURCE_LOCATION (decl),
9258 "in-class initialization of static data member %q#D of "
9259 "non-literal type", decl);
9260 return 1;
9263 /* Motion 10 at San Diego: If a static const integral data member is
9264 initialized with an integral constant expression, the initializer
9265 may appear either in the declaration (within the class), or in
9266 the definition, but not both. If it appears in the class, the
9267 member is a member constant. The file-scope definition is always
9268 required. */
9269 if (!ARITHMETIC_TYPE_P (type) && TREE_CODE (type) != ENUMERAL_TYPE)
9271 error_at (DECL_SOURCE_LOCATION (decl),
9272 "invalid in-class initialization of static data member "
9273 "of non-integral type %qT",
9274 type);
9275 return 1;
9277 else if (!CP_TYPE_CONST_P (type))
9278 error_at (DECL_SOURCE_LOCATION (decl),
9279 "ISO C++ forbids in-class initialization of non-const "
9280 "static member %qD",
9281 decl);
9282 else if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type))
9283 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
9284 "ISO C++ forbids initialization of member constant "
9285 "%qD of non-integral type %qT", decl, type);
9287 return 0;
9290 /* *expr_p is part of the TYPE_SIZE of a variably-sized array. If any
9291 SAVE_EXPRs in *expr_p wrap expressions with side-effects, break those
9292 expressions out into temporary variables so that walk_tree doesn't
9293 step into them (c++/15764). */
9295 static tree
9296 stabilize_save_expr_r (tree *expr_p, int *walk_subtrees, void *data)
9298 hash_set<tree> *pset = (hash_set<tree> *)data;
9299 tree expr = *expr_p;
9300 if (TREE_CODE (expr) == SAVE_EXPR)
9302 tree op = TREE_OPERAND (expr, 0);
9303 cp_walk_tree (&op, stabilize_save_expr_r, data, pset);
9304 if (TREE_SIDE_EFFECTS (op))
9305 TREE_OPERAND (expr, 0) = get_temp_regvar (TREE_TYPE (op), op);
9306 *walk_subtrees = 0;
9308 else if (!EXPR_P (expr) || !TREE_SIDE_EFFECTS (expr))
9309 *walk_subtrees = 0;
9310 return NULL;
9313 /* Entry point for the above. */
9315 static void
9316 stabilize_vla_size (tree size)
9318 hash_set<tree> pset;
9319 /* Break out any function calls into temporary variables. */
9320 cp_walk_tree (&size, stabilize_save_expr_r, &pset, &pset);
9323 /* Reduce a SIZEOF_EXPR to its value. */
9325 tree
9326 fold_sizeof_expr (tree t)
9328 tree r;
9329 if (SIZEOF_EXPR_TYPE_P (t))
9330 r = cxx_sizeof_or_alignof_type (TREE_TYPE (TREE_OPERAND (t, 0)),
9331 SIZEOF_EXPR, false);
9332 else if (TYPE_P (TREE_OPERAND (t, 0)))
9333 r = cxx_sizeof_or_alignof_type (TREE_OPERAND (t, 0), SIZEOF_EXPR,
9334 false);
9335 else
9336 r = cxx_sizeof_or_alignof_expr (TREE_OPERAND (t, 0), SIZEOF_EXPR,
9337 false);
9338 if (r == error_mark_node)
9339 r = size_one_node;
9340 return r;
9343 /* Given the SIZE (i.e., number of elements) in an array, compute
9344 an appropriate index type for the array. If non-NULL, NAME is
9345 the name of the entity being declared. */
9347 tree
9348 compute_array_index_type (tree name, tree size, tsubst_flags_t complain)
9350 tree itype;
9351 tree osize = size;
9353 if (error_operand_p (size))
9354 return error_mark_node;
9356 if (!type_dependent_expression_p (size))
9358 tree type = TREE_TYPE (size);
9360 mark_rvalue_use (size);
9362 if (cxx_dialect < cxx11 && TREE_CODE (size) == NOP_EXPR
9363 && TREE_SIDE_EFFECTS (size))
9364 /* In C++98, we mark a non-constant array bound with a magic
9365 NOP_EXPR with TREE_SIDE_EFFECTS; don't fold in that case. */;
9366 else
9368 size = instantiate_non_dependent_expr_sfinae (size, complain);
9370 if (CLASS_TYPE_P (type)
9371 && CLASSTYPE_LITERAL_P (type))
9373 size = build_expr_type_conversion (WANT_INT, size, true);
9374 if (!size)
9376 if (!(complain & tf_error))
9377 return error_mark_node;
9378 if (name)
9379 error ("size of array %qD has non-integral type %qT",
9380 name, type);
9381 else
9382 error ("size of array has non-integral type %qT", type);
9383 size = integer_one_node;
9385 if (size == error_mark_node)
9386 return error_mark_node;
9387 type = TREE_TYPE (size);
9390 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
9391 size = maybe_constant_value (size);
9393 if (!TREE_CONSTANT (size))
9394 size = osize;
9397 if (error_operand_p (size))
9398 return error_mark_node;
9400 /* The array bound must be an integer type. */
9401 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
9403 if (!(complain & tf_error))
9404 return error_mark_node;
9405 if (name)
9406 error ("size of array %qD has non-integral type %qT", name, type);
9407 else
9408 error ("size of array has non-integral type %qT", type);
9409 size = integer_one_node;
9410 type = TREE_TYPE (size);
9414 /* A type is dependent if it is...an array type constructed from any
9415 dependent type or whose size is specified by a constant expression
9416 that is value-dependent. */
9417 /* We can only call value_dependent_expression_p on integral constant
9418 expressions; treat non-constant expressions as dependent, too. */
9419 if (processing_template_decl
9420 && (type_dependent_expression_p (size)
9421 || !TREE_CONSTANT (size) || value_dependent_expression_p (size)))
9423 /* We cannot do any checking for a SIZE that isn't known to be
9424 constant. Just build the index type and mark that it requires
9425 structural equality checks. */
9426 itype = build_index_type (build_min (MINUS_EXPR, sizetype,
9427 size, size_one_node));
9428 TYPE_DEPENDENT_P (itype) = 1;
9429 TYPE_DEPENDENT_P_VALID (itype) = 1;
9430 SET_TYPE_STRUCTURAL_EQUALITY (itype);
9431 return itype;
9434 if (TREE_CODE (size) != INTEGER_CST)
9436 tree folded = cp_fully_fold (size);
9437 if (TREE_CODE (folded) == INTEGER_CST)
9438 pedwarn (location_of (size), OPT_Wpedantic,
9439 "size of array is not an integral constant-expression");
9440 /* Use the folded result for VLAs, too; it will have resolved
9441 SIZEOF_EXPR. */
9442 size = folded;
9445 /* Normally, the array-bound will be a constant. */
9446 if (TREE_CODE (size) == INTEGER_CST)
9448 /* Check to see if the array bound overflowed. Make that an
9449 error, no matter how generous we're being. */
9450 constant_expression_error (size);
9452 /* An array must have a positive number of elements. */
9453 if (tree_int_cst_lt (size, integer_zero_node))
9455 if (!(complain & tf_error))
9456 return error_mark_node;
9457 if (name)
9458 error ("size of array %qD is negative", name);
9459 else
9460 error ("size of array is negative");
9461 size = integer_one_node;
9463 /* As an extension we allow zero-sized arrays. */
9464 else if (integer_zerop (size))
9466 if (!(complain & tf_error))
9467 /* We must fail if performing argument deduction (as
9468 indicated by the state of complain), so that
9469 another substitution can be found. */
9470 return error_mark_node;
9471 else if (in_system_header_at (input_location))
9472 /* Allow them in system headers because glibc uses them. */;
9473 else if (name)
9474 pedwarn (input_location, OPT_Wpedantic, "ISO C++ forbids zero-size array %qD", name);
9475 else
9476 pedwarn (input_location, OPT_Wpedantic, "ISO C++ forbids zero-size array");
9479 else if (TREE_CONSTANT (size)
9480 /* We don't allow VLAs at non-function scopes, or during
9481 tentative template substitution. */
9482 || !at_function_scope_p ()
9483 || !(complain & tf_error))
9485 if (!(complain & tf_error))
9486 return error_mark_node;
9487 /* `(int) &fn' is not a valid array bound. */
9488 if (name)
9489 error ("size of array %qD is not an integral constant-expression",
9490 name);
9491 else
9492 error ("size of array is not an integral constant-expression");
9493 size = integer_one_node;
9495 else if (pedantic && warn_vla != 0)
9497 if (name)
9498 pedwarn (input_location, OPT_Wvla, "ISO C++ forbids variable length array %qD", name);
9499 else
9500 pedwarn (input_location, OPT_Wvla, "ISO C++ forbids variable length array");
9502 else if (warn_vla > 0)
9504 if (name)
9505 warning (OPT_Wvla,
9506 "variable length array %qD is used", name);
9507 else
9508 warning (OPT_Wvla,
9509 "variable length array is used");
9512 if (processing_template_decl && !TREE_CONSTANT (size))
9513 /* A variable sized array. */
9514 itype = build_min (MINUS_EXPR, sizetype, size, integer_one_node);
9515 else
9517 HOST_WIDE_INT saved_processing_template_decl;
9519 /* Compute the index of the largest element in the array. It is
9520 one less than the number of elements in the array. We save
9521 and restore PROCESSING_TEMPLATE_DECL so that computations in
9522 cp_build_binary_op will be appropriately folded. */
9523 saved_processing_template_decl = processing_template_decl;
9524 processing_template_decl = 0;
9525 itype = cp_build_binary_op (input_location,
9526 MINUS_EXPR,
9527 cp_convert (ssizetype, size, complain),
9528 cp_convert (ssizetype, integer_one_node,
9529 complain),
9530 complain);
9531 itype = maybe_constant_value (itype);
9532 processing_template_decl = saved_processing_template_decl;
9534 if (!TREE_CONSTANT (itype))
9536 /* A variable sized array. */
9537 itype = variable_size (itype);
9539 stabilize_vla_size (itype);
9541 if (flag_sanitize & SANITIZE_VLA
9542 && do_ubsan_in_current_function ())
9544 /* We have to add 1 -- in the ubsan routine we generate
9545 LE_EXPR rather than LT_EXPR. */
9546 tree t = fold_build2 (PLUS_EXPR, TREE_TYPE (itype), itype,
9547 build_one_cst (TREE_TYPE (itype)));
9548 t = ubsan_instrument_vla (input_location, t);
9549 finish_expr_stmt (t);
9552 /* Make sure that there was no overflow when creating to a signed
9553 index type. (For example, on a 32-bit machine, an array with
9554 size 2^32 - 1 is too big.) */
9555 else if (TREE_CODE (itype) == INTEGER_CST
9556 && TREE_OVERFLOW (itype))
9558 if (!(complain & tf_error))
9559 return error_mark_node;
9560 error ("overflow in array dimension");
9561 TREE_OVERFLOW (itype) = 0;
9565 /* Create and return the appropriate index type. */
9566 itype = build_index_type (itype);
9568 /* If the index type were dependent, we would have returned early, so
9569 remember that it isn't. */
9570 TYPE_DEPENDENT_P (itype) = 0;
9571 TYPE_DEPENDENT_P_VALID (itype) = 1;
9572 return itype;
9575 /* Returns the scope (if any) in which the entity declared by
9576 DECLARATOR will be located. If the entity was declared with an
9577 unqualified name, NULL_TREE is returned. */
9579 tree
9580 get_scope_of_declarator (const cp_declarator *declarator)
9582 while (declarator && declarator->kind != cdk_id)
9583 declarator = declarator->declarator;
9585 /* If the declarator-id is a SCOPE_REF, the scope in which the
9586 declaration occurs is the first operand. */
9587 if (declarator
9588 && declarator->u.id.qualifying_scope)
9589 return declarator->u.id.qualifying_scope;
9591 /* Otherwise, the declarator is not a qualified name; the entity will
9592 be declared in the current scope. */
9593 return NULL_TREE;
9596 /* Returns an ARRAY_TYPE for an array with SIZE elements of the
9597 indicated TYPE. If non-NULL, NAME is the NAME of the declaration
9598 with this type. */
9600 static tree
9601 create_array_type_for_decl (tree name, tree type, tree size)
9603 tree itype = NULL_TREE;
9605 /* If things have already gone awry, bail now. */
9606 if (type == error_mark_node || size == error_mark_node)
9607 return error_mark_node;
9609 /* 8.3.4/1: If the type of the identifier of D contains the auto
9610 type-specifier, the program is ill-formed. */
9611 if (type_uses_auto (type))
9613 error ("%qD declared as array of %qT", name, type);
9614 return error_mark_node;
9617 /* If there are some types which cannot be array elements,
9618 issue an error-message and return. */
9619 switch (TREE_CODE (type))
9621 case VOID_TYPE:
9622 if (name)
9623 error ("declaration of %qD as array of void", name);
9624 else
9625 error ("creating array of void");
9626 return error_mark_node;
9628 case FUNCTION_TYPE:
9629 if (name)
9630 error ("declaration of %qD as array of functions", name);
9631 else
9632 error ("creating array of functions");
9633 return error_mark_node;
9635 case REFERENCE_TYPE:
9636 if (name)
9637 error ("declaration of %qD as array of references", name);
9638 else
9639 error ("creating array of references");
9640 return error_mark_node;
9642 case METHOD_TYPE:
9643 if (name)
9644 error ("declaration of %qD as array of function members", name);
9645 else
9646 error ("creating array of function members");
9647 return error_mark_node;
9649 default:
9650 break;
9653 /* [dcl.array]
9655 The constant expressions that specify the bounds of the arrays
9656 can be omitted only for the first member of the sequence. */
9657 if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type))
9659 if (name)
9660 error ("declaration of %qD as multidimensional array must "
9661 "have bounds for all dimensions except the first",
9662 name);
9663 else
9664 error ("multidimensional array must have bounds for all "
9665 "dimensions except the first");
9667 return error_mark_node;
9670 /* Figure out the index type for the array. */
9671 if (size)
9672 itype = compute_array_index_type (name, size, tf_warning_or_error);
9674 /* [dcl.array]
9675 T is called the array element type; this type shall not be [...] an
9676 abstract class type. */
9677 abstract_virtuals_error (name, type);
9679 return build_cplus_array_type (type, itype);
9682 /* Returns the smallest location != UNKNOWN_LOCATION among the
9683 three stored in LOCATIONS[ds_const], LOCATIONS[ds_volatile],
9684 and LOCATIONS[ds_restrict]. */
9686 static location_t
9687 smallest_type_quals_location (int type_quals, const location_t* locations)
9689 location_t loc = UNKNOWN_LOCATION;
9691 if (type_quals & TYPE_QUAL_CONST)
9692 loc = locations[ds_const];
9694 if ((type_quals & TYPE_QUAL_VOLATILE)
9695 && (loc == UNKNOWN_LOCATION || locations[ds_volatile] < loc))
9696 loc = locations[ds_volatile];
9698 if ((type_quals & TYPE_QUAL_RESTRICT)
9699 && (loc == UNKNOWN_LOCATION || locations[ds_restrict] < loc))
9700 loc = locations[ds_restrict];
9702 return loc;
9705 /* Check that it's OK to declare a function with the indicated TYPE
9706 and TYPE_QUALS. SFK indicates the kind of special function (if any)
9707 that this function is. OPTYPE is the type given in a conversion
9708 operator declaration, or the class type for a constructor/destructor.
9709 Returns the actual return type of the function; that may be different
9710 than TYPE if an error occurs, or for certain special functions. */
9712 static tree
9713 check_special_function_return_type (special_function_kind sfk,
9714 tree type,
9715 tree optype,
9716 int type_quals,
9717 const location_t* locations)
9719 switch (sfk)
9721 case sfk_constructor:
9722 if (type)
9723 error ("return type specification for constructor invalid");
9724 else if (type_quals != TYPE_UNQUALIFIED)
9725 error_at (smallest_type_quals_location (type_quals, locations),
9726 "qualifiers are not allowed on constructor declaration");
9728 if (targetm.cxx.cdtor_returns_this ())
9729 type = build_pointer_type (optype);
9730 else
9731 type = void_type_node;
9732 break;
9734 case sfk_destructor:
9735 if (type)
9736 error ("return type specification for destructor invalid");
9737 else if (type_quals != TYPE_UNQUALIFIED)
9738 error_at (smallest_type_quals_location (type_quals, locations),
9739 "qualifiers are not allowed on destructor declaration");
9741 /* We can't use the proper return type here because we run into
9742 problems with ambiguous bases and covariant returns. */
9743 if (targetm.cxx.cdtor_returns_this ())
9744 type = build_pointer_type (void_type_node);
9745 else
9746 type = void_type_node;
9747 break;
9749 case sfk_conversion:
9750 if (type)
9751 error ("return type specified for %<operator %T%>", optype);
9752 else if (type_quals != TYPE_UNQUALIFIED)
9753 error_at (smallest_type_quals_location (type_quals, locations),
9754 "qualifiers are not allowed on declaration of "
9755 "%<operator %T%>", optype);
9757 type = optype;
9758 break;
9760 default:
9761 gcc_unreachable ();
9764 return type;
9767 /* A variable or data member (whose unqualified name is IDENTIFIER)
9768 has been declared with the indicated TYPE. If the TYPE is not
9769 acceptable, issue an error message and return a type to use for
9770 error-recovery purposes. */
9772 tree
9773 check_var_type (tree identifier, tree type)
9775 if (VOID_TYPE_P (type))
9777 if (!identifier)
9778 error ("unnamed variable or field declared void");
9779 else if (identifier_p (identifier))
9781 gcc_assert (!IDENTIFIER_OPNAME_P (identifier));
9782 error ("variable or field %qE declared void", identifier);
9784 else
9785 error ("variable or field declared void");
9786 type = error_mark_node;
9789 return type;
9792 /* Handle declaring DECL as an inline variable. */
9794 static void
9795 mark_inline_variable (tree decl)
9797 bool inlinep = true;
9798 if (! toplevel_bindings_p ())
9800 error ("%<inline%> specifier invalid for variable "
9801 "%qD declared at block scope", decl);
9802 inlinep = false;
9804 else if (cxx_dialect < cxx1z)
9805 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
9806 "inline variables are only available "
9807 "with -std=c++1z or -std=gnu++1z");
9808 if (inlinep)
9810 retrofit_lang_decl (decl);
9811 SET_DECL_VAR_DECLARED_INLINE_P (decl);
9815 /* Given declspecs and a declarator (abstract or otherwise), determine
9816 the name and type of the object declared and construct a DECL node
9817 for it.
9819 DECLSPECS points to the representation of declaration-specifier
9820 sequence that precedes declarator.
9822 DECL_CONTEXT says which syntactic context this declaration is in:
9823 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
9824 FUNCDEF for a function definition. Like NORMAL but a few different
9825 error messages in each case. Return value may be zero meaning
9826 this definition is too screwy to try to parse.
9827 MEMFUNCDEF for a function definition. Like FUNCDEF but prepares to
9828 handle member functions (which have FIELD context).
9829 Return value may be zero meaning this definition is too screwy to
9830 try to parse.
9831 PARM for a parameter declaration (either within a function prototype
9832 or before a function body). Make a PARM_DECL, or return void_type_node.
9833 TPARM for a template parameter declaration.
9834 CATCHPARM for a parameter declaration before a catch clause.
9835 TYPENAME if for a typename (in a cast or sizeof).
9836 Don't make a DECL node; just return the ..._TYPE node.
9837 FIELD for a struct or union field; make a FIELD_DECL.
9838 BITFIELD for a field with specified width.
9840 INITIALIZED is as for start_decl.
9842 ATTRLIST is a pointer to the list of attributes, which may be NULL
9843 if there are none; *ATTRLIST may be modified if attributes from inside
9844 the declarator should be applied to the declaration.
9846 When this function is called, scoping variables (such as
9847 CURRENT_CLASS_TYPE) should reflect the scope in which the
9848 declaration occurs, not the scope in which the new declaration will
9849 be placed. For example, on:
9851 void S::f() { ... }
9853 when grokdeclarator is called for `S::f', the CURRENT_CLASS_TYPE
9854 should not be `S'.
9856 Returns a DECL (if a declarator is present), a TYPE (if there is no
9857 declarator, in cases like "struct S;"), or the ERROR_MARK_NODE if an
9858 error occurs. */
9860 tree
9861 grokdeclarator (const cp_declarator *declarator,
9862 cp_decl_specifier_seq *declspecs,
9863 enum decl_context decl_context,
9864 int initialized,
9865 tree* attrlist)
9867 tree type = NULL_TREE;
9868 int longlong = 0;
9869 int explicit_intN = 0;
9870 int virtualp, explicitp, friendp, inlinep, staticp;
9871 int explicit_int = 0;
9872 int explicit_char = 0;
9873 int defaulted_int = 0;
9875 tree typedef_decl = NULL_TREE;
9876 const char *name = NULL;
9877 tree typedef_type = NULL_TREE;
9878 /* True if this declarator is a function definition. */
9879 bool funcdef_flag = false;
9880 cp_declarator_kind innermost_code = cdk_error;
9881 int bitfield = 0;
9882 #if 0
9883 /* See the code below that used this. */
9884 tree decl_attr = NULL_TREE;
9885 #endif
9887 /* Keep track of what sort of function is being processed
9888 so that we can warn about default return values, or explicit
9889 return values which do not match prescribed defaults. */
9890 special_function_kind sfk = sfk_none;
9892 tree dname = NULL_TREE;
9893 tree ctor_return_type = NULL_TREE;
9894 enum overload_flags flags = NO_SPECIAL;
9895 /* cv-qualifiers that apply to the declarator, for a declaration of
9896 a member function. */
9897 cp_cv_quals memfn_quals = TYPE_UNQUALIFIED;
9898 /* virt-specifiers that apply to the declarator, for a declaration of
9899 a member function. */
9900 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
9901 /* ref-qualifier that applies to the declarator, for a declaration of
9902 a member function. */
9903 cp_ref_qualifier rqual = REF_QUAL_NONE;
9904 /* cv-qualifiers that apply to the type specified by the DECLSPECS. */
9905 int type_quals = TYPE_UNQUALIFIED;
9906 tree raises = NULL_TREE;
9907 int template_count = 0;
9908 tree returned_attrs = NULL_TREE;
9909 tree parms = NULL_TREE;
9910 const cp_declarator *id_declarator;
9911 /* The unqualified name of the declarator; either an
9912 IDENTIFIER_NODE, BIT_NOT_EXPR, or TEMPLATE_ID_EXPR. */
9913 tree unqualified_id;
9914 /* The class type, if any, in which this entity is located,
9915 or NULL_TREE if none. Note that this value may be different from
9916 the current class type; for example if an attempt is made to declare
9917 "A::f" inside "B", this value will be "A". */
9918 tree ctype = current_class_type;
9919 /* The NAMESPACE_DECL for the namespace in which this entity is
9920 located. If an unqualified name is used to declare the entity,
9921 this value will be NULL_TREE, even if the entity is located at
9922 namespace scope. */
9923 tree in_namespace = NULL_TREE;
9924 cp_storage_class storage_class;
9925 bool unsigned_p, signed_p, short_p, long_p, thread_p;
9926 bool type_was_error_mark_node = false;
9927 bool parameter_pack_p = declarator ? declarator->parameter_pack_p : false;
9928 bool template_type_arg = false;
9929 bool template_parm_flag = false;
9930 bool typedef_p = decl_spec_seq_has_spec_p (declspecs, ds_typedef);
9931 bool constexpr_p = decl_spec_seq_has_spec_p (declspecs, ds_constexpr);
9932 bool late_return_type_p = false;
9933 bool array_parameter_p = false;
9934 source_location saved_loc = input_location;
9935 tree reqs = NULL_TREE;
9937 signed_p = decl_spec_seq_has_spec_p (declspecs, ds_signed);
9938 unsigned_p = decl_spec_seq_has_spec_p (declspecs, ds_unsigned);
9939 short_p = decl_spec_seq_has_spec_p (declspecs, ds_short);
9940 long_p = decl_spec_seq_has_spec_p (declspecs, ds_long);
9941 longlong = decl_spec_seq_has_spec_p (declspecs, ds_long_long);
9942 explicit_intN = declspecs->explicit_intN_p;
9943 thread_p = decl_spec_seq_has_spec_p (declspecs, ds_thread);
9945 // Was concept_p specified? Note that ds_concept
9946 // implies ds_constexpr!
9947 bool concept_p = decl_spec_seq_has_spec_p (declspecs, ds_concept);
9948 if (concept_p)
9949 constexpr_p = true;
9951 if (decl_spec_seq_has_spec_p (declspecs, ds_const))
9952 type_quals |= TYPE_QUAL_CONST;
9953 if (decl_spec_seq_has_spec_p (declspecs, ds_volatile))
9954 type_quals |= TYPE_QUAL_VOLATILE;
9955 if (decl_spec_seq_has_spec_p (declspecs, ds_restrict))
9956 type_quals |= TYPE_QUAL_RESTRICT;
9958 if (decl_context == FUNCDEF)
9959 funcdef_flag = true, decl_context = NORMAL;
9960 else if (decl_context == MEMFUNCDEF)
9961 funcdef_flag = true, decl_context = FIELD;
9962 else if (decl_context == BITFIELD)
9963 bitfield = 1, decl_context = FIELD;
9964 else if (decl_context == TEMPLATE_TYPE_ARG)
9965 template_type_arg = true, decl_context = TYPENAME;
9966 else if (decl_context == TPARM)
9967 template_parm_flag = true, decl_context = PARM;
9969 if (initialized > 1)
9970 funcdef_flag = true;
9972 location_t typespec_loc = smallest_type_quals_location (type_quals,
9973 declspecs->locations);
9974 if (typespec_loc == UNKNOWN_LOCATION)
9975 typespec_loc = declspecs->locations[ds_type_spec];
9977 /* Look inside a declarator for the name being declared
9978 and get it as a string, for an error message. */
9979 for (id_declarator = declarator;
9980 id_declarator;
9981 id_declarator = id_declarator->declarator)
9983 if (id_declarator->kind != cdk_id)
9984 innermost_code = id_declarator->kind;
9986 switch (id_declarator->kind)
9988 case cdk_function:
9989 if (id_declarator->declarator
9990 && id_declarator->declarator->kind == cdk_id)
9992 sfk = id_declarator->declarator->u.id.sfk;
9993 if (sfk == sfk_destructor)
9994 flags = DTOR_FLAG;
9996 break;
9998 case cdk_id:
10000 tree qualifying_scope = id_declarator->u.id.qualifying_scope;
10001 tree decl = id_declarator->u.id.unqualified_name;
10002 if (!decl)
10003 break;
10004 if (qualifying_scope)
10006 if (at_function_scope_p ())
10008 /* [dcl.meaning]
10010 A declarator-id shall not be qualified except
10011 for ...
10013 None of the cases are permitted in block
10014 scope. */
10015 if (qualifying_scope == global_namespace)
10016 error ("invalid use of qualified-name %<::%D%>",
10017 decl);
10018 else if (TYPE_P (qualifying_scope))
10019 error ("invalid use of qualified-name %<%T::%D%>",
10020 qualifying_scope, decl);
10021 else
10022 error ("invalid use of qualified-name %<%D::%D%>",
10023 qualifying_scope, decl);
10024 return error_mark_node;
10026 else if (TYPE_P (qualifying_scope))
10028 ctype = qualifying_scope;
10029 if (!MAYBE_CLASS_TYPE_P (ctype))
10031 error ("%q#T is not a class or a namespace", ctype);
10032 ctype = NULL_TREE;
10034 else if (innermost_code != cdk_function
10035 && current_class_type
10036 && !uniquely_derived_from_p (ctype,
10037 current_class_type))
10039 error ("invalid use of qualified-name %<%T::%D%>",
10040 qualifying_scope, decl);
10041 return error_mark_node;
10044 else if (TREE_CODE (qualifying_scope) == NAMESPACE_DECL)
10045 in_namespace = qualifying_scope;
10047 switch (TREE_CODE (decl))
10049 case BIT_NOT_EXPR:
10051 tree type;
10053 if (innermost_code != cdk_function)
10055 error ("declaration of %qD as non-function", decl);
10056 return error_mark_node;
10058 else if (!qualifying_scope
10059 && !(current_class_type && at_class_scope_p ()))
10061 error ("declaration of %qD as non-member", decl);
10062 return error_mark_node;
10065 type = TREE_OPERAND (decl, 0);
10066 if (TYPE_P (type))
10067 type = constructor_name (type);
10068 name = identifier_to_locale (IDENTIFIER_POINTER (type));
10069 dname = decl;
10071 break;
10073 case TEMPLATE_ID_EXPR:
10075 tree fns = TREE_OPERAND (decl, 0);
10077 dname = fns;
10078 if (!identifier_p (dname))
10080 if (variable_template_p (dname))
10081 dname = DECL_NAME (dname);
10082 else
10084 gcc_assert (is_overloaded_fn (dname));
10085 dname = DECL_NAME (get_first_fn (dname));
10089 /* Fall through. */
10091 case IDENTIFIER_NODE:
10092 if (identifier_p (decl))
10093 dname = decl;
10095 if (C_IS_RESERVED_WORD (dname))
10097 error ("declarator-id missing; using reserved word %qD",
10098 dname);
10099 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
10101 else if (!IDENTIFIER_TYPENAME_P (dname))
10102 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
10103 else
10105 gcc_assert (flags == NO_SPECIAL);
10106 flags = TYPENAME_FLAG;
10107 ctor_return_type = TREE_TYPE (dname);
10108 sfk = sfk_conversion;
10109 if (is_typename_at_global_scope (dname))
10110 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
10111 else
10112 name = "<invalid operator>";
10114 break;
10116 default:
10117 gcc_unreachable ();
10119 break;
10122 case cdk_array:
10123 case cdk_pointer:
10124 case cdk_reference:
10125 case cdk_ptrmem:
10126 break;
10128 case cdk_decomp:
10129 name = "decomposition";
10130 break;
10132 case cdk_error:
10133 return error_mark_node;
10135 default:
10136 gcc_unreachable ();
10138 if (id_declarator->kind == cdk_id)
10139 break;
10142 /* [dcl.fct.edf]
10144 The declarator in a function-definition shall have the form
10145 D1 ( parameter-declaration-clause) ... */
10146 if (funcdef_flag && innermost_code != cdk_function)
10148 error ("function definition does not declare parameters");
10149 return error_mark_node;
10152 if (flags == TYPENAME_FLAG
10153 && innermost_code != cdk_function
10154 && ! (ctype && !declspecs->any_specifiers_p))
10156 error ("declaration of %qD as non-function", dname);
10157 return error_mark_node;
10160 if (dname
10161 && identifier_p (dname)
10162 && UDLIT_OPER_P (dname)
10163 && innermost_code != cdk_function)
10165 error ("declaration of %qD as non-function", dname);
10166 return error_mark_node;
10169 if (dname && IDENTIFIER_OPNAME_P (dname))
10171 if (typedef_p)
10173 error ("declaration of %qD as %<typedef%>", dname);
10174 return error_mark_node;
10176 else if (decl_context == PARM || decl_context == CATCHPARM)
10178 error ("declaration of %qD as parameter", dname);
10179 return error_mark_node;
10183 /* Anything declared one level down from the top level
10184 must be one of the parameters of a function
10185 (because the body is at least two levels down). */
10187 /* This heuristic cannot be applied to C++ nodes! Fixed, however,
10188 by not allowing C++ class definitions to specify their parameters
10189 with xdecls (must be spec.d in the parmlist).
10191 Since we now wait to push a class scope until we are sure that
10192 we are in a legitimate method context, we must set oldcname
10193 explicitly (since current_class_name is not yet alive).
10195 We also want to avoid calling this a PARM if it is in a namespace. */
10197 if (decl_context == NORMAL && !toplevel_bindings_p ())
10199 cp_binding_level *b = current_binding_level;
10200 current_binding_level = b->level_chain;
10201 if (current_binding_level != 0 && toplevel_bindings_p ())
10202 decl_context = PARM;
10203 current_binding_level = b;
10206 if (name == NULL)
10207 name = decl_context == PARM ? "parameter" : "type name";
10209 if (concept_p && typedef_p)
10211 error ("%<concept%> cannot appear in a typedef declaration");
10212 return error_mark_node;
10215 if (constexpr_p && typedef_p)
10217 error ("%<constexpr%> cannot appear in a typedef declaration");
10218 return error_mark_node;
10221 /* If there were multiple types specified in the decl-specifier-seq,
10222 issue an error message. */
10223 if (declspecs->multiple_types_p)
10225 error ("two or more data types in declaration of %qs", name);
10226 return error_mark_node;
10229 if (declspecs->conflicting_specifiers_p)
10231 error ("conflicting specifiers in declaration of %qs", name);
10232 return error_mark_node;
10235 /* Extract the basic type from the decl-specifier-seq. */
10236 type = declspecs->type;
10237 if (type == error_mark_node)
10239 type = NULL_TREE;
10240 type_was_error_mark_node = true;
10242 /* If the entire declaration is itself tagged as deprecated then
10243 suppress reports of deprecated items. */
10244 if (type && TREE_DEPRECATED (type)
10245 && deprecated_state != DEPRECATED_SUPPRESS)
10246 warn_deprecated_use (type, NULL_TREE);
10247 if (type && TREE_CODE (type) == TYPE_DECL)
10249 typedef_decl = type;
10250 type = TREE_TYPE (typedef_decl);
10251 if (TREE_DEPRECATED (type)
10252 && DECL_ARTIFICIAL (typedef_decl)
10253 && deprecated_state != DEPRECATED_SUPPRESS)
10254 warn_deprecated_use (type, NULL_TREE);
10256 /* No type at all: default to `int', and set DEFAULTED_INT
10257 because it was not a user-defined typedef. */
10258 if (type == NULL_TREE)
10260 if (signed_p || unsigned_p || long_p || short_p)
10262 /* These imply 'int'. */
10263 type = integer_type_node;
10264 defaulted_int = 1;
10266 /* If we just have "complex", it is equivalent to "complex double". */
10267 else if (!longlong && !explicit_intN
10268 && decl_spec_seq_has_spec_p (declspecs, ds_complex))
10270 type = double_type_node;
10271 pedwarn (declspecs->locations[ds_complex], OPT_Wpedantic,
10272 "ISO C++ does not support plain %<complex%> meaning "
10273 "%<double complex%>");
10276 /* Gather flags. */
10277 explicit_int = declspecs->explicit_int_p;
10278 explicit_char = declspecs->explicit_char_p;
10280 #if 0
10281 /* See the code below that used this. */
10282 if (typedef_decl)
10283 decl_attr = DECL_ATTRIBUTES (typedef_decl);
10284 #endif
10285 typedef_type = type;
10288 if (sfk != sfk_conversion)
10289 ctor_return_type = ctype;
10291 if (sfk != sfk_none)
10293 type = check_special_function_return_type (sfk, type,
10294 ctor_return_type,
10295 type_quals,
10296 declspecs->locations);
10297 type_quals = TYPE_UNQUALIFIED;
10299 else if (type == NULL_TREE)
10301 int is_main;
10303 explicit_int = -1;
10305 /* We handle `main' specially here, because 'main () { }' is so
10306 common. With no options, it is allowed. With -Wreturn-type,
10307 it is a warning. It is only an error with -pedantic-errors. */
10308 is_main = (funcdef_flag
10309 && dname && identifier_p (dname)
10310 && MAIN_NAME_P (dname)
10311 && ctype == NULL_TREE
10312 && in_namespace == NULL_TREE
10313 && current_namespace == global_namespace);
10315 if (type_was_error_mark_node)
10316 /* We've already issued an error, don't complain more. */;
10317 else if (in_system_header_at (input_location) || flag_ms_extensions)
10318 /* Allow it, sigh. */;
10319 else if (! is_main)
10320 permerror (input_location, "ISO C++ forbids declaration of %qs with no type", name);
10321 else if (pedantic)
10322 pedwarn (input_location, OPT_Wpedantic,
10323 "ISO C++ forbids declaration of %qs with no type", name);
10324 else
10325 warning (OPT_Wreturn_type,
10326 "ISO C++ forbids declaration of %qs with no type", name);
10328 if (type_was_error_mark_node && template_parm_flag)
10329 /* FIXME we should be able to propagate the error_mark_node as is
10330 for other contexts too. */
10331 type = error_mark_node;
10332 else
10333 type = integer_type_node;
10336 ctype = NULL_TREE;
10338 if (explicit_intN)
10340 if (! int_n_enabled_p[declspecs->int_n_idx])
10342 error ("%<__int%d%> is not supported by this target",
10343 int_n_data[declspecs->int_n_idx].bitsize);
10344 explicit_intN = false;
10346 else if (pedantic && ! in_system_header_at (input_location))
10347 pedwarn (input_location, OPT_Wpedantic,
10348 "ISO C++ does not support %<__int%d%> for %qs",
10349 int_n_data[declspecs->int_n_idx].bitsize, name);
10352 /* Now process the modifiers that were specified
10353 and check for invalid combinations. */
10355 /* Long double is a special combination. */
10356 if (long_p && !longlong && TYPE_MAIN_VARIANT (type) == double_type_node)
10358 long_p = false;
10359 type = cp_build_qualified_type (long_double_type_node,
10360 cp_type_quals (type));
10363 /* Check all other uses of type modifiers. */
10365 if (unsigned_p || signed_p || long_p || short_p)
10367 int ok = 0;
10369 if ((signed_p || unsigned_p) && TREE_CODE (type) != INTEGER_TYPE)
10370 error ("%<signed%> or %<unsigned%> invalid for %qs", name);
10371 else if (signed_p && unsigned_p)
10372 error ("%<signed%> and %<unsigned%> specified together for %qs", name);
10373 else if (longlong && TREE_CODE (type) != INTEGER_TYPE)
10374 error ("%<long long%> invalid for %qs", name);
10375 else if (long_p && TREE_CODE (type) == REAL_TYPE)
10376 error ("%<long%> invalid for %qs", name);
10377 else if (short_p && TREE_CODE (type) == REAL_TYPE)
10378 error ("%<short%> invalid for %qs", name);
10379 else if ((long_p || short_p) && TREE_CODE (type) != INTEGER_TYPE)
10380 error ("%<long%> or %<short%> invalid for %qs", name);
10381 else if ((long_p || short_p || explicit_char || explicit_int) && explicit_intN)
10382 error ("%<long%>, %<int%>, %<short%>, or %<char%> invalid for %qs", name);
10383 else if ((long_p || short_p) && explicit_char)
10384 error ("%<long%> or %<short%> specified with char for %qs", name);
10385 else if (long_p && short_p)
10386 error ("%<long%> and %<short%> specified together for %qs", name);
10387 else if (type == char16_type_node || type == char32_type_node)
10389 if (signed_p || unsigned_p)
10390 error ("%<signed%> or %<unsigned%> invalid for %qs", name);
10391 else if (short_p || long_p)
10392 error ("%<short%> or %<long%> invalid for %qs", name);
10394 else
10396 ok = 1;
10397 if (!explicit_int && !defaulted_int && !explicit_char && !explicit_intN && pedantic)
10399 pedwarn (input_location, OPT_Wpedantic,
10400 "long, short, signed or unsigned used invalidly for %qs",
10401 name);
10402 if (flag_pedantic_errors)
10403 ok = 0;
10407 /* Discard the type modifiers if they are invalid. */
10408 if (! ok)
10410 unsigned_p = false;
10411 signed_p = false;
10412 long_p = false;
10413 short_p = false;
10414 longlong = 0;
10418 /* Decide whether an integer type is signed or not.
10419 Optionally treat bitfields as signed by default. */
10420 if (unsigned_p
10421 /* [class.bit]
10423 It is implementation-defined whether a plain (neither
10424 explicitly signed or unsigned) char, short, int, or long
10425 bit-field is signed or unsigned.
10427 Naturally, we extend this to long long as well. Note that
10428 this does not include wchar_t. */
10429 || (bitfield && !flag_signed_bitfields
10430 && !signed_p
10431 /* A typedef for plain `int' without `signed' can be
10432 controlled just like plain `int', but a typedef for
10433 `signed int' cannot be so controlled. */
10434 && !(typedef_decl
10435 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl))
10436 && TREE_CODE (type) == INTEGER_TYPE
10437 && !same_type_p (TYPE_MAIN_VARIANT (type), wchar_type_node)))
10439 if (explicit_intN)
10440 type = int_n_trees[declspecs->int_n_idx].unsigned_type;
10441 else if (longlong)
10442 type = long_long_unsigned_type_node;
10443 else if (long_p)
10444 type = long_unsigned_type_node;
10445 else if (short_p)
10446 type = short_unsigned_type_node;
10447 else if (type == char_type_node)
10448 type = unsigned_char_type_node;
10449 else if (typedef_decl)
10450 type = unsigned_type_for (type);
10451 else
10452 type = unsigned_type_node;
10454 else if (signed_p && type == char_type_node)
10455 type = signed_char_type_node;
10456 else if (explicit_intN)
10457 type = int_n_trees[declspecs->int_n_idx].signed_type;
10458 else if (longlong)
10459 type = long_long_integer_type_node;
10460 else if (long_p)
10461 type = long_integer_type_node;
10462 else if (short_p)
10463 type = short_integer_type_node;
10465 if (decl_spec_seq_has_spec_p (declspecs, ds_complex))
10467 if (TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
10468 error ("complex invalid for %qs", name);
10469 /* If a modifier is specified, the resulting complex is the complex
10470 form of TYPE. E.g, "complex short" is "complex short int". */
10471 else if (type == integer_type_node)
10472 type = complex_integer_type_node;
10473 else if (type == float_type_node)
10474 type = complex_float_type_node;
10475 else if (type == double_type_node)
10476 type = complex_double_type_node;
10477 else if (type == long_double_type_node)
10478 type = complex_long_double_type_node;
10479 else
10480 type = build_complex_type (type);
10483 /* If we're using the injected-class-name to form a compound type or a
10484 declaration, replace it with the underlying class so we don't get
10485 redundant typedefs in the debug output. But if we are returning the
10486 type unchanged, leave it alone so that it's available to
10487 maybe_get_template_decl_from_type_decl. */
10488 if (CLASS_TYPE_P (type)
10489 && DECL_SELF_REFERENCE_P (TYPE_NAME (type))
10490 && type == TREE_TYPE (TYPE_NAME (type))
10491 && (declarator || type_quals))
10492 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
10494 type_quals |= cp_type_quals (type);
10495 type = cp_build_qualified_type_real
10496 (type, type_quals, ((((typedef_decl && !DECL_ARTIFICIAL (typedef_decl))
10497 || declspecs->decltype_p)
10498 ? tf_ignore_bad_quals : 0) | tf_warning_or_error));
10499 /* We might have ignored or rejected some of the qualifiers. */
10500 type_quals = cp_type_quals (type);
10502 if (cxx_dialect >= cxx1z && type && is_auto (type)
10503 && innermost_code != cdk_function
10504 && id_declarator && declarator != id_declarator)
10505 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (type))
10507 error_at (typespec_loc, "template placeholder type %qT must be followed "
10508 "by a simple declarator-id", type);
10509 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here", tmpl);
10512 staticp = 0;
10513 inlinep = decl_spec_seq_has_spec_p (declspecs, ds_inline);
10514 virtualp = decl_spec_seq_has_spec_p (declspecs, ds_virtual);
10515 explicitp = decl_spec_seq_has_spec_p (declspecs, ds_explicit);
10517 storage_class = declspecs->storage_class;
10518 if (storage_class == sc_static)
10519 staticp = 1 + (decl_context == FIELD);
10521 if (virtualp)
10523 if (staticp == 2)
10525 error ("member %qD cannot be declared both %<virtual%> "
10526 "and %<static%>", dname);
10527 storage_class = sc_none;
10528 staticp = 0;
10530 if (constexpr_p)
10531 error ("member %qD cannot be declared both %<virtual%> "
10532 "and %<constexpr%>", dname);
10534 friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
10536 /* Issue errors about use of storage classes for parameters. */
10537 if (decl_context == PARM)
10539 if (typedef_p)
10541 error ("typedef declaration invalid in parameter declaration");
10542 return error_mark_node;
10544 else if (template_parm_flag && storage_class != sc_none)
10546 error ("storage class specified for template parameter %qs", name);
10547 return error_mark_node;
10549 else if (storage_class == sc_static
10550 || storage_class == sc_extern
10551 || thread_p)
10552 error ("storage class specifiers invalid in parameter declarations");
10554 /* Function parameters cannot be concept. */
10555 if (concept_p)
10556 error ("a parameter cannot be declared %<concept%>");
10557 /* Function parameters cannot be constexpr. If we saw one, moan
10558 and pretend it wasn't there. */
10559 else if (constexpr_p)
10561 error ("a parameter cannot be declared %<constexpr%>");
10562 constexpr_p = 0;
10566 /* Give error if `virtual' is used outside of class declaration. */
10567 if (virtualp
10568 && (current_class_name == NULL_TREE || decl_context != FIELD))
10570 error_at (declspecs->locations[ds_virtual],
10571 "%<virtual%> outside class declaration");
10572 virtualp = 0;
10575 if (innermost_code == cdk_decomp)
10577 location_t loc = (declarator->kind == cdk_reference
10578 ? declarator->declarator->id_loc : declarator->id_loc);
10579 if (inlinep)
10580 error_at (declspecs->locations[ds_inline],
10581 "decomposition declaration cannot be declared %<inline%>");
10582 if (typedef_p)
10583 error_at (declspecs->locations[ds_typedef],
10584 "decomposition declaration cannot be declared %<typedef%>");
10585 if (constexpr_p)
10586 error_at (declspecs->locations[ds_constexpr], "decomposition "
10587 "declaration cannot be declared %<constexpr%>");
10588 if (thread_p)
10589 error_at (declspecs->locations[ds_thread],
10590 "decomposition declaration cannot be declared %qs",
10591 declspecs->gnu_thread_keyword_p
10592 ? "__thread" : "thread_local");
10593 if (concept_p)
10594 error_at (declspecs->locations[ds_concept],
10595 "decomposition declaration cannot be declared %<concept%>");
10596 switch (storage_class)
10598 case sc_none:
10599 break;
10600 case sc_register:
10601 error_at (loc, "decomposition declaration cannot be declared "
10602 "%<register%>");
10603 break;
10604 case sc_static:
10605 error_at (loc, "decomposition declaration cannot be declared "
10606 "%<static%>");
10607 break;
10608 case sc_extern:
10609 error_at (loc, "decomposition declaration cannot be declared "
10610 "%<extern%>");
10611 break;
10612 case sc_mutable:
10613 error_at (loc, "decomposition declaration cannot be declared "
10614 "%<mutable%>");
10615 break;
10616 case sc_auto:
10617 error_at (loc, "decomposition declaration cannot be declared "
10618 "C++98 %<auto%>");
10619 break;
10620 default:
10621 gcc_unreachable ();
10623 if (TREE_CODE (type) != TEMPLATE_TYPE_PARM
10624 || TYPE_IDENTIFIER (type) != auto_identifier)
10626 if (type != error_mark_node)
10628 error_at (loc, "decomposition declaration cannot be declared "
10629 "with type %qT", type);
10630 inform (loc,
10631 "type must be cv-qualified %<auto%> or reference to "
10632 "cv-qualified %<auto%>");
10634 type = build_qualified_type (make_auto (), type_quals);
10635 declspecs->type = type;
10637 inlinep = 0;
10638 typedef_p = 0;
10639 constexpr_p = 0;
10640 thread_p = 0;
10641 concept_p = 0;
10642 storage_class = sc_none;
10643 staticp = 0;
10644 declspecs->storage_class = sc_none;
10645 declspecs->locations[ds_thread] = UNKNOWN_LOCATION;
10648 /* Static anonymous unions are dealt with here. */
10649 if (staticp && decl_context == TYPENAME
10650 && declspecs->type
10651 && ANON_AGGR_TYPE_P (declspecs->type))
10652 decl_context = FIELD;
10654 /* Warn about storage classes that are invalid for certain
10655 kinds of declarations (parameters, typenames, etc.). */
10656 if (thread_p
10657 && ((storage_class
10658 && storage_class != sc_extern
10659 && storage_class != sc_static)
10660 || typedef_p))
10662 error ("multiple storage classes in declaration of %qs", name);
10663 thread_p = false;
10665 if (decl_context != NORMAL
10666 && ((storage_class != sc_none
10667 && storage_class != sc_mutable)
10668 || thread_p))
10670 if ((decl_context == PARM || decl_context == CATCHPARM)
10671 && (storage_class == sc_register
10672 || storage_class == sc_auto))
10674 else if (typedef_p)
10676 else if (decl_context == FIELD
10677 /* C++ allows static class elements. */
10678 && storage_class == sc_static)
10679 /* C++ also allows inlines and signed and unsigned elements,
10680 but in those cases we don't come in here. */
10682 else
10684 if (decl_context == FIELD)
10685 error ("storage class specified for %qs", name);
10686 else
10688 if (decl_context == PARM || decl_context == CATCHPARM)
10689 error ("storage class specified for parameter %qs", name);
10690 else
10691 error ("storage class specified for typename");
10693 if (storage_class == sc_register
10694 || storage_class == sc_auto
10695 || storage_class == sc_extern
10696 || thread_p)
10697 storage_class = sc_none;
10700 else if (storage_class == sc_extern && funcdef_flag
10701 && ! toplevel_bindings_p ())
10702 error ("nested function %qs declared %<extern%>", name);
10703 else if (toplevel_bindings_p ())
10705 if (storage_class == sc_auto)
10706 error ("top-level declaration of %qs specifies %<auto%>", name);
10708 else if (thread_p
10709 && storage_class != sc_extern
10710 && storage_class != sc_static)
10712 if (declspecs->gnu_thread_keyword_p)
10713 pedwarn (input_location, 0, "function-scope %qs implicitly auto and "
10714 "declared %<__thread%>", name);
10716 /* When thread_local is applied to a variable of block scope the
10717 storage-class-specifier static is implied if it does not appear
10718 explicitly. */
10719 storage_class = declspecs->storage_class = sc_static;
10720 staticp = 1;
10723 if (storage_class && friendp)
10725 error ("storage class specifiers invalid in friend function declarations");
10726 storage_class = sc_none;
10727 staticp = 0;
10730 if (!id_declarator)
10731 unqualified_id = NULL_TREE;
10732 else
10734 unqualified_id = id_declarator->u.id.unqualified_name;
10735 switch (TREE_CODE (unqualified_id))
10737 case BIT_NOT_EXPR:
10738 unqualified_id = TREE_OPERAND (unqualified_id, 0);
10739 if (TYPE_P (unqualified_id))
10740 unqualified_id = constructor_name (unqualified_id);
10741 break;
10743 case IDENTIFIER_NODE:
10744 case TEMPLATE_ID_EXPR:
10745 break;
10747 default:
10748 gcc_unreachable ();
10752 if (declspecs->std_attributes)
10754 /* Apply the c++11 attributes to the type preceding them. */
10755 input_location = declspecs->locations[ds_std_attribute];
10756 decl_attributes (&type, declspecs->std_attributes, 0);
10757 input_location = saved_loc;
10760 /* Determine the type of the entity declared by recurring on the
10761 declarator. */
10762 for (; declarator; declarator = declarator->declarator)
10764 const cp_declarator *inner_declarator;
10765 tree attrs;
10767 if (type == error_mark_node)
10768 return error_mark_node;
10770 attrs = declarator->attributes;
10771 if (attrs)
10773 int attr_flags;
10775 attr_flags = 0;
10776 if (declarator == NULL || declarator->kind == cdk_id)
10777 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
10778 if (declarator->kind == cdk_function)
10779 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
10780 if (declarator->kind == cdk_array)
10781 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
10782 returned_attrs = decl_attributes (&type,
10783 chainon (returned_attrs, attrs),
10784 attr_flags);
10787 if (declarator->kind == cdk_id || declarator->kind == cdk_decomp)
10788 break;
10790 inner_declarator = declarator->declarator;
10792 switch (declarator->kind)
10794 case cdk_array:
10795 type = create_array_type_for_decl (dname, type,
10796 declarator->u.array.bounds);
10797 if (!valid_array_size_p (input_location, type, dname))
10798 type = error_mark_node;
10800 if (declarator->std_attributes)
10801 /* [dcl.array]/1:
10803 The optional attribute-specifier-seq appertains to the
10804 array. */
10805 returned_attrs = chainon (returned_attrs,
10806 declarator->std_attributes);
10807 break;
10809 case cdk_function:
10811 tree arg_types;
10812 int funcdecl_p;
10814 /* Declaring a function type.
10815 Make sure we have a valid type for the function to return. */
10817 if (type_quals != TYPE_UNQUALIFIED)
10819 if (SCALAR_TYPE_P (type) || VOID_TYPE_P (type))
10821 warning_at (typespec_loc, OPT_Wignored_qualifiers, "type "
10822 "qualifiers ignored on function return type");
10824 /* We now know that the TYPE_QUALS don't apply to the
10825 decl, but to its return type. */
10826 type_quals = TYPE_UNQUALIFIED;
10829 /* Error about some types functions can't return. */
10831 if (TREE_CODE (type) == FUNCTION_TYPE)
10833 error ("%qs declared as function returning a function", name);
10834 return error_mark_node;
10836 if (TREE_CODE (type) == ARRAY_TYPE)
10838 error ("%qs declared as function returning an array", name);
10839 return error_mark_node;
10842 input_location = declspecs->locations[ds_type_spec];
10843 abstract_virtuals_error (ACU_RETURN, type);
10844 input_location = saved_loc;
10846 /* Pick up type qualifiers which should be applied to `this'. */
10847 memfn_quals = declarator->u.function.qualifiers;
10848 /* Pick up virt-specifiers. */
10849 virt_specifiers = declarator->u.function.virt_specifiers;
10850 /* And ref-qualifier, too */
10851 rqual = declarator->u.function.ref_qualifier;
10852 /* And tx-qualifier. */
10853 tree tx_qual = declarator->u.function.tx_qualifier;
10854 /* Pick up the exception specifications. */
10855 raises = declarator->u.function.exception_specification;
10856 /* If the exception-specification is ill-formed, let's pretend
10857 there wasn't one. */
10858 if (raises == error_mark_node)
10859 raises = NULL_TREE;
10861 if (reqs)
10862 error_at (location_of (reqs), "requires-clause on return type");
10863 reqs = declarator->u.function.requires_clause;
10865 /* Say it's a definition only for the CALL_EXPR
10866 closest to the identifier. */
10867 funcdecl_p = inner_declarator && inner_declarator->kind == cdk_id;
10869 /* Handle a late-specified return type. */
10870 tree late_return_type = declarator->u.function.late_return_type;
10871 if (funcdecl_p)
10873 if (tree auto_node = type_uses_auto (type))
10875 if (!late_return_type)
10877 if (current_class_type
10878 && LAMBDA_TYPE_P (current_class_type))
10879 /* OK for C++11 lambdas. */;
10880 else if (cxx_dialect < cxx14)
10882 error ("%qs function uses "
10883 "%<auto%> type specifier without trailing "
10884 "return type", name);
10885 inform (input_location, "deduced return type "
10886 "only available with -std=c++14 or "
10887 "-std=gnu++14");
10889 else if (virtualp)
10891 error ("virtual function cannot "
10892 "have deduced return type");
10893 virtualp = false;
10896 else if (!is_auto (type) && sfk != sfk_conversion)
10898 error ("%qs function with trailing return type has"
10899 " %qT as its type rather than plain %<auto%>",
10900 name, type);
10901 return error_mark_node;
10903 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
10905 if (!late_return_type)
10907 if (dguide_name_p (unqualified_id))
10908 error_at (typespec_loc, "deduction guide for "
10909 "%qT must have trailing return type",
10910 TREE_TYPE (tmpl));
10911 else
10912 error_at (typespec_loc, "deduced class type %qT "
10913 "in function return type", type);
10914 inform (DECL_SOURCE_LOCATION (tmpl),
10915 "%qD declared here", tmpl);
10917 else if (CLASS_TYPE_P (late_return_type)
10918 && CLASSTYPE_TEMPLATE_INFO (late_return_type)
10919 && (CLASSTYPE_TI_TEMPLATE (late_return_type)
10920 == tmpl))
10921 /* OK */;
10922 else
10923 error ("trailing return type %qT of deduction guide "
10924 "is not a specialization of %qT",
10925 late_return_type, TREE_TYPE (tmpl));
10928 else if (late_return_type
10929 && sfk != sfk_conversion)
10931 if (cxx_dialect < cxx11)
10932 /* Not using maybe_warn_cpp0x because this should
10933 always be an error. */
10934 error ("trailing return type only available with "
10935 "-std=c++11 or -std=gnu++11");
10936 else
10937 error ("%qs function with trailing return type not "
10938 "declared with %<auto%> type specifier", name);
10939 return error_mark_node;
10942 type = splice_late_return_type (type, late_return_type);
10943 if (type == error_mark_node)
10944 return error_mark_node;
10946 if (late_return_type)
10947 late_return_type_p = true;
10949 if (ctype == NULL_TREE
10950 && decl_context == FIELD
10951 && funcdecl_p
10952 && friendp == 0)
10953 ctype = current_class_type;
10955 if (ctype && (sfk == sfk_constructor
10956 || sfk == sfk_destructor))
10958 /* We are within a class's scope. If our declarator name
10959 is the same as the class name, and we are defining
10960 a function, then it is a constructor/destructor, and
10961 therefore returns a void type. */
10963 /* ISO C++ 12.4/2. A destructor may not be declared
10964 const or volatile. A destructor may not be static.
10965 A destructor may not be declared with ref-qualifier.
10967 ISO C++ 12.1. A constructor may not be declared
10968 const or volatile. A constructor may not be
10969 virtual. A constructor may not be static.
10970 A constructor may not be declared with ref-qualifier. */
10971 if (staticp == 2)
10972 error ((flags == DTOR_FLAG)
10973 ? G_("destructor cannot be static member function")
10974 : G_("constructor cannot be static member function"));
10975 if (memfn_quals)
10977 error ((flags == DTOR_FLAG)
10978 ? G_("destructors may not be cv-qualified")
10979 : G_("constructors may not be cv-qualified"));
10980 memfn_quals = TYPE_UNQUALIFIED;
10983 if (rqual)
10985 maybe_warn_cpp0x (CPP0X_REF_QUALIFIER);
10986 error ((flags == DTOR_FLAG)
10987 ? "destructors may not be ref-qualified"
10988 : "constructors may not be ref-qualified");
10989 rqual = REF_QUAL_NONE;
10992 if (decl_context == FIELD
10993 && !member_function_or_else (ctype,
10994 current_class_type,
10995 flags))
10996 return error_mark_node;
10998 if (flags != DTOR_FLAG)
11000 /* It's a constructor. */
11001 if (explicitp == 1)
11002 explicitp = 2;
11003 if (virtualp)
11005 permerror (input_location,
11006 "constructors cannot be declared %<virtual%>");
11007 virtualp = 0;
11009 if (decl_context == FIELD
11010 && sfk != sfk_constructor)
11011 return error_mark_node;
11013 if (decl_context == FIELD)
11014 staticp = 0;
11016 else if (friendp)
11018 if (virtualp)
11020 /* Cannot be both friend and virtual. */
11021 error ("virtual functions cannot be friends");
11022 friendp = 0;
11024 if (decl_context == NORMAL)
11025 error ("friend declaration not in class definition");
11026 if (current_function_decl && funcdef_flag)
11027 error ("can%'t define friend function %qs in a local "
11028 "class definition",
11029 name);
11031 else if (ctype && sfk == sfk_conversion)
11033 if (explicitp == 1)
11035 maybe_warn_cpp0x (CPP0X_EXPLICIT_CONVERSION);
11036 explicitp = 2;
11038 if (late_return_type_p)
11039 error ("a conversion function cannot have a trailing return type");
11042 arg_types = grokparms (declarator->u.function.parameters,
11043 &parms);
11045 if (inner_declarator
11046 && inner_declarator->kind == cdk_id
11047 && inner_declarator->u.id.sfk == sfk_destructor
11048 && arg_types != void_list_node)
11050 error ("destructors may not have parameters");
11051 arg_types = void_list_node;
11052 parms = NULL_TREE;
11055 type = build_function_type (type, arg_types);
11057 tree attrs = declarator->std_attributes;
11058 if (tx_qual)
11060 tree att = build_tree_list (tx_qual, NULL_TREE);
11061 /* transaction_safe applies to the type, but
11062 transaction_safe_dynamic applies to the function. */
11063 if (is_attribute_p ("transaction_safe", tx_qual))
11064 attrs = chainon (attrs, att);
11065 else
11066 returned_attrs = chainon (returned_attrs, att);
11068 if (attrs)
11069 /* [dcl.fct]/2:
11071 The optional attribute-specifier-seq appertains to
11072 the function type. */
11073 decl_attributes (&type, attrs, 0);
11075 if (raises)
11076 type = build_exception_variant (type, raises);
11078 break;
11080 case cdk_pointer:
11081 case cdk_reference:
11082 case cdk_ptrmem:
11083 /* Filter out pointers-to-references and references-to-references.
11084 We can get these if a TYPE_DECL is used. */
11086 if (TREE_CODE (type) == REFERENCE_TYPE)
11088 if (declarator->kind != cdk_reference)
11090 error ("cannot declare pointer to %q#T", type);
11091 type = TREE_TYPE (type);
11094 /* In C++0x, we allow reference to reference declarations
11095 that occur indirectly through typedefs [7.1.3/8 dcl.typedef]
11096 and template type arguments [14.3.1/4 temp.arg.type]. The
11097 check for direct reference to reference declarations, which
11098 are still forbidden, occurs below. Reasoning behind the change
11099 can be found in DR106, DR540, and the rvalue reference
11100 proposals. */
11101 else if (cxx_dialect == cxx98)
11103 error ("cannot declare reference to %q#T", type);
11104 type = TREE_TYPE (type);
11107 else if (VOID_TYPE_P (type))
11109 if (declarator->kind == cdk_reference)
11110 error ("cannot declare reference to %q#T", type);
11111 else if (declarator->kind == cdk_ptrmem)
11112 error ("cannot declare pointer to %q#T member", type);
11115 /* We now know that the TYPE_QUALS don't apply to the decl,
11116 but to the target of the pointer. */
11117 type_quals = TYPE_UNQUALIFIED;
11119 /* This code used to handle METHOD_TYPE, but I don't think it's
11120 possible to get it here anymore. */
11121 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
11122 if (declarator->kind == cdk_ptrmem
11123 && TREE_CODE (type) == FUNCTION_TYPE)
11125 memfn_quals |= type_memfn_quals (type);
11126 type = build_memfn_type (type,
11127 declarator->u.pointer.class_type,
11128 memfn_quals,
11129 rqual);
11130 if (type == error_mark_node)
11131 return error_mark_node;
11133 rqual = REF_QUAL_NONE;
11134 memfn_quals = TYPE_UNQUALIFIED;
11137 if (TREE_CODE (type) == FUNCTION_TYPE
11138 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
11139 || type_memfn_rqual (type) != REF_QUAL_NONE))
11140 error (declarator->kind == cdk_reference
11141 ? G_("cannot declare reference to qualified function type %qT")
11142 : G_("cannot declare pointer to qualified function type %qT"),
11143 type);
11145 /* When the pointed-to type involves components of variable size,
11146 care must be taken to ensure that the size evaluation code is
11147 emitted early enough to dominate all the possible later uses
11148 and late enough for the variables on which it depends to have
11149 been assigned.
11151 This is expected to happen automatically when the pointed-to
11152 type has a name/declaration of it's own, but special attention
11153 is required if the type is anonymous.
11155 We handle the NORMAL and FIELD contexts here by inserting a
11156 dummy statement that just evaluates the size at a safe point
11157 and ensures it is not deferred until e.g. within a deeper
11158 conditional context (c++/43555).
11160 We expect nothing to be needed here for PARM or TYPENAME.
11161 Evaluating the size at this point for TYPENAME would
11162 actually be incorrect, as we might be in the middle of an
11163 expression with side effects on the pointed-to type size
11164 "arguments" prior to the pointer declaration point and the
11165 size evaluation could end up prior to the side effects. */
11167 if (!TYPE_NAME (type)
11168 && (decl_context == NORMAL || decl_context == FIELD)
11169 && at_function_scope_p ()
11170 && variably_modified_type_p (type, NULL_TREE))
11172 TYPE_NAME (type) = build_decl (UNKNOWN_LOCATION, TYPE_DECL,
11173 NULL_TREE, type);
11174 add_decl_expr (TYPE_NAME (type));
11177 if (declarator->kind == cdk_reference)
11179 /* In C++0x, the type we are creating a reference to might be
11180 a typedef which is itself a reference type. In that case,
11181 we follow the reference collapsing rules in
11182 [7.1.3/8 dcl.typedef] to create the final reference type:
11184 "If a typedef TD names a type that is a reference to a type
11185 T, an attempt to create the type 'lvalue reference to cv TD'
11186 creates the type 'lvalue reference to T,' while an attempt
11187 to create the type "rvalue reference to cv TD' creates the
11188 type TD."
11190 if (VOID_TYPE_P (type))
11191 /* We already gave an error. */;
11192 else if (TREE_CODE (type) == REFERENCE_TYPE)
11194 if (declarator->u.reference.rvalue_ref)
11195 /* Leave type alone. */;
11196 else
11197 type = cp_build_reference_type (TREE_TYPE (type), false);
11199 else
11200 type = cp_build_reference_type
11201 (type, declarator->u.reference.rvalue_ref);
11203 /* In C++0x, we need this check for direct reference to
11204 reference declarations, which are forbidden by
11205 [8.3.2/5 dcl.ref]. Reference to reference declarations
11206 are only allowed indirectly through typedefs and template
11207 type arguments. Example:
11209 void foo(int & &); // invalid ref-to-ref decl
11211 typedef int & int_ref;
11212 void foo(int_ref &); // valid ref-to-ref decl
11214 if (inner_declarator && inner_declarator->kind == cdk_reference)
11215 error ("cannot declare reference to %q#T, which is not "
11216 "a typedef or a template type argument", type);
11218 else if (TREE_CODE (type) == METHOD_TYPE)
11219 type = build_ptrmemfunc_type (build_pointer_type (type));
11220 else if (declarator->kind == cdk_ptrmem)
11222 gcc_assert (TREE_CODE (declarator->u.pointer.class_type)
11223 != NAMESPACE_DECL);
11224 if (declarator->u.pointer.class_type == error_mark_node)
11225 /* We will already have complained. */
11226 type = error_mark_node;
11227 else
11228 type = build_ptrmem_type (declarator->u.pointer.class_type,
11229 type);
11231 else
11232 type = build_pointer_type (type);
11234 /* Process a list of type modifier keywords (such as
11235 const or volatile) that were given inside the `*' or `&'. */
11237 if (declarator->u.pointer.qualifiers)
11239 type
11240 = cp_build_qualified_type (type,
11241 declarator->u.pointer.qualifiers);
11242 type_quals = cp_type_quals (type);
11245 /* Apply C++11 attributes to the pointer, and not to the
11246 type pointed to. This is unlike what is done for GNU
11247 attributes above. It is to comply with [dcl.ptr]/1:
11249 [the optional attribute-specifier-seq (7.6.1) appertains
11250 to the pointer and not to the object pointed to]. */
11251 if (declarator->std_attributes)
11252 decl_attributes (&type, declarator->std_attributes,
11255 ctype = NULL_TREE;
11256 break;
11258 case cdk_error:
11259 break;
11261 default:
11262 gcc_unreachable ();
11266 /* A `constexpr' specifier used in an object declaration declares
11267 the object as `const'. */
11268 if (constexpr_p && innermost_code != cdk_function)
11270 /* DR1688 says that a `constexpr' specifier in combination with
11271 `volatile' is valid. */
11273 if (TREE_CODE (type) != REFERENCE_TYPE)
11275 type_quals |= TYPE_QUAL_CONST;
11276 type = cp_build_qualified_type (type, type_quals);
11280 if (unqualified_id && TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR
11281 && TREE_CODE (type) != FUNCTION_TYPE
11282 && TREE_CODE (type) != METHOD_TYPE
11283 && !variable_template_p (TREE_OPERAND (unqualified_id, 0)))
11285 error ("template-id %qD used as a declarator",
11286 unqualified_id);
11287 unqualified_id = dname;
11290 /* If TYPE is a FUNCTION_TYPE, but the function name was explicitly
11291 qualified with a class-name, turn it into a METHOD_TYPE, unless
11292 we know that the function is static. We take advantage of this
11293 opportunity to do other processing that pertains to entities
11294 explicitly declared to be class members. Note that if DECLARATOR
11295 is non-NULL, we know it is a cdk_id declarator; otherwise, we
11296 would not have exited the loop above. */
11297 if (declarator
11298 && declarator->kind == cdk_id
11299 && declarator->u.id.qualifying_scope
11300 && MAYBE_CLASS_TYPE_P (declarator->u.id.qualifying_scope))
11302 ctype = declarator->u.id.qualifying_scope;
11303 ctype = TYPE_MAIN_VARIANT (ctype);
11304 template_count = num_template_headers_for_class (ctype);
11306 if (ctype == current_class_type)
11308 if (friendp)
11310 permerror (input_location, "member functions are implicitly "
11311 "friends of their class");
11312 friendp = 0;
11314 else
11315 permerror (declarator->id_loc,
11316 "extra qualification %<%T::%> on member %qs",
11317 ctype, name);
11319 else if (/* If the qualifying type is already complete, then we
11320 can skip the following checks. */
11321 !COMPLETE_TYPE_P (ctype)
11322 && (/* If the function is being defined, then
11323 qualifying type must certainly be complete. */
11324 funcdef_flag
11325 /* A friend declaration of "T::f" is OK, even if
11326 "T" is a template parameter. But, if this
11327 function is not a friend, the qualifying type
11328 must be a class. */
11329 || (!friendp && !CLASS_TYPE_P (ctype))
11330 /* For a declaration, the type need not be
11331 complete, if either it is dependent (since there
11332 is no meaningful definition of complete in that
11333 case) or the qualifying class is currently being
11334 defined. */
11335 || !(dependent_type_p (ctype)
11336 || currently_open_class (ctype)))
11337 /* Check that the qualifying type is complete. */
11338 && !complete_type_or_else (ctype, NULL_TREE))
11339 return error_mark_node;
11340 else if (TREE_CODE (type) == FUNCTION_TYPE)
11342 if (current_class_type
11343 && (!friendp || funcdef_flag))
11345 error (funcdef_flag
11346 ? G_("cannot define member function %<%T::%s%> "
11347 "within %<%T%>")
11348 : G_("cannot declare member function %<%T::%s%> "
11349 "within %<%T%>"),
11350 ctype, name, current_class_type);
11351 return error_mark_node;
11354 else if (typedef_p && current_class_type)
11356 error ("cannot declare member %<%T::%s%> within %qT",
11357 ctype, name, current_class_type);
11358 return error_mark_node;
11362 if (ctype == NULL_TREE && decl_context == FIELD && friendp == 0)
11363 ctype = current_class_type;
11365 /* Now TYPE has the actual type. */
11367 if (returned_attrs)
11369 if (attrlist)
11370 *attrlist = chainon (returned_attrs, *attrlist);
11371 else
11372 attrlist = &returned_attrs;
11375 if (declarator
11376 && declarator->kind == cdk_id
11377 && declarator->std_attributes)
11378 /* [dcl.meaning]/1: The optional attribute-specifier-seq following
11379 a declarator-id appertains to the entity that is declared. */
11380 *attrlist = chainon (*attrlist, declarator->std_attributes);
11382 /* Handle parameter packs. */
11383 if (parameter_pack_p)
11385 if (decl_context == PARM)
11386 /* Turn the type into a pack expansion.*/
11387 type = make_pack_expansion (type);
11388 else
11389 error ("non-parameter %qs cannot be a parameter pack", name);
11392 if ((decl_context == FIELD || decl_context == PARM)
11393 && !processing_template_decl
11394 && variably_modified_type_p (type, NULL_TREE))
11396 if (decl_context == FIELD)
11397 error ("data member may not have variably modified type %qT", type);
11398 else
11399 error ("parameter may not have variably modified type %qT", type);
11400 type = error_mark_node;
11403 if (explicitp == 1 || (explicitp && friendp))
11405 /* [dcl.fct.spec] (C++11) The explicit specifier shall be used only
11406 in the declaration of a constructor or conversion function within
11407 a class definition. */
11408 if (!current_class_type)
11409 error_at (declspecs->locations[ds_explicit],
11410 "%<explicit%> outside class declaration");
11411 else if (friendp)
11412 error_at (declspecs->locations[ds_explicit],
11413 "%<explicit%> in friend declaration");
11414 else
11415 error_at (declspecs->locations[ds_explicit],
11416 "only declarations of constructors and conversion operators "
11417 "can be %<explicit%>");
11418 explicitp = 0;
11421 if (storage_class == sc_mutable)
11423 if (decl_context != FIELD || friendp)
11425 error ("non-member %qs cannot be declared %<mutable%>", name);
11426 storage_class = sc_none;
11428 else if (decl_context == TYPENAME || typedef_p)
11430 error ("non-object member %qs cannot be declared %<mutable%>", name);
11431 storage_class = sc_none;
11433 else if (TREE_CODE (type) == FUNCTION_TYPE
11434 || TREE_CODE (type) == METHOD_TYPE)
11436 error ("function %qs cannot be declared %<mutable%>", name);
11437 storage_class = sc_none;
11439 else if (staticp)
11441 error ("static %qs cannot be declared %<mutable%>", name);
11442 storage_class = sc_none;
11444 else if (type_quals & TYPE_QUAL_CONST)
11446 error ("const %qs cannot be declared %<mutable%>", name);
11447 storage_class = sc_none;
11449 else if (TREE_CODE (type) == REFERENCE_TYPE)
11451 permerror (input_location, "reference %qs cannot be declared "
11452 "%<mutable%>", name);
11453 storage_class = sc_none;
11457 /* If this is declaring a typedef name, return a TYPE_DECL. */
11458 if (typedef_p && decl_context != TYPENAME)
11460 tree decl;
11462 /* This declaration:
11464 typedef void f(int) const;
11466 declares a function type which is not a member of any
11467 particular class, but which is cv-qualified; for
11468 example "f S::*" declares a pointer to a const-qualified
11469 member function of S. We record the cv-qualification in the
11470 function type. */
11471 if ((rqual || memfn_quals) && TREE_CODE (type) == FUNCTION_TYPE)
11473 type = apply_memfn_quals (type, memfn_quals, rqual);
11475 /* We have now dealt with these qualifiers. */
11476 memfn_quals = TYPE_UNQUALIFIED;
11477 rqual = REF_QUAL_NONE;
11480 if (type_uses_auto (type))
11482 error ("typedef declared %<auto%>");
11483 type = error_mark_node;
11486 if (reqs)
11487 error_at (location_of (reqs), "requires-clause on typedef");
11489 if (decl_context == FIELD)
11490 decl = build_lang_decl (TYPE_DECL, unqualified_id, type);
11491 else
11492 decl = build_decl (input_location, TYPE_DECL, unqualified_id, type);
11493 if (id_declarator && declarator->u.id.qualifying_scope) {
11494 error_at (DECL_SOURCE_LOCATION (decl),
11495 "typedef name may not be a nested-name-specifier");
11496 TREE_TYPE (decl) = error_mark_node;
11499 if (decl_context != FIELD)
11501 if (!current_function_decl)
11502 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
11503 else if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (current_function_decl)
11504 || (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P
11505 (current_function_decl)))
11506 /* The TYPE_DECL is "abstract" because there will be
11507 clones of this constructor/destructor, and there will
11508 be copies of this TYPE_DECL generated in those
11509 clones. The decloning optimization (for space) may
11510 revert this subsequently if it determines that
11511 the clones should share a common implementation. */
11512 DECL_ABSTRACT_P (decl) = true;
11514 else if (current_class_type
11515 && constructor_name_p (unqualified_id, current_class_type))
11516 permerror (input_location, "ISO C++ forbids nested type %qD with same name "
11517 "as enclosing class",
11518 unqualified_id);
11520 /* If the user declares "typedef struct {...} foo" then the
11521 struct will have an anonymous name. Fill that name in now.
11522 Nothing can refer to it, so nothing needs know about the name
11523 change. */
11524 if (type != error_mark_node
11525 && unqualified_id
11526 && TYPE_NAME (type)
11527 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
11528 && TYPE_UNNAMED_P (type)
11529 && declspecs->type_definition_p
11530 && attributes_naming_typedef_ok (*attrlist)
11531 && cp_type_quals (type) == TYPE_UNQUALIFIED)
11533 tree t;
11535 /* Replace the anonymous name with the real name everywhere. */
11536 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
11538 if (anon_aggrname_p (TYPE_IDENTIFIER (t)))
11539 /* We do not rename the debug info representing the
11540 unnamed tagged type because the standard says in
11541 [dcl.typedef] that the naming applies only for
11542 linkage purposes. */
11543 /*debug_hooks->set_name (t, decl);*/
11544 TYPE_NAME (t) = decl;
11547 if (TYPE_LANG_SPECIFIC (type))
11548 TYPE_WAS_UNNAMED (type) = 1;
11550 /* If this is a typedef within a template class, the nested
11551 type is a (non-primary) template. The name for the
11552 template needs updating as well. */
11553 if (TYPE_LANG_SPECIFIC (type) && CLASSTYPE_TEMPLATE_INFO (type))
11554 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type))
11555 = TYPE_IDENTIFIER (type);
11557 /* Adjust linkage now that we aren't unnamed anymore. */
11558 reset_type_linkage (type);
11560 /* FIXME remangle member functions; member functions of a
11561 type with external linkage have external linkage. */
11564 if (signed_p
11565 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
11566 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
11568 bad_specifiers (decl, BSP_TYPE, virtualp,
11569 memfn_quals != TYPE_UNQUALIFIED,
11570 inlinep, friendp, raises != NULL_TREE);
11572 if (decl_spec_seq_has_spec_p (declspecs, ds_alias))
11573 /* Acknowledge that this was written:
11574 `using analias = atype;'. */
11575 TYPE_DECL_ALIAS_P (decl) = 1;
11577 return decl;
11580 /* Detect the case of an array type of unspecified size
11581 which came, as such, direct from a typedef name.
11582 We must copy the type, so that the array's domain can be
11583 individually set by the object's initializer. */
11585 if (type && typedef_type
11586 && TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
11587 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
11588 type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
11590 /* Detect where we're using a typedef of function type to declare a
11591 function. PARMS will not be set, so we must create it now. */
11593 if (type == typedef_type && TREE_CODE (type) == FUNCTION_TYPE)
11595 tree decls = NULL_TREE;
11596 tree args;
11598 for (args = TYPE_ARG_TYPES (type);
11599 args && args != void_list_node;
11600 args = TREE_CHAIN (args))
11602 tree decl = cp_build_parm_decl (NULL_TREE, TREE_VALUE (args));
11604 DECL_CHAIN (decl) = decls;
11605 decls = decl;
11608 parms = nreverse (decls);
11610 if (decl_context != TYPENAME)
11612 /* The qualifiers on the function type become the qualifiers on
11613 the non-static member function. */
11614 memfn_quals |= type_memfn_quals (type);
11615 rqual = type_memfn_rqual (type);
11616 type_quals = TYPE_UNQUALIFIED;
11620 /* If this is a type name (such as, in a cast or sizeof),
11621 compute the type and return it now. */
11623 if (decl_context == TYPENAME)
11625 /* Note that here we don't care about type_quals. */
11627 /* Special case: "friend class foo" looks like a TYPENAME context. */
11628 if (friendp)
11630 if (inlinep)
11632 error ("%<inline%> specified for friend class declaration");
11633 inlinep = 0;
11636 if (!current_aggr)
11638 /* Don't allow friend declaration without a class-key. */
11639 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
11640 permerror (input_location, "template parameters cannot be friends");
11641 else if (TREE_CODE (type) == TYPENAME_TYPE)
11642 permerror (input_location, "friend declaration requires class-key, "
11643 "i.e. %<friend class %T::%D%>",
11644 TYPE_CONTEXT (type), TYPENAME_TYPE_FULLNAME (type));
11645 else
11646 permerror (input_location, "friend declaration requires class-key, "
11647 "i.e. %<friend %#T%>",
11648 type);
11651 /* Only try to do this stuff if we didn't already give up. */
11652 if (type != integer_type_node)
11654 /* A friendly class? */
11655 if (current_class_type)
11656 make_friend_class (current_class_type, TYPE_MAIN_VARIANT (type),
11657 /*complain=*/true);
11658 else
11659 error ("trying to make class %qT a friend of global scope",
11660 type);
11662 type = void_type_node;
11665 else if (memfn_quals || rqual)
11667 if (ctype == NULL_TREE
11668 && TREE_CODE (type) == METHOD_TYPE)
11669 ctype = TYPE_METHOD_BASETYPE (type);
11671 if (ctype)
11672 type = build_memfn_type (type, ctype, memfn_quals, rqual);
11673 /* Core issue #547: need to allow this in template type args.
11674 Allow it in general in C++11 for alias-declarations. */
11675 else if ((template_type_arg || cxx_dialect >= cxx11)
11676 && TREE_CODE (type) == FUNCTION_TYPE)
11677 type = apply_memfn_quals (type, memfn_quals, rqual);
11678 else
11679 error ("invalid qualifiers on non-member function type");
11682 if (reqs)
11683 error_at (location_of (reqs), "requires-clause on type-id");
11685 return type;
11687 else if (unqualified_id == NULL_TREE && decl_context != PARM
11688 && decl_context != CATCHPARM
11689 && TREE_CODE (type) != UNION_TYPE
11690 && ! bitfield
11691 && innermost_code != cdk_decomp)
11693 error ("abstract declarator %qT used as declaration", type);
11694 return error_mark_node;
11697 /* Only functions may be declared using an operator-function-id. */
11698 if (unqualified_id
11699 && IDENTIFIER_OPNAME_P (unqualified_id)
11700 && TREE_CODE (type) != FUNCTION_TYPE
11701 && TREE_CODE (type) != METHOD_TYPE)
11703 error ("declaration of %qD as non-function", unqualified_id);
11704 return error_mark_node;
11707 if (reqs
11708 && TREE_CODE (type) != FUNCTION_TYPE
11709 && TREE_CODE (type) != METHOD_TYPE)
11710 error_at (location_of (reqs),
11711 "requires-clause on declaration of non-function type %qT",
11712 type);
11714 /* We don't check parameter types here because we can emit a better
11715 error message later. */
11716 if (decl_context != PARM)
11718 type = check_var_type (unqualified_id, type);
11719 if (type == error_mark_node)
11720 return error_mark_node;
11723 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
11724 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
11726 if (decl_context == PARM || decl_context == CATCHPARM)
11728 if (ctype || in_namespace)
11729 error ("cannot use %<::%> in parameter declaration");
11731 if (type_uses_auto (type)
11732 && !(cxx_dialect >= cxx1z && template_parm_flag))
11734 if (cxx_dialect >= cxx14)
11735 error ("%<auto%> parameter not permitted in this context");
11736 else
11737 error ("parameter declared %<auto%>");
11738 type = error_mark_node;
11741 /* A parameter declared as an array of T is really a pointer to T.
11742 One declared as a function is really a pointer to a function.
11743 One declared as a member is really a pointer to member. */
11745 if (TREE_CODE (type) == ARRAY_TYPE)
11747 /* Transfer const-ness of array into that of type pointed to. */
11748 type = build_pointer_type (TREE_TYPE (type));
11749 type_quals = TYPE_UNQUALIFIED;
11750 array_parameter_p = true;
11752 else if (TREE_CODE (type) == FUNCTION_TYPE)
11753 type = build_pointer_type (type);
11756 if (ctype && TREE_CODE (type) == FUNCTION_TYPE && staticp < 2
11757 && !NEW_DELETE_OPNAME_P (unqualified_id))
11759 cp_cv_quals real_quals = memfn_quals;
11760 if (cxx_dialect < cxx14 && constexpr_p
11761 && sfk != sfk_constructor && sfk != sfk_destructor)
11762 real_quals |= TYPE_QUAL_CONST;
11763 type = build_memfn_type (type, ctype, real_quals, rqual);
11767 tree decl = NULL_TREE;
11769 if (decl_context == PARM)
11771 decl = cp_build_parm_decl (unqualified_id, type);
11772 DECL_ARRAY_PARAMETER_P (decl) = array_parameter_p;
11774 bad_specifiers (decl, BSP_PARM, virtualp,
11775 memfn_quals != TYPE_UNQUALIFIED,
11776 inlinep, friendp, raises != NULL_TREE);
11778 else if (decl_context == FIELD)
11780 if (!staticp && !friendp && TREE_CODE (type) != METHOD_TYPE)
11781 if (tree auto_node = type_uses_auto (type))
11783 location_t loc = declspecs->locations[ds_type_spec];
11784 if (CLASS_PLACEHOLDER_TEMPLATE (auto_node))
11785 error_at (loc, "invalid use of template-name %qE without an "
11786 "argument list",
11787 CLASS_PLACEHOLDER_TEMPLATE (auto_node));
11788 else
11789 error_at (loc, "non-static data member declared with "
11790 "placeholder %qT", auto_node);
11791 type = error_mark_node;
11794 /* The C99 flexible array extension. */
11795 if (!staticp && TREE_CODE (type) == ARRAY_TYPE
11796 && TYPE_DOMAIN (type) == NULL_TREE)
11798 if (ctype
11799 && (TREE_CODE (ctype) == UNION_TYPE
11800 || TREE_CODE (ctype) == QUAL_UNION_TYPE))
11802 error ("flexible array member in union");
11803 type = error_mark_node;
11805 else
11807 /* Array is a flexible member. */
11808 if (in_system_header_at (input_location))
11809 /* Do not warn on flexible array members in system
11810 headers because glibc uses them. */;
11811 else if (name)
11812 pedwarn (input_location, OPT_Wpedantic,
11813 "ISO C++ forbids flexible array member %<%s%>", name);
11814 else
11815 pedwarn (input_location, OPT_Wpedantic,
11816 "ISO C++ forbids flexible array members");
11818 /* Flexible array member has a null domain. */
11819 type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
11823 if (type == error_mark_node)
11825 /* Happens when declaring arrays of sizes which
11826 are error_mark_node, for example. */
11827 decl = NULL_TREE;
11829 else if (in_namespace && !friendp)
11831 /* Something like struct S { int N::j; }; */
11832 error ("invalid use of %<::%>");
11833 return error_mark_node;
11835 else if (TREE_CODE (type) == FUNCTION_TYPE
11836 || TREE_CODE (type) == METHOD_TYPE)
11838 int publicp = 0;
11839 tree function_context;
11841 if (friendp == 0)
11843 /* This should never happen in pure C++ (the check
11844 could be an assert). It could happen in
11845 Objective-C++ if someone writes invalid code that
11846 uses a function declaration for an instance
11847 variable or property (instance variables and
11848 properties are parsed as FIELD_DECLs, but they are
11849 part of an Objective-C class, not a C++ class).
11850 That code is invalid and is caught by this
11851 check. */
11852 if (!ctype)
11854 error ("declaration of function %qD in invalid context",
11855 unqualified_id);
11856 return error_mark_node;
11859 /* ``A union may [ ... ] not [ have ] virtual functions.''
11860 ARM 9.5 */
11861 if (virtualp && TREE_CODE (ctype) == UNION_TYPE)
11863 error ("function %qD declared %<virtual%> inside a union",
11864 unqualified_id);
11865 return error_mark_node;
11868 if (NEW_DELETE_OPNAME_P (unqualified_id))
11870 if (virtualp)
11872 error ("%qD cannot be declared %<virtual%>, since it "
11873 "is always static",
11874 unqualified_id);
11875 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 (thread_p)
12054 CP_DECL_THREAD_LOCAL_P (decl) = true;
12055 if (!processing_template_decl)
12056 set_decl_tls_model (decl, decl_default_tls_model (decl));
12057 if (declspecs->gnu_thread_keyword_p)
12058 SET_DECL_GNU_TLS_P (decl);
12060 if (concept_p)
12061 error ("static data member %qE declared %<concept%>",
12062 unqualified_id);
12063 else if (constexpr_p && !initialized)
12065 error ("constexpr static data member %qD must have an "
12066 "initializer", decl);
12067 constexpr_p = false;
12070 if (inlinep)
12071 mark_inline_variable (decl);
12073 if (!DECL_VAR_DECLARED_INLINE_P (decl)
12074 && !(cxx_dialect >= cxx1z && constexpr_p))
12075 /* Even if there is an in-class initialization, DECL
12076 is considered undefined until an out-of-class
12077 definition is provided, unless this is an inline
12078 variable. */
12079 DECL_EXTERNAL (decl) = 1;
12081 else
12083 if (concept_p)
12084 error ("non-static data member %qE declared %<concept%>",
12085 unqualified_id);
12086 else if (constexpr_p)
12088 error ("non-static data member %qE declared %<constexpr%>",
12089 unqualified_id);
12090 constexpr_p = false;
12092 decl = build_decl (input_location,
12093 FIELD_DECL, unqualified_id, type);
12094 DECL_NONADDRESSABLE_P (decl) = bitfield;
12095 if (bitfield && !unqualified_id)
12096 TREE_NO_WARNING (decl) = 1;
12098 if (storage_class == sc_mutable)
12100 DECL_MUTABLE_P (decl) = 1;
12101 storage_class = sc_none;
12104 if (initialized)
12106 /* An attempt is being made to initialize a non-static
12107 member. This is new in C++11. */
12108 maybe_warn_cpp0x (CPP0X_NSDMI);
12110 /* If this has been parsed with static storage class, but
12111 errors forced staticp to be cleared, ensure NSDMI is
12112 not present. */
12113 if (declspecs->storage_class == sc_static)
12114 DECL_INITIAL (decl) = error_mark_node;
12118 bad_specifiers (decl, BSP_FIELD, virtualp,
12119 memfn_quals != TYPE_UNQUALIFIED,
12120 staticp ? false : inlinep, friendp,
12121 raises != NULL_TREE);
12124 else if (TREE_CODE (type) == FUNCTION_TYPE
12125 || TREE_CODE (type) == METHOD_TYPE)
12127 tree original_name;
12128 int publicp = 0;
12130 if (!unqualified_id)
12131 return error_mark_node;
12133 if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
12134 original_name = dname;
12135 else
12136 original_name = unqualified_id;
12138 if (storage_class == sc_auto)
12139 error ("storage class %<auto%> invalid for function %qs", name);
12140 else if (storage_class == sc_register)
12141 error ("storage class %<register%> invalid for function %qs", name);
12142 else if (thread_p)
12144 if (declspecs->gnu_thread_keyword_p)
12145 error ("storage class %<__thread%> invalid for function %qs",
12146 name);
12147 else
12148 error ("storage class %<thread_local%> invalid for function %qs",
12149 name);
12152 if (virt_specifiers)
12153 error ("virt-specifiers in %qs not allowed outside a class definition", name);
12154 /* Function declaration not at top level.
12155 Storage classes other than `extern' are not allowed
12156 and `extern' makes no difference. */
12157 if (! toplevel_bindings_p ()
12158 && (storage_class == sc_static
12159 || decl_spec_seq_has_spec_p (declspecs, ds_inline))
12160 && pedantic)
12162 if (storage_class == sc_static)
12163 pedwarn (input_location, OPT_Wpedantic,
12164 "%<static%> specifier invalid for function %qs "
12165 "declared out of global scope", name);
12166 else
12167 pedwarn (input_location, OPT_Wpedantic,
12168 "%<inline%> specifier invalid for function %qs "
12169 "declared out of global scope", name);
12172 if (ctype == NULL_TREE)
12174 if (virtualp)
12176 error ("virtual non-class function %qs", name);
12177 virtualp = 0;
12179 else if (sfk == sfk_constructor
12180 || sfk == sfk_destructor)
12182 error (funcdef_flag
12183 ? G_("%qs defined in a non-class scope")
12184 : G_("%qs declared in a non-class scope"), name);
12185 sfk = sfk_none;
12189 /* Record whether the function is public. */
12190 publicp = (ctype != NULL_TREE
12191 || storage_class != sc_static);
12193 if (late_return_type_p)
12194 TYPE_HAS_LATE_RETURN_TYPE (type) = 1;
12196 decl = grokfndecl (ctype, type, original_name, parms, unqualified_id,
12197 reqs, virtualp, flags, memfn_quals, rqual, raises,
12198 1, friendp,
12199 publicp,
12200 inlinep | (2 * constexpr_p) | (4 * concept_p),
12201 initialized == SD_DELETED,
12202 sfk,
12203 funcdef_flag,
12204 template_count, in_namespace, attrlist,
12205 declarator->id_loc);
12206 if (decl == NULL_TREE)
12207 return error_mark_node;
12209 if (staticp == 1)
12211 int invalid_static = 0;
12213 /* Don't allow a static member function in a class, and forbid
12214 declaring main to be static. */
12215 if (TREE_CODE (type) == METHOD_TYPE)
12217 permerror (input_location, "cannot declare member function %qD to have "
12218 "static linkage", decl);
12219 invalid_static = 1;
12221 else if (current_function_decl)
12223 /* 7.1.1: There can be no static function declarations within a
12224 block. */
12225 error ("cannot declare static function inside another function");
12226 invalid_static = 1;
12229 if (invalid_static)
12231 staticp = 0;
12232 storage_class = sc_none;
12236 else
12238 /* It's a variable. */
12240 /* An uninitialized decl with `extern' is a reference. */
12241 decl = grokvardecl (type, dname, unqualified_id,
12242 declspecs,
12243 initialized,
12244 type_quals,
12245 inlinep,
12246 concept_p,
12247 template_count,
12248 ctype ? ctype : in_namespace);
12249 if (decl == NULL_TREE)
12250 return error_mark_node;
12252 bad_specifiers (decl, BSP_VAR, virtualp,
12253 memfn_quals != TYPE_UNQUALIFIED,
12254 inlinep, friendp, raises != NULL_TREE);
12256 if (ctype)
12258 DECL_CONTEXT (decl) = ctype;
12259 if (staticp == 1)
12261 permerror (input_location, "%<static%> may not be used when defining "
12262 "(as opposed to declaring) a static data member");
12263 staticp = 0;
12264 storage_class = sc_none;
12266 if (storage_class == sc_register && TREE_STATIC (decl))
12268 error ("static member %qD declared %<register%>", decl);
12269 storage_class = sc_none;
12271 if (storage_class == sc_extern && pedantic)
12273 pedwarn (input_location, OPT_Wpedantic,
12274 "cannot explicitly declare member %q#D to have "
12275 "extern linkage", decl);
12276 storage_class = sc_none;
12279 else if (constexpr_p && DECL_EXTERNAL (decl))
12281 error ("declaration of constexpr variable %qD is not a definition",
12282 decl);
12283 constexpr_p = false;
12286 if (inlinep)
12287 mark_inline_variable (decl);
12288 if (innermost_code == cdk_decomp)
12290 gcc_assert (declarator && declarator->kind == cdk_decomp);
12291 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
12292 retrofit_lang_decl (decl);
12293 DECL_ARTIFICIAL (decl) = 1;
12294 SET_DECL_DECOMPOSITION_P (decl);
12298 if (VAR_P (decl) && !initialized)
12299 if (tree auto_node = type_uses_auto (type))
12301 location_t loc = declspecs->locations[ds_type_spec];
12302 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
12304 error_at (loc, "invalid use of template-name %qE without an "
12305 "argument list", tmpl);
12306 inform (loc, "class template argument deduction "
12307 "requires an initializer");
12309 else
12310 error_at (loc, "declaration of %q#D has no initializer", decl);
12311 TREE_TYPE (decl) = error_mark_node;
12314 if (storage_class == sc_extern && initialized && !funcdef_flag)
12316 if (toplevel_bindings_p ())
12318 /* It's common practice (and completely valid) to have a const
12319 be initialized and declared extern. */
12320 if (!(type_quals & TYPE_QUAL_CONST))
12321 warning (0, "%qs initialized and declared %<extern%>", name);
12323 else
12325 error ("%qs has both %<extern%> and initializer", name);
12326 return error_mark_node;
12330 /* Record `register' declaration for warnings on &
12331 and in case doing stupid register allocation. */
12333 if (storage_class == sc_register)
12335 DECL_REGISTER (decl) = 1;
12336 /* Warn about register storage specifiers on PARM_DECLs. */
12337 if (TREE_CODE (decl) == PARM_DECL)
12339 if (cxx_dialect >= cxx1z)
12340 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
12341 "ISO C++1z does not allow %<register%> storage "
12342 "class specifier");
12343 else
12344 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
12345 "%<register%> storage class specifier used");
12348 else if (storage_class == sc_extern)
12349 DECL_THIS_EXTERN (decl) = 1;
12350 else if (storage_class == sc_static)
12351 DECL_THIS_STATIC (decl) = 1;
12353 /* Set constexpr flag on vars (functions got it in grokfndecl). */
12354 if (constexpr_p && VAR_P (decl))
12355 DECL_DECLARED_CONSTEXPR_P (decl) = true;
12357 /* Record constancy and volatility on the DECL itself . There's
12358 no need to do this when processing a template; we'll do this
12359 for the instantiated declaration based on the type of DECL. */
12360 if (!processing_template_decl)
12361 cp_apply_type_quals_to_decl (type_quals, decl);
12363 return decl;
12367 /* Subroutine of start_function. Ensure that each of the parameter
12368 types (as listed in PARMS) is complete, as is required for a
12369 function definition. */
12371 static void
12372 require_complete_types_for_parms (tree parms)
12374 for (; parms; parms = DECL_CHAIN (parms))
12376 if (dependent_type_p (TREE_TYPE (parms)))
12377 continue;
12378 if (!VOID_TYPE_P (TREE_TYPE (parms))
12379 && complete_type_or_else (TREE_TYPE (parms), parms))
12381 relayout_decl (parms);
12382 DECL_ARG_TYPE (parms) = type_passed_as (TREE_TYPE (parms));
12384 else
12385 /* grokparms or complete_type_or_else will have already issued
12386 an error. */
12387 TREE_TYPE (parms) = error_mark_node;
12391 /* Returns nonzero if T is a local variable. */
12394 local_variable_p (const_tree t)
12396 if ((VAR_P (t)
12397 /* A VAR_DECL with a context that is a _TYPE is a static data
12398 member. */
12399 && !TYPE_P (CP_DECL_CONTEXT (t))
12400 /* Any other non-local variable must be at namespace scope. */
12401 && !DECL_NAMESPACE_SCOPE_P (t))
12402 || (TREE_CODE (t) == PARM_DECL))
12403 return 1;
12405 return 0;
12408 /* Like local_variable_p, but suitable for use as a tree-walking
12409 function. */
12411 static tree
12412 local_variable_p_walkfn (tree *tp, int *walk_subtrees,
12413 void * /*data*/)
12415 if (local_variable_p (*tp)
12416 && (!DECL_ARTIFICIAL (*tp) || DECL_NAME (*tp) == this_identifier))
12417 return *tp;
12418 else if (TYPE_P (*tp))
12419 *walk_subtrees = 0;
12421 return NULL_TREE;
12424 /* Check that ARG, which is a default-argument expression for a
12425 parameter DECL, is valid. Returns ARG, or ERROR_MARK_NODE, if
12426 something goes wrong. DECL may also be a _TYPE node, rather than a
12427 DECL, if there is no DECL available. */
12429 tree
12430 check_default_argument (tree decl, tree arg, tsubst_flags_t complain)
12432 tree var;
12433 tree decl_type;
12435 if (TREE_CODE (arg) == DEFAULT_ARG)
12436 /* We get a DEFAULT_ARG when looking at an in-class declaration
12437 with a default argument. Ignore the argument for now; we'll
12438 deal with it after the class is complete. */
12439 return arg;
12441 if (TYPE_P (decl))
12443 decl_type = decl;
12444 decl = NULL_TREE;
12446 else
12447 decl_type = TREE_TYPE (decl);
12449 if (arg == error_mark_node
12450 || decl == error_mark_node
12451 || TREE_TYPE (arg) == error_mark_node
12452 || decl_type == error_mark_node)
12453 /* Something already went wrong. There's no need to check
12454 further. */
12455 return error_mark_node;
12457 /* [dcl.fct.default]
12459 A default argument expression is implicitly converted to the
12460 parameter type. */
12461 ++cp_unevaluated_operand;
12462 perform_implicit_conversion_flags (decl_type, arg, complain,
12463 LOOKUP_IMPLICIT);
12464 --cp_unevaluated_operand;
12466 /* Avoid redundant -Wzero-as-null-pointer-constant warnings at
12467 the call sites. */
12468 if (TYPE_PTR_OR_PTRMEM_P (decl_type)
12469 && null_ptr_cst_p (arg))
12470 return nullptr_node;
12472 /* [dcl.fct.default]
12474 Local variables shall not be used in default argument
12475 expressions.
12477 The keyword `this' shall not be used in a default argument of a
12478 member function. */
12479 var = cp_walk_tree_without_duplicates (&arg, local_variable_p_walkfn, NULL);
12480 if (var)
12482 if (complain & tf_warning_or_error)
12484 if (DECL_NAME (var) == this_identifier)
12485 permerror (input_location, "default argument %qE uses %qD",
12486 arg, var);
12487 else
12488 error ("default argument %qE uses local variable %qD", arg, var);
12490 return error_mark_node;
12493 /* All is well. */
12494 return arg;
12497 /* Returns a deprecated type used within TYPE, or NULL_TREE if none. */
12499 static tree
12500 type_is_deprecated (tree type)
12502 enum tree_code code;
12503 if (TREE_DEPRECATED (type))
12504 return type;
12505 if (TYPE_NAME (type))
12507 if (TREE_DEPRECATED (TYPE_NAME (type)))
12508 return type;
12509 else
12510 return NULL_TREE;
12513 /* Do warn about using typedefs to a deprecated class. */
12514 if (OVERLOAD_TYPE_P (type) && type != TYPE_MAIN_VARIANT (type))
12515 return type_is_deprecated (TYPE_MAIN_VARIANT (type));
12517 code = TREE_CODE (type);
12519 if (code == POINTER_TYPE || code == REFERENCE_TYPE
12520 || code == OFFSET_TYPE || code == FUNCTION_TYPE
12521 || code == METHOD_TYPE || code == ARRAY_TYPE)
12522 return type_is_deprecated (TREE_TYPE (type));
12524 if (TYPE_PTRMEMFUNC_P (type))
12525 return type_is_deprecated
12526 (TREE_TYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type))));
12528 return NULL_TREE;
12531 /* Decode the list of parameter types for a function type.
12532 Given the list of things declared inside the parens,
12533 return a list of types.
12535 If this parameter does not end with an ellipsis, we append
12536 void_list_node.
12538 *PARMS is set to the chain of PARM_DECLs created. */
12540 tree
12541 grokparms (tree parmlist, tree *parms)
12543 tree result = NULL_TREE;
12544 tree decls = NULL_TREE;
12545 tree parm;
12546 int any_error = 0;
12548 for (parm = parmlist; parm != NULL_TREE; parm = TREE_CHAIN (parm))
12550 tree type = NULL_TREE;
12551 tree init = TREE_PURPOSE (parm);
12552 tree decl = TREE_VALUE (parm);
12554 if (parm == void_list_node)
12555 break;
12557 if (! decl || TREE_TYPE (decl) == error_mark_node)
12558 continue;
12560 type = TREE_TYPE (decl);
12561 if (VOID_TYPE_P (type))
12563 if (same_type_p (type, void_type_node)
12564 && !init
12565 && !DECL_NAME (decl) && !result
12566 && TREE_CHAIN (parm) == void_list_node)
12567 /* DR 577: A parameter list consisting of a single
12568 unnamed parameter of non-dependent type 'void'. */
12569 break;
12570 else if (cv_qualified_p (type))
12571 error_at (DECL_SOURCE_LOCATION (decl),
12572 "invalid use of cv-qualified type %qT in "
12573 "parameter declaration", type);
12574 else
12575 error_at (DECL_SOURCE_LOCATION (decl),
12576 "invalid use of type %<void%> in parameter "
12577 "declaration");
12578 /* It's not a good idea to actually create parameters of
12579 type `void'; other parts of the compiler assume that a
12580 void type terminates the parameter list. */
12581 type = error_mark_node;
12582 TREE_TYPE (decl) = error_mark_node;
12585 if (type != error_mark_node)
12587 if (deprecated_state != DEPRECATED_SUPPRESS)
12589 tree deptype = type_is_deprecated (type);
12590 if (deptype)
12591 warn_deprecated_use (deptype, NULL_TREE);
12594 /* Top-level qualifiers on the parameters are
12595 ignored for function types. */
12596 type = cp_build_qualified_type (type, 0);
12597 if (TREE_CODE (type) == METHOD_TYPE)
12599 error ("parameter %qD invalidly declared method type", decl);
12600 type = build_pointer_type (type);
12601 TREE_TYPE (decl) = type;
12603 else if (abstract_virtuals_error (decl, type))
12604 any_error = 1; /* Seems like a good idea. */
12605 else if (POINTER_TYPE_P (type))
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 error (ptr
12624 ? G_("parameter %qD includes pointer to array of "
12625 "unknown bound %qT")
12626 : G_("parameter %qD includes reference to array of "
12627 "unknown bound %qT"),
12628 decl, t);
12631 if (any_error)
12632 init = NULL_TREE;
12633 else if (init && !processing_template_decl)
12634 init = check_default_argument (decl, init, tf_warning_or_error);
12637 DECL_CHAIN (decl) = decls;
12638 decls = decl;
12639 result = tree_cons (init, type, result);
12641 decls = nreverse (decls);
12642 result = nreverse (result);
12643 if (parm)
12644 result = chainon (result, void_list_node);
12645 *parms = decls;
12647 return result;
12651 /* D is a constructor or overloaded `operator='.
12653 Let T be the class in which D is declared. Then, this function
12654 returns:
12656 -1 if D's is an ill-formed constructor or copy assignment operator
12657 whose first parameter is of type `T'.
12658 0 if D is not a copy constructor or copy assignment
12659 operator.
12660 1 if D is a copy constructor or copy assignment operator whose
12661 first parameter is a reference to non-const qualified T.
12662 2 if D is a copy constructor or copy assignment operator whose
12663 first parameter is a reference to const qualified T.
12665 This function can be used as a predicate. Positive values indicate
12666 a copy constructor and nonzero values indicate a copy assignment
12667 operator. */
12670 copy_fn_p (const_tree d)
12672 tree args;
12673 tree arg_type;
12674 int result = 1;
12676 gcc_assert (DECL_FUNCTION_MEMBER_P (d));
12678 if (TREE_CODE (d) == TEMPLATE_DECL
12679 || (DECL_TEMPLATE_INFO (d)
12680 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d))))
12681 /* Instantiations of template member functions are never copy
12682 functions. Note that member functions of templated classes are
12683 represented as template functions internally, and we must
12684 accept those as copy functions. */
12685 return 0;
12687 args = FUNCTION_FIRST_USER_PARMTYPE (d);
12688 if (!args)
12689 return 0;
12691 arg_type = TREE_VALUE (args);
12692 if (arg_type == error_mark_node)
12693 return 0;
12695 if (TYPE_MAIN_VARIANT (arg_type) == DECL_CONTEXT (d))
12697 /* Pass by value copy assignment operator. */
12698 result = -1;
12700 else if (TREE_CODE (arg_type) == REFERENCE_TYPE
12701 && !TYPE_REF_IS_RVALUE (arg_type)
12702 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)) == DECL_CONTEXT (d))
12704 if (CP_TYPE_CONST_P (TREE_TYPE (arg_type)))
12705 result = 2;
12707 else
12708 return 0;
12710 args = TREE_CHAIN (args);
12712 if (args && args != void_list_node && !TREE_PURPOSE (args))
12713 /* There are more non-optional args. */
12714 return 0;
12716 return result;
12719 /* D is a constructor or overloaded `operator='.
12721 Let T be the class in which D is declared. Then, this function
12722 returns true when D is a move constructor or move assignment
12723 operator, false otherwise. */
12725 bool
12726 move_fn_p (const_tree d)
12728 gcc_assert (DECL_FUNCTION_MEMBER_P (d));
12730 if (cxx_dialect == cxx98)
12731 /* There are no move constructors if we are in C++98 mode. */
12732 return false;
12734 if (TREE_CODE (d) == TEMPLATE_DECL
12735 || (DECL_TEMPLATE_INFO (d)
12736 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d))))
12737 /* Instantiations of template member functions are never move
12738 functions. Note that member functions of templated classes are
12739 represented as template functions internally, and we must
12740 accept those as move functions. */
12741 return 0;
12743 return move_signature_fn_p (d);
12746 /* D is a constructor or overloaded `operator='.
12748 Then, this function returns true when D has the same signature as a move
12749 constructor or move assignment operator (because either it is such a
12750 ctor/op= or it is a template specialization with the same signature),
12751 false otherwise. */
12753 bool
12754 move_signature_fn_p (const_tree d)
12756 tree args;
12757 tree arg_type;
12758 bool result = false;
12760 args = FUNCTION_FIRST_USER_PARMTYPE (d);
12761 if (!args)
12762 return 0;
12764 arg_type = TREE_VALUE (args);
12765 if (arg_type == error_mark_node)
12766 return 0;
12768 if (TREE_CODE (arg_type) == REFERENCE_TYPE
12769 && TYPE_REF_IS_RVALUE (arg_type)
12770 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)),
12771 DECL_CONTEXT (d)))
12772 result = true;
12774 args = TREE_CHAIN (args);
12776 if (args && args != void_list_node && !TREE_PURPOSE (args))
12777 /* There are more non-optional args. */
12778 return false;
12780 return result;
12783 /* Remember any special properties of member function DECL. */
12785 void
12786 grok_special_member_properties (tree decl)
12788 tree class_type;
12790 if (!DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
12791 return;
12793 class_type = DECL_CONTEXT (decl);
12794 if (DECL_CONSTRUCTOR_P (decl))
12796 int ctor = copy_fn_p (decl);
12798 if (!DECL_ARTIFICIAL (decl))
12799 TYPE_HAS_USER_CONSTRUCTOR (class_type) = 1;
12801 if (ctor > 0)
12803 /* [class.copy]
12805 A non-template constructor for class X is a copy
12806 constructor if its first parameter is of type X&, const
12807 X&, volatile X& or const volatile X&, and either there
12808 are no other parameters or else all other parameters have
12809 default arguments. */
12810 TYPE_HAS_COPY_CTOR (class_type) = 1;
12811 if (user_provided_p (decl))
12812 TYPE_HAS_COMPLEX_COPY_CTOR (class_type) = 1;
12813 if (ctor > 1)
12814 TYPE_HAS_CONST_COPY_CTOR (class_type) = 1;
12816 else if (sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (decl)))
12817 TYPE_HAS_DEFAULT_CONSTRUCTOR (class_type) = 1;
12818 else if (move_fn_p (decl) && user_provided_p (decl))
12819 TYPE_HAS_COMPLEX_MOVE_CTOR (class_type) = 1;
12820 else if (is_list_ctor (decl))
12821 TYPE_HAS_LIST_CTOR (class_type) = 1;
12823 if (DECL_DECLARED_CONSTEXPR_P (decl)
12824 && !copy_fn_p (decl) && !move_fn_p (decl))
12825 TYPE_HAS_CONSTEXPR_CTOR (class_type) = 1;
12827 else if (DECL_OVERLOADED_OPERATOR_P (decl) == NOP_EXPR)
12829 /* [class.copy]
12831 A non-template assignment operator for class X is a copy
12832 assignment operator if its parameter is of type X, X&, const
12833 X&, volatile X& or const volatile X&. */
12835 int assop = copy_fn_p (decl);
12837 if (assop)
12839 TYPE_HAS_COPY_ASSIGN (class_type) = 1;
12840 if (user_provided_p (decl))
12841 TYPE_HAS_COMPLEX_COPY_ASSIGN (class_type) = 1;
12842 if (assop != 1)
12843 TYPE_HAS_CONST_COPY_ASSIGN (class_type) = 1;
12845 else if (move_fn_p (decl) && user_provided_p (decl))
12846 TYPE_HAS_COMPLEX_MOVE_ASSIGN (class_type) = 1;
12848 /* Destructors are handled in check_methods. */
12851 /* Check a constructor DECL has the correct form. Complains
12852 if the class has a constructor of the form X(X). */
12855 grok_ctor_properties (const_tree ctype, const_tree decl)
12857 int ctor_parm = copy_fn_p (decl);
12859 if (ctor_parm < 0)
12861 /* [class.copy]
12863 A declaration of a constructor for a class X is ill-formed if
12864 its first parameter is of type (optionally cv-qualified) X
12865 and either there are no other parameters or else all other
12866 parameters have default arguments.
12868 We *don't* complain about member template instantiations that
12869 have this form, though; they can occur as we try to decide
12870 what constructor to use during overload resolution. Since
12871 overload resolution will never prefer such a constructor to
12872 the non-template copy constructor (which is either explicitly
12873 or implicitly defined), there's no need to worry about their
12874 existence. Theoretically, they should never even be
12875 instantiated, but that's hard to forestall. */
12876 error ("invalid constructor; you probably meant %<%T (const %T&)%>",
12877 ctype, ctype);
12878 return 0;
12881 return 1;
12884 /* An operator with this code is unary, but can also be binary. */
12886 static int
12887 ambi_op_p (enum tree_code code)
12889 return (code == INDIRECT_REF
12890 || code == ADDR_EXPR
12891 || code == UNARY_PLUS_EXPR
12892 || code == NEGATE_EXPR
12893 || code == PREINCREMENT_EXPR
12894 || code == PREDECREMENT_EXPR);
12897 /* An operator with this name can only be unary. */
12899 static int
12900 unary_op_p (enum tree_code code)
12902 return (code == TRUTH_NOT_EXPR
12903 || code == BIT_NOT_EXPR
12904 || code == COMPONENT_REF
12905 || code == TYPE_EXPR);
12908 /* DECL is a declaration for an overloaded operator. If COMPLAIN is true,
12909 errors are issued for invalid declarations. */
12911 bool
12912 grok_op_properties (tree decl, bool complain)
12914 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
12915 tree argtype;
12916 int methodp = (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE);
12917 tree name = DECL_NAME (decl);
12918 enum tree_code operator_code;
12919 int arity;
12920 bool ellipsis_p;
12921 tree class_type;
12923 /* Count the number of arguments and check for ellipsis. */
12924 for (argtype = argtypes, arity = 0;
12925 argtype && argtype != void_list_node;
12926 argtype = TREE_CHAIN (argtype))
12927 ++arity;
12928 ellipsis_p = !argtype;
12930 class_type = DECL_CONTEXT (decl);
12931 if (class_type && !CLASS_TYPE_P (class_type))
12932 class_type = NULL_TREE;
12934 if (DECL_CONV_FN_P (decl))
12935 operator_code = TYPE_EXPR;
12936 else
12939 #define DEF_OPERATOR(NAME, CODE, MANGLING, ARITY, ASSN_P) \
12940 if (ansi_opname (CODE) == name) \
12942 operator_code = (CODE); \
12943 break; \
12945 else if (ansi_assopname (CODE) == name) \
12947 operator_code = (CODE); \
12948 DECL_ASSIGNMENT_OPERATOR_P (decl) = 1; \
12949 break; \
12952 #include "operators.def"
12953 #undef DEF_OPERATOR
12955 gcc_unreachable ();
12957 while (0);
12958 gcc_assert (operator_code != MAX_TREE_CODES);
12959 SET_OVERLOADED_OPERATOR_CODE (decl, operator_code);
12961 if (class_type)
12962 switch (operator_code)
12964 case NEW_EXPR:
12965 TYPE_HAS_NEW_OPERATOR (class_type) = 1;
12966 break;
12968 case DELETE_EXPR:
12969 TYPE_GETS_DELETE (class_type) |= 1;
12970 break;
12972 case VEC_NEW_EXPR:
12973 TYPE_HAS_ARRAY_NEW_OPERATOR (class_type) = 1;
12974 break;
12976 case VEC_DELETE_EXPR:
12977 TYPE_GETS_DELETE (class_type) |= 2;
12978 break;
12980 default:
12981 break;
12984 /* [basic.std.dynamic.allocation]/1:
12986 A program is ill-formed if an allocation function is declared
12987 in a namespace scope other than global scope or declared static
12988 in global scope.
12990 The same also holds true for deallocation functions. */
12991 if (operator_code == NEW_EXPR || operator_code == VEC_NEW_EXPR
12992 || operator_code == DELETE_EXPR || operator_code == VEC_DELETE_EXPR)
12994 if (DECL_NAMESPACE_SCOPE_P (decl))
12996 if (CP_DECL_CONTEXT (decl) != global_namespace)
12998 error ("%qD may not be declared within a namespace", decl);
12999 return false;
13001 else if (!TREE_PUBLIC (decl))
13003 error ("%qD may not be declared as static", decl);
13004 return false;
13009 if (operator_code == NEW_EXPR || operator_code == VEC_NEW_EXPR)
13011 TREE_TYPE (decl) = coerce_new_type (TREE_TYPE (decl));
13012 DECL_IS_OPERATOR_NEW (decl) = 1;
13014 else if (operator_code == DELETE_EXPR || operator_code == VEC_DELETE_EXPR)
13015 TREE_TYPE (decl) = coerce_delete_type (TREE_TYPE (decl));
13016 else
13018 /* An operator function must either be a non-static member function
13019 or have at least one parameter of a class, a reference to a class,
13020 an enumeration, or a reference to an enumeration. 13.4.0.6 */
13021 if (! methodp || DECL_STATIC_FUNCTION_P (decl))
13023 if (operator_code == TYPE_EXPR
13024 || operator_code == CALL_EXPR
13025 || operator_code == COMPONENT_REF
13026 || operator_code == ARRAY_REF
13027 || operator_code == NOP_EXPR)
13029 error ("%qD must be a nonstatic member function", decl);
13030 return false;
13032 else
13034 tree p;
13036 if (DECL_STATIC_FUNCTION_P (decl))
13038 error ("%qD must be either a non-static member "
13039 "function or a non-member function", decl);
13040 return false;
13043 for (p = argtypes; p && p != void_list_node; p = TREE_CHAIN (p))
13045 tree arg = non_reference (TREE_VALUE (p));
13046 if (arg == error_mark_node)
13047 return false;
13049 /* MAYBE_CLASS_TYPE_P, rather than CLASS_TYPE_P, is used
13050 because these checks are performed even on
13051 template functions. */
13052 if (MAYBE_CLASS_TYPE_P (arg)
13053 || TREE_CODE (arg) == ENUMERAL_TYPE)
13054 break;
13057 if (!p || p == void_list_node)
13059 if (complain)
13060 error ("%qD must have an argument of class or "
13061 "enumerated type", decl);
13062 return false;
13067 /* There are no restrictions on the arguments to an overloaded
13068 "operator ()". */
13069 if (operator_code == CALL_EXPR)
13070 return true;
13072 /* Warn about conversion operators that will never be used. */
13073 if (IDENTIFIER_TYPENAME_P (name)
13074 && ! DECL_TEMPLATE_INFO (decl)
13075 && warn_conversion
13076 /* Warn only declaring the function; there is no need to
13077 warn again about out-of-class definitions. */
13078 && class_type == current_class_type)
13080 tree t = TREE_TYPE (name);
13081 int ref = (TREE_CODE (t) == REFERENCE_TYPE);
13083 if (ref)
13084 t = TYPE_MAIN_VARIANT (TREE_TYPE (t));
13086 if (VOID_TYPE_P (t))
13087 warning (OPT_Wconversion,
13089 ? G_("conversion to a reference to void "
13090 "will never use a type conversion operator")
13091 : G_("conversion to void "
13092 "will never use a type conversion operator"));
13093 else if (class_type)
13095 if (t == class_type)
13096 warning (OPT_Wconversion,
13098 ? G_("conversion to a reference to the same type "
13099 "will never use a type conversion operator")
13100 : G_("conversion to the same type "
13101 "will never use a type conversion operator"));
13102 /* Don't force t to be complete here. */
13103 else if (MAYBE_CLASS_TYPE_P (t)
13104 && COMPLETE_TYPE_P (t)
13105 && DERIVED_FROM_P (t, class_type))
13106 warning (OPT_Wconversion,
13108 ? G_("conversion to a reference to a base class "
13109 "will never use a type conversion operator")
13110 : G_("conversion to a base class "
13111 "will never use a type conversion operator"));
13116 if (operator_code == COND_EXPR)
13118 /* 13.4.0.3 */
13119 error ("ISO C++ prohibits overloading operator ?:");
13120 return false;
13122 else if (ellipsis_p)
13124 error ("%qD must not have variable number of arguments", decl);
13125 return false;
13127 else if (ambi_op_p (operator_code))
13129 if (arity == 1)
13130 /* We pick the one-argument operator codes by default, so
13131 we don't have to change anything. */
13133 else if (arity == 2)
13135 /* If we thought this was a unary operator, we now know
13136 it to be a binary operator. */
13137 switch (operator_code)
13139 case INDIRECT_REF:
13140 operator_code = MULT_EXPR;
13141 break;
13143 case ADDR_EXPR:
13144 operator_code = BIT_AND_EXPR;
13145 break;
13147 case UNARY_PLUS_EXPR:
13148 operator_code = PLUS_EXPR;
13149 break;
13151 case NEGATE_EXPR:
13152 operator_code = MINUS_EXPR;
13153 break;
13155 case PREINCREMENT_EXPR:
13156 operator_code = POSTINCREMENT_EXPR;
13157 break;
13159 case PREDECREMENT_EXPR:
13160 operator_code = POSTDECREMENT_EXPR;
13161 break;
13163 default:
13164 gcc_unreachable ();
13167 SET_OVERLOADED_OPERATOR_CODE (decl, operator_code);
13169 if ((operator_code == POSTINCREMENT_EXPR
13170 || operator_code == POSTDECREMENT_EXPR)
13171 && ! processing_template_decl
13172 && ! same_type_p (TREE_VALUE (TREE_CHAIN (argtypes)), integer_type_node))
13174 if (methodp)
13175 error ("postfix %qD must take %<int%> as its argument",
13176 decl);
13177 else
13178 error ("postfix %qD must take %<int%> as its second "
13179 "argument", decl);
13180 return false;
13183 else
13185 if (methodp)
13186 error ("%qD must take either zero or one argument", decl);
13187 else
13188 error ("%qD must take either one or two arguments", decl);
13189 return false;
13192 /* More Effective C++ rule 6. */
13193 if (warn_ecpp
13194 && (operator_code == POSTINCREMENT_EXPR
13195 || operator_code == POSTDECREMENT_EXPR
13196 || operator_code == PREINCREMENT_EXPR
13197 || operator_code == PREDECREMENT_EXPR))
13199 tree arg = TREE_VALUE (argtypes);
13200 tree ret = TREE_TYPE (TREE_TYPE (decl));
13201 if (methodp || TREE_CODE (arg) == REFERENCE_TYPE)
13202 arg = TREE_TYPE (arg);
13203 arg = TYPE_MAIN_VARIANT (arg);
13204 if (operator_code == PREINCREMENT_EXPR
13205 || operator_code == PREDECREMENT_EXPR)
13207 if (TREE_CODE (ret) != REFERENCE_TYPE
13208 || !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (ret)),
13209 arg))
13210 warning (OPT_Weffc__, "prefix %qD should return %qT", decl,
13211 build_reference_type (arg));
13213 else
13215 if (!same_type_p (TYPE_MAIN_VARIANT (ret), arg))
13216 warning (OPT_Weffc__, "postfix %qD should return %qT", decl, arg);
13220 else if (unary_op_p (operator_code))
13222 if (arity != 1)
13224 if (methodp)
13225 error ("%qD must take %<void%>", decl);
13226 else
13227 error ("%qD must take exactly one argument", decl);
13228 return false;
13231 else /* if (binary_op_p (operator_code)) */
13233 if (arity != 2)
13235 if (methodp)
13236 error ("%qD must take exactly one argument", decl);
13237 else
13238 error ("%qD must take exactly two arguments", decl);
13239 return false;
13242 /* More Effective C++ rule 7. */
13243 if (warn_ecpp
13244 && (operator_code == TRUTH_ANDIF_EXPR
13245 || operator_code == TRUTH_ORIF_EXPR
13246 || operator_code == COMPOUND_EXPR))
13247 warning (OPT_Weffc__, "user-defined %qD always evaluates both arguments",
13248 decl);
13251 /* Effective C++ rule 23. */
13252 if (warn_ecpp
13253 && arity == 2
13254 && !DECL_ASSIGNMENT_OPERATOR_P (decl)
13255 && (operator_code == PLUS_EXPR
13256 || operator_code == MINUS_EXPR
13257 || operator_code == TRUNC_DIV_EXPR
13258 || operator_code == MULT_EXPR
13259 || operator_code == TRUNC_MOD_EXPR)
13260 && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == REFERENCE_TYPE)
13261 warning (OPT_Weffc__, "%qD should return by value", decl);
13263 /* [over.oper]/8 */
13264 for (; argtypes && argtypes != void_list_node;
13265 argtypes = TREE_CHAIN (argtypes))
13266 if (TREE_PURPOSE (argtypes))
13268 TREE_PURPOSE (argtypes) = NULL_TREE;
13269 if (operator_code == POSTINCREMENT_EXPR
13270 || operator_code == POSTDECREMENT_EXPR)
13272 pedwarn (input_location, OPT_Wpedantic, "%qD cannot have default arguments",
13273 decl);
13275 else
13277 error ("%qD cannot have default arguments", decl);
13278 return false;
13282 return true;
13285 /* Return a string giving the keyword associate with CODE. */
13287 static const char *
13288 tag_name (enum tag_types code)
13290 switch (code)
13292 case record_type:
13293 return "struct";
13294 case class_type:
13295 return "class";
13296 case union_type:
13297 return "union";
13298 case enum_type:
13299 return "enum";
13300 case typename_type:
13301 return "typename";
13302 default:
13303 gcc_unreachable ();
13307 /* Name lookup in an elaborated-type-specifier (after the keyword
13308 indicated by TAG_CODE) has found the TYPE_DECL DECL. If the
13309 elaborated-type-specifier is invalid, issue a diagnostic and return
13310 error_mark_node; otherwise, return the *_TYPE to which it referred.
13311 If ALLOW_TEMPLATE_P is true, TYPE may be a class template. */
13313 tree
13314 check_elaborated_type_specifier (enum tag_types tag_code,
13315 tree decl,
13316 bool allow_template_p)
13318 tree type;
13320 /* In the case of:
13322 struct S { struct S *p; };
13324 name lookup will find the TYPE_DECL for the implicit "S::S"
13325 typedef. Adjust for that here. */
13326 if (DECL_SELF_REFERENCE_P (decl))
13327 decl = TYPE_NAME (TREE_TYPE (decl));
13329 type = TREE_TYPE (decl);
13331 /* Check TEMPLATE_TYPE_PARM first because DECL_IMPLICIT_TYPEDEF_P
13332 is false for this case as well. */
13333 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
13335 error ("using template type parameter %qT after %qs",
13336 type, tag_name (tag_code));
13337 return error_mark_node;
13339 /* Accept template template parameters. */
13340 else if (allow_template_p
13341 && (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM
13342 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM))
13344 /* [dcl.type.elab]
13346 If the identifier resolves to a typedef-name or the
13347 simple-template-id resolves to an alias template
13348 specialization, the elaborated-type-specifier is ill-formed.
13350 In other words, the only legitimate declaration to use in the
13351 elaborated type specifier is the implicit typedef created when
13352 the type is declared. */
13353 else if (!DECL_IMPLICIT_TYPEDEF_P (decl)
13354 && !DECL_SELF_REFERENCE_P (decl)
13355 && tag_code != typename_type)
13357 if (alias_template_specialization_p (type))
13358 error ("using alias template specialization %qT after %qs",
13359 type, tag_name (tag_code));
13360 else
13361 error ("using typedef-name %qD after %qs", decl, tag_name (tag_code));
13362 inform (DECL_SOURCE_LOCATION (decl),
13363 "%qD has a previous declaration here", decl);
13364 return error_mark_node;
13366 else if (TREE_CODE (type) != RECORD_TYPE
13367 && TREE_CODE (type) != UNION_TYPE
13368 && tag_code != enum_type
13369 && tag_code != typename_type)
13371 error ("%qT referred to as %qs", type, tag_name (tag_code));
13372 inform (location_of (type), "%qT has a previous declaration here", type);
13373 return error_mark_node;
13375 else if (TREE_CODE (type) != ENUMERAL_TYPE
13376 && tag_code == enum_type)
13378 error ("%qT referred to as enum", type);
13379 inform (location_of (type), "%qT has a previous declaration here", type);
13380 return error_mark_node;
13382 else if (!allow_template_p
13383 && TREE_CODE (type) == RECORD_TYPE
13384 && CLASSTYPE_IS_TEMPLATE (type))
13386 /* If a class template appears as elaborated type specifier
13387 without a template header such as:
13389 template <class T> class C {};
13390 void f(class C); // No template header here
13392 then the required template argument is missing. */
13393 error ("template argument required for %<%s %T%>",
13394 tag_name (tag_code),
13395 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)));
13396 return error_mark_node;
13399 return type;
13402 /* Lookup NAME in elaborate type specifier in scope according to
13403 SCOPE and issue diagnostics if necessary.
13404 Return *_TYPE node upon success, NULL_TREE when the NAME is not
13405 found, and ERROR_MARK_NODE for type error. */
13407 static tree
13408 lookup_and_check_tag (enum tag_types tag_code, tree name,
13409 tag_scope scope, bool template_header_p)
13411 tree t;
13412 tree decl;
13413 if (scope == ts_global)
13415 /* First try ordinary name lookup, ignoring hidden class name
13416 injected via friend declaration. */
13417 decl = lookup_name_prefer_type (name, 2);
13418 decl = strip_using_decl (decl);
13419 /* If that fails, the name will be placed in the smallest
13420 non-class, non-function-prototype scope according to 3.3.1/5.
13421 We may already have a hidden name declared as friend in this
13422 scope. So lookup again but not ignoring hidden names.
13423 If we find one, that name will be made visible rather than
13424 creating a new tag. */
13425 if (!decl)
13426 decl = lookup_type_scope (name, ts_within_enclosing_non_class);
13428 else
13429 decl = lookup_type_scope (name, scope);
13431 if (decl
13432 && (DECL_CLASS_TEMPLATE_P (decl)
13433 /* If scope is ts_current we're defining a class, so ignore a
13434 template template parameter. */
13435 || (scope != ts_current
13436 && DECL_TEMPLATE_TEMPLATE_PARM_P (decl))))
13437 decl = DECL_TEMPLATE_RESULT (decl);
13439 if (decl && TREE_CODE (decl) == TYPE_DECL)
13441 /* Look for invalid nested type:
13442 class C {
13443 class C {};
13444 }; */
13445 if (scope == ts_current && DECL_SELF_REFERENCE_P (decl))
13447 error ("%qD has the same name as the class in which it is "
13448 "declared",
13449 decl);
13450 return error_mark_node;
13453 /* Two cases we need to consider when deciding if a class
13454 template is allowed as an elaborated type specifier:
13455 1. It is a self reference to its own class.
13456 2. It comes with a template header.
13458 For example:
13460 template <class T> class C {
13461 class C *c1; // DECL_SELF_REFERENCE_P is true
13462 class D;
13464 template <class U> class C; // template_header_p is true
13465 template <class T> class C<T>::D {
13466 class C *c2; // DECL_SELF_REFERENCE_P is true
13467 }; */
13469 t = check_elaborated_type_specifier (tag_code,
13470 decl,
13471 template_header_p
13472 | DECL_SELF_REFERENCE_P (decl));
13473 if (template_header_p && t && CLASS_TYPE_P (t)
13474 && (!CLASSTYPE_TEMPLATE_INFO (t)
13475 || (!PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)))))
13477 error ("%qT is not a template", t);
13478 inform (location_of (t), "previous declaration here");
13479 if (TYPE_CLASS_SCOPE_P (t)
13480 && CLASSTYPE_TEMPLATE_INFO (TYPE_CONTEXT (t)))
13481 inform (input_location,
13482 "perhaps you want to explicitly add %<%T::%>",
13483 TYPE_CONTEXT (t));
13484 t = error_mark_node;
13487 return t;
13489 else if (decl && TREE_CODE (decl) == TREE_LIST)
13491 error ("reference to %qD is ambiguous", name);
13492 print_candidates (decl);
13493 return error_mark_node;
13495 else
13496 return NULL_TREE;
13499 /* Get the struct, enum or union (TAG_CODE says which) with tag NAME.
13500 Define the tag as a forward-reference if it is not defined.
13502 If a declaration is given, process it here, and report an error if
13503 multiple declarations are not identical.
13505 SCOPE is TS_CURRENT when this is also a definition. Only look in
13506 the current frame for the name (since C++ allows new names in any
13507 scope.) It is TS_WITHIN_ENCLOSING_NON_CLASS if this is a friend
13508 declaration. Only look beginning from the current scope outward up
13509 till the nearest non-class scope. Otherwise it is TS_GLOBAL.
13511 TEMPLATE_HEADER_P is true when this declaration is preceded by
13512 a set of template parameters. */
13514 static tree
13515 xref_tag_1 (enum tag_types tag_code, tree name,
13516 tag_scope orig_scope, bool template_header_p)
13518 enum tree_code code;
13519 tree t;
13520 tree context = NULL_TREE;
13521 tag_scope scope;
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 if (orig_scope == ts_lambda)
13542 scope = ts_current;
13543 else
13544 scope = orig_scope;
13546 /* In case of anonymous name, xref_tag is only called to
13547 make type node and push name. Name lookup is not required. */
13548 if (anon_aggrname_p (name))
13549 t = NULL_TREE;
13550 else
13551 t = lookup_and_check_tag (tag_code, name,
13552 scope, template_header_p);
13554 if (t == error_mark_node)
13555 return error_mark_node;
13557 if (scope != ts_current && t && current_class_type
13558 && template_class_depth (current_class_type)
13559 && template_header_p)
13561 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
13562 return t;
13564 /* Since SCOPE is not TS_CURRENT, we are not looking at a
13565 definition of this tag. Since, in addition, we are currently
13566 processing a (member) template declaration of a template
13567 class, we must be very careful; consider:
13569 template <class X>
13570 struct S1
13572 template <class U>
13573 struct S2
13574 { template <class V>
13575 friend struct S1; };
13577 Here, the S2::S1 declaration should not be confused with the
13578 outer declaration. In particular, the inner version should
13579 have a template parameter of level 2, not level 1. This
13580 would be particularly important if the member declaration
13581 were instead:
13583 template <class V = U> friend struct S1;
13585 say, when we should tsubst into `U' when instantiating
13586 S2. On the other hand, when presented with:
13588 template <class T>
13589 struct S1 {
13590 template <class U>
13591 struct S2 {};
13592 template <class U>
13593 friend struct S2;
13596 we must find the inner binding eventually. We
13597 accomplish this by making sure that the new type we
13598 create to represent this declaration has the right
13599 TYPE_CONTEXT. */
13600 context = TYPE_CONTEXT (t);
13601 t = NULL_TREE;
13604 if (! t)
13606 /* If no such tag is yet defined, create a forward-reference node
13607 and record it as the "definition".
13608 When a real declaration of this type is found,
13609 the forward-reference will be altered into a real type. */
13610 if (code == ENUMERAL_TYPE)
13612 error ("use of enum %q#D without previous declaration", name);
13613 return error_mark_node;
13615 else
13617 t = make_class_type (code);
13618 TYPE_CONTEXT (t) = context;
13619 if (orig_scope == ts_lambda)
13620 /* Remember that we're declaring a lambda to avoid bogus errors
13621 in push_template_decl. */
13622 CLASSTYPE_LAMBDA_EXPR (t) = error_mark_node;
13623 t = pushtag (name, t, scope);
13626 else
13628 if (template_header_p && MAYBE_CLASS_TYPE_P (t))
13630 /* Check that we aren't trying to overload a class with different
13631 constraints. */
13632 tree constr = NULL_TREE;
13633 if (current_template_parms)
13635 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
13636 constr = build_constraints (reqs, NULL_TREE);
13638 if (!redeclare_class_template (t, current_template_parms, constr))
13639 return error_mark_node;
13641 else if (!processing_template_decl
13642 && CLASS_TYPE_P (t)
13643 && CLASSTYPE_IS_TEMPLATE (t))
13645 error ("redeclaration of %qT as a non-template", t);
13646 inform (location_of (t), "previous declaration %qD", t);
13647 return error_mark_node;
13650 /* Make injected friend class visible. */
13651 if (scope != ts_within_enclosing_non_class
13652 && hidden_name_p (TYPE_NAME (t)))
13654 DECL_ANTICIPATED (TYPE_NAME (t)) = 0;
13655 DECL_FRIEND_P (TYPE_NAME (t)) = 0;
13657 if (TYPE_TEMPLATE_INFO (t))
13659 DECL_ANTICIPATED (TYPE_TI_TEMPLATE (t)) = 0;
13660 DECL_FRIEND_P (TYPE_TI_TEMPLATE (t)) = 0;
13665 return t;
13668 /* Wrapper for xref_tag_1. */
13670 tree
13671 xref_tag (enum tag_types tag_code, tree name,
13672 tag_scope scope, bool template_header_p)
13674 tree ret;
13675 bool subtime;
13676 subtime = timevar_cond_start (TV_NAME_LOOKUP);
13677 ret = xref_tag_1 (tag_code, name, scope, template_header_p);
13678 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
13679 return ret;
13683 tree
13684 xref_tag_from_type (tree old, tree id, tag_scope scope)
13686 enum tag_types tag_kind;
13688 if (TREE_CODE (old) == RECORD_TYPE)
13689 tag_kind = (CLASSTYPE_DECLARED_CLASS (old) ? class_type : record_type);
13690 else
13691 tag_kind = union_type;
13693 if (id == NULL_TREE)
13694 id = TYPE_IDENTIFIER (old);
13696 return xref_tag (tag_kind, id, scope, false);
13699 /* Create the binfo hierarchy for REF with (possibly NULL) base list
13700 BASE_LIST. For each element on BASE_LIST the TREE_PURPOSE is an
13701 access_* node, and the TREE_VALUE is the type of the base-class.
13702 Non-NULL TREE_TYPE indicates virtual inheritance. */
13704 void
13705 xref_basetypes (tree ref, tree base_list)
13707 tree *basep;
13708 tree binfo, base_binfo;
13709 unsigned max_vbases = 0; /* Maximum direct & indirect virtual bases. */
13710 unsigned max_bases = 0; /* Maximum direct bases. */
13711 unsigned max_dvbases = 0; /* Maximum direct virtual bases. */
13712 int i;
13713 tree default_access;
13714 tree igo_prev; /* Track Inheritance Graph Order. */
13716 if (ref == error_mark_node)
13717 return;
13719 /* The base of a derived class is private by default, all others are
13720 public. */
13721 default_access = (TREE_CODE (ref) == RECORD_TYPE
13722 && CLASSTYPE_DECLARED_CLASS (ref)
13723 ? access_private_node : access_public_node);
13725 /* First, make sure that any templates in base-classes are
13726 instantiated. This ensures that if we call ourselves recursively
13727 we do not get confused about which classes are marked and which
13728 are not. */
13729 basep = &base_list;
13730 while (*basep)
13732 tree basetype = TREE_VALUE (*basep);
13734 /* The dependent_type_p call below should really be dependent_scope_p
13735 so that we give a hard error about using an incomplete type as a
13736 base, but we allow it with a pedwarn for backward
13737 compatibility. */
13738 if (processing_template_decl
13739 && CLASS_TYPE_P (basetype) && TYPE_BEING_DEFINED (basetype))
13740 cxx_incomplete_type_diagnostic (NULL_TREE, basetype, DK_PEDWARN);
13741 if (!dependent_type_p (basetype)
13742 && !complete_type_or_else (basetype, NULL))
13743 /* An incomplete type. Remove it from the list. */
13744 *basep = TREE_CHAIN (*basep);
13745 else
13747 max_bases++;
13748 if (TREE_TYPE (*basep))
13749 max_dvbases++;
13750 if (CLASS_TYPE_P (basetype))
13751 max_vbases += vec_safe_length (CLASSTYPE_VBASECLASSES (basetype));
13752 basep = &TREE_CHAIN (*basep);
13755 max_vbases += max_dvbases;
13757 TYPE_MARKED_P (ref) = 1;
13759 /* The binfo slot should be empty, unless this is an (ill-formed)
13760 redefinition. */
13761 gcc_assert (!TYPE_BINFO (ref) || TYPE_SIZE (ref));
13763 gcc_assert (TYPE_MAIN_VARIANT (ref) == ref);
13765 binfo = make_tree_binfo (max_bases);
13767 TYPE_BINFO (ref) = binfo;
13768 BINFO_OFFSET (binfo) = size_zero_node;
13769 BINFO_TYPE (binfo) = ref;
13771 /* Apply base-class info set up to the variants of this type. */
13772 fixup_type_variants (ref);
13774 if (max_bases)
13776 vec_alloc (BINFO_BASE_ACCESSES (binfo), max_bases);
13777 /* A C++98 POD cannot have base classes. */
13778 CLASSTYPE_NON_LAYOUT_POD_P (ref) = true;
13780 if (TREE_CODE (ref) == UNION_TYPE)
13781 error ("derived union %qT invalid", ref);
13784 if (max_bases > 1)
13785 warning (OPT_Wmultiple_inheritance,
13786 "%qT defined with multiple direct bases", ref);
13788 if (max_vbases)
13790 vec_alloc (CLASSTYPE_VBASECLASSES (ref), max_vbases);
13792 if (max_dvbases)
13793 warning (OPT_Wvirtual_inheritance,
13794 "%qT defined with direct virtual base", ref);
13797 for (igo_prev = binfo; base_list; base_list = TREE_CHAIN (base_list))
13799 tree access = TREE_PURPOSE (base_list);
13800 int via_virtual = TREE_TYPE (base_list) != NULL_TREE;
13801 tree basetype = TREE_VALUE (base_list);
13803 if (access == access_default_node)
13804 access = default_access;
13806 /* Before C++17, an aggregate cannot have base classes. In C++17, an
13807 aggregate can't have virtual, private, or protected base classes. */
13808 if (cxx_dialect < cxx1z
13809 || access != access_public_node
13810 || via_virtual)
13811 CLASSTYPE_NON_AGGREGATE (ref) = true;
13813 if (PACK_EXPANSION_P (basetype))
13814 basetype = PACK_EXPANSION_PATTERN (basetype);
13815 if (TREE_CODE (basetype) == TYPE_DECL)
13816 basetype = TREE_TYPE (basetype);
13817 if (!MAYBE_CLASS_TYPE_P (basetype) || TREE_CODE (basetype) == UNION_TYPE)
13819 error ("base type %qT fails to be a struct or class type",
13820 basetype);
13821 goto dropped_base;
13824 base_binfo = NULL_TREE;
13825 if (CLASS_TYPE_P (basetype) && !dependent_scope_p (basetype))
13827 base_binfo = TYPE_BINFO (basetype);
13828 /* The original basetype could have been a typedef'd type. */
13829 basetype = BINFO_TYPE (base_binfo);
13831 /* Inherit flags from the base. */
13832 TYPE_HAS_NEW_OPERATOR (ref)
13833 |= TYPE_HAS_NEW_OPERATOR (basetype);
13834 TYPE_HAS_ARRAY_NEW_OPERATOR (ref)
13835 |= TYPE_HAS_ARRAY_NEW_OPERATOR (basetype);
13836 TYPE_GETS_DELETE (ref) |= TYPE_GETS_DELETE (basetype);
13837 TYPE_HAS_CONVERSION (ref) |= TYPE_HAS_CONVERSION (basetype);
13838 CLASSTYPE_DIAMOND_SHAPED_P (ref)
13839 |= CLASSTYPE_DIAMOND_SHAPED_P (basetype);
13840 CLASSTYPE_REPEATED_BASE_P (ref)
13841 |= CLASSTYPE_REPEATED_BASE_P (basetype);
13844 /* We must do this test after we've seen through a typedef
13845 type. */
13846 if (TYPE_MARKED_P (basetype))
13848 if (basetype == ref)
13849 error ("recursive type %qT undefined", basetype);
13850 else
13851 error ("duplicate base type %qT invalid", basetype);
13852 goto dropped_base;
13855 if (PACK_EXPANSION_P (TREE_VALUE (base_list)))
13856 /* Regenerate the pack expansion for the bases. */
13857 basetype = make_pack_expansion (basetype);
13859 TYPE_MARKED_P (basetype) = 1;
13861 base_binfo = copy_binfo (base_binfo, basetype, ref,
13862 &igo_prev, via_virtual);
13863 if (!BINFO_INHERITANCE_CHAIN (base_binfo))
13864 BINFO_INHERITANCE_CHAIN (base_binfo) = binfo;
13866 BINFO_BASE_APPEND (binfo, base_binfo);
13867 BINFO_BASE_ACCESS_APPEND (binfo, access);
13868 continue;
13870 dropped_base:
13871 /* Update max_vbases to reflect the reality that we are dropping
13872 this base: if it reaches zero we want to undo the vec_alloc
13873 above to avoid inconsistencies during error-recovery: eg, in
13874 build_special_member_call, CLASSTYPE_VBASECLASSES non null
13875 and vtt null (c++/27952). */
13876 if (via_virtual)
13877 max_vbases--;
13878 if (CLASS_TYPE_P (basetype))
13879 max_vbases
13880 -= vec_safe_length (CLASSTYPE_VBASECLASSES (basetype));
13883 if (CLASSTYPE_VBASECLASSES (ref)
13884 && max_vbases == 0)
13885 vec_free (CLASSTYPE_VBASECLASSES (ref));
13887 if (vec_safe_length (CLASSTYPE_VBASECLASSES (ref)) < max_vbases)
13888 /* If we didn't get max_vbases vbases, we must have shared at
13889 least one of them, and are therefore diamond shaped. */
13890 CLASSTYPE_DIAMOND_SHAPED_P (ref) = 1;
13892 /* Unmark all the types. */
13893 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
13894 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
13895 TYPE_MARKED_P (ref) = 0;
13897 /* Now see if we have a repeated base type. */
13898 if (!CLASSTYPE_REPEATED_BASE_P (ref))
13900 for (base_binfo = binfo; base_binfo;
13901 base_binfo = TREE_CHAIN (base_binfo))
13903 if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
13905 CLASSTYPE_REPEATED_BASE_P (ref) = 1;
13906 break;
13908 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 1;
13910 for (base_binfo = binfo; base_binfo;
13911 base_binfo = TREE_CHAIN (base_binfo))
13912 if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
13913 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
13914 else
13915 break;
13920 /* Copies the enum-related properties from type SRC to type DST.
13921 Used with the underlying type of an enum and the enum itself. */
13922 static void
13923 copy_type_enum (tree dst, tree src)
13925 tree t;
13926 for (t = dst; t; t = TYPE_NEXT_VARIANT (t))
13928 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (src);
13929 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (src);
13930 TYPE_SIZE (t) = TYPE_SIZE (src);
13931 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (src);
13932 SET_TYPE_MODE (dst, TYPE_MODE (src));
13933 TYPE_PRECISION (t) = TYPE_PRECISION (src);
13934 unsigned valign = TYPE_ALIGN (src);
13935 if (TYPE_USER_ALIGN (t))
13936 valign = MAX (valign, TYPE_ALIGN (t));
13937 else
13938 TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (src);
13939 SET_TYPE_ALIGN (t, valign);
13940 TYPE_UNSIGNED (t) = TYPE_UNSIGNED (src);
13944 /* Begin compiling the definition of an enumeration type.
13945 NAME is its name,
13947 if ENUMTYPE is not NULL_TREE then the type has alredy been found.
13949 UNDERLYING_TYPE is the type that will be used as the storage for
13950 the enumeration type. This should be NULL_TREE if no storage type
13951 was specified.
13953 ATTRIBUTES are any attributes specified after the enum-key.
13955 SCOPED_ENUM_P is true if this is a scoped enumeration type.
13957 if IS_NEW is not NULL, gets TRUE iff a new type is created.
13959 Returns the type object, as yet incomplete.
13960 Also records info about it so that build_enumerator
13961 may be used to declare the individual values as they are read. */
13963 tree
13964 start_enum (tree name, tree enumtype, tree underlying_type,
13965 tree attributes, bool scoped_enum_p, bool *is_new)
13967 tree prevtype = NULL_TREE;
13968 gcc_assert (identifier_p (name));
13970 if (is_new)
13971 *is_new = false;
13972 /* [C++0x dcl.enum]p5:
13974 If not explicitly specified, the underlying type of a scoped
13975 enumeration type is int. */
13976 if (!underlying_type && scoped_enum_p)
13977 underlying_type = integer_type_node;
13979 if (underlying_type)
13980 underlying_type = cv_unqualified (underlying_type);
13982 /* If this is the real definition for a previous forward reference,
13983 fill in the contents in the same object that used to be the
13984 forward reference. */
13985 if (!enumtype)
13986 enumtype = lookup_and_check_tag (enum_type, name,
13987 /*tag_scope=*/ts_current,
13988 /*template_header_p=*/false);
13990 /* In case of a template_decl, the only check that should be deferred
13991 to instantiation time is the comparison of underlying types. */
13992 if (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE)
13994 if (scoped_enum_p != SCOPED_ENUM_P (enumtype))
13996 error_at (input_location, "scoped/unscoped mismatch "
13997 "in enum %q#T", enumtype);
13998 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
13999 "previous definition here");
14000 enumtype = error_mark_node;
14002 else if (ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) != !! underlying_type)
14004 error_at (input_location, "underlying type mismatch "
14005 "in enum %q#T", enumtype);
14006 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
14007 "previous definition here");
14008 enumtype = error_mark_node;
14010 else if (underlying_type && ENUM_UNDERLYING_TYPE (enumtype)
14011 && !dependent_type_p (underlying_type)
14012 && !dependent_type_p (ENUM_UNDERLYING_TYPE (enumtype))
14013 && !same_type_p (underlying_type,
14014 ENUM_UNDERLYING_TYPE (enumtype)))
14016 error_at (input_location, "different underlying type "
14017 "in enum %q#T", enumtype);
14018 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
14019 "previous definition here");
14020 underlying_type = NULL_TREE;
14024 if (!enumtype || TREE_CODE (enumtype) != ENUMERAL_TYPE
14025 || processing_template_decl)
14027 /* In case of error, make a dummy enum to allow parsing to
14028 continue. */
14029 if (enumtype == error_mark_node)
14031 name = make_anon_name ();
14032 enumtype = NULL_TREE;
14035 /* enumtype may be an ENUMERAL_TYPE if this is a redefinition
14036 of an opaque enum, or an opaque enum of an already defined
14037 enumeration (C++0x only).
14038 In any other case, it'll be NULL_TREE. */
14039 if (!enumtype)
14041 if (is_new)
14042 *is_new = true;
14044 prevtype = enumtype;
14046 /* Do not push the decl more than once, unless we need to
14047 compare underlying types at instantiation time */
14048 if (!enumtype
14049 || TREE_CODE (enumtype) != ENUMERAL_TYPE
14050 || (underlying_type
14051 && dependent_type_p (underlying_type))
14052 || (ENUM_UNDERLYING_TYPE (enumtype)
14053 && dependent_type_p (ENUM_UNDERLYING_TYPE (enumtype))))
14055 enumtype = cxx_make_type (ENUMERAL_TYPE);
14056 enumtype = pushtag (name, enumtype, /*tag_scope=*/ts_current);
14058 else
14059 enumtype = xref_tag (enum_type, name, /*tag_scope=*/ts_current,
14060 false);
14062 if (enumtype == error_mark_node)
14063 return error_mark_node;
14065 /* The enum is considered opaque until the opening '{' of the
14066 enumerator list. */
14067 SET_OPAQUE_ENUM_P (enumtype, true);
14068 ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) = !! underlying_type;
14071 SET_SCOPED_ENUM_P (enumtype, scoped_enum_p);
14073 cplus_decl_attributes (&enumtype, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
14075 if (underlying_type)
14077 if (ENUM_UNDERLYING_TYPE (enumtype))
14078 /* We already checked that it matches, don't change it to a different
14079 typedef variant. */;
14080 else if (CP_INTEGRAL_TYPE_P (underlying_type))
14082 copy_type_enum (enumtype, underlying_type);
14083 ENUM_UNDERLYING_TYPE (enumtype) = underlying_type;
14085 else if (dependent_type_p (underlying_type))
14086 ENUM_UNDERLYING_TYPE (enumtype) = underlying_type;
14087 else
14088 error ("underlying type %<%T%> of %<%T%> must be an integral type",
14089 underlying_type, enumtype);
14092 /* If into a template class, the returned enum is always the first
14093 declaration (opaque or not) seen. This way all the references to
14094 this type will be to the same declaration. The following ones are used
14095 only to check for definition errors. */
14096 if (prevtype && processing_template_decl)
14097 return prevtype;
14098 else
14099 return enumtype;
14102 /* After processing and defining all the values of an enumeration type,
14103 install their decls in the enumeration type.
14104 ENUMTYPE is the type object. */
14106 void
14107 finish_enum_value_list (tree enumtype)
14109 tree values;
14110 tree underlying_type;
14111 tree decl;
14112 tree value;
14113 tree minnode, maxnode;
14114 tree t;
14116 bool fixed_underlying_type_p
14117 = ENUM_UNDERLYING_TYPE (enumtype) != NULL_TREE;
14119 /* We built up the VALUES in reverse order. */
14120 TYPE_VALUES (enumtype) = nreverse (TYPE_VALUES (enumtype));
14122 /* For an enum defined in a template, just set the type of the values;
14123 all further processing is postponed until the template is
14124 instantiated. We need to set the type so that tsubst of a CONST_DECL
14125 works. */
14126 if (processing_template_decl)
14128 for (values = TYPE_VALUES (enumtype);
14129 values;
14130 values = TREE_CHAIN (values))
14131 TREE_TYPE (TREE_VALUE (values)) = enumtype;
14132 return;
14135 /* Determine the minimum and maximum values of the enumerators. */
14136 if (TYPE_VALUES (enumtype))
14138 minnode = maxnode = NULL_TREE;
14140 for (values = TYPE_VALUES (enumtype);
14141 values;
14142 values = TREE_CHAIN (values))
14144 decl = TREE_VALUE (values);
14146 /* [dcl.enum]: Following the closing brace of an enum-specifier,
14147 each enumerator has the type of its enumeration. Prior to the
14148 closing brace, the type of each enumerator is the type of its
14149 initializing value. */
14150 TREE_TYPE (decl) = enumtype;
14152 /* Update the minimum and maximum values, if appropriate. */
14153 value = DECL_INITIAL (decl);
14154 if (value == error_mark_node)
14155 value = integer_zero_node;
14156 /* Figure out what the minimum and maximum values of the
14157 enumerators are. */
14158 if (!minnode)
14159 minnode = maxnode = value;
14160 else if (tree_int_cst_lt (maxnode, value))
14161 maxnode = value;
14162 else if (tree_int_cst_lt (value, minnode))
14163 minnode = value;
14166 else
14167 /* [dcl.enum]
14169 If the enumerator-list is empty, the underlying type is as if
14170 the enumeration had a single enumerator with value 0. */
14171 minnode = maxnode = integer_zero_node;
14173 if (!fixed_underlying_type_p)
14175 /* Compute the number of bits require to represent all values of the
14176 enumeration. We must do this before the type of MINNODE and
14177 MAXNODE are transformed, since tree_int_cst_min_precision relies
14178 on the TREE_TYPE of the value it is passed. */
14179 signop sgn = tree_int_cst_sgn (minnode) >= 0 ? UNSIGNED : SIGNED;
14180 int lowprec = tree_int_cst_min_precision (minnode, sgn);
14181 int highprec = tree_int_cst_min_precision (maxnode, sgn);
14182 int precision = MAX (lowprec, highprec);
14183 unsigned int itk;
14184 bool use_short_enum;
14186 /* Determine the underlying type of the enumeration.
14188 [dcl.enum]
14190 The underlying type of an enumeration is an integral type that
14191 can represent all the enumerator values defined in the
14192 enumeration. It is implementation-defined which integral type is
14193 used as the underlying type for an enumeration except that the
14194 underlying type shall not be larger than int unless the value of
14195 an enumerator cannot fit in an int or unsigned int.
14197 We use "int" or an "unsigned int" as the underlying type, even if
14198 a smaller integral type would work, unless the user has
14199 explicitly requested that we use the smallest possible type. The
14200 user can request that for all enumerations with a command line
14201 flag, or for just one enumeration with an attribute. */
14203 use_short_enum = flag_short_enums
14204 || lookup_attribute ("packed", TYPE_ATTRIBUTES (enumtype));
14206 /* If the precision of the type was specified with an attribute and it
14207 was too small, give an error. Otherwise, use it. */
14208 if (TYPE_PRECISION (enumtype))
14210 if (precision > TYPE_PRECISION (enumtype))
14211 error ("specified mode too small for enumeral values");
14212 else
14214 use_short_enum = true;
14215 precision = TYPE_PRECISION (enumtype);
14219 for (itk = (use_short_enum ? itk_char : itk_int);
14220 itk != itk_none;
14221 itk++)
14223 underlying_type = integer_types[itk];
14224 if (underlying_type != NULL_TREE
14225 && TYPE_PRECISION (underlying_type) >= precision
14226 && TYPE_SIGN (underlying_type) == sgn)
14227 break;
14229 if (itk == itk_none)
14231 /* DR 377
14233 IF no integral type can represent all the enumerator values, the
14234 enumeration is ill-formed. */
14235 error ("no integral type can represent all of the enumerator values "
14236 "for %qT", enumtype);
14237 precision = TYPE_PRECISION (long_long_integer_type_node);
14238 underlying_type = integer_types[itk_unsigned_long_long];
14241 /* [dcl.enum]
14243 The value of sizeof() applied to an enumeration type, an object
14244 of an enumeration type, or an enumerator, is the value of sizeof()
14245 applied to the underlying type. */
14246 copy_type_enum (enumtype, underlying_type);
14248 /* Compute the minimum and maximum values for the type.
14250 [dcl.enum]
14252 For an enumeration where emin is the smallest enumerator and emax
14253 is the largest, the values of the enumeration are the values of the
14254 underlying type in the range bmin to bmax, where bmin and bmax are,
14255 respectively, the smallest and largest values of the smallest bit-
14256 field that can store emin and emax. */
14258 /* The middle-end currently assumes that types with TYPE_PRECISION
14259 narrower than their underlying type are suitably zero or sign
14260 extended to fill their mode. Similarly, it assumes that the front
14261 end assures that a value of a particular type must be within
14262 TYPE_MIN_VALUE and TYPE_MAX_VALUE.
14264 We used to set these fields based on bmin and bmax, but that led
14265 to invalid assumptions like optimizing away bounds checking. So
14266 now we just set the TYPE_PRECISION, TYPE_MIN_VALUE, and
14267 TYPE_MAX_VALUE to the values for the mode above and only restrict
14268 the ENUM_UNDERLYING_TYPE for the benefit of diagnostics. */
14269 ENUM_UNDERLYING_TYPE (enumtype)
14270 = build_distinct_type_copy (underlying_type);
14271 TYPE_PRECISION (ENUM_UNDERLYING_TYPE (enumtype)) = precision;
14272 set_min_and_max_values_for_integral_type
14273 (ENUM_UNDERLYING_TYPE (enumtype), precision, sgn);
14275 /* If -fstrict-enums, still constrain TYPE_MIN/MAX_VALUE. */
14276 if (flag_strict_enums)
14277 set_min_and_max_values_for_integral_type (enumtype, precision, sgn);
14279 else
14280 underlying_type = ENUM_UNDERLYING_TYPE (enumtype);
14282 /* Convert each of the enumerators to the type of the underlying
14283 type of the enumeration. */
14284 for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values))
14286 location_t saved_location;
14288 decl = TREE_VALUE (values);
14289 saved_location = input_location;
14290 input_location = DECL_SOURCE_LOCATION (decl);
14291 if (fixed_underlying_type_p)
14292 /* If the enumeration type has a fixed underlying type, we
14293 already checked all of the enumerator values. */
14294 value = DECL_INITIAL (decl);
14295 else
14296 value = perform_implicit_conversion (underlying_type,
14297 DECL_INITIAL (decl),
14298 tf_warning_or_error);
14299 input_location = saved_location;
14301 /* Do not clobber shared ints. */
14302 value = copy_node (value);
14304 TREE_TYPE (value) = enumtype;
14305 DECL_INITIAL (decl) = value;
14308 /* Fix up all variant types of this enum type. */
14309 for (t = TYPE_MAIN_VARIANT (enumtype); t; t = TYPE_NEXT_VARIANT (t))
14310 TYPE_VALUES (t) = TYPE_VALUES (enumtype);
14312 if (at_class_scope_p ()
14313 && COMPLETE_TYPE_P (current_class_type)
14314 && UNSCOPED_ENUM_P (enumtype))
14316 insert_late_enum_def_into_classtype_sorted_fields (enumtype,
14317 current_class_type);
14318 fixup_type_variants (current_class_type);
14321 /* Finish debugging output for this type. */
14322 rest_of_type_compilation (enumtype, namespace_bindings_p ());
14324 /* Each enumerator now has the type of its enumeration. Clear the cache
14325 so that this change in types doesn't confuse us later on. */
14326 clear_cv_and_fold_caches ();
14329 /* Finishes the enum type. This is called only the first time an
14330 enumeration is seen, be it opaque or odinary.
14331 ENUMTYPE is the type object. */
14333 void
14334 finish_enum (tree enumtype)
14336 if (processing_template_decl)
14338 if (at_function_scope_p ())
14339 add_stmt (build_min (TAG_DEFN, enumtype));
14340 return;
14343 /* If this is a forward declaration, there should not be any variants,
14344 though we can get a variant in the middle of an enum-specifier with
14345 wacky code like 'enum E { e = sizeof(const E*) };' */
14346 gcc_assert (enumtype == TYPE_MAIN_VARIANT (enumtype)
14347 && (TYPE_VALUES (enumtype)
14348 || !TYPE_NEXT_VARIANT (enumtype)));
14351 /* Build and install a CONST_DECL for an enumeration constant of the
14352 enumeration type ENUMTYPE whose NAME and VALUE (if any) are provided.
14353 Apply ATTRIBUTES if available. LOC is the location of NAME.
14354 Assignment of sequential values by default is handled here. */
14356 void
14357 build_enumerator (tree name, tree value, tree enumtype, tree attributes,
14358 location_t loc)
14360 tree decl;
14361 tree context;
14362 tree type;
14364 /* scalar_constant_value will pull out this expression, so make sure
14365 it's folded as appropriate. */
14366 if (processing_template_decl)
14367 value = fold_non_dependent_expr (value);
14369 /* If the VALUE was erroneous, pretend it wasn't there; that will
14370 result in the enum being assigned the next value in sequence. */
14371 if (value == error_mark_node)
14372 value = NULL_TREE;
14374 /* Remove no-op casts from the value. */
14375 if (value)
14376 STRIP_TYPE_NOPS (value);
14378 if (! processing_template_decl)
14380 /* Validate and default VALUE. */
14381 if (value != NULL_TREE)
14383 if (!ENUM_UNDERLYING_TYPE (enumtype))
14385 tree tmp_value = build_expr_type_conversion (WANT_INT | WANT_ENUM,
14386 value, true);
14387 if (tmp_value)
14388 value = tmp_value;
14390 else if (! INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P
14391 (TREE_TYPE (value)))
14392 value = perform_implicit_conversion_flags
14393 (ENUM_UNDERLYING_TYPE (enumtype), value, tf_warning_or_error,
14394 LOOKUP_IMPLICIT | LOOKUP_NO_NARROWING);
14396 if (value == error_mark_node)
14397 value = NULL_TREE;
14399 if (value != NULL_TREE)
14401 if (! INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P
14402 (TREE_TYPE (value)))
14404 error ("enumerator value for %qD must have integral or "
14405 "unscoped enumeration type", name);
14406 value = NULL_TREE;
14408 else
14410 value = cxx_constant_value (value);
14412 if (TREE_CODE (value) != INTEGER_CST)
14414 error ("enumerator value for %qD is not an integer "
14415 "constant", name);
14416 value = NULL_TREE;
14422 /* Default based on previous value. */
14423 if (value == NULL_TREE)
14425 if (TYPE_VALUES (enumtype))
14427 tree prev_value;
14428 bool overflowed;
14430 /* C++03 7.2/4: If no initializer is specified for the first
14431 enumerator, the type is an unspecified integral
14432 type. Otherwise the type is the same as the type of the
14433 initializing value of the preceding enumerator unless the
14434 incremented value is not representable in that type, in
14435 which case the type is an unspecified integral type
14436 sufficient to contain the incremented value. */
14437 prev_value = DECL_INITIAL (TREE_VALUE (TYPE_VALUES (enumtype)));
14438 if (error_operand_p (prev_value))
14439 value = error_mark_node;
14440 else
14442 tree type = TREE_TYPE (prev_value);
14443 signop sgn = TYPE_SIGN (type);
14444 widest_int wi = wi::add (wi::to_widest (prev_value), 1, sgn,
14445 &overflowed);
14446 if (!overflowed)
14448 bool pos = !wi::neg_p (wi, sgn);
14449 if (!wi::fits_to_tree_p (wi, type))
14451 unsigned int itk;
14452 for (itk = itk_int; itk != itk_none; itk++)
14454 type = integer_types[itk];
14455 if (type != NULL_TREE
14456 && (pos || !TYPE_UNSIGNED (type))
14457 && wi::fits_to_tree_p (wi, type))
14458 break;
14460 if (type && cxx_dialect < cxx11
14461 && itk > itk_unsigned_long)
14462 pedwarn (input_location, OPT_Wlong_long, pos ? "\
14463 incremented enumerator value is too large for %<unsigned long%>" : "\
14464 incremented enumerator value is too large for %<long%>");
14466 if (type == NULL_TREE)
14467 overflowed = true;
14468 else
14469 value = wide_int_to_tree (type, wi);
14472 if (overflowed)
14474 error ("overflow in enumeration values at %qD", name);
14475 value = error_mark_node;
14479 else
14480 value = integer_zero_node;
14483 /* Remove no-op casts from the value. */
14484 STRIP_TYPE_NOPS (value);
14486 /* If the underlying type of the enum is fixed, check whether
14487 the enumerator values fits in the underlying type. If it
14488 does not fit, the program is ill-formed [C++0x dcl.enum]. */
14489 if (ENUM_UNDERLYING_TYPE (enumtype)
14490 && value
14491 && TREE_CODE (value) == INTEGER_CST)
14493 if (!int_fits_type_p (value, ENUM_UNDERLYING_TYPE (enumtype)))
14494 error ("enumerator value %E is outside the range of underlying "
14495 "type %<%T%>", value, ENUM_UNDERLYING_TYPE (enumtype));
14497 /* Convert the value to the appropriate type. */
14498 value = fold_convert (ENUM_UNDERLYING_TYPE (enumtype), value);
14502 /* C++ associates enums with global, function, or class declarations. */
14503 context = current_scope ();
14505 /* Build the actual enumeration constant. Note that the enumeration
14506 constants have the underlying type of the enum (if it is fixed)
14507 or the type of their initializer (if the underlying type of the
14508 enum is not fixed):
14510 [ C++0x dcl.enum ]
14512 If the underlying type is fixed, the type of each enumerator
14513 prior to the closing brace is the underlying type; if the
14514 initializing value of an enumerator cannot be represented by
14515 the underlying type, the program is ill-formed. If the
14516 underlying type is not fixed, the type of each enumerator is
14517 the type of its initializing value.
14519 If the underlying type is not fixed, it will be computed by
14520 finish_enum and we will reset the type of this enumerator. Of
14521 course, if we're processing a template, there may be no value. */
14522 type = value ? TREE_TYPE (value) : NULL_TREE;
14524 decl = build_decl (loc, CONST_DECL, name, type);
14526 DECL_CONTEXT (decl) = enumtype;
14527 TREE_CONSTANT (decl) = 1;
14528 TREE_READONLY (decl) = 1;
14529 DECL_INITIAL (decl) = value;
14531 if (attributes)
14532 cplus_decl_attributes (&decl, attributes, 0);
14534 if (context && context == current_class_type && !SCOPED_ENUM_P (enumtype))
14536 /* In something like `struct S { enum E { i = 7 }; };' we put `i'
14537 on the TYPE_FIELDS list for `S'. (That's so that you can say
14538 things like `S::i' later.) */
14540 /* The enumerator may be getting declared outside of its enclosing
14541 class, like so:
14543 class S { public: enum E : int; }; enum S::E : int { i = 7; };
14545 For which case we need to make sure that the access of `S::i'
14546 matches the access of `S::E'. */
14547 tree saved_cas = current_access_specifier;
14548 if (TREE_PRIVATE (TYPE_NAME (enumtype)))
14549 current_access_specifier = access_private_node;
14550 else if (TREE_PROTECTED (TYPE_NAME (enumtype)))
14551 current_access_specifier = access_protected_node;
14552 else
14553 current_access_specifier = access_public_node;
14555 finish_member_declaration (decl);
14557 current_access_specifier = saved_cas;
14559 else
14560 pushdecl (decl);
14562 /* Add this enumeration constant to the list for this type. */
14563 TYPE_VALUES (enumtype) = tree_cons (name, decl, TYPE_VALUES (enumtype));
14566 /* Look for an enumerator with the given NAME within the enumeration
14567 type ENUMTYPE. This routine is used primarily for qualified name
14568 lookup into an enumerator in C++0x, e.g.,
14570 enum class Color { Red, Green, Blue };
14572 Color color = Color::Red;
14574 Returns the value corresponding to the enumerator, or
14575 NULL_TREE if no such enumerator was found. */
14576 tree
14577 lookup_enumerator (tree enumtype, tree name)
14579 tree e;
14580 gcc_assert (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE);
14582 e = purpose_member (name, TYPE_VALUES (enumtype));
14583 return e? TREE_VALUE (e) : NULL_TREE;
14587 /* We're defining DECL. Make sure that its type is OK. */
14589 static void
14590 check_function_type (tree decl, tree current_function_parms)
14592 tree fntype = TREE_TYPE (decl);
14593 tree return_type = complete_type (TREE_TYPE (fntype));
14595 /* In a function definition, arg types must be complete. */
14596 require_complete_types_for_parms (current_function_parms);
14598 if (dependent_type_p (return_type)
14599 || type_uses_auto (return_type))
14600 return;
14601 if (!COMPLETE_OR_VOID_TYPE_P (return_type))
14603 tree args = TYPE_ARG_TYPES (fntype);
14605 error ("return type %q#T is incomplete", return_type);
14607 /* Make it return void instead. */
14608 if (TREE_CODE (fntype) == METHOD_TYPE)
14609 fntype = build_method_type_directly (TREE_TYPE (TREE_VALUE (args)),
14610 void_type_node,
14611 TREE_CHAIN (args));
14612 else
14613 fntype = build_function_type (void_type_node, args);
14614 fntype
14615 = build_exception_variant (fntype,
14616 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl)));
14617 fntype = (cp_build_type_attribute_variant
14618 (fntype, TYPE_ATTRIBUTES (TREE_TYPE (decl))));
14619 TREE_TYPE (decl) = fntype;
14621 else
14622 abstract_virtuals_error (decl, TREE_TYPE (fntype));
14625 /* True iff FN is an implicitly-defined default constructor. */
14627 static bool
14628 implicit_default_ctor_p (tree fn)
14630 return (DECL_CONSTRUCTOR_P (fn)
14631 && !user_provided_p (fn)
14632 && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn)));
14635 /* Clobber the contents of *this to let the back end know that the object
14636 storage is dead when we enter the constructor or leave the destructor. */
14638 static tree
14639 build_clobber_this ()
14641 /* Clobbering an empty base is pointless, and harmful if its one byte
14642 TYPE_SIZE overlays real data. */
14643 if (is_empty_class (current_class_type))
14644 return void_node;
14646 /* If we have virtual bases, clobber the whole object, but only if we're in
14647 charge. If we don't have virtual bases, clobber the as-base type so we
14648 don't mess with tail padding. */
14649 bool vbases = CLASSTYPE_VBASECLASSES (current_class_type);
14651 tree ctype = current_class_type;
14652 if (!vbases)
14653 ctype = CLASSTYPE_AS_BASE (ctype);
14655 tree clobber = build_constructor (ctype, NULL);
14656 TREE_THIS_VOLATILE (clobber) = true;
14658 tree thisref = current_class_ref;
14659 if (ctype != current_class_type)
14661 thisref = build_nop (build_reference_type (ctype), current_class_ptr);
14662 thisref = convert_from_reference (thisref);
14665 tree exprstmt = build2 (MODIFY_EXPR, void_type_node, thisref, clobber);
14666 if (vbases)
14667 exprstmt = build_if_in_charge (exprstmt);
14669 return exprstmt;
14672 /* Create the FUNCTION_DECL for a function definition.
14673 DECLSPECS and DECLARATOR are the parts of the declaration;
14674 they describe the function's name and the type it returns,
14675 but twisted together in a fashion that parallels the syntax of C.
14677 FLAGS is a bitwise or of SF_PRE_PARSED (indicating that the
14678 DECLARATOR is really the DECL for the function we are about to
14679 process and that DECLSPECS should be ignored), SF_INCLASS_INLINE
14680 indicating that the function is an inline defined in-class.
14682 This function creates a binding context for the function body
14683 as well as setting up the FUNCTION_DECL in current_function_decl.
14685 For C++, we must first check whether that datum makes any sense.
14686 For example, "class A local_a(1,2);" means that variable local_a
14687 is an aggregate of type A, which should have a constructor
14688 applied to it with the argument list [1, 2].
14690 On entry, DECL_INITIAL (decl1) should be NULL_TREE or error_mark_node,
14691 or may be a BLOCK if the function has been defined previously
14692 in this translation unit. On exit, DECL_INITIAL (decl1) will be
14693 error_mark_node if the function has never been defined, or
14694 a BLOCK if the function has been defined somewhere. */
14696 bool
14697 start_preparsed_function (tree decl1, tree attrs, int flags)
14699 tree ctype = NULL_TREE;
14700 tree fntype;
14701 tree restype;
14702 int doing_friend = 0;
14703 cp_binding_level *bl;
14704 tree current_function_parms;
14705 struct c_fileinfo *finfo
14706 = get_fileinfo (LOCATION_FILE (DECL_SOURCE_LOCATION (decl1)));
14707 bool honor_interface;
14709 /* Sanity check. */
14710 gcc_assert (VOID_TYPE_P (TREE_VALUE (void_list_node)));
14711 gcc_assert (TREE_CHAIN (void_list_node) == NULL_TREE);
14713 fntype = TREE_TYPE (decl1);
14714 if (TREE_CODE (fntype) == METHOD_TYPE)
14715 ctype = TYPE_METHOD_BASETYPE (fntype);
14717 /* ISO C++ 11.4/5. A friend function defined in a class is in
14718 the (lexical) scope of the class in which it is defined. */
14719 if (!ctype && DECL_FRIEND_P (decl1))
14721 ctype = DECL_FRIEND_CONTEXT (decl1);
14723 /* CTYPE could be null here if we're dealing with a template;
14724 for example, `inline friend float foo()' inside a template
14725 will have no CTYPE set. */
14726 if (ctype && TREE_CODE (ctype) != RECORD_TYPE)
14727 ctype = NULL_TREE;
14728 else
14729 doing_friend = 1;
14732 if (DECL_DECLARED_INLINE_P (decl1)
14733 && lookup_attribute ("noinline", attrs))
14734 warning_at (DECL_SOURCE_LOCATION (decl1), 0,
14735 "inline function %qD given attribute noinline", decl1);
14737 /* Handle gnu_inline attribute. */
14738 if (GNU_INLINE_P (decl1))
14740 DECL_EXTERNAL (decl1) = 1;
14741 DECL_NOT_REALLY_EXTERN (decl1) = 0;
14742 DECL_INTERFACE_KNOWN (decl1) = 1;
14743 DECL_DISREGARD_INLINE_LIMITS (decl1) = 1;
14746 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl1))
14747 /* This is a constructor, we must ensure that any default args
14748 introduced by this definition are propagated to the clones
14749 now. The clones are used directly in overload resolution. */
14750 adjust_clone_args (decl1);
14752 /* Sometimes we don't notice that a function is a static member, and
14753 build a METHOD_TYPE for it. Fix that up now. */
14754 gcc_assert (!(ctype != NULL_TREE && DECL_STATIC_FUNCTION_P (decl1)
14755 && TREE_CODE (TREE_TYPE (decl1)) == METHOD_TYPE));
14757 /* Set up current_class_type, and enter the scope of the class, if
14758 appropriate. */
14759 if (ctype)
14760 push_nested_class (ctype);
14761 else if (DECL_STATIC_FUNCTION_P (decl1))
14762 push_nested_class (DECL_CONTEXT (decl1));
14764 /* Now that we have entered the scope of the class, we must restore
14765 the bindings for any template parameters surrounding DECL1, if it
14766 is an inline member template. (Order is important; consider the
14767 case where a template parameter has the same name as a field of
14768 the class.) It is not until after this point that
14769 PROCESSING_TEMPLATE_DECL is guaranteed to be set up correctly. */
14770 if (flags & SF_INCLASS_INLINE)
14771 maybe_begin_member_template_processing (decl1);
14773 /* Effective C++ rule 15. */
14774 if (warn_ecpp
14775 && DECL_OVERLOADED_OPERATOR_P (decl1) == NOP_EXPR
14776 && VOID_TYPE_P (TREE_TYPE (fntype)))
14777 warning (OPT_Weffc__, "%<operator=%> should return a reference to %<*this%>");
14779 /* Make the init_value nonzero so pushdecl knows this is not tentative.
14780 error_mark_node is replaced below (in poplevel) with the BLOCK. */
14781 if (!DECL_INITIAL (decl1))
14782 DECL_INITIAL (decl1) = error_mark_node;
14784 /* This function exists in static storage.
14785 (This does not mean `static' in the C sense!) */
14786 TREE_STATIC (decl1) = 1;
14788 /* We must call push_template_decl after current_class_type is set
14789 up. (If we are processing inline definitions after exiting a
14790 class scope, current_class_type will be NULL_TREE until set above
14791 by push_nested_class.) */
14792 if (processing_template_decl)
14794 tree newdecl1 = push_template_decl (decl1);
14795 if (newdecl1 == error_mark_node)
14797 if (ctype || DECL_STATIC_FUNCTION_P (decl1))
14798 pop_nested_class ();
14799 return false;
14801 decl1 = newdecl1;
14804 /* We are now in the scope of the function being defined. */
14805 current_function_decl = decl1;
14807 /* Save the parm names or decls from this function's declarator
14808 where store_parm_decls will find them. */
14809 current_function_parms = DECL_ARGUMENTS (decl1);
14811 /* Make sure the parameter and return types are reasonable. When
14812 you declare a function, these types can be incomplete, but they
14813 must be complete when you define the function. */
14814 check_function_type (decl1, current_function_parms);
14816 /* Build the return declaration for the function. */
14817 restype = TREE_TYPE (fntype);
14819 if (DECL_RESULT (decl1) == NULL_TREE)
14821 tree resdecl;
14823 resdecl = build_decl (input_location, RESULT_DECL, 0, restype);
14824 DECL_ARTIFICIAL (resdecl) = 1;
14825 DECL_IGNORED_P (resdecl) = 1;
14826 DECL_RESULT (decl1) = resdecl;
14828 cp_apply_type_quals_to_decl (cp_type_quals (restype), resdecl);
14831 /* Let the user know we're compiling this function. */
14832 announce_function (decl1);
14834 /* Record the decl so that the function name is defined.
14835 If we already have a decl for this name, and it is a FUNCTION_DECL,
14836 use the old decl. */
14837 if (!processing_template_decl && !(flags & SF_PRE_PARSED))
14839 /* A specialization is not used to guide overload resolution. */
14840 if (!DECL_FUNCTION_MEMBER_P (decl1)
14841 && !(DECL_USE_TEMPLATE (decl1) &&
14842 PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl1))))
14844 tree olddecl = pushdecl (decl1);
14846 if (olddecl == error_mark_node)
14847 /* If something went wrong when registering the declaration,
14848 use DECL1; we have to have a FUNCTION_DECL to use when
14849 parsing the body of the function. */
14851 else
14853 /* Otherwise, OLDDECL is either a previous declaration
14854 of the same function or DECL1 itself. */
14856 if (warn_missing_declarations
14857 && olddecl == decl1
14858 && !DECL_MAIN_P (decl1)
14859 && TREE_PUBLIC (decl1)
14860 && !DECL_DECLARED_INLINE_P (decl1))
14862 tree context;
14864 /* Check whether DECL1 is in an anonymous
14865 namespace. */
14866 for (context = DECL_CONTEXT (decl1);
14867 context;
14868 context = DECL_CONTEXT (context))
14870 if (TREE_CODE (context) == NAMESPACE_DECL
14871 && DECL_NAME (context) == NULL_TREE)
14872 break;
14875 if (context == NULL)
14876 warning_at (DECL_SOURCE_LOCATION (decl1),
14877 OPT_Wmissing_declarations,
14878 "no previous declaration for %qD", decl1);
14881 decl1 = olddecl;
14884 else
14886 /* We need to set the DECL_CONTEXT. */
14887 if (!DECL_CONTEXT (decl1) && DECL_TEMPLATE_INFO (decl1))
14888 DECL_CONTEXT (decl1) = DECL_CONTEXT (DECL_TI_TEMPLATE (decl1));
14890 fntype = TREE_TYPE (decl1);
14891 restype = TREE_TYPE (fntype);
14893 /* If #pragma weak applies, mark the decl appropriately now.
14894 The pragma only applies to global functions. Because
14895 determining whether or not the #pragma applies involves
14896 computing the mangled name for the declaration, we cannot
14897 apply the pragma until after we have merged this declaration
14898 with any previous declarations; if the original declaration
14899 has a linkage specification, that specification applies to
14900 the definition as well, and may affect the mangled name. */
14901 if (DECL_FILE_SCOPE_P (decl1))
14902 maybe_apply_pragma_weak (decl1);
14905 /* Reset this in case the call to pushdecl changed it. */
14906 current_function_decl = decl1;
14908 gcc_assert (DECL_INITIAL (decl1));
14910 /* This function may already have been parsed, in which case just
14911 return; our caller will skip over the body without parsing. */
14912 if (DECL_INITIAL (decl1) != error_mark_node)
14913 return true;
14915 /* Initialize RTL machinery. We cannot do this until
14916 CURRENT_FUNCTION_DECL and DECL_RESULT are set up. We do this
14917 even when processing a template; this is how we get
14918 CFUN set up, and our per-function variables initialized.
14919 FIXME factor out the non-RTL stuff. */
14920 bl = current_binding_level;
14921 allocate_struct_function (decl1, processing_template_decl);
14923 /* Initialize the language data structures. Whenever we start
14924 a new function, we destroy temporaries in the usual way. */
14925 cfun->language = ggc_cleared_alloc<language_function> ();
14926 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
14927 current_binding_level = bl;
14929 if (!processing_template_decl && type_uses_auto (restype))
14931 FNDECL_USED_AUTO (decl1) = true;
14932 current_function_auto_return_pattern = restype;
14935 /* Start the statement-tree, start the tree now. */
14936 DECL_SAVED_TREE (decl1) = push_stmt_list ();
14938 /* If we are (erroneously) defining a function that we have already
14939 defined before, wipe out what we knew before. */
14940 if (!DECL_PENDING_INLINE_P (decl1))
14941 DECL_SAVED_FUNCTION_DATA (decl1) = NULL;
14943 if (ctype && !doing_friend && !DECL_STATIC_FUNCTION_P (decl1))
14945 /* We know that this was set up by `grokclassfn'. We do not
14946 wait until `store_parm_decls', since evil parse errors may
14947 never get us to that point. Here we keep the consistency
14948 between `current_class_type' and `current_class_ptr'. */
14949 tree t = DECL_ARGUMENTS (decl1);
14951 gcc_assert (t != NULL_TREE && TREE_CODE (t) == PARM_DECL);
14952 gcc_assert (TYPE_PTR_P (TREE_TYPE (t)));
14954 cp_function_chain->x_current_class_ref
14955 = cp_build_indirect_ref (t, RO_NULL, tf_warning_or_error);
14956 /* Set this second to avoid shortcut in cp_build_indirect_ref. */
14957 cp_function_chain->x_current_class_ptr = t;
14959 /* Constructors and destructors need to know whether they're "in
14960 charge" of initializing virtual base classes. */
14961 t = DECL_CHAIN (t);
14962 if (DECL_HAS_IN_CHARGE_PARM_P (decl1))
14964 current_in_charge_parm = t;
14965 t = DECL_CHAIN (t);
14967 if (DECL_HAS_VTT_PARM_P (decl1))
14969 gcc_assert (DECL_NAME (t) == vtt_parm_identifier);
14970 current_vtt_parm = t;
14974 honor_interface = (!DECL_TEMPLATE_INSTANTIATION (decl1)
14975 /* Implicitly-defined methods (like the
14976 destructor for a class in which no destructor
14977 is explicitly declared) must not be defined
14978 until their definition is needed. So, we
14979 ignore interface specifications for
14980 compiler-generated functions. */
14981 && !DECL_ARTIFICIAL (decl1));
14983 if (processing_template_decl)
14984 /* Don't mess with interface flags. */;
14985 else if (DECL_INTERFACE_KNOWN (decl1))
14987 tree ctx = decl_function_context (decl1);
14989 if (DECL_NOT_REALLY_EXTERN (decl1))
14990 DECL_EXTERNAL (decl1) = 0;
14992 if (ctx != NULL_TREE && vague_linkage_p (ctx))
14993 /* This is a function in a local class in an extern inline
14994 or template function. */
14995 comdat_linkage (decl1);
14997 /* If this function belongs to an interface, it is public.
14998 If it belongs to someone else's interface, it is also external.
14999 This only affects inlines and template instantiations. */
15000 else if (!finfo->interface_unknown && honor_interface)
15002 if (DECL_DECLARED_INLINE_P (decl1)
15003 || DECL_TEMPLATE_INSTANTIATION (decl1))
15005 DECL_EXTERNAL (decl1)
15006 = (finfo->interface_only
15007 || (DECL_DECLARED_INLINE_P (decl1)
15008 && ! flag_implement_inlines
15009 && !DECL_VINDEX (decl1)));
15011 /* For WIN32 we also want to put these in linkonce sections. */
15012 maybe_make_one_only (decl1);
15014 else
15015 DECL_EXTERNAL (decl1) = 0;
15016 DECL_INTERFACE_KNOWN (decl1) = 1;
15017 /* If this function is in an interface implemented in this file,
15018 make sure that the back end knows to emit this function
15019 here. */
15020 if (!DECL_EXTERNAL (decl1))
15021 mark_needed (decl1);
15023 else if (finfo->interface_unknown && finfo->interface_only
15024 && honor_interface)
15026 /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma
15027 interface, we will have both finfo->interface_unknown and
15028 finfo->interface_only set. In that case, we don't want to
15029 use the normal heuristics because someone will supply a
15030 #pragma implementation elsewhere, and deducing it here would
15031 produce a conflict. */
15032 comdat_linkage (decl1);
15033 DECL_EXTERNAL (decl1) = 0;
15034 DECL_INTERFACE_KNOWN (decl1) = 1;
15035 DECL_DEFER_OUTPUT (decl1) = 1;
15037 else
15039 /* This is a definition, not a reference.
15040 So clear DECL_EXTERNAL, unless this is a GNU extern inline. */
15041 if (!GNU_INLINE_P (decl1))
15042 DECL_EXTERNAL (decl1) = 0;
15044 if ((DECL_DECLARED_INLINE_P (decl1)
15045 || DECL_TEMPLATE_INSTANTIATION (decl1))
15046 && ! DECL_INTERFACE_KNOWN (decl1))
15047 DECL_DEFER_OUTPUT (decl1) = 1;
15048 else
15049 DECL_INTERFACE_KNOWN (decl1) = 1;
15052 /* Determine the ELF visibility attribute for the function. We must not
15053 do this before calling "pushdecl", as we must allow "duplicate_decls"
15054 to merge any attributes appropriately. We also need to wait until
15055 linkage is set. */
15056 if (!DECL_CLONED_FUNCTION_P (decl1))
15057 determine_visibility (decl1);
15059 if (!processing_template_decl)
15060 maybe_instantiate_noexcept (decl1);
15062 begin_scope (sk_function_parms, decl1);
15064 ++function_depth;
15066 if (DECL_DESTRUCTOR_P (decl1)
15067 || (DECL_CONSTRUCTOR_P (decl1)
15068 && targetm.cxx.cdtor_returns_this ()))
15069 cdtor_label = create_artificial_label (input_location);
15071 start_fname_decls ();
15073 store_parm_decls (current_function_parms);
15075 if (!processing_template_decl
15076 && (flag_lifetime_dse > 1)
15077 && DECL_CONSTRUCTOR_P (decl1)
15078 && !DECL_CLONED_FUNCTION_P (decl1)
15079 /* Clobbering an empty base is harmful if it overlays real data. */
15080 && !is_empty_class (current_class_type)
15081 /* We can't clobber safely for an implicitly-defined default constructor
15082 because part of the initialization might happen before we enter the
15083 constructor, via AGGR_INIT_ZERO_FIRST (c++/68006). */
15084 && !implicit_default_ctor_p (decl1))
15085 finish_expr_stmt (build_clobber_this ());
15087 if (!processing_template_decl
15088 && DECL_CONSTRUCTOR_P (decl1)
15089 && (flag_sanitize & SANITIZE_VPTR)
15090 && !DECL_CLONED_FUNCTION_P (decl1)
15091 && !implicit_default_ctor_p (decl1))
15092 cp_ubsan_maybe_initialize_vtbl_ptrs (current_class_ptr);
15094 return true;
15098 /* Like start_preparsed_function, except that instead of a
15099 FUNCTION_DECL, this function takes DECLSPECS and DECLARATOR.
15101 Returns true on success. If the DECLARATOR is not suitable
15102 for a function, we return false, which tells the parser to
15103 skip the entire function. */
15105 bool
15106 start_function (cp_decl_specifier_seq *declspecs,
15107 const cp_declarator *declarator,
15108 tree attrs)
15110 tree decl1;
15112 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, &attrs);
15113 invoke_plugin_callbacks (PLUGIN_START_PARSE_FUNCTION, decl1);
15114 if (decl1 == error_mark_node)
15115 return false;
15116 /* If the declarator is not suitable for a function definition,
15117 cause a syntax error. */
15118 if (decl1 == NULL_TREE || TREE_CODE (decl1) != FUNCTION_DECL)
15120 error ("invalid function declaration");
15121 return false;
15124 if (DECL_MAIN_P (decl1))
15125 /* main must return int. grokfndecl should have corrected it
15126 (and issued a diagnostic) if the user got it wrong. */
15127 gcc_assert (same_type_p (TREE_TYPE (TREE_TYPE (decl1)),
15128 integer_type_node));
15130 return start_preparsed_function (decl1, attrs, /*flags=*/SF_DEFAULT);
15133 /* Returns true iff an EH_SPEC_BLOCK should be created in the body of
15134 FN. */
15136 static bool
15137 use_eh_spec_block (tree fn)
15139 return (flag_exceptions && flag_enforce_eh_specs
15140 && !processing_template_decl
15141 && !type_throw_all_p (TREE_TYPE (fn))
15142 /* We insert the EH_SPEC_BLOCK only in the original
15143 function; then, it is copied automatically to the
15144 clones. */
15145 && !DECL_CLONED_FUNCTION_P (fn)
15146 /* Implicitly-generated constructors and destructors have
15147 exception specifications. However, those specifications
15148 are the union of the possible exceptions specified by the
15149 constructors/destructors for bases and members, so no
15150 unallowed exception will ever reach this function. By
15151 not creating the EH_SPEC_BLOCK we save a little memory,
15152 and we avoid spurious warnings about unreachable
15153 code. */
15154 && !DECL_DEFAULTED_FN (fn));
15157 /* Store the parameter declarations into the current function declaration.
15158 This is called after parsing the parameter declarations, before
15159 digesting the body of the function.
15161 Also install to binding contour return value identifier, if any. */
15163 static void
15164 store_parm_decls (tree current_function_parms)
15166 tree fndecl = current_function_decl;
15167 tree parm;
15169 /* This is a chain of any other decls that came in among the parm
15170 declarations. If a parm is declared with enum {foo, bar} x;
15171 then CONST_DECLs for foo and bar are put here. */
15172 tree nonparms = NULL_TREE;
15174 if (current_function_parms)
15176 /* This case is when the function was defined with an ANSI prototype.
15177 The parms already have decls, so we need not do anything here
15178 except record them as in effect
15179 and complain if any redundant old-style parm decls were written. */
15181 tree specparms = current_function_parms;
15182 tree next;
15184 /* Must clear this because it might contain TYPE_DECLs declared
15185 at class level. */
15186 current_binding_level->names = NULL;
15188 /* If we're doing semantic analysis, then we'll call pushdecl
15189 for each of these. We must do them in reverse order so that
15190 they end in the correct forward order. */
15191 specparms = nreverse (specparms);
15193 for (parm = specparms; parm; parm = next)
15195 next = DECL_CHAIN (parm);
15196 if (TREE_CODE (parm) == PARM_DECL)
15197 pushdecl (parm);
15198 else
15200 /* If we find an enum constant or a type tag,
15201 put it aside for the moment. */
15202 TREE_CHAIN (parm) = NULL_TREE;
15203 nonparms = chainon (nonparms, parm);
15207 /* Get the decls in their original chain order and record in the
15208 function. This is all and only the PARM_DECLs that were
15209 pushed into scope by the loop above. */
15210 DECL_ARGUMENTS (fndecl) = getdecls ();
15212 else
15213 DECL_ARGUMENTS (fndecl) = NULL_TREE;
15215 /* Now store the final chain of decls for the arguments
15216 as the decl-chain of the current lexical scope.
15217 Put the enumerators in as well, at the front so that
15218 DECL_ARGUMENTS is not modified. */
15219 current_binding_level->names = chainon (nonparms, DECL_ARGUMENTS (fndecl));
15221 if (use_eh_spec_block (current_function_decl))
15222 current_eh_spec_block = begin_eh_spec_block ();
15226 /* We have finished doing semantic analysis on DECL, but have not yet
15227 generated RTL for its body. Save away our current state, so that
15228 when we want to generate RTL later we know what to do. */
15230 static void
15231 save_function_data (tree decl)
15233 struct language_function *f;
15235 /* Save the language-specific per-function data so that we can
15236 get it back when we really expand this function. */
15237 gcc_assert (!DECL_PENDING_INLINE_P (decl));
15239 /* Make a copy. */
15240 f = ggc_alloc<language_function> ();
15241 memcpy (f, cp_function_chain, sizeof (struct language_function));
15242 DECL_SAVED_FUNCTION_DATA (decl) = f;
15244 /* Clear out the bits we don't need. */
15245 f->base.x_stmt_tree.x_cur_stmt_list = NULL;
15246 f->bindings = NULL;
15247 f->x_local_names = NULL;
15248 f->base.local_typedefs = NULL;
15252 /* Set the return value of the constructor (if present). */
15254 static void
15255 finish_constructor_body (void)
15257 tree val;
15258 tree exprstmt;
15260 if (targetm.cxx.cdtor_returns_this ())
15262 /* Any return from a constructor will end up here. */
15263 add_stmt (build_stmt (input_location, LABEL_EXPR, cdtor_label));
15265 val = DECL_ARGUMENTS (current_function_decl);
15266 val = build2 (MODIFY_EXPR, TREE_TYPE (val),
15267 DECL_RESULT (current_function_decl), val);
15268 /* Return the address of the object. */
15269 exprstmt = build_stmt (input_location, RETURN_EXPR, val);
15270 add_stmt (exprstmt);
15274 /* Do all the processing for the beginning of a destructor; set up the
15275 vtable pointers and cleanups for bases and members. */
15277 static void
15278 begin_destructor_body (void)
15280 tree compound_stmt;
15282 /* If the CURRENT_CLASS_TYPE is incomplete, we will have already
15283 issued an error message. We still want to try to process the
15284 body of the function, but initialize_vtbl_ptrs will crash if
15285 TYPE_BINFO is NULL. */
15286 if (COMPLETE_TYPE_P (current_class_type))
15288 compound_stmt = begin_compound_stmt (0);
15289 /* Make all virtual function table pointers in non-virtual base
15290 classes point to CURRENT_CLASS_TYPE's virtual function
15291 tables. */
15292 initialize_vtbl_ptrs (current_class_ptr);
15293 finish_compound_stmt (compound_stmt);
15295 if (flag_lifetime_dse
15296 /* Clobbering an empty base is harmful if it overlays real data. */
15297 && !is_empty_class (current_class_type))
15298 finish_decl_cleanup (NULL_TREE, build_clobber_this ());
15300 /* And insert cleanups for our bases and members so that they
15301 will be properly destroyed if we throw. */
15302 push_base_cleanups ();
15306 /* At the end of every destructor we generate code to delete the object if
15307 necessary. Do that now. */
15309 static void
15310 finish_destructor_body (void)
15312 tree exprstmt;
15314 /* Any return from a destructor will end up here; that way all base
15315 and member cleanups will be run when the function returns. */
15316 add_stmt (build_stmt (input_location, LABEL_EXPR, cdtor_label));
15318 if (targetm.cxx.cdtor_returns_this ())
15320 tree val;
15322 val = DECL_ARGUMENTS (current_function_decl);
15323 val = build2 (MODIFY_EXPR, TREE_TYPE (val),
15324 DECL_RESULT (current_function_decl), val);
15325 /* Return the address of the object. */
15326 exprstmt = build_stmt (input_location, RETURN_EXPR, val);
15327 add_stmt (exprstmt);
15331 /* Do the necessary processing for the beginning of a function body, which
15332 in this case includes member-initializers, but not the catch clauses of
15333 a function-try-block. Currently, this means opening a binding level
15334 for the member-initializers (in a ctor), member cleanups (in a dtor),
15335 and capture proxies (in a lambda operator()). */
15337 tree
15338 begin_function_body (void)
15340 tree stmt;
15342 if (! FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
15343 return NULL_TREE;
15345 if (processing_template_decl)
15346 /* Do nothing now. */;
15347 else
15348 /* Always keep the BLOCK node associated with the outermost pair of
15349 curly braces of a function. These are needed for correct
15350 operation of dwarfout.c. */
15351 keep_next_level (true);
15353 stmt = begin_compound_stmt (BCS_FN_BODY);
15355 if (processing_template_decl)
15356 /* Do nothing now. */;
15357 else if (DECL_DESTRUCTOR_P (current_function_decl))
15358 begin_destructor_body ();
15360 return stmt;
15363 /* Do the processing for the end of a function body. Currently, this means
15364 closing out the cleanups for fully-constructed bases and members, and in
15365 the case of the destructor, deleting the object if desired. Again, this
15366 is only meaningful for [cd]tors, since they are the only functions where
15367 there is a significant distinction between the main body and any
15368 function catch clauses. Handling, say, main() return semantics here
15369 would be wrong, as flowing off the end of a function catch clause for
15370 main() would also need to return 0. */
15372 void
15373 finish_function_body (tree compstmt)
15375 if (compstmt == NULL_TREE)
15376 return;
15378 /* Close the block. */
15379 finish_compound_stmt (compstmt);
15381 if (processing_template_decl)
15382 /* Do nothing now. */;
15383 else if (DECL_CONSTRUCTOR_P (current_function_decl))
15384 finish_constructor_body ();
15385 else if (DECL_DESTRUCTOR_P (current_function_decl))
15386 finish_destructor_body ();
15389 /* Given a function, returns the BLOCK corresponding to the outermost level
15390 of curly braces, skipping the artificial block created for constructor
15391 initializers. */
15393 tree
15394 outer_curly_brace_block (tree fndecl)
15396 tree block = DECL_INITIAL (fndecl);
15397 if (BLOCK_OUTER_CURLY_BRACE_P (block))
15398 return block;
15399 block = BLOCK_SUBBLOCKS (block);
15400 if (BLOCK_OUTER_CURLY_BRACE_P (block))
15401 return block;
15402 block = BLOCK_SUBBLOCKS (block);
15403 gcc_assert (BLOCK_OUTER_CURLY_BRACE_P (block));
15404 return block;
15407 /* If FNDECL is a class's key method, add the class to the list of
15408 keyed classes that should be emitted. */
15410 static void
15411 record_key_method_defined (tree fndecl)
15413 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fndecl)
15414 && DECL_VIRTUAL_P (fndecl)
15415 && !processing_template_decl)
15417 tree fnclass = DECL_CONTEXT (fndecl);
15418 if (fndecl == CLASSTYPE_KEY_METHOD (fnclass))
15419 keyed_classes = tree_cons (NULL_TREE, fnclass, keyed_classes);
15423 /* Subroutine of finish_function.
15424 Save the body of constexpr functions for possible
15425 future compile time evaluation. */
15427 static void
15428 maybe_save_function_definition (tree fun)
15430 if (!processing_template_decl
15431 && DECL_DECLARED_CONSTEXPR_P (fun)
15432 && !cp_function_chain->invalid_constexpr
15433 && !DECL_CLONED_FUNCTION_P (fun))
15434 register_constexpr_fundef (fun, DECL_SAVED_TREE (fun));
15437 /* Finish up a function declaration and compile that function
15438 all the way to assembler language output. The free the storage
15439 for the function definition.
15441 FLAGS is a bitwise or of the following values:
15442 2 - INCLASS_INLINE
15443 We just finished processing the body of an in-class inline
15444 function definition. (This processing will have taken place
15445 after the class definition is complete.) */
15447 tree
15448 finish_function (int flags)
15450 tree fndecl = current_function_decl;
15451 tree fntype, ctype = NULL_TREE;
15452 int inclass_inline = (flags & 2) != 0;
15454 /* When we get some parse errors, we can end up without a
15455 current_function_decl, so cope. */
15456 if (fndecl == NULL_TREE)
15457 return error_mark_node;
15459 if (c_dialect_objc ())
15460 objc_finish_function ();
15462 record_key_method_defined (fndecl);
15464 fntype = TREE_TYPE (fndecl);
15466 /* TREE_READONLY (fndecl) = 1;
15467 This caused &foo to be of type ptr-to-const-function
15468 which then got a warning when stored in a ptr-to-function variable. */
15470 gcc_assert (building_stmt_list_p ());
15471 /* The current function is being defined, so its DECL_INITIAL should
15472 be set, and unless there's a multiple definition, it should be
15473 error_mark_node. */
15474 gcc_assert (DECL_INITIAL (fndecl) == error_mark_node);
15476 /* For a cloned function, we've already got all the code we need;
15477 there's no need to add any extra bits. */
15478 if (!DECL_CLONED_FUNCTION_P (fndecl))
15480 /* Make it so that `main' always returns 0 by default. */
15481 if (DECL_MAIN_P (current_function_decl))
15482 finish_return_stmt (integer_zero_node);
15484 if (use_eh_spec_block (current_function_decl))
15485 finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
15486 (TREE_TYPE (current_function_decl)),
15487 current_eh_spec_block);
15490 /* If we're saving up tree structure, tie off the function now. */
15491 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
15493 if (fn_contains_cilk_spawn_p (cfun) && !processing_template_decl)
15494 cfun->cilk_frame_decl = insert_cilk_frame (fndecl);
15496 finish_fname_decls ();
15498 /* If this function can't throw any exceptions, remember that. */
15499 if (!processing_template_decl
15500 && !cp_function_chain->can_throw
15501 && !flag_non_call_exceptions
15502 && !decl_replaceable_p (fndecl))
15503 TREE_NOTHROW (fndecl) = 1;
15505 /* This must come after expand_function_end because cleanups might
15506 have declarations (from inline functions) that need to go into
15507 this function's blocks. */
15509 /* If the current binding level isn't the outermost binding level
15510 for this function, either there is a bug, or we have experienced
15511 syntax errors and the statement tree is malformed. */
15512 if (current_binding_level->kind != sk_function_parms)
15514 /* Make sure we have already experienced errors. */
15515 gcc_assert (errorcount);
15517 /* Throw away the broken statement tree and extra binding
15518 levels. */
15519 DECL_SAVED_TREE (fndecl) = alloc_stmt_list ();
15521 while (current_binding_level->kind != sk_function_parms)
15523 if (current_binding_level->kind == sk_class)
15524 pop_nested_class ();
15525 else
15526 poplevel (0, 0, 0);
15529 poplevel (1, 0, 1);
15531 /* Statements should always be full-expressions at the outermost set
15532 of curly braces for a function. */
15533 gcc_assert (stmts_are_full_exprs_p ());
15535 /* If there are no return statements in a function with auto return type,
15536 the return type is void. But if the declared type is something like
15537 auto*, this is an error. */
15538 if (!processing_template_decl && FNDECL_USED_AUTO (fndecl)
15539 && TREE_TYPE (fntype) == current_function_auto_return_pattern)
15541 if (!is_auto (current_function_auto_return_pattern)
15542 && !current_function_returns_value && !current_function_returns_null)
15544 error ("no return statements in function returning %qT",
15545 current_function_auto_return_pattern);
15546 inform (input_location, "only plain %<auto%> return type can be "
15547 "deduced to %<void%>");
15549 apply_deduced_return_type (fndecl, void_type_node);
15550 fntype = TREE_TYPE (fndecl);
15553 // If this is a concept, check that the definition is reasonable.
15554 if (DECL_DECLARED_CONCEPT_P (fndecl))
15555 check_function_concept (fndecl);
15557 /* Lambda closure members are implicitly constexpr if possible. */
15558 if (cxx_dialect >= cxx1z
15559 && LAMBDA_TYPE_P (CP_DECL_CONTEXT (fndecl))
15560 && (processing_template_decl
15561 || is_valid_constexpr_fn (fndecl, /*complain*/false))
15562 && potential_constant_expression (DECL_SAVED_TREE (fndecl)))
15563 DECL_DECLARED_CONSTEXPR_P (fndecl) = true;
15565 /* Save constexpr function body before it gets munged by
15566 the NRV transformation. */
15567 maybe_save_function_definition (fndecl);
15569 /* Invoke the pre-genericize plugin before we start munging things. */
15570 if (!processing_template_decl)
15571 invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE, fndecl);
15573 /* Perform delayed folding before NRV transformation. */
15574 if (!processing_template_decl)
15575 cp_fold_function (fndecl);
15577 /* Set up the named return value optimization, if we can. Candidate
15578 variables are selected in check_return_expr. */
15579 if (current_function_return_value)
15581 tree r = current_function_return_value;
15582 tree outer;
15584 if (r != error_mark_node
15585 /* This is only worth doing for fns that return in memory--and
15586 simpler, since we don't have to worry about promoted modes. */
15587 && aggregate_value_p (TREE_TYPE (TREE_TYPE (fndecl)), fndecl)
15588 /* Only allow this for variables declared in the outer scope of
15589 the function so we know that their lifetime always ends with a
15590 return; see g++.dg/opt/nrv6.C. We could be more flexible if
15591 we were to do this optimization in tree-ssa. */
15592 && (outer = outer_curly_brace_block (fndecl))
15593 && chain_member (r, BLOCK_VARS (outer)))
15594 finalize_nrv (&DECL_SAVED_TREE (fndecl), r, DECL_RESULT (fndecl));
15596 current_function_return_value = NULL_TREE;
15599 /* Remember that we were in class scope. */
15600 if (current_class_name)
15601 ctype = current_class_type;
15603 /* Must mark the RESULT_DECL as being in this function. */
15604 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
15606 /* Set the BLOCK_SUPERCONTEXT of the outermost function scope to point
15607 to the FUNCTION_DECL node itself. */
15608 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
15610 /* Save away current state, if appropriate. */
15611 if (!processing_template_decl)
15612 save_function_data (fndecl);
15614 /* Complain if there's just no return statement. */
15615 if (warn_return_type
15616 && !VOID_TYPE_P (TREE_TYPE (fntype))
15617 && !dependent_type_p (TREE_TYPE (fntype))
15618 && !current_function_returns_value && !current_function_returns_null
15619 /* Don't complain if we abort or throw. */
15620 && !current_function_returns_abnormally
15621 /* Don't complain if there's an infinite loop. */
15622 && !current_function_infinite_loop
15623 /* Don't complain if we are declared noreturn. */
15624 && !TREE_THIS_VOLATILE (fndecl)
15625 && !DECL_NAME (DECL_RESULT (fndecl))
15626 && !TREE_NO_WARNING (fndecl)
15627 /* Structor return values (if any) are set by the compiler. */
15628 && !DECL_CONSTRUCTOR_P (fndecl)
15629 && !DECL_DESTRUCTOR_P (fndecl)
15630 && targetm.warn_func_return (fndecl))
15632 warning (OPT_Wreturn_type,
15633 "no return statement in function returning non-void");
15634 TREE_NO_WARNING (fndecl) = 1;
15637 /* Store the end of the function, so that we get good line number
15638 info for the epilogue. */
15639 cfun->function_end_locus = input_location;
15641 /* Complain about parameters that are only set, but never otherwise used. */
15642 if (warn_unused_but_set_parameter
15643 && !processing_template_decl
15644 && errorcount == unused_but_set_errorcount
15645 && !DECL_CLONED_FUNCTION_P (fndecl))
15647 tree decl;
15649 for (decl = DECL_ARGUMENTS (fndecl);
15650 decl;
15651 decl = DECL_CHAIN (decl))
15652 if (TREE_USED (decl)
15653 && TREE_CODE (decl) == PARM_DECL
15654 && !DECL_READ_P (decl)
15655 && DECL_NAME (decl)
15656 && !DECL_ARTIFICIAL (decl)
15657 && !TREE_NO_WARNING (decl)
15658 && !DECL_IN_SYSTEM_HEADER (decl)
15659 && TREE_TYPE (decl) != error_mark_node
15660 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
15661 && (!CLASS_TYPE_P (TREE_TYPE (decl))
15662 || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl))))
15663 warning_at (DECL_SOURCE_LOCATION (decl),
15664 OPT_Wunused_but_set_parameter,
15665 "parameter %qD set but not used", decl);
15666 unused_but_set_errorcount = errorcount;
15669 /* Complain about locally defined typedefs that are not used in this
15670 function. */
15671 maybe_warn_unused_local_typedefs ();
15673 /* Possibly warn about unused parameters. */
15674 if (warn_unused_parameter
15675 && !processing_template_decl
15676 && !DECL_CLONED_FUNCTION_P (fndecl))
15677 do_warn_unused_parameter (fndecl);
15679 /* Genericize before inlining. */
15680 if (!processing_template_decl)
15682 struct language_function *f = DECL_SAVED_FUNCTION_DATA (fndecl);
15683 cp_genericize (fndecl);
15684 /* Clear out the bits we don't need. */
15685 f->x_current_class_ptr = NULL;
15686 f->x_current_class_ref = NULL;
15687 f->x_eh_spec_block = NULL;
15688 f->x_in_charge_parm = NULL;
15689 f->x_vtt_parm = NULL;
15690 f->x_return_value = NULL;
15691 f->bindings = NULL;
15692 f->extern_decl_map = NULL;
15693 f->infinite_loops = NULL;
15695 /* Clear out the bits we don't need. */
15696 local_names = NULL;
15698 /* We're leaving the context of this function, so zap cfun. It's still in
15699 DECL_STRUCT_FUNCTION, and we'll restore it in tree_rest_of_compilation. */
15700 set_cfun (NULL);
15701 current_function_decl = NULL;
15703 /* If this is an in-class inline definition, we may have to pop the
15704 bindings for the template parameters that we added in
15705 maybe_begin_member_template_processing when start_function was
15706 called. */
15707 if (inclass_inline)
15708 maybe_end_member_template_processing ();
15710 /* Leave the scope of the class. */
15711 if (ctype)
15712 pop_nested_class ();
15714 --function_depth;
15716 /* Clean up. */
15717 current_function_decl = NULL_TREE;
15719 invoke_plugin_callbacks (PLUGIN_FINISH_PARSE_FUNCTION, fndecl);
15720 return fndecl;
15723 /* Create the FUNCTION_DECL for a function definition.
15724 DECLSPECS and DECLARATOR are the parts of the declaration;
15725 they describe the return type and the name of the function,
15726 but twisted together in a fashion that parallels the syntax of C.
15728 This function creates a binding context for the function body
15729 as well as setting up the FUNCTION_DECL in current_function_decl.
15731 Returns a FUNCTION_DECL on success.
15733 If the DECLARATOR is not suitable for a function (it defines a datum
15734 instead), we return 0, which tells yyparse to report a parse error.
15736 May return void_type_node indicating that this method is actually
15737 a friend. See grokfield for more details.
15739 Came here with a `.pushlevel' .
15741 DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
15742 CHANGES TO CODE IN `grokfield'. */
15744 tree
15745 grokmethod (cp_decl_specifier_seq *declspecs,
15746 const cp_declarator *declarator, tree attrlist)
15748 tree fndecl = grokdeclarator (declarator, declspecs, MEMFUNCDEF, 0,
15749 &attrlist);
15751 if (fndecl == error_mark_node)
15752 return error_mark_node;
15754 if (fndecl == NULL || TREE_CODE (fndecl) != FUNCTION_DECL)
15756 error ("invalid member function declaration");
15757 return error_mark_node;
15760 if (attrlist)
15761 cplus_decl_attributes (&fndecl, attrlist, 0);
15763 /* Pass friends other than inline friend functions back. */
15764 if (fndecl == void_type_node)
15765 return fndecl;
15767 if (DECL_IN_AGGR_P (fndecl))
15769 if (DECL_CLASS_SCOPE_P (fndecl))
15770 error ("%qD is already defined in class %qT", fndecl,
15771 DECL_CONTEXT (fndecl));
15772 return error_mark_node;
15775 check_template_shadow (fndecl);
15777 if (TREE_PUBLIC (fndecl))
15778 DECL_COMDAT (fndecl) = 1;
15779 DECL_DECLARED_INLINE_P (fndecl) = 1;
15780 DECL_NO_INLINE_WARNING_P (fndecl) = 1;
15782 /* We process method specializations in finish_struct_1. */
15783 if (processing_template_decl && !DECL_TEMPLATE_SPECIALIZATION (fndecl))
15785 fndecl = push_template_decl (fndecl);
15786 if (fndecl == error_mark_node)
15787 return fndecl;
15790 if (! DECL_FRIEND_P (fndecl))
15792 if (DECL_CHAIN (fndecl))
15794 fndecl = copy_node (fndecl);
15795 TREE_CHAIN (fndecl) = NULL_TREE;
15799 cp_finish_decl (fndecl, NULL_TREE, false, NULL_TREE, 0);
15801 DECL_IN_AGGR_P (fndecl) = 1;
15802 return fndecl;
15806 /* VAR is a VAR_DECL. If its type is incomplete, remember VAR so that
15807 we can lay it out later, when and if its type becomes complete.
15809 Also handle constexpr variables where the initializer involves
15810 an unlowered PTRMEM_CST because the class isn't complete yet. */
15812 void
15813 maybe_register_incomplete_var (tree var)
15815 gcc_assert (VAR_P (var));
15817 /* Keep track of variables with incomplete types. */
15818 if (!processing_template_decl && TREE_TYPE (var) != error_mark_node
15819 && DECL_EXTERNAL (var))
15821 tree inner_type = TREE_TYPE (var);
15823 while (TREE_CODE (inner_type) == ARRAY_TYPE)
15824 inner_type = TREE_TYPE (inner_type);
15825 inner_type = TYPE_MAIN_VARIANT (inner_type);
15827 if ((!COMPLETE_TYPE_P (inner_type) && CLASS_TYPE_P (inner_type))
15828 /* RTTI TD entries are created while defining the type_info. */
15829 || (TYPE_LANG_SPECIFIC (inner_type)
15830 && TYPE_BEING_DEFINED (inner_type)))
15832 incomplete_var iv = {var, inner_type};
15833 vec_safe_push (incomplete_vars, iv);
15835 else if (!(DECL_LANG_SPECIFIC (var) && DECL_TEMPLATE_INFO (var))
15836 && decl_constant_var_p (var)
15837 && (TYPE_PTRMEM_P (inner_type) || CLASS_TYPE_P (inner_type)))
15839 /* When the outermost open class is complete we can resolve any
15840 pointers-to-members. */
15841 tree context = outermost_open_class ();
15842 incomplete_var iv = {var, context};
15843 vec_safe_push (incomplete_vars, iv);
15848 /* Called when a class type (given by TYPE) is defined. If there are
15849 any existing VAR_DECLs whose type has been completed by this
15850 declaration, update them now. */
15852 void
15853 complete_vars (tree type)
15855 unsigned ix;
15856 incomplete_var *iv;
15858 for (ix = 0; vec_safe_iterate (incomplete_vars, ix, &iv); )
15860 if (same_type_p (type, iv->incomplete_type))
15862 tree var = iv->decl;
15863 tree type = TREE_TYPE (var);
15865 if (type != error_mark_node
15866 && (TYPE_MAIN_VARIANT (strip_array_types (type))
15867 == iv->incomplete_type))
15869 /* Complete the type of the variable. The VAR_DECL itself
15870 will be laid out in expand_expr. */
15871 complete_type (type);
15872 cp_apply_type_quals_to_decl (cp_type_quals (type), var);
15875 /* Remove this entry from the list. */
15876 incomplete_vars->unordered_remove (ix);
15878 else
15879 ix++;
15882 /* Check for pending declarations which may have abstract type. */
15883 complete_type_check_abstract (type);
15886 /* If DECL is of a type which needs a cleanup, build and return an
15887 expression to perform that cleanup here. Return NULL_TREE if no
15888 cleanup need be done. DECL can also be a _REF when called from
15889 split_nonconstant_init_1. */
15891 tree
15892 cxx_maybe_build_cleanup (tree decl, tsubst_flags_t complain)
15894 tree type;
15895 tree attr;
15896 tree cleanup;
15898 /* Assume no cleanup is required. */
15899 cleanup = NULL_TREE;
15901 if (error_operand_p (decl))
15902 return cleanup;
15904 /* Handle "__attribute__((cleanup))". We run the cleanup function
15905 before the destructor since the destructor is what actually
15906 terminates the lifetime of the object. */
15907 if (DECL_P (decl))
15908 attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
15909 else
15910 attr = NULL_TREE;
15911 if (attr)
15913 tree id;
15914 tree fn;
15915 tree arg;
15917 /* Get the name specified by the user for the cleanup function. */
15918 id = TREE_VALUE (TREE_VALUE (attr));
15919 /* Look up the name to find the cleanup function to call. It is
15920 important to use lookup_name here because that is what is
15921 used in c-common.c:handle_cleanup_attribute when performing
15922 initial checks on the attribute. Note that those checks
15923 include ensuring that the function found is not an overloaded
15924 function, or an object with an overloaded call operator,
15925 etc.; we can rely on the fact that the function found is an
15926 ordinary FUNCTION_DECL. */
15927 fn = lookup_name (id);
15928 arg = build_address (decl);
15929 if (!mark_used (decl, complain) && !(complain & tf_error))
15930 return error_mark_node;
15931 cleanup = cp_build_function_call_nary (fn, complain, arg, NULL_TREE);
15932 if (cleanup == error_mark_node)
15933 return error_mark_node;
15935 /* Handle ordinary C++ destructors. */
15936 type = TREE_TYPE (decl);
15937 if (type_build_dtor_call (type))
15939 int flags = LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR;
15940 tree addr;
15941 tree call;
15943 if (TREE_CODE (type) == ARRAY_TYPE)
15944 addr = decl;
15945 else
15946 addr = build_address (decl);
15948 call = build_delete (TREE_TYPE (addr), addr,
15949 sfk_complete_destructor, flags, 0, complain);
15950 if (call == error_mark_node)
15951 cleanup = error_mark_node;
15952 else if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
15953 /* Discard the call. */;
15954 else if (cleanup)
15955 cleanup = cp_build_compound_expr (cleanup, call, complain);
15956 else
15957 cleanup = call;
15960 /* build_delete sets the location of the destructor call to the
15961 current location, even though the destructor is going to be
15962 called later, at the end of the current scope. This can lead to
15963 a "jumpy" behavior for users of debuggers when they step around
15964 the end of the block. So let's unset the location of the
15965 destructor call instead. */
15966 protected_set_expr_location (cleanup, UNKNOWN_LOCATION);
15968 if (cleanup
15969 && DECL_P (decl)
15970 && !lookup_attribute ("warn_unused", TYPE_ATTRIBUTES (TREE_TYPE (decl)))
15971 /* Treat objects with destructors as used; the destructor may do
15972 something substantive. */
15973 && !mark_used (decl, complain) && !(complain & tf_error))
15974 return error_mark_node;
15976 return cleanup;
15980 /* Return the FUNCTION_TYPE that corresponds to MEMFNTYPE, which can be a
15981 FUNCTION_DECL, METHOD_TYPE, FUNCTION_TYPE, pointer or reference to
15982 METHOD_TYPE or FUNCTION_TYPE, or pointer to member function. */
15984 tree
15985 static_fn_type (tree memfntype)
15987 tree fntype;
15988 tree args;
15990 if (TYPE_PTRMEMFUNC_P (memfntype))
15991 memfntype = TYPE_PTRMEMFUNC_FN_TYPE (memfntype);
15992 if (POINTER_TYPE_P (memfntype)
15993 || TREE_CODE (memfntype) == FUNCTION_DECL)
15994 memfntype = TREE_TYPE (memfntype);
15995 if (TREE_CODE (memfntype) == FUNCTION_TYPE)
15996 return memfntype;
15997 gcc_assert (TREE_CODE (memfntype) == METHOD_TYPE);
15998 args = TYPE_ARG_TYPES (memfntype);
15999 cp_ref_qualifier rqual = type_memfn_rqual (memfntype);
16000 fntype = build_function_type (TREE_TYPE (memfntype), TREE_CHAIN (args));
16001 fntype = apply_memfn_quals (fntype, type_memfn_quals (memfntype), rqual);
16002 fntype = (cp_build_type_attribute_variant
16003 (fntype, TYPE_ATTRIBUTES (memfntype)));
16004 fntype = (build_exception_variant
16005 (fntype, TYPE_RAISES_EXCEPTIONS (memfntype)));
16006 if (TYPE_HAS_LATE_RETURN_TYPE (memfntype))
16007 TYPE_HAS_LATE_RETURN_TYPE (fntype) = 1;
16008 return fntype;
16011 /* DECL was originally constructed as a non-static member function,
16012 but turned out to be static. Update it accordingly. */
16014 void
16015 revert_static_member_fn (tree decl)
16017 tree stype = static_fn_type (decl);
16018 cp_cv_quals quals = type_memfn_quals (stype);
16019 cp_ref_qualifier rqual = type_memfn_rqual (stype);
16021 if (quals != TYPE_UNQUALIFIED || rqual != REF_QUAL_NONE)
16022 stype = apply_memfn_quals (stype, TYPE_UNQUALIFIED, REF_QUAL_NONE);
16024 TREE_TYPE (decl) = stype;
16026 if (DECL_ARGUMENTS (decl))
16027 DECL_ARGUMENTS (decl) = DECL_CHAIN (DECL_ARGUMENTS (decl));
16028 DECL_STATIC_FUNCTION_P (decl) = 1;
16031 /* Return which tree structure is used by T, or TS_CP_GENERIC if T is
16032 one of the language-independent trees. */
16034 enum cp_tree_node_structure_enum
16035 cp_tree_node_structure (union lang_tree_node * t)
16037 switch (TREE_CODE (&t->generic))
16039 case DEFAULT_ARG: return TS_CP_DEFAULT_ARG;
16040 case DEFERRED_NOEXCEPT: return TS_CP_DEFERRED_NOEXCEPT;
16041 case IDENTIFIER_NODE: return TS_CP_IDENTIFIER;
16042 case OVERLOAD: return TS_CP_OVERLOAD;
16043 case TEMPLATE_PARM_INDEX: return TS_CP_TPI;
16044 case PTRMEM_CST: return TS_CP_PTRMEM;
16045 case BASELINK: return TS_CP_BASELINK;
16046 case TEMPLATE_DECL: return TS_CP_TEMPLATE_DECL;
16047 case STATIC_ASSERT: return TS_CP_STATIC_ASSERT;
16048 case ARGUMENT_PACK_SELECT: return TS_CP_ARGUMENT_PACK_SELECT;
16049 case TRAIT_EXPR: return TS_CP_TRAIT_EXPR;
16050 case LAMBDA_EXPR: return TS_CP_LAMBDA_EXPR;
16051 case TEMPLATE_INFO: return TS_CP_TEMPLATE_INFO;
16052 case CONSTRAINT_INFO: return TS_CP_CONSTRAINT_INFO;
16053 case USERDEF_LITERAL: return TS_CP_USERDEF_LITERAL;
16054 default: return TS_CP_GENERIC;
16058 /* Build the void_list_node (void_type_node having been created). */
16059 tree
16060 build_void_list_node (void)
16062 tree t = build_tree_list (NULL_TREE, void_type_node);
16063 return t;
16066 bool
16067 cp_missing_noreturn_ok_p (tree decl)
16069 /* A missing noreturn is ok for the `main' function. */
16070 return DECL_MAIN_P (decl);
16073 /* Return the decl used to identify the COMDAT group into which DECL should
16074 be placed. */
16076 tree
16077 cxx_comdat_group (tree decl)
16079 /* Virtual tables, construction virtual tables, and virtual table
16080 tables all go in a single COMDAT group, named after the primary
16081 virtual table. */
16082 if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl))
16083 decl = CLASSTYPE_VTABLES (DECL_CONTEXT (decl));
16084 /* For all other DECLs, the COMDAT group is the mangled name of the
16085 declaration itself. */
16086 else
16088 while (DECL_THUNK_P (decl))
16090 /* If TARGET_USE_LOCAL_THUNK_ALIAS_P, use_thunk puts the thunk
16091 into the same section as the target function. In that case
16092 we must return target's name. */
16093 tree target = THUNK_TARGET (decl);
16094 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (target)
16095 && DECL_SECTION_NAME (target) != NULL
16096 && DECL_ONE_ONLY (target))
16097 decl = target;
16098 else
16099 break;
16103 return decl;
16106 /* Returns the return type for FN as written by the user, which may include
16107 a placeholder for a deduced return type. */
16109 tree
16110 fndecl_declared_return_type (tree fn)
16112 fn = STRIP_TEMPLATE (fn);
16113 if (FNDECL_USED_AUTO (fn))
16115 struct language_function *f = NULL;
16116 if (DECL_STRUCT_FUNCTION (fn))
16117 f = DECL_STRUCT_FUNCTION (fn)->language;
16118 if (f == NULL)
16119 f = DECL_SAVED_FUNCTION_DATA (fn);
16120 return f->x_auto_return_pattern;
16122 return TREE_TYPE (TREE_TYPE (fn));
16125 /* Returns true iff DECL was declared with an auto return type and it has
16126 not yet been deduced to a real type. */
16128 bool
16129 undeduced_auto_decl (tree decl)
16131 if (cxx_dialect < cxx14)
16132 return false;
16133 return type_uses_auto (TREE_TYPE (decl));
16136 /* Complain if DECL has an undeduced return type. */
16138 void
16139 require_deduced_type (tree decl)
16141 if (undeduced_auto_decl (decl))
16142 error ("use of %qD before deduction of %<auto%>", decl);
16145 #include "gt-cp-decl.h"