2017-03-06 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / gcc / cp / decl.c
blob3e7316f3e0b739df02354e025769ad68eb1da8f6
1 /* Process declarations and variables for C++ compiler.
2 Copyright (C) 1988-2017 Free Software Foundation, Inc.
3 Contributed by Michael Tiemann (tiemann@cygnus.com)
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 /* Process declarations and symbol lookup for C++ front end.
23 Also constructs types; the standard scalar types at initialization,
24 and structure, union, array and enum types when they are declared. */
26 /* ??? not all decl nodes are given the most useful possible
27 line numbers. For example, the CONST_DECLs for enum values. */
29 #include "config.h"
30 #include "system.h"
31 #include "coretypes.h"
32 #include "target.h"
33 #include "c-family/c-target.h"
34 #include "cp-tree.h"
35 #include "timevar.h"
36 #include "stringpool.h"
37 #include "cgraph.h"
38 #include "stor-layout.h"
39 #include "varasm.h"
40 #include "attribs.h"
41 #include "flags.h"
42 #include "tree-iterator.h"
43 #include "decl.h"
44 #include "intl.h"
45 #include "toplev.h"
46 #include "c-family/c-objc.h"
47 #include "c-family/c-pragma.h"
48 #include "c-family/c-ubsan.h"
49 #include "debug.h"
50 #include "plugin.h"
51 #include "cilk.h"
52 #include "builtins.h"
53 #include "gimplify.h"
55 /* Possible cases of bad specifiers type used by bad_specifiers. */
56 enum bad_spec_place {
57 BSP_VAR, /* variable */
58 BSP_PARM, /* parameter */
59 BSP_TYPE, /* type */
60 BSP_FIELD /* field */
63 static const char *redeclaration_error_message (tree, tree);
65 static int decl_jump_unsafe (tree);
66 static void require_complete_types_for_parms (tree);
67 static int ambi_op_p (enum tree_code);
68 static int unary_op_p (enum tree_code);
69 static void push_local_name (tree);
70 static tree grok_reference_init (tree, tree, tree, int);
71 static tree grokvardecl (tree, tree, tree, const cp_decl_specifier_seq *,
72 int, int, int, bool, int, tree);
73 static int check_static_variable_definition (tree, tree);
74 static void record_unknown_type (tree, const char *);
75 static tree builtin_function_1 (tree, tree, bool);
76 static int member_function_or_else (tree, tree, enum overload_flags);
77 static void check_for_uninitialized_const_var (tree);
78 static tree local_variable_p_walkfn (tree *, int *, void *);
79 static const char *tag_name (enum tag_types);
80 static tree lookup_and_check_tag (enum tag_types, tree, tag_scope, bool);
81 static int walk_namespaces_r (tree, walk_namespaces_fn, void *);
82 static void maybe_deduce_size_from_array_init (tree, tree);
83 static void layout_var_decl (tree);
84 static tree check_initializer (tree, tree, int, vec<tree, va_gc> **);
85 static void make_rtl_for_nonlocal_decl (tree, tree, const char *);
86 static void save_function_data (tree);
87 static void copy_type_enum (tree , tree);
88 static void check_function_type (tree, tree);
89 static void finish_constructor_body (void);
90 static void begin_destructor_body (void);
91 static void finish_destructor_body (void);
92 static void record_key_method_defined (tree);
93 static tree create_array_type_for_decl (tree, tree, tree);
94 static tree get_atexit_node (void);
95 static tree get_dso_handle_node (void);
96 static tree start_cleanup_fn (void);
97 static void end_cleanup_fn (void);
98 static tree cp_make_fname_decl (location_t, tree, int);
99 static void initialize_predefined_identifiers (void);
100 static tree check_special_function_return_type
101 (special_function_kind, tree, tree, int, const location_t*);
102 static tree push_cp_library_fn (enum tree_code, tree, int);
103 static tree build_cp_library_fn (tree, enum tree_code, tree, int);
104 static void store_parm_decls (tree);
105 static void initialize_local_var (tree, tree);
106 static void expand_static_init (tree, tree);
108 /* The following symbols are subsumed in the cp_global_trees array, and
109 listed here individually for documentation purposes.
111 C++ extensions
112 tree wchar_decl_node;
114 tree vtable_entry_type;
115 tree delta_type_node;
116 tree __t_desc_type_node;
118 tree class_type_node;
119 tree unknown_type_node;
121 Array type `vtable_entry_type[]'
123 tree vtbl_type_node;
124 tree vtbl_ptr_type_node;
126 Namespaces,
128 tree std_node;
129 tree abi_node;
131 A FUNCTION_DECL which can call `abort'. Not necessarily the
132 one that the user will declare, but sufficient to be called
133 by routines that want to abort the program.
135 tree abort_fndecl;
137 Used by RTTI
138 tree type_info_type_node, tinfo_decl_id, tinfo_decl_type;
139 tree tinfo_var_id; */
141 tree cp_global_trees[CPTI_MAX];
143 /* Indicates that there is a type value in some namespace, although
144 that is not necessarily in scope at the moment. */
146 tree global_type_node;
148 /* The node that holds the "name" of the global scope. */
149 tree global_scope_name;
151 #define local_names cp_function_chain->x_local_names
153 /* A list of objects which have constructors or destructors
154 which reside in the global scope. The decl is stored in
155 the TREE_VALUE slot and the initializer is stored
156 in the TREE_PURPOSE slot. */
157 tree static_aggregates;
159 /* Like static_aggregates, but for thread_local variables. */
160 tree tls_aggregates;
162 /* -- end of C++ */
164 /* A node for the integer constant 2. */
166 tree integer_two_node;
168 /* Used only for jumps to as-yet undefined labels, since jumps to
169 defined labels can have their validity checked immediately. */
171 struct GTY((chain_next ("%h.next"))) named_label_use_entry {
172 struct named_label_use_entry *next;
173 /* The binding level to which this entry is *currently* attached.
174 This is initially the binding level in which the goto appeared,
175 but is modified as scopes are closed. */
176 cp_binding_level *binding_level;
177 /* The head of the names list that was current when the goto appeared,
178 or the inner scope popped. These are the decls that will *not* be
179 skipped when jumping to the label. */
180 tree names_in_scope;
181 /* The location of the goto, for error reporting. */
182 location_t o_goto_locus;
183 /* True if an OpenMP structured block scope has been closed since
184 the goto appeared. This means that the branch from the label will
185 illegally exit an OpenMP scope. */
186 bool in_omp_scope;
189 /* A list of all LABEL_DECLs in the function that have names. Here so
190 we can clear out their names' definitions at the end of the
191 function, and so we can check the validity of jumps to these labels. */
193 struct GTY((for_user)) named_label_entry {
194 /* The decl itself. */
195 tree label_decl;
197 /* The binding level to which the label is *currently* attached.
198 This is initially set to the binding level in which the label
199 is defined, but is modified as scopes are closed. */
200 cp_binding_level *binding_level;
201 /* The head of the names list that was current when the label was
202 defined, or the inner scope popped. These are the decls that will
203 be skipped when jumping to the label. */
204 tree names_in_scope;
205 /* A vector of all decls from all binding levels that would be
206 crossed by a backward branch to the label. */
207 vec<tree, va_gc> *bad_decls;
209 /* A list of uses of the label, before the label is defined. */
210 struct named_label_use_entry *uses;
212 /* The following bits are set after the label is defined, and are
213 updated as scopes are popped. They indicate that a backward jump
214 to the label will illegally enter a scope of the given flavor. */
215 bool in_try_scope;
216 bool in_catch_scope;
217 bool in_omp_scope;
218 bool in_transaction_scope;
219 bool in_constexpr_if;
222 #define named_labels cp_function_chain->x_named_labels
224 /* The number of function bodies which we are currently processing.
225 (Zero if we are at namespace scope, one inside the body of a
226 function, two inside the body of a function in a local class, etc.) */
227 int function_depth;
229 /* Whether the exception-specifier is part of a function type (i.e. C++17). */
230 bool flag_noexcept_type;
232 /* States indicating how grokdeclarator() should handle declspecs marked
233 with __attribute__((deprecated)). An object declared as
234 __attribute__((deprecated)) suppresses warnings of uses of other
235 deprecated items. */
236 enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
239 /* A list of VAR_DECLs whose type was incomplete at the time the
240 variable was declared. */
242 struct GTY(()) incomplete_var {
243 tree decl;
244 tree incomplete_type;
248 static GTY(()) vec<incomplete_var, va_gc> *incomplete_vars;
250 /* Returns the kind of template specialization we are currently
251 processing, given that it's declaration contained N_CLASS_SCOPES
252 explicit scope qualifications. */
254 tmpl_spec_kind
255 current_tmpl_spec_kind (int n_class_scopes)
257 int n_template_parm_scopes = 0;
258 int seen_specialization_p = 0;
259 int innermost_specialization_p = 0;
260 cp_binding_level *b;
262 /* Scan through the template parameter scopes. */
263 for (b = current_binding_level;
264 b->kind == sk_template_parms;
265 b = b->level_chain)
267 /* If we see a specialization scope inside a parameter scope,
268 then something is wrong. That corresponds to a declaration
269 like:
271 template <class T> template <> ...
273 which is always invalid since [temp.expl.spec] forbids the
274 specialization of a class member template if the enclosing
275 class templates are not explicitly specialized as well. */
276 if (b->explicit_spec_p)
278 if (n_template_parm_scopes == 0)
279 innermost_specialization_p = 1;
280 else
281 seen_specialization_p = 1;
283 else if (seen_specialization_p == 1)
284 return tsk_invalid_member_spec;
286 ++n_template_parm_scopes;
289 /* Handle explicit instantiations. */
290 if (processing_explicit_instantiation)
292 if (n_template_parm_scopes != 0)
293 /* We've seen a template parameter list during an explicit
294 instantiation. For example:
296 template <class T> template void f(int);
298 This is erroneous. */
299 return tsk_invalid_expl_inst;
300 else
301 return tsk_expl_inst;
304 if (n_template_parm_scopes < n_class_scopes)
305 /* We've not seen enough template headers to match all the
306 specialized classes present. For example:
308 template <class T> void R<T>::S<T>::f(int);
310 This is invalid; there needs to be one set of template
311 parameters for each class. */
312 return tsk_insufficient_parms;
313 else if (n_template_parm_scopes == n_class_scopes)
314 /* We're processing a non-template declaration (even though it may
315 be a member of a template class.) For example:
317 template <class T> void S<T>::f(int);
319 The `class T' matches the `S<T>', leaving no template headers
320 corresponding to the `f'. */
321 return tsk_none;
322 else if (n_template_parm_scopes > n_class_scopes + 1)
323 /* We've got too many template headers. For example:
325 template <> template <class T> void f (T);
327 There need to be more enclosing classes. */
328 return tsk_excessive_parms;
329 else
330 /* This must be a template. It's of the form:
332 template <class T> template <class U> void S<T>::f(U);
334 This is a specialization if the innermost level was a
335 specialization; otherwise it's just a definition of the
336 template. */
337 return innermost_specialization_p ? tsk_expl_spec : tsk_template;
340 /* Exit the current scope. */
342 void
343 finish_scope (void)
345 poplevel (0, 0, 0);
348 /* When a label goes out of scope, check to see if that label was used
349 in a valid manner, and issue any appropriate warnings or errors. */
351 static void
352 pop_label (tree label, tree old_value)
354 if (!processing_template_decl)
356 if (DECL_INITIAL (label) == NULL_TREE)
358 location_t location;
360 error ("label %q+D used but not defined", label);
361 location = input_location;
362 /* FIXME want (LOCATION_FILE (input_location), (line)0) */
363 /* Avoid crashing later. */
364 define_label (location, DECL_NAME (label));
366 else
367 warn_for_unused_label (label);
370 SET_IDENTIFIER_LABEL_VALUE (DECL_NAME (label), old_value);
373 /* Push all named labels into a vector, so that we can sort it on DECL_UID
374 to avoid code generation differences. */
377 note_label (named_label_entry **slot, vec<named_label_entry **> &labels)
379 labels.quick_push (slot);
380 return 1;
383 /* Helper function to sort named label entries in a vector by DECL_UID. */
385 static int
386 sort_labels (const void *a, const void *b)
388 named_label_entry **slot1 = *(named_label_entry **const *) a;
389 named_label_entry **slot2 = *(named_label_entry **const *) b;
390 if (DECL_UID ((*slot1)->label_decl) < DECL_UID ((*slot2)->label_decl))
391 return -1;
392 if (DECL_UID ((*slot1)->label_decl) > DECL_UID ((*slot2)->label_decl))
393 return 1;
394 return 0;
397 /* At the end of a function, all labels declared within the function
398 go out of scope. BLOCK is the top-level block for the
399 function. */
401 static void
402 pop_labels (tree block)
404 if (named_labels)
406 auto_vec<named_label_entry **, 32> labels;
407 named_label_entry **slot;
408 unsigned int i;
410 /* Push all the labels into a vector and sort them by DECL_UID,
411 so that gaps between DECL_UIDs don't affect code generation. */
412 labels.reserve_exact (named_labels->elements ());
413 named_labels->traverse<vec<named_label_entry **> &, note_label> (labels);
414 labels.qsort (sort_labels);
415 FOR_EACH_VEC_ELT (labels, i, slot)
417 struct named_label_entry *ent = *slot;
419 pop_label (ent->label_decl, NULL_TREE);
421 /* Put the labels into the "variables" of the top-level block,
422 so debugger can see them. */
423 DECL_CHAIN (ent->label_decl) = BLOCK_VARS (block);
424 BLOCK_VARS (block) = ent->label_decl;
426 named_labels->clear_slot (slot);
428 named_labels = NULL;
432 /* At the end of a block with local labels, restore the outer definition. */
434 static void
435 pop_local_label (tree label, tree old_value)
437 struct named_label_entry dummy;
439 pop_label (label, old_value);
441 dummy.label_decl = label;
442 named_label_entry **slot = named_labels->find_slot (&dummy, NO_INSERT);
443 named_labels->clear_slot (slot);
446 /* The following two routines are used to interface to Objective-C++.
447 The binding level is purposely treated as an opaque type. */
449 void *
450 objc_get_current_scope (void)
452 return current_binding_level;
455 /* The following routine is used by the NeXT-style SJLJ exceptions;
456 variables get marked 'volatile' so as to not be clobbered by
457 _setjmp()/_longjmp() calls. All variables in the current scope,
458 as well as parent scopes up to (but not including) ENCLOSING_BLK
459 shall be thusly marked. */
461 void
462 objc_mark_locals_volatile (void *enclosing_blk)
464 cp_binding_level *scope;
466 for (scope = current_binding_level;
467 scope && scope != enclosing_blk;
468 scope = scope->level_chain)
470 tree decl;
472 for (decl = scope->names; decl; decl = TREE_CHAIN (decl))
473 objc_volatilize_decl (decl);
475 /* Do not climb up past the current function. */
476 if (scope->kind == sk_function_parms)
477 break;
481 /* True if B is the level for the condition of a constexpr if. */
483 static bool
484 level_for_constexpr_if (cp_binding_level *b)
486 return (b->kind == sk_cond && b->this_entity
487 && TREE_CODE (b->this_entity) == IF_STMT
488 && IF_STMT_CONSTEXPR_P (b->this_entity));
491 /* Update data for defined and undefined labels when leaving a scope. */
494 poplevel_named_label_1 (named_label_entry **slot, cp_binding_level *bl)
496 named_label_entry *ent = *slot;
497 cp_binding_level *obl = bl->level_chain;
499 if (ent->binding_level == bl)
501 tree decl;
503 /* ENT->NAMES_IN_SCOPE may contain a mixture of DECLs and
504 TREE_LISTs representing OVERLOADs, so be careful. */
505 for (decl = ent->names_in_scope; decl; decl = (DECL_P (decl)
506 ? DECL_CHAIN (decl)
507 : TREE_CHAIN (decl)))
508 if (decl_jump_unsafe (decl))
509 vec_safe_push (ent->bad_decls, decl);
511 ent->binding_level = obl;
512 ent->names_in_scope = obl->names;
513 switch (bl->kind)
515 case sk_try:
516 ent->in_try_scope = true;
517 break;
518 case sk_catch:
519 ent->in_catch_scope = true;
520 break;
521 case sk_omp:
522 ent->in_omp_scope = true;
523 break;
524 case sk_transaction:
525 ent->in_transaction_scope = true;
526 break;
527 case sk_block:
528 if (level_for_constexpr_if (bl->level_chain))
529 ent->in_constexpr_if = true;
530 break;
531 default:
532 break;
535 else if (ent->uses)
537 struct named_label_use_entry *use;
539 for (use = ent->uses; use ; use = use->next)
540 if (use->binding_level == bl)
542 use->binding_level = obl;
543 use->names_in_scope = obl->names;
544 if (bl->kind == sk_omp)
545 use->in_omp_scope = true;
549 return 1;
552 /* Saved errorcount to avoid -Wunused-but-set-{parameter,variable} warnings
553 when errors were reported, except for -Werror-unused-but-set-*. */
554 static int unused_but_set_errorcount;
556 /* Exit a binding level.
557 Pop the level off, and restore the state of the identifier-decl mappings
558 that were in effect when this level was entered.
560 If KEEP == 1, this level had explicit declarations, so
561 and create a "block" (a BLOCK node) for the level
562 to record its declarations and subblocks for symbol table output.
564 If FUNCTIONBODY is nonzero, this level is the body of a function,
565 so create a block as if KEEP were set and also clear out all
566 label names.
568 If REVERSE is nonzero, reverse the order of decls before putting
569 them into the BLOCK. */
571 tree
572 poplevel (int keep, int reverse, int functionbody)
574 tree link;
575 /* The chain of decls was accumulated in reverse order.
576 Put it into forward order, just for cleanliness. */
577 tree decls;
578 tree subblocks;
579 tree block;
580 tree decl;
581 int leaving_for_scope;
582 scope_kind kind;
583 unsigned ix;
584 cp_label_binding *label_bind;
586 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
587 restart:
589 block = NULL_TREE;
591 gcc_assert (current_binding_level->kind != sk_class);
593 if (current_binding_level->kind == sk_cleanup)
594 functionbody = 0;
595 subblocks = functionbody >= 0 ? current_binding_level->blocks : 0;
597 gcc_assert (!vec_safe_length (current_binding_level->class_shadowed));
599 /* We used to use KEEP == 2 to indicate that the new block should go
600 at the beginning of the list of blocks at this binding level,
601 rather than the end. This hack is no longer used. */
602 gcc_assert (keep == 0 || keep == 1);
604 if (current_binding_level->keep)
605 keep = 1;
607 /* Any uses of undefined labels, and any defined labels, now operate
608 under constraints of next binding contour. */
609 if (cfun && !functionbody && named_labels)
610 named_labels->traverse<cp_binding_level *, poplevel_named_label_1>
611 (current_binding_level);
613 /* Get the decls in the order they were written.
614 Usually current_binding_level->names is in reverse order.
615 But parameter decls were previously put in forward order. */
617 if (reverse)
618 current_binding_level->names
619 = decls = nreverse (current_binding_level->names);
620 else
621 decls = current_binding_level->names;
623 /* If there were any declarations or structure tags in that level,
624 or if this level is a function body,
625 create a BLOCK to record them for the life of this function. */
626 block = NULL_TREE;
627 /* Avoid function body block if possible. */
628 if (functionbody && subblocks && BLOCK_CHAIN (subblocks) == NULL_TREE)
629 keep = 0;
630 else if (keep == 1 || functionbody)
631 block = make_node (BLOCK);
632 if (block != NULL_TREE)
634 BLOCK_VARS (block) = decls;
635 BLOCK_SUBBLOCKS (block) = subblocks;
638 /* In each subblock, record that this is its superior. */
639 if (keep >= 0)
640 for (link = subblocks; link; link = BLOCK_CHAIN (link))
641 BLOCK_SUPERCONTEXT (link) = block;
643 /* We still support the old for-scope rules, whereby the variables
644 in a init statement were in scope after the for-statement ended.
645 We only use the new rules if flag_new_for_scope is nonzero. */
646 leaving_for_scope
647 = current_binding_level->kind == sk_for && flag_new_for_scope == 1;
649 /* Before we remove the declarations first check for unused variables. */
650 if ((warn_unused_variable || warn_unused_but_set_variable)
651 && current_binding_level->kind != sk_template_parms
652 && !processing_template_decl)
653 for (tree d = getdecls (); d; d = TREE_CHAIN (d))
655 /* There are cases where D itself is a TREE_LIST. See in
656 push_local_binding where the list of decls returned by
657 getdecls is built. */
658 decl = TREE_CODE (d) == TREE_LIST ? TREE_VALUE (d) : d;
659 tree type = TREE_TYPE (decl);
660 if (VAR_P (decl)
661 && (! TREE_USED (decl) || !DECL_READ_P (decl))
662 && ! DECL_IN_SYSTEM_HEADER (decl)
663 && DECL_NAME (decl) && ! DECL_ARTIFICIAL (decl)
664 && type != error_mark_node
665 && (!CLASS_TYPE_P (type)
666 || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
667 || lookup_attribute ("warn_unused",
668 TYPE_ATTRIBUTES (TREE_TYPE (decl)))))
670 if (! TREE_USED (decl))
671 warning_at (DECL_SOURCE_LOCATION (decl),
672 OPT_Wunused_variable, "unused variable %qD", decl);
673 else if (DECL_CONTEXT (decl) == current_function_decl
674 // For -Wunused-but-set-variable leave references alone.
675 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
676 && errorcount == unused_but_set_errorcount)
678 warning_at (DECL_SOURCE_LOCATION (decl),
679 OPT_Wunused_but_set_variable,
680 "variable %qD set but not used", decl);
681 unused_but_set_errorcount = errorcount;
686 /* Remove declarations for all the DECLs in this level. */
687 for (link = decls; link; link = TREE_CHAIN (link))
689 if (leaving_for_scope && VAR_P (link)
690 /* It's hard to make this ARM compatibility hack play nicely with
691 lambdas, and it really isn't necessary in C++11 mode. */
692 && cxx_dialect < cxx11
693 && DECL_NAME (link))
695 tree name = DECL_NAME (link);
696 cxx_binding *ob;
697 tree ns_binding;
699 ob = outer_binding (name,
700 IDENTIFIER_BINDING (name),
701 /*class_p=*/true);
702 if (!ob)
703 ns_binding = IDENTIFIER_NAMESPACE_VALUE (name);
704 else
705 ns_binding = NULL_TREE;
707 if (ob && ob->scope == current_binding_level->level_chain)
708 /* We have something like:
710 int i;
711 for (int i; ;);
713 and we are leaving the `for' scope. There's no reason to
714 keep the binding of the inner `i' in this case. */
715 pop_binding (name, link);
716 else if ((ob && (TREE_CODE (ob->value) == TYPE_DECL))
717 || (ns_binding && TREE_CODE (ns_binding) == TYPE_DECL))
718 /* Here, we have something like:
720 typedef int I;
722 void f () {
723 for (int I; ;);
726 We must pop the for-scope binding so we know what's a
727 type and what isn't. */
728 pop_binding (name, link);
729 else
731 /* Mark this VAR_DECL as dead so that we can tell we left it
732 there only for backward compatibility. */
733 DECL_DEAD_FOR_LOCAL (link) = 1;
735 /* Keep track of what should have happened when we
736 popped the binding. */
737 if (ob && ob->value)
739 SET_DECL_SHADOWED_FOR_VAR (link, ob->value);
740 DECL_HAS_SHADOWED_FOR_VAR_P (link) = 1;
743 /* Add it to the list of dead variables in the next
744 outermost binding to that we can remove these when we
745 leave that binding. */
746 vec_safe_push (
747 current_binding_level->level_chain->dead_vars_from_for,
748 link);
750 /* Although we don't pop the cxx_binding, we do clear
751 its SCOPE since the scope is going away now. */
752 IDENTIFIER_BINDING (name)->scope
753 = current_binding_level->level_chain;
756 else
758 tree name;
760 /* Remove the binding. */
761 decl = link;
763 if (TREE_CODE (decl) == TREE_LIST)
764 decl = TREE_VALUE (decl);
765 name = decl;
767 if (TREE_CODE (name) == OVERLOAD)
768 name = OVL_FUNCTION (name);
770 gcc_assert (DECL_P (name));
771 pop_binding (DECL_NAME (name), decl);
775 /* Remove declarations for any `for' variables from inner scopes
776 that we kept around. */
777 FOR_EACH_VEC_SAFE_ELT_REVERSE (current_binding_level->dead_vars_from_for,
778 ix, decl)
779 pop_binding (DECL_NAME (decl), decl);
781 /* Restore the IDENTIFIER_TYPE_VALUEs. */
782 for (link = current_binding_level->type_shadowed;
783 link; link = TREE_CHAIN (link))
784 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link), TREE_VALUE (link));
786 /* Restore the IDENTIFIER_LABEL_VALUEs for local labels. */
787 FOR_EACH_VEC_SAFE_ELT_REVERSE (current_binding_level->shadowed_labels,
788 ix, label_bind)
789 pop_local_label (label_bind->label, label_bind->prev_value);
791 /* There may be OVERLOADs (wrapped in TREE_LISTs) on the BLOCK_VARs
792 list if a `using' declaration put them there. The debugging
793 back ends won't understand OVERLOAD, so we remove them here.
794 Because the BLOCK_VARS are (temporarily) shared with
795 CURRENT_BINDING_LEVEL->NAMES we must do this fixup after we have
796 popped all the bindings. Also remove undeduced 'auto' decls,
797 which LTO doesn't understand, and can't have been used by anything. */
798 if (block)
800 tree* d;
802 for (d = &BLOCK_VARS (block); *d; )
804 if (TREE_CODE (*d) == TREE_LIST
805 || (!processing_template_decl
806 && undeduced_auto_decl (*d)))
807 *d = TREE_CHAIN (*d);
808 else
809 d = &DECL_CHAIN (*d);
813 /* If the level being exited is the top level of a function,
814 check over all the labels. */
815 if (functionbody)
817 if (block)
819 /* Since this is the top level block of a function, the vars are
820 the function's parameters. Don't leave them in the BLOCK
821 because they are found in the FUNCTION_DECL instead. */
822 BLOCK_VARS (block) = 0;
823 pop_labels (block);
825 else
826 pop_labels (subblocks);
829 kind = current_binding_level->kind;
830 if (kind == sk_cleanup)
832 tree stmt;
834 /* If this is a temporary binding created for a cleanup, then we'll
835 have pushed a statement list level. Pop that, create a new
836 BIND_EXPR for the block, and insert it into the stream. */
837 stmt = pop_stmt_list (current_binding_level->statement_list);
838 stmt = c_build_bind_expr (input_location, block, stmt);
839 add_stmt (stmt);
842 leave_scope ();
843 if (functionbody)
845 /* The current function is being defined, so its DECL_INITIAL
846 should be error_mark_node. */
847 gcc_assert (DECL_INITIAL (current_function_decl) == error_mark_node);
848 DECL_INITIAL (current_function_decl) = block ? block : subblocks;
849 if (subblocks)
851 if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
853 if (BLOCK_SUBBLOCKS (subblocks))
854 BLOCK_OUTER_CURLY_BRACE_P (BLOCK_SUBBLOCKS (subblocks)) = 1;
856 else
857 BLOCK_OUTER_CURLY_BRACE_P (subblocks) = 1;
860 else if (block)
861 current_binding_level->blocks
862 = block_chainon (current_binding_level->blocks, block);
864 /* If we did not make a block for the level just exited,
865 any blocks made for inner levels
866 (since they cannot be recorded as subblocks in that level)
867 must be carried forward so they will later become subblocks
868 of something else. */
869 else if (subblocks)
870 current_binding_level->blocks
871 = block_chainon (current_binding_level->blocks, subblocks);
873 /* Each and every BLOCK node created here in `poplevel' is important
874 (e.g. for proper debugging information) so if we created one
875 earlier, mark it as "used". */
876 if (block)
877 TREE_USED (block) = 1;
879 /* All temporary bindings created for cleanups are popped silently. */
880 if (kind == sk_cleanup)
881 goto restart;
883 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
884 return block;
887 /* Walk all the namespaces contained NAMESPACE, including NAMESPACE
888 itself, calling F for each. The DATA is passed to F as well. */
890 static int
891 walk_namespaces_r (tree name_space, walk_namespaces_fn f, void* data)
893 int result = 0;
894 tree current = NAMESPACE_LEVEL (name_space)->namespaces;
896 result |= (*f) (name_space, data);
898 for (; current; current = DECL_CHAIN (current))
899 result |= walk_namespaces_r (current, f, data);
901 return result;
904 /* Walk all the namespaces, calling F for each. The DATA is passed to
905 F as well. */
908 walk_namespaces (walk_namespaces_fn f, void* data)
910 return walk_namespaces_r (global_namespace, f, data);
913 /* Call wrapup_globals_declarations for the globals in NAMESPACE. */
916 wrapup_globals_for_namespace (tree name_space, void* data ATTRIBUTE_UNUSED)
918 cp_binding_level *level = NAMESPACE_LEVEL (name_space);
919 vec<tree, va_gc> *statics = level->static_decls;
920 tree *vec = statics->address ();
921 int len = statics->length ();
923 if (warn_unused_function)
925 tree decl;
926 unsigned int i;
927 FOR_EACH_VEC_SAFE_ELT (statics, i, decl)
928 if (TREE_CODE (decl) == FUNCTION_DECL
929 && DECL_INITIAL (decl) == 0
930 && DECL_EXTERNAL (decl)
931 && !TREE_PUBLIC (decl)
932 && !DECL_ARTIFICIAL (decl)
933 && !DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
934 && !TREE_NO_WARNING (decl))
936 warning_at (DECL_SOURCE_LOCATION (decl),
937 OPT_Wunused_function,
938 "%qF declared %<static%> but never defined", decl);
939 TREE_NO_WARNING (decl) = 1;
943 /* Write out any globals that need to be output. */
944 return wrapup_global_declarations (vec, len);
947 /* Diagnose odr-used extern inline variables without definitions
948 in the current TU. */
950 diagnose_inline_vars_for_namespace (tree name_space, void *)
952 cp_binding_level *level = NAMESPACE_LEVEL (name_space);
953 vec<tree, va_gc> *statics = level->static_decls;
954 tree decl;
955 unsigned int i;
957 FOR_EACH_VEC_SAFE_ELT (statics, i, decl)
958 if (VAR_P (decl)
959 && DECL_EXTERNAL (decl)
960 && DECL_INLINE_VAR_P (decl)
961 && DECL_ODR_USED (decl))
962 error_at (DECL_SOURCE_LOCATION (decl),
963 "odr-used inline variable %qD is not defined", decl);
965 return 0;
968 /* In C++, you don't have to write `struct S' to refer to `S'; you
969 can just use `S'. We accomplish this by creating a TYPE_DECL as
970 if the user had written `typedef struct S S'. Create and return
971 the TYPE_DECL for TYPE. */
973 tree
974 create_implicit_typedef (tree name, tree type)
976 tree decl;
978 decl = build_decl (input_location, TYPE_DECL, name, type);
979 DECL_ARTIFICIAL (decl) = 1;
980 /* There are other implicit type declarations, like the one *within*
981 a class that allows you to write `S::S'. We must distinguish
982 amongst these. */
983 SET_DECL_IMPLICIT_TYPEDEF_P (decl);
984 TYPE_NAME (type) = decl;
985 TYPE_STUB_DECL (type) = decl;
987 return decl;
990 /* Remember a local name for name-mangling purposes. */
992 static void
993 push_local_name (tree decl)
995 size_t i, nelts;
996 tree t, name;
998 timevar_start (TV_NAME_LOOKUP);
1000 name = DECL_NAME (decl);
1002 nelts = vec_safe_length (local_names);
1003 for (i = 0; i < nelts; i++)
1005 t = (*local_names)[i];
1006 if (DECL_NAME (t) == name)
1008 if (!DECL_LANG_SPECIFIC (decl))
1009 retrofit_lang_decl (decl);
1010 DECL_LANG_SPECIFIC (decl)->u.base.u2sel = 1;
1011 if (DECL_DISCRIMINATOR_SET_P (t))
1012 DECL_DISCRIMINATOR (decl) = DECL_DISCRIMINATOR (t) + 1;
1013 else
1014 DECL_DISCRIMINATOR (decl) = 1;
1016 (*local_names)[i] = decl;
1017 timevar_stop (TV_NAME_LOOKUP);
1018 return;
1022 vec_safe_push (local_names, decl);
1023 timevar_stop (TV_NAME_LOOKUP);
1026 /* Subroutine of duplicate_decls: return truthvalue of whether
1027 or not types of these decls match.
1029 For C++, we must compare the parameter list so that `int' can match
1030 `int&' in a parameter position, but `int&' is not confused with
1031 `const int&'. */
1034 decls_match (tree newdecl, tree olddecl)
1036 int types_match;
1038 if (newdecl == olddecl)
1039 return 1;
1041 if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
1042 /* If the two DECLs are not even the same kind of thing, we're not
1043 interested in their types. */
1044 return 0;
1046 gcc_assert (DECL_P (newdecl));
1048 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1050 tree f1 = TREE_TYPE (newdecl);
1051 tree f2 = TREE_TYPE (olddecl);
1052 tree p1 = TYPE_ARG_TYPES (f1);
1053 tree p2 = TYPE_ARG_TYPES (f2);
1054 tree r2;
1056 /* Specializations of different templates are different functions
1057 even if they have the same type. */
1058 tree t1 = (DECL_USE_TEMPLATE (newdecl)
1059 ? DECL_TI_TEMPLATE (newdecl)
1060 : NULL_TREE);
1061 tree t2 = (DECL_USE_TEMPLATE (olddecl)
1062 ? DECL_TI_TEMPLATE (olddecl)
1063 : NULL_TREE);
1064 if (t1 != t2)
1065 return 0;
1067 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
1068 && ! (DECL_EXTERN_C_P (newdecl)
1069 && DECL_EXTERN_C_P (olddecl)))
1070 return 0;
1072 /* A new declaration doesn't match a built-in one unless it
1073 is also extern "C". */
1074 if (DECL_IS_BUILTIN (olddecl)
1075 && DECL_EXTERN_C_P (olddecl) && !DECL_EXTERN_C_P (newdecl))
1076 return 0;
1078 if (TREE_CODE (f1) != TREE_CODE (f2))
1079 return 0;
1081 /* A declaration with deduced return type should use its pre-deduction
1082 type for declaration matching. */
1083 r2 = fndecl_declared_return_type (olddecl);
1085 if (same_type_p (TREE_TYPE (f1), r2))
1087 if (!prototype_p (f2) && DECL_EXTERN_C_P (olddecl)
1088 && (DECL_BUILT_IN (olddecl)
1089 #ifndef NO_IMPLICIT_EXTERN_C
1090 || (DECL_IN_SYSTEM_HEADER (newdecl) && !DECL_CLASS_SCOPE_P (newdecl))
1091 || (DECL_IN_SYSTEM_HEADER (olddecl) && !DECL_CLASS_SCOPE_P (olddecl))
1092 #endif
1095 types_match = self_promoting_args_p (p1);
1096 if (p1 == void_list_node)
1097 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
1099 #ifndef NO_IMPLICIT_EXTERN_C
1100 else if (!prototype_p (f1)
1101 && (DECL_EXTERN_C_P (olddecl)
1102 && DECL_IN_SYSTEM_HEADER (olddecl)
1103 && !DECL_CLASS_SCOPE_P (olddecl))
1104 && (DECL_EXTERN_C_P (newdecl)
1105 && DECL_IN_SYSTEM_HEADER (newdecl)
1106 && !DECL_CLASS_SCOPE_P (newdecl)))
1108 types_match = self_promoting_args_p (p2);
1109 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
1111 #endif
1112 else
1113 types_match =
1114 compparms (p1, p2)
1115 && type_memfn_rqual (f1) == type_memfn_rqual (f2)
1116 && (TYPE_ATTRIBUTES (TREE_TYPE (newdecl)) == NULL_TREE
1117 || comp_type_attributes (TREE_TYPE (newdecl),
1118 TREE_TYPE (olddecl)) != 0);
1120 else
1121 types_match = 0;
1123 /* The decls dont match if they correspond to two different versions
1124 of the same function. Disallow extern "C" functions to be
1125 versions for now. */
1126 if (types_match
1127 && !DECL_EXTERN_C_P (newdecl)
1128 && !DECL_EXTERN_C_P (olddecl)
1129 && targetm.target_option.function_versions (newdecl, olddecl))
1131 /* Mark functions as versions if necessary. Modify the mangled decl
1132 name if necessary. */
1133 if (DECL_FUNCTION_VERSIONED (newdecl)
1134 && DECL_FUNCTION_VERSIONED (olddecl))
1135 return 0;
1136 if (!DECL_FUNCTION_VERSIONED (newdecl))
1138 DECL_FUNCTION_VERSIONED (newdecl) = 1;
1139 if (DECL_ASSEMBLER_NAME_SET_P (newdecl))
1140 mangle_decl (newdecl);
1142 if (!DECL_FUNCTION_VERSIONED (olddecl))
1144 DECL_FUNCTION_VERSIONED (olddecl) = 1;
1145 if (DECL_ASSEMBLER_NAME_SET_P (olddecl))
1146 mangle_decl (olddecl);
1148 cgraph_node::record_function_versions (olddecl, newdecl);
1149 return 0;
1152 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1154 tree oldres = DECL_TEMPLATE_RESULT (olddecl);
1155 tree newres = DECL_TEMPLATE_RESULT (newdecl);
1157 if (TREE_CODE (newres) != TREE_CODE (oldres))
1158 return 0;
1160 if (!comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
1161 DECL_TEMPLATE_PARMS (olddecl)))
1162 return 0;
1164 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
1165 types_match = (same_type_p (TREE_TYPE (oldres), TREE_TYPE (newres))
1166 && equivalently_constrained (olddecl, newdecl));
1167 else
1168 // We don't need to check equivalently_constrained for variable and
1169 // function templates because we check it on the results.
1170 types_match = decls_match (oldres, newres);
1172 else
1174 /* Need to check scope for variable declaration (VAR_DECL).
1175 For typedef (TYPE_DECL), scope is ignored. */
1176 if (VAR_P (newdecl)
1177 && CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
1178 /* [dcl.link]
1179 Two declarations for an object with C language linkage
1180 with the same name (ignoring the namespace that qualify
1181 it) that appear in different namespace scopes refer to
1182 the same object. */
1183 && !(DECL_EXTERN_C_P (olddecl) && DECL_EXTERN_C_P (newdecl)))
1184 return 0;
1186 if (TREE_TYPE (newdecl) == error_mark_node)
1187 types_match = TREE_TYPE (olddecl) == error_mark_node;
1188 else if (TREE_TYPE (olddecl) == NULL_TREE)
1189 types_match = TREE_TYPE (newdecl) == NULL_TREE;
1190 else if (TREE_TYPE (newdecl) == NULL_TREE)
1191 types_match = 0;
1192 else
1193 types_match = comptypes (TREE_TYPE (newdecl),
1194 TREE_TYPE (olddecl),
1195 COMPARE_REDECLARATION);
1198 // Normal functions can be constrained, as can variable partial
1199 // specializations.
1200 if (types_match && VAR_OR_FUNCTION_DECL_P (newdecl))
1201 types_match = equivalently_constrained (newdecl, olddecl);
1203 return types_match;
1206 /* If NEWDECL is `static' and an `extern' was seen previously,
1207 warn about it. OLDDECL is the previous declaration.
1209 Note that this does not apply to the C++ case of declaring
1210 a variable `extern const' and then later `const'.
1212 Don't complain about built-in functions, since they are beyond
1213 the user's control. */
1215 void
1216 warn_extern_redeclared_static (tree newdecl, tree olddecl)
1218 if (TREE_CODE (newdecl) == TYPE_DECL
1219 || TREE_CODE (newdecl) == TEMPLATE_DECL
1220 || TREE_CODE (newdecl) == CONST_DECL
1221 || TREE_CODE (newdecl) == NAMESPACE_DECL)
1222 return;
1224 /* Don't get confused by static member functions; that's a different
1225 use of `static'. */
1226 if (TREE_CODE (newdecl) == FUNCTION_DECL
1227 && DECL_STATIC_FUNCTION_P (newdecl))
1228 return;
1230 /* If the old declaration was `static', or the new one isn't, then
1231 everything is OK. */
1232 if (DECL_THIS_STATIC (olddecl) || !DECL_THIS_STATIC (newdecl))
1233 return;
1235 /* It's OK to declare a builtin function as `static'. */
1236 if (TREE_CODE (olddecl) == FUNCTION_DECL
1237 && DECL_ARTIFICIAL (olddecl))
1238 return;
1240 if (permerror (DECL_SOURCE_LOCATION (newdecl),
1241 "%qD was declared %<extern%> and later %<static%>", newdecl))
1242 inform (DECL_SOURCE_LOCATION (olddecl),
1243 "previous declaration of %qD", olddecl);
1246 /* NEW_DECL is a redeclaration of OLD_DECL; both are functions or
1247 function templates. If their exception specifications do not
1248 match, issue a diagnostic. */
1250 static void
1251 check_redeclaration_exception_specification (tree new_decl,
1252 tree old_decl)
1254 tree new_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (new_decl));
1255 tree old_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old_decl));
1257 /* Two default specs are equivalent, don't force evaluation. */
1258 if (UNEVALUATED_NOEXCEPT_SPEC_P (new_exceptions)
1259 && UNEVALUATED_NOEXCEPT_SPEC_P (old_exceptions))
1260 return;
1262 maybe_instantiate_noexcept (new_decl);
1263 maybe_instantiate_noexcept (old_decl);
1264 new_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (new_decl));
1265 old_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old_decl));
1267 /* [except.spec]
1269 If any declaration of a function has an exception-specification,
1270 all declarations, including the definition and an explicit
1271 specialization, of that function shall have an
1272 exception-specification with the same set of type-ids. */
1273 if (! DECL_IS_BUILTIN (old_decl)
1274 && !comp_except_specs (new_exceptions, old_exceptions, ce_normal))
1276 const char *const msg
1277 = G_("declaration of %qF has a different exception specifier");
1278 bool complained = true;
1279 location_t new_loc = DECL_SOURCE_LOCATION (new_decl);
1280 if (DECL_IN_SYSTEM_HEADER (old_decl))
1281 complained = pedwarn (new_loc, OPT_Wsystem_headers, msg, new_decl);
1282 else if (!flag_exceptions)
1283 /* We used to silently permit mismatched eh specs with
1284 -fno-exceptions, so make them a pedwarn now. */
1285 complained = pedwarn (new_loc, OPT_Wpedantic, msg, new_decl);
1286 else
1287 error_at (new_loc, msg, new_decl);
1288 if (complained)
1289 inform (DECL_SOURCE_LOCATION (old_decl),
1290 "from previous declaration %qF", old_decl);
1294 /* Return true if OLD_DECL and NEW_DECL agree on constexprness.
1295 Otherwise issue diagnostics. */
1297 static bool
1298 validate_constexpr_redeclaration (tree old_decl, tree new_decl)
1300 old_decl = STRIP_TEMPLATE (old_decl);
1301 new_decl = STRIP_TEMPLATE (new_decl);
1302 if (!VAR_OR_FUNCTION_DECL_P (old_decl)
1303 || !VAR_OR_FUNCTION_DECL_P (new_decl))
1304 return true;
1305 if (DECL_DECLARED_CONSTEXPR_P (old_decl)
1306 == DECL_DECLARED_CONSTEXPR_P (new_decl))
1307 return true;
1308 if (TREE_CODE (old_decl) == FUNCTION_DECL)
1310 if (DECL_BUILT_IN (old_decl))
1312 /* Hide a built-in declaration. */
1313 DECL_DECLARED_CONSTEXPR_P (old_decl)
1314 = DECL_DECLARED_CONSTEXPR_P (new_decl);
1315 return true;
1317 /* 7.1.5 [dcl.constexpr]
1318 Note: An explicit specialization can differ from the template
1319 declaration with respect to the constexpr specifier. */
1320 if (! DECL_TEMPLATE_SPECIALIZATION (old_decl)
1321 && DECL_TEMPLATE_SPECIALIZATION (new_decl))
1322 return true;
1324 error_at (DECL_SOURCE_LOCATION (new_decl),
1325 "redeclaration %qD differs in %<constexpr%> "
1326 "from previous declaration", new_decl);
1327 inform (DECL_SOURCE_LOCATION (old_decl),
1328 "previous declaration %qD", old_decl);
1329 return false;
1331 return true;
1334 // If OLDDECL and NEWDECL are concept declarations with the same type
1335 // (i.e., and template parameters), but different requirements,
1336 // emit diagnostics and return true. Otherwise, return false.
1337 static inline bool
1338 check_concept_refinement (tree olddecl, tree newdecl)
1340 if (!DECL_DECLARED_CONCEPT_P (olddecl) || !DECL_DECLARED_CONCEPT_P (newdecl))
1341 return false;
1343 tree d1 = DECL_TEMPLATE_RESULT (olddecl);
1344 tree d2 = DECL_TEMPLATE_RESULT (newdecl);
1345 if (TREE_CODE (d1) != TREE_CODE (d2))
1346 return false;
1348 tree t1 = TREE_TYPE (d1);
1349 tree t2 = TREE_TYPE (d2);
1350 if (TREE_CODE (d1) == FUNCTION_DECL)
1352 if (compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2))
1353 && comp_template_parms (DECL_TEMPLATE_PARMS (olddecl),
1354 DECL_TEMPLATE_PARMS (newdecl))
1355 && !equivalently_constrained (olddecl, newdecl))
1357 error ("cannot specialize concept %q#D", olddecl);
1358 return true;
1361 return false;
1364 /* DECL is a redeclaration of a function or function template. If
1365 it does have default arguments issue a diagnostic. Note: this
1366 function is used to enforce the requirements in C++11 8.3.6 about
1367 no default arguments in redeclarations. */
1369 static void
1370 check_redeclaration_no_default_args (tree decl)
1372 gcc_assert (DECL_DECLARES_FUNCTION_P (decl));
1374 for (tree t = FUNCTION_FIRST_USER_PARMTYPE (decl);
1375 t && t != void_list_node; t = TREE_CHAIN (t))
1376 if (TREE_PURPOSE (t))
1378 permerror (DECL_SOURCE_LOCATION (decl),
1379 "redeclaration of %q#D may not have default "
1380 "arguments", decl);
1381 return;
1385 #define GNU_INLINE_P(fn) (DECL_DECLARED_INLINE_P (fn) \
1386 && lookup_attribute ("gnu_inline", \
1387 DECL_ATTRIBUTES (fn)))
1389 /* If NEWDECL is a redeclaration of OLDDECL, merge the declarations.
1390 If the redeclaration is invalid, a diagnostic is issued, and the
1391 error_mark_node is returned. Otherwise, OLDDECL is returned.
1393 If NEWDECL is not a redeclaration of OLDDECL, NULL_TREE is
1394 returned.
1396 NEWDECL_IS_FRIEND is true if NEWDECL was declared as a friend. */
1398 tree
1399 duplicate_decls (tree newdecl, tree olddecl, bool newdecl_is_friend)
1401 unsigned olddecl_uid = DECL_UID (olddecl);
1402 int olddecl_friend = 0, types_match = 0, hidden_friend = 0;
1403 int new_defines_function = 0;
1404 tree new_template_info;
1406 if (newdecl == olddecl)
1407 return olddecl;
1409 types_match = decls_match (newdecl, olddecl);
1411 /* If either the type of the new decl or the type of the old decl is an
1412 error_mark_node, then that implies that we have already issued an
1413 error (earlier) for some bogus type specification, and in that case,
1414 it is rather pointless to harass the user with yet more error message
1415 about the same declaration, so just pretend the types match here. */
1416 if (TREE_TYPE (newdecl) == error_mark_node
1417 || TREE_TYPE (olddecl) == error_mark_node)
1418 return error_mark_node;
1420 if (UDLIT_OPER_P (DECL_NAME (newdecl))
1421 && UDLIT_OPER_P (DECL_NAME (olddecl)))
1423 if (TREE_CODE (newdecl) == TEMPLATE_DECL
1424 && TREE_CODE (olddecl) != TEMPLATE_DECL
1425 && check_raw_literal_operator (olddecl))
1426 error ("literal operator template %q+D conflicts with"
1427 " raw literal operator %qD", newdecl, olddecl);
1428 else if (TREE_CODE (newdecl) != TEMPLATE_DECL
1429 && TREE_CODE (olddecl) == TEMPLATE_DECL
1430 && check_raw_literal_operator (newdecl))
1431 error ("raw literal operator %q+D conflicts with"
1432 " literal operator template %qD", newdecl, olddecl);
1435 if (DECL_P (olddecl)
1436 && TREE_CODE (newdecl) == FUNCTION_DECL
1437 && TREE_CODE (olddecl) == FUNCTION_DECL
1438 && diagnose_mismatched_attributes (olddecl, newdecl))
1440 if (DECL_INITIAL (olddecl))
1441 inform (DECL_SOURCE_LOCATION (olddecl),
1442 "previous definition of %qD was here", olddecl);
1443 else
1444 inform (DECL_SOURCE_LOCATION (olddecl),
1445 "previous declaration of %qD was here", olddecl);
1448 /* Check for redeclaration and other discrepancies. */
1449 if (TREE_CODE (olddecl) == FUNCTION_DECL
1450 && DECL_ARTIFICIAL (olddecl))
1452 gcc_assert (!DECL_HIDDEN_FRIEND_P (olddecl));
1453 if (TREE_CODE (newdecl) != FUNCTION_DECL)
1455 /* Avoid warnings redeclaring built-ins which have not been
1456 explicitly declared. */
1457 if (DECL_ANTICIPATED (olddecl))
1458 return NULL_TREE;
1460 /* If you declare a built-in or predefined function name as static,
1461 the old definition is overridden, but optionally warn this was a
1462 bad choice of name. */
1463 if (! TREE_PUBLIC (newdecl))
1465 warning (OPT_Wshadow,
1466 DECL_BUILT_IN (olddecl)
1467 ? G_("shadowing built-in function %q#D")
1468 : G_("shadowing library function %q#D"), olddecl);
1469 /* Discard the old built-in function. */
1470 return NULL_TREE;
1472 /* If the built-in is not ansi, then programs can override
1473 it even globally without an error. */
1474 else if (! DECL_BUILT_IN (olddecl))
1475 warning_at (DECL_SOURCE_LOCATION (newdecl), 0,
1476 "library function %q#D redeclared as non-function %q#D",
1477 olddecl, newdecl);
1478 else
1479 error ("declaration of %q+#D conflicts with built-in "
1480 "declaration %q#D", newdecl, olddecl);
1481 return NULL_TREE;
1483 else if (DECL_OMP_DECLARE_REDUCTION_P (olddecl))
1485 gcc_assert (DECL_OMP_DECLARE_REDUCTION_P (newdecl));
1486 error_at (DECL_SOURCE_LOCATION (newdecl),
1487 "redeclaration of %<pragma omp declare reduction%>");
1488 inform (DECL_SOURCE_LOCATION (olddecl),
1489 "previous %<pragma omp declare reduction%> declaration");
1490 return error_mark_node;
1492 else if (!types_match)
1494 /* Avoid warnings redeclaring built-ins which have not been
1495 explicitly declared. */
1496 if (DECL_ANTICIPATED (olddecl))
1498 tree t1, t2;
1500 /* A new declaration doesn't match a built-in one unless it
1501 is also extern "C". */
1502 gcc_assert (DECL_IS_BUILTIN (olddecl));
1503 gcc_assert (DECL_EXTERN_C_P (olddecl));
1504 if (!DECL_EXTERN_C_P (newdecl))
1505 return NULL_TREE;
1507 for (t1 = TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1508 t2 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1509 t1 || t2;
1510 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1511 if (!t1 || !t2)
1512 break;
1513 /* Deal with fileptr_type_node. FILE type is not known
1514 at the time we create the builtins. */
1515 else if (TREE_VALUE (t2) == fileptr_type_node)
1517 tree t = TREE_VALUE (t1);
1519 if (TYPE_PTR_P (t)
1520 && TYPE_IDENTIFIER (TREE_TYPE (t))
1521 == get_identifier ("FILE")
1522 && compparms (TREE_CHAIN (t1), TREE_CHAIN (t2)))
1524 tree oldargs = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1526 TYPE_ARG_TYPES (TREE_TYPE (olddecl))
1527 = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
1528 types_match = decls_match (newdecl, olddecl);
1529 if (types_match)
1530 return duplicate_decls (newdecl, olddecl,
1531 newdecl_is_friend);
1532 TYPE_ARG_TYPES (TREE_TYPE (olddecl)) = oldargs;
1535 /* Likewise for const struct tm*. */
1536 else if (TREE_VALUE (t2) == const_tm_ptr_type_node)
1538 tree t = TREE_VALUE (t1);
1540 if (TYPE_PTR_P (t)
1541 && TYPE_IDENTIFIER (TREE_TYPE (t))
1542 == get_identifier ("tm")
1543 && compparms (TREE_CHAIN (t1), TREE_CHAIN (t2)))
1545 tree oldargs = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1547 TYPE_ARG_TYPES (TREE_TYPE (olddecl))
1548 = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
1549 types_match = decls_match (newdecl, olddecl);
1550 if (types_match)
1551 return duplicate_decls (newdecl, olddecl,
1552 newdecl_is_friend);
1553 TYPE_ARG_TYPES (TREE_TYPE (olddecl)) = oldargs;
1556 else if (! same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1557 break;
1559 warning_at (DECL_SOURCE_LOCATION (newdecl),
1560 OPT_Wbuiltin_declaration_mismatch,
1561 "declaration of %q+#D conflicts with built-in "
1562 "declaration %q#D", newdecl, olddecl);
1564 else if ((DECL_EXTERN_C_P (newdecl)
1565 && DECL_EXTERN_C_P (olddecl))
1566 || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1567 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
1569 /* A near match; override the builtin. */
1571 if (TREE_PUBLIC (newdecl))
1572 warning_at (DECL_SOURCE_LOCATION (newdecl),
1573 OPT_Wbuiltin_declaration_mismatch,
1574 "new declaration %q#D ambiguates built-in "
1575 "declaration %q#D", newdecl, olddecl);
1576 else
1577 warning (OPT_Wshadow,
1578 DECL_BUILT_IN (olddecl)
1579 ? G_("shadowing built-in function %q#D")
1580 : G_("shadowing library function %q#D"), olddecl);
1582 else
1583 /* Discard the old built-in function. */
1584 return NULL_TREE;
1586 /* Replace the old RTL to avoid problems with inlining. */
1587 COPY_DECL_RTL (newdecl, olddecl);
1589 /* Even if the types match, prefer the new declarations type for
1590 built-ins which have not been explicitly declared, for
1591 exception lists, etc... */
1592 else if (DECL_IS_BUILTIN (olddecl))
1594 tree type = TREE_TYPE (newdecl);
1595 tree attribs = (*targetm.merge_type_attributes)
1596 (TREE_TYPE (olddecl), type);
1598 type = cp_build_type_attribute_variant (type, attribs);
1599 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = type;
1602 /* If a function is explicitly declared "throw ()", propagate that to
1603 the corresponding builtin. */
1604 if (DECL_BUILT_IN_CLASS (olddecl) == BUILT_IN_NORMAL
1605 && DECL_ANTICIPATED (olddecl)
1606 && TREE_NOTHROW (newdecl)
1607 && !TREE_NOTHROW (olddecl))
1609 enum built_in_function fncode = DECL_FUNCTION_CODE (olddecl);
1610 tree tmpdecl = builtin_decl_explicit (fncode);
1611 if (tmpdecl && tmpdecl != olddecl && types_match)
1612 TREE_NOTHROW (tmpdecl) = 1;
1615 /* Whether or not the builtin can throw exceptions has no
1616 bearing on this declarator. */
1617 TREE_NOTHROW (olddecl) = 0;
1619 if (DECL_THIS_STATIC (newdecl) && !DECL_THIS_STATIC (olddecl))
1621 /* If a builtin function is redeclared as `static', merge
1622 the declarations, but make the original one static. */
1623 DECL_THIS_STATIC (olddecl) = 1;
1624 TREE_PUBLIC (olddecl) = 0;
1626 /* Make the old declaration consistent with the new one so
1627 that all remnants of the builtin-ness of this function
1628 will be banished. */
1629 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
1630 COPY_DECL_RTL (newdecl, olddecl);
1633 else if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1635 /* C++ Standard, 3.3, clause 4:
1636 "[Note: a namespace name or a class template name must be unique
1637 in its declarative region (7.3.2, clause 14). ]" */
1638 if (TREE_CODE (olddecl) != NAMESPACE_DECL
1639 && TREE_CODE (newdecl) != NAMESPACE_DECL
1640 && (TREE_CODE (olddecl) != TEMPLATE_DECL
1641 || TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) != TYPE_DECL)
1642 && (TREE_CODE (newdecl) != TEMPLATE_DECL
1643 || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) != TYPE_DECL))
1645 if ((TREE_CODE (olddecl) == TYPE_DECL && DECL_ARTIFICIAL (olddecl)
1646 && TREE_CODE (newdecl) != TYPE_DECL)
1647 || (TREE_CODE (newdecl) == TYPE_DECL && DECL_ARTIFICIAL (newdecl)
1648 && TREE_CODE (olddecl) != TYPE_DECL))
1650 /* We do nothing special here, because C++ does such nasty
1651 things with TYPE_DECLs. Instead, just let the TYPE_DECL
1652 get shadowed, and know that if we need to find a TYPE_DECL
1653 for a given name, we can look in the IDENTIFIER_TYPE_VALUE
1654 slot of the identifier. */
1655 return NULL_TREE;
1658 if ((TREE_CODE (newdecl) == FUNCTION_DECL
1659 && DECL_FUNCTION_TEMPLATE_P (olddecl))
1660 || (TREE_CODE (olddecl) == FUNCTION_DECL
1661 && DECL_FUNCTION_TEMPLATE_P (newdecl)))
1662 return NULL_TREE;
1665 error ("%q#D redeclared as different kind of symbol", newdecl);
1666 if (TREE_CODE (olddecl) == TREE_LIST)
1667 olddecl = TREE_VALUE (olddecl);
1668 inform (DECL_SOURCE_LOCATION (olddecl),
1669 "previous declaration %q#D", olddecl);
1671 return error_mark_node;
1673 else if (!types_match)
1675 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl))
1676 /* These are certainly not duplicate declarations; they're
1677 from different scopes. */
1678 return NULL_TREE;
1680 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1682 /* The name of a class template may not be declared to refer to
1683 any other template, class, function, object, namespace, value,
1684 or type in the same scope. */
1685 if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == TYPE_DECL
1686 || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
1688 error ("conflicting declaration of template %q+#D", newdecl);
1689 inform (DECL_SOURCE_LOCATION (olddecl),
1690 "previous declaration %q#D", olddecl);
1691 return error_mark_node;
1693 else if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == FUNCTION_DECL
1694 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL
1695 && compparms (TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl))),
1696 TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl))))
1697 && comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
1698 DECL_TEMPLATE_PARMS (olddecl))
1699 /* Template functions can be disambiguated by
1700 return type. */
1701 && same_type_p (TREE_TYPE (TREE_TYPE (newdecl)),
1702 TREE_TYPE (TREE_TYPE (olddecl)))
1703 // Template functions can also be disambiguated by
1704 // constraints.
1705 && equivalently_constrained (olddecl, newdecl))
1707 error ("ambiguating new declaration %q+#D", newdecl);
1708 inform (DECL_SOURCE_LOCATION (olddecl),
1709 "old declaration %q#D", olddecl);
1711 else if (check_concept_refinement (olddecl, newdecl))
1712 return error_mark_node;
1713 return NULL_TREE;
1715 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1717 if (DECL_EXTERN_C_P (newdecl) && DECL_EXTERN_C_P (olddecl))
1719 error ("conflicting declaration of C function %q+#D",
1720 newdecl);
1721 inform (DECL_SOURCE_LOCATION (olddecl),
1722 "previous declaration %q#D", olddecl);
1723 return NULL_TREE;
1725 /* For function versions, params and types match, but they
1726 are not ambiguous. */
1727 else if ((!DECL_FUNCTION_VERSIONED (newdecl)
1728 && !DECL_FUNCTION_VERSIONED (olddecl))
1729 // The functions have the same parameter types.
1730 && compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1731 TYPE_ARG_TYPES (TREE_TYPE (olddecl)))
1732 // And the same constraints.
1733 && equivalently_constrained (newdecl, olddecl))
1735 error ("ambiguating new declaration of %q+#D", newdecl);
1736 inform (DECL_SOURCE_LOCATION (olddecl),
1737 "old declaration %q#D", olddecl);
1738 return error_mark_node;
1740 else
1741 return NULL_TREE;
1743 else
1745 error ("conflicting declaration %q+#D", newdecl);
1746 inform (DECL_SOURCE_LOCATION (olddecl),
1747 "previous declaration as %q#D", olddecl);
1748 return error_mark_node;
1751 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1752 && ((DECL_TEMPLATE_SPECIALIZATION (olddecl)
1753 && (!DECL_TEMPLATE_INFO (newdecl)
1754 || (DECL_TI_TEMPLATE (newdecl)
1755 != DECL_TI_TEMPLATE (olddecl))))
1756 || (DECL_TEMPLATE_SPECIALIZATION (newdecl)
1757 && (!DECL_TEMPLATE_INFO (olddecl)
1758 || (DECL_TI_TEMPLATE (olddecl)
1759 != DECL_TI_TEMPLATE (newdecl))))))
1760 /* It's OK to have a template specialization and a non-template
1761 with the same type, or to have specializations of two
1762 different templates with the same type. Note that if one is a
1763 specialization, and the other is an instantiation of the same
1764 template, that we do not exit at this point. That situation
1765 can occur if we instantiate a template class, and then
1766 specialize one of its methods. This situation is valid, but
1767 the declarations must be merged in the usual way. */
1768 return NULL_TREE;
1769 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1770 && ((DECL_TEMPLATE_INSTANTIATION (olddecl)
1771 && !DECL_USE_TEMPLATE (newdecl))
1772 || (DECL_TEMPLATE_INSTANTIATION (newdecl)
1773 && !DECL_USE_TEMPLATE (olddecl))))
1774 /* One of the declarations is a template instantiation, and the
1775 other is not a template at all. That's OK. */
1776 return NULL_TREE;
1777 else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
1779 /* In [namespace.alias] we have:
1781 In a declarative region, a namespace-alias-definition can be
1782 used to redefine a namespace-alias declared in that declarative
1783 region to refer only to the namespace to which it already
1784 refers.
1786 Therefore, if we encounter a second alias directive for the same
1787 alias, we can just ignore the second directive. */
1788 if (DECL_NAMESPACE_ALIAS (newdecl)
1789 && (DECL_NAMESPACE_ALIAS (newdecl)
1790 == DECL_NAMESPACE_ALIAS (olddecl)))
1791 return olddecl;
1792 /* [namespace.alias]
1794 A namespace-name or namespace-alias shall not be declared as
1795 the name of any other entity in the same declarative region.
1796 A namespace-name defined at global scope shall not be
1797 declared as the name of any other entity in any global scope
1798 of the program. */
1799 error ("conflicting declaration of namespace %q+D", newdecl);
1800 inform (DECL_SOURCE_LOCATION (olddecl),
1801 "previous declaration of namespace %qD here", olddecl);
1802 return error_mark_node;
1804 else
1806 const char *errmsg = redeclaration_error_message (newdecl, olddecl);
1807 if (errmsg)
1809 error_at (DECL_SOURCE_LOCATION (newdecl), errmsg, newdecl);
1810 if (DECL_NAME (olddecl) != NULL_TREE)
1811 inform (DECL_SOURCE_LOCATION (olddecl),
1812 (DECL_INITIAL (olddecl) && namespace_bindings_p ())
1813 ? G_("%q#D previously defined here")
1814 : G_("%q#D previously declared here"), olddecl);
1815 return error_mark_node;
1817 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1818 && DECL_INITIAL (olddecl) != NULL_TREE
1819 && !prototype_p (TREE_TYPE (olddecl))
1820 && prototype_p (TREE_TYPE (newdecl)))
1822 /* Prototype decl follows defn w/o prototype. */
1823 if (warning_at (DECL_SOURCE_LOCATION (newdecl), 0,
1824 "prototype specified for %q#D", newdecl))
1825 inform (DECL_SOURCE_LOCATION (olddecl),
1826 "previous non-prototype definition here");
1828 else if (VAR_OR_FUNCTION_DECL_P (olddecl)
1829 && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl))
1831 /* [dcl.link]
1832 If two declarations of the same function or object
1833 specify different linkage-specifications ..., the program
1834 is ill-formed.... Except for functions with C++ linkage,
1835 a function declaration without a linkage specification
1836 shall not precede the first linkage specification for
1837 that function. A function can be declared without a
1838 linkage specification after an explicit linkage
1839 specification has been seen; the linkage explicitly
1840 specified in the earlier declaration is not affected by
1841 such a function declaration.
1843 DR 563 raises the question why the restrictions on
1844 functions should not also apply to objects. Older
1845 versions of G++ silently ignore the linkage-specification
1846 for this example:
1848 namespace N {
1849 extern int i;
1850 extern "C" int i;
1853 which is clearly wrong. Therefore, we now treat objects
1854 like functions. */
1855 if (current_lang_depth () == 0)
1857 /* There is no explicit linkage-specification, so we use
1858 the linkage from the previous declaration. */
1859 if (!DECL_LANG_SPECIFIC (newdecl))
1860 retrofit_lang_decl (newdecl);
1861 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
1863 else
1865 error ("conflicting declaration of %q+#D with %qL linkage",
1866 newdecl, DECL_LANGUAGE (newdecl));
1867 inform (DECL_SOURCE_LOCATION (olddecl),
1868 "previous declaration with %qL linkage",
1869 DECL_LANGUAGE (olddecl));
1873 if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl))
1875 else if (TREE_CODE (olddecl) == FUNCTION_DECL)
1877 /* Note: free functions, as TEMPLATE_DECLs, are handled below. */
1878 if (DECL_FUNCTION_MEMBER_P (olddecl)
1879 && (/* grokfndecl passes member function templates too
1880 as FUNCTION_DECLs. */
1881 DECL_TEMPLATE_INFO (olddecl)
1882 /* C++11 8.3.6/6.
1883 Default arguments for a member function of a class
1884 template shall be specified on the initial declaration
1885 of the member function within the class template. */
1886 || CLASSTYPE_TEMPLATE_INFO (CP_DECL_CONTEXT (olddecl))))
1887 check_redeclaration_no_default_args (newdecl);
1888 else
1890 tree t1 = FUNCTION_FIRST_USER_PARMTYPE (olddecl);
1891 tree t2 = FUNCTION_FIRST_USER_PARMTYPE (newdecl);
1892 int i = 1;
1894 for (; t1 && t1 != void_list_node;
1895 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++)
1896 if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
1898 if (1 == simple_cst_equal (TREE_PURPOSE (t1),
1899 TREE_PURPOSE (t2)))
1901 if (permerror (input_location,
1902 "default argument given for parameter "
1903 "%d of %q#D", i, newdecl))
1904 inform (DECL_SOURCE_LOCATION (olddecl),
1905 "previous specification in %q#D here",
1906 olddecl);
1908 else
1910 error ("default argument given for parameter %d "
1911 "of %q#D", i, newdecl);
1912 inform (DECL_SOURCE_LOCATION (olddecl),
1913 "previous specification in %q#D here",
1914 olddecl);
1921 /* Do not merge an implicit typedef with an explicit one. In:
1923 class A;
1925 typedef class A A __attribute__ ((foo));
1927 the attribute should apply only to the typedef. */
1928 if (TREE_CODE (olddecl) == TYPE_DECL
1929 && (DECL_IMPLICIT_TYPEDEF_P (olddecl)
1930 || DECL_IMPLICIT_TYPEDEF_P (newdecl)))
1931 return NULL_TREE;
1933 /* If new decl is `static' and an `extern' was seen previously,
1934 warn about it. */
1935 warn_extern_redeclared_static (newdecl, olddecl);
1937 if (!validate_constexpr_redeclaration (olddecl, newdecl))
1938 return error_mark_node;
1940 /* We have committed to returning 1 at this point. */
1941 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1943 /* Now that functions must hold information normally held
1944 by field decls, there is extra work to do so that
1945 declaration information does not get destroyed during
1946 definition. */
1947 if (DECL_VINDEX (olddecl))
1948 DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl);
1949 if (DECL_CONTEXT (olddecl))
1950 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1951 DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl);
1952 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1953 DECL_PURE_VIRTUAL_P (newdecl) |= DECL_PURE_VIRTUAL_P (olddecl);
1954 DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl);
1955 DECL_INVALID_OVERRIDER_P (newdecl) |= DECL_INVALID_OVERRIDER_P (olddecl);
1956 DECL_FINAL_P (newdecl) |= DECL_FINAL_P (olddecl);
1957 DECL_OVERRIDE_P (newdecl) |= DECL_OVERRIDE_P (olddecl);
1958 DECL_THIS_STATIC (newdecl) |= DECL_THIS_STATIC (olddecl);
1959 if (DECL_OVERLOADED_OPERATOR_P (olddecl) != ERROR_MARK)
1960 SET_OVERLOADED_OPERATOR_CODE
1961 (newdecl, DECL_OVERLOADED_OPERATOR_P (olddecl));
1962 new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE;
1964 /* Optionally warn about more than one declaration for the same
1965 name, but don't warn about a function declaration followed by a
1966 definition. */
1967 if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl)
1968 && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE)
1969 /* Don't warn about extern decl followed by definition. */
1970 && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl))
1971 /* Don't warn about friends, let add_friend take care of it. */
1972 && ! (newdecl_is_friend || DECL_FRIEND_P (olddecl))
1973 /* Don't warn about declaration followed by specialization. */
1974 && (! DECL_TEMPLATE_SPECIALIZATION (newdecl)
1975 || DECL_TEMPLATE_SPECIALIZATION (olddecl)))
1977 if (warning_at (DECL_SOURCE_LOCATION (newdecl),
1978 OPT_Wredundant_decls,
1979 "redundant redeclaration of %qD in same scope",
1980 newdecl))
1981 inform (DECL_SOURCE_LOCATION (olddecl),
1982 "previous declaration of %qD", olddecl);
1985 if (!(DECL_TEMPLATE_INSTANTIATION (olddecl)
1986 && DECL_TEMPLATE_SPECIALIZATION (newdecl)))
1988 if (DECL_DELETED_FN (newdecl))
1990 error ("deleted definition of %q+D", newdecl);
1991 inform (DECL_SOURCE_LOCATION (olddecl),
1992 "previous declaration of %qD", olddecl);
1994 DECL_DELETED_FN (newdecl) |= DECL_DELETED_FN (olddecl);
1998 /* Deal with C++: must preserve virtual function table size. */
1999 if (TREE_CODE (olddecl) == TYPE_DECL)
2001 tree newtype = TREE_TYPE (newdecl);
2002 tree oldtype = TREE_TYPE (olddecl);
2004 if (newtype != error_mark_node && oldtype != error_mark_node
2005 && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype))
2006 CLASSTYPE_FRIEND_CLASSES (newtype)
2007 = CLASSTYPE_FRIEND_CLASSES (oldtype);
2009 DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl);
2012 /* Copy all the DECL_... slots specified in the new decl
2013 except for any that we copy here from the old type. */
2014 DECL_ATTRIBUTES (newdecl)
2015 = (*targetm.merge_decl_attributes) (olddecl, newdecl);
2017 if (DECL_DECLARES_FUNCTION_P (olddecl) && DECL_DECLARES_FUNCTION_P (newdecl))
2019 olddecl_friend = DECL_FRIEND_P (olddecl);
2020 hidden_friend = (DECL_ANTICIPATED (olddecl)
2021 && DECL_HIDDEN_FRIEND_P (olddecl)
2022 && newdecl_is_friend);
2023 if (!hidden_friend)
2025 DECL_ANTICIPATED (olddecl) = 0;
2026 DECL_HIDDEN_FRIEND_P (olddecl) = 0;
2030 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2032 tree old_result;
2033 tree new_result;
2034 old_result = DECL_TEMPLATE_RESULT (olddecl);
2035 new_result = DECL_TEMPLATE_RESULT (newdecl);
2036 TREE_TYPE (olddecl) = TREE_TYPE (old_result);
2037 DECL_TEMPLATE_SPECIALIZATIONS (olddecl)
2038 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (olddecl),
2039 DECL_TEMPLATE_SPECIALIZATIONS (newdecl));
2041 DECL_ATTRIBUTES (old_result)
2042 = (*targetm.merge_decl_attributes) (old_result, new_result);
2044 if (DECL_FUNCTION_TEMPLATE_P (newdecl))
2046 /* Per C++11 8.3.6/4, default arguments cannot be added in later
2047 declarations of a function template. */
2048 if (DECL_SOURCE_LOCATION (newdecl)
2049 != DECL_SOURCE_LOCATION (olddecl))
2050 check_redeclaration_no_default_args (newdecl);
2052 check_default_args (newdecl);
2054 if (GNU_INLINE_P (old_result) != GNU_INLINE_P (new_result)
2055 && DECL_INITIAL (new_result))
2057 if (DECL_INITIAL (old_result))
2058 DECL_UNINLINABLE (old_result) = 1;
2059 else
2060 DECL_UNINLINABLE (old_result) = DECL_UNINLINABLE (new_result);
2061 DECL_EXTERNAL (old_result) = DECL_EXTERNAL (new_result);
2062 DECL_NOT_REALLY_EXTERN (old_result)
2063 = DECL_NOT_REALLY_EXTERN (new_result);
2064 DECL_INTERFACE_KNOWN (old_result)
2065 = DECL_INTERFACE_KNOWN (new_result);
2066 DECL_DECLARED_INLINE_P (old_result)
2067 = DECL_DECLARED_INLINE_P (new_result);
2068 DECL_DISREGARD_INLINE_LIMITS (old_result)
2069 |= DECL_DISREGARD_INLINE_LIMITS (new_result);
2072 else
2074 DECL_DECLARED_INLINE_P (old_result)
2075 |= DECL_DECLARED_INLINE_P (new_result);
2076 DECL_DISREGARD_INLINE_LIMITS (old_result)
2077 |= DECL_DISREGARD_INLINE_LIMITS (new_result);
2078 check_redeclaration_exception_specification (newdecl, olddecl);
2082 /* If the new declaration is a definition, update the file and
2083 line information on the declaration, and also make
2084 the old declaration the same definition. */
2085 if (DECL_INITIAL (new_result) != NULL_TREE)
2087 DECL_SOURCE_LOCATION (olddecl)
2088 = DECL_SOURCE_LOCATION (old_result)
2089 = DECL_SOURCE_LOCATION (newdecl);
2090 DECL_INITIAL (old_result) = DECL_INITIAL (new_result);
2091 if (DECL_FUNCTION_TEMPLATE_P (newdecl))
2093 tree parm;
2094 DECL_ARGUMENTS (old_result)
2095 = DECL_ARGUMENTS (new_result);
2096 for (parm = DECL_ARGUMENTS (old_result); parm;
2097 parm = DECL_CHAIN (parm))
2098 DECL_CONTEXT (parm) = old_result;
2102 return olddecl;
2105 if (types_match)
2107 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2108 check_redeclaration_exception_specification (newdecl, olddecl);
2110 /* Automatically handles default parameters. */
2111 tree oldtype = TREE_TYPE (olddecl);
2112 tree newtype;
2114 /* For typedefs use the old type, as the new type's DECL_NAME points
2115 at newdecl, which will be ggc_freed. */
2116 if (TREE_CODE (newdecl) == TYPE_DECL)
2118 /* But NEWTYPE might have an attribute, honor that. */
2119 tree tem = TREE_TYPE (newdecl);
2120 newtype = oldtype;
2122 if (TYPE_USER_ALIGN (tem))
2124 if (TYPE_ALIGN (tem) > TYPE_ALIGN (newtype))
2125 SET_TYPE_ALIGN (newtype, TYPE_ALIGN (tem));
2126 TYPE_USER_ALIGN (newtype) = true;
2129 /* And remove the new type from the variants list. */
2130 if (TYPE_NAME (TREE_TYPE (newdecl)) == newdecl)
2132 tree remove = TREE_TYPE (newdecl);
2133 for (tree t = TYPE_MAIN_VARIANT (remove); ;
2134 t = TYPE_NEXT_VARIANT (t))
2135 if (TYPE_NEXT_VARIANT (t) == remove)
2137 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (remove);
2138 break;
2142 else
2143 /* Merge the data types specified in the two decls. */
2144 newtype = merge_types (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
2146 if (VAR_P (newdecl))
2148 DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl);
2149 /* For already initialized vars, TREE_READONLY could have been
2150 cleared in cp_finish_decl, because the var needs runtime
2151 initialization or destruction. Make sure not to set
2152 TREE_READONLY on it again. */
2153 if (DECL_INITIALIZED_P (olddecl)
2154 && !DECL_EXTERNAL (olddecl)
2155 && !TREE_READONLY (olddecl))
2156 TREE_READONLY (newdecl) = 0;
2157 DECL_INITIALIZED_P (newdecl) |= DECL_INITIALIZED_P (olddecl);
2158 DECL_NONTRIVIALLY_INITIALIZED_P (newdecl)
2159 |= DECL_NONTRIVIALLY_INITIALIZED_P (olddecl);
2160 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (newdecl)
2161 |= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (olddecl);
2162 if (DECL_CLASS_SCOPE_P (olddecl))
2163 DECL_DECLARED_CONSTEXPR_P (newdecl)
2164 |= DECL_DECLARED_CONSTEXPR_P (olddecl);
2166 /* Merge the threadprivate attribute from OLDDECL into NEWDECL. */
2167 if (DECL_LANG_SPECIFIC (olddecl)
2168 && CP_DECL_THREADPRIVATE_P (olddecl))
2170 /* Allocate a LANG_SPECIFIC structure for NEWDECL, if needed. */
2171 if (!DECL_LANG_SPECIFIC (newdecl))
2172 retrofit_lang_decl (newdecl);
2174 CP_DECL_THREADPRIVATE_P (newdecl) = 1;
2178 /* An explicit specialization of a function template or of a member
2179 function of a class template can be declared transaction_safe
2180 independently of whether the corresponding template entity is declared
2181 transaction_safe. */
2182 if (flag_tm && TREE_CODE (newdecl) == FUNCTION_DECL
2183 && DECL_TEMPLATE_INSTANTIATION (olddecl)
2184 && DECL_TEMPLATE_SPECIALIZATION (newdecl)
2185 && tx_safe_fn_type_p (newtype)
2186 && !tx_safe_fn_type_p (TREE_TYPE (newdecl)))
2187 newtype = tx_unsafe_fn_variant (newtype);
2189 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype;
2191 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2192 check_default_args (newdecl);
2194 /* Lay the type out, unless already done. */
2195 if (! same_type_p (newtype, oldtype)
2196 && TREE_TYPE (newdecl) != error_mark_node
2197 && !(processing_template_decl && uses_template_parms (newdecl)))
2198 layout_type (TREE_TYPE (newdecl));
2200 if ((VAR_P (newdecl)
2201 || TREE_CODE (newdecl) == PARM_DECL
2202 || TREE_CODE (newdecl) == RESULT_DECL
2203 || TREE_CODE (newdecl) == FIELD_DECL
2204 || TREE_CODE (newdecl) == TYPE_DECL)
2205 && !(processing_template_decl && uses_template_parms (newdecl)))
2206 layout_decl (newdecl, 0);
2208 /* Merge the type qualifiers. */
2209 if (TREE_READONLY (newdecl))
2210 TREE_READONLY (olddecl) = 1;
2211 if (TREE_THIS_VOLATILE (newdecl))
2212 TREE_THIS_VOLATILE (olddecl) = 1;
2213 if (TREE_NOTHROW (newdecl))
2214 TREE_NOTHROW (olddecl) = 1;
2216 /* Merge deprecatedness. */
2217 if (TREE_DEPRECATED (newdecl))
2218 TREE_DEPRECATED (olddecl) = 1;
2220 /* Preserve function specific target and optimization options */
2221 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2223 if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
2224 && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
2225 DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
2226 = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
2228 if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
2229 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
2230 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
2231 = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
2234 /* Merge the initialization information. */
2235 if (DECL_INITIAL (newdecl) == NULL_TREE
2236 && DECL_INITIAL (olddecl) != NULL_TREE)
2238 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2239 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2240 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2242 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
2243 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
2247 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2249 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
2250 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
2251 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
2252 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
2253 TREE_NOTHROW (newdecl) |= TREE_NOTHROW (olddecl);
2254 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
2255 DECL_IS_OPERATOR_NEW (newdecl) |= DECL_IS_OPERATOR_NEW (olddecl);
2256 DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
2257 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
2258 DECL_LOOPING_CONST_OR_PURE_P (newdecl)
2259 |= DECL_LOOPING_CONST_OR_PURE_P (olddecl);
2260 /* Keep the old RTL. */
2261 COPY_DECL_RTL (olddecl, newdecl);
2263 else if (VAR_P (newdecl)
2264 && (DECL_SIZE (olddecl) || !DECL_SIZE (newdecl)))
2266 /* Keep the old RTL. We cannot keep the old RTL if the old
2267 declaration was for an incomplete object and the new
2268 declaration is not since many attributes of the RTL will
2269 change. */
2270 COPY_DECL_RTL (olddecl, newdecl);
2273 /* If cannot merge, then use the new type and qualifiers,
2274 and don't preserve the old rtl. */
2275 else
2277 /* Clean out any memory we had of the old declaration. */
2278 tree oldstatic = value_member (olddecl, static_aggregates);
2279 if (oldstatic)
2280 TREE_VALUE (oldstatic) = error_mark_node;
2282 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
2283 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
2284 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
2285 TREE_NOTHROW (olddecl) = TREE_NOTHROW (newdecl);
2286 TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
2289 /* Merge the storage class information. */
2290 merge_weak (newdecl, olddecl);
2292 DECL_DEFER_OUTPUT (newdecl) |= DECL_DEFER_OUTPUT (olddecl);
2293 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
2294 TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl);
2295 if (! DECL_EXTERNAL (olddecl))
2296 DECL_EXTERNAL (newdecl) = 0;
2297 if (! DECL_COMDAT (olddecl))
2298 DECL_COMDAT (newdecl) = 0;
2300 new_template_info = NULL_TREE;
2301 if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl))
2303 bool new_redefines_gnu_inline = false;
2305 if (new_defines_function
2306 && ((DECL_INTERFACE_KNOWN (olddecl)
2307 && TREE_CODE (olddecl) == FUNCTION_DECL)
2308 || (TREE_CODE (olddecl) == TEMPLATE_DECL
2309 && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl))
2310 == FUNCTION_DECL))))
2312 tree fn = olddecl;
2314 if (TREE_CODE (fn) == TEMPLATE_DECL)
2315 fn = DECL_TEMPLATE_RESULT (olddecl);
2317 new_redefines_gnu_inline = GNU_INLINE_P (fn) && DECL_INITIAL (fn);
2320 if (!new_redefines_gnu_inline)
2322 DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl);
2323 DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl);
2324 DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl);
2326 DECL_TEMPLATE_INSTANTIATED (newdecl)
2327 |= DECL_TEMPLATE_INSTANTIATED (olddecl);
2328 DECL_ODR_USED (newdecl) |= DECL_ODR_USED (olddecl);
2330 /* If the OLDDECL is an instantiation and/or specialization,
2331 then the NEWDECL must be too. But, it may not yet be marked
2332 as such if the caller has created NEWDECL, but has not yet
2333 figured out that it is a redeclaration. */
2334 if (!DECL_USE_TEMPLATE (newdecl))
2335 DECL_USE_TEMPLATE (newdecl) = DECL_USE_TEMPLATE (olddecl);
2337 /* Don't really know how much of the language-specific
2338 values we should copy from old to new. */
2339 DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl);
2340 DECL_REPO_AVAILABLE_P (newdecl) = DECL_REPO_AVAILABLE_P (olddecl);
2341 DECL_INITIALIZED_IN_CLASS_P (newdecl)
2342 |= DECL_INITIALIZED_IN_CLASS_P (olddecl);
2344 if (LANG_DECL_HAS_MIN (newdecl))
2346 DECL_LANG_SPECIFIC (newdecl)->u.min.u2 =
2347 DECL_LANG_SPECIFIC (olddecl)->u.min.u2;
2348 if (DECL_TEMPLATE_INFO (newdecl))
2350 new_template_info = DECL_TEMPLATE_INFO (newdecl);
2351 if (DECL_TEMPLATE_INSTANTIATION (olddecl)
2352 && DECL_TEMPLATE_SPECIALIZATION (newdecl))
2353 /* Remember the presence of explicit specialization args. */
2354 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (olddecl))
2355 = TINFO_USED_TEMPLATE_ID (new_template_info);
2357 DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl);
2359 /* Only functions have these fields. */
2360 if (DECL_DECLARES_FUNCTION_P (newdecl))
2362 DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl);
2363 DECL_BEFRIENDING_CLASSES (newdecl)
2364 = chainon (DECL_BEFRIENDING_CLASSES (newdecl),
2365 DECL_BEFRIENDING_CLASSES (olddecl));
2366 /* DECL_THUNKS is only valid for virtual functions,
2367 otherwise it is a DECL_FRIEND_CONTEXT. */
2368 if (DECL_VIRTUAL_P (newdecl))
2369 SET_DECL_THUNKS (newdecl, DECL_THUNKS (olddecl));
2371 /* Only variables have this field. */
2372 else if (VAR_P (newdecl)
2373 && VAR_HAD_UNKNOWN_BOUND (olddecl))
2374 SET_VAR_HAD_UNKNOWN_BOUND (newdecl);
2377 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2379 tree parm;
2381 /* Merge parameter attributes. */
2382 tree oldarg, newarg;
2383 for (oldarg = DECL_ARGUMENTS(olddecl),
2384 newarg = DECL_ARGUMENTS(newdecl);
2385 oldarg && newarg;
2386 oldarg = DECL_CHAIN(oldarg), newarg = DECL_CHAIN(newarg)) {
2387 DECL_ATTRIBUTES (newarg)
2388 = (*targetm.merge_decl_attributes) (oldarg, newarg);
2389 DECL_ATTRIBUTES (oldarg) = DECL_ATTRIBUTES (newarg);
2392 if (DECL_TEMPLATE_INSTANTIATION (olddecl)
2393 && !DECL_TEMPLATE_INSTANTIATION (newdecl))
2395 /* If newdecl is not a specialization, then it is not a
2396 template-related function at all. And that means that we
2397 should have exited above, returning 0. */
2398 gcc_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl));
2400 if (DECL_ODR_USED (olddecl))
2401 /* From [temp.expl.spec]:
2403 If a template, a member template or the member of a class
2404 template is explicitly specialized then that
2405 specialization shall be declared before the first use of
2406 that specialization that would cause an implicit
2407 instantiation to take place, in every translation unit in
2408 which such a use occurs. */
2409 error ("explicit specialization of %qD after first use",
2410 olddecl);
2412 SET_DECL_TEMPLATE_SPECIALIZATION (olddecl);
2413 DECL_COMDAT (newdecl) = (TREE_PUBLIC (newdecl)
2414 && DECL_DECLARED_INLINE_P (newdecl));
2416 /* Don't propagate visibility from the template to the
2417 specialization here. We'll do that in determine_visibility if
2418 appropriate. */
2419 DECL_VISIBILITY_SPECIFIED (olddecl) = 0;
2421 /* [temp.expl.spec/14] We don't inline explicit specialization
2422 just because the primary template says so. */
2424 /* But still keep DECL_DISREGARD_INLINE_LIMITS in sync with
2425 the always_inline attribute. */
2426 if (DECL_DISREGARD_INLINE_LIMITS (olddecl)
2427 && !DECL_DISREGARD_INLINE_LIMITS (newdecl))
2429 if (DECL_DECLARED_INLINE_P (newdecl))
2430 DECL_DISREGARD_INLINE_LIMITS (newdecl) = true;
2431 else
2432 DECL_ATTRIBUTES (newdecl)
2433 = remove_attribute ("always_inline",
2434 DECL_ATTRIBUTES (newdecl));
2437 else if (new_defines_function && DECL_INITIAL (olddecl))
2439 /* Never inline re-defined extern inline functions.
2440 FIXME: this could be better handled by keeping both
2441 function as separate declarations. */
2442 DECL_UNINLINABLE (newdecl) = 1;
2444 else
2446 if (DECL_PENDING_INLINE_P (olddecl))
2448 DECL_PENDING_INLINE_P (newdecl) = 1;
2449 DECL_PENDING_INLINE_INFO (newdecl)
2450 = DECL_PENDING_INLINE_INFO (olddecl);
2452 else if (DECL_PENDING_INLINE_P (newdecl))
2454 else if (DECL_SAVED_FUNCTION_DATA (newdecl) == NULL)
2455 DECL_SAVED_FUNCTION_DATA (newdecl)
2456 = DECL_SAVED_FUNCTION_DATA (olddecl);
2458 DECL_DECLARED_INLINE_P (newdecl) |= DECL_DECLARED_INLINE_P (olddecl);
2460 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
2461 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
2463 DECL_DISREGARD_INLINE_LIMITS (newdecl)
2464 = DECL_DISREGARD_INLINE_LIMITS (olddecl)
2465 = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
2466 || DECL_DISREGARD_INLINE_LIMITS (olddecl));
2469 /* Preserve abstractness on cloned [cd]tors. */
2470 DECL_ABSTRACT_P (newdecl) = DECL_ABSTRACT_P (olddecl);
2472 /* Update newdecl's parms to point at olddecl. */
2473 for (parm = DECL_ARGUMENTS (newdecl); parm;
2474 parm = DECL_CHAIN (parm))
2475 DECL_CONTEXT (parm) = olddecl;
2477 if (! types_match)
2479 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
2480 COPY_DECL_ASSEMBLER_NAME (newdecl, olddecl);
2481 COPY_DECL_RTL (newdecl, olddecl);
2483 if (! types_match || new_defines_function)
2485 /* These need to be copied so that the names are available.
2486 Note that if the types do match, we'll preserve inline
2487 info and other bits, but if not, we won't. */
2488 DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl);
2489 DECL_RESULT (olddecl) = DECL_RESULT (newdecl);
2491 /* If redeclaring a builtin function, it stays built in
2492 if newdecl is a gnu_inline definition, or if newdecl is just
2493 a declaration. */
2494 if (DECL_BUILT_IN (olddecl)
2495 && (new_defines_function ? GNU_INLINE_P (newdecl) : types_match))
2497 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
2498 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
2499 /* If we're keeping the built-in definition, keep the rtl,
2500 regardless of declaration matches. */
2501 COPY_DECL_RTL (olddecl, newdecl);
2502 if (DECL_BUILT_IN_CLASS (newdecl) == BUILT_IN_NORMAL)
2504 enum built_in_function fncode = DECL_FUNCTION_CODE (newdecl);
2505 switch (fncode)
2507 /* If a compatible prototype of these builtin functions
2508 is seen, assume the runtime implements it with the
2509 expected semantics. */
2510 case BUILT_IN_STPCPY:
2511 if (builtin_decl_explicit_p (fncode))
2512 set_builtin_decl_implicit_p (fncode, true);
2513 break;
2514 default:
2515 if (builtin_decl_explicit_p (fncode))
2516 set_builtin_decl_declared_p (fncode, true);
2517 break;
2521 if (new_defines_function)
2522 /* If defining a function declared with other language
2523 linkage, use the previously declared language linkage. */
2524 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
2525 else if (types_match)
2527 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
2528 /* Don't clear out the arguments if we're just redeclaring a
2529 function. */
2530 if (DECL_ARGUMENTS (olddecl))
2531 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
2534 else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
2535 NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl);
2537 /* Now preserve various other info from the definition. */
2538 TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl);
2539 TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl);
2540 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
2541 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
2543 /* Warn about conflicting visibility specifications. */
2544 if (DECL_VISIBILITY_SPECIFIED (olddecl)
2545 && DECL_VISIBILITY_SPECIFIED (newdecl)
2546 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
2548 if (warning_at (DECL_SOURCE_LOCATION (newdecl), OPT_Wattributes,
2549 "%qD: visibility attribute ignored because it "
2550 "conflicts with previous declaration", newdecl))
2551 inform (DECL_SOURCE_LOCATION (olddecl),
2552 "previous declaration of %qD", olddecl);
2554 /* Choose the declaration which specified visibility. */
2555 if (DECL_VISIBILITY_SPECIFIED (olddecl))
2557 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
2558 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
2560 /* Init priority used to be merged from newdecl to olddecl by the memcpy,
2561 so keep this behavior. */
2562 if (VAR_P (newdecl) && DECL_HAS_INIT_PRIORITY_P (newdecl))
2564 SET_DECL_INIT_PRIORITY (olddecl, DECL_INIT_PRIORITY (newdecl));
2565 DECL_HAS_INIT_PRIORITY_P (olddecl) = 1;
2567 /* Likewise for DECL_ALIGN, DECL_USER_ALIGN and DECL_PACKED. */
2568 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
2570 SET_DECL_ALIGN (newdecl, DECL_ALIGN (olddecl));
2571 DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
2573 DECL_USER_ALIGN (olddecl) = DECL_USER_ALIGN (newdecl);
2574 if (TREE_CODE (newdecl) == FIELD_DECL)
2575 DECL_PACKED (olddecl) = DECL_PACKED (newdecl);
2577 /* The DECL_LANG_SPECIFIC information in OLDDECL will be replaced
2578 with that from NEWDECL below. */
2579 if (DECL_LANG_SPECIFIC (olddecl))
2581 gcc_assert (DECL_LANG_SPECIFIC (olddecl)
2582 != DECL_LANG_SPECIFIC (newdecl));
2583 ggc_free (DECL_LANG_SPECIFIC (olddecl));
2586 /* Merge the USED information. */
2587 if (TREE_USED (olddecl))
2588 TREE_USED (newdecl) = 1;
2589 else if (TREE_USED (newdecl))
2590 TREE_USED (olddecl) = 1;
2591 if (VAR_P (newdecl))
2593 if (DECL_READ_P (olddecl))
2594 DECL_READ_P (newdecl) = 1;
2595 else if (DECL_READ_P (newdecl))
2596 DECL_READ_P (olddecl) = 1;
2598 if (DECL_PRESERVE_P (olddecl))
2599 DECL_PRESERVE_P (newdecl) = 1;
2600 else if (DECL_PRESERVE_P (newdecl))
2601 DECL_PRESERVE_P (olddecl) = 1;
2603 /* Merge the DECL_FUNCTION_VERSIONED information. newdecl will be copied
2604 to olddecl and deleted. */
2605 if (TREE_CODE (newdecl) == FUNCTION_DECL
2606 && DECL_FUNCTION_VERSIONED (olddecl))
2608 /* Set the flag for newdecl so that it gets copied to olddecl. */
2609 DECL_FUNCTION_VERSIONED (newdecl) = 1;
2610 /* newdecl will be purged after copying to olddecl and is no longer
2611 a version. */
2612 cgraph_node::delete_function_version (newdecl);
2615 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2617 int function_size;
2618 struct symtab_node *snode = symtab_node::get (olddecl);
2620 function_size = sizeof (struct tree_decl_common);
2622 memcpy ((char *) olddecl + sizeof (struct tree_common),
2623 (char *) newdecl + sizeof (struct tree_common),
2624 function_size - sizeof (struct tree_common));
2626 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2627 (char *) newdecl + sizeof (struct tree_decl_common),
2628 sizeof (struct tree_function_decl) - sizeof (struct tree_decl_common));
2630 /* Preserve symtab node mapping. */
2631 olddecl->decl_with_vis.symtab_node = snode;
2633 if (new_template_info)
2634 /* If newdecl is a template instantiation, it is possible that
2635 the following sequence of events has occurred:
2637 o A friend function was declared in a class template. The
2638 class template was instantiated.
2640 o The instantiation of the friend declaration was
2641 recorded on the instantiation list, and is newdecl.
2643 o Later, however, instantiate_class_template called pushdecl
2644 on the newdecl to perform name injection. But, pushdecl in
2645 turn called duplicate_decls when it discovered that another
2646 declaration of a global function with the same name already
2647 existed.
2649 o Here, in duplicate_decls, we decided to clobber newdecl.
2651 If we're going to do that, we'd better make sure that
2652 olddecl, and not newdecl, is on the list of
2653 instantiations so that if we try to do the instantiation
2654 again we won't get the clobbered declaration. */
2655 reregister_specialization (newdecl,
2656 new_template_info,
2657 olddecl);
2659 else
2661 size_t size = tree_code_size (TREE_CODE (newdecl));
2663 memcpy ((char *) olddecl + sizeof (struct tree_common),
2664 (char *) newdecl + sizeof (struct tree_common),
2665 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
2666 switch (TREE_CODE (newdecl))
2668 case LABEL_DECL:
2669 case VAR_DECL:
2670 case RESULT_DECL:
2671 case PARM_DECL:
2672 case FIELD_DECL:
2673 case TYPE_DECL:
2674 case CONST_DECL:
2676 struct symtab_node *snode = NULL;
2678 if (VAR_P (newdecl)
2679 && (TREE_STATIC (olddecl) || TREE_PUBLIC (olddecl)
2680 || DECL_EXTERNAL (olddecl)))
2681 snode = symtab_node::get (olddecl);
2682 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2683 (char *) newdecl + sizeof (struct tree_decl_common),
2684 size - sizeof (struct tree_decl_common)
2685 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
2686 if (VAR_P (newdecl))
2687 olddecl->decl_with_vis.symtab_node = snode;
2689 break;
2690 default:
2691 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2692 (char *) newdecl + sizeof (struct tree_decl_common),
2693 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common)
2694 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
2695 break;
2699 if (VAR_OR_FUNCTION_DECL_P (newdecl))
2701 if (DECL_EXTERNAL (olddecl)
2702 || TREE_PUBLIC (olddecl)
2703 || TREE_STATIC (olddecl))
2705 /* Merge the section attribute.
2706 We want to issue an error if the sections conflict but that must be
2707 done later in decl_attributes since we are called before attributes
2708 are assigned. */
2709 if (DECL_SECTION_NAME (newdecl) != NULL)
2710 set_decl_section_name (olddecl, DECL_SECTION_NAME (newdecl));
2712 if (DECL_ONE_ONLY (newdecl))
2714 struct symtab_node *oldsym, *newsym;
2715 if (TREE_CODE (olddecl) == FUNCTION_DECL)
2716 oldsym = cgraph_node::get_create (olddecl);
2717 else
2718 oldsym = varpool_node::get_create (olddecl);
2719 newsym = symtab_node::get (newdecl);
2720 oldsym->set_comdat_group (newsym->get_comdat_group ());
2724 if (VAR_P (newdecl)
2725 && CP_DECL_THREAD_LOCAL_P (newdecl))
2727 CP_DECL_THREAD_LOCAL_P (olddecl) = true;
2728 if (!processing_template_decl)
2729 set_decl_tls_model (olddecl, DECL_TLS_MODEL (newdecl));
2733 DECL_UID (olddecl) = olddecl_uid;
2734 if (olddecl_friend)
2735 DECL_FRIEND_P (olddecl) = 1;
2736 if (hidden_friend)
2738 DECL_ANTICIPATED (olddecl) = 1;
2739 DECL_HIDDEN_FRIEND_P (olddecl) = 1;
2742 /* NEWDECL contains the merged attribute lists.
2743 Update OLDDECL to be the same. */
2744 DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
2746 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
2747 so that encode_section_info has a chance to look at the new decl
2748 flags and attributes. */
2749 if (DECL_RTL_SET_P (olddecl)
2750 && (TREE_CODE (olddecl) == FUNCTION_DECL
2751 || (VAR_P (olddecl)
2752 && TREE_STATIC (olddecl))))
2753 make_decl_rtl (olddecl);
2755 /* The NEWDECL will no longer be needed. Because every out-of-class
2756 declaration of a member results in a call to duplicate_decls,
2757 freeing these nodes represents in a significant savings.
2759 Before releasing the node, be sore to remove function from symbol
2760 table that might have been inserted there to record comdat group.
2761 Be sure to however do not free DECL_STRUCT_FUNCTION because this
2762 structure is shared in between newdecl and oldecl. */
2763 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2764 DECL_STRUCT_FUNCTION (newdecl) = NULL;
2765 if (VAR_OR_FUNCTION_DECL_P (newdecl))
2767 struct symtab_node *snode = symtab_node::get (newdecl);
2768 if (snode)
2769 snode->remove ();
2772 /* Remove the associated constraints for newdecl, if any, before
2773 reclaiming memory. */
2774 if (flag_concepts)
2775 remove_constraints (newdecl);
2777 ggc_free (newdecl);
2779 return olddecl;
2782 /* Return zero if the declaration NEWDECL is valid
2783 when the declaration OLDDECL (assumed to be for the same name)
2784 has already been seen.
2785 Otherwise return an error message format string with a %s
2786 where the identifier should go. */
2788 static const char *
2789 redeclaration_error_message (tree newdecl, tree olddecl)
2791 if (TREE_CODE (newdecl) == TYPE_DECL)
2793 /* Because C++ can put things into name space for free,
2794 constructs like "typedef struct foo { ... } foo"
2795 would look like an erroneous redeclaration. */
2796 if (same_type_p (TREE_TYPE (newdecl), TREE_TYPE (olddecl)))
2797 return NULL;
2798 else
2799 return G_("redefinition of %q#D");
2801 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
2803 /* If this is a pure function, its olddecl will actually be
2804 the original initialization to `0' (which we force to call
2805 abort()). Don't complain about redefinition in this case. */
2806 if (DECL_LANG_SPECIFIC (olddecl) && DECL_PURE_VIRTUAL_P (olddecl)
2807 && DECL_INITIAL (olddecl) == NULL_TREE)
2808 return NULL;
2810 /* If both functions come from different namespaces, this is not
2811 a redeclaration - this is a conflict with a used function. */
2812 if (DECL_NAMESPACE_SCOPE_P (olddecl)
2813 && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl)
2814 && ! decls_match (olddecl, newdecl))
2815 return G_("%qD conflicts with used function");
2817 /* We'll complain about linkage mismatches in
2818 warn_extern_redeclared_static. */
2820 /* Defining the same name twice is no good. */
2821 if (decl_defined_p (olddecl)
2822 && decl_defined_p (newdecl))
2824 if (DECL_NAME (olddecl) == NULL_TREE)
2825 return G_("%q#D not declared in class");
2826 else if (!GNU_INLINE_P (olddecl)
2827 || GNU_INLINE_P (newdecl))
2828 return G_("redefinition of %q#D");
2831 if (DECL_DECLARED_INLINE_P (olddecl) && DECL_DECLARED_INLINE_P (newdecl))
2833 bool olda = GNU_INLINE_P (olddecl);
2834 bool newa = GNU_INLINE_P (newdecl);
2836 if (olda != newa)
2838 if (newa)
2839 return G_("%q+D redeclared inline with "
2840 "%<gnu_inline%> attribute");
2841 else
2842 return G_("%q+D redeclared inline without "
2843 "%<gnu_inline%> attribute");
2847 check_abi_tag_redeclaration
2848 (olddecl, lookup_attribute ("abi_tag", DECL_ATTRIBUTES (olddecl)),
2849 lookup_attribute ("abi_tag", DECL_ATTRIBUTES (newdecl)));
2851 return NULL;
2853 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2855 tree nt, ot;
2857 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
2859 if (COMPLETE_TYPE_P (TREE_TYPE (newdecl))
2860 && COMPLETE_TYPE_P (TREE_TYPE (olddecl)))
2861 return G_("redefinition of %q#D");
2862 return NULL;
2865 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) != FUNCTION_DECL
2866 || (DECL_TEMPLATE_RESULT (newdecl)
2867 == DECL_TEMPLATE_RESULT (olddecl)))
2868 return NULL;
2870 nt = DECL_TEMPLATE_RESULT (newdecl);
2871 if (DECL_TEMPLATE_INFO (nt))
2872 nt = DECL_TEMPLATE_RESULT (template_for_substitution (nt));
2873 ot = DECL_TEMPLATE_RESULT (olddecl);
2874 if (DECL_TEMPLATE_INFO (ot))
2875 ot = DECL_TEMPLATE_RESULT (template_for_substitution (ot));
2876 if (DECL_INITIAL (nt) && DECL_INITIAL (ot)
2877 && (!GNU_INLINE_P (ot) || GNU_INLINE_P (nt)))
2878 return G_("redefinition of %q#D");
2880 if (DECL_DECLARED_INLINE_P (ot) && DECL_DECLARED_INLINE_P (nt))
2882 bool olda = GNU_INLINE_P (ot);
2883 bool newa = GNU_INLINE_P (nt);
2885 if (olda != newa)
2887 if (newa)
2888 return G_("%q+D redeclared inline with "
2889 "%<gnu_inline%> attribute");
2890 else
2891 return G_("%q+D redeclared inline without "
2892 "%<gnu_inline%> attribute");
2896 /* Core issue #226 (C++0x):
2898 If a friend function template declaration specifies a
2899 default template-argument, that declaration shall be a
2900 definition and shall be the only declaration of the
2901 function template in the translation unit. */
2902 if ((cxx_dialect != cxx98)
2903 && TREE_CODE (ot) == FUNCTION_DECL && DECL_FRIEND_P (ot)
2904 && !check_default_tmpl_args (nt, DECL_TEMPLATE_PARMS (newdecl),
2905 /*is_primary=*/true,
2906 /*is_partial=*/false,
2907 /*is_friend_decl=*/2))
2908 return G_("redeclaration of friend %q#D "
2909 "may not have default template arguments");
2911 return NULL;
2913 else if (VAR_P (newdecl)
2914 && CP_DECL_THREAD_LOCAL_P (newdecl) != CP_DECL_THREAD_LOCAL_P (olddecl)
2915 && (! DECL_LANG_SPECIFIC (olddecl)
2916 || ! CP_DECL_THREADPRIVATE_P (olddecl)
2917 || CP_DECL_THREAD_LOCAL_P (newdecl)))
2919 /* Only variables can be thread-local, and all declarations must
2920 agree on this property. */
2921 if (CP_DECL_THREAD_LOCAL_P (newdecl))
2922 return G_("thread-local declaration of %q#D follows "
2923 "non-thread-local declaration");
2924 else
2925 return G_("non-thread-local declaration of %q#D follows "
2926 "thread-local declaration");
2928 else if (toplevel_bindings_p () || DECL_NAMESPACE_SCOPE_P (newdecl))
2930 /* The objects have been declared at namespace scope. If either
2931 is a member of an anonymous union, then this is an invalid
2932 redeclaration. For example:
2934 int i;
2935 union { int i; };
2937 is invalid. */
2938 if ((VAR_P (newdecl) && DECL_ANON_UNION_VAR_P (newdecl))
2939 || (VAR_P (olddecl) && DECL_ANON_UNION_VAR_P (olddecl)))
2940 return G_("redeclaration of %q#D");
2941 /* If at least one declaration is a reference, there is no
2942 conflict. For example:
2944 int i = 3;
2945 extern int i;
2947 is valid. */
2948 if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
2949 return NULL;
2951 /* Static data member declared outside a class definition
2952 if the variable is defined within the class with constexpr
2953 specifier is declaration rather than definition (and
2954 deprecated). */
2955 if (cxx_dialect >= cxx1z
2956 && DECL_CLASS_SCOPE_P (olddecl)
2957 && DECL_DECLARED_CONSTEXPR_P (olddecl)
2958 && !DECL_INITIAL (newdecl))
2960 DECL_EXTERNAL (newdecl) = 1;
2961 /* For now, only warn with explicit -Wdeprecated. */
2962 if (global_options_set.x_warn_deprecated
2963 && warning_at (DECL_SOURCE_LOCATION (newdecl), OPT_Wdeprecated,
2964 "redundant redeclaration of %<constexpr%> static "
2965 "data member %qD", newdecl))
2966 inform (DECL_SOURCE_LOCATION (olddecl),
2967 "previous declaration of %qD", olddecl);
2968 return NULL;
2971 /* Reject two definitions. */
2972 return G_("redefinition of %q#D");
2974 else
2976 /* Objects declared with block scope: */
2977 /* Reject two definitions, and reject a definition
2978 together with an external reference. */
2979 if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl)))
2980 return G_("redeclaration of %q#D");
2981 return NULL;
2985 /* Hash and equality functions for the named_label table. */
2987 hashval_t
2988 named_label_hasher::hash (named_label_entry *ent)
2990 return DECL_UID (ent->label_decl);
2993 bool
2994 named_label_hasher::equal (named_label_entry *a, named_label_entry *b)
2996 return a->label_decl == b->label_decl;
2999 /* Create a new label, named ID. */
3001 static tree
3002 make_label_decl (tree id, int local_p)
3004 struct named_label_entry *ent;
3005 tree decl;
3007 decl = build_decl (input_location, LABEL_DECL, id, void_type_node);
3009 DECL_CONTEXT (decl) = current_function_decl;
3010 SET_DECL_MODE (decl, VOIDmode);
3011 C_DECLARED_LABEL_FLAG (decl) = local_p;
3013 /* Say where one reference is to the label, for the sake of the
3014 error if it is not defined. */
3015 DECL_SOURCE_LOCATION (decl) = input_location;
3017 /* Record the fact that this identifier is bound to this label. */
3018 SET_IDENTIFIER_LABEL_VALUE (id, decl);
3020 /* Create the label htab for the function on demand. */
3021 if (!named_labels)
3022 named_labels = hash_table<named_label_hasher>::create_ggc (13);
3024 /* Record this label on the list of labels used in this function.
3025 We do this before calling make_label_decl so that we get the
3026 IDENTIFIER_LABEL_VALUE before the new label is declared. */
3027 ent = ggc_cleared_alloc<named_label_entry> ();
3028 ent->label_decl = decl;
3030 named_label_entry **slot = named_labels->find_slot (ent, INSERT);
3031 gcc_assert (*slot == NULL);
3032 *slot = ent;
3034 return decl;
3037 /* Look for a label named ID in the current function. If one cannot
3038 be found, create one. (We keep track of used, but undefined,
3039 labels, and complain about them at the end of a function.) */
3041 static tree
3042 lookup_label_1 (tree id)
3044 tree decl;
3046 /* You can't use labels at global scope. */
3047 if (current_function_decl == NULL_TREE)
3049 error ("label %qE referenced outside of any function", id);
3050 return NULL_TREE;
3053 /* See if we've already got this label. */
3054 decl = IDENTIFIER_LABEL_VALUE (id);
3055 if (decl != NULL_TREE && DECL_CONTEXT (decl) == current_function_decl)
3056 return decl;
3058 decl = make_label_decl (id, /*local_p=*/0);
3059 return decl;
3062 /* Wrapper for lookup_label_1. */
3064 tree
3065 lookup_label (tree id)
3067 tree ret;
3068 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3069 ret = lookup_label_1 (id);
3070 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3071 return ret;
3074 /* Declare a local label named ID. */
3076 tree
3077 declare_local_label (tree id)
3079 tree decl;
3080 cp_label_binding bind;
3082 /* Add a new entry to the SHADOWED_LABELS list so that when we leave
3083 this scope we can restore the old value of IDENTIFIER_TYPE_VALUE. */
3084 bind.prev_value = IDENTIFIER_LABEL_VALUE (id);
3086 decl = make_label_decl (id, /*local_p=*/1);
3087 bind.label = decl;
3088 vec_safe_push (current_binding_level->shadowed_labels, bind);
3090 return decl;
3093 /* Returns nonzero if it is ill-formed to jump past the declaration of
3094 DECL. Returns 2 if it's also a real problem. */
3096 static int
3097 decl_jump_unsafe (tree decl)
3099 /* [stmt.dcl]/3: A program that jumps from a point where a local variable
3100 with automatic storage duration is not in scope to a point where it is
3101 in scope is ill-formed unless the variable has scalar type, class type
3102 with a trivial default constructor and a trivial destructor, a
3103 cv-qualified version of one of these types, or an array of one of the
3104 preceding types and is declared without an initializer (8.5). */
3105 tree type = TREE_TYPE (decl);
3107 if (!VAR_P (decl) || TREE_STATIC (decl)
3108 || type == error_mark_node)
3109 return 0;
3111 if (DECL_NONTRIVIALLY_INITIALIZED_P (decl)
3112 || variably_modified_type_p (type, NULL_TREE))
3113 return 2;
3115 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3116 return 1;
3118 return 0;
3121 /* A subroutine of check_previous_goto_1 and check_goto to identify a branch
3122 to the user. */
3124 static bool
3125 identify_goto (tree decl, location_t loc, const location_t *locus,
3126 diagnostic_t diag_kind)
3128 bool complained
3129 = (decl ? emit_diagnostic (diag_kind, loc, 0, "jump to label %qD", decl)
3130 : emit_diagnostic (diag_kind, loc, 0, "jump to case label"));
3131 if (complained && locus)
3132 inform (*locus, " from here");
3133 return complained;
3136 /* Check that a single previously seen jump to a newly defined label
3137 is OK. DECL is the LABEL_DECL or 0; LEVEL is the binding_level for
3138 the jump context; NAMES are the names in scope in LEVEL at the jump
3139 context; LOCUS is the source position of the jump or 0. Returns
3140 true if all is well. */
3142 static bool
3143 check_previous_goto_1 (tree decl, cp_binding_level* level, tree names,
3144 bool exited_omp, const location_t *locus)
3146 cp_binding_level *b;
3147 bool complained = false;
3148 int identified = 0;
3149 bool saw_eh = false, saw_omp = false, saw_tm = false, saw_cxif = false;
3151 if (exited_omp)
3153 complained = identify_goto (decl, input_location, locus, DK_ERROR);
3154 if (complained)
3155 inform (input_location, " exits OpenMP structured block");
3156 saw_omp = true;
3157 identified = 2;
3160 for (b = current_binding_level; b ; b = b->level_chain)
3162 tree new_decls, old_decls = (b == level ? names : NULL_TREE);
3164 for (new_decls = b->names; new_decls != old_decls;
3165 new_decls = (DECL_P (new_decls) ? DECL_CHAIN (new_decls)
3166 : TREE_CHAIN (new_decls)))
3168 int problem = decl_jump_unsafe (new_decls);
3169 if (! problem)
3170 continue;
3172 if (!identified)
3174 complained = identify_goto (decl, input_location, locus,
3175 DK_PERMERROR);
3176 identified = 1;
3178 if (complained)
3180 if (problem > 1)
3181 inform (DECL_SOURCE_LOCATION (new_decls),
3182 " crosses initialization of %q#D", new_decls);
3183 else
3184 inform (DECL_SOURCE_LOCATION (new_decls),
3185 " enters scope of %q#D which has "
3186 "non-trivial destructor", new_decls);
3190 if (b == level)
3191 break;
3192 if ((b->kind == sk_try || b->kind == sk_catch) && !saw_eh)
3194 if (identified < 2)
3196 complained = identify_goto (decl, input_location, locus,
3197 DK_ERROR);
3198 identified = 2;
3200 if (complained)
3202 if (b->kind == sk_try)
3203 inform (input_location, " enters try block");
3204 else
3205 inform (input_location, " enters catch block");
3207 saw_eh = true;
3209 if (b->kind == sk_omp && !saw_omp)
3211 if (identified < 2)
3213 complained = identify_goto (decl, input_location, locus,
3214 DK_ERROR);
3215 identified = 2;
3217 if (complained)
3218 inform (input_location, " enters OpenMP structured block");
3219 saw_omp = true;
3221 if (b->kind == sk_transaction && !saw_tm)
3223 if (identified < 2)
3225 complained = identify_goto (decl, input_location, locus,
3226 DK_ERROR);
3227 identified = 2;
3229 if (complained)
3230 inform (input_location,
3231 " enters synchronized or atomic statement");
3232 saw_tm = true;
3234 if (!saw_cxif && b->kind == sk_block
3235 && level_for_constexpr_if (b->level_chain))
3237 if (identified < 2)
3239 complained = identify_goto (decl, input_location, locus,
3240 DK_ERROR);
3241 identified = 2;
3243 if (complained)
3244 inform (EXPR_LOCATION (b->level_chain->this_entity),
3245 " enters constexpr if statement");
3246 saw_cxif = true;
3250 return !identified;
3253 static void
3254 check_previous_goto (tree decl, struct named_label_use_entry *use)
3256 check_previous_goto_1 (decl, use->binding_level,
3257 use->names_in_scope, use->in_omp_scope,
3258 &use->o_goto_locus);
3261 static bool
3262 check_switch_goto (cp_binding_level* level)
3264 return check_previous_goto_1 (NULL_TREE, level, level->names, false, NULL);
3267 /* Check that a new jump to a label DECL is OK. Called by
3268 finish_goto_stmt. */
3270 void
3271 check_goto (tree decl)
3273 struct named_label_entry *ent, dummy;
3274 bool saw_catch = false, complained = false;
3275 int identified = 0;
3276 tree bad;
3277 unsigned ix;
3279 /* We can't know where a computed goto is jumping.
3280 So we assume that it's OK. */
3281 if (TREE_CODE (decl) != LABEL_DECL)
3282 return;
3284 /* We didn't record any information about this label when we created it,
3285 and there's not much point since it's trivial to analyze as a return. */
3286 if (decl == cdtor_label)
3287 return;
3289 dummy.label_decl = decl;
3290 ent = named_labels->find (&dummy);
3291 gcc_assert (ent != NULL);
3293 /* If the label hasn't been defined yet, defer checking. */
3294 if (! DECL_INITIAL (decl))
3296 struct named_label_use_entry *new_use;
3298 /* Don't bother creating another use if the last goto had the
3299 same data, and will therefore create the same set of errors. */
3300 if (ent->uses
3301 && ent->uses->names_in_scope == current_binding_level->names)
3302 return;
3304 new_use = ggc_alloc<named_label_use_entry> ();
3305 new_use->binding_level = current_binding_level;
3306 new_use->names_in_scope = current_binding_level->names;
3307 new_use->o_goto_locus = input_location;
3308 new_use->in_omp_scope = false;
3310 new_use->next = ent->uses;
3311 ent->uses = new_use;
3312 return;
3315 if (ent->in_try_scope || ent->in_catch_scope || ent->in_transaction_scope
3316 || ent->in_constexpr_if
3317 || ent->in_omp_scope || !vec_safe_is_empty (ent->bad_decls))
3319 diagnostic_t diag_kind = DK_PERMERROR;
3320 if (ent->in_try_scope || ent->in_catch_scope || ent->in_constexpr_if
3321 || ent->in_transaction_scope || ent->in_omp_scope)
3322 diag_kind = DK_ERROR;
3323 complained = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
3324 &input_location, diag_kind);
3325 identified = 1 + (diag_kind == DK_ERROR);
3328 FOR_EACH_VEC_SAFE_ELT (ent->bad_decls, ix, bad)
3330 int u = decl_jump_unsafe (bad);
3332 if (u > 1 && DECL_ARTIFICIAL (bad))
3334 /* Can't skip init of __exception_info. */
3335 if (identified == 1)
3337 complained = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
3338 &input_location, DK_ERROR);
3339 identified = 2;
3341 if (complained)
3342 inform (DECL_SOURCE_LOCATION (bad), " enters catch block");
3343 saw_catch = true;
3345 else if (complained)
3347 if (u > 1)
3348 inform (DECL_SOURCE_LOCATION (bad),
3349 " skips initialization of %q#D", bad);
3350 else
3351 inform (DECL_SOURCE_LOCATION (bad),
3352 " enters scope of %q#D which has "
3353 "non-trivial destructor", bad);
3357 if (complained)
3359 if (ent->in_try_scope)
3360 inform (input_location, " enters try block");
3361 else if (ent->in_catch_scope && !saw_catch)
3362 inform (input_location, " enters catch block");
3363 else if (ent->in_transaction_scope)
3364 inform (input_location, " enters synchronized or atomic statement");
3365 else if (ent->in_constexpr_if)
3366 inform (input_location, " enters constexpr if statement");
3369 if (ent->in_omp_scope)
3371 if (complained)
3372 inform (input_location, " enters OpenMP structured block");
3374 else if (flag_openmp)
3376 cp_binding_level *b;
3377 for (b = current_binding_level; b ; b = b->level_chain)
3379 if (b == ent->binding_level)
3380 break;
3381 if (b->kind == sk_omp)
3383 if (identified < 2)
3385 complained = identify_goto (decl,
3386 DECL_SOURCE_LOCATION (decl),
3387 &input_location, DK_ERROR);
3388 identified = 2;
3390 if (complained)
3391 inform (input_location, " exits OpenMP structured block");
3392 break;
3398 /* Check that a return is ok wrt OpenMP structured blocks.
3399 Called by finish_return_stmt. Returns true if all is well. */
3401 bool
3402 check_omp_return (void)
3404 cp_binding_level *b;
3405 for (b = current_binding_level; b ; b = b->level_chain)
3406 if (b->kind == sk_omp)
3408 error ("invalid exit from OpenMP structured block");
3409 return false;
3411 else if (b->kind == sk_function_parms)
3412 break;
3413 return true;
3416 /* Define a label, specifying the location in the source file.
3417 Return the LABEL_DECL node for the label. */
3419 static tree
3420 define_label_1 (location_t location, tree name)
3422 struct named_label_entry *ent, dummy;
3423 cp_binding_level *p;
3424 tree decl;
3426 decl = lookup_label (name);
3428 dummy.label_decl = decl;
3429 ent = named_labels->find (&dummy);
3430 gcc_assert (ent != NULL);
3432 /* After labels, make any new cleanups in the function go into their
3433 own new (temporary) binding contour. */
3434 for (p = current_binding_level;
3435 p->kind != sk_function_parms;
3436 p = p->level_chain)
3437 p->more_cleanups_ok = 0;
3439 if (name == get_identifier ("wchar_t"))
3440 permerror (input_location, "label named wchar_t");
3442 if (DECL_INITIAL (decl) != NULL_TREE)
3444 error ("duplicate label %qD", decl);
3445 return error_mark_node;
3447 else
3449 struct named_label_use_entry *use;
3451 /* Mark label as having been defined. */
3452 DECL_INITIAL (decl) = error_mark_node;
3453 /* Say where in the source. */
3454 DECL_SOURCE_LOCATION (decl) = location;
3456 ent->binding_level = current_binding_level;
3457 ent->names_in_scope = current_binding_level->names;
3459 for (use = ent->uses; use ; use = use->next)
3460 check_previous_goto (decl, use);
3461 ent->uses = NULL;
3464 return decl;
3467 /* Wrapper for define_label_1. */
3469 tree
3470 define_label (location_t location, tree name)
3472 tree ret;
3473 bool running = timevar_cond_start (TV_NAME_LOOKUP);
3474 ret = define_label_1 (location, name);
3475 timevar_cond_stop (TV_NAME_LOOKUP, running);
3476 return ret;
3480 struct cp_switch
3482 cp_binding_level *level;
3483 struct cp_switch *next;
3484 /* The SWITCH_STMT being built. */
3485 tree switch_stmt;
3486 /* A splay-tree mapping the low element of a case range to the high
3487 element, or NULL_TREE if there is no high element. Used to
3488 determine whether or not a new case label duplicates an old case
3489 label. We need a tree, rather than simply a hash table, because
3490 of the GNU case range extension. */
3491 splay_tree cases;
3492 /* Remember whether there was a case value that is outside the
3493 range of the original type of the controlling expression. */
3494 bool outside_range_p;
3497 /* A stack of the currently active switch statements. The innermost
3498 switch statement is on the top of the stack. There is no need to
3499 mark the stack for garbage collection because it is only active
3500 during the processing of the body of a function, and we never
3501 collect at that point. */
3503 static struct cp_switch *switch_stack;
3505 /* Called right after a switch-statement condition is parsed.
3506 SWITCH_STMT is the switch statement being parsed. */
3508 void
3509 push_switch (tree switch_stmt)
3511 struct cp_switch *p = XNEW (struct cp_switch);
3512 p->level = current_binding_level;
3513 p->next = switch_stack;
3514 p->switch_stmt = switch_stmt;
3515 p->cases = splay_tree_new (case_compare, NULL, NULL);
3516 p->outside_range_p = false;
3517 switch_stack = p;
3520 void
3521 pop_switch (void)
3523 struct cp_switch *cs = switch_stack;
3524 location_t switch_location;
3526 /* Emit warnings as needed. */
3527 switch_location = EXPR_LOC_OR_LOC (cs->switch_stmt, input_location);
3528 const bool bool_cond_p
3529 = (SWITCH_STMT_TYPE (cs->switch_stmt)
3530 && TREE_CODE (SWITCH_STMT_TYPE (cs->switch_stmt)) == BOOLEAN_TYPE);
3531 if (!processing_template_decl)
3532 c_do_switch_warnings (cs->cases, switch_location,
3533 SWITCH_STMT_TYPE (cs->switch_stmt),
3534 SWITCH_STMT_COND (cs->switch_stmt),
3535 bool_cond_p, cs->outside_range_p);
3537 splay_tree_delete (cs->cases);
3538 switch_stack = switch_stack->next;
3539 free (cs);
3542 /* Convert a case constant VALUE in a switch to the type TYPE of the switch
3543 condition. Note that if TYPE and VALUE are already integral we don't
3544 really do the conversion because the language-independent
3545 warning/optimization code will work better that way. */
3547 static tree
3548 case_conversion (tree type, tree value)
3550 if (value == NULL_TREE)
3551 return value;
3553 if (cxx_dialect >= cxx11
3554 && (SCOPED_ENUM_P (type)
3555 || !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (value))))
3557 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
3558 type = type_promotes_to (type);
3559 value = (perform_implicit_conversion_flags
3560 (type, value, tf_warning_or_error,
3561 LOOKUP_IMPLICIT | LOOKUP_NO_NON_INTEGRAL));
3563 return cxx_constant_value (value);
3566 /* Note that we've seen a definition of a case label, and complain if this
3567 is a bad place for one. */
3569 tree
3570 finish_case_label (location_t loc, tree low_value, tree high_value)
3572 tree cond, r;
3573 cp_binding_level *p;
3574 tree type;
3576 if (processing_template_decl)
3578 tree label;
3580 /* For templates, just add the case label; we'll do semantic
3581 analysis at instantiation-time. */
3582 label = build_decl (loc, LABEL_DECL, NULL_TREE, NULL_TREE);
3583 return add_stmt (build_case_label (low_value, high_value, label));
3586 /* Find the condition on which this switch statement depends. */
3587 cond = SWITCH_STMT_COND (switch_stack->switch_stmt);
3588 if (cond && TREE_CODE (cond) == TREE_LIST)
3589 cond = TREE_VALUE (cond);
3591 if (!check_switch_goto (switch_stack->level))
3592 return error_mark_node;
3594 type = SWITCH_STMT_TYPE (switch_stack->switch_stmt);
3596 low_value = case_conversion (type, low_value);
3597 high_value = case_conversion (type, high_value);
3599 r = c_add_case_label (loc, switch_stack->cases, cond, type,
3600 low_value, high_value,
3601 &switch_stack->outside_range_p);
3603 /* After labels, make any new cleanups in the function go into their
3604 own new (temporary) binding contour. */
3605 for (p = current_binding_level;
3606 p->kind != sk_function_parms;
3607 p = p->level_chain)
3608 p->more_cleanups_ok = 0;
3610 return r;
3613 struct typename_info {
3614 tree scope;
3615 tree name;
3616 tree template_id;
3617 bool enum_p;
3618 bool class_p;
3621 struct typename_hasher : ggc_ptr_hash<tree_node>
3623 typedef typename_info *compare_type;
3625 /* Hash a TYPENAME_TYPE. */
3627 static hashval_t
3628 hash (tree t)
3630 hashval_t hash;
3632 hash = (htab_hash_pointer (TYPE_CONTEXT (t))
3633 ^ htab_hash_pointer (TYPE_IDENTIFIER (t)));
3635 return hash;
3638 /* Compare two TYPENAME_TYPEs. */
3640 static bool
3641 equal (tree t1, const typename_info *t2)
3643 return (TYPE_IDENTIFIER (t1) == t2->name
3644 && TYPE_CONTEXT (t1) == t2->scope
3645 && TYPENAME_TYPE_FULLNAME (t1) == t2->template_id
3646 && TYPENAME_IS_ENUM_P (t1) == t2->enum_p
3647 && TYPENAME_IS_CLASS_P (t1) == t2->class_p);
3651 /* Build a TYPENAME_TYPE. If the type is `typename T::t', CONTEXT is
3652 the type of `T', NAME is the IDENTIFIER_NODE for `t'.
3654 Returns the new TYPENAME_TYPE. */
3656 static GTY (()) hash_table<typename_hasher> *typename_htab;
3658 static tree
3659 build_typename_type (tree context, tree name, tree fullname,
3660 enum tag_types tag_type)
3662 tree t;
3663 tree d;
3664 typename_info ti;
3665 tree *e;
3666 hashval_t hash;
3668 if (typename_htab == NULL)
3669 typename_htab = hash_table<typename_hasher>::create_ggc (61);
3671 ti.scope = FROB_CONTEXT (context);
3672 ti.name = name;
3673 ti.template_id = fullname;
3674 ti.enum_p = tag_type == enum_type;
3675 ti.class_p = (tag_type == class_type
3676 || tag_type == record_type
3677 || tag_type == union_type);
3678 hash = (htab_hash_pointer (ti.scope)
3679 ^ htab_hash_pointer (ti.name));
3681 /* See if we already have this type. */
3682 e = typename_htab->find_slot_with_hash (&ti, hash, INSERT);
3683 if (*e)
3684 t = *e;
3685 else
3687 /* Build the TYPENAME_TYPE. */
3688 t = cxx_make_type (TYPENAME_TYPE);
3689 TYPE_CONTEXT (t) = ti.scope;
3690 TYPENAME_TYPE_FULLNAME (t) = ti.template_id;
3691 TYPENAME_IS_ENUM_P (t) = ti.enum_p;
3692 TYPENAME_IS_CLASS_P (t) = ti.class_p;
3694 /* Build the corresponding TYPE_DECL. */
3695 d = build_decl (input_location, TYPE_DECL, name, t);
3696 TYPE_NAME (TREE_TYPE (d)) = d;
3697 TYPE_STUB_DECL (TREE_TYPE (d)) = d;
3698 DECL_CONTEXT (d) = FROB_CONTEXT (context);
3699 DECL_ARTIFICIAL (d) = 1;
3701 /* Store it in the hash table. */
3702 *e = t;
3704 /* TYPENAME_TYPEs must always be compared structurally, because
3705 they may or may not resolve down to another type depending on
3706 the currently open classes. */
3707 SET_TYPE_STRUCTURAL_EQUALITY (t);
3710 return t;
3713 /* Resolve `typename CONTEXT::NAME'. TAG_TYPE indicates the tag
3714 provided to name the type. Returns an appropriate type, unless an
3715 error occurs, in which case error_mark_node is returned. If we
3716 locate a non-artificial TYPE_DECL and TF_KEEP_TYPE_DECL is set, we
3717 return that, rather than the _TYPE it corresponds to, in other
3718 cases we look through the type decl. If TF_ERROR is set, complain
3719 about errors, otherwise be quiet. */
3721 tree
3722 make_typename_type (tree context, tree name, enum tag_types tag_type,
3723 tsubst_flags_t complain)
3725 tree fullname;
3726 tree t;
3727 bool want_template;
3729 if (name == error_mark_node
3730 || context == NULL_TREE
3731 || context == error_mark_node)
3732 return error_mark_node;
3734 if (TYPE_P (name))
3736 if (!(TYPE_LANG_SPECIFIC (name)
3737 && (CLASSTYPE_IS_TEMPLATE (name)
3738 || CLASSTYPE_USE_TEMPLATE (name))))
3739 name = TYPE_IDENTIFIER (name);
3740 else
3741 /* Create a TEMPLATE_ID_EXPR for the type. */
3742 name = build_nt (TEMPLATE_ID_EXPR,
3743 CLASSTYPE_TI_TEMPLATE (name),
3744 CLASSTYPE_TI_ARGS (name));
3746 else if (TREE_CODE (name) == TYPE_DECL)
3747 name = DECL_NAME (name);
3749 fullname = name;
3751 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3753 name = TREE_OPERAND (name, 0);
3754 if (DECL_TYPE_TEMPLATE_P (name))
3755 name = TREE_OPERAND (fullname, 0) = DECL_NAME (name);
3756 if (TREE_CODE (name) != IDENTIFIER_NODE)
3758 if (complain & tf_error)
3759 error ("%qD is not a type", name);
3760 return error_mark_node;
3763 if (TREE_CODE (name) == TEMPLATE_DECL)
3765 if (complain & tf_error)
3766 error ("%qD used without template parameters", name);
3767 return error_mark_node;
3769 gcc_assert (identifier_p (name));
3770 gcc_assert (TYPE_P (context));
3772 if (!MAYBE_CLASS_TYPE_P (context))
3774 if (complain & tf_error)
3775 error ("%q#T is not a class", context);
3776 return error_mark_node;
3779 /* When the CONTEXT is a dependent type, NAME could refer to a
3780 dependent base class of CONTEXT. But look inside it anyway
3781 if CONTEXT is a currently open scope, in case it refers to a
3782 member of the current instantiation or a non-dependent base;
3783 lookup will stop when we hit a dependent base. */
3784 if (!dependent_scope_p (context))
3785 /* We should only set WANT_TYPE when we're a nested typename type.
3786 Then we can give better diagnostics if we find a non-type. */
3787 t = lookup_field (context, name, 2, /*want_type=*/true);
3788 else
3789 t = NULL_TREE;
3791 if ((!t || TREE_CODE (t) == TREE_LIST) && dependent_type_p (context))
3792 return build_typename_type (context, name, fullname, tag_type);
3794 want_template = TREE_CODE (fullname) == TEMPLATE_ID_EXPR;
3796 if (!t)
3798 if (complain & tf_error)
3800 if (!COMPLETE_TYPE_P (context))
3801 cxx_incomplete_type_error (NULL_TREE, context);
3802 else
3803 error (want_template ? G_("no class template named %q#T in %q#T")
3804 : G_("no type named %q#T in %q#T"), name, context);
3806 return error_mark_node;
3809 /* Pull out the template from an injected-class-name (or multiple). */
3810 if (want_template)
3811 t = maybe_get_template_decl_from_type_decl (t);
3813 if (TREE_CODE (t) == TREE_LIST)
3815 if (complain & tf_error)
3817 error ("lookup of %qT in %qT is ambiguous", name, context);
3818 print_candidates (t);
3820 return error_mark_node;
3823 if (want_template && !DECL_TYPE_TEMPLATE_P (t))
3825 if (complain & tf_error)
3826 error ("%<typename %T::%D%> names %q#T, which is not a class template",
3827 context, name, t);
3828 return error_mark_node;
3830 if (!want_template && TREE_CODE (t) != TYPE_DECL)
3832 if (complain & tf_error)
3833 error ("%<typename %T::%D%> names %q#T, which is not a type",
3834 context, name, t);
3835 return error_mark_node;
3838 if (!perform_or_defer_access_check (TYPE_BINFO (context), t, t, complain))
3839 return error_mark_node;
3841 /* If we are currently parsing a template and if T is a typedef accessed
3842 through CONTEXT then we need to remember and check access of T at
3843 template instantiation time. */
3844 add_typedef_to_current_template_for_access_check (t, context, input_location);
3846 if (want_template)
3847 return lookup_template_class (t, TREE_OPERAND (fullname, 1),
3848 NULL_TREE, context,
3849 /*entering_scope=*/0,
3850 complain | tf_user);
3852 if (DECL_ARTIFICIAL (t) || !(complain & tf_keep_type_decl))
3853 t = TREE_TYPE (t);
3855 maybe_record_typedef_use (t);
3857 return t;
3860 /* Resolve `CONTEXT::template NAME'. Returns a TEMPLATE_DECL if the name
3861 can be resolved or an UNBOUND_CLASS_TEMPLATE, unless an error occurs,
3862 in which case error_mark_node is returned.
3864 If PARM_LIST is non-NULL, also make sure that the template parameter
3865 list of TEMPLATE_DECL matches.
3867 If COMPLAIN zero, don't complain about any errors that occur. */
3869 tree
3870 make_unbound_class_template (tree context, tree name, tree parm_list,
3871 tsubst_flags_t complain)
3873 tree t;
3874 tree d;
3876 if (TYPE_P (name))
3877 name = TYPE_IDENTIFIER (name);
3878 else if (DECL_P (name))
3879 name = DECL_NAME (name);
3880 gcc_assert (identifier_p (name));
3882 if (!dependent_type_p (context)
3883 || currently_open_class (context))
3885 tree tmpl = NULL_TREE;
3887 if (MAYBE_CLASS_TYPE_P (context))
3888 tmpl = lookup_field (context, name, 0, false);
3890 if (tmpl && TREE_CODE (tmpl) == TYPE_DECL)
3891 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
3893 if (!tmpl || !DECL_TYPE_TEMPLATE_P (tmpl))
3895 if (complain & tf_error)
3896 error ("no class template named %q#T in %q#T", name, context);
3897 return error_mark_node;
3900 if (parm_list
3901 && !comp_template_parms (DECL_TEMPLATE_PARMS (tmpl), parm_list))
3903 if (complain & tf_error)
3905 error ("template parameters do not match template %qD", tmpl);
3906 inform (DECL_SOURCE_LOCATION (tmpl),
3907 "%qD declared here", tmpl);
3909 return error_mark_node;
3912 if (!perform_or_defer_access_check (TYPE_BINFO (context), tmpl, tmpl,
3913 complain))
3914 return error_mark_node;
3916 return tmpl;
3919 /* Build the UNBOUND_CLASS_TEMPLATE. */
3920 t = cxx_make_type (UNBOUND_CLASS_TEMPLATE);
3921 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
3922 TREE_TYPE (t) = NULL_TREE;
3923 SET_TYPE_STRUCTURAL_EQUALITY (t);
3925 /* Build the corresponding TEMPLATE_DECL. */
3926 d = build_decl (input_location, TEMPLATE_DECL, name, t);
3927 TYPE_NAME (TREE_TYPE (d)) = d;
3928 TYPE_STUB_DECL (TREE_TYPE (d)) = d;
3929 DECL_CONTEXT (d) = FROB_CONTEXT (context);
3930 DECL_ARTIFICIAL (d) = 1;
3931 DECL_TEMPLATE_PARMS (d) = parm_list;
3933 return t;
3938 /* Push the declarations of builtin types into the namespace.
3939 RID_INDEX is the index of the builtin type in the array
3940 RID_POINTERS. NAME is the name used when looking up the builtin
3941 type. TYPE is the _TYPE node for the builtin type. */
3943 void
3944 record_builtin_type (enum rid rid_index,
3945 const char* name,
3946 tree type)
3948 tree rname = NULL_TREE, tname = NULL_TREE;
3949 tree tdecl = NULL_TREE;
3951 if ((int) rid_index < (int) RID_MAX)
3952 rname = ridpointers[(int) rid_index];
3953 if (name)
3954 tname = get_identifier (name);
3956 /* The calls to SET_IDENTIFIER_GLOBAL_VALUE below should be
3957 eliminated. Built-in types should not be looked up name; their
3958 names are keywords that the parser can recognize. However, there
3959 is code in c-common.c that uses identifier_global_value to look
3960 up built-in types by name. */
3961 if (tname)
3963 tdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, tname, type);
3964 DECL_ARTIFICIAL (tdecl) = 1;
3965 SET_IDENTIFIER_GLOBAL_VALUE (tname, tdecl);
3967 if (rname)
3969 if (!tdecl)
3971 tdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, rname, type);
3972 DECL_ARTIFICIAL (tdecl) = 1;
3974 SET_IDENTIFIER_GLOBAL_VALUE (rname, tdecl);
3977 if (!TYPE_NAME (type))
3978 TYPE_NAME (type) = tdecl;
3980 if (tdecl)
3981 debug_hooks->type_decl (tdecl, 0);
3984 /* Push a type into the namespace so that the back ends ignore it. */
3986 static void
3987 record_unknown_type (tree type, const char* name)
3989 tree decl = pushdecl (build_decl (UNKNOWN_LOCATION,
3990 TYPE_DECL, get_identifier (name), type));
3991 /* Make sure the "unknown type" typedecl gets ignored for debug info. */
3992 DECL_IGNORED_P (decl) = 1;
3993 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
3994 TYPE_SIZE (type) = TYPE_SIZE (void_type_node);
3995 SET_TYPE_ALIGN (type, 1);
3996 TYPE_USER_ALIGN (type) = 0;
3997 SET_TYPE_MODE (type, TYPE_MODE (void_type_node));
4000 /* A string for which we should create an IDENTIFIER_NODE at
4001 startup. */
4003 struct predefined_identifier
4005 /* The name of the identifier. */
4006 const char *const name;
4007 /* The place where the IDENTIFIER_NODE should be stored. */
4008 tree *const node;
4009 /* Nonzero if this is the name of a constructor or destructor. */
4010 const int ctor_or_dtor_p;
4013 /* Create all the predefined identifiers. */
4015 static void
4016 initialize_predefined_identifiers (void)
4018 const predefined_identifier *pid;
4020 /* A table of identifiers to create at startup. */
4021 static const predefined_identifier predefined_identifiers[] = {
4022 { "C++", &lang_name_cplusplus, 0 },
4023 { "C", &lang_name_c, 0 },
4024 /* Some of these names have a trailing space so that it is
4025 impossible for them to conflict with names written by users. */
4026 { "__ct ", &ctor_identifier, 1 },
4027 { "__base_ctor ", &base_ctor_identifier, 1 },
4028 { "__comp_ctor ", &complete_ctor_identifier, 1 },
4029 { "__dt ", &dtor_identifier, 1 },
4030 { "__comp_dtor ", &complete_dtor_identifier, 1 },
4031 { "__base_dtor ", &base_dtor_identifier, 1 },
4032 { "__deleting_dtor ", &deleting_dtor_identifier, 1 },
4033 { IN_CHARGE_NAME, &in_charge_identifier, 0 },
4034 { "nelts", &nelts_identifier, 0 },
4035 { THIS_NAME, &this_identifier, 0 },
4036 { VTABLE_DELTA_NAME, &delta_identifier, 0 },
4037 { VTABLE_PFN_NAME, &pfn_identifier, 0 },
4038 { "_vptr", &vptr_identifier, 0 },
4039 { "__vtt_parm", &vtt_parm_identifier, 0 },
4040 { "::", &global_scope_name, 0 },
4041 { "std", &std_identifier, 0 },
4042 { "auto", &auto_identifier, 0 },
4043 { "decltype(auto)", &decltype_auto_identifier, 0 },
4044 { NULL, NULL, 0 }
4047 for (pid = predefined_identifiers; pid->name; ++pid)
4049 *pid->node = get_identifier (pid->name);
4050 if (pid->ctor_or_dtor_p)
4051 IDENTIFIER_CTOR_OR_DTOR_P (*pid->node) = 1;
4055 /* Create the predefined scalar types of C,
4056 and some nodes representing standard constants (0, 1, (void *)0).
4057 Initialize the global binding level.
4058 Make definitions for built-in primitive functions. */
4060 void
4061 cxx_init_decl_processing (void)
4063 tree void_ftype;
4064 tree void_ftype_ptr;
4066 /* Create all the identifiers we need. */
4067 initialize_predefined_identifiers ();
4069 /* Create the global variables. */
4070 push_to_top_level ();
4072 current_function_decl = NULL_TREE;
4073 current_binding_level = NULL;
4074 /* Enter the global namespace. */
4075 gcc_assert (global_namespace == NULL_TREE);
4076 global_namespace = build_lang_decl (NAMESPACE_DECL, global_scope_name,
4077 void_type_node);
4078 DECL_CONTEXT (global_namespace)
4079 = build_translation_unit_decl (get_identifier (main_input_filename));
4080 debug_hooks->register_main_translation_unit
4081 (DECL_CONTEXT (global_namespace));
4082 TREE_PUBLIC (global_namespace) = 1;
4083 begin_scope (sk_namespace, global_namespace);
4085 if (flag_visibility_ms_compat)
4086 default_visibility = VISIBILITY_HIDDEN;
4088 /* Initially, C. */
4089 current_lang_name = lang_name_c;
4091 /* Create the `std' namespace. */
4092 push_namespace (std_identifier);
4093 std_node = current_namespace;
4094 pop_namespace ();
4096 flag_noexcept_type = (cxx_dialect >= cxx1z);
4098 c_common_nodes_and_builtins ();
4100 integer_two_node = build_int_cst (NULL_TREE, 2);
4102 record_builtin_type (RID_BOOL, "bool", boolean_type_node);
4103 truthvalue_type_node = boolean_type_node;
4104 truthvalue_false_node = boolean_false_node;
4105 truthvalue_true_node = boolean_true_node;
4107 empty_except_spec = build_tree_list (NULL_TREE, NULL_TREE);
4108 noexcept_true_spec = build_tree_list (boolean_true_node, NULL_TREE);
4109 noexcept_false_spec = build_tree_list (boolean_false_node, NULL_TREE);
4111 #if 0
4112 record_builtin_type (RID_MAX, NULL, string_type_node);
4113 #endif
4115 delta_type_node = ptrdiff_type_node;
4116 vtable_index_type = ptrdiff_type_node;
4118 vtt_parm_type = build_pointer_type (const_ptr_type_node);
4119 void_ftype = build_function_type_list (void_type_node, NULL_TREE);
4120 void_ftype_ptr = build_function_type_list (void_type_node,
4121 ptr_type_node, NULL_TREE);
4122 void_ftype_ptr
4123 = build_exception_variant (void_ftype_ptr, empty_except_spec);
4125 /* C++ extensions */
4127 unknown_type_node = make_node (LANG_TYPE);
4128 record_unknown_type (unknown_type_node, "unknown type");
4130 /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node. */
4131 TREE_TYPE (unknown_type_node) = unknown_type_node;
4133 /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same
4134 result. */
4135 TYPE_POINTER_TO (unknown_type_node) = unknown_type_node;
4136 TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node;
4138 init_list_type_node = make_node (LANG_TYPE);
4139 record_unknown_type (init_list_type_node, "init list");
4142 /* Make sure we get a unique function type, so we can give
4143 its pointer type a name. (This wins for gdb.) */
4144 tree vfunc_type = make_node (FUNCTION_TYPE);
4145 TREE_TYPE (vfunc_type) = integer_type_node;
4146 TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
4147 layout_type (vfunc_type);
4149 vtable_entry_type = build_pointer_type (vfunc_type);
4151 record_builtin_type (RID_MAX, VTBL_PTR_TYPE, vtable_entry_type);
4153 vtbl_type_node
4154 = build_cplus_array_type (vtable_entry_type, NULL_TREE);
4155 layout_type (vtbl_type_node);
4156 vtbl_type_node = cp_build_qualified_type (vtbl_type_node, TYPE_QUAL_CONST);
4157 record_builtin_type (RID_MAX, NULL, vtbl_type_node);
4158 vtbl_ptr_type_node = build_pointer_type (vtable_entry_type);
4159 layout_type (vtbl_ptr_type_node);
4160 record_builtin_type (RID_MAX, NULL, vtbl_ptr_type_node);
4162 push_namespace (get_identifier ("__cxxabiv1"));
4163 abi_node = current_namespace;
4164 pop_namespace ();
4166 global_type_node = make_node (LANG_TYPE);
4167 record_unknown_type (global_type_node, "global type");
4169 any_targ_node = make_node (LANG_TYPE);
4170 record_unknown_type (any_targ_node, "any type");
4172 /* Now, C++. */
4173 current_lang_name = lang_name_cplusplus;
4175 if (aligned_new_threshold > 1
4176 && !pow2p_hwi (aligned_new_threshold))
4178 error ("-faligned-new=%d is not a power of two", aligned_new_threshold);
4179 aligned_new_threshold = 1;
4181 if (aligned_new_threshold == -1)
4182 aligned_new_threshold = (cxx_dialect >= cxx1z) ? 1 : 0;
4183 if (aligned_new_threshold == 1)
4184 aligned_new_threshold = malloc_alignment () / BITS_PER_UNIT;
4187 tree newattrs, extvisattr;
4188 tree newtype, deltype;
4189 tree ptr_ftype_sizetype;
4190 tree new_eh_spec;
4192 ptr_ftype_sizetype
4193 = build_function_type_list (ptr_type_node, size_type_node, NULL_TREE);
4194 if (cxx_dialect == cxx98)
4196 tree bad_alloc_id;
4197 tree bad_alloc_type_node;
4198 tree bad_alloc_decl;
4200 push_namespace (std_identifier);
4201 bad_alloc_id = get_identifier ("bad_alloc");
4202 bad_alloc_type_node = make_class_type (RECORD_TYPE);
4203 TYPE_CONTEXT (bad_alloc_type_node) = current_namespace;
4204 bad_alloc_decl
4205 = create_implicit_typedef (bad_alloc_id, bad_alloc_type_node);
4206 DECL_CONTEXT (bad_alloc_decl) = current_namespace;
4207 pop_namespace ();
4209 new_eh_spec
4210 = add_exception_specifier (NULL_TREE, bad_alloc_type_node, -1);
4212 else
4213 new_eh_spec = noexcept_false_spec;
4215 /* Ensure attribs.c is initialized. */
4216 init_attributes ();
4218 /* Ensure constraint.cc is initialized. */
4219 init_constraint_processing ();
4221 extvisattr = build_tree_list (get_identifier ("externally_visible"),
4222 NULL_TREE);
4223 newattrs = tree_cons (get_identifier ("alloc_size"),
4224 build_tree_list (NULL_TREE, integer_one_node),
4225 extvisattr);
4226 newtype = cp_build_type_attribute_variant (ptr_ftype_sizetype, newattrs);
4227 newtype = build_exception_variant (newtype, new_eh_spec);
4228 deltype = cp_build_type_attribute_variant (void_ftype_ptr, extvisattr);
4229 deltype = build_exception_variant (deltype, empty_except_spec);
4230 tree opnew = push_cp_library_fn (NEW_EXPR, newtype, 0);
4231 DECL_IS_MALLOC (opnew) = 1;
4232 DECL_IS_OPERATOR_NEW (opnew) = 1;
4233 opnew = push_cp_library_fn (VEC_NEW_EXPR, newtype, 0);
4234 DECL_IS_MALLOC (opnew) = 1;
4235 DECL_IS_OPERATOR_NEW (opnew) = 1;
4236 push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
4237 push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
4238 if (flag_sized_deallocation)
4240 /* Also push the sized deallocation variants:
4241 void operator delete(void*, std::size_t) throw();
4242 void operator delete[](void*, std::size_t) throw(); */
4243 tree void_ftype_ptr_size
4244 = build_function_type_list (void_type_node, ptr_type_node,
4245 size_type_node, NULL_TREE);
4246 deltype = cp_build_type_attribute_variant (void_ftype_ptr_size,
4247 extvisattr);
4248 deltype = build_exception_variant (deltype, empty_except_spec);
4249 push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
4250 push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
4253 if (aligned_new_threshold)
4255 push_namespace (std_identifier);
4256 tree align_id = get_identifier ("align_val_t");
4257 align_type_node = start_enum (align_id, NULL_TREE, size_type_node,
4258 NULL_TREE, /*scoped*/true, NULL);
4259 pop_namespace ();
4261 /* operator new (size_t, align_val_t); */
4262 newtype = build_function_type_list (ptr_type_node, size_type_node,
4263 align_type_node, NULL_TREE);
4264 newtype = cp_build_type_attribute_variant (newtype, newattrs);
4265 newtype = build_exception_variant (newtype, new_eh_spec);
4266 opnew = push_cp_library_fn (NEW_EXPR, newtype, 0);
4267 DECL_IS_MALLOC (opnew) = 1;
4268 DECL_IS_OPERATOR_NEW (opnew) = 1;
4269 opnew = push_cp_library_fn (VEC_NEW_EXPR, newtype, 0);
4270 DECL_IS_MALLOC (opnew) = 1;
4271 DECL_IS_OPERATOR_NEW (opnew) = 1;
4273 /* operator delete (void *, align_val_t); */
4274 deltype = build_function_type_list (void_type_node, ptr_type_node,
4275 align_type_node, NULL_TREE);
4276 deltype = cp_build_type_attribute_variant (deltype, extvisattr);
4277 deltype = build_exception_variant (deltype, empty_except_spec);
4278 push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
4279 push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
4281 if (flag_sized_deallocation)
4283 /* operator delete (void *, size_t, align_val_t); */
4284 deltype = build_function_type_list (void_type_node, ptr_type_node,
4285 size_type_node, align_type_node,
4286 NULL_TREE);
4287 deltype = cp_build_type_attribute_variant (deltype, extvisattr);
4288 deltype = build_exception_variant (deltype, empty_except_spec);
4289 push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
4290 push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
4294 nullptr_type_node = make_node (NULLPTR_TYPE);
4295 TYPE_SIZE (nullptr_type_node) = bitsize_int (GET_MODE_BITSIZE (ptr_mode));
4296 TYPE_SIZE_UNIT (nullptr_type_node) = size_int (GET_MODE_SIZE (ptr_mode));
4297 TYPE_UNSIGNED (nullptr_type_node) = 1;
4298 TYPE_PRECISION (nullptr_type_node) = GET_MODE_BITSIZE (ptr_mode);
4299 if (abi_version_at_least (9))
4300 SET_TYPE_ALIGN (nullptr_type_node, GET_MODE_ALIGNMENT (ptr_mode));
4301 SET_TYPE_MODE (nullptr_type_node, ptr_mode);
4302 record_builtin_type (RID_MAX, "decltype(nullptr)", nullptr_type_node);
4303 nullptr_node = build_int_cst (nullptr_type_node, 0);
4306 abort_fndecl
4307 = build_library_fn_ptr ("__cxa_pure_virtual", void_ftype,
4308 ECF_NORETURN | ECF_NOTHROW);
4310 /* Perform other language dependent initializations. */
4311 init_class_processing ();
4312 init_rtti_processing ();
4313 init_template_processing ();
4315 if (flag_exceptions)
4316 init_exception_processing ();
4318 if (! supports_one_only ())
4319 flag_weak = 0;
4321 make_fname_decl = cp_make_fname_decl;
4322 start_fname_decls ();
4324 /* Show we use EH for cleanups. */
4325 if (flag_exceptions)
4326 using_eh_for_cleanups ();
4329 /* Generate an initializer for a function naming variable from
4330 NAME. NAME may be NULL, to indicate a dependent name. TYPE_P is
4331 filled in with the type of the init. */
4333 tree
4334 cp_fname_init (const char* name, tree *type_p)
4336 tree domain = NULL_TREE;
4337 tree type;
4338 tree init = NULL_TREE;
4339 size_t length = 0;
4341 if (name)
4343 length = strlen (name);
4344 domain = build_index_type (size_int (length));
4345 init = build_string (length + 1, name);
4348 type = cp_build_qualified_type (char_type_node, TYPE_QUAL_CONST);
4349 type = build_cplus_array_type (type, domain);
4351 *type_p = type;
4353 if (init)
4354 TREE_TYPE (init) = type;
4355 else
4356 init = error_mark_node;
4358 return init;
4361 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give
4362 the decl, LOC is the location to give the decl, NAME is the
4363 initialization string and TYPE_DEP indicates whether NAME depended
4364 on the type of the function. We make use of that to detect
4365 __PRETTY_FUNCTION__ inside a template fn. This is being done lazily
4366 at the point of first use, so we mustn't push the decl now. */
4368 static tree
4369 cp_make_fname_decl (location_t loc, tree id, int type_dep)
4371 const char *const name = (type_dep && processing_template_decl
4372 ? NULL : fname_as_string (type_dep));
4373 tree type;
4374 tree init = cp_fname_init (name, &type);
4375 tree decl = build_decl (loc, VAR_DECL, id, type);
4377 if (name)
4378 free (CONST_CAST (char *, name));
4380 /* As we're using pushdecl_with_scope, we must set the context. */
4381 DECL_CONTEXT (decl) = current_function_decl;
4383 TREE_STATIC (decl) = 1;
4384 TREE_READONLY (decl) = 1;
4385 DECL_ARTIFICIAL (decl) = 1;
4387 TREE_USED (decl) = 1;
4389 if (current_function_decl)
4391 cp_binding_level *b = current_binding_level;
4392 if (b->kind == sk_function_parms)
4393 return error_mark_node;
4394 while (b->level_chain->kind != sk_function_parms)
4395 b = b->level_chain;
4396 pushdecl_with_scope (decl, b, /*is_friend=*/false);
4397 cp_finish_decl (decl, init, /*init_const_expr_p=*/false, NULL_TREE,
4398 LOOKUP_ONLYCONVERTING);
4400 else
4402 DECL_THIS_STATIC (decl) = true;
4403 pushdecl_top_level_and_finish (decl, init);
4406 return decl;
4409 static tree
4410 builtin_function_1 (tree decl, tree context, bool is_global)
4412 tree id = DECL_NAME (decl);
4413 const char *name = IDENTIFIER_POINTER (id);
4415 retrofit_lang_decl (decl);
4417 DECL_ARTIFICIAL (decl) = 1;
4418 SET_OVERLOADED_OPERATOR_CODE (decl, ERROR_MARK);
4419 SET_DECL_LANGUAGE (decl, lang_c);
4420 /* Runtime library routines are, by definition, available in an
4421 external shared object. */
4422 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
4423 DECL_VISIBILITY_SPECIFIED (decl) = 1;
4425 DECL_CONTEXT (decl) = context;
4427 if (is_global)
4428 pushdecl_top_level (decl);
4429 else
4430 pushdecl (decl);
4432 /* A function in the user's namespace should have an explicit
4433 declaration before it is used. Mark the built-in function as
4434 anticipated but not actually declared. */
4435 if (name[0] != '_' || name[1] != '_')
4436 DECL_ANTICIPATED (decl) = 1;
4437 else if (strncmp (name + 2, "builtin_", strlen ("builtin_")) != 0)
4439 size_t len = strlen (name);
4441 /* Treat __*_chk fortification functions as anticipated as well,
4442 unless they are __builtin_*. */
4443 if (len > strlen ("___chk")
4444 && memcmp (name + len - strlen ("_chk"),
4445 "_chk", strlen ("_chk") + 1) == 0)
4446 DECL_ANTICIPATED (decl) = 1;
4449 return decl;
4452 tree
4453 cxx_builtin_function (tree decl)
4455 tree id = DECL_NAME (decl);
4456 const char *name = IDENTIFIER_POINTER (id);
4457 /* All builtins that don't begin with an '_' should additionally
4458 go in the 'std' namespace. */
4459 if (name[0] != '_')
4461 tree decl2 = copy_node(decl);
4462 push_namespace (std_identifier);
4463 builtin_function_1 (decl2, std_node, false);
4464 pop_namespace ();
4467 return builtin_function_1 (decl, NULL_TREE, false);
4470 /* Like cxx_builtin_function, but guarantee the function is added to the global
4471 scope. This is to allow function specific options to add new machine
4472 dependent builtins when the target ISA changes via attribute((target(...)))
4473 which saves space on program startup if the program does not use non-generic
4474 ISAs. */
4476 tree
4477 cxx_builtin_function_ext_scope (tree decl)
4480 tree id = DECL_NAME (decl);
4481 const char *name = IDENTIFIER_POINTER (id);
4482 /* All builtins that don't begin with an '_' should additionally
4483 go in the 'std' namespace. */
4484 if (name[0] != '_')
4486 tree decl2 = copy_node(decl);
4487 push_namespace (std_identifier);
4488 builtin_function_1 (decl2, std_node, true);
4489 pop_namespace ();
4492 return builtin_function_1 (decl, NULL_TREE, true);
4495 /* Generate a FUNCTION_DECL with the typical flags for a runtime library
4496 function. Not called directly. */
4498 static tree
4499 build_library_fn (tree name, enum tree_code operator_code, tree type,
4500 int ecf_flags)
4502 tree fn = build_lang_decl (FUNCTION_DECL, name, type);
4503 DECL_EXTERNAL (fn) = 1;
4504 TREE_PUBLIC (fn) = 1;
4505 DECL_ARTIFICIAL (fn) = 1;
4506 SET_OVERLOADED_OPERATOR_CODE (fn, operator_code);
4507 SET_DECL_LANGUAGE (fn, lang_c);
4508 /* Runtime library routines are, by definition, available in an
4509 external shared object. */
4510 DECL_VISIBILITY (fn) = VISIBILITY_DEFAULT;
4511 DECL_VISIBILITY_SPECIFIED (fn) = 1;
4512 set_call_expr_flags (fn, ecf_flags);
4513 return fn;
4516 /* Returns the _DECL for a library function with C++ linkage. */
4518 static tree
4519 build_cp_library_fn (tree name, enum tree_code operator_code, tree type,
4520 int ecf_flags)
4522 tree fn = build_library_fn (name, operator_code, type, ecf_flags);
4523 DECL_CONTEXT (fn) = FROB_CONTEXT (current_namespace);
4524 SET_DECL_LANGUAGE (fn, lang_cplusplus);
4525 return fn;
4528 /* Like build_library_fn, but takes a C string instead of an
4529 IDENTIFIER_NODE. */
4531 tree
4532 build_library_fn_ptr (const char* name, tree type, int ecf_flags)
4534 return build_library_fn (get_identifier (name), ERROR_MARK, type, ecf_flags);
4537 /* Like build_cp_library_fn, but takes a C string instead of an
4538 IDENTIFIER_NODE. */
4540 tree
4541 build_cp_library_fn_ptr (const char* name, tree type, int ecf_flags)
4543 return build_cp_library_fn (get_identifier (name), ERROR_MARK, type,
4544 ecf_flags);
4547 /* Like build_library_fn, but also pushes the function so that we will
4548 be able to find it via IDENTIFIER_GLOBAL_VALUE. Also, the function
4549 may throw exceptions listed in RAISES. */
4551 tree
4552 push_library_fn (tree name, tree type, tree raises, int ecf_flags)
4554 tree fn;
4556 if (raises)
4557 type = build_exception_variant (type, raises);
4559 fn = build_library_fn (name, ERROR_MARK, type, ecf_flags);
4560 pushdecl_top_level (fn);
4561 return fn;
4564 /* Like build_cp_library_fn, but also pushes the function so that it
4565 will be found by normal lookup. */
4567 static tree
4568 push_cp_library_fn (enum tree_code operator_code, tree type,
4569 int ecf_flags)
4571 tree fn = build_cp_library_fn (cp_operator_id (operator_code),
4572 operator_code,
4573 type, ecf_flags);
4574 pushdecl (fn);
4575 if (flag_tm)
4576 apply_tm_attr (fn, get_identifier ("transaction_safe"));
4577 return fn;
4580 /* Like push_library_fn, but takes a TREE_LIST of parm types rather than
4581 a FUNCTION_TYPE. */
4583 tree
4584 push_void_library_fn (tree name, tree parmtypes, int ecf_flags)
4586 tree type = build_function_type (void_type_node, parmtypes);
4587 return push_library_fn (name, type, NULL_TREE, ecf_flags);
4590 /* Like push_library_fn, but also note that this function throws
4591 and does not return. Used for __throw_foo and the like. */
4593 tree
4594 push_throw_library_fn (tree name, tree type)
4596 tree fn = push_library_fn (name, type, NULL_TREE, ECF_NORETURN);
4597 return fn;
4600 /* When we call finish_struct for an anonymous union, we create
4601 default copy constructors and such. But, an anonymous union
4602 shouldn't have such things; this function undoes the damage to the
4603 anonymous union type T.
4605 (The reason that we create the synthesized methods is that we don't
4606 distinguish `union { int i; }' from `typedef union { int i; } U'.
4607 The first is an anonymous union; the second is just an ordinary
4608 union type.) */
4610 void
4611 fixup_anonymous_aggr (tree t)
4613 tree *q;
4615 /* Wipe out memory of synthesized methods. */
4616 TYPE_HAS_USER_CONSTRUCTOR (t) = 0;
4617 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0;
4618 TYPE_HAS_COPY_CTOR (t) = 0;
4619 TYPE_HAS_CONST_COPY_CTOR (t) = 0;
4620 TYPE_HAS_COPY_ASSIGN (t) = 0;
4621 TYPE_HAS_CONST_COPY_ASSIGN (t) = 0;
4623 /* Splice the implicitly generated functions out of the TYPE_METHODS
4624 list. */
4625 q = &TYPE_METHODS (t);
4626 while (*q)
4628 if (DECL_ARTIFICIAL (*q))
4629 *q = TREE_CHAIN (*q);
4630 else
4631 q = &DECL_CHAIN (*q);
4634 /* ISO C++ 9.5.3. Anonymous unions may not have function members. */
4635 if (TYPE_METHODS (t))
4637 tree decl = TYPE_MAIN_DECL (t);
4639 if (TREE_CODE (t) != UNION_TYPE)
4640 error_at (DECL_SOURCE_LOCATION (decl),
4641 "an anonymous struct cannot have function members");
4642 else
4643 error_at (DECL_SOURCE_LOCATION (decl),
4644 "an anonymous union cannot have function members");
4647 /* Anonymous aggregates cannot have fields with ctors, dtors or complex
4648 assignment operators (because they cannot have these methods themselves).
4649 For anonymous unions this is already checked because they are not allowed
4650 in any union, otherwise we have to check it. */
4651 if (TREE_CODE (t) != UNION_TYPE)
4653 tree field, type;
4655 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
4656 if (TREE_CODE (field) == FIELD_DECL)
4658 type = TREE_TYPE (field);
4659 if (CLASS_TYPE_P (type))
4661 if (TYPE_NEEDS_CONSTRUCTING (type))
4662 error ("member %q+#D with constructor not allowed "
4663 "in anonymous aggregate", field);
4664 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
4665 error ("member %q+#D with destructor not allowed "
4666 "in anonymous aggregate", field);
4667 if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type))
4668 error ("member %q+#D with copy assignment operator "
4669 "not allowed in anonymous aggregate", field);
4675 /* Warn for an attribute located at LOCATION that appertains to the
4676 class type CLASS_TYPE that has not been properly placed after its
4677 class-key, in it class-specifier. */
4679 void
4680 warn_misplaced_attr_for_class_type (source_location location,
4681 tree class_type)
4683 gcc_assert (OVERLOAD_TYPE_P (class_type));
4685 if (warning_at (location, OPT_Wattributes,
4686 "attribute ignored in declaration "
4687 "of %q#T", class_type))
4688 inform (location,
4689 "attribute for %q#T must follow the %qs keyword",
4690 class_type, class_key_or_enum_as_string (class_type));
4693 /* Make sure that a declaration with no declarator is well-formed, i.e.
4694 just declares a tagged type or anonymous union.
4696 Returns the type declared; or NULL_TREE if none. */
4698 tree
4699 check_tag_decl (cp_decl_specifier_seq *declspecs,
4700 bool explicit_type_instantiation_p)
4702 int saw_friend = decl_spec_seq_has_spec_p (declspecs, ds_friend);
4703 int saw_typedef = decl_spec_seq_has_spec_p (declspecs, ds_typedef);
4704 /* If a class, struct, or enum type is declared by the DECLSPECS
4705 (i.e, if a class-specifier, enum-specifier, or non-typename
4706 elaborated-type-specifier appears in the DECLSPECS),
4707 DECLARED_TYPE is set to the corresponding type. */
4708 tree declared_type = NULL_TREE;
4709 bool error_p = false;
4711 if (declspecs->multiple_types_p)
4712 error ("multiple types in one declaration");
4713 else if (declspecs->redefined_builtin_type)
4715 if (!in_system_header_at (input_location))
4716 permerror (declspecs->locations[ds_redefined_builtin_type_spec],
4717 "redeclaration of C++ built-in type %qT",
4718 declspecs->redefined_builtin_type);
4719 return NULL_TREE;
4722 if (declspecs->type
4723 && TYPE_P (declspecs->type)
4724 && ((TREE_CODE (declspecs->type) != TYPENAME_TYPE
4725 && MAYBE_CLASS_TYPE_P (declspecs->type))
4726 || TREE_CODE (declspecs->type) == ENUMERAL_TYPE))
4727 declared_type = declspecs->type;
4728 else if (declspecs->type == error_mark_node)
4729 error_p = true;
4730 if (declared_type == NULL_TREE && ! saw_friend && !error_p)
4731 permerror (input_location, "declaration does not declare anything");
4732 else if (declared_type != NULL_TREE && type_uses_auto (declared_type))
4734 error_at (declspecs->locations[ds_type_spec],
4735 "%<auto%> can only be specified for variables "
4736 "or function declarations");
4737 return error_mark_node;
4739 /* Check for an anonymous union. */
4740 else if (declared_type && RECORD_OR_UNION_CODE_P (TREE_CODE (declared_type))
4741 && TYPE_UNNAMED_P (declared_type))
4743 /* 7/3 In a simple-declaration, the optional init-declarator-list
4744 can be omitted only when declaring a class (clause 9) or
4745 enumeration (7.2), that is, when the decl-specifier-seq contains
4746 either a class-specifier, an elaborated-type-specifier with
4747 a class-key (9.1), or an enum-specifier. In these cases and
4748 whenever a class-specifier or enum-specifier is present in the
4749 decl-specifier-seq, the identifiers in these specifiers are among
4750 the names being declared by the declaration (as class-name,
4751 enum-names, or enumerators, depending on the syntax). In such
4752 cases, and except for the declaration of an unnamed bit-field (9.6),
4753 the decl-specifier-seq shall introduce one or more names into the
4754 program, or shall redeclare a name introduced by a previous
4755 declaration. [Example:
4756 enum { }; // ill-formed
4757 typedef class { }; // ill-formed
4758 --end example] */
4759 if (saw_typedef)
4761 error ("missing type-name in typedef-declaration");
4762 return NULL_TREE;
4764 /* Anonymous unions are objects, so they can have specifiers. */;
4765 SET_ANON_AGGR_TYPE_P (declared_type);
4767 if (TREE_CODE (declared_type) != UNION_TYPE
4768 && !in_system_header_at (input_location))
4769 pedwarn (input_location, OPT_Wpedantic, "ISO C++ prohibits anonymous structs");
4772 else
4774 if (decl_spec_seq_has_spec_p (declspecs, ds_inline))
4775 error_at (declspecs->locations[ds_inline],
4776 "%<inline%> can only be specified for functions");
4777 else if (decl_spec_seq_has_spec_p (declspecs, ds_virtual))
4778 error_at (declspecs->locations[ds_virtual],
4779 "%<virtual%> can only be specified for functions");
4780 else if (saw_friend
4781 && (!current_class_type
4782 || current_scope () != current_class_type))
4783 error_at (declspecs->locations[ds_friend],
4784 "%<friend%> can only be specified inside a class");
4785 else if (decl_spec_seq_has_spec_p (declspecs, ds_explicit))
4786 error_at (declspecs->locations[ds_explicit],
4787 "%<explicit%> can only be specified for constructors");
4788 else if (declspecs->storage_class)
4789 error_at (declspecs->locations[ds_storage_class],
4790 "a storage class can only be specified for objects "
4791 "and functions");
4792 else if (decl_spec_seq_has_spec_p (declspecs, ds_const))
4793 error_at (declspecs->locations[ds_const],
4794 "%<const%> can only be specified for objects and "
4795 "functions");
4796 else if (decl_spec_seq_has_spec_p (declspecs, ds_volatile))
4797 error_at (declspecs->locations[ds_volatile],
4798 "%<volatile%> can only be specified for objects and "
4799 "functions");
4800 else if (decl_spec_seq_has_spec_p (declspecs, ds_restrict))
4801 error_at (declspecs->locations[ds_restrict],
4802 "%<__restrict%> can only be specified for objects and "
4803 "functions");
4804 else if (decl_spec_seq_has_spec_p (declspecs, ds_thread))
4805 error_at (declspecs->locations[ds_thread],
4806 "%<__thread%> can only be specified for objects "
4807 "and functions");
4808 else if (saw_typedef)
4809 warning_at (declspecs->locations[ds_typedef], 0,
4810 "%<typedef%> was ignored in this declaration");
4811 else if (decl_spec_seq_has_spec_p (declspecs, ds_constexpr))
4812 error_at (declspecs->locations[ds_constexpr],
4813 "%<constexpr%> cannot be used for type declarations");
4816 if (declspecs->attributes && warn_attributes && declared_type)
4818 location_t loc;
4819 if (!CLASS_TYPE_P (declared_type)
4820 || !CLASSTYPE_TEMPLATE_INSTANTIATION (declared_type))
4821 /* For a non-template class, use the name location. */
4822 loc = location_of (declared_type);
4823 else
4824 /* For a template class (an explicit instantiation), use the
4825 current location. */
4826 loc = input_location;
4828 if (explicit_type_instantiation_p)
4829 /* [dcl.attr.grammar]/4:
4831 No attribute-specifier-seq shall appertain to an explicit
4832 instantiation. */
4834 if (warning_at (loc, OPT_Wattributes,
4835 "attribute ignored in explicit instantiation %q#T",
4836 declared_type))
4837 inform (loc,
4838 "no attribute can be applied to "
4839 "an explicit instantiation");
4841 else
4842 warn_misplaced_attr_for_class_type (loc, declared_type);
4845 return declared_type;
4848 /* Called when a declaration is seen that contains no names to declare.
4849 If its type is a reference to a structure, union or enum inherited
4850 from a containing scope, shadow that tag name for the current scope
4851 with a forward reference.
4852 If its type defines a new named structure or union
4853 or defines an enum, it is valid but we need not do anything here.
4854 Otherwise, it is an error.
4856 C++: may have to grok the declspecs to learn about static,
4857 complain for anonymous unions.
4859 Returns the TYPE declared -- or NULL_TREE if none. */
4861 tree
4862 shadow_tag (cp_decl_specifier_seq *declspecs)
4864 tree t = check_tag_decl (declspecs,
4865 /*explicit_type_instantiation_p=*/false);
4867 if (!t)
4868 return NULL_TREE;
4870 if (maybe_process_partial_specialization (t) == error_mark_node)
4871 return NULL_TREE;
4873 /* This is where the variables in an anonymous union are
4874 declared. An anonymous union declaration looks like:
4875 union { ... } ;
4876 because there is no declarator after the union, the parser
4877 sends that declaration here. */
4878 if (ANON_AGGR_TYPE_P (t))
4880 fixup_anonymous_aggr (t);
4882 if (TYPE_FIELDS (t))
4884 tree decl = grokdeclarator (/*declarator=*/NULL,
4885 declspecs, NORMAL, 0, NULL);
4886 finish_anon_union (decl);
4890 return t;
4893 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
4895 tree
4896 groktypename (cp_decl_specifier_seq *type_specifiers,
4897 const cp_declarator *declarator,
4898 bool is_template_arg)
4900 tree attrs;
4901 tree type;
4902 enum decl_context context
4903 = is_template_arg ? TEMPLATE_TYPE_ARG : TYPENAME;
4904 attrs = type_specifiers->attributes;
4905 type_specifiers->attributes = NULL_TREE;
4906 type = grokdeclarator (declarator, type_specifiers, context, 0, &attrs);
4907 if (attrs && type != error_mark_node)
4909 if (CLASS_TYPE_P (type))
4910 warning (OPT_Wattributes, "ignoring attributes applied to class type %qT "
4911 "outside of definition", type);
4912 else if (MAYBE_CLASS_TYPE_P (type))
4913 /* A template type parameter or other dependent type. */
4914 warning (OPT_Wattributes, "ignoring attributes applied to dependent "
4915 "type %qT without an associated declaration", type);
4916 else
4917 cplus_decl_attributes (&type, attrs, 0);
4919 return type;
4922 /* Process a DECLARATOR for a function-scope variable declaration,
4923 namespace-scope variable declaration, or function declaration.
4924 (Function definitions go through start_function; class member
4925 declarations appearing in the body of the class go through
4926 grokfield.) The DECL corresponding to the DECLARATOR is returned.
4927 If an error occurs, the error_mark_node is returned instead.
4929 DECLSPECS are the decl-specifiers for the declaration. INITIALIZED is
4930 SD_INITIALIZED if an explicit initializer is present, or SD_DEFAULTED
4931 for an explicitly defaulted function, or SD_DELETED for an explicitly
4932 deleted function, but 0 (SD_UNINITIALIZED) if this is a variable
4933 implicitly initialized via a default constructor. ATTRIBUTES and
4934 PREFIX_ATTRIBUTES are GNU attributes associated with this declaration.
4936 The scope represented by the context of the returned DECL is pushed
4937 (if it is not the global namespace) and is assigned to
4938 *PUSHED_SCOPE_P. The caller is then responsible for calling
4939 pop_scope on *PUSHED_SCOPE_P if it is set. */
4941 tree
4942 start_decl (const cp_declarator *declarator,
4943 cp_decl_specifier_seq *declspecs,
4944 int initialized,
4945 tree attributes,
4946 tree prefix_attributes,
4947 tree *pushed_scope_p)
4949 tree decl;
4950 tree context;
4951 bool was_public;
4952 int flags;
4953 bool alias;
4955 *pushed_scope_p = NULL_TREE;
4957 /* An object declared as __attribute__((deprecated)) suppresses
4958 warnings of uses of other deprecated items. */
4959 if (lookup_attribute ("deprecated", attributes))
4960 deprecated_state = DEPRECATED_SUPPRESS;
4962 attributes = chainon (attributes, prefix_attributes);
4964 decl = grokdeclarator (declarator, declspecs, NORMAL, initialized,
4965 &attributes);
4967 deprecated_state = DEPRECATED_NORMAL;
4969 if (decl == NULL_TREE || VOID_TYPE_P (decl)
4970 || decl == error_mark_node)
4971 return error_mark_node;
4973 context = CP_DECL_CONTEXT (decl);
4974 if (context != global_namespace)
4975 *pushed_scope_p = push_scope (context);
4977 /* Is it valid for this decl to have an initializer at all?
4978 If not, set INITIALIZED to zero, which will indirectly
4979 tell `cp_finish_decl' to ignore the initializer once it is parsed. */
4980 if (initialized
4981 && TREE_CODE (decl) == TYPE_DECL)
4983 error ("typedef %qD is initialized (use decltype instead)", decl);
4984 return error_mark_node;
4987 if (initialized)
4989 if (! toplevel_bindings_p ()
4990 && DECL_EXTERNAL (decl))
4991 warning (0, "declaration of %q#D has %<extern%> and is initialized",
4992 decl);
4993 DECL_EXTERNAL (decl) = 0;
4994 if (toplevel_bindings_p ())
4995 TREE_STATIC (decl) = 1;
4997 alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl)) != 0;
4999 if (alias && TREE_CODE (decl) == FUNCTION_DECL)
5000 record_key_method_defined (decl);
5002 /* If this is a typedef that names the class for linkage purposes
5003 (7.1.3p8), apply any attributes directly to the type. */
5004 if (TREE_CODE (decl) == TYPE_DECL
5005 && OVERLOAD_TYPE_P (TREE_TYPE (decl))
5006 && decl == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (decl))))
5007 flags = ATTR_FLAG_TYPE_IN_PLACE;
5008 else
5009 flags = 0;
5011 /* Set attributes here so if duplicate decl, will have proper attributes. */
5012 cplus_decl_attributes (&decl, attributes, flags);
5014 /* Dllimported symbols cannot be defined. Static data members (which
5015 can be initialized in-class and dllimported) go through grokfield,
5016 not here, so we don't need to exclude those decls when checking for
5017 a definition. */
5018 if (initialized && DECL_DLLIMPORT_P (decl))
5020 error ("definition of %q#D is marked %<dllimport%>", decl);
5021 DECL_DLLIMPORT_P (decl) = 0;
5024 /* If #pragma weak was used, mark the decl weak now. */
5025 if (!processing_template_decl)
5026 maybe_apply_pragma_weak (decl);
5028 if (TREE_CODE (decl) == FUNCTION_DECL
5029 && DECL_DECLARED_INLINE_P (decl)
5030 && DECL_UNINLINABLE (decl)
5031 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
5032 warning_at (DECL_SOURCE_LOCATION (decl), 0,
5033 "inline function %qD given attribute noinline", decl);
5035 if (TYPE_P (context) && COMPLETE_TYPE_P (complete_type (context)))
5037 bool this_tmpl = (processing_template_decl
5038 > template_class_depth (context));
5039 if (VAR_P (decl))
5041 tree field = lookup_field (context, DECL_NAME (decl), 0, false);
5042 if (field == NULL_TREE
5043 || !(VAR_P (field) || variable_template_p (field)))
5044 error ("%q+#D is not a static data member of %q#T", decl, context);
5045 else if (variable_template_p (field) && !this_tmpl)
5047 if (DECL_LANG_SPECIFIC (decl)
5048 && DECL_TEMPLATE_SPECIALIZATION (decl))
5049 /* OK, specialization was already checked. */;
5050 else
5052 error_at (DECL_SOURCE_LOCATION (decl),
5053 "non-member-template declaration of %qD", decl);
5054 inform (DECL_SOURCE_LOCATION (field), "does not match "
5055 "member template declaration here");
5056 return error_mark_node;
5059 else
5061 if (variable_template_p (field))
5062 field = DECL_TEMPLATE_RESULT (field);
5064 if (DECL_CONTEXT (field) != context)
5066 if (!same_type_p (DECL_CONTEXT (field), context))
5067 permerror (input_location, "ISO C++ does not permit %<%T::%D%> "
5068 "to be defined as %<%T::%D%>",
5069 DECL_CONTEXT (field), DECL_NAME (decl),
5070 context, DECL_NAME (decl));
5071 DECL_CONTEXT (decl) = DECL_CONTEXT (field);
5073 /* Static data member are tricky; an in-class initialization
5074 still doesn't provide a definition, so the in-class
5075 declaration will have DECL_EXTERNAL set, but will have an
5076 initialization. Thus, duplicate_decls won't warn
5077 about this situation, and so we check here. */
5078 if (initialized && DECL_INITIALIZED_IN_CLASS_P (field))
5079 error ("duplicate initialization of %qD", decl);
5080 if (duplicate_decls (decl, field, /*newdecl_is_friend=*/false))
5081 decl = field;
5082 if (decl_spec_seq_has_spec_p (declspecs, ds_constexpr)
5083 && !DECL_DECLARED_CONSTEXPR_P (field))
5084 error ("%qD declared %<constexpr%> outside its class", field);
5087 else
5089 tree field = check_classfn (context, decl,
5090 this_tmpl
5091 ? current_template_parms
5092 : NULL_TREE);
5093 if (field && field != error_mark_node
5094 && duplicate_decls (decl, field,
5095 /*newdecl_is_friend=*/false))
5096 decl = field;
5099 /* cp_finish_decl sets DECL_EXTERNAL if DECL_IN_AGGR_P is set. */
5100 DECL_IN_AGGR_P (decl) = 0;
5101 /* Do not mark DECL as an explicit specialization if it was not
5102 already marked as an instantiation; a declaration should
5103 never be marked as a specialization unless we know what
5104 template is being specialized. */
5105 if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
5107 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
5108 if (TREE_CODE (decl) == FUNCTION_DECL)
5109 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
5110 && DECL_DECLARED_INLINE_P (decl));
5111 else
5112 DECL_COMDAT (decl) = false;
5114 /* [temp.expl.spec] An explicit specialization of a static data
5115 member of a template is a definition if the declaration
5116 includes an initializer; otherwise, it is a declaration.
5118 We check for processing_specialization so this only applies
5119 to the new specialization syntax. */
5120 if (!initialized && processing_specialization)
5121 DECL_EXTERNAL (decl) = 1;
5124 if (DECL_EXTERNAL (decl) && ! DECL_TEMPLATE_SPECIALIZATION (decl)
5125 /* Aliases are definitions. */
5126 && !alias)
5127 permerror (input_location, "declaration of %q#D outside of class is not definition",
5128 decl);
5131 was_public = TREE_PUBLIC (decl);
5133 /* Enter this declaration into the symbol table. Don't push the plain
5134 VAR_DECL for a variable template. */
5135 if (!template_parm_scope_p ()
5136 || !VAR_P (decl))
5137 decl = maybe_push_decl (decl);
5139 if (processing_template_decl)
5140 decl = push_template_decl (decl);
5141 if (decl == error_mark_node)
5142 return error_mark_node;
5144 if (VAR_P (decl)
5145 && DECL_NAMESPACE_SCOPE_P (decl) && !TREE_PUBLIC (decl) && !was_public
5146 && !DECL_THIS_STATIC (decl) && !DECL_ARTIFICIAL (decl))
5148 /* This is a const variable with implicit 'static'. Set
5149 DECL_THIS_STATIC so we can tell it from variables that are
5150 !TREE_PUBLIC because of the anonymous namespace. */
5151 gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (decl)) || errorcount);
5152 DECL_THIS_STATIC (decl) = 1;
5155 if (current_function_decl && VAR_P (decl)
5156 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
5158 bool ok = false;
5159 if (CP_DECL_THREAD_LOCAL_P (decl))
5160 error ("%qD declared %<thread_local%> in %<constexpr%> function",
5161 decl);
5162 else if (TREE_STATIC (decl))
5163 error ("%qD declared %<static%> in %<constexpr%> function", decl);
5164 else
5165 ok = true;
5166 if (!ok)
5167 cp_function_chain->invalid_constexpr = true;
5170 if (!processing_template_decl && VAR_P (decl))
5171 start_decl_1 (decl, initialized);
5173 return decl;
5176 /* Process the declaration of a variable DECL. INITIALIZED is true
5177 iff DECL is explicitly initialized. (INITIALIZED is false if the
5178 variable is initialized via an implicitly-called constructor.)
5179 This function must be called for ordinary variables (including, for
5180 example, implicit instantiations of templates), but must not be
5181 called for template declarations. */
5183 void
5184 start_decl_1 (tree decl, bool initialized)
5186 tree type;
5187 bool complete_p;
5188 bool aggregate_definition_p;
5190 gcc_assert (!processing_template_decl);
5192 if (error_operand_p (decl))
5193 return;
5195 gcc_assert (VAR_P (decl));
5197 type = TREE_TYPE (decl);
5198 complete_p = COMPLETE_TYPE_P (type);
5199 aggregate_definition_p = MAYBE_CLASS_TYPE_P (type) && !DECL_EXTERNAL (decl);
5201 /* If an explicit initializer is present, or if this is a definition
5202 of an aggregate, then we need a complete type at this point.
5203 (Scalars are always complete types, so there is nothing to
5204 check.) This code just sets COMPLETE_P; errors (if necessary)
5205 are issued below. */
5206 if ((initialized || aggregate_definition_p)
5207 && !complete_p
5208 && COMPLETE_TYPE_P (complete_type (type)))
5210 complete_p = true;
5211 /* We will not yet have set TREE_READONLY on DECL if the type
5212 was "const", but incomplete, before this point. But, now, we
5213 have a complete type, so we can try again. */
5214 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
5217 if (initialized)
5218 /* Is it valid for this decl to have an initializer at all? */
5220 /* Don't allow initializations for incomplete types except for
5221 arrays which might be completed by the initialization. */
5222 if (complete_p)
5223 ; /* A complete type is ok. */
5224 else if (type_uses_auto (type))
5225 ; /* An auto type is ok. */
5226 else if (TREE_CODE (type) != ARRAY_TYPE)
5228 error ("variable %q#D has initializer but incomplete type", decl);
5229 type = TREE_TYPE (decl) = error_mark_node;
5231 else if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
5233 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
5234 error ("elements of array %q#D have incomplete type", decl);
5235 /* else we already gave an error in start_decl. */
5238 else if (aggregate_definition_p && !complete_p)
5240 if (type_uses_auto (type))
5241 gcc_assert (CLASS_PLACEHOLDER_TEMPLATE (type));
5242 else
5244 error ("aggregate %q#D has incomplete type and cannot be defined",
5245 decl);
5246 /* Change the type so that assemble_variable will give
5247 DECL an rtl we can live with: (mem (const_int 0)). */
5248 type = TREE_TYPE (decl) = error_mark_node;
5252 /* Create a new scope to hold this declaration if necessary.
5253 Whether or not a new scope is necessary cannot be determined
5254 until after the type has been completed; if the type is a
5255 specialization of a class template it is not until after
5256 instantiation has occurred that TYPE_HAS_NONTRIVIAL_DESTRUCTOR
5257 will be set correctly. */
5258 maybe_push_cleanup_level (type);
5261 /* Handle initialization of references. DECL, TYPE, and INIT have the
5262 same meaning as in cp_finish_decl. *CLEANUP must be NULL on entry,
5263 but will be set to a new CLEANUP_STMT if a temporary is created
5264 that must be destroyed subsequently.
5266 Returns an initializer expression to use to initialize DECL, or
5267 NULL if the initialization can be performed statically.
5269 Quotes on semantics can be found in ARM 8.4.3. */
5271 static tree
5272 grok_reference_init (tree decl, tree type, tree init, int flags)
5274 if (init == NULL_TREE)
5276 if ((DECL_LANG_SPECIFIC (decl) == 0
5277 || DECL_IN_AGGR_P (decl) == 0)
5278 && ! DECL_THIS_EXTERN (decl))
5279 error ("%qD declared as reference but not initialized", decl);
5280 return NULL_TREE;
5283 if (TREE_CODE (init) == TREE_LIST)
5284 init = build_x_compound_expr_from_list (init, ELK_INIT,
5285 tf_warning_or_error);
5287 tree ttype = TREE_TYPE (type);
5288 if (TREE_CODE (ttype) != ARRAY_TYPE
5289 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
5290 /* Note: default conversion is only called in very special cases. */
5291 init = decay_conversion (init, tf_warning_or_error);
5293 /* check_initializer handles this for non-reference variables, but for
5294 references we need to do it here or the initializer will get the
5295 incomplete array type and confuse later calls to
5296 cp_complete_array_type. */
5297 if (TREE_CODE (ttype) == ARRAY_TYPE
5298 && TYPE_DOMAIN (ttype) == NULL_TREE
5299 && (BRACE_ENCLOSED_INITIALIZER_P (init)
5300 || TREE_CODE (init) == STRING_CST))
5302 cp_complete_array_type (&ttype, init, false);
5303 if (ttype != TREE_TYPE (type))
5304 type = cp_build_reference_type (ttype, TYPE_REF_IS_RVALUE (type));
5307 /* Convert INIT to the reference type TYPE. This may involve the
5308 creation of a temporary, whose lifetime must be the same as that
5309 of the reference. If so, a DECL_EXPR for the temporary will be
5310 added just after the DECL_EXPR for DECL. That's why we don't set
5311 DECL_INITIAL for local references (instead assigning to them
5312 explicitly); we need to allow the temporary to be initialized
5313 first. */
5314 return initialize_reference (type, init, flags,
5315 tf_warning_or_error);
5318 /* Designated initializers in arrays are not supported in GNU C++.
5319 The parser cannot detect this error since it does not know whether
5320 a given brace-enclosed initializer is for a class type or for an
5321 array. This function checks that CE does not use a designated
5322 initializer. If it does, an error is issued. Returns true if CE
5323 is valid, i.e., does not have a designated initializer. */
5325 static bool
5326 check_array_designated_initializer (constructor_elt *ce,
5327 unsigned HOST_WIDE_INT index)
5329 /* Designated initializers for array elements are not supported. */
5330 if (ce->index)
5332 /* The parser only allows identifiers as designated
5333 initializers. */
5334 if (ce->index == error_mark_node)
5336 error ("name used in a GNU-style designated "
5337 "initializer for an array");
5338 return false;
5340 else if (identifier_p (ce->index))
5342 error ("name %qD used in a GNU-style designated "
5343 "initializer for an array", ce->index);
5344 return false;
5347 tree ce_index = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5348 ce->index, true);
5349 if (ce_index
5350 && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (ce_index))
5351 && (TREE_CODE (ce_index = maybe_constant_value (ce_index))
5352 == INTEGER_CST))
5354 /* A C99 designator is OK if it matches the current index. */
5355 if (wi::eq_p (ce_index, index))
5356 return true;
5357 else
5358 sorry ("non-trivial designated initializers not supported");
5360 else
5361 error ("C99 designator %qE is not an integral constant-expression",
5362 ce->index);
5364 return false;
5367 return true;
5370 /* When parsing `int a[] = {1, 2};' we don't know the size of the
5371 array until we finish parsing the initializer. If that's the
5372 situation we're in, update DECL accordingly. */
5374 static void
5375 maybe_deduce_size_from_array_init (tree decl, tree init)
5377 tree type = TREE_TYPE (decl);
5379 if (TREE_CODE (type) == ARRAY_TYPE
5380 && TYPE_DOMAIN (type) == NULL_TREE
5381 && TREE_CODE (decl) != TYPE_DECL)
5383 /* do_default is really a C-ism to deal with tentative definitions.
5384 But let's leave it here to ease the eventual merge. */
5385 int do_default = !DECL_EXTERNAL (decl);
5386 tree initializer = init ? init : DECL_INITIAL (decl);
5387 int failure = 0;
5389 /* Check that there are no designated initializers in INIT, as
5390 those are not supported in GNU C++, and as the middle-end
5391 will crash if presented with a non-numeric designated
5392 initializer. */
5393 if (initializer && BRACE_ENCLOSED_INITIALIZER_P (initializer))
5395 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initializer);
5396 constructor_elt *ce;
5397 HOST_WIDE_INT i;
5398 FOR_EACH_VEC_SAFE_ELT (v, i, ce)
5399 if (!check_array_designated_initializer (ce, i))
5400 failure = 1;
5403 if (!failure)
5405 failure = cp_complete_array_type (&TREE_TYPE (decl), initializer,
5406 do_default);
5407 if (failure == 1)
5409 error_at (EXPR_LOC_OR_LOC (initializer,
5410 DECL_SOURCE_LOCATION (decl)),
5411 "initializer fails to determine size of %qD", decl);
5413 else if (failure == 2)
5415 if (do_default)
5417 error_at (DECL_SOURCE_LOCATION (decl),
5418 "array size missing in %qD", decl);
5420 /* If a `static' var's size isn't known, make it extern as
5421 well as static, so it does not get allocated. If it's not
5422 `static', then don't mark it extern; finish_incomplete_decl
5423 will give it a default size and it will get allocated. */
5424 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
5425 DECL_EXTERNAL (decl) = 1;
5427 else if (failure == 3)
5429 error_at (DECL_SOURCE_LOCATION (decl),
5430 "zero-size array %qD", decl);
5434 cp_apply_type_quals_to_decl (cp_type_quals (TREE_TYPE (decl)), decl);
5436 relayout_decl (decl);
5440 /* Set DECL_SIZE, DECL_ALIGN, etc. for DECL (a VAR_DECL), and issue
5441 any appropriate error messages regarding the layout. */
5443 static void
5444 layout_var_decl (tree decl)
5446 tree type;
5448 type = TREE_TYPE (decl);
5449 if (type == error_mark_node)
5450 return;
5452 /* If we haven't already laid out this declaration, do so now.
5453 Note that we must not call complete type for an external object
5454 because it's type might involve templates that we are not
5455 supposed to instantiate yet. (And it's perfectly valid to say
5456 `extern X x' for some incomplete type `X'.) */
5457 if (!DECL_EXTERNAL (decl))
5458 complete_type (type);
5459 if (!DECL_SIZE (decl)
5460 && TREE_TYPE (decl) != error_mark_node
5461 && complete_or_array_type_p (type))
5462 layout_decl (decl, 0);
5464 if (!DECL_EXTERNAL (decl) && DECL_SIZE (decl) == NULL_TREE)
5466 /* An automatic variable with an incomplete type: that is an error.
5467 Don't talk about array types here, since we took care of that
5468 message in grokdeclarator. */
5469 error_at (DECL_SOURCE_LOCATION (decl),
5470 "storage size of %qD isn%'t known", decl);
5471 TREE_TYPE (decl) = error_mark_node;
5473 #if 0
5474 /* Keep this code around in case we later want to control debug info
5475 based on whether a type is "used". (jason 1999-11-11) */
5477 else if (!DECL_EXTERNAL (decl) && MAYBE_CLASS_TYPE_P (ttype))
5478 /* Let debugger know it should output info for this type. */
5479 note_debug_info_needed (ttype);
5481 if (TREE_STATIC (decl) && DECL_CLASS_SCOPE_P (decl))
5482 note_debug_info_needed (DECL_CONTEXT (decl));
5483 #endif
5485 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
5486 && DECL_SIZE (decl) != NULL_TREE
5487 && ! TREE_CONSTANT (DECL_SIZE (decl)))
5489 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
5490 constant_expression_warning (DECL_SIZE (decl));
5491 else
5493 error_at (DECL_SOURCE_LOCATION (decl),
5494 "storage size of %qD isn%'t constant", decl);
5495 TREE_TYPE (decl) = error_mark_node;
5500 /* If a local static variable is declared in an inline function, or if
5501 we have a weak definition, we must endeavor to create only one
5502 instance of the variable at link-time. */
5504 void
5505 maybe_commonize_var (tree decl)
5507 /* Static data in a function with comdat linkage also has comdat
5508 linkage. */
5509 if ((TREE_STATIC (decl)
5510 /* Don't mess with __FUNCTION__. */
5511 && ! DECL_ARTIFICIAL (decl)
5512 && DECL_FUNCTION_SCOPE_P (decl)
5513 && vague_linkage_p (DECL_CONTEXT (decl)))
5514 || (TREE_PUBLIC (decl) && DECL_INLINE_VAR_P (decl)))
5516 if (flag_weak)
5518 /* With weak symbols, we simply make the variable COMDAT;
5519 that will cause copies in multiple translations units to
5520 be merged. */
5521 comdat_linkage (decl);
5523 else
5525 if (DECL_INITIAL (decl) == NULL_TREE
5526 || DECL_INITIAL (decl) == error_mark_node)
5528 /* Without weak symbols, we can use COMMON to merge
5529 uninitialized variables. */
5530 TREE_PUBLIC (decl) = 1;
5531 DECL_COMMON (decl) = 1;
5533 else
5535 /* While for initialized variables, we must use internal
5536 linkage -- which means that multiple copies will not
5537 be merged. */
5538 TREE_PUBLIC (decl) = 0;
5539 DECL_COMMON (decl) = 0;
5540 const char *msg;
5541 if (DECL_INLINE_VAR_P (decl))
5542 msg = G_("sorry: semantics of inline variable "
5543 "%q#D are wrong (you%'ll wind up with "
5544 "multiple copies)");
5545 else
5546 msg = G_("sorry: semantics of inline function "
5547 "static data %q#D are wrong (you%'ll wind "
5548 "up with multiple copies)");
5549 if (warning_at (DECL_SOURCE_LOCATION (decl), 0,
5550 msg, decl))
5551 inform (DECL_SOURCE_LOCATION (decl),
5552 "you can work around this by removing the initializer");
5558 /* Issue an error message if DECL is an uninitialized const variable. */
5560 static void
5561 check_for_uninitialized_const_var (tree decl)
5563 tree type = strip_array_types (TREE_TYPE (decl));
5565 /* ``Unless explicitly declared extern, a const object does not have
5566 external linkage and must be initialized. ($8.4; $12.1)'' ARM
5567 7.1.6 */
5568 if (VAR_P (decl)
5569 && TREE_CODE (type) != REFERENCE_TYPE
5570 && (CP_TYPE_CONST_P (type) || var_in_constexpr_fn (decl))
5571 && !DECL_INITIAL (decl))
5573 tree field = default_init_uninitialized_part (type);
5574 if (!field)
5575 return;
5577 if (CP_TYPE_CONST_P (type))
5578 permerror (DECL_SOURCE_LOCATION (decl),
5579 "uninitialized const %qD", decl);
5580 else
5582 error_at (DECL_SOURCE_LOCATION (decl),
5583 "uninitialized variable %qD in %<constexpr%> function",
5584 decl);
5585 cp_function_chain->invalid_constexpr = true;
5588 if (CLASS_TYPE_P (type))
5590 tree defaulted_ctor;
5592 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
5593 "%q#T has no user-provided default constructor", type);
5594 defaulted_ctor = in_class_defaulted_default_constructor (type);
5595 if (defaulted_ctor)
5596 inform (DECL_SOURCE_LOCATION (defaulted_ctor),
5597 "constructor is not user-provided because it is "
5598 "explicitly defaulted in the class body");
5599 inform (DECL_SOURCE_LOCATION (field),
5600 "and the implicitly-defined constructor does not "
5601 "initialize %q#D", field);
5606 /* Structure holding the current initializer being processed by reshape_init.
5607 CUR is a pointer to the current element being processed, END is a pointer
5608 after the last element present in the initializer. */
5609 struct reshape_iter
5611 constructor_elt *cur;
5612 constructor_elt *end;
5615 static tree reshape_init_r (tree, reshape_iter *, bool, tsubst_flags_t);
5617 /* FIELD is a FIELD_DECL or NULL. In the former case, the value
5618 returned is the next FIELD_DECL (possibly FIELD itself) that can be
5619 initialized. If there are no more such fields, the return value
5620 will be NULL. */
5622 tree
5623 next_initializable_field (tree field)
5625 while (field
5626 && (TREE_CODE (field) != FIELD_DECL
5627 || (DECL_C_BIT_FIELD (field) && !DECL_NAME (field))
5628 || (DECL_ARTIFICIAL (field)
5629 && !(cxx_dialect >= cxx1z && DECL_FIELD_IS_BASE (field)))))
5630 field = DECL_CHAIN (field);
5632 return field;
5635 /* Return true for [dcl.init.list] direct-list-initialization from
5636 single element of enumeration with a fixed underlying type. */
5638 bool
5639 is_direct_enum_init (tree type, tree init)
5641 if (cxx_dialect >= cxx1z
5642 && TREE_CODE (type) == ENUMERAL_TYPE
5643 && ENUM_FIXED_UNDERLYING_TYPE_P (type)
5644 && TREE_CODE (init) == CONSTRUCTOR
5645 && CONSTRUCTOR_IS_DIRECT_INIT (init)
5646 && CONSTRUCTOR_NELTS (init) == 1)
5647 return true;
5648 return false;
5651 /* Subroutine of reshape_init_array and reshape_init_vector, which does
5652 the actual work. ELT_TYPE is the element type of the array. MAX_INDEX is an
5653 INTEGER_CST representing the size of the array minus one (the maximum index),
5654 or NULL_TREE if the array was declared without specifying the size. D is
5655 the iterator within the constructor. */
5657 static tree
5658 reshape_init_array_1 (tree elt_type, tree max_index, reshape_iter *d,
5659 tsubst_flags_t complain)
5661 tree new_init;
5662 bool sized_array_p = (max_index && TREE_CONSTANT (max_index));
5663 unsigned HOST_WIDE_INT max_index_cst = 0;
5664 unsigned HOST_WIDE_INT index;
5666 /* The initializer for an array is always a CONSTRUCTOR. */
5667 new_init = build_constructor (init_list_type_node, NULL);
5669 if (sized_array_p)
5671 /* Minus 1 is used for zero sized arrays. */
5672 if (integer_all_onesp (max_index))
5673 return new_init;
5675 if (tree_fits_uhwi_p (max_index))
5676 max_index_cst = tree_to_uhwi (max_index);
5677 /* sizetype is sign extended, not zero extended. */
5678 else
5679 max_index_cst = tree_to_uhwi (fold_convert (size_type_node, max_index));
5682 /* Loop until there are no more initializers. */
5683 for (index = 0;
5684 d->cur != d->end && (!sized_array_p || index <= max_index_cst);
5685 ++index)
5687 tree elt_init;
5688 constructor_elt *old_cur = d->cur;
5690 check_array_designated_initializer (d->cur, index);
5691 elt_init = reshape_init_r (elt_type, d, /*first_initializer_p=*/false,
5692 complain);
5693 if (elt_init == error_mark_node)
5694 return error_mark_node;
5695 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init),
5696 size_int (index), elt_init);
5697 if (!TREE_CONSTANT (elt_init))
5698 TREE_CONSTANT (new_init) = false;
5700 /* This can happen with an invalid initializer (c++/54501). */
5701 if (d->cur == old_cur && !sized_array_p)
5702 break;
5705 return new_init;
5708 /* Subroutine of reshape_init_r, processes the initializers for arrays.
5709 Parameters are the same of reshape_init_r. */
5711 static tree
5712 reshape_init_array (tree type, reshape_iter *d, tsubst_flags_t complain)
5714 tree max_index = NULL_TREE;
5716 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
5718 if (TYPE_DOMAIN (type))
5719 max_index = array_type_nelts (type);
5721 return reshape_init_array_1 (TREE_TYPE (type), max_index, d, complain);
5724 /* Subroutine of reshape_init_r, processes the initializers for vectors.
5725 Parameters are the same of reshape_init_r. */
5727 static tree
5728 reshape_init_vector (tree type, reshape_iter *d, tsubst_flags_t complain)
5730 tree max_index = NULL_TREE;
5732 gcc_assert (VECTOR_TYPE_P (type));
5734 if (COMPOUND_LITERAL_P (d->cur->value))
5736 tree value = d->cur->value;
5737 if (!same_type_p (TREE_TYPE (value), type))
5739 if (complain & tf_error)
5740 error ("invalid type %qT as initializer for a vector of type %qT",
5741 TREE_TYPE (d->cur->value), type);
5742 value = error_mark_node;
5744 ++d->cur;
5745 return value;
5748 /* For a vector, we initialize it as an array of the appropriate size. */
5749 if (VECTOR_TYPE_P (type))
5750 max_index = size_int (TYPE_VECTOR_SUBPARTS (type) - 1);
5752 return reshape_init_array_1 (TREE_TYPE (type), max_index, d, complain);
5755 /* Subroutine of reshape_init_r, processes the initializers for classes
5756 or union. Parameters are the same of reshape_init_r. */
5758 static tree
5759 reshape_init_class (tree type, reshape_iter *d, bool first_initializer_p,
5760 tsubst_flags_t complain)
5762 tree field;
5763 tree new_init;
5765 gcc_assert (CLASS_TYPE_P (type));
5767 /* The initializer for a class is always a CONSTRUCTOR. */
5768 new_init = build_constructor (init_list_type_node, NULL);
5769 field = next_initializable_field (TYPE_FIELDS (type));
5771 if (!field)
5773 /* [dcl.init.aggr]
5775 An initializer for an aggregate member that is an
5776 empty class shall have the form of an empty
5777 initializer-list {}. */
5778 if (!first_initializer_p)
5780 if (complain & tf_error)
5781 error ("initializer for %qT must be brace-enclosed", type);
5782 return error_mark_node;
5784 return new_init;
5787 /* Loop through the initializable fields, gathering initializers. */
5788 while (d->cur != d->end)
5790 tree field_init;
5791 constructor_elt *old_cur = d->cur;
5793 /* Handle designated initializers, as an extension. */
5794 if (d->cur->index)
5796 if (d->cur->index == error_mark_node)
5797 return error_mark_node;
5799 if (TREE_CODE (d->cur->index) == FIELD_DECL)
5800 /* We already reshaped this. */
5801 gcc_assert (d->cur->index == field);
5802 else if (TREE_CODE (d->cur->index) == IDENTIFIER_NODE)
5803 field = lookup_field_1 (type, d->cur->index, /*want_type=*/false);
5804 else
5806 if (complain & tf_error)
5807 error ("%<[%E] =%> used in a GNU-style designated initializer"
5808 " for class %qT", d->cur->index, type);
5809 return error_mark_node;
5812 if (!field || TREE_CODE (field) != FIELD_DECL)
5814 if (complain & tf_error)
5815 error ("%qT has no non-static data member named %qD", type,
5816 d->cur->index);
5817 return error_mark_node;
5821 /* If we processed all the member of the class, we are done. */
5822 if (!field)
5823 break;
5825 field_init = reshape_init_r (TREE_TYPE (field), d,
5826 /*first_initializer_p=*/false, complain);
5827 if (field_init == error_mark_node)
5828 return error_mark_node;
5830 if (d->cur == old_cur && d->cur->index)
5832 /* This can happen with an invalid initializer for a flexible
5833 array member (c++/54441). */
5834 if (complain & tf_error)
5835 error ("invalid initializer for %q#D", field);
5836 return error_mark_node;
5839 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init), field, field_init);
5841 /* [dcl.init.aggr]
5843 When a union is initialized with a brace-enclosed
5844 initializer, the braces shall only contain an
5845 initializer for the first member of the union. */
5846 if (TREE_CODE (type) == UNION_TYPE)
5847 break;
5849 field = next_initializable_field (DECL_CHAIN (field));
5852 return new_init;
5855 /* Subroutine of reshape_init_r. We're in a context where C99 initializer
5856 designators are not valid; either complain or return true to indicate
5857 that reshape_init_r should return error_mark_node. */
5859 static bool
5860 has_designator_problem (reshape_iter *d, tsubst_flags_t complain)
5862 if (d->cur->index)
5864 if (complain & tf_error)
5865 error ("C99 designator %qE outside aggregate initializer",
5866 d->cur->index);
5867 else
5868 return true;
5870 return false;
5873 /* Subroutine of reshape_init, which processes a single initializer (part of
5874 a CONSTRUCTOR). TYPE is the type of the variable being initialized, D is the
5875 iterator within the CONSTRUCTOR which points to the initializer to process.
5876 FIRST_INITIALIZER_P is true if this is the first initializer of the
5877 outermost CONSTRUCTOR node. */
5879 static tree
5880 reshape_init_r (tree type, reshape_iter *d, bool first_initializer_p,
5881 tsubst_flags_t complain)
5883 tree init = d->cur->value;
5885 if (error_operand_p (init))
5886 return error_mark_node;
5888 if (first_initializer_p && !CP_AGGREGATE_TYPE_P (type)
5889 && has_designator_problem (d, complain))
5890 return error_mark_node;
5892 if (TREE_CODE (type) == COMPLEX_TYPE)
5894 /* A complex type can be initialized from one or two initializers,
5895 but braces are not elided. */
5896 d->cur++;
5897 if (BRACE_ENCLOSED_INITIALIZER_P (init))
5899 if (CONSTRUCTOR_NELTS (init) > 2)
5901 if (complain & tf_error)
5902 error ("too many initializers for %qT", type);
5903 else
5904 return error_mark_node;
5907 else if (first_initializer_p && d->cur != d->end)
5909 vec<constructor_elt, va_gc> *v = 0;
5910 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, init);
5911 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, d->cur->value);
5912 if (has_designator_problem (d, complain))
5913 return error_mark_node;
5914 d->cur++;
5915 init = build_constructor (init_list_type_node, v);
5917 return init;
5920 /* A non-aggregate type is always initialized with a single
5921 initializer. */
5922 if (!CP_AGGREGATE_TYPE_P (type))
5924 /* It is invalid to initialize a non-aggregate type with a
5925 brace-enclosed initializer before C++0x.
5926 We need to check for BRACE_ENCLOSED_INITIALIZER_P here because
5927 of g++.old-deja/g++.mike/p7626.C: a pointer-to-member constant is
5928 a CONSTRUCTOR (with a record type). */
5929 if (TREE_CODE (init) == CONSTRUCTOR
5930 /* Don't complain about a capture-init. */
5931 && !CONSTRUCTOR_IS_DIRECT_INIT (init)
5932 && BRACE_ENCLOSED_INITIALIZER_P (init)) /* p7626.C */
5934 if (SCALAR_TYPE_P (type))
5936 if (cxx_dialect < cxx11
5937 /* Isn't value-initialization. */
5938 || CONSTRUCTOR_NELTS (init) > 0)
5940 if (complain & tf_error)
5941 error ("braces around scalar initializer for type %qT",
5942 type);
5943 init = error_mark_node;
5946 else
5947 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5950 d->cur++;
5951 return init;
5954 /* "If T is a class type and the initializer list has a single element of
5955 type cv U, where U is T or a class derived from T, the object is
5956 initialized from that element." Even if T is an aggregate. */
5957 if (cxx_dialect >= cxx11 && (CLASS_TYPE_P (type) || VECTOR_TYPE_P (type))
5958 && first_initializer_p
5959 && d->end - d->cur == 1
5960 && reference_related_p (type, TREE_TYPE (init)))
5962 d->cur++;
5963 return init;
5966 /* [dcl.init.aggr]
5968 All implicit type conversions (clause _conv_) are considered when
5969 initializing the aggregate member with an initializer from an
5970 initializer-list. If the initializer can initialize a member,
5971 the member is initialized. Otherwise, if the member is itself a
5972 non-empty subaggregate, brace elision is assumed and the
5973 initializer is considered for the initialization of the first
5974 member of the subaggregate. */
5975 if (TREE_CODE (init) != CONSTRUCTOR
5976 /* But don't try this for the first initializer, since that would be
5977 looking through the outermost braces; A a2 = { a1 }; is not a
5978 valid aggregate initialization. */
5979 && !first_initializer_p
5980 && (same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (init))
5981 || can_convert_arg (type, TREE_TYPE (init), init, LOOKUP_NORMAL,
5982 complain)))
5984 d->cur++;
5985 return init;
5988 /* [dcl.init.string]
5990 A char array (whether plain char, signed char, or unsigned char)
5991 can be initialized by a string-literal (optionally enclosed in
5992 braces); a wchar_t array can be initialized by a wide
5993 string-literal (optionally enclosed in braces). */
5994 if (TREE_CODE (type) == ARRAY_TYPE
5995 && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type))))
5997 tree str_init = init;
5999 /* Strip one level of braces if and only if they enclose a single
6000 element (as allowed by [dcl.init.string]). */
6001 if (!first_initializer_p
6002 && TREE_CODE (str_init) == CONSTRUCTOR
6003 && CONSTRUCTOR_NELTS (str_init) == 1)
6005 str_init = (*CONSTRUCTOR_ELTS (str_init))[0].value;
6008 /* If it's a string literal, then it's the initializer for the array
6009 as a whole. Otherwise, continue with normal initialization for
6010 array types (one value per array element). */
6011 if (TREE_CODE (str_init) == STRING_CST)
6013 if (has_designator_problem (d, complain))
6014 return error_mark_node;
6015 d->cur++;
6016 return str_init;
6020 /* The following cases are about aggregates. If we are not within a full
6021 initializer already, and there is not a CONSTRUCTOR, it means that there
6022 is a missing set of braces (that is, we are processing the case for
6023 which reshape_init exists). */
6024 if (!first_initializer_p)
6026 if (TREE_CODE (init) == CONSTRUCTOR)
6028 if (TREE_TYPE (init) && TYPE_PTRMEMFUNC_P (TREE_TYPE (init)))
6029 /* There is no need to reshape pointer-to-member function
6030 initializers, as they are always constructed correctly
6031 by the front end. */
6033 else if (COMPOUND_LITERAL_P (init))
6034 /* For a nested compound literal, there is no need to reshape since
6035 brace elision is not allowed. Even if we decided to allow it,
6036 we should add a call to reshape_init in finish_compound_literal,
6037 before calling digest_init, so changing this code would still
6038 not be necessary. */
6039 gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (init));
6040 else
6042 ++d->cur;
6043 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
6044 return reshape_init (type, init, complain);
6048 if (complain & tf_warning)
6049 warning (OPT_Wmissing_braces,
6050 "missing braces around initializer for %qT",
6051 type);
6054 /* Dispatch to specialized routines. */
6055 if (CLASS_TYPE_P (type))
6056 return reshape_init_class (type, d, first_initializer_p, complain);
6057 else if (TREE_CODE (type) == ARRAY_TYPE)
6058 return reshape_init_array (type, d, complain);
6059 else if (VECTOR_TYPE_P (type))
6060 return reshape_init_vector (type, d, complain);
6061 else
6062 gcc_unreachable();
6065 /* Undo the brace-elision allowed by [dcl.init.aggr] in a
6066 brace-enclosed aggregate initializer.
6068 INIT is the CONSTRUCTOR containing the list of initializers describing
6069 a brace-enclosed initializer for an entity of the indicated aggregate TYPE.
6070 It may not presently match the shape of the TYPE; for example:
6072 struct S { int a; int b; };
6073 struct S a[] = { 1, 2, 3, 4 };
6075 Here INIT will hold a vector of four elements, rather than a
6076 vector of two elements, each itself a vector of two elements. This
6077 routine transforms INIT from the former form into the latter. The
6078 revised CONSTRUCTOR node is returned. */
6080 tree
6081 reshape_init (tree type, tree init, tsubst_flags_t complain)
6083 vec<constructor_elt, va_gc> *v;
6084 reshape_iter d;
6085 tree new_init;
6087 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
6089 v = CONSTRUCTOR_ELTS (init);
6091 /* An empty constructor does not need reshaping, and it is always a valid
6092 initializer. */
6093 if (vec_safe_is_empty (v))
6094 return init;
6096 /* Handle [dcl.init.list] direct-list-initialization from
6097 single element of enumeration with a fixed underlying type. */
6098 if (is_direct_enum_init (type, init))
6100 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
6101 if (check_narrowing (ENUM_UNDERLYING_TYPE (type), elt, complain))
6102 return cp_build_c_cast (type, elt, tf_warning_or_error);
6103 else
6104 return error_mark_node;
6107 /* Recurse on this CONSTRUCTOR. */
6108 d.cur = &(*v)[0];
6109 d.end = d.cur + v->length ();
6111 new_init = reshape_init_r (type, &d, true, complain);
6112 if (new_init == error_mark_node)
6113 return error_mark_node;
6115 /* Make sure all the element of the constructor were used. Otherwise,
6116 issue an error about exceeding initializers. */
6117 if (d.cur != d.end)
6119 if (complain & tf_error)
6120 error ("too many initializers for %qT", type);
6121 else
6122 return error_mark_node;
6125 if (CONSTRUCTOR_IS_DIRECT_INIT (init)
6126 && BRACE_ENCLOSED_INITIALIZER_P (new_init))
6127 CONSTRUCTOR_IS_DIRECT_INIT (new_init) = true;
6129 return new_init;
6132 /* Verify array initializer. Returns true if errors have been reported. */
6134 bool
6135 check_array_initializer (tree decl, tree type, tree init)
6137 tree element_type = TREE_TYPE (type);
6139 /* The array type itself need not be complete, because the
6140 initializer may tell us how many elements are in the array.
6141 But, the elements of the array must be complete. */
6142 if (!COMPLETE_TYPE_P (complete_type (element_type)))
6144 if (decl)
6145 error_at (DECL_SOURCE_LOCATION (decl),
6146 "elements of array %q#D have incomplete type", decl);
6147 else
6148 error ("elements of array %q#T have incomplete type", type);
6149 return true;
6151 /* A compound literal can't have variable size. */
6152 if (init && !decl
6153 && ((COMPLETE_TYPE_P (type) && !TREE_CONSTANT (TYPE_SIZE (type)))
6154 || !TREE_CONSTANT (TYPE_SIZE (element_type))))
6156 error ("variable-sized compound literal");
6157 return true;
6159 return false;
6162 /* Subroutine of check_initializer; args are passed down from that function.
6163 Set stmts_are_full_exprs_p to 1 across a call to build_aggr_init. */
6165 static tree
6166 build_aggr_init_full_exprs (tree decl, tree init, int flags)
6169 gcc_assert (stmts_are_full_exprs_p ());
6170 return build_aggr_init (decl, init, flags, tf_warning_or_error);
6173 /* Verify INIT (the initializer for DECL), and record the
6174 initialization in DECL_INITIAL, if appropriate. CLEANUP is as for
6175 grok_reference_init.
6177 If the return value is non-NULL, it is an expression that must be
6178 evaluated dynamically to initialize DECL. */
6180 static tree
6181 check_initializer (tree decl, tree init, int flags, vec<tree, va_gc> **cleanups)
6183 tree type = TREE_TYPE (decl);
6184 tree init_code = NULL;
6185 tree core_type;
6187 /* Things that are going to be initialized need to have complete
6188 type. */
6189 TREE_TYPE (decl) = type = complete_type (TREE_TYPE (decl));
6191 if (DECL_HAS_VALUE_EXPR_P (decl))
6193 /* A variable with DECL_HAS_VALUE_EXPR_P set is just a placeholder,
6194 it doesn't have storage to be initialized. */
6195 gcc_assert (init == NULL_TREE);
6196 return NULL_TREE;
6199 if (type == error_mark_node)
6200 /* We will have already complained. */
6201 return NULL_TREE;
6203 if (TREE_CODE (type) == ARRAY_TYPE)
6205 if (check_array_initializer (decl, type, init))
6206 return NULL_TREE;
6208 else if (!COMPLETE_TYPE_P (type))
6210 error_at (DECL_SOURCE_LOCATION (decl),
6211 "%q#D has incomplete type", decl);
6212 TREE_TYPE (decl) = error_mark_node;
6213 return NULL_TREE;
6215 else
6216 /* There is no way to make a variable-sized class type in GNU C++. */
6217 gcc_assert (TREE_CONSTANT (TYPE_SIZE (type)));
6219 if (init && BRACE_ENCLOSED_INITIALIZER_P (init))
6221 int init_len = CONSTRUCTOR_NELTS (init);
6222 if (SCALAR_TYPE_P (type))
6224 if (init_len == 0)
6226 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6227 init = build_zero_init (type, NULL_TREE, false);
6229 else if (init_len != 1 && TREE_CODE (type) != COMPLEX_TYPE)
6231 error_at (EXPR_LOC_OR_LOC (init, DECL_SOURCE_LOCATION (decl)),
6232 "scalar object %qD requires one element in "
6233 "initializer", decl);
6234 TREE_TYPE (decl) = error_mark_node;
6235 return NULL_TREE;
6240 if (TREE_CODE (decl) == CONST_DECL)
6242 gcc_assert (TREE_CODE (type) != REFERENCE_TYPE);
6244 DECL_INITIAL (decl) = init;
6246 gcc_assert (init != NULL_TREE);
6247 init = NULL_TREE;
6249 else if (!init && DECL_REALLY_EXTERN (decl))
6251 else if (init || type_build_ctor_call (type)
6252 || TREE_CODE (type) == REFERENCE_TYPE)
6254 if (TREE_CODE (type) == REFERENCE_TYPE)
6256 init = grok_reference_init (decl, type, init, flags);
6257 flags |= LOOKUP_ALREADY_DIGESTED;
6259 else if (!init)
6260 check_for_uninitialized_const_var (decl);
6261 /* Do not reshape constructors of vectors (they don't need to be
6262 reshaped. */
6263 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
6265 if (is_std_init_list (type))
6267 init = perform_implicit_conversion (type, init,
6268 tf_warning_or_error);
6269 flags |= LOOKUP_ALREADY_DIGESTED;
6271 else if (TYPE_NON_AGGREGATE_CLASS (type))
6273 /* Don't reshape if the class has constructors. */
6274 if (cxx_dialect == cxx98)
6275 error_at (EXPR_LOC_OR_LOC (init, DECL_SOURCE_LOCATION (decl)),
6276 "in C++98 %qD must be initialized by "
6277 "constructor, not by %<{...}%>",
6278 decl);
6280 else if (VECTOR_TYPE_P (type) && TYPE_VECTOR_OPAQUE (type))
6282 error ("opaque vector types cannot be initialized");
6283 init = error_mark_node;
6285 else
6287 init = reshape_init (type, init, tf_warning_or_error);
6288 flags |= LOOKUP_NO_NARROWING;
6291 else if (TREE_CODE (init) == TREE_LIST
6292 && TREE_TYPE (init) != unknown_type_node
6293 && !MAYBE_CLASS_TYPE_P (type))
6295 gcc_assert (TREE_CODE (decl) != RESULT_DECL);
6297 /* We get here with code like `int a (2);' */
6298 init = build_x_compound_expr_from_list (init, ELK_INIT,
6299 tf_warning_or_error);
6302 /* If DECL has an array type without a specific bound, deduce the
6303 array size from the initializer. */
6304 maybe_deduce_size_from_array_init (decl, init);
6305 type = TREE_TYPE (decl);
6306 if (type == error_mark_node)
6307 return NULL_TREE;
6309 if (((type_build_ctor_call (type) || CLASS_TYPE_P (type))
6310 && !(flags & LOOKUP_ALREADY_DIGESTED)
6311 && !(init && BRACE_ENCLOSED_INITIALIZER_P (init)
6312 && CP_AGGREGATE_TYPE_P (type)
6313 && (CLASS_TYPE_P (type)
6314 || !TYPE_NEEDS_CONSTRUCTING (type)
6315 || type_has_extended_temps (type))))
6316 || (DECL_DECOMPOSITION_P (decl) && TREE_CODE (type) == ARRAY_TYPE))
6318 init_code = build_aggr_init_full_exprs (decl, init, flags);
6320 /* A constructor call is a non-trivial initializer even if
6321 it isn't explicitly written. */
6322 if (TREE_SIDE_EFFECTS (init_code))
6323 DECL_NONTRIVIALLY_INITIALIZED_P (decl) = true;
6325 /* If this is a constexpr initializer, expand_default_init will
6326 have returned an INIT_EXPR rather than a CALL_EXPR. In that
6327 case, pull the initializer back out and pass it down into
6328 store_init_value. */
6329 while (TREE_CODE (init_code) == EXPR_STMT
6330 || TREE_CODE (init_code) == CONVERT_EXPR)
6331 init_code = TREE_OPERAND (init_code, 0);
6332 if (TREE_CODE (init_code) == INIT_EXPR)
6334 init = TREE_OPERAND (init_code, 1);
6335 init_code = NULL_TREE;
6336 /* Don't call digest_init; it's unnecessary and will complain
6337 about aggregate initialization of non-aggregate classes. */
6338 flags |= LOOKUP_ALREADY_DIGESTED;
6340 else if (DECL_DECLARED_CONSTEXPR_P (decl))
6342 /* Declared constexpr, but no suitable initializer; massage
6343 init appropriately so we can pass it into store_init_value
6344 for the error. */
6345 if (CLASS_TYPE_P (type)
6346 && (!init || TREE_CODE (init) == TREE_LIST))
6348 init = build_functional_cast (type, init, tf_none);
6349 if (TREE_CODE (init) == TARGET_EXPR)
6350 TARGET_EXPR_DIRECT_INIT_P (init) = true;
6352 init_code = NULL_TREE;
6354 else
6355 init = NULL_TREE;
6358 if (init && TREE_CODE (init) != TREE_VEC)
6360 /* In aggregate initialization of a variable, each element
6361 initialization is a full-expression because there is no
6362 enclosing expression. */
6363 gcc_assert (stmts_are_full_exprs_p ());
6365 init_code = store_init_value (decl, init, cleanups, flags);
6367 if (pedantic && TREE_CODE (type) == ARRAY_TYPE
6368 && DECL_INITIAL (decl)
6369 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
6370 && PAREN_STRING_LITERAL_P (DECL_INITIAL (decl)))
6371 warning_at (EXPR_LOC_OR_LOC (DECL_INITIAL (decl),
6372 DECL_SOURCE_LOCATION (decl)),
6373 0, "array %qD initialized by parenthesized "
6374 "string literal %qE",
6375 decl, DECL_INITIAL (decl));
6376 init = NULL;
6379 else
6381 if (CLASS_TYPE_P (core_type = strip_array_types (type))
6382 && (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type)
6383 || CLASSTYPE_REF_FIELDS_NEED_INIT (core_type)))
6384 diagnose_uninitialized_cst_or_ref_member (core_type, /*using_new=*/false,
6385 /*complain=*/true);
6387 check_for_uninitialized_const_var (decl);
6390 if (init && init != error_mark_node)
6391 init_code = build2 (INIT_EXPR, type, decl, init);
6393 if (init_code)
6395 /* We might have set these in cp_finish_decl. */
6396 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = false;
6397 TREE_CONSTANT (decl) = false;
6400 if (init_code
6401 && (DECL_IN_AGGR_P (decl) && !DECL_VAR_DECLARED_INLINE_P (decl)))
6403 static int explained = 0;
6405 if (cxx_dialect < cxx11)
6406 error ("initializer invalid for static member with constructor");
6407 else if (cxx_dialect < cxx1z)
6408 error ("non-constant in-class initialization invalid for static "
6409 "member %qD", decl);
6410 else
6411 error ("non-constant in-class initialization invalid for non-inline "
6412 "static member %qD", decl);
6413 if (!explained)
6415 inform (input_location,
6416 "(an out of class initialization is required)");
6417 explained = 1;
6419 return NULL_TREE;
6422 return init_code;
6425 /* If DECL is not a local variable, give it RTL. */
6427 static void
6428 make_rtl_for_nonlocal_decl (tree decl, tree init, const char* asmspec)
6430 int toplev = toplevel_bindings_p ();
6431 int defer_p;
6433 /* Set the DECL_ASSEMBLER_NAME for the object. */
6434 if (asmspec)
6436 /* The `register' keyword, when used together with an
6437 asm-specification, indicates that the variable should be
6438 placed in a particular register. */
6439 if (VAR_P (decl) && DECL_REGISTER (decl))
6441 set_user_assembler_name (decl, asmspec);
6442 DECL_HARD_REGISTER (decl) = 1;
6444 else
6446 if (TREE_CODE (decl) == FUNCTION_DECL
6447 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
6448 set_builtin_user_assembler_name (decl, asmspec);
6449 set_user_assembler_name (decl, asmspec);
6453 /* Handle non-variables up front. */
6454 if (!VAR_P (decl))
6456 rest_of_decl_compilation (decl, toplev, at_eof);
6457 return;
6460 /* If we see a class member here, it should be a static data
6461 member. */
6462 if (DECL_LANG_SPECIFIC (decl) && DECL_IN_AGGR_P (decl))
6464 gcc_assert (TREE_STATIC (decl));
6465 /* An in-class declaration of a static data member should be
6466 external; it is only a declaration, and not a definition. */
6467 if (init == NULL_TREE)
6468 gcc_assert (DECL_EXTERNAL (decl)
6469 || !TREE_PUBLIC (decl)
6470 || DECL_INLINE_VAR_P (decl));
6473 /* We don't create any RTL for local variables. */
6474 if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
6475 return;
6477 /* We defer emission of local statics until the corresponding
6478 DECL_EXPR is expanded. But with constexpr its function might never
6479 be expanded, so go ahead and tell cgraph about the variable now. */
6480 defer_p = ((DECL_FUNCTION_SCOPE_P (decl)
6481 && !var_in_maybe_constexpr_fn (decl))
6482 || DECL_VIRTUAL_P (decl));
6484 /* Defer template instantiations. */
6485 if (DECL_LANG_SPECIFIC (decl)
6486 && DECL_IMPLICIT_INSTANTIATION (decl))
6487 defer_p = 1;
6489 /* If we're not deferring, go ahead and assemble the variable. */
6490 if (!defer_p)
6491 rest_of_decl_compilation (decl, toplev, at_eof);
6494 /* walk_tree helper for wrap_temporary_cleanups, below. */
6496 static tree
6497 wrap_cleanups_r (tree *stmt_p, int *walk_subtrees, void *data)
6499 /* Stop at types or full-expression boundaries. */
6500 if (TYPE_P (*stmt_p)
6501 || TREE_CODE (*stmt_p) == CLEANUP_POINT_EXPR)
6503 *walk_subtrees = 0;
6504 return NULL_TREE;
6507 if (TREE_CODE (*stmt_p) == TARGET_EXPR)
6509 tree guard = (tree)data;
6510 tree tcleanup = TARGET_EXPR_CLEANUP (*stmt_p);
6512 tcleanup = build2 (TRY_CATCH_EXPR, void_type_node, tcleanup, guard);
6513 /* Tell honor_protect_cleanup_actions to handle this as a separate
6514 cleanup. */
6515 TRY_CATCH_IS_CLEANUP (tcleanup) = 1;
6517 TARGET_EXPR_CLEANUP (*stmt_p) = tcleanup;
6520 return NULL_TREE;
6523 /* We're initializing a local variable which has a cleanup GUARD. If there
6524 are any temporaries used in the initializer INIT of this variable, we
6525 need to wrap their cleanups with TRY_CATCH_EXPR (, GUARD) so that the
6526 variable will be cleaned up properly if one of them throws.
6528 Unfortunately, there's no way to express this properly in terms of
6529 nesting, as the regions for the temporaries overlap the region for the
6530 variable itself; if there are two temporaries, the variable needs to be
6531 the first thing destroyed if either of them throws. However, we only
6532 want to run the variable's cleanup if it actually got constructed. So
6533 we need to guard the temporary cleanups with the variable's cleanup if
6534 they are run on the normal path, but not if they are run on the
6535 exceptional path. We implement this by telling
6536 honor_protect_cleanup_actions to strip the variable cleanup from the
6537 exceptional path. */
6539 static void
6540 wrap_temporary_cleanups (tree init, tree guard)
6542 cp_walk_tree_without_duplicates (&init, wrap_cleanups_r, (void *)guard);
6545 /* Generate code to initialize DECL (a local variable). */
6547 static void
6548 initialize_local_var (tree decl, tree init)
6550 tree type = TREE_TYPE (decl);
6551 tree cleanup;
6552 int already_used;
6554 gcc_assert (VAR_P (decl)
6555 || TREE_CODE (decl) == RESULT_DECL);
6556 gcc_assert (!TREE_STATIC (decl));
6558 if (DECL_SIZE (decl) == NULL_TREE)
6560 /* If we used it already as memory, it must stay in memory. */
6561 DECL_INITIAL (decl) = NULL_TREE;
6562 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
6563 return;
6566 if (type == error_mark_node)
6567 return;
6569 /* Compute and store the initial value. */
6570 already_used = TREE_USED (decl) || TREE_USED (type);
6571 if (TREE_USED (type))
6572 DECL_READ_P (decl) = 1;
6574 /* Generate a cleanup, if necessary. */
6575 cleanup = cxx_maybe_build_cleanup (decl, tf_warning_or_error);
6577 /* Perform the initialization. */
6578 if (init)
6580 tree rinit = (TREE_CODE (init) == INIT_EXPR
6581 ? TREE_OPERAND (init, 1) : NULL_TREE);
6582 if (rinit && !TREE_SIDE_EFFECTS (rinit))
6584 /* Stick simple initializers in DECL_INITIAL so that
6585 -Wno-init-self works (c++/34772). */
6586 gcc_assert (TREE_OPERAND (init, 0) == decl);
6587 DECL_INITIAL (decl) = rinit;
6589 if (warn_init_self && TREE_CODE (type) == REFERENCE_TYPE)
6591 STRIP_NOPS (rinit);
6592 if (rinit == decl)
6593 warning_at (DECL_SOURCE_LOCATION (decl),
6594 OPT_Winit_self,
6595 "reference %qD is initialized with itself", decl);
6598 else
6600 int saved_stmts_are_full_exprs_p;
6602 /* If we're only initializing a single object, guard the
6603 destructors of any temporaries used in its initializer with
6604 its destructor. This isn't right for arrays because each
6605 element initialization is a full-expression. */
6606 if (cleanup && TREE_CODE (type) != ARRAY_TYPE)
6607 wrap_temporary_cleanups (init, cleanup);
6609 gcc_assert (building_stmt_list_p ());
6610 saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
6611 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
6612 finish_expr_stmt (init);
6613 current_stmt_tree ()->stmts_are_full_exprs_p =
6614 saved_stmts_are_full_exprs_p;
6618 /* Set this to 0 so we can tell whether an aggregate which was
6619 initialized was ever used. Don't do this if it has a
6620 destructor, so we don't complain about the 'resource
6621 allocation is initialization' idiom. Now set
6622 attribute((unused)) on types so decls of that type will be
6623 marked used. (see TREE_USED, above.) */
6624 if (TYPE_NEEDS_CONSTRUCTING (type)
6625 && ! already_used
6626 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type)
6627 && DECL_NAME (decl))
6628 TREE_USED (decl) = 0;
6629 else if (already_used)
6630 TREE_USED (decl) = 1;
6632 if (cleanup)
6633 finish_decl_cleanup (decl, cleanup);
6636 /* DECL is a VAR_DECL for a compiler-generated variable with static
6637 storage duration (like a virtual table) whose initializer is a
6638 compile-time constant. Initialize the variable and provide it to the
6639 back end. */
6641 void
6642 initialize_artificial_var (tree decl, vec<constructor_elt, va_gc> *v)
6644 tree init;
6645 gcc_assert (DECL_ARTIFICIAL (decl));
6646 init = build_constructor (TREE_TYPE (decl), v);
6647 gcc_assert (TREE_CODE (init) == CONSTRUCTOR);
6648 DECL_INITIAL (decl) = init;
6649 DECL_INITIALIZED_P (decl) = 1;
6650 determine_visibility (decl);
6651 layout_var_decl (decl);
6652 maybe_commonize_var (decl);
6653 make_rtl_for_nonlocal_decl (decl, init, /*asmspec=*/NULL);
6656 /* INIT is the initializer for a variable, as represented by the
6657 parser. Returns true iff INIT is type-dependent. */
6659 static bool
6660 type_dependent_init_p (tree init)
6662 if (TREE_CODE (init) == TREE_LIST)
6663 /* A parenthesized initializer, e.g.: int i (3, 2); ? */
6664 return any_type_dependent_elements_p (init);
6665 else if (TREE_CODE (init) == CONSTRUCTOR)
6666 /* A brace-enclosed initializer, e.g.: int i = { 3 }; ? */
6668 if (dependent_type_p (TREE_TYPE (init)))
6669 return true;
6671 vec<constructor_elt, va_gc> *elts;
6672 size_t nelts;
6673 size_t i;
6675 elts = CONSTRUCTOR_ELTS (init);
6676 nelts = vec_safe_length (elts);
6677 for (i = 0; i < nelts; ++i)
6678 if (type_dependent_init_p ((*elts)[i].value))
6679 return true;
6681 else
6682 /* It must be a simple expression, e.g., int i = 3; */
6683 return type_dependent_expression_p (init);
6685 return false;
6688 /* INIT is the initializer for a variable, as represented by the
6689 parser. Returns true iff INIT is value-dependent. */
6691 static bool
6692 value_dependent_init_p (tree init)
6694 if (TREE_CODE (init) == TREE_LIST)
6695 /* A parenthesized initializer, e.g.: int i (3, 2); ? */
6696 return any_value_dependent_elements_p (init);
6697 else if (TREE_CODE (init) == CONSTRUCTOR)
6698 /* A brace-enclosed initializer, e.g.: int i = { 3 }; ? */
6700 vec<constructor_elt, va_gc> *elts;
6701 size_t nelts;
6702 size_t i;
6704 elts = CONSTRUCTOR_ELTS (init);
6705 nelts = vec_safe_length (elts);
6706 for (i = 0; i < nelts; ++i)
6707 if (value_dependent_init_p ((*elts)[i].value))
6708 return true;
6710 else
6711 /* It must be a simple expression, e.g., int i = 3; */
6712 return value_dependent_expression_p (init);
6714 return false;
6717 // Returns true if a DECL is VAR_DECL with the concept specifier.
6718 static inline bool
6719 is_concept_var (tree decl)
6721 return (VAR_P (decl)
6722 // Not all variables have DECL_LANG_SPECIFIC.
6723 && DECL_LANG_SPECIFIC (decl)
6724 && DECL_DECLARED_CONCEPT_P (decl));
6727 /* A helper function to be called via walk_tree. If any label exists
6728 under *TP, it is (going to be) forced. Set has_forced_label_in_static. */
6730 static tree
6731 notice_forced_label_r (tree *tp, int *walk_subtrees, void *)
6733 if (TYPE_P (*tp))
6734 *walk_subtrees = 0;
6735 if (TREE_CODE (*tp) == LABEL_DECL)
6736 cfun->has_forced_label_in_static = 1;
6737 return NULL_TREE;
6740 /* Finish processing of a declaration;
6741 install its line number and initial value.
6742 If the length of an array type is not known before,
6743 it must be determined now, from the initial value, or it is an error.
6745 INIT is the initializer (if any) for DECL. If INIT_CONST_EXPR_P is
6746 true, then INIT is an integral constant expression.
6748 FLAGS is LOOKUP_ONLYCONVERTING if the = init syntax was used, else 0
6749 if the (init) syntax was used. */
6751 void
6752 cp_finish_decl (tree decl, tree init, bool init_const_expr_p,
6753 tree asmspec_tree, int flags)
6755 tree type;
6756 vec<tree, va_gc> *cleanups = NULL;
6757 const char *asmspec = NULL;
6758 int was_readonly = 0;
6759 bool var_definition_p = false;
6760 tree auto_node;
6762 if (decl == error_mark_node)
6763 return;
6764 else if (! decl)
6766 if (init)
6767 error ("assignment (not initialization) in declaration");
6768 return;
6771 gcc_assert (TREE_CODE (decl) != RESULT_DECL);
6772 /* Parameters are handled by store_parm_decls, not cp_finish_decl. */
6773 gcc_assert (TREE_CODE (decl) != PARM_DECL);
6775 type = TREE_TYPE (decl);
6776 if (type == error_mark_node)
6777 return;
6779 /* Warn about register storage specifiers except when in GNU global
6780 or local register variable extension. */
6781 if (VAR_P (decl) && DECL_REGISTER (decl) && asmspec_tree == NULL_TREE)
6783 if (cxx_dialect >= cxx1z)
6784 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
6785 "ISO C++1z does not allow %<register%> storage "
6786 "class specifier");
6787 else
6788 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
6789 "%<register%> storage class specifier used");
6792 /* If a name was specified, get the string. */
6793 if (at_namespace_scope_p ())
6794 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
6795 if (asmspec_tree && asmspec_tree != error_mark_node)
6796 asmspec = TREE_STRING_POINTER (asmspec_tree);
6798 if (current_class_type
6799 && CP_DECL_CONTEXT (decl) == current_class_type
6800 && TYPE_BEING_DEFINED (current_class_type)
6801 && !CLASSTYPE_TEMPLATE_INSTANTIATION (current_class_type)
6802 && (DECL_INITIAL (decl) || init))
6803 DECL_INITIALIZED_IN_CLASS_P (decl) = 1;
6805 if (TREE_CODE (decl) != FUNCTION_DECL
6806 && (auto_node = type_uses_auto (type)))
6808 tree d_init;
6809 if (init == NULL_TREE)
6811 if (DECL_LANG_SPECIFIC (decl)
6812 && DECL_TEMPLATE_INSTANTIATION (decl)
6813 && !DECL_TEMPLATE_INSTANTIATED (decl))
6815 /* init is null because we're deferring instantiating the
6816 initializer until we need it. Well, we need it now. */
6817 instantiate_decl (decl, /*defer_ok*/true, /*expl*/false);
6818 return;
6821 gcc_assert (CLASS_PLACEHOLDER_TEMPLATE (auto_node));
6823 d_init = init;
6824 if (d_init)
6826 if (TREE_CODE (d_init) == TREE_LIST
6827 && !CLASS_PLACEHOLDER_TEMPLATE (auto_node))
6828 d_init = build_x_compound_expr_from_list (d_init, ELK_INIT,
6829 tf_warning_or_error);
6830 d_init = resolve_nondeduced_context (d_init, tf_warning_or_error);
6832 enum auto_deduction_context adc = adc_variable_type;
6833 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
6834 adc = adc_decomp_type;
6835 type = TREE_TYPE (decl) = do_auto_deduction (type, d_init, auto_node,
6836 tf_warning_or_error, adc,
6837 NULL_TREE, flags);
6838 if (type == error_mark_node)
6839 return;
6840 if (TREE_CODE (type) == FUNCTION_TYPE)
6842 error ("initializer for %<decltype(auto) %D%> has function type "
6843 "(did you forget the %<()%> ?)", decl);
6844 TREE_TYPE (decl) = error_mark_node;
6845 return;
6847 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
6850 if (!ensure_literal_type_for_constexpr_object (decl))
6851 DECL_DECLARED_CONSTEXPR_P (decl) = 0;
6853 if (VAR_P (decl)
6854 && DECL_CLASS_SCOPE_P (decl)
6855 && DECL_INITIALIZED_IN_CLASS_P (decl))
6856 check_static_variable_definition (decl, type);
6858 if (init && TREE_CODE (decl) == FUNCTION_DECL)
6860 tree clone;
6861 if (init == ridpointers[(int)RID_DELETE])
6863 /* FIXME check this is 1st decl. */
6864 DECL_DELETED_FN (decl) = 1;
6865 DECL_DECLARED_INLINE_P (decl) = 1;
6866 DECL_INITIAL (decl) = error_mark_node;
6867 FOR_EACH_CLONE (clone, decl)
6869 DECL_DELETED_FN (clone) = 1;
6870 DECL_DECLARED_INLINE_P (clone) = 1;
6871 DECL_INITIAL (clone) = error_mark_node;
6873 init = NULL_TREE;
6875 else if (init == ridpointers[(int)RID_DEFAULT])
6877 if (defaultable_fn_check (decl))
6878 DECL_DEFAULTED_FN (decl) = 1;
6879 else
6880 DECL_INITIAL (decl) = NULL_TREE;
6884 if (init && VAR_P (decl))
6886 DECL_NONTRIVIALLY_INITIALIZED_P (decl) = 1;
6887 /* If DECL is a reference, then we want to know whether init is a
6888 reference constant; init_const_expr_p as passed tells us whether
6889 it's an rvalue constant. */
6890 if (TREE_CODE (type) == REFERENCE_TYPE)
6891 init_const_expr_p = potential_constant_expression (init);
6892 if (init_const_expr_p)
6894 /* Set these flags now for templates. We'll update the flags in
6895 store_init_value for instantiations. */
6896 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
6897 if (decl_maybe_constant_var_p (decl)
6898 /* FIXME setting TREE_CONSTANT on refs breaks the back end. */
6899 && TREE_CODE (type) != REFERENCE_TYPE)
6900 TREE_CONSTANT (decl) = 1;
6904 if (processing_template_decl)
6906 bool type_dependent_p;
6908 /* Add this declaration to the statement-tree. */
6909 if (at_function_scope_p ())
6910 add_decl_expr (decl);
6912 type_dependent_p = dependent_type_p (type);
6914 if (check_for_bare_parameter_packs (init))
6916 init = NULL_TREE;
6917 DECL_INITIAL (decl) = NULL_TREE;
6920 /* Generally, initializers in templates are expanded when the
6921 template is instantiated. But, if DECL is a variable constant
6922 then it can be used in future constant expressions, so its value
6923 must be available. */
6925 if (!VAR_P (decl) || type_dependent_p)
6926 /* We can't do anything if the decl has dependent type. */;
6927 else if (init
6928 && init_const_expr_p
6929 && TREE_CODE (type) != REFERENCE_TYPE
6930 && decl_maybe_constant_var_p (decl)
6931 && !type_dependent_init_p (init)
6932 && !value_dependent_init_p (init))
6934 /* This variable seems to be a non-dependent constant, so process
6935 its initializer. If check_initializer returns non-null the
6936 initialization wasn't constant after all. */
6937 tree init_code;
6938 cleanups = make_tree_vector ();
6939 init_code = check_initializer (decl, init, flags, &cleanups);
6940 if (init_code == NULL_TREE)
6941 init = NULL_TREE;
6942 release_tree_vector (cleanups);
6944 else if (!init && is_concept_var (decl))
6945 error ("variable concept has no initializer");
6946 else if (!DECL_PRETTY_FUNCTION_P (decl))
6948 /* Deduce array size even if the initializer is dependent. */
6949 maybe_deduce_size_from_array_init (decl, init);
6950 /* And complain about multiple initializers. */
6951 if (init && TREE_CODE (init) == TREE_LIST && TREE_CHAIN (init)
6952 && !MAYBE_CLASS_TYPE_P (type))
6953 init = build_x_compound_expr_from_list (init, ELK_INIT,
6954 tf_warning_or_error);
6957 if (init)
6958 DECL_INITIAL (decl) = init;
6959 return;
6962 /* Just store non-static data member initializers for later. */
6963 if (init && TREE_CODE (decl) == FIELD_DECL)
6964 DECL_INITIAL (decl) = init;
6966 /* Take care of TYPE_DECLs up front. */
6967 if (TREE_CODE (decl) == TYPE_DECL)
6969 if (type != error_mark_node
6970 && MAYBE_CLASS_TYPE_P (type) && DECL_NAME (decl))
6972 if (TREE_TYPE (DECL_NAME (decl)) && TREE_TYPE (decl) != type)
6973 warning (0, "shadowing previous type declaration of %q#D", decl);
6974 set_identifier_type_value (DECL_NAME (decl), decl);
6977 /* If we have installed this as the canonical typedef for this
6978 type, and that type has not been defined yet, delay emitting
6979 the debug information for it, as we will emit it later. */
6980 if (TYPE_MAIN_DECL (TREE_TYPE (decl)) == decl
6981 && !COMPLETE_TYPE_P (TREE_TYPE (decl)))
6982 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
6984 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl),
6985 at_eof);
6986 return;
6989 /* A reference will be modified here, as it is initialized. */
6990 if (! DECL_EXTERNAL (decl)
6991 && TREE_READONLY (decl)
6992 && TREE_CODE (type) == REFERENCE_TYPE)
6994 was_readonly = 1;
6995 TREE_READONLY (decl) = 0;
6998 if (VAR_P (decl))
7000 /* If this is a local variable that will need a mangled name,
7001 register it now. We must do this before processing the
7002 initializer for the variable, since the initialization might
7003 require a guard variable, and since the mangled name of the
7004 guard variable will depend on the mangled name of this
7005 variable. */
7006 if (DECL_FUNCTION_SCOPE_P (decl)
7007 && TREE_STATIC (decl)
7008 && !DECL_ARTIFICIAL (decl))
7010 push_local_name (decl);
7011 /* Normally has_forced_label_in_static is set during GIMPLE
7012 lowering, but [cd]tors are never actually compiled directly.
7013 We need to set this early so we can deal with the label
7014 address extension. */
7015 if ((DECL_CONSTRUCTOR_P (current_function_decl)
7016 || DECL_DESTRUCTOR_P (current_function_decl))
7017 && init)
7019 walk_tree (&init, notice_forced_label_r, NULL, NULL);
7020 add_local_decl (cfun, decl);
7022 /* And make sure it's in the symbol table for
7023 c_parse_final_cleanups to find. */
7024 varpool_node::get_create (decl);
7027 /* Convert the initializer to the type of DECL, if we have not
7028 already initialized DECL. */
7029 if (!DECL_INITIALIZED_P (decl)
7030 /* If !DECL_EXTERNAL then DECL is being defined. In the
7031 case of a static data member initialized inside the
7032 class-specifier, there can be an initializer even if DECL
7033 is *not* defined. */
7034 && (!DECL_EXTERNAL (decl) || init))
7036 cleanups = make_tree_vector ();
7037 init = check_initializer (decl, init, flags, &cleanups);
7039 /* Handle:
7041 [dcl.init]
7043 The memory occupied by any object of static storage
7044 duration is zero-initialized at program startup before
7045 any other initialization takes place.
7047 We cannot create an appropriate initializer until after
7048 the type of DECL is finalized. If DECL_INITIAL is set,
7049 then the DECL is statically initialized, and any
7050 necessary zero-initialization has already been performed. */
7051 if (TREE_STATIC (decl) && !DECL_INITIAL (decl))
7052 DECL_INITIAL (decl) = build_zero_init (TREE_TYPE (decl),
7053 /*nelts=*/NULL_TREE,
7054 /*static_storage_p=*/true);
7055 /* Remember that the initialization for this variable has
7056 taken place. */
7057 DECL_INITIALIZED_P (decl) = 1;
7058 /* This declaration is the definition of this variable,
7059 unless we are initializing a static data member within
7060 the class specifier. */
7061 if (!DECL_EXTERNAL (decl))
7062 var_definition_p = true;
7064 /* If the variable has an array type, lay out the type, even if
7065 there is no initializer. It is valid to index through the
7066 array, and we must get TYPE_ALIGN set correctly on the array
7067 type. */
7068 else if (TREE_CODE (type) == ARRAY_TYPE)
7069 layout_type (type);
7071 if (TREE_STATIC (decl)
7072 && !at_function_scope_p ()
7073 && current_function_decl == NULL)
7074 /* So decl is a global variable or a static member of a
7075 non local class. Record the types it uses
7076 so that we can decide later to emit debug info for them. */
7077 record_types_used_by_current_var_decl (decl);
7080 /* Add this declaration to the statement-tree. This needs to happen
7081 after the call to check_initializer so that the DECL_EXPR for a
7082 reference temp is added before the DECL_EXPR for the reference itself. */
7083 if (DECL_FUNCTION_SCOPE_P (decl))
7085 /* If we're building a variable sized type, and we might be
7086 reachable other than via the top of the current binding
7087 level, then create a new BIND_EXPR so that we deallocate
7088 the object at the right time. */
7089 if (VAR_P (decl)
7090 && DECL_SIZE (decl)
7091 && !TREE_CONSTANT (DECL_SIZE (decl))
7092 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
7094 tree bind;
7095 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
7096 TREE_SIDE_EFFECTS (bind) = 1;
7097 add_stmt (bind);
7098 BIND_EXPR_BODY (bind) = push_stmt_list ();
7100 add_decl_expr (decl);
7103 /* Let the middle end know about variables and functions -- but not
7104 static data members in uninstantiated class templates. */
7105 if (VAR_OR_FUNCTION_DECL_P (decl))
7107 if (VAR_P (decl))
7109 layout_var_decl (decl);
7110 maybe_commonize_var (decl);
7113 /* This needs to happen after the linkage is set. */
7114 determine_visibility (decl);
7116 if (var_definition_p && TREE_STATIC (decl))
7118 /* If a TREE_READONLY variable needs initialization
7119 at runtime, it is no longer readonly and we need to
7120 avoid MEM_READONLY_P being set on RTL created for it. */
7121 if (init)
7123 if (TREE_READONLY (decl))
7124 TREE_READONLY (decl) = 0;
7125 was_readonly = 0;
7127 else if (was_readonly)
7128 TREE_READONLY (decl) = 1;
7130 /* Likewise if it needs destruction. */
7131 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
7132 TREE_READONLY (decl) = 0;
7135 make_rtl_for_nonlocal_decl (decl, init, asmspec);
7137 /* Check for abstractness of the type. Notice that there is no
7138 need to strip array types here since the check for those types
7139 is already done within create_array_type_for_decl. */
7140 abstract_virtuals_error (decl, type);
7142 if (TREE_TYPE (decl) == error_mark_node)
7143 /* No initialization required. */
7145 else if (TREE_CODE (decl) == FUNCTION_DECL)
7147 if (init)
7149 if (init == ridpointers[(int)RID_DEFAULT])
7151 /* An out-of-class default definition is defined at
7152 the point where it is explicitly defaulted. */
7153 if (DECL_DELETED_FN (decl))
7154 maybe_explain_implicit_delete (decl);
7155 else if (DECL_INITIAL (decl) == error_mark_node)
7156 synthesize_method (decl);
7158 else
7159 error ("function %q#D is initialized like a variable", decl);
7161 /* else no initialization required. */
7163 else if (DECL_EXTERNAL (decl)
7164 && ! (DECL_LANG_SPECIFIC (decl)
7165 && DECL_NOT_REALLY_EXTERN (decl)))
7167 if (init)
7168 DECL_INITIAL (decl) = init;
7170 /* A variable definition. */
7171 else if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
7172 /* Initialize the local variable. */
7173 initialize_local_var (decl, init);
7175 /* If a variable is defined, and then a subsequent
7176 definition with external linkage is encountered, we will
7177 get here twice for the same variable. We want to avoid
7178 calling expand_static_init more than once. For variables
7179 that are not static data members, we can call
7180 expand_static_init only when we actually process the
7181 initializer. It is not legal to redeclare a static data
7182 member, so this issue does not arise in that case. */
7183 else if (var_definition_p && TREE_STATIC (decl))
7184 expand_static_init (decl, init);
7187 /* If a CLEANUP_STMT was created to destroy a temporary bound to a
7188 reference, insert it in the statement-tree now. */
7189 if (cleanups)
7191 unsigned i; tree t;
7192 FOR_EACH_VEC_ELT (*cleanups, i, t)
7193 push_cleanup (decl, t, false);
7194 release_tree_vector (cleanups);
7197 if (was_readonly)
7198 TREE_READONLY (decl) = 1;
7200 invoke_plugin_callbacks (PLUGIN_FINISH_DECL, decl);
7203 /* For class TYPE return itself or some its bases that contain
7204 any direct non-static data members. Return error_mark_node if an
7205 error has been diagnosed. */
7207 static tree
7208 find_decomp_class_base (location_t loc, tree type, tree ret)
7210 bool member_seen = false;
7211 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
7212 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
7213 continue;
7214 else if (ret)
7215 return type;
7216 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
7218 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE)
7219 error_at (loc, "cannot decompose class type %qT because it has an "
7220 "anonymous struct member", type);
7221 else
7222 error_at (loc, "cannot decompose class type %qT because it has an "
7223 "anonymous union member", type);
7224 inform (DECL_SOURCE_LOCATION (field), "declared here");
7225 return error_mark_node;
7227 else if (TREE_PRIVATE (field) || TREE_PROTECTED (field))
7229 error_at (loc, "cannot decompose non-public member %qD of %qT",
7230 field, type);
7231 inform (DECL_SOURCE_LOCATION (field),
7232 TREE_PRIVATE (field)
7233 ? G_("declared private here")
7234 : G_("declared protected here"));
7235 return error_mark_node;
7237 else
7238 member_seen = true;
7240 tree base_binfo, binfo;
7241 tree orig_ret = ret;
7242 int i;
7243 if (member_seen)
7244 ret = type;
7245 for (binfo = TYPE_BINFO (type), i = 0;
7246 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
7248 tree t = find_decomp_class_base (loc, TREE_TYPE (base_binfo), ret);
7249 if (t == error_mark_node)
7250 return error_mark_node;
7251 if (t != NULL_TREE)
7253 if (ret == type)
7255 error_at (loc, "cannot decompose class type %qT: both it and "
7256 "its base class %qT have non-static data members",
7257 type, t);
7258 return error_mark_node;
7260 else if (orig_ret != NULL_TREE)
7261 return t;
7262 else if (ret == t)
7263 /* OK, found the same base along another path. We'll complain
7264 in convert_to_base if it's ambiguous. */;
7265 else if (ret != NULL_TREE)
7267 error_at (loc, "cannot decompose class type %qT: its base "
7268 "classes %qT and %qT have non-static data "
7269 "members", type, ret, t);
7270 return error_mark_node;
7272 else
7273 ret = t;
7276 return ret;
7279 /* Return std::tuple_size<TYPE>::value. */
7281 static tree
7282 get_tuple_size (tree type)
7284 tree args = make_tree_vec (1);
7285 TREE_VEC_ELT (args, 0) = type;
7286 tree inst = lookup_template_class (get_identifier ("tuple_size"), args,
7287 /*in_decl*/NULL_TREE,
7288 /*context*/std_node,
7289 /*entering_scope*/false, tf_none);
7290 inst = complete_type (inst);
7291 if (inst == error_mark_node || !COMPLETE_TYPE_P (inst))
7292 return NULL_TREE;
7293 tree val = lookup_qualified_name (inst, get_identifier ("value"),
7294 /*type*/false, /*complain*/false);
7295 if (TREE_CODE (val) == VAR_DECL || TREE_CODE (val) == CONST_DECL)
7296 val = maybe_constant_value (val);
7297 if (TREE_CODE (val) == INTEGER_CST)
7298 return val;
7299 else
7300 return error_mark_node;
7303 /* Return std::tuple_element<I,TYPE>::type. */
7305 static tree
7306 get_tuple_element_type (tree type, unsigned i)
7308 tree args = make_tree_vec (2);
7309 TREE_VEC_ELT (args, 0) = build_int_cst (integer_type_node, i);
7310 TREE_VEC_ELT (args, 1) = type;
7311 tree inst = lookup_template_class (get_identifier ("tuple_element"), args,
7312 /*in_decl*/NULL_TREE,
7313 /*context*/std_node,
7314 /*entering_scope*/false,
7315 tf_warning_or_error);
7316 return make_typename_type (inst, get_identifier ("type"),
7317 none_type, tf_warning_or_error);
7320 /* Return e.get<i>() or get<i>(e). */
7322 static tree
7323 get_tuple_decomp_init (tree decl, unsigned i)
7325 tree get_id = get_identifier ("get");
7326 tree targs = make_tree_vec (1);
7327 TREE_VEC_ELT (targs, 0) = build_int_cst (integer_type_node, i);
7329 tree etype = TREE_TYPE (decl);
7330 tree e = convert_from_reference (decl);
7332 /* [The id-expression] e is an lvalue if the type of the entity e is an
7333 lvalue reference and an xvalue otherwise. */
7334 if (TREE_CODE (etype) != REFERENCE_TYPE
7335 || TYPE_REF_IS_RVALUE (etype))
7336 e = move (e);
7338 tree fns = lookup_qualified_name (TREE_TYPE (e), get_id,
7339 /*type*/false, /*complain*/false);
7340 if (fns != error_mark_node)
7342 fns = lookup_template_function (fns, targs);
7343 return build_new_method_call (e, fns, /*args*/NULL,
7344 /*path*/NULL_TREE, LOOKUP_NORMAL,
7345 /*fn_p*/NULL, tf_warning_or_error);
7347 else
7349 vec<tree,va_gc> *args = make_tree_vector_single (e);
7350 fns = lookup_template_function (get_id, targs);
7351 fns = perform_koenig_lookup (fns, args, tf_warning_or_error);
7352 return finish_call_expr (fns, &args, /*novirt*/false,
7353 /*koenig*/true, tf_warning_or_error);
7357 /* It's impossible to recover the decltype of a tuple decomposition variable
7358 based on the actual type of the variable, so store it in a hash table. */
7359 static GTY(()) hash_map<tree,tree> *decomp_type_table;
7360 static void
7361 store_decomp_type (tree v, tree t)
7363 if (!decomp_type_table)
7364 decomp_type_table = hash_map<tree,tree>::create_ggc (13);
7365 decomp_type_table->put (v, t);
7368 tree
7369 lookup_decomp_type (tree v)
7371 return *decomp_type_table->get (v);
7374 /* Finish a decomposition declaration. DECL is the underlying declaration
7375 "e", FIRST is the head of a chain of decls for the individual identifiers
7376 chained through DECL_CHAIN in reverse order and COUNT is the number of
7377 those decls. */
7379 void
7380 cp_finish_decomp (tree decl, tree first, unsigned int count)
7382 if (error_operand_p (decl))
7384 error_out:
7385 while (count--)
7387 TREE_TYPE (first) = error_mark_node;
7388 if (DECL_HAS_VALUE_EXPR_P (first))
7390 SET_DECL_VALUE_EXPR (first, NULL_TREE);
7391 DECL_HAS_VALUE_EXPR_P (first) = 0;
7393 first = DECL_CHAIN (first);
7395 if (DECL_P (decl) && DECL_NAMESPACE_SCOPE_P (decl))
7396 SET_DECL_ASSEMBLER_NAME (decl, get_identifier ("<decomp>"));
7397 return;
7400 location_t loc = DECL_SOURCE_LOCATION (decl);
7401 if (type_dependent_expression_p (decl)
7402 /* This happens for range for when not in templates.
7403 Still add the DECL_VALUE_EXPRs for later processing. */
7404 || (!processing_template_decl
7405 && type_uses_auto (TREE_TYPE (decl))))
7407 for (unsigned int i = 0; i < count; i++)
7409 if (!DECL_HAS_VALUE_EXPR_P (first))
7411 tree v = build_nt (ARRAY_REF, decl,
7412 size_int (count - i - 1),
7413 NULL_TREE, NULL_TREE);
7414 SET_DECL_VALUE_EXPR (first, v);
7415 DECL_HAS_VALUE_EXPR_P (first) = 1;
7417 if (processing_template_decl)
7419 retrofit_lang_decl (first);
7420 SET_DECL_DECOMPOSITION_P (first);
7422 first = DECL_CHAIN (first);
7424 return;
7427 auto_vec<tree, 16> v;
7428 v.safe_grow (count);
7429 tree d = first;
7430 for (unsigned int i = 0; i < count; i++, d = DECL_CHAIN (d))
7432 v[count - i - 1] = d;
7433 retrofit_lang_decl (d);
7434 SET_DECL_DECOMPOSITION_P (d);
7437 tree type = TREE_TYPE (decl);
7438 tree dexp = decl;
7440 if (TREE_CODE (type) == REFERENCE_TYPE)
7442 /* If e is a constant reference, use the referent directly. */
7443 if (DECL_INITIAL (decl))
7444 dexp = DECL_INITIAL (decl);
7445 dexp = convert_from_reference (dexp);
7446 type = TREE_TYPE (type);
7449 tree eltype = NULL_TREE;
7450 unsigned HOST_WIDE_INT eltscnt = 0;
7451 if (TREE_CODE (type) == ARRAY_TYPE)
7453 tree nelts;
7454 nelts = array_type_nelts_top (type);
7455 if (nelts == error_mark_node)
7456 goto error_out;
7457 if (!tree_fits_uhwi_p (nelts))
7459 error_at (loc, "cannot decompose variable length array %qT", type);
7460 goto error_out;
7462 eltscnt = tree_to_uhwi (nelts);
7463 if (count != eltscnt)
7465 cnt_mismatch:
7466 if (count > eltscnt)
7467 error_at (loc, "%u names provided while %qT decomposes into "
7468 "%wu elements", count, type, eltscnt);
7469 else
7470 error_at (loc, "only %u names provided while %qT decomposes into "
7471 "%wu elements", count, type, eltscnt);
7472 goto error_out;
7474 eltype = TREE_TYPE (type);
7475 for (unsigned int i = 0; i < count; i++)
7477 TREE_TYPE (v[i]) = eltype;
7478 layout_decl (v[i], 0);
7479 tree t = unshare_expr (dexp);
7480 t = build4_loc (DECL_SOURCE_LOCATION (v[i]), ARRAY_REF,
7481 eltype, t, size_int (i), NULL_TREE,
7482 NULL_TREE);
7483 SET_DECL_VALUE_EXPR (v[i], t);
7484 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
7487 /* 2 GNU extensions. */
7488 else if (TREE_CODE (type) == COMPLEX_TYPE)
7490 eltscnt = 2;
7491 if (count != eltscnt)
7492 goto cnt_mismatch;
7493 eltype = cp_build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
7494 for (unsigned int i = 0; i < count; i++)
7496 TREE_TYPE (v[i]) = eltype;
7497 layout_decl (v[i], 0);
7498 tree t = unshare_expr (dexp);
7499 t = build1_loc (DECL_SOURCE_LOCATION (v[i]),
7500 i ? IMAGPART_EXPR : REALPART_EXPR, eltype,
7502 SET_DECL_VALUE_EXPR (v[i], t);
7503 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
7506 else if (TREE_CODE (type) == VECTOR_TYPE)
7508 eltscnt = TYPE_VECTOR_SUBPARTS (type);
7509 if (count != eltscnt)
7510 goto cnt_mismatch;
7511 eltype = cp_build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
7512 for (unsigned int i = 0; i < count; i++)
7514 TREE_TYPE (v[i]) = eltype;
7515 layout_decl (v[i], 0);
7516 tree t = unshare_expr (dexp);
7517 convert_vector_to_array_for_subscript (DECL_SOURCE_LOCATION (v[i]),
7518 &t, size_int (i));
7519 t = build4_loc (DECL_SOURCE_LOCATION (v[i]), ARRAY_REF,
7520 eltype, t, size_int (i), NULL_TREE,
7521 NULL_TREE);
7522 SET_DECL_VALUE_EXPR (v[i], t);
7523 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
7526 else if (tree tsize = get_tuple_size (type))
7528 if (tsize == error_mark_node)
7530 error_at (loc, "%<std::tuple_size<%T>::value%> is not an integral "
7531 "constant expression", type);
7532 goto error_out;
7534 eltscnt = tree_to_uhwi (tsize);
7535 if (count != eltscnt)
7536 goto cnt_mismatch;
7537 for (unsigned i = 0; i < count; ++i)
7539 location_t sloc = input_location;
7540 location_t dloc = DECL_SOURCE_LOCATION (v[i]);
7542 input_location = dloc;
7543 tree init = get_tuple_decomp_init (decl, i);
7544 tree eltype = (init == error_mark_node ? error_mark_node
7545 : get_tuple_element_type (type, i));
7546 input_location = sloc;
7548 if (init == error_mark_node || eltype == error_mark_node)
7550 inform (dloc, "in initialization of decomposition variable %qD",
7551 v[i]);
7552 goto error_out;
7554 /* Save the decltype away before reference collapse. */
7555 store_decomp_type (v[i], eltype);
7556 eltype = cp_build_reference_type (eltype, !lvalue_p (init));
7557 TREE_TYPE (v[i]) = eltype;
7558 layout_decl (v[i], 0);
7559 if (DECL_HAS_VALUE_EXPR_P (v[i]))
7561 /* In this case the names are variables, not just proxies. */
7562 SET_DECL_VALUE_EXPR (v[i], NULL_TREE);
7563 DECL_HAS_VALUE_EXPR_P (v[i]) = 0;
7565 cp_finish_decl (v[i], init, /*constexpr*/false,
7566 /*asm*/NULL_TREE, LOOKUP_NORMAL);
7569 else if (TREE_CODE (type) == UNION_TYPE)
7571 error_at (loc, "cannot decompose union type %qT", type);
7572 goto error_out;
7574 else if (!CLASS_TYPE_P (type))
7576 error_at (loc, "cannot decompose non-array non-class type %qT", type);
7577 goto error_out;
7579 else if (LAMBDA_TYPE_P (type))
7581 error_at (loc, "cannot decompose lambda closure type %qT", type);
7582 goto error_out;
7584 else
7586 tree btype = find_decomp_class_base (loc, type, NULL_TREE);
7587 if (btype == error_mark_node)
7588 goto error_out;
7589 else if (btype == NULL_TREE)
7591 error_at (loc, "cannot decompose class type %qT without non-static "
7592 "data members", type);
7593 goto error_out;
7595 for (tree field = TYPE_FIELDS (btype); field; field = TREE_CHAIN (field))
7596 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
7597 continue;
7598 else
7599 eltscnt++;
7600 if (count != eltscnt)
7601 goto cnt_mismatch;
7602 tree t = dexp;
7603 if (type != btype)
7605 t = convert_to_base (t, btype, /*check_access*/true,
7606 /*nonnull*/false, tf_warning_or_error);
7607 type = btype;
7609 unsigned int i = 0;
7610 for (tree field = TYPE_FIELDS (btype); field; field = TREE_CHAIN (field))
7611 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
7612 continue;
7613 else
7615 tree tt = finish_non_static_data_member (field, unshare_expr (t),
7616 NULL_TREE);
7617 if (REFERENCE_REF_P (tt))
7618 tt = TREE_OPERAND (tt, 0);
7619 TREE_TYPE (v[i]) = TREE_TYPE (tt);
7620 layout_decl (v[i], 0);
7621 SET_DECL_VALUE_EXPR (v[i], tt);
7622 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
7623 i++;
7626 if (DECL_NAMESPACE_SCOPE_P (decl))
7627 SET_DECL_ASSEMBLER_NAME (decl, mangle_decomp (decl, v));
7630 /* Returns a declaration for a VAR_DECL as if:
7632 extern "C" TYPE NAME;
7634 had been seen. Used to create compiler-generated global
7635 variables. */
7637 static tree
7638 declare_global_var (tree name, tree type)
7640 tree decl;
7642 push_to_top_level ();
7643 decl = build_decl (input_location, VAR_DECL, name, type);
7644 TREE_PUBLIC (decl) = 1;
7645 DECL_EXTERNAL (decl) = 1;
7646 DECL_ARTIFICIAL (decl) = 1;
7647 /* If the user has explicitly declared this variable (perhaps
7648 because the code we are compiling is part of a low-level runtime
7649 library), then it is possible that our declaration will be merged
7650 with theirs by pushdecl. */
7651 decl = pushdecl (decl);
7652 cp_finish_decl (decl, NULL_TREE, false, NULL_TREE, 0);
7653 pop_from_top_level ();
7655 return decl;
7658 /* Returns the type for the argument to "__cxa_atexit" (or "atexit",
7659 if "__cxa_atexit" is not being used) corresponding to the function
7660 to be called when the program exits. */
7662 static tree
7663 get_atexit_fn_ptr_type (void)
7665 tree fn_type;
7667 if (!atexit_fn_ptr_type_node)
7669 tree arg_type;
7670 if (flag_use_cxa_atexit
7671 && !targetm.cxx.use_atexit_for_cxa_atexit ())
7672 /* The parameter to "__cxa_atexit" is "void (*)(void *)". */
7673 arg_type = ptr_type_node;
7674 else
7675 /* The parameter to "atexit" is "void (*)(void)". */
7676 arg_type = NULL_TREE;
7678 fn_type = build_function_type_list (void_type_node,
7679 arg_type, NULL_TREE);
7680 atexit_fn_ptr_type_node = build_pointer_type (fn_type);
7683 return atexit_fn_ptr_type_node;
7686 /* Returns a pointer to the `atexit' function. Note that if
7687 FLAG_USE_CXA_ATEXIT is nonzero, then this will actually be the new
7688 `__cxa_atexit' function specified in the IA64 C++ ABI. */
7690 static tree
7691 get_atexit_node (void)
7693 tree atexit_fndecl;
7694 tree fn_type;
7695 tree fn_ptr_type;
7696 const char *name;
7697 bool use_aeabi_atexit;
7699 if (atexit_node)
7700 return atexit_node;
7702 if (flag_use_cxa_atexit && !targetm.cxx.use_atexit_for_cxa_atexit ())
7704 /* The declaration for `__cxa_atexit' is:
7706 int __cxa_atexit (void (*)(void *), void *, void *)
7708 We build up the argument types and then the function type
7709 itself. */
7710 tree argtype0, argtype1, argtype2;
7712 use_aeabi_atexit = targetm.cxx.use_aeabi_atexit ();
7713 /* First, build the pointer-to-function type for the first
7714 argument. */
7715 fn_ptr_type = get_atexit_fn_ptr_type ();
7716 /* Then, build the rest of the argument types. */
7717 argtype2 = ptr_type_node;
7718 if (use_aeabi_atexit)
7720 argtype1 = fn_ptr_type;
7721 argtype0 = ptr_type_node;
7723 else
7725 argtype1 = ptr_type_node;
7726 argtype0 = fn_ptr_type;
7728 /* And the final __cxa_atexit type. */
7729 fn_type = build_function_type_list (integer_type_node,
7730 argtype0, argtype1, argtype2,
7731 NULL_TREE);
7732 if (use_aeabi_atexit)
7733 name = "__aeabi_atexit";
7734 else
7735 name = "__cxa_atexit";
7737 else
7739 /* The declaration for `atexit' is:
7741 int atexit (void (*)());
7743 We build up the argument types and then the function type
7744 itself. */
7745 fn_ptr_type = get_atexit_fn_ptr_type ();
7746 /* Build the final atexit type. */
7747 fn_type = build_function_type_list (integer_type_node,
7748 fn_ptr_type, NULL_TREE);
7749 name = "atexit";
7752 /* Now, build the function declaration. */
7753 push_lang_context (lang_name_c);
7754 atexit_fndecl = build_library_fn_ptr (name, fn_type, ECF_LEAF | ECF_NOTHROW);
7755 mark_used (atexit_fndecl);
7756 pop_lang_context ();
7757 atexit_node = decay_conversion (atexit_fndecl, tf_warning_or_error);
7759 return atexit_node;
7762 /* Like get_atexit_node, but for thread-local cleanups. */
7764 static tree
7765 get_thread_atexit_node (void)
7767 /* The declaration for `__cxa_thread_atexit' is:
7769 int __cxa_thread_atexit (void (*)(void *), void *, void *) */
7770 tree fn_type = build_function_type_list (integer_type_node,
7771 get_atexit_fn_ptr_type (),
7772 ptr_type_node, ptr_type_node,
7773 NULL_TREE);
7775 /* Now, build the function declaration. */
7776 tree atexit_fndecl = build_library_fn_ptr ("__cxa_thread_atexit", fn_type,
7777 ECF_LEAF | ECF_NOTHROW);
7778 return decay_conversion (atexit_fndecl, tf_warning_or_error);
7781 /* Returns the __dso_handle VAR_DECL. */
7783 static tree
7784 get_dso_handle_node (void)
7786 if (dso_handle_node)
7787 return dso_handle_node;
7789 /* Declare the variable. */
7790 dso_handle_node = declare_global_var (get_identifier ("__dso_handle"),
7791 ptr_type_node);
7793 #ifdef HAVE_GAS_HIDDEN
7794 if (dso_handle_node != error_mark_node)
7796 DECL_VISIBILITY (dso_handle_node) = VISIBILITY_HIDDEN;
7797 DECL_VISIBILITY_SPECIFIED (dso_handle_node) = 1;
7799 #endif
7801 return dso_handle_node;
7804 /* Begin a new function with internal linkage whose job will be simply
7805 to destroy some particular variable. */
7807 static GTY(()) int start_cleanup_cnt;
7809 static tree
7810 start_cleanup_fn (void)
7812 char name[32];
7813 tree fntype;
7814 tree fndecl;
7815 bool use_cxa_atexit = flag_use_cxa_atexit
7816 && !targetm.cxx.use_atexit_for_cxa_atexit ();
7818 push_to_top_level ();
7820 /* No need to mangle this. */
7821 push_lang_context (lang_name_c);
7823 /* Build the name of the function. */
7824 sprintf (name, "__tcf_%d", start_cleanup_cnt++);
7825 /* Build the function declaration. */
7826 fntype = TREE_TYPE (get_atexit_fn_ptr_type ());
7827 fndecl = build_lang_decl (FUNCTION_DECL, get_identifier (name), fntype);
7828 /* It's a function with internal linkage, generated by the
7829 compiler. */
7830 TREE_PUBLIC (fndecl) = 0;
7831 DECL_ARTIFICIAL (fndecl) = 1;
7832 /* Make the function `inline' so that it is only emitted if it is
7833 actually needed. It is unlikely that it will be inlined, since
7834 it is only called via a function pointer, but we avoid unnecessary
7835 emissions this way. */
7836 DECL_DECLARED_INLINE_P (fndecl) = 1;
7837 DECL_INTERFACE_KNOWN (fndecl) = 1;
7838 /* Build the parameter. */
7839 if (use_cxa_atexit)
7841 tree parmdecl;
7843 parmdecl = cp_build_parm_decl (NULL_TREE, ptr_type_node);
7844 DECL_CONTEXT (parmdecl) = fndecl;
7845 TREE_USED (parmdecl) = 1;
7846 DECL_READ_P (parmdecl) = 1;
7847 DECL_ARGUMENTS (fndecl) = parmdecl;
7850 pushdecl (fndecl);
7851 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
7853 pop_lang_context ();
7855 return current_function_decl;
7858 /* Finish the cleanup function begun by start_cleanup_fn. */
7860 static void
7861 end_cleanup_fn (void)
7863 expand_or_defer_fn (finish_function (0));
7865 pop_from_top_level ();
7868 /* Generate code to handle the destruction of DECL, an object with
7869 static storage duration. */
7871 tree
7872 register_dtor_fn (tree decl)
7874 tree cleanup;
7875 tree addr;
7876 tree compound_stmt;
7877 tree fcall;
7878 tree type;
7879 bool ob_parm, dso_parm, use_dtor;
7880 tree arg0, arg1, arg2;
7881 tree atex_node;
7883 type = TREE_TYPE (decl);
7884 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
7885 return void_node;
7887 /* If we're using "__cxa_atexit" (or "__cxa_thread_atexit" or
7888 "__aeabi_atexit"), and DECL is a class object, we can just pass the
7889 destructor to "__cxa_atexit"; we don't have to build a temporary
7890 function to do the cleanup. */
7891 dso_parm = (flag_use_cxa_atexit
7892 && !targetm.cxx.use_atexit_for_cxa_atexit ());
7893 ob_parm = (CP_DECL_THREAD_LOCAL_P (decl) || dso_parm);
7894 use_dtor = ob_parm && CLASS_TYPE_P (type);
7895 if (use_dtor)
7897 int idx;
7899 /* Find the destructor. */
7900 idx = lookup_fnfields_1 (type, complete_dtor_identifier);
7901 gcc_assert (idx >= 0);
7902 cleanup = (*CLASSTYPE_METHOD_VEC (type))[idx];
7903 /* Make sure it is accessible. */
7904 perform_or_defer_access_check (TYPE_BINFO (type), cleanup, cleanup,
7905 tf_warning_or_error);
7907 else
7909 /* Call build_cleanup before we enter the anonymous function so
7910 that any access checks will be done relative to the current
7911 scope, rather than the scope of the anonymous function. */
7912 build_cleanup (decl);
7914 /* Now start the function. */
7915 cleanup = start_cleanup_fn ();
7917 /* Now, recompute the cleanup. It may contain SAVE_EXPRs that refer
7918 to the original function, rather than the anonymous one. That
7919 will make the back end think that nested functions are in use,
7920 which causes confusion. */
7921 push_deferring_access_checks (dk_no_check);
7922 fcall = build_cleanup (decl);
7923 pop_deferring_access_checks ();
7925 /* Create the body of the anonymous function. */
7926 compound_stmt = begin_compound_stmt (BCS_FN_BODY);
7927 finish_expr_stmt (fcall);
7928 finish_compound_stmt (compound_stmt);
7929 end_cleanup_fn ();
7932 /* Call atexit with the cleanup function. */
7933 mark_used (cleanup);
7934 cleanup = build_address (cleanup);
7936 if (CP_DECL_THREAD_LOCAL_P (decl))
7937 atex_node = get_thread_atexit_node ();
7938 else
7939 atex_node = get_atexit_node ();
7941 if (use_dtor)
7943 /* We must convert CLEANUP to the type that "__cxa_atexit"
7944 expects. */
7945 cleanup = build_nop (get_atexit_fn_ptr_type (), cleanup);
7946 /* "__cxa_atexit" will pass the address of DECL to the
7947 cleanup function. */
7948 mark_used (decl);
7949 addr = build_address (decl);
7950 /* The declared type of the parameter to "__cxa_atexit" is
7951 "void *". For plain "T*", we could just let the
7952 machinery in cp_build_function_call convert it -- but if the
7953 type is "cv-qualified T *", then we need to convert it
7954 before passing it in, to avoid spurious errors. */
7955 addr = build_nop (ptr_type_node, addr);
7957 else
7958 /* Since the cleanup functions we build ignore the address
7959 they're given, there's no reason to pass the actual address
7960 in, and, in general, it's cheaper to pass NULL than any
7961 other value. */
7962 addr = null_pointer_node;
7964 if (dso_parm)
7965 arg2 = cp_build_addr_expr (get_dso_handle_node (),
7966 tf_warning_or_error);
7967 else if (ob_parm)
7968 /* Just pass NULL to the dso handle parm if we don't actually
7969 have a DSO handle on this target. */
7970 arg2 = null_pointer_node;
7971 else
7972 arg2 = NULL_TREE;
7974 if (ob_parm)
7976 if (!CP_DECL_THREAD_LOCAL_P (decl)
7977 && targetm.cxx.use_aeabi_atexit ())
7979 arg1 = cleanup;
7980 arg0 = addr;
7982 else
7984 arg1 = addr;
7985 arg0 = cleanup;
7988 else
7990 arg0 = cleanup;
7991 arg1 = NULL_TREE;
7993 return cp_build_function_call_nary (atex_node, tf_warning_or_error,
7994 arg0, arg1, arg2, NULL_TREE);
7997 /* DECL is a VAR_DECL with static storage duration. INIT, if present,
7998 is its initializer. Generate code to handle the construction
7999 and destruction of DECL. */
8001 static void
8002 expand_static_init (tree decl, tree init)
8004 gcc_assert (VAR_P (decl));
8005 gcc_assert (TREE_STATIC (decl));
8007 /* Some variables require no dynamic initialization. */
8008 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
8010 /* Make sure the destructor is callable. */
8011 cxx_maybe_build_cleanup (decl, tf_warning_or_error);
8012 if (!init)
8013 return;
8016 if (CP_DECL_THREAD_LOCAL_P (decl) && DECL_GNU_TLS_P (decl)
8017 && !DECL_FUNCTION_SCOPE_P (decl))
8019 if (init)
8020 error ("non-local variable %qD declared %<__thread%> "
8021 "needs dynamic initialization", decl);
8022 else
8023 error ("non-local variable %qD declared %<__thread%> "
8024 "has a non-trivial destructor", decl);
8025 static bool informed;
8026 if (!informed)
8028 inform (DECL_SOURCE_LOCATION (decl),
8029 "C++11 %<thread_local%> allows dynamic initialization "
8030 "and destruction");
8031 informed = true;
8033 return;
8036 if (DECL_FUNCTION_SCOPE_P (decl))
8038 /* Emit code to perform this initialization but once. */
8039 tree if_stmt = NULL_TREE, inner_if_stmt = NULL_TREE;
8040 tree then_clause = NULL_TREE, inner_then_clause = NULL_TREE;
8041 tree guard, guard_addr;
8042 tree flag, begin;
8043 /* We don't need thread-safety code for thread-local vars. */
8044 bool thread_guard = (flag_threadsafe_statics
8045 && !CP_DECL_THREAD_LOCAL_P (decl));
8047 /* Emit code to perform this initialization but once. This code
8048 looks like:
8050 static <type> guard;
8051 if (!__atomic_load (guard.first_byte)) {
8052 if (__cxa_guard_acquire (&guard)) {
8053 bool flag = false;
8054 try {
8055 // Do initialization.
8056 flag = true; __cxa_guard_release (&guard);
8057 // Register variable for destruction at end of program.
8058 } catch {
8059 if (!flag) __cxa_guard_abort (&guard);
8064 Note that the `flag' variable is only set to 1 *after* the
8065 initialization is complete. This ensures that an exception,
8066 thrown during the construction, will cause the variable to
8067 reinitialized when we pass through this code again, as per:
8069 [stmt.dcl]
8071 If the initialization exits by throwing an exception, the
8072 initialization is not complete, so it will be tried again
8073 the next time control enters the declaration.
8075 This process should be thread-safe, too; multiple threads
8076 should not be able to initialize the variable more than
8077 once. */
8079 /* Create the guard variable. */
8080 guard = get_guard (decl);
8082 /* Begin the conditional initialization. */
8083 if_stmt = begin_if_stmt ();
8085 finish_if_stmt_cond (get_guard_cond (guard, thread_guard), if_stmt);
8086 then_clause = begin_compound_stmt (BCS_NO_SCOPE);
8088 if (thread_guard)
8090 tree vfntype = NULL_TREE;
8091 tree acquire_name, release_name, abort_name;
8092 tree acquire_fn, release_fn, abort_fn;
8093 guard_addr = build_address (guard);
8095 acquire_name = get_identifier ("__cxa_guard_acquire");
8096 release_name = get_identifier ("__cxa_guard_release");
8097 abort_name = get_identifier ("__cxa_guard_abort");
8098 acquire_fn = identifier_global_value (acquire_name);
8099 release_fn = identifier_global_value (release_name);
8100 abort_fn = identifier_global_value (abort_name);
8101 if (!acquire_fn)
8102 acquire_fn = push_library_fn
8103 (acquire_name, build_function_type_list (integer_type_node,
8104 TREE_TYPE (guard_addr),
8105 NULL_TREE),
8106 NULL_TREE, ECF_NOTHROW | ECF_LEAF);
8107 if (!release_fn || !abort_fn)
8108 vfntype = build_function_type_list (void_type_node,
8109 TREE_TYPE (guard_addr),
8110 NULL_TREE);
8111 if (!release_fn)
8112 release_fn = push_library_fn (release_name, vfntype, NULL_TREE,
8113 ECF_NOTHROW | ECF_LEAF);
8114 if (!abort_fn)
8115 abort_fn = push_library_fn (abort_name, vfntype, NULL_TREE,
8116 ECF_NOTHROW | ECF_LEAF);
8118 inner_if_stmt = begin_if_stmt ();
8119 finish_if_stmt_cond (build_call_n (acquire_fn, 1, guard_addr),
8120 inner_if_stmt);
8122 inner_then_clause = begin_compound_stmt (BCS_NO_SCOPE);
8123 begin = get_target_expr (boolean_false_node);
8124 flag = TARGET_EXPR_SLOT (begin);
8126 TARGET_EXPR_CLEANUP (begin)
8127 = build3 (COND_EXPR, void_type_node, flag,
8128 void_node,
8129 build_call_n (abort_fn, 1, guard_addr));
8130 CLEANUP_EH_ONLY (begin) = 1;
8132 /* Do the initialization itself. */
8133 init = add_stmt_to_compound (begin, init);
8134 init = add_stmt_to_compound
8135 (init, build2 (MODIFY_EXPR, void_type_node, flag, boolean_true_node));
8136 init = add_stmt_to_compound
8137 (init, build_call_n (release_fn, 1, guard_addr));
8139 else
8140 init = add_stmt_to_compound (init, set_guard (guard));
8142 /* Use atexit to register a function for destroying this static
8143 variable. */
8144 init = add_stmt_to_compound (init, register_dtor_fn (decl));
8146 finish_expr_stmt (init);
8148 if (thread_guard)
8150 finish_compound_stmt (inner_then_clause);
8151 finish_then_clause (inner_if_stmt);
8152 finish_if_stmt (inner_if_stmt);
8155 finish_compound_stmt (then_clause);
8156 finish_then_clause (if_stmt);
8157 finish_if_stmt (if_stmt);
8159 else if (CP_DECL_THREAD_LOCAL_P (decl))
8160 tls_aggregates = tree_cons (init, decl, tls_aggregates);
8161 else
8162 static_aggregates = tree_cons (init, decl, static_aggregates);
8166 /* Make TYPE a complete type based on INITIAL_VALUE.
8167 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
8168 2 if there was no information (in which case assume 0 if DO_DEFAULT),
8169 3 if the initializer list is empty (in pedantic mode). */
8172 cp_complete_array_type (tree *ptype, tree initial_value, bool do_default)
8174 int failure;
8175 tree type, elt_type;
8177 /* Don't get confused by a CONSTRUCTOR for some other type. */
8178 if (initial_value && TREE_CODE (initial_value) == CONSTRUCTOR
8179 && !BRACE_ENCLOSED_INITIALIZER_P (initial_value)
8180 && TREE_CODE (TREE_TYPE (initial_value)) != ARRAY_TYPE)
8181 return 1;
8183 if (initial_value)
8185 unsigned HOST_WIDE_INT i;
8186 tree value;
8188 /* An array of character type can be initialized from a
8189 brace-enclosed string constant.
8191 FIXME: this code is duplicated from reshape_init. Probably
8192 we should just call reshape_init here? */
8193 if (char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (*ptype)))
8194 && TREE_CODE (initial_value) == CONSTRUCTOR
8195 && !vec_safe_is_empty (CONSTRUCTOR_ELTS (initial_value)))
8197 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value);
8198 tree value = (*v)[0].value;
8200 if (TREE_CODE (value) == STRING_CST
8201 && v->length () == 1)
8202 initial_value = value;
8205 /* If any of the elements are parameter packs, we can't actually
8206 complete this type now because the array size is dependent. */
8207 if (TREE_CODE (initial_value) == CONSTRUCTOR)
8209 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (initial_value),
8210 i, value)
8212 if (PACK_EXPANSION_P (value))
8213 return 0;
8218 failure = complete_array_type (ptype, initial_value, do_default);
8220 /* We can create the array before the element type is complete, which
8221 means that we didn't have these two bits set in the original type
8222 either. In completing the type, we are expected to propagate these
8223 bits. See also complete_type which does the same thing for arrays
8224 of fixed size. */
8225 type = *ptype;
8226 if (TYPE_DOMAIN (type))
8228 elt_type = TREE_TYPE (type);
8229 TYPE_NEEDS_CONSTRUCTING (type) = TYPE_NEEDS_CONSTRUCTING (elt_type);
8230 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
8231 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (elt_type);
8234 return failure;
8237 /* As above, but either give an error or reject zero-size arrays, depending
8238 on COMPLAIN. */
8241 cp_complete_array_type_or_error (tree *ptype, tree initial_value,
8242 bool do_default, tsubst_flags_t complain)
8244 int failure;
8245 bool sfinae = !(complain & tf_error);
8246 /* In SFINAE context we can't be lenient about zero-size arrays. */
8247 if (sfinae)
8248 ++pedantic;
8249 failure = cp_complete_array_type (ptype, initial_value, do_default);
8250 if (sfinae)
8251 --pedantic;
8252 if (failure)
8254 if (sfinae)
8255 /* Not an error. */;
8256 else if (failure == 1)
8257 error ("initializer fails to determine size of %qT", *ptype);
8258 else if (failure == 2)
8260 if (do_default)
8261 error ("array size missing in %qT", *ptype);
8263 else if (failure == 3)
8264 error ("zero-size array %qT", *ptype);
8265 *ptype = error_mark_node;
8267 return failure;
8270 /* Return zero if something is declared to be a member of type
8271 CTYPE when in the context of CUR_TYPE. STRING is the error
8272 message to print in that case. Otherwise, quietly return 1. */
8274 static int
8275 member_function_or_else (tree ctype, tree cur_type, enum overload_flags flags)
8277 if (ctype && ctype != cur_type)
8279 if (flags == DTOR_FLAG)
8280 error ("destructor for alien class %qT cannot be a member", ctype);
8281 else
8282 error ("constructor for alien class %qT cannot be a member", ctype);
8283 return 0;
8285 return 1;
8288 /* Subroutine of `grokdeclarator'. */
8290 /* Generate errors possibly applicable for a given set of specifiers.
8291 This is for ARM $7.1.2. */
8293 static void
8294 bad_specifiers (tree object,
8295 enum bad_spec_place type,
8296 int virtualp,
8297 int quals,
8298 int inlinep,
8299 int friendp,
8300 int raises)
8302 switch (type)
8304 case BSP_VAR:
8305 if (virtualp)
8306 error ("%qD declared as a %<virtual%> variable", object);
8307 if (quals)
8308 error ("%<const%> and %<volatile%> function specifiers on "
8309 "%qD invalid in variable declaration", object);
8310 break;
8311 case BSP_PARM:
8312 if (virtualp)
8313 error ("%qD declared as a %<virtual%> parameter", object);
8314 if (inlinep)
8315 error ("%qD declared as an %<inline%> parameter", object);
8316 if (quals)
8317 error ("%<const%> and %<volatile%> function specifiers on "
8318 "%qD invalid in parameter declaration", object);
8319 break;
8320 case BSP_TYPE:
8321 if (virtualp)
8322 error ("%qD declared as a %<virtual%> type", object);
8323 if (inlinep)
8324 error ("%qD declared as an %<inline%> type", object);
8325 if (quals)
8326 error ("%<const%> and %<volatile%> function specifiers on "
8327 "%qD invalid in type declaration", object);
8328 break;
8329 case BSP_FIELD:
8330 if (virtualp)
8331 error ("%qD declared as a %<virtual%> field", object);
8332 if (inlinep)
8333 error ("%qD declared as an %<inline%> field", object);
8334 if (quals)
8335 error ("%<const%> and %<volatile%> function specifiers on "
8336 "%qD invalid in field declaration", object);
8337 break;
8338 default:
8339 gcc_unreachable();
8341 if (friendp)
8342 error ("%q+D declared as a friend", object);
8343 if (raises
8344 && !flag_noexcept_type
8345 && (TREE_CODE (object) == TYPE_DECL
8346 || (!TYPE_PTRFN_P (TREE_TYPE (object))
8347 && !TYPE_REFFN_P (TREE_TYPE (object))
8348 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (object)))))
8349 error ("%q+D declared with an exception specification", object);
8352 /* DECL is a member function or static data member and is presently
8353 being defined. Check that the definition is taking place in a
8354 valid namespace. */
8356 static void
8357 check_class_member_definition_namespace (tree decl)
8359 /* These checks only apply to member functions and static data
8360 members. */
8361 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
8362 /* We check for problems with specializations in pt.c in
8363 check_specialization_namespace, where we can issue better
8364 diagnostics. */
8365 if (processing_specialization)
8366 return;
8367 /* We check this in check_explicit_instantiation_namespace. */
8368 if (processing_explicit_instantiation)
8369 return;
8370 /* [class.mfct]
8372 A member function definition that appears outside of the
8373 class definition shall appear in a namespace scope enclosing
8374 the class definition.
8376 [class.static.data]
8378 The definition for a static data member shall appear in a
8379 namespace scope enclosing the member's class definition. */
8380 if (!is_ancestor (current_namespace, DECL_CONTEXT (decl)))
8381 permerror (input_location, "definition of %qD is not in namespace enclosing %qT",
8382 decl, DECL_CONTEXT (decl));
8385 /* Build a PARM_DECL for the "this" parameter. TYPE is the
8386 METHOD_TYPE for a non-static member function; QUALS are the
8387 cv-qualifiers that apply to the function. */
8389 tree
8390 build_this_parm (tree type, cp_cv_quals quals)
8392 tree this_type;
8393 tree qual_type;
8394 tree parm;
8395 cp_cv_quals this_quals;
8397 if (CLASS_TYPE_P (type))
8399 this_type
8400 = cp_build_qualified_type (type, quals & ~TYPE_QUAL_RESTRICT);
8401 this_type = build_pointer_type (this_type);
8403 else
8404 this_type = type_of_this_parm (type);
8405 /* The `this' parameter is implicitly `const'; it cannot be
8406 assigned to. */
8407 this_quals = (quals & TYPE_QUAL_RESTRICT) | TYPE_QUAL_CONST;
8408 qual_type = cp_build_qualified_type (this_type, this_quals);
8409 parm = build_artificial_parm (this_identifier, qual_type);
8410 cp_apply_type_quals_to_decl (this_quals, parm);
8411 return parm;
8414 /* DECL is a static member function. Complain if it was declared
8415 with function-cv-quals. */
8417 static void
8418 check_static_quals (tree decl, cp_cv_quals quals)
8420 if (quals != TYPE_UNQUALIFIED)
8421 error ("static member function %q#D declared with type qualifiers",
8422 decl);
8425 // Check that FN takes no arguments and returns bool.
8426 static void
8427 check_concept_fn (tree fn)
8429 // A constraint is nullary.
8430 if (DECL_ARGUMENTS (fn))
8431 error ("concept %q#D declared with function parameters", fn);
8433 // The declared return type of the concept shall be bool, and
8434 // it shall not be deduced from it definition.
8435 tree type = TREE_TYPE (TREE_TYPE (fn));
8436 if (is_auto (type))
8437 error ("concept %q#D declared with a deduced return type", fn);
8438 else if (type != boolean_type_node)
8439 error ("concept %q#D with non-%<bool%> return type %qT", fn, type);
8442 /* Helper function. Replace the temporary this parameter injected
8443 during cp_finish_omp_declare_simd with the real this parameter. */
8445 static tree
8446 declare_simd_adjust_this (tree *tp, int *walk_subtrees, void *data)
8448 tree this_parm = (tree) data;
8449 if (TREE_CODE (*tp) == PARM_DECL
8450 && DECL_NAME (*tp) == this_identifier
8451 && *tp != this_parm)
8452 *tp = this_parm;
8453 else if (TYPE_P (*tp))
8454 *walk_subtrees = 0;
8455 return NULL_TREE;
8458 /* CTYPE is class type, or null if non-class.
8459 TYPE is type this FUNCTION_DECL should have, either FUNCTION_TYPE
8460 or METHOD_TYPE.
8461 DECLARATOR is the function's name.
8462 PARMS is a chain of PARM_DECLs for the function.
8463 VIRTUALP is truthvalue of whether the function is virtual or not.
8464 FLAGS are to be passed through to `grokclassfn'.
8465 QUALS are qualifiers indicating whether the function is `const'
8466 or `volatile'.
8467 RAISES is a list of exceptions that this function can raise.
8468 CHECK is 1 if we must find this method in CTYPE, 0 if we should
8469 not look, and -1 if we should not call `grokclassfn' at all.
8471 SFK is the kind of special function (if any) for the new function.
8473 Returns `NULL_TREE' if something goes wrong, after issuing
8474 applicable error messages. */
8476 static tree
8477 grokfndecl (tree ctype,
8478 tree type,
8479 tree declarator,
8480 tree parms,
8481 tree orig_declarator,
8482 tree decl_reqs,
8483 int virtualp,
8484 enum overload_flags flags,
8485 cp_cv_quals quals,
8486 cp_ref_qualifier rqual,
8487 tree raises,
8488 int check,
8489 int friendp,
8490 int publicp,
8491 int inlinep,
8492 bool deletedp,
8493 special_function_kind sfk,
8494 bool funcdef_flag,
8495 int template_count,
8496 tree in_namespace,
8497 tree* attrlist,
8498 location_t location)
8500 tree decl;
8501 int staticp = ctype && TREE_CODE (type) == FUNCTION_TYPE;
8502 tree t;
8504 // Was the concept specifier present?
8505 bool concept_p = inlinep & 4;
8507 // Concept declarations must have a corresponding definition.
8508 if (concept_p && !funcdef_flag)
8510 error ("concept %qD has no definition", declarator);
8511 return NULL_TREE;
8514 if (rqual)
8515 type = build_ref_qualified_type (type, rqual);
8516 if (raises)
8517 type = build_exception_variant (type, raises);
8519 decl = build_lang_decl (FUNCTION_DECL, declarator, type);
8521 /* Set the constraints on the declaration. */
8522 if (flag_concepts)
8524 tree tmpl_reqs = NULL_TREE;
8525 if (processing_template_decl > template_class_depth (ctype))
8526 tmpl_reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
8528 /* Adjust the required expression into a constraint. */
8529 if (decl_reqs)
8530 decl_reqs = normalize_expression (decl_reqs);
8532 tree ci = build_constraints (tmpl_reqs, decl_reqs);
8533 set_constraints (decl, ci);
8536 /* If we have an explicit location, use it, otherwise use whatever
8537 build_lang_decl used (probably input_location). */
8538 if (location != UNKNOWN_LOCATION)
8539 DECL_SOURCE_LOCATION (decl) = location;
8541 if (TREE_CODE (type) == METHOD_TYPE)
8543 tree parm;
8544 parm = build_this_parm (type, quals);
8545 DECL_CHAIN (parm) = parms;
8546 parms = parm;
8548 /* Allocate space to hold the vptr bit if needed. */
8549 SET_DECL_ALIGN (decl, MINIMUM_METHOD_BOUNDARY);
8551 DECL_ARGUMENTS (decl) = parms;
8552 for (t = parms; t; t = DECL_CHAIN (t))
8553 DECL_CONTEXT (t) = decl;
8554 /* Propagate volatile out from type to decl. */
8555 if (TYPE_VOLATILE (type))
8556 TREE_THIS_VOLATILE (decl) = 1;
8558 /* Setup decl according to sfk. */
8559 switch (sfk)
8561 case sfk_constructor:
8562 case sfk_copy_constructor:
8563 case sfk_move_constructor:
8564 DECL_CONSTRUCTOR_P (decl) = 1;
8565 break;
8566 case sfk_destructor:
8567 DECL_DESTRUCTOR_P (decl) = 1;
8568 break;
8569 default:
8570 break;
8573 if (friendp
8574 && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)
8576 if (funcdef_flag)
8577 error
8578 ("defining explicit specialization %qD in friend declaration",
8579 orig_declarator);
8580 else
8582 tree fns = TREE_OPERAND (orig_declarator, 0);
8583 tree args = TREE_OPERAND (orig_declarator, 1);
8585 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8587 /* Something like `template <class T> friend void f<T>()'. */
8588 error ("invalid use of template-id %qD in declaration "
8589 "of primary template",
8590 orig_declarator);
8591 return NULL_TREE;
8595 /* A friend declaration of the form friend void f<>(). Record
8596 the information in the TEMPLATE_ID_EXPR. */
8597 SET_DECL_IMPLICIT_INSTANTIATION (decl);
8599 gcc_assert (identifier_p (fns) || TREE_CODE (fns) == OVERLOAD);
8600 DECL_TEMPLATE_INFO (decl) = build_template_info (fns, args);
8602 for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t))
8603 if (TREE_PURPOSE (t)
8604 && TREE_CODE (TREE_PURPOSE (t)) == DEFAULT_ARG)
8606 error ("default arguments are not allowed in declaration "
8607 "of friend template specialization %qD",
8608 decl);
8609 return NULL_TREE;
8612 if (inlinep & 1)
8614 error ("%<inline%> is not allowed in declaration of friend "
8615 "template specialization %qD",
8616 decl);
8617 return NULL_TREE;
8622 /* If this decl has namespace scope, set that up. */
8623 if (in_namespace)
8624 set_decl_namespace (decl, in_namespace, friendp);
8625 else if (!ctype)
8626 DECL_CONTEXT (decl) = FROB_CONTEXT (current_decl_namespace ());
8628 /* `main' and builtins have implicit 'C' linkage. */
8629 if ((MAIN_NAME_P (declarator)
8630 || (IDENTIFIER_LENGTH (declarator) > 10
8631 && IDENTIFIER_POINTER (declarator)[0] == '_'
8632 && IDENTIFIER_POINTER (declarator)[1] == '_'
8633 && strncmp (IDENTIFIER_POINTER (declarator)+2, "builtin_", 8) == 0)
8634 || (targetcm.cxx_implicit_extern_c
8635 && targetcm.cxx_implicit_extern_c(IDENTIFIER_POINTER (declarator))))
8636 && current_lang_name == lang_name_cplusplus
8637 && ctype == NULL_TREE
8638 && DECL_FILE_SCOPE_P (decl))
8639 SET_DECL_LANGUAGE (decl, lang_c);
8641 /* Should probably propagate const out from type to decl I bet (mrs). */
8642 if (staticp)
8644 DECL_STATIC_FUNCTION_P (decl) = 1;
8645 DECL_CONTEXT (decl) = ctype;
8648 if (deletedp)
8649 DECL_DELETED_FN (decl) = 1;
8651 if (ctype)
8653 DECL_CONTEXT (decl) = ctype;
8654 if (funcdef_flag)
8655 check_class_member_definition_namespace (decl);
8658 if (ctype == NULL_TREE && DECL_MAIN_P (decl))
8660 if (PROCESSING_REAL_TEMPLATE_DECL_P())
8661 error ("cannot declare %<::main%> to be a template");
8662 if (inlinep & 1)
8663 error ("cannot declare %<::main%> to be inline");
8664 if (inlinep & 2)
8665 error ("cannot declare %<::main%> to be constexpr");
8666 if (!publicp)
8667 error ("cannot declare %<::main%> to be static");
8668 inlinep = 0;
8669 publicp = 1;
8672 /* Members of anonymous types and local classes have no linkage; make
8673 them internal. If a typedef is made later, this will be changed. */
8674 if (ctype && (!TREE_PUBLIC (TYPE_MAIN_DECL (ctype))
8675 || decl_function_context (TYPE_MAIN_DECL (ctype))))
8676 publicp = 0;
8678 if (publicp && cxx_dialect == cxx98)
8680 /* [basic.link]: A name with no linkage (notably, the name of a class
8681 or enumeration declared in a local scope) shall not be used to
8682 declare an entity with linkage.
8684 DR 757 relaxes this restriction for C++0x. */
8685 no_linkage_error (decl);
8688 TREE_PUBLIC (decl) = publicp;
8689 if (! publicp)
8691 DECL_INTERFACE_KNOWN (decl) = 1;
8692 DECL_NOT_REALLY_EXTERN (decl) = 1;
8695 /* If the declaration was declared inline, mark it as such. */
8696 if (inlinep)
8698 DECL_DECLARED_INLINE_P (decl) = 1;
8699 if (publicp)
8700 DECL_COMDAT (decl) = 1;
8702 if (inlinep & 2)
8703 DECL_DECLARED_CONSTEXPR_P (decl) = true;
8705 // If the concept declaration specifier was found, check
8706 // that the declaration satisfies the necessary requirements.
8707 if (concept_p)
8709 DECL_DECLARED_CONCEPT_P (decl) = true;
8710 check_concept_fn (decl);
8713 DECL_EXTERNAL (decl) = 1;
8714 if (TREE_CODE (type) == FUNCTION_TYPE)
8716 if (quals || rqual)
8717 TREE_TYPE (decl) = apply_memfn_quals (TREE_TYPE (decl),
8718 TYPE_UNQUALIFIED,
8719 REF_QUAL_NONE);
8721 if (quals)
8723 error (ctype
8724 ? G_("static member function %qD cannot have cv-qualifier")
8725 : G_("non-member function %qD cannot have cv-qualifier"),
8726 decl);
8727 quals = TYPE_UNQUALIFIED;
8730 if (rqual)
8732 error (ctype
8733 ? G_("static member function %qD cannot have ref-qualifier")
8734 : G_("non-member function %qD cannot have ref-qualifier"),
8735 decl);
8736 rqual = REF_QUAL_NONE;
8740 if (deduction_guide_p (decl))
8742 if (!DECL_NAMESPACE_SCOPE_P (decl))
8744 error_at (location, "deduction guide %qD must be declared at "
8745 "namespace scope", decl);
8746 return NULL_TREE;
8748 if (funcdef_flag)
8749 error_at (location,
8750 "deduction guide %qD must not have a function body", decl);
8752 else if (IDENTIFIER_OPNAME_P (DECL_NAME (decl))
8753 && !grok_op_properties (decl, /*complain=*/true))
8754 return NULL_TREE;
8755 else if (UDLIT_OPER_P (DECL_NAME (decl)))
8757 bool long_long_unsigned_p;
8758 bool long_double_p;
8759 const char *suffix = NULL;
8760 /* [over.literal]/6: Literal operators shall not have C linkage. */
8761 if (DECL_LANGUAGE (decl) == lang_c)
8763 error ("literal operator with C linkage");
8764 return NULL_TREE;
8767 if (DECL_NAMESPACE_SCOPE_P (decl))
8769 if (!check_literal_operator_args (decl, &long_long_unsigned_p,
8770 &long_double_p))
8772 error ("%qD has invalid argument list", decl);
8773 return NULL_TREE;
8776 suffix = UDLIT_OP_SUFFIX (DECL_NAME (decl));
8777 if (long_long_unsigned_p)
8779 if (cpp_interpret_int_suffix (parse_in, suffix, strlen (suffix)))
8780 warning (0, "integer suffix %<%s%>"
8781 " shadowed by implementation", suffix);
8783 else if (long_double_p)
8785 if (cpp_interpret_float_suffix (parse_in, suffix, strlen (suffix)))
8786 warning (0, "floating point suffix %<%s%>"
8787 " shadowed by implementation", suffix);
8790 else
8792 error ("%qD must be a non-member function", decl);
8793 return NULL_TREE;
8797 if (funcdef_flag)
8798 /* Make the init_value nonzero so pushdecl knows this is not
8799 tentative. error_mark_node is replaced later with the BLOCK. */
8800 DECL_INITIAL (decl) = error_mark_node;
8802 if (TYPE_NOTHROW_P (type) || nothrow_libfn_p (decl))
8803 TREE_NOTHROW (decl) = 1;
8805 if (flag_openmp || flag_openmp_simd || flag_cilkplus)
8807 /* Adjust "omp declare simd" attributes. */
8808 tree ods = lookup_attribute ("omp declare simd", *attrlist);
8809 if (ods)
8811 tree attr;
8812 for (attr = ods; attr;
8813 attr = lookup_attribute ("omp declare simd", TREE_CHAIN (attr)))
8815 if (TREE_CODE (type) == METHOD_TYPE)
8816 walk_tree (&TREE_VALUE (attr), declare_simd_adjust_this,
8817 DECL_ARGUMENTS (decl), NULL);
8818 if (TREE_VALUE (attr) != NULL_TREE)
8820 tree cl = TREE_VALUE (TREE_VALUE (attr));
8821 cl = c_omp_declare_simd_clauses_to_numbers
8822 (DECL_ARGUMENTS (decl), cl);
8823 if (cl)
8824 TREE_VALUE (TREE_VALUE (attr)) = cl;
8825 else
8826 TREE_VALUE (attr) = NULL_TREE;
8832 /* Caller will do the rest of this. */
8833 if (check < 0)
8834 return decl;
8836 if (ctype != NULL_TREE)
8837 grokclassfn (ctype, decl, flags);
8839 /* 12.4/3 */
8840 if (cxx_dialect >= cxx11
8841 && DECL_DESTRUCTOR_P (decl)
8842 && !TYPE_BEING_DEFINED (DECL_CONTEXT (decl))
8843 && !processing_template_decl)
8844 deduce_noexcept_on_destructor (decl);
8846 decl = check_explicit_specialization (orig_declarator, decl,
8847 template_count,
8848 2 * funcdef_flag +
8849 4 * (friendp != 0) +
8850 8 * concept_p);
8851 if (decl == error_mark_node)
8852 return NULL_TREE;
8854 if (DECL_STATIC_FUNCTION_P (decl))
8855 check_static_quals (decl, quals);
8857 if (attrlist)
8859 cplus_decl_attributes (&decl, *attrlist, 0);
8860 *attrlist = NULL_TREE;
8863 /* Check main's type after attributes have been applied. */
8864 if (ctype == NULL_TREE && DECL_MAIN_P (decl))
8866 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
8867 integer_type_node))
8869 tree oldtypeargs = TYPE_ARG_TYPES (TREE_TYPE (decl));
8870 tree newtype;
8871 error ("%<::main%> must return %<int%>");
8872 newtype = build_function_type (integer_type_node, oldtypeargs);
8873 TREE_TYPE (decl) = newtype;
8875 if (warn_main)
8876 check_main_parameter_types (decl);
8879 if (ctype != NULL_TREE && check)
8881 tree old_decl = check_classfn (ctype, decl,
8882 (processing_template_decl
8883 > template_class_depth (ctype))
8884 ? current_template_parms
8885 : NULL_TREE);
8887 if (old_decl == error_mark_node)
8888 return NULL_TREE;
8890 if (old_decl)
8892 tree ok;
8893 tree pushed_scope;
8895 if (TREE_CODE (old_decl) == TEMPLATE_DECL)
8896 /* Because grokfndecl is always supposed to return a
8897 FUNCTION_DECL, we pull out the DECL_TEMPLATE_RESULT
8898 here. We depend on our callers to figure out that its
8899 really a template that's being returned. */
8900 old_decl = DECL_TEMPLATE_RESULT (old_decl);
8902 if (DECL_STATIC_FUNCTION_P (old_decl)
8903 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
8905 /* Remove the `this' parm added by grokclassfn. */
8906 revert_static_member_fn (decl);
8907 check_static_quals (decl, quals);
8909 if (DECL_ARTIFICIAL (old_decl))
8911 error ("definition of implicitly-declared %qD", old_decl);
8912 return NULL_TREE;
8914 else if (DECL_DEFAULTED_FN (old_decl))
8916 error ("definition of explicitly-defaulted %q+D", decl);
8917 inform (DECL_SOURCE_LOCATION (old_decl),
8918 "%q#D explicitly defaulted here", old_decl);
8919 return NULL_TREE;
8922 /* Since we've smashed OLD_DECL to its
8923 DECL_TEMPLATE_RESULT, we must do the same to DECL. */
8924 if (TREE_CODE (decl) == TEMPLATE_DECL)
8925 decl = DECL_TEMPLATE_RESULT (decl);
8927 /* Attempt to merge the declarations. This can fail, in
8928 the case of some invalid specialization declarations. */
8929 pushed_scope = push_scope (ctype);
8930 ok = duplicate_decls (decl, old_decl, friendp);
8931 if (pushed_scope)
8932 pop_scope (pushed_scope);
8933 if (!ok)
8935 error ("no %q#D member function declared in class %qT",
8936 decl, ctype);
8937 return NULL_TREE;
8939 if (ok == error_mark_node)
8940 return NULL_TREE;
8941 return old_decl;
8945 if (DECL_CONSTRUCTOR_P (decl) && !grok_ctor_properties (ctype, decl))
8946 return NULL_TREE;
8948 if (ctype == NULL_TREE || check)
8949 return decl;
8951 if (virtualp)
8952 DECL_VIRTUAL_P (decl) = 1;
8954 return decl;
8957 /* decl is a FUNCTION_DECL.
8958 specifiers are the parsed virt-specifiers.
8960 Set flags to reflect the virt-specifiers.
8962 Returns decl. */
8964 static tree
8965 set_virt_specifiers (tree decl, cp_virt_specifiers specifiers)
8967 if (decl == NULL_TREE)
8968 return decl;
8969 if (specifiers & VIRT_SPEC_OVERRIDE)
8970 DECL_OVERRIDE_P (decl) = 1;
8971 if (specifiers & VIRT_SPEC_FINAL)
8972 DECL_FINAL_P (decl) = 1;
8973 return decl;
8976 /* DECL is a VAR_DECL for a static data member. Set flags to reflect
8977 the linkage that DECL will receive in the object file. */
8979 static void
8980 set_linkage_for_static_data_member (tree decl)
8982 /* A static data member always has static storage duration and
8983 external linkage. Note that static data members are forbidden in
8984 local classes -- the only situation in which a class has
8985 non-external linkage. */
8986 TREE_PUBLIC (decl) = 1;
8987 TREE_STATIC (decl) = 1;
8988 /* For non-template classes, static data members are always put
8989 out in exactly those files where they are defined, just as
8990 with ordinary namespace-scope variables. */
8991 if (!processing_template_decl)
8992 DECL_INTERFACE_KNOWN (decl) = 1;
8995 /* Create a VAR_DECL named NAME with the indicated TYPE.
8997 If SCOPE is non-NULL, it is the class type or namespace containing
8998 the variable. If SCOPE is NULL, the variable should is created in
8999 the innermost enclosing scope. */
9001 static tree
9002 grokvardecl (tree type,
9003 tree name,
9004 tree orig_declarator,
9005 const cp_decl_specifier_seq *declspecs,
9006 int initialized,
9007 int type_quals,
9008 int inlinep,
9009 bool conceptp,
9010 int template_count,
9011 tree scope)
9013 tree decl;
9014 tree explicit_scope;
9016 gcc_assert (!name || identifier_p (name));
9018 bool constp = (type_quals & TYPE_QUAL_CONST) != 0;
9019 bool volatilep = (type_quals & TYPE_QUAL_VOLATILE) != 0;
9021 /* Compute the scope in which to place the variable, but remember
9022 whether or not that scope was explicitly specified by the user. */
9023 explicit_scope = scope;
9024 if (!scope)
9026 /* An explicit "extern" specifier indicates a namespace-scope
9027 variable. */
9028 if (declspecs->storage_class == sc_extern)
9029 scope = current_decl_namespace ();
9030 else if (!at_function_scope_p ())
9031 scope = current_scope ();
9034 if (scope
9035 && (/* If the variable is a namespace-scope variable declared in a
9036 template, we need DECL_LANG_SPECIFIC. */
9037 (TREE_CODE (scope) == NAMESPACE_DECL && processing_template_decl)
9038 /* Similarly for namespace-scope variables with language linkage
9039 other than C++. */
9040 || (TREE_CODE (scope) == NAMESPACE_DECL
9041 && current_lang_name != lang_name_cplusplus)
9042 /* Similarly for static data members. */
9043 || TYPE_P (scope)
9044 /* Similarly for explicit specializations. */
9045 || (orig_declarator
9046 && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)))
9047 decl = build_lang_decl (VAR_DECL, name, type);
9048 else
9049 decl = build_decl (input_location, VAR_DECL, name, type);
9051 if (explicit_scope && TREE_CODE (explicit_scope) == NAMESPACE_DECL)
9052 set_decl_namespace (decl, explicit_scope, 0);
9053 else
9054 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
9056 if (declspecs->storage_class == sc_extern)
9058 DECL_THIS_EXTERN (decl) = 1;
9059 DECL_EXTERNAL (decl) = !initialized;
9062 if (DECL_CLASS_SCOPE_P (decl))
9064 set_linkage_for_static_data_member (decl);
9065 /* This function is only called with out-of-class definitions. */
9066 DECL_EXTERNAL (decl) = 0;
9067 check_class_member_definition_namespace (decl);
9069 /* At top level, either `static' or no s.c. makes a definition
9070 (perhaps tentative), and absence of `static' makes it public. */
9071 else if (toplevel_bindings_p ())
9073 TREE_PUBLIC (decl) = (declspecs->storage_class != sc_static
9074 && (DECL_THIS_EXTERN (decl)
9075 || ! constp
9076 || volatilep
9077 || inlinep));
9078 TREE_STATIC (decl) = ! DECL_EXTERNAL (decl);
9080 /* Not at top level, only `static' makes a static definition. */
9081 else
9083 TREE_STATIC (decl) = declspecs->storage_class == sc_static;
9084 TREE_PUBLIC (decl) = DECL_EXTERNAL (decl);
9087 if (decl_spec_seq_has_spec_p (declspecs, ds_thread))
9089 if (DECL_EXTERNAL (decl) || TREE_STATIC (decl))
9091 CP_DECL_THREAD_LOCAL_P (decl) = true;
9092 if (!processing_template_decl)
9093 set_decl_tls_model (decl, decl_default_tls_model (decl));
9095 if (declspecs->gnu_thread_keyword_p)
9096 SET_DECL_GNU_TLS_P (decl);
9099 /* If the type of the decl has no linkage, make sure that we'll
9100 notice that in mark_used. */
9101 if (cxx_dialect > cxx98
9102 && decl_linkage (decl) != lk_none
9103 && DECL_LANG_SPECIFIC (decl) == NULL
9104 && !DECL_EXTERN_C_P (decl)
9105 && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
9106 retrofit_lang_decl (decl);
9108 if (TREE_PUBLIC (decl))
9110 /* [basic.link]: A name with no linkage (notably, the name of a class
9111 or enumeration declared in a local scope) shall not be used to
9112 declare an entity with linkage.
9114 DR 757 relaxes this restriction for C++0x. */
9115 if (cxx_dialect < cxx11)
9116 no_linkage_error (decl);
9118 else
9119 DECL_INTERFACE_KNOWN (decl) = 1;
9121 if (DECL_NAME (decl)
9122 && MAIN_NAME_P (DECL_NAME (decl))
9123 && scope == global_namespace)
9124 error ("cannot declare %<::main%> to be a global variable");
9126 /* Check that the variable can be safely declared as a concept.
9127 Note that this also forbids explicit specializations. */
9128 if (conceptp)
9130 if (!processing_template_decl)
9132 error ("a non-template variable cannot be %<concept%>");
9133 return NULL_TREE;
9135 else
9136 DECL_DECLARED_CONCEPT_P (decl) = true;
9137 if (!same_type_ignoring_top_level_qualifiers_p (type, boolean_type_node))
9138 error_at (declspecs->locations[ds_type_spec],
9139 "concept must have type %<bool%>");
9141 else if (flag_concepts
9142 && processing_template_decl > template_class_depth (scope))
9144 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
9145 tree ci = build_constraints (reqs, NULL_TREE);
9146 set_constraints (decl, ci);
9149 // Handle explicit specializations and instantiations of variable templates.
9150 if (orig_declarator)
9151 decl = check_explicit_specialization (orig_declarator, decl,
9152 template_count, conceptp * 8);
9154 return decl != error_mark_node ? decl : NULL_TREE;
9157 /* Create and return a canonical pointer to member function type, for
9158 TYPE, which is a POINTER_TYPE to a METHOD_TYPE. */
9160 tree
9161 build_ptrmemfunc_type (tree type)
9163 tree field, fields;
9164 tree t;
9166 if (type == error_mark_node)
9167 return type;
9169 /* Make sure that we always have the unqualified pointer-to-member
9170 type first. */
9171 if (cp_cv_quals quals = cp_type_quals (type))
9173 tree unqual = build_ptrmemfunc_type (TYPE_MAIN_VARIANT (type));
9174 return cp_build_qualified_type (unqual, quals);
9177 /* If a canonical type already exists for this type, use it. We use
9178 this method instead of type_hash_canon, because it only does a
9179 simple equality check on the list of field members. */
9181 if ((t = TYPE_GET_PTRMEMFUNC_TYPE (type)))
9182 return t;
9184 t = make_node (RECORD_TYPE);
9186 /* Let the front end know this is a pointer to member function. */
9187 TYPE_PTRMEMFUNC_FLAG (t) = 1;
9189 field = build_decl (input_location, FIELD_DECL, pfn_identifier, type);
9190 fields = field;
9192 field = build_decl (input_location, FIELD_DECL, delta_identifier,
9193 delta_type_node);
9194 DECL_CHAIN (field) = fields;
9195 fields = field;
9197 finish_builtin_struct (t, "__ptrmemfunc_type", fields, ptr_type_node);
9199 /* Zap out the name so that the back end will give us the debugging
9200 information for this anonymous RECORD_TYPE. */
9201 TYPE_NAME (t) = NULL_TREE;
9203 /* Cache this pointer-to-member type so that we can find it again
9204 later. */
9205 TYPE_SET_PTRMEMFUNC_TYPE (type, t);
9207 if (TYPE_STRUCTURAL_EQUALITY_P (type))
9208 SET_TYPE_STRUCTURAL_EQUALITY (t);
9209 else if (TYPE_CANONICAL (type) != type)
9210 TYPE_CANONICAL (t) = build_ptrmemfunc_type (TYPE_CANONICAL (type));
9212 return t;
9215 /* Create and return a pointer to data member type. */
9217 tree
9218 build_ptrmem_type (tree class_type, tree member_type)
9220 if (TREE_CODE (member_type) == METHOD_TYPE)
9222 cp_cv_quals quals = type_memfn_quals (member_type);
9223 cp_ref_qualifier rqual = type_memfn_rqual (member_type);
9224 member_type = build_memfn_type (member_type, class_type, quals, rqual);
9225 return build_ptrmemfunc_type (build_pointer_type (member_type));
9227 else
9229 gcc_assert (TREE_CODE (member_type) != FUNCTION_TYPE);
9230 return build_offset_type (class_type, member_type);
9234 /* DECL is a VAR_DECL defined in-class, whose TYPE is also given.
9235 Check to see that the definition is valid. Issue appropriate error
9236 messages. Return 1 if the definition is particularly bad, or 0
9237 otherwise. */
9239 static int
9240 check_static_variable_definition (tree decl, tree type)
9242 /* Avoid redundant diagnostics on out-of-class definitions. */
9243 if (!current_class_type || !TYPE_BEING_DEFINED (current_class_type))
9244 return 0;
9245 /* Can't check yet if we don't know the type. */
9246 if (dependent_type_p (type))
9247 return 0;
9248 /* If DECL is declared constexpr, we'll do the appropriate checks
9249 in check_initializer. Similarly for inline static data members. */
9250 if (DECL_P (decl)
9251 && (DECL_DECLARED_CONSTEXPR_P (decl)
9252 || DECL_VAR_DECLARED_INLINE_P (decl)))
9253 return 0;
9254 else if (cxx_dialect >= cxx11 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
9256 if (!COMPLETE_TYPE_P (type))
9257 error_at (DECL_SOURCE_LOCATION (decl),
9258 "in-class initialization of static data member %q#D of "
9259 "incomplete type", decl);
9260 else if (literal_type_p (type))
9261 permerror (DECL_SOURCE_LOCATION (decl),
9262 "%<constexpr%> needed for in-class initialization of "
9263 "static data member %q#D of non-integral type", decl);
9264 else
9265 error_at (DECL_SOURCE_LOCATION (decl),
9266 "in-class initialization of static data member %q#D of "
9267 "non-literal type", decl);
9268 return 1;
9271 /* Motion 10 at San Diego: If a static const integral data member is
9272 initialized with an integral constant expression, the initializer
9273 may appear either in the declaration (within the class), or in
9274 the definition, but not both. If it appears in the class, the
9275 member is a member constant. The file-scope definition is always
9276 required. */
9277 if (!ARITHMETIC_TYPE_P (type) && TREE_CODE (type) != ENUMERAL_TYPE)
9279 error_at (DECL_SOURCE_LOCATION (decl),
9280 "invalid in-class initialization of static data member "
9281 "of non-integral type %qT",
9282 type);
9283 return 1;
9285 else if (!CP_TYPE_CONST_P (type))
9286 error_at (DECL_SOURCE_LOCATION (decl),
9287 "ISO C++ forbids in-class initialization of non-const "
9288 "static member %qD",
9289 decl);
9290 else if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type))
9291 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
9292 "ISO C++ forbids initialization of member constant "
9293 "%qD of non-integral type %qT", decl, type);
9295 return 0;
9298 /* *expr_p is part of the TYPE_SIZE of a variably-sized array. If any
9299 SAVE_EXPRs in *expr_p wrap expressions with side-effects, break those
9300 expressions out into temporary variables so that walk_tree doesn't
9301 step into them (c++/15764). */
9303 static tree
9304 stabilize_save_expr_r (tree *expr_p, int *walk_subtrees, void *data)
9306 hash_set<tree> *pset = (hash_set<tree> *)data;
9307 tree expr = *expr_p;
9308 if (TREE_CODE (expr) == SAVE_EXPR)
9310 tree op = TREE_OPERAND (expr, 0);
9311 cp_walk_tree (&op, stabilize_save_expr_r, data, pset);
9312 if (TREE_SIDE_EFFECTS (op))
9313 TREE_OPERAND (expr, 0) = get_temp_regvar (TREE_TYPE (op), op);
9314 *walk_subtrees = 0;
9316 else if (!EXPR_P (expr) || !TREE_SIDE_EFFECTS (expr))
9317 *walk_subtrees = 0;
9318 return NULL;
9321 /* Entry point for the above. */
9323 static void
9324 stabilize_vla_size (tree size)
9326 hash_set<tree> pset;
9327 /* Break out any function calls into temporary variables. */
9328 cp_walk_tree (&size, stabilize_save_expr_r, &pset, &pset);
9331 /* Reduce a SIZEOF_EXPR to its value. */
9333 tree
9334 fold_sizeof_expr (tree t)
9336 tree r;
9337 if (SIZEOF_EXPR_TYPE_P (t))
9338 r = cxx_sizeof_or_alignof_type (TREE_TYPE (TREE_OPERAND (t, 0)),
9339 SIZEOF_EXPR, false);
9340 else if (TYPE_P (TREE_OPERAND (t, 0)))
9341 r = cxx_sizeof_or_alignof_type (TREE_OPERAND (t, 0), SIZEOF_EXPR,
9342 false);
9343 else
9344 r = cxx_sizeof_or_alignof_expr (TREE_OPERAND (t, 0), SIZEOF_EXPR,
9345 false);
9346 if (r == error_mark_node)
9347 r = size_one_node;
9348 return r;
9351 /* Given the SIZE (i.e., number of elements) in an array, compute
9352 an appropriate index type for the array. If non-NULL, NAME is
9353 the name of the entity being declared. */
9355 tree
9356 compute_array_index_type (tree name, tree size, tsubst_flags_t complain)
9358 tree itype;
9359 tree osize = size;
9361 if (error_operand_p (size))
9362 return error_mark_node;
9364 if (!type_dependent_expression_p (size))
9366 tree type = TREE_TYPE (size);
9368 mark_rvalue_use (size);
9370 if (cxx_dialect < cxx11 && TREE_CODE (size) == NOP_EXPR
9371 && TREE_SIDE_EFFECTS (size))
9372 /* In C++98, we mark a non-constant array bound with a magic
9373 NOP_EXPR with TREE_SIDE_EFFECTS; don't fold in that case. */;
9374 else
9376 size = instantiate_non_dependent_expr_sfinae (size, complain);
9378 if (CLASS_TYPE_P (type)
9379 && CLASSTYPE_LITERAL_P (type))
9381 size = build_expr_type_conversion (WANT_INT, size, true);
9382 if (!size)
9384 if (!(complain & tf_error))
9385 return error_mark_node;
9386 if (name)
9387 error ("size of array %qD has non-integral type %qT",
9388 name, type);
9389 else
9390 error ("size of array has non-integral type %qT", type);
9391 size = integer_one_node;
9393 if (size == error_mark_node)
9394 return error_mark_node;
9395 type = TREE_TYPE (size);
9398 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
9399 size = maybe_constant_value (size);
9401 if (!TREE_CONSTANT (size))
9402 size = osize;
9405 if (error_operand_p (size))
9406 return error_mark_node;
9408 /* The array bound must be an integer type. */
9409 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
9411 if (!(complain & tf_error))
9412 return error_mark_node;
9413 if (name)
9414 error ("size of array %qD has non-integral type %qT", name, type);
9415 else
9416 error ("size of array has non-integral type %qT", type);
9417 size = integer_one_node;
9418 type = TREE_TYPE (size);
9422 /* A type is dependent if it is...an array type constructed from any
9423 dependent type or whose size is specified by a constant expression
9424 that is value-dependent. */
9425 /* We can only call value_dependent_expression_p on integral constant
9426 expressions; treat non-constant expressions as dependent, too. */
9427 if (processing_template_decl
9428 && (type_dependent_expression_p (size)
9429 || !TREE_CONSTANT (size) || value_dependent_expression_p (size)))
9431 /* We cannot do any checking for a SIZE that isn't known to be
9432 constant. Just build the index type and mark that it requires
9433 structural equality checks. */
9434 itype = build_index_type (build_min (MINUS_EXPR, sizetype,
9435 size, size_one_node));
9436 TYPE_DEPENDENT_P (itype) = 1;
9437 TYPE_DEPENDENT_P_VALID (itype) = 1;
9438 SET_TYPE_STRUCTURAL_EQUALITY (itype);
9439 return itype;
9442 if (TREE_CODE (size) != INTEGER_CST)
9444 tree folded = cp_fully_fold (size);
9445 if (TREE_CODE (folded) == INTEGER_CST)
9446 pedwarn (location_of (size), OPT_Wpedantic,
9447 "size of array is not an integral constant-expression");
9448 /* Use the folded result for VLAs, too; it will have resolved
9449 SIZEOF_EXPR. */
9450 size = folded;
9453 /* Normally, the array-bound will be a constant. */
9454 if (TREE_CODE (size) == INTEGER_CST)
9456 /* Check to see if the array bound overflowed. Make that an
9457 error, no matter how generous we're being. */
9458 constant_expression_error (size);
9460 /* An array must have a positive number of elements. */
9461 if (tree_int_cst_lt (size, integer_zero_node))
9463 if (!(complain & tf_error))
9464 return error_mark_node;
9465 if (name)
9466 error ("size of array %qD is negative", name);
9467 else
9468 error ("size of array is negative");
9469 size = integer_one_node;
9471 /* As an extension we allow zero-sized arrays. */
9472 else if (integer_zerop (size))
9474 if (!(complain & tf_error))
9475 /* We must fail if performing argument deduction (as
9476 indicated by the state of complain), so that
9477 another substitution can be found. */
9478 return error_mark_node;
9479 else if (in_system_header_at (input_location))
9480 /* Allow them in system headers because glibc uses them. */;
9481 else if (name)
9482 pedwarn (input_location, OPT_Wpedantic, "ISO C++ forbids zero-size array %qD", name);
9483 else
9484 pedwarn (input_location, OPT_Wpedantic, "ISO C++ forbids zero-size array");
9487 else if (TREE_CONSTANT (size)
9488 /* We don't allow VLAs at non-function scopes, or during
9489 tentative template substitution. */
9490 || !at_function_scope_p ()
9491 || !(complain & tf_error))
9493 if (!(complain & tf_error))
9494 return error_mark_node;
9495 /* `(int) &fn' is not a valid array bound. */
9496 if (name)
9497 error ("size of array %qD is not an integral constant-expression",
9498 name);
9499 else
9500 error ("size of array is not an integral constant-expression");
9501 size = integer_one_node;
9503 else if (pedantic && warn_vla != 0)
9505 if (name)
9506 pedwarn (input_location, OPT_Wvla, "ISO C++ forbids variable length array %qD", name);
9507 else
9508 pedwarn (input_location, OPT_Wvla, "ISO C++ forbids variable length array");
9510 else if (warn_vla > 0)
9512 if (name)
9513 warning (OPT_Wvla,
9514 "variable length array %qD is used", name);
9515 else
9516 warning (OPT_Wvla,
9517 "variable length array is used");
9520 if (processing_template_decl && !TREE_CONSTANT (size))
9521 /* A variable sized array. */
9522 itype = build_min (MINUS_EXPR, sizetype, size, integer_one_node);
9523 else
9525 HOST_WIDE_INT saved_processing_template_decl;
9527 /* Compute the index of the largest element in the array. It is
9528 one less than the number of elements in the array. We save
9529 and restore PROCESSING_TEMPLATE_DECL so that computations in
9530 cp_build_binary_op will be appropriately folded. */
9531 saved_processing_template_decl = processing_template_decl;
9532 processing_template_decl = 0;
9533 itype = cp_build_binary_op (input_location,
9534 MINUS_EXPR,
9535 cp_convert (ssizetype, size, complain),
9536 cp_convert (ssizetype, integer_one_node,
9537 complain),
9538 complain);
9539 itype = maybe_constant_value (itype);
9540 processing_template_decl = saved_processing_template_decl;
9542 if (!TREE_CONSTANT (itype))
9544 /* A variable sized array. */
9545 itype = variable_size (itype);
9547 stabilize_vla_size (itype);
9549 if (flag_sanitize & SANITIZE_VLA
9550 && do_ubsan_in_current_function ())
9552 /* We have to add 1 -- in the ubsan routine we generate
9553 LE_EXPR rather than LT_EXPR. */
9554 tree t = fold_build2 (PLUS_EXPR, TREE_TYPE (itype), itype,
9555 build_one_cst (TREE_TYPE (itype)));
9556 t = ubsan_instrument_vla (input_location, t);
9557 finish_expr_stmt (t);
9560 /* Make sure that there was no overflow when creating to a signed
9561 index type. (For example, on a 32-bit machine, an array with
9562 size 2^32 - 1 is too big.) */
9563 else if (TREE_CODE (itype) == INTEGER_CST
9564 && TREE_OVERFLOW (itype))
9566 if (!(complain & tf_error))
9567 return error_mark_node;
9568 error ("overflow in array dimension");
9569 TREE_OVERFLOW (itype) = 0;
9573 /* Create and return the appropriate index type. */
9574 itype = build_index_type (itype);
9576 /* If the index type were dependent, we would have returned early, so
9577 remember that it isn't. */
9578 TYPE_DEPENDENT_P (itype) = 0;
9579 TYPE_DEPENDENT_P_VALID (itype) = 1;
9580 return itype;
9583 /* Returns the scope (if any) in which the entity declared by
9584 DECLARATOR will be located. If the entity was declared with an
9585 unqualified name, NULL_TREE is returned. */
9587 tree
9588 get_scope_of_declarator (const cp_declarator *declarator)
9590 while (declarator && declarator->kind != cdk_id)
9591 declarator = declarator->declarator;
9593 /* If the declarator-id is a SCOPE_REF, the scope in which the
9594 declaration occurs is the first operand. */
9595 if (declarator
9596 && declarator->u.id.qualifying_scope)
9597 return declarator->u.id.qualifying_scope;
9599 /* Otherwise, the declarator is not a qualified name; the entity will
9600 be declared in the current scope. */
9601 return NULL_TREE;
9604 /* Returns an ARRAY_TYPE for an array with SIZE elements of the
9605 indicated TYPE. If non-NULL, NAME is the NAME of the declaration
9606 with this type. */
9608 static tree
9609 create_array_type_for_decl (tree name, tree type, tree size)
9611 tree itype = NULL_TREE;
9613 /* If things have already gone awry, bail now. */
9614 if (type == error_mark_node || size == error_mark_node)
9615 return error_mark_node;
9617 /* 8.3.4/1: If the type of the identifier of D contains the auto
9618 type-specifier, the program is ill-formed. */
9619 if (type_uses_auto (type))
9621 error ("%qD declared as array of %qT", name, type);
9622 return error_mark_node;
9625 /* If there are some types which cannot be array elements,
9626 issue an error-message and return. */
9627 switch (TREE_CODE (type))
9629 case VOID_TYPE:
9630 if (name)
9631 error ("declaration of %qD as array of void", name);
9632 else
9633 error ("creating array of void");
9634 return error_mark_node;
9636 case FUNCTION_TYPE:
9637 if (name)
9638 error ("declaration of %qD as array of functions", name);
9639 else
9640 error ("creating array of functions");
9641 return error_mark_node;
9643 case REFERENCE_TYPE:
9644 if (name)
9645 error ("declaration of %qD as array of references", name);
9646 else
9647 error ("creating array of references");
9648 return error_mark_node;
9650 case METHOD_TYPE:
9651 if (name)
9652 error ("declaration of %qD as array of function members", name);
9653 else
9654 error ("creating array of function members");
9655 return error_mark_node;
9657 default:
9658 break;
9661 /* [dcl.array]
9663 The constant expressions that specify the bounds of the arrays
9664 can be omitted only for the first member of the sequence. */
9665 if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type))
9667 if (name)
9668 error ("declaration of %qD as multidimensional array must "
9669 "have bounds for all dimensions except the first",
9670 name);
9671 else
9672 error ("multidimensional array must have bounds for all "
9673 "dimensions except the first");
9675 return error_mark_node;
9678 /* Figure out the index type for the array. */
9679 if (size)
9680 itype = compute_array_index_type (name, size, tf_warning_or_error);
9682 /* [dcl.array]
9683 T is called the array element type; this type shall not be [...] an
9684 abstract class type. */
9685 abstract_virtuals_error (name, type);
9687 return build_cplus_array_type (type, itype);
9690 /* Returns the smallest location != UNKNOWN_LOCATION among the
9691 three stored in LOCATIONS[ds_const], LOCATIONS[ds_volatile],
9692 and LOCATIONS[ds_restrict]. */
9694 static location_t
9695 smallest_type_quals_location (int type_quals, const location_t* locations)
9697 location_t loc = UNKNOWN_LOCATION;
9699 if (type_quals & TYPE_QUAL_CONST)
9700 loc = locations[ds_const];
9702 if ((type_quals & TYPE_QUAL_VOLATILE)
9703 && (loc == UNKNOWN_LOCATION || locations[ds_volatile] < loc))
9704 loc = locations[ds_volatile];
9706 if ((type_quals & TYPE_QUAL_RESTRICT)
9707 && (loc == UNKNOWN_LOCATION || locations[ds_restrict] < loc))
9708 loc = locations[ds_restrict];
9710 return loc;
9713 /* Check that it's OK to declare a function with the indicated TYPE
9714 and TYPE_QUALS. SFK indicates the kind of special function (if any)
9715 that this function is. OPTYPE is the type given in a conversion
9716 operator declaration, or the class type for a constructor/destructor.
9717 Returns the actual return type of the function; that may be different
9718 than TYPE if an error occurs, or for certain special functions. */
9720 static tree
9721 check_special_function_return_type (special_function_kind sfk,
9722 tree type,
9723 tree optype,
9724 int type_quals,
9725 const location_t* locations)
9727 switch (sfk)
9729 case sfk_constructor:
9730 if (type)
9731 error ("return type specification for constructor invalid");
9732 else if (type_quals != TYPE_UNQUALIFIED)
9733 error_at (smallest_type_quals_location (type_quals, locations),
9734 "qualifiers are not allowed on constructor declaration");
9736 if (targetm.cxx.cdtor_returns_this ())
9737 type = build_pointer_type (optype);
9738 else
9739 type = void_type_node;
9740 break;
9742 case sfk_destructor:
9743 if (type)
9744 error ("return type specification for destructor invalid");
9745 else if (type_quals != TYPE_UNQUALIFIED)
9746 error_at (smallest_type_quals_location (type_quals, locations),
9747 "qualifiers are not allowed on destructor declaration");
9749 /* We can't use the proper return type here because we run into
9750 problems with ambiguous bases and covariant returns. */
9751 if (targetm.cxx.cdtor_returns_this ())
9752 type = build_pointer_type (void_type_node);
9753 else
9754 type = void_type_node;
9755 break;
9757 case sfk_conversion:
9758 if (type)
9759 error ("return type specified for %<operator %T%>", optype);
9760 else if (type_quals != TYPE_UNQUALIFIED)
9761 error_at (smallest_type_quals_location (type_quals, locations),
9762 "qualifiers are not allowed on declaration of "
9763 "%<operator %T%>", optype);
9765 type = optype;
9766 break;
9768 case sfk_deduction_guide:
9769 if (type)
9770 error ("return type specified for deduction guide");
9771 else if (type_quals != TYPE_UNQUALIFIED)
9772 error_at (smallest_type_quals_location (type_quals, locations),
9773 "qualifiers are not allowed on declaration of "
9774 "deduction guide");
9775 type = make_template_placeholder (CLASSTYPE_TI_TEMPLATE (optype));
9776 for (int i = 0; i < ds_last; ++i)
9777 if (i != ds_explicit && locations[i])
9778 error_at (locations[i],
9779 "decl-specifier in declaration of deduction guide");
9780 break;
9782 default:
9783 gcc_unreachable ();
9786 return type;
9789 /* A variable or data member (whose unqualified name is IDENTIFIER)
9790 has been declared with the indicated TYPE. If the TYPE is not
9791 acceptable, issue an error message and return a type to use for
9792 error-recovery purposes. */
9794 tree
9795 check_var_type (tree identifier, tree type)
9797 if (VOID_TYPE_P (type))
9799 if (!identifier)
9800 error ("unnamed variable or field declared void");
9801 else if (identifier_p (identifier))
9803 gcc_assert (!IDENTIFIER_OPNAME_P (identifier));
9804 error ("variable or field %qE declared void", identifier);
9806 else
9807 error ("variable or field declared void");
9808 type = error_mark_node;
9811 return type;
9814 /* Handle declaring DECL as an inline variable. */
9816 static void
9817 mark_inline_variable (tree decl)
9819 bool inlinep = true;
9820 if (! toplevel_bindings_p ())
9822 error ("%<inline%> specifier invalid for variable "
9823 "%qD declared at block scope", decl);
9824 inlinep = false;
9826 else if (cxx_dialect < cxx1z)
9827 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
9828 "inline variables are only available "
9829 "with -std=c++1z or -std=gnu++1z");
9830 if (inlinep)
9832 retrofit_lang_decl (decl);
9833 SET_DECL_VAR_DECLARED_INLINE_P (decl);
9837 /* Given declspecs and a declarator (abstract or otherwise), determine
9838 the name and type of the object declared and construct a DECL node
9839 for it.
9841 DECLSPECS points to the representation of declaration-specifier
9842 sequence that precedes declarator.
9844 DECL_CONTEXT says which syntactic context this declaration is in:
9845 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
9846 FUNCDEF for a function definition. Like NORMAL but a few different
9847 error messages in each case. Return value may be zero meaning
9848 this definition is too screwy to try to parse.
9849 MEMFUNCDEF for a function definition. Like FUNCDEF but prepares to
9850 handle member functions (which have FIELD context).
9851 Return value may be zero meaning this definition is too screwy to
9852 try to parse.
9853 PARM for a parameter declaration (either within a function prototype
9854 or before a function body). Make a PARM_DECL, or return void_type_node.
9855 TPARM for a template parameter declaration.
9856 CATCHPARM for a parameter declaration before a catch clause.
9857 TYPENAME if for a typename (in a cast or sizeof).
9858 Don't make a DECL node; just return the ..._TYPE node.
9859 FIELD for a struct or union field; make a FIELD_DECL.
9860 BITFIELD for a field with specified width.
9862 INITIALIZED is as for start_decl.
9864 ATTRLIST is a pointer to the list of attributes, which may be NULL
9865 if there are none; *ATTRLIST may be modified if attributes from inside
9866 the declarator should be applied to the declaration.
9868 When this function is called, scoping variables (such as
9869 CURRENT_CLASS_TYPE) should reflect the scope in which the
9870 declaration occurs, not the scope in which the new declaration will
9871 be placed. For example, on:
9873 void S::f() { ... }
9875 when grokdeclarator is called for `S::f', the CURRENT_CLASS_TYPE
9876 should not be `S'.
9878 Returns a DECL (if a declarator is present), a TYPE (if there is no
9879 declarator, in cases like "struct S;"), or the ERROR_MARK_NODE if an
9880 error occurs. */
9882 tree
9883 grokdeclarator (const cp_declarator *declarator,
9884 cp_decl_specifier_seq *declspecs,
9885 enum decl_context decl_context,
9886 int initialized,
9887 tree* attrlist)
9889 tree type = NULL_TREE;
9890 int longlong = 0;
9891 int explicit_intN = 0;
9892 int virtualp, explicitp, friendp, inlinep, staticp;
9893 int explicit_int = 0;
9894 int explicit_char = 0;
9895 int defaulted_int = 0;
9897 tree typedef_decl = NULL_TREE;
9898 const char *name = NULL;
9899 tree typedef_type = NULL_TREE;
9900 /* True if this declarator is a function definition. */
9901 bool funcdef_flag = false;
9902 cp_declarator_kind innermost_code = cdk_error;
9903 int bitfield = 0;
9904 #if 0
9905 /* See the code below that used this. */
9906 tree decl_attr = NULL_TREE;
9907 #endif
9909 /* Keep track of what sort of function is being processed
9910 so that we can warn about default return values, or explicit
9911 return values which do not match prescribed defaults. */
9912 special_function_kind sfk = sfk_none;
9914 tree dname = NULL_TREE;
9915 tree ctor_return_type = NULL_TREE;
9916 enum overload_flags flags = NO_SPECIAL;
9917 /* cv-qualifiers that apply to the declarator, for a declaration of
9918 a member function. */
9919 cp_cv_quals memfn_quals = TYPE_UNQUALIFIED;
9920 /* virt-specifiers that apply to the declarator, for a declaration of
9921 a member function. */
9922 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
9923 /* ref-qualifier that applies to the declarator, for a declaration of
9924 a member function. */
9925 cp_ref_qualifier rqual = REF_QUAL_NONE;
9926 /* cv-qualifiers that apply to the type specified by the DECLSPECS. */
9927 int type_quals = TYPE_UNQUALIFIED;
9928 tree raises = NULL_TREE;
9929 int template_count = 0;
9930 tree returned_attrs = NULL_TREE;
9931 tree parms = NULL_TREE;
9932 const cp_declarator *id_declarator;
9933 /* The unqualified name of the declarator; either an
9934 IDENTIFIER_NODE, BIT_NOT_EXPR, or TEMPLATE_ID_EXPR. */
9935 tree unqualified_id;
9936 /* The class type, if any, in which this entity is located,
9937 or NULL_TREE if none. Note that this value may be different from
9938 the current class type; for example if an attempt is made to declare
9939 "A::f" inside "B", this value will be "A". */
9940 tree ctype = current_class_type;
9941 /* The NAMESPACE_DECL for the namespace in which this entity is
9942 located. If an unqualified name is used to declare the entity,
9943 this value will be NULL_TREE, even if the entity is located at
9944 namespace scope. */
9945 tree in_namespace = NULL_TREE;
9946 cp_storage_class storage_class;
9947 bool unsigned_p, signed_p, short_p, long_p, thread_p;
9948 bool type_was_error_mark_node = false;
9949 bool parameter_pack_p = declarator ? declarator->parameter_pack_p : false;
9950 bool template_type_arg = false;
9951 bool template_parm_flag = false;
9952 bool typedef_p = decl_spec_seq_has_spec_p (declspecs, ds_typedef);
9953 bool constexpr_p = decl_spec_seq_has_spec_p (declspecs, ds_constexpr);
9954 bool late_return_type_p = false;
9955 bool array_parameter_p = false;
9956 source_location saved_loc = input_location;
9957 tree reqs = NULL_TREE;
9959 signed_p = decl_spec_seq_has_spec_p (declspecs, ds_signed);
9960 unsigned_p = decl_spec_seq_has_spec_p (declspecs, ds_unsigned);
9961 short_p = decl_spec_seq_has_spec_p (declspecs, ds_short);
9962 long_p = decl_spec_seq_has_spec_p (declspecs, ds_long);
9963 longlong = decl_spec_seq_has_spec_p (declspecs, ds_long_long);
9964 explicit_intN = declspecs->explicit_intN_p;
9965 thread_p = decl_spec_seq_has_spec_p (declspecs, ds_thread);
9967 // Was concept_p specified? Note that ds_concept
9968 // implies ds_constexpr!
9969 bool concept_p = decl_spec_seq_has_spec_p (declspecs, ds_concept);
9970 if (concept_p)
9971 constexpr_p = true;
9973 if (decl_spec_seq_has_spec_p (declspecs, ds_const))
9974 type_quals |= TYPE_QUAL_CONST;
9975 if (decl_spec_seq_has_spec_p (declspecs, ds_volatile))
9976 type_quals |= TYPE_QUAL_VOLATILE;
9977 if (decl_spec_seq_has_spec_p (declspecs, ds_restrict))
9978 type_quals |= TYPE_QUAL_RESTRICT;
9980 if (decl_context == FUNCDEF)
9981 funcdef_flag = true, decl_context = NORMAL;
9982 else if (decl_context == MEMFUNCDEF)
9983 funcdef_flag = true, decl_context = FIELD;
9984 else if (decl_context == BITFIELD)
9985 bitfield = 1, decl_context = FIELD;
9986 else if (decl_context == TEMPLATE_TYPE_ARG)
9987 template_type_arg = true, decl_context = TYPENAME;
9988 else if (decl_context == TPARM)
9989 template_parm_flag = true, decl_context = PARM;
9991 if (initialized > 1)
9992 funcdef_flag = true;
9994 location_t typespec_loc = smallest_type_quals_location (type_quals,
9995 declspecs->locations);
9996 if (typespec_loc == UNKNOWN_LOCATION)
9997 typespec_loc = declspecs->locations[ds_type_spec];
9999 /* Look inside a declarator for the name being declared
10000 and get it as a string, for an error message. */
10001 for (id_declarator = declarator;
10002 id_declarator;
10003 id_declarator = id_declarator->declarator)
10005 if (id_declarator->kind != cdk_id)
10006 innermost_code = id_declarator->kind;
10008 switch (id_declarator->kind)
10010 case cdk_function:
10011 if (id_declarator->declarator
10012 && id_declarator->declarator->kind == cdk_id)
10014 sfk = id_declarator->declarator->u.id.sfk;
10015 if (sfk == sfk_destructor)
10016 flags = DTOR_FLAG;
10018 break;
10020 case cdk_id:
10022 tree qualifying_scope = id_declarator->u.id.qualifying_scope;
10023 tree decl = id_declarator->u.id.unqualified_name;
10024 if (!decl)
10025 break;
10026 if (qualifying_scope)
10028 if (at_function_scope_p ())
10030 /* [dcl.meaning]
10032 A declarator-id shall not be qualified except
10033 for ...
10035 None of the cases are permitted in block
10036 scope. */
10037 if (qualifying_scope == global_namespace)
10038 error ("invalid use of qualified-name %<::%D%>",
10039 decl);
10040 else if (TYPE_P (qualifying_scope))
10041 error ("invalid use of qualified-name %<%T::%D%>",
10042 qualifying_scope, decl);
10043 else
10044 error ("invalid use of qualified-name %<%D::%D%>",
10045 qualifying_scope, decl);
10046 return error_mark_node;
10048 else if (TYPE_P (qualifying_scope))
10050 ctype = qualifying_scope;
10051 if (!MAYBE_CLASS_TYPE_P (ctype))
10053 error ("%q#T is not a class or a namespace", ctype);
10054 ctype = NULL_TREE;
10056 else if (innermost_code != cdk_function
10057 && current_class_type
10058 && !uniquely_derived_from_p (ctype,
10059 current_class_type))
10061 error ("invalid use of qualified-name %<%T::%D%>",
10062 qualifying_scope, decl);
10063 return error_mark_node;
10066 else if (TREE_CODE (qualifying_scope) == NAMESPACE_DECL)
10067 in_namespace = qualifying_scope;
10069 switch (TREE_CODE (decl))
10071 case BIT_NOT_EXPR:
10073 tree type;
10075 if (innermost_code != cdk_function)
10077 error ("declaration of %qD as non-function", decl);
10078 return error_mark_node;
10080 else if (!qualifying_scope
10081 && !(current_class_type && at_class_scope_p ()))
10083 error ("declaration of %qD as non-member", decl);
10084 return error_mark_node;
10087 type = TREE_OPERAND (decl, 0);
10088 if (TYPE_P (type))
10089 type = constructor_name (type);
10090 name = identifier_to_locale (IDENTIFIER_POINTER (type));
10091 dname = decl;
10093 break;
10095 case TEMPLATE_ID_EXPR:
10097 tree fns = TREE_OPERAND (decl, 0);
10099 dname = fns;
10100 if (!identifier_p (dname))
10102 if (variable_template_p (dname))
10103 dname = DECL_NAME (dname);
10104 else
10106 gcc_assert (is_overloaded_fn (dname));
10107 dname = DECL_NAME (get_first_fn (dname));
10111 /* Fall through. */
10113 case IDENTIFIER_NODE:
10114 if (identifier_p (decl))
10115 dname = decl;
10117 if (C_IS_RESERVED_WORD (dname))
10119 error ("declarator-id missing; using reserved word %qD",
10120 dname);
10121 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
10123 else if (!IDENTIFIER_TYPENAME_P (dname))
10124 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
10125 else
10127 gcc_assert (flags == NO_SPECIAL);
10128 flags = TYPENAME_FLAG;
10129 sfk = sfk_conversion;
10130 if (is_typename_at_global_scope (dname))
10131 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
10132 else
10133 name = "<invalid operator>";
10135 break;
10137 default:
10138 gcc_unreachable ();
10140 break;
10143 case cdk_array:
10144 case cdk_pointer:
10145 case cdk_reference:
10146 case cdk_ptrmem:
10147 break;
10149 case cdk_decomp:
10150 name = "decomposition";
10151 break;
10153 case cdk_error:
10154 return error_mark_node;
10156 default:
10157 gcc_unreachable ();
10159 if (id_declarator->kind == cdk_id)
10160 break;
10163 /* [dcl.fct.edf]
10165 The declarator in a function-definition shall have the form
10166 D1 ( parameter-declaration-clause) ... */
10167 if (funcdef_flag && innermost_code != cdk_function)
10169 error ("function definition does not declare parameters");
10170 return error_mark_node;
10173 if (flags == TYPENAME_FLAG
10174 && innermost_code != cdk_function
10175 && ! (ctype && !declspecs->any_specifiers_p))
10177 error ("declaration of %qD as non-function", dname);
10178 return error_mark_node;
10181 if (dname
10182 && identifier_p (dname)
10183 && UDLIT_OPER_P (dname)
10184 && innermost_code != cdk_function)
10186 error ("declaration of %qD as non-function", dname);
10187 return error_mark_node;
10190 if (dname && IDENTIFIER_OPNAME_P (dname))
10192 if (typedef_p)
10194 error ("declaration of %qD as %<typedef%>", dname);
10195 return error_mark_node;
10197 else if (decl_context == PARM || decl_context == CATCHPARM)
10199 error ("declaration of %qD as parameter", dname);
10200 return error_mark_node;
10204 /* Anything declared one level down from the top level
10205 must be one of the parameters of a function
10206 (because the body is at least two levels down). */
10208 /* This heuristic cannot be applied to C++ nodes! Fixed, however,
10209 by not allowing C++ class definitions to specify their parameters
10210 with xdecls (must be spec.d in the parmlist).
10212 Since we now wait to push a class scope until we are sure that
10213 we are in a legitimate method context, we must set oldcname
10214 explicitly (since current_class_name is not yet alive).
10216 We also want to avoid calling this a PARM if it is in a namespace. */
10218 if (decl_context == NORMAL && !toplevel_bindings_p ())
10220 cp_binding_level *b = current_binding_level;
10221 current_binding_level = b->level_chain;
10222 if (current_binding_level != 0 && toplevel_bindings_p ())
10223 decl_context = PARM;
10224 current_binding_level = b;
10227 if (name == NULL)
10228 name = decl_context == PARM ? "parameter" : "type name";
10230 if (concept_p && typedef_p)
10232 error ("%<concept%> cannot appear in a typedef declaration");
10233 return error_mark_node;
10236 if (constexpr_p && typedef_p)
10238 error ("%<constexpr%> cannot appear in a typedef declaration");
10239 return error_mark_node;
10242 /* If there were multiple types specified in the decl-specifier-seq,
10243 issue an error message. */
10244 if (declspecs->multiple_types_p)
10246 error ("two or more data types in declaration of %qs", name);
10247 return error_mark_node;
10250 if (declspecs->conflicting_specifiers_p)
10252 error ("conflicting specifiers in declaration of %qs", name);
10253 return error_mark_node;
10256 /* Extract the basic type from the decl-specifier-seq. */
10257 type = declspecs->type;
10258 if (type == error_mark_node)
10260 type = NULL_TREE;
10261 type_was_error_mark_node = true;
10263 /* If the entire declaration is itself tagged as deprecated then
10264 suppress reports of deprecated items. */
10265 if (type && TREE_DEPRECATED (type)
10266 && deprecated_state != DEPRECATED_SUPPRESS)
10267 warn_deprecated_use (type, NULL_TREE);
10268 if (type && TREE_CODE (type) == TYPE_DECL)
10270 typedef_decl = type;
10271 type = TREE_TYPE (typedef_decl);
10272 if (TREE_DEPRECATED (type)
10273 && DECL_ARTIFICIAL (typedef_decl)
10274 && deprecated_state != DEPRECATED_SUPPRESS)
10275 warn_deprecated_use (type, NULL_TREE);
10277 /* No type at all: default to `int', and set DEFAULTED_INT
10278 because it was not a user-defined typedef. */
10279 if (type == NULL_TREE)
10281 if (signed_p || unsigned_p || long_p || short_p)
10283 /* These imply 'int'. */
10284 type = integer_type_node;
10285 defaulted_int = 1;
10287 /* If we just have "complex", it is equivalent to "complex double". */
10288 else if (!longlong && !explicit_intN
10289 && decl_spec_seq_has_spec_p (declspecs, ds_complex))
10291 type = double_type_node;
10292 pedwarn (declspecs->locations[ds_complex], OPT_Wpedantic,
10293 "ISO C++ does not support plain %<complex%> meaning "
10294 "%<double complex%>");
10297 /* Gather flags. */
10298 explicit_int = declspecs->explicit_int_p;
10299 explicit_char = declspecs->explicit_char_p;
10301 #if 0
10302 /* See the code below that used this. */
10303 if (typedef_decl)
10304 decl_attr = DECL_ATTRIBUTES (typedef_decl);
10305 #endif
10306 typedef_type = type;
10308 if (sfk == sfk_conversion || sfk == sfk_deduction_guide)
10309 ctor_return_type = TREE_TYPE (dname);
10310 else
10311 ctor_return_type = ctype;
10313 if (sfk != sfk_none)
10315 type = check_special_function_return_type (sfk, type,
10316 ctor_return_type,
10317 type_quals,
10318 declspecs->locations);
10319 type_quals = TYPE_UNQUALIFIED;
10321 else if (type == NULL_TREE)
10323 int is_main;
10325 explicit_int = -1;
10327 /* We handle `main' specially here, because 'main () { }' is so
10328 common. With no options, it is allowed. With -Wreturn-type,
10329 it is a warning. It is only an error with -pedantic-errors. */
10330 is_main = (funcdef_flag
10331 && dname && identifier_p (dname)
10332 && MAIN_NAME_P (dname)
10333 && ctype == NULL_TREE
10334 && in_namespace == NULL_TREE
10335 && current_namespace == global_namespace);
10337 if (type_was_error_mark_node)
10338 /* We've already issued an error, don't complain more. */;
10339 else if (in_system_header_at (input_location) || flag_ms_extensions)
10340 /* Allow it, sigh. */;
10341 else if (! is_main)
10342 permerror (input_location, "ISO C++ forbids declaration of %qs with no type", name);
10343 else if (pedantic)
10344 pedwarn (input_location, OPT_Wpedantic,
10345 "ISO C++ forbids declaration of %qs with no type", name);
10346 else
10347 warning (OPT_Wreturn_type,
10348 "ISO C++ forbids declaration of %qs with no type", name);
10350 if (type_was_error_mark_node && template_parm_flag)
10351 /* FIXME we should be able to propagate the error_mark_node as is
10352 for other contexts too. */
10353 type = error_mark_node;
10354 else
10355 type = integer_type_node;
10358 ctype = NULL_TREE;
10360 if (explicit_intN)
10362 if (! int_n_enabled_p[declspecs->int_n_idx])
10364 error ("%<__int%d%> is not supported by this target",
10365 int_n_data[declspecs->int_n_idx].bitsize);
10366 explicit_intN = false;
10368 else if (pedantic && ! in_system_header_at (input_location))
10369 pedwarn (input_location, OPT_Wpedantic,
10370 "ISO C++ does not support %<__int%d%> for %qs",
10371 int_n_data[declspecs->int_n_idx].bitsize, name);
10374 /* Now process the modifiers that were specified
10375 and check for invalid combinations. */
10377 /* Long double is a special combination. */
10378 if (long_p && !longlong && TYPE_MAIN_VARIANT (type) == double_type_node)
10380 long_p = false;
10381 type = cp_build_qualified_type (long_double_type_node,
10382 cp_type_quals (type));
10385 /* Check all other uses of type modifiers. */
10387 if (unsigned_p || signed_p || long_p || short_p)
10389 int ok = 0;
10391 if ((signed_p || unsigned_p) && TREE_CODE (type) != INTEGER_TYPE)
10392 error ("%<signed%> or %<unsigned%> invalid for %qs", name);
10393 else if (signed_p && unsigned_p)
10394 error ("%<signed%> and %<unsigned%> specified together for %qs", name);
10395 else if (longlong && TREE_CODE (type) != INTEGER_TYPE)
10396 error ("%<long long%> invalid for %qs", name);
10397 else if (long_p && TREE_CODE (type) == REAL_TYPE)
10398 error ("%<long%> invalid for %qs", name);
10399 else if (short_p && TREE_CODE (type) == REAL_TYPE)
10400 error ("%<short%> invalid for %qs", name);
10401 else if ((long_p || short_p) && TREE_CODE (type) != INTEGER_TYPE)
10402 error ("%<long%> or %<short%> invalid for %qs", name);
10403 else if ((long_p || short_p || explicit_char || explicit_int) && explicit_intN)
10404 error ("%<long%>, %<int%>, %<short%>, or %<char%> invalid for %qs", name);
10405 else if ((long_p || short_p) && explicit_char)
10406 error ("%<long%> or %<short%> specified with char for %qs", name);
10407 else if (long_p && short_p)
10408 error ("%<long%> and %<short%> specified together for %qs", name);
10409 else if (type == char16_type_node || type == char32_type_node)
10411 if (signed_p || unsigned_p)
10412 error ("%<signed%> or %<unsigned%> invalid for %qs", name);
10413 else if (short_p || long_p)
10414 error ("%<short%> or %<long%> invalid for %qs", name);
10416 else
10418 ok = 1;
10419 if (!explicit_int && !defaulted_int && !explicit_char && !explicit_intN && pedantic)
10421 pedwarn (input_location, OPT_Wpedantic,
10422 "long, short, signed or unsigned used invalidly for %qs",
10423 name);
10424 if (flag_pedantic_errors)
10425 ok = 0;
10429 /* Discard the type modifiers if they are invalid. */
10430 if (! ok)
10432 unsigned_p = false;
10433 signed_p = false;
10434 long_p = false;
10435 short_p = false;
10436 longlong = 0;
10440 /* Decide whether an integer type is signed or not.
10441 Optionally treat bitfields as signed by default. */
10442 if (unsigned_p
10443 /* [class.bit]
10445 It is implementation-defined whether a plain (neither
10446 explicitly signed or unsigned) char, short, int, or long
10447 bit-field is signed or unsigned.
10449 Naturally, we extend this to long long as well. Note that
10450 this does not include wchar_t. */
10451 || (bitfield && !flag_signed_bitfields
10452 && !signed_p
10453 /* A typedef for plain `int' without `signed' can be
10454 controlled just like plain `int', but a typedef for
10455 `signed int' cannot be so controlled. */
10456 && !(typedef_decl
10457 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl))
10458 && TREE_CODE (type) == INTEGER_TYPE
10459 && !same_type_p (TYPE_MAIN_VARIANT (type), wchar_type_node)))
10461 if (explicit_intN)
10462 type = int_n_trees[declspecs->int_n_idx].unsigned_type;
10463 else if (longlong)
10464 type = long_long_unsigned_type_node;
10465 else if (long_p)
10466 type = long_unsigned_type_node;
10467 else if (short_p)
10468 type = short_unsigned_type_node;
10469 else if (type == char_type_node)
10470 type = unsigned_char_type_node;
10471 else if (typedef_decl)
10472 type = unsigned_type_for (type);
10473 else
10474 type = unsigned_type_node;
10476 else if (signed_p && type == char_type_node)
10477 type = signed_char_type_node;
10478 else if (explicit_intN)
10479 type = int_n_trees[declspecs->int_n_idx].signed_type;
10480 else if (longlong)
10481 type = long_long_integer_type_node;
10482 else if (long_p)
10483 type = long_integer_type_node;
10484 else if (short_p)
10485 type = short_integer_type_node;
10487 if (decl_spec_seq_has_spec_p (declspecs, ds_complex))
10489 if (TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
10490 error ("complex invalid for %qs", name);
10491 /* If a modifier is specified, the resulting complex is the complex
10492 form of TYPE. E.g, "complex short" is "complex short int". */
10493 else if (type == integer_type_node)
10494 type = complex_integer_type_node;
10495 else if (type == float_type_node)
10496 type = complex_float_type_node;
10497 else if (type == double_type_node)
10498 type = complex_double_type_node;
10499 else if (type == long_double_type_node)
10500 type = complex_long_double_type_node;
10501 else
10502 type = build_complex_type (type);
10505 /* If we're using the injected-class-name to form a compound type or a
10506 declaration, replace it with the underlying class so we don't get
10507 redundant typedefs in the debug output. But if we are returning the
10508 type unchanged, leave it alone so that it's available to
10509 maybe_get_template_decl_from_type_decl. */
10510 if (CLASS_TYPE_P (type)
10511 && DECL_SELF_REFERENCE_P (TYPE_NAME (type))
10512 && type == TREE_TYPE (TYPE_NAME (type))
10513 && (declarator || type_quals))
10514 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
10516 type_quals |= cp_type_quals (type);
10517 type = cp_build_qualified_type_real
10518 (type, type_quals, ((((typedef_decl && !DECL_ARTIFICIAL (typedef_decl))
10519 || declspecs->decltype_p)
10520 ? tf_ignore_bad_quals : 0) | tf_warning_or_error));
10521 /* We might have ignored or rejected some of the qualifiers. */
10522 type_quals = cp_type_quals (type);
10524 if (cxx_dialect >= cxx1z && type && is_auto (type)
10525 && innermost_code != cdk_function
10526 && id_declarator && declarator != id_declarator)
10527 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (type))
10529 error_at (typespec_loc, "template placeholder type %qT must be followed "
10530 "by a simple declarator-id", type);
10531 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here", tmpl);
10534 staticp = 0;
10535 inlinep = decl_spec_seq_has_spec_p (declspecs, ds_inline);
10536 virtualp = decl_spec_seq_has_spec_p (declspecs, ds_virtual);
10537 explicitp = decl_spec_seq_has_spec_p (declspecs, ds_explicit);
10539 storage_class = declspecs->storage_class;
10540 if (storage_class == sc_static)
10541 staticp = 1 + (decl_context == FIELD);
10543 if (virtualp)
10545 if (staticp == 2)
10547 error ("member %qD cannot be declared both %<virtual%> "
10548 "and %<static%>", dname);
10549 storage_class = sc_none;
10550 staticp = 0;
10552 if (constexpr_p)
10553 error ("member %qD cannot be declared both %<virtual%> "
10554 "and %<constexpr%>", dname);
10556 friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
10558 /* Issue errors about use of storage classes for parameters. */
10559 if (decl_context == PARM)
10561 if (typedef_p)
10563 error ("typedef declaration invalid in parameter declaration");
10564 return error_mark_node;
10566 else if (template_parm_flag && storage_class != sc_none)
10568 error ("storage class specified for template parameter %qs", name);
10569 return error_mark_node;
10571 else if (storage_class == sc_static
10572 || storage_class == sc_extern
10573 || thread_p)
10574 error ("storage class specifiers invalid in parameter declarations");
10576 /* Function parameters cannot be concept. */
10577 if (concept_p)
10578 error ("a parameter cannot be declared %<concept%>");
10579 /* Function parameters cannot be constexpr. If we saw one, moan
10580 and pretend it wasn't there. */
10581 else if (constexpr_p)
10583 error ("a parameter cannot be declared %<constexpr%>");
10584 constexpr_p = 0;
10588 /* Give error if `virtual' is used outside of class declaration. */
10589 if (virtualp
10590 && (current_class_name == NULL_TREE || decl_context != FIELD))
10592 error_at (declspecs->locations[ds_virtual],
10593 "%<virtual%> outside class declaration");
10594 virtualp = 0;
10597 if (innermost_code == cdk_decomp)
10599 location_t loc = (declarator->kind == cdk_reference
10600 ? declarator->declarator->id_loc : declarator->id_loc);
10601 if (inlinep)
10602 error_at (declspecs->locations[ds_inline],
10603 "decomposition declaration cannot be declared %<inline%>");
10604 if (typedef_p)
10605 error_at (declspecs->locations[ds_typedef],
10606 "decomposition declaration cannot be declared %<typedef%>");
10607 if (constexpr_p)
10608 error_at (declspecs->locations[ds_constexpr], "decomposition "
10609 "declaration cannot be declared %<constexpr%>");
10610 if (thread_p)
10611 error_at (declspecs->locations[ds_thread],
10612 "decomposition declaration cannot be declared %qs",
10613 declspecs->gnu_thread_keyword_p
10614 ? "__thread" : "thread_local");
10615 if (concept_p)
10616 error_at (declspecs->locations[ds_concept],
10617 "decomposition declaration cannot be declared %<concept%>");
10618 switch (storage_class)
10620 case sc_none:
10621 break;
10622 case sc_register:
10623 error_at (loc, "decomposition declaration cannot be declared "
10624 "%<register%>");
10625 break;
10626 case sc_static:
10627 error_at (loc, "decomposition declaration cannot be declared "
10628 "%<static%>");
10629 break;
10630 case sc_extern:
10631 error_at (loc, "decomposition declaration cannot be declared "
10632 "%<extern%>");
10633 break;
10634 case sc_mutable:
10635 error_at (loc, "decomposition declaration cannot be declared "
10636 "%<mutable%>");
10637 break;
10638 case sc_auto:
10639 error_at (loc, "decomposition declaration cannot be declared "
10640 "C++98 %<auto%>");
10641 break;
10642 default:
10643 gcc_unreachable ();
10645 if (TREE_CODE (type) != TEMPLATE_TYPE_PARM
10646 || TYPE_IDENTIFIER (type) != auto_identifier)
10648 if (type != error_mark_node)
10650 error_at (loc, "decomposition declaration cannot be declared "
10651 "with type %qT", type);
10652 inform (loc,
10653 "type must be cv-qualified %<auto%> or reference to "
10654 "cv-qualified %<auto%>");
10656 type = build_qualified_type (make_auto (), type_quals);
10657 declspecs->type = type;
10659 inlinep = 0;
10660 typedef_p = 0;
10661 constexpr_p = 0;
10662 thread_p = 0;
10663 concept_p = 0;
10664 storage_class = sc_none;
10665 staticp = 0;
10666 declspecs->storage_class = sc_none;
10667 declspecs->locations[ds_thread] = UNKNOWN_LOCATION;
10670 /* Static anonymous unions are dealt with here. */
10671 if (staticp && decl_context == TYPENAME
10672 && declspecs->type
10673 && ANON_AGGR_TYPE_P (declspecs->type))
10674 decl_context = FIELD;
10676 /* Warn about storage classes that are invalid for certain
10677 kinds of declarations (parameters, typenames, etc.). */
10678 if (thread_p
10679 && ((storage_class
10680 && storage_class != sc_extern
10681 && storage_class != sc_static)
10682 || typedef_p))
10684 error ("multiple storage classes in declaration of %qs", name);
10685 thread_p = false;
10687 if (decl_context != NORMAL
10688 && ((storage_class != sc_none
10689 && storage_class != sc_mutable)
10690 || thread_p))
10692 if ((decl_context == PARM || decl_context == CATCHPARM)
10693 && (storage_class == sc_register
10694 || storage_class == sc_auto))
10696 else if (typedef_p)
10698 else if (decl_context == FIELD
10699 /* C++ allows static class elements. */
10700 && storage_class == sc_static)
10701 /* C++ also allows inlines and signed and unsigned elements,
10702 but in those cases we don't come in here. */
10704 else
10706 if (decl_context == FIELD)
10707 error ("storage class specified for %qs", name);
10708 else
10710 if (decl_context == PARM || decl_context == CATCHPARM)
10711 error ("storage class specified for parameter %qs", name);
10712 else
10713 error ("storage class specified for typename");
10715 if (storage_class == sc_register
10716 || storage_class == sc_auto
10717 || storage_class == sc_extern
10718 || thread_p)
10719 storage_class = sc_none;
10722 else if (storage_class == sc_extern && funcdef_flag
10723 && ! toplevel_bindings_p ())
10724 error ("nested function %qs declared %<extern%>", name);
10725 else if (toplevel_bindings_p ())
10727 if (storage_class == sc_auto)
10728 error ("top-level declaration of %qs specifies %<auto%>", name);
10730 else if (thread_p
10731 && storage_class != sc_extern
10732 && storage_class != sc_static)
10734 if (declspecs->gnu_thread_keyword_p)
10735 pedwarn (input_location, 0, "function-scope %qs implicitly auto and "
10736 "declared %<__thread%>", name);
10738 /* When thread_local is applied to a variable of block scope the
10739 storage-class-specifier static is implied if it does not appear
10740 explicitly. */
10741 storage_class = declspecs->storage_class = sc_static;
10742 staticp = 1;
10745 if (storage_class && friendp)
10747 error ("storage class specifiers invalid in friend function declarations");
10748 storage_class = sc_none;
10749 staticp = 0;
10752 if (!id_declarator)
10753 unqualified_id = NULL_TREE;
10754 else
10756 unqualified_id = id_declarator->u.id.unqualified_name;
10757 switch (TREE_CODE (unqualified_id))
10759 case BIT_NOT_EXPR:
10760 unqualified_id = TREE_OPERAND (unqualified_id, 0);
10761 if (TYPE_P (unqualified_id))
10762 unqualified_id = constructor_name (unqualified_id);
10763 break;
10765 case IDENTIFIER_NODE:
10766 case TEMPLATE_ID_EXPR:
10767 break;
10769 default:
10770 gcc_unreachable ();
10774 if (declspecs->std_attributes)
10776 /* Apply the c++11 attributes to the type preceding them. */
10777 input_location = declspecs->locations[ds_std_attribute];
10778 decl_attributes (&type, declspecs->std_attributes, 0);
10779 input_location = saved_loc;
10782 /* Determine the type of the entity declared by recurring on the
10783 declarator. */
10784 for (; declarator; declarator = declarator->declarator)
10786 const cp_declarator *inner_declarator;
10787 tree attrs;
10789 if (type == error_mark_node)
10790 return error_mark_node;
10792 attrs = declarator->attributes;
10793 if (attrs)
10795 int attr_flags;
10797 attr_flags = 0;
10798 if (declarator == NULL || declarator->kind == cdk_id)
10799 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
10800 if (declarator->kind == cdk_function)
10801 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
10802 if (declarator->kind == cdk_array)
10803 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
10804 returned_attrs = decl_attributes (&type,
10805 chainon (returned_attrs, attrs),
10806 attr_flags);
10809 if (declarator->kind == cdk_id || declarator->kind == cdk_decomp)
10810 break;
10812 inner_declarator = declarator->declarator;
10814 switch (declarator->kind)
10816 case cdk_array:
10817 type = create_array_type_for_decl (dname, type,
10818 declarator->u.array.bounds);
10819 if (!valid_array_size_p (input_location, type, dname))
10820 type = error_mark_node;
10822 if (declarator->std_attributes)
10823 /* [dcl.array]/1:
10825 The optional attribute-specifier-seq appertains to the
10826 array. */
10827 returned_attrs = chainon (returned_attrs,
10828 declarator->std_attributes);
10829 break;
10831 case cdk_function:
10833 tree arg_types;
10834 int funcdecl_p;
10836 /* Declaring a function type.
10837 Make sure we have a valid type for the function to return. */
10839 if (type_quals != TYPE_UNQUALIFIED)
10841 if (SCALAR_TYPE_P (type) || VOID_TYPE_P (type))
10843 warning_at (typespec_loc, OPT_Wignored_qualifiers, "type "
10844 "qualifiers ignored on function return type");
10846 /* We now know that the TYPE_QUALS don't apply to the
10847 decl, but to its return type. */
10848 type_quals = TYPE_UNQUALIFIED;
10851 /* Error about some types functions can't return. */
10853 if (TREE_CODE (type) == FUNCTION_TYPE)
10855 error ("%qs declared as function returning a function", name);
10856 return error_mark_node;
10858 if (TREE_CODE (type) == ARRAY_TYPE)
10860 error ("%qs declared as function returning an array", name);
10861 return error_mark_node;
10864 input_location = declspecs->locations[ds_type_spec];
10865 abstract_virtuals_error (ACU_RETURN, type);
10866 input_location = saved_loc;
10868 /* Pick up type qualifiers which should be applied to `this'. */
10869 memfn_quals = declarator->u.function.qualifiers;
10870 /* Pick up virt-specifiers. */
10871 virt_specifiers = declarator->u.function.virt_specifiers;
10872 /* And ref-qualifier, too */
10873 rqual = declarator->u.function.ref_qualifier;
10874 /* And tx-qualifier. */
10875 tree tx_qual = declarator->u.function.tx_qualifier;
10876 /* Pick up the exception specifications. */
10877 raises = declarator->u.function.exception_specification;
10878 /* If the exception-specification is ill-formed, let's pretend
10879 there wasn't one. */
10880 if (raises == error_mark_node)
10881 raises = NULL_TREE;
10883 if (reqs)
10884 error_at (location_of (reqs), "requires-clause on return type");
10885 reqs = declarator->u.function.requires_clause;
10887 /* Say it's a definition only for the CALL_EXPR
10888 closest to the identifier. */
10889 funcdecl_p = inner_declarator && inner_declarator->kind == cdk_id;
10891 /* Handle a late-specified return type. */
10892 tree late_return_type = declarator->u.function.late_return_type;
10893 if (funcdecl_p)
10895 if (tree auto_node = type_uses_auto (type))
10897 if (!late_return_type)
10899 if (current_class_type
10900 && LAMBDA_TYPE_P (current_class_type))
10901 /* OK for C++11 lambdas. */;
10902 else if (cxx_dialect < cxx14)
10904 error ("%qs function uses "
10905 "%<auto%> type specifier without trailing "
10906 "return type", name);
10907 inform (input_location, "deduced return type "
10908 "only available with -std=c++14 or "
10909 "-std=gnu++14");
10911 else if (virtualp)
10913 error ("virtual function cannot "
10914 "have deduced return type");
10915 virtualp = false;
10918 else if (!is_auto (type) && sfk != sfk_conversion)
10920 error ("%qs function with trailing return type has"
10921 " %qT as its type rather than plain %<auto%>",
10922 name, type);
10923 return error_mark_node;
10925 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
10927 if (!late_return_type)
10929 if (dguide_name_p (unqualified_id))
10930 error_at (declarator->id_loc, "deduction guide "
10931 "for %qT must have trailing return "
10932 "type", TREE_TYPE (tmpl));
10933 else
10934 error_at (declarator->id_loc, "deduced class "
10935 "type %qT in function return type",
10936 type);
10937 inform (DECL_SOURCE_LOCATION (tmpl),
10938 "%qD declared here", tmpl);
10940 else if (CLASS_TYPE_P (late_return_type)
10941 && CLASSTYPE_TEMPLATE_INFO (late_return_type)
10942 && (CLASSTYPE_TI_TEMPLATE (late_return_type)
10943 == tmpl))
10944 /* OK */;
10945 else
10946 error ("trailing return type %qT of deduction guide "
10947 "is not a specialization of %qT",
10948 late_return_type, TREE_TYPE (tmpl));
10951 else if (late_return_type
10952 && sfk != sfk_conversion)
10954 if (cxx_dialect < cxx11)
10955 /* Not using maybe_warn_cpp0x because this should
10956 always be an error. */
10957 error ("trailing return type only available with "
10958 "-std=c++11 or -std=gnu++11");
10959 else
10960 error ("%qs function with trailing return type not "
10961 "declared with %<auto%> type specifier", name);
10962 return error_mark_node;
10965 type = splice_late_return_type (type, late_return_type);
10966 if (type == error_mark_node)
10967 return error_mark_node;
10969 if (late_return_type)
10970 late_return_type_p = true;
10972 if (ctype == NULL_TREE
10973 && decl_context == FIELD
10974 && funcdecl_p
10975 && friendp == 0)
10976 ctype = current_class_type;
10978 if (ctype && (sfk == sfk_constructor
10979 || sfk == sfk_destructor))
10981 /* We are within a class's scope. If our declarator name
10982 is the same as the class name, and we are defining
10983 a function, then it is a constructor/destructor, and
10984 therefore returns a void type. */
10986 /* ISO C++ 12.4/2. A destructor may not be declared
10987 const or volatile. A destructor may not be static.
10988 A destructor may not be declared with ref-qualifier.
10990 ISO C++ 12.1. A constructor may not be declared
10991 const or volatile. A constructor may not be
10992 virtual. A constructor may not be static.
10993 A constructor may not be declared with ref-qualifier. */
10994 if (staticp == 2)
10995 error ((flags == DTOR_FLAG)
10996 ? G_("destructor cannot be static member function")
10997 : G_("constructor cannot be static member function"));
10998 if (memfn_quals)
11000 error ((flags == DTOR_FLAG)
11001 ? G_("destructors may not be cv-qualified")
11002 : G_("constructors may not be cv-qualified"));
11003 memfn_quals = TYPE_UNQUALIFIED;
11006 if (rqual)
11008 maybe_warn_cpp0x (CPP0X_REF_QUALIFIER);
11009 error ((flags == DTOR_FLAG)
11010 ? G_("destructors may not be ref-qualified")
11011 : G_("constructors may not be ref-qualified"));
11012 rqual = REF_QUAL_NONE;
11015 if (decl_context == FIELD
11016 && !member_function_or_else (ctype,
11017 current_class_type,
11018 flags))
11019 return error_mark_node;
11021 if (flags != DTOR_FLAG)
11023 /* It's a constructor. */
11024 if (explicitp == 1)
11025 explicitp = 2;
11026 if (virtualp)
11028 permerror (input_location,
11029 "constructors cannot be declared %<virtual%>");
11030 virtualp = 0;
11032 if (decl_context == FIELD
11033 && sfk != sfk_constructor)
11034 return error_mark_node;
11036 if (decl_context == FIELD)
11037 staticp = 0;
11039 else if (friendp)
11041 if (virtualp)
11043 /* Cannot be both friend and virtual. */
11044 error ("virtual functions cannot be friends");
11045 friendp = 0;
11047 if (decl_context == NORMAL)
11048 error ("friend declaration not in class definition");
11049 if (current_function_decl && funcdef_flag)
11050 error ("can%'t define friend function %qs in a local "
11051 "class definition",
11052 name);
11054 else if (ctype && sfk == sfk_conversion)
11056 if (explicitp == 1)
11058 maybe_warn_cpp0x (CPP0X_EXPLICIT_CONVERSION);
11059 explicitp = 2;
11061 if (late_return_type_p)
11062 error ("a conversion function cannot have a trailing return type");
11064 else if (sfk == sfk_deduction_guide)
11066 if (explicitp == 1)
11067 explicitp = 2;
11070 arg_types = grokparms (declarator->u.function.parameters,
11071 &parms);
11073 if (inner_declarator
11074 && inner_declarator->kind == cdk_id
11075 && inner_declarator->u.id.sfk == sfk_destructor
11076 && arg_types != void_list_node)
11078 error ("destructors may not have parameters");
11079 arg_types = void_list_node;
11080 parms = NULL_TREE;
11083 type = build_function_type (type, arg_types);
11085 tree attrs = declarator->std_attributes;
11086 if (tx_qual)
11088 tree att = build_tree_list (tx_qual, NULL_TREE);
11089 /* transaction_safe applies to the type, but
11090 transaction_safe_dynamic applies to the function. */
11091 if (is_attribute_p ("transaction_safe", tx_qual))
11092 attrs = chainon (attrs, att);
11093 else
11094 returned_attrs = chainon (returned_attrs, att);
11096 if (attrs)
11097 /* [dcl.fct]/2:
11099 The optional attribute-specifier-seq appertains to
11100 the function type. */
11101 decl_attributes (&type, attrs, 0);
11103 if (raises)
11104 type = build_exception_variant (type, raises);
11106 break;
11108 case cdk_pointer:
11109 case cdk_reference:
11110 case cdk_ptrmem:
11111 /* Filter out pointers-to-references and references-to-references.
11112 We can get these if a TYPE_DECL is used. */
11114 if (TREE_CODE (type) == REFERENCE_TYPE)
11116 if (declarator->kind != cdk_reference)
11118 error ("cannot declare pointer to %q#T", type);
11119 type = TREE_TYPE (type);
11122 /* In C++0x, we allow reference to reference declarations
11123 that occur indirectly through typedefs [7.1.3/8 dcl.typedef]
11124 and template type arguments [14.3.1/4 temp.arg.type]. The
11125 check for direct reference to reference declarations, which
11126 are still forbidden, occurs below. Reasoning behind the change
11127 can be found in DR106, DR540, and the rvalue reference
11128 proposals. */
11129 else if (cxx_dialect == cxx98)
11131 error ("cannot declare reference to %q#T", type);
11132 type = TREE_TYPE (type);
11135 else if (VOID_TYPE_P (type))
11137 if (declarator->kind == cdk_reference)
11138 error ("cannot declare reference to %q#T", type);
11139 else if (declarator->kind == cdk_ptrmem)
11140 error ("cannot declare pointer to %q#T member", type);
11143 /* We now know that the TYPE_QUALS don't apply to the decl,
11144 but to the target of the pointer. */
11145 type_quals = TYPE_UNQUALIFIED;
11147 /* This code used to handle METHOD_TYPE, but I don't think it's
11148 possible to get it here anymore. */
11149 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
11150 if (declarator->kind == cdk_ptrmem
11151 && TREE_CODE (type) == FUNCTION_TYPE)
11153 memfn_quals |= type_memfn_quals (type);
11154 type = build_memfn_type (type,
11155 declarator->u.pointer.class_type,
11156 memfn_quals,
11157 rqual);
11158 if (type == error_mark_node)
11159 return error_mark_node;
11161 rqual = REF_QUAL_NONE;
11162 memfn_quals = TYPE_UNQUALIFIED;
11165 if (TREE_CODE (type) == FUNCTION_TYPE
11166 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
11167 || type_memfn_rqual (type) != REF_QUAL_NONE))
11168 error (declarator->kind == cdk_reference
11169 ? G_("cannot declare reference to qualified function type %qT")
11170 : G_("cannot declare pointer to qualified function type %qT"),
11171 type);
11173 /* When the pointed-to type involves components of variable size,
11174 care must be taken to ensure that the size evaluation code is
11175 emitted early enough to dominate all the possible later uses
11176 and late enough for the variables on which it depends to have
11177 been assigned.
11179 This is expected to happen automatically when the pointed-to
11180 type has a name/declaration of it's own, but special attention
11181 is required if the type is anonymous.
11183 We handle the NORMAL and FIELD contexts here by inserting a
11184 dummy statement that just evaluates the size at a safe point
11185 and ensures it is not deferred until e.g. within a deeper
11186 conditional context (c++/43555).
11188 We expect nothing to be needed here for PARM or TYPENAME.
11189 Evaluating the size at this point for TYPENAME would
11190 actually be incorrect, as we might be in the middle of an
11191 expression with side effects on the pointed-to type size
11192 "arguments" prior to the pointer declaration point and the
11193 size evaluation could end up prior to the side effects. */
11195 if (!TYPE_NAME (type)
11196 && (decl_context == NORMAL || decl_context == FIELD)
11197 && at_function_scope_p ()
11198 && variably_modified_type_p (type, NULL_TREE))
11200 TYPE_NAME (type) = build_decl (UNKNOWN_LOCATION, TYPE_DECL,
11201 NULL_TREE, type);
11202 add_decl_expr (TYPE_NAME (type));
11205 if (declarator->kind == cdk_reference)
11207 /* In C++0x, the type we are creating a reference to might be
11208 a typedef which is itself a reference type. In that case,
11209 we follow the reference collapsing rules in
11210 [7.1.3/8 dcl.typedef] to create the final reference type:
11212 "If a typedef TD names a type that is a reference to a type
11213 T, an attempt to create the type 'lvalue reference to cv TD'
11214 creates the type 'lvalue reference to T,' while an attempt
11215 to create the type "rvalue reference to cv TD' creates the
11216 type TD."
11218 if (VOID_TYPE_P (type))
11219 /* We already gave an error. */;
11220 else if (TREE_CODE (type) == REFERENCE_TYPE)
11222 if (declarator->u.reference.rvalue_ref)
11223 /* Leave type alone. */;
11224 else
11225 type = cp_build_reference_type (TREE_TYPE (type), false);
11227 else
11228 type = cp_build_reference_type
11229 (type, declarator->u.reference.rvalue_ref);
11231 /* In C++0x, we need this check for direct reference to
11232 reference declarations, which are forbidden by
11233 [8.3.2/5 dcl.ref]. Reference to reference declarations
11234 are only allowed indirectly through typedefs and template
11235 type arguments. Example:
11237 void foo(int & &); // invalid ref-to-ref decl
11239 typedef int & int_ref;
11240 void foo(int_ref &); // valid ref-to-ref decl
11242 if (inner_declarator && inner_declarator->kind == cdk_reference)
11243 error ("cannot declare reference to %q#T, which is not "
11244 "a typedef or a template type argument", type);
11246 else if (TREE_CODE (type) == METHOD_TYPE)
11247 type = build_ptrmemfunc_type (build_pointer_type (type));
11248 else if (declarator->kind == cdk_ptrmem)
11250 gcc_assert (TREE_CODE (declarator->u.pointer.class_type)
11251 != NAMESPACE_DECL);
11252 if (declarator->u.pointer.class_type == error_mark_node)
11253 /* We will already have complained. */
11254 type = error_mark_node;
11255 else
11256 type = build_ptrmem_type (declarator->u.pointer.class_type,
11257 type);
11259 else
11260 type = build_pointer_type (type);
11262 /* Process a list of type modifier keywords (such as
11263 const or volatile) that were given inside the `*' or `&'. */
11265 if (declarator->u.pointer.qualifiers)
11267 type
11268 = cp_build_qualified_type (type,
11269 declarator->u.pointer.qualifiers);
11270 type_quals = cp_type_quals (type);
11273 /* Apply C++11 attributes to the pointer, and not to the
11274 type pointed to. This is unlike what is done for GNU
11275 attributes above. It is to comply with [dcl.ptr]/1:
11277 [the optional attribute-specifier-seq (7.6.1) appertains
11278 to the pointer and not to the object pointed to]. */
11279 if (declarator->std_attributes)
11280 decl_attributes (&type, declarator->std_attributes,
11283 ctype = NULL_TREE;
11284 break;
11286 case cdk_error:
11287 break;
11289 default:
11290 gcc_unreachable ();
11294 /* A `constexpr' specifier used in an object declaration declares
11295 the object as `const'. */
11296 if (constexpr_p && innermost_code != cdk_function)
11298 /* DR1688 says that a `constexpr' specifier in combination with
11299 `volatile' is valid. */
11301 if (TREE_CODE (type) != REFERENCE_TYPE)
11303 type_quals |= TYPE_QUAL_CONST;
11304 type = cp_build_qualified_type (type, type_quals);
11308 if (unqualified_id && TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR
11309 && TREE_CODE (type) != FUNCTION_TYPE
11310 && TREE_CODE (type) != METHOD_TYPE
11311 && !variable_template_p (TREE_OPERAND (unqualified_id, 0)))
11313 error ("template-id %qD used as a declarator",
11314 unqualified_id);
11315 unqualified_id = dname;
11318 /* If TYPE is a FUNCTION_TYPE, but the function name was explicitly
11319 qualified with a class-name, turn it into a METHOD_TYPE, unless
11320 we know that the function is static. We take advantage of this
11321 opportunity to do other processing that pertains to entities
11322 explicitly declared to be class members. Note that if DECLARATOR
11323 is non-NULL, we know it is a cdk_id declarator; otherwise, we
11324 would not have exited the loop above. */
11325 if (declarator
11326 && declarator->kind == cdk_id
11327 && declarator->u.id.qualifying_scope
11328 && MAYBE_CLASS_TYPE_P (declarator->u.id.qualifying_scope))
11330 ctype = declarator->u.id.qualifying_scope;
11331 ctype = TYPE_MAIN_VARIANT (ctype);
11332 template_count = num_template_headers_for_class (ctype);
11334 if (ctype == current_class_type)
11336 if (friendp)
11338 permerror (input_location, "member functions are implicitly "
11339 "friends of their class");
11340 friendp = 0;
11342 else
11343 permerror (declarator->id_loc,
11344 "extra qualification %<%T::%> on member %qs",
11345 ctype, name);
11347 else if (/* If the qualifying type is already complete, then we
11348 can skip the following checks. */
11349 !COMPLETE_TYPE_P (ctype)
11350 && (/* If the function is being defined, then
11351 qualifying type must certainly be complete. */
11352 funcdef_flag
11353 /* A friend declaration of "T::f" is OK, even if
11354 "T" is a template parameter. But, if this
11355 function is not a friend, the qualifying type
11356 must be a class. */
11357 || (!friendp && !CLASS_TYPE_P (ctype))
11358 /* For a declaration, the type need not be
11359 complete, if either it is dependent (since there
11360 is no meaningful definition of complete in that
11361 case) or the qualifying class is currently being
11362 defined. */
11363 || !(dependent_type_p (ctype)
11364 || currently_open_class (ctype)))
11365 /* Check that the qualifying type is complete. */
11366 && !complete_type_or_else (ctype, NULL_TREE))
11367 return error_mark_node;
11368 else if (TREE_CODE (type) == FUNCTION_TYPE)
11370 if (current_class_type
11371 && (!friendp || funcdef_flag))
11373 error (funcdef_flag
11374 ? G_("cannot define member function %<%T::%s%> "
11375 "within %<%T%>")
11376 : G_("cannot declare member function %<%T::%s%> "
11377 "within %<%T%>"),
11378 ctype, name, current_class_type);
11379 return error_mark_node;
11382 else if (typedef_p && current_class_type)
11384 error ("cannot declare member %<%T::%s%> within %qT",
11385 ctype, name, current_class_type);
11386 return error_mark_node;
11390 if (ctype == NULL_TREE && decl_context == FIELD && friendp == 0)
11391 ctype = current_class_type;
11393 /* Now TYPE has the actual type. */
11395 if (returned_attrs)
11397 if (attrlist)
11398 *attrlist = chainon (returned_attrs, *attrlist);
11399 else
11400 attrlist = &returned_attrs;
11403 if (declarator
11404 && declarator->kind == cdk_id
11405 && declarator->std_attributes)
11406 /* [dcl.meaning]/1: The optional attribute-specifier-seq following
11407 a declarator-id appertains to the entity that is declared. */
11408 *attrlist = chainon (*attrlist, declarator->std_attributes);
11410 /* Handle parameter packs. */
11411 if (parameter_pack_p)
11413 if (decl_context == PARM)
11414 /* Turn the type into a pack expansion.*/
11415 type = make_pack_expansion (type);
11416 else
11417 error ("non-parameter %qs cannot be a parameter pack", name);
11420 if ((decl_context == FIELD || decl_context == PARM)
11421 && !processing_template_decl
11422 && variably_modified_type_p (type, NULL_TREE))
11424 if (decl_context == FIELD)
11425 error ("data member may not have variably modified type %qT", type);
11426 else
11427 error ("parameter may not have variably modified type %qT", type);
11428 type = error_mark_node;
11431 if (explicitp == 1 || (explicitp && friendp))
11433 /* [dcl.fct.spec] (C++11) The explicit specifier shall be used only
11434 in the declaration of a constructor or conversion function within
11435 a class definition. */
11436 if (!current_class_type)
11437 error_at (declspecs->locations[ds_explicit],
11438 "%<explicit%> outside class declaration");
11439 else if (friendp)
11440 error_at (declspecs->locations[ds_explicit],
11441 "%<explicit%> in friend declaration");
11442 else
11443 error_at (declspecs->locations[ds_explicit],
11444 "only declarations of constructors and conversion operators "
11445 "can be %<explicit%>");
11446 explicitp = 0;
11449 if (storage_class == sc_mutable)
11451 if (decl_context != FIELD || friendp)
11453 error ("non-member %qs cannot be declared %<mutable%>", name);
11454 storage_class = sc_none;
11456 else if (decl_context == TYPENAME || typedef_p)
11458 error ("non-object member %qs cannot be declared %<mutable%>", name);
11459 storage_class = sc_none;
11461 else if (TREE_CODE (type) == FUNCTION_TYPE
11462 || TREE_CODE (type) == METHOD_TYPE)
11464 error ("function %qs cannot be declared %<mutable%>", name);
11465 storage_class = sc_none;
11467 else if (staticp)
11469 error ("static %qs cannot be declared %<mutable%>", name);
11470 storage_class = sc_none;
11472 else if (type_quals & TYPE_QUAL_CONST)
11474 error ("const %qs cannot be declared %<mutable%>", name);
11475 storage_class = sc_none;
11477 else if (TREE_CODE (type) == REFERENCE_TYPE)
11479 permerror (input_location, "reference %qs cannot be declared "
11480 "%<mutable%>", name);
11481 storage_class = sc_none;
11485 /* If this is declaring a typedef name, return a TYPE_DECL. */
11486 if (typedef_p && decl_context != TYPENAME)
11488 tree decl;
11490 /* This declaration:
11492 typedef void f(int) const;
11494 declares a function type which is not a member of any
11495 particular class, but which is cv-qualified; for
11496 example "f S::*" declares a pointer to a const-qualified
11497 member function of S. We record the cv-qualification in the
11498 function type. */
11499 if ((rqual || memfn_quals) && TREE_CODE (type) == FUNCTION_TYPE)
11501 type = apply_memfn_quals (type, memfn_quals, rqual);
11503 /* We have now dealt with these qualifiers. */
11504 memfn_quals = TYPE_UNQUALIFIED;
11505 rqual = REF_QUAL_NONE;
11508 if (type_uses_auto (type))
11510 error ("typedef declared %<auto%>");
11511 type = error_mark_node;
11514 if (reqs)
11515 error_at (location_of (reqs), "requires-clause on typedef");
11517 if (decl_context == FIELD)
11518 decl = build_lang_decl (TYPE_DECL, unqualified_id, type);
11519 else
11520 decl = build_decl (input_location, TYPE_DECL, unqualified_id, type);
11521 if (id_declarator && declarator->u.id.qualifying_scope) {
11522 error_at (DECL_SOURCE_LOCATION (decl),
11523 "typedef name may not be a nested-name-specifier");
11524 TREE_TYPE (decl) = error_mark_node;
11527 if (decl_context != FIELD)
11529 if (!current_function_decl)
11530 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
11531 else if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (current_function_decl)
11532 || (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P
11533 (current_function_decl)))
11534 /* The TYPE_DECL is "abstract" because there will be
11535 clones of this constructor/destructor, and there will
11536 be copies of this TYPE_DECL generated in those
11537 clones. The decloning optimization (for space) may
11538 revert this subsequently if it determines that
11539 the clones should share a common implementation. */
11540 DECL_ABSTRACT_P (decl) = true;
11542 else if (current_class_type
11543 && constructor_name_p (unqualified_id, current_class_type))
11544 permerror (input_location, "ISO C++ forbids nested type %qD with same name "
11545 "as enclosing class",
11546 unqualified_id);
11548 /* If the user declares "typedef struct {...} foo" then the
11549 struct will have an anonymous name. Fill that name in now.
11550 Nothing can refer to it, so nothing needs know about the name
11551 change. */
11552 if (type != error_mark_node
11553 && unqualified_id
11554 && TYPE_NAME (type)
11555 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
11556 && TYPE_UNNAMED_P (type)
11557 && declspecs->type_definition_p
11558 && attributes_naming_typedef_ok (*attrlist)
11559 && cp_type_quals (type) == TYPE_UNQUALIFIED)
11561 tree t;
11563 /* Replace the anonymous name with the real name everywhere. */
11564 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
11566 if (anon_aggrname_p (TYPE_IDENTIFIER (t)))
11567 /* We do not rename the debug info representing the
11568 unnamed tagged type because the standard says in
11569 [dcl.typedef] that the naming applies only for
11570 linkage purposes. */
11571 /*debug_hooks->set_name (t, decl);*/
11572 TYPE_NAME (t) = decl;
11575 if (TYPE_LANG_SPECIFIC (type))
11576 TYPE_WAS_UNNAMED (type) = 1;
11578 /* If this is a typedef within a template class, the nested
11579 type is a (non-primary) template. The name for the
11580 template needs updating as well. */
11581 if (TYPE_LANG_SPECIFIC (type) && CLASSTYPE_TEMPLATE_INFO (type))
11582 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type))
11583 = TYPE_IDENTIFIER (type);
11585 /* Adjust linkage now that we aren't unnamed anymore. */
11586 reset_type_linkage (type);
11588 /* FIXME remangle member functions; member functions of a
11589 type with external linkage have external linkage. */
11592 if (signed_p
11593 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
11594 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
11596 bad_specifiers (decl, BSP_TYPE, virtualp,
11597 memfn_quals != TYPE_UNQUALIFIED,
11598 inlinep, friendp, raises != NULL_TREE);
11600 if (decl_spec_seq_has_spec_p (declspecs, ds_alias))
11601 /* Acknowledge that this was written:
11602 `using analias = atype;'. */
11603 TYPE_DECL_ALIAS_P (decl) = 1;
11605 return decl;
11608 /* Detect the case of an array type of unspecified size
11609 which came, as such, direct from a typedef name.
11610 We must copy the type, so that the array's domain can be
11611 individually set by the object's initializer. */
11613 if (type && typedef_type
11614 && TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
11615 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
11616 type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
11618 /* Detect where we're using a typedef of function type to declare a
11619 function. PARMS will not be set, so we must create it now. */
11621 if (type == typedef_type && TREE_CODE (type) == FUNCTION_TYPE)
11623 tree decls = NULL_TREE;
11624 tree args;
11626 for (args = TYPE_ARG_TYPES (type);
11627 args && args != void_list_node;
11628 args = TREE_CHAIN (args))
11630 tree decl = cp_build_parm_decl (NULL_TREE, TREE_VALUE (args));
11632 DECL_CHAIN (decl) = decls;
11633 decls = decl;
11636 parms = nreverse (decls);
11638 if (decl_context != TYPENAME)
11640 /* The qualifiers on the function type become the qualifiers on
11641 the non-static member function. */
11642 memfn_quals |= type_memfn_quals (type);
11643 rqual = type_memfn_rqual (type);
11644 type_quals = TYPE_UNQUALIFIED;
11648 /* If this is a type name (such as, in a cast or sizeof),
11649 compute the type and return it now. */
11651 if (decl_context == TYPENAME)
11653 /* Note that here we don't care about type_quals. */
11655 /* Special case: "friend class foo" looks like a TYPENAME context. */
11656 if (friendp)
11658 if (inlinep)
11660 error ("%<inline%> specified for friend class declaration");
11661 inlinep = 0;
11664 if (!current_aggr)
11666 /* Don't allow friend declaration without a class-key. */
11667 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
11668 permerror (input_location, "template parameters cannot be friends");
11669 else if (TREE_CODE (type) == TYPENAME_TYPE)
11670 permerror (input_location, "friend declaration requires class-key, "
11671 "i.e. %<friend class %T::%D%>",
11672 TYPE_CONTEXT (type), TYPENAME_TYPE_FULLNAME (type));
11673 else
11674 permerror (input_location, "friend declaration requires class-key, "
11675 "i.e. %<friend %#T%>",
11676 type);
11679 /* Only try to do this stuff if we didn't already give up. */
11680 if (type != integer_type_node)
11682 /* A friendly class? */
11683 if (current_class_type)
11684 make_friend_class (current_class_type, TYPE_MAIN_VARIANT (type),
11685 /*complain=*/true);
11686 else
11687 error ("trying to make class %qT a friend of global scope",
11688 type);
11690 type = void_type_node;
11693 else if (memfn_quals || rqual)
11695 if (ctype == NULL_TREE
11696 && TREE_CODE (type) == METHOD_TYPE)
11697 ctype = TYPE_METHOD_BASETYPE (type);
11699 if (ctype)
11700 type = build_memfn_type (type, ctype, memfn_quals, rqual);
11701 /* Core issue #547: need to allow this in template type args.
11702 Allow it in general in C++11 for alias-declarations. */
11703 else if ((template_type_arg || cxx_dialect >= cxx11)
11704 && TREE_CODE (type) == FUNCTION_TYPE)
11705 type = apply_memfn_quals (type, memfn_quals, rqual);
11706 else
11707 error ("invalid qualifiers on non-member function type");
11710 if (reqs)
11711 error_at (location_of (reqs), "requires-clause on type-id");
11713 return type;
11715 else if (unqualified_id == NULL_TREE && decl_context != PARM
11716 && decl_context != CATCHPARM
11717 && TREE_CODE (type) != UNION_TYPE
11718 && ! bitfield
11719 && innermost_code != cdk_decomp)
11721 error ("abstract declarator %qT used as declaration", type);
11722 return error_mark_node;
11725 /* Only functions may be declared using an operator-function-id. */
11726 if (unqualified_id
11727 && IDENTIFIER_OPNAME_P (unqualified_id)
11728 && TREE_CODE (type) != FUNCTION_TYPE
11729 && TREE_CODE (type) != METHOD_TYPE)
11731 error ("declaration of %qD as non-function", unqualified_id);
11732 return error_mark_node;
11735 if (reqs
11736 && TREE_CODE (type) != FUNCTION_TYPE
11737 && TREE_CODE (type) != METHOD_TYPE)
11738 error_at (location_of (reqs),
11739 "requires-clause on declaration of non-function type %qT",
11740 type);
11742 /* We don't check parameter types here because we can emit a better
11743 error message later. */
11744 if (decl_context != PARM)
11746 type = check_var_type (unqualified_id, type);
11747 if (type == error_mark_node)
11748 return error_mark_node;
11751 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
11752 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
11754 if (decl_context == PARM || decl_context == CATCHPARM)
11756 if (ctype || in_namespace)
11757 error ("cannot use %<::%> in parameter declaration");
11759 if (type_uses_auto (type)
11760 && !(cxx_dialect >= cxx1z && template_parm_flag))
11762 if (cxx_dialect >= cxx14)
11763 error ("%<auto%> parameter not permitted in this context");
11764 else
11765 error ("parameter declared %<auto%>");
11766 type = error_mark_node;
11769 /* A parameter declared as an array of T is really a pointer to T.
11770 One declared as a function is really a pointer to a function.
11771 One declared as a member is really a pointer to member. */
11773 if (TREE_CODE (type) == ARRAY_TYPE)
11775 /* Transfer const-ness of array into that of type pointed to. */
11776 type = build_pointer_type (TREE_TYPE (type));
11777 type_quals = TYPE_UNQUALIFIED;
11778 array_parameter_p = true;
11780 else if (TREE_CODE (type) == FUNCTION_TYPE)
11781 type = build_pointer_type (type);
11784 if (ctype && TREE_CODE (type) == FUNCTION_TYPE && staticp < 2
11785 && !NEW_DELETE_OPNAME_P (unqualified_id))
11787 cp_cv_quals real_quals = memfn_quals;
11788 if (cxx_dialect < cxx14 && constexpr_p
11789 && sfk != sfk_constructor && sfk != sfk_destructor)
11790 real_quals |= TYPE_QUAL_CONST;
11791 type = build_memfn_type (type, ctype, real_quals, rqual);
11795 tree decl = NULL_TREE;
11797 if (decl_context == PARM)
11799 decl = cp_build_parm_decl (unqualified_id, type);
11800 DECL_ARRAY_PARAMETER_P (decl) = array_parameter_p;
11802 bad_specifiers (decl, BSP_PARM, virtualp,
11803 memfn_quals != TYPE_UNQUALIFIED,
11804 inlinep, friendp, raises != NULL_TREE);
11806 else if (decl_context == FIELD)
11808 if (!staticp && !friendp && TREE_CODE (type) != METHOD_TYPE)
11809 if (tree auto_node = type_uses_auto (type))
11811 location_t loc = declspecs->locations[ds_type_spec];
11812 if (CLASS_PLACEHOLDER_TEMPLATE (auto_node))
11813 error_at (loc, "invalid use of template-name %qE without an "
11814 "argument list",
11815 CLASS_PLACEHOLDER_TEMPLATE (auto_node));
11816 else
11817 error_at (loc, "non-static data member declared with "
11818 "placeholder %qT", auto_node);
11819 type = error_mark_node;
11822 /* The C99 flexible array extension. */
11823 if (!staticp && TREE_CODE (type) == ARRAY_TYPE
11824 && TYPE_DOMAIN (type) == NULL_TREE)
11826 if (ctype
11827 && (TREE_CODE (ctype) == UNION_TYPE
11828 || TREE_CODE (ctype) == QUAL_UNION_TYPE))
11830 error ("flexible array member in union");
11831 type = error_mark_node;
11833 else
11835 /* Array is a flexible member. */
11836 if (in_system_header_at (input_location))
11837 /* Do not warn on flexible array members in system
11838 headers because glibc uses them. */;
11839 else if (name)
11840 pedwarn (input_location, OPT_Wpedantic,
11841 "ISO C++ forbids flexible array member %<%s%>", name);
11842 else
11843 pedwarn (input_location, OPT_Wpedantic,
11844 "ISO C++ forbids flexible array members");
11846 /* Flexible array member has a null domain. */
11847 type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
11851 if (type == error_mark_node)
11853 /* Happens when declaring arrays of sizes which
11854 are error_mark_node, for example. */
11855 decl = NULL_TREE;
11857 else if (in_namespace && !friendp)
11859 /* Something like struct S { int N::j; }; */
11860 error ("invalid use of %<::%>");
11861 return error_mark_node;
11863 else if (TREE_CODE (type) == FUNCTION_TYPE
11864 || TREE_CODE (type) == METHOD_TYPE)
11866 int publicp = 0;
11867 tree function_context;
11869 if (friendp == 0)
11871 /* This should never happen in pure C++ (the check
11872 could be an assert). It could happen in
11873 Objective-C++ if someone writes invalid code that
11874 uses a function declaration for an instance
11875 variable or property (instance variables and
11876 properties are parsed as FIELD_DECLs, but they are
11877 part of an Objective-C class, not a C++ class).
11878 That code is invalid and is caught by this
11879 check. */
11880 if (!ctype)
11882 error ("declaration of function %qD in invalid context",
11883 unqualified_id);
11884 return error_mark_node;
11887 /* ``A union may [ ... ] not [ have ] virtual functions.''
11888 ARM 9.5 */
11889 if (virtualp && TREE_CODE (ctype) == UNION_TYPE)
11891 error ("function %qD declared %<virtual%> inside a union",
11892 unqualified_id);
11893 return error_mark_node;
11896 if (NEW_DELETE_OPNAME_P (unqualified_id))
11898 if (virtualp)
11900 error ("%qD cannot be declared %<virtual%>, since it "
11901 "is always static",
11902 unqualified_id);
11903 virtualp = 0;
11908 /* Check that the name used for a destructor makes sense. */
11909 if (sfk == sfk_destructor)
11911 tree uqname = id_declarator->u.id.unqualified_name;
11913 if (!ctype)
11915 gcc_assert (friendp);
11916 error ("expected qualified name in friend declaration "
11917 "for destructor %qD", uqname);
11918 return error_mark_node;
11921 if (!check_dtor_name (ctype, TREE_OPERAND (uqname, 0)))
11923 error ("declaration of %qD as member of %qT",
11924 uqname, ctype);
11925 return error_mark_node;
11927 if (concept_p)
11929 error ("a destructor cannot be %<concept%>");
11930 return error_mark_node;
11932 if (constexpr_p)
11934 error ("a destructor cannot be %<constexpr%>");
11935 return error_mark_node;
11938 else if (sfk == sfk_constructor && friendp && !ctype)
11940 error ("expected qualified name in friend declaration "
11941 "for constructor %qD",
11942 id_declarator->u.id.unqualified_name);
11943 return error_mark_node;
11945 if (sfk == sfk_constructor)
11946 if (concept_p)
11948 error ("a constructor cannot be %<concept%>");
11949 return error_mark_node;
11951 if (concept_p)
11953 error ("a concept cannot be a member function");
11954 concept_p = false;
11957 if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
11959 tree tmpl = TREE_OPERAND (unqualified_id, 0);
11960 if (variable_template_p (tmpl))
11962 error ("specialization of variable template %qD "
11963 "declared as function", tmpl);
11964 inform (DECL_SOURCE_LOCATION (tmpl),
11965 "variable template declared here");
11966 return error_mark_node;
11970 /* Tell grokfndecl if it needs to set TREE_PUBLIC on the node. */
11971 function_context = (ctype != NULL_TREE) ?
11972 decl_function_context (TYPE_MAIN_DECL (ctype)) : NULL_TREE;
11973 publicp = (! friendp || ! staticp)
11974 && function_context == NULL_TREE;
11976 if (late_return_type_p)
11977 TYPE_HAS_LATE_RETURN_TYPE (type) = 1;
11979 decl = grokfndecl (ctype, type,
11980 TREE_CODE (unqualified_id) != TEMPLATE_ID_EXPR
11981 ? unqualified_id : dname,
11982 parms,
11983 unqualified_id,
11984 reqs,
11985 virtualp, flags, memfn_quals, rqual, raises,
11986 friendp ? -1 : 0, friendp, publicp,
11987 inlinep | (2 * constexpr_p) | (4 * concept_p),
11988 initialized == SD_DELETED, sfk,
11989 funcdef_flag, template_count, in_namespace,
11990 attrlist, declarator->id_loc);
11991 decl = set_virt_specifiers (decl, virt_specifiers);
11992 if (decl == NULL_TREE)
11993 return error_mark_node;
11994 #if 0
11995 /* This clobbers the attrs stored in `decl' from `attrlist'. */
11996 /* The decl and setting of decl_attr is also turned off. */
11997 decl = build_decl_attribute_variant (decl, decl_attr);
11998 #endif
12000 /* [class.conv.ctor]
12002 A constructor declared without the function-specifier
12003 explicit that can be called with a single parameter
12004 specifies a conversion from the type of its first
12005 parameter to the type of its class. Such a constructor
12006 is called a converting constructor. */
12007 if (explicitp == 2)
12008 DECL_NONCONVERTING_P (decl) = 1;
12010 else if (!staticp && !dependent_type_p (type)
12011 && !COMPLETE_TYPE_P (complete_type (type))
12012 && (!complete_or_array_type_p (type)
12013 || initialized == 0))
12015 if (TREE_CODE (type) != ARRAY_TYPE
12016 || !COMPLETE_TYPE_P (TREE_TYPE (type)))
12018 if (unqualified_id)
12020 error ("field %qD has incomplete type %qT",
12021 unqualified_id, type);
12022 cxx_incomplete_type_inform (strip_array_types (type));
12024 else
12025 error ("name %qT has incomplete type", type);
12027 type = error_mark_node;
12028 decl = NULL_TREE;
12031 else
12033 if (friendp)
12035 error ("%qE is neither function nor member function; "
12036 "cannot be declared friend", unqualified_id);
12037 friendp = 0;
12039 decl = NULL_TREE;
12042 if (friendp)
12044 /* Friends are treated specially. */
12045 if (ctype == current_class_type)
12046 ; /* We already issued a permerror. */
12047 else if (decl && DECL_NAME (decl))
12049 if (template_class_depth (current_class_type) == 0)
12051 decl = check_explicit_specialization
12052 (unqualified_id, decl, template_count,
12053 2 * funcdef_flag + 4);
12054 if (decl == error_mark_node)
12055 return error_mark_node;
12058 decl = do_friend (ctype, unqualified_id, decl,
12059 *attrlist, flags,
12060 funcdef_flag);
12061 return decl;
12063 else
12064 return error_mark_node;
12067 /* Structure field. It may not be a function, except for C++. */
12069 if (decl == NULL_TREE)
12071 if (staticp)
12073 /* C++ allows static class members. All other work
12074 for this is done by grokfield. */
12075 decl = build_lang_decl_loc (declarator
12076 ? declarator->id_loc
12077 : input_location,
12078 VAR_DECL, unqualified_id, type);
12079 set_linkage_for_static_data_member (decl);
12080 if (concept_p)
12081 error ("static data member %qE declared %<concept%>",
12082 unqualified_id);
12083 else if (constexpr_p && !initialized)
12085 error ("constexpr static data member %qD must have an "
12086 "initializer", decl);
12087 constexpr_p = false;
12090 if (inlinep)
12091 mark_inline_variable (decl);
12093 if (!DECL_VAR_DECLARED_INLINE_P (decl)
12094 && !(cxx_dialect >= cxx1z && constexpr_p))
12095 /* Even if there is an in-class initialization, DECL
12096 is considered undefined until an out-of-class
12097 definition is provided, unless this is an inline
12098 variable. */
12099 DECL_EXTERNAL (decl) = 1;
12101 if (thread_p)
12103 CP_DECL_THREAD_LOCAL_P (decl) = true;
12104 if (!processing_template_decl)
12105 set_decl_tls_model (decl, decl_default_tls_model (decl));
12106 if (declspecs->gnu_thread_keyword_p)
12107 SET_DECL_GNU_TLS_P (decl);
12110 else
12112 if (concept_p)
12113 error ("non-static data member %qE declared %<concept%>",
12114 unqualified_id);
12115 else if (constexpr_p)
12117 error ("non-static data member %qE declared %<constexpr%>",
12118 unqualified_id);
12119 constexpr_p = false;
12121 decl = build_decl (input_location,
12122 FIELD_DECL, unqualified_id, type);
12123 DECL_NONADDRESSABLE_P (decl) = bitfield;
12124 if (bitfield && !unqualified_id)
12125 TREE_NO_WARNING (decl) = 1;
12127 if (storage_class == sc_mutable)
12129 DECL_MUTABLE_P (decl) = 1;
12130 storage_class = sc_none;
12133 if (initialized)
12135 /* An attempt is being made to initialize a non-static
12136 member. This is new in C++11. */
12137 maybe_warn_cpp0x (CPP0X_NSDMI);
12139 /* If this has been parsed with static storage class, but
12140 errors forced staticp to be cleared, ensure NSDMI is
12141 not present. */
12142 if (declspecs->storage_class == sc_static)
12143 DECL_INITIAL (decl) = error_mark_node;
12147 bad_specifiers (decl, BSP_FIELD, virtualp,
12148 memfn_quals != TYPE_UNQUALIFIED,
12149 staticp ? false : inlinep, friendp,
12150 raises != NULL_TREE);
12153 else if (TREE_CODE (type) == FUNCTION_TYPE
12154 || TREE_CODE (type) == METHOD_TYPE)
12156 tree original_name;
12157 int publicp = 0;
12159 if (!unqualified_id)
12160 return error_mark_node;
12162 if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
12163 original_name = dname;
12164 else
12165 original_name = unqualified_id;
12167 if (storage_class == sc_auto)
12168 error ("storage class %<auto%> invalid for function %qs", name);
12169 else if (storage_class == sc_register)
12170 error ("storage class %<register%> invalid for function %qs", name);
12171 else if (thread_p)
12173 if (declspecs->gnu_thread_keyword_p)
12174 error ("storage class %<__thread%> invalid for function %qs",
12175 name);
12176 else
12177 error ("storage class %<thread_local%> invalid for function %qs",
12178 name);
12181 if (virt_specifiers)
12182 error ("virt-specifiers in %qs not allowed outside a class definition", name);
12183 /* Function declaration not at top level.
12184 Storage classes other than `extern' are not allowed
12185 and `extern' makes no difference. */
12186 if (! toplevel_bindings_p ()
12187 && (storage_class == sc_static
12188 || decl_spec_seq_has_spec_p (declspecs, ds_inline))
12189 && pedantic)
12191 if (storage_class == sc_static)
12192 pedwarn (input_location, OPT_Wpedantic,
12193 "%<static%> specifier invalid for function %qs "
12194 "declared out of global scope", name);
12195 else
12196 pedwarn (input_location, OPT_Wpedantic,
12197 "%<inline%> specifier invalid for function %qs "
12198 "declared out of global scope", name);
12201 if (ctype == NULL_TREE)
12203 if (virtualp)
12205 error ("virtual non-class function %qs", name);
12206 virtualp = 0;
12208 else if (sfk == sfk_constructor
12209 || sfk == sfk_destructor)
12211 error (funcdef_flag
12212 ? G_("%qs defined in a non-class scope")
12213 : G_("%qs declared in a non-class scope"), name);
12214 sfk = sfk_none;
12218 /* Record whether the function is public. */
12219 publicp = (ctype != NULL_TREE
12220 || storage_class != sc_static);
12222 if (late_return_type_p)
12223 TYPE_HAS_LATE_RETURN_TYPE (type) = 1;
12225 decl = grokfndecl (ctype, type, original_name, parms, unqualified_id,
12226 reqs, virtualp, flags, memfn_quals, rqual, raises,
12227 1, friendp,
12228 publicp,
12229 inlinep | (2 * constexpr_p) | (4 * concept_p),
12230 initialized == SD_DELETED,
12231 sfk,
12232 funcdef_flag,
12233 template_count, in_namespace, attrlist,
12234 declarator->id_loc);
12235 if (decl == NULL_TREE)
12236 return error_mark_node;
12238 if (explicitp == 2)
12239 DECL_NONCONVERTING_P (decl) = 1;
12240 if (staticp == 1)
12242 int invalid_static = 0;
12244 /* Don't allow a static member function in a class, and forbid
12245 declaring main to be static. */
12246 if (TREE_CODE (type) == METHOD_TYPE)
12248 permerror (input_location, "cannot declare member function %qD to have "
12249 "static linkage", decl);
12250 invalid_static = 1;
12252 else if (current_function_decl)
12254 /* 7.1.1: There can be no static function declarations within a
12255 block. */
12256 error ("cannot declare static function inside another function");
12257 invalid_static = 1;
12260 if (invalid_static)
12262 staticp = 0;
12263 storage_class = sc_none;
12267 else
12269 /* It's a variable. */
12271 /* An uninitialized decl with `extern' is a reference. */
12272 decl = grokvardecl (type, dname, unqualified_id,
12273 declspecs,
12274 initialized,
12275 type_quals,
12276 inlinep,
12277 concept_p,
12278 template_count,
12279 ctype ? ctype : in_namespace);
12280 if (decl == NULL_TREE)
12281 return error_mark_node;
12283 bad_specifiers (decl, BSP_VAR, virtualp,
12284 memfn_quals != TYPE_UNQUALIFIED,
12285 inlinep, friendp, raises != NULL_TREE);
12287 if (ctype)
12289 DECL_CONTEXT (decl) = ctype;
12290 if (staticp == 1)
12292 permerror (input_location, "%<static%> may not be used when defining "
12293 "(as opposed to declaring) a static data member");
12294 staticp = 0;
12295 storage_class = sc_none;
12297 if (storage_class == sc_register && TREE_STATIC (decl))
12299 error ("static member %qD declared %<register%>", decl);
12300 storage_class = sc_none;
12302 if (storage_class == sc_extern && pedantic)
12304 pedwarn (input_location, OPT_Wpedantic,
12305 "cannot explicitly declare member %q#D to have "
12306 "extern linkage", decl);
12307 storage_class = sc_none;
12310 else if (constexpr_p && DECL_EXTERNAL (decl))
12312 error ("declaration of constexpr variable %qD is not a definition",
12313 decl);
12314 constexpr_p = false;
12317 if (inlinep)
12318 mark_inline_variable (decl);
12319 if (innermost_code == cdk_decomp)
12321 gcc_assert (declarator && declarator->kind == cdk_decomp);
12322 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
12323 retrofit_lang_decl (decl);
12324 DECL_ARTIFICIAL (decl) = 1;
12325 SET_DECL_DECOMPOSITION_P (decl);
12329 if (VAR_P (decl) && !initialized)
12330 if (tree auto_node = type_uses_auto (type))
12331 if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node))
12333 location_t loc = declspecs->locations[ds_type_spec];
12334 error_at (loc, "declaration of %q#D has no initializer", decl);
12335 TREE_TYPE (decl) = error_mark_node;
12338 if (storage_class == sc_extern && initialized && !funcdef_flag)
12340 if (toplevel_bindings_p ())
12342 /* It's common practice (and completely valid) to have a const
12343 be initialized and declared extern. */
12344 if (!(type_quals & TYPE_QUAL_CONST))
12345 warning (0, "%qs initialized and declared %<extern%>", name);
12347 else
12349 error ("%qs has both %<extern%> and initializer", name);
12350 return error_mark_node;
12354 /* Record `register' declaration for warnings on &
12355 and in case doing stupid register allocation. */
12357 if (storage_class == sc_register)
12359 DECL_REGISTER (decl) = 1;
12360 /* Warn about register storage specifiers on PARM_DECLs. */
12361 if (TREE_CODE (decl) == PARM_DECL)
12363 if (cxx_dialect >= cxx1z)
12364 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
12365 "ISO C++1z does not allow %<register%> storage "
12366 "class specifier");
12367 else
12368 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
12369 "%<register%> storage class specifier used");
12372 else if (storage_class == sc_extern)
12373 DECL_THIS_EXTERN (decl) = 1;
12374 else if (storage_class == sc_static)
12375 DECL_THIS_STATIC (decl) = 1;
12377 /* Set constexpr flag on vars (functions got it in grokfndecl). */
12378 if (constexpr_p && VAR_P (decl))
12379 DECL_DECLARED_CONSTEXPR_P (decl) = true;
12381 /* Record constancy and volatility on the DECL itself . There's
12382 no need to do this when processing a template; we'll do this
12383 for the instantiated declaration based on the type of DECL. */
12384 if (!processing_template_decl)
12385 cp_apply_type_quals_to_decl (type_quals, decl);
12387 return decl;
12391 /* Subroutine of start_function. Ensure that each of the parameter
12392 types (as listed in PARMS) is complete, as is required for a
12393 function definition. */
12395 static void
12396 require_complete_types_for_parms (tree parms)
12398 for (; parms; parms = DECL_CHAIN (parms))
12400 if (dependent_type_p (TREE_TYPE (parms)))
12401 continue;
12402 if (!VOID_TYPE_P (TREE_TYPE (parms))
12403 && complete_type_or_else (TREE_TYPE (parms), parms))
12405 relayout_decl (parms);
12406 DECL_ARG_TYPE (parms) = type_passed_as (TREE_TYPE (parms));
12408 else
12409 /* grokparms or complete_type_or_else will have already issued
12410 an error. */
12411 TREE_TYPE (parms) = error_mark_node;
12415 /* Returns nonzero if T is a local variable. */
12418 local_variable_p (const_tree t)
12420 if ((VAR_P (t)
12421 /* A VAR_DECL with a context that is a _TYPE is a static data
12422 member. */
12423 && !TYPE_P (CP_DECL_CONTEXT (t))
12424 /* Any other non-local variable must be at namespace scope. */
12425 && !DECL_NAMESPACE_SCOPE_P (t))
12426 || (TREE_CODE (t) == PARM_DECL))
12427 return 1;
12429 return 0;
12432 /* Like local_variable_p, but suitable for use as a tree-walking
12433 function. */
12435 static tree
12436 local_variable_p_walkfn (tree *tp, int *walk_subtrees,
12437 void * /*data*/)
12439 if (local_variable_p (*tp)
12440 && (!DECL_ARTIFICIAL (*tp) || DECL_NAME (*tp) == this_identifier))
12441 return *tp;
12442 else if (TYPE_P (*tp))
12443 *walk_subtrees = 0;
12445 return NULL_TREE;
12448 /* Check that ARG, which is a default-argument expression for a
12449 parameter DECL, is valid. Returns ARG, or ERROR_MARK_NODE, if
12450 something goes wrong. DECL may also be a _TYPE node, rather than a
12451 DECL, if there is no DECL available. */
12453 tree
12454 check_default_argument (tree decl, tree arg, tsubst_flags_t complain)
12456 tree var;
12457 tree decl_type;
12459 if (TREE_CODE (arg) == DEFAULT_ARG)
12460 /* We get a DEFAULT_ARG when looking at an in-class declaration
12461 with a default argument. Ignore the argument for now; we'll
12462 deal with it after the class is complete. */
12463 return arg;
12465 if (TYPE_P (decl))
12467 decl_type = decl;
12468 decl = NULL_TREE;
12470 else
12471 decl_type = TREE_TYPE (decl);
12473 if (arg == error_mark_node
12474 || decl == error_mark_node
12475 || TREE_TYPE (arg) == error_mark_node
12476 || decl_type == error_mark_node)
12477 /* Something already went wrong. There's no need to check
12478 further. */
12479 return error_mark_node;
12481 /* [dcl.fct.default]
12483 A default argument expression is implicitly converted to the
12484 parameter type. */
12485 ++cp_unevaluated_operand;
12486 perform_implicit_conversion_flags (decl_type, arg, complain,
12487 LOOKUP_IMPLICIT);
12488 --cp_unevaluated_operand;
12490 /* Avoid redundant -Wzero-as-null-pointer-constant warnings at
12491 the call sites. */
12492 if (TYPE_PTR_OR_PTRMEM_P (decl_type)
12493 && null_ptr_cst_p (arg))
12494 return nullptr_node;
12496 /* [dcl.fct.default]
12498 Local variables shall not be used in default argument
12499 expressions.
12501 The keyword `this' shall not be used in a default argument of a
12502 member function. */
12503 var = cp_walk_tree_without_duplicates (&arg, local_variable_p_walkfn, NULL);
12504 if (var)
12506 if (complain & tf_warning_or_error)
12508 if (DECL_NAME (var) == this_identifier)
12509 permerror (input_location, "default argument %qE uses %qD",
12510 arg, var);
12511 else
12512 error ("default argument %qE uses local variable %qD", arg, var);
12514 return error_mark_node;
12517 /* All is well. */
12518 return arg;
12521 /* Returns a deprecated type used within TYPE, or NULL_TREE if none. */
12523 static tree
12524 type_is_deprecated (tree type)
12526 enum tree_code code;
12527 if (TREE_DEPRECATED (type))
12528 return type;
12529 if (TYPE_NAME (type))
12531 if (TREE_DEPRECATED (TYPE_NAME (type)))
12532 return type;
12533 else
12534 return NULL_TREE;
12537 /* Do warn about using typedefs to a deprecated class. */
12538 if (OVERLOAD_TYPE_P (type) && type != TYPE_MAIN_VARIANT (type))
12539 return type_is_deprecated (TYPE_MAIN_VARIANT (type));
12541 code = TREE_CODE (type);
12543 if (code == POINTER_TYPE || code == REFERENCE_TYPE
12544 || code == OFFSET_TYPE || code == FUNCTION_TYPE
12545 || code == METHOD_TYPE || code == ARRAY_TYPE)
12546 return type_is_deprecated (TREE_TYPE (type));
12548 if (TYPE_PTRMEMFUNC_P (type))
12549 return type_is_deprecated
12550 (TREE_TYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type))));
12552 return NULL_TREE;
12555 /* Decode the list of parameter types for a function type.
12556 Given the list of things declared inside the parens,
12557 return a list of types.
12559 If this parameter does not end with an ellipsis, we append
12560 void_list_node.
12562 *PARMS is set to the chain of PARM_DECLs created. */
12564 tree
12565 grokparms (tree parmlist, tree *parms)
12567 tree result = NULL_TREE;
12568 tree decls = NULL_TREE;
12569 tree parm;
12570 int any_error = 0;
12572 for (parm = parmlist; parm != NULL_TREE; parm = TREE_CHAIN (parm))
12574 tree type = NULL_TREE;
12575 tree init = TREE_PURPOSE (parm);
12576 tree decl = TREE_VALUE (parm);
12578 if (parm == void_list_node)
12579 break;
12581 if (! decl || TREE_TYPE (decl) == error_mark_node)
12582 continue;
12584 type = TREE_TYPE (decl);
12585 if (VOID_TYPE_P (type))
12587 if (same_type_p (type, void_type_node)
12588 && !init
12589 && !DECL_NAME (decl) && !result
12590 && TREE_CHAIN (parm) == void_list_node)
12591 /* DR 577: A parameter list consisting of a single
12592 unnamed parameter of non-dependent type 'void'. */
12593 break;
12594 else if (cv_qualified_p (type))
12595 error_at (DECL_SOURCE_LOCATION (decl),
12596 "invalid use of cv-qualified type %qT in "
12597 "parameter declaration", type);
12598 else
12599 error_at (DECL_SOURCE_LOCATION (decl),
12600 "invalid use of type %<void%> in parameter "
12601 "declaration");
12602 /* It's not a good idea to actually create parameters of
12603 type `void'; other parts of the compiler assume that a
12604 void type terminates the parameter list. */
12605 type = error_mark_node;
12606 TREE_TYPE (decl) = error_mark_node;
12609 if (type != error_mark_node)
12611 if (deprecated_state != DEPRECATED_SUPPRESS)
12613 tree deptype = type_is_deprecated (type);
12614 if (deptype)
12615 warn_deprecated_use (deptype, NULL_TREE);
12618 /* Top-level qualifiers on the parameters are
12619 ignored for function types. */
12620 type = cp_build_qualified_type (type, 0);
12621 if (TREE_CODE (type) == METHOD_TYPE)
12623 error ("parameter %qD invalidly declared method type", decl);
12624 type = build_pointer_type (type);
12625 TREE_TYPE (decl) = type;
12627 else if (abstract_virtuals_error (decl, type))
12628 any_error = 1; /* Seems like a good idea. */
12629 else if (POINTER_TYPE_P (type))
12631 /* [dcl.fct]/6, parameter types cannot contain pointers
12632 (references) to arrays of unknown bound. */
12633 tree t = TREE_TYPE (type);
12634 int ptr = TYPE_PTR_P (type);
12636 while (1)
12638 if (TYPE_PTR_P (t))
12639 ptr = 1;
12640 else if (TREE_CODE (t) != ARRAY_TYPE)
12641 break;
12642 else if (!TYPE_DOMAIN (t))
12643 break;
12644 t = TREE_TYPE (t);
12646 if (TREE_CODE (t) == ARRAY_TYPE)
12647 error (ptr
12648 ? G_("parameter %qD includes pointer to array of "
12649 "unknown bound %qT")
12650 : G_("parameter %qD includes reference to array of "
12651 "unknown bound %qT"),
12652 decl, t);
12655 if (any_error)
12656 init = NULL_TREE;
12657 else if (init && !processing_template_decl)
12658 init = check_default_argument (decl, init, tf_warning_or_error);
12661 DECL_CHAIN (decl) = decls;
12662 decls = decl;
12663 result = tree_cons (init, type, result);
12665 decls = nreverse (decls);
12666 result = nreverse (result);
12667 if (parm)
12668 result = chainon (result, void_list_node);
12669 *parms = decls;
12671 return result;
12675 /* D is a constructor or overloaded `operator='.
12677 Let T be the class in which D is declared. Then, this function
12678 returns:
12680 -1 if D's is an ill-formed constructor or copy assignment operator
12681 whose first parameter is of type `T'.
12682 0 if D is not a copy constructor or copy assignment
12683 operator.
12684 1 if D is a copy constructor or copy assignment operator whose
12685 first parameter is a reference to non-const qualified T.
12686 2 if D is a copy constructor or copy assignment operator whose
12687 first parameter is a reference to const qualified T.
12689 This function can be used as a predicate. Positive values indicate
12690 a copy constructor and nonzero values indicate a copy assignment
12691 operator. */
12694 copy_fn_p (const_tree d)
12696 tree args;
12697 tree arg_type;
12698 int result = 1;
12700 gcc_assert (DECL_FUNCTION_MEMBER_P (d));
12702 if (TREE_CODE (d) == TEMPLATE_DECL
12703 || (DECL_TEMPLATE_INFO (d)
12704 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d))))
12705 /* Instantiations of template member functions are never copy
12706 functions. Note that member functions of templated classes are
12707 represented as template functions internally, and we must
12708 accept those as copy functions. */
12709 return 0;
12711 args = FUNCTION_FIRST_USER_PARMTYPE (d);
12712 if (!args)
12713 return 0;
12715 arg_type = TREE_VALUE (args);
12716 if (arg_type == error_mark_node)
12717 return 0;
12719 if (TYPE_MAIN_VARIANT (arg_type) == DECL_CONTEXT (d))
12721 /* Pass by value copy assignment operator. */
12722 result = -1;
12724 else if (TREE_CODE (arg_type) == REFERENCE_TYPE
12725 && !TYPE_REF_IS_RVALUE (arg_type)
12726 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)) == DECL_CONTEXT (d))
12728 if (CP_TYPE_CONST_P (TREE_TYPE (arg_type)))
12729 result = 2;
12731 else
12732 return 0;
12734 args = TREE_CHAIN (args);
12736 if (args && args != void_list_node && !TREE_PURPOSE (args))
12737 /* There are more non-optional args. */
12738 return 0;
12740 return result;
12743 /* D is a constructor or overloaded `operator='.
12745 Let T be the class in which D is declared. Then, this function
12746 returns true when D is a move constructor or move assignment
12747 operator, false otherwise. */
12749 bool
12750 move_fn_p (const_tree d)
12752 gcc_assert (DECL_FUNCTION_MEMBER_P (d));
12754 if (cxx_dialect == cxx98)
12755 /* There are no move constructors if we are in C++98 mode. */
12756 return false;
12758 if (TREE_CODE (d) == TEMPLATE_DECL
12759 || (DECL_TEMPLATE_INFO (d)
12760 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d))))
12761 /* Instantiations of template member functions are never move
12762 functions. Note that member functions of templated classes are
12763 represented as template functions internally, and we must
12764 accept those as move functions. */
12765 return 0;
12767 return move_signature_fn_p (d);
12770 /* D is a constructor or overloaded `operator='.
12772 Then, this function returns true when D has the same signature as a move
12773 constructor or move assignment operator (because either it is such a
12774 ctor/op= or it is a template specialization with the same signature),
12775 false otherwise. */
12777 bool
12778 move_signature_fn_p (const_tree d)
12780 tree args;
12781 tree arg_type;
12782 bool result = false;
12784 args = FUNCTION_FIRST_USER_PARMTYPE (d);
12785 if (!args)
12786 return 0;
12788 arg_type = TREE_VALUE (args);
12789 if (arg_type == error_mark_node)
12790 return 0;
12792 if (TREE_CODE (arg_type) == REFERENCE_TYPE
12793 && TYPE_REF_IS_RVALUE (arg_type)
12794 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)),
12795 DECL_CONTEXT (d)))
12796 result = true;
12798 args = TREE_CHAIN (args);
12800 if (args && args != void_list_node && !TREE_PURPOSE (args))
12801 /* There are more non-optional args. */
12802 return false;
12804 return result;
12807 /* Remember any special properties of member function DECL. */
12809 void
12810 grok_special_member_properties (tree decl)
12812 tree class_type;
12814 if (!DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
12815 return;
12817 class_type = DECL_CONTEXT (decl);
12818 if (DECL_CONSTRUCTOR_P (decl))
12820 int ctor = copy_fn_p (decl);
12822 if (!DECL_ARTIFICIAL (decl))
12823 TYPE_HAS_USER_CONSTRUCTOR (class_type) = 1;
12825 if (ctor > 0)
12827 /* [class.copy]
12829 A non-template constructor for class X is a copy
12830 constructor if its first parameter is of type X&, const
12831 X&, volatile X& or const volatile X&, and either there
12832 are no other parameters or else all other parameters have
12833 default arguments. */
12834 TYPE_HAS_COPY_CTOR (class_type) = 1;
12835 if (user_provided_p (decl))
12836 TYPE_HAS_COMPLEX_COPY_CTOR (class_type) = 1;
12837 if (ctor > 1)
12838 TYPE_HAS_CONST_COPY_CTOR (class_type) = 1;
12840 else if (sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (decl)))
12841 TYPE_HAS_DEFAULT_CONSTRUCTOR (class_type) = 1;
12842 else if (move_fn_p (decl) && user_provided_p (decl))
12843 TYPE_HAS_COMPLEX_MOVE_CTOR (class_type) = 1;
12844 else if (is_list_ctor (decl))
12845 TYPE_HAS_LIST_CTOR (class_type) = 1;
12847 if (DECL_DECLARED_CONSTEXPR_P (decl)
12848 && !copy_fn_p (decl) && !move_fn_p (decl))
12849 TYPE_HAS_CONSTEXPR_CTOR (class_type) = 1;
12851 else if (DECL_OVERLOADED_OPERATOR_P (decl) == NOP_EXPR)
12853 /* [class.copy]
12855 A non-template assignment operator for class X is a copy
12856 assignment operator if its parameter is of type X, X&, const
12857 X&, volatile X& or const volatile X&. */
12859 int assop = copy_fn_p (decl);
12861 if (assop)
12863 TYPE_HAS_COPY_ASSIGN (class_type) = 1;
12864 if (user_provided_p (decl))
12865 TYPE_HAS_COMPLEX_COPY_ASSIGN (class_type) = 1;
12866 if (assop != 1)
12867 TYPE_HAS_CONST_COPY_ASSIGN (class_type) = 1;
12869 else if (move_fn_p (decl) && user_provided_p (decl))
12870 TYPE_HAS_COMPLEX_MOVE_ASSIGN (class_type) = 1;
12872 /* Destructors are handled in check_methods. */
12875 /* Check a constructor DECL has the correct form. Complains
12876 if the class has a constructor of the form X(X). */
12879 grok_ctor_properties (const_tree ctype, const_tree decl)
12881 int ctor_parm = copy_fn_p (decl);
12883 if (ctor_parm < 0)
12885 /* [class.copy]
12887 A declaration of a constructor for a class X is ill-formed if
12888 its first parameter is of type (optionally cv-qualified) X
12889 and either there are no other parameters or else all other
12890 parameters have default arguments.
12892 We *don't* complain about member template instantiations that
12893 have this form, though; they can occur as we try to decide
12894 what constructor to use during overload resolution. Since
12895 overload resolution will never prefer such a constructor to
12896 the non-template copy constructor (which is either explicitly
12897 or implicitly defined), there's no need to worry about their
12898 existence. Theoretically, they should never even be
12899 instantiated, but that's hard to forestall. */
12900 error ("invalid constructor; you probably meant %<%T (const %T&)%>",
12901 ctype, ctype);
12902 return 0;
12905 return 1;
12908 /* An operator with this code is unary, but can also be binary. */
12910 static int
12911 ambi_op_p (enum tree_code code)
12913 return (code == INDIRECT_REF
12914 || code == ADDR_EXPR
12915 || code == UNARY_PLUS_EXPR
12916 || code == NEGATE_EXPR
12917 || code == PREINCREMENT_EXPR
12918 || code == PREDECREMENT_EXPR);
12921 /* An operator with this name can only be unary. */
12923 static int
12924 unary_op_p (enum tree_code code)
12926 return (code == TRUTH_NOT_EXPR
12927 || code == BIT_NOT_EXPR
12928 || code == COMPONENT_REF
12929 || code == TYPE_EXPR);
12932 /* DECL is a declaration for an overloaded operator. If COMPLAIN is true,
12933 errors are issued for invalid declarations. */
12935 bool
12936 grok_op_properties (tree decl, bool complain)
12938 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
12939 tree argtype;
12940 int methodp = (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE);
12941 tree name = DECL_NAME (decl);
12942 enum tree_code operator_code;
12943 int arity;
12944 bool ellipsis_p;
12945 tree class_type;
12947 /* Count the number of arguments and check for ellipsis. */
12948 for (argtype = argtypes, arity = 0;
12949 argtype && argtype != void_list_node;
12950 argtype = TREE_CHAIN (argtype))
12951 ++arity;
12952 ellipsis_p = !argtype;
12954 class_type = DECL_CONTEXT (decl);
12955 if (class_type && !CLASS_TYPE_P (class_type))
12956 class_type = NULL_TREE;
12958 if (DECL_CONV_FN_P (decl))
12959 operator_code = TYPE_EXPR;
12960 else
12963 #define DEF_OPERATOR(NAME, CODE, MANGLING, ARITY, ASSN_P) \
12964 if (cp_operator_id (CODE) == name) \
12966 operator_code = (CODE); \
12967 break; \
12969 else if (cp_assignment_operator_id (CODE) == name) \
12971 operator_code = (CODE); \
12972 DECL_ASSIGNMENT_OPERATOR_P (decl) = 1; \
12973 break; \
12976 #include "operators.def"
12977 #undef DEF_OPERATOR
12979 gcc_unreachable ();
12981 while (0);
12982 gcc_assert (operator_code != MAX_TREE_CODES);
12983 SET_OVERLOADED_OPERATOR_CODE (decl, operator_code);
12985 if (class_type)
12986 switch (operator_code)
12988 case NEW_EXPR:
12989 TYPE_HAS_NEW_OPERATOR (class_type) = 1;
12990 break;
12992 case DELETE_EXPR:
12993 TYPE_GETS_DELETE (class_type) |= 1;
12994 break;
12996 case VEC_NEW_EXPR:
12997 TYPE_HAS_ARRAY_NEW_OPERATOR (class_type) = 1;
12998 break;
13000 case VEC_DELETE_EXPR:
13001 TYPE_GETS_DELETE (class_type) |= 2;
13002 break;
13004 default:
13005 break;
13008 /* [basic.std.dynamic.allocation]/1:
13010 A program is ill-formed if an allocation function is declared
13011 in a namespace scope other than global scope or declared static
13012 in global scope.
13014 The same also holds true for deallocation functions. */
13015 if (operator_code == NEW_EXPR || operator_code == VEC_NEW_EXPR
13016 || operator_code == DELETE_EXPR || operator_code == VEC_DELETE_EXPR)
13018 if (DECL_NAMESPACE_SCOPE_P (decl))
13020 if (CP_DECL_CONTEXT (decl) != global_namespace)
13022 error ("%qD may not be declared within a namespace", decl);
13023 return false;
13025 else if (!TREE_PUBLIC (decl))
13027 error ("%qD may not be declared as static", decl);
13028 return false;
13033 if (operator_code == NEW_EXPR || operator_code == VEC_NEW_EXPR)
13035 TREE_TYPE (decl) = coerce_new_type (TREE_TYPE (decl));
13036 DECL_IS_OPERATOR_NEW (decl) = 1;
13038 else if (operator_code == DELETE_EXPR || operator_code == VEC_DELETE_EXPR)
13039 TREE_TYPE (decl) = coerce_delete_type (TREE_TYPE (decl));
13040 else
13042 /* An operator function must either be a non-static member function
13043 or have at least one parameter of a class, a reference to a class,
13044 an enumeration, or a reference to an enumeration. 13.4.0.6 */
13045 if (! methodp || DECL_STATIC_FUNCTION_P (decl))
13047 if (operator_code == TYPE_EXPR
13048 || operator_code == CALL_EXPR
13049 || operator_code == COMPONENT_REF
13050 || operator_code == ARRAY_REF
13051 || operator_code == NOP_EXPR)
13053 error ("%qD must be a nonstatic member function", decl);
13054 return false;
13056 else
13058 tree p;
13060 if (DECL_STATIC_FUNCTION_P (decl))
13062 error ("%qD must be either a non-static member "
13063 "function or a non-member function", decl);
13064 return false;
13067 for (p = argtypes; p && p != void_list_node; p = TREE_CHAIN (p))
13069 tree arg = non_reference (TREE_VALUE (p));
13070 if (arg == error_mark_node)
13071 return false;
13073 /* MAYBE_CLASS_TYPE_P, rather than CLASS_TYPE_P, is used
13074 because these checks are performed even on
13075 template functions. */
13076 if (MAYBE_CLASS_TYPE_P (arg)
13077 || TREE_CODE (arg) == ENUMERAL_TYPE)
13078 break;
13081 if (!p || p == void_list_node)
13083 if (complain)
13084 error ("%qD must have an argument of class or "
13085 "enumerated type", decl);
13086 return false;
13091 /* There are no restrictions on the arguments to an overloaded
13092 "operator ()". */
13093 if (operator_code == CALL_EXPR)
13094 return true;
13096 /* Warn about conversion operators that will never be used. */
13097 if (IDENTIFIER_TYPENAME_P (name)
13098 && ! DECL_TEMPLATE_INFO (decl)
13099 && warn_conversion
13100 /* Warn only declaring the function; there is no need to
13101 warn again about out-of-class definitions. */
13102 && class_type == current_class_type)
13104 tree t = TREE_TYPE (name);
13105 int ref = (TREE_CODE (t) == REFERENCE_TYPE);
13107 if (ref)
13108 t = TYPE_MAIN_VARIANT (TREE_TYPE (t));
13110 if (VOID_TYPE_P (t))
13111 warning (OPT_Wconversion,
13113 ? G_("conversion to a reference to void "
13114 "will never use a type conversion operator")
13115 : G_("conversion to void "
13116 "will never use a type conversion operator"));
13117 else if (class_type)
13119 if (t == class_type)
13120 warning (OPT_Wconversion,
13122 ? G_("conversion to a reference to the same type "
13123 "will never use a type conversion operator")
13124 : G_("conversion to the same type "
13125 "will never use a type conversion operator"));
13126 /* Don't force t to be complete here. */
13127 else if (MAYBE_CLASS_TYPE_P (t)
13128 && COMPLETE_TYPE_P (t)
13129 && DERIVED_FROM_P (t, class_type))
13130 warning (OPT_Wconversion,
13132 ? G_("conversion to a reference to a base class "
13133 "will never use a type conversion operator")
13134 : G_("conversion to a base class "
13135 "will never use a type conversion operator"));
13140 if (operator_code == COND_EXPR)
13142 /* 13.4.0.3 */
13143 error ("ISO C++ prohibits overloading operator ?:");
13144 return false;
13146 else if (ellipsis_p)
13148 error ("%qD must not have variable number of arguments", decl);
13149 return false;
13151 else if (ambi_op_p (operator_code))
13153 if (arity == 1)
13154 /* We pick the one-argument operator codes by default, so
13155 we don't have to change anything. */
13157 else if (arity == 2)
13159 /* If we thought this was a unary operator, we now know
13160 it to be a binary operator. */
13161 switch (operator_code)
13163 case INDIRECT_REF:
13164 operator_code = MULT_EXPR;
13165 break;
13167 case ADDR_EXPR:
13168 operator_code = BIT_AND_EXPR;
13169 break;
13171 case UNARY_PLUS_EXPR:
13172 operator_code = PLUS_EXPR;
13173 break;
13175 case NEGATE_EXPR:
13176 operator_code = MINUS_EXPR;
13177 break;
13179 case PREINCREMENT_EXPR:
13180 operator_code = POSTINCREMENT_EXPR;
13181 break;
13183 case PREDECREMENT_EXPR:
13184 operator_code = POSTDECREMENT_EXPR;
13185 break;
13187 default:
13188 gcc_unreachable ();
13191 SET_OVERLOADED_OPERATOR_CODE (decl, operator_code);
13193 if ((operator_code == POSTINCREMENT_EXPR
13194 || operator_code == POSTDECREMENT_EXPR)
13195 && ! processing_template_decl
13196 && ! same_type_p (TREE_VALUE (TREE_CHAIN (argtypes)), integer_type_node))
13198 if (methodp)
13199 error ("postfix %qD must take %<int%> as its argument",
13200 decl);
13201 else
13202 error ("postfix %qD must take %<int%> as its second "
13203 "argument", decl);
13204 return false;
13207 else
13209 if (methodp)
13210 error ("%qD must take either zero or one argument", decl);
13211 else
13212 error ("%qD must take either one or two arguments", decl);
13213 return false;
13216 /* More Effective C++ rule 6. */
13217 if (warn_ecpp
13218 && (operator_code == POSTINCREMENT_EXPR
13219 || operator_code == POSTDECREMENT_EXPR
13220 || operator_code == PREINCREMENT_EXPR
13221 || operator_code == PREDECREMENT_EXPR))
13223 tree arg = TREE_VALUE (argtypes);
13224 tree ret = TREE_TYPE (TREE_TYPE (decl));
13225 if (methodp || TREE_CODE (arg) == REFERENCE_TYPE)
13226 arg = TREE_TYPE (arg);
13227 arg = TYPE_MAIN_VARIANT (arg);
13228 if (operator_code == PREINCREMENT_EXPR
13229 || operator_code == PREDECREMENT_EXPR)
13231 if (TREE_CODE (ret) != REFERENCE_TYPE
13232 || !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (ret)),
13233 arg))
13234 warning (OPT_Weffc__, "prefix %qD should return %qT", decl,
13235 build_reference_type (arg));
13237 else
13239 if (!same_type_p (TYPE_MAIN_VARIANT (ret), arg))
13240 warning (OPT_Weffc__, "postfix %qD should return %qT", decl, arg);
13244 else if (unary_op_p (operator_code))
13246 if (arity != 1)
13248 if (methodp)
13249 error ("%qD must take %<void%>", decl);
13250 else
13251 error ("%qD must take exactly one argument", decl);
13252 return false;
13255 else /* if (binary_op_p (operator_code)) */
13257 if (arity != 2)
13259 if (methodp)
13260 error ("%qD must take exactly one argument", decl);
13261 else
13262 error ("%qD must take exactly two arguments", decl);
13263 return false;
13266 /* More Effective C++ rule 7. */
13267 if (warn_ecpp
13268 && (operator_code == TRUTH_ANDIF_EXPR
13269 || operator_code == TRUTH_ORIF_EXPR
13270 || operator_code == COMPOUND_EXPR))
13271 warning (OPT_Weffc__, "user-defined %qD always evaluates both arguments",
13272 decl);
13275 /* Effective C++ rule 23. */
13276 if (warn_ecpp
13277 && arity == 2
13278 && !DECL_ASSIGNMENT_OPERATOR_P (decl)
13279 && (operator_code == PLUS_EXPR
13280 || operator_code == MINUS_EXPR
13281 || operator_code == TRUNC_DIV_EXPR
13282 || operator_code == MULT_EXPR
13283 || operator_code == TRUNC_MOD_EXPR)
13284 && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == REFERENCE_TYPE)
13285 warning (OPT_Weffc__, "%qD should return by value", decl);
13287 /* [over.oper]/8 */
13288 for (; argtypes && argtypes != void_list_node;
13289 argtypes = TREE_CHAIN (argtypes))
13290 if (TREE_PURPOSE (argtypes))
13292 TREE_PURPOSE (argtypes) = NULL_TREE;
13293 if (operator_code == POSTINCREMENT_EXPR
13294 || operator_code == POSTDECREMENT_EXPR)
13296 pedwarn (input_location, OPT_Wpedantic, "%qD cannot have default arguments",
13297 decl);
13299 else
13301 error ("%qD cannot have default arguments", decl);
13302 return false;
13306 return true;
13309 /* Return a string giving the keyword associate with CODE. */
13311 static const char *
13312 tag_name (enum tag_types code)
13314 switch (code)
13316 case record_type:
13317 return "struct";
13318 case class_type:
13319 return "class";
13320 case union_type:
13321 return "union";
13322 case enum_type:
13323 return "enum";
13324 case typename_type:
13325 return "typename";
13326 default:
13327 gcc_unreachable ();
13331 /* Name lookup in an elaborated-type-specifier (after the keyword
13332 indicated by TAG_CODE) has found the TYPE_DECL DECL. If the
13333 elaborated-type-specifier is invalid, issue a diagnostic and return
13334 error_mark_node; otherwise, return the *_TYPE to which it referred.
13335 If ALLOW_TEMPLATE_P is true, TYPE may be a class template. */
13337 tree
13338 check_elaborated_type_specifier (enum tag_types tag_code,
13339 tree decl,
13340 bool allow_template_p)
13342 tree type;
13344 /* In the case of:
13346 struct S { struct S *p; };
13348 name lookup will find the TYPE_DECL for the implicit "S::S"
13349 typedef. Adjust for that here. */
13350 if (DECL_SELF_REFERENCE_P (decl))
13351 decl = TYPE_NAME (TREE_TYPE (decl));
13353 type = TREE_TYPE (decl);
13355 /* Check TEMPLATE_TYPE_PARM first because DECL_IMPLICIT_TYPEDEF_P
13356 is false for this case as well. */
13357 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
13359 error ("using template type parameter %qT after %qs",
13360 type, tag_name (tag_code));
13361 return error_mark_node;
13363 /* Accept template template parameters. */
13364 else if (allow_template_p
13365 && (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM
13366 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM))
13368 /* [dcl.type.elab]
13370 If the identifier resolves to a typedef-name or the
13371 simple-template-id resolves to an alias template
13372 specialization, the elaborated-type-specifier is ill-formed.
13374 In other words, the only legitimate declaration to use in the
13375 elaborated type specifier is the implicit typedef created when
13376 the type is declared. */
13377 else if (!DECL_IMPLICIT_TYPEDEF_P (decl)
13378 && !DECL_SELF_REFERENCE_P (decl)
13379 && tag_code != typename_type)
13381 if (alias_template_specialization_p (type))
13382 error ("using alias template specialization %qT after %qs",
13383 type, tag_name (tag_code));
13384 else
13385 error ("using typedef-name %qD after %qs", decl, tag_name (tag_code));
13386 inform (DECL_SOURCE_LOCATION (decl),
13387 "%qD has a previous declaration here", decl);
13388 return error_mark_node;
13390 else if (TREE_CODE (type) != RECORD_TYPE
13391 && TREE_CODE (type) != UNION_TYPE
13392 && tag_code != enum_type
13393 && tag_code != typename_type)
13395 error ("%qT referred to as %qs", type, tag_name (tag_code));
13396 inform (location_of (type), "%qT has a previous declaration here", type);
13397 return error_mark_node;
13399 else if (TREE_CODE (type) != ENUMERAL_TYPE
13400 && tag_code == enum_type)
13402 error ("%qT referred to as enum", type);
13403 inform (location_of (type), "%qT has a previous declaration here", type);
13404 return error_mark_node;
13406 else if (!allow_template_p
13407 && TREE_CODE (type) == RECORD_TYPE
13408 && CLASSTYPE_IS_TEMPLATE (type))
13410 /* If a class template appears as elaborated type specifier
13411 without a template header such as:
13413 template <class T> class C {};
13414 void f(class C); // No template header here
13416 then the required template argument is missing. */
13417 error ("template argument required for %<%s %T%>",
13418 tag_name (tag_code),
13419 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)));
13420 return error_mark_node;
13423 return type;
13426 /* Lookup NAME in elaborate type specifier in scope according to
13427 SCOPE and issue diagnostics if necessary.
13428 Return *_TYPE node upon success, NULL_TREE when the NAME is not
13429 found, and ERROR_MARK_NODE for type error. */
13431 static tree
13432 lookup_and_check_tag (enum tag_types tag_code, tree name,
13433 tag_scope scope, bool template_header_p)
13435 tree t;
13436 tree decl;
13437 if (scope == ts_global)
13439 /* First try ordinary name lookup, ignoring hidden class name
13440 injected via friend declaration. */
13441 decl = lookup_name_prefer_type (name, 2);
13442 decl = strip_using_decl (decl);
13443 /* If that fails, the name will be placed in the smallest
13444 non-class, non-function-prototype scope according to 3.3.1/5.
13445 We may already have a hidden name declared as friend in this
13446 scope. So lookup again but not ignoring hidden names.
13447 If we find one, that name will be made visible rather than
13448 creating a new tag. */
13449 if (!decl)
13450 decl = lookup_type_scope (name, ts_within_enclosing_non_class);
13452 else
13453 decl = lookup_type_scope (name, scope);
13455 if (decl
13456 && (DECL_CLASS_TEMPLATE_P (decl)
13457 /* If scope is ts_current we're defining a class, so ignore a
13458 template template parameter. */
13459 || (scope != ts_current
13460 && DECL_TEMPLATE_TEMPLATE_PARM_P (decl))))
13461 decl = DECL_TEMPLATE_RESULT (decl);
13463 if (decl && TREE_CODE (decl) == TYPE_DECL)
13465 /* Look for invalid nested type:
13466 class C {
13467 class C {};
13468 }; */
13469 if (scope == ts_current && DECL_SELF_REFERENCE_P (decl))
13471 error ("%qD has the same name as the class in which it is "
13472 "declared",
13473 decl);
13474 return error_mark_node;
13477 /* Two cases we need to consider when deciding if a class
13478 template is allowed as an elaborated type specifier:
13479 1. It is a self reference to its own class.
13480 2. It comes with a template header.
13482 For example:
13484 template <class T> class C {
13485 class C *c1; // DECL_SELF_REFERENCE_P is true
13486 class D;
13488 template <class U> class C; // template_header_p is true
13489 template <class T> class C<T>::D {
13490 class C *c2; // DECL_SELF_REFERENCE_P is true
13491 }; */
13493 t = check_elaborated_type_specifier (tag_code,
13494 decl,
13495 template_header_p
13496 | DECL_SELF_REFERENCE_P (decl));
13497 if (template_header_p && t && CLASS_TYPE_P (t)
13498 && (!CLASSTYPE_TEMPLATE_INFO (t)
13499 || (!PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)))))
13501 error ("%qT is not a template", t);
13502 inform (location_of (t), "previous declaration here");
13503 if (TYPE_CLASS_SCOPE_P (t)
13504 && CLASSTYPE_TEMPLATE_INFO (TYPE_CONTEXT (t)))
13505 inform (input_location,
13506 "perhaps you want to explicitly add %<%T::%>",
13507 TYPE_CONTEXT (t));
13508 t = error_mark_node;
13511 return t;
13513 else if (decl && TREE_CODE (decl) == TREE_LIST)
13515 error ("reference to %qD is ambiguous", name);
13516 print_candidates (decl);
13517 return error_mark_node;
13519 else
13520 return NULL_TREE;
13523 /* Get the struct, enum or union (TAG_CODE says which) with tag NAME.
13524 Define the tag as a forward-reference if it is not defined.
13526 If a declaration is given, process it here, and report an error if
13527 multiple declarations are not identical.
13529 SCOPE is TS_CURRENT when this is also a definition. Only look in
13530 the current frame for the name (since C++ allows new names in any
13531 scope.) It is TS_WITHIN_ENCLOSING_NON_CLASS if this is a friend
13532 declaration. Only look beginning from the current scope outward up
13533 till the nearest non-class scope. Otherwise it is TS_GLOBAL.
13535 TEMPLATE_HEADER_P is true when this declaration is preceded by
13536 a set of template parameters. */
13538 static tree
13539 xref_tag_1 (enum tag_types tag_code, tree name,
13540 tag_scope orig_scope, bool template_header_p)
13542 enum tree_code code;
13543 tree t;
13544 tree context = NULL_TREE;
13545 tag_scope scope;
13547 gcc_assert (identifier_p (name));
13549 switch (tag_code)
13551 case record_type:
13552 case class_type:
13553 code = RECORD_TYPE;
13554 break;
13555 case union_type:
13556 code = UNION_TYPE;
13557 break;
13558 case enum_type:
13559 code = ENUMERAL_TYPE;
13560 break;
13561 default:
13562 gcc_unreachable ();
13565 if (orig_scope == ts_lambda)
13566 scope = ts_current;
13567 else
13568 scope = orig_scope;
13570 /* In case of anonymous name, xref_tag is only called to
13571 make type node and push name. Name lookup is not required. */
13572 if (anon_aggrname_p (name))
13573 t = NULL_TREE;
13574 else
13575 t = lookup_and_check_tag (tag_code, name,
13576 scope, template_header_p);
13578 if (t == error_mark_node)
13579 return error_mark_node;
13581 if (scope != ts_current && t && current_class_type
13582 && template_class_depth (current_class_type)
13583 && template_header_p)
13585 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
13586 return t;
13588 /* Since SCOPE is not TS_CURRENT, we are not looking at a
13589 definition of this tag. Since, in addition, we are currently
13590 processing a (member) template declaration of a template
13591 class, we must be very careful; consider:
13593 template <class X>
13594 struct S1
13596 template <class U>
13597 struct S2
13598 { template <class V>
13599 friend struct S1; };
13601 Here, the S2::S1 declaration should not be confused with the
13602 outer declaration. In particular, the inner version should
13603 have a template parameter of level 2, not level 1. This
13604 would be particularly important if the member declaration
13605 were instead:
13607 template <class V = U> friend struct S1;
13609 say, when we should tsubst into `U' when instantiating
13610 S2. On the other hand, when presented with:
13612 template <class T>
13613 struct S1 {
13614 template <class U>
13615 struct S2 {};
13616 template <class U>
13617 friend struct S2;
13620 we must find the inner binding eventually. We
13621 accomplish this by making sure that the new type we
13622 create to represent this declaration has the right
13623 TYPE_CONTEXT. */
13624 context = TYPE_CONTEXT (t);
13625 t = NULL_TREE;
13628 if (! t)
13630 /* If no such tag is yet defined, create a forward-reference node
13631 and record it as the "definition".
13632 When a real declaration of this type is found,
13633 the forward-reference will be altered into a real type. */
13634 if (code == ENUMERAL_TYPE)
13636 error ("use of enum %q#D without previous declaration", name);
13637 return error_mark_node;
13639 else
13641 t = make_class_type (code);
13642 TYPE_CONTEXT (t) = context;
13643 if (orig_scope == ts_lambda)
13644 /* Remember that we're declaring a lambda to avoid bogus errors
13645 in push_template_decl. */
13646 CLASSTYPE_LAMBDA_EXPR (t) = error_mark_node;
13647 t = pushtag (name, t, scope);
13650 else
13652 if (template_header_p && MAYBE_CLASS_TYPE_P (t))
13654 /* Check that we aren't trying to overload a class with different
13655 constraints. */
13656 tree constr = NULL_TREE;
13657 if (current_template_parms)
13659 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
13660 constr = build_constraints (reqs, NULL_TREE);
13662 if (!redeclare_class_template (t, current_template_parms, constr))
13663 return error_mark_node;
13665 else if (!processing_template_decl
13666 && CLASS_TYPE_P (t)
13667 && CLASSTYPE_IS_TEMPLATE (t))
13669 error ("redeclaration of %qT as a non-template", t);
13670 inform (location_of (t), "previous declaration %qD", t);
13671 return error_mark_node;
13674 /* Make injected friend class visible. */
13675 if (scope != ts_within_enclosing_non_class
13676 && hidden_name_p (TYPE_NAME (t)))
13678 DECL_ANTICIPATED (TYPE_NAME (t)) = 0;
13679 DECL_FRIEND_P (TYPE_NAME (t)) = 0;
13681 if (TYPE_TEMPLATE_INFO (t))
13683 DECL_ANTICIPATED (TYPE_TI_TEMPLATE (t)) = 0;
13684 DECL_FRIEND_P (TYPE_TI_TEMPLATE (t)) = 0;
13689 return t;
13692 /* Wrapper for xref_tag_1. */
13694 tree
13695 xref_tag (enum tag_types tag_code, tree name,
13696 tag_scope scope, bool template_header_p)
13698 tree ret;
13699 bool subtime;
13700 subtime = timevar_cond_start (TV_NAME_LOOKUP);
13701 ret = xref_tag_1 (tag_code, name, scope, template_header_p);
13702 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
13703 return ret;
13707 tree
13708 xref_tag_from_type (tree old, tree id, tag_scope scope)
13710 enum tag_types tag_kind;
13712 if (TREE_CODE (old) == RECORD_TYPE)
13713 tag_kind = (CLASSTYPE_DECLARED_CLASS (old) ? class_type : record_type);
13714 else
13715 tag_kind = union_type;
13717 if (id == NULL_TREE)
13718 id = TYPE_IDENTIFIER (old);
13720 return xref_tag (tag_kind, id, scope, false);
13723 /* Create the binfo hierarchy for REF with (possibly NULL) base list
13724 BASE_LIST. For each element on BASE_LIST the TREE_PURPOSE is an
13725 access_* node, and the TREE_VALUE is the type of the base-class.
13726 Non-NULL TREE_TYPE indicates virtual inheritance. */
13728 void
13729 xref_basetypes (tree ref, tree base_list)
13731 tree *basep;
13732 tree binfo, base_binfo;
13733 unsigned max_vbases = 0; /* Maximum direct & indirect virtual bases. */
13734 unsigned max_bases = 0; /* Maximum direct bases. */
13735 unsigned max_dvbases = 0; /* Maximum direct virtual bases. */
13736 int i;
13737 tree default_access;
13738 tree igo_prev; /* Track Inheritance Graph Order. */
13740 if (ref == error_mark_node)
13741 return;
13743 /* The base of a derived class is private by default, all others are
13744 public. */
13745 default_access = (TREE_CODE (ref) == RECORD_TYPE
13746 && CLASSTYPE_DECLARED_CLASS (ref)
13747 ? access_private_node : access_public_node);
13749 /* First, make sure that any templates in base-classes are
13750 instantiated. This ensures that if we call ourselves recursively
13751 we do not get confused about which classes are marked and which
13752 are not. */
13753 basep = &base_list;
13754 while (*basep)
13756 tree basetype = TREE_VALUE (*basep);
13758 /* The dependent_type_p call below should really be dependent_scope_p
13759 so that we give a hard error about using an incomplete type as a
13760 base, but we allow it with a pedwarn for backward
13761 compatibility. */
13762 if (processing_template_decl
13763 && CLASS_TYPE_P (basetype) && TYPE_BEING_DEFINED (basetype))
13764 cxx_incomplete_type_diagnostic (NULL_TREE, basetype, DK_PEDWARN);
13765 if (!dependent_type_p (basetype)
13766 && !complete_type_or_else (basetype, NULL))
13767 /* An incomplete type. Remove it from the list. */
13768 *basep = TREE_CHAIN (*basep);
13769 else
13771 max_bases++;
13772 if (TREE_TYPE (*basep))
13773 max_dvbases++;
13774 if (CLASS_TYPE_P (basetype))
13775 max_vbases += vec_safe_length (CLASSTYPE_VBASECLASSES (basetype));
13776 basep = &TREE_CHAIN (*basep);
13779 max_vbases += max_dvbases;
13781 TYPE_MARKED_P (ref) = 1;
13783 /* The binfo slot should be empty, unless this is an (ill-formed)
13784 redefinition. */
13785 gcc_assert (!TYPE_BINFO (ref) || TYPE_SIZE (ref));
13787 gcc_assert (TYPE_MAIN_VARIANT (ref) == ref);
13789 binfo = make_tree_binfo (max_bases);
13791 TYPE_BINFO (ref) = binfo;
13792 BINFO_OFFSET (binfo) = size_zero_node;
13793 BINFO_TYPE (binfo) = ref;
13795 /* Apply base-class info set up to the variants of this type. */
13796 fixup_type_variants (ref);
13798 if (max_bases)
13800 vec_alloc (BINFO_BASE_ACCESSES (binfo), max_bases);
13801 /* A C++98 POD cannot have base classes. */
13802 CLASSTYPE_NON_LAYOUT_POD_P (ref) = true;
13804 if (TREE_CODE (ref) == UNION_TYPE)
13805 error ("derived union %qT invalid", ref);
13808 if (max_bases > 1)
13809 warning (OPT_Wmultiple_inheritance,
13810 "%qT defined with multiple direct bases", ref);
13812 if (max_vbases)
13814 vec_alloc (CLASSTYPE_VBASECLASSES (ref), max_vbases);
13816 if (max_dvbases)
13817 warning (OPT_Wvirtual_inheritance,
13818 "%qT defined with direct virtual base", ref);
13821 for (igo_prev = binfo; base_list; base_list = TREE_CHAIN (base_list))
13823 tree access = TREE_PURPOSE (base_list);
13824 int via_virtual = TREE_TYPE (base_list) != NULL_TREE;
13825 tree basetype = TREE_VALUE (base_list);
13827 if (access == access_default_node)
13828 access = default_access;
13830 /* Before C++17, an aggregate cannot have base classes. In C++17, an
13831 aggregate can't have virtual, private, or protected base classes. */
13832 if (cxx_dialect < cxx1z
13833 || access != access_public_node
13834 || via_virtual)
13835 CLASSTYPE_NON_AGGREGATE (ref) = true;
13837 if (PACK_EXPANSION_P (basetype))
13838 basetype = PACK_EXPANSION_PATTERN (basetype);
13839 if (TREE_CODE (basetype) == TYPE_DECL)
13840 basetype = TREE_TYPE (basetype);
13841 if (!MAYBE_CLASS_TYPE_P (basetype) || TREE_CODE (basetype) == UNION_TYPE)
13843 error ("base type %qT fails to be a struct or class type",
13844 basetype);
13845 goto dropped_base;
13848 base_binfo = NULL_TREE;
13849 if (CLASS_TYPE_P (basetype) && !dependent_scope_p (basetype))
13851 base_binfo = TYPE_BINFO (basetype);
13852 /* The original basetype could have been a typedef'd type. */
13853 basetype = BINFO_TYPE (base_binfo);
13855 /* Inherit flags from the base. */
13856 TYPE_HAS_NEW_OPERATOR (ref)
13857 |= TYPE_HAS_NEW_OPERATOR (basetype);
13858 TYPE_HAS_ARRAY_NEW_OPERATOR (ref)
13859 |= TYPE_HAS_ARRAY_NEW_OPERATOR (basetype);
13860 TYPE_GETS_DELETE (ref) |= TYPE_GETS_DELETE (basetype);
13861 TYPE_HAS_CONVERSION (ref) |= TYPE_HAS_CONVERSION (basetype);
13862 CLASSTYPE_DIAMOND_SHAPED_P (ref)
13863 |= CLASSTYPE_DIAMOND_SHAPED_P (basetype);
13864 CLASSTYPE_REPEATED_BASE_P (ref)
13865 |= CLASSTYPE_REPEATED_BASE_P (basetype);
13868 /* We must do this test after we've seen through a typedef
13869 type. */
13870 if (TYPE_MARKED_P (basetype))
13872 if (basetype == ref)
13873 error ("recursive type %qT undefined", basetype);
13874 else
13875 error ("duplicate base type %qT invalid", basetype);
13876 goto dropped_base;
13879 if (PACK_EXPANSION_P (TREE_VALUE (base_list)))
13880 /* Regenerate the pack expansion for the bases. */
13881 basetype = make_pack_expansion (basetype);
13883 TYPE_MARKED_P (basetype) = 1;
13885 base_binfo = copy_binfo (base_binfo, basetype, ref,
13886 &igo_prev, via_virtual);
13887 if (!BINFO_INHERITANCE_CHAIN (base_binfo))
13888 BINFO_INHERITANCE_CHAIN (base_binfo) = binfo;
13890 BINFO_BASE_APPEND (binfo, base_binfo);
13891 BINFO_BASE_ACCESS_APPEND (binfo, access);
13892 continue;
13894 dropped_base:
13895 /* Update max_vbases to reflect the reality that we are dropping
13896 this base: if it reaches zero we want to undo the vec_alloc
13897 above to avoid inconsistencies during error-recovery: eg, in
13898 build_special_member_call, CLASSTYPE_VBASECLASSES non null
13899 and vtt null (c++/27952). */
13900 if (via_virtual)
13901 max_vbases--;
13902 if (CLASS_TYPE_P (basetype))
13903 max_vbases
13904 -= vec_safe_length (CLASSTYPE_VBASECLASSES (basetype));
13907 if (CLASSTYPE_VBASECLASSES (ref)
13908 && max_vbases == 0)
13909 vec_free (CLASSTYPE_VBASECLASSES (ref));
13911 if (vec_safe_length (CLASSTYPE_VBASECLASSES (ref)) < max_vbases)
13912 /* If we didn't get max_vbases vbases, we must have shared at
13913 least one of them, and are therefore diamond shaped. */
13914 CLASSTYPE_DIAMOND_SHAPED_P (ref) = 1;
13916 /* Unmark all the types. */
13917 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
13918 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
13919 TYPE_MARKED_P (ref) = 0;
13921 /* Now see if we have a repeated base type. */
13922 if (!CLASSTYPE_REPEATED_BASE_P (ref))
13924 for (base_binfo = binfo; base_binfo;
13925 base_binfo = TREE_CHAIN (base_binfo))
13927 if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
13929 CLASSTYPE_REPEATED_BASE_P (ref) = 1;
13930 break;
13932 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 1;
13934 for (base_binfo = binfo; base_binfo;
13935 base_binfo = TREE_CHAIN (base_binfo))
13936 if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
13937 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
13938 else
13939 break;
13944 /* Copies the enum-related properties from type SRC to type DST.
13945 Used with the underlying type of an enum and the enum itself. */
13946 static void
13947 copy_type_enum (tree dst, tree src)
13949 tree t;
13950 for (t = dst; t; t = TYPE_NEXT_VARIANT (t))
13952 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (src);
13953 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (src);
13954 TYPE_SIZE (t) = TYPE_SIZE (src);
13955 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (src);
13956 SET_TYPE_MODE (dst, TYPE_MODE (src));
13957 TYPE_PRECISION (t) = TYPE_PRECISION (src);
13958 unsigned valign = TYPE_ALIGN (src);
13959 if (TYPE_USER_ALIGN (t))
13960 valign = MAX (valign, TYPE_ALIGN (t));
13961 else
13962 TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (src);
13963 SET_TYPE_ALIGN (t, valign);
13964 TYPE_UNSIGNED (t) = TYPE_UNSIGNED (src);
13968 /* Begin compiling the definition of an enumeration type.
13969 NAME is its name,
13971 if ENUMTYPE is not NULL_TREE then the type has alredy been found.
13973 UNDERLYING_TYPE is the type that will be used as the storage for
13974 the enumeration type. This should be NULL_TREE if no storage type
13975 was specified.
13977 ATTRIBUTES are any attributes specified after the enum-key.
13979 SCOPED_ENUM_P is true if this is a scoped enumeration type.
13981 if IS_NEW is not NULL, gets TRUE iff a new type is created.
13983 Returns the type object, as yet incomplete.
13984 Also records info about it so that build_enumerator
13985 may be used to declare the individual values as they are read. */
13987 tree
13988 start_enum (tree name, tree enumtype, tree underlying_type,
13989 tree attributes, bool scoped_enum_p, bool *is_new)
13991 tree prevtype = NULL_TREE;
13992 gcc_assert (identifier_p (name));
13994 if (is_new)
13995 *is_new = false;
13996 /* [C++0x dcl.enum]p5:
13998 If not explicitly specified, the underlying type of a scoped
13999 enumeration type is int. */
14000 if (!underlying_type && scoped_enum_p)
14001 underlying_type = integer_type_node;
14003 if (underlying_type)
14004 underlying_type = cv_unqualified (underlying_type);
14006 /* If this is the real definition for a previous forward reference,
14007 fill in the contents in the same object that used to be the
14008 forward reference. */
14009 if (!enumtype)
14010 enumtype = lookup_and_check_tag (enum_type, name,
14011 /*tag_scope=*/ts_current,
14012 /*template_header_p=*/false);
14014 /* In case of a template_decl, the only check that should be deferred
14015 to instantiation time is the comparison of underlying types. */
14016 if (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE)
14018 if (scoped_enum_p != SCOPED_ENUM_P (enumtype))
14020 error_at (input_location, "scoped/unscoped mismatch "
14021 "in enum %q#T", enumtype);
14022 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
14023 "previous definition here");
14024 enumtype = error_mark_node;
14026 else if (ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) != !! underlying_type)
14028 error_at (input_location, "underlying type mismatch "
14029 "in enum %q#T", enumtype);
14030 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
14031 "previous definition here");
14032 enumtype = error_mark_node;
14034 else if (underlying_type && ENUM_UNDERLYING_TYPE (enumtype)
14035 && !dependent_type_p (underlying_type)
14036 && !dependent_type_p (ENUM_UNDERLYING_TYPE (enumtype))
14037 && !same_type_p (underlying_type,
14038 ENUM_UNDERLYING_TYPE (enumtype)))
14040 error_at (input_location, "different underlying type "
14041 "in enum %q#T", enumtype);
14042 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
14043 "previous definition here");
14044 underlying_type = NULL_TREE;
14048 if (!enumtype || TREE_CODE (enumtype) != ENUMERAL_TYPE
14049 || processing_template_decl)
14051 /* In case of error, make a dummy enum to allow parsing to
14052 continue. */
14053 if (enumtype == error_mark_node)
14055 name = make_anon_name ();
14056 enumtype = NULL_TREE;
14059 /* enumtype may be an ENUMERAL_TYPE if this is a redefinition
14060 of an opaque enum, or an opaque enum of an already defined
14061 enumeration (C++0x only).
14062 In any other case, it'll be NULL_TREE. */
14063 if (!enumtype)
14065 if (is_new)
14066 *is_new = true;
14068 prevtype = enumtype;
14070 /* Do not push the decl more than once, unless we need to
14071 compare underlying types at instantiation time */
14072 if (!enumtype
14073 || TREE_CODE (enumtype) != ENUMERAL_TYPE
14074 || (underlying_type
14075 && dependent_type_p (underlying_type))
14076 || (ENUM_UNDERLYING_TYPE (enumtype)
14077 && dependent_type_p (ENUM_UNDERLYING_TYPE (enumtype))))
14079 enumtype = cxx_make_type (ENUMERAL_TYPE);
14080 enumtype = pushtag (name, enumtype, /*tag_scope=*/ts_current);
14082 else
14083 enumtype = xref_tag (enum_type, name, /*tag_scope=*/ts_current,
14084 false);
14086 if (enumtype == error_mark_node)
14087 return error_mark_node;
14089 /* The enum is considered opaque until the opening '{' of the
14090 enumerator list. */
14091 SET_OPAQUE_ENUM_P (enumtype, true);
14092 ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) = !! underlying_type;
14095 SET_SCOPED_ENUM_P (enumtype, scoped_enum_p);
14097 cplus_decl_attributes (&enumtype, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
14099 if (underlying_type)
14101 if (ENUM_UNDERLYING_TYPE (enumtype))
14102 /* We already checked that it matches, don't change it to a different
14103 typedef variant. */;
14104 else if (CP_INTEGRAL_TYPE_P (underlying_type))
14106 copy_type_enum (enumtype, underlying_type);
14107 ENUM_UNDERLYING_TYPE (enumtype) = underlying_type;
14109 else if (dependent_type_p (underlying_type))
14110 ENUM_UNDERLYING_TYPE (enumtype) = underlying_type;
14111 else
14112 error ("underlying type %<%T%> of %<%T%> must be an integral type",
14113 underlying_type, enumtype);
14116 /* If into a template class, the returned enum is always the first
14117 declaration (opaque or not) seen. This way all the references to
14118 this type will be to the same declaration. The following ones are used
14119 only to check for definition errors. */
14120 if (prevtype && processing_template_decl)
14121 return prevtype;
14122 else
14123 return enumtype;
14126 /* After processing and defining all the values of an enumeration type,
14127 install their decls in the enumeration type.
14128 ENUMTYPE is the type object. */
14130 void
14131 finish_enum_value_list (tree enumtype)
14133 tree values;
14134 tree underlying_type;
14135 tree decl;
14136 tree value;
14137 tree minnode, maxnode;
14138 tree t;
14140 bool fixed_underlying_type_p
14141 = ENUM_UNDERLYING_TYPE (enumtype) != NULL_TREE;
14143 /* We built up the VALUES in reverse order. */
14144 TYPE_VALUES (enumtype) = nreverse (TYPE_VALUES (enumtype));
14146 /* For an enum defined in a template, just set the type of the values;
14147 all further processing is postponed until the template is
14148 instantiated. We need to set the type so that tsubst of a CONST_DECL
14149 works. */
14150 if (processing_template_decl)
14152 for (values = TYPE_VALUES (enumtype);
14153 values;
14154 values = TREE_CHAIN (values))
14155 TREE_TYPE (TREE_VALUE (values)) = enumtype;
14156 return;
14159 /* Determine the minimum and maximum values of the enumerators. */
14160 if (TYPE_VALUES (enumtype))
14162 minnode = maxnode = NULL_TREE;
14164 for (values = TYPE_VALUES (enumtype);
14165 values;
14166 values = TREE_CHAIN (values))
14168 decl = TREE_VALUE (values);
14170 /* [dcl.enum]: Following the closing brace of an enum-specifier,
14171 each enumerator has the type of its enumeration. Prior to the
14172 closing brace, the type of each enumerator is the type of its
14173 initializing value. */
14174 TREE_TYPE (decl) = enumtype;
14176 /* Update the minimum and maximum values, if appropriate. */
14177 value = DECL_INITIAL (decl);
14178 if (value == error_mark_node)
14179 value = integer_zero_node;
14180 /* Figure out what the minimum and maximum values of the
14181 enumerators are. */
14182 if (!minnode)
14183 minnode = maxnode = value;
14184 else if (tree_int_cst_lt (maxnode, value))
14185 maxnode = value;
14186 else if (tree_int_cst_lt (value, minnode))
14187 minnode = value;
14190 else
14191 /* [dcl.enum]
14193 If the enumerator-list is empty, the underlying type is as if
14194 the enumeration had a single enumerator with value 0. */
14195 minnode = maxnode = integer_zero_node;
14197 if (!fixed_underlying_type_p)
14199 /* Compute the number of bits require to represent all values of the
14200 enumeration. We must do this before the type of MINNODE and
14201 MAXNODE are transformed, since tree_int_cst_min_precision relies
14202 on the TREE_TYPE of the value it is passed. */
14203 signop sgn = tree_int_cst_sgn (minnode) >= 0 ? UNSIGNED : SIGNED;
14204 int lowprec = tree_int_cst_min_precision (minnode, sgn);
14205 int highprec = tree_int_cst_min_precision (maxnode, sgn);
14206 int precision = MAX (lowprec, highprec);
14207 unsigned int itk;
14208 bool use_short_enum;
14210 /* Determine the underlying type of the enumeration.
14212 [dcl.enum]
14214 The underlying type of an enumeration is an integral type that
14215 can represent all the enumerator values defined in the
14216 enumeration. It is implementation-defined which integral type is
14217 used as the underlying type for an enumeration except that the
14218 underlying type shall not be larger than int unless the value of
14219 an enumerator cannot fit in an int or unsigned int.
14221 We use "int" or an "unsigned int" as the underlying type, even if
14222 a smaller integral type would work, unless the user has
14223 explicitly requested that we use the smallest possible type. The
14224 user can request that for all enumerations with a command line
14225 flag, or for just one enumeration with an attribute. */
14227 use_short_enum = flag_short_enums
14228 || lookup_attribute ("packed", TYPE_ATTRIBUTES (enumtype));
14230 /* If the precision of the type was specified with an attribute and it
14231 was too small, give an error. Otherwise, use it. */
14232 if (TYPE_PRECISION (enumtype))
14234 if (precision > TYPE_PRECISION (enumtype))
14235 error ("specified mode too small for enumeral values");
14236 else
14238 use_short_enum = true;
14239 precision = TYPE_PRECISION (enumtype);
14243 for (itk = (use_short_enum ? itk_char : itk_int);
14244 itk != itk_none;
14245 itk++)
14247 underlying_type = integer_types[itk];
14248 if (underlying_type != NULL_TREE
14249 && TYPE_PRECISION (underlying_type) >= precision
14250 && TYPE_SIGN (underlying_type) == sgn)
14251 break;
14253 if (itk == itk_none)
14255 /* DR 377
14257 IF no integral type can represent all the enumerator values, the
14258 enumeration is ill-formed. */
14259 error ("no integral type can represent all of the enumerator values "
14260 "for %qT", enumtype);
14261 precision = TYPE_PRECISION (long_long_integer_type_node);
14262 underlying_type = integer_types[itk_unsigned_long_long];
14265 /* [dcl.enum]
14267 The value of sizeof() applied to an enumeration type, an object
14268 of an enumeration type, or an enumerator, is the value of sizeof()
14269 applied to the underlying type. */
14270 copy_type_enum (enumtype, underlying_type);
14272 /* Compute the minimum and maximum values for the type.
14274 [dcl.enum]
14276 For an enumeration where emin is the smallest enumerator and emax
14277 is the largest, the values of the enumeration are the values of the
14278 underlying type in the range bmin to bmax, where bmin and bmax are,
14279 respectively, the smallest and largest values of the smallest bit-
14280 field that can store emin and emax. */
14282 /* The middle-end currently assumes that types with TYPE_PRECISION
14283 narrower than their underlying type are suitably zero or sign
14284 extended to fill their mode. Similarly, it assumes that the front
14285 end assures that a value of a particular type must be within
14286 TYPE_MIN_VALUE and TYPE_MAX_VALUE.
14288 We used to set these fields based on bmin and bmax, but that led
14289 to invalid assumptions like optimizing away bounds checking. So
14290 now we just set the TYPE_PRECISION, TYPE_MIN_VALUE, and
14291 TYPE_MAX_VALUE to the values for the mode above and only restrict
14292 the ENUM_UNDERLYING_TYPE for the benefit of diagnostics. */
14293 ENUM_UNDERLYING_TYPE (enumtype)
14294 = build_distinct_type_copy (underlying_type);
14295 TYPE_PRECISION (ENUM_UNDERLYING_TYPE (enumtype)) = precision;
14296 set_min_and_max_values_for_integral_type
14297 (ENUM_UNDERLYING_TYPE (enumtype), precision, sgn);
14299 /* If -fstrict-enums, still constrain TYPE_MIN/MAX_VALUE. */
14300 if (flag_strict_enums)
14301 set_min_and_max_values_for_integral_type (enumtype, precision, sgn);
14303 else
14304 underlying_type = ENUM_UNDERLYING_TYPE (enumtype);
14306 /* Convert each of the enumerators to the type of the underlying
14307 type of the enumeration. */
14308 for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values))
14310 location_t saved_location;
14312 decl = TREE_VALUE (values);
14313 saved_location = input_location;
14314 input_location = DECL_SOURCE_LOCATION (decl);
14315 if (fixed_underlying_type_p)
14316 /* If the enumeration type has a fixed underlying type, we
14317 already checked all of the enumerator values. */
14318 value = DECL_INITIAL (decl);
14319 else
14320 value = perform_implicit_conversion (underlying_type,
14321 DECL_INITIAL (decl),
14322 tf_warning_or_error);
14323 input_location = saved_location;
14325 /* Do not clobber shared ints. */
14326 value = copy_node (value);
14328 TREE_TYPE (value) = enumtype;
14329 DECL_INITIAL (decl) = value;
14332 /* Fix up all variant types of this enum type. */
14333 for (t = TYPE_MAIN_VARIANT (enumtype); t; t = TYPE_NEXT_VARIANT (t))
14334 TYPE_VALUES (t) = TYPE_VALUES (enumtype);
14336 if (at_class_scope_p ()
14337 && COMPLETE_TYPE_P (current_class_type)
14338 && UNSCOPED_ENUM_P (enumtype))
14340 insert_late_enum_def_into_classtype_sorted_fields (enumtype,
14341 current_class_type);
14342 fixup_type_variants (current_class_type);
14345 /* Finish debugging output for this type. */
14346 rest_of_type_compilation (enumtype, namespace_bindings_p ());
14348 /* Each enumerator now has the type of its enumeration. Clear the cache
14349 so that this change in types doesn't confuse us later on. */
14350 clear_cv_and_fold_caches ();
14353 /* Finishes the enum type. This is called only the first time an
14354 enumeration is seen, be it opaque or odinary.
14355 ENUMTYPE is the type object. */
14357 void
14358 finish_enum (tree enumtype)
14360 if (processing_template_decl)
14362 if (at_function_scope_p ())
14363 add_stmt (build_min (TAG_DEFN, enumtype));
14364 return;
14367 /* If this is a forward declaration, there should not be any variants,
14368 though we can get a variant in the middle of an enum-specifier with
14369 wacky code like 'enum E { e = sizeof(const E*) };' */
14370 gcc_assert (enumtype == TYPE_MAIN_VARIANT (enumtype)
14371 && (TYPE_VALUES (enumtype)
14372 || !TYPE_NEXT_VARIANT (enumtype)));
14375 /* Build and install a CONST_DECL for an enumeration constant of the
14376 enumeration type ENUMTYPE whose NAME and VALUE (if any) are provided.
14377 Apply ATTRIBUTES if available. LOC is the location of NAME.
14378 Assignment of sequential values by default is handled here. */
14380 void
14381 build_enumerator (tree name, tree value, tree enumtype, tree attributes,
14382 location_t loc)
14384 tree decl;
14385 tree context;
14386 tree type;
14388 /* scalar_constant_value will pull out this expression, so make sure
14389 it's folded as appropriate. */
14390 if (processing_template_decl)
14391 value = fold_non_dependent_expr (value);
14393 /* If the VALUE was erroneous, pretend it wasn't there; that will
14394 result in the enum being assigned the next value in sequence. */
14395 if (value == error_mark_node)
14396 value = NULL_TREE;
14398 /* Remove no-op casts from the value. */
14399 if (value)
14400 STRIP_TYPE_NOPS (value);
14402 if (! processing_template_decl)
14404 /* Validate and default VALUE. */
14405 if (value != NULL_TREE)
14407 if (!ENUM_UNDERLYING_TYPE (enumtype))
14409 tree tmp_value = build_expr_type_conversion (WANT_INT | WANT_ENUM,
14410 value, true);
14411 if (tmp_value)
14412 value = tmp_value;
14414 else if (! INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P
14415 (TREE_TYPE (value)))
14416 value = perform_implicit_conversion_flags
14417 (ENUM_UNDERLYING_TYPE (enumtype), value, tf_warning_or_error,
14418 LOOKUP_IMPLICIT | LOOKUP_NO_NARROWING);
14420 if (value == error_mark_node)
14421 value = NULL_TREE;
14423 if (value != NULL_TREE)
14425 if (! INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P
14426 (TREE_TYPE (value)))
14428 error ("enumerator value for %qD must have integral or "
14429 "unscoped enumeration type", name);
14430 value = NULL_TREE;
14432 else
14434 value = cxx_constant_value (value);
14436 if (TREE_CODE (value) != INTEGER_CST)
14438 error ("enumerator value for %qD is not an integer "
14439 "constant", name);
14440 value = NULL_TREE;
14446 /* Default based on previous value. */
14447 if (value == NULL_TREE)
14449 if (TYPE_VALUES (enumtype))
14451 tree prev_value;
14452 bool overflowed;
14454 /* C++03 7.2/4: If no initializer is specified for the first
14455 enumerator, the type is an unspecified integral
14456 type. Otherwise the type is the same as the type of the
14457 initializing value of the preceding enumerator unless the
14458 incremented value is not representable in that type, in
14459 which case the type is an unspecified integral type
14460 sufficient to contain the incremented value. */
14461 prev_value = DECL_INITIAL (TREE_VALUE (TYPE_VALUES (enumtype)));
14462 if (error_operand_p (prev_value))
14463 value = error_mark_node;
14464 else
14466 tree type = TREE_TYPE (prev_value);
14467 signop sgn = TYPE_SIGN (type);
14468 widest_int wi = wi::add (wi::to_widest (prev_value), 1, sgn,
14469 &overflowed);
14470 if (!overflowed)
14472 bool pos = !wi::neg_p (wi, sgn);
14473 if (!wi::fits_to_tree_p (wi, type))
14475 unsigned int itk;
14476 for (itk = itk_int; itk != itk_none; itk++)
14478 type = integer_types[itk];
14479 if (type != NULL_TREE
14480 && (pos || !TYPE_UNSIGNED (type))
14481 && wi::fits_to_tree_p (wi, type))
14482 break;
14484 if (type && cxx_dialect < cxx11
14485 && itk > itk_unsigned_long)
14486 pedwarn (input_location, OPT_Wlong_long,
14487 pos ? G_("\
14488 incremented enumerator value is too large for %<unsigned long%>") : G_("\
14489 incremented enumerator value is too large for %<long%>"));
14491 if (type == NULL_TREE)
14492 overflowed = true;
14493 else
14494 value = wide_int_to_tree (type, wi);
14497 if (overflowed)
14499 error ("overflow in enumeration values at %qD", name);
14500 value = error_mark_node;
14504 else
14505 value = integer_zero_node;
14508 /* Remove no-op casts from the value. */
14509 STRIP_TYPE_NOPS (value);
14511 /* If the underlying type of the enum is fixed, check whether
14512 the enumerator values fits in the underlying type. If it
14513 does not fit, the program is ill-formed [C++0x dcl.enum]. */
14514 if (ENUM_UNDERLYING_TYPE (enumtype)
14515 && value
14516 && TREE_CODE (value) == INTEGER_CST)
14518 if (!int_fits_type_p (value, ENUM_UNDERLYING_TYPE (enumtype)))
14519 error ("enumerator value %E is outside the range of underlying "
14520 "type %<%T%>", value, ENUM_UNDERLYING_TYPE (enumtype));
14522 /* Convert the value to the appropriate type. */
14523 value = fold_convert (ENUM_UNDERLYING_TYPE (enumtype), value);
14527 /* C++ associates enums with global, function, or class declarations. */
14528 context = current_scope ();
14530 /* Build the actual enumeration constant. Note that the enumeration
14531 constants have the underlying type of the enum (if it is fixed)
14532 or the type of their initializer (if the underlying type of the
14533 enum is not fixed):
14535 [ C++0x dcl.enum ]
14537 If the underlying type is fixed, the type of each enumerator
14538 prior to the closing brace is the underlying type; if the
14539 initializing value of an enumerator cannot be represented by
14540 the underlying type, the program is ill-formed. If the
14541 underlying type is not fixed, the type of each enumerator is
14542 the type of its initializing value.
14544 If the underlying type is not fixed, it will be computed by
14545 finish_enum and we will reset the type of this enumerator. Of
14546 course, if we're processing a template, there may be no value. */
14547 type = value ? TREE_TYPE (value) : NULL_TREE;
14549 decl = build_decl (loc, CONST_DECL, name, type);
14551 DECL_CONTEXT (decl) = enumtype;
14552 TREE_CONSTANT (decl) = 1;
14553 TREE_READONLY (decl) = 1;
14554 DECL_INITIAL (decl) = value;
14556 if (attributes)
14557 cplus_decl_attributes (&decl, attributes, 0);
14559 if (context && context == current_class_type && !SCOPED_ENUM_P (enumtype))
14561 /* In something like `struct S { enum E { i = 7 }; };' we put `i'
14562 on the TYPE_FIELDS list for `S'. (That's so that you can say
14563 things like `S::i' later.) */
14565 /* The enumerator may be getting declared outside of its enclosing
14566 class, like so:
14568 class S { public: enum E : int; }; enum S::E : int { i = 7; };
14570 For which case we need to make sure that the access of `S::i'
14571 matches the access of `S::E'. */
14572 tree saved_cas = current_access_specifier;
14573 if (TREE_PRIVATE (TYPE_NAME (enumtype)))
14574 current_access_specifier = access_private_node;
14575 else if (TREE_PROTECTED (TYPE_NAME (enumtype)))
14576 current_access_specifier = access_protected_node;
14577 else
14578 current_access_specifier = access_public_node;
14580 finish_member_declaration (decl);
14582 current_access_specifier = saved_cas;
14584 else
14585 pushdecl (decl);
14587 /* Add this enumeration constant to the list for this type. */
14588 TYPE_VALUES (enumtype) = tree_cons (name, decl, TYPE_VALUES (enumtype));
14591 /* Look for an enumerator with the given NAME within the enumeration
14592 type ENUMTYPE. This routine is used primarily for qualified name
14593 lookup into an enumerator in C++0x, e.g.,
14595 enum class Color { Red, Green, Blue };
14597 Color color = Color::Red;
14599 Returns the value corresponding to the enumerator, or
14600 NULL_TREE if no such enumerator was found. */
14601 tree
14602 lookup_enumerator (tree enumtype, tree name)
14604 tree e;
14605 gcc_assert (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE);
14607 e = purpose_member (name, TYPE_VALUES (enumtype));
14608 return e? TREE_VALUE (e) : NULL_TREE;
14612 /* We're defining DECL. Make sure that its type is OK. */
14614 static void
14615 check_function_type (tree decl, tree current_function_parms)
14617 tree fntype = TREE_TYPE (decl);
14618 tree return_type = complete_type (TREE_TYPE (fntype));
14620 /* In a function definition, arg types must be complete. */
14621 require_complete_types_for_parms (current_function_parms);
14623 if (dependent_type_p (return_type)
14624 || type_uses_auto (return_type))
14625 return;
14626 if (!COMPLETE_OR_VOID_TYPE_P (return_type))
14628 tree args = TYPE_ARG_TYPES (fntype);
14630 error ("return type %q#T is incomplete", return_type);
14632 /* Make it return void instead. */
14633 if (TREE_CODE (fntype) == METHOD_TYPE)
14634 fntype = build_method_type_directly (TREE_TYPE (TREE_VALUE (args)),
14635 void_type_node,
14636 TREE_CHAIN (args));
14637 else
14638 fntype = build_function_type (void_type_node, args);
14639 fntype
14640 = build_exception_variant (fntype,
14641 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl)));
14642 fntype = (cp_build_type_attribute_variant
14643 (fntype, TYPE_ATTRIBUTES (TREE_TYPE (decl))));
14644 TREE_TYPE (decl) = fntype;
14646 else
14647 abstract_virtuals_error (decl, TREE_TYPE (fntype));
14650 /* True iff FN is an implicitly-defined default constructor. */
14652 static bool
14653 implicit_default_ctor_p (tree fn)
14655 return (DECL_CONSTRUCTOR_P (fn)
14656 && !user_provided_p (fn)
14657 && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn)));
14660 /* Clobber the contents of *this to let the back end know that the object
14661 storage is dead when we enter the constructor or leave the destructor. */
14663 static tree
14664 build_clobber_this ()
14666 /* Clobbering an empty base is pointless, and harmful if its one byte
14667 TYPE_SIZE overlays real data. */
14668 if (is_empty_class (current_class_type))
14669 return void_node;
14671 /* If we have virtual bases, clobber the whole object, but only if we're in
14672 charge. If we don't have virtual bases, clobber the as-base type so we
14673 don't mess with tail padding. */
14674 bool vbases = CLASSTYPE_VBASECLASSES (current_class_type);
14676 tree ctype = current_class_type;
14677 if (!vbases)
14678 ctype = CLASSTYPE_AS_BASE (ctype);
14680 tree clobber = build_constructor (ctype, NULL);
14681 TREE_THIS_VOLATILE (clobber) = true;
14683 tree thisref = current_class_ref;
14684 if (ctype != current_class_type)
14686 thisref = build_nop (build_reference_type (ctype), current_class_ptr);
14687 thisref = convert_from_reference (thisref);
14690 tree exprstmt = build2 (MODIFY_EXPR, void_type_node, thisref, clobber);
14691 if (vbases)
14692 exprstmt = build_if_in_charge (exprstmt);
14694 return exprstmt;
14697 /* Create the FUNCTION_DECL for a function definition.
14698 DECLSPECS and DECLARATOR are the parts of the declaration;
14699 they describe the function's name and the type it returns,
14700 but twisted together in a fashion that parallels the syntax of C.
14702 FLAGS is a bitwise or of SF_PRE_PARSED (indicating that the
14703 DECLARATOR is really the DECL for the function we are about to
14704 process and that DECLSPECS should be ignored), SF_INCLASS_INLINE
14705 indicating that the function is an inline defined in-class.
14707 This function creates a binding context for the function body
14708 as well as setting up the FUNCTION_DECL in current_function_decl.
14710 For C++, we must first check whether that datum makes any sense.
14711 For example, "class A local_a(1,2);" means that variable local_a
14712 is an aggregate of type A, which should have a constructor
14713 applied to it with the argument list [1, 2].
14715 On entry, DECL_INITIAL (decl1) should be NULL_TREE or error_mark_node,
14716 or may be a BLOCK if the function has been defined previously
14717 in this translation unit. On exit, DECL_INITIAL (decl1) will be
14718 error_mark_node if the function has never been defined, or
14719 a BLOCK if the function has been defined somewhere. */
14721 bool
14722 start_preparsed_function (tree decl1, tree attrs, int flags)
14724 tree ctype = NULL_TREE;
14725 tree fntype;
14726 tree restype;
14727 int doing_friend = 0;
14728 cp_binding_level *bl;
14729 tree current_function_parms;
14730 struct c_fileinfo *finfo
14731 = get_fileinfo (LOCATION_FILE (DECL_SOURCE_LOCATION (decl1)));
14732 bool honor_interface;
14734 /* Sanity check. */
14735 gcc_assert (VOID_TYPE_P (TREE_VALUE (void_list_node)));
14736 gcc_assert (TREE_CHAIN (void_list_node) == NULL_TREE);
14738 fntype = TREE_TYPE (decl1);
14739 if (TREE_CODE (fntype) == METHOD_TYPE)
14740 ctype = TYPE_METHOD_BASETYPE (fntype);
14742 /* ISO C++ 11.4/5. A friend function defined in a class is in
14743 the (lexical) scope of the class in which it is defined. */
14744 if (!ctype && DECL_FRIEND_P (decl1))
14746 ctype = DECL_FRIEND_CONTEXT (decl1);
14748 /* CTYPE could be null here if we're dealing with a template;
14749 for example, `inline friend float foo()' inside a template
14750 will have no CTYPE set. */
14751 if (ctype && TREE_CODE (ctype) != RECORD_TYPE)
14752 ctype = NULL_TREE;
14753 else
14754 doing_friend = 1;
14757 if (DECL_DECLARED_INLINE_P (decl1)
14758 && lookup_attribute ("noinline", attrs))
14759 warning_at (DECL_SOURCE_LOCATION (decl1), 0,
14760 "inline function %qD given attribute noinline", decl1);
14762 /* Handle gnu_inline attribute. */
14763 if (GNU_INLINE_P (decl1))
14765 DECL_EXTERNAL (decl1) = 1;
14766 DECL_NOT_REALLY_EXTERN (decl1) = 0;
14767 DECL_INTERFACE_KNOWN (decl1) = 1;
14768 DECL_DISREGARD_INLINE_LIMITS (decl1) = 1;
14771 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl1))
14772 /* This is a constructor, we must ensure that any default args
14773 introduced by this definition are propagated to the clones
14774 now. The clones are used directly in overload resolution. */
14775 adjust_clone_args (decl1);
14777 /* Sometimes we don't notice that a function is a static member, and
14778 build a METHOD_TYPE for it. Fix that up now. */
14779 gcc_assert (!(ctype != NULL_TREE && DECL_STATIC_FUNCTION_P (decl1)
14780 && TREE_CODE (TREE_TYPE (decl1)) == METHOD_TYPE));
14782 /* Set up current_class_type, and enter the scope of the class, if
14783 appropriate. */
14784 if (ctype)
14785 push_nested_class (ctype);
14786 else if (DECL_STATIC_FUNCTION_P (decl1))
14787 push_nested_class (DECL_CONTEXT (decl1));
14789 /* Now that we have entered the scope of the class, we must restore
14790 the bindings for any template parameters surrounding DECL1, if it
14791 is an inline member template. (Order is important; consider the
14792 case where a template parameter has the same name as a field of
14793 the class.) It is not until after this point that
14794 PROCESSING_TEMPLATE_DECL is guaranteed to be set up correctly. */
14795 if (flags & SF_INCLASS_INLINE)
14796 maybe_begin_member_template_processing (decl1);
14798 /* Effective C++ rule 15. */
14799 if (warn_ecpp
14800 && DECL_OVERLOADED_OPERATOR_P (decl1) == NOP_EXPR
14801 && VOID_TYPE_P (TREE_TYPE (fntype)))
14802 warning (OPT_Weffc__, "%<operator=%> should return a reference to %<*this%>");
14804 /* Make the init_value nonzero so pushdecl knows this is not tentative.
14805 error_mark_node is replaced below (in poplevel) with the BLOCK. */
14806 if (!DECL_INITIAL (decl1))
14807 DECL_INITIAL (decl1) = error_mark_node;
14809 /* This function exists in static storage.
14810 (This does not mean `static' in the C sense!) */
14811 TREE_STATIC (decl1) = 1;
14813 /* We must call push_template_decl after current_class_type is set
14814 up. (If we are processing inline definitions after exiting a
14815 class scope, current_class_type will be NULL_TREE until set above
14816 by push_nested_class.) */
14817 if (processing_template_decl)
14819 tree newdecl1 = push_template_decl (decl1);
14820 if (newdecl1 == error_mark_node)
14822 if (ctype || DECL_STATIC_FUNCTION_P (decl1))
14823 pop_nested_class ();
14824 return false;
14826 decl1 = newdecl1;
14829 /* We are now in the scope of the function being defined. */
14830 current_function_decl = decl1;
14832 /* Save the parm names or decls from this function's declarator
14833 where store_parm_decls will find them. */
14834 current_function_parms = DECL_ARGUMENTS (decl1);
14836 /* Make sure the parameter and return types are reasonable. When
14837 you declare a function, these types can be incomplete, but they
14838 must be complete when you define the function. */
14839 check_function_type (decl1, current_function_parms);
14841 /* Build the return declaration for the function. */
14842 restype = TREE_TYPE (fntype);
14844 if (DECL_RESULT (decl1) == NULL_TREE)
14846 tree resdecl;
14848 resdecl = build_decl (input_location, RESULT_DECL, 0, restype);
14849 DECL_ARTIFICIAL (resdecl) = 1;
14850 DECL_IGNORED_P (resdecl) = 1;
14851 DECL_RESULT (decl1) = resdecl;
14853 cp_apply_type_quals_to_decl (cp_type_quals (restype), resdecl);
14856 /* Let the user know we're compiling this function. */
14857 announce_function (decl1);
14859 /* Record the decl so that the function name is defined.
14860 If we already have a decl for this name, and it is a FUNCTION_DECL,
14861 use the old decl. */
14862 if (!processing_template_decl && !(flags & SF_PRE_PARSED))
14864 /* A specialization is not used to guide overload resolution. */
14865 if (!DECL_FUNCTION_MEMBER_P (decl1)
14866 && !(DECL_USE_TEMPLATE (decl1) &&
14867 PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl1))))
14869 tree olddecl = pushdecl (decl1);
14871 if (olddecl == error_mark_node)
14872 /* If something went wrong when registering the declaration,
14873 use DECL1; we have to have a FUNCTION_DECL to use when
14874 parsing the body of the function. */
14876 else
14878 /* Otherwise, OLDDECL is either a previous declaration
14879 of the same function or DECL1 itself. */
14881 if (warn_missing_declarations
14882 && olddecl == decl1
14883 && !DECL_MAIN_P (decl1)
14884 && TREE_PUBLIC (decl1)
14885 && !DECL_DECLARED_INLINE_P (decl1))
14887 tree context;
14889 /* Check whether DECL1 is in an anonymous
14890 namespace. */
14891 for (context = DECL_CONTEXT (decl1);
14892 context;
14893 context = DECL_CONTEXT (context))
14895 if (TREE_CODE (context) == NAMESPACE_DECL
14896 && DECL_NAME (context) == NULL_TREE)
14897 break;
14900 if (context == NULL)
14901 warning_at (DECL_SOURCE_LOCATION (decl1),
14902 OPT_Wmissing_declarations,
14903 "no previous declaration for %qD", decl1);
14906 decl1 = olddecl;
14909 else
14911 /* We need to set the DECL_CONTEXT. */
14912 if (!DECL_CONTEXT (decl1) && DECL_TEMPLATE_INFO (decl1))
14913 DECL_CONTEXT (decl1) = DECL_CONTEXT (DECL_TI_TEMPLATE (decl1));
14915 fntype = TREE_TYPE (decl1);
14916 restype = TREE_TYPE (fntype);
14918 /* If #pragma weak applies, mark the decl appropriately now.
14919 The pragma only applies to global functions. Because
14920 determining whether or not the #pragma applies involves
14921 computing the mangled name for the declaration, we cannot
14922 apply the pragma until after we have merged this declaration
14923 with any previous declarations; if the original declaration
14924 has a linkage specification, that specification applies to
14925 the definition as well, and may affect the mangled name. */
14926 if (DECL_FILE_SCOPE_P (decl1))
14927 maybe_apply_pragma_weak (decl1);
14930 /* Reset this in case the call to pushdecl changed it. */
14931 current_function_decl = decl1;
14933 gcc_assert (DECL_INITIAL (decl1));
14935 /* This function may already have been parsed, in which case just
14936 return; our caller will skip over the body without parsing. */
14937 if (DECL_INITIAL (decl1) != error_mark_node)
14938 return true;
14940 /* Initialize RTL machinery. We cannot do this until
14941 CURRENT_FUNCTION_DECL and DECL_RESULT are set up. We do this
14942 even when processing a template; this is how we get
14943 CFUN set up, and our per-function variables initialized.
14944 FIXME factor out the non-RTL stuff. */
14945 bl = current_binding_level;
14946 allocate_struct_function (decl1, processing_template_decl);
14948 /* Initialize the language data structures. Whenever we start
14949 a new function, we destroy temporaries in the usual way. */
14950 cfun->language = ggc_cleared_alloc<language_function> ();
14951 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
14952 current_binding_level = bl;
14954 if (!processing_template_decl && type_uses_auto (restype))
14956 FNDECL_USED_AUTO (decl1) = true;
14957 current_function_auto_return_pattern = restype;
14960 /* Start the statement-tree, start the tree now. */
14961 DECL_SAVED_TREE (decl1) = push_stmt_list ();
14963 /* If we are (erroneously) defining a function that we have already
14964 defined before, wipe out what we knew before. */
14965 if (!DECL_PENDING_INLINE_P (decl1))
14966 DECL_SAVED_FUNCTION_DATA (decl1) = NULL;
14968 if (ctype && !doing_friend && !DECL_STATIC_FUNCTION_P (decl1))
14970 /* We know that this was set up by `grokclassfn'. We do not
14971 wait until `store_parm_decls', since evil parse errors may
14972 never get us to that point. Here we keep the consistency
14973 between `current_class_type' and `current_class_ptr'. */
14974 tree t = DECL_ARGUMENTS (decl1);
14976 gcc_assert (t != NULL_TREE && TREE_CODE (t) == PARM_DECL);
14977 gcc_assert (TYPE_PTR_P (TREE_TYPE (t)));
14979 cp_function_chain->x_current_class_ref
14980 = cp_build_indirect_ref (t, RO_NULL, tf_warning_or_error);
14981 /* Set this second to avoid shortcut in cp_build_indirect_ref. */
14982 cp_function_chain->x_current_class_ptr = t;
14984 /* Constructors and destructors need to know whether they're "in
14985 charge" of initializing virtual base classes. */
14986 t = DECL_CHAIN (t);
14987 if (DECL_HAS_IN_CHARGE_PARM_P (decl1))
14989 current_in_charge_parm = t;
14990 t = DECL_CHAIN (t);
14992 if (DECL_HAS_VTT_PARM_P (decl1))
14994 gcc_assert (DECL_NAME (t) == vtt_parm_identifier);
14995 current_vtt_parm = t;
14999 honor_interface = (!DECL_TEMPLATE_INSTANTIATION (decl1)
15000 /* Implicitly-defined methods (like the
15001 destructor for a class in which no destructor
15002 is explicitly declared) must not be defined
15003 until their definition is needed. So, we
15004 ignore interface specifications for
15005 compiler-generated functions. */
15006 && !DECL_ARTIFICIAL (decl1));
15008 if (processing_template_decl)
15009 /* Don't mess with interface flags. */;
15010 else if (DECL_INTERFACE_KNOWN (decl1))
15012 tree ctx = decl_function_context (decl1);
15014 if (DECL_NOT_REALLY_EXTERN (decl1))
15015 DECL_EXTERNAL (decl1) = 0;
15017 if (ctx != NULL_TREE && vague_linkage_p (ctx))
15018 /* This is a function in a local class in an extern inline
15019 or template function. */
15020 comdat_linkage (decl1);
15022 /* If this function belongs to an interface, it is public.
15023 If it belongs to someone else's interface, it is also external.
15024 This only affects inlines and template instantiations. */
15025 else if (!finfo->interface_unknown && honor_interface)
15027 if (DECL_DECLARED_INLINE_P (decl1)
15028 || DECL_TEMPLATE_INSTANTIATION (decl1))
15030 DECL_EXTERNAL (decl1)
15031 = (finfo->interface_only
15032 || (DECL_DECLARED_INLINE_P (decl1)
15033 && ! flag_implement_inlines
15034 && !DECL_VINDEX (decl1)));
15036 /* For WIN32 we also want to put these in linkonce sections. */
15037 maybe_make_one_only (decl1);
15039 else
15040 DECL_EXTERNAL (decl1) = 0;
15041 DECL_INTERFACE_KNOWN (decl1) = 1;
15042 /* If this function is in an interface implemented in this file,
15043 make sure that the back end knows to emit this function
15044 here. */
15045 if (!DECL_EXTERNAL (decl1))
15046 mark_needed (decl1);
15048 else if (finfo->interface_unknown && finfo->interface_only
15049 && honor_interface)
15051 /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma
15052 interface, we will have both finfo->interface_unknown and
15053 finfo->interface_only set. In that case, we don't want to
15054 use the normal heuristics because someone will supply a
15055 #pragma implementation elsewhere, and deducing it here would
15056 produce a conflict. */
15057 comdat_linkage (decl1);
15058 DECL_EXTERNAL (decl1) = 0;
15059 DECL_INTERFACE_KNOWN (decl1) = 1;
15060 DECL_DEFER_OUTPUT (decl1) = 1;
15062 else
15064 /* This is a definition, not a reference.
15065 So clear DECL_EXTERNAL, unless this is a GNU extern inline. */
15066 if (!GNU_INLINE_P (decl1))
15067 DECL_EXTERNAL (decl1) = 0;
15069 if ((DECL_DECLARED_INLINE_P (decl1)
15070 || DECL_TEMPLATE_INSTANTIATION (decl1))
15071 && ! DECL_INTERFACE_KNOWN (decl1))
15072 DECL_DEFER_OUTPUT (decl1) = 1;
15073 else
15074 DECL_INTERFACE_KNOWN (decl1) = 1;
15077 /* Determine the ELF visibility attribute for the function. We must not
15078 do this before calling "pushdecl", as we must allow "duplicate_decls"
15079 to merge any attributes appropriately. We also need to wait until
15080 linkage is set. */
15081 if (!DECL_CLONED_FUNCTION_P (decl1))
15082 determine_visibility (decl1);
15084 if (!processing_template_decl)
15085 maybe_instantiate_noexcept (decl1);
15087 begin_scope (sk_function_parms, decl1);
15089 ++function_depth;
15091 if (DECL_DESTRUCTOR_P (decl1)
15092 || (DECL_CONSTRUCTOR_P (decl1)
15093 && targetm.cxx.cdtor_returns_this ()))
15094 cdtor_label = create_artificial_label (input_location);
15096 start_fname_decls ();
15098 store_parm_decls (current_function_parms);
15100 if (!processing_template_decl
15101 && (flag_lifetime_dse > 1)
15102 && DECL_CONSTRUCTOR_P (decl1)
15103 && !DECL_CLONED_FUNCTION_P (decl1)
15104 /* Clobbering an empty base is harmful if it overlays real data. */
15105 && !is_empty_class (current_class_type)
15106 /* We can't clobber safely for an implicitly-defined default constructor
15107 because part of the initialization might happen before we enter the
15108 constructor, via AGGR_INIT_ZERO_FIRST (c++/68006). */
15109 && !implicit_default_ctor_p (decl1))
15110 finish_expr_stmt (build_clobber_this ());
15112 if (!processing_template_decl
15113 && DECL_CONSTRUCTOR_P (decl1)
15114 && (flag_sanitize & SANITIZE_VPTR)
15115 && !DECL_CLONED_FUNCTION_P (decl1)
15116 && !implicit_default_ctor_p (decl1))
15117 cp_ubsan_maybe_initialize_vtbl_ptrs (current_class_ptr);
15119 return true;
15123 /* Like start_preparsed_function, except that instead of a
15124 FUNCTION_DECL, this function takes DECLSPECS and DECLARATOR.
15126 Returns true on success. If the DECLARATOR is not suitable
15127 for a function, we return false, which tells the parser to
15128 skip the entire function. */
15130 bool
15131 start_function (cp_decl_specifier_seq *declspecs,
15132 const cp_declarator *declarator,
15133 tree attrs)
15135 tree decl1;
15137 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, &attrs);
15138 invoke_plugin_callbacks (PLUGIN_START_PARSE_FUNCTION, decl1);
15139 if (decl1 == error_mark_node)
15140 return false;
15141 /* If the declarator is not suitable for a function definition,
15142 cause a syntax error. */
15143 if (decl1 == NULL_TREE || TREE_CODE (decl1) != FUNCTION_DECL)
15145 error ("invalid function declaration");
15146 return false;
15149 if (DECL_MAIN_P (decl1))
15150 /* main must return int. grokfndecl should have corrected it
15151 (and issued a diagnostic) if the user got it wrong. */
15152 gcc_assert (same_type_p (TREE_TYPE (TREE_TYPE (decl1)),
15153 integer_type_node));
15155 return start_preparsed_function (decl1, attrs, /*flags=*/SF_DEFAULT);
15158 /* Returns true iff an EH_SPEC_BLOCK should be created in the body of
15159 FN. */
15161 static bool
15162 use_eh_spec_block (tree fn)
15164 return (flag_exceptions && flag_enforce_eh_specs
15165 && !processing_template_decl
15166 && !type_throw_all_p (TREE_TYPE (fn))
15167 /* We insert the EH_SPEC_BLOCK only in the original
15168 function; then, it is copied automatically to the
15169 clones. */
15170 && !DECL_CLONED_FUNCTION_P (fn)
15171 /* Implicitly-generated constructors and destructors have
15172 exception specifications. However, those specifications
15173 are the union of the possible exceptions specified by the
15174 constructors/destructors for bases and members, so no
15175 unallowed exception will ever reach this function. By
15176 not creating the EH_SPEC_BLOCK we save a little memory,
15177 and we avoid spurious warnings about unreachable
15178 code. */
15179 && !DECL_DEFAULTED_FN (fn));
15182 /* Store the parameter declarations into the current function declaration.
15183 This is called after parsing the parameter declarations, before
15184 digesting the body of the function.
15186 Also install to binding contour return value identifier, if any. */
15188 static void
15189 store_parm_decls (tree current_function_parms)
15191 tree fndecl = current_function_decl;
15192 tree parm;
15194 /* This is a chain of any other decls that came in among the parm
15195 declarations. If a parm is declared with enum {foo, bar} x;
15196 then CONST_DECLs for foo and bar are put here. */
15197 tree nonparms = NULL_TREE;
15199 if (current_function_parms)
15201 /* This case is when the function was defined with an ANSI prototype.
15202 The parms already have decls, so we need not do anything here
15203 except record them as in effect
15204 and complain if any redundant old-style parm decls were written. */
15206 tree specparms = current_function_parms;
15207 tree next;
15209 /* Must clear this because it might contain TYPE_DECLs declared
15210 at class level. */
15211 current_binding_level->names = NULL;
15213 /* If we're doing semantic analysis, then we'll call pushdecl
15214 for each of these. We must do them in reverse order so that
15215 they end in the correct forward order. */
15216 specparms = nreverse (specparms);
15218 for (parm = specparms; parm; parm = next)
15220 next = DECL_CHAIN (parm);
15221 if (TREE_CODE (parm) == PARM_DECL)
15222 pushdecl (parm);
15223 else
15225 /* If we find an enum constant or a type tag,
15226 put it aside for the moment. */
15227 TREE_CHAIN (parm) = NULL_TREE;
15228 nonparms = chainon (nonparms, parm);
15232 /* Get the decls in their original chain order and record in the
15233 function. This is all and only the PARM_DECLs that were
15234 pushed into scope by the loop above. */
15235 DECL_ARGUMENTS (fndecl) = getdecls ();
15237 else
15238 DECL_ARGUMENTS (fndecl) = NULL_TREE;
15240 /* Now store the final chain of decls for the arguments
15241 as the decl-chain of the current lexical scope.
15242 Put the enumerators in as well, at the front so that
15243 DECL_ARGUMENTS is not modified. */
15244 current_binding_level->names = chainon (nonparms, DECL_ARGUMENTS (fndecl));
15246 if (use_eh_spec_block (current_function_decl))
15247 current_eh_spec_block = begin_eh_spec_block ();
15251 /* We have finished doing semantic analysis on DECL, but have not yet
15252 generated RTL for its body. Save away our current state, so that
15253 when we want to generate RTL later we know what to do. */
15255 static void
15256 save_function_data (tree decl)
15258 struct language_function *f;
15260 /* Save the language-specific per-function data so that we can
15261 get it back when we really expand this function. */
15262 gcc_assert (!DECL_PENDING_INLINE_P (decl));
15264 /* Make a copy. */
15265 f = ggc_alloc<language_function> ();
15266 memcpy (f, cp_function_chain, sizeof (struct language_function));
15267 DECL_SAVED_FUNCTION_DATA (decl) = f;
15269 /* Clear out the bits we don't need. */
15270 f->base.x_stmt_tree.x_cur_stmt_list = NULL;
15271 f->bindings = NULL;
15272 f->x_local_names = NULL;
15273 f->base.local_typedefs = NULL;
15277 /* Set the return value of the constructor (if present). */
15279 static void
15280 finish_constructor_body (void)
15282 tree val;
15283 tree exprstmt;
15285 if (targetm.cxx.cdtor_returns_this ())
15287 /* Any return from a constructor will end up here. */
15288 add_stmt (build_stmt (input_location, LABEL_EXPR, cdtor_label));
15290 val = DECL_ARGUMENTS (current_function_decl);
15291 val = build2 (MODIFY_EXPR, TREE_TYPE (val),
15292 DECL_RESULT (current_function_decl), val);
15293 /* Return the address of the object. */
15294 exprstmt = build_stmt (input_location, RETURN_EXPR, val);
15295 add_stmt (exprstmt);
15299 /* Do all the processing for the beginning of a destructor; set up the
15300 vtable pointers and cleanups for bases and members. */
15302 static void
15303 begin_destructor_body (void)
15305 tree compound_stmt;
15307 /* If the CURRENT_CLASS_TYPE is incomplete, we will have already
15308 issued an error message. We still want to try to process the
15309 body of the function, but initialize_vtbl_ptrs will crash if
15310 TYPE_BINFO is NULL. */
15311 if (COMPLETE_TYPE_P (current_class_type))
15313 compound_stmt = begin_compound_stmt (0);
15314 /* Make all virtual function table pointers in non-virtual base
15315 classes point to CURRENT_CLASS_TYPE's virtual function
15316 tables. */
15317 initialize_vtbl_ptrs (current_class_ptr);
15318 finish_compound_stmt (compound_stmt);
15320 if (flag_lifetime_dse
15321 /* Clobbering an empty base is harmful if it overlays real data. */
15322 && !is_empty_class (current_class_type))
15323 finish_decl_cleanup (NULL_TREE, build_clobber_this ());
15325 /* And insert cleanups for our bases and members so that they
15326 will be properly destroyed if we throw. */
15327 push_base_cleanups ();
15331 /* At the end of every destructor we generate code to delete the object if
15332 necessary. Do that now. */
15334 static void
15335 finish_destructor_body (void)
15337 tree exprstmt;
15339 /* Any return from a destructor will end up here; that way all base
15340 and member cleanups will be run when the function returns. */
15341 add_stmt (build_stmt (input_location, LABEL_EXPR, cdtor_label));
15343 if (targetm.cxx.cdtor_returns_this ())
15345 tree val;
15347 val = DECL_ARGUMENTS (current_function_decl);
15348 val = build2 (MODIFY_EXPR, TREE_TYPE (val),
15349 DECL_RESULT (current_function_decl), val);
15350 /* Return the address of the object. */
15351 exprstmt = build_stmt (input_location, RETURN_EXPR, val);
15352 add_stmt (exprstmt);
15356 /* Do the necessary processing for the beginning of a function body, which
15357 in this case includes member-initializers, but not the catch clauses of
15358 a function-try-block. Currently, this means opening a binding level
15359 for the member-initializers (in a ctor), member cleanups (in a dtor),
15360 and capture proxies (in a lambda operator()). */
15362 tree
15363 begin_function_body (void)
15365 tree stmt;
15367 if (! FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
15368 return NULL_TREE;
15370 if (processing_template_decl)
15371 /* Do nothing now. */;
15372 else
15373 /* Always keep the BLOCK node associated with the outermost pair of
15374 curly braces of a function. These are needed for correct
15375 operation of dwarfout.c. */
15376 keep_next_level (true);
15378 stmt = begin_compound_stmt (BCS_FN_BODY);
15380 if (processing_template_decl)
15381 /* Do nothing now. */;
15382 else if (DECL_DESTRUCTOR_P (current_function_decl))
15383 begin_destructor_body ();
15385 return stmt;
15388 /* Do the processing for the end of a function body. Currently, this means
15389 closing out the cleanups for fully-constructed bases and members, and in
15390 the case of the destructor, deleting the object if desired. Again, this
15391 is only meaningful for [cd]tors, since they are the only functions where
15392 there is a significant distinction between the main body and any
15393 function catch clauses. Handling, say, main() return semantics here
15394 would be wrong, as flowing off the end of a function catch clause for
15395 main() would also need to return 0. */
15397 void
15398 finish_function_body (tree compstmt)
15400 if (compstmt == NULL_TREE)
15401 return;
15403 /* Close the block. */
15404 finish_compound_stmt (compstmt);
15406 if (processing_template_decl)
15407 /* Do nothing now. */;
15408 else if (DECL_CONSTRUCTOR_P (current_function_decl))
15409 finish_constructor_body ();
15410 else if (DECL_DESTRUCTOR_P (current_function_decl))
15411 finish_destructor_body ();
15414 /* Given a function, returns the BLOCK corresponding to the outermost level
15415 of curly braces, skipping the artificial block created for constructor
15416 initializers. */
15418 tree
15419 outer_curly_brace_block (tree fndecl)
15421 tree block = DECL_INITIAL (fndecl);
15422 if (BLOCK_OUTER_CURLY_BRACE_P (block))
15423 return block;
15424 block = BLOCK_SUBBLOCKS (block);
15425 if (BLOCK_OUTER_CURLY_BRACE_P (block))
15426 return block;
15427 block = BLOCK_SUBBLOCKS (block);
15428 gcc_assert (BLOCK_OUTER_CURLY_BRACE_P (block));
15429 return block;
15432 /* If FNDECL is a class's key method, add the class to the list of
15433 keyed classes that should be emitted. */
15435 static void
15436 record_key_method_defined (tree fndecl)
15438 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fndecl)
15439 && DECL_VIRTUAL_P (fndecl)
15440 && !processing_template_decl)
15442 tree fnclass = DECL_CONTEXT (fndecl);
15443 if (fndecl == CLASSTYPE_KEY_METHOD (fnclass))
15444 keyed_classes = tree_cons (NULL_TREE, fnclass, keyed_classes);
15448 /* Subroutine of finish_function.
15449 Save the body of constexpr functions for possible
15450 future compile time evaluation. */
15452 static void
15453 maybe_save_function_definition (tree fun)
15455 if (!processing_template_decl
15456 && DECL_DECLARED_CONSTEXPR_P (fun)
15457 && !cp_function_chain->invalid_constexpr
15458 && !DECL_CLONED_FUNCTION_P (fun))
15459 register_constexpr_fundef (fun, DECL_SAVED_TREE (fun));
15462 /* Finish up a function declaration and compile that function
15463 all the way to assembler language output. The free the storage
15464 for the function definition.
15466 FLAGS is a bitwise or of the following values:
15467 2 - INCLASS_INLINE
15468 We just finished processing the body of an in-class inline
15469 function definition. (This processing will have taken place
15470 after the class definition is complete.) */
15472 tree
15473 finish_function (int flags)
15475 tree fndecl = current_function_decl;
15476 tree fntype, ctype = NULL_TREE;
15477 int inclass_inline = (flags & 2) != 0;
15479 /* When we get some parse errors, we can end up without a
15480 current_function_decl, so cope. */
15481 if (fndecl == NULL_TREE)
15482 return error_mark_node;
15484 if (c_dialect_objc ())
15485 objc_finish_function ();
15487 record_key_method_defined (fndecl);
15489 fntype = TREE_TYPE (fndecl);
15491 /* TREE_READONLY (fndecl) = 1;
15492 This caused &foo to be of type ptr-to-const-function
15493 which then got a warning when stored in a ptr-to-function variable. */
15495 gcc_assert (building_stmt_list_p ());
15496 /* The current function is being defined, so its DECL_INITIAL should
15497 be set, and unless there's a multiple definition, it should be
15498 error_mark_node. */
15499 gcc_assert (DECL_INITIAL (fndecl) == error_mark_node);
15501 /* For a cloned function, we've already got all the code we need;
15502 there's no need to add any extra bits. */
15503 if (!DECL_CLONED_FUNCTION_P (fndecl))
15505 /* Make it so that `main' always returns 0 by default. */
15506 if (DECL_MAIN_P (current_function_decl))
15507 finish_return_stmt (integer_zero_node);
15509 if (use_eh_spec_block (current_function_decl))
15510 finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
15511 (TREE_TYPE (current_function_decl)),
15512 current_eh_spec_block);
15515 /* If we're saving up tree structure, tie off the function now. */
15516 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
15518 if (fn_contains_cilk_spawn_p (cfun) && !processing_template_decl)
15519 cfun->cilk_frame_decl = insert_cilk_frame (fndecl);
15521 finish_fname_decls ();
15523 /* If this function can't throw any exceptions, remember that. */
15524 if (!processing_template_decl
15525 && !cp_function_chain->can_throw
15526 && !flag_non_call_exceptions
15527 && !decl_replaceable_p (fndecl))
15528 TREE_NOTHROW (fndecl) = 1;
15530 /* This must come after expand_function_end because cleanups might
15531 have declarations (from inline functions) that need to go into
15532 this function's blocks. */
15534 /* If the current binding level isn't the outermost binding level
15535 for this function, either there is a bug, or we have experienced
15536 syntax errors and the statement tree is malformed. */
15537 if (current_binding_level->kind != sk_function_parms)
15539 /* Make sure we have already experienced errors. */
15540 gcc_assert (errorcount);
15542 /* Throw away the broken statement tree and extra binding
15543 levels. */
15544 DECL_SAVED_TREE (fndecl) = alloc_stmt_list ();
15546 while (current_binding_level->kind != sk_function_parms)
15548 if (current_binding_level->kind == sk_class)
15549 pop_nested_class ();
15550 else
15551 poplevel (0, 0, 0);
15554 poplevel (1, 0, 1);
15556 /* Statements should always be full-expressions at the outermost set
15557 of curly braces for a function. */
15558 gcc_assert (stmts_are_full_exprs_p ());
15560 /* If there are no return statements in a function with auto return type,
15561 the return type is void. But if the declared type is something like
15562 auto*, this is an error. */
15563 if (!processing_template_decl && FNDECL_USED_AUTO (fndecl)
15564 && TREE_TYPE (fntype) == current_function_auto_return_pattern)
15566 if (!is_auto (current_function_auto_return_pattern)
15567 && !current_function_returns_value && !current_function_returns_null)
15569 error ("no return statements in function returning %qT",
15570 current_function_auto_return_pattern);
15571 inform (input_location, "only plain %<auto%> return type can be "
15572 "deduced to %<void%>");
15574 apply_deduced_return_type (fndecl, void_type_node);
15575 fntype = TREE_TYPE (fndecl);
15578 // If this is a concept, check that the definition is reasonable.
15579 if (DECL_DECLARED_CONCEPT_P (fndecl))
15580 check_function_concept (fndecl);
15582 /* Lambda closure members are implicitly constexpr if possible. */
15583 if (cxx_dialect >= cxx1z
15584 && LAMBDA_TYPE_P (CP_DECL_CONTEXT (fndecl))
15585 && (processing_template_decl
15586 || is_valid_constexpr_fn (fndecl, /*complain*/false))
15587 && potential_constant_expression (DECL_SAVED_TREE (fndecl)))
15588 DECL_DECLARED_CONSTEXPR_P (fndecl) = true;
15590 /* Save constexpr function body before it gets munged by
15591 the NRV transformation. */
15592 maybe_save_function_definition (fndecl);
15594 /* Invoke the pre-genericize plugin before we start munging things. */
15595 if (!processing_template_decl)
15596 invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE, fndecl);
15598 /* Perform delayed folding before NRV transformation. */
15599 if (!processing_template_decl)
15600 cp_fold_function (fndecl);
15602 /* Set up the named return value optimization, if we can. Candidate
15603 variables are selected in check_return_expr. */
15604 if (current_function_return_value)
15606 tree r = current_function_return_value;
15607 tree outer;
15609 if (r != error_mark_node
15610 /* This is only worth doing for fns that return in memory--and
15611 simpler, since we don't have to worry about promoted modes. */
15612 && aggregate_value_p (TREE_TYPE (TREE_TYPE (fndecl)), fndecl)
15613 /* Only allow this for variables declared in the outer scope of
15614 the function so we know that their lifetime always ends with a
15615 return; see g++.dg/opt/nrv6.C. We could be more flexible if
15616 we were to do this optimization in tree-ssa. */
15617 && (outer = outer_curly_brace_block (fndecl))
15618 && chain_member (r, BLOCK_VARS (outer)))
15619 finalize_nrv (&DECL_SAVED_TREE (fndecl), r, DECL_RESULT (fndecl));
15621 current_function_return_value = NULL_TREE;
15624 /* Remember that we were in class scope. */
15625 if (current_class_name)
15626 ctype = current_class_type;
15628 /* Must mark the RESULT_DECL as being in this function. */
15629 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
15631 /* Set the BLOCK_SUPERCONTEXT of the outermost function scope to point
15632 to the FUNCTION_DECL node itself. */
15633 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
15635 /* Save away current state, if appropriate. */
15636 if (!processing_template_decl)
15637 save_function_data (fndecl);
15639 /* Complain if there's just no return statement. */
15640 if (warn_return_type
15641 && !VOID_TYPE_P (TREE_TYPE (fntype))
15642 && !dependent_type_p (TREE_TYPE (fntype))
15643 && !current_function_returns_value && !current_function_returns_null
15644 /* Don't complain if we abort or throw. */
15645 && !current_function_returns_abnormally
15646 /* Don't complain if there's an infinite loop. */
15647 && !current_function_infinite_loop
15648 /* Don't complain if we are declared noreturn. */
15649 && !TREE_THIS_VOLATILE (fndecl)
15650 && !DECL_NAME (DECL_RESULT (fndecl))
15651 && !TREE_NO_WARNING (fndecl)
15652 /* Structor return values (if any) are set by the compiler. */
15653 && !DECL_CONSTRUCTOR_P (fndecl)
15654 && !DECL_DESTRUCTOR_P (fndecl)
15655 && targetm.warn_func_return (fndecl))
15657 warning (OPT_Wreturn_type,
15658 "no return statement in function returning non-void");
15659 TREE_NO_WARNING (fndecl) = 1;
15662 /* Store the end of the function, so that we get good line number
15663 info for the epilogue. */
15664 cfun->function_end_locus = input_location;
15666 /* Complain about parameters that are only set, but never otherwise used. */
15667 if (warn_unused_but_set_parameter
15668 && !processing_template_decl
15669 && errorcount == unused_but_set_errorcount
15670 && !DECL_CLONED_FUNCTION_P (fndecl))
15672 tree decl;
15674 for (decl = DECL_ARGUMENTS (fndecl);
15675 decl;
15676 decl = DECL_CHAIN (decl))
15677 if (TREE_USED (decl)
15678 && TREE_CODE (decl) == PARM_DECL
15679 && !DECL_READ_P (decl)
15680 && DECL_NAME (decl)
15681 && !DECL_ARTIFICIAL (decl)
15682 && !TREE_NO_WARNING (decl)
15683 && !DECL_IN_SYSTEM_HEADER (decl)
15684 && TREE_TYPE (decl) != error_mark_node
15685 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
15686 && (!CLASS_TYPE_P (TREE_TYPE (decl))
15687 || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl))))
15688 warning_at (DECL_SOURCE_LOCATION (decl),
15689 OPT_Wunused_but_set_parameter,
15690 "parameter %qD set but not used", decl);
15691 unused_but_set_errorcount = errorcount;
15694 /* Complain about locally defined typedefs that are not used in this
15695 function. */
15696 maybe_warn_unused_local_typedefs ();
15698 /* Possibly warn about unused parameters. */
15699 if (warn_unused_parameter
15700 && !processing_template_decl
15701 && !DECL_CLONED_FUNCTION_P (fndecl))
15702 do_warn_unused_parameter (fndecl);
15704 /* Genericize before inlining. */
15705 if (!processing_template_decl)
15707 struct language_function *f = DECL_SAVED_FUNCTION_DATA (fndecl);
15708 cp_genericize (fndecl);
15709 /* Clear out the bits we don't need. */
15710 f->x_current_class_ptr = NULL;
15711 f->x_current_class_ref = NULL;
15712 f->x_eh_spec_block = NULL;
15713 f->x_in_charge_parm = NULL;
15714 f->x_vtt_parm = NULL;
15715 f->x_return_value = NULL;
15716 f->bindings = NULL;
15717 f->extern_decl_map = NULL;
15718 f->infinite_loops = NULL;
15720 /* Clear out the bits we don't need. */
15721 local_names = NULL;
15723 /* We're leaving the context of this function, so zap cfun. It's still in
15724 DECL_STRUCT_FUNCTION, and we'll restore it in tree_rest_of_compilation. */
15725 set_cfun (NULL);
15726 current_function_decl = NULL;
15728 /* If this is an in-class inline definition, we may have to pop the
15729 bindings for the template parameters that we added in
15730 maybe_begin_member_template_processing when start_function was
15731 called. */
15732 if (inclass_inline)
15733 maybe_end_member_template_processing ();
15735 /* Leave the scope of the class. */
15736 if (ctype)
15737 pop_nested_class ();
15739 --function_depth;
15741 /* Clean up. */
15742 current_function_decl = NULL_TREE;
15744 invoke_plugin_callbacks (PLUGIN_FINISH_PARSE_FUNCTION, fndecl);
15745 return fndecl;
15748 /* Create the FUNCTION_DECL for a function definition.
15749 DECLSPECS and DECLARATOR are the parts of the declaration;
15750 they describe the return type and the name of the function,
15751 but twisted together in a fashion that parallels the syntax of C.
15753 This function creates a binding context for the function body
15754 as well as setting up the FUNCTION_DECL in current_function_decl.
15756 Returns a FUNCTION_DECL on success.
15758 If the DECLARATOR is not suitable for a function (it defines a datum
15759 instead), we return 0, which tells yyparse to report a parse error.
15761 May return void_type_node indicating that this method is actually
15762 a friend. See grokfield for more details.
15764 Came here with a `.pushlevel' .
15766 DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
15767 CHANGES TO CODE IN `grokfield'. */
15769 tree
15770 grokmethod (cp_decl_specifier_seq *declspecs,
15771 const cp_declarator *declarator, tree attrlist)
15773 tree fndecl = grokdeclarator (declarator, declspecs, MEMFUNCDEF, 0,
15774 &attrlist);
15776 if (fndecl == error_mark_node)
15777 return error_mark_node;
15779 if (fndecl == NULL || TREE_CODE (fndecl) != FUNCTION_DECL)
15781 error ("invalid member function declaration");
15782 return error_mark_node;
15785 if (attrlist)
15786 cplus_decl_attributes (&fndecl, attrlist, 0);
15788 /* Pass friends other than inline friend functions back. */
15789 if (fndecl == void_type_node)
15790 return fndecl;
15792 if (DECL_IN_AGGR_P (fndecl))
15794 if (DECL_CLASS_SCOPE_P (fndecl))
15795 error ("%qD is already defined in class %qT", fndecl,
15796 DECL_CONTEXT (fndecl));
15797 return error_mark_node;
15800 check_template_shadow (fndecl);
15802 if (TREE_PUBLIC (fndecl))
15803 DECL_COMDAT (fndecl) = 1;
15804 DECL_DECLARED_INLINE_P (fndecl) = 1;
15805 DECL_NO_INLINE_WARNING_P (fndecl) = 1;
15807 /* We process method specializations in finish_struct_1. */
15808 if (processing_template_decl && !DECL_TEMPLATE_SPECIALIZATION (fndecl))
15810 fndecl = push_template_decl (fndecl);
15811 if (fndecl == error_mark_node)
15812 return fndecl;
15815 if (! DECL_FRIEND_P (fndecl))
15817 if (DECL_CHAIN (fndecl))
15819 fndecl = copy_node (fndecl);
15820 TREE_CHAIN (fndecl) = NULL_TREE;
15824 cp_finish_decl (fndecl, NULL_TREE, false, NULL_TREE, 0);
15826 DECL_IN_AGGR_P (fndecl) = 1;
15827 return fndecl;
15831 /* VAR is a VAR_DECL. If its type is incomplete, remember VAR so that
15832 we can lay it out later, when and if its type becomes complete.
15834 Also handle constexpr variables where the initializer involves
15835 an unlowered PTRMEM_CST because the class isn't complete yet. */
15837 void
15838 maybe_register_incomplete_var (tree var)
15840 gcc_assert (VAR_P (var));
15842 /* Keep track of variables with incomplete types. */
15843 if (!processing_template_decl && TREE_TYPE (var) != error_mark_node
15844 && DECL_EXTERNAL (var))
15846 tree inner_type = TREE_TYPE (var);
15848 while (TREE_CODE (inner_type) == ARRAY_TYPE)
15849 inner_type = TREE_TYPE (inner_type);
15850 inner_type = TYPE_MAIN_VARIANT (inner_type);
15852 if ((!COMPLETE_TYPE_P (inner_type) && CLASS_TYPE_P (inner_type))
15853 /* RTTI TD entries are created while defining the type_info. */
15854 || (TYPE_LANG_SPECIFIC (inner_type)
15855 && TYPE_BEING_DEFINED (inner_type)))
15857 incomplete_var iv = {var, inner_type};
15858 vec_safe_push (incomplete_vars, iv);
15860 else if (!(DECL_LANG_SPECIFIC (var) && DECL_TEMPLATE_INFO (var))
15861 && decl_constant_var_p (var)
15862 && (TYPE_PTRMEM_P (inner_type) || CLASS_TYPE_P (inner_type)))
15864 /* When the outermost open class is complete we can resolve any
15865 pointers-to-members. */
15866 tree context = outermost_open_class ();
15867 incomplete_var iv = {var, context};
15868 vec_safe_push (incomplete_vars, iv);
15873 /* Called when a class type (given by TYPE) is defined. If there are
15874 any existing VAR_DECLs whose type has been completed by this
15875 declaration, update them now. */
15877 void
15878 complete_vars (tree type)
15880 unsigned ix;
15881 incomplete_var *iv;
15883 for (ix = 0; vec_safe_iterate (incomplete_vars, ix, &iv); )
15885 if (same_type_p (type, iv->incomplete_type))
15887 tree var = iv->decl;
15888 tree type = TREE_TYPE (var);
15890 if (type != error_mark_node
15891 && (TYPE_MAIN_VARIANT (strip_array_types (type))
15892 == iv->incomplete_type))
15894 /* Complete the type of the variable. The VAR_DECL itself
15895 will be laid out in expand_expr. */
15896 complete_type (type);
15897 cp_apply_type_quals_to_decl (cp_type_quals (type), var);
15900 /* Remove this entry from the list. */
15901 incomplete_vars->unordered_remove (ix);
15903 else
15904 ix++;
15907 /* Check for pending declarations which may have abstract type. */
15908 complete_type_check_abstract (type);
15911 /* If DECL is of a type which needs a cleanup, build and return an
15912 expression to perform that cleanup here. Return NULL_TREE if no
15913 cleanup need be done. DECL can also be a _REF when called from
15914 split_nonconstant_init_1. */
15916 tree
15917 cxx_maybe_build_cleanup (tree decl, tsubst_flags_t complain)
15919 tree type;
15920 tree attr;
15921 tree cleanup;
15923 /* Assume no cleanup is required. */
15924 cleanup = NULL_TREE;
15926 if (error_operand_p (decl))
15927 return cleanup;
15929 /* Handle "__attribute__((cleanup))". We run the cleanup function
15930 before the destructor since the destructor is what actually
15931 terminates the lifetime of the object. */
15932 if (DECL_P (decl))
15933 attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
15934 else
15935 attr = NULL_TREE;
15936 if (attr)
15938 tree id;
15939 tree fn;
15940 tree arg;
15942 /* Get the name specified by the user for the cleanup function. */
15943 id = TREE_VALUE (TREE_VALUE (attr));
15944 /* Look up the name to find the cleanup function to call. It is
15945 important to use lookup_name here because that is what is
15946 used in c-common.c:handle_cleanup_attribute when performing
15947 initial checks on the attribute. Note that those checks
15948 include ensuring that the function found is not an overloaded
15949 function, or an object with an overloaded call operator,
15950 etc.; we can rely on the fact that the function found is an
15951 ordinary FUNCTION_DECL. */
15952 fn = lookup_name (id);
15953 arg = build_address (decl);
15954 if (!mark_used (decl, complain) && !(complain & tf_error))
15955 return error_mark_node;
15956 cleanup = cp_build_function_call_nary (fn, complain, arg, NULL_TREE);
15957 if (cleanup == error_mark_node)
15958 return error_mark_node;
15960 /* Handle ordinary C++ destructors. */
15961 type = TREE_TYPE (decl);
15962 if (type_build_dtor_call (type))
15964 int flags = LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR;
15965 tree addr;
15966 tree call;
15968 if (TREE_CODE (type) == ARRAY_TYPE)
15969 addr = decl;
15970 else
15971 addr = build_address (decl);
15973 call = build_delete (TREE_TYPE (addr), addr,
15974 sfk_complete_destructor, flags, 0, complain);
15975 if (call == error_mark_node)
15976 cleanup = error_mark_node;
15977 else if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
15978 /* Discard the call. */;
15979 else if (cleanup)
15980 cleanup = cp_build_compound_expr (cleanup, call, complain);
15981 else
15982 cleanup = call;
15985 /* build_delete sets the location of the destructor call to the
15986 current location, even though the destructor is going to be
15987 called later, at the end of the current scope. This can lead to
15988 a "jumpy" behavior for users of debuggers when they step around
15989 the end of the block. So let's unset the location of the
15990 destructor call instead. */
15991 protected_set_expr_location (cleanup, UNKNOWN_LOCATION);
15993 if (cleanup
15994 && DECL_P (decl)
15995 && !lookup_attribute ("warn_unused", TYPE_ATTRIBUTES (TREE_TYPE (decl)))
15996 /* Treat objects with destructors as used; the destructor may do
15997 something substantive. */
15998 && !mark_used (decl, complain) && !(complain & tf_error))
15999 return error_mark_node;
16001 return cleanup;
16005 /* Return the FUNCTION_TYPE that corresponds to MEMFNTYPE, which can be a
16006 FUNCTION_DECL, METHOD_TYPE, FUNCTION_TYPE, pointer or reference to
16007 METHOD_TYPE or FUNCTION_TYPE, or pointer to member function. */
16009 tree
16010 static_fn_type (tree memfntype)
16012 tree fntype;
16013 tree args;
16015 if (TYPE_PTRMEMFUNC_P (memfntype))
16016 memfntype = TYPE_PTRMEMFUNC_FN_TYPE (memfntype);
16017 if (POINTER_TYPE_P (memfntype)
16018 || TREE_CODE (memfntype) == FUNCTION_DECL)
16019 memfntype = TREE_TYPE (memfntype);
16020 if (TREE_CODE (memfntype) == FUNCTION_TYPE)
16021 return memfntype;
16022 gcc_assert (TREE_CODE (memfntype) == METHOD_TYPE);
16023 args = TYPE_ARG_TYPES (memfntype);
16024 cp_ref_qualifier rqual = type_memfn_rqual (memfntype);
16025 fntype = build_function_type (TREE_TYPE (memfntype), TREE_CHAIN (args));
16026 fntype = apply_memfn_quals (fntype, type_memfn_quals (memfntype), rqual);
16027 fntype = (cp_build_type_attribute_variant
16028 (fntype, TYPE_ATTRIBUTES (memfntype)));
16029 fntype = (build_exception_variant
16030 (fntype, TYPE_RAISES_EXCEPTIONS (memfntype)));
16031 if (TYPE_HAS_LATE_RETURN_TYPE (memfntype))
16032 TYPE_HAS_LATE_RETURN_TYPE (fntype) = 1;
16033 return fntype;
16036 /* DECL was originally constructed as a non-static member function,
16037 but turned out to be static. Update it accordingly. */
16039 void
16040 revert_static_member_fn (tree decl)
16042 tree stype = static_fn_type (decl);
16043 cp_cv_quals quals = type_memfn_quals (stype);
16044 cp_ref_qualifier rqual = type_memfn_rqual (stype);
16046 if (quals != TYPE_UNQUALIFIED || rqual != REF_QUAL_NONE)
16047 stype = apply_memfn_quals (stype, TYPE_UNQUALIFIED, REF_QUAL_NONE);
16049 TREE_TYPE (decl) = stype;
16051 if (DECL_ARGUMENTS (decl))
16052 DECL_ARGUMENTS (decl) = DECL_CHAIN (DECL_ARGUMENTS (decl));
16053 DECL_STATIC_FUNCTION_P (decl) = 1;
16056 /* Return which tree structure is used by T, or TS_CP_GENERIC if T is
16057 one of the language-independent trees. */
16059 enum cp_tree_node_structure_enum
16060 cp_tree_node_structure (union lang_tree_node * t)
16062 switch (TREE_CODE (&t->generic))
16064 case DEFAULT_ARG: return TS_CP_DEFAULT_ARG;
16065 case DEFERRED_NOEXCEPT: return TS_CP_DEFERRED_NOEXCEPT;
16066 case IDENTIFIER_NODE: return TS_CP_IDENTIFIER;
16067 case OVERLOAD: return TS_CP_OVERLOAD;
16068 case TEMPLATE_PARM_INDEX: return TS_CP_TPI;
16069 case PTRMEM_CST: return TS_CP_PTRMEM;
16070 case BASELINK: return TS_CP_BASELINK;
16071 case TEMPLATE_DECL: return TS_CP_TEMPLATE_DECL;
16072 case STATIC_ASSERT: return TS_CP_STATIC_ASSERT;
16073 case ARGUMENT_PACK_SELECT: return TS_CP_ARGUMENT_PACK_SELECT;
16074 case TRAIT_EXPR: return TS_CP_TRAIT_EXPR;
16075 case LAMBDA_EXPR: return TS_CP_LAMBDA_EXPR;
16076 case TEMPLATE_INFO: return TS_CP_TEMPLATE_INFO;
16077 case CONSTRAINT_INFO: return TS_CP_CONSTRAINT_INFO;
16078 case USERDEF_LITERAL: return TS_CP_USERDEF_LITERAL;
16079 default: return TS_CP_GENERIC;
16083 /* Build the void_list_node (void_type_node having been created). */
16084 tree
16085 build_void_list_node (void)
16087 tree t = build_tree_list (NULL_TREE, void_type_node);
16088 return t;
16091 bool
16092 cp_missing_noreturn_ok_p (tree decl)
16094 /* A missing noreturn is ok for the `main' function. */
16095 return DECL_MAIN_P (decl);
16098 /* Return the decl used to identify the COMDAT group into which DECL should
16099 be placed. */
16101 tree
16102 cxx_comdat_group (tree decl)
16104 /* Virtual tables, construction virtual tables, and virtual table
16105 tables all go in a single COMDAT group, named after the primary
16106 virtual table. */
16107 if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl))
16108 decl = CLASSTYPE_VTABLES (DECL_CONTEXT (decl));
16109 /* For all other DECLs, the COMDAT group is the mangled name of the
16110 declaration itself. */
16111 else
16113 while (DECL_THUNK_P (decl))
16115 /* If TARGET_USE_LOCAL_THUNK_ALIAS_P, use_thunk puts the thunk
16116 into the same section as the target function. In that case
16117 we must return target's name. */
16118 tree target = THUNK_TARGET (decl);
16119 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (target)
16120 && DECL_SECTION_NAME (target) != NULL
16121 && DECL_ONE_ONLY (target))
16122 decl = target;
16123 else
16124 break;
16128 return decl;
16131 /* Returns the return type for FN as written by the user, which may include
16132 a placeholder for a deduced return type. */
16134 tree
16135 fndecl_declared_return_type (tree fn)
16137 fn = STRIP_TEMPLATE (fn);
16138 if (FNDECL_USED_AUTO (fn))
16140 struct language_function *f = NULL;
16141 if (DECL_STRUCT_FUNCTION (fn))
16142 f = DECL_STRUCT_FUNCTION (fn)->language;
16143 if (f == NULL)
16144 f = DECL_SAVED_FUNCTION_DATA (fn);
16145 return f->x_auto_return_pattern;
16147 return TREE_TYPE (TREE_TYPE (fn));
16150 /* Returns true iff DECL was declared with an auto type and it has
16151 not yet been deduced to a real type. */
16153 bool
16154 undeduced_auto_decl (tree decl)
16156 if (cxx_dialect < cxx11)
16157 return false;
16158 return type_uses_auto (TREE_TYPE (decl));
16161 /* Complain if DECL has an undeduced return type. */
16163 bool
16164 require_deduced_type (tree decl, tsubst_flags_t complain)
16166 if (undeduced_auto_decl (decl))
16168 if (complain & tf_error)
16169 error ("use of %qD before deduction of %<auto%>", decl);
16170 return false;
16172 return true;
16175 #include "gt-cp-decl.h"