[gcc]
[official-gcc.git] / gcc / cp / decl.c
blob93e24fc8984473b5ff6bd9988d729104658c613f
1 /* Process declarations and variables for C++ compiler.
2 Copyright (C) 1988-2017 Free Software Foundation, Inc.
3 Contributed by Michael Tiemann (tiemann@cygnus.com)
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 /* Process declarations and symbol lookup for C++ front end.
23 Also constructs types; the standard scalar types at initialization,
24 and structure, union, array and enum types when they are declared. */
26 /* ??? not all decl nodes are given the most useful possible
27 line numbers. For example, the CONST_DECLs for enum values. */
29 #include "config.h"
30 #include "system.h"
31 #include "coretypes.h"
32 #include "target.h"
33 #include "c-family/c-target.h"
34 #include "cp-tree.h"
35 #include "timevar.h"
36 #include "stringpool.h"
37 #include "cgraph.h"
38 #include "stor-layout.h"
39 #include "varasm.h"
40 #include "attribs.h"
41 #include "flags.h"
42 #include "tree-iterator.h"
43 #include "decl.h"
44 #include "intl.h"
45 #include "toplev.h"
46 #include "c-family/c-objc.h"
47 #include "c-family/c-pragma.h"
48 #include "c-family/c-ubsan.h"
49 #include "debug.h"
50 #include "plugin.h"
51 #include "cilk.h"
52 #include "builtins.h"
53 #include "gimplify.h"
54 #include "asan.h"
56 /* Possible cases of bad specifiers type used by bad_specifiers. */
57 enum bad_spec_place {
58 BSP_VAR, /* variable */
59 BSP_PARM, /* parameter */
60 BSP_TYPE, /* type */
61 BSP_FIELD /* field */
64 static const char *redeclaration_error_message (tree, tree);
66 static int decl_jump_unsafe (tree);
67 static void require_complete_types_for_parms (tree);
68 static bool ambi_op_p (enum tree_code);
69 static bool unary_op_p (enum tree_code);
70 static void push_local_name (tree);
71 static tree grok_reference_init (tree, tree, tree, int);
72 static tree grokvardecl (tree, tree, tree, const cp_decl_specifier_seq *,
73 int, int, int, bool, int, tree);
74 static int check_static_variable_definition (tree, tree);
75 static void record_unknown_type (tree, const char *);
76 static tree builtin_function_1 (tree, tree, bool);
77 static int member_function_or_else (tree, tree, enum overload_flags);
78 static void check_for_uninitialized_const_var (tree);
79 static tree local_variable_p_walkfn (tree *, int *, void *);
80 static const char *tag_name (enum tag_types);
81 static tree lookup_and_check_tag (enum tag_types, tree, tag_scope, bool);
82 static void maybe_deduce_size_from_array_init (tree, tree);
83 static void layout_var_decl (tree);
84 static tree check_initializer (tree, tree, int, vec<tree, va_gc> **);
85 static void make_rtl_for_nonlocal_decl (tree, tree, const char *);
86 static void save_function_data (tree);
87 static void copy_type_enum (tree , tree);
88 static void check_function_type (tree, tree);
89 static void finish_constructor_body (void);
90 static void begin_destructor_body (void);
91 static void finish_destructor_body (void);
92 static void record_key_method_defined (tree);
93 static tree create_array_type_for_decl (tree, tree, tree);
94 static tree get_atexit_node (void);
95 static tree get_dso_handle_node (void);
96 static tree start_cleanup_fn (void);
97 static void end_cleanup_fn (void);
98 static tree cp_make_fname_decl (location_t, tree, int);
99 static void initialize_predefined_identifiers (void);
100 static tree check_special_function_return_type
101 (special_function_kind, tree, tree, int, const location_t*);
102 static tree push_cp_library_fn (enum tree_code, tree, int);
103 static tree build_cp_library_fn (tree, enum tree_code, tree, int);
104 static void store_parm_decls (tree);
105 static void initialize_local_var (tree, tree);
106 static void expand_static_init (tree, tree);
108 /* The following symbols are subsumed in the cp_global_trees array, and
109 listed here individually for documentation purposes.
111 C++ extensions
112 tree wchar_decl_node;
114 tree vtable_entry_type;
115 tree delta_type_node;
116 tree __t_desc_type_node;
118 tree class_type_node;
119 tree unknown_type_node;
121 Array type `vtable_entry_type[]'
123 tree vtbl_type_node;
124 tree vtbl_ptr_type_node;
126 Namespaces,
128 tree std_node;
129 tree abi_node;
131 A FUNCTION_DECL which can call `abort'. Not necessarily the
132 one that the user will declare, but sufficient to be called
133 by routines that want to abort the program.
135 tree abort_fndecl;
137 Used by RTTI
138 tree type_info_type_node, tinfo_decl_id, tinfo_decl_type;
139 tree tinfo_var_id; */
141 tree cp_global_trees[CPTI_MAX];
143 #define local_names cp_function_chain->x_local_names
145 /* A list of objects which have constructors or destructors
146 which reside in the global scope. The decl is stored in
147 the TREE_VALUE slot and the initializer is stored
148 in the TREE_PURPOSE slot. */
149 tree static_aggregates;
151 /* Like static_aggregates, but for thread_local variables. */
152 tree tls_aggregates;
154 /* -- end of C++ */
156 /* A node for the integer constant 2. */
158 tree integer_two_node;
160 /* vector of static decls. */
161 vec<tree, va_gc> *static_decls;
163 /* vector of keyed classes. */
164 vec<tree, va_gc> *keyed_classes;
166 /* Used only for jumps to as-yet undefined labels, since jumps to
167 defined labels can have their validity checked immediately. */
169 struct GTY((chain_next ("%h.next"))) named_label_use_entry {
170 struct named_label_use_entry *next;
171 /* The binding level to which this entry is *currently* attached.
172 This is initially the binding level in which the goto appeared,
173 but is modified as scopes are closed. */
174 cp_binding_level *binding_level;
175 /* The head of the names list that was current when the goto appeared,
176 or the inner scope popped. These are the decls that will *not* be
177 skipped when jumping to the label. */
178 tree names_in_scope;
179 /* The location of the goto, for error reporting. */
180 location_t o_goto_locus;
181 /* True if an OpenMP structured block scope has been closed since
182 the goto appeared. This means that the branch from the label will
183 illegally exit an OpenMP scope. */
184 bool in_omp_scope;
187 /* A list of all LABEL_DECLs in the function that have names. Here so
188 we can clear out their names' definitions at the end of the
189 function, and so we can check the validity of jumps to these labels. */
191 struct GTY((for_user)) named_label_entry {
192 /* The decl itself. */
193 tree label_decl;
195 /* The binding level to which the label is *currently* attached.
196 This is initially set to the binding level in which the label
197 is defined, but is modified as scopes are closed. */
198 cp_binding_level *binding_level;
199 /* The head of the names list that was current when the label was
200 defined, or the inner scope popped. These are the decls that will
201 be skipped when jumping to the label. */
202 tree names_in_scope;
203 /* A vector of all decls from all binding levels that would be
204 crossed by a backward branch to the label. */
205 vec<tree, va_gc> *bad_decls;
207 /* A list of uses of the label, before the label is defined. */
208 struct named_label_use_entry *uses;
210 /* The following bits are set after the label is defined, and are
211 updated as scopes are popped. They indicate that a backward jump
212 to the label will illegally enter a scope of the given flavor. */
213 bool in_try_scope;
214 bool in_catch_scope;
215 bool in_omp_scope;
216 bool in_transaction_scope;
217 bool in_constexpr_if;
220 #define named_labels cp_function_chain->x_named_labels
222 /* The number of function bodies which we are currently processing.
223 (Zero if we are at namespace scope, one inside the body of a
224 function, two inside the body of a function in a local class, etc.) */
225 int function_depth;
227 /* Whether the exception-specifier is part of a function type (i.e. C++17). */
228 bool flag_noexcept_type;
230 /* States indicating how grokdeclarator() should handle declspecs marked
231 with __attribute__((deprecated)). An object declared as
232 __attribute__((deprecated)) suppresses warnings of uses of other
233 deprecated items. */
234 enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
237 /* A list of VAR_DECLs whose type was incomplete at the time the
238 variable was declared. */
240 struct GTY(()) incomplete_var {
241 tree decl;
242 tree incomplete_type;
246 static GTY(()) vec<incomplete_var, va_gc> *incomplete_vars;
248 /* Returns the kind of template specialization we are currently
249 processing, given that it's declaration contained N_CLASS_SCOPES
250 explicit scope qualifications. */
252 tmpl_spec_kind
253 current_tmpl_spec_kind (int n_class_scopes)
255 int n_template_parm_scopes = 0;
256 int seen_specialization_p = 0;
257 int innermost_specialization_p = 0;
258 cp_binding_level *b;
260 /* Scan through the template parameter scopes. */
261 for (b = current_binding_level;
262 b->kind == sk_template_parms;
263 b = b->level_chain)
265 /* If we see a specialization scope inside a parameter scope,
266 then something is wrong. That corresponds to a declaration
267 like:
269 template <class T> template <> ...
271 which is always invalid since [temp.expl.spec] forbids the
272 specialization of a class member template if the enclosing
273 class templates are not explicitly specialized as well. */
274 if (b->explicit_spec_p)
276 if (n_template_parm_scopes == 0)
277 innermost_specialization_p = 1;
278 else
279 seen_specialization_p = 1;
281 else if (seen_specialization_p == 1)
282 return tsk_invalid_member_spec;
284 ++n_template_parm_scopes;
287 /* Handle explicit instantiations. */
288 if (processing_explicit_instantiation)
290 if (n_template_parm_scopes != 0)
291 /* We've seen a template parameter list during an explicit
292 instantiation. For example:
294 template <class T> template void f(int);
296 This is erroneous. */
297 return tsk_invalid_expl_inst;
298 else
299 return tsk_expl_inst;
302 if (n_template_parm_scopes < n_class_scopes)
303 /* We've not seen enough template headers to match all the
304 specialized classes present. For example:
306 template <class T> void R<T>::S<T>::f(int);
308 This is invalid; there needs to be one set of template
309 parameters for each class. */
310 return tsk_insufficient_parms;
311 else if (n_template_parm_scopes == n_class_scopes)
312 /* We're processing a non-template declaration (even though it may
313 be a member of a template class.) For example:
315 template <class T> void S<T>::f(int);
317 The `class T' matches the `S<T>', leaving no template headers
318 corresponding to the `f'. */
319 return tsk_none;
320 else if (n_template_parm_scopes > n_class_scopes + 1)
321 /* We've got too many template headers. For example:
323 template <> template <class T> void f (T);
325 There need to be more enclosing classes. */
326 return tsk_excessive_parms;
327 else
328 /* This must be a template. It's of the form:
330 template <class T> template <class U> void S<T>::f(U);
332 This is a specialization if the innermost level was a
333 specialization; otherwise it's just a definition of the
334 template. */
335 return innermost_specialization_p ? tsk_expl_spec : tsk_template;
338 /* Exit the current scope. */
340 void
341 finish_scope (void)
343 poplevel (0, 0, 0);
346 /* When a label goes out of scope, check to see if that label was used
347 in a valid manner, and issue any appropriate warnings or errors. */
349 static void
350 pop_label (tree label, tree old_value)
352 if (!processing_template_decl)
354 if (DECL_INITIAL (label) == NULL_TREE)
356 location_t location;
358 error ("label %q+D used but not defined", label);
359 location = input_location;
360 /* FIXME want (LOCATION_FILE (input_location), (line)0) */
361 /* Avoid crashing later. */
362 define_label (location, DECL_NAME (label));
364 else
365 warn_for_unused_label (label);
368 SET_IDENTIFIER_LABEL_VALUE (DECL_NAME (label), old_value);
371 /* Push all named labels into a vector, so that we can sort it on DECL_UID
372 to avoid code generation differences. */
375 note_label (named_label_entry **slot, vec<named_label_entry **> &labels)
377 labels.quick_push (slot);
378 return 1;
381 /* Helper function to sort named label entries in a vector by DECL_UID. */
383 static int
384 sort_labels (const void *a, const void *b)
386 named_label_entry **slot1 = *(named_label_entry **const *) a;
387 named_label_entry **slot2 = *(named_label_entry **const *) b;
388 if (DECL_UID ((*slot1)->label_decl) < DECL_UID ((*slot2)->label_decl))
389 return -1;
390 if (DECL_UID ((*slot1)->label_decl) > DECL_UID ((*slot2)->label_decl))
391 return 1;
392 return 0;
395 /* At the end of a function, all labels declared within the function
396 go out of scope. BLOCK is the top-level block for the
397 function. */
399 static void
400 pop_labels (tree block)
402 if (named_labels)
404 auto_vec<named_label_entry **, 32> labels;
405 named_label_entry **slot;
406 unsigned int i;
408 /* Push all the labels into a vector and sort them by DECL_UID,
409 so that gaps between DECL_UIDs don't affect code generation. */
410 labels.reserve_exact (named_labels->elements ());
411 named_labels->traverse<vec<named_label_entry **> &, note_label> (labels);
412 labels.qsort (sort_labels);
413 FOR_EACH_VEC_ELT (labels, i, slot)
415 struct named_label_entry *ent = *slot;
417 pop_label (ent->label_decl, NULL_TREE);
419 /* Put the labels into the "variables" of the top-level block,
420 so debugger can see them. */
421 DECL_CHAIN (ent->label_decl) = BLOCK_VARS (block);
422 BLOCK_VARS (block) = ent->label_decl;
424 named_labels->clear_slot (slot);
426 named_labels = NULL;
430 /* At the end of a block with local labels, restore the outer definition. */
432 static void
433 pop_local_label (tree label, tree old_value)
435 struct named_label_entry dummy;
437 pop_label (label, old_value);
439 dummy.label_decl = label;
440 named_label_entry **slot = named_labels->find_slot (&dummy, NO_INSERT);
441 named_labels->clear_slot (slot);
444 /* The following two routines are used to interface to Objective-C++.
445 The binding level is purposely treated as an opaque type. */
447 void *
448 objc_get_current_scope (void)
450 return current_binding_level;
453 /* The following routine is used by the NeXT-style SJLJ exceptions;
454 variables get marked 'volatile' so as to not be clobbered by
455 _setjmp()/_longjmp() calls. All variables in the current scope,
456 as well as parent scopes up to (but not including) ENCLOSING_BLK
457 shall be thusly marked. */
459 void
460 objc_mark_locals_volatile (void *enclosing_blk)
462 cp_binding_level *scope;
464 for (scope = current_binding_level;
465 scope && scope != enclosing_blk;
466 scope = scope->level_chain)
468 tree decl;
470 for (decl = scope->names; decl; decl = TREE_CHAIN (decl))
471 objc_volatilize_decl (decl);
473 /* Do not climb up past the current function. */
474 if (scope->kind == sk_function_parms)
475 break;
479 /* True if B is the level for the condition of a constexpr if. */
481 static bool
482 level_for_constexpr_if (cp_binding_level *b)
484 return (b->kind == sk_cond && b->this_entity
485 && TREE_CODE (b->this_entity) == IF_STMT
486 && IF_STMT_CONSTEXPR_P (b->this_entity));
489 /* Update data for defined and undefined labels when leaving a scope. */
492 poplevel_named_label_1 (named_label_entry **slot, cp_binding_level *bl)
494 named_label_entry *ent = *slot;
495 cp_binding_level *obl = bl->level_chain;
497 if (ent->binding_level == bl)
499 tree decl;
501 /* ENT->NAMES_IN_SCOPE may contain a mixture of DECLs and
502 TREE_LISTs representing OVERLOADs, so be careful. */
503 for (decl = ent->names_in_scope; decl; decl = (DECL_P (decl)
504 ? DECL_CHAIN (decl)
505 : TREE_CHAIN (decl)))
506 if (decl_jump_unsafe (decl))
507 vec_safe_push (ent->bad_decls, decl);
509 ent->binding_level = obl;
510 ent->names_in_scope = obl->names;
511 switch (bl->kind)
513 case sk_try:
514 ent->in_try_scope = true;
515 break;
516 case sk_catch:
517 ent->in_catch_scope = true;
518 break;
519 case sk_omp:
520 ent->in_omp_scope = true;
521 break;
522 case sk_transaction:
523 ent->in_transaction_scope = true;
524 break;
525 case sk_block:
526 if (level_for_constexpr_if (bl->level_chain))
527 ent->in_constexpr_if = true;
528 break;
529 default:
530 break;
533 else if (ent->uses)
535 struct named_label_use_entry *use;
537 for (use = ent->uses; use ; use = use->next)
538 if (use->binding_level == bl)
540 use->binding_level = obl;
541 use->names_in_scope = obl->names;
542 if (bl->kind == sk_omp)
543 use->in_omp_scope = true;
547 return 1;
550 /* Saved errorcount to avoid -Wunused-but-set-{parameter,variable} warnings
551 when errors were reported, except for -Werror-unused-but-set-*. */
552 static int unused_but_set_errorcount;
554 /* Exit a binding level.
555 Pop the level off, and restore the state of the identifier-decl mappings
556 that were in effect when this level was entered.
558 If KEEP == 1, this level had explicit declarations, so
559 and create a "block" (a BLOCK node) for the level
560 to record its declarations and subblocks for symbol table output.
562 If FUNCTIONBODY is nonzero, this level is the body of a function,
563 so create a block as if KEEP were set and also clear out all
564 label names.
566 If REVERSE is nonzero, reverse the order of decls before putting
567 them into the BLOCK. */
569 tree
570 poplevel (int keep, int reverse, int functionbody)
572 tree link;
573 /* The chain of decls was accumulated in reverse order.
574 Put it into forward order, just for cleanliness. */
575 tree decls;
576 tree subblocks;
577 tree block;
578 tree decl;
579 int leaving_for_scope;
580 scope_kind kind;
581 unsigned ix;
582 cp_label_binding *label_bind;
584 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
585 restart:
587 block = NULL_TREE;
589 gcc_assert (current_binding_level->kind != sk_class
590 && current_binding_level->kind != sk_namespace);
592 if (current_binding_level->kind == sk_cleanup)
593 functionbody = 0;
594 subblocks = functionbody >= 0 ? current_binding_level->blocks : 0;
596 gcc_assert (!vec_safe_length (current_binding_level->class_shadowed));
598 /* We used to use KEEP == 2 to indicate that the new block should go
599 at the beginning of the list of blocks at this binding level,
600 rather than the end. This hack is no longer used. */
601 gcc_assert (keep == 0 || keep == 1);
603 if (current_binding_level->keep)
604 keep = 1;
606 /* Any uses of undefined labels, and any defined labels, now operate
607 under constraints of next binding contour. */
608 if (cfun && !functionbody && named_labels)
609 named_labels->traverse<cp_binding_level *, poplevel_named_label_1>
610 (current_binding_level);
612 /* Get the decls in the order they were written.
613 Usually current_binding_level->names is in reverse order.
614 But parameter decls were previously put in forward order. */
616 if (reverse)
617 current_binding_level->names
618 = decls = nreverse (current_binding_level->names);
619 else
620 decls = current_binding_level->names;
622 /* If there were any declarations or structure tags in that level,
623 or if this level is a function body,
624 create a BLOCK to record them for the life of this function. */
625 block = NULL_TREE;
626 /* Avoid function body block if possible. */
627 if (functionbody && subblocks && BLOCK_CHAIN (subblocks) == NULL_TREE)
628 keep = 0;
629 else if (keep == 1 || functionbody)
630 block = make_node (BLOCK);
631 if (block != NULL_TREE)
633 BLOCK_VARS (block) = decls;
634 BLOCK_SUBBLOCKS (block) = subblocks;
637 /* In each subblock, record that this is its superior. */
638 if (keep >= 0)
639 for (link = subblocks; link; link = BLOCK_CHAIN (link))
640 BLOCK_SUPERCONTEXT (link) = block;
642 /* We still support the old for-scope rules, whereby the variables
643 in a init statement were in scope after the for-statement ended.
644 We only use the new rules if flag_new_for_scope is nonzero. */
645 leaving_for_scope
646 = current_binding_level->kind == sk_for && flag_new_for_scope == 1;
648 /* Before we remove the declarations first check for unused variables. */
649 if ((warn_unused_variable || warn_unused_but_set_variable)
650 && current_binding_level->kind != sk_template_parms
651 && !processing_template_decl)
652 for (tree d = get_local_decls (); d; d = TREE_CHAIN (d))
654 /* There are cases where D itself is a TREE_LIST. See in
655 push_local_binding where the list of decls returned by
656 getdecls is built. */
657 decl = TREE_CODE (d) == TREE_LIST ? TREE_VALUE (d) : d;
659 tree type = TREE_TYPE (decl);
660 if (VAR_P (decl)
661 && (! TREE_USED (decl) || !DECL_READ_P (decl))
662 && ! DECL_IN_SYSTEM_HEADER (decl)
663 /* For structured bindings, consider only real variables, not
664 subobjects. */
665 && (DECL_DECOMPOSITION_P (decl) ? !DECL_DECOMP_BASE (decl)
666 : (DECL_NAME (decl) && !DECL_ARTIFICIAL (decl)))
667 && type != error_mark_node
668 && (!CLASS_TYPE_P (type)
669 || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
670 || lookup_attribute ("warn_unused",
671 TYPE_ATTRIBUTES (TREE_TYPE (decl)))))
673 if (! TREE_USED (decl))
675 if (!DECL_NAME (decl) && DECL_DECOMPOSITION_P (decl))
676 warning_at (DECL_SOURCE_LOCATION (decl),
677 OPT_Wunused_variable,
678 "unused structured binding declaration");
679 else
680 warning_at (DECL_SOURCE_LOCATION (decl),
681 OPT_Wunused_variable, "unused variable %qD", decl);
683 else if (DECL_CONTEXT (decl) == current_function_decl
684 // For -Wunused-but-set-variable leave references alone.
685 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
686 && errorcount == unused_but_set_errorcount)
688 if (!DECL_NAME (decl) && DECL_DECOMPOSITION_P (decl))
689 warning_at (DECL_SOURCE_LOCATION (decl),
690 OPT_Wunused_but_set_variable, "structured "
691 "binding declaration set but not used");
692 else
693 warning_at (DECL_SOURCE_LOCATION (decl),
694 OPT_Wunused_but_set_variable,
695 "variable %qD set but not used", decl);
696 unused_but_set_errorcount = errorcount;
701 /* Remove declarations for all the DECLs in this level. */
702 for (link = decls; link; link = TREE_CHAIN (link))
704 decl = TREE_CODE (link) == TREE_LIST ? TREE_VALUE (link) : link;
705 tree name = OVL_NAME (decl);
707 if (leaving_for_scope && VAR_P (decl)
708 /* It's hard to make this ARM compatibility hack play nicely with
709 lambdas, and it really isn't necessary in C++11 mode. */
710 && cxx_dialect < cxx11
711 && name)
713 cxx_binding *ob = outer_binding (name,
714 IDENTIFIER_BINDING (name),
715 /*class_p=*/true);
716 tree ns_binding = NULL_TREE;
717 if (!ob)
718 ns_binding = get_namespace_binding (current_namespace, name);
720 if (ob && ob->scope == current_binding_level->level_chain)
721 /* We have something like:
723 int i;
724 for (int i; ;);
726 and we are leaving the `for' scope. There's no reason to
727 keep the binding of the inner `i' in this case. */
729 else if ((ob && (TREE_CODE (ob->value) == TYPE_DECL))
730 || (ns_binding && TREE_CODE (ns_binding) == TYPE_DECL))
731 /* Here, we have something like:
733 typedef int I;
735 void f () {
736 for (int I; ;);
739 We must pop the for-scope binding so we know what's a
740 type and what isn't. */
742 else
744 /* Mark this VAR_DECL as dead so that we can tell we left it
745 there only for backward compatibility. */
746 DECL_DEAD_FOR_LOCAL (link) = 1;
748 /* Keep track of what should have happened when we
749 popped the binding. */
750 if (ob && ob->value)
752 SET_DECL_SHADOWED_FOR_VAR (link, ob->value);
753 DECL_HAS_SHADOWED_FOR_VAR_P (link) = 1;
756 /* Add it to the list of dead variables in the next
757 outermost binding to that we can remove these when we
758 leave that binding. */
759 vec_safe_push (
760 current_binding_level->level_chain->dead_vars_from_for,
761 link);
763 /* Although we don't pop the cxx_binding, we do clear
764 its SCOPE since the scope is going away now. */
765 IDENTIFIER_BINDING (name)->scope
766 = current_binding_level->level_chain;
768 /* Don't remove the binding. */
769 name = NULL_TREE;
772 /* Remove the binding. */
773 pop_local_binding (name, decl);
776 /* Remove declarations for any `for' variables from inner scopes
777 that we kept around. */
778 FOR_EACH_VEC_SAFE_ELT_REVERSE (current_binding_level->dead_vars_from_for,
779 ix, decl)
780 pop_local_binding (DECL_NAME (decl), decl);
782 /* Restore the IDENTIFIER_TYPE_VALUEs. */
783 for (link = current_binding_level->type_shadowed;
784 link; link = TREE_CHAIN (link))
785 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link), TREE_VALUE (link));
787 /* Restore the IDENTIFIER_LABEL_VALUEs for local labels. */
788 FOR_EACH_VEC_SAFE_ELT_REVERSE (current_binding_level->shadowed_labels,
789 ix, label_bind)
790 pop_local_label (label_bind->label, label_bind->prev_value);
792 /* There may be OVERLOADs (wrapped in TREE_LISTs) on the BLOCK_VARs
793 list if a `using' declaration put them there. The debugging
794 back ends won't understand OVERLOAD, so we remove them here.
795 Because the BLOCK_VARS are (temporarily) shared with
796 CURRENT_BINDING_LEVEL->NAMES we must do this fixup after we have
797 popped all the bindings. Also remove undeduced 'auto' decls,
798 which LTO doesn't understand, and can't have been used by anything. */
799 if (block)
801 tree* d;
803 for (d = &BLOCK_VARS (block); *d; )
805 if (TREE_CODE (*d) == TREE_LIST
806 || (!processing_template_decl
807 && undeduced_auto_decl (*d)))
808 *d = TREE_CHAIN (*d);
809 else
810 d = &DECL_CHAIN (*d);
814 /* If the level being exited is the top level of a function,
815 check over all the labels. */
816 if (functionbody)
818 if (block)
820 /* Since this is the top level block of a function, the vars are
821 the function's parameters. Don't leave them in the BLOCK
822 because they are found in the FUNCTION_DECL instead. */
823 BLOCK_VARS (block) = 0;
824 pop_labels (block);
826 else
827 pop_labels (subblocks);
830 kind = current_binding_level->kind;
831 if (kind == sk_cleanup)
833 tree stmt;
835 /* If this is a temporary binding created for a cleanup, then we'll
836 have pushed a statement list level. Pop that, create a new
837 BIND_EXPR for the block, and insert it into the stream. */
838 stmt = pop_stmt_list (current_binding_level->statement_list);
839 stmt = c_build_bind_expr (input_location, block, stmt);
840 add_stmt (stmt);
843 leave_scope ();
844 if (functionbody)
846 /* The current function is being defined, so its DECL_INITIAL
847 should be error_mark_node. */
848 gcc_assert (DECL_INITIAL (current_function_decl) == error_mark_node);
849 DECL_INITIAL (current_function_decl) = block ? block : subblocks;
850 if (subblocks)
852 if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
854 if (BLOCK_SUBBLOCKS (subblocks))
855 BLOCK_OUTER_CURLY_BRACE_P (BLOCK_SUBBLOCKS (subblocks)) = 1;
857 else
858 BLOCK_OUTER_CURLY_BRACE_P (subblocks) = 1;
861 else if (block)
862 current_binding_level->blocks
863 = block_chainon (current_binding_level->blocks, block);
865 /* If we did not make a block for the level just exited,
866 any blocks made for inner levels
867 (since they cannot be recorded as subblocks in that level)
868 must be carried forward so they will later become subblocks
869 of something else. */
870 else if (subblocks)
871 current_binding_level->blocks
872 = block_chainon (current_binding_level->blocks, subblocks);
874 /* Each and every BLOCK node created here in `poplevel' is important
875 (e.g. for proper debugging information) so if we created one
876 earlier, mark it as "used". */
877 if (block)
878 TREE_USED (block) = 1;
880 /* All temporary bindings created for cleanups are popped silently. */
881 if (kind == sk_cleanup)
882 goto restart;
884 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
885 return block;
888 /* Call wrapup_globals_declarations for the globals in NAMESPACE. */
889 /* Diagnose odr-used extern inline variables without definitions
890 in the current TU. */
893 wrapup_namespace_globals ()
895 if (vec<tree, va_gc> *statics = static_decls)
897 tree decl;
898 unsigned int i;
899 FOR_EACH_VEC_ELT (*statics, i, decl)
901 if (warn_unused_function
902 && TREE_CODE (decl) == FUNCTION_DECL
903 && DECL_INITIAL (decl) == 0
904 && DECL_EXTERNAL (decl)
905 && !TREE_PUBLIC (decl)
906 && !DECL_ARTIFICIAL (decl)
907 && !DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
908 && !TREE_NO_WARNING (decl))
909 warning_at (DECL_SOURCE_LOCATION (decl),
910 OPT_Wunused_function,
911 "%qF declared %<static%> but never defined", decl);
913 if (VAR_P (decl)
914 && DECL_EXTERNAL (decl)
915 && DECL_INLINE_VAR_P (decl)
916 && DECL_ODR_USED (decl))
917 error_at (DECL_SOURCE_LOCATION (decl),
918 "odr-used inline variable %qD is not defined", decl);
921 /* Clear out the list, so we don't rescan next time. */
922 static_decls = NULL;
924 /* Write out any globals that need to be output. */
925 return wrapup_global_declarations (statics->address (),
926 statics->length ());
928 return 0;
931 /* In C++, you don't have to write `struct S' to refer to `S'; you
932 can just use `S'. We accomplish this by creating a TYPE_DECL as
933 if the user had written `typedef struct S S'. Create and return
934 the TYPE_DECL for TYPE. */
936 tree
937 create_implicit_typedef (tree name, tree type)
939 tree decl;
941 decl = build_decl (input_location, TYPE_DECL, name, type);
942 DECL_ARTIFICIAL (decl) = 1;
943 /* There are other implicit type declarations, like the one *within*
944 a class that allows you to write `S::S'. We must distinguish
945 amongst these. */
946 SET_DECL_IMPLICIT_TYPEDEF_P (decl);
947 TYPE_NAME (type) = decl;
948 TYPE_STUB_DECL (type) = decl;
950 return decl;
953 /* Remember a local name for name-mangling purposes. */
955 static void
956 push_local_name (tree decl)
958 size_t i, nelts;
959 tree t, name;
961 timevar_start (TV_NAME_LOOKUP);
963 name = DECL_NAME (decl);
965 nelts = vec_safe_length (local_names);
966 for (i = 0; i < nelts; i++)
968 t = (*local_names)[i];
969 if (DECL_NAME (t) == name)
971 retrofit_lang_decl (decl);
972 DECL_LANG_SPECIFIC (decl)->u.base.u2sel = 1;
973 if (DECL_DISCRIMINATOR_SET_P (t))
974 DECL_DISCRIMINATOR (decl) = DECL_DISCRIMINATOR (t) + 1;
975 else
976 DECL_DISCRIMINATOR (decl) = 1;
978 (*local_names)[i] = decl;
979 timevar_stop (TV_NAME_LOOKUP);
980 return;
984 vec_safe_push (local_names, decl);
985 timevar_stop (TV_NAME_LOOKUP);
988 /* Subroutine of duplicate_decls: return truthvalue of whether
989 or not types of these decls match.
991 For C++, we must compare the parameter list so that `int' can match
992 `int&' in a parameter position, but `int&' is not confused with
993 `const int&'. */
996 decls_match (tree newdecl, tree olddecl)
998 int types_match;
1000 if (newdecl == olddecl)
1001 return 1;
1003 if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
1004 /* If the two DECLs are not even the same kind of thing, we're not
1005 interested in their types. */
1006 return 0;
1008 gcc_assert (DECL_P (newdecl));
1010 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1012 tree f1 = TREE_TYPE (newdecl);
1013 tree f2 = TREE_TYPE (olddecl);
1014 tree p1 = TYPE_ARG_TYPES (f1);
1015 tree p2 = TYPE_ARG_TYPES (f2);
1016 tree r2;
1018 /* Specializations of different templates are different functions
1019 even if they have the same type. */
1020 tree t1 = (DECL_USE_TEMPLATE (newdecl)
1021 ? DECL_TI_TEMPLATE (newdecl)
1022 : NULL_TREE);
1023 tree t2 = (DECL_USE_TEMPLATE (olddecl)
1024 ? DECL_TI_TEMPLATE (olddecl)
1025 : NULL_TREE);
1026 if (t1 != t2)
1027 return 0;
1029 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
1030 && ! (DECL_EXTERN_C_P (newdecl)
1031 && DECL_EXTERN_C_P (olddecl)))
1032 return 0;
1034 /* A new declaration doesn't match a built-in one unless it
1035 is also extern "C". */
1036 if (DECL_IS_BUILTIN (olddecl)
1037 && DECL_EXTERN_C_P (olddecl) && !DECL_EXTERN_C_P (newdecl))
1038 return 0;
1040 if (TREE_CODE (f1) != TREE_CODE (f2))
1041 return 0;
1043 /* A declaration with deduced return type should use its pre-deduction
1044 type for declaration matching. */
1045 r2 = fndecl_declared_return_type (olddecl);
1047 if (same_type_p (TREE_TYPE (f1), r2))
1049 if (!prototype_p (f2) && DECL_EXTERN_C_P (olddecl)
1050 && (DECL_BUILT_IN (olddecl)
1051 #ifndef NO_IMPLICIT_EXTERN_C
1052 || (DECL_IN_SYSTEM_HEADER (newdecl) && !DECL_CLASS_SCOPE_P (newdecl))
1053 || (DECL_IN_SYSTEM_HEADER (olddecl) && !DECL_CLASS_SCOPE_P (olddecl))
1054 #endif
1057 types_match = self_promoting_args_p (p1);
1058 if (p1 == void_list_node)
1059 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
1061 #ifndef NO_IMPLICIT_EXTERN_C
1062 else if (!prototype_p (f1)
1063 && (DECL_EXTERN_C_P (olddecl)
1064 && DECL_IN_SYSTEM_HEADER (olddecl)
1065 && !DECL_CLASS_SCOPE_P (olddecl))
1066 && (DECL_EXTERN_C_P (newdecl)
1067 && DECL_IN_SYSTEM_HEADER (newdecl)
1068 && !DECL_CLASS_SCOPE_P (newdecl)))
1070 types_match = self_promoting_args_p (p2);
1071 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
1073 #endif
1074 else
1075 types_match =
1076 compparms (p1, p2)
1077 && type_memfn_rqual (f1) == type_memfn_rqual (f2)
1078 && (TYPE_ATTRIBUTES (TREE_TYPE (newdecl)) == NULL_TREE
1079 || comp_type_attributes (TREE_TYPE (newdecl),
1080 TREE_TYPE (olddecl)) != 0);
1082 else
1083 types_match = 0;
1085 /* The decls dont match if they correspond to two different versions
1086 of the same function. Disallow extern "C" functions to be
1087 versions for now. */
1088 if (types_match
1089 && !DECL_EXTERN_C_P (newdecl)
1090 && !DECL_EXTERN_C_P (olddecl)
1091 && targetm.target_option.function_versions (newdecl, olddecl))
1093 /* Mark functions as versions if necessary. Modify the mangled decl
1094 name if necessary. */
1095 if (DECL_FUNCTION_VERSIONED (newdecl)
1096 && DECL_FUNCTION_VERSIONED (olddecl))
1097 return 0;
1098 if (!DECL_FUNCTION_VERSIONED (newdecl))
1100 DECL_FUNCTION_VERSIONED (newdecl) = 1;
1101 if (DECL_ASSEMBLER_NAME_SET_P (newdecl))
1102 mangle_decl (newdecl);
1104 if (!DECL_FUNCTION_VERSIONED (olddecl))
1106 DECL_FUNCTION_VERSIONED (olddecl) = 1;
1107 if (DECL_ASSEMBLER_NAME_SET_P (olddecl))
1108 mangle_decl (olddecl);
1110 cgraph_node::record_function_versions (olddecl, newdecl);
1111 return 0;
1114 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1116 tree oldres = DECL_TEMPLATE_RESULT (olddecl);
1117 tree newres = DECL_TEMPLATE_RESULT (newdecl);
1119 if (TREE_CODE (newres) != TREE_CODE (oldres))
1120 return 0;
1122 if (!comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
1123 DECL_TEMPLATE_PARMS (olddecl)))
1124 return 0;
1126 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
1127 types_match = (same_type_p (TREE_TYPE (oldres), TREE_TYPE (newres))
1128 && equivalently_constrained (olddecl, newdecl));
1129 else
1130 // We don't need to check equivalently_constrained for variable and
1131 // function templates because we check it on the results.
1132 types_match = decls_match (oldres, newres);
1134 else
1136 /* Need to check scope for variable declaration (VAR_DECL).
1137 For typedef (TYPE_DECL), scope is ignored. */
1138 if (VAR_P (newdecl)
1139 && CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
1140 /* [dcl.link]
1141 Two declarations for an object with C language linkage
1142 with the same name (ignoring the namespace that qualify
1143 it) that appear in different namespace scopes refer to
1144 the same object. */
1145 && !(DECL_EXTERN_C_P (olddecl) && DECL_EXTERN_C_P (newdecl)))
1146 return 0;
1148 if (TREE_TYPE (newdecl) == error_mark_node)
1149 types_match = TREE_TYPE (olddecl) == error_mark_node;
1150 else if (TREE_TYPE (olddecl) == NULL_TREE)
1151 types_match = TREE_TYPE (newdecl) == NULL_TREE;
1152 else if (TREE_TYPE (newdecl) == NULL_TREE)
1153 types_match = 0;
1154 else
1155 types_match = comptypes (TREE_TYPE (newdecl),
1156 TREE_TYPE (olddecl),
1157 COMPARE_REDECLARATION);
1160 // Normal functions can be constrained, as can variable partial
1161 // specializations.
1162 if (types_match && VAR_OR_FUNCTION_DECL_P (newdecl))
1163 types_match = equivalently_constrained (newdecl, olddecl);
1165 return types_match;
1168 /* If NEWDECL is `static' and an `extern' was seen previously,
1169 warn about it. OLDDECL is the previous declaration.
1171 Note that this does not apply to the C++ case of declaring
1172 a variable `extern const' and then later `const'.
1174 Don't complain about built-in functions, since they are beyond
1175 the user's control. */
1177 void
1178 warn_extern_redeclared_static (tree newdecl, tree olddecl)
1180 if (TREE_CODE (newdecl) == TYPE_DECL
1181 || TREE_CODE (newdecl) == TEMPLATE_DECL
1182 || TREE_CODE (newdecl) == CONST_DECL
1183 || TREE_CODE (newdecl) == NAMESPACE_DECL)
1184 return;
1186 /* Don't get confused by static member functions; that's a different
1187 use of `static'. */
1188 if (TREE_CODE (newdecl) == FUNCTION_DECL
1189 && DECL_STATIC_FUNCTION_P (newdecl))
1190 return;
1192 /* If the old declaration was `static', or the new one isn't, then
1193 everything is OK. */
1194 if (DECL_THIS_STATIC (olddecl) || !DECL_THIS_STATIC (newdecl))
1195 return;
1197 /* It's OK to declare a builtin function as `static'. */
1198 if (TREE_CODE (olddecl) == FUNCTION_DECL
1199 && DECL_ARTIFICIAL (olddecl))
1200 return;
1202 if (permerror (DECL_SOURCE_LOCATION (newdecl),
1203 "%qD was declared %<extern%> and later %<static%>", newdecl))
1204 inform (DECL_SOURCE_LOCATION (olddecl),
1205 "previous declaration of %qD", olddecl);
1208 /* NEW_DECL is a redeclaration of OLD_DECL; both are functions or
1209 function templates. If their exception specifications do not
1210 match, issue a diagnostic. */
1212 static void
1213 check_redeclaration_exception_specification (tree new_decl,
1214 tree old_decl)
1216 tree new_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (new_decl));
1217 tree old_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old_decl));
1219 /* Two default specs are equivalent, don't force evaluation. */
1220 if (UNEVALUATED_NOEXCEPT_SPEC_P (new_exceptions)
1221 && UNEVALUATED_NOEXCEPT_SPEC_P (old_exceptions))
1222 return;
1224 maybe_instantiate_noexcept (new_decl);
1225 maybe_instantiate_noexcept (old_decl);
1226 new_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (new_decl));
1227 old_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old_decl));
1229 /* [except.spec]
1231 If any declaration of a function has an exception-specification,
1232 all declarations, including the definition and an explicit
1233 specialization, of that function shall have an
1234 exception-specification with the same set of type-ids. */
1235 if (! DECL_IS_BUILTIN (old_decl)
1236 && !comp_except_specs (new_exceptions, old_exceptions, ce_normal))
1238 const char *const msg
1239 = G_("declaration of %qF has a different exception specifier");
1240 bool complained = true;
1241 location_t new_loc = DECL_SOURCE_LOCATION (new_decl);
1242 if (DECL_IN_SYSTEM_HEADER (old_decl))
1243 complained = pedwarn (new_loc, OPT_Wsystem_headers, msg, new_decl);
1244 else if (!flag_exceptions)
1245 /* We used to silently permit mismatched eh specs with
1246 -fno-exceptions, so make them a pedwarn now. */
1247 complained = pedwarn (new_loc, OPT_Wpedantic, msg, new_decl);
1248 else
1249 error_at (new_loc, msg, new_decl);
1250 if (complained)
1251 inform (DECL_SOURCE_LOCATION (old_decl),
1252 "from previous declaration %qF", old_decl);
1256 /* Return true if OLD_DECL and NEW_DECL agree on constexprness.
1257 Otherwise issue diagnostics. */
1259 static bool
1260 validate_constexpr_redeclaration (tree old_decl, tree new_decl)
1262 old_decl = STRIP_TEMPLATE (old_decl);
1263 new_decl = STRIP_TEMPLATE (new_decl);
1264 if (!VAR_OR_FUNCTION_DECL_P (old_decl)
1265 || !VAR_OR_FUNCTION_DECL_P (new_decl))
1266 return true;
1267 if (DECL_DECLARED_CONSTEXPR_P (old_decl)
1268 == DECL_DECLARED_CONSTEXPR_P (new_decl))
1269 return true;
1270 if (TREE_CODE (old_decl) == FUNCTION_DECL)
1272 if (DECL_BUILT_IN (old_decl))
1274 /* Hide a built-in declaration. */
1275 DECL_DECLARED_CONSTEXPR_P (old_decl)
1276 = DECL_DECLARED_CONSTEXPR_P (new_decl);
1277 return true;
1279 /* 7.1.5 [dcl.constexpr]
1280 Note: An explicit specialization can differ from the template
1281 declaration with respect to the constexpr specifier. */
1282 if (! DECL_TEMPLATE_SPECIALIZATION (old_decl)
1283 && DECL_TEMPLATE_SPECIALIZATION (new_decl))
1284 return true;
1286 error_at (DECL_SOURCE_LOCATION (new_decl),
1287 "redeclaration %qD differs in %<constexpr%> "
1288 "from previous declaration", new_decl);
1289 inform (DECL_SOURCE_LOCATION (old_decl),
1290 "previous declaration %qD", old_decl);
1291 return false;
1293 return true;
1296 // If OLDDECL and NEWDECL are concept declarations with the same type
1297 // (i.e., and template parameters), but different requirements,
1298 // emit diagnostics and return true. Otherwise, return false.
1299 static inline bool
1300 check_concept_refinement (tree olddecl, tree newdecl)
1302 if (!DECL_DECLARED_CONCEPT_P (olddecl) || !DECL_DECLARED_CONCEPT_P (newdecl))
1303 return false;
1305 tree d1 = DECL_TEMPLATE_RESULT (olddecl);
1306 tree d2 = DECL_TEMPLATE_RESULT (newdecl);
1307 if (TREE_CODE (d1) != TREE_CODE (d2))
1308 return false;
1310 tree t1 = TREE_TYPE (d1);
1311 tree t2 = TREE_TYPE (d2);
1312 if (TREE_CODE (d1) == FUNCTION_DECL)
1314 if (compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2))
1315 && comp_template_parms (DECL_TEMPLATE_PARMS (olddecl),
1316 DECL_TEMPLATE_PARMS (newdecl))
1317 && !equivalently_constrained (olddecl, newdecl))
1319 error ("cannot specialize concept %q#D", olddecl);
1320 return true;
1323 return false;
1326 /* DECL is a redeclaration of a function or function template. If
1327 it does have default arguments issue a diagnostic. Note: this
1328 function is used to enforce the requirements in C++11 8.3.6 about
1329 no default arguments in redeclarations. */
1331 static void
1332 check_redeclaration_no_default_args (tree decl)
1334 gcc_assert (DECL_DECLARES_FUNCTION_P (decl));
1336 for (tree t = FUNCTION_FIRST_USER_PARMTYPE (decl);
1337 t && t != void_list_node; t = TREE_CHAIN (t))
1338 if (TREE_PURPOSE (t))
1340 permerror (DECL_SOURCE_LOCATION (decl),
1341 "redeclaration of %q#D may not have default "
1342 "arguments", decl);
1343 return;
1347 #define GNU_INLINE_P(fn) (DECL_DECLARED_INLINE_P (fn) \
1348 && lookup_attribute ("gnu_inline", \
1349 DECL_ATTRIBUTES (fn)))
1351 /* If NEWDECL is a redeclaration of OLDDECL, merge the declarations.
1352 If the redeclaration is invalid, a diagnostic is issued, and the
1353 error_mark_node is returned. Otherwise, OLDDECL is returned.
1355 If NEWDECL is not a redeclaration of OLDDECL, NULL_TREE is
1356 returned.
1358 NEWDECL_IS_FRIEND is true if NEWDECL was declared as a friend. */
1360 tree
1361 duplicate_decls (tree newdecl, tree olddecl, bool newdecl_is_friend)
1363 unsigned olddecl_uid = DECL_UID (olddecl);
1364 int olddecl_friend = 0, types_match = 0, hidden_friend = 0;
1365 int new_defines_function = 0;
1366 tree new_template_info;
1368 if (newdecl == olddecl)
1369 return olddecl;
1371 types_match = decls_match (newdecl, olddecl);
1373 /* If either the type of the new decl or the type of the old decl is an
1374 error_mark_node, then that implies that we have already issued an
1375 error (earlier) for some bogus type specification, and in that case,
1376 it is rather pointless to harass the user with yet more error message
1377 about the same declaration, so just pretend the types match here. */
1378 if (TREE_TYPE (newdecl) == error_mark_node
1379 || TREE_TYPE (olddecl) == error_mark_node)
1380 return error_mark_node;
1382 if (UDLIT_OPER_P (DECL_NAME (newdecl))
1383 && UDLIT_OPER_P (DECL_NAME (olddecl)))
1385 if (TREE_CODE (newdecl) == TEMPLATE_DECL
1386 && TREE_CODE (olddecl) != TEMPLATE_DECL
1387 && check_raw_literal_operator (olddecl))
1388 error ("literal operator template %q+D conflicts with"
1389 " raw literal operator %qD", newdecl, olddecl);
1390 else if (TREE_CODE (newdecl) != TEMPLATE_DECL
1391 && TREE_CODE (olddecl) == TEMPLATE_DECL
1392 && check_raw_literal_operator (newdecl))
1393 error ("raw literal operator %q+D conflicts with"
1394 " literal operator template %qD", newdecl, olddecl);
1397 if (DECL_P (olddecl)
1398 && TREE_CODE (newdecl) == FUNCTION_DECL
1399 && TREE_CODE (olddecl) == FUNCTION_DECL
1400 && diagnose_mismatched_attributes (olddecl, newdecl))
1402 if (DECL_INITIAL (olddecl))
1403 inform (DECL_SOURCE_LOCATION (olddecl),
1404 "previous definition of %qD was here", olddecl);
1405 else
1406 inform (DECL_SOURCE_LOCATION (olddecl),
1407 "previous declaration of %qD was here", olddecl);
1410 /* Check for redeclaration and other discrepancies. */
1411 if (TREE_CODE (olddecl) == FUNCTION_DECL
1412 && DECL_ARTIFICIAL (olddecl))
1414 gcc_assert (!DECL_HIDDEN_FRIEND_P (olddecl));
1415 if (TREE_CODE (newdecl) != FUNCTION_DECL)
1417 /* Avoid warnings redeclaring built-ins which have not been
1418 explicitly declared. */
1419 if (DECL_ANTICIPATED (olddecl))
1420 return NULL_TREE;
1422 /* If you declare a built-in or predefined function name as static,
1423 the old definition is overridden, but optionally warn this was a
1424 bad choice of name. */
1425 if (! TREE_PUBLIC (newdecl))
1427 warning (OPT_Wshadow,
1428 DECL_BUILT_IN (olddecl)
1429 ? G_("shadowing built-in function %q#D")
1430 : G_("shadowing library function %q#D"), olddecl);
1431 /* Discard the old built-in function. */
1432 return NULL_TREE;
1434 /* If the built-in is not ansi, then programs can override
1435 it even globally without an error. */
1436 else if (! DECL_BUILT_IN (olddecl))
1437 warning_at (DECL_SOURCE_LOCATION (newdecl), 0,
1438 "library function %q#D redeclared as non-function %q#D",
1439 olddecl, newdecl);
1440 else
1441 error ("declaration of %q+#D conflicts with built-in "
1442 "declaration %q#D", newdecl, olddecl);
1443 return NULL_TREE;
1445 else if (DECL_OMP_DECLARE_REDUCTION_P (olddecl))
1447 gcc_assert (DECL_OMP_DECLARE_REDUCTION_P (newdecl));
1448 error_at (DECL_SOURCE_LOCATION (newdecl),
1449 "redeclaration of %<pragma omp declare reduction%>");
1450 inform (DECL_SOURCE_LOCATION (olddecl),
1451 "previous %<pragma omp declare reduction%> declaration");
1452 return error_mark_node;
1454 else if (!types_match)
1456 /* Avoid warnings redeclaring built-ins which have not been
1457 explicitly declared. */
1458 if (DECL_ANTICIPATED (olddecl))
1460 tree t1, t2;
1462 /* A new declaration doesn't match a built-in one unless it
1463 is also extern "C". */
1464 gcc_assert (DECL_IS_BUILTIN (olddecl));
1465 gcc_assert (DECL_EXTERN_C_P (olddecl));
1466 if (!DECL_EXTERN_C_P (newdecl))
1467 return NULL_TREE;
1469 for (t1 = TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1470 t2 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1471 t1 || t2;
1472 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1474 if (!t1 || !t2)
1475 break;
1476 /* FILE, tm types are not known at the time
1477 we create the builtins. */
1478 for (unsigned i = 0;
1479 i < sizeof (builtin_structptr_types)
1480 / sizeof (builtin_structptr_type);
1481 ++i)
1482 if (TREE_VALUE (t2) == builtin_structptr_types[i].node)
1484 tree t = TREE_VALUE (t1);
1486 if (TYPE_PTR_P (t)
1487 && TYPE_IDENTIFIER (TREE_TYPE (t))
1488 == get_identifier (builtin_structptr_types[i].str)
1489 && compparms (TREE_CHAIN (t1), TREE_CHAIN (t2)))
1491 tree oldargs = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1493 TYPE_ARG_TYPES (TREE_TYPE (olddecl))
1494 = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
1495 types_match = decls_match (newdecl, olddecl);
1496 if (types_match)
1497 return duplicate_decls (newdecl, olddecl,
1498 newdecl_is_friend);
1499 TYPE_ARG_TYPES (TREE_TYPE (olddecl)) = oldargs;
1501 goto next_arg;
1504 if (! same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1505 break;
1506 next_arg:;
1509 warning_at (DECL_SOURCE_LOCATION (newdecl),
1510 OPT_Wbuiltin_declaration_mismatch,
1511 "declaration of %q+#D conflicts with built-in "
1512 "declaration %q#D", newdecl, olddecl);
1514 else if ((DECL_EXTERN_C_P (newdecl)
1515 && DECL_EXTERN_C_P (olddecl))
1516 || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1517 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
1519 /* A near match; override the builtin. */
1521 if (TREE_PUBLIC (newdecl))
1522 warning_at (DECL_SOURCE_LOCATION (newdecl),
1523 OPT_Wbuiltin_declaration_mismatch,
1524 "new declaration %q#D ambiguates built-in "
1525 "declaration %q#D", newdecl, olddecl);
1526 else
1527 warning (OPT_Wshadow,
1528 DECL_BUILT_IN (olddecl)
1529 ? G_("shadowing built-in function %q#D")
1530 : G_("shadowing library function %q#D"), olddecl);
1532 else
1533 /* Discard the old built-in function. */
1534 return NULL_TREE;
1536 /* Replace the old RTL to avoid problems with inlining. */
1537 COPY_DECL_RTL (newdecl, olddecl);
1539 /* Even if the types match, prefer the new declarations type for
1540 built-ins which have not been explicitly declared, for
1541 exception lists, etc... */
1542 else if (DECL_IS_BUILTIN (olddecl))
1544 tree type = TREE_TYPE (newdecl);
1545 tree attribs = (*targetm.merge_type_attributes)
1546 (TREE_TYPE (olddecl), type);
1548 type = cp_build_type_attribute_variant (type, attribs);
1549 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = type;
1552 /* If a function is explicitly declared "throw ()", propagate that to
1553 the corresponding builtin. */
1554 if (DECL_BUILT_IN_CLASS (olddecl) == BUILT_IN_NORMAL
1555 && DECL_ANTICIPATED (olddecl)
1556 && TREE_NOTHROW (newdecl)
1557 && !TREE_NOTHROW (olddecl))
1559 enum built_in_function fncode = DECL_FUNCTION_CODE (olddecl);
1560 tree tmpdecl = builtin_decl_explicit (fncode);
1561 if (tmpdecl && tmpdecl != olddecl && types_match)
1562 TREE_NOTHROW (tmpdecl) = 1;
1565 /* Whether or not the builtin can throw exceptions has no
1566 bearing on this declarator. */
1567 TREE_NOTHROW (olddecl) = 0;
1569 if (DECL_THIS_STATIC (newdecl) && !DECL_THIS_STATIC (olddecl))
1571 /* If a builtin function is redeclared as `static', merge
1572 the declarations, but make the original one static. */
1573 DECL_THIS_STATIC (olddecl) = 1;
1574 TREE_PUBLIC (olddecl) = 0;
1576 /* Make the old declaration consistent with the new one so
1577 that all remnants of the builtin-ness of this function
1578 will be banished. */
1579 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
1580 COPY_DECL_RTL (newdecl, olddecl);
1583 else if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1585 /* C++ Standard, 3.3, clause 4:
1586 "[Note: a namespace name or a class template name must be unique
1587 in its declarative region (7.3.2, clause 14). ]" */
1588 if (TREE_CODE (olddecl) != NAMESPACE_DECL
1589 && TREE_CODE (newdecl) != NAMESPACE_DECL
1590 && (TREE_CODE (olddecl) != TEMPLATE_DECL
1591 || TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) != TYPE_DECL)
1592 && (TREE_CODE (newdecl) != TEMPLATE_DECL
1593 || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) != TYPE_DECL))
1595 if ((TREE_CODE (olddecl) == TYPE_DECL && DECL_ARTIFICIAL (olddecl)
1596 && TREE_CODE (newdecl) != TYPE_DECL)
1597 || (TREE_CODE (newdecl) == TYPE_DECL && DECL_ARTIFICIAL (newdecl)
1598 && TREE_CODE (olddecl) != TYPE_DECL))
1600 /* We do nothing special here, because C++ does such nasty
1601 things with TYPE_DECLs. Instead, just let the TYPE_DECL
1602 get shadowed, and know that if we need to find a TYPE_DECL
1603 for a given name, we can look in the IDENTIFIER_TYPE_VALUE
1604 slot of the identifier. */
1605 return NULL_TREE;
1608 if ((TREE_CODE (newdecl) == FUNCTION_DECL
1609 && DECL_FUNCTION_TEMPLATE_P (olddecl))
1610 || (TREE_CODE (olddecl) == FUNCTION_DECL
1611 && DECL_FUNCTION_TEMPLATE_P (newdecl)))
1612 return NULL_TREE;
1615 error ("%q#D redeclared as different kind of symbol", newdecl);
1616 if (TREE_CODE (olddecl) == TREE_LIST)
1617 olddecl = TREE_VALUE (olddecl);
1618 inform (DECL_SOURCE_LOCATION (olddecl),
1619 "previous declaration %q#D", olddecl);
1621 return error_mark_node;
1623 else if (!types_match)
1625 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl))
1626 /* These are certainly not duplicate declarations; they're
1627 from different scopes. */
1628 return NULL_TREE;
1630 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1632 /* The name of a class template may not be declared to refer to
1633 any other template, class, function, object, namespace, value,
1634 or type in the same scope. */
1635 if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == TYPE_DECL
1636 || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
1638 error ("conflicting declaration of template %q+#D", newdecl);
1639 inform (DECL_SOURCE_LOCATION (olddecl),
1640 "previous declaration %q#D", olddecl);
1641 return error_mark_node;
1643 else if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == FUNCTION_DECL
1644 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL
1645 && compparms (TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl))),
1646 TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl))))
1647 && comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
1648 DECL_TEMPLATE_PARMS (olddecl))
1649 /* Template functions can be disambiguated by
1650 return type. */
1651 && same_type_p (TREE_TYPE (TREE_TYPE (newdecl)),
1652 TREE_TYPE (TREE_TYPE (olddecl)))
1653 // Template functions can also be disambiguated by
1654 // constraints.
1655 && equivalently_constrained (olddecl, newdecl))
1657 error ("ambiguating new declaration %q+#D", newdecl);
1658 inform (DECL_SOURCE_LOCATION (olddecl),
1659 "old declaration %q#D", olddecl);
1661 else if (check_concept_refinement (olddecl, newdecl))
1662 return error_mark_node;
1663 return NULL_TREE;
1665 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1667 if (DECL_EXTERN_C_P (newdecl) && DECL_EXTERN_C_P (olddecl))
1669 error ("conflicting declaration of C function %q+#D",
1670 newdecl);
1671 inform (DECL_SOURCE_LOCATION (olddecl),
1672 "previous declaration %q#D", olddecl);
1673 return NULL_TREE;
1675 /* For function versions, params and types match, but they
1676 are not ambiguous. */
1677 else if ((!DECL_FUNCTION_VERSIONED (newdecl)
1678 && !DECL_FUNCTION_VERSIONED (olddecl))
1679 // The functions have the same parameter types.
1680 && compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1681 TYPE_ARG_TYPES (TREE_TYPE (olddecl)))
1682 // And the same constraints.
1683 && equivalently_constrained (newdecl, olddecl))
1685 error ("ambiguating new declaration of %q+#D", newdecl);
1686 inform (DECL_SOURCE_LOCATION (olddecl),
1687 "old declaration %q#D", olddecl);
1688 return error_mark_node;
1690 else
1691 return NULL_TREE;
1693 else
1695 error ("conflicting declaration %q+#D", newdecl);
1696 inform (DECL_SOURCE_LOCATION (olddecl),
1697 "previous declaration as %q#D", olddecl);
1698 return error_mark_node;
1701 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1702 && ((DECL_TEMPLATE_SPECIALIZATION (olddecl)
1703 && (!DECL_TEMPLATE_INFO (newdecl)
1704 || (DECL_TI_TEMPLATE (newdecl)
1705 != DECL_TI_TEMPLATE (olddecl))))
1706 || (DECL_TEMPLATE_SPECIALIZATION (newdecl)
1707 && (!DECL_TEMPLATE_INFO (olddecl)
1708 || (DECL_TI_TEMPLATE (olddecl)
1709 != DECL_TI_TEMPLATE (newdecl))))))
1710 /* It's OK to have a template specialization and a non-template
1711 with the same type, or to have specializations of two
1712 different templates with the same type. Note that if one is a
1713 specialization, and the other is an instantiation of the same
1714 template, that we do not exit at this point. That situation
1715 can occur if we instantiate a template class, and then
1716 specialize one of its methods. This situation is valid, but
1717 the declarations must be merged in the usual way. */
1718 return NULL_TREE;
1719 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1720 && ((DECL_TEMPLATE_INSTANTIATION (olddecl)
1721 && !DECL_USE_TEMPLATE (newdecl))
1722 || (DECL_TEMPLATE_INSTANTIATION (newdecl)
1723 && !DECL_USE_TEMPLATE (olddecl))))
1724 /* One of the declarations is a template instantiation, and the
1725 other is not a template at all. That's OK. */
1726 return NULL_TREE;
1727 else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
1729 /* In [namespace.alias] we have:
1731 In a declarative region, a namespace-alias-definition can be
1732 used to redefine a namespace-alias declared in that declarative
1733 region to refer only to the namespace to which it already
1734 refers.
1736 Therefore, if we encounter a second alias directive for the same
1737 alias, we can just ignore the second directive. */
1738 if (DECL_NAMESPACE_ALIAS (newdecl)
1739 && (DECL_NAMESPACE_ALIAS (newdecl)
1740 == DECL_NAMESPACE_ALIAS (olddecl)))
1741 return olddecl;
1743 /* Leave it to update_binding to merge or report error. */
1744 return NULL_TREE;
1746 else
1748 const char *errmsg = redeclaration_error_message (newdecl, olddecl);
1749 if (errmsg)
1751 error_at (DECL_SOURCE_LOCATION (newdecl), errmsg, newdecl);
1752 if (DECL_NAME (olddecl) != NULL_TREE)
1753 inform (DECL_SOURCE_LOCATION (olddecl),
1754 (DECL_INITIAL (olddecl) && namespace_bindings_p ())
1755 ? G_("%q#D previously defined here")
1756 : G_("%q#D previously declared here"), olddecl);
1757 return error_mark_node;
1759 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1760 && DECL_INITIAL (olddecl) != NULL_TREE
1761 && !prototype_p (TREE_TYPE (olddecl))
1762 && prototype_p (TREE_TYPE (newdecl)))
1764 /* Prototype decl follows defn w/o prototype. */
1765 if (warning_at (DECL_SOURCE_LOCATION (newdecl), 0,
1766 "prototype specified for %q#D", newdecl))
1767 inform (DECL_SOURCE_LOCATION (olddecl),
1768 "previous non-prototype definition here");
1770 else if (VAR_OR_FUNCTION_DECL_P (olddecl)
1771 && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl))
1773 /* [dcl.link]
1774 If two declarations of the same function or object
1775 specify different linkage-specifications ..., the program
1776 is ill-formed.... Except for functions with C++ linkage,
1777 a function declaration without a linkage specification
1778 shall not precede the first linkage specification for
1779 that function. A function can be declared without a
1780 linkage specification after an explicit linkage
1781 specification has been seen; the linkage explicitly
1782 specified in the earlier declaration is not affected by
1783 such a function declaration.
1785 DR 563 raises the question why the restrictions on
1786 functions should not also apply to objects. Older
1787 versions of G++ silently ignore the linkage-specification
1788 for this example:
1790 namespace N {
1791 extern int i;
1792 extern "C" int i;
1795 which is clearly wrong. Therefore, we now treat objects
1796 like functions. */
1797 if (current_lang_depth () == 0)
1799 /* There is no explicit linkage-specification, so we use
1800 the linkage from the previous declaration. */
1801 retrofit_lang_decl (newdecl);
1802 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
1804 else
1806 error ("conflicting declaration of %q+#D with %qL linkage",
1807 newdecl, DECL_LANGUAGE (newdecl));
1808 inform (DECL_SOURCE_LOCATION (olddecl),
1809 "previous declaration with %qL linkage",
1810 DECL_LANGUAGE (olddecl));
1814 if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl))
1816 else if (TREE_CODE (olddecl) == FUNCTION_DECL)
1818 /* Note: free functions, as TEMPLATE_DECLs, are handled below. */
1819 if (DECL_FUNCTION_MEMBER_P (olddecl)
1820 && (/* grokfndecl passes member function templates too
1821 as FUNCTION_DECLs. */
1822 DECL_TEMPLATE_INFO (olddecl)
1823 /* C++11 8.3.6/6.
1824 Default arguments for a member function of a class
1825 template shall be specified on the initial declaration
1826 of the member function within the class template. */
1827 || CLASSTYPE_TEMPLATE_INFO (CP_DECL_CONTEXT (olddecl))))
1828 check_redeclaration_no_default_args (newdecl);
1829 else
1831 tree t1 = FUNCTION_FIRST_USER_PARMTYPE (olddecl);
1832 tree t2 = FUNCTION_FIRST_USER_PARMTYPE (newdecl);
1833 int i = 1;
1835 for (; t1 && t1 != void_list_node;
1836 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++)
1837 if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
1839 if (1 == simple_cst_equal (TREE_PURPOSE (t1),
1840 TREE_PURPOSE (t2)))
1842 if (permerror (input_location,
1843 "default argument given for parameter "
1844 "%d of %q#D", i, newdecl))
1845 inform (DECL_SOURCE_LOCATION (olddecl),
1846 "previous specification in %q#D here",
1847 olddecl);
1849 else
1851 error ("default argument given for parameter %d "
1852 "of %q#D", i, newdecl);
1853 inform (DECL_SOURCE_LOCATION (olddecl),
1854 "previous specification in %q#D here",
1855 olddecl);
1862 /* Do not merge an implicit typedef with an explicit one. In:
1864 class A;
1866 typedef class A A __attribute__ ((foo));
1868 the attribute should apply only to the typedef. */
1869 if (TREE_CODE (olddecl) == TYPE_DECL
1870 && (DECL_IMPLICIT_TYPEDEF_P (olddecl)
1871 || DECL_IMPLICIT_TYPEDEF_P (newdecl)))
1872 return NULL_TREE;
1874 /* If new decl is `static' and an `extern' was seen previously,
1875 warn about it. */
1876 warn_extern_redeclared_static (newdecl, olddecl);
1878 if (!validate_constexpr_redeclaration (olddecl, newdecl))
1879 return error_mark_node;
1881 /* We have committed to returning 1 at this point. */
1882 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1884 /* Now that functions must hold information normally held
1885 by field decls, there is extra work to do so that
1886 declaration information does not get destroyed during
1887 definition. */
1888 if (DECL_VINDEX (olddecl))
1889 DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl);
1890 if (DECL_CONTEXT (olddecl))
1891 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1892 DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl);
1893 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1894 DECL_PURE_VIRTUAL_P (newdecl) |= DECL_PURE_VIRTUAL_P (olddecl);
1895 DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl);
1896 DECL_INVALID_OVERRIDER_P (newdecl) |= DECL_INVALID_OVERRIDER_P (olddecl);
1897 DECL_FINAL_P (newdecl) |= DECL_FINAL_P (olddecl);
1898 DECL_OVERRIDE_P (newdecl) |= DECL_OVERRIDE_P (olddecl);
1899 DECL_THIS_STATIC (newdecl) |= DECL_THIS_STATIC (olddecl);
1900 if (DECL_OVERLOADED_OPERATOR_P (olddecl) != ERROR_MARK)
1901 SET_OVERLOADED_OPERATOR_CODE
1902 (newdecl, DECL_OVERLOADED_OPERATOR_P (olddecl));
1903 new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE;
1905 /* Optionally warn about more than one declaration for the same
1906 name, but don't warn about a function declaration followed by a
1907 definition. */
1908 if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl)
1909 && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE)
1910 /* Don't warn about extern decl followed by definition. */
1911 && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl))
1912 /* Don't warn about friends, let add_friend take care of it. */
1913 && ! (newdecl_is_friend || DECL_FRIEND_P (olddecl))
1914 /* Don't warn about declaration followed by specialization. */
1915 && (! DECL_TEMPLATE_SPECIALIZATION (newdecl)
1916 || DECL_TEMPLATE_SPECIALIZATION (olddecl)))
1918 if (warning_at (DECL_SOURCE_LOCATION (newdecl),
1919 OPT_Wredundant_decls,
1920 "redundant redeclaration of %qD in same scope",
1921 newdecl))
1922 inform (DECL_SOURCE_LOCATION (olddecl),
1923 "previous declaration of %qD", olddecl);
1926 if (!(DECL_TEMPLATE_INSTANTIATION (olddecl)
1927 && DECL_TEMPLATE_SPECIALIZATION (newdecl)))
1929 if (DECL_DELETED_FN (newdecl))
1931 error ("deleted definition of %q+D", newdecl);
1932 inform (DECL_SOURCE_LOCATION (olddecl),
1933 "previous declaration of %qD", olddecl);
1935 DECL_DELETED_FN (newdecl) |= DECL_DELETED_FN (olddecl);
1939 /* Deal with C++: must preserve virtual function table size. */
1940 if (TREE_CODE (olddecl) == TYPE_DECL)
1942 tree newtype = TREE_TYPE (newdecl);
1943 tree oldtype = TREE_TYPE (olddecl);
1945 if (newtype != error_mark_node && oldtype != error_mark_node
1946 && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype))
1947 CLASSTYPE_FRIEND_CLASSES (newtype)
1948 = CLASSTYPE_FRIEND_CLASSES (oldtype);
1950 DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl);
1953 /* Copy all the DECL_... slots specified in the new decl
1954 except for any that we copy here from the old type. */
1955 DECL_ATTRIBUTES (newdecl)
1956 = (*targetm.merge_decl_attributes) (olddecl, newdecl);
1958 if (DECL_DECLARES_FUNCTION_P (olddecl) && DECL_DECLARES_FUNCTION_P (newdecl))
1960 olddecl_friend = DECL_FRIEND_P (olddecl);
1961 hidden_friend = (DECL_ANTICIPATED (olddecl)
1962 && DECL_HIDDEN_FRIEND_P (olddecl)
1963 && newdecl_is_friend);
1964 if (!hidden_friend)
1966 DECL_ANTICIPATED (olddecl) = 0;
1967 DECL_HIDDEN_FRIEND_P (olddecl) = 0;
1971 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1973 tree old_result;
1974 tree new_result;
1975 old_result = DECL_TEMPLATE_RESULT (olddecl);
1976 new_result = DECL_TEMPLATE_RESULT (newdecl);
1977 TREE_TYPE (olddecl) = TREE_TYPE (old_result);
1978 DECL_TEMPLATE_SPECIALIZATIONS (olddecl)
1979 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (olddecl),
1980 DECL_TEMPLATE_SPECIALIZATIONS (newdecl));
1982 DECL_ATTRIBUTES (old_result)
1983 = (*targetm.merge_decl_attributes) (old_result, new_result);
1985 if (DECL_FUNCTION_TEMPLATE_P (newdecl))
1987 /* Per C++11 8.3.6/4, default arguments cannot be added in later
1988 declarations of a function template. */
1989 if (DECL_SOURCE_LOCATION (newdecl)
1990 != DECL_SOURCE_LOCATION (olddecl))
1991 check_redeclaration_no_default_args (newdecl);
1993 check_default_args (newdecl);
1995 if (GNU_INLINE_P (old_result) != GNU_INLINE_P (new_result)
1996 && DECL_INITIAL (new_result))
1998 if (DECL_INITIAL (old_result))
1999 DECL_UNINLINABLE (old_result) = 1;
2000 else
2001 DECL_UNINLINABLE (old_result) = DECL_UNINLINABLE (new_result);
2002 DECL_EXTERNAL (old_result) = DECL_EXTERNAL (new_result);
2003 DECL_NOT_REALLY_EXTERN (old_result)
2004 = DECL_NOT_REALLY_EXTERN (new_result);
2005 DECL_INTERFACE_KNOWN (old_result)
2006 = DECL_INTERFACE_KNOWN (new_result);
2007 DECL_DECLARED_INLINE_P (old_result)
2008 = DECL_DECLARED_INLINE_P (new_result);
2009 DECL_DISREGARD_INLINE_LIMITS (old_result)
2010 |= DECL_DISREGARD_INLINE_LIMITS (new_result);
2013 else
2015 DECL_DECLARED_INLINE_P (old_result)
2016 |= DECL_DECLARED_INLINE_P (new_result);
2017 DECL_DISREGARD_INLINE_LIMITS (old_result)
2018 |= DECL_DISREGARD_INLINE_LIMITS (new_result);
2019 check_redeclaration_exception_specification (newdecl, olddecl);
2023 /* If the new declaration is a definition, update the file and
2024 line information on the declaration, and also make
2025 the old declaration the same definition. */
2026 if (DECL_INITIAL (new_result) != NULL_TREE)
2028 DECL_SOURCE_LOCATION (olddecl)
2029 = DECL_SOURCE_LOCATION (old_result)
2030 = DECL_SOURCE_LOCATION (newdecl);
2031 DECL_INITIAL (old_result) = DECL_INITIAL (new_result);
2032 if (DECL_FUNCTION_TEMPLATE_P (newdecl))
2034 tree parm;
2035 DECL_ARGUMENTS (old_result)
2036 = DECL_ARGUMENTS (new_result);
2037 for (parm = DECL_ARGUMENTS (old_result); parm;
2038 parm = DECL_CHAIN (parm))
2039 DECL_CONTEXT (parm) = old_result;
2043 return olddecl;
2046 if (types_match)
2048 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2049 check_redeclaration_exception_specification (newdecl, olddecl);
2051 /* Automatically handles default parameters. */
2052 tree oldtype = TREE_TYPE (olddecl);
2053 tree newtype;
2055 /* For typedefs use the old type, as the new type's DECL_NAME points
2056 at newdecl, which will be ggc_freed. */
2057 if (TREE_CODE (newdecl) == TYPE_DECL)
2059 /* But NEWTYPE might have an attribute, honor that. */
2060 tree tem = TREE_TYPE (newdecl);
2061 newtype = oldtype;
2063 if (TYPE_USER_ALIGN (tem))
2065 if (TYPE_ALIGN (tem) > TYPE_ALIGN (newtype))
2066 SET_TYPE_ALIGN (newtype, TYPE_ALIGN (tem));
2067 TYPE_USER_ALIGN (newtype) = true;
2070 /* And remove the new type from the variants list. */
2071 if (TYPE_NAME (TREE_TYPE (newdecl)) == newdecl)
2073 tree remove = TREE_TYPE (newdecl);
2074 for (tree t = TYPE_MAIN_VARIANT (remove); ;
2075 t = TYPE_NEXT_VARIANT (t))
2076 if (TYPE_NEXT_VARIANT (t) == remove)
2078 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (remove);
2079 break;
2083 else
2084 /* Merge the data types specified in the two decls. */
2085 newtype = merge_types (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
2087 if (VAR_P (newdecl))
2089 DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl);
2090 /* For already initialized vars, TREE_READONLY could have been
2091 cleared in cp_finish_decl, because the var needs runtime
2092 initialization or destruction. Make sure not to set
2093 TREE_READONLY on it again. */
2094 if (DECL_INITIALIZED_P (olddecl)
2095 && !DECL_EXTERNAL (olddecl)
2096 && !TREE_READONLY (olddecl))
2097 TREE_READONLY (newdecl) = 0;
2098 DECL_INITIALIZED_P (newdecl) |= DECL_INITIALIZED_P (olddecl);
2099 DECL_NONTRIVIALLY_INITIALIZED_P (newdecl)
2100 |= DECL_NONTRIVIALLY_INITIALIZED_P (olddecl);
2101 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (newdecl)
2102 |= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (olddecl);
2103 if (DECL_CLASS_SCOPE_P (olddecl))
2104 DECL_DECLARED_CONSTEXPR_P (newdecl)
2105 |= DECL_DECLARED_CONSTEXPR_P (olddecl);
2107 /* Merge the threadprivate attribute from OLDDECL into NEWDECL. */
2108 if (DECL_LANG_SPECIFIC (olddecl)
2109 && CP_DECL_THREADPRIVATE_P (olddecl))
2111 /* Allocate a LANG_SPECIFIC structure for NEWDECL, if needed. */
2112 retrofit_lang_decl (newdecl);
2113 CP_DECL_THREADPRIVATE_P (newdecl) = 1;
2117 /* An explicit specialization of a function template or of a member
2118 function of a class template can be declared transaction_safe
2119 independently of whether the corresponding template entity is declared
2120 transaction_safe. */
2121 if (flag_tm && TREE_CODE (newdecl) == FUNCTION_DECL
2122 && DECL_TEMPLATE_INSTANTIATION (olddecl)
2123 && DECL_TEMPLATE_SPECIALIZATION (newdecl)
2124 && tx_safe_fn_type_p (newtype)
2125 && !tx_safe_fn_type_p (TREE_TYPE (newdecl)))
2126 newtype = tx_unsafe_fn_variant (newtype);
2128 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype;
2130 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2131 check_default_args (newdecl);
2133 /* Lay the type out, unless already done. */
2134 if (! same_type_p (newtype, oldtype)
2135 && TREE_TYPE (newdecl) != error_mark_node
2136 && !(processing_template_decl && uses_template_parms (newdecl)))
2137 layout_type (TREE_TYPE (newdecl));
2139 if ((VAR_P (newdecl)
2140 || TREE_CODE (newdecl) == PARM_DECL
2141 || TREE_CODE (newdecl) == RESULT_DECL
2142 || TREE_CODE (newdecl) == FIELD_DECL
2143 || TREE_CODE (newdecl) == TYPE_DECL)
2144 && !(processing_template_decl && uses_template_parms (newdecl)))
2145 layout_decl (newdecl, 0);
2147 /* Merge the type qualifiers. */
2148 if (TREE_READONLY (newdecl))
2149 TREE_READONLY (olddecl) = 1;
2150 if (TREE_THIS_VOLATILE (newdecl))
2151 TREE_THIS_VOLATILE (olddecl) = 1;
2152 if (TREE_NOTHROW (newdecl))
2153 TREE_NOTHROW (olddecl) = 1;
2155 /* Merge deprecatedness. */
2156 if (TREE_DEPRECATED (newdecl))
2157 TREE_DEPRECATED (olddecl) = 1;
2159 /* Preserve function specific target and optimization options */
2160 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2162 if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
2163 && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
2164 DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
2165 = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
2167 if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
2168 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
2169 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
2170 = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
2173 /* Merge the initialization information. */
2174 if (DECL_INITIAL (newdecl) == NULL_TREE
2175 && DECL_INITIAL (olddecl) != NULL_TREE)
2177 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2178 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2179 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2181 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
2182 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
2186 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2188 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
2189 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
2190 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
2191 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
2192 TREE_NOTHROW (newdecl) |= TREE_NOTHROW (olddecl);
2193 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
2194 DECL_IS_OPERATOR_NEW (newdecl) |= DECL_IS_OPERATOR_NEW (olddecl);
2195 DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
2196 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
2197 DECL_LOOPING_CONST_OR_PURE_P (newdecl)
2198 |= DECL_LOOPING_CONST_OR_PURE_P (olddecl);
2199 /* Keep the old RTL. */
2200 COPY_DECL_RTL (olddecl, newdecl);
2202 else if (VAR_P (newdecl)
2203 && (DECL_SIZE (olddecl) || !DECL_SIZE (newdecl)))
2205 /* Keep the old RTL. We cannot keep the old RTL if the old
2206 declaration was for an incomplete object and the new
2207 declaration is not since many attributes of the RTL will
2208 change. */
2209 COPY_DECL_RTL (olddecl, newdecl);
2212 /* If cannot merge, then use the new type and qualifiers,
2213 and don't preserve the old rtl. */
2214 else
2216 /* Clean out any memory we had of the old declaration. */
2217 tree oldstatic = value_member (olddecl, static_aggregates);
2218 if (oldstatic)
2219 TREE_VALUE (oldstatic) = error_mark_node;
2221 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
2222 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
2223 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
2224 TREE_NOTHROW (olddecl) = TREE_NOTHROW (newdecl);
2225 TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
2228 /* Merge the storage class information. */
2229 merge_weak (newdecl, olddecl);
2231 DECL_DEFER_OUTPUT (newdecl) |= DECL_DEFER_OUTPUT (olddecl);
2232 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
2233 TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl);
2234 if (! DECL_EXTERNAL (olddecl))
2235 DECL_EXTERNAL (newdecl) = 0;
2236 if (! DECL_COMDAT (olddecl))
2237 DECL_COMDAT (newdecl) = 0;
2239 new_template_info = NULL_TREE;
2240 if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl))
2242 bool new_redefines_gnu_inline = false;
2244 if (new_defines_function
2245 && ((DECL_INTERFACE_KNOWN (olddecl)
2246 && TREE_CODE (olddecl) == FUNCTION_DECL)
2247 || (TREE_CODE (olddecl) == TEMPLATE_DECL
2248 && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl))
2249 == FUNCTION_DECL))))
2251 tree fn = olddecl;
2253 if (TREE_CODE (fn) == TEMPLATE_DECL)
2254 fn = DECL_TEMPLATE_RESULT (olddecl);
2256 new_redefines_gnu_inline = GNU_INLINE_P (fn) && DECL_INITIAL (fn);
2259 if (!new_redefines_gnu_inline)
2261 DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl);
2262 DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl);
2263 DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl);
2265 DECL_TEMPLATE_INSTANTIATED (newdecl)
2266 |= DECL_TEMPLATE_INSTANTIATED (olddecl);
2267 DECL_ODR_USED (newdecl) |= DECL_ODR_USED (olddecl);
2269 /* If the OLDDECL is an instantiation and/or specialization,
2270 then the NEWDECL must be too. But, it may not yet be marked
2271 as such if the caller has created NEWDECL, but has not yet
2272 figured out that it is a redeclaration. */
2273 if (!DECL_USE_TEMPLATE (newdecl))
2274 DECL_USE_TEMPLATE (newdecl) = DECL_USE_TEMPLATE (olddecl);
2276 /* Don't really know how much of the language-specific
2277 values we should copy from old to new. */
2278 DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl);
2279 DECL_REPO_AVAILABLE_P (newdecl) = DECL_REPO_AVAILABLE_P (olddecl);
2280 DECL_INITIALIZED_IN_CLASS_P (newdecl)
2281 |= DECL_INITIALIZED_IN_CLASS_P (olddecl);
2283 if (LANG_DECL_HAS_MIN (newdecl))
2285 DECL_LANG_SPECIFIC (newdecl)->u.min.u2 =
2286 DECL_LANG_SPECIFIC (olddecl)->u.min.u2;
2287 if (DECL_TEMPLATE_INFO (newdecl))
2289 new_template_info = DECL_TEMPLATE_INFO (newdecl);
2290 if (DECL_TEMPLATE_INSTANTIATION (olddecl)
2291 && DECL_TEMPLATE_SPECIALIZATION (newdecl))
2292 /* Remember the presence of explicit specialization args. */
2293 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (olddecl))
2294 = TINFO_USED_TEMPLATE_ID (new_template_info);
2296 DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl);
2298 /* Only functions have these fields. */
2299 if (DECL_DECLARES_FUNCTION_P (newdecl))
2301 DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl);
2302 DECL_BEFRIENDING_CLASSES (newdecl)
2303 = chainon (DECL_BEFRIENDING_CLASSES (newdecl),
2304 DECL_BEFRIENDING_CLASSES (olddecl));
2305 /* DECL_THUNKS is only valid for virtual functions,
2306 otherwise it is a DECL_FRIEND_CONTEXT. */
2307 if (DECL_VIRTUAL_P (newdecl))
2308 SET_DECL_THUNKS (newdecl, DECL_THUNKS (olddecl));
2310 /* Only variables have this field. */
2311 else if (VAR_P (newdecl)
2312 && VAR_HAD_UNKNOWN_BOUND (olddecl))
2313 SET_VAR_HAD_UNKNOWN_BOUND (newdecl);
2316 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2318 tree parm;
2320 /* Merge parameter attributes. */
2321 tree oldarg, newarg;
2322 for (oldarg = DECL_ARGUMENTS(olddecl),
2323 newarg = DECL_ARGUMENTS(newdecl);
2324 oldarg && newarg;
2325 oldarg = DECL_CHAIN(oldarg), newarg = DECL_CHAIN(newarg)) {
2326 DECL_ATTRIBUTES (newarg)
2327 = (*targetm.merge_decl_attributes) (oldarg, newarg);
2328 DECL_ATTRIBUTES (oldarg) = DECL_ATTRIBUTES (newarg);
2331 if (DECL_TEMPLATE_INSTANTIATION (olddecl)
2332 && !DECL_TEMPLATE_INSTANTIATION (newdecl))
2334 /* If newdecl is not a specialization, then it is not a
2335 template-related function at all. And that means that we
2336 should have exited above, returning 0. */
2337 gcc_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl));
2339 if (DECL_ODR_USED (olddecl))
2340 /* From [temp.expl.spec]:
2342 If a template, a member template or the member of a class
2343 template is explicitly specialized then that
2344 specialization shall be declared before the first use of
2345 that specialization that would cause an implicit
2346 instantiation to take place, in every translation unit in
2347 which such a use occurs. */
2348 error ("explicit specialization of %qD after first use",
2349 olddecl);
2351 SET_DECL_TEMPLATE_SPECIALIZATION (olddecl);
2352 DECL_COMDAT (newdecl) = (TREE_PUBLIC (newdecl)
2353 && DECL_DECLARED_INLINE_P (newdecl));
2355 /* Don't propagate visibility from the template to the
2356 specialization here. We'll do that in determine_visibility if
2357 appropriate. */
2358 DECL_VISIBILITY_SPECIFIED (olddecl) = 0;
2360 /* [temp.expl.spec/14] We don't inline explicit specialization
2361 just because the primary template says so. */
2363 /* But still keep DECL_DISREGARD_INLINE_LIMITS in sync with
2364 the always_inline attribute. */
2365 if (DECL_DISREGARD_INLINE_LIMITS (olddecl)
2366 && !DECL_DISREGARD_INLINE_LIMITS (newdecl))
2368 if (DECL_DECLARED_INLINE_P (newdecl))
2369 DECL_DISREGARD_INLINE_LIMITS (newdecl) = true;
2370 else
2371 DECL_ATTRIBUTES (newdecl)
2372 = remove_attribute ("always_inline",
2373 DECL_ATTRIBUTES (newdecl));
2376 else if (new_defines_function && DECL_INITIAL (olddecl))
2378 /* Never inline re-defined extern inline functions.
2379 FIXME: this could be better handled by keeping both
2380 function as separate declarations. */
2381 DECL_UNINLINABLE (newdecl) = 1;
2383 else
2385 if (DECL_PENDING_INLINE_P (olddecl))
2387 DECL_PENDING_INLINE_P (newdecl) = 1;
2388 DECL_PENDING_INLINE_INFO (newdecl)
2389 = DECL_PENDING_INLINE_INFO (olddecl);
2391 else if (DECL_PENDING_INLINE_P (newdecl))
2393 else if (DECL_SAVED_FUNCTION_DATA (newdecl) == NULL)
2394 DECL_SAVED_FUNCTION_DATA (newdecl)
2395 = DECL_SAVED_FUNCTION_DATA (olddecl);
2397 DECL_DECLARED_INLINE_P (newdecl) |= DECL_DECLARED_INLINE_P (olddecl);
2399 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
2400 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
2402 DECL_DISREGARD_INLINE_LIMITS (newdecl)
2403 = DECL_DISREGARD_INLINE_LIMITS (olddecl)
2404 = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
2405 || DECL_DISREGARD_INLINE_LIMITS (olddecl));
2408 /* Preserve abstractness on cloned [cd]tors. */
2409 DECL_ABSTRACT_P (newdecl) = DECL_ABSTRACT_P (olddecl);
2411 /* Update newdecl's parms to point at olddecl. */
2412 for (parm = DECL_ARGUMENTS (newdecl); parm;
2413 parm = DECL_CHAIN (parm))
2414 DECL_CONTEXT (parm) = olddecl;
2416 if (! types_match)
2418 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
2419 COPY_DECL_ASSEMBLER_NAME (newdecl, olddecl);
2420 COPY_DECL_RTL (newdecl, olddecl);
2422 if (! types_match || new_defines_function)
2424 /* These need to be copied so that the names are available.
2425 Note that if the types do match, we'll preserve inline
2426 info and other bits, but if not, we won't. */
2427 DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl);
2428 DECL_RESULT (olddecl) = DECL_RESULT (newdecl);
2430 /* If redeclaring a builtin function, it stays built in
2431 if newdecl is a gnu_inline definition, or if newdecl is just
2432 a declaration. */
2433 if (DECL_BUILT_IN (olddecl)
2434 && (new_defines_function ? GNU_INLINE_P (newdecl) : types_match))
2436 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
2437 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
2438 /* If we're keeping the built-in definition, keep the rtl,
2439 regardless of declaration matches. */
2440 COPY_DECL_RTL (olddecl, newdecl);
2441 if (DECL_BUILT_IN_CLASS (newdecl) == BUILT_IN_NORMAL)
2443 enum built_in_function fncode = DECL_FUNCTION_CODE (newdecl);
2444 switch (fncode)
2446 /* If a compatible prototype of these builtin functions
2447 is seen, assume the runtime implements it with the
2448 expected semantics. */
2449 case BUILT_IN_STPCPY:
2450 if (builtin_decl_explicit_p (fncode))
2451 set_builtin_decl_implicit_p (fncode, true);
2452 break;
2453 default:
2454 if (builtin_decl_explicit_p (fncode))
2455 set_builtin_decl_declared_p (fncode, true);
2456 break;
2460 if (new_defines_function)
2461 /* If defining a function declared with other language
2462 linkage, use the previously declared language linkage. */
2463 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
2464 else if (types_match)
2466 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
2467 /* Don't clear out the arguments if we're just redeclaring a
2468 function. */
2469 if (DECL_ARGUMENTS (olddecl))
2470 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
2473 else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
2474 NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl);
2476 /* Now preserve various other info from the definition. */
2477 TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl);
2478 TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl);
2479 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
2480 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
2482 /* Warn about conflicting visibility specifications. */
2483 if (DECL_VISIBILITY_SPECIFIED (olddecl)
2484 && DECL_VISIBILITY_SPECIFIED (newdecl)
2485 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
2487 if (warning_at (DECL_SOURCE_LOCATION (newdecl), OPT_Wattributes,
2488 "%qD: visibility attribute ignored because it "
2489 "conflicts with previous declaration", newdecl))
2490 inform (DECL_SOURCE_LOCATION (olddecl),
2491 "previous declaration of %qD", olddecl);
2493 /* Choose the declaration which specified visibility. */
2494 if (DECL_VISIBILITY_SPECIFIED (olddecl))
2496 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
2497 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
2499 /* Init priority used to be merged from newdecl to olddecl by the memcpy,
2500 so keep this behavior. */
2501 if (VAR_P (newdecl) && DECL_HAS_INIT_PRIORITY_P (newdecl))
2503 SET_DECL_INIT_PRIORITY (olddecl, DECL_INIT_PRIORITY (newdecl));
2504 DECL_HAS_INIT_PRIORITY_P (olddecl) = 1;
2506 /* Likewise for DECL_ALIGN, DECL_USER_ALIGN and DECL_PACKED. */
2507 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
2509 SET_DECL_ALIGN (newdecl, DECL_ALIGN (olddecl));
2510 DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
2512 DECL_USER_ALIGN (olddecl) = DECL_USER_ALIGN (newdecl);
2513 if (TREE_CODE (newdecl) == FIELD_DECL)
2514 DECL_PACKED (olddecl) = DECL_PACKED (newdecl);
2516 /* The DECL_LANG_SPECIFIC information in OLDDECL will be replaced
2517 with that from NEWDECL below. */
2518 if (DECL_LANG_SPECIFIC (olddecl))
2520 gcc_assert (DECL_LANG_SPECIFIC (olddecl)
2521 != DECL_LANG_SPECIFIC (newdecl));
2522 ggc_free (DECL_LANG_SPECIFIC (olddecl));
2525 /* Merge the USED information. */
2526 if (TREE_USED (olddecl))
2527 TREE_USED (newdecl) = 1;
2528 else if (TREE_USED (newdecl))
2529 TREE_USED (olddecl) = 1;
2530 if (VAR_P (newdecl))
2532 if (DECL_READ_P (olddecl))
2533 DECL_READ_P (newdecl) = 1;
2534 else if (DECL_READ_P (newdecl))
2535 DECL_READ_P (olddecl) = 1;
2537 if (DECL_PRESERVE_P (olddecl))
2538 DECL_PRESERVE_P (newdecl) = 1;
2539 else if (DECL_PRESERVE_P (newdecl))
2540 DECL_PRESERVE_P (olddecl) = 1;
2542 /* Merge the DECL_FUNCTION_VERSIONED information. newdecl will be copied
2543 to olddecl and deleted. */
2544 if (TREE_CODE (newdecl) == FUNCTION_DECL
2545 && DECL_FUNCTION_VERSIONED (olddecl))
2547 /* Set the flag for newdecl so that it gets copied to olddecl. */
2548 DECL_FUNCTION_VERSIONED (newdecl) = 1;
2549 /* newdecl will be purged after copying to olddecl and is no longer
2550 a version. */
2551 cgraph_node::delete_function_version (newdecl);
2554 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2556 int function_size;
2557 struct symtab_node *snode = symtab_node::get (olddecl);
2559 function_size = sizeof (struct tree_decl_common);
2561 memcpy ((char *) olddecl + sizeof (struct tree_common),
2562 (char *) newdecl + sizeof (struct tree_common),
2563 function_size - sizeof (struct tree_common));
2565 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2566 (char *) newdecl + sizeof (struct tree_decl_common),
2567 sizeof (struct tree_function_decl) - sizeof (struct tree_decl_common));
2569 /* Preserve symtab node mapping. */
2570 olddecl->decl_with_vis.symtab_node = snode;
2572 if (new_template_info)
2573 /* If newdecl is a template instantiation, it is possible that
2574 the following sequence of events has occurred:
2576 o A friend function was declared in a class template. The
2577 class template was instantiated.
2579 o The instantiation of the friend declaration was
2580 recorded on the instantiation list, and is newdecl.
2582 o Later, however, instantiate_class_template called pushdecl
2583 on the newdecl to perform name injection. But, pushdecl in
2584 turn called duplicate_decls when it discovered that another
2585 declaration of a global function with the same name already
2586 existed.
2588 o Here, in duplicate_decls, we decided to clobber newdecl.
2590 If we're going to do that, we'd better make sure that
2591 olddecl, and not newdecl, is on the list of
2592 instantiations so that if we try to do the instantiation
2593 again we won't get the clobbered declaration. */
2594 reregister_specialization (newdecl,
2595 new_template_info,
2596 olddecl);
2598 else
2600 size_t size = tree_code_size (TREE_CODE (newdecl));
2602 memcpy ((char *) olddecl + sizeof (struct tree_common),
2603 (char *) newdecl + sizeof (struct tree_common),
2604 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
2605 switch (TREE_CODE (newdecl))
2607 case LABEL_DECL:
2608 case VAR_DECL:
2609 case RESULT_DECL:
2610 case PARM_DECL:
2611 case FIELD_DECL:
2612 case TYPE_DECL:
2613 case CONST_DECL:
2615 struct symtab_node *snode = NULL;
2617 if (VAR_P (newdecl)
2618 && (TREE_STATIC (olddecl) || TREE_PUBLIC (olddecl)
2619 || DECL_EXTERNAL (olddecl)))
2620 snode = symtab_node::get (olddecl);
2621 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2622 (char *) newdecl + sizeof (struct tree_decl_common),
2623 size - sizeof (struct tree_decl_common)
2624 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
2625 if (VAR_P (newdecl))
2626 olddecl->decl_with_vis.symtab_node = snode;
2628 break;
2629 default:
2630 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2631 (char *) newdecl + sizeof (struct tree_decl_common),
2632 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common)
2633 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
2634 break;
2638 if (VAR_OR_FUNCTION_DECL_P (newdecl))
2640 if (DECL_EXTERNAL (olddecl)
2641 || TREE_PUBLIC (olddecl)
2642 || TREE_STATIC (olddecl))
2644 /* Merge the section attribute.
2645 We want to issue an error if the sections conflict but that must be
2646 done later in decl_attributes since we are called before attributes
2647 are assigned. */
2648 if (DECL_SECTION_NAME (newdecl) != NULL)
2649 set_decl_section_name (olddecl, DECL_SECTION_NAME (newdecl));
2651 if (DECL_ONE_ONLY (newdecl))
2653 struct symtab_node *oldsym, *newsym;
2654 if (TREE_CODE (olddecl) == FUNCTION_DECL)
2655 oldsym = cgraph_node::get_create (olddecl);
2656 else
2657 oldsym = varpool_node::get_create (olddecl);
2658 newsym = symtab_node::get (newdecl);
2659 oldsym->set_comdat_group (newsym->get_comdat_group ());
2663 if (VAR_P (newdecl)
2664 && CP_DECL_THREAD_LOCAL_P (newdecl))
2666 CP_DECL_THREAD_LOCAL_P (olddecl) = true;
2667 if (!processing_template_decl)
2668 set_decl_tls_model (olddecl, DECL_TLS_MODEL (newdecl));
2672 DECL_UID (olddecl) = olddecl_uid;
2673 if (olddecl_friend)
2674 DECL_FRIEND_P (olddecl) = 1;
2675 if (hidden_friend)
2677 DECL_ANTICIPATED (olddecl) = 1;
2678 DECL_HIDDEN_FRIEND_P (olddecl) = 1;
2681 /* NEWDECL contains the merged attribute lists.
2682 Update OLDDECL to be the same. */
2683 DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
2685 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
2686 so that encode_section_info has a chance to look at the new decl
2687 flags and attributes. */
2688 if (DECL_RTL_SET_P (olddecl)
2689 && (TREE_CODE (olddecl) == FUNCTION_DECL
2690 || (VAR_P (olddecl)
2691 && TREE_STATIC (olddecl))))
2692 make_decl_rtl (olddecl);
2694 /* The NEWDECL will no longer be needed. Because every out-of-class
2695 declaration of a member results in a call to duplicate_decls,
2696 freeing these nodes represents in a significant savings.
2698 Before releasing the node, be sore to remove function from symbol
2699 table that might have been inserted there to record comdat group.
2700 Be sure to however do not free DECL_STRUCT_FUNCTION because this
2701 structure is shared in between newdecl and oldecl. */
2702 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2703 DECL_STRUCT_FUNCTION (newdecl) = NULL;
2704 if (VAR_OR_FUNCTION_DECL_P (newdecl))
2706 struct symtab_node *snode = symtab_node::get (newdecl);
2707 if (snode)
2708 snode->remove ();
2711 /* Remove the associated constraints for newdecl, if any, before
2712 reclaiming memory. */
2713 if (flag_concepts)
2714 remove_constraints (newdecl);
2716 ggc_free (newdecl);
2718 return olddecl;
2721 /* Return zero if the declaration NEWDECL is valid
2722 when the declaration OLDDECL (assumed to be for the same name)
2723 has already been seen.
2724 Otherwise return an error message format string with a %s
2725 where the identifier should go. */
2727 static const char *
2728 redeclaration_error_message (tree newdecl, tree olddecl)
2730 if (TREE_CODE (newdecl) == TYPE_DECL)
2732 /* Because C++ can put things into name space for free,
2733 constructs like "typedef struct foo { ... } foo"
2734 would look like an erroneous redeclaration. */
2735 if (same_type_p (TREE_TYPE (newdecl), TREE_TYPE (olddecl)))
2736 return NULL;
2737 else
2738 return G_("redefinition of %q#D");
2740 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
2742 /* If this is a pure function, its olddecl will actually be
2743 the original initialization to `0' (which we force to call
2744 abort()). Don't complain about redefinition in this case. */
2745 if (DECL_LANG_SPECIFIC (olddecl) && DECL_PURE_VIRTUAL_P (olddecl)
2746 && DECL_INITIAL (olddecl) == NULL_TREE)
2747 return NULL;
2749 /* If both functions come from different namespaces, this is not
2750 a redeclaration - this is a conflict with a used function. */
2751 if (DECL_NAMESPACE_SCOPE_P (olddecl)
2752 && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl)
2753 && ! decls_match (olddecl, newdecl))
2754 return G_("%qD conflicts with used function");
2756 /* We'll complain about linkage mismatches in
2757 warn_extern_redeclared_static. */
2759 /* Defining the same name twice is no good. */
2760 if (decl_defined_p (olddecl)
2761 && decl_defined_p (newdecl))
2763 if (DECL_NAME (olddecl) == NULL_TREE)
2764 return G_("%q#D not declared in class");
2765 else if (!GNU_INLINE_P (olddecl)
2766 || GNU_INLINE_P (newdecl))
2767 return G_("redefinition of %q#D");
2770 if (DECL_DECLARED_INLINE_P (olddecl) && DECL_DECLARED_INLINE_P (newdecl))
2772 bool olda = GNU_INLINE_P (olddecl);
2773 bool newa = GNU_INLINE_P (newdecl);
2775 if (olda != newa)
2777 if (newa)
2778 return G_("%q+D redeclared inline with "
2779 "%<gnu_inline%> attribute");
2780 else
2781 return G_("%q+D redeclared inline without "
2782 "%<gnu_inline%> attribute");
2786 check_abi_tag_redeclaration
2787 (olddecl, lookup_attribute ("abi_tag", DECL_ATTRIBUTES (olddecl)),
2788 lookup_attribute ("abi_tag", DECL_ATTRIBUTES (newdecl)));
2790 return NULL;
2792 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2794 tree nt, ot;
2796 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
2798 if (COMPLETE_TYPE_P (TREE_TYPE (newdecl))
2799 && COMPLETE_TYPE_P (TREE_TYPE (olddecl)))
2800 return G_("redefinition of %q#D");
2801 return NULL;
2804 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) != FUNCTION_DECL
2805 || (DECL_TEMPLATE_RESULT (newdecl)
2806 == DECL_TEMPLATE_RESULT (olddecl)))
2807 return NULL;
2809 nt = DECL_TEMPLATE_RESULT (newdecl);
2810 if (DECL_TEMPLATE_INFO (nt))
2811 nt = DECL_TEMPLATE_RESULT (template_for_substitution (nt));
2812 ot = DECL_TEMPLATE_RESULT (olddecl);
2813 if (DECL_TEMPLATE_INFO (ot))
2814 ot = DECL_TEMPLATE_RESULT (template_for_substitution (ot));
2815 if (DECL_INITIAL (nt) && DECL_INITIAL (ot)
2816 && (!GNU_INLINE_P (ot) || GNU_INLINE_P (nt)))
2817 return G_("redefinition of %q#D");
2819 if (DECL_DECLARED_INLINE_P (ot) && DECL_DECLARED_INLINE_P (nt))
2821 bool olda = GNU_INLINE_P (ot);
2822 bool newa = GNU_INLINE_P (nt);
2824 if (olda != newa)
2826 if (newa)
2827 return G_("%q+D redeclared inline with "
2828 "%<gnu_inline%> attribute");
2829 else
2830 return G_("%q+D redeclared inline without "
2831 "%<gnu_inline%> attribute");
2835 /* Core issue #226 (C++0x):
2837 If a friend function template declaration specifies a
2838 default template-argument, that declaration shall be a
2839 definition and shall be the only declaration of the
2840 function template in the translation unit. */
2841 if ((cxx_dialect != cxx98)
2842 && TREE_CODE (ot) == FUNCTION_DECL && DECL_FRIEND_P (ot)
2843 && !check_default_tmpl_args (nt, DECL_TEMPLATE_PARMS (newdecl),
2844 /*is_primary=*/true,
2845 /*is_partial=*/false,
2846 /*is_friend_decl=*/2))
2847 return G_("redeclaration of friend %q#D "
2848 "may not have default template arguments");
2850 return NULL;
2852 else if (VAR_P (newdecl)
2853 && CP_DECL_THREAD_LOCAL_P (newdecl) != CP_DECL_THREAD_LOCAL_P (olddecl)
2854 && (! DECL_LANG_SPECIFIC (olddecl)
2855 || ! CP_DECL_THREADPRIVATE_P (olddecl)
2856 || CP_DECL_THREAD_LOCAL_P (newdecl)))
2858 /* Only variables can be thread-local, and all declarations must
2859 agree on this property. */
2860 if (CP_DECL_THREAD_LOCAL_P (newdecl))
2861 return G_("thread-local declaration of %q#D follows "
2862 "non-thread-local declaration");
2863 else
2864 return G_("non-thread-local declaration of %q#D follows "
2865 "thread-local declaration");
2867 else if (toplevel_bindings_p () || DECL_NAMESPACE_SCOPE_P (newdecl))
2869 /* The objects have been declared at namespace scope. If either
2870 is a member of an anonymous union, then this is an invalid
2871 redeclaration. For example:
2873 int i;
2874 union { int i; };
2876 is invalid. */
2877 if ((VAR_P (newdecl) && DECL_ANON_UNION_VAR_P (newdecl))
2878 || (VAR_P (olddecl) && DECL_ANON_UNION_VAR_P (olddecl)))
2879 return G_("redeclaration of %q#D");
2880 /* If at least one declaration is a reference, there is no
2881 conflict. For example:
2883 int i = 3;
2884 extern int i;
2886 is valid. */
2887 if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
2888 return NULL;
2890 /* Static data member declared outside a class definition
2891 if the variable is defined within the class with constexpr
2892 specifier is declaration rather than definition (and
2893 deprecated). */
2894 if (cxx_dialect >= cxx1z
2895 && DECL_CLASS_SCOPE_P (olddecl)
2896 && DECL_DECLARED_CONSTEXPR_P (olddecl)
2897 && !DECL_INITIAL (newdecl))
2899 DECL_EXTERNAL (newdecl) = 1;
2900 /* For now, only warn with explicit -Wdeprecated. */
2901 if (global_options_set.x_warn_deprecated
2902 && warning_at (DECL_SOURCE_LOCATION (newdecl), OPT_Wdeprecated,
2903 "redundant redeclaration of %<constexpr%> static "
2904 "data member %qD", newdecl))
2905 inform (DECL_SOURCE_LOCATION (olddecl),
2906 "previous declaration of %qD", olddecl);
2907 return NULL;
2910 /* Reject two definitions. */
2911 return G_("redefinition of %q#D");
2913 else
2915 /* Objects declared with block scope: */
2916 /* Reject two definitions, and reject a definition
2917 together with an external reference. */
2918 if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl)))
2919 return G_("redeclaration of %q#D");
2920 return NULL;
2924 /* Hash and equality functions for the named_label table. */
2926 hashval_t
2927 named_label_hasher::hash (named_label_entry *ent)
2929 return DECL_UID (ent->label_decl);
2932 bool
2933 named_label_hasher::equal (named_label_entry *a, named_label_entry *b)
2935 return a->label_decl == b->label_decl;
2938 /* Create a new label, named ID. */
2940 static tree
2941 make_label_decl (tree id, int local_p)
2943 struct named_label_entry *ent;
2944 tree decl;
2946 decl = build_decl (input_location, LABEL_DECL, id, void_type_node);
2948 DECL_CONTEXT (decl) = current_function_decl;
2949 SET_DECL_MODE (decl, VOIDmode);
2950 C_DECLARED_LABEL_FLAG (decl) = local_p;
2952 /* Say where one reference is to the label, for the sake of the
2953 error if it is not defined. */
2954 DECL_SOURCE_LOCATION (decl) = input_location;
2956 /* Record the fact that this identifier is bound to this label. */
2957 SET_IDENTIFIER_LABEL_VALUE (id, decl);
2959 /* Create the label htab for the function on demand. */
2960 if (!named_labels)
2961 named_labels = hash_table<named_label_hasher>::create_ggc (13);
2963 /* Record this label on the list of labels used in this function.
2964 We do this before calling make_label_decl so that we get the
2965 IDENTIFIER_LABEL_VALUE before the new label is declared. */
2966 ent = ggc_cleared_alloc<named_label_entry> ();
2967 ent->label_decl = decl;
2969 named_label_entry **slot = named_labels->find_slot (ent, INSERT);
2970 gcc_assert (*slot == NULL);
2971 *slot = ent;
2973 return decl;
2976 /* Look for a label named ID in the current function. If one cannot
2977 be found, create one. (We keep track of used, but undefined,
2978 labels, and complain about them at the end of a function.) */
2980 static tree
2981 lookup_label_1 (tree id)
2983 tree decl;
2985 /* You can't use labels at global scope. */
2986 if (current_function_decl == NULL_TREE)
2988 error ("label %qE referenced outside of any function", id);
2989 return NULL_TREE;
2992 /* See if we've already got this label. */
2993 decl = IDENTIFIER_LABEL_VALUE (id);
2994 if (decl != NULL_TREE && DECL_CONTEXT (decl) == current_function_decl)
2995 return decl;
2997 decl = make_label_decl (id, /*local_p=*/0);
2998 return decl;
3001 /* Wrapper for lookup_label_1. */
3003 tree
3004 lookup_label (tree id)
3006 tree ret;
3007 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3008 ret = lookup_label_1 (id);
3009 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3010 return ret;
3013 /* Declare a local label named ID. */
3015 tree
3016 declare_local_label (tree id)
3018 tree decl;
3019 cp_label_binding bind;
3021 /* Add a new entry to the SHADOWED_LABELS list so that when we leave
3022 this scope we can restore the old value of IDENTIFIER_TYPE_VALUE. */
3023 bind.prev_value = IDENTIFIER_LABEL_VALUE (id);
3025 decl = make_label_decl (id, /*local_p=*/1);
3026 bind.label = decl;
3027 vec_safe_push (current_binding_level->shadowed_labels, bind);
3029 return decl;
3032 /* Returns nonzero if it is ill-formed to jump past the declaration of
3033 DECL. Returns 2 if it's also a real problem. */
3035 static int
3036 decl_jump_unsafe (tree decl)
3038 /* [stmt.dcl]/3: A program that jumps from a point where a local variable
3039 with automatic storage duration is not in scope to a point where it is
3040 in scope is ill-formed unless the variable has scalar type, class type
3041 with a trivial default constructor and a trivial destructor, a
3042 cv-qualified version of one of these types, or an array of one of the
3043 preceding types and is declared without an initializer (8.5). */
3044 tree type = TREE_TYPE (decl);
3046 if (!VAR_P (decl) || TREE_STATIC (decl)
3047 || type == error_mark_node)
3048 return 0;
3050 if (DECL_NONTRIVIALLY_INITIALIZED_P (decl)
3051 || variably_modified_type_p (type, NULL_TREE))
3052 return 2;
3054 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3055 return 1;
3057 return 0;
3060 /* A subroutine of check_previous_goto_1 and check_goto to identify a branch
3061 to the user. */
3063 static bool
3064 identify_goto (tree decl, location_t loc, const location_t *locus,
3065 diagnostic_t diag_kind)
3067 bool complained
3068 = (decl ? emit_diagnostic (diag_kind, loc, 0, "jump to label %qD", decl)
3069 : emit_diagnostic (diag_kind, loc, 0, "jump to case label"));
3070 if (complained && locus)
3071 inform (*locus, " from here");
3072 return complained;
3075 /* Check that a single previously seen jump to a newly defined label
3076 is OK. DECL is the LABEL_DECL or 0; LEVEL is the binding_level for
3077 the jump context; NAMES are the names in scope in LEVEL at the jump
3078 context; LOCUS is the source position of the jump or 0. Returns
3079 true if all is well. */
3081 static bool
3082 check_previous_goto_1 (tree decl, cp_binding_level* level, tree names,
3083 bool exited_omp, const location_t *locus)
3085 cp_binding_level *b;
3086 bool complained = false;
3087 int identified = 0;
3088 bool saw_eh = false, saw_omp = false, saw_tm = false, saw_cxif = false;
3090 if (exited_omp)
3092 complained = identify_goto (decl, input_location, locus, DK_ERROR);
3093 if (complained)
3094 inform (input_location, " exits OpenMP structured block");
3095 saw_omp = true;
3096 identified = 2;
3099 for (b = current_binding_level; b ; b = b->level_chain)
3101 tree new_decls, old_decls = (b == level ? names : NULL_TREE);
3103 for (new_decls = b->names; new_decls != old_decls;
3104 new_decls = (DECL_P (new_decls) ? DECL_CHAIN (new_decls)
3105 : TREE_CHAIN (new_decls)))
3107 int problem = decl_jump_unsafe (new_decls);
3108 if (! problem)
3109 continue;
3111 if (!identified)
3113 complained = identify_goto (decl, input_location, locus,
3114 DK_PERMERROR);
3115 identified = 1;
3117 if (complained)
3119 if (problem > 1)
3120 inform (DECL_SOURCE_LOCATION (new_decls),
3121 " crosses initialization of %q#D", new_decls);
3122 else
3123 inform (DECL_SOURCE_LOCATION (new_decls),
3124 " enters scope of %q#D which has "
3125 "non-trivial destructor", new_decls);
3129 if (b == level)
3130 break;
3131 if ((b->kind == sk_try || b->kind == sk_catch) && !saw_eh)
3133 if (identified < 2)
3135 complained = identify_goto (decl, input_location, locus,
3136 DK_ERROR);
3137 identified = 2;
3139 if (complained)
3141 if (b->kind == sk_try)
3142 inform (input_location, " enters try block");
3143 else
3144 inform (input_location, " enters catch block");
3146 saw_eh = true;
3148 if (b->kind == sk_omp && !saw_omp)
3150 if (identified < 2)
3152 complained = identify_goto (decl, input_location, locus,
3153 DK_ERROR);
3154 identified = 2;
3156 if (complained)
3157 inform (input_location, " enters OpenMP structured block");
3158 saw_omp = true;
3160 if (b->kind == sk_transaction && !saw_tm)
3162 if (identified < 2)
3164 complained = identify_goto (decl, input_location, locus,
3165 DK_ERROR);
3166 identified = 2;
3168 if (complained)
3169 inform (input_location,
3170 " enters synchronized or atomic statement");
3171 saw_tm = true;
3173 if (!saw_cxif && b->kind == sk_block
3174 && level_for_constexpr_if (b->level_chain))
3176 if (identified < 2)
3178 complained = identify_goto (decl, input_location, locus,
3179 DK_ERROR);
3180 identified = 2;
3182 if (complained)
3183 inform (EXPR_LOCATION (b->level_chain->this_entity),
3184 " enters constexpr if statement");
3185 saw_cxif = true;
3189 return !identified;
3192 static void
3193 check_previous_goto (tree decl, struct named_label_use_entry *use)
3195 check_previous_goto_1 (decl, use->binding_level,
3196 use->names_in_scope, use->in_omp_scope,
3197 &use->o_goto_locus);
3200 static bool
3201 check_switch_goto (cp_binding_level* level)
3203 return check_previous_goto_1 (NULL_TREE, level, level->names, false, NULL);
3206 /* Check that a new jump to a label DECL is OK. Called by
3207 finish_goto_stmt. */
3209 void
3210 check_goto (tree decl)
3212 struct named_label_entry *ent, dummy;
3213 bool saw_catch = false, complained = false;
3214 int identified = 0;
3215 tree bad;
3216 unsigned ix;
3218 /* We can't know where a computed goto is jumping.
3219 So we assume that it's OK. */
3220 if (TREE_CODE (decl) != LABEL_DECL)
3221 return;
3223 /* We didn't record any information about this label when we created it,
3224 and there's not much point since it's trivial to analyze as a return. */
3225 if (decl == cdtor_label)
3226 return;
3228 dummy.label_decl = decl;
3229 ent = named_labels->find (&dummy);
3230 gcc_assert (ent != NULL);
3232 /* If the label hasn't been defined yet, defer checking. */
3233 if (! DECL_INITIAL (decl))
3235 struct named_label_use_entry *new_use;
3237 /* Don't bother creating another use if the last goto had the
3238 same data, and will therefore create the same set of errors. */
3239 if (ent->uses
3240 && ent->uses->names_in_scope == current_binding_level->names)
3241 return;
3243 new_use = ggc_alloc<named_label_use_entry> ();
3244 new_use->binding_level = current_binding_level;
3245 new_use->names_in_scope = current_binding_level->names;
3246 new_use->o_goto_locus = input_location;
3247 new_use->in_omp_scope = false;
3249 new_use->next = ent->uses;
3250 ent->uses = new_use;
3251 return;
3254 if (ent->in_try_scope || ent->in_catch_scope || ent->in_transaction_scope
3255 || ent->in_constexpr_if
3256 || ent->in_omp_scope || !vec_safe_is_empty (ent->bad_decls))
3258 diagnostic_t diag_kind = DK_PERMERROR;
3259 if (ent->in_try_scope || ent->in_catch_scope || ent->in_constexpr_if
3260 || ent->in_transaction_scope || ent->in_omp_scope)
3261 diag_kind = DK_ERROR;
3262 complained = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
3263 &input_location, diag_kind);
3264 identified = 1 + (diag_kind == DK_ERROR);
3267 FOR_EACH_VEC_SAFE_ELT (ent->bad_decls, ix, bad)
3269 int u = decl_jump_unsafe (bad);
3271 if (u > 1 && DECL_ARTIFICIAL (bad))
3273 /* Can't skip init of __exception_info. */
3274 if (identified == 1)
3276 complained = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
3277 &input_location, DK_ERROR);
3278 identified = 2;
3280 if (complained)
3281 inform (DECL_SOURCE_LOCATION (bad), " enters catch block");
3282 saw_catch = true;
3284 else if (complained)
3286 if (u > 1)
3287 inform (DECL_SOURCE_LOCATION (bad),
3288 " skips initialization of %q#D", bad);
3289 else
3290 inform (DECL_SOURCE_LOCATION (bad),
3291 " enters scope of %q#D which has "
3292 "non-trivial destructor", bad);
3296 if (complained)
3298 if (ent->in_try_scope)
3299 inform (input_location, " enters try block");
3300 else if (ent->in_catch_scope && !saw_catch)
3301 inform (input_location, " enters catch block");
3302 else if (ent->in_transaction_scope)
3303 inform (input_location, " enters synchronized or atomic statement");
3304 else if (ent->in_constexpr_if)
3305 inform (input_location, " enters constexpr if statement");
3308 if (ent->in_omp_scope)
3310 if (complained)
3311 inform (input_location, " enters OpenMP structured block");
3313 else if (flag_openmp)
3315 cp_binding_level *b;
3316 for (b = current_binding_level; b ; b = b->level_chain)
3318 if (b == ent->binding_level)
3319 break;
3320 if (b->kind == sk_omp)
3322 if (identified < 2)
3324 complained = identify_goto (decl,
3325 DECL_SOURCE_LOCATION (decl),
3326 &input_location, DK_ERROR);
3327 identified = 2;
3329 if (complained)
3330 inform (input_location, " exits OpenMP structured block");
3331 break;
3337 /* Check that a return is ok wrt OpenMP structured blocks.
3338 Called by finish_return_stmt. Returns true if all is well. */
3340 bool
3341 check_omp_return (void)
3343 cp_binding_level *b;
3344 for (b = current_binding_level; b ; b = b->level_chain)
3345 if (b->kind == sk_omp)
3347 error ("invalid exit from OpenMP structured block");
3348 return false;
3350 else if (b->kind == sk_function_parms)
3351 break;
3352 return true;
3355 /* Define a label, specifying the location in the source file.
3356 Return the LABEL_DECL node for the label. */
3358 static tree
3359 define_label_1 (location_t location, tree name)
3361 struct named_label_entry *ent, dummy;
3362 cp_binding_level *p;
3363 tree decl;
3365 decl = lookup_label (name);
3367 dummy.label_decl = decl;
3368 ent = named_labels->find (&dummy);
3369 gcc_assert (ent != NULL);
3371 /* After labels, make any new cleanups in the function go into their
3372 own new (temporary) binding contour. */
3373 for (p = current_binding_level;
3374 p->kind != sk_function_parms;
3375 p = p->level_chain)
3376 p->more_cleanups_ok = 0;
3378 if (name == get_identifier ("wchar_t"))
3379 permerror (input_location, "label named wchar_t");
3381 if (DECL_INITIAL (decl) != NULL_TREE)
3383 error ("duplicate label %qD", decl);
3384 return error_mark_node;
3386 else
3388 struct named_label_use_entry *use;
3390 /* Mark label as having been defined. */
3391 DECL_INITIAL (decl) = error_mark_node;
3392 /* Say where in the source. */
3393 DECL_SOURCE_LOCATION (decl) = location;
3395 ent->binding_level = current_binding_level;
3396 ent->names_in_scope = current_binding_level->names;
3398 for (use = ent->uses; use ; use = use->next)
3399 check_previous_goto (decl, use);
3400 ent->uses = NULL;
3403 return decl;
3406 /* Wrapper for define_label_1. */
3408 tree
3409 define_label (location_t location, tree name)
3411 tree ret;
3412 bool running = timevar_cond_start (TV_NAME_LOOKUP);
3413 ret = define_label_1 (location, name);
3414 timevar_cond_stop (TV_NAME_LOOKUP, running);
3415 return ret;
3419 struct cp_switch
3421 cp_binding_level *level;
3422 struct cp_switch *next;
3423 /* The SWITCH_STMT being built. */
3424 tree switch_stmt;
3425 /* A splay-tree mapping the low element of a case range to the high
3426 element, or NULL_TREE if there is no high element. Used to
3427 determine whether or not a new case label duplicates an old case
3428 label. We need a tree, rather than simply a hash table, because
3429 of the GNU case range extension. */
3430 splay_tree cases;
3431 /* Remember whether there was a case value that is outside the
3432 range of the original type of the controlling expression. */
3433 bool outside_range_p;
3436 /* A stack of the currently active switch statements. The innermost
3437 switch statement is on the top of the stack. There is no need to
3438 mark the stack for garbage collection because it is only active
3439 during the processing of the body of a function, and we never
3440 collect at that point. */
3442 static struct cp_switch *switch_stack;
3444 /* Called right after a switch-statement condition is parsed.
3445 SWITCH_STMT is the switch statement being parsed. */
3447 void
3448 push_switch (tree switch_stmt)
3450 struct cp_switch *p = XNEW (struct cp_switch);
3451 p->level = current_binding_level;
3452 p->next = switch_stack;
3453 p->switch_stmt = switch_stmt;
3454 p->cases = splay_tree_new (case_compare, NULL, NULL);
3455 p->outside_range_p = false;
3456 switch_stack = p;
3459 void
3460 pop_switch (void)
3462 struct cp_switch *cs = switch_stack;
3463 location_t switch_location;
3465 /* Emit warnings as needed. */
3466 switch_location = EXPR_LOC_OR_LOC (cs->switch_stmt, input_location);
3467 const bool bool_cond_p
3468 = (SWITCH_STMT_TYPE (cs->switch_stmt)
3469 && TREE_CODE (SWITCH_STMT_TYPE (cs->switch_stmt)) == BOOLEAN_TYPE);
3470 if (!processing_template_decl)
3471 c_do_switch_warnings (cs->cases, switch_location,
3472 SWITCH_STMT_TYPE (cs->switch_stmt),
3473 SWITCH_STMT_COND (cs->switch_stmt),
3474 bool_cond_p, cs->outside_range_p);
3476 splay_tree_delete (cs->cases);
3477 switch_stack = switch_stack->next;
3478 free (cs);
3481 /* Convert a case constant VALUE in a switch to the type TYPE of the switch
3482 condition. Note that if TYPE and VALUE are already integral we don't
3483 really do the conversion because the language-independent
3484 warning/optimization code will work better that way. */
3486 static tree
3487 case_conversion (tree type, tree value)
3489 if (value == NULL_TREE)
3490 return value;
3492 if (cxx_dialect >= cxx11
3493 && (SCOPED_ENUM_P (type)
3494 || !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (value))))
3496 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
3497 type = type_promotes_to (type);
3498 value = (perform_implicit_conversion_flags
3499 (type, value, tf_warning_or_error,
3500 LOOKUP_IMPLICIT | LOOKUP_NO_NON_INTEGRAL));
3502 return cxx_constant_value (value);
3505 /* Note that we've seen a definition of a case label, and complain if this
3506 is a bad place for one. */
3508 tree
3509 finish_case_label (location_t loc, tree low_value, tree high_value)
3511 tree cond, r;
3512 cp_binding_level *p;
3513 tree type;
3515 if (processing_template_decl)
3517 tree label;
3519 /* For templates, just add the case label; we'll do semantic
3520 analysis at instantiation-time. */
3521 label = build_decl (loc, LABEL_DECL, NULL_TREE, NULL_TREE);
3522 return add_stmt (build_case_label (low_value, high_value, label));
3525 /* Find the condition on which this switch statement depends. */
3526 cond = SWITCH_STMT_COND (switch_stack->switch_stmt);
3527 if (cond && TREE_CODE (cond) == TREE_LIST)
3528 cond = TREE_VALUE (cond);
3530 if (!check_switch_goto (switch_stack->level))
3531 return error_mark_node;
3533 type = SWITCH_STMT_TYPE (switch_stack->switch_stmt);
3535 low_value = case_conversion (type, low_value);
3536 high_value = case_conversion (type, high_value);
3538 r = c_add_case_label (loc, switch_stack->cases, cond, type,
3539 low_value, high_value,
3540 &switch_stack->outside_range_p);
3542 /* After labels, make any new cleanups in the function go into their
3543 own new (temporary) binding contour. */
3544 for (p = current_binding_level;
3545 p->kind != sk_function_parms;
3546 p = p->level_chain)
3547 p->more_cleanups_ok = 0;
3549 return r;
3552 struct typename_info {
3553 tree scope;
3554 tree name;
3555 tree template_id;
3556 bool enum_p;
3557 bool class_p;
3560 struct typename_hasher : ggc_ptr_hash<tree_node>
3562 typedef typename_info *compare_type;
3564 /* Hash a TYPENAME_TYPE. */
3566 static hashval_t
3567 hash (tree t)
3569 hashval_t hash;
3571 hash = (htab_hash_pointer (TYPE_CONTEXT (t))
3572 ^ htab_hash_pointer (TYPE_IDENTIFIER (t)));
3574 return hash;
3577 /* Compare two TYPENAME_TYPEs. */
3579 static bool
3580 equal (tree t1, const typename_info *t2)
3582 return (TYPE_IDENTIFIER (t1) == t2->name
3583 && TYPE_CONTEXT (t1) == t2->scope
3584 && TYPENAME_TYPE_FULLNAME (t1) == t2->template_id
3585 && TYPENAME_IS_ENUM_P (t1) == t2->enum_p
3586 && TYPENAME_IS_CLASS_P (t1) == t2->class_p);
3590 /* Build a TYPENAME_TYPE. If the type is `typename T::t', CONTEXT is
3591 the type of `T', NAME is the IDENTIFIER_NODE for `t'.
3593 Returns the new TYPENAME_TYPE. */
3595 static GTY (()) hash_table<typename_hasher> *typename_htab;
3597 static tree
3598 build_typename_type (tree context, tree name, tree fullname,
3599 enum tag_types tag_type)
3601 tree t;
3602 tree d;
3603 typename_info ti;
3604 tree *e;
3605 hashval_t hash;
3607 if (typename_htab == NULL)
3608 typename_htab = hash_table<typename_hasher>::create_ggc (61);
3610 ti.scope = FROB_CONTEXT (context);
3611 ti.name = name;
3612 ti.template_id = fullname;
3613 ti.enum_p = tag_type == enum_type;
3614 ti.class_p = (tag_type == class_type
3615 || tag_type == record_type
3616 || tag_type == union_type);
3617 hash = (htab_hash_pointer (ti.scope)
3618 ^ htab_hash_pointer (ti.name));
3620 /* See if we already have this type. */
3621 e = typename_htab->find_slot_with_hash (&ti, hash, INSERT);
3622 if (*e)
3623 t = *e;
3624 else
3626 /* Build the TYPENAME_TYPE. */
3627 t = cxx_make_type (TYPENAME_TYPE);
3628 TYPE_CONTEXT (t) = ti.scope;
3629 TYPENAME_TYPE_FULLNAME (t) = ti.template_id;
3630 TYPENAME_IS_ENUM_P (t) = ti.enum_p;
3631 TYPENAME_IS_CLASS_P (t) = ti.class_p;
3633 /* Build the corresponding TYPE_DECL. */
3634 d = build_decl (input_location, TYPE_DECL, name, t);
3635 TYPE_NAME (TREE_TYPE (d)) = d;
3636 TYPE_STUB_DECL (TREE_TYPE (d)) = d;
3637 DECL_CONTEXT (d) = FROB_CONTEXT (context);
3638 DECL_ARTIFICIAL (d) = 1;
3640 /* Store it in the hash table. */
3641 *e = t;
3643 /* TYPENAME_TYPEs must always be compared structurally, because
3644 they may or may not resolve down to another type depending on
3645 the currently open classes. */
3646 SET_TYPE_STRUCTURAL_EQUALITY (t);
3649 return t;
3652 /* Resolve `typename CONTEXT::NAME'. TAG_TYPE indicates the tag
3653 provided to name the type. Returns an appropriate type, unless an
3654 error occurs, in which case error_mark_node is returned. If we
3655 locate a non-artificial TYPE_DECL and TF_KEEP_TYPE_DECL is set, we
3656 return that, rather than the _TYPE it corresponds to, in other
3657 cases we look through the type decl. If TF_ERROR is set, complain
3658 about errors, otherwise be quiet. */
3660 tree
3661 make_typename_type (tree context, tree name, enum tag_types tag_type,
3662 tsubst_flags_t complain)
3664 tree fullname;
3665 tree t;
3666 bool want_template;
3668 if (name == error_mark_node
3669 || context == NULL_TREE
3670 || context == error_mark_node)
3671 return error_mark_node;
3673 if (TYPE_P (name))
3675 if (!(TYPE_LANG_SPECIFIC (name)
3676 && (CLASSTYPE_IS_TEMPLATE (name)
3677 || CLASSTYPE_USE_TEMPLATE (name))))
3678 name = TYPE_IDENTIFIER (name);
3679 else
3680 /* Create a TEMPLATE_ID_EXPR for the type. */
3681 name = build_nt (TEMPLATE_ID_EXPR,
3682 CLASSTYPE_TI_TEMPLATE (name),
3683 CLASSTYPE_TI_ARGS (name));
3685 else if (TREE_CODE (name) == TYPE_DECL)
3686 name = DECL_NAME (name);
3688 fullname = name;
3690 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3692 name = TREE_OPERAND (name, 0);
3693 if (DECL_TYPE_TEMPLATE_P (name))
3694 name = TREE_OPERAND (fullname, 0) = DECL_NAME (name);
3695 if (TREE_CODE (name) != IDENTIFIER_NODE)
3697 if (complain & tf_error)
3698 error ("%qD is not a type", name);
3699 return error_mark_node;
3702 if (TREE_CODE (name) == TEMPLATE_DECL)
3704 if (complain & tf_error)
3705 error ("%qD used without template parameters", name);
3706 return error_mark_node;
3708 gcc_assert (identifier_p (name));
3709 gcc_assert (TYPE_P (context));
3711 if (!MAYBE_CLASS_TYPE_P (context))
3713 if (complain & tf_error)
3714 error ("%q#T is not a class", context);
3715 return error_mark_node;
3718 /* When the CONTEXT is a dependent type, NAME could refer to a
3719 dependent base class of CONTEXT. But look inside it anyway
3720 if CONTEXT is a currently open scope, in case it refers to a
3721 member of the current instantiation or a non-dependent base;
3722 lookup will stop when we hit a dependent base. */
3723 if (!dependent_scope_p (context))
3724 /* We should only set WANT_TYPE when we're a nested typename type.
3725 Then we can give better diagnostics if we find a non-type. */
3726 t = lookup_field (context, name, 2, /*want_type=*/true);
3727 else
3728 t = NULL_TREE;
3730 if ((!t || TREE_CODE (t) == TREE_LIST) && dependent_type_p (context))
3731 return build_typename_type (context, name, fullname, tag_type);
3733 want_template = TREE_CODE (fullname) == TEMPLATE_ID_EXPR;
3735 if (!t)
3737 if (complain & tf_error)
3739 if (!COMPLETE_TYPE_P (context))
3740 cxx_incomplete_type_error (NULL_TREE, context);
3741 else
3742 error (want_template ? G_("no class template named %q#T in %q#T")
3743 : G_("no type named %q#T in %q#T"), name, context);
3745 return error_mark_node;
3748 /* Pull out the template from an injected-class-name (or multiple). */
3749 if (want_template)
3750 t = maybe_get_template_decl_from_type_decl (t);
3752 if (TREE_CODE (t) == TREE_LIST)
3754 if (complain & tf_error)
3756 error ("lookup of %qT in %qT is ambiguous", name, context);
3757 print_candidates (t);
3759 return error_mark_node;
3762 if (want_template && !DECL_TYPE_TEMPLATE_P (t))
3764 if (complain & tf_error)
3765 error ("%<typename %T::%D%> names %q#T, which is not a class template",
3766 context, name, t);
3767 return error_mark_node;
3769 if (!want_template && TREE_CODE (t) != TYPE_DECL)
3771 if (complain & tf_error)
3772 error ("%<typename %T::%D%> names %q#T, which is not a type",
3773 context, name, t);
3774 return error_mark_node;
3777 if (!perform_or_defer_access_check (TYPE_BINFO (context), t, t, complain))
3778 return error_mark_node;
3780 /* If we are currently parsing a template and if T is a typedef accessed
3781 through CONTEXT then we need to remember and check access of T at
3782 template instantiation time. */
3783 add_typedef_to_current_template_for_access_check (t, context, input_location);
3785 if (want_template)
3786 return lookup_template_class (t, TREE_OPERAND (fullname, 1),
3787 NULL_TREE, context,
3788 /*entering_scope=*/0,
3789 complain | tf_user);
3791 if (DECL_ARTIFICIAL (t) || !(complain & tf_keep_type_decl))
3792 t = TREE_TYPE (t);
3794 maybe_record_typedef_use (t);
3796 return t;
3799 /* Resolve `CONTEXT::template NAME'. Returns a TEMPLATE_DECL if the name
3800 can be resolved or an UNBOUND_CLASS_TEMPLATE, unless an error occurs,
3801 in which case error_mark_node is returned.
3803 If PARM_LIST is non-NULL, also make sure that the template parameter
3804 list of TEMPLATE_DECL matches.
3806 If COMPLAIN zero, don't complain about any errors that occur. */
3808 tree
3809 make_unbound_class_template (tree context, tree name, tree parm_list,
3810 tsubst_flags_t complain)
3812 tree t;
3813 tree d;
3815 if (TYPE_P (name))
3816 name = TYPE_IDENTIFIER (name);
3817 else if (DECL_P (name))
3818 name = DECL_NAME (name);
3819 gcc_assert (identifier_p (name));
3821 if (!dependent_type_p (context)
3822 || currently_open_class (context))
3824 tree tmpl = NULL_TREE;
3826 if (MAYBE_CLASS_TYPE_P (context))
3827 tmpl = lookup_field (context, name, 0, false);
3829 if (tmpl && TREE_CODE (tmpl) == TYPE_DECL)
3830 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
3832 if (!tmpl || !DECL_TYPE_TEMPLATE_P (tmpl))
3834 if (complain & tf_error)
3835 error ("no class template named %q#T in %q#T", name, context);
3836 return error_mark_node;
3839 if (parm_list
3840 && !comp_template_parms (DECL_TEMPLATE_PARMS (tmpl), parm_list))
3842 if (complain & tf_error)
3844 error ("template parameters do not match template %qD", tmpl);
3845 inform (DECL_SOURCE_LOCATION (tmpl),
3846 "%qD declared here", tmpl);
3848 return error_mark_node;
3851 if (!perform_or_defer_access_check (TYPE_BINFO (context), tmpl, tmpl,
3852 complain))
3853 return error_mark_node;
3855 return tmpl;
3858 /* Build the UNBOUND_CLASS_TEMPLATE. */
3859 t = cxx_make_type (UNBOUND_CLASS_TEMPLATE);
3860 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
3861 TREE_TYPE (t) = NULL_TREE;
3862 SET_TYPE_STRUCTURAL_EQUALITY (t);
3864 /* Build the corresponding TEMPLATE_DECL. */
3865 d = build_decl (input_location, TEMPLATE_DECL, name, t);
3866 TYPE_NAME (TREE_TYPE (d)) = d;
3867 TYPE_STUB_DECL (TREE_TYPE (d)) = d;
3868 DECL_CONTEXT (d) = FROB_CONTEXT (context);
3869 DECL_ARTIFICIAL (d) = 1;
3870 DECL_TEMPLATE_PARMS (d) = parm_list;
3872 return t;
3877 /* Push the declarations of builtin types into the global namespace.
3878 RID_INDEX is the index of the builtin type in the array
3879 RID_POINTERS. NAME is the name used when looking up the builtin
3880 type. TYPE is the _TYPE node for the builtin type. */
3882 void
3883 record_builtin_type (enum rid rid_index,
3884 const char* name,
3885 tree type)
3887 tree rname = NULL_TREE, tname = NULL_TREE;
3888 tree tdecl = NULL_TREE;
3890 if ((int) rid_index < (int) RID_MAX)
3891 rname = ridpointers[(int) rid_index];
3892 if (name)
3893 tname = get_identifier (name);
3895 /* The calls to SET_IDENTIFIER_GLOBAL_VALUE below should be
3896 eliminated. Built-in types should not be looked up name; their
3897 names are keywords that the parser can recognize. However, there
3898 is code in c-common.c that uses identifier_global_value to look
3899 up built-in types by name. */
3900 if (tname)
3902 tdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, tname, type);
3903 DECL_ARTIFICIAL (tdecl) = 1;
3904 SET_IDENTIFIER_GLOBAL_VALUE (tname, tdecl);
3906 if (rname)
3908 if (!tdecl)
3910 tdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, rname, type);
3911 DECL_ARTIFICIAL (tdecl) = 1;
3913 SET_IDENTIFIER_GLOBAL_VALUE (rname, tdecl);
3916 if (!TYPE_NAME (type))
3917 TYPE_NAME (type) = tdecl;
3919 if (tdecl)
3920 debug_hooks->type_decl (tdecl, 0);
3923 /* Push a type into the namespace so that the back ends ignore it. */
3925 static void
3926 record_unknown_type (tree type, const char* name)
3928 tree decl = pushdecl (build_decl (UNKNOWN_LOCATION,
3929 TYPE_DECL, get_identifier (name), type));
3930 /* Make sure the "unknown type" typedecl gets ignored for debug info. */
3931 DECL_IGNORED_P (decl) = 1;
3932 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
3933 TYPE_SIZE (type) = TYPE_SIZE (void_type_node);
3934 SET_TYPE_ALIGN (type, 1);
3935 TYPE_USER_ALIGN (type) = 0;
3936 SET_TYPE_MODE (type, TYPE_MODE (void_type_node));
3939 /* Create all the predefined identifiers. */
3941 static void
3942 initialize_predefined_identifiers (void)
3944 struct predefined_identifier
3946 const char *name; /* Name. */
3947 tree *node; /* Node to store it in. */
3948 cp_identifier_kind kind; /* Kind of identifier. */
3951 /* A table of identifiers to create at startup. */
3952 static const predefined_identifier predefined_identifiers[] = {
3953 {"C++", &lang_name_cplusplus, cik_normal},
3954 {"C", &lang_name_c, cik_normal},
3955 /* Some of these names have a trailing space so that it is
3956 impossible for them to conflict with names written by users. */
3957 {"__ct ", &ctor_identifier, cik_ctor},
3958 {"__base_ctor ", &base_ctor_identifier, cik_ctor},
3959 {"__comp_ctor ", &complete_ctor_identifier, cik_ctor},
3960 {"__dt ", &dtor_identifier, cik_dtor},
3961 {"__comp_dtor ", &complete_dtor_identifier, cik_dtor},
3962 {"__base_dtor ", &base_dtor_identifier, cik_dtor},
3963 {"__deleting_dtor ", &deleting_dtor_identifier, cik_dtor},
3964 {IN_CHARGE_NAME, &in_charge_identifier, cik_normal},
3965 {THIS_NAME, &this_identifier, cik_normal},
3966 {VTABLE_DELTA_NAME, &delta_identifier, cik_normal},
3967 {VTABLE_PFN_NAME, &pfn_identifier, cik_normal},
3968 {"_vptr", &vptr_identifier, cik_normal},
3969 {"__vtt_parm", &vtt_parm_identifier, cik_normal},
3970 {"::", &global_identifier, cik_normal},
3971 {"std", &std_identifier, cik_normal},
3972 /* The demangler expects anonymous namespaces to be called
3973 something starting with '_GLOBAL__N_'. It no longer needs
3974 to be unique to the TU. */
3975 {"_GLOBAL__N_1", &anon_identifier, cik_normal},
3976 {"auto", &auto_identifier, cik_normal},
3977 {"decltype(auto)", &decltype_auto_identifier, cik_normal},
3978 {"initializer_list", &init_list_identifier, cik_normal},
3979 {NULL, NULL, cik_normal}
3982 for (const predefined_identifier *pid = predefined_identifiers;
3983 pid->name; ++pid)
3985 *pid->node = get_identifier (pid->name);
3986 /* Some of these identifiers already have a special kind. */
3987 if (pid->kind != cik_normal)
3988 set_identifier_kind (*pid->node, pid->kind);
3992 /* Create the predefined scalar types of C,
3993 and some nodes representing standard constants (0, 1, (void *)0).
3994 Initialize the global binding level.
3995 Make definitions for built-in primitive functions. */
3997 void
3998 cxx_init_decl_processing (void)
4000 tree void_ftype;
4001 tree void_ftype_ptr;
4003 /* Create all the identifiers we need. */
4004 initialize_predefined_identifiers ();
4006 /* Create the global variables. */
4007 push_to_top_level ();
4009 current_function_decl = NULL_TREE;
4010 current_binding_level = NULL;
4011 /* Enter the global namespace. */
4012 gcc_assert (global_namespace == NULL_TREE);
4013 global_namespace = build_lang_decl (NAMESPACE_DECL, global_identifier,
4014 void_type_node);
4015 TREE_PUBLIC (global_namespace) = 1;
4016 DECL_CONTEXT (global_namespace)
4017 = build_translation_unit_decl (get_identifier (main_input_filename));
4018 debug_hooks->register_main_translation_unit
4019 (DECL_CONTEXT (global_namespace));
4020 begin_scope (sk_namespace, global_namespace);
4021 current_namespace = global_namespace;
4023 if (flag_visibility_ms_compat)
4024 default_visibility = VISIBILITY_HIDDEN;
4026 /* Initially, C. */
4027 current_lang_name = lang_name_c;
4029 /* Create the `std' namespace. */
4030 push_namespace (std_identifier);
4031 std_node = current_namespace;
4032 pop_namespace ();
4034 flag_noexcept_type = (cxx_dialect >= cxx1z);
4036 c_common_nodes_and_builtins ();
4038 integer_two_node = build_int_cst (NULL_TREE, 2);
4040 /* Guess at the initial static decls size. */
4041 vec_alloc (static_decls, 500);
4043 /* ... and keyed classes. */
4044 vec_alloc (keyed_classes, 100);
4046 record_builtin_type (RID_BOOL, "bool", boolean_type_node);
4047 truthvalue_type_node = boolean_type_node;
4048 truthvalue_false_node = boolean_false_node;
4049 truthvalue_true_node = boolean_true_node;
4051 empty_except_spec = build_tree_list (NULL_TREE, NULL_TREE);
4052 noexcept_true_spec = build_tree_list (boolean_true_node, NULL_TREE);
4053 noexcept_false_spec = build_tree_list (boolean_false_node, NULL_TREE);
4054 noexcept_deferred_spec = build_tree_list (make_node (DEFERRED_NOEXCEPT),
4055 NULL_TREE);
4057 #if 0
4058 record_builtin_type (RID_MAX, NULL, string_type_node);
4059 #endif
4061 delta_type_node = ptrdiff_type_node;
4062 vtable_index_type = ptrdiff_type_node;
4064 vtt_parm_type = build_pointer_type (const_ptr_type_node);
4065 void_ftype = build_function_type_list (void_type_node, NULL_TREE);
4066 void_ftype_ptr = build_function_type_list (void_type_node,
4067 ptr_type_node, NULL_TREE);
4068 void_ftype_ptr
4069 = build_exception_variant (void_ftype_ptr, empty_except_spec);
4071 /* C++ extensions */
4073 unknown_type_node = make_node (LANG_TYPE);
4074 record_unknown_type (unknown_type_node, "unknown type");
4076 /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node. */
4077 TREE_TYPE (unknown_type_node) = unknown_type_node;
4079 /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same
4080 result. */
4081 TYPE_POINTER_TO (unknown_type_node) = unknown_type_node;
4082 TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node;
4084 init_list_type_node = make_node (LANG_TYPE);
4085 record_unknown_type (init_list_type_node, "init list");
4088 /* Make sure we get a unique function type, so we can give
4089 its pointer type a name. (This wins for gdb.) */
4090 tree vfunc_type = make_node (FUNCTION_TYPE);
4091 TREE_TYPE (vfunc_type) = integer_type_node;
4092 TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
4093 layout_type (vfunc_type);
4095 vtable_entry_type = build_pointer_type (vfunc_type);
4097 record_builtin_type (RID_MAX, VTBL_PTR_TYPE, vtable_entry_type);
4099 vtbl_type_node
4100 = build_cplus_array_type (vtable_entry_type, NULL_TREE);
4101 layout_type (vtbl_type_node);
4102 vtbl_type_node = cp_build_qualified_type (vtbl_type_node, TYPE_QUAL_CONST);
4103 record_builtin_type (RID_MAX, NULL, vtbl_type_node);
4104 vtbl_ptr_type_node = build_pointer_type (vtable_entry_type);
4105 layout_type (vtbl_ptr_type_node);
4106 record_builtin_type (RID_MAX, NULL, vtbl_ptr_type_node);
4108 push_namespace (get_identifier ("__cxxabiv1"));
4109 abi_node = current_namespace;
4110 pop_namespace ();
4112 global_type_node = make_node (LANG_TYPE);
4113 record_unknown_type (global_type_node, "global type");
4115 any_targ_node = make_node (LANG_TYPE);
4116 record_unknown_type (any_targ_node, "any type");
4118 /* Now, C++. */
4119 current_lang_name = lang_name_cplusplus;
4121 if (aligned_new_threshold > 1
4122 && !pow2p_hwi (aligned_new_threshold))
4124 error ("-faligned-new=%d is not a power of two", aligned_new_threshold);
4125 aligned_new_threshold = 1;
4127 if (aligned_new_threshold == -1)
4128 aligned_new_threshold = (cxx_dialect >= cxx1z) ? 1 : 0;
4129 if (aligned_new_threshold == 1)
4130 aligned_new_threshold = malloc_alignment () / BITS_PER_UNIT;
4133 tree newattrs, extvisattr;
4134 tree newtype, deltype;
4135 tree ptr_ftype_sizetype;
4136 tree new_eh_spec;
4138 ptr_ftype_sizetype
4139 = build_function_type_list (ptr_type_node, size_type_node, NULL_TREE);
4140 if (cxx_dialect == cxx98)
4142 tree bad_alloc_id;
4143 tree bad_alloc_type_node;
4144 tree bad_alloc_decl;
4146 push_namespace (std_identifier);
4147 bad_alloc_id = get_identifier ("bad_alloc");
4148 bad_alloc_type_node = make_class_type (RECORD_TYPE);
4149 TYPE_CONTEXT (bad_alloc_type_node) = current_namespace;
4150 bad_alloc_decl
4151 = create_implicit_typedef (bad_alloc_id, bad_alloc_type_node);
4152 DECL_CONTEXT (bad_alloc_decl) = current_namespace;
4153 pop_namespace ();
4155 new_eh_spec
4156 = add_exception_specifier (NULL_TREE, bad_alloc_type_node, -1);
4158 else
4159 new_eh_spec = noexcept_false_spec;
4161 /* Ensure attribs.c is initialized. */
4162 init_attributes ();
4164 /* Ensure constraint.cc is initialized. */
4165 init_constraint_processing ();
4167 extvisattr = build_tree_list (get_identifier ("externally_visible"),
4168 NULL_TREE);
4169 newattrs = tree_cons (get_identifier ("alloc_size"),
4170 build_tree_list (NULL_TREE, integer_one_node),
4171 extvisattr);
4172 newtype = cp_build_type_attribute_variant (ptr_ftype_sizetype, newattrs);
4173 newtype = build_exception_variant (newtype, new_eh_spec);
4174 deltype = cp_build_type_attribute_variant (void_ftype_ptr, extvisattr);
4175 deltype = build_exception_variant (deltype, empty_except_spec);
4176 tree opnew = push_cp_library_fn (NEW_EXPR, newtype, 0);
4177 DECL_IS_MALLOC (opnew) = 1;
4178 DECL_IS_OPERATOR_NEW (opnew) = 1;
4179 opnew = push_cp_library_fn (VEC_NEW_EXPR, newtype, 0);
4180 DECL_IS_MALLOC (opnew) = 1;
4181 DECL_IS_OPERATOR_NEW (opnew) = 1;
4182 push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
4183 push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
4184 if (flag_sized_deallocation)
4186 /* Also push the sized deallocation variants:
4187 void operator delete(void*, std::size_t) throw();
4188 void operator delete[](void*, std::size_t) throw(); */
4189 tree void_ftype_ptr_size
4190 = build_function_type_list (void_type_node, ptr_type_node,
4191 size_type_node, NULL_TREE);
4192 deltype = cp_build_type_attribute_variant (void_ftype_ptr_size,
4193 extvisattr);
4194 deltype = build_exception_variant (deltype, empty_except_spec);
4195 push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
4196 push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
4199 if (aligned_new_threshold)
4201 push_namespace (std_identifier);
4202 tree align_id = get_identifier ("align_val_t");
4203 align_type_node = start_enum (align_id, NULL_TREE, size_type_node,
4204 NULL_TREE, /*scoped*/true, NULL);
4205 pop_namespace ();
4207 /* operator new (size_t, align_val_t); */
4208 newtype = build_function_type_list (ptr_type_node, size_type_node,
4209 align_type_node, NULL_TREE);
4210 newtype = cp_build_type_attribute_variant (newtype, newattrs);
4211 newtype = build_exception_variant (newtype, new_eh_spec);
4212 opnew = push_cp_library_fn (NEW_EXPR, newtype, 0);
4213 DECL_IS_MALLOC (opnew) = 1;
4214 DECL_IS_OPERATOR_NEW (opnew) = 1;
4215 opnew = push_cp_library_fn (VEC_NEW_EXPR, newtype, 0);
4216 DECL_IS_MALLOC (opnew) = 1;
4217 DECL_IS_OPERATOR_NEW (opnew) = 1;
4219 /* operator delete (void *, align_val_t); */
4220 deltype = build_function_type_list (void_type_node, ptr_type_node,
4221 align_type_node, NULL_TREE);
4222 deltype = cp_build_type_attribute_variant (deltype, extvisattr);
4223 deltype = build_exception_variant (deltype, empty_except_spec);
4224 push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
4225 push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
4227 if (flag_sized_deallocation)
4229 /* operator delete (void *, size_t, align_val_t); */
4230 deltype = build_function_type_list (void_type_node, ptr_type_node,
4231 size_type_node, align_type_node,
4232 NULL_TREE);
4233 deltype = cp_build_type_attribute_variant (deltype, extvisattr);
4234 deltype = build_exception_variant (deltype, empty_except_spec);
4235 push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
4236 push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
4240 nullptr_type_node = make_node (NULLPTR_TYPE);
4241 TYPE_SIZE (nullptr_type_node) = bitsize_int (GET_MODE_BITSIZE (ptr_mode));
4242 TYPE_SIZE_UNIT (nullptr_type_node) = size_int (GET_MODE_SIZE (ptr_mode));
4243 TYPE_UNSIGNED (nullptr_type_node) = 1;
4244 TYPE_PRECISION (nullptr_type_node) = GET_MODE_BITSIZE (ptr_mode);
4245 if (abi_version_at_least (9))
4246 SET_TYPE_ALIGN (nullptr_type_node, GET_MODE_ALIGNMENT (ptr_mode));
4247 SET_TYPE_MODE (nullptr_type_node, ptr_mode);
4248 record_builtin_type (RID_MAX, "decltype(nullptr)", nullptr_type_node);
4249 nullptr_node = build_int_cst (nullptr_type_node, 0);
4252 abort_fndecl
4253 = build_library_fn_ptr ("__cxa_pure_virtual", void_ftype,
4254 ECF_NORETURN | ECF_NOTHROW | ECF_COLD);
4256 /* Perform other language dependent initializations. */
4257 init_class_processing ();
4258 init_rtti_processing ();
4259 init_template_processing ();
4261 if (flag_exceptions)
4262 init_exception_processing ();
4264 if (! supports_one_only ())
4265 flag_weak = 0;
4267 make_fname_decl = cp_make_fname_decl;
4268 start_fname_decls ();
4270 /* Show we use EH for cleanups. */
4271 if (flag_exceptions)
4272 using_eh_for_cleanups ();
4275 /* Generate an initializer for a function naming variable from
4276 NAME. NAME may be NULL, to indicate a dependent name. TYPE_P is
4277 filled in with the type of the init. */
4279 tree
4280 cp_fname_init (const char* name, tree *type_p)
4282 tree domain = NULL_TREE;
4283 tree type;
4284 tree init = NULL_TREE;
4285 size_t length = 0;
4287 if (name)
4289 length = strlen (name);
4290 domain = build_index_type (size_int (length));
4291 init = build_string (length + 1, name);
4294 type = cp_build_qualified_type (char_type_node, TYPE_QUAL_CONST);
4295 type = build_cplus_array_type (type, domain);
4297 *type_p = type;
4299 if (init)
4300 TREE_TYPE (init) = type;
4301 else
4302 init = error_mark_node;
4304 return init;
4307 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give
4308 the decl, LOC is the location to give the decl, NAME is the
4309 initialization string and TYPE_DEP indicates whether NAME depended
4310 on the type of the function. We make use of that to detect
4311 __PRETTY_FUNCTION__ inside a template fn. This is being done lazily
4312 at the point of first use, so we mustn't push the decl now. */
4314 static tree
4315 cp_make_fname_decl (location_t loc, tree id, int type_dep)
4317 const char *const name = (type_dep && processing_template_decl
4318 ? NULL : fname_as_string (type_dep));
4319 tree type;
4320 tree init = cp_fname_init (name, &type);
4321 tree decl = build_decl (loc, VAR_DECL, id, type);
4323 if (name)
4324 free (CONST_CAST (char *, name));
4326 TREE_STATIC (decl) = 1;
4327 TREE_READONLY (decl) = 1;
4328 DECL_ARTIFICIAL (decl) = 1;
4330 TREE_USED (decl) = 1;
4332 if (current_function_decl)
4334 DECL_CONTEXT (decl) = current_function_decl;
4335 decl = pushdecl_outermost_localscope (decl);
4336 cp_finish_decl (decl, init, /*init_const_expr_p=*/false, NULL_TREE,
4337 LOOKUP_ONLYCONVERTING);
4339 else
4341 DECL_THIS_STATIC (decl) = true;
4342 pushdecl_top_level_and_finish (decl, init);
4345 return decl;
4348 static tree
4349 builtin_function_1 (tree decl, tree context, bool is_global)
4351 tree id = DECL_NAME (decl);
4352 const char *name = IDENTIFIER_POINTER (id);
4354 retrofit_lang_decl (decl);
4356 DECL_ARTIFICIAL (decl) = 1;
4357 SET_OVERLOADED_OPERATOR_CODE (decl, ERROR_MARK);
4358 SET_DECL_LANGUAGE (decl, lang_c);
4359 /* Runtime library routines are, by definition, available in an
4360 external shared object. */
4361 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
4362 DECL_VISIBILITY_SPECIFIED (decl) = 1;
4364 DECL_CONTEXT (decl) = context;
4366 /* A function in the user's namespace should have an explicit
4367 declaration before it is used. Mark the built-in function as
4368 anticipated but not actually declared. */
4369 if (name[0] != '_' || name[1] != '_')
4370 DECL_ANTICIPATED (decl) = 1;
4371 else if (strncmp (name + 2, "builtin_", strlen ("builtin_")) != 0)
4373 size_t len = strlen (name);
4375 /* Treat __*_chk fortification functions as anticipated as well,
4376 unless they are __builtin_*. */
4377 if (len > strlen ("___chk")
4378 && memcmp (name + len - strlen ("_chk"),
4379 "_chk", strlen ("_chk") + 1) == 0)
4380 DECL_ANTICIPATED (decl) = 1;
4383 if (is_global)
4384 pushdecl_top_level (decl);
4385 else
4386 pushdecl (decl);
4388 return decl;
4391 tree
4392 cxx_builtin_function (tree decl)
4394 tree id = DECL_NAME (decl);
4395 const char *name = IDENTIFIER_POINTER (id);
4396 /* All builtins that don't begin with an '_' should additionally
4397 go in the 'std' namespace. */
4398 if (name[0] != '_')
4400 tree decl2 = copy_node(decl);
4401 push_namespace (std_identifier);
4402 builtin_function_1 (decl2, std_node, false);
4403 pop_namespace ();
4406 return builtin_function_1 (decl, NULL_TREE, false);
4409 /* Like cxx_builtin_function, but guarantee the function is added to the global
4410 scope. This is to allow function specific options to add new machine
4411 dependent builtins when the target ISA changes via attribute((target(...)))
4412 which saves space on program startup if the program does not use non-generic
4413 ISAs. */
4415 tree
4416 cxx_builtin_function_ext_scope (tree decl)
4419 tree id = DECL_NAME (decl);
4420 const char *name = IDENTIFIER_POINTER (id);
4421 /* All builtins that don't begin with an '_' should additionally
4422 go in the 'std' namespace. */
4423 if (name[0] != '_')
4425 tree decl2 = copy_node(decl);
4426 push_namespace (std_identifier);
4427 builtin_function_1 (decl2, std_node, true);
4428 pop_namespace ();
4431 return builtin_function_1 (decl, NULL_TREE, true);
4434 /* Generate a FUNCTION_DECL with the typical flags for a runtime library
4435 function. Not called directly. */
4437 static tree
4438 build_library_fn (tree name, enum tree_code operator_code, tree type,
4439 int ecf_flags)
4441 tree fn = build_lang_decl (FUNCTION_DECL, name, type);
4442 DECL_EXTERNAL (fn) = 1;
4443 TREE_PUBLIC (fn) = 1;
4444 DECL_ARTIFICIAL (fn) = 1;
4445 SET_OVERLOADED_OPERATOR_CODE (fn, operator_code);
4446 SET_DECL_LANGUAGE (fn, lang_c);
4447 /* Runtime library routines are, by definition, available in an
4448 external shared object. */
4449 DECL_VISIBILITY (fn) = VISIBILITY_DEFAULT;
4450 DECL_VISIBILITY_SPECIFIED (fn) = 1;
4451 set_call_expr_flags (fn, ecf_flags);
4452 return fn;
4455 /* Returns the _DECL for a library function with C++ linkage. */
4457 static tree
4458 build_cp_library_fn (tree name, enum tree_code operator_code, tree type,
4459 int ecf_flags)
4461 tree fn = build_library_fn (name, operator_code, type, ecf_flags);
4462 DECL_CONTEXT (fn) = FROB_CONTEXT (current_namespace);
4463 SET_DECL_LANGUAGE (fn, lang_cplusplus);
4464 return fn;
4467 /* Like build_library_fn, but takes a C string instead of an
4468 IDENTIFIER_NODE. */
4470 tree
4471 build_library_fn_ptr (const char* name, tree type, int ecf_flags)
4473 return build_library_fn (get_identifier (name), ERROR_MARK, type, ecf_flags);
4476 /* Like build_cp_library_fn, but takes a C string instead of an
4477 IDENTIFIER_NODE. */
4479 tree
4480 build_cp_library_fn_ptr (const char* name, tree type, int ecf_flags)
4482 return build_cp_library_fn (get_identifier (name), ERROR_MARK, type,
4483 ecf_flags);
4486 /* Like build_library_fn, but also pushes the function so that we will
4487 be able to find it via IDENTIFIER_GLOBAL_VALUE. Also, the function
4488 may throw exceptions listed in RAISES. */
4490 tree
4491 push_library_fn (tree name, tree type, tree raises, int ecf_flags)
4493 tree fn;
4495 if (raises)
4496 type = build_exception_variant (type, raises);
4498 fn = build_library_fn (name, ERROR_MARK, type, ecf_flags);
4499 pushdecl_top_level (fn);
4500 return fn;
4503 /* Like build_cp_library_fn, but also pushes the function so that it
4504 will be found by normal lookup. */
4506 static tree
4507 push_cp_library_fn (enum tree_code operator_code, tree type,
4508 int ecf_flags)
4510 tree fn = build_cp_library_fn (cp_operator_id (operator_code),
4511 operator_code,
4512 type, ecf_flags);
4513 pushdecl (fn);
4514 if (flag_tm)
4515 apply_tm_attr (fn, get_identifier ("transaction_safe"));
4516 return fn;
4519 /* Like push_library_fn, but takes a TREE_LIST of parm types rather than
4520 a FUNCTION_TYPE. */
4522 tree
4523 push_void_library_fn (tree name, tree parmtypes, int ecf_flags)
4525 tree type = build_function_type (void_type_node, parmtypes);
4526 return push_library_fn (name, type, NULL_TREE, ecf_flags);
4529 /* Like push_library_fn, but also note that this function throws
4530 and does not return. Used for __throw_foo and the like. */
4532 tree
4533 push_throw_library_fn (tree name, tree type)
4535 tree fn = push_library_fn (name, type, NULL_TREE, ECF_NORETURN | ECF_COLD);
4536 return fn;
4539 /* When we call finish_struct for an anonymous union, we create
4540 default copy constructors and such. But, an anonymous union
4541 shouldn't have such things; this function undoes the damage to the
4542 anonymous union type T.
4544 (The reason that we create the synthesized methods is that we don't
4545 distinguish `union { int i; }' from `typedef union { int i; } U'.
4546 The first is an anonymous union; the second is just an ordinary
4547 union type.) */
4549 void
4550 fixup_anonymous_aggr (tree t)
4552 tree *q;
4554 /* Wipe out memory of synthesized methods. */
4555 TYPE_HAS_USER_CONSTRUCTOR (t) = 0;
4556 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0;
4557 TYPE_HAS_COPY_CTOR (t) = 0;
4558 TYPE_HAS_CONST_COPY_CTOR (t) = 0;
4559 TYPE_HAS_COPY_ASSIGN (t) = 0;
4560 TYPE_HAS_CONST_COPY_ASSIGN (t) = 0;
4562 /* Splice the implicitly generated functions out of the TYPE_METHODS
4563 list. */
4564 q = &TYPE_METHODS (t);
4565 while (*q)
4567 if (DECL_ARTIFICIAL (*q))
4568 *q = TREE_CHAIN (*q);
4569 else
4570 q = &DECL_CHAIN (*q);
4573 /* ISO C++ 9.5.3. Anonymous unions may not have function members. */
4574 if (TYPE_METHODS (t))
4576 tree decl = TYPE_MAIN_DECL (t);
4578 if (TREE_CODE (t) != UNION_TYPE)
4579 error_at (DECL_SOURCE_LOCATION (decl),
4580 "an anonymous struct cannot have function members");
4581 else
4582 error_at (DECL_SOURCE_LOCATION (decl),
4583 "an anonymous union cannot have function members");
4586 /* Anonymous aggregates cannot have fields with ctors, dtors or complex
4587 assignment operators (because they cannot have these methods themselves).
4588 For anonymous unions this is already checked because they are not allowed
4589 in any union, otherwise we have to check it. */
4590 if (TREE_CODE (t) != UNION_TYPE)
4592 tree field, type;
4594 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
4595 if (TREE_CODE (field) == FIELD_DECL)
4597 type = TREE_TYPE (field);
4598 if (CLASS_TYPE_P (type))
4600 if (TYPE_NEEDS_CONSTRUCTING (type))
4601 error ("member %q+#D with constructor not allowed "
4602 "in anonymous aggregate", field);
4603 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
4604 error ("member %q+#D with destructor not allowed "
4605 "in anonymous aggregate", field);
4606 if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type))
4607 error ("member %q+#D with copy assignment operator "
4608 "not allowed in anonymous aggregate", field);
4614 /* Warn for an attribute located at LOCATION that appertains to the
4615 class type CLASS_TYPE that has not been properly placed after its
4616 class-key, in it class-specifier. */
4618 void
4619 warn_misplaced_attr_for_class_type (source_location location,
4620 tree class_type)
4622 gcc_assert (OVERLOAD_TYPE_P (class_type));
4624 if (warning_at (location, OPT_Wattributes,
4625 "attribute ignored in declaration "
4626 "of %q#T", class_type))
4627 inform (location,
4628 "attribute for %q#T must follow the %qs keyword",
4629 class_type, class_key_or_enum_as_string (class_type));
4632 /* Make sure that a declaration with no declarator is well-formed, i.e.
4633 just declares a tagged type or anonymous union.
4635 Returns the type declared; or NULL_TREE if none. */
4637 tree
4638 check_tag_decl (cp_decl_specifier_seq *declspecs,
4639 bool explicit_type_instantiation_p)
4641 int saw_friend = decl_spec_seq_has_spec_p (declspecs, ds_friend);
4642 int saw_typedef = decl_spec_seq_has_spec_p (declspecs, ds_typedef);
4643 /* If a class, struct, or enum type is declared by the DECLSPECS
4644 (i.e, if a class-specifier, enum-specifier, or non-typename
4645 elaborated-type-specifier appears in the DECLSPECS),
4646 DECLARED_TYPE is set to the corresponding type. */
4647 tree declared_type = NULL_TREE;
4648 bool error_p = false;
4650 if (declspecs->multiple_types_p)
4651 error ("multiple types in one declaration");
4652 else if (declspecs->redefined_builtin_type)
4654 if (!in_system_header_at (input_location))
4655 permerror (declspecs->locations[ds_redefined_builtin_type_spec],
4656 "redeclaration of C++ built-in type %qT",
4657 declspecs->redefined_builtin_type);
4658 return NULL_TREE;
4661 if (declspecs->type
4662 && TYPE_P (declspecs->type)
4663 && ((TREE_CODE (declspecs->type) != TYPENAME_TYPE
4664 && MAYBE_CLASS_TYPE_P (declspecs->type))
4665 || TREE_CODE (declspecs->type) == ENUMERAL_TYPE))
4666 declared_type = declspecs->type;
4667 else if (declspecs->type == error_mark_node)
4668 error_p = true;
4669 if (declared_type == NULL_TREE && ! saw_friend && !error_p)
4670 permerror (input_location, "declaration does not declare anything");
4671 else if (declared_type != NULL_TREE && type_uses_auto (declared_type))
4673 error_at (declspecs->locations[ds_type_spec],
4674 "%<auto%> can only be specified for variables "
4675 "or function declarations");
4676 return error_mark_node;
4678 /* Check for an anonymous union. */
4679 else if (declared_type && RECORD_OR_UNION_CODE_P (TREE_CODE (declared_type))
4680 && TYPE_UNNAMED_P (declared_type))
4682 /* 7/3 In a simple-declaration, the optional init-declarator-list
4683 can be omitted only when declaring a class (clause 9) or
4684 enumeration (7.2), that is, when the decl-specifier-seq contains
4685 either a class-specifier, an elaborated-type-specifier with
4686 a class-key (9.1), or an enum-specifier. In these cases and
4687 whenever a class-specifier or enum-specifier is present in the
4688 decl-specifier-seq, the identifiers in these specifiers are among
4689 the names being declared by the declaration (as class-name,
4690 enum-names, or enumerators, depending on the syntax). In such
4691 cases, and except for the declaration of an unnamed bit-field (9.6),
4692 the decl-specifier-seq shall introduce one or more names into the
4693 program, or shall redeclare a name introduced by a previous
4694 declaration. [Example:
4695 enum { }; // ill-formed
4696 typedef class { }; // ill-formed
4697 --end example] */
4698 if (saw_typedef)
4700 error ("missing type-name in typedef-declaration");
4701 return NULL_TREE;
4703 /* Anonymous unions are objects, so they can have specifiers. */;
4704 SET_ANON_AGGR_TYPE_P (declared_type);
4706 if (TREE_CODE (declared_type) != UNION_TYPE
4707 && !in_system_header_at (input_location))
4708 pedwarn (input_location, OPT_Wpedantic, "ISO C++ prohibits anonymous structs");
4711 else
4713 if (decl_spec_seq_has_spec_p (declspecs, ds_inline))
4714 error_at (declspecs->locations[ds_inline],
4715 "%<inline%> can only be specified for functions");
4716 else if (decl_spec_seq_has_spec_p (declspecs, ds_virtual))
4717 error_at (declspecs->locations[ds_virtual],
4718 "%<virtual%> can only be specified for functions");
4719 else if (saw_friend
4720 && (!current_class_type
4721 || current_scope () != current_class_type))
4722 error_at (declspecs->locations[ds_friend],
4723 "%<friend%> can only be specified inside a class");
4724 else if (decl_spec_seq_has_spec_p (declspecs, ds_explicit))
4725 error_at (declspecs->locations[ds_explicit],
4726 "%<explicit%> can only be specified for constructors");
4727 else if (declspecs->storage_class)
4728 error_at (declspecs->locations[ds_storage_class],
4729 "a storage class can only be specified for objects "
4730 "and functions");
4731 else if (decl_spec_seq_has_spec_p (declspecs, ds_const))
4732 error_at (declspecs->locations[ds_const],
4733 "%<const%> can only be specified for objects and "
4734 "functions");
4735 else if (decl_spec_seq_has_spec_p (declspecs, ds_volatile))
4736 error_at (declspecs->locations[ds_volatile],
4737 "%<volatile%> can only be specified for objects and "
4738 "functions");
4739 else if (decl_spec_seq_has_spec_p (declspecs, ds_restrict))
4740 error_at (declspecs->locations[ds_restrict],
4741 "%<__restrict%> can only be specified for objects and "
4742 "functions");
4743 else if (decl_spec_seq_has_spec_p (declspecs, ds_thread))
4744 error_at (declspecs->locations[ds_thread],
4745 "%<__thread%> can only be specified for objects "
4746 "and functions");
4747 else if (saw_typedef)
4748 warning_at (declspecs->locations[ds_typedef], 0,
4749 "%<typedef%> was ignored in this declaration");
4750 else if (decl_spec_seq_has_spec_p (declspecs, ds_constexpr))
4751 error_at (declspecs->locations[ds_constexpr],
4752 "%<constexpr%> cannot be used for type declarations");
4755 if (declspecs->attributes && warn_attributes && declared_type)
4757 location_t loc;
4758 if (!CLASS_TYPE_P (declared_type)
4759 || !CLASSTYPE_TEMPLATE_INSTANTIATION (declared_type))
4760 /* For a non-template class, use the name location. */
4761 loc = location_of (declared_type);
4762 else
4763 /* For a template class (an explicit instantiation), use the
4764 current location. */
4765 loc = input_location;
4767 if (explicit_type_instantiation_p)
4768 /* [dcl.attr.grammar]/4:
4770 No attribute-specifier-seq shall appertain to an explicit
4771 instantiation. */
4773 if (warning_at (loc, OPT_Wattributes,
4774 "attribute ignored in explicit instantiation %q#T",
4775 declared_type))
4776 inform (loc,
4777 "no attribute can be applied to "
4778 "an explicit instantiation");
4780 else
4781 warn_misplaced_attr_for_class_type (loc, declared_type);
4784 return declared_type;
4787 /* Called when a declaration is seen that contains no names to declare.
4788 If its type is a reference to a structure, union or enum inherited
4789 from a containing scope, shadow that tag name for the current scope
4790 with a forward reference.
4791 If its type defines a new named structure or union
4792 or defines an enum, it is valid but we need not do anything here.
4793 Otherwise, it is an error.
4795 C++: may have to grok the declspecs to learn about static,
4796 complain for anonymous unions.
4798 Returns the TYPE declared -- or NULL_TREE if none. */
4800 tree
4801 shadow_tag (cp_decl_specifier_seq *declspecs)
4803 tree t = check_tag_decl (declspecs,
4804 /*explicit_type_instantiation_p=*/false);
4806 if (!t)
4807 return NULL_TREE;
4809 if (maybe_process_partial_specialization (t) == error_mark_node)
4810 return NULL_TREE;
4812 /* This is where the variables in an anonymous union are
4813 declared. An anonymous union declaration looks like:
4814 union { ... } ;
4815 because there is no declarator after the union, the parser
4816 sends that declaration here. */
4817 if (ANON_AGGR_TYPE_P (t))
4819 fixup_anonymous_aggr (t);
4821 if (TYPE_FIELDS (t))
4823 tree decl = grokdeclarator (/*declarator=*/NULL,
4824 declspecs, NORMAL, 0, NULL);
4825 finish_anon_union (decl);
4829 return t;
4832 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
4834 tree
4835 groktypename (cp_decl_specifier_seq *type_specifiers,
4836 const cp_declarator *declarator,
4837 bool is_template_arg)
4839 tree attrs;
4840 tree type;
4841 enum decl_context context
4842 = is_template_arg ? TEMPLATE_TYPE_ARG : TYPENAME;
4843 attrs = type_specifiers->attributes;
4844 type_specifiers->attributes = NULL_TREE;
4845 type = grokdeclarator (declarator, type_specifiers, context, 0, &attrs);
4846 if (attrs && type != error_mark_node)
4848 if (CLASS_TYPE_P (type))
4849 warning (OPT_Wattributes, "ignoring attributes applied to class type %qT "
4850 "outside of definition", type);
4851 else if (MAYBE_CLASS_TYPE_P (type))
4852 /* A template type parameter or other dependent type. */
4853 warning (OPT_Wattributes, "ignoring attributes applied to dependent "
4854 "type %qT without an associated declaration", type);
4855 else
4856 cplus_decl_attributes (&type, attrs, 0);
4858 return type;
4861 /* Process a DECLARATOR for a function-scope variable declaration,
4862 namespace-scope variable declaration, or function declaration.
4863 (Function definitions go through start_function; class member
4864 declarations appearing in the body of the class go through
4865 grokfield.) The DECL corresponding to the DECLARATOR is returned.
4866 If an error occurs, the error_mark_node is returned instead.
4868 DECLSPECS are the decl-specifiers for the declaration. INITIALIZED is
4869 SD_INITIALIZED if an explicit initializer is present, or SD_DEFAULTED
4870 for an explicitly defaulted function, or SD_DELETED for an explicitly
4871 deleted function, but 0 (SD_UNINITIALIZED) if this is a variable
4872 implicitly initialized via a default constructor. ATTRIBUTES and
4873 PREFIX_ATTRIBUTES are GNU attributes associated with this declaration.
4875 The scope represented by the context of the returned DECL is pushed
4876 (if it is not the global namespace) and is assigned to
4877 *PUSHED_SCOPE_P. The caller is then responsible for calling
4878 pop_scope on *PUSHED_SCOPE_P if it is set. */
4880 tree
4881 start_decl (const cp_declarator *declarator,
4882 cp_decl_specifier_seq *declspecs,
4883 int initialized,
4884 tree attributes,
4885 tree prefix_attributes,
4886 tree *pushed_scope_p)
4888 tree decl;
4889 tree context;
4890 bool was_public;
4891 int flags;
4892 bool alias;
4894 *pushed_scope_p = NULL_TREE;
4896 /* An object declared as __attribute__((deprecated)) suppresses
4897 warnings of uses of other deprecated items. */
4898 if (lookup_attribute ("deprecated", attributes))
4899 deprecated_state = DEPRECATED_SUPPRESS;
4901 attributes = chainon (attributes, prefix_attributes);
4903 decl = grokdeclarator (declarator, declspecs, NORMAL, initialized,
4904 &attributes);
4906 deprecated_state = DEPRECATED_NORMAL;
4908 if (decl == NULL_TREE || VOID_TYPE_P (decl)
4909 || decl == error_mark_node)
4910 return error_mark_node;
4912 context = CP_DECL_CONTEXT (decl);
4913 if (context != global_namespace)
4914 *pushed_scope_p = push_scope (context);
4916 /* Is it valid for this decl to have an initializer at all?
4917 If not, set INITIALIZED to zero, which will indirectly
4918 tell `cp_finish_decl' to ignore the initializer once it is parsed. */
4919 if (initialized
4920 && TREE_CODE (decl) == TYPE_DECL)
4922 error ("typedef %qD is initialized (use decltype instead)", decl);
4923 return error_mark_node;
4926 if (initialized)
4928 if (! toplevel_bindings_p ()
4929 && DECL_EXTERNAL (decl))
4930 warning (0, "declaration of %q#D has %<extern%> and is initialized",
4931 decl);
4932 DECL_EXTERNAL (decl) = 0;
4933 if (toplevel_bindings_p ())
4934 TREE_STATIC (decl) = 1;
4936 alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl)) != 0;
4938 if (alias && TREE_CODE (decl) == FUNCTION_DECL)
4939 record_key_method_defined (decl);
4941 /* If this is a typedef that names the class for linkage purposes
4942 (7.1.3p8), apply any attributes directly to the type. */
4943 if (TREE_CODE (decl) == TYPE_DECL
4944 && OVERLOAD_TYPE_P (TREE_TYPE (decl))
4945 && decl == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (decl))))
4946 flags = ATTR_FLAG_TYPE_IN_PLACE;
4947 else
4948 flags = 0;
4950 /* Set attributes here so if duplicate decl, will have proper attributes. */
4951 cplus_decl_attributes (&decl, attributes, flags);
4953 /* Dllimported symbols cannot be defined. Static data members (which
4954 can be initialized in-class and dllimported) go through grokfield,
4955 not here, so we don't need to exclude those decls when checking for
4956 a definition. */
4957 if (initialized && DECL_DLLIMPORT_P (decl))
4959 error ("definition of %q#D is marked %<dllimport%>", decl);
4960 DECL_DLLIMPORT_P (decl) = 0;
4963 /* If #pragma weak was used, mark the decl weak now. */
4964 if (!processing_template_decl)
4965 maybe_apply_pragma_weak (decl);
4967 if (TREE_CODE (decl) == FUNCTION_DECL
4968 && DECL_DECLARED_INLINE_P (decl)
4969 && DECL_UNINLINABLE (decl)
4970 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
4971 warning_at (DECL_SOURCE_LOCATION (decl), 0,
4972 "inline function %qD given attribute noinline", decl);
4974 if (TYPE_P (context) && COMPLETE_TYPE_P (complete_type (context)))
4976 bool this_tmpl = (processing_template_decl
4977 > template_class_depth (context));
4978 if (VAR_P (decl))
4980 tree field = lookup_field (context, DECL_NAME (decl), 0, false);
4981 if (field == NULL_TREE
4982 || !(VAR_P (field) || variable_template_p (field)))
4983 error ("%q+#D is not a static data member of %q#T", decl, context);
4984 else if (variable_template_p (field) && !this_tmpl)
4986 if (DECL_LANG_SPECIFIC (decl)
4987 && DECL_TEMPLATE_SPECIALIZATION (decl))
4988 /* OK, specialization was already checked. */;
4989 else
4991 error_at (DECL_SOURCE_LOCATION (decl),
4992 "non-member-template declaration of %qD", decl);
4993 inform (DECL_SOURCE_LOCATION (field), "does not match "
4994 "member template declaration here");
4995 return error_mark_node;
4998 else
5000 if (variable_template_p (field))
5001 field = DECL_TEMPLATE_RESULT (field);
5003 if (DECL_CONTEXT (field) != context)
5005 if (!same_type_p (DECL_CONTEXT (field), context))
5006 permerror (input_location, "ISO C++ does not permit %<%T::%D%> "
5007 "to be defined as %<%T::%D%>",
5008 DECL_CONTEXT (field), DECL_NAME (decl),
5009 context, DECL_NAME (decl));
5010 DECL_CONTEXT (decl) = DECL_CONTEXT (field);
5012 /* Static data member are tricky; an in-class initialization
5013 still doesn't provide a definition, so the in-class
5014 declaration will have DECL_EXTERNAL set, but will have an
5015 initialization. Thus, duplicate_decls won't warn
5016 about this situation, and so we check here. */
5017 if (initialized && DECL_INITIALIZED_IN_CLASS_P (field))
5018 error ("duplicate initialization of %qD", decl);
5019 if (duplicate_decls (decl, field, /*newdecl_is_friend=*/false))
5020 decl = field;
5021 if (decl_spec_seq_has_spec_p (declspecs, ds_constexpr)
5022 && !DECL_DECLARED_CONSTEXPR_P (field))
5023 error ("%qD declared %<constexpr%> outside its class", field);
5026 else
5028 tree field = check_classfn (context, decl,
5029 this_tmpl
5030 ? current_template_parms
5031 : NULL_TREE);
5032 if (field && field != error_mark_node
5033 && duplicate_decls (decl, field,
5034 /*newdecl_is_friend=*/false))
5035 decl = field;
5038 /* cp_finish_decl sets DECL_EXTERNAL if DECL_IN_AGGR_P is set. */
5039 DECL_IN_AGGR_P (decl) = 0;
5040 /* Do not mark DECL as an explicit specialization if it was not
5041 already marked as an instantiation; a declaration should
5042 never be marked as a specialization unless we know what
5043 template is being specialized. */
5044 if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
5046 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
5047 if (TREE_CODE (decl) == FUNCTION_DECL)
5048 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
5049 && DECL_DECLARED_INLINE_P (decl));
5050 else
5051 DECL_COMDAT (decl) = false;
5053 /* [temp.expl.spec] An explicit specialization of a static data
5054 member of a template is a definition if the declaration
5055 includes an initializer; otherwise, it is a declaration.
5057 We check for processing_specialization so this only applies
5058 to the new specialization syntax. */
5059 if (!initialized && processing_specialization)
5060 DECL_EXTERNAL (decl) = 1;
5063 if (DECL_EXTERNAL (decl) && ! DECL_TEMPLATE_SPECIALIZATION (decl)
5064 /* Aliases are definitions. */
5065 && !alias)
5066 permerror (input_location, "declaration of %q#D outside of class is not definition",
5067 decl);
5070 was_public = TREE_PUBLIC (decl);
5072 /* Enter this declaration into the symbol table. Don't push the plain
5073 VAR_DECL for a variable template. */
5074 if (!template_parm_scope_p ()
5075 || !VAR_P (decl))
5076 decl = maybe_push_decl (decl);
5078 if (processing_template_decl)
5079 decl = push_template_decl (decl);
5080 if (decl == error_mark_node)
5081 return error_mark_node;
5083 if (VAR_P (decl)
5084 && DECL_NAMESPACE_SCOPE_P (decl) && !TREE_PUBLIC (decl) && !was_public
5085 && !DECL_THIS_STATIC (decl) && !DECL_ARTIFICIAL (decl))
5087 /* This is a const variable with implicit 'static'. Set
5088 DECL_THIS_STATIC so we can tell it from variables that are
5089 !TREE_PUBLIC because of the anonymous namespace. */
5090 gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (decl)) || errorcount);
5091 DECL_THIS_STATIC (decl) = 1;
5094 if (current_function_decl && VAR_P (decl)
5095 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
5097 bool ok = false;
5098 if (CP_DECL_THREAD_LOCAL_P (decl))
5099 error ("%qD declared %<thread_local%> in %<constexpr%> function",
5100 decl);
5101 else if (TREE_STATIC (decl))
5102 error ("%qD declared %<static%> in %<constexpr%> function", decl);
5103 else
5104 ok = true;
5105 if (!ok)
5106 cp_function_chain->invalid_constexpr = true;
5109 if (!processing_template_decl && VAR_P (decl))
5110 start_decl_1 (decl, initialized);
5112 return decl;
5115 /* Process the declaration of a variable DECL. INITIALIZED is true
5116 iff DECL is explicitly initialized. (INITIALIZED is false if the
5117 variable is initialized via an implicitly-called constructor.)
5118 This function must be called for ordinary variables (including, for
5119 example, implicit instantiations of templates), but must not be
5120 called for template declarations. */
5122 void
5123 start_decl_1 (tree decl, bool initialized)
5125 tree type;
5126 bool complete_p;
5127 bool aggregate_definition_p;
5129 gcc_assert (!processing_template_decl);
5131 if (error_operand_p (decl))
5132 return;
5134 gcc_assert (VAR_P (decl));
5136 type = TREE_TYPE (decl);
5137 complete_p = COMPLETE_TYPE_P (type);
5138 aggregate_definition_p = MAYBE_CLASS_TYPE_P (type) && !DECL_EXTERNAL (decl);
5140 /* If an explicit initializer is present, or if this is a definition
5141 of an aggregate, then we need a complete type at this point.
5142 (Scalars are always complete types, so there is nothing to
5143 check.) This code just sets COMPLETE_P; errors (if necessary)
5144 are issued below. */
5145 if ((initialized || aggregate_definition_p)
5146 && !complete_p
5147 && COMPLETE_TYPE_P (complete_type (type)))
5149 complete_p = true;
5150 /* We will not yet have set TREE_READONLY on DECL if the type
5151 was "const", but incomplete, before this point. But, now, we
5152 have a complete type, so we can try again. */
5153 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
5156 if (initialized)
5157 /* Is it valid for this decl to have an initializer at all? */
5159 /* Don't allow initializations for incomplete types except for
5160 arrays which might be completed by the initialization. */
5161 if (complete_p)
5162 ; /* A complete type is ok. */
5163 else if (type_uses_auto (type))
5164 ; /* An auto type is ok. */
5165 else if (TREE_CODE (type) != ARRAY_TYPE)
5167 error ("variable %q#D has initializer but incomplete type", decl);
5168 type = TREE_TYPE (decl) = error_mark_node;
5170 else if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
5172 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
5173 error ("elements of array %q#D have incomplete type", decl);
5174 /* else we already gave an error in start_decl. */
5177 else if (aggregate_definition_p && !complete_p)
5179 if (type_uses_auto (type))
5180 gcc_assert (CLASS_PLACEHOLDER_TEMPLATE (type));
5181 else
5183 error ("aggregate %q#D has incomplete type and cannot be defined",
5184 decl);
5185 /* Change the type so that assemble_variable will give
5186 DECL an rtl we can live with: (mem (const_int 0)). */
5187 type = TREE_TYPE (decl) = error_mark_node;
5191 /* Create a new scope to hold this declaration if necessary.
5192 Whether or not a new scope is necessary cannot be determined
5193 until after the type has been completed; if the type is a
5194 specialization of a class template it is not until after
5195 instantiation has occurred that TYPE_HAS_NONTRIVIAL_DESTRUCTOR
5196 will be set correctly. */
5197 maybe_push_cleanup_level (type);
5200 /* Handle initialization of references. DECL, TYPE, and INIT have the
5201 same meaning as in cp_finish_decl. *CLEANUP must be NULL on entry,
5202 but will be set to a new CLEANUP_STMT if a temporary is created
5203 that must be destroyed subsequently.
5205 Returns an initializer expression to use to initialize DECL, or
5206 NULL if the initialization can be performed statically.
5208 Quotes on semantics can be found in ARM 8.4.3. */
5210 static tree
5211 grok_reference_init (tree decl, tree type, tree init, int flags)
5213 if (init == NULL_TREE)
5215 if ((DECL_LANG_SPECIFIC (decl) == 0
5216 || DECL_IN_AGGR_P (decl) == 0)
5217 && ! DECL_THIS_EXTERN (decl))
5218 error ("%qD declared as reference but not initialized", decl);
5219 return NULL_TREE;
5222 if (TREE_CODE (init) == TREE_LIST)
5223 init = build_x_compound_expr_from_list (init, ELK_INIT,
5224 tf_warning_or_error);
5226 tree ttype = TREE_TYPE (type);
5227 if (TREE_CODE (ttype) != ARRAY_TYPE
5228 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
5229 /* Note: default conversion is only called in very special cases. */
5230 init = decay_conversion (init, tf_warning_or_error);
5232 /* check_initializer handles this for non-reference variables, but for
5233 references we need to do it here or the initializer will get the
5234 incomplete array type and confuse later calls to
5235 cp_complete_array_type. */
5236 if (TREE_CODE (ttype) == ARRAY_TYPE
5237 && TYPE_DOMAIN (ttype) == NULL_TREE
5238 && (BRACE_ENCLOSED_INITIALIZER_P (init)
5239 || TREE_CODE (init) == STRING_CST))
5241 cp_complete_array_type (&ttype, init, false);
5242 if (ttype != TREE_TYPE (type))
5243 type = cp_build_reference_type (ttype, TYPE_REF_IS_RVALUE (type));
5246 /* Convert INIT to the reference type TYPE. This may involve the
5247 creation of a temporary, whose lifetime must be the same as that
5248 of the reference. If so, a DECL_EXPR for the temporary will be
5249 added just after the DECL_EXPR for DECL. That's why we don't set
5250 DECL_INITIAL for local references (instead assigning to them
5251 explicitly); we need to allow the temporary to be initialized
5252 first. */
5253 return initialize_reference (type, init, flags,
5254 tf_warning_or_error);
5257 /* Designated initializers in arrays are not supported in GNU C++.
5258 The parser cannot detect this error since it does not know whether
5259 a given brace-enclosed initializer is for a class type or for an
5260 array. This function checks that CE does not use a designated
5261 initializer. If it does, an error is issued. Returns true if CE
5262 is valid, i.e., does not have a designated initializer. */
5264 static bool
5265 check_array_designated_initializer (constructor_elt *ce,
5266 unsigned HOST_WIDE_INT index)
5268 /* Designated initializers for array elements are not supported. */
5269 if (ce->index)
5271 /* The parser only allows identifiers as designated
5272 initializers. */
5273 if (ce->index == error_mark_node)
5275 error ("name used in a GNU-style designated "
5276 "initializer for an array");
5277 return false;
5279 else if (identifier_p (ce->index))
5281 error ("name %qD used in a GNU-style designated "
5282 "initializer for an array", ce->index);
5283 return false;
5286 tree ce_index = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5287 ce->index, true);
5288 if (ce_index
5289 && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (ce_index))
5290 && (TREE_CODE (ce_index = maybe_constant_value (ce_index))
5291 == INTEGER_CST))
5293 /* A C99 designator is OK if it matches the current index. */
5294 if (wi::eq_p (ce_index, index))
5295 return true;
5296 else
5297 sorry ("non-trivial designated initializers not supported");
5299 else
5300 error ("C99 designator %qE is not an integral constant-expression",
5301 ce->index);
5303 return false;
5306 return true;
5309 /* When parsing `int a[] = {1, 2};' we don't know the size of the
5310 array until we finish parsing the initializer. If that's the
5311 situation we're in, update DECL accordingly. */
5313 static void
5314 maybe_deduce_size_from_array_init (tree decl, tree init)
5316 tree type = TREE_TYPE (decl);
5318 if (TREE_CODE (type) == ARRAY_TYPE
5319 && TYPE_DOMAIN (type) == NULL_TREE
5320 && TREE_CODE (decl) != TYPE_DECL)
5322 /* do_default is really a C-ism to deal with tentative definitions.
5323 But let's leave it here to ease the eventual merge. */
5324 int do_default = !DECL_EXTERNAL (decl);
5325 tree initializer = init ? init : DECL_INITIAL (decl);
5326 int failure = 0;
5328 /* Check that there are no designated initializers in INIT, as
5329 those are not supported in GNU C++, and as the middle-end
5330 will crash if presented with a non-numeric designated
5331 initializer. */
5332 if (initializer && BRACE_ENCLOSED_INITIALIZER_P (initializer))
5334 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initializer);
5335 constructor_elt *ce;
5336 HOST_WIDE_INT i;
5337 FOR_EACH_VEC_SAFE_ELT (v, i, ce)
5338 if (!check_array_designated_initializer (ce, i))
5339 failure = 1;
5342 if (!failure)
5344 failure = cp_complete_array_type (&TREE_TYPE (decl), initializer,
5345 do_default);
5346 if (failure == 1)
5348 error_at (EXPR_LOC_OR_LOC (initializer,
5349 DECL_SOURCE_LOCATION (decl)),
5350 "initializer fails to determine size of %qD", decl);
5352 else if (failure == 2)
5354 if (do_default)
5356 error_at (DECL_SOURCE_LOCATION (decl),
5357 "array size missing in %qD", decl);
5359 /* If a `static' var's size isn't known, make it extern as
5360 well as static, so it does not get allocated. If it's not
5361 `static', then don't mark it extern; finish_incomplete_decl
5362 will give it a default size and it will get allocated. */
5363 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
5364 DECL_EXTERNAL (decl) = 1;
5366 else if (failure == 3)
5368 error_at (DECL_SOURCE_LOCATION (decl),
5369 "zero-size array %qD", decl);
5373 cp_apply_type_quals_to_decl (cp_type_quals (TREE_TYPE (decl)), decl);
5375 relayout_decl (decl);
5379 /* Set DECL_SIZE, DECL_ALIGN, etc. for DECL (a VAR_DECL), and issue
5380 any appropriate error messages regarding the layout. */
5382 static void
5383 layout_var_decl (tree decl)
5385 tree type;
5387 type = TREE_TYPE (decl);
5388 if (type == error_mark_node)
5389 return;
5391 /* If we haven't already laid out this declaration, do so now.
5392 Note that we must not call complete type for an external object
5393 because it's type might involve templates that we are not
5394 supposed to instantiate yet. (And it's perfectly valid to say
5395 `extern X x' for some incomplete type `X'.) */
5396 if (!DECL_EXTERNAL (decl))
5397 complete_type (type);
5398 if (!DECL_SIZE (decl)
5399 && TREE_TYPE (decl) != error_mark_node
5400 && complete_or_array_type_p (type))
5401 layout_decl (decl, 0);
5403 if (!DECL_EXTERNAL (decl) && DECL_SIZE (decl) == NULL_TREE)
5405 /* An automatic variable with an incomplete type: that is an error.
5406 Don't talk about array types here, since we took care of that
5407 message in grokdeclarator. */
5408 error_at (DECL_SOURCE_LOCATION (decl),
5409 "storage size of %qD isn%'t known", decl);
5410 TREE_TYPE (decl) = error_mark_node;
5412 #if 0
5413 /* Keep this code around in case we later want to control debug info
5414 based on whether a type is "used". (jason 1999-11-11) */
5416 else if (!DECL_EXTERNAL (decl) && MAYBE_CLASS_TYPE_P (ttype))
5417 /* Let debugger know it should output info for this type. */
5418 note_debug_info_needed (ttype);
5420 if (TREE_STATIC (decl) && DECL_CLASS_SCOPE_P (decl))
5421 note_debug_info_needed (DECL_CONTEXT (decl));
5422 #endif
5424 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
5425 && DECL_SIZE (decl) != NULL_TREE
5426 && ! TREE_CONSTANT (DECL_SIZE (decl)))
5428 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
5429 constant_expression_warning (DECL_SIZE (decl));
5430 else
5432 error_at (DECL_SOURCE_LOCATION (decl),
5433 "storage size of %qD isn%'t constant", decl);
5434 TREE_TYPE (decl) = error_mark_node;
5439 /* If a local static variable is declared in an inline function, or if
5440 we have a weak definition, we must endeavor to create only one
5441 instance of the variable at link-time. */
5443 void
5444 maybe_commonize_var (tree decl)
5446 /* Static data in a function with comdat linkage also has comdat
5447 linkage. */
5448 if ((TREE_STATIC (decl)
5449 /* Don't mess with __FUNCTION__. */
5450 && ! DECL_ARTIFICIAL (decl)
5451 && DECL_FUNCTION_SCOPE_P (decl)
5452 && vague_linkage_p (DECL_CONTEXT (decl)))
5453 || (TREE_PUBLIC (decl) && DECL_INLINE_VAR_P (decl)))
5455 if (flag_weak)
5457 /* With weak symbols, we simply make the variable COMDAT;
5458 that will cause copies in multiple translations units to
5459 be merged. */
5460 comdat_linkage (decl);
5462 else
5464 if (DECL_INITIAL (decl) == NULL_TREE
5465 || DECL_INITIAL (decl) == error_mark_node)
5467 /* Without weak symbols, we can use COMMON to merge
5468 uninitialized variables. */
5469 TREE_PUBLIC (decl) = 1;
5470 DECL_COMMON (decl) = 1;
5472 else
5474 /* While for initialized variables, we must use internal
5475 linkage -- which means that multiple copies will not
5476 be merged. */
5477 TREE_PUBLIC (decl) = 0;
5478 DECL_COMMON (decl) = 0;
5479 const char *msg;
5480 if (DECL_INLINE_VAR_P (decl))
5481 msg = G_("sorry: semantics of inline variable "
5482 "%q#D are wrong (you%'ll wind up with "
5483 "multiple copies)");
5484 else
5485 msg = G_("sorry: semantics of inline function "
5486 "static data %q#D are wrong (you%'ll wind "
5487 "up with multiple copies)");
5488 if (warning_at (DECL_SOURCE_LOCATION (decl), 0,
5489 msg, decl))
5490 inform (DECL_SOURCE_LOCATION (decl),
5491 "you can work around this by removing the initializer");
5497 /* Issue an error message if DECL is an uninitialized const variable. */
5499 static void
5500 check_for_uninitialized_const_var (tree decl)
5502 tree type = strip_array_types (TREE_TYPE (decl));
5504 /* ``Unless explicitly declared extern, a const object does not have
5505 external linkage and must be initialized. ($8.4; $12.1)'' ARM
5506 7.1.6 */
5507 if (VAR_P (decl)
5508 && TREE_CODE (type) != REFERENCE_TYPE
5509 && (CP_TYPE_CONST_P (type) || var_in_constexpr_fn (decl))
5510 && !DECL_INITIAL (decl))
5512 tree field = default_init_uninitialized_part (type);
5513 if (!field)
5514 return;
5516 if (CP_TYPE_CONST_P (type))
5517 permerror (DECL_SOURCE_LOCATION (decl),
5518 "uninitialized const %qD", decl);
5519 else
5521 error_at (DECL_SOURCE_LOCATION (decl),
5522 "uninitialized variable %qD in %<constexpr%> function",
5523 decl);
5524 cp_function_chain->invalid_constexpr = true;
5527 if (CLASS_TYPE_P (type))
5529 tree defaulted_ctor;
5531 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
5532 "%q#T has no user-provided default constructor", type);
5533 defaulted_ctor = in_class_defaulted_default_constructor (type);
5534 if (defaulted_ctor)
5535 inform (DECL_SOURCE_LOCATION (defaulted_ctor),
5536 "constructor is not user-provided because it is "
5537 "explicitly defaulted in the class body");
5538 inform (DECL_SOURCE_LOCATION (field),
5539 "and the implicitly-defined constructor does not "
5540 "initialize %q#D", field);
5545 /* Structure holding the current initializer being processed by reshape_init.
5546 CUR is a pointer to the current element being processed, END is a pointer
5547 after the last element present in the initializer. */
5548 struct reshape_iter
5550 constructor_elt *cur;
5551 constructor_elt *end;
5554 static tree reshape_init_r (tree, reshape_iter *, bool, tsubst_flags_t);
5556 /* FIELD is a FIELD_DECL or NULL. In the former case, the value
5557 returned is the next FIELD_DECL (possibly FIELD itself) that can be
5558 initialized. If there are no more such fields, the return value
5559 will be NULL. */
5561 tree
5562 next_initializable_field (tree field)
5564 while (field
5565 && (TREE_CODE (field) != FIELD_DECL
5566 || (DECL_C_BIT_FIELD (field) && !DECL_NAME (field))
5567 || (DECL_ARTIFICIAL (field)
5568 && !(cxx_dialect >= cxx1z && DECL_FIELD_IS_BASE (field)))))
5569 field = DECL_CHAIN (field);
5571 return field;
5574 /* Return true for [dcl.init.list] direct-list-initialization from
5575 single element of enumeration with a fixed underlying type. */
5577 bool
5578 is_direct_enum_init (tree type, tree init)
5580 if (cxx_dialect >= cxx1z
5581 && TREE_CODE (type) == ENUMERAL_TYPE
5582 && ENUM_FIXED_UNDERLYING_TYPE_P (type)
5583 && TREE_CODE (init) == CONSTRUCTOR
5584 && CONSTRUCTOR_IS_DIRECT_INIT (init)
5585 && CONSTRUCTOR_NELTS (init) == 1)
5586 return true;
5587 return false;
5590 /* Subroutine of reshape_init_array and reshape_init_vector, which does
5591 the actual work. ELT_TYPE is the element type of the array. MAX_INDEX is an
5592 INTEGER_CST representing the size of the array minus one (the maximum index),
5593 or NULL_TREE if the array was declared without specifying the size. D is
5594 the iterator within the constructor. */
5596 static tree
5597 reshape_init_array_1 (tree elt_type, tree max_index, reshape_iter *d,
5598 tsubst_flags_t complain)
5600 tree new_init;
5601 bool sized_array_p = (max_index && TREE_CONSTANT (max_index));
5602 unsigned HOST_WIDE_INT max_index_cst = 0;
5603 unsigned HOST_WIDE_INT index;
5605 /* The initializer for an array is always a CONSTRUCTOR. */
5606 new_init = build_constructor (init_list_type_node, NULL);
5608 if (sized_array_p)
5610 /* Minus 1 is used for zero sized arrays. */
5611 if (integer_all_onesp (max_index))
5612 return new_init;
5614 if (tree_fits_uhwi_p (max_index))
5615 max_index_cst = tree_to_uhwi (max_index);
5616 /* sizetype is sign extended, not zero extended. */
5617 else
5618 max_index_cst = tree_to_uhwi (fold_convert (size_type_node, max_index));
5621 /* Loop until there are no more initializers. */
5622 for (index = 0;
5623 d->cur != d->end && (!sized_array_p || index <= max_index_cst);
5624 ++index)
5626 tree elt_init;
5627 constructor_elt *old_cur = d->cur;
5629 check_array_designated_initializer (d->cur, index);
5630 elt_init = reshape_init_r (elt_type, d, /*first_initializer_p=*/false,
5631 complain);
5632 if (elt_init == error_mark_node)
5633 return error_mark_node;
5634 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init),
5635 size_int (index), elt_init);
5636 if (!TREE_CONSTANT (elt_init))
5637 TREE_CONSTANT (new_init) = false;
5639 /* This can happen with an invalid initializer (c++/54501). */
5640 if (d->cur == old_cur && !sized_array_p)
5641 break;
5644 return new_init;
5647 /* Subroutine of reshape_init_r, processes the initializers for arrays.
5648 Parameters are the same of reshape_init_r. */
5650 static tree
5651 reshape_init_array (tree type, reshape_iter *d, tsubst_flags_t complain)
5653 tree max_index = NULL_TREE;
5655 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
5657 if (TYPE_DOMAIN (type))
5658 max_index = array_type_nelts (type);
5660 return reshape_init_array_1 (TREE_TYPE (type), max_index, d, complain);
5663 /* Subroutine of reshape_init_r, processes the initializers for vectors.
5664 Parameters are the same of reshape_init_r. */
5666 static tree
5667 reshape_init_vector (tree type, reshape_iter *d, tsubst_flags_t complain)
5669 tree max_index = NULL_TREE;
5671 gcc_assert (VECTOR_TYPE_P (type));
5673 if (COMPOUND_LITERAL_P (d->cur->value))
5675 tree value = d->cur->value;
5676 if (!same_type_p (TREE_TYPE (value), type))
5678 if (complain & tf_error)
5679 error ("invalid type %qT as initializer for a vector of type %qT",
5680 TREE_TYPE (d->cur->value), type);
5681 value = error_mark_node;
5683 ++d->cur;
5684 return value;
5687 /* For a vector, we initialize it as an array of the appropriate size. */
5688 if (VECTOR_TYPE_P (type))
5689 max_index = size_int (TYPE_VECTOR_SUBPARTS (type) - 1);
5691 return reshape_init_array_1 (TREE_TYPE (type), max_index, d, complain);
5694 /* Subroutine of reshape_init_r, processes the initializers for classes
5695 or union. Parameters are the same of reshape_init_r. */
5697 static tree
5698 reshape_init_class (tree type, reshape_iter *d, bool first_initializer_p,
5699 tsubst_flags_t complain)
5701 tree field;
5702 tree new_init;
5704 gcc_assert (CLASS_TYPE_P (type));
5706 /* The initializer for a class is always a CONSTRUCTOR. */
5707 new_init = build_constructor (init_list_type_node, NULL);
5708 field = next_initializable_field (TYPE_FIELDS (type));
5710 if (!field)
5712 /* [dcl.init.aggr]
5714 An initializer for an aggregate member that is an
5715 empty class shall have the form of an empty
5716 initializer-list {}. */
5717 if (!first_initializer_p)
5719 if (complain & tf_error)
5720 error ("initializer for %qT must be brace-enclosed", type);
5721 return error_mark_node;
5723 return new_init;
5726 /* Loop through the initializable fields, gathering initializers. */
5727 while (d->cur != d->end)
5729 tree field_init;
5730 constructor_elt *old_cur = d->cur;
5732 /* Handle designated initializers, as an extension. */
5733 if (d->cur->index)
5735 if (d->cur->index == error_mark_node)
5736 return error_mark_node;
5738 if (TREE_CODE (d->cur->index) == FIELD_DECL)
5739 /* We already reshaped this. */
5740 gcc_assert (d->cur->index == field);
5741 else if (TREE_CODE (d->cur->index) == IDENTIFIER_NODE)
5742 field = lookup_field_1 (type, d->cur->index, /*want_type=*/false);
5743 else
5745 if (complain & tf_error)
5746 error ("%<[%E] =%> used in a GNU-style designated initializer"
5747 " for class %qT", d->cur->index, type);
5748 return error_mark_node;
5751 if (!field || TREE_CODE (field) != FIELD_DECL)
5753 if (complain & tf_error)
5754 error ("%qT has no non-static data member named %qD", type,
5755 d->cur->index);
5756 return error_mark_node;
5760 /* If we processed all the member of the class, we are done. */
5761 if (!field)
5762 break;
5764 field_init = reshape_init_r (TREE_TYPE (field), d,
5765 /*first_initializer_p=*/false, complain);
5766 if (field_init == error_mark_node)
5767 return error_mark_node;
5769 if (d->cur == old_cur && d->cur->index)
5771 /* This can happen with an invalid initializer for a flexible
5772 array member (c++/54441). */
5773 if (complain & tf_error)
5774 error ("invalid initializer for %q#D", field);
5775 return error_mark_node;
5778 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init), field, field_init);
5780 /* [dcl.init.aggr]
5782 When a union is initialized with a brace-enclosed
5783 initializer, the braces shall only contain an
5784 initializer for the first member of the union. */
5785 if (TREE_CODE (type) == UNION_TYPE)
5786 break;
5788 field = next_initializable_field (DECL_CHAIN (field));
5791 return new_init;
5794 /* Subroutine of reshape_init_r. We're in a context where C99 initializer
5795 designators are not valid; either complain or return true to indicate
5796 that reshape_init_r should return error_mark_node. */
5798 static bool
5799 has_designator_problem (reshape_iter *d, tsubst_flags_t complain)
5801 if (d->cur->index)
5803 if (complain & tf_error)
5804 error ("C99 designator %qE outside aggregate initializer",
5805 d->cur->index);
5806 else
5807 return true;
5809 return false;
5812 /* Subroutine of reshape_init, which processes a single initializer (part of
5813 a CONSTRUCTOR). TYPE is the type of the variable being initialized, D is the
5814 iterator within the CONSTRUCTOR which points to the initializer to process.
5815 FIRST_INITIALIZER_P is true if this is the first initializer of the
5816 outermost CONSTRUCTOR node. */
5818 static tree
5819 reshape_init_r (tree type, reshape_iter *d, bool first_initializer_p,
5820 tsubst_flags_t complain)
5822 tree init = d->cur->value;
5824 if (error_operand_p (init))
5825 return error_mark_node;
5827 if (first_initializer_p && !CP_AGGREGATE_TYPE_P (type)
5828 && has_designator_problem (d, complain))
5829 return error_mark_node;
5831 if (TREE_CODE (type) == COMPLEX_TYPE)
5833 /* A complex type can be initialized from one or two initializers,
5834 but braces are not elided. */
5835 d->cur++;
5836 if (BRACE_ENCLOSED_INITIALIZER_P (init))
5838 if (CONSTRUCTOR_NELTS (init) > 2)
5840 if (complain & tf_error)
5841 error ("too many initializers for %qT", type);
5842 else
5843 return error_mark_node;
5846 else if (first_initializer_p && d->cur != d->end)
5848 vec<constructor_elt, va_gc> *v = 0;
5849 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, init);
5850 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, d->cur->value);
5851 if (has_designator_problem (d, complain))
5852 return error_mark_node;
5853 d->cur++;
5854 init = build_constructor (init_list_type_node, v);
5856 return init;
5859 /* A non-aggregate type is always initialized with a single
5860 initializer. */
5861 if (!CP_AGGREGATE_TYPE_P (type))
5863 /* It is invalid to initialize a non-aggregate type with a
5864 brace-enclosed initializer before C++0x.
5865 We need to check for BRACE_ENCLOSED_INITIALIZER_P here because
5866 of g++.old-deja/g++.mike/p7626.C: a pointer-to-member constant is
5867 a CONSTRUCTOR (with a record type). */
5868 if (TREE_CODE (init) == CONSTRUCTOR
5869 /* Don't complain about a capture-init. */
5870 && !CONSTRUCTOR_IS_DIRECT_INIT (init)
5871 && BRACE_ENCLOSED_INITIALIZER_P (init)) /* p7626.C */
5873 if (SCALAR_TYPE_P (type))
5875 if (cxx_dialect < cxx11
5876 /* Isn't value-initialization. */
5877 || CONSTRUCTOR_NELTS (init) > 0)
5879 if (complain & tf_error)
5880 error ("braces around scalar initializer for type %qT",
5881 type);
5882 init = error_mark_node;
5885 else
5886 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5889 d->cur++;
5890 return init;
5893 /* "If T is a class type and the initializer list has a single element of
5894 type cv U, where U is T or a class derived from T, the object is
5895 initialized from that element." Even if T is an aggregate. */
5896 if (cxx_dialect >= cxx11 && (CLASS_TYPE_P (type) || VECTOR_TYPE_P (type))
5897 && first_initializer_p
5898 && d->end - d->cur == 1
5899 && reference_related_p (type, TREE_TYPE (init)))
5901 d->cur++;
5902 return init;
5905 /* [dcl.init.aggr]
5907 All implicit type conversions (clause _conv_) are considered when
5908 initializing the aggregate member with an initializer from an
5909 initializer-list. If the initializer can initialize a member,
5910 the member is initialized. Otherwise, if the member is itself a
5911 non-empty subaggregate, brace elision is assumed and the
5912 initializer is considered for the initialization of the first
5913 member of the subaggregate. */
5914 if (TREE_CODE (init) != CONSTRUCTOR
5915 /* But don't try this for the first initializer, since that would be
5916 looking through the outermost braces; A a2 = { a1 }; is not a
5917 valid aggregate initialization. */
5918 && !first_initializer_p
5919 && (same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (init))
5920 || can_convert_arg (type, TREE_TYPE (init), init, LOOKUP_NORMAL,
5921 complain)))
5923 d->cur++;
5924 return init;
5927 /* [dcl.init.string]
5929 A char array (whether plain char, signed char, or unsigned char)
5930 can be initialized by a string-literal (optionally enclosed in
5931 braces); a wchar_t array can be initialized by a wide
5932 string-literal (optionally enclosed in braces). */
5933 if (TREE_CODE (type) == ARRAY_TYPE
5934 && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type))))
5936 tree str_init = init;
5938 /* Strip one level of braces if and only if they enclose a single
5939 element (as allowed by [dcl.init.string]). */
5940 if (!first_initializer_p
5941 && TREE_CODE (str_init) == CONSTRUCTOR
5942 && CONSTRUCTOR_NELTS (str_init) == 1)
5944 str_init = (*CONSTRUCTOR_ELTS (str_init))[0].value;
5947 /* If it's a string literal, then it's the initializer for the array
5948 as a whole. Otherwise, continue with normal initialization for
5949 array types (one value per array element). */
5950 if (TREE_CODE (str_init) == STRING_CST)
5952 if (has_designator_problem (d, complain))
5953 return error_mark_node;
5954 d->cur++;
5955 return str_init;
5959 /* The following cases are about aggregates. If we are not within a full
5960 initializer already, and there is not a CONSTRUCTOR, it means that there
5961 is a missing set of braces (that is, we are processing the case for
5962 which reshape_init exists). */
5963 if (!first_initializer_p)
5965 if (TREE_CODE (init) == CONSTRUCTOR)
5967 if (TREE_TYPE (init) && TYPE_PTRMEMFUNC_P (TREE_TYPE (init)))
5968 /* There is no need to reshape pointer-to-member function
5969 initializers, as they are always constructed correctly
5970 by the front end. */
5972 else if (COMPOUND_LITERAL_P (init))
5973 /* For a nested compound literal, there is no need to reshape since
5974 brace elision is not allowed. Even if we decided to allow it,
5975 we should add a call to reshape_init in finish_compound_literal,
5976 before calling digest_init, so changing this code would still
5977 not be necessary. */
5978 gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (init));
5979 else
5981 ++d->cur;
5982 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
5983 return reshape_init (type, init, complain);
5987 if (complain & tf_warning)
5988 warning (OPT_Wmissing_braces,
5989 "missing braces around initializer for %qT",
5990 type);
5993 /* Dispatch to specialized routines. */
5994 if (CLASS_TYPE_P (type))
5995 return reshape_init_class (type, d, first_initializer_p, complain);
5996 else if (TREE_CODE (type) == ARRAY_TYPE)
5997 return reshape_init_array (type, d, complain);
5998 else if (VECTOR_TYPE_P (type))
5999 return reshape_init_vector (type, d, complain);
6000 else
6001 gcc_unreachable();
6004 /* Undo the brace-elision allowed by [dcl.init.aggr] in a
6005 brace-enclosed aggregate initializer.
6007 INIT is the CONSTRUCTOR containing the list of initializers describing
6008 a brace-enclosed initializer for an entity of the indicated aggregate TYPE.
6009 It may not presently match the shape of the TYPE; for example:
6011 struct S { int a; int b; };
6012 struct S a[] = { 1, 2, 3, 4 };
6014 Here INIT will hold a vector of four elements, rather than a
6015 vector of two elements, each itself a vector of two elements. This
6016 routine transforms INIT from the former form into the latter. The
6017 revised CONSTRUCTOR node is returned. */
6019 tree
6020 reshape_init (tree type, tree init, tsubst_flags_t complain)
6022 vec<constructor_elt, va_gc> *v;
6023 reshape_iter d;
6024 tree new_init;
6026 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
6028 v = CONSTRUCTOR_ELTS (init);
6030 /* An empty constructor does not need reshaping, and it is always a valid
6031 initializer. */
6032 if (vec_safe_is_empty (v))
6033 return init;
6035 /* Handle [dcl.init.list] direct-list-initialization from
6036 single element of enumeration with a fixed underlying type. */
6037 if (is_direct_enum_init (type, init))
6039 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
6040 type = cv_unqualified (type);
6041 if (check_narrowing (ENUM_UNDERLYING_TYPE (type), elt, complain))
6042 return cp_build_c_cast (type, elt, tf_warning_or_error);
6043 else
6044 return error_mark_node;
6047 /* Recurse on this CONSTRUCTOR. */
6048 d.cur = &(*v)[0];
6049 d.end = d.cur + v->length ();
6051 new_init = reshape_init_r (type, &d, true, complain);
6052 if (new_init == error_mark_node)
6053 return error_mark_node;
6055 /* Make sure all the element of the constructor were used. Otherwise,
6056 issue an error about exceeding initializers. */
6057 if (d.cur != d.end)
6059 if (complain & tf_error)
6060 error ("too many initializers for %qT", type);
6061 return error_mark_node;
6064 if (CONSTRUCTOR_IS_DIRECT_INIT (init)
6065 && BRACE_ENCLOSED_INITIALIZER_P (new_init))
6066 CONSTRUCTOR_IS_DIRECT_INIT (new_init) = true;
6068 return new_init;
6071 /* Verify array initializer. Returns true if errors have been reported. */
6073 bool
6074 check_array_initializer (tree decl, tree type, tree init)
6076 tree element_type = TREE_TYPE (type);
6078 /* The array type itself need not be complete, because the
6079 initializer may tell us how many elements are in the array.
6080 But, the elements of the array must be complete. */
6081 if (!COMPLETE_TYPE_P (complete_type (element_type)))
6083 if (decl)
6084 error_at (DECL_SOURCE_LOCATION (decl),
6085 "elements of array %q#D have incomplete type", decl);
6086 else
6087 error ("elements of array %q#T have incomplete type", type);
6088 return true;
6090 /* A compound literal can't have variable size. */
6091 if (init && !decl
6092 && ((COMPLETE_TYPE_P (type) && !TREE_CONSTANT (TYPE_SIZE (type)))
6093 || !TREE_CONSTANT (TYPE_SIZE (element_type))))
6095 error ("variable-sized compound literal");
6096 return true;
6098 return false;
6101 /* Subroutine of check_initializer; args are passed down from that function.
6102 Set stmts_are_full_exprs_p to 1 across a call to build_aggr_init. */
6104 static tree
6105 build_aggr_init_full_exprs (tree decl, tree init, int flags)
6108 gcc_assert (stmts_are_full_exprs_p ());
6109 return build_aggr_init (decl, init, flags, tf_warning_or_error);
6112 /* Verify INIT (the initializer for DECL), and record the
6113 initialization in DECL_INITIAL, if appropriate. CLEANUP is as for
6114 grok_reference_init.
6116 If the return value is non-NULL, it is an expression that must be
6117 evaluated dynamically to initialize DECL. */
6119 static tree
6120 check_initializer (tree decl, tree init, int flags, vec<tree, va_gc> **cleanups)
6122 tree type = TREE_TYPE (decl);
6123 tree init_code = NULL;
6124 tree core_type;
6126 /* Things that are going to be initialized need to have complete
6127 type. */
6128 TREE_TYPE (decl) = type = complete_type (TREE_TYPE (decl));
6130 if (DECL_HAS_VALUE_EXPR_P (decl))
6132 /* A variable with DECL_HAS_VALUE_EXPR_P set is just a placeholder,
6133 it doesn't have storage to be initialized. */
6134 gcc_assert (init == NULL_TREE);
6135 return NULL_TREE;
6138 if (type == error_mark_node)
6139 /* We will have already complained. */
6140 return NULL_TREE;
6142 if (TREE_CODE (type) == ARRAY_TYPE)
6144 if (check_array_initializer (decl, type, init))
6145 return NULL_TREE;
6147 else if (!COMPLETE_TYPE_P (type))
6149 error_at (DECL_SOURCE_LOCATION (decl),
6150 "%q#D has incomplete type", decl);
6151 TREE_TYPE (decl) = error_mark_node;
6152 return NULL_TREE;
6154 else
6155 /* There is no way to make a variable-sized class type in GNU C++. */
6156 gcc_assert (TREE_CONSTANT (TYPE_SIZE (type)));
6158 if (init && BRACE_ENCLOSED_INITIALIZER_P (init))
6160 int init_len = CONSTRUCTOR_NELTS (init);
6161 if (SCALAR_TYPE_P (type))
6163 if (init_len == 0)
6165 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6166 init = build_zero_init (type, NULL_TREE, false);
6168 else if (init_len != 1 && TREE_CODE (type) != COMPLEX_TYPE)
6170 error_at (EXPR_LOC_OR_LOC (init, DECL_SOURCE_LOCATION (decl)),
6171 "scalar object %qD requires one element in "
6172 "initializer", decl);
6173 TREE_TYPE (decl) = error_mark_node;
6174 return NULL_TREE;
6179 if (TREE_CODE (decl) == CONST_DECL)
6181 gcc_assert (TREE_CODE (type) != REFERENCE_TYPE);
6183 DECL_INITIAL (decl) = init;
6185 gcc_assert (init != NULL_TREE);
6186 init = NULL_TREE;
6188 else if (!init && DECL_REALLY_EXTERN (decl))
6190 else if (init || type_build_ctor_call (type)
6191 || TREE_CODE (type) == REFERENCE_TYPE)
6193 if (TREE_CODE (type) == REFERENCE_TYPE)
6195 init = grok_reference_init (decl, type, init, flags);
6196 flags |= LOOKUP_ALREADY_DIGESTED;
6198 else if (!init)
6199 check_for_uninitialized_const_var (decl);
6200 /* Do not reshape constructors of vectors (they don't need to be
6201 reshaped. */
6202 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
6204 if (is_std_init_list (type))
6206 init = perform_implicit_conversion (type, init,
6207 tf_warning_or_error);
6208 flags |= LOOKUP_ALREADY_DIGESTED;
6210 else if (TYPE_NON_AGGREGATE_CLASS (type))
6212 /* Don't reshape if the class has constructors. */
6213 if (cxx_dialect == cxx98)
6214 error_at (EXPR_LOC_OR_LOC (init, DECL_SOURCE_LOCATION (decl)),
6215 "in C++98 %qD must be initialized by "
6216 "constructor, not by %<{...}%>",
6217 decl);
6219 else if (VECTOR_TYPE_P (type) && TYPE_VECTOR_OPAQUE (type))
6221 error ("opaque vector types cannot be initialized");
6222 init = error_mark_node;
6224 else
6226 init = reshape_init (type, init, tf_warning_or_error);
6227 flags |= LOOKUP_NO_NARROWING;
6230 else if (TREE_CODE (init) == TREE_LIST
6231 && TREE_TYPE (init) != unknown_type_node
6232 && !MAYBE_CLASS_TYPE_P (type))
6234 gcc_assert (TREE_CODE (decl) != RESULT_DECL);
6236 /* We get here with code like `int a (2);' */
6237 init = build_x_compound_expr_from_list (init, ELK_INIT,
6238 tf_warning_or_error);
6241 /* If DECL has an array type without a specific bound, deduce the
6242 array size from the initializer. */
6243 maybe_deduce_size_from_array_init (decl, init);
6244 type = TREE_TYPE (decl);
6245 if (type == error_mark_node)
6246 return NULL_TREE;
6248 if (((type_build_ctor_call (type) || CLASS_TYPE_P (type))
6249 && !(flags & LOOKUP_ALREADY_DIGESTED)
6250 && !(init && BRACE_ENCLOSED_INITIALIZER_P (init)
6251 && CP_AGGREGATE_TYPE_P (type)
6252 && (CLASS_TYPE_P (type)
6253 || !TYPE_NEEDS_CONSTRUCTING (type)
6254 || type_has_extended_temps (type))))
6255 || (DECL_DECOMPOSITION_P (decl) && TREE_CODE (type) == ARRAY_TYPE))
6257 init_code = build_aggr_init_full_exprs (decl, init, flags);
6259 /* A constructor call is a non-trivial initializer even if
6260 it isn't explicitly written. */
6261 if (TREE_SIDE_EFFECTS (init_code))
6262 DECL_NONTRIVIALLY_INITIALIZED_P (decl) = true;
6264 /* If this is a constexpr initializer, expand_default_init will
6265 have returned an INIT_EXPR rather than a CALL_EXPR. In that
6266 case, pull the initializer back out and pass it down into
6267 store_init_value. */
6268 while (TREE_CODE (init_code) == EXPR_STMT
6269 || TREE_CODE (init_code) == CONVERT_EXPR)
6270 init_code = TREE_OPERAND (init_code, 0);
6271 if (TREE_CODE (init_code) == INIT_EXPR)
6273 init = TREE_OPERAND (init_code, 1);
6274 init_code = NULL_TREE;
6275 /* Don't call digest_init; it's unnecessary and will complain
6276 about aggregate initialization of non-aggregate classes. */
6277 flags |= LOOKUP_ALREADY_DIGESTED;
6279 else if (DECL_DECLARED_CONSTEXPR_P (decl))
6281 /* Declared constexpr, but no suitable initializer; massage
6282 init appropriately so we can pass it into store_init_value
6283 for the error. */
6284 if (CLASS_TYPE_P (type)
6285 && (!init || TREE_CODE (init) == TREE_LIST))
6287 init = build_functional_cast (type, init, tf_none);
6288 if (TREE_CODE (init) == TARGET_EXPR)
6289 TARGET_EXPR_DIRECT_INIT_P (init) = true;
6291 init_code = NULL_TREE;
6293 else
6294 init = NULL_TREE;
6297 if (init && TREE_CODE (init) != TREE_VEC)
6299 /* In aggregate initialization of a variable, each element
6300 initialization is a full-expression because there is no
6301 enclosing expression. */
6302 gcc_assert (stmts_are_full_exprs_p ());
6304 init_code = store_init_value (decl, init, cleanups, flags);
6306 if (pedantic && TREE_CODE (type) == ARRAY_TYPE
6307 && DECL_INITIAL (decl)
6308 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
6309 && PAREN_STRING_LITERAL_P (DECL_INITIAL (decl)))
6310 warning_at (EXPR_LOC_OR_LOC (DECL_INITIAL (decl),
6311 DECL_SOURCE_LOCATION (decl)),
6312 0, "array %qD initialized by parenthesized "
6313 "string literal %qE",
6314 decl, DECL_INITIAL (decl));
6315 init = NULL;
6318 else
6320 if (CLASS_TYPE_P (core_type = strip_array_types (type))
6321 && (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type)
6322 || CLASSTYPE_REF_FIELDS_NEED_INIT (core_type)))
6323 diagnose_uninitialized_cst_or_ref_member (core_type, /*using_new=*/false,
6324 /*complain=*/true);
6326 check_for_uninitialized_const_var (decl);
6329 if (init && init != error_mark_node)
6330 init_code = build2 (INIT_EXPR, type, decl, init);
6332 if (init_code)
6334 /* We might have set these in cp_finish_decl. */
6335 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = false;
6336 TREE_CONSTANT (decl) = false;
6339 if (init_code
6340 && (DECL_IN_AGGR_P (decl) && !DECL_VAR_DECLARED_INLINE_P (decl)))
6342 static int explained = 0;
6344 if (cxx_dialect < cxx11)
6345 error ("initializer invalid for static member with constructor");
6346 else if (cxx_dialect < cxx1z)
6347 error ("non-constant in-class initialization invalid for static "
6348 "member %qD", decl);
6349 else
6350 error ("non-constant in-class initialization invalid for non-inline "
6351 "static member %qD", decl);
6352 if (!explained)
6354 inform (input_location,
6355 "(an out of class initialization is required)");
6356 explained = 1;
6358 return NULL_TREE;
6361 return init_code;
6364 /* If DECL is not a local variable, give it RTL. */
6366 static void
6367 make_rtl_for_nonlocal_decl (tree decl, tree init, const char* asmspec)
6369 int toplev = toplevel_bindings_p ();
6370 int defer_p;
6372 /* Set the DECL_ASSEMBLER_NAME for the object. */
6373 if (asmspec)
6375 /* The `register' keyword, when used together with an
6376 asm-specification, indicates that the variable should be
6377 placed in a particular register. */
6378 if (VAR_P (decl) && DECL_REGISTER (decl))
6380 set_user_assembler_name (decl, asmspec);
6381 DECL_HARD_REGISTER (decl) = 1;
6383 else
6385 if (TREE_CODE (decl) == FUNCTION_DECL
6386 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
6387 set_builtin_user_assembler_name (decl, asmspec);
6388 set_user_assembler_name (decl, asmspec);
6392 /* Handle non-variables up front. */
6393 if (!VAR_P (decl))
6395 rest_of_decl_compilation (decl, toplev, at_eof);
6396 return;
6399 /* If we see a class member here, it should be a static data
6400 member. */
6401 if (DECL_LANG_SPECIFIC (decl) && DECL_IN_AGGR_P (decl))
6403 gcc_assert (TREE_STATIC (decl));
6404 /* An in-class declaration of a static data member should be
6405 external; it is only a declaration, and not a definition. */
6406 if (init == NULL_TREE)
6407 gcc_assert (DECL_EXTERNAL (decl)
6408 || !TREE_PUBLIC (decl)
6409 || DECL_INLINE_VAR_P (decl));
6412 /* We don't create any RTL for local variables. */
6413 if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
6414 return;
6416 /* We defer emission of local statics until the corresponding
6417 DECL_EXPR is expanded. But with constexpr its function might never
6418 be expanded, so go ahead and tell cgraph about the variable now. */
6419 defer_p = ((DECL_FUNCTION_SCOPE_P (decl)
6420 && !var_in_maybe_constexpr_fn (decl))
6421 || DECL_VIRTUAL_P (decl));
6423 /* Defer template instantiations. */
6424 if (DECL_LANG_SPECIFIC (decl)
6425 && DECL_IMPLICIT_INSTANTIATION (decl))
6426 defer_p = 1;
6428 /* If we're not deferring, go ahead and assemble the variable. */
6429 if (!defer_p)
6430 rest_of_decl_compilation (decl, toplev, at_eof);
6433 /* walk_tree helper for wrap_temporary_cleanups, below. */
6435 static tree
6436 wrap_cleanups_r (tree *stmt_p, int *walk_subtrees, void *data)
6438 /* Stop at types or full-expression boundaries. */
6439 if (TYPE_P (*stmt_p)
6440 || TREE_CODE (*stmt_p) == CLEANUP_POINT_EXPR)
6442 *walk_subtrees = 0;
6443 return NULL_TREE;
6446 if (TREE_CODE (*stmt_p) == TARGET_EXPR)
6448 tree guard = (tree)data;
6449 tree tcleanup = TARGET_EXPR_CLEANUP (*stmt_p);
6451 tcleanup = build2 (TRY_CATCH_EXPR, void_type_node, tcleanup, guard);
6452 /* Tell honor_protect_cleanup_actions to handle this as a separate
6453 cleanup. */
6454 TRY_CATCH_IS_CLEANUP (tcleanup) = 1;
6456 TARGET_EXPR_CLEANUP (*stmt_p) = tcleanup;
6459 return NULL_TREE;
6462 /* We're initializing a local variable which has a cleanup GUARD. If there
6463 are any temporaries used in the initializer INIT of this variable, we
6464 need to wrap their cleanups with TRY_CATCH_EXPR (, GUARD) so that the
6465 variable will be cleaned up properly if one of them throws.
6467 Unfortunately, there's no way to express this properly in terms of
6468 nesting, as the regions for the temporaries overlap the region for the
6469 variable itself; if there are two temporaries, the variable needs to be
6470 the first thing destroyed if either of them throws. However, we only
6471 want to run the variable's cleanup if it actually got constructed. So
6472 we need to guard the temporary cleanups with the variable's cleanup if
6473 they are run on the normal path, but not if they are run on the
6474 exceptional path. We implement this by telling
6475 honor_protect_cleanup_actions to strip the variable cleanup from the
6476 exceptional path. */
6478 static void
6479 wrap_temporary_cleanups (tree init, tree guard)
6481 cp_walk_tree_without_duplicates (&init, wrap_cleanups_r, (void *)guard);
6484 /* Generate code to initialize DECL (a local variable). */
6486 static void
6487 initialize_local_var (tree decl, tree init)
6489 tree type = TREE_TYPE (decl);
6490 tree cleanup;
6491 int already_used;
6493 gcc_assert (VAR_P (decl)
6494 || TREE_CODE (decl) == RESULT_DECL);
6495 gcc_assert (!TREE_STATIC (decl));
6497 if (DECL_SIZE (decl) == NULL_TREE)
6499 /* If we used it already as memory, it must stay in memory. */
6500 DECL_INITIAL (decl) = NULL_TREE;
6501 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
6502 return;
6505 if (type == error_mark_node)
6506 return;
6508 /* Compute and store the initial value. */
6509 already_used = TREE_USED (decl) || TREE_USED (type);
6510 if (TREE_USED (type))
6511 DECL_READ_P (decl) = 1;
6513 /* Generate a cleanup, if necessary. */
6514 cleanup = cxx_maybe_build_cleanup (decl, tf_warning_or_error);
6516 /* Perform the initialization. */
6517 if (init)
6519 tree rinit = (TREE_CODE (init) == INIT_EXPR
6520 ? TREE_OPERAND (init, 1) : NULL_TREE);
6521 if (rinit && !TREE_SIDE_EFFECTS (rinit))
6523 /* Stick simple initializers in DECL_INITIAL so that
6524 -Wno-init-self works (c++/34772). */
6525 gcc_assert (TREE_OPERAND (init, 0) == decl);
6526 DECL_INITIAL (decl) = rinit;
6528 if (warn_init_self && TREE_CODE (type) == REFERENCE_TYPE)
6530 STRIP_NOPS (rinit);
6531 if (rinit == decl)
6532 warning_at (DECL_SOURCE_LOCATION (decl),
6533 OPT_Winit_self,
6534 "reference %qD is initialized with itself", decl);
6537 else
6539 int saved_stmts_are_full_exprs_p;
6541 /* If we're only initializing a single object, guard the
6542 destructors of any temporaries used in its initializer with
6543 its destructor. This isn't right for arrays because each
6544 element initialization is a full-expression. */
6545 if (cleanup && TREE_CODE (type) != ARRAY_TYPE)
6546 wrap_temporary_cleanups (init, cleanup);
6548 gcc_assert (building_stmt_list_p ());
6549 saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
6550 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
6551 finish_expr_stmt (init);
6552 current_stmt_tree ()->stmts_are_full_exprs_p =
6553 saved_stmts_are_full_exprs_p;
6557 /* Set this to 0 so we can tell whether an aggregate which was
6558 initialized was ever used. Don't do this if it has a
6559 destructor, so we don't complain about the 'resource
6560 allocation is initialization' idiom. Now set
6561 attribute((unused)) on types so decls of that type will be
6562 marked used. (see TREE_USED, above.) */
6563 if (TYPE_NEEDS_CONSTRUCTING (type)
6564 && ! already_used
6565 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type)
6566 && DECL_NAME (decl))
6567 TREE_USED (decl) = 0;
6568 else if (already_used)
6569 TREE_USED (decl) = 1;
6571 if (cleanup)
6572 finish_decl_cleanup (decl, cleanup);
6575 /* DECL is a VAR_DECL for a compiler-generated variable with static
6576 storage duration (like a virtual table) whose initializer is a
6577 compile-time constant. Initialize the variable and provide it to the
6578 back end. */
6580 void
6581 initialize_artificial_var (tree decl, vec<constructor_elt, va_gc> *v)
6583 tree init;
6584 gcc_assert (DECL_ARTIFICIAL (decl));
6585 init = build_constructor (TREE_TYPE (decl), v);
6586 gcc_assert (TREE_CODE (init) == CONSTRUCTOR);
6587 DECL_INITIAL (decl) = init;
6588 DECL_INITIALIZED_P (decl) = 1;
6589 determine_visibility (decl);
6590 layout_var_decl (decl);
6591 maybe_commonize_var (decl);
6592 make_rtl_for_nonlocal_decl (decl, init, /*asmspec=*/NULL);
6595 /* INIT is the initializer for a variable, as represented by the
6596 parser. Returns true iff INIT is type-dependent. */
6598 static bool
6599 type_dependent_init_p (tree init)
6601 if (TREE_CODE (init) == TREE_LIST)
6602 /* A parenthesized initializer, e.g.: int i (3, 2); ? */
6603 return any_type_dependent_elements_p (init);
6604 else if (TREE_CODE (init) == CONSTRUCTOR)
6605 /* A brace-enclosed initializer, e.g.: int i = { 3 }; ? */
6607 if (dependent_type_p (TREE_TYPE (init)))
6608 return true;
6610 vec<constructor_elt, va_gc> *elts;
6611 size_t nelts;
6612 size_t i;
6614 elts = CONSTRUCTOR_ELTS (init);
6615 nelts = vec_safe_length (elts);
6616 for (i = 0; i < nelts; ++i)
6617 if (type_dependent_init_p ((*elts)[i].value))
6618 return true;
6620 else
6621 /* It must be a simple expression, e.g., int i = 3; */
6622 return type_dependent_expression_p (init);
6624 return false;
6627 /* INIT is the initializer for a variable, as represented by the
6628 parser. Returns true iff INIT is value-dependent. */
6630 static bool
6631 value_dependent_init_p (tree init)
6633 if (TREE_CODE (init) == TREE_LIST)
6634 /* A parenthesized initializer, e.g.: int i (3, 2); ? */
6635 return any_value_dependent_elements_p (init);
6636 else if (TREE_CODE (init) == CONSTRUCTOR)
6637 /* A brace-enclosed initializer, e.g.: int i = { 3 }; ? */
6639 vec<constructor_elt, va_gc> *elts;
6640 size_t nelts;
6641 size_t i;
6643 elts = CONSTRUCTOR_ELTS (init);
6644 nelts = vec_safe_length (elts);
6645 for (i = 0; i < nelts; ++i)
6646 if (value_dependent_init_p ((*elts)[i].value))
6647 return true;
6649 else
6650 /* It must be a simple expression, e.g., int i = 3; */
6651 return value_dependent_expression_p (init);
6653 return false;
6656 // Returns true if a DECL is VAR_DECL with the concept specifier.
6657 static inline bool
6658 is_concept_var (tree decl)
6660 return (VAR_P (decl)
6661 // Not all variables have DECL_LANG_SPECIFIC.
6662 && DECL_LANG_SPECIFIC (decl)
6663 && DECL_DECLARED_CONCEPT_P (decl));
6666 /* A helper function to be called via walk_tree. If any label exists
6667 under *TP, it is (going to be) forced. Set has_forced_label_in_static. */
6669 static tree
6670 notice_forced_label_r (tree *tp, int *walk_subtrees, void *)
6672 if (TYPE_P (*tp))
6673 *walk_subtrees = 0;
6674 if (TREE_CODE (*tp) == LABEL_DECL)
6675 cfun->has_forced_label_in_static = 1;
6676 return NULL_TREE;
6679 /* Finish processing of a declaration;
6680 install its line number and initial value.
6681 If the length of an array type is not known before,
6682 it must be determined now, from the initial value, or it is an error.
6684 INIT is the initializer (if any) for DECL. If INIT_CONST_EXPR_P is
6685 true, then INIT is an integral constant expression.
6687 FLAGS is LOOKUP_ONLYCONVERTING if the = init syntax was used, else 0
6688 if the (init) syntax was used. */
6690 void
6691 cp_finish_decl (tree decl, tree init, bool init_const_expr_p,
6692 tree asmspec_tree, int flags)
6694 tree type;
6695 vec<tree, va_gc> *cleanups = NULL;
6696 const char *asmspec = NULL;
6697 int was_readonly = 0;
6698 bool var_definition_p = false;
6699 tree auto_node;
6701 if (decl == error_mark_node)
6702 return;
6703 else if (! decl)
6705 if (init)
6706 error ("assignment (not initialization) in declaration");
6707 return;
6710 gcc_assert (TREE_CODE (decl) != RESULT_DECL);
6711 /* Parameters are handled by store_parm_decls, not cp_finish_decl. */
6712 gcc_assert (TREE_CODE (decl) != PARM_DECL);
6714 type = TREE_TYPE (decl);
6715 if (type == error_mark_node)
6716 return;
6718 /* Warn about register storage specifiers except when in GNU global
6719 or local register variable extension. */
6720 if (VAR_P (decl) && DECL_REGISTER (decl) && asmspec_tree == NULL_TREE)
6722 if (cxx_dialect >= cxx1z)
6723 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
6724 "ISO C++1z does not allow %<register%> storage "
6725 "class specifier");
6726 else
6727 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
6728 "%<register%> storage class specifier used");
6731 /* If a name was specified, get the string. */
6732 if (at_namespace_scope_p ())
6733 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
6734 if (asmspec_tree && asmspec_tree != error_mark_node)
6735 asmspec = TREE_STRING_POINTER (asmspec_tree);
6737 if (current_class_type
6738 && CP_DECL_CONTEXT (decl) == current_class_type
6739 && TYPE_BEING_DEFINED (current_class_type)
6740 && !CLASSTYPE_TEMPLATE_INSTANTIATION (current_class_type)
6741 && (DECL_INITIAL (decl) || init))
6742 DECL_INITIALIZED_IN_CLASS_P (decl) = 1;
6744 if (TREE_CODE (decl) != FUNCTION_DECL
6745 && (auto_node = type_uses_auto (type)))
6747 tree d_init;
6748 if (init == NULL_TREE)
6750 if (DECL_LANG_SPECIFIC (decl)
6751 && DECL_TEMPLATE_INSTANTIATION (decl)
6752 && !DECL_TEMPLATE_INSTANTIATED (decl))
6754 /* init is null because we're deferring instantiating the
6755 initializer until we need it. Well, we need it now. */
6756 instantiate_decl (decl, /*defer_ok*/true, /*expl*/false);
6757 return;
6760 gcc_assert (CLASS_PLACEHOLDER_TEMPLATE (auto_node));
6762 d_init = init;
6763 if (d_init)
6765 if (TREE_CODE (d_init) == TREE_LIST
6766 && !CLASS_PLACEHOLDER_TEMPLATE (auto_node))
6767 d_init = build_x_compound_expr_from_list (d_init, ELK_INIT,
6768 tf_warning_or_error);
6769 d_init = resolve_nondeduced_context (d_init, tf_warning_or_error);
6771 enum auto_deduction_context adc = adc_variable_type;
6772 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
6773 adc = adc_decomp_type;
6774 type = TREE_TYPE (decl) = do_auto_deduction (type, d_init, auto_node,
6775 tf_warning_or_error, adc,
6776 NULL_TREE, flags);
6777 if (type == error_mark_node)
6778 return;
6779 if (TREE_CODE (type) == FUNCTION_TYPE)
6781 error ("initializer for %<decltype(auto) %D%> has function type "
6782 "(did you forget the %<()%> ?)", decl);
6783 TREE_TYPE (decl) = error_mark_node;
6784 return;
6786 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
6789 if (!ensure_literal_type_for_constexpr_object (decl))
6790 DECL_DECLARED_CONSTEXPR_P (decl) = 0;
6792 if (VAR_P (decl)
6793 && DECL_CLASS_SCOPE_P (decl)
6794 && DECL_INITIALIZED_IN_CLASS_P (decl))
6795 check_static_variable_definition (decl, type);
6797 if (init && TREE_CODE (decl) == FUNCTION_DECL)
6799 tree clone;
6800 if (init == ridpointers[(int)RID_DELETE])
6802 /* FIXME check this is 1st decl. */
6803 DECL_DELETED_FN (decl) = 1;
6804 DECL_DECLARED_INLINE_P (decl) = 1;
6805 DECL_INITIAL (decl) = error_mark_node;
6806 FOR_EACH_CLONE (clone, decl)
6808 DECL_DELETED_FN (clone) = 1;
6809 DECL_DECLARED_INLINE_P (clone) = 1;
6810 DECL_INITIAL (clone) = error_mark_node;
6812 init = NULL_TREE;
6814 else if (init == ridpointers[(int)RID_DEFAULT])
6816 if (defaultable_fn_check (decl))
6817 DECL_DEFAULTED_FN (decl) = 1;
6818 else
6819 DECL_INITIAL (decl) = NULL_TREE;
6823 if (init && VAR_P (decl))
6825 DECL_NONTRIVIALLY_INITIALIZED_P (decl) = 1;
6826 /* If DECL is a reference, then we want to know whether init is a
6827 reference constant; init_const_expr_p as passed tells us whether
6828 it's an rvalue constant. */
6829 if (TREE_CODE (type) == REFERENCE_TYPE)
6830 init_const_expr_p = potential_constant_expression (init);
6831 if (init_const_expr_p)
6833 /* Set these flags now for templates. We'll update the flags in
6834 store_init_value for instantiations. */
6835 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
6836 if (decl_maybe_constant_var_p (decl)
6837 /* FIXME setting TREE_CONSTANT on refs breaks the back end. */
6838 && TREE_CODE (type) != REFERENCE_TYPE)
6839 TREE_CONSTANT (decl) = 1;
6843 if (processing_template_decl)
6845 bool type_dependent_p;
6847 /* Add this declaration to the statement-tree. */
6848 if (at_function_scope_p ())
6849 add_decl_expr (decl);
6851 type_dependent_p = dependent_type_p (type);
6853 if (check_for_bare_parameter_packs (init))
6855 init = NULL_TREE;
6856 DECL_INITIAL (decl) = NULL_TREE;
6859 /* Generally, initializers in templates are expanded when the
6860 template is instantiated. But, if DECL is a variable constant
6861 then it can be used in future constant expressions, so its value
6862 must be available. */
6864 if (!VAR_P (decl) || type_dependent_p)
6865 /* We can't do anything if the decl has dependent type. */;
6866 else if (init
6867 && init_const_expr_p
6868 && TREE_CODE (type) != REFERENCE_TYPE
6869 && decl_maybe_constant_var_p (decl)
6870 && !type_dependent_init_p (init)
6871 && !value_dependent_init_p (init))
6873 /* This variable seems to be a non-dependent constant, so process
6874 its initializer. If check_initializer returns non-null the
6875 initialization wasn't constant after all. */
6876 tree init_code;
6877 cleanups = make_tree_vector ();
6878 init_code = check_initializer (decl, init, flags, &cleanups);
6879 if (init_code == NULL_TREE)
6880 init = NULL_TREE;
6881 release_tree_vector (cleanups);
6883 else if (!init && is_concept_var (decl))
6884 error ("variable concept has no initializer");
6885 else if (!DECL_PRETTY_FUNCTION_P (decl))
6887 /* Deduce array size even if the initializer is dependent. */
6888 maybe_deduce_size_from_array_init (decl, init);
6889 /* And complain about multiple initializers. */
6890 if (init && TREE_CODE (init) == TREE_LIST && TREE_CHAIN (init)
6891 && !MAYBE_CLASS_TYPE_P (type))
6892 init = build_x_compound_expr_from_list (init, ELK_INIT,
6893 tf_warning_or_error);
6896 if (init)
6897 DECL_INITIAL (decl) = init;
6898 return;
6901 /* Just store non-static data member initializers for later. */
6902 if (init && TREE_CODE (decl) == FIELD_DECL)
6903 DECL_INITIAL (decl) = init;
6905 /* Take care of TYPE_DECLs up front. */
6906 if (TREE_CODE (decl) == TYPE_DECL)
6908 if (type != error_mark_node
6909 && MAYBE_CLASS_TYPE_P (type) && DECL_NAME (decl))
6911 if (TREE_TYPE (DECL_NAME (decl)) && TREE_TYPE (decl) != type)
6912 warning (0, "shadowing previous type declaration of %q#D", decl);
6913 set_identifier_type_value (DECL_NAME (decl), decl);
6916 /* If we have installed this as the canonical typedef for this
6917 type, and that type has not been defined yet, delay emitting
6918 the debug information for it, as we will emit it later. */
6919 if (TYPE_MAIN_DECL (TREE_TYPE (decl)) == decl
6920 && !COMPLETE_TYPE_P (TREE_TYPE (decl)))
6921 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
6923 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl),
6924 at_eof);
6925 return;
6928 /* A reference will be modified here, as it is initialized. */
6929 if (! DECL_EXTERNAL (decl)
6930 && TREE_READONLY (decl)
6931 && TREE_CODE (type) == REFERENCE_TYPE)
6933 was_readonly = 1;
6934 TREE_READONLY (decl) = 0;
6937 if (VAR_P (decl))
6939 /* If this is a local variable that will need a mangled name,
6940 register it now. We must do this before processing the
6941 initializer for the variable, since the initialization might
6942 require a guard variable, and since the mangled name of the
6943 guard variable will depend on the mangled name of this
6944 variable. */
6945 if (DECL_FUNCTION_SCOPE_P (decl)
6946 && TREE_STATIC (decl)
6947 && !DECL_ARTIFICIAL (decl))
6949 push_local_name (decl);
6950 /* Normally has_forced_label_in_static is set during GIMPLE
6951 lowering, but [cd]tors are never actually compiled directly.
6952 We need to set this early so we can deal with the label
6953 address extension. */
6954 if ((DECL_CONSTRUCTOR_P (current_function_decl)
6955 || DECL_DESTRUCTOR_P (current_function_decl))
6956 && init)
6958 walk_tree (&init, notice_forced_label_r, NULL, NULL);
6959 add_local_decl (cfun, decl);
6961 /* And make sure it's in the symbol table for
6962 c_parse_final_cleanups to find. */
6963 varpool_node::get_create (decl);
6966 /* Convert the initializer to the type of DECL, if we have not
6967 already initialized DECL. */
6968 if (!DECL_INITIALIZED_P (decl)
6969 /* If !DECL_EXTERNAL then DECL is being defined. In the
6970 case of a static data member initialized inside the
6971 class-specifier, there can be an initializer even if DECL
6972 is *not* defined. */
6973 && (!DECL_EXTERNAL (decl) || init))
6975 cleanups = make_tree_vector ();
6976 init = check_initializer (decl, init, flags, &cleanups);
6978 /* Handle:
6980 [dcl.init]
6982 The memory occupied by any object of static storage
6983 duration is zero-initialized at program startup before
6984 any other initialization takes place.
6986 We cannot create an appropriate initializer until after
6987 the type of DECL is finalized. If DECL_INITIAL is set,
6988 then the DECL is statically initialized, and any
6989 necessary zero-initialization has already been performed. */
6990 if (TREE_STATIC (decl) && !DECL_INITIAL (decl))
6991 DECL_INITIAL (decl) = build_zero_init (TREE_TYPE (decl),
6992 /*nelts=*/NULL_TREE,
6993 /*static_storage_p=*/true);
6994 /* Remember that the initialization for this variable has
6995 taken place. */
6996 DECL_INITIALIZED_P (decl) = 1;
6997 /* This declaration is the definition of this variable,
6998 unless we are initializing a static data member within
6999 the class specifier. */
7000 if (!DECL_EXTERNAL (decl))
7001 var_definition_p = true;
7003 /* If the variable has an array type, lay out the type, even if
7004 there is no initializer. It is valid to index through the
7005 array, and we must get TYPE_ALIGN set correctly on the array
7006 type. */
7007 else if (TREE_CODE (type) == ARRAY_TYPE)
7008 layout_type (type);
7010 if (TREE_STATIC (decl)
7011 && !at_function_scope_p ()
7012 && current_function_decl == NULL)
7013 /* So decl is a global variable or a static member of a
7014 non local class. Record the types it uses
7015 so that we can decide later to emit debug info for them. */
7016 record_types_used_by_current_var_decl (decl);
7019 /* Add this declaration to the statement-tree. This needs to happen
7020 after the call to check_initializer so that the DECL_EXPR for a
7021 reference temp is added before the DECL_EXPR for the reference itself. */
7022 if (DECL_FUNCTION_SCOPE_P (decl))
7024 /* If we're building a variable sized type, and we might be
7025 reachable other than via the top of the current binding
7026 level, then create a new BIND_EXPR so that we deallocate
7027 the object at the right time. */
7028 if (VAR_P (decl)
7029 && DECL_SIZE (decl)
7030 && !TREE_CONSTANT (DECL_SIZE (decl))
7031 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
7033 tree bind;
7034 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
7035 TREE_SIDE_EFFECTS (bind) = 1;
7036 add_stmt (bind);
7037 BIND_EXPR_BODY (bind) = push_stmt_list ();
7039 add_decl_expr (decl);
7042 /* Let the middle end know about variables and functions -- but not
7043 static data members in uninstantiated class templates. */
7044 if (VAR_OR_FUNCTION_DECL_P (decl))
7046 if (VAR_P (decl))
7048 layout_var_decl (decl);
7049 maybe_commonize_var (decl);
7052 /* This needs to happen after the linkage is set. */
7053 determine_visibility (decl);
7055 if (var_definition_p && TREE_STATIC (decl))
7057 /* If a TREE_READONLY variable needs initialization
7058 at runtime, it is no longer readonly and we need to
7059 avoid MEM_READONLY_P being set on RTL created for it. */
7060 if (init)
7062 if (TREE_READONLY (decl))
7063 TREE_READONLY (decl) = 0;
7064 was_readonly = 0;
7066 else if (was_readonly)
7067 TREE_READONLY (decl) = 1;
7069 /* Likewise if it needs destruction. */
7070 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
7071 TREE_READONLY (decl) = 0;
7074 make_rtl_for_nonlocal_decl (decl, init, asmspec);
7076 /* Check for abstractness of the type. Notice that there is no
7077 need to strip array types here since the check for those types
7078 is already done within create_array_type_for_decl. */
7079 abstract_virtuals_error (decl, type);
7081 if (TREE_TYPE (decl) == error_mark_node)
7082 /* No initialization required. */
7084 else if (TREE_CODE (decl) == FUNCTION_DECL)
7086 if (init)
7088 if (init == ridpointers[(int)RID_DEFAULT])
7090 /* An out-of-class default definition is defined at
7091 the point where it is explicitly defaulted. */
7092 if (DECL_DELETED_FN (decl))
7093 maybe_explain_implicit_delete (decl);
7094 else if (DECL_INITIAL (decl) == error_mark_node)
7095 synthesize_method (decl);
7097 else
7098 error ("function %q#D is initialized like a variable", decl);
7100 /* else no initialization required. */
7102 else if (DECL_EXTERNAL (decl)
7103 && ! (DECL_LANG_SPECIFIC (decl)
7104 && DECL_NOT_REALLY_EXTERN (decl)))
7106 if (init)
7107 DECL_INITIAL (decl) = init;
7109 /* A variable definition. */
7110 else if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
7111 /* Initialize the local variable. */
7112 initialize_local_var (decl, init);
7114 /* If a variable is defined, and then a subsequent
7115 definition with external linkage is encountered, we will
7116 get here twice for the same variable. We want to avoid
7117 calling expand_static_init more than once. For variables
7118 that are not static data members, we can call
7119 expand_static_init only when we actually process the
7120 initializer. It is not legal to redeclare a static data
7121 member, so this issue does not arise in that case. */
7122 else if (var_definition_p && TREE_STATIC (decl))
7123 expand_static_init (decl, init);
7126 /* If a CLEANUP_STMT was created to destroy a temporary bound to a
7127 reference, insert it in the statement-tree now. */
7128 if (cleanups)
7130 unsigned i; tree t;
7131 FOR_EACH_VEC_ELT (*cleanups, i, t)
7132 push_cleanup (decl, t, false);
7133 release_tree_vector (cleanups);
7136 if (was_readonly)
7137 TREE_READONLY (decl) = 1;
7139 invoke_plugin_callbacks (PLUGIN_FINISH_DECL, decl);
7142 /* For class TYPE return itself or some its bases that contain
7143 any direct non-static data members. Return error_mark_node if an
7144 error has been diagnosed. */
7146 static tree
7147 find_decomp_class_base (location_t loc, tree type, tree ret)
7149 bool member_seen = false;
7150 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
7151 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
7152 continue;
7153 else if (ret)
7154 return type;
7155 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
7157 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE)
7158 error_at (loc, "cannot decompose class type %qT because it has an "
7159 "anonymous struct member", type);
7160 else
7161 error_at (loc, "cannot decompose class type %qT because it has an "
7162 "anonymous union member", type);
7163 inform (DECL_SOURCE_LOCATION (field), "declared here");
7164 return error_mark_node;
7166 else if (TREE_PRIVATE (field) || TREE_PROTECTED (field))
7168 error_at (loc, "cannot decompose non-public member %qD of %qT",
7169 field, type);
7170 inform (DECL_SOURCE_LOCATION (field),
7171 TREE_PRIVATE (field)
7172 ? G_("declared private here")
7173 : G_("declared protected here"));
7174 return error_mark_node;
7176 else
7177 member_seen = true;
7179 tree base_binfo, binfo;
7180 tree orig_ret = ret;
7181 int i;
7182 if (member_seen)
7183 ret = type;
7184 for (binfo = TYPE_BINFO (type), i = 0;
7185 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
7187 tree t = find_decomp_class_base (loc, TREE_TYPE (base_binfo), ret);
7188 if (t == error_mark_node)
7189 return error_mark_node;
7190 if (t != NULL_TREE)
7192 if (ret == type)
7194 error_at (loc, "cannot decompose class type %qT: both it and "
7195 "its base class %qT have non-static data members",
7196 type, t);
7197 return error_mark_node;
7199 else if (orig_ret != NULL_TREE)
7200 return t;
7201 else if (ret == t)
7202 /* OK, found the same base along another path. We'll complain
7203 in convert_to_base if it's ambiguous. */;
7204 else if (ret != NULL_TREE)
7206 error_at (loc, "cannot decompose class type %qT: its base "
7207 "classes %qT and %qT have non-static data "
7208 "members", type, ret, t);
7209 return error_mark_node;
7211 else
7212 ret = t;
7215 return ret;
7218 /* Return std::tuple_size<TYPE>::value. */
7220 static tree
7221 get_tuple_size (tree type)
7223 tree args = make_tree_vec (1);
7224 TREE_VEC_ELT (args, 0) = type;
7225 tree inst = lookup_template_class (get_identifier ("tuple_size"), args,
7226 /*in_decl*/NULL_TREE,
7227 /*context*/std_node,
7228 /*entering_scope*/false, tf_none);
7229 inst = complete_type (inst);
7230 if (inst == error_mark_node || !COMPLETE_TYPE_P (inst))
7231 return NULL_TREE;
7232 tree val = lookup_qualified_name (inst, get_identifier ("value"),
7233 /*type*/false, /*complain*/false);
7234 if (TREE_CODE (val) == VAR_DECL || TREE_CODE (val) == CONST_DECL)
7235 val = maybe_constant_value (val);
7236 if (TREE_CODE (val) == INTEGER_CST)
7237 return val;
7238 else
7239 return error_mark_node;
7242 /* Return std::tuple_element<I,TYPE>::type. */
7244 static tree
7245 get_tuple_element_type (tree type, unsigned i)
7247 tree args = make_tree_vec (2);
7248 TREE_VEC_ELT (args, 0) = build_int_cst (integer_type_node, i);
7249 TREE_VEC_ELT (args, 1) = type;
7250 tree inst = lookup_template_class (get_identifier ("tuple_element"), args,
7251 /*in_decl*/NULL_TREE,
7252 /*context*/std_node,
7253 /*entering_scope*/false,
7254 tf_warning_or_error);
7255 return make_typename_type (inst, get_identifier ("type"),
7256 none_type, tf_warning_or_error);
7259 /* Return e.get<i>() or get<i>(e). */
7261 static tree
7262 get_tuple_decomp_init (tree decl, unsigned i)
7264 tree get_id = get_identifier ("get");
7265 tree targs = make_tree_vec (1);
7266 TREE_VEC_ELT (targs, 0) = build_int_cst (integer_type_node, i);
7268 tree etype = TREE_TYPE (decl);
7269 tree e = convert_from_reference (decl);
7271 /* [The id-expression] e is an lvalue if the type of the entity e is an
7272 lvalue reference and an xvalue otherwise. */
7273 if (TREE_CODE (etype) != REFERENCE_TYPE
7274 || TYPE_REF_IS_RVALUE (etype))
7275 e = move (e);
7277 tree fns = lookup_qualified_name (TREE_TYPE (e), get_id,
7278 /*type*/false, /*complain*/false);
7279 if (fns != error_mark_node)
7281 fns = lookup_template_function (fns, targs);
7282 return build_new_method_call (e, fns, /*args*/NULL,
7283 /*path*/NULL_TREE, LOOKUP_NORMAL,
7284 /*fn_p*/NULL, tf_warning_or_error);
7286 else
7288 vec<tree,va_gc> *args = make_tree_vector_single (e);
7289 fns = lookup_template_function (get_id, targs);
7290 fns = perform_koenig_lookup (fns, args, tf_warning_or_error);
7291 return finish_call_expr (fns, &args, /*novirt*/false,
7292 /*koenig*/true, tf_warning_or_error);
7296 /* It's impossible to recover the decltype of a tuple decomposition variable
7297 based on the actual type of the variable, so store it in a hash table. */
7298 static GTY(()) hash_map<tree,tree> *decomp_type_table;
7299 static void
7300 store_decomp_type (tree v, tree t)
7302 if (!decomp_type_table)
7303 decomp_type_table = hash_map<tree,tree>::create_ggc (13);
7304 decomp_type_table->put (v, t);
7307 tree
7308 lookup_decomp_type (tree v)
7310 return *decomp_type_table->get (v);
7313 /* Finish a decomposition declaration. DECL is the underlying declaration
7314 "e", FIRST is the head of a chain of decls for the individual identifiers
7315 chained through DECL_CHAIN in reverse order and COUNT is the number of
7316 those decls. */
7318 void
7319 cp_finish_decomp (tree decl, tree first, unsigned int count)
7321 if (error_operand_p (decl))
7323 error_out:
7324 while (count--)
7326 TREE_TYPE (first) = error_mark_node;
7327 if (DECL_HAS_VALUE_EXPR_P (first))
7329 SET_DECL_VALUE_EXPR (first, NULL_TREE);
7330 DECL_HAS_VALUE_EXPR_P (first) = 0;
7332 first = DECL_CHAIN (first);
7334 if (DECL_P (decl) && DECL_NAMESPACE_SCOPE_P (decl))
7335 SET_DECL_ASSEMBLER_NAME (decl, get_identifier ("<decomp>"));
7336 return;
7339 location_t loc = DECL_SOURCE_LOCATION (decl);
7340 if (type_dependent_expression_p (decl)
7341 /* This happens for range for when not in templates.
7342 Still add the DECL_VALUE_EXPRs for later processing. */
7343 || (!processing_template_decl
7344 && type_uses_auto (TREE_TYPE (decl))))
7346 for (unsigned int i = 0; i < count; i++)
7348 if (!DECL_HAS_VALUE_EXPR_P (first))
7350 tree v = build_nt (ARRAY_REF, decl,
7351 size_int (count - i - 1),
7352 NULL_TREE, NULL_TREE);
7353 SET_DECL_VALUE_EXPR (first, v);
7354 DECL_HAS_VALUE_EXPR_P (first) = 1;
7356 if (processing_template_decl)
7357 fit_decomposition_lang_decl (first, decl);
7358 first = DECL_CHAIN (first);
7360 return;
7363 auto_vec<tree, 16> v;
7364 v.safe_grow (count);
7365 tree d = first;
7366 for (unsigned int i = 0; i < count; i++, d = DECL_CHAIN (d))
7368 v[count - i - 1] = d;
7369 fit_decomposition_lang_decl (d, decl);
7372 tree type = TREE_TYPE (decl);
7373 tree dexp = decl;
7375 if (TREE_CODE (type) == REFERENCE_TYPE)
7377 dexp = convert_from_reference (dexp);
7378 type = TREE_TYPE (type);
7381 tree eltype = NULL_TREE;
7382 unsigned HOST_WIDE_INT eltscnt = 0;
7383 if (TREE_CODE (type) == ARRAY_TYPE)
7385 tree nelts;
7386 nelts = array_type_nelts_top (type);
7387 if (nelts == error_mark_node)
7388 goto error_out;
7389 if (!tree_fits_uhwi_p (nelts))
7391 error_at (loc, "cannot decompose variable length array %qT", type);
7392 goto error_out;
7394 eltscnt = tree_to_uhwi (nelts);
7395 if (count != eltscnt)
7397 cnt_mismatch:
7398 if (count > eltscnt)
7399 error_at (loc, "%u names provided while %qT decomposes into "
7400 "%wu elements", count, type, eltscnt);
7401 else
7402 error_at (loc, "only %u names provided while %qT decomposes into "
7403 "%wu elements", count, type, eltscnt);
7404 goto error_out;
7406 eltype = TREE_TYPE (type);
7407 for (unsigned int i = 0; i < count; i++)
7409 TREE_TYPE (v[i]) = eltype;
7410 layout_decl (v[i], 0);
7411 if (processing_template_decl)
7412 continue;
7413 tree t = unshare_expr (dexp);
7414 t = build4_loc (DECL_SOURCE_LOCATION (v[i]), ARRAY_REF,
7415 eltype, t, size_int (i), NULL_TREE,
7416 NULL_TREE);
7417 SET_DECL_VALUE_EXPR (v[i], t);
7418 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
7421 /* 2 GNU extensions. */
7422 else if (TREE_CODE (type) == COMPLEX_TYPE)
7424 eltscnt = 2;
7425 if (count != eltscnt)
7426 goto cnt_mismatch;
7427 eltype = cp_build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
7428 for (unsigned int i = 0; i < count; i++)
7430 TREE_TYPE (v[i]) = eltype;
7431 layout_decl (v[i], 0);
7432 if (processing_template_decl)
7433 continue;
7434 tree t = unshare_expr (dexp);
7435 t = build1_loc (DECL_SOURCE_LOCATION (v[i]),
7436 i ? IMAGPART_EXPR : REALPART_EXPR, eltype,
7438 SET_DECL_VALUE_EXPR (v[i], t);
7439 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
7442 else if (TREE_CODE (type) == VECTOR_TYPE)
7444 eltscnt = TYPE_VECTOR_SUBPARTS (type);
7445 if (count != eltscnt)
7446 goto cnt_mismatch;
7447 eltype = cp_build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
7448 for (unsigned int i = 0; i < count; i++)
7450 TREE_TYPE (v[i]) = eltype;
7451 layout_decl (v[i], 0);
7452 if (processing_template_decl)
7453 continue;
7454 tree t = unshare_expr (dexp);
7455 convert_vector_to_array_for_subscript (DECL_SOURCE_LOCATION (v[i]),
7456 &t, size_int (i));
7457 t = build4_loc (DECL_SOURCE_LOCATION (v[i]), ARRAY_REF,
7458 eltype, t, size_int (i), NULL_TREE,
7459 NULL_TREE);
7460 SET_DECL_VALUE_EXPR (v[i], t);
7461 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
7464 else if (tree tsize = get_tuple_size (type))
7466 if (tsize == error_mark_node)
7468 error_at (loc, "%<std::tuple_size<%T>::value%> is not an integral "
7469 "constant expression", type);
7470 goto error_out;
7472 eltscnt = tree_to_uhwi (tsize);
7473 if (count != eltscnt)
7474 goto cnt_mismatch;
7475 int save_read = DECL_READ_P (decl);
7476 for (unsigned i = 0; i < count; ++i)
7478 location_t sloc = input_location;
7479 location_t dloc = DECL_SOURCE_LOCATION (v[i]);
7481 input_location = dloc;
7482 tree init = get_tuple_decomp_init (decl, i);
7483 tree eltype = (init == error_mark_node ? error_mark_node
7484 : get_tuple_element_type (type, i));
7485 input_location = sloc;
7487 if (init == error_mark_node || eltype == error_mark_node)
7489 inform (dloc, "in initialization of decomposition variable %qD",
7490 v[i]);
7491 goto error_out;
7493 /* Save the decltype away before reference collapse. */
7494 store_decomp_type (v[i], eltype);
7495 eltype = cp_build_reference_type (eltype, !lvalue_p (init));
7496 TREE_TYPE (v[i]) = eltype;
7497 layout_decl (v[i], 0);
7498 if (DECL_HAS_VALUE_EXPR_P (v[i]))
7500 /* In this case the names are variables, not just proxies. */
7501 SET_DECL_VALUE_EXPR (v[i], NULL_TREE);
7502 DECL_HAS_VALUE_EXPR_P (v[i]) = 0;
7504 if (!processing_template_decl)
7505 cp_finish_decl (v[i], init, /*constexpr*/false,
7506 /*asm*/NULL_TREE, LOOKUP_NORMAL);
7508 /* Ignore reads from the underlying decl performed during initialization
7509 of the individual variables. If those will be read, we'll mark
7510 the underlying decl as read at that point. */
7511 DECL_READ_P (decl) = save_read;
7513 else if (TREE_CODE (type) == UNION_TYPE)
7515 error_at (loc, "cannot decompose union type %qT", type);
7516 goto error_out;
7518 else if (!CLASS_TYPE_P (type))
7520 error_at (loc, "cannot decompose non-array non-class type %qT", type);
7521 goto error_out;
7523 else if (LAMBDA_TYPE_P (type))
7525 error_at (loc, "cannot decompose lambda closure type %qT", type);
7526 goto error_out;
7528 else
7530 tree btype = find_decomp_class_base (loc, type, NULL_TREE);
7531 if (btype == error_mark_node)
7532 goto error_out;
7533 else if (btype == NULL_TREE)
7535 error_at (loc, "cannot decompose class type %qT without non-static "
7536 "data members", type);
7537 goto error_out;
7539 for (tree field = TYPE_FIELDS (btype); field; field = TREE_CHAIN (field))
7540 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
7541 continue;
7542 else
7543 eltscnt++;
7544 if (count != eltscnt)
7545 goto cnt_mismatch;
7546 tree t = dexp;
7547 if (type != btype)
7549 t = convert_to_base (t, btype, /*check_access*/true,
7550 /*nonnull*/false, tf_warning_or_error);
7551 type = btype;
7553 unsigned int i = 0;
7554 for (tree field = TYPE_FIELDS (btype); field; field = TREE_CHAIN (field))
7555 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
7556 continue;
7557 else
7559 tree tt = finish_non_static_data_member (field, unshare_expr (t),
7560 NULL_TREE);
7561 if (REFERENCE_REF_P (tt))
7562 tt = TREE_OPERAND (tt, 0);
7563 TREE_TYPE (v[i]) = TREE_TYPE (tt);
7564 layout_decl (v[i], 0);
7565 if (!processing_template_decl)
7567 SET_DECL_VALUE_EXPR (v[i], tt);
7568 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
7570 i++;
7573 if (processing_template_decl)
7575 for (unsigned int i = 0; i < count; i++)
7576 if (!DECL_HAS_VALUE_EXPR_P (v[i]))
7578 tree a = build_nt (ARRAY_REF, decl, size_int (i),
7579 NULL_TREE, NULL_TREE);
7580 SET_DECL_VALUE_EXPR (v[i], a);
7581 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
7584 else if (DECL_NAMESPACE_SCOPE_P (decl))
7585 SET_DECL_ASSEMBLER_NAME (decl, mangle_decomp (decl, v));
7588 /* Returns a declaration for a VAR_DECL as if:
7590 extern "C" TYPE NAME;
7592 had been seen. Used to create compiler-generated global
7593 variables. */
7595 static tree
7596 declare_global_var (tree name, tree type)
7598 tree decl;
7600 push_to_top_level ();
7601 decl = build_decl (input_location, VAR_DECL, name, type);
7602 TREE_PUBLIC (decl) = 1;
7603 DECL_EXTERNAL (decl) = 1;
7604 DECL_ARTIFICIAL (decl) = 1;
7605 /* If the user has explicitly declared this variable (perhaps
7606 because the code we are compiling is part of a low-level runtime
7607 library), then it is possible that our declaration will be merged
7608 with theirs by pushdecl. */
7609 decl = pushdecl (decl);
7610 cp_finish_decl (decl, NULL_TREE, false, NULL_TREE, 0);
7611 pop_from_top_level ();
7613 return decl;
7616 /* Returns the type for the argument to "__cxa_atexit" (or "atexit",
7617 if "__cxa_atexit" is not being used) corresponding to the function
7618 to be called when the program exits. */
7620 static tree
7621 get_atexit_fn_ptr_type (void)
7623 tree fn_type;
7625 if (!atexit_fn_ptr_type_node)
7627 tree arg_type;
7628 if (flag_use_cxa_atexit
7629 && !targetm.cxx.use_atexit_for_cxa_atexit ())
7630 /* The parameter to "__cxa_atexit" is "void (*)(void *)". */
7631 arg_type = ptr_type_node;
7632 else
7633 /* The parameter to "atexit" is "void (*)(void)". */
7634 arg_type = NULL_TREE;
7636 fn_type = build_function_type_list (void_type_node,
7637 arg_type, NULL_TREE);
7638 atexit_fn_ptr_type_node = build_pointer_type (fn_type);
7641 return atexit_fn_ptr_type_node;
7644 /* Returns a pointer to the `atexit' function. Note that if
7645 FLAG_USE_CXA_ATEXIT is nonzero, then this will actually be the new
7646 `__cxa_atexit' function specified in the IA64 C++ ABI. */
7648 static tree
7649 get_atexit_node (void)
7651 tree atexit_fndecl;
7652 tree fn_type;
7653 tree fn_ptr_type;
7654 const char *name;
7655 bool use_aeabi_atexit;
7657 if (atexit_node)
7658 return atexit_node;
7660 if (flag_use_cxa_atexit && !targetm.cxx.use_atexit_for_cxa_atexit ())
7662 /* The declaration for `__cxa_atexit' is:
7664 int __cxa_atexit (void (*)(void *), void *, void *)
7666 We build up the argument types and then the function type
7667 itself. */
7668 tree argtype0, argtype1, argtype2;
7670 use_aeabi_atexit = targetm.cxx.use_aeabi_atexit ();
7671 /* First, build the pointer-to-function type for the first
7672 argument. */
7673 fn_ptr_type = get_atexit_fn_ptr_type ();
7674 /* Then, build the rest of the argument types. */
7675 argtype2 = ptr_type_node;
7676 if (use_aeabi_atexit)
7678 argtype1 = fn_ptr_type;
7679 argtype0 = ptr_type_node;
7681 else
7683 argtype1 = ptr_type_node;
7684 argtype0 = fn_ptr_type;
7686 /* And the final __cxa_atexit type. */
7687 fn_type = build_function_type_list (integer_type_node,
7688 argtype0, argtype1, argtype2,
7689 NULL_TREE);
7690 if (use_aeabi_atexit)
7691 name = "__aeabi_atexit";
7692 else
7693 name = "__cxa_atexit";
7695 else
7697 /* The declaration for `atexit' is:
7699 int atexit (void (*)());
7701 We build up the argument types and then the function type
7702 itself. */
7703 fn_ptr_type = get_atexit_fn_ptr_type ();
7704 /* Build the final atexit type. */
7705 fn_type = build_function_type_list (integer_type_node,
7706 fn_ptr_type, NULL_TREE);
7707 name = "atexit";
7710 /* Now, build the function declaration. */
7711 push_lang_context (lang_name_c);
7712 atexit_fndecl = build_library_fn_ptr (name, fn_type, ECF_LEAF | ECF_NOTHROW);
7713 mark_used (atexit_fndecl);
7714 pop_lang_context ();
7715 atexit_node = decay_conversion (atexit_fndecl, tf_warning_or_error);
7717 return atexit_node;
7720 /* Like get_atexit_node, but for thread-local cleanups. */
7722 static tree
7723 get_thread_atexit_node (void)
7725 /* The declaration for `__cxa_thread_atexit' is:
7727 int __cxa_thread_atexit (void (*)(void *), void *, void *) */
7728 tree fn_type = build_function_type_list (integer_type_node,
7729 get_atexit_fn_ptr_type (),
7730 ptr_type_node, ptr_type_node,
7731 NULL_TREE);
7733 /* Now, build the function declaration. */
7734 tree atexit_fndecl = build_library_fn_ptr ("__cxa_thread_atexit", fn_type,
7735 ECF_LEAF | ECF_NOTHROW);
7736 return decay_conversion (atexit_fndecl, tf_warning_or_error);
7739 /* Returns the __dso_handle VAR_DECL. */
7741 static tree
7742 get_dso_handle_node (void)
7744 if (dso_handle_node)
7745 return dso_handle_node;
7747 /* Declare the variable. */
7748 dso_handle_node = declare_global_var (get_identifier ("__dso_handle"),
7749 ptr_type_node);
7751 #ifdef HAVE_GAS_HIDDEN
7752 if (dso_handle_node != error_mark_node)
7754 DECL_VISIBILITY (dso_handle_node) = VISIBILITY_HIDDEN;
7755 DECL_VISIBILITY_SPECIFIED (dso_handle_node) = 1;
7757 #endif
7759 return dso_handle_node;
7762 /* Begin a new function with internal linkage whose job will be simply
7763 to destroy some particular variable. */
7765 static GTY(()) int start_cleanup_cnt;
7767 static tree
7768 start_cleanup_fn (void)
7770 char name[32];
7771 tree fntype;
7772 tree fndecl;
7773 bool use_cxa_atexit = flag_use_cxa_atexit
7774 && !targetm.cxx.use_atexit_for_cxa_atexit ();
7776 push_to_top_level ();
7778 /* No need to mangle this. */
7779 push_lang_context (lang_name_c);
7781 /* Build the name of the function. */
7782 sprintf (name, "__tcf_%d", start_cleanup_cnt++);
7783 /* Build the function declaration. */
7784 fntype = TREE_TYPE (get_atexit_fn_ptr_type ());
7785 fndecl = build_lang_decl (FUNCTION_DECL, get_identifier (name), fntype);
7786 /* It's a function with internal linkage, generated by the
7787 compiler. */
7788 TREE_PUBLIC (fndecl) = 0;
7789 DECL_ARTIFICIAL (fndecl) = 1;
7790 /* Make the function `inline' so that it is only emitted if it is
7791 actually needed. It is unlikely that it will be inlined, since
7792 it is only called via a function pointer, but we avoid unnecessary
7793 emissions this way. */
7794 DECL_DECLARED_INLINE_P (fndecl) = 1;
7795 DECL_INTERFACE_KNOWN (fndecl) = 1;
7796 /* Build the parameter. */
7797 if (use_cxa_atexit)
7799 tree parmdecl = cp_build_parm_decl (fndecl, NULL_TREE, ptr_type_node);
7800 TREE_USED (parmdecl) = 1;
7801 DECL_READ_P (parmdecl) = 1;
7802 DECL_ARGUMENTS (fndecl) = parmdecl;
7805 pushdecl (fndecl);
7806 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
7808 pop_lang_context ();
7810 return current_function_decl;
7813 /* Finish the cleanup function begun by start_cleanup_fn. */
7815 static void
7816 end_cleanup_fn (void)
7818 expand_or_defer_fn (finish_function (0));
7820 pop_from_top_level ();
7823 /* Generate code to handle the destruction of DECL, an object with
7824 static storage duration. */
7826 tree
7827 register_dtor_fn (tree decl)
7829 tree cleanup;
7830 tree addr;
7831 tree compound_stmt;
7832 tree fcall;
7833 tree type;
7834 bool ob_parm, dso_parm, use_dtor;
7835 tree arg0, arg1, arg2;
7836 tree atex_node;
7838 type = TREE_TYPE (decl);
7839 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
7840 return void_node;
7842 /* If we're using "__cxa_atexit" (or "__cxa_thread_atexit" or
7843 "__aeabi_atexit"), and DECL is a class object, we can just pass the
7844 destructor to "__cxa_atexit"; we don't have to build a temporary
7845 function to do the cleanup. */
7846 dso_parm = (flag_use_cxa_atexit
7847 && !targetm.cxx.use_atexit_for_cxa_atexit ());
7848 ob_parm = (CP_DECL_THREAD_LOCAL_P (decl) || dso_parm);
7849 use_dtor = ob_parm && CLASS_TYPE_P (type);
7850 if (use_dtor)
7852 int idx;
7854 /* Find the destructor. */
7855 idx = lookup_fnfields_1 (type, complete_dtor_identifier);
7856 gcc_assert (idx >= 0);
7857 cleanup = (*CLASSTYPE_METHOD_VEC (type))[idx];
7858 /* Make sure it is accessible. */
7859 perform_or_defer_access_check (TYPE_BINFO (type), cleanup, cleanup,
7860 tf_warning_or_error);
7862 else
7864 /* Call build_cleanup before we enter the anonymous function so
7865 that any access checks will be done relative to the current
7866 scope, rather than the scope of the anonymous function. */
7867 build_cleanup (decl);
7869 /* Now start the function. */
7870 cleanup = start_cleanup_fn ();
7872 /* Now, recompute the cleanup. It may contain SAVE_EXPRs that refer
7873 to the original function, rather than the anonymous one. That
7874 will make the back end think that nested functions are in use,
7875 which causes confusion. */
7876 push_deferring_access_checks (dk_no_check);
7877 fcall = build_cleanup (decl);
7878 pop_deferring_access_checks ();
7880 /* Create the body of the anonymous function. */
7881 compound_stmt = begin_compound_stmt (BCS_FN_BODY);
7882 finish_expr_stmt (fcall);
7883 finish_compound_stmt (compound_stmt);
7884 end_cleanup_fn ();
7887 /* Call atexit with the cleanup function. */
7888 mark_used (cleanup);
7889 cleanup = build_address (cleanup);
7891 if (CP_DECL_THREAD_LOCAL_P (decl))
7892 atex_node = get_thread_atexit_node ();
7893 else
7894 atex_node = get_atexit_node ();
7896 if (use_dtor)
7898 /* We must convert CLEANUP to the type that "__cxa_atexit"
7899 expects. */
7900 cleanup = build_nop (get_atexit_fn_ptr_type (), cleanup);
7901 /* "__cxa_atexit" will pass the address of DECL to the
7902 cleanup function. */
7903 mark_used (decl);
7904 addr = build_address (decl);
7905 /* The declared type of the parameter to "__cxa_atexit" is
7906 "void *". For plain "T*", we could just let the
7907 machinery in cp_build_function_call convert it -- but if the
7908 type is "cv-qualified T *", then we need to convert it
7909 before passing it in, to avoid spurious errors. */
7910 addr = build_nop (ptr_type_node, addr);
7912 else
7913 /* Since the cleanup functions we build ignore the address
7914 they're given, there's no reason to pass the actual address
7915 in, and, in general, it's cheaper to pass NULL than any
7916 other value. */
7917 addr = null_pointer_node;
7919 if (dso_parm)
7920 arg2 = cp_build_addr_expr (get_dso_handle_node (),
7921 tf_warning_or_error);
7922 else if (ob_parm)
7923 /* Just pass NULL to the dso handle parm if we don't actually
7924 have a DSO handle on this target. */
7925 arg2 = null_pointer_node;
7926 else
7927 arg2 = NULL_TREE;
7929 if (ob_parm)
7931 if (!CP_DECL_THREAD_LOCAL_P (decl)
7932 && targetm.cxx.use_aeabi_atexit ())
7934 arg1 = cleanup;
7935 arg0 = addr;
7937 else
7939 arg1 = addr;
7940 arg0 = cleanup;
7943 else
7945 arg0 = cleanup;
7946 arg1 = NULL_TREE;
7948 return cp_build_function_call_nary (atex_node, tf_warning_or_error,
7949 arg0, arg1, arg2, NULL_TREE);
7952 /* DECL is a VAR_DECL with static storage duration. INIT, if present,
7953 is its initializer. Generate code to handle the construction
7954 and destruction of DECL. */
7956 static void
7957 expand_static_init (tree decl, tree init)
7959 gcc_assert (VAR_P (decl));
7960 gcc_assert (TREE_STATIC (decl));
7962 /* Some variables require no dynamic initialization. */
7963 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
7965 /* Make sure the destructor is callable. */
7966 cxx_maybe_build_cleanup (decl, tf_warning_or_error);
7967 if (!init)
7968 return;
7971 if (CP_DECL_THREAD_LOCAL_P (decl) && DECL_GNU_TLS_P (decl)
7972 && !DECL_FUNCTION_SCOPE_P (decl))
7974 if (init)
7975 error ("non-local variable %qD declared %<__thread%> "
7976 "needs dynamic initialization", decl);
7977 else
7978 error ("non-local variable %qD declared %<__thread%> "
7979 "has a non-trivial destructor", decl);
7980 static bool informed;
7981 if (!informed)
7983 inform (DECL_SOURCE_LOCATION (decl),
7984 "C++11 %<thread_local%> allows dynamic initialization "
7985 "and destruction");
7986 informed = true;
7988 return;
7991 if (DECL_FUNCTION_SCOPE_P (decl))
7993 /* Emit code to perform this initialization but once. */
7994 tree if_stmt = NULL_TREE, inner_if_stmt = NULL_TREE;
7995 tree then_clause = NULL_TREE, inner_then_clause = NULL_TREE;
7996 tree guard, guard_addr;
7997 tree flag, begin;
7998 /* We don't need thread-safety code for thread-local vars. */
7999 bool thread_guard = (flag_threadsafe_statics
8000 && !CP_DECL_THREAD_LOCAL_P (decl));
8002 /* Emit code to perform this initialization but once. This code
8003 looks like:
8005 static <type> guard;
8006 if (!__atomic_load (guard.first_byte)) {
8007 if (__cxa_guard_acquire (&guard)) {
8008 bool flag = false;
8009 try {
8010 // Do initialization.
8011 flag = true; __cxa_guard_release (&guard);
8012 // Register variable for destruction at end of program.
8013 } catch {
8014 if (!flag) __cxa_guard_abort (&guard);
8019 Note that the `flag' variable is only set to 1 *after* the
8020 initialization is complete. This ensures that an exception,
8021 thrown during the construction, will cause the variable to
8022 reinitialized when we pass through this code again, as per:
8024 [stmt.dcl]
8026 If the initialization exits by throwing an exception, the
8027 initialization is not complete, so it will be tried again
8028 the next time control enters the declaration.
8030 This process should be thread-safe, too; multiple threads
8031 should not be able to initialize the variable more than
8032 once. */
8034 /* Create the guard variable. */
8035 guard = get_guard (decl);
8037 /* Begin the conditional initialization. */
8038 if_stmt = begin_if_stmt ();
8040 finish_if_stmt_cond (get_guard_cond (guard, thread_guard), if_stmt);
8041 then_clause = begin_compound_stmt (BCS_NO_SCOPE);
8043 if (thread_guard)
8045 tree vfntype = NULL_TREE;
8046 tree acquire_name, release_name, abort_name;
8047 tree acquire_fn, release_fn, abort_fn;
8048 guard_addr = build_address (guard);
8050 acquire_name = get_identifier ("__cxa_guard_acquire");
8051 release_name = get_identifier ("__cxa_guard_release");
8052 abort_name = get_identifier ("__cxa_guard_abort");
8053 acquire_fn = identifier_global_value (acquire_name);
8054 release_fn = identifier_global_value (release_name);
8055 abort_fn = identifier_global_value (abort_name);
8056 if (!acquire_fn)
8057 acquire_fn = push_library_fn
8058 (acquire_name, build_function_type_list (integer_type_node,
8059 TREE_TYPE (guard_addr),
8060 NULL_TREE),
8061 NULL_TREE, ECF_NOTHROW | ECF_LEAF);
8062 if (!release_fn || !abort_fn)
8063 vfntype = build_function_type_list (void_type_node,
8064 TREE_TYPE (guard_addr),
8065 NULL_TREE);
8066 if (!release_fn)
8067 release_fn = push_library_fn (release_name, vfntype, NULL_TREE,
8068 ECF_NOTHROW | ECF_LEAF);
8069 if (!abort_fn)
8070 abort_fn = push_library_fn (abort_name, vfntype, NULL_TREE,
8071 ECF_NOTHROW | ECF_LEAF);
8073 inner_if_stmt = begin_if_stmt ();
8074 finish_if_stmt_cond (build_call_n (acquire_fn, 1, guard_addr),
8075 inner_if_stmt);
8077 inner_then_clause = begin_compound_stmt (BCS_NO_SCOPE);
8078 begin = get_target_expr (boolean_false_node);
8079 flag = TARGET_EXPR_SLOT (begin);
8081 TARGET_EXPR_CLEANUP (begin)
8082 = build3 (COND_EXPR, void_type_node, flag,
8083 void_node,
8084 build_call_n (abort_fn, 1, guard_addr));
8085 CLEANUP_EH_ONLY (begin) = 1;
8087 /* Do the initialization itself. */
8088 init = add_stmt_to_compound (begin, init);
8089 init = add_stmt_to_compound
8090 (init, build2 (MODIFY_EXPR, void_type_node, flag, boolean_true_node));
8091 init = add_stmt_to_compound
8092 (init, build_call_n (release_fn, 1, guard_addr));
8094 else
8095 init = add_stmt_to_compound (init, set_guard (guard));
8097 /* Use atexit to register a function for destroying this static
8098 variable. */
8099 init = add_stmt_to_compound (init, register_dtor_fn (decl));
8101 finish_expr_stmt (init);
8103 if (thread_guard)
8105 finish_compound_stmt (inner_then_clause);
8106 finish_then_clause (inner_if_stmt);
8107 finish_if_stmt (inner_if_stmt);
8110 finish_compound_stmt (then_clause);
8111 finish_then_clause (if_stmt);
8112 finish_if_stmt (if_stmt);
8114 else if (CP_DECL_THREAD_LOCAL_P (decl))
8115 tls_aggregates = tree_cons (init, decl, tls_aggregates);
8116 else
8117 static_aggregates = tree_cons (init, decl, static_aggregates);
8121 /* Make TYPE a complete type based on INITIAL_VALUE.
8122 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
8123 2 if there was no information (in which case assume 0 if DO_DEFAULT),
8124 3 if the initializer list is empty (in pedantic mode). */
8127 cp_complete_array_type (tree *ptype, tree initial_value, bool do_default)
8129 int failure;
8130 tree type, elt_type;
8132 /* Don't get confused by a CONSTRUCTOR for some other type. */
8133 if (initial_value && TREE_CODE (initial_value) == CONSTRUCTOR
8134 && !BRACE_ENCLOSED_INITIALIZER_P (initial_value)
8135 && TREE_CODE (TREE_TYPE (initial_value)) != ARRAY_TYPE)
8136 return 1;
8138 if (initial_value)
8140 unsigned HOST_WIDE_INT i;
8141 tree value;
8143 /* An array of character type can be initialized from a
8144 brace-enclosed string constant.
8146 FIXME: this code is duplicated from reshape_init. Probably
8147 we should just call reshape_init here? */
8148 if (char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (*ptype)))
8149 && TREE_CODE (initial_value) == CONSTRUCTOR
8150 && !vec_safe_is_empty (CONSTRUCTOR_ELTS (initial_value)))
8152 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value);
8153 tree value = (*v)[0].value;
8155 if (TREE_CODE (value) == STRING_CST
8156 && v->length () == 1)
8157 initial_value = value;
8160 /* If any of the elements are parameter packs, we can't actually
8161 complete this type now because the array size is dependent. */
8162 if (TREE_CODE (initial_value) == CONSTRUCTOR)
8164 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (initial_value),
8165 i, value)
8167 if (PACK_EXPANSION_P (value))
8168 return 0;
8173 failure = complete_array_type (ptype, initial_value, do_default);
8175 /* We can create the array before the element type is complete, which
8176 means that we didn't have these two bits set in the original type
8177 either. In completing the type, we are expected to propagate these
8178 bits. See also complete_type which does the same thing for arrays
8179 of fixed size. */
8180 type = *ptype;
8181 if (TYPE_DOMAIN (type))
8183 elt_type = TREE_TYPE (type);
8184 TYPE_NEEDS_CONSTRUCTING (type) = TYPE_NEEDS_CONSTRUCTING (elt_type);
8185 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
8186 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (elt_type);
8189 return failure;
8192 /* As above, but either give an error or reject zero-size arrays, depending
8193 on COMPLAIN. */
8196 cp_complete_array_type_or_error (tree *ptype, tree initial_value,
8197 bool do_default, tsubst_flags_t complain)
8199 int failure;
8200 bool sfinae = !(complain & tf_error);
8201 /* In SFINAE context we can't be lenient about zero-size arrays. */
8202 if (sfinae)
8203 ++pedantic;
8204 failure = cp_complete_array_type (ptype, initial_value, do_default);
8205 if (sfinae)
8206 --pedantic;
8207 if (failure)
8209 if (sfinae)
8210 /* Not an error. */;
8211 else if (failure == 1)
8212 error ("initializer fails to determine size of %qT", *ptype);
8213 else if (failure == 2)
8215 if (do_default)
8216 error ("array size missing in %qT", *ptype);
8218 else if (failure == 3)
8219 error ("zero-size array %qT", *ptype);
8220 *ptype = error_mark_node;
8222 return failure;
8225 /* Return zero if something is declared to be a member of type
8226 CTYPE when in the context of CUR_TYPE. STRING is the error
8227 message to print in that case. Otherwise, quietly return 1. */
8229 static int
8230 member_function_or_else (tree ctype, tree cur_type, enum overload_flags flags)
8232 if (ctype && ctype != cur_type)
8234 if (flags == DTOR_FLAG)
8235 error ("destructor for alien class %qT cannot be a member", ctype);
8236 else
8237 error ("constructor for alien class %qT cannot be a member", ctype);
8238 return 0;
8240 return 1;
8243 /* Subroutine of `grokdeclarator'. */
8245 /* Generate errors possibly applicable for a given set of specifiers.
8246 This is for ARM $7.1.2. */
8248 static void
8249 bad_specifiers (tree object,
8250 enum bad_spec_place type,
8251 int virtualp,
8252 int quals,
8253 int inlinep,
8254 int friendp,
8255 int raises)
8257 switch (type)
8259 case BSP_VAR:
8260 if (virtualp)
8261 error ("%qD declared as a %<virtual%> variable", object);
8262 if (quals)
8263 error ("%<const%> and %<volatile%> function specifiers on "
8264 "%qD invalid in variable declaration", object);
8265 break;
8266 case BSP_PARM:
8267 if (virtualp)
8268 error ("%qD declared as a %<virtual%> parameter", object);
8269 if (inlinep)
8270 error ("%qD declared as an %<inline%> parameter", object);
8271 if (quals)
8272 error ("%<const%> and %<volatile%> function specifiers on "
8273 "%qD invalid in parameter declaration", object);
8274 break;
8275 case BSP_TYPE:
8276 if (virtualp)
8277 error ("%qD declared as a %<virtual%> type", object);
8278 if (inlinep)
8279 error ("%qD declared as an %<inline%> type", object);
8280 if (quals)
8281 error ("%<const%> and %<volatile%> function specifiers on "
8282 "%qD invalid in type declaration", object);
8283 break;
8284 case BSP_FIELD:
8285 if (virtualp)
8286 error ("%qD declared as a %<virtual%> field", object);
8287 if (inlinep)
8288 error ("%qD declared as an %<inline%> field", object);
8289 if (quals)
8290 error ("%<const%> and %<volatile%> function specifiers on "
8291 "%qD invalid in field declaration", object);
8292 break;
8293 default:
8294 gcc_unreachable();
8296 if (friendp)
8297 error ("%q+D declared as a friend", object);
8298 if (raises
8299 && !flag_noexcept_type
8300 && (TREE_CODE (object) == TYPE_DECL
8301 || (!TYPE_PTRFN_P (TREE_TYPE (object))
8302 && !TYPE_REFFN_P (TREE_TYPE (object))
8303 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (object)))))
8304 error ("%q+D declared with an exception specification", object);
8307 /* DECL is a member function or static data member and is presently
8308 being defined. Check that the definition is taking place in a
8309 valid namespace. */
8311 static void
8312 check_class_member_definition_namespace (tree decl)
8314 /* These checks only apply to member functions and static data
8315 members. */
8316 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
8317 /* We check for problems with specializations in pt.c in
8318 check_specialization_namespace, where we can issue better
8319 diagnostics. */
8320 if (processing_specialization)
8321 return;
8322 /* We check this in check_explicit_instantiation_namespace. */
8323 if (processing_explicit_instantiation)
8324 return;
8325 /* [class.mfct]
8327 A member function definition that appears outside of the
8328 class definition shall appear in a namespace scope enclosing
8329 the class definition.
8331 [class.static.data]
8333 The definition for a static data member shall appear in a
8334 namespace scope enclosing the member's class definition. */
8335 if (!is_ancestor (current_namespace, DECL_CONTEXT (decl)))
8336 permerror (input_location, "definition of %qD is not in namespace enclosing %qT",
8337 decl, DECL_CONTEXT (decl));
8340 /* Build a PARM_DECL for the "this" parameter of FN. TYPE is the
8341 METHOD_TYPE for a non-static member function; QUALS are the
8342 cv-qualifiers that apply to the function. */
8344 tree
8345 build_this_parm (tree fn, tree type, cp_cv_quals quals)
8347 tree this_type;
8348 tree qual_type;
8349 tree parm;
8350 cp_cv_quals this_quals;
8352 if (CLASS_TYPE_P (type))
8354 this_type
8355 = cp_build_qualified_type (type, quals & ~TYPE_QUAL_RESTRICT);
8356 this_type = build_pointer_type (this_type);
8358 else
8359 this_type = type_of_this_parm (type);
8360 /* The `this' parameter is implicitly `const'; it cannot be
8361 assigned to. */
8362 this_quals = (quals & TYPE_QUAL_RESTRICT) | TYPE_QUAL_CONST;
8363 qual_type = cp_build_qualified_type (this_type, this_quals);
8364 parm = build_artificial_parm (fn, this_identifier, qual_type);
8365 cp_apply_type_quals_to_decl (this_quals, parm);
8366 return parm;
8369 /* DECL is a static member function. Complain if it was declared
8370 with function-cv-quals. */
8372 static void
8373 check_static_quals (tree decl, cp_cv_quals quals)
8375 if (quals != TYPE_UNQUALIFIED)
8376 error ("static member function %q#D declared with type qualifiers",
8377 decl);
8380 // Check that FN takes no arguments and returns bool.
8381 static void
8382 check_concept_fn (tree fn)
8384 // A constraint is nullary.
8385 if (DECL_ARGUMENTS (fn))
8386 error ("concept %q#D declared with function parameters", fn);
8388 // The declared return type of the concept shall be bool, and
8389 // it shall not be deduced from it definition.
8390 tree type = TREE_TYPE (TREE_TYPE (fn));
8391 if (is_auto (type))
8392 error ("concept %q#D declared with a deduced return type", fn);
8393 else if (type != boolean_type_node)
8394 error ("concept %q#D with non-%<bool%> return type %qT", fn, type);
8397 /* Helper function. Replace the temporary this parameter injected
8398 during cp_finish_omp_declare_simd with the real this parameter. */
8400 static tree
8401 declare_simd_adjust_this (tree *tp, int *walk_subtrees, void *data)
8403 tree this_parm = (tree) data;
8404 if (TREE_CODE (*tp) == PARM_DECL
8405 && DECL_NAME (*tp) == this_identifier
8406 && *tp != this_parm)
8407 *tp = this_parm;
8408 else if (TYPE_P (*tp))
8409 *walk_subtrees = 0;
8410 return NULL_TREE;
8413 /* CTYPE is class type, or null if non-class.
8414 TYPE is type this FUNCTION_DECL should have, either FUNCTION_TYPE
8415 or METHOD_TYPE.
8416 DECLARATOR is the function's name.
8417 PARMS is a chain of PARM_DECLs for the function.
8418 VIRTUALP is truthvalue of whether the function is virtual or not.
8419 FLAGS are to be passed through to `grokclassfn'.
8420 QUALS are qualifiers indicating whether the function is `const'
8421 or `volatile'.
8422 RAISES is a list of exceptions that this function can raise.
8423 CHECK is 1 if we must find this method in CTYPE, 0 if we should
8424 not look, and -1 if we should not call `grokclassfn' at all.
8426 SFK is the kind of special function (if any) for the new function.
8428 Returns `NULL_TREE' if something goes wrong, after issuing
8429 applicable error messages. */
8431 static tree
8432 grokfndecl (tree ctype,
8433 tree type,
8434 tree declarator,
8435 tree parms,
8436 tree orig_declarator,
8437 tree decl_reqs,
8438 int virtualp,
8439 enum overload_flags flags,
8440 cp_cv_quals quals,
8441 cp_ref_qualifier rqual,
8442 tree raises,
8443 int check,
8444 int friendp,
8445 int publicp,
8446 int inlinep,
8447 bool deletedp,
8448 special_function_kind sfk,
8449 bool funcdef_flag,
8450 int template_count,
8451 tree in_namespace,
8452 tree* attrlist,
8453 location_t location)
8455 tree decl;
8456 int staticp = ctype && TREE_CODE (type) == FUNCTION_TYPE;
8457 tree t;
8459 // Was the concept specifier present?
8460 bool concept_p = inlinep & 4;
8462 // Concept declarations must have a corresponding definition.
8463 if (concept_p && !funcdef_flag)
8465 error ("concept %qD has no definition", declarator);
8466 return NULL_TREE;
8469 if (rqual)
8470 type = build_ref_qualified_type (type, rqual);
8471 if (raises)
8472 type = build_exception_variant (type, raises);
8474 decl = build_lang_decl (FUNCTION_DECL, declarator, type);
8476 /* Set the constraints on the declaration. */
8477 if (flag_concepts)
8479 tree tmpl_reqs = NULL_TREE;
8480 if (processing_template_decl > template_class_depth (ctype))
8481 tmpl_reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
8483 /* Adjust the required expression into a constraint. */
8484 if (decl_reqs)
8485 decl_reqs = normalize_expression (decl_reqs);
8487 tree ci = build_constraints (tmpl_reqs, decl_reqs);
8488 set_constraints (decl, ci);
8491 /* If we have an explicit location, use it, otherwise use whatever
8492 build_lang_decl used (probably input_location). */
8493 if (location != UNKNOWN_LOCATION)
8494 DECL_SOURCE_LOCATION (decl) = location;
8496 if (TREE_CODE (type) == METHOD_TYPE)
8498 tree parm = build_this_parm (decl, type, quals);
8499 DECL_CHAIN (parm) = parms;
8500 parms = parm;
8502 /* Allocate space to hold the vptr bit if needed. */
8503 SET_DECL_ALIGN (decl, MINIMUM_METHOD_BOUNDARY);
8505 DECL_ARGUMENTS (decl) = parms;
8506 for (t = parms; t; t = DECL_CHAIN (t))
8507 DECL_CONTEXT (t) = decl;
8508 /* Propagate volatile out from type to decl. */
8509 if (TYPE_VOLATILE (type))
8510 TREE_THIS_VOLATILE (decl) = 1;
8512 /* Setup decl according to sfk. */
8513 switch (sfk)
8515 case sfk_constructor:
8516 case sfk_copy_constructor:
8517 case sfk_move_constructor:
8518 DECL_CONSTRUCTOR_P (decl) = 1;
8519 break;
8520 case sfk_destructor:
8521 DECL_DESTRUCTOR_P (decl) = 1;
8522 break;
8523 default:
8524 break;
8527 if (friendp
8528 && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)
8530 if (funcdef_flag)
8531 error
8532 ("defining explicit specialization %qD in friend declaration",
8533 orig_declarator);
8534 else
8536 tree fns = TREE_OPERAND (orig_declarator, 0);
8537 tree args = TREE_OPERAND (orig_declarator, 1);
8539 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8541 /* Something like `template <class T> friend void f<T>()'. */
8542 error ("invalid use of template-id %qD in declaration "
8543 "of primary template",
8544 orig_declarator);
8545 return NULL_TREE;
8549 /* A friend declaration of the form friend void f<>(). Record
8550 the information in the TEMPLATE_ID_EXPR. */
8551 SET_DECL_IMPLICIT_INSTANTIATION (decl);
8553 gcc_assert (identifier_p (fns) || TREE_CODE (fns) == OVERLOAD);
8554 DECL_TEMPLATE_INFO (decl) = build_template_info (fns, args);
8556 for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t))
8557 if (TREE_PURPOSE (t)
8558 && TREE_CODE (TREE_PURPOSE (t)) == DEFAULT_ARG)
8560 error ("default arguments are not allowed in declaration "
8561 "of friend template specialization %qD",
8562 decl);
8563 return NULL_TREE;
8566 if (inlinep & 1)
8568 error ("%<inline%> is not allowed in declaration of friend "
8569 "template specialization %qD",
8570 decl);
8571 return NULL_TREE;
8576 /* If this decl has namespace scope, set that up. */
8577 if (in_namespace)
8578 set_decl_namespace (decl, in_namespace, friendp);
8579 else if (!ctype)
8580 DECL_CONTEXT (decl) = FROB_CONTEXT (current_decl_namespace ());
8582 /* `main' and builtins have implicit 'C' linkage. */
8583 if ((MAIN_NAME_P (declarator)
8584 || (IDENTIFIER_LENGTH (declarator) > 10
8585 && IDENTIFIER_POINTER (declarator)[0] == '_'
8586 && IDENTIFIER_POINTER (declarator)[1] == '_'
8587 && strncmp (IDENTIFIER_POINTER (declarator)+2, "builtin_", 8) == 0)
8588 || (targetcm.cxx_implicit_extern_c
8589 && targetcm.cxx_implicit_extern_c(IDENTIFIER_POINTER (declarator))))
8590 && current_lang_name == lang_name_cplusplus
8591 && ctype == NULL_TREE
8592 && DECL_FILE_SCOPE_P (decl))
8593 SET_DECL_LANGUAGE (decl, lang_c);
8595 /* Should probably propagate const out from type to decl I bet (mrs). */
8596 if (staticp)
8598 DECL_STATIC_FUNCTION_P (decl) = 1;
8599 DECL_CONTEXT (decl) = ctype;
8602 if (deletedp)
8603 DECL_DELETED_FN (decl) = 1;
8605 if (ctype)
8607 DECL_CONTEXT (decl) = ctype;
8608 if (funcdef_flag)
8609 check_class_member_definition_namespace (decl);
8612 if (ctype == NULL_TREE && DECL_MAIN_P (decl))
8614 if (PROCESSING_REAL_TEMPLATE_DECL_P())
8615 error ("cannot declare %<::main%> to be a template");
8616 if (inlinep & 1)
8617 error ("cannot declare %<::main%> to be inline");
8618 if (inlinep & 2)
8619 error ("cannot declare %<::main%> to be constexpr");
8620 if (!publicp)
8621 error ("cannot declare %<::main%> to be static");
8622 inlinep = 0;
8623 publicp = 1;
8626 /* Members of anonymous types and local classes have no linkage; make
8627 them internal. If a typedef is made later, this will be changed. */
8628 if (ctype && (!TREE_PUBLIC (TYPE_MAIN_DECL (ctype))
8629 || decl_function_context (TYPE_MAIN_DECL (ctype))))
8630 publicp = 0;
8632 if (publicp && cxx_dialect == cxx98)
8634 /* [basic.link]: A name with no linkage (notably, the name of a class
8635 or enumeration declared in a local scope) shall not be used to
8636 declare an entity with linkage.
8638 DR 757 relaxes this restriction for C++0x. */
8639 no_linkage_error (decl);
8642 TREE_PUBLIC (decl) = publicp;
8643 if (! publicp)
8645 DECL_INTERFACE_KNOWN (decl) = 1;
8646 DECL_NOT_REALLY_EXTERN (decl) = 1;
8649 /* If the declaration was declared inline, mark it as such. */
8650 if (inlinep)
8652 DECL_DECLARED_INLINE_P (decl) = 1;
8653 if (publicp)
8654 DECL_COMDAT (decl) = 1;
8656 if (inlinep & 2)
8657 DECL_DECLARED_CONSTEXPR_P (decl) = true;
8659 // If the concept declaration specifier was found, check
8660 // that the declaration satisfies the necessary requirements.
8661 if (concept_p)
8663 DECL_DECLARED_CONCEPT_P (decl) = true;
8664 check_concept_fn (decl);
8667 DECL_EXTERNAL (decl) = 1;
8668 if (TREE_CODE (type) == FUNCTION_TYPE)
8670 if (quals || rqual)
8671 TREE_TYPE (decl) = apply_memfn_quals (TREE_TYPE (decl),
8672 TYPE_UNQUALIFIED,
8673 REF_QUAL_NONE);
8675 if (quals)
8677 error (ctype
8678 ? G_("static member function %qD cannot have cv-qualifier")
8679 : G_("non-member function %qD cannot have cv-qualifier"),
8680 decl);
8681 quals = TYPE_UNQUALIFIED;
8684 if (rqual)
8686 error (ctype
8687 ? G_("static member function %qD cannot have ref-qualifier")
8688 : G_("non-member function %qD cannot have ref-qualifier"),
8689 decl);
8690 rqual = REF_QUAL_NONE;
8694 if (deduction_guide_p (decl))
8696 if (!DECL_NAMESPACE_SCOPE_P (decl))
8698 error_at (location, "deduction guide %qD must be declared at "
8699 "namespace scope", decl);
8700 return NULL_TREE;
8702 if (funcdef_flag)
8703 error_at (location,
8704 "deduction guide %qD must not have a function body", decl);
8706 else if (IDENTIFIER_ANY_OP_P (DECL_NAME (decl))
8707 && !grok_op_properties (decl, /*complain=*/true))
8708 return NULL_TREE;
8709 else if (UDLIT_OPER_P (DECL_NAME (decl)))
8711 bool long_long_unsigned_p;
8712 bool long_double_p;
8713 const char *suffix = NULL;
8714 /* [over.literal]/6: Literal operators shall not have C linkage. */
8715 if (DECL_LANGUAGE (decl) == lang_c)
8717 error ("literal operator with C linkage");
8718 return NULL_TREE;
8721 if (DECL_NAMESPACE_SCOPE_P (decl))
8723 if (!check_literal_operator_args (decl, &long_long_unsigned_p,
8724 &long_double_p))
8726 error ("%qD has invalid argument list", decl);
8727 return NULL_TREE;
8730 suffix = UDLIT_OP_SUFFIX (DECL_NAME (decl));
8731 if (long_long_unsigned_p)
8733 if (cpp_interpret_int_suffix (parse_in, suffix, strlen (suffix)))
8734 warning (0, "integer suffix %qs"
8735 " shadowed by implementation", suffix);
8737 else if (long_double_p)
8739 if (cpp_interpret_float_suffix (parse_in, suffix, strlen (suffix)))
8740 warning (0, "floating point suffix %qs"
8741 " shadowed by implementation", suffix);
8744 else
8746 error ("%qD must be a non-member function", decl);
8747 return NULL_TREE;
8751 if (funcdef_flag)
8752 /* Make the init_value nonzero so pushdecl knows this is not
8753 tentative. error_mark_node is replaced later with the BLOCK. */
8754 DECL_INITIAL (decl) = error_mark_node;
8756 if (TYPE_NOTHROW_P (type) || nothrow_libfn_p (decl))
8757 TREE_NOTHROW (decl) = 1;
8759 if (flag_openmp || flag_openmp_simd || flag_cilkplus)
8761 /* Adjust "omp declare simd" attributes. */
8762 tree ods = lookup_attribute ("omp declare simd", *attrlist);
8763 if (ods)
8765 tree attr;
8766 for (attr = ods; attr;
8767 attr = lookup_attribute ("omp declare simd", TREE_CHAIN (attr)))
8769 if (TREE_CODE (type) == METHOD_TYPE)
8770 walk_tree (&TREE_VALUE (attr), declare_simd_adjust_this,
8771 DECL_ARGUMENTS (decl), NULL);
8772 if (TREE_VALUE (attr) != NULL_TREE)
8774 tree cl = TREE_VALUE (TREE_VALUE (attr));
8775 cl = c_omp_declare_simd_clauses_to_numbers
8776 (DECL_ARGUMENTS (decl), cl);
8777 if (cl)
8778 TREE_VALUE (TREE_VALUE (attr)) = cl;
8779 else
8780 TREE_VALUE (attr) = NULL_TREE;
8786 /* Caller will do the rest of this. */
8787 if (check < 0)
8788 return decl;
8790 if (ctype != NULL_TREE)
8791 grokclassfn (ctype, decl, flags);
8793 /* 12.4/3 */
8794 if (cxx_dialect >= cxx11
8795 && DECL_DESTRUCTOR_P (decl)
8796 && !TYPE_BEING_DEFINED (DECL_CONTEXT (decl))
8797 && !processing_template_decl)
8798 deduce_noexcept_on_destructor (decl);
8800 decl = check_explicit_specialization (orig_declarator, decl,
8801 template_count,
8802 2 * funcdef_flag +
8803 4 * (friendp != 0) +
8804 8 * concept_p);
8805 if (decl == error_mark_node)
8806 return NULL_TREE;
8808 if (DECL_STATIC_FUNCTION_P (decl))
8809 check_static_quals (decl, quals);
8811 if (attrlist)
8813 cplus_decl_attributes (&decl, *attrlist, 0);
8814 *attrlist = NULL_TREE;
8817 /* Check main's type after attributes have been applied. */
8818 if (ctype == NULL_TREE && DECL_MAIN_P (decl))
8820 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
8821 integer_type_node))
8823 tree oldtypeargs = TYPE_ARG_TYPES (TREE_TYPE (decl));
8824 tree newtype;
8825 error ("%<::main%> must return %<int%>");
8826 newtype = build_function_type (integer_type_node, oldtypeargs);
8827 TREE_TYPE (decl) = newtype;
8829 if (warn_main)
8830 check_main_parameter_types (decl);
8833 if (ctype != NULL_TREE && check)
8835 tree old_decl = check_classfn (ctype, decl,
8836 (processing_template_decl
8837 > template_class_depth (ctype))
8838 ? current_template_parms
8839 : NULL_TREE);
8841 if (old_decl == error_mark_node)
8842 return NULL_TREE;
8844 if (old_decl)
8846 tree ok;
8847 tree pushed_scope;
8849 if (TREE_CODE (old_decl) == TEMPLATE_DECL)
8850 /* Because grokfndecl is always supposed to return a
8851 FUNCTION_DECL, we pull out the DECL_TEMPLATE_RESULT
8852 here. We depend on our callers to figure out that its
8853 really a template that's being returned. */
8854 old_decl = DECL_TEMPLATE_RESULT (old_decl);
8856 if (DECL_STATIC_FUNCTION_P (old_decl)
8857 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
8859 /* Remove the `this' parm added by grokclassfn. */
8860 revert_static_member_fn (decl);
8861 check_static_quals (decl, quals);
8863 if (DECL_ARTIFICIAL (old_decl))
8865 error ("definition of implicitly-declared %qD", old_decl);
8866 return NULL_TREE;
8868 else if (DECL_DEFAULTED_FN (old_decl))
8870 error ("definition of explicitly-defaulted %q+D", decl);
8871 inform (DECL_SOURCE_LOCATION (old_decl),
8872 "%q#D explicitly defaulted here", old_decl);
8873 return NULL_TREE;
8876 /* Since we've smashed OLD_DECL to its
8877 DECL_TEMPLATE_RESULT, we must do the same to DECL. */
8878 if (TREE_CODE (decl) == TEMPLATE_DECL)
8879 decl = DECL_TEMPLATE_RESULT (decl);
8881 /* Attempt to merge the declarations. This can fail, in
8882 the case of some invalid specialization declarations. */
8883 pushed_scope = push_scope (ctype);
8884 ok = duplicate_decls (decl, old_decl, friendp);
8885 if (pushed_scope)
8886 pop_scope (pushed_scope);
8887 if (!ok)
8889 error ("no %q#D member function declared in class %qT",
8890 decl, ctype);
8891 return NULL_TREE;
8893 if (ok == error_mark_node)
8894 return NULL_TREE;
8895 return old_decl;
8899 if (DECL_CONSTRUCTOR_P (decl) && !grok_ctor_properties (ctype, decl))
8900 return NULL_TREE;
8902 if (ctype == NULL_TREE || check)
8903 return decl;
8905 if (virtualp)
8906 DECL_VIRTUAL_P (decl) = 1;
8908 return decl;
8911 /* decl is a FUNCTION_DECL.
8912 specifiers are the parsed virt-specifiers.
8914 Set flags to reflect the virt-specifiers.
8916 Returns decl. */
8918 static tree
8919 set_virt_specifiers (tree decl, cp_virt_specifiers specifiers)
8921 if (decl == NULL_TREE)
8922 return decl;
8923 if (specifiers & VIRT_SPEC_OVERRIDE)
8924 DECL_OVERRIDE_P (decl) = 1;
8925 if (specifiers & VIRT_SPEC_FINAL)
8926 DECL_FINAL_P (decl) = 1;
8927 return decl;
8930 /* DECL is a VAR_DECL for a static data member. Set flags to reflect
8931 the linkage that DECL will receive in the object file. */
8933 static void
8934 set_linkage_for_static_data_member (tree decl)
8936 /* A static data member always has static storage duration and
8937 external linkage. Note that static data members are forbidden in
8938 local classes -- the only situation in which a class has
8939 non-external linkage. */
8940 TREE_PUBLIC (decl) = 1;
8941 TREE_STATIC (decl) = 1;
8942 /* For non-template classes, static data members are always put
8943 out in exactly those files where they are defined, just as
8944 with ordinary namespace-scope variables. */
8945 if (!processing_template_decl)
8946 DECL_INTERFACE_KNOWN (decl) = 1;
8949 /* Create a VAR_DECL named NAME with the indicated TYPE.
8951 If SCOPE is non-NULL, it is the class type or namespace containing
8952 the variable. If SCOPE is NULL, the variable should is created in
8953 the innermost enclosing scope. */
8955 static tree
8956 grokvardecl (tree type,
8957 tree name,
8958 tree orig_declarator,
8959 const cp_decl_specifier_seq *declspecs,
8960 int initialized,
8961 int type_quals,
8962 int inlinep,
8963 bool conceptp,
8964 int template_count,
8965 tree scope)
8967 tree decl;
8968 tree explicit_scope;
8970 gcc_assert (!name || identifier_p (name));
8972 bool constp = (type_quals & TYPE_QUAL_CONST) != 0;
8973 bool volatilep = (type_quals & TYPE_QUAL_VOLATILE) != 0;
8975 /* Compute the scope in which to place the variable, but remember
8976 whether or not that scope was explicitly specified by the user. */
8977 explicit_scope = scope;
8978 if (!scope)
8980 /* An explicit "extern" specifier indicates a namespace-scope
8981 variable. */
8982 if (declspecs->storage_class == sc_extern)
8983 scope = current_decl_namespace ();
8984 else if (!at_function_scope_p ())
8985 scope = current_scope ();
8988 if (scope
8989 && (/* If the variable is a namespace-scope variable declared in a
8990 template, we need DECL_LANG_SPECIFIC. */
8991 (TREE_CODE (scope) == NAMESPACE_DECL && processing_template_decl)
8992 /* Similarly for namespace-scope variables with language linkage
8993 other than C++. */
8994 || (TREE_CODE (scope) == NAMESPACE_DECL
8995 && current_lang_name != lang_name_cplusplus)
8996 /* Similarly for static data members. */
8997 || TYPE_P (scope)
8998 /* Similarly for explicit specializations. */
8999 || (orig_declarator
9000 && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)))
9001 decl = build_lang_decl (VAR_DECL, name, type);
9002 else
9003 decl = build_decl (input_location, VAR_DECL, name, type);
9005 if (explicit_scope && TREE_CODE (explicit_scope) == NAMESPACE_DECL)
9006 set_decl_namespace (decl, explicit_scope, 0);
9007 else
9008 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
9010 if (declspecs->storage_class == sc_extern)
9012 DECL_THIS_EXTERN (decl) = 1;
9013 DECL_EXTERNAL (decl) = !initialized;
9016 if (DECL_CLASS_SCOPE_P (decl))
9018 set_linkage_for_static_data_member (decl);
9019 /* This function is only called with out-of-class definitions. */
9020 DECL_EXTERNAL (decl) = 0;
9021 check_class_member_definition_namespace (decl);
9023 /* At top level, either `static' or no s.c. makes a definition
9024 (perhaps tentative), and absence of `static' makes it public. */
9025 else if (toplevel_bindings_p ())
9027 TREE_PUBLIC (decl) = (declspecs->storage_class != sc_static
9028 && (DECL_THIS_EXTERN (decl)
9029 || ! constp
9030 || volatilep
9031 || inlinep));
9032 TREE_STATIC (decl) = ! DECL_EXTERNAL (decl);
9034 /* Not at top level, only `static' makes a static definition. */
9035 else
9037 TREE_STATIC (decl) = declspecs->storage_class == sc_static;
9038 TREE_PUBLIC (decl) = DECL_EXTERNAL (decl);
9041 if (decl_spec_seq_has_spec_p (declspecs, ds_thread))
9043 if (DECL_EXTERNAL (decl) || TREE_STATIC (decl))
9045 CP_DECL_THREAD_LOCAL_P (decl) = true;
9046 if (!processing_template_decl)
9047 set_decl_tls_model (decl, decl_default_tls_model (decl));
9049 if (declspecs->gnu_thread_keyword_p)
9050 SET_DECL_GNU_TLS_P (decl);
9053 /* If the type of the decl has no linkage, make sure that we'll
9054 notice that in mark_used. */
9055 if (cxx_dialect > cxx98
9056 && decl_linkage (decl) != lk_none
9057 && DECL_LANG_SPECIFIC (decl) == NULL
9058 && !DECL_EXTERN_C_P (decl)
9059 && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
9060 retrofit_lang_decl (decl);
9062 if (TREE_PUBLIC (decl))
9064 /* [basic.link]: A name with no linkage (notably, the name of a class
9065 or enumeration declared in a local scope) shall not be used to
9066 declare an entity with linkage.
9068 DR 757 relaxes this restriction for C++0x. */
9069 if (cxx_dialect < cxx11)
9070 no_linkage_error (decl);
9072 else
9073 DECL_INTERFACE_KNOWN (decl) = 1;
9075 if (DECL_NAME (decl)
9076 && MAIN_NAME_P (DECL_NAME (decl))
9077 && scope == global_namespace)
9078 error ("cannot declare %<::main%> to be a global variable");
9080 /* Check that the variable can be safely declared as a concept.
9081 Note that this also forbids explicit specializations. */
9082 if (conceptp)
9084 if (!processing_template_decl)
9086 error ("a non-template variable cannot be %<concept%>");
9087 return NULL_TREE;
9089 else
9090 DECL_DECLARED_CONCEPT_P (decl) = true;
9091 if (!same_type_ignoring_top_level_qualifiers_p (type, boolean_type_node))
9092 error_at (declspecs->locations[ds_type_spec],
9093 "concept must have type %<bool%>");
9095 else if (flag_concepts
9096 && processing_template_decl > template_class_depth (scope))
9098 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
9099 tree ci = build_constraints (reqs, NULL_TREE);
9100 set_constraints (decl, ci);
9103 // Handle explicit specializations and instantiations of variable templates.
9104 if (orig_declarator)
9105 decl = check_explicit_specialization (orig_declarator, decl,
9106 template_count, conceptp * 8);
9108 return decl != error_mark_node ? decl : NULL_TREE;
9111 /* Create and return a canonical pointer to member function type, for
9112 TYPE, which is a POINTER_TYPE to a METHOD_TYPE. */
9114 tree
9115 build_ptrmemfunc_type (tree type)
9117 tree field, fields;
9118 tree t;
9120 if (type == error_mark_node)
9121 return type;
9123 /* Make sure that we always have the unqualified pointer-to-member
9124 type first. */
9125 if (cp_cv_quals quals = cp_type_quals (type))
9127 tree unqual = build_ptrmemfunc_type (TYPE_MAIN_VARIANT (type));
9128 return cp_build_qualified_type (unqual, quals);
9131 /* If a canonical type already exists for this type, use it. We use
9132 this method instead of type_hash_canon, because it only does a
9133 simple equality check on the list of field members. */
9136 t = TYPE_PTRMEMFUNC_TYPE (type);
9137 if (t)
9138 return t;
9140 t = make_node (RECORD_TYPE);
9142 /* Let the front end know this is a pointer to member function. */
9143 TYPE_PTRMEMFUNC_FLAG (t) = 1;
9145 field = build_decl (input_location, FIELD_DECL, pfn_identifier, type);
9146 fields = field;
9148 field = build_decl (input_location, FIELD_DECL, delta_identifier,
9149 delta_type_node);
9150 DECL_CHAIN (field) = fields;
9151 fields = field;
9153 finish_builtin_struct (t, "__ptrmemfunc_type", fields, ptr_type_node);
9155 /* Zap out the name so that the back end will give us the debugging
9156 information for this anonymous RECORD_TYPE. */
9157 TYPE_NAME (t) = NULL_TREE;
9159 /* Cache this pointer-to-member type so that we can find it again
9160 later. */
9161 TYPE_PTRMEMFUNC_TYPE (type) = t;
9163 if (TYPE_STRUCTURAL_EQUALITY_P (type))
9164 SET_TYPE_STRUCTURAL_EQUALITY (t);
9165 else if (TYPE_CANONICAL (type) != type)
9166 TYPE_CANONICAL (t) = build_ptrmemfunc_type (TYPE_CANONICAL (type));
9168 return t;
9171 /* Create and return a pointer to data member type. */
9173 tree
9174 build_ptrmem_type (tree class_type, tree member_type)
9176 if (TREE_CODE (member_type) == METHOD_TYPE)
9178 cp_cv_quals quals = type_memfn_quals (member_type);
9179 cp_ref_qualifier rqual = type_memfn_rqual (member_type);
9180 member_type = build_memfn_type (member_type, class_type, quals, rqual);
9181 return build_ptrmemfunc_type (build_pointer_type (member_type));
9183 else
9185 gcc_assert (TREE_CODE (member_type) != FUNCTION_TYPE);
9186 return build_offset_type (class_type, member_type);
9190 /* DECL is a VAR_DECL defined in-class, whose TYPE is also given.
9191 Check to see that the definition is valid. Issue appropriate error
9192 messages. Return 1 if the definition is particularly bad, or 0
9193 otherwise. */
9195 static int
9196 check_static_variable_definition (tree decl, tree type)
9198 /* Avoid redundant diagnostics on out-of-class definitions. */
9199 if (!current_class_type || !TYPE_BEING_DEFINED (current_class_type))
9200 return 0;
9201 /* Can't check yet if we don't know the type. */
9202 if (dependent_type_p (type))
9203 return 0;
9204 /* If DECL is declared constexpr, we'll do the appropriate checks
9205 in check_initializer. Similarly for inline static data members. */
9206 if (DECL_P (decl)
9207 && (DECL_DECLARED_CONSTEXPR_P (decl)
9208 || DECL_VAR_DECLARED_INLINE_P (decl)))
9209 return 0;
9210 else if (cxx_dialect >= cxx11 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
9212 if (!COMPLETE_TYPE_P (type))
9213 error_at (DECL_SOURCE_LOCATION (decl),
9214 "in-class initialization of static data member %q#D of "
9215 "incomplete type", decl);
9216 else if (literal_type_p (type))
9217 permerror (DECL_SOURCE_LOCATION (decl),
9218 "%<constexpr%> needed for in-class initialization of "
9219 "static data member %q#D of non-integral type", decl);
9220 else
9221 error_at (DECL_SOURCE_LOCATION (decl),
9222 "in-class initialization of static data member %q#D of "
9223 "non-literal type", decl);
9224 return 1;
9227 /* Motion 10 at San Diego: If a static const integral data member is
9228 initialized with an integral constant expression, the initializer
9229 may appear either in the declaration (within the class), or in
9230 the definition, but not both. If it appears in the class, the
9231 member is a member constant. The file-scope definition is always
9232 required. */
9233 if (!ARITHMETIC_TYPE_P (type) && TREE_CODE (type) != ENUMERAL_TYPE)
9235 error_at (DECL_SOURCE_LOCATION (decl),
9236 "invalid in-class initialization of static data member "
9237 "of non-integral type %qT",
9238 type);
9239 return 1;
9241 else if (!CP_TYPE_CONST_P (type))
9242 error_at (DECL_SOURCE_LOCATION (decl),
9243 "ISO C++ forbids in-class initialization of non-const "
9244 "static member %qD",
9245 decl);
9246 else if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type))
9247 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
9248 "ISO C++ forbids initialization of member constant "
9249 "%qD of non-integral type %qT", decl, type);
9251 return 0;
9254 /* *expr_p is part of the TYPE_SIZE of a variably-sized array. If any
9255 SAVE_EXPRs in *expr_p wrap expressions with side-effects, break those
9256 expressions out into temporary variables so that walk_tree doesn't
9257 step into them (c++/15764). */
9259 static tree
9260 stabilize_save_expr_r (tree *expr_p, int *walk_subtrees, void *data)
9262 hash_set<tree> *pset = (hash_set<tree> *)data;
9263 tree expr = *expr_p;
9264 if (TREE_CODE (expr) == SAVE_EXPR)
9266 tree op = TREE_OPERAND (expr, 0);
9267 cp_walk_tree (&op, stabilize_save_expr_r, data, pset);
9268 if (TREE_SIDE_EFFECTS (op))
9269 TREE_OPERAND (expr, 0) = get_temp_regvar (TREE_TYPE (op), op);
9270 *walk_subtrees = 0;
9272 else if (!EXPR_P (expr) || !TREE_SIDE_EFFECTS (expr))
9273 *walk_subtrees = 0;
9274 return NULL;
9277 /* Entry point for the above. */
9279 static void
9280 stabilize_vla_size (tree size)
9282 hash_set<tree> pset;
9283 /* Break out any function calls into temporary variables. */
9284 cp_walk_tree (&size, stabilize_save_expr_r, &pset, &pset);
9287 /* Reduce a SIZEOF_EXPR to its value. */
9289 tree
9290 fold_sizeof_expr (tree t)
9292 tree r;
9293 if (SIZEOF_EXPR_TYPE_P (t))
9294 r = cxx_sizeof_or_alignof_type (TREE_TYPE (TREE_OPERAND (t, 0)),
9295 SIZEOF_EXPR, false);
9296 else if (TYPE_P (TREE_OPERAND (t, 0)))
9297 r = cxx_sizeof_or_alignof_type (TREE_OPERAND (t, 0), SIZEOF_EXPR,
9298 false);
9299 else
9300 r = cxx_sizeof_or_alignof_expr (TREE_OPERAND (t, 0), SIZEOF_EXPR,
9301 false);
9302 if (r == error_mark_node)
9303 r = size_one_node;
9304 return r;
9307 /* Given the SIZE (i.e., number of elements) in an array, compute
9308 an appropriate index type for the array. If non-NULL, NAME is
9309 the name of the entity being declared. */
9311 tree
9312 compute_array_index_type (tree name, tree size, tsubst_flags_t complain)
9314 tree itype;
9315 tree osize = size;
9317 if (error_operand_p (size))
9318 return error_mark_node;
9320 if (!type_dependent_expression_p (size))
9322 tree type = TREE_TYPE (size);
9324 mark_rvalue_use (size);
9326 if (cxx_dialect < cxx11 && TREE_CODE (size) == NOP_EXPR
9327 && TREE_SIDE_EFFECTS (size))
9328 /* In C++98, we mark a non-constant array bound with a magic
9329 NOP_EXPR with TREE_SIDE_EFFECTS; don't fold in that case. */;
9330 else
9332 size = instantiate_non_dependent_expr_sfinae (size, complain);
9334 if (CLASS_TYPE_P (type)
9335 && CLASSTYPE_LITERAL_P (type))
9337 size = build_expr_type_conversion (WANT_INT, size, true);
9338 if (!size)
9340 if (!(complain & tf_error))
9341 return error_mark_node;
9342 if (name)
9343 error ("size of array %qD has non-integral type %qT",
9344 name, type);
9345 else
9346 error ("size of array has non-integral type %qT", type);
9347 size = integer_one_node;
9349 if (size == error_mark_node)
9350 return error_mark_node;
9351 type = TREE_TYPE (size);
9354 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
9355 size = maybe_constant_value (size);
9357 if (!TREE_CONSTANT (size))
9358 size = osize;
9361 if (error_operand_p (size))
9362 return error_mark_node;
9364 /* The array bound must be an integer type. */
9365 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
9367 if (!(complain & tf_error))
9368 return error_mark_node;
9369 if (name)
9370 error ("size of array %qD has non-integral type %qT", name, type);
9371 else
9372 error ("size of array has non-integral type %qT", type);
9373 size = integer_one_node;
9374 type = TREE_TYPE (size);
9378 /* A type is dependent if it is...an array type constructed from any
9379 dependent type or whose size is specified by a constant expression
9380 that is value-dependent. */
9381 /* We can only call value_dependent_expression_p on integral constant
9382 expressions; treat non-constant expressions as dependent, too. */
9383 if (processing_template_decl
9384 && (type_dependent_expression_p (size)
9385 || !TREE_CONSTANT (size) || value_dependent_expression_p (size)))
9387 /* We cannot do any checking for a SIZE that isn't known to be
9388 constant. Just build the index type and mark that it requires
9389 structural equality checks. */
9390 itype = build_index_type (build_min (MINUS_EXPR, sizetype,
9391 size, size_one_node));
9392 TYPE_DEPENDENT_P (itype) = 1;
9393 TYPE_DEPENDENT_P_VALID (itype) = 1;
9394 SET_TYPE_STRUCTURAL_EQUALITY (itype);
9395 return itype;
9398 if (TREE_CODE (size) != INTEGER_CST)
9400 tree folded = cp_fully_fold (size);
9401 if (TREE_CODE (folded) == INTEGER_CST)
9402 pedwarn (location_of (size), OPT_Wpedantic,
9403 "size of array is not an integral constant-expression");
9404 /* Use the folded result for VLAs, too; it will have resolved
9405 SIZEOF_EXPR. */
9406 size = folded;
9409 /* Normally, the array-bound will be a constant. */
9410 if (TREE_CODE (size) == INTEGER_CST)
9412 /* Check to see if the array bound overflowed. Make that an
9413 error, no matter how generous we're being. */
9414 constant_expression_error (size);
9416 /* An array must have a positive number of elements. */
9417 if (tree_int_cst_lt (size, integer_zero_node))
9419 if (!(complain & tf_error))
9420 return error_mark_node;
9421 if (name)
9422 error ("size of array %qD is negative", name);
9423 else
9424 error ("size of array is negative");
9425 size = integer_one_node;
9427 /* As an extension we allow zero-sized arrays. */
9428 else if (integer_zerop (size))
9430 if (!(complain & tf_error))
9431 /* We must fail if performing argument deduction (as
9432 indicated by the state of complain), so that
9433 another substitution can be found. */
9434 return error_mark_node;
9435 else if (in_system_header_at (input_location))
9436 /* Allow them in system headers because glibc uses them. */;
9437 else if (name)
9438 pedwarn (input_location, OPT_Wpedantic, "ISO C++ forbids zero-size array %qD", name);
9439 else
9440 pedwarn (input_location, OPT_Wpedantic, "ISO C++ forbids zero-size array");
9443 else if (TREE_CONSTANT (size)
9444 /* We don't allow VLAs at non-function scopes, or during
9445 tentative template substitution. */
9446 || !at_function_scope_p ()
9447 || !(complain & tf_error))
9449 if (!(complain & tf_error))
9450 return error_mark_node;
9451 /* `(int) &fn' is not a valid array bound. */
9452 if (name)
9453 error ("size of array %qD is not an integral constant-expression",
9454 name);
9455 else
9456 error ("size of array is not an integral constant-expression");
9457 size = integer_one_node;
9459 else if (pedantic && warn_vla != 0)
9461 if (name)
9462 pedwarn (input_location, OPT_Wvla, "ISO C++ forbids variable length array %qD", name);
9463 else
9464 pedwarn (input_location, OPT_Wvla, "ISO C++ forbids variable length array");
9466 else if (warn_vla > 0)
9468 if (name)
9469 warning (OPT_Wvla,
9470 "variable length array %qD is used", name);
9471 else
9472 warning (OPT_Wvla,
9473 "variable length array is used");
9476 if (processing_template_decl && !TREE_CONSTANT (size))
9477 /* A variable sized array. */
9478 itype = build_min (MINUS_EXPR, sizetype, size, integer_one_node);
9479 else
9481 HOST_WIDE_INT saved_processing_template_decl;
9483 /* Compute the index of the largest element in the array. It is
9484 one less than the number of elements in the array. We save
9485 and restore PROCESSING_TEMPLATE_DECL so that computations in
9486 cp_build_binary_op will be appropriately folded. */
9487 saved_processing_template_decl = processing_template_decl;
9488 processing_template_decl = 0;
9489 itype = cp_build_binary_op (input_location,
9490 MINUS_EXPR,
9491 cp_convert (ssizetype, size, complain),
9492 cp_convert (ssizetype, integer_one_node,
9493 complain),
9494 complain);
9495 itype = maybe_constant_value (itype);
9496 processing_template_decl = saved_processing_template_decl;
9498 if (!TREE_CONSTANT (itype))
9500 /* A variable sized array. */
9501 itype = variable_size (itype);
9503 stabilize_vla_size (itype);
9505 if (sanitize_flags_p (SANITIZE_VLA))
9507 /* We have to add 1 -- in the ubsan routine we generate
9508 LE_EXPR rather than LT_EXPR. */
9509 tree t = fold_build2 (PLUS_EXPR, TREE_TYPE (itype), itype,
9510 build_one_cst (TREE_TYPE (itype)));
9511 t = ubsan_instrument_vla (input_location, t);
9512 finish_expr_stmt (t);
9515 /* Make sure that there was no overflow when creating to a signed
9516 index type. (For example, on a 32-bit machine, an array with
9517 size 2^32 - 1 is too big.) */
9518 else if (TREE_CODE (itype) == INTEGER_CST
9519 && TREE_OVERFLOW (itype))
9521 if (!(complain & tf_error))
9522 return error_mark_node;
9523 error ("overflow in array dimension");
9524 TREE_OVERFLOW (itype) = 0;
9528 /* Create and return the appropriate index type. */
9529 itype = build_index_type (itype);
9531 /* If the index type were dependent, we would have returned early, so
9532 remember that it isn't. */
9533 TYPE_DEPENDENT_P (itype) = 0;
9534 TYPE_DEPENDENT_P_VALID (itype) = 1;
9535 return itype;
9538 /* Returns the scope (if any) in which the entity declared by
9539 DECLARATOR will be located. If the entity was declared with an
9540 unqualified name, NULL_TREE is returned. */
9542 tree
9543 get_scope_of_declarator (const cp_declarator *declarator)
9545 while (declarator && declarator->kind != cdk_id)
9546 declarator = declarator->declarator;
9548 /* If the declarator-id is a SCOPE_REF, the scope in which the
9549 declaration occurs is the first operand. */
9550 if (declarator
9551 && declarator->u.id.qualifying_scope)
9552 return declarator->u.id.qualifying_scope;
9554 /* Otherwise, the declarator is not a qualified name; the entity will
9555 be declared in the current scope. */
9556 return NULL_TREE;
9559 /* Returns an ARRAY_TYPE for an array with SIZE elements of the
9560 indicated TYPE. If non-NULL, NAME is the NAME of the declaration
9561 with this type. */
9563 static tree
9564 create_array_type_for_decl (tree name, tree type, tree size)
9566 tree itype = NULL_TREE;
9568 /* If things have already gone awry, bail now. */
9569 if (type == error_mark_node || size == error_mark_node)
9570 return error_mark_node;
9572 /* 8.3.4/1: If the type of the identifier of D contains the auto
9573 type-specifier, the program is ill-formed. */
9574 if (type_uses_auto (type))
9576 error ("%qD declared as array of %qT", name, type);
9577 return error_mark_node;
9580 /* If there are some types which cannot be array elements,
9581 issue an error-message and return. */
9582 switch (TREE_CODE (type))
9584 case VOID_TYPE:
9585 if (name)
9586 error ("declaration of %qD as array of void", name);
9587 else
9588 error ("creating array of void");
9589 return error_mark_node;
9591 case FUNCTION_TYPE:
9592 if (name)
9593 error ("declaration of %qD as array of functions", name);
9594 else
9595 error ("creating array of functions");
9596 return error_mark_node;
9598 case REFERENCE_TYPE:
9599 if (name)
9600 error ("declaration of %qD as array of references", name);
9601 else
9602 error ("creating array of references");
9603 return error_mark_node;
9605 case METHOD_TYPE:
9606 if (name)
9607 error ("declaration of %qD as array of function members", name);
9608 else
9609 error ("creating array of function members");
9610 return error_mark_node;
9612 default:
9613 break;
9616 /* [dcl.array]
9618 The constant expressions that specify the bounds of the arrays
9619 can be omitted only for the first member of the sequence. */
9620 if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type))
9622 if (name)
9623 error ("declaration of %qD as multidimensional array must "
9624 "have bounds for all dimensions except the first",
9625 name);
9626 else
9627 error ("multidimensional array must have bounds for all "
9628 "dimensions except the first");
9630 return error_mark_node;
9633 /* Figure out the index type for the array. */
9634 if (size)
9635 itype = compute_array_index_type (name, size, tf_warning_or_error);
9637 /* [dcl.array]
9638 T is called the array element type; this type shall not be [...] an
9639 abstract class type. */
9640 abstract_virtuals_error (name, type);
9642 return build_cplus_array_type (type, itype);
9645 /* Returns the smallest location != UNKNOWN_LOCATION among the
9646 three stored in LOCATIONS[ds_const], LOCATIONS[ds_volatile],
9647 and LOCATIONS[ds_restrict]. */
9649 static location_t
9650 smallest_type_quals_location (int type_quals, const location_t* locations)
9652 location_t loc = UNKNOWN_LOCATION;
9654 if (type_quals & TYPE_QUAL_CONST)
9655 loc = locations[ds_const];
9657 if ((type_quals & TYPE_QUAL_VOLATILE)
9658 && (loc == UNKNOWN_LOCATION || locations[ds_volatile] < loc))
9659 loc = locations[ds_volatile];
9661 if ((type_quals & TYPE_QUAL_RESTRICT)
9662 && (loc == UNKNOWN_LOCATION || locations[ds_restrict] < loc))
9663 loc = locations[ds_restrict];
9665 return loc;
9668 /* Check that it's OK to declare a function with the indicated TYPE
9669 and TYPE_QUALS. SFK indicates the kind of special function (if any)
9670 that this function is. OPTYPE is the type given in a conversion
9671 operator declaration, or the class type for a constructor/destructor.
9672 Returns the actual return type of the function; that may be different
9673 than TYPE if an error occurs, or for certain special functions. */
9675 static tree
9676 check_special_function_return_type (special_function_kind sfk,
9677 tree type,
9678 tree optype,
9679 int type_quals,
9680 const location_t* locations)
9682 switch (sfk)
9684 case sfk_constructor:
9685 if (type)
9686 error ("return type specification for constructor invalid");
9687 else if (type_quals != TYPE_UNQUALIFIED)
9688 error_at (smallest_type_quals_location (type_quals, locations),
9689 "qualifiers are not allowed on constructor declaration");
9691 if (targetm.cxx.cdtor_returns_this ())
9692 type = build_pointer_type (optype);
9693 else
9694 type = void_type_node;
9695 break;
9697 case sfk_destructor:
9698 if (type)
9699 error ("return type specification for destructor invalid");
9700 else if (type_quals != TYPE_UNQUALIFIED)
9701 error_at (smallest_type_quals_location (type_quals, locations),
9702 "qualifiers are not allowed on destructor declaration");
9704 /* We can't use the proper return type here because we run into
9705 problems with ambiguous bases and covariant returns. */
9706 if (targetm.cxx.cdtor_returns_this ())
9707 type = build_pointer_type (void_type_node);
9708 else
9709 type = void_type_node;
9710 break;
9712 case sfk_conversion:
9713 if (type)
9714 error ("return type specified for %<operator %T%>", optype);
9715 else if (type_quals != TYPE_UNQUALIFIED)
9716 error_at (smallest_type_quals_location (type_quals, locations),
9717 "qualifiers are not allowed on declaration of "
9718 "%<operator %T%>", optype);
9720 type = optype;
9721 break;
9723 case sfk_deduction_guide:
9724 if (type)
9725 error ("return type specified for deduction guide");
9726 else if (type_quals != TYPE_UNQUALIFIED)
9727 error_at (smallest_type_quals_location (type_quals, locations),
9728 "qualifiers are not allowed on declaration of "
9729 "deduction guide");
9730 type = make_template_placeholder (CLASSTYPE_TI_TEMPLATE (optype));
9731 for (int i = 0; i < ds_last; ++i)
9732 if (i != ds_explicit && locations[i])
9733 error_at (locations[i],
9734 "decl-specifier in declaration of deduction guide");
9735 break;
9737 default:
9738 gcc_unreachable ();
9741 return type;
9744 /* A variable or data member (whose unqualified name is IDENTIFIER)
9745 has been declared with the indicated TYPE. If the TYPE is not
9746 acceptable, issue an error message and return a type to use for
9747 error-recovery purposes. */
9749 tree
9750 check_var_type (tree identifier, tree type)
9752 if (VOID_TYPE_P (type))
9754 if (!identifier)
9755 error ("unnamed variable or field declared void");
9756 else if (identifier_p (identifier))
9758 gcc_assert (!IDENTIFIER_ANY_OP_P (identifier));
9759 error ("variable or field %qE declared void", identifier);
9761 else
9762 error ("variable or field declared void");
9763 type = error_mark_node;
9766 return type;
9769 /* Handle declaring DECL as an inline variable. */
9771 static void
9772 mark_inline_variable (tree decl)
9774 bool inlinep = true;
9775 if (! toplevel_bindings_p ())
9777 error ("%<inline%> specifier invalid for variable "
9778 "%qD declared at block scope", decl);
9779 inlinep = false;
9781 else if (cxx_dialect < cxx1z)
9782 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
9783 "inline variables are only available "
9784 "with -std=c++1z or -std=gnu++1z");
9785 if (inlinep)
9787 retrofit_lang_decl (decl);
9788 SET_DECL_VAR_DECLARED_INLINE_P (decl);
9793 /* Assign a typedef-given name to a class or enumeration type declared
9794 as anonymous at first. This was split out of grokdeclarator
9795 because it is also used in libcc1. */
9797 void
9798 name_unnamed_type (tree type, tree decl)
9800 gcc_assert (TYPE_UNNAMED_P (type));
9802 /* Replace the anonymous name with the real name everywhere. */
9803 for (tree t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
9805 if (anon_aggrname_p (TYPE_IDENTIFIER (t)))
9806 /* We do not rename the debug info representing the
9807 unnamed tagged type because the standard says in
9808 [dcl.typedef] that the naming applies only for
9809 linkage purposes. */
9810 /*debug_hooks->set_name (t, decl);*/
9811 TYPE_NAME (t) = decl;
9814 if (TYPE_LANG_SPECIFIC (type))
9815 TYPE_WAS_UNNAMED (type) = 1;
9817 /* If this is a typedef within a template class, the nested
9818 type is a (non-primary) template. The name for the
9819 template needs updating as well. */
9820 if (TYPE_LANG_SPECIFIC (type) && CLASSTYPE_TEMPLATE_INFO (type))
9821 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type))
9822 = TYPE_IDENTIFIER (type);
9824 /* Adjust linkage now that we aren't unnamed anymore. */
9825 reset_type_linkage (type);
9827 /* FIXME remangle member functions; member functions of a
9828 type with external linkage have external linkage. */
9830 /* Check that our job is done, and that it would fail if we
9831 attempted to do it again. */
9832 gcc_assert (!TYPE_UNNAMED_P (type));
9835 /* Given declspecs and a declarator (abstract or otherwise), determine
9836 the name and type of the object declared and construct a DECL node
9837 for it.
9839 DECLSPECS points to the representation of declaration-specifier
9840 sequence that precedes declarator.
9842 DECL_CONTEXT says which syntactic context this declaration is in:
9843 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
9844 FUNCDEF for a function definition. Like NORMAL but a few different
9845 error messages in each case. Return value may be zero meaning
9846 this definition is too screwy to try to parse.
9847 MEMFUNCDEF for a function definition. Like FUNCDEF but prepares to
9848 handle member functions (which have FIELD context).
9849 Return value may be zero meaning this definition is too screwy to
9850 try to parse.
9851 PARM for a parameter declaration (either within a function prototype
9852 or before a function body). Make a PARM_DECL, or return void_type_node.
9853 TPARM for a template parameter declaration.
9854 CATCHPARM for a parameter declaration before a catch clause.
9855 TYPENAME if for a typename (in a cast or sizeof).
9856 Don't make a DECL node; just return the ..._TYPE node.
9857 FIELD for a struct or union field; make a FIELD_DECL.
9858 BITFIELD for a field with specified width.
9860 INITIALIZED is as for start_decl.
9862 ATTRLIST is a pointer to the list of attributes, which may be NULL
9863 if there are none; *ATTRLIST may be modified if attributes from inside
9864 the declarator should be applied to the declaration.
9866 When this function is called, scoping variables (such as
9867 CURRENT_CLASS_TYPE) should reflect the scope in which the
9868 declaration occurs, not the scope in which the new declaration will
9869 be placed. For example, on:
9871 void S::f() { ... }
9873 when grokdeclarator is called for `S::f', the CURRENT_CLASS_TYPE
9874 should not be `S'.
9876 Returns a DECL (if a declarator is present), a TYPE (if there is no
9877 declarator, in cases like "struct S;"), or the ERROR_MARK_NODE if an
9878 error occurs. */
9880 tree
9881 grokdeclarator (const cp_declarator *declarator,
9882 cp_decl_specifier_seq *declspecs,
9883 enum decl_context decl_context,
9884 int initialized,
9885 tree* attrlist)
9887 tree type = NULL_TREE;
9888 int longlong = 0;
9889 int explicit_intN = 0;
9890 int virtualp, explicitp, friendp, inlinep, staticp;
9891 int explicit_int = 0;
9892 int explicit_char = 0;
9893 int defaulted_int = 0;
9895 tree typedef_decl = NULL_TREE;
9896 const char *name = NULL;
9897 tree typedef_type = NULL_TREE;
9898 /* True if this declarator is a function definition. */
9899 bool funcdef_flag = false;
9900 cp_declarator_kind innermost_code = cdk_error;
9901 int bitfield = 0;
9902 #if 0
9903 /* See the code below that used this. */
9904 tree decl_attr = NULL_TREE;
9905 #endif
9907 /* Keep track of what sort of function is being processed
9908 so that we can warn about default return values, or explicit
9909 return values which do not match prescribed defaults. */
9910 special_function_kind sfk = sfk_none;
9912 tree dname = NULL_TREE;
9913 tree ctor_return_type = NULL_TREE;
9914 enum overload_flags flags = NO_SPECIAL;
9915 /* cv-qualifiers that apply to the declarator, for a declaration of
9916 a member function. */
9917 cp_cv_quals memfn_quals = TYPE_UNQUALIFIED;
9918 /* virt-specifiers that apply to the declarator, for a declaration of
9919 a member function. */
9920 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
9921 /* ref-qualifier that applies to the declarator, for a declaration of
9922 a member function. */
9923 cp_ref_qualifier rqual = REF_QUAL_NONE;
9924 /* cv-qualifiers that apply to the type specified by the DECLSPECS. */
9925 int type_quals = TYPE_UNQUALIFIED;
9926 tree raises = NULL_TREE;
9927 int template_count = 0;
9928 tree returned_attrs = NULL_TREE;
9929 tree parms = NULL_TREE;
9930 const cp_declarator *id_declarator;
9931 /* The unqualified name of the declarator; either an
9932 IDENTIFIER_NODE, BIT_NOT_EXPR, or TEMPLATE_ID_EXPR. */
9933 tree unqualified_id;
9934 /* The class type, if any, in which this entity is located,
9935 or NULL_TREE if none. Note that this value may be different from
9936 the current class type; for example if an attempt is made to declare
9937 "A::f" inside "B", this value will be "A". */
9938 tree ctype = current_class_type;
9939 /* The NAMESPACE_DECL for the namespace in which this entity is
9940 located. If an unqualified name is used to declare the entity,
9941 this value will be NULL_TREE, even if the entity is located at
9942 namespace scope. */
9943 tree in_namespace = NULL_TREE;
9944 cp_storage_class storage_class;
9945 bool unsigned_p, signed_p, short_p, long_p, thread_p;
9946 bool type_was_error_mark_node = false;
9947 bool parameter_pack_p = declarator ? declarator->parameter_pack_p : false;
9948 bool template_type_arg = false;
9949 bool template_parm_flag = false;
9950 bool typedef_p = decl_spec_seq_has_spec_p (declspecs, ds_typedef);
9951 bool constexpr_p = decl_spec_seq_has_spec_p (declspecs, ds_constexpr);
9952 bool late_return_type_p = false;
9953 bool array_parameter_p = false;
9954 source_location saved_loc = input_location;
9955 tree reqs = NULL_TREE;
9957 signed_p = decl_spec_seq_has_spec_p (declspecs, ds_signed);
9958 unsigned_p = decl_spec_seq_has_spec_p (declspecs, ds_unsigned);
9959 short_p = decl_spec_seq_has_spec_p (declspecs, ds_short);
9960 long_p = decl_spec_seq_has_spec_p (declspecs, ds_long);
9961 longlong = decl_spec_seq_has_spec_p (declspecs, ds_long_long);
9962 explicit_intN = declspecs->explicit_intN_p;
9963 thread_p = decl_spec_seq_has_spec_p (declspecs, ds_thread);
9965 // Was concept_p specified? Note that ds_concept
9966 // implies ds_constexpr!
9967 bool concept_p = decl_spec_seq_has_spec_p (declspecs, ds_concept);
9968 if (concept_p)
9969 constexpr_p = true;
9971 if (decl_spec_seq_has_spec_p (declspecs, ds_const))
9972 type_quals |= TYPE_QUAL_CONST;
9973 if (decl_spec_seq_has_spec_p (declspecs, ds_volatile))
9974 type_quals |= TYPE_QUAL_VOLATILE;
9975 if (decl_spec_seq_has_spec_p (declspecs, ds_restrict))
9976 type_quals |= TYPE_QUAL_RESTRICT;
9978 if (decl_context == FUNCDEF)
9979 funcdef_flag = true, decl_context = NORMAL;
9980 else if (decl_context == MEMFUNCDEF)
9981 funcdef_flag = true, decl_context = FIELD;
9982 else if (decl_context == BITFIELD)
9983 bitfield = 1, decl_context = FIELD;
9984 else if (decl_context == TEMPLATE_TYPE_ARG)
9985 template_type_arg = true, decl_context = TYPENAME;
9986 else if (decl_context == TPARM)
9987 template_parm_flag = true, decl_context = PARM;
9989 if (initialized > 1)
9990 funcdef_flag = true;
9992 location_t typespec_loc = smallest_type_quals_location (type_quals,
9993 declspecs->locations);
9994 if (typespec_loc == UNKNOWN_LOCATION)
9995 typespec_loc = declspecs->locations[ds_type_spec];
9997 /* Look inside a declarator for the name being declared
9998 and get it as a string, for an error message. */
9999 for (id_declarator = declarator;
10000 id_declarator;
10001 id_declarator = id_declarator->declarator)
10003 if (id_declarator->kind != cdk_id)
10004 innermost_code = id_declarator->kind;
10006 switch (id_declarator->kind)
10008 case cdk_function:
10009 if (id_declarator->declarator
10010 && id_declarator->declarator->kind == cdk_id)
10012 sfk = id_declarator->declarator->u.id.sfk;
10013 if (sfk == sfk_destructor)
10014 flags = DTOR_FLAG;
10016 break;
10018 case cdk_id:
10020 tree qualifying_scope = id_declarator->u.id.qualifying_scope;
10021 tree decl = id_declarator->u.id.unqualified_name;
10022 if (!decl)
10023 break;
10024 if (qualifying_scope)
10026 if (at_function_scope_p ())
10028 /* [dcl.meaning]
10030 A declarator-id shall not be qualified except
10031 for ...
10033 None of the cases are permitted in block
10034 scope. */
10035 if (qualifying_scope == global_namespace)
10036 error ("invalid use of qualified-name %<::%D%>",
10037 decl);
10038 else if (TYPE_P (qualifying_scope))
10039 error ("invalid use of qualified-name %<%T::%D%>",
10040 qualifying_scope, decl);
10041 else
10042 error ("invalid use of qualified-name %<%D::%D%>",
10043 qualifying_scope, decl);
10044 return error_mark_node;
10046 else if (TYPE_P (qualifying_scope))
10048 ctype = qualifying_scope;
10049 if (!MAYBE_CLASS_TYPE_P (ctype))
10051 error ("%q#T is not a class or a namespace", ctype);
10052 ctype = NULL_TREE;
10054 else if (innermost_code != cdk_function
10055 && current_class_type
10056 && !uniquely_derived_from_p (ctype,
10057 current_class_type))
10059 error ("invalid use of qualified-name %<%T::%D%>",
10060 qualifying_scope, decl);
10061 return error_mark_node;
10064 else if (TREE_CODE (qualifying_scope) == NAMESPACE_DECL)
10065 in_namespace = qualifying_scope;
10067 switch (TREE_CODE (decl))
10069 case BIT_NOT_EXPR:
10071 tree type;
10073 if (innermost_code != cdk_function)
10075 error ("declaration of %qD as non-function", decl);
10076 return error_mark_node;
10078 else if (!qualifying_scope
10079 && !(current_class_type && at_class_scope_p ()))
10081 error ("declaration of %qD as non-member", decl);
10082 return error_mark_node;
10085 type = TREE_OPERAND (decl, 0);
10086 if (TYPE_P (type))
10087 type = constructor_name (type);
10088 name = identifier_to_locale (IDENTIFIER_POINTER (type));
10089 dname = decl;
10091 break;
10093 case TEMPLATE_ID_EXPR:
10095 tree fns = TREE_OPERAND (decl, 0);
10097 dname = fns;
10098 if (!identifier_p (dname))
10099 dname = OVL_NAME (dname);
10101 /* Fall through. */
10103 case IDENTIFIER_NODE:
10104 if (identifier_p (decl))
10105 dname = decl;
10107 if (IDENTIFIER_KEYWORD_P (dname))
10109 error ("declarator-id missing; using reserved word %qD",
10110 dname);
10111 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
10113 else if (!IDENTIFIER_CONV_OP_P (dname))
10114 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
10115 else
10117 gcc_assert (flags == NO_SPECIAL);
10118 flags = TYPENAME_FLAG;
10119 sfk = sfk_conversion;
10120 tree glob = IDENTIFIER_GLOBAL_VALUE (dname);
10121 if (glob && TREE_CODE (glob) == TYPE_DECL)
10122 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
10123 else
10124 name = "<invalid operator>";
10126 break;
10128 default:
10129 gcc_unreachable ();
10131 break;
10134 case cdk_array:
10135 case cdk_pointer:
10136 case cdk_reference:
10137 case cdk_ptrmem:
10138 break;
10140 case cdk_decomp:
10141 name = "decomposition";
10142 break;
10144 case cdk_error:
10145 return error_mark_node;
10147 default:
10148 gcc_unreachable ();
10150 if (id_declarator->kind == cdk_id)
10151 break;
10154 /* [dcl.fct.edf]
10156 The declarator in a function-definition shall have the form
10157 D1 ( parameter-declaration-clause) ... */
10158 if (funcdef_flag && innermost_code != cdk_function)
10160 error ("function definition does not declare parameters");
10161 return error_mark_node;
10164 if (flags == TYPENAME_FLAG
10165 && innermost_code != cdk_function
10166 && ! (ctype && !declspecs->any_specifiers_p))
10168 error ("declaration of %qD as non-function", dname);
10169 return error_mark_node;
10172 if (dname && identifier_p (dname))
10174 if (UDLIT_OPER_P (dname)
10175 && innermost_code != cdk_function)
10177 error ("declaration of %qD as non-function", dname);
10178 return error_mark_node;
10181 if (IDENTIFIER_ANY_OP_P (dname))
10183 if (typedef_p)
10185 error ("declaration of %qD as %<typedef%>", dname);
10186 return error_mark_node;
10188 else if (decl_context == PARM || decl_context == CATCHPARM)
10190 error ("declaration of %qD as parameter", dname);
10191 return error_mark_node;
10196 /* Anything declared one level down from the top level
10197 must be one of the parameters of a function
10198 (because the body is at least two levels down). */
10200 /* This heuristic cannot be applied to C++ nodes! Fixed, however,
10201 by not allowing C++ class definitions to specify their parameters
10202 with xdecls (must be spec.d in the parmlist).
10204 Since we now wait to push a class scope until we are sure that
10205 we are in a legitimate method context, we must set oldcname
10206 explicitly (since current_class_name is not yet alive).
10208 We also want to avoid calling this a PARM if it is in a namespace. */
10210 if (decl_context == NORMAL && !toplevel_bindings_p ())
10212 cp_binding_level *b = current_binding_level;
10213 current_binding_level = b->level_chain;
10214 if (current_binding_level != 0 && toplevel_bindings_p ())
10215 decl_context = PARM;
10216 current_binding_level = b;
10219 if (name == NULL)
10220 name = decl_context == PARM ? "parameter" : "type name";
10222 if (concept_p && typedef_p)
10224 error ("%<concept%> cannot appear in a typedef declaration");
10225 return error_mark_node;
10228 if (constexpr_p && typedef_p)
10230 error ("%<constexpr%> cannot appear in a typedef declaration");
10231 return error_mark_node;
10234 /* If there were multiple types specified in the decl-specifier-seq,
10235 issue an error message. */
10236 if (declspecs->multiple_types_p)
10238 error ("two or more data types in declaration of %qs", name);
10239 return error_mark_node;
10242 if (declspecs->conflicting_specifiers_p)
10244 error ("conflicting specifiers in declaration of %qs", name);
10245 return error_mark_node;
10248 /* Extract the basic type from the decl-specifier-seq. */
10249 type = declspecs->type;
10250 if (type == error_mark_node)
10252 type = NULL_TREE;
10253 type_was_error_mark_node = true;
10255 /* If the entire declaration is itself tagged as deprecated then
10256 suppress reports of deprecated items. */
10257 if (type && TREE_DEPRECATED (type)
10258 && deprecated_state != DEPRECATED_SUPPRESS)
10259 warn_deprecated_use (type, NULL_TREE);
10260 if (type && TREE_CODE (type) == TYPE_DECL)
10262 typedef_decl = type;
10263 type = TREE_TYPE (typedef_decl);
10264 if (TREE_DEPRECATED (type)
10265 && DECL_ARTIFICIAL (typedef_decl)
10266 && deprecated_state != DEPRECATED_SUPPRESS)
10267 warn_deprecated_use (type, NULL_TREE);
10269 /* No type at all: default to `int', and set DEFAULTED_INT
10270 because it was not a user-defined typedef. */
10271 if (type == NULL_TREE)
10273 if (signed_p || unsigned_p || long_p || short_p)
10275 /* These imply 'int'. */
10276 type = integer_type_node;
10277 defaulted_int = 1;
10279 /* If we just have "complex", it is equivalent to "complex double". */
10280 else if (!longlong && !explicit_intN
10281 && decl_spec_seq_has_spec_p (declspecs, ds_complex))
10283 type = double_type_node;
10284 pedwarn (declspecs->locations[ds_complex], OPT_Wpedantic,
10285 "ISO C++ does not support plain %<complex%> meaning "
10286 "%<double complex%>");
10289 /* Gather flags. */
10290 explicit_int = declspecs->explicit_int_p;
10291 explicit_char = declspecs->explicit_char_p;
10293 #if 0
10294 /* See the code below that used this. */
10295 if (typedef_decl)
10296 decl_attr = DECL_ATTRIBUTES (typedef_decl);
10297 #endif
10298 typedef_type = type;
10300 if (sfk == sfk_conversion || sfk == sfk_deduction_guide)
10301 ctor_return_type = TREE_TYPE (dname);
10302 else
10303 ctor_return_type = ctype;
10305 if (sfk != sfk_none)
10307 type = check_special_function_return_type (sfk, type,
10308 ctor_return_type,
10309 type_quals,
10310 declspecs->locations);
10311 type_quals = TYPE_UNQUALIFIED;
10313 else if (type == NULL_TREE)
10315 int is_main;
10317 explicit_int = -1;
10319 /* We handle `main' specially here, because 'main () { }' is so
10320 common. With no options, it is allowed. With -Wreturn-type,
10321 it is a warning. It is only an error with -pedantic-errors. */
10322 is_main = (funcdef_flag
10323 && dname && identifier_p (dname)
10324 && MAIN_NAME_P (dname)
10325 && ctype == NULL_TREE
10326 && in_namespace == NULL_TREE
10327 && current_namespace == global_namespace);
10329 if (type_was_error_mark_node)
10330 /* We've already issued an error, don't complain more. */;
10331 else if (in_system_header_at (input_location) || flag_ms_extensions)
10332 /* Allow it, sigh. */;
10333 else if (! is_main)
10334 permerror (input_location, "ISO C++ forbids declaration of %qs with no type", name);
10335 else if (pedantic)
10336 pedwarn (input_location, OPT_Wpedantic,
10337 "ISO C++ forbids declaration of %qs with no type", name);
10338 else
10339 warning (OPT_Wreturn_type,
10340 "ISO C++ forbids declaration of %qs with no type", name);
10342 if (type_was_error_mark_node && template_parm_flag)
10343 /* FIXME we should be able to propagate the error_mark_node as is
10344 for other contexts too. */
10345 type = error_mark_node;
10346 else
10347 type = integer_type_node;
10350 ctype = NULL_TREE;
10352 if (explicit_intN)
10354 if (! int_n_enabled_p[declspecs->int_n_idx])
10356 error ("%<__int%d%> is not supported by this target",
10357 int_n_data[declspecs->int_n_idx].bitsize);
10358 explicit_intN = false;
10360 else if (pedantic && ! in_system_header_at (input_location))
10361 pedwarn (input_location, OPT_Wpedantic,
10362 "ISO C++ does not support %<__int%d%> for %qs",
10363 int_n_data[declspecs->int_n_idx].bitsize, name);
10366 /* Now process the modifiers that were specified
10367 and check for invalid combinations. */
10369 /* Long double is a special combination. */
10370 if (long_p && !longlong && TYPE_MAIN_VARIANT (type) == double_type_node)
10372 long_p = false;
10373 type = cp_build_qualified_type (long_double_type_node,
10374 cp_type_quals (type));
10377 /* Check all other uses of type modifiers. */
10379 if (unsigned_p || signed_p || long_p || short_p)
10381 int ok = 0;
10383 if ((signed_p || unsigned_p) && TREE_CODE (type) != INTEGER_TYPE)
10384 error ("%<signed%> or %<unsigned%> invalid for %qs", name);
10385 else if (signed_p && unsigned_p)
10386 error ("%<signed%> and %<unsigned%> specified together for %qs", name);
10387 else if (longlong && TREE_CODE (type) != INTEGER_TYPE)
10388 error ("%<long long%> invalid for %qs", name);
10389 else if (long_p && TREE_CODE (type) == REAL_TYPE)
10390 error ("%<long%> invalid for %qs", name);
10391 else if (short_p && TREE_CODE (type) == REAL_TYPE)
10392 error ("%<short%> invalid for %qs", name);
10393 else if ((long_p || short_p) && TREE_CODE (type) != INTEGER_TYPE)
10394 error ("%<long%> or %<short%> invalid for %qs", name);
10395 else if ((long_p || short_p || explicit_char || explicit_int) && explicit_intN)
10396 error ("%<long%>, %<int%>, %<short%>, or %<char%> invalid for %qs", name);
10397 else if ((long_p || short_p) && explicit_char)
10398 error ("%<long%> or %<short%> specified with char for %qs", name);
10399 else if (long_p && short_p)
10400 error ("%<long%> and %<short%> specified together for %qs", name);
10401 else if (type == char16_type_node || type == char32_type_node)
10403 if (signed_p || unsigned_p)
10404 error ("%<signed%> or %<unsigned%> invalid for %qs", name);
10405 else if (short_p || long_p)
10406 error ("%<short%> or %<long%> invalid for %qs", name);
10408 else
10410 ok = 1;
10411 if (!explicit_int && !defaulted_int && !explicit_char && !explicit_intN && pedantic)
10413 pedwarn (input_location, OPT_Wpedantic,
10414 "long, short, signed or unsigned used invalidly for %qs",
10415 name);
10416 if (flag_pedantic_errors)
10417 ok = 0;
10421 /* Discard the type modifiers if they are invalid. */
10422 if (! ok)
10424 unsigned_p = false;
10425 signed_p = false;
10426 long_p = false;
10427 short_p = false;
10428 longlong = 0;
10432 /* Decide whether an integer type is signed or not.
10433 Optionally treat bitfields as signed by default. */
10434 if (unsigned_p
10435 /* [class.bit]
10437 It is implementation-defined whether a plain (neither
10438 explicitly signed or unsigned) char, short, int, or long
10439 bit-field is signed or unsigned.
10441 Naturally, we extend this to long long as well. Note that
10442 this does not include wchar_t. */
10443 || (bitfield && !flag_signed_bitfields
10444 && !signed_p
10445 /* A typedef for plain `int' without `signed' can be
10446 controlled just like plain `int', but a typedef for
10447 `signed int' cannot be so controlled. */
10448 && !(typedef_decl
10449 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl))
10450 && TREE_CODE (type) == INTEGER_TYPE
10451 && !same_type_p (TYPE_MAIN_VARIANT (type), wchar_type_node)))
10453 if (explicit_intN)
10454 type = int_n_trees[declspecs->int_n_idx].unsigned_type;
10455 else if (longlong)
10456 type = long_long_unsigned_type_node;
10457 else if (long_p)
10458 type = long_unsigned_type_node;
10459 else if (short_p)
10460 type = short_unsigned_type_node;
10461 else if (type == char_type_node)
10462 type = unsigned_char_type_node;
10463 else if (typedef_decl)
10464 type = unsigned_type_for (type);
10465 else
10466 type = unsigned_type_node;
10468 else if (signed_p && type == char_type_node)
10469 type = signed_char_type_node;
10470 else if (explicit_intN)
10471 type = int_n_trees[declspecs->int_n_idx].signed_type;
10472 else if (longlong)
10473 type = long_long_integer_type_node;
10474 else if (long_p)
10475 type = long_integer_type_node;
10476 else if (short_p)
10477 type = short_integer_type_node;
10479 if (decl_spec_seq_has_spec_p (declspecs, ds_complex))
10481 if (TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
10482 error ("complex invalid for %qs", name);
10483 /* If a modifier is specified, the resulting complex is the complex
10484 form of TYPE. E.g, "complex short" is "complex short int". */
10485 else if (type == integer_type_node)
10486 type = complex_integer_type_node;
10487 else if (type == float_type_node)
10488 type = complex_float_type_node;
10489 else if (type == double_type_node)
10490 type = complex_double_type_node;
10491 else if (type == long_double_type_node)
10492 type = complex_long_double_type_node;
10493 else
10494 type = build_complex_type (type);
10497 /* If we're using the injected-class-name to form a compound type or a
10498 declaration, replace it with the underlying class so we don't get
10499 redundant typedefs in the debug output. But if we are returning the
10500 type unchanged, leave it alone so that it's available to
10501 maybe_get_template_decl_from_type_decl. */
10502 if (CLASS_TYPE_P (type)
10503 && DECL_SELF_REFERENCE_P (TYPE_NAME (type))
10504 && type == TREE_TYPE (TYPE_NAME (type))
10505 && (declarator || type_quals))
10506 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
10508 type_quals |= cp_type_quals (type);
10509 type = cp_build_qualified_type_real
10510 (type, type_quals, ((((typedef_decl && !DECL_ARTIFICIAL (typedef_decl))
10511 || declspecs->decltype_p)
10512 ? tf_ignore_bad_quals : 0) | tf_warning_or_error));
10513 /* We might have ignored or rejected some of the qualifiers. */
10514 type_quals = cp_type_quals (type);
10516 if (cxx_dialect >= cxx1z && type && is_auto (type)
10517 && innermost_code != cdk_function
10518 && id_declarator && declarator != id_declarator)
10519 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (type))
10521 error_at (typespec_loc, "template placeholder type %qT must be followed "
10522 "by a simple declarator-id", type);
10523 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here", tmpl);
10526 staticp = 0;
10527 inlinep = decl_spec_seq_has_spec_p (declspecs, ds_inline);
10528 virtualp = decl_spec_seq_has_spec_p (declspecs, ds_virtual);
10529 explicitp = decl_spec_seq_has_spec_p (declspecs, ds_explicit);
10531 storage_class = declspecs->storage_class;
10532 if (storage_class == sc_static)
10533 staticp = 1 + (decl_context == FIELD);
10535 if (virtualp)
10537 if (staticp == 2)
10539 error ("member %qD cannot be declared both %<virtual%> "
10540 "and %<static%>", dname);
10541 storage_class = sc_none;
10542 staticp = 0;
10544 if (constexpr_p)
10545 error ("member %qD cannot be declared both %<virtual%> "
10546 "and %<constexpr%>", dname);
10548 friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
10550 /* Issue errors about use of storage classes for parameters. */
10551 if (decl_context == PARM)
10553 if (typedef_p)
10555 error ("typedef declaration invalid in parameter declaration");
10556 return error_mark_node;
10558 else if (template_parm_flag && storage_class != sc_none)
10560 error ("storage class specified for template parameter %qs", name);
10561 return error_mark_node;
10563 else if (storage_class == sc_static
10564 || storage_class == sc_extern
10565 || thread_p)
10566 error ("storage class specifiers invalid in parameter declarations");
10568 /* Function parameters cannot be concept. */
10569 if (concept_p)
10570 error ("a parameter cannot be declared %<concept%>");
10571 /* Function parameters cannot be constexpr. If we saw one, moan
10572 and pretend it wasn't there. */
10573 else if (constexpr_p)
10575 error ("a parameter cannot be declared %<constexpr%>");
10576 constexpr_p = 0;
10580 /* Give error if `virtual' is used outside of class declaration. */
10581 if (virtualp
10582 && (current_class_name == NULL_TREE || decl_context != FIELD))
10584 error_at (declspecs->locations[ds_virtual],
10585 "%<virtual%> outside class declaration");
10586 virtualp = 0;
10589 if (innermost_code == cdk_decomp)
10591 location_t loc = (declarator->kind == cdk_reference
10592 ? declarator->declarator->id_loc : declarator->id_loc);
10593 if (inlinep)
10594 error_at (declspecs->locations[ds_inline],
10595 "decomposition declaration cannot be declared %<inline%>");
10596 if (typedef_p)
10597 error_at (declspecs->locations[ds_typedef],
10598 "decomposition declaration cannot be declared %<typedef%>");
10599 if (constexpr_p)
10600 error_at (declspecs->locations[ds_constexpr], "decomposition "
10601 "declaration cannot be declared %<constexpr%>");
10602 if (thread_p)
10603 error_at (declspecs->locations[ds_thread],
10604 "decomposition declaration cannot be declared %qs",
10605 declspecs->gnu_thread_keyword_p
10606 ? "__thread" : "thread_local");
10607 if (concept_p)
10608 error_at (declspecs->locations[ds_concept],
10609 "decomposition declaration cannot be declared %<concept%>");
10610 switch (storage_class)
10612 case sc_none:
10613 break;
10614 case sc_register:
10615 error_at (loc, "decomposition declaration cannot be declared "
10616 "%<register%>");
10617 break;
10618 case sc_static:
10619 error_at (loc, "decomposition declaration cannot be declared "
10620 "%<static%>");
10621 break;
10622 case sc_extern:
10623 error_at (loc, "decomposition declaration cannot be declared "
10624 "%<extern%>");
10625 break;
10626 case sc_mutable:
10627 error_at (loc, "decomposition declaration cannot be declared "
10628 "%<mutable%>");
10629 break;
10630 case sc_auto:
10631 error_at (loc, "decomposition declaration cannot be declared "
10632 "C++98 %<auto%>");
10633 break;
10634 default:
10635 gcc_unreachable ();
10637 if (TREE_CODE (type) != TEMPLATE_TYPE_PARM
10638 || TYPE_IDENTIFIER (type) != auto_identifier)
10640 if (type != error_mark_node)
10642 error_at (loc, "decomposition declaration cannot be declared "
10643 "with type %qT", type);
10644 inform (loc,
10645 "type must be cv-qualified %<auto%> or reference to "
10646 "cv-qualified %<auto%>");
10648 type = build_qualified_type (make_auto (), type_quals);
10649 declspecs->type = type;
10651 inlinep = 0;
10652 typedef_p = 0;
10653 constexpr_p = 0;
10654 thread_p = 0;
10655 concept_p = 0;
10656 storage_class = sc_none;
10657 staticp = 0;
10658 declspecs->storage_class = sc_none;
10659 declspecs->locations[ds_thread] = UNKNOWN_LOCATION;
10662 /* Static anonymous unions are dealt with here. */
10663 if (staticp && decl_context == TYPENAME
10664 && declspecs->type
10665 && ANON_AGGR_TYPE_P (declspecs->type))
10666 decl_context = FIELD;
10668 /* Warn about storage classes that are invalid for certain
10669 kinds of declarations (parameters, typenames, etc.). */
10670 if (thread_p
10671 && ((storage_class
10672 && storage_class != sc_extern
10673 && storage_class != sc_static)
10674 || typedef_p))
10676 error ("multiple storage classes in declaration of %qs", name);
10677 thread_p = false;
10679 if (decl_context != NORMAL
10680 && ((storage_class != sc_none
10681 && storage_class != sc_mutable)
10682 || thread_p))
10684 if ((decl_context == PARM || decl_context == CATCHPARM)
10685 && (storage_class == sc_register
10686 || storage_class == sc_auto))
10688 else if (typedef_p)
10690 else if (decl_context == FIELD
10691 /* C++ allows static class elements. */
10692 && storage_class == sc_static)
10693 /* C++ also allows inlines and signed and unsigned elements,
10694 but in those cases we don't come in here. */
10696 else
10698 if (decl_context == FIELD)
10699 error ("storage class specified for %qs", name);
10700 else
10702 if (decl_context == PARM || decl_context == CATCHPARM)
10703 error ("storage class specified for parameter %qs", name);
10704 else
10705 error ("storage class specified for typename");
10707 if (storage_class == sc_register
10708 || storage_class == sc_auto
10709 || storage_class == sc_extern
10710 || thread_p)
10711 storage_class = sc_none;
10714 else if (storage_class == sc_extern && funcdef_flag
10715 && ! toplevel_bindings_p ())
10716 error ("nested function %qs declared %<extern%>", name);
10717 else if (toplevel_bindings_p ())
10719 if (storage_class == sc_auto)
10720 error ("top-level declaration of %qs specifies %<auto%>", name);
10722 else if (thread_p
10723 && storage_class != sc_extern
10724 && storage_class != sc_static)
10726 if (declspecs->gnu_thread_keyword_p)
10727 pedwarn (input_location, 0, "function-scope %qs implicitly auto and "
10728 "declared %<__thread%>", name);
10730 /* When thread_local is applied to a variable of block scope the
10731 storage-class-specifier static is implied if it does not appear
10732 explicitly. */
10733 storage_class = declspecs->storage_class = sc_static;
10734 staticp = 1;
10737 if (storage_class && friendp)
10739 error ("storage class specifiers invalid in friend function declarations");
10740 storage_class = sc_none;
10741 staticp = 0;
10744 if (!id_declarator)
10745 unqualified_id = NULL_TREE;
10746 else
10748 unqualified_id = id_declarator->u.id.unqualified_name;
10749 switch (TREE_CODE (unqualified_id))
10751 case BIT_NOT_EXPR:
10752 unqualified_id = TREE_OPERAND (unqualified_id, 0);
10753 if (TYPE_P (unqualified_id))
10754 unqualified_id = constructor_name (unqualified_id);
10755 break;
10757 case IDENTIFIER_NODE:
10758 case TEMPLATE_ID_EXPR:
10759 break;
10761 default:
10762 gcc_unreachable ();
10766 if (declspecs->std_attributes)
10768 /* Apply the c++11 attributes to the type preceding them. */
10769 input_location = declspecs->locations[ds_std_attribute];
10770 decl_attributes (&type, declspecs->std_attributes, 0);
10771 input_location = saved_loc;
10774 /* Determine the type of the entity declared by recurring on the
10775 declarator. */
10776 for (; declarator; declarator = declarator->declarator)
10778 const cp_declarator *inner_declarator;
10779 tree attrs;
10781 if (type == error_mark_node)
10782 return error_mark_node;
10784 attrs = declarator->attributes;
10785 if (attrs)
10787 int attr_flags;
10789 attr_flags = 0;
10790 if (declarator == NULL || declarator->kind == cdk_id)
10791 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
10792 if (declarator->kind == cdk_function)
10793 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
10794 if (declarator->kind == cdk_array)
10795 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
10796 returned_attrs = decl_attributes (&type,
10797 chainon (returned_attrs, attrs),
10798 attr_flags);
10801 if (declarator->kind == cdk_id || declarator->kind == cdk_decomp)
10802 break;
10804 inner_declarator = declarator->declarator;
10806 switch (declarator->kind)
10808 case cdk_array:
10809 type = create_array_type_for_decl (dname, type,
10810 declarator->u.array.bounds);
10811 if (!valid_array_size_p (input_location, type, dname))
10812 type = error_mark_node;
10814 if (declarator->std_attributes)
10815 /* [dcl.array]/1:
10817 The optional attribute-specifier-seq appertains to the
10818 array. */
10819 returned_attrs = chainon (returned_attrs,
10820 declarator->std_attributes);
10821 break;
10823 case cdk_function:
10825 tree arg_types;
10826 int funcdecl_p;
10828 /* Declaring a function type.
10829 Make sure we have a valid type for the function to return. */
10831 if (type_quals != TYPE_UNQUALIFIED)
10833 if (SCALAR_TYPE_P (type) || VOID_TYPE_P (type))
10835 warning_at (typespec_loc, OPT_Wignored_qualifiers, "type "
10836 "qualifiers ignored on function return type");
10838 /* We now know that the TYPE_QUALS don't apply to the
10839 decl, but to its return type. */
10840 type_quals = TYPE_UNQUALIFIED;
10843 /* Error about some types functions can't return. */
10845 if (TREE_CODE (type) == FUNCTION_TYPE)
10847 error ("%qs declared as function returning a function", name);
10848 return error_mark_node;
10850 if (TREE_CODE (type) == ARRAY_TYPE)
10852 error ("%qs declared as function returning an array", name);
10853 return error_mark_node;
10856 input_location = declspecs->locations[ds_type_spec];
10857 abstract_virtuals_error (ACU_RETURN, type);
10858 input_location = saved_loc;
10860 /* Pick up type qualifiers which should be applied to `this'. */
10861 memfn_quals = declarator->u.function.qualifiers;
10862 /* Pick up virt-specifiers. */
10863 virt_specifiers = declarator->u.function.virt_specifiers;
10864 /* And ref-qualifier, too */
10865 rqual = declarator->u.function.ref_qualifier;
10866 /* And tx-qualifier. */
10867 tree tx_qual = declarator->u.function.tx_qualifier;
10868 /* Pick up the exception specifications. */
10869 raises = declarator->u.function.exception_specification;
10870 /* If the exception-specification is ill-formed, let's pretend
10871 there wasn't one. */
10872 if (raises == error_mark_node)
10873 raises = NULL_TREE;
10875 if (reqs)
10876 error_at (location_of (reqs), "requires-clause on return type");
10877 reqs = declarator->u.function.requires_clause;
10879 /* Say it's a definition only for the CALL_EXPR
10880 closest to the identifier. */
10881 funcdecl_p = inner_declarator && inner_declarator->kind == cdk_id;
10883 /* Handle a late-specified return type. */
10884 tree late_return_type = declarator->u.function.late_return_type;
10885 if (funcdecl_p)
10887 if (tree auto_node = type_uses_auto (type))
10889 if (!late_return_type)
10891 if (current_class_type
10892 && LAMBDA_TYPE_P (current_class_type))
10893 /* OK for C++11 lambdas. */;
10894 else if (cxx_dialect < cxx14)
10896 error ("%qs function uses "
10897 "%<auto%> type specifier without trailing "
10898 "return type", name);
10899 inform (input_location, "deduced return type "
10900 "only available with -std=c++14 or "
10901 "-std=gnu++14");
10903 else if (virtualp)
10905 error ("virtual function cannot "
10906 "have deduced return type");
10907 virtualp = false;
10910 else if (!is_auto (type) && sfk != sfk_conversion)
10912 error ("%qs function with trailing return type has"
10913 " %qT as its type rather than plain %<auto%>",
10914 name, type);
10915 return error_mark_node;
10917 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
10919 if (!late_return_type)
10921 if (dguide_name_p (unqualified_id))
10922 error_at (declarator->id_loc, "deduction guide "
10923 "for %qT must have trailing return "
10924 "type", TREE_TYPE (tmpl));
10925 else
10926 error_at (declarator->id_loc, "deduced class "
10927 "type %qT in function return type",
10928 type);
10929 inform (DECL_SOURCE_LOCATION (tmpl),
10930 "%qD declared here", tmpl);
10932 else if (CLASS_TYPE_P (late_return_type)
10933 && CLASSTYPE_TEMPLATE_INFO (late_return_type)
10934 && (CLASSTYPE_TI_TEMPLATE (late_return_type)
10935 == tmpl))
10936 /* OK */;
10937 else
10938 error ("trailing return type %qT of deduction guide "
10939 "is not a specialization of %qT",
10940 late_return_type, TREE_TYPE (tmpl));
10943 else if (late_return_type
10944 && sfk != sfk_conversion)
10946 if (cxx_dialect < cxx11)
10947 /* Not using maybe_warn_cpp0x because this should
10948 always be an error. */
10949 error ("trailing return type only available with "
10950 "-std=c++11 or -std=gnu++11");
10951 else
10952 error ("%qs function with trailing return type not "
10953 "declared with %<auto%> type specifier", name);
10954 return error_mark_node;
10957 type = splice_late_return_type (type, late_return_type);
10958 if (type == error_mark_node)
10959 return error_mark_node;
10961 if (late_return_type)
10962 late_return_type_p = true;
10964 if (ctype == NULL_TREE
10965 && decl_context == FIELD
10966 && funcdecl_p
10967 && friendp == 0)
10968 ctype = current_class_type;
10970 if (ctype && (sfk == sfk_constructor
10971 || sfk == sfk_destructor))
10973 /* We are within a class's scope. If our declarator name
10974 is the same as the class name, and we are defining
10975 a function, then it is a constructor/destructor, and
10976 therefore returns a void type. */
10978 /* ISO C++ 12.4/2. A destructor may not be declared
10979 const or volatile. A destructor may not be static.
10980 A destructor may not be declared with ref-qualifier.
10982 ISO C++ 12.1. A constructor may not be declared
10983 const or volatile. A constructor may not be
10984 virtual. A constructor may not be static.
10985 A constructor may not be declared with ref-qualifier. */
10986 if (staticp == 2)
10987 error ((flags == DTOR_FLAG)
10988 ? G_("destructor cannot be static member function")
10989 : G_("constructor cannot be static member function"));
10990 if (memfn_quals)
10992 error ((flags == DTOR_FLAG)
10993 ? G_("destructors may not be cv-qualified")
10994 : G_("constructors may not be cv-qualified"));
10995 memfn_quals = TYPE_UNQUALIFIED;
10998 if (rqual)
11000 maybe_warn_cpp0x (CPP0X_REF_QUALIFIER);
11001 error ((flags == DTOR_FLAG)
11002 ? G_("destructors may not be ref-qualified")
11003 : G_("constructors may not be ref-qualified"));
11004 rqual = REF_QUAL_NONE;
11007 if (decl_context == FIELD
11008 && !member_function_or_else (ctype,
11009 current_class_type,
11010 flags))
11011 return error_mark_node;
11013 if (flags != DTOR_FLAG)
11015 /* It's a constructor. */
11016 if (explicitp == 1)
11017 explicitp = 2;
11018 if (virtualp)
11020 permerror (input_location,
11021 "constructors cannot be declared %<virtual%>");
11022 virtualp = 0;
11024 if (decl_context == FIELD
11025 && sfk != sfk_constructor)
11026 return error_mark_node;
11028 if (decl_context == FIELD)
11029 staticp = 0;
11031 else if (friendp)
11033 if (virtualp)
11035 /* Cannot be both friend and virtual. */
11036 error ("virtual functions cannot be friends");
11037 friendp = 0;
11039 if (decl_context == NORMAL)
11040 error ("friend declaration not in class definition");
11041 if (current_function_decl && funcdef_flag)
11042 error ("can%'t define friend function %qs in a local "
11043 "class definition",
11044 name);
11046 else if (ctype && sfk == sfk_conversion)
11048 if (explicitp == 1)
11050 maybe_warn_cpp0x (CPP0X_EXPLICIT_CONVERSION);
11051 explicitp = 2;
11053 if (late_return_type_p)
11054 error ("a conversion function cannot have a trailing return type");
11056 else if (sfk == sfk_deduction_guide)
11058 if (explicitp == 1)
11059 explicitp = 2;
11062 arg_types = grokparms (declarator->u.function.parameters,
11063 &parms);
11065 if (inner_declarator
11066 && inner_declarator->kind == cdk_id
11067 && inner_declarator->u.id.sfk == sfk_destructor
11068 && arg_types != void_list_node)
11070 error ("destructors may not have parameters");
11071 arg_types = void_list_node;
11072 parms = NULL_TREE;
11075 type = build_function_type (type, arg_types);
11077 tree attrs = declarator->std_attributes;
11078 if (tx_qual)
11080 tree att = build_tree_list (tx_qual, NULL_TREE);
11081 /* transaction_safe applies to the type, but
11082 transaction_safe_dynamic applies to the function. */
11083 if (is_attribute_p ("transaction_safe", tx_qual))
11084 attrs = chainon (attrs, att);
11085 else
11086 returned_attrs = chainon (returned_attrs, att);
11088 if (attrs)
11089 /* [dcl.fct]/2:
11091 The optional attribute-specifier-seq appertains to
11092 the function type. */
11093 decl_attributes (&type, attrs, 0);
11095 if (raises)
11096 type = build_exception_variant (type, raises);
11098 break;
11100 case cdk_pointer:
11101 case cdk_reference:
11102 case cdk_ptrmem:
11103 /* Filter out pointers-to-references and references-to-references.
11104 We can get these if a TYPE_DECL is used. */
11106 if (TREE_CODE (type) == REFERENCE_TYPE)
11108 if (declarator->kind != cdk_reference)
11110 error ("cannot declare pointer to %q#T", type);
11111 type = TREE_TYPE (type);
11114 /* In C++0x, we allow reference to reference declarations
11115 that occur indirectly through typedefs [7.1.3/8 dcl.typedef]
11116 and template type arguments [14.3.1/4 temp.arg.type]. The
11117 check for direct reference to reference declarations, which
11118 are still forbidden, occurs below. Reasoning behind the change
11119 can be found in DR106, DR540, and the rvalue reference
11120 proposals. */
11121 else if (cxx_dialect == cxx98)
11123 error ("cannot declare reference to %q#T", type);
11124 type = TREE_TYPE (type);
11127 else if (VOID_TYPE_P (type))
11129 if (declarator->kind == cdk_reference)
11130 error ("cannot declare reference to %q#T", type);
11131 else if (declarator->kind == cdk_ptrmem)
11132 error ("cannot declare pointer to %q#T member", type);
11135 /* We now know that the TYPE_QUALS don't apply to the decl,
11136 but to the target of the pointer. */
11137 type_quals = TYPE_UNQUALIFIED;
11139 /* This code used to handle METHOD_TYPE, but I don't think it's
11140 possible to get it here anymore. */
11141 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
11142 if (declarator->kind == cdk_ptrmem
11143 && TREE_CODE (type) == FUNCTION_TYPE)
11145 memfn_quals |= type_memfn_quals (type);
11146 type = build_memfn_type (type,
11147 declarator->u.pointer.class_type,
11148 memfn_quals,
11149 rqual);
11150 if (type == error_mark_node)
11151 return error_mark_node;
11153 rqual = REF_QUAL_NONE;
11154 memfn_quals = TYPE_UNQUALIFIED;
11157 if (TREE_CODE (type) == FUNCTION_TYPE
11158 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
11159 || type_memfn_rqual (type) != REF_QUAL_NONE))
11160 error (declarator->kind == cdk_reference
11161 ? G_("cannot declare reference to qualified function type %qT")
11162 : G_("cannot declare pointer to qualified function type %qT"),
11163 type);
11165 /* When the pointed-to type involves components of variable size,
11166 care must be taken to ensure that the size evaluation code is
11167 emitted early enough to dominate all the possible later uses
11168 and late enough for the variables on which it depends to have
11169 been assigned.
11171 This is expected to happen automatically when the pointed-to
11172 type has a name/declaration of it's own, but special attention
11173 is required if the type is anonymous.
11175 We handle the NORMAL and FIELD contexts here by inserting a
11176 dummy statement that just evaluates the size at a safe point
11177 and ensures it is not deferred until e.g. within a deeper
11178 conditional context (c++/43555).
11180 We expect nothing to be needed here for PARM or TYPENAME.
11181 Evaluating the size at this point for TYPENAME would
11182 actually be incorrect, as we might be in the middle of an
11183 expression with side effects on the pointed-to type size
11184 "arguments" prior to the pointer declaration point and the
11185 size evaluation could end up prior to the side effects. */
11187 if (!TYPE_NAME (type)
11188 && (decl_context == NORMAL || decl_context == FIELD)
11189 && at_function_scope_p ()
11190 && variably_modified_type_p (type, NULL_TREE))
11192 TYPE_NAME (type) = build_decl (UNKNOWN_LOCATION, TYPE_DECL,
11193 NULL_TREE, type);
11194 add_decl_expr (TYPE_NAME (type));
11197 if (declarator->kind == cdk_reference)
11199 /* In C++0x, the type we are creating a reference to might be
11200 a typedef which is itself a reference type. In that case,
11201 we follow the reference collapsing rules in
11202 [7.1.3/8 dcl.typedef] to create the final reference type:
11204 "If a typedef TD names a type that is a reference to a type
11205 T, an attempt to create the type 'lvalue reference to cv TD'
11206 creates the type 'lvalue reference to T,' while an attempt
11207 to create the type "rvalue reference to cv TD' creates the
11208 type TD."
11210 if (VOID_TYPE_P (type))
11211 /* We already gave an error. */;
11212 else if (TREE_CODE (type) == REFERENCE_TYPE)
11214 if (declarator->u.reference.rvalue_ref)
11215 /* Leave type alone. */;
11216 else
11217 type = cp_build_reference_type (TREE_TYPE (type), false);
11219 else
11220 type = cp_build_reference_type
11221 (type, declarator->u.reference.rvalue_ref);
11223 /* In C++0x, we need this check for direct reference to
11224 reference declarations, which are forbidden by
11225 [8.3.2/5 dcl.ref]. Reference to reference declarations
11226 are only allowed indirectly through typedefs and template
11227 type arguments. Example:
11229 void foo(int & &); // invalid ref-to-ref decl
11231 typedef int & int_ref;
11232 void foo(int_ref &); // valid ref-to-ref decl
11234 if (inner_declarator && inner_declarator->kind == cdk_reference)
11235 error ("cannot declare reference to %q#T, which is not "
11236 "a typedef or a template type argument", type);
11238 else if (TREE_CODE (type) == METHOD_TYPE)
11239 type = build_ptrmemfunc_type (build_pointer_type (type));
11240 else if (declarator->kind == cdk_ptrmem)
11242 gcc_assert (TREE_CODE (declarator->u.pointer.class_type)
11243 != NAMESPACE_DECL);
11244 if (declarator->u.pointer.class_type == error_mark_node)
11245 /* We will already have complained. */
11246 type = error_mark_node;
11247 else
11248 type = build_ptrmem_type (declarator->u.pointer.class_type,
11249 type);
11251 else
11252 type = build_pointer_type (type);
11254 /* Process a list of type modifier keywords (such as
11255 const or volatile) that were given inside the `*' or `&'. */
11257 if (declarator->u.pointer.qualifiers)
11259 type
11260 = cp_build_qualified_type (type,
11261 declarator->u.pointer.qualifiers);
11262 type_quals = cp_type_quals (type);
11265 /* Apply C++11 attributes to the pointer, and not to the
11266 type pointed to. This is unlike what is done for GNU
11267 attributes above. It is to comply with [dcl.ptr]/1:
11269 [the optional attribute-specifier-seq (7.6.1) appertains
11270 to the pointer and not to the object pointed to]. */
11271 if (declarator->std_attributes)
11272 decl_attributes (&type, declarator->std_attributes,
11275 ctype = NULL_TREE;
11276 break;
11278 case cdk_error:
11279 break;
11281 default:
11282 gcc_unreachable ();
11286 /* A `constexpr' specifier used in an object declaration declares
11287 the object as `const'. */
11288 if (constexpr_p && innermost_code != cdk_function)
11290 /* DR1688 says that a `constexpr' specifier in combination with
11291 `volatile' is valid. */
11293 if (TREE_CODE (type) != REFERENCE_TYPE)
11295 type_quals |= TYPE_QUAL_CONST;
11296 type = cp_build_qualified_type (type, type_quals);
11300 if (unqualified_id && TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR
11301 && TREE_CODE (type) != FUNCTION_TYPE
11302 && TREE_CODE (type) != METHOD_TYPE
11303 && !variable_template_p (TREE_OPERAND (unqualified_id, 0)))
11305 error ("template-id %qD used as a declarator",
11306 unqualified_id);
11307 unqualified_id = dname;
11310 /* If TYPE is a FUNCTION_TYPE, but the function name was explicitly
11311 qualified with a class-name, turn it into a METHOD_TYPE, unless
11312 we know that the function is static. We take advantage of this
11313 opportunity to do other processing that pertains to entities
11314 explicitly declared to be class members. Note that if DECLARATOR
11315 is non-NULL, we know it is a cdk_id declarator; otherwise, we
11316 would not have exited the loop above. */
11317 if (declarator
11318 && declarator->kind == cdk_id
11319 && declarator->u.id.qualifying_scope
11320 && MAYBE_CLASS_TYPE_P (declarator->u.id.qualifying_scope))
11322 ctype = declarator->u.id.qualifying_scope;
11323 ctype = TYPE_MAIN_VARIANT (ctype);
11324 template_count = num_template_headers_for_class (ctype);
11326 if (ctype == current_class_type)
11328 if (friendp)
11330 permerror (input_location, "member functions are implicitly "
11331 "friends of their class");
11332 friendp = 0;
11334 else
11335 permerror (declarator->id_loc,
11336 "extra qualification %<%T::%> on member %qs",
11337 ctype, name);
11339 else if (/* If the qualifying type is already complete, then we
11340 can skip the following checks. */
11341 !COMPLETE_TYPE_P (ctype)
11342 && (/* If the function is being defined, then
11343 qualifying type must certainly be complete. */
11344 funcdef_flag
11345 /* A friend declaration of "T::f" is OK, even if
11346 "T" is a template parameter. But, if this
11347 function is not a friend, the qualifying type
11348 must be a class. */
11349 || (!friendp && !CLASS_TYPE_P (ctype))
11350 /* For a declaration, the type need not be
11351 complete, if either it is dependent (since there
11352 is no meaningful definition of complete in that
11353 case) or the qualifying class is currently being
11354 defined. */
11355 || !(dependent_type_p (ctype)
11356 || currently_open_class (ctype)))
11357 /* Check that the qualifying type is complete. */
11358 && !complete_type_or_else (ctype, NULL_TREE))
11359 return error_mark_node;
11360 else if (TREE_CODE (type) == FUNCTION_TYPE)
11362 if (current_class_type
11363 && (!friendp || funcdef_flag || initialized))
11365 error (funcdef_flag || initialized
11366 ? G_("cannot define member function %<%T::%s%> "
11367 "within %qT")
11368 : G_("cannot declare member function %<%T::%s%> "
11369 "within %qT"),
11370 ctype, name, current_class_type);
11371 return error_mark_node;
11374 else if (typedef_p && current_class_type)
11376 error ("cannot declare member %<%T::%s%> within %qT",
11377 ctype, name, current_class_type);
11378 return error_mark_node;
11382 if (ctype == NULL_TREE && decl_context == FIELD && friendp == 0)
11383 ctype = current_class_type;
11385 /* Now TYPE has the actual type. */
11387 if (returned_attrs)
11389 if (attrlist)
11390 *attrlist = chainon (returned_attrs, *attrlist);
11391 else
11392 attrlist = &returned_attrs;
11395 if (declarator
11396 && declarator->kind == cdk_id
11397 && declarator->std_attributes
11398 && attrlist != NULL)
11399 /* [dcl.meaning]/1: The optional attribute-specifier-seq following
11400 a declarator-id appertains to the entity that is declared. */
11401 *attrlist = chainon (*attrlist, declarator->std_attributes);
11403 /* Handle parameter packs. */
11404 if (parameter_pack_p)
11406 if (decl_context == PARM)
11407 /* Turn the type into a pack expansion.*/
11408 type = make_pack_expansion (type);
11409 else
11410 error ("non-parameter %qs cannot be a parameter pack", name);
11413 if ((decl_context == FIELD || decl_context == PARM)
11414 && !processing_template_decl
11415 && variably_modified_type_p (type, NULL_TREE))
11417 if (decl_context == FIELD)
11418 error ("data member may not have variably modified type %qT", type);
11419 else
11420 error ("parameter may not have variably modified type %qT", type);
11421 type = error_mark_node;
11424 if (explicitp == 1 || (explicitp && friendp))
11426 /* [dcl.fct.spec] (C++11) The explicit specifier shall be used only
11427 in the declaration of a constructor or conversion function within
11428 a class definition. */
11429 if (!current_class_type)
11430 error_at (declspecs->locations[ds_explicit],
11431 "%<explicit%> outside class declaration");
11432 else if (friendp)
11433 error_at (declspecs->locations[ds_explicit],
11434 "%<explicit%> in friend declaration");
11435 else
11436 error_at (declspecs->locations[ds_explicit],
11437 "only declarations of constructors and conversion operators "
11438 "can be %<explicit%>");
11439 explicitp = 0;
11442 if (storage_class == sc_mutable)
11444 if (decl_context != FIELD || friendp)
11446 error ("non-member %qs cannot be declared %<mutable%>", name);
11447 storage_class = sc_none;
11449 else if (decl_context == TYPENAME || typedef_p)
11451 error ("non-object member %qs cannot be declared %<mutable%>", name);
11452 storage_class = sc_none;
11454 else if (TREE_CODE (type) == FUNCTION_TYPE
11455 || TREE_CODE (type) == METHOD_TYPE)
11457 error ("function %qs cannot be declared %<mutable%>", name);
11458 storage_class = sc_none;
11460 else if (staticp)
11462 error ("static %qs cannot be declared %<mutable%>", name);
11463 storage_class = sc_none;
11465 else if (type_quals & TYPE_QUAL_CONST)
11467 error ("const %qs cannot be declared %<mutable%>", name);
11468 storage_class = sc_none;
11470 else if (TREE_CODE (type) == REFERENCE_TYPE)
11472 permerror (input_location, "reference %qs cannot be declared "
11473 "%<mutable%>", name);
11474 storage_class = sc_none;
11478 /* If this is declaring a typedef name, return a TYPE_DECL. */
11479 if (typedef_p && decl_context != TYPENAME)
11481 tree decl;
11483 /* This declaration:
11485 typedef void f(int) const;
11487 declares a function type which is not a member of any
11488 particular class, but which is cv-qualified; for
11489 example "f S::*" declares a pointer to a const-qualified
11490 member function of S. We record the cv-qualification in the
11491 function type. */
11492 if ((rqual || memfn_quals) && TREE_CODE (type) == FUNCTION_TYPE)
11494 type = apply_memfn_quals (type, memfn_quals, rqual);
11496 /* We have now dealt with these qualifiers. */
11497 memfn_quals = TYPE_UNQUALIFIED;
11498 rqual = REF_QUAL_NONE;
11501 if (type_uses_auto (type))
11503 error ("typedef declared %<auto%>");
11504 type = error_mark_node;
11507 if (reqs)
11508 error_at (location_of (reqs), "requires-clause on typedef");
11510 if (decl_context == FIELD)
11511 decl = build_lang_decl (TYPE_DECL, unqualified_id, type);
11512 else
11513 decl = build_decl (input_location, TYPE_DECL, unqualified_id, type);
11514 if (id_declarator && declarator->u.id.qualifying_scope) {
11515 error_at (DECL_SOURCE_LOCATION (decl),
11516 "typedef name may not be a nested-name-specifier");
11517 TREE_TYPE (decl) = error_mark_node;
11520 if (decl_context != FIELD)
11522 if (!current_function_decl)
11523 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
11524 else if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (current_function_decl)
11525 || (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P
11526 (current_function_decl)))
11527 /* The TYPE_DECL is "abstract" because there will be
11528 clones of this constructor/destructor, and there will
11529 be copies of this TYPE_DECL generated in those
11530 clones. The decloning optimization (for space) may
11531 revert this subsequently if it determines that
11532 the clones should share a common implementation. */
11533 DECL_ABSTRACT_P (decl) = true;
11535 else if (current_class_type
11536 && constructor_name_p (unqualified_id, current_class_type))
11537 permerror (input_location, "ISO C++ forbids nested type %qD with same name "
11538 "as enclosing class",
11539 unqualified_id);
11541 /* If the user declares "typedef struct {...} foo" then the
11542 struct will have an anonymous name. Fill that name in now.
11543 Nothing can refer to it, so nothing needs know about the name
11544 change. */
11545 if (type != error_mark_node
11546 && unqualified_id
11547 && TYPE_NAME (type)
11548 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
11549 && TYPE_UNNAMED_P (type)
11550 && declspecs->type_definition_p
11551 && attributes_naming_typedef_ok (*attrlist)
11552 && cp_type_quals (type) == TYPE_UNQUALIFIED)
11553 name_unnamed_type (type, decl);
11555 if (signed_p
11556 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
11557 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
11559 bad_specifiers (decl, BSP_TYPE, virtualp,
11560 memfn_quals != TYPE_UNQUALIFIED,
11561 inlinep, friendp, raises != NULL_TREE);
11563 if (decl_spec_seq_has_spec_p (declspecs, ds_alias))
11564 /* Acknowledge that this was written:
11565 `using analias = atype;'. */
11566 TYPE_DECL_ALIAS_P (decl) = 1;
11568 return decl;
11571 /* Detect the case of an array type of unspecified size
11572 which came, as such, direct from a typedef name.
11573 We must copy the type, so that the array's domain can be
11574 individually set by the object's initializer. */
11576 if (type && typedef_type
11577 && TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
11578 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
11579 type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
11581 /* Detect where we're using a typedef of function type to declare a
11582 function. PARMS will not be set, so we must create it now. */
11584 if (type == typedef_type && TREE_CODE (type) == FUNCTION_TYPE)
11586 tree decls = NULL_TREE;
11587 tree args;
11589 for (args = TYPE_ARG_TYPES (type);
11590 args && args != void_list_node;
11591 args = TREE_CHAIN (args))
11593 tree decl = cp_build_parm_decl (NULL_TREE, NULL_TREE,
11594 TREE_VALUE (args));
11596 DECL_CHAIN (decl) = decls;
11597 decls = decl;
11600 parms = nreverse (decls);
11602 if (decl_context != TYPENAME)
11604 /* The qualifiers on the function type become the qualifiers on
11605 the non-static member function. */
11606 memfn_quals |= type_memfn_quals (type);
11607 rqual = type_memfn_rqual (type);
11608 type_quals = TYPE_UNQUALIFIED;
11612 /* If this is a type name (such as, in a cast or sizeof),
11613 compute the type and return it now. */
11615 if (decl_context == TYPENAME)
11617 /* Note that here we don't care about type_quals. */
11619 /* Special case: "friend class foo" looks like a TYPENAME context. */
11620 if (friendp)
11622 if (inlinep)
11624 error ("%<inline%> specified for friend class declaration");
11625 inlinep = 0;
11628 if (!current_aggr)
11630 /* Don't allow friend declaration without a class-key. */
11631 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
11632 permerror (input_location, "template parameters cannot be friends");
11633 else if (TREE_CODE (type) == TYPENAME_TYPE)
11634 permerror (input_location, "friend declaration requires class-key, "
11635 "i.e. %<friend class %T::%D%>",
11636 TYPE_CONTEXT (type), TYPENAME_TYPE_FULLNAME (type));
11637 else
11638 permerror (input_location, "friend declaration requires class-key, "
11639 "i.e. %<friend %#T%>",
11640 type);
11643 /* Only try to do this stuff if we didn't already give up. */
11644 if (type != integer_type_node)
11646 /* A friendly class? */
11647 if (current_class_type)
11648 make_friend_class (current_class_type, TYPE_MAIN_VARIANT (type),
11649 /*complain=*/true);
11650 else
11651 error ("trying to make class %qT a friend of global scope",
11652 type);
11654 type = void_type_node;
11657 else if (memfn_quals || rqual)
11659 if (ctype == NULL_TREE
11660 && TREE_CODE (type) == METHOD_TYPE)
11661 ctype = TYPE_METHOD_BASETYPE (type);
11663 if (ctype)
11664 type = build_memfn_type (type, ctype, memfn_quals, rqual);
11665 /* Core issue #547: need to allow this in template type args.
11666 Allow it in general in C++11 for alias-declarations. */
11667 else if ((template_type_arg || cxx_dialect >= cxx11)
11668 && TREE_CODE (type) == FUNCTION_TYPE)
11669 type = apply_memfn_quals (type, memfn_quals, rqual);
11670 else
11671 error ("invalid qualifiers on non-member function type");
11674 if (reqs)
11675 error_at (location_of (reqs), "requires-clause on type-id");
11677 return type;
11679 else if (unqualified_id == NULL_TREE && decl_context != PARM
11680 && decl_context != CATCHPARM
11681 && TREE_CODE (type) != UNION_TYPE
11682 && ! bitfield
11683 && innermost_code != cdk_decomp)
11685 error ("abstract declarator %qT used as declaration", type);
11686 return error_mark_node;
11689 if (!FUNC_OR_METHOD_TYPE_P (type))
11691 /* Only functions may be declared using an operator-function-id. */
11692 if (dname && IDENTIFIER_ANY_OP_P (dname))
11694 error ("declaration of %qD as non-function", dname);
11695 return error_mark_node;
11698 if (reqs)
11699 error_at (location_of (reqs),
11700 "requires-clause on declaration of non-function type %qT",
11701 type);
11704 /* We don't check parameter types here because we can emit a better
11705 error message later. */
11706 if (decl_context != PARM)
11708 type = check_var_type (unqualified_id, type);
11709 if (type == error_mark_node)
11710 return error_mark_node;
11713 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
11714 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
11716 if (decl_context == PARM || decl_context == CATCHPARM)
11718 if (ctype || in_namespace)
11719 error ("cannot use %<::%> in parameter declaration");
11721 if (type_uses_auto (type)
11722 && !(cxx_dialect >= cxx1z && template_parm_flag))
11724 if (cxx_dialect >= cxx14)
11725 error ("%<auto%> parameter not permitted in this context");
11726 else
11727 error ("parameter declared %<auto%>");
11728 type = error_mark_node;
11731 /* A parameter declared as an array of T is really a pointer to T.
11732 One declared as a function is really a pointer to a function.
11733 One declared as a member is really a pointer to member. */
11735 if (TREE_CODE (type) == ARRAY_TYPE)
11737 /* Transfer const-ness of array into that of type pointed to. */
11738 type = build_pointer_type (TREE_TYPE (type));
11739 type_quals = TYPE_UNQUALIFIED;
11740 array_parameter_p = true;
11742 else if (TREE_CODE (type) == FUNCTION_TYPE)
11743 type = build_pointer_type (type);
11746 if (ctype && TREE_CODE (type) == FUNCTION_TYPE && staticp < 2
11747 && !(identifier_p (unqualified_id)
11748 && IDENTIFIER_NEWDEL_OP_P (unqualified_id)))
11750 cp_cv_quals real_quals = memfn_quals;
11751 if (cxx_dialect < cxx14 && constexpr_p
11752 && sfk != sfk_constructor && sfk != sfk_destructor)
11753 real_quals |= TYPE_QUAL_CONST;
11754 type = build_memfn_type (type, ctype, real_quals, rqual);
11758 tree decl = NULL_TREE;
11760 if (decl_context == PARM)
11762 decl = cp_build_parm_decl (NULL_TREE, unqualified_id, type);
11763 DECL_ARRAY_PARAMETER_P (decl) = array_parameter_p;
11765 bad_specifiers (decl, BSP_PARM, virtualp,
11766 memfn_quals != TYPE_UNQUALIFIED,
11767 inlinep, friendp, raises != NULL_TREE);
11769 else if (decl_context == FIELD)
11771 if (!staticp && !friendp && TREE_CODE (type) != METHOD_TYPE)
11772 if (tree auto_node = type_uses_auto (type))
11774 location_t loc = declspecs->locations[ds_type_spec];
11775 if (CLASS_PLACEHOLDER_TEMPLATE (auto_node))
11776 error_at (loc, "invalid use of template-name %qE without an "
11777 "argument list",
11778 CLASS_PLACEHOLDER_TEMPLATE (auto_node));
11779 else
11780 error_at (loc, "non-static data member declared with "
11781 "placeholder %qT", auto_node);
11782 type = error_mark_node;
11785 /* The C99 flexible array extension. */
11786 if (!staticp && TREE_CODE (type) == ARRAY_TYPE
11787 && TYPE_DOMAIN (type) == NULL_TREE)
11789 if (ctype
11790 && (TREE_CODE (ctype) == UNION_TYPE
11791 || TREE_CODE (ctype) == QUAL_UNION_TYPE))
11793 error ("flexible array member in union");
11794 type = error_mark_node;
11796 else
11798 /* Array is a flexible member. */
11799 if (in_system_header_at (input_location))
11800 /* Do not warn on flexible array members in system
11801 headers because glibc uses them. */;
11802 else if (name)
11803 pedwarn (input_location, OPT_Wpedantic,
11804 "ISO C++ forbids flexible array member %qs", name);
11805 else
11806 pedwarn (input_location, OPT_Wpedantic,
11807 "ISO C++ forbids flexible array members");
11809 /* Flexible array member has a null domain. */
11810 type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
11814 if (type == error_mark_node)
11816 /* Happens when declaring arrays of sizes which
11817 are error_mark_node, for example. */
11818 decl = NULL_TREE;
11820 else if (in_namespace && !friendp)
11822 /* Something like struct S { int N::j; }; */
11823 error ("invalid use of %<::%>");
11824 return error_mark_node;
11826 else if (TREE_CODE (type) == FUNCTION_TYPE
11827 || TREE_CODE (type) == METHOD_TYPE)
11829 int publicp = 0;
11830 tree function_context;
11832 if (friendp == 0)
11834 /* This should never happen in pure C++ (the check
11835 could be an assert). It could happen in
11836 Objective-C++ if someone writes invalid code that
11837 uses a function declaration for an instance
11838 variable or property (instance variables and
11839 properties are parsed as FIELD_DECLs, but they are
11840 part of an Objective-C class, not a C++ class).
11841 That code is invalid and is caught by this
11842 check. */
11843 if (!ctype)
11845 error ("declaration of function %qD in invalid context",
11846 unqualified_id);
11847 return error_mark_node;
11850 /* ``A union may [ ... ] not [ have ] virtual functions.''
11851 ARM 9.5 */
11852 if (virtualp && TREE_CODE (ctype) == UNION_TYPE)
11854 error ("function %qD declared %<virtual%> inside a union",
11855 unqualified_id);
11856 return error_mark_node;
11859 if (virtualp
11860 && identifier_p (unqualified_id)
11861 && IDENTIFIER_NEWDEL_OP_P (unqualified_id))
11863 error ("%qD cannot be declared %<virtual%>, since it "
11864 "is always static", unqualified_id);
11865 virtualp = 0;
11869 /* Check that the name used for a destructor makes sense. */
11870 if (sfk == sfk_destructor)
11872 tree uqname = id_declarator->u.id.unqualified_name;
11874 if (!ctype)
11876 gcc_assert (friendp);
11877 error ("expected qualified name in friend declaration "
11878 "for destructor %qD", uqname);
11879 return error_mark_node;
11882 if (!check_dtor_name (ctype, TREE_OPERAND (uqname, 0)))
11884 error ("declaration of %qD as member of %qT",
11885 uqname, ctype);
11886 return error_mark_node;
11888 if (concept_p)
11890 error ("a destructor cannot be %<concept%>");
11891 return error_mark_node;
11893 if (constexpr_p)
11895 error ("a destructor cannot be %<constexpr%>");
11896 return error_mark_node;
11899 else if (sfk == sfk_constructor && friendp && !ctype)
11901 error ("expected qualified name in friend declaration "
11902 "for constructor %qD",
11903 id_declarator->u.id.unqualified_name);
11904 return error_mark_node;
11906 if (sfk == sfk_constructor)
11907 if (concept_p)
11909 error ("a constructor cannot be %<concept%>");
11910 return error_mark_node;
11912 if (concept_p)
11914 error ("a concept cannot be a member function");
11915 concept_p = false;
11918 if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
11920 tree tmpl = TREE_OPERAND (unqualified_id, 0);
11921 if (variable_template_p (tmpl))
11923 error ("specialization of variable template %qD "
11924 "declared as function", tmpl);
11925 inform (DECL_SOURCE_LOCATION (tmpl),
11926 "variable template declared here");
11927 return error_mark_node;
11931 /* Tell grokfndecl if it needs to set TREE_PUBLIC on the node. */
11932 function_context = (ctype != NULL_TREE) ?
11933 decl_function_context (TYPE_MAIN_DECL (ctype)) : NULL_TREE;
11934 publicp = (! friendp || ! staticp)
11935 && function_context == NULL_TREE;
11937 if (late_return_type_p)
11938 TYPE_HAS_LATE_RETURN_TYPE (type) = 1;
11940 decl = grokfndecl (ctype, type,
11941 TREE_CODE (unqualified_id) != TEMPLATE_ID_EXPR
11942 ? unqualified_id : dname,
11943 parms,
11944 unqualified_id,
11945 reqs,
11946 virtualp, flags, memfn_quals, rqual, raises,
11947 friendp ? -1 : 0, friendp, publicp,
11948 inlinep | (2 * constexpr_p) | (4 * concept_p),
11949 initialized == SD_DELETED, sfk,
11950 funcdef_flag, template_count, in_namespace,
11951 attrlist, declarator->id_loc);
11952 decl = set_virt_specifiers (decl, virt_specifiers);
11953 if (decl == NULL_TREE)
11954 return error_mark_node;
11955 #if 0
11956 /* This clobbers the attrs stored in `decl' from `attrlist'. */
11957 /* The decl and setting of decl_attr is also turned off. */
11958 decl = build_decl_attribute_variant (decl, decl_attr);
11959 #endif
11961 /* [class.conv.ctor]
11963 A constructor declared without the function-specifier
11964 explicit that can be called with a single parameter
11965 specifies a conversion from the type of its first
11966 parameter to the type of its class. Such a constructor
11967 is called a converting constructor. */
11968 if (explicitp == 2)
11969 DECL_NONCONVERTING_P (decl) = 1;
11971 else if (!staticp && !dependent_type_p (type)
11972 && !COMPLETE_TYPE_P (complete_type (type))
11973 && (!complete_or_array_type_p (type)
11974 || initialized == 0))
11976 if (TREE_CODE (type) != ARRAY_TYPE
11977 || !COMPLETE_TYPE_P (TREE_TYPE (type)))
11979 if (unqualified_id)
11981 error ("field %qD has incomplete type %qT",
11982 unqualified_id, type);
11983 cxx_incomplete_type_inform (strip_array_types (type));
11985 else
11986 error ("name %qT has incomplete type", type);
11988 type = error_mark_node;
11989 decl = NULL_TREE;
11992 else
11994 if (friendp)
11996 error ("%qE is neither function nor member function; "
11997 "cannot be declared friend", unqualified_id);
11998 friendp = 0;
12000 decl = NULL_TREE;
12003 if (friendp)
12005 /* Friends are treated specially. */
12006 if (ctype == current_class_type)
12007 ; /* We already issued a permerror. */
12008 else if (decl && DECL_NAME (decl))
12010 if (template_class_depth (current_class_type) == 0)
12012 decl = check_explicit_specialization
12013 (unqualified_id, decl, template_count,
12014 2 * funcdef_flag + 4);
12015 if (decl == error_mark_node)
12016 return error_mark_node;
12019 decl = do_friend (ctype, unqualified_id, decl,
12020 *attrlist, flags,
12021 funcdef_flag);
12022 return decl;
12024 else
12025 return error_mark_node;
12028 /* Structure field. It may not be a function, except for C++. */
12030 if (decl == NULL_TREE)
12032 if (staticp)
12034 /* C++ allows static class members. All other work
12035 for this is done by grokfield. */
12036 decl = build_lang_decl_loc (declarator
12037 ? declarator->id_loc
12038 : input_location,
12039 VAR_DECL, unqualified_id, type);
12040 set_linkage_for_static_data_member (decl);
12041 if (concept_p)
12042 error ("static data member %qE declared %<concept%>",
12043 unqualified_id);
12044 else if (constexpr_p && !initialized)
12046 error ("constexpr static data member %qD must have an "
12047 "initializer", decl);
12048 constexpr_p = false;
12051 if (inlinep)
12052 mark_inline_variable (decl);
12054 if (!DECL_VAR_DECLARED_INLINE_P (decl)
12055 && !(cxx_dialect >= cxx1z && constexpr_p))
12056 /* Even if there is an in-class initialization, DECL
12057 is considered undefined until an out-of-class
12058 definition is provided, unless this is an inline
12059 variable. */
12060 DECL_EXTERNAL (decl) = 1;
12062 if (thread_p)
12064 CP_DECL_THREAD_LOCAL_P (decl) = true;
12065 if (!processing_template_decl)
12066 set_decl_tls_model (decl, decl_default_tls_model (decl));
12067 if (declspecs->gnu_thread_keyword_p)
12068 SET_DECL_GNU_TLS_P (decl);
12071 else
12073 if (concept_p)
12074 error ("non-static data member %qE declared %<concept%>",
12075 unqualified_id);
12076 else if (constexpr_p)
12078 error ("non-static data member %qE declared %<constexpr%>",
12079 unqualified_id);
12080 constexpr_p = false;
12082 decl = build_decl (input_location,
12083 FIELD_DECL, unqualified_id, type);
12084 DECL_NONADDRESSABLE_P (decl) = bitfield;
12085 if (bitfield && !unqualified_id)
12086 TREE_NO_WARNING (decl) = 1;
12088 if (storage_class == sc_mutable)
12090 DECL_MUTABLE_P (decl) = 1;
12091 storage_class = sc_none;
12094 if (initialized)
12096 /* An attempt is being made to initialize a non-static
12097 member. This is new in C++11. */
12098 maybe_warn_cpp0x (CPP0X_NSDMI);
12100 /* If this has been parsed with static storage class, but
12101 errors forced staticp to be cleared, ensure NSDMI is
12102 not present. */
12103 if (declspecs->storage_class == sc_static)
12104 DECL_INITIAL (decl) = error_mark_node;
12108 bad_specifiers (decl, BSP_FIELD, virtualp,
12109 memfn_quals != TYPE_UNQUALIFIED,
12110 staticp ? false : inlinep, friendp,
12111 raises != NULL_TREE);
12114 else if (TREE_CODE (type) == FUNCTION_TYPE
12115 || TREE_CODE (type) == METHOD_TYPE)
12117 tree original_name;
12118 int publicp = 0;
12120 if (!unqualified_id)
12121 return error_mark_node;
12123 if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
12124 original_name = dname;
12125 else
12126 original_name = unqualified_id;
12127 // FIXME:gcc_assert (original_name == dname);
12129 if (storage_class == sc_auto)
12130 error ("storage class %<auto%> invalid for function %qs", name);
12131 else if (storage_class == sc_register)
12132 error ("storage class %<register%> invalid for function %qs", name);
12133 else if (thread_p)
12135 if (declspecs->gnu_thread_keyword_p)
12136 error ("storage class %<__thread%> invalid for function %qs",
12137 name);
12138 else
12139 error ("storage class %<thread_local%> invalid for function %qs",
12140 name);
12143 if (virt_specifiers)
12144 error ("virt-specifiers in %qs not allowed outside a class definition", name);
12145 /* Function declaration not at top level.
12146 Storage classes other than `extern' are not allowed
12147 and `extern' makes no difference. */
12148 if (! toplevel_bindings_p ()
12149 && (storage_class == sc_static
12150 || decl_spec_seq_has_spec_p (declspecs, ds_inline))
12151 && pedantic)
12153 if (storage_class == sc_static)
12154 pedwarn (input_location, OPT_Wpedantic,
12155 "%<static%> specifier invalid for function %qs "
12156 "declared out of global scope", name);
12157 else
12158 pedwarn (input_location, OPT_Wpedantic,
12159 "%<inline%> specifier invalid for function %qs "
12160 "declared out of global scope", name);
12163 if (ctype == NULL_TREE)
12165 if (virtualp)
12167 error ("virtual non-class function %qs", name);
12168 virtualp = 0;
12170 else if (sfk == sfk_constructor
12171 || sfk == sfk_destructor)
12173 error (funcdef_flag
12174 ? G_("%qs defined in a non-class scope")
12175 : G_("%qs declared in a non-class scope"), name);
12176 sfk = sfk_none;
12180 /* Record whether the function is public. */
12181 publicp = (ctype != NULL_TREE
12182 || storage_class != sc_static);
12184 if (late_return_type_p)
12185 TYPE_HAS_LATE_RETURN_TYPE (type) = 1;
12187 decl = grokfndecl (ctype, type, original_name, parms, unqualified_id,
12188 reqs, virtualp, flags, memfn_quals, rqual, raises,
12189 1, friendp,
12190 publicp,
12191 inlinep | (2 * constexpr_p) | (4 * concept_p),
12192 initialized == SD_DELETED,
12193 sfk,
12194 funcdef_flag,
12195 template_count, in_namespace, attrlist,
12196 declarator->id_loc);
12197 if (decl == NULL_TREE)
12198 return error_mark_node;
12200 if (explicitp == 2)
12201 DECL_NONCONVERTING_P (decl) = 1;
12202 if (staticp == 1)
12204 int invalid_static = 0;
12206 /* Don't allow a static member function in a class, and forbid
12207 declaring main to be static. */
12208 if (TREE_CODE (type) == METHOD_TYPE)
12210 permerror (input_location, "cannot declare member function %qD to have "
12211 "static linkage", decl);
12212 invalid_static = 1;
12214 else if (current_function_decl)
12216 /* 7.1.1: There can be no static function declarations within a
12217 block. */
12218 error ("cannot declare static function inside another function");
12219 invalid_static = 1;
12222 if (invalid_static)
12224 staticp = 0;
12225 storage_class = sc_none;
12229 else
12231 /* It's a variable. */
12233 /* An uninitialized decl with `extern' is a reference. */
12234 decl = grokvardecl (type, dname, unqualified_id,
12235 declspecs,
12236 initialized,
12237 type_quals,
12238 inlinep,
12239 concept_p,
12240 template_count,
12241 ctype ? ctype : in_namespace);
12242 if (decl == NULL_TREE)
12243 return error_mark_node;
12245 bad_specifiers (decl, BSP_VAR, virtualp,
12246 memfn_quals != TYPE_UNQUALIFIED,
12247 inlinep, friendp, raises != NULL_TREE);
12249 if (ctype)
12251 DECL_CONTEXT (decl) = ctype;
12252 if (staticp == 1)
12254 permerror (input_location, "%<static%> may not be used when defining "
12255 "(as opposed to declaring) a static data member");
12256 staticp = 0;
12257 storage_class = sc_none;
12259 if (storage_class == sc_register && TREE_STATIC (decl))
12261 error ("static member %qD declared %<register%>", decl);
12262 storage_class = sc_none;
12264 if (storage_class == sc_extern && pedantic)
12266 pedwarn (input_location, OPT_Wpedantic,
12267 "cannot explicitly declare member %q#D to have "
12268 "extern linkage", decl);
12269 storage_class = sc_none;
12272 else if (constexpr_p && DECL_EXTERNAL (decl))
12274 error ("declaration of constexpr variable %qD is not a definition",
12275 decl);
12276 constexpr_p = false;
12279 if (inlinep)
12280 mark_inline_variable (decl);
12281 if (innermost_code == cdk_decomp)
12283 gcc_assert (declarator && declarator->kind == cdk_decomp);
12284 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
12285 DECL_ARTIFICIAL (decl) = 1;
12286 fit_decomposition_lang_decl (decl, NULL_TREE);
12290 if (VAR_P (decl) && !initialized)
12291 if (tree auto_node = type_uses_auto (type))
12292 if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node))
12294 location_t loc = declspecs->locations[ds_type_spec];
12295 error_at (loc, "declaration of %q#D has no initializer", decl);
12296 TREE_TYPE (decl) = error_mark_node;
12299 if (storage_class == sc_extern && initialized && !funcdef_flag)
12301 if (toplevel_bindings_p ())
12303 /* It's common practice (and completely valid) to have a const
12304 be initialized and declared extern. */
12305 if (!(type_quals & TYPE_QUAL_CONST))
12306 warning (0, "%qs initialized and declared %<extern%>", name);
12308 else
12310 error ("%qs has both %<extern%> and initializer", name);
12311 return error_mark_node;
12315 /* Record `register' declaration for warnings on &
12316 and in case doing stupid register allocation. */
12318 if (storage_class == sc_register)
12320 DECL_REGISTER (decl) = 1;
12321 /* Warn about register storage specifiers on PARM_DECLs. */
12322 if (TREE_CODE (decl) == PARM_DECL)
12324 if (cxx_dialect >= cxx1z)
12325 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
12326 "ISO C++1z does not allow %<register%> storage "
12327 "class specifier");
12328 else
12329 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
12330 "%<register%> storage class specifier used");
12333 else if (storage_class == sc_extern)
12334 DECL_THIS_EXTERN (decl) = 1;
12335 else if (storage_class == sc_static)
12336 DECL_THIS_STATIC (decl) = 1;
12338 /* Set constexpr flag on vars (functions got it in grokfndecl). */
12339 if (constexpr_p && VAR_P (decl))
12340 DECL_DECLARED_CONSTEXPR_P (decl) = true;
12342 /* Record constancy and volatility on the DECL itself . There's
12343 no need to do this when processing a template; we'll do this
12344 for the instantiated declaration based on the type of DECL. */
12345 if (!processing_template_decl)
12346 cp_apply_type_quals_to_decl (type_quals, decl);
12348 return decl;
12352 /* Subroutine of start_function. Ensure that each of the parameter
12353 types (as listed in PARMS) is complete, as is required for a
12354 function definition. */
12356 static void
12357 require_complete_types_for_parms (tree parms)
12359 for (; parms; parms = DECL_CHAIN (parms))
12361 if (dependent_type_p (TREE_TYPE (parms)))
12362 continue;
12363 if (!VOID_TYPE_P (TREE_TYPE (parms))
12364 && complete_type_or_else (TREE_TYPE (parms), parms))
12366 relayout_decl (parms);
12367 DECL_ARG_TYPE (parms) = type_passed_as (TREE_TYPE (parms));
12369 maybe_warn_parm_abi (TREE_TYPE (parms),
12370 DECL_SOURCE_LOCATION (parms));
12372 else
12373 /* grokparms or complete_type_or_else will have already issued
12374 an error. */
12375 TREE_TYPE (parms) = error_mark_node;
12379 /* Returns nonzero if T is a local variable. */
12382 local_variable_p (const_tree t)
12384 if ((VAR_P (t)
12385 /* A VAR_DECL with a context that is a _TYPE is a static data
12386 member. */
12387 && !TYPE_P (CP_DECL_CONTEXT (t))
12388 /* Any other non-local variable must be at namespace scope. */
12389 && !DECL_NAMESPACE_SCOPE_P (t))
12390 || (TREE_CODE (t) == PARM_DECL))
12391 return 1;
12393 return 0;
12396 /* Like local_variable_p, but suitable for use as a tree-walking
12397 function. */
12399 static tree
12400 local_variable_p_walkfn (tree *tp, int *walk_subtrees,
12401 void * /*data*/)
12403 if (local_variable_p (*tp)
12404 && (!DECL_ARTIFICIAL (*tp) || DECL_NAME (*tp) == this_identifier))
12405 return *tp;
12406 else if (TYPE_P (*tp))
12407 *walk_subtrees = 0;
12409 return NULL_TREE;
12412 /* Check that ARG, which is a default-argument expression for a
12413 parameter DECL, is valid. Returns ARG, or ERROR_MARK_NODE, if
12414 something goes wrong. DECL may also be a _TYPE node, rather than a
12415 DECL, if there is no DECL available. */
12417 tree
12418 check_default_argument (tree decl, tree arg, tsubst_flags_t complain)
12420 tree var;
12421 tree decl_type;
12423 if (TREE_CODE (arg) == DEFAULT_ARG)
12424 /* We get a DEFAULT_ARG when looking at an in-class declaration
12425 with a default argument. Ignore the argument for now; we'll
12426 deal with it after the class is complete. */
12427 return arg;
12429 if (TYPE_P (decl))
12431 decl_type = decl;
12432 decl = NULL_TREE;
12434 else
12435 decl_type = TREE_TYPE (decl);
12437 if (arg == error_mark_node
12438 || decl == error_mark_node
12439 || TREE_TYPE (arg) == error_mark_node
12440 || decl_type == error_mark_node)
12441 /* Something already went wrong. There's no need to check
12442 further. */
12443 return error_mark_node;
12445 /* [dcl.fct.default]
12447 A default argument expression is implicitly converted to the
12448 parameter type. */
12449 ++cp_unevaluated_operand;
12450 perform_implicit_conversion_flags (decl_type, arg, complain,
12451 LOOKUP_IMPLICIT);
12452 --cp_unevaluated_operand;
12454 /* Avoid redundant -Wzero-as-null-pointer-constant warnings at
12455 the call sites. */
12456 if (TYPE_PTR_OR_PTRMEM_P (decl_type)
12457 && null_ptr_cst_p (arg))
12458 return nullptr_node;
12460 /* [dcl.fct.default]
12462 Local variables shall not be used in default argument
12463 expressions.
12465 The keyword `this' shall not be used in a default argument of a
12466 member function. */
12467 var = cp_walk_tree_without_duplicates (&arg, local_variable_p_walkfn, NULL);
12468 if (var)
12470 if (complain & tf_warning_or_error)
12472 if (DECL_NAME (var) == this_identifier)
12473 permerror (input_location, "default argument %qE uses %qD",
12474 arg, var);
12475 else
12476 error ("default argument %qE uses local variable %qD", arg, var);
12478 return error_mark_node;
12481 /* All is well. */
12482 return arg;
12485 /* Returns a deprecated type used within TYPE, or NULL_TREE if none. */
12487 static tree
12488 type_is_deprecated (tree type)
12490 enum tree_code code;
12491 if (TREE_DEPRECATED (type))
12492 return type;
12493 if (TYPE_NAME (type))
12495 if (TREE_DEPRECATED (TYPE_NAME (type)))
12496 return type;
12497 else
12498 return NULL_TREE;
12501 /* Do warn about using typedefs to a deprecated class. */
12502 if (OVERLOAD_TYPE_P (type) && type != TYPE_MAIN_VARIANT (type))
12503 return type_is_deprecated (TYPE_MAIN_VARIANT (type));
12505 code = TREE_CODE (type);
12507 if (code == POINTER_TYPE || code == REFERENCE_TYPE
12508 || code == OFFSET_TYPE || code == FUNCTION_TYPE
12509 || code == METHOD_TYPE || code == ARRAY_TYPE)
12510 return type_is_deprecated (TREE_TYPE (type));
12512 if (TYPE_PTRMEMFUNC_P (type))
12513 return type_is_deprecated
12514 (TREE_TYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type))));
12516 return NULL_TREE;
12519 /* Decode the list of parameter types for a function type.
12520 Given the list of things declared inside the parens,
12521 return a list of types.
12523 If this parameter does not end with an ellipsis, we append
12524 void_list_node.
12526 *PARMS is set to the chain of PARM_DECLs created. */
12528 tree
12529 grokparms (tree parmlist, tree *parms)
12531 tree result = NULL_TREE;
12532 tree decls = NULL_TREE;
12533 tree parm;
12534 int any_error = 0;
12536 for (parm = parmlist; parm != NULL_TREE; parm = TREE_CHAIN (parm))
12538 tree type = NULL_TREE;
12539 tree init = TREE_PURPOSE (parm);
12540 tree decl = TREE_VALUE (parm);
12542 if (parm == void_list_node)
12543 break;
12545 if (! decl || TREE_TYPE (decl) == error_mark_node)
12546 continue;
12548 type = TREE_TYPE (decl);
12549 if (VOID_TYPE_P (type))
12551 if (same_type_p (type, void_type_node)
12552 && !init
12553 && !DECL_NAME (decl) && !result
12554 && TREE_CHAIN (parm) == void_list_node)
12555 /* DR 577: A parameter list consisting of a single
12556 unnamed parameter of non-dependent type 'void'. */
12557 break;
12558 else if (cv_qualified_p (type))
12559 error_at (DECL_SOURCE_LOCATION (decl),
12560 "invalid use of cv-qualified type %qT in "
12561 "parameter declaration", type);
12562 else
12563 error_at (DECL_SOURCE_LOCATION (decl),
12564 "invalid use of type %<void%> in parameter "
12565 "declaration");
12566 /* It's not a good idea to actually create parameters of
12567 type `void'; other parts of the compiler assume that a
12568 void type terminates the parameter list. */
12569 type = error_mark_node;
12570 TREE_TYPE (decl) = error_mark_node;
12573 if (type != error_mark_node)
12575 if (deprecated_state != DEPRECATED_SUPPRESS)
12577 tree deptype = type_is_deprecated (type);
12578 if (deptype)
12579 warn_deprecated_use (deptype, NULL_TREE);
12582 /* Top-level qualifiers on the parameters are
12583 ignored for function types. */
12584 type = cp_build_qualified_type (type, 0);
12585 if (TREE_CODE (type) == METHOD_TYPE)
12587 error ("parameter %qD invalidly declared method type", decl);
12588 type = build_pointer_type (type);
12589 TREE_TYPE (decl) = type;
12591 else if (abstract_virtuals_error (decl, type))
12592 any_error = 1; /* Seems like a good idea. */
12593 else if (POINTER_TYPE_P (type))
12595 /* [dcl.fct]/6, parameter types cannot contain pointers
12596 (references) to arrays of unknown bound. */
12597 tree t = TREE_TYPE (type);
12598 int ptr = TYPE_PTR_P (type);
12600 while (1)
12602 if (TYPE_PTR_P (t))
12603 ptr = 1;
12604 else if (TREE_CODE (t) != ARRAY_TYPE)
12605 break;
12606 else if (!TYPE_DOMAIN (t))
12607 break;
12608 t = TREE_TYPE (t);
12610 if (TREE_CODE (t) == ARRAY_TYPE)
12611 error (ptr
12612 ? G_("parameter %qD includes pointer to array of "
12613 "unknown bound %qT")
12614 : G_("parameter %qD includes reference to array of "
12615 "unknown bound %qT"),
12616 decl, t);
12619 if (any_error)
12620 init = NULL_TREE;
12621 else if (init && !processing_template_decl)
12622 init = check_default_argument (decl, init, tf_warning_or_error);
12625 DECL_CHAIN (decl) = decls;
12626 decls = decl;
12627 result = tree_cons (init, type, result);
12629 decls = nreverse (decls);
12630 result = nreverse (result);
12631 if (parm)
12632 result = chainon (result, void_list_node);
12633 *parms = decls;
12635 return result;
12639 /* D is a constructor or overloaded `operator='.
12641 Let T be the class in which D is declared. Then, this function
12642 returns:
12644 -1 if D's is an ill-formed constructor or copy assignment operator
12645 whose first parameter is of type `T'.
12646 0 if D is not a copy constructor or copy assignment
12647 operator.
12648 1 if D is a copy constructor or copy assignment operator whose
12649 first parameter is a reference to non-const qualified T.
12650 2 if D is a copy constructor or copy assignment operator whose
12651 first parameter is a reference to const qualified T.
12653 This function can be used as a predicate. Positive values indicate
12654 a copy constructor and nonzero values indicate a copy assignment
12655 operator. */
12658 copy_fn_p (const_tree d)
12660 tree args;
12661 tree arg_type;
12662 int result = 1;
12664 gcc_assert (DECL_FUNCTION_MEMBER_P (d));
12666 if (TREE_CODE (d) == TEMPLATE_DECL
12667 || (DECL_TEMPLATE_INFO (d)
12668 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d))))
12669 /* Instantiations of template member functions are never copy
12670 functions. Note that member functions of templated classes are
12671 represented as template functions internally, and we must
12672 accept those as copy functions. */
12673 return 0;
12675 args = FUNCTION_FIRST_USER_PARMTYPE (d);
12676 if (!args)
12677 return 0;
12679 arg_type = TREE_VALUE (args);
12680 if (arg_type == error_mark_node)
12681 return 0;
12683 if (TYPE_MAIN_VARIANT (arg_type) == DECL_CONTEXT (d))
12685 /* Pass by value copy assignment operator. */
12686 result = -1;
12688 else if (TREE_CODE (arg_type) == REFERENCE_TYPE
12689 && !TYPE_REF_IS_RVALUE (arg_type)
12690 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)) == DECL_CONTEXT (d))
12692 if (CP_TYPE_CONST_P (TREE_TYPE (arg_type)))
12693 result = 2;
12695 else
12696 return 0;
12698 args = TREE_CHAIN (args);
12700 if (args && args != void_list_node && !TREE_PURPOSE (args))
12701 /* There are more non-optional args. */
12702 return 0;
12704 return result;
12707 /* D is a constructor or overloaded `operator='.
12709 Let T be the class in which D is declared. Then, this function
12710 returns true when D is a move constructor or move assignment
12711 operator, false otherwise. */
12713 bool
12714 move_fn_p (const_tree d)
12716 gcc_assert (DECL_FUNCTION_MEMBER_P (d));
12718 if (cxx_dialect == cxx98)
12719 /* There are no move constructors if we are in C++98 mode. */
12720 return false;
12722 if (TREE_CODE (d) == TEMPLATE_DECL
12723 || (DECL_TEMPLATE_INFO (d)
12724 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d))))
12725 /* Instantiations of template member functions are never move
12726 functions. Note that member functions of templated classes are
12727 represented as template functions internally, and we must
12728 accept those as move functions. */
12729 return 0;
12731 return move_signature_fn_p (d);
12734 /* D is a constructor or overloaded `operator='.
12736 Then, this function returns true when D has the same signature as a move
12737 constructor or move assignment operator (because either it is such a
12738 ctor/op= or it is a template specialization with the same signature),
12739 false otherwise. */
12741 bool
12742 move_signature_fn_p (const_tree d)
12744 tree args;
12745 tree arg_type;
12746 bool result = false;
12748 args = FUNCTION_FIRST_USER_PARMTYPE (d);
12749 if (!args)
12750 return 0;
12752 arg_type = TREE_VALUE (args);
12753 if (arg_type == error_mark_node)
12754 return 0;
12756 if (TREE_CODE (arg_type) == REFERENCE_TYPE
12757 && TYPE_REF_IS_RVALUE (arg_type)
12758 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)),
12759 DECL_CONTEXT (d)))
12760 result = true;
12762 args = TREE_CHAIN (args);
12764 if (args && args != void_list_node && !TREE_PURPOSE (args))
12765 /* There are more non-optional args. */
12766 return false;
12768 return result;
12771 /* Remember any special properties of member function DECL. */
12773 void
12774 grok_special_member_properties (tree decl)
12776 tree class_type;
12778 if (!DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
12779 return;
12781 class_type = DECL_CONTEXT (decl);
12782 if (DECL_CONSTRUCTOR_P (decl))
12784 int ctor = copy_fn_p (decl);
12786 if (!DECL_ARTIFICIAL (decl))
12787 TYPE_HAS_USER_CONSTRUCTOR (class_type) = 1;
12789 if (ctor > 0)
12791 /* [class.copy]
12793 A non-template constructor for class X is a copy
12794 constructor if its first parameter is of type X&, const
12795 X&, volatile X& or const volatile X&, and either there
12796 are no other parameters or else all other parameters have
12797 default arguments. */
12798 TYPE_HAS_COPY_CTOR (class_type) = 1;
12799 if (user_provided_p (decl))
12800 TYPE_HAS_COMPLEX_COPY_CTOR (class_type) = 1;
12801 if (ctor > 1)
12802 TYPE_HAS_CONST_COPY_CTOR (class_type) = 1;
12804 else if (sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (decl)))
12805 TYPE_HAS_DEFAULT_CONSTRUCTOR (class_type) = 1;
12806 else if (move_fn_p (decl) && user_provided_p (decl))
12807 TYPE_HAS_COMPLEX_MOVE_CTOR (class_type) = 1;
12808 else if (is_list_ctor (decl))
12809 TYPE_HAS_LIST_CTOR (class_type) = 1;
12811 if (DECL_DECLARED_CONSTEXPR_P (decl)
12812 && !copy_fn_p (decl) && !move_fn_p (decl))
12813 TYPE_HAS_CONSTEXPR_CTOR (class_type) = 1;
12815 else if (DECL_OVERLOADED_OPERATOR_P (decl) == NOP_EXPR)
12817 /* [class.copy]
12819 A non-template assignment operator for class X is a copy
12820 assignment operator if its parameter is of type X, X&, const
12821 X&, volatile X& or const volatile X&. */
12823 int assop = copy_fn_p (decl);
12825 if (assop)
12827 TYPE_HAS_COPY_ASSIGN (class_type) = 1;
12828 if (user_provided_p (decl))
12829 TYPE_HAS_COMPLEX_COPY_ASSIGN (class_type) = 1;
12830 if (assop != 1)
12831 TYPE_HAS_CONST_COPY_ASSIGN (class_type) = 1;
12833 else if (move_fn_p (decl) && user_provided_p (decl))
12834 TYPE_HAS_COMPLEX_MOVE_ASSIGN (class_type) = 1;
12836 /* Destructors are handled in check_methods. */
12839 /* Check a constructor DECL has the correct form. Complains
12840 if the class has a constructor of the form X(X). */
12842 bool
12843 grok_ctor_properties (const_tree ctype, const_tree decl)
12845 int ctor_parm = copy_fn_p (decl);
12847 if (ctor_parm < 0)
12849 /* [class.copy]
12851 A declaration of a constructor for a class X is ill-formed if
12852 its first parameter is of type (optionally cv-qualified) X
12853 and either there are no other parameters or else all other
12854 parameters have default arguments.
12856 We *don't* complain about member template instantiations that
12857 have this form, though; they can occur as we try to decide
12858 what constructor to use during overload resolution. Since
12859 overload resolution will never prefer such a constructor to
12860 the non-template copy constructor (which is either explicitly
12861 or implicitly defined), there's no need to worry about their
12862 existence. Theoretically, they should never even be
12863 instantiated, but that's hard to forestall. */
12864 error ("invalid constructor; you probably meant %<%T (const %T&)%>",
12865 ctype, ctype);
12866 return false;
12869 return true;
12872 /* An operator with this code is unary, but can also be binary. */
12874 static bool
12875 ambi_op_p (enum tree_code code)
12877 return (code == INDIRECT_REF
12878 || code == ADDR_EXPR
12879 || code == UNARY_PLUS_EXPR
12880 || code == NEGATE_EXPR
12881 || code == PREINCREMENT_EXPR
12882 || code == PREDECREMENT_EXPR);
12885 /* An operator with this name can only be unary. */
12887 static bool
12888 unary_op_p (enum tree_code code)
12890 return (code == TRUTH_NOT_EXPR
12891 || code == BIT_NOT_EXPR
12892 || code == COMPONENT_REF
12893 || code == TYPE_EXPR);
12896 /* DECL is a declaration for an overloaded operator. If COMPLAIN is true,
12897 errors are issued for invalid declarations. */
12899 bool
12900 grok_op_properties (tree decl, bool complain)
12902 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
12903 tree argtype;
12904 int methodp = (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE);
12905 tree name = DECL_NAME (decl);
12906 enum tree_code operator_code;
12907 int arity;
12908 bool ellipsis_p;
12909 tree class_type;
12911 /* Count the number of arguments and check for ellipsis. */
12912 for (argtype = argtypes, arity = 0;
12913 argtype && argtype != void_list_node;
12914 argtype = TREE_CHAIN (argtype))
12915 ++arity;
12916 ellipsis_p = !argtype;
12918 class_type = DECL_CONTEXT (decl);
12919 if (class_type && !CLASS_TYPE_P (class_type))
12920 class_type = NULL_TREE;
12922 if (IDENTIFIER_CONV_OP_P (name))
12923 operator_code = TYPE_EXPR;
12924 else
12926 /* It'd be nice to hang something else of the identifier to
12927 find CODE more directly. */
12928 bool assign_op = IDENTIFIER_ASSIGN_OP_P (name);
12929 const operator_name_info_t *oni
12930 = (assign_op ? assignment_operator_name_info : operator_name_info);
12932 if (false)
12934 #define DEF_OPERATOR(NAME, CODE, MANGLING, ARITY, KIND) \
12935 else if (assign_op == (KIND == cik_assign_op) \
12936 && oni[int (CODE)].identifier == name) \
12937 operator_code = (CODE);
12938 #include "operators.def"
12939 #undef DEF_OPERATOR
12940 else
12941 gcc_unreachable ();
12943 while (0);
12944 gcc_assert (operator_code != MAX_TREE_CODES);
12945 SET_OVERLOADED_OPERATOR_CODE (decl, operator_code);
12947 if (class_type)
12948 switch (operator_code)
12950 case NEW_EXPR:
12951 TYPE_HAS_NEW_OPERATOR (class_type) = 1;
12952 break;
12954 case DELETE_EXPR:
12955 TYPE_GETS_DELETE (class_type) |= 1;
12956 break;
12958 case VEC_NEW_EXPR:
12959 TYPE_HAS_ARRAY_NEW_OPERATOR (class_type) = 1;
12960 break;
12962 case VEC_DELETE_EXPR:
12963 TYPE_GETS_DELETE (class_type) |= 2;
12964 break;
12966 default:
12967 break;
12970 /* [basic.std.dynamic.allocation]/1:
12972 A program is ill-formed if an allocation function is declared
12973 in a namespace scope other than global scope or declared static
12974 in global scope.
12976 The same also holds true for deallocation functions. */
12977 if (operator_code == NEW_EXPR || operator_code == VEC_NEW_EXPR
12978 || operator_code == DELETE_EXPR || operator_code == VEC_DELETE_EXPR)
12980 if (DECL_NAMESPACE_SCOPE_P (decl))
12982 if (CP_DECL_CONTEXT (decl) != global_namespace)
12984 error ("%qD may not be declared within a namespace", decl);
12985 return false;
12987 else if (!TREE_PUBLIC (decl))
12989 error ("%qD may not be declared as static", decl);
12990 return false;
12995 if (operator_code == NEW_EXPR || operator_code == VEC_NEW_EXPR)
12997 TREE_TYPE (decl) = coerce_new_type (TREE_TYPE (decl));
12998 DECL_IS_OPERATOR_NEW (decl) = 1;
13000 else if (operator_code == DELETE_EXPR || operator_code == VEC_DELETE_EXPR)
13001 TREE_TYPE (decl) = coerce_delete_type (TREE_TYPE (decl));
13002 else
13004 /* An operator function must either be a non-static member function
13005 or have at least one parameter of a class, a reference to a class,
13006 an enumeration, or a reference to an enumeration. 13.4.0.6 */
13007 if (! methodp || DECL_STATIC_FUNCTION_P (decl))
13009 if (operator_code == TYPE_EXPR
13010 || operator_code == CALL_EXPR
13011 || operator_code == COMPONENT_REF
13012 || operator_code == ARRAY_REF
13013 || operator_code == NOP_EXPR)
13015 error ("%qD must be a nonstatic member function", decl);
13016 return false;
13018 else
13020 tree p;
13022 if (DECL_STATIC_FUNCTION_P (decl))
13024 error ("%qD must be either a non-static member "
13025 "function or a non-member function", decl);
13026 return false;
13029 for (p = argtypes; p && p != void_list_node; p = TREE_CHAIN (p))
13031 tree arg = non_reference (TREE_VALUE (p));
13032 if (arg == error_mark_node)
13033 return false;
13035 /* MAYBE_CLASS_TYPE_P, rather than CLASS_TYPE_P, is used
13036 because these checks are performed even on
13037 template functions. */
13038 if (MAYBE_CLASS_TYPE_P (arg)
13039 || TREE_CODE (arg) == ENUMERAL_TYPE)
13040 break;
13043 if (!p || p == void_list_node)
13045 if (complain)
13046 error ("%qD must have an argument of class or "
13047 "enumerated type", decl);
13048 return false;
13053 /* There are no restrictions on the arguments to an overloaded
13054 "operator ()". */
13055 if (operator_code == CALL_EXPR)
13056 return true;
13058 /* Warn about conversion operators that will never be used. */
13059 if (IDENTIFIER_CONV_OP_P (name)
13060 && ! DECL_TEMPLATE_INFO (decl)
13061 && warn_conversion
13062 /* Warn only declaring the function; there is no need to
13063 warn again about out-of-class definitions. */
13064 && class_type == current_class_type)
13066 tree t = TREE_TYPE (name);
13067 int ref = (TREE_CODE (t) == REFERENCE_TYPE);
13069 if (ref)
13070 t = TYPE_MAIN_VARIANT (TREE_TYPE (t));
13072 if (VOID_TYPE_P (t))
13073 warning (OPT_Wconversion,
13075 ? G_("conversion to a reference to void "
13076 "will never use a type conversion operator")
13077 : G_("conversion to void "
13078 "will never use a type conversion operator"));
13079 else if (class_type)
13081 if (t == class_type)
13082 warning (OPT_Wconversion,
13084 ? G_("conversion to a reference to the same type "
13085 "will never use a type conversion operator")
13086 : G_("conversion to the same type "
13087 "will never use a type conversion operator"));
13088 /* Don't force t to be complete here. */
13089 else if (MAYBE_CLASS_TYPE_P (t)
13090 && COMPLETE_TYPE_P (t)
13091 && DERIVED_FROM_P (t, class_type))
13092 warning (OPT_Wconversion,
13094 ? G_("conversion to a reference to a base class "
13095 "will never use a type conversion operator")
13096 : G_("conversion to a base class "
13097 "will never use a type conversion operator"));
13102 if (operator_code == COND_EXPR)
13104 /* 13.4.0.3 */
13105 error ("ISO C++ prohibits overloading operator ?:");
13106 return false;
13108 else if (ellipsis_p)
13110 error ("%qD must not have variable number of arguments", decl);
13111 return false;
13113 else if (ambi_op_p (operator_code))
13115 if (arity == 1)
13116 /* We pick the one-argument operator codes by default, so
13117 we don't have to change anything. */
13119 else if (arity == 2)
13121 /* If we thought this was a unary operator, we now know
13122 it to be a binary operator. */
13123 switch (operator_code)
13125 case INDIRECT_REF:
13126 operator_code = MULT_EXPR;
13127 break;
13129 case ADDR_EXPR:
13130 operator_code = BIT_AND_EXPR;
13131 break;
13133 case UNARY_PLUS_EXPR:
13134 operator_code = PLUS_EXPR;
13135 break;
13137 case NEGATE_EXPR:
13138 operator_code = MINUS_EXPR;
13139 break;
13141 case PREINCREMENT_EXPR:
13142 operator_code = POSTINCREMENT_EXPR;
13143 break;
13145 case PREDECREMENT_EXPR:
13146 operator_code = POSTDECREMENT_EXPR;
13147 break;
13149 default:
13150 gcc_unreachable ();
13153 SET_OVERLOADED_OPERATOR_CODE (decl, operator_code);
13155 if ((operator_code == POSTINCREMENT_EXPR
13156 || operator_code == POSTDECREMENT_EXPR)
13157 && ! processing_template_decl
13158 && ! same_type_p (TREE_VALUE (TREE_CHAIN (argtypes)), integer_type_node))
13160 if (methodp)
13161 error ("postfix %qD must take %<int%> as its argument",
13162 decl);
13163 else
13164 error ("postfix %qD must take %<int%> as its second "
13165 "argument", decl);
13166 return false;
13169 else
13171 if (methodp)
13172 error ("%qD must take either zero or one argument", decl);
13173 else
13174 error ("%qD must take either one or two arguments", decl);
13175 return false;
13178 /* More Effective C++ rule 6. */
13179 if (warn_ecpp
13180 && (operator_code == POSTINCREMENT_EXPR
13181 || operator_code == POSTDECREMENT_EXPR
13182 || operator_code == PREINCREMENT_EXPR
13183 || operator_code == PREDECREMENT_EXPR))
13185 tree arg = TREE_VALUE (argtypes);
13186 tree ret = TREE_TYPE (TREE_TYPE (decl));
13187 if (methodp || TREE_CODE (arg) == REFERENCE_TYPE)
13188 arg = TREE_TYPE (arg);
13189 arg = TYPE_MAIN_VARIANT (arg);
13190 if (operator_code == PREINCREMENT_EXPR
13191 || operator_code == PREDECREMENT_EXPR)
13193 if (TREE_CODE (ret) != REFERENCE_TYPE
13194 || !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (ret)),
13195 arg))
13196 warning (OPT_Weffc__, "prefix %qD should return %qT", decl,
13197 build_reference_type (arg));
13199 else
13201 if (!same_type_p (TYPE_MAIN_VARIANT (ret), arg))
13202 warning (OPT_Weffc__, "postfix %qD should return %qT", decl, arg);
13206 else if (unary_op_p (operator_code))
13208 if (arity != 1)
13210 if (methodp)
13211 error ("%qD must take %<void%>", decl);
13212 else
13213 error ("%qD must take exactly one argument", decl);
13214 return false;
13217 else /* if (binary_op_p (operator_code)) */
13219 if (arity != 2)
13221 if (methodp)
13222 error ("%qD must take exactly one argument", decl);
13223 else
13224 error ("%qD must take exactly two arguments", decl);
13225 return false;
13228 /* More Effective C++ rule 7. */
13229 if (warn_ecpp
13230 && (operator_code == TRUTH_ANDIF_EXPR
13231 || operator_code == TRUTH_ORIF_EXPR
13232 || operator_code == COMPOUND_EXPR))
13233 warning (OPT_Weffc__, "user-defined %qD always evaluates both arguments",
13234 decl);
13237 /* Effective C++ rule 23. */
13238 if (warn_ecpp
13239 && arity == 2
13240 && !DECL_ASSIGNMENT_OPERATOR_P (decl)
13241 && (operator_code == PLUS_EXPR
13242 || operator_code == MINUS_EXPR
13243 || operator_code == TRUNC_DIV_EXPR
13244 || operator_code == MULT_EXPR
13245 || operator_code == TRUNC_MOD_EXPR)
13246 && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == REFERENCE_TYPE)
13247 warning (OPT_Weffc__, "%qD should return by value", decl);
13249 /* [over.oper]/8 */
13250 for (; argtypes && argtypes != void_list_node;
13251 argtypes = TREE_CHAIN (argtypes))
13252 if (TREE_PURPOSE (argtypes))
13254 TREE_PURPOSE (argtypes) = NULL_TREE;
13255 if (operator_code == POSTINCREMENT_EXPR
13256 || operator_code == POSTDECREMENT_EXPR)
13258 pedwarn (input_location, OPT_Wpedantic, "%qD cannot have default arguments",
13259 decl);
13261 else
13263 error ("%qD cannot have default arguments", decl);
13264 return false;
13268 return true;
13271 /* Return a string giving the keyword associate with CODE. */
13273 static const char *
13274 tag_name (enum tag_types code)
13276 switch (code)
13278 case record_type:
13279 return "struct";
13280 case class_type:
13281 return "class";
13282 case union_type:
13283 return "union";
13284 case enum_type:
13285 return "enum";
13286 case typename_type:
13287 return "typename";
13288 default:
13289 gcc_unreachable ();
13293 /* Name lookup in an elaborated-type-specifier (after the keyword
13294 indicated by TAG_CODE) has found the TYPE_DECL DECL. If the
13295 elaborated-type-specifier is invalid, issue a diagnostic and return
13296 error_mark_node; otherwise, return the *_TYPE to which it referred.
13297 If ALLOW_TEMPLATE_P is true, TYPE may be a class template. */
13299 tree
13300 check_elaborated_type_specifier (enum tag_types tag_code,
13301 tree decl,
13302 bool allow_template_p)
13304 tree type;
13306 /* In the case of:
13308 struct S { struct S *p; };
13310 name lookup will find the TYPE_DECL for the implicit "S::S"
13311 typedef. Adjust for that here. */
13312 if (DECL_SELF_REFERENCE_P (decl))
13313 decl = TYPE_NAME (TREE_TYPE (decl));
13315 type = TREE_TYPE (decl);
13317 /* Check TEMPLATE_TYPE_PARM first because DECL_IMPLICIT_TYPEDEF_P
13318 is false for this case as well. */
13319 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
13321 error ("using template type parameter %qT after %qs",
13322 type, tag_name (tag_code));
13323 return error_mark_node;
13325 /* Accept template template parameters. */
13326 else if (allow_template_p
13327 && (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM
13328 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM))
13330 /* [dcl.type.elab]
13332 If the identifier resolves to a typedef-name or the
13333 simple-template-id resolves to an alias template
13334 specialization, the elaborated-type-specifier is ill-formed.
13336 In other words, the only legitimate declaration to use in the
13337 elaborated type specifier is the implicit typedef created when
13338 the type is declared. */
13339 else if (!DECL_IMPLICIT_TYPEDEF_P (decl)
13340 && !DECL_SELF_REFERENCE_P (decl)
13341 && tag_code != typename_type)
13343 if (alias_template_specialization_p (type))
13344 error ("using alias template specialization %qT after %qs",
13345 type, tag_name (tag_code));
13346 else
13347 error ("using typedef-name %qD after %qs", decl, tag_name (tag_code));
13348 inform (DECL_SOURCE_LOCATION (decl),
13349 "%qD has a previous declaration here", decl);
13350 return error_mark_node;
13352 else if (TREE_CODE (type) != RECORD_TYPE
13353 && TREE_CODE (type) != UNION_TYPE
13354 && tag_code != enum_type
13355 && tag_code != typename_type)
13357 error ("%qT referred to as %qs", type, tag_name (tag_code));
13358 inform (location_of (type), "%qT has a previous declaration here", type);
13359 return error_mark_node;
13361 else if (TREE_CODE (type) != ENUMERAL_TYPE
13362 && tag_code == enum_type)
13364 error ("%qT referred to as enum", type);
13365 inform (location_of (type), "%qT has a previous declaration here", type);
13366 return error_mark_node;
13368 else if (!allow_template_p
13369 && TREE_CODE (type) == RECORD_TYPE
13370 && CLASSTYPE_IS_TEMPLATE (type))
13372 /* If a class template appears as elaborated type specifier
13373 without a template header such as:
13375 template <class T> class C {};
13376 void f(class C); // No template header here
13378 then the required template argument is missing. */
13379 error ("template argument required for %<%s %T%>",
13380 tag_name (tag_code),
13381 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)));
13382 return error_mark_node;
13385 return type;
13388 /* Lookup NAME in elaborate type specifier in scope according to
13389 SCOPE and issue diagnostics if necessary.
13390 Return *_TYPE node upon success, NULL_TREE when the NAME is not
13391 found, and ERROR_MARK_NODE for type error. */
13393 static tree
13394 lookup_and_check_tag (enum tag_types tag_code, tree name,
13395 tag_scope scope, bool template_header_p)
13397 tree t;
13398 tree decl;
13399 if (scope == ts_global)
13401 /* First try ordinary name lookup, ignoring hidden class name
13402 injected via friend declaration. */
13403 decl = lookup_name_prefer_type (name, 2);
13404 decl = strip_using_decl (decl);
13405 /* If that fails, the name will be placed in the smallest
13406 non-class, non-function-prototype scope according to 3.3.1/5.
13407 We may already have a hidden name declared as friend in this
13408 scope. So lookup again but not ignoring hidden names.
13409 If we find one, that name will be made visible rather than
13410 creating a new tag. */
13411 if (!decl)
13412 decl = lookup_type_scope (name, ts_within_enclosing_non_class);
13414 else
13415 decl = lookup_type_scope (name, scope);
13417 if (decl
13418 && (DECL_CLASS_TEMPLATE_P (decl)
13419 /* If scope is ts_current we're defining a class, so ignore a
13420 template template parameter. */
13421 || (scope != ts_current
13422 && DECL_TEMPLATE_TEMPLATE_PARM_P (decl))))
13423 decl = DECL_TEMPLATE_RESULT (decl);
13425 if (decl && TREE_CODE (decl) == TYPE_DECL)
13427 /* Look for invalid nested type:
13428 class C {
13429 class C {};
13430 }; */
13431 if (scope == ts_current && DECL_SELF_REFERENCE_P (decl))
13433 error ("%qD has the same name as the class in which it is "
13434 "declared",
13435 decl);
13436 return error_mark_node;
13439 /* Two cases we need to consider when deciding if a class
13440 template is allowed as an elaborated type specifier:
13441 1. It is a self reference to its own class.
13442 2. It comes with a template header.
13444 For example:
13446 template <class T> class C {
13447 class C *c1; // DECL_SELF_REFERENCE_P is true
13448 class D;
13450 template <class U> class C; // template_header_p is true
13451 template <class T> class C<T>::D {
13452 class C *c2; // DECL_SELF_REFERENCE_P is true
13453 }; */
13455 t = check_elaborated_type_specifier (tag_code,
13456 decl,
13457 template_header_p
13458 | DECL_SELF_REFERENCE_P (decl));
13459 if (template_header_p && t && CLASS_TYPE_P (t)
13460 && (!CLASSTYPE_TEMPLATE_INFO (t)
13461 || (!PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)))))
13463 error ("%qT is not a template", t);
13464 inform (location_of (t), "previous declaration here");
13465 if (TYPE_CLASS_SCOPE_P (t)
13466 && CLASSTYPE_TEMPLATE_INFO (TYPE_CONTEXT (t)))
13467 inform (input_location,
13468 "perhaps you want to explicitly add %<%T::%>",
13469 TYPE_CONTEXT (t));
13470 t = error_mark_node;
13473 return t;
13475 else if (decl && TREE_CODE (decl) == TREE_LIST)
13477 error ("reference to %qD is ambiguous", name);
13478 print_candidates (decl);
13479 return error_mark_node;
13481 else
13482 return NULL_TREE;
13485 /* Get the struct, enum or union (TAG_CODE says which) with tag NAME.
13486 Define the tag as a forward-reference if it is not defined.
13488 If a declaration is given, process it here, and report an error if
13489 multiple declarations are not identical.
13491 SCOPE is TS_CURRENT when this is also a definition. Only look in
13492 the current frame for the name (since C++ allows new names in any
13493 scope.) It is TS_WITHIN_ENCLOSING_NON_CLASS if this is a friend
13494 declaration. Only look beginning from the current scope outward up
13495 till the nearest non-class scope. Otherwise it is TS_GLOBAL.
13497 TEMPLATE_HEADER_P is true when this declaration is preceded by
13498 a set of template parameters. */
13500 static tree
13501 xref_tag_1 (enum tag_types tag_code, tree name,
13502 tag_scope scope, bool template_header_p)
13504 enum tree_code code;
13505 tree context = NULL_TREE;
13507 gcc_assert (identifier_p (name));
13509 switch (tag_code)
13511 case record_type:
13512 case class_type:
13513 code = RECORD_TYPE;
13514 break;
13515 case union_type:
13516 code = UNION_TYPE;
13517 break;
13518 case enum_type:
13519 code = ENUMERAL_TYPE;
13520 break;
13521 default:
13522 gcc_unreachable ();
13525 /* In case of anonymous name, xref_tag is only called to
13526 make type node and push name. Name lookup is not required. */
13527 tree t = NULL_TREE;
13528 if (scope != ts_lambda && !anon_aggrname_p (name))
13529 t = lookup_and_check_tag (tag_code, name, scope, template_header_p);
13531 if (t == error_mark_node)
13532 return error_mark_node;
13534 if (scope != ts_current && t && current_class_type
13535 && template_class_depth (current_class_type)
13536 && template_header_p)
13538 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
13539 return t;
13541 /* Since SCOPE is not TS_CURRENT, we are not looking at a
13542 definition of this tag. Since, in addition, we are currently
13543 processing a (member) template declaration of a template
13544 class, we must be very careful; consider:
13546 template <class X>
13547 struct S1
13549 template <class U>
13550 struct S2
13551 { template <class V>
13552 friend struct S1; };
13554 Here, the S2::S1 declaration should not be confused with the
13555 outer declaration. In particular, the inner version should
13556 have a template parameter of level 2, not level 1. This
13557 would be particularly important if the member declaration
13558 were instead:
13560 template <class V = U> friend struct S1;
13562 say, when we should tsubst into `U' when instantiating
13563 S2. On the other hand, when presented with:
13565 template <class T>
13566 struct S1 {
13567 template <class U>
13568 struct S2 {};
13569 template <class U>
13570 friend struct S2;
13573 we must find the inner binding eventually. We
13574 accomplish this by making sure that the new type we
13575 create to represent this declaration has the right
13576 TYPE_CONTEXT. */
13577 context = TYPE_CONTEXT (t);
13578 t = NULL_TREE;
13581 if (! t)
13583 /* If no such tag is yet defined, create a forward-reference node
13584 and record it as the "definition".
13585 When a real declaration of this type is found,
13586 the forward-reference will be altered into a real type. */
13587 if (code == ENUMERAL_TYPE)
13589 error ("use of enum %q#D without previous declaration", name);
13590 return error_mark_node;
13592 else
13594 t = make_class_type (code);
13595 TYPE_CONTEXT (t) = context;
13596 if (scope == ts_lambda)
13597 /* Mark it as a lambda type. */
13598 CLASSTYPE_LAMBDA_EXPR (t) = error_mark_node;
13599 t = pushtag (name, t, scope);
13602 else
13604 if (template_header_p && MAYBE_CLASS_TYPE_P (t))
13606 /* Check that we aren't trying to overload a class with different
13607 constraints. */
13608 tree constr = NULL_TREE;
13609 if (current_template_parms)
13611 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
13612 constr = build_constraints (reqs, NULL_TREE);
13614 if (!redeclare_class_template (t, current_template_parms, constr))
13615 return error_mark_node;
13617 else if (!processing_template_decl
13618 && CLASS_TYPE_P (t)
13619 && CLASSTYPE_IS_TEMPLATE (t))
13621 error ("redeclaration of %qT as a non-template", t);
13622 inform (location_of (t), "previous declaration %qD", t);
13623 return error_mark_node;
13626 /* Make injected friend class visible. */
13627 if (scope != ts_within_enclosing_non_class && TYPE_HIDDEN_P (t))
13629 tree decl = TYPE_NAME (t);
13631 DECL_ANTICIPATED (decl) = false;
13632 DECL_FRIEND_P (decl) = false;
13634 if (TYPE_TEMPLATE_INFO (t))
13636 tree tmpl = TYPE_TI_TEMPLATE (t);
13637 DECL_ANTICIPATED (tmpl) = false;
13638 DECL_FRIEND_P (tmpl) = false;
13643 return t;
13646 /* Wrapper for xref_tag_1. */
13648 tree
13649 xref_tag (enum tag_types tag_code, tree name,
13650 tag_scope scope, bool template_header_p)
13652 tree ret;
13653 bool subtime;
13654 subtime = timevar_cond_start (TV_NAME_LOOKUP);
13655 ret = xref_tag_1 (tag_code, name, scope, template_header_p);
13656 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
13657 return ret;
13661 tree
13662 xref_tag_from_type (tree old, tree id, tag_scope scope)
13664 enum tag_types tag_kind;
13666 if (TREE_CODE (old) == RECORD_TYPE)
13667 tag_kind = (CLASSTYPE_DECLARED_CLASS (old) ? class_type : record_type);
13668 else
13669 tag_kind = union_type;
13671 if (id == NULL_TREE)
13672 id = TYPE_IDENTIFIER (old);
13674 return xref_tag (tag_kind, id, scope, false);
13677 /* Create the binfo hierarchy for REF with (possibly NULL) base list
13678 BASE_LIST. For each element on BASE_LIST the TREE_PURPOSE is an
13679 access_* node, and the TREE_VALUE is the type of the base-class.
13680 Non-NULL TREE_TYPE indicates virtual inheritance. */
13682 void
13683 xref_basetypes (tree ref, tree base_list)
13685 tree *basep;
13686 tree binfo, base_binfo;
13687 unsigned max_vbases = 0; /* Maximum direct & indirect virtual bases. */
13688 unsigned max_bases = 0; /* Maximum direct bases. */
13689 unsigned max_dvbases = 0; /* Maximum direct virtual bases. */
13690 int i;
13691 tree default_access;
13692 tree igo_prev; /* Track Inheritance Graph Order. */
13694 if (ref == error_mark_node)
13695 return;
13697 /* The base of a derived class is private by default, all others are
13698 public. */
13699 default_access = (TREE_CODE (ref) == RECORD_TYPE
13700 && CLASSTYPE_DECLARED_CLASS (ref)
13701 ? access_private_node : access_public_node);
13703 /* First, make sure that any templates in base-classes are
13704 instantiated. This ensures that if we call ourselves recursively
13705 we do not get confused about which classes are marked and which
13706 are not. */
13707 basep = &base_list;
13708 while (*basep)
13710 tree basetype = TREE_VALUE (*basep);
13712 /* The dependent_type_p call below should really be dependent_scope_p
13713 so that we give a hard error about using an incomplete type as a
13714 base, but we allow it with a pedwarn for backward
13715 compatibility. */
13716 if (processing_template_decl
13717 && CLASS_TYPE_P (basetype) && TYPE_BEING_DEFINED (basetype))
13718 cxx_incomplete_type_diagnostic (NULL_TREE, basetype, DK_PEDWARN);
13719 if (!dependent_type_p (basetype)
13720 && !complete_type_or_else (basetype, NULL))
13721 /* An incomplete type. Remove it from the list. */
13722 *basep = TREE_CHAIN (*basep);
13723 else
13725 max_bases++;
13726 if (TREE_TYPE (*basep))
13727 max_dvbases++;
13728 if (CLASS_TYPE_P (basetype))
13729 max_vbases += vec_safe_length (CLASSTYPE_VBASECLASSES (basetype));
13730 basep = &TREE_CHAIN (*basep);
13733 max_vbases += max_dvbases;
13735 TYPE_MARKED_P (ref) = 1;
13737 /* The binfo slot should be empty, unless this is an (ill-formed)
13738 redefinition. */
13739 gcc_assert (!TYPE_BINFO (ref) || TYPE_SIZE (ref));
13741 gcc_assert (TYPE_MAIN_VARIANT (ref) == ref);
13743 binfo = make_tree_binfo (max_bases);
13745 TYPE_BINFO (ref) = binfo;
13746 BINFO_OFFSET (binfo) = size_zero_node;
13747 BINFO_TYPE (binfo) = ref;
13749 /* Apply base-class info set up to the variants of this type. */
13750 fixup_type_variants (ref);
13752 if (max_bases)
13754 vec_alloc (BINFO_BASE_ACCESSES (binfo), max_bases);
13755 /* A C++98 POD cannot have base classes. */
13756 CLASSTYPE_NON_LAYOUT_POD_P (ref) = true;
13758 if (TREE_CODE (ref) == UNION_TYPE)
13759 error ("derived union %qT invalid", ref);
13762 if (max_bases > 1)
13763 warning (OPT_Wmultiple_inheritance,
13764 "%qT defined with multiple direct bases", ref);
13766 if (max_vbases)
13768 /* An aggregate can't have virtual base classes. */
13769 CLASSTYPE_NON_AGGREGATE (ref) = true;
13771 vec_alloc (CLASSTYPE_VBASECLASSES (ref), max_vbases);
13773 if (max_dvbases)
13774 warning (OPT_Wvirtual_inheritance,
13775 "%qT defined with direct virtual base", ref);
13778 for (igo_prev = binfo; base_list; base_list = TREE_CHAIN (base_list))
13780 tree access = TREE_PURPOSE (base_list);
13781 int via_virtual = TREE_TYPE (base_list) != NULL_TREE;
13782 tree basetype = TREE_VALUE (base_list);
13784 if (access == access_default_node)
13785 access = default_access;
13787 /* Before C++17, an aggregate cannot have base classes. In C++17, an
13788 aggregate can't have virtual, private, or protected base classes. */
13789 if (cxx_dialect < cxx1z
13790 || access != access_public_node
13791 || via_virtual)
13792 CLASSTYPE_NON_AGGREGATE (ref) = true;
13794 if (PACK_EXPANSION_P (basetype))
13795 basetype = PACK_EXPANSION_PATTERN (basetype);
13796 if (TREE_CODE (basetype) == TYPE_DECL)
13797 basetype = TREE_TYPE (basetype);
13798 if (!MAYBE_CLASS_TYPE_P (basetype) || TREE_CODE (basetype) == UNION_TYPE)
13800 error ("base type %qT fails to be a struct or class type",
13801 basetype);
13802 goto dropped_base;
13805 base_binfo = NULL_TREE;
13806 if (CLASS_TYPE_P (basetype) && !dependent_scope_p (basetype))
13808 base_binfo = TYPE_BINFO (basetype);
13809 /* The original basetype could have been a typedef'd type. */
13810 basetype = BINFO_TYPE (base_binfo);
13812 /* Inherit flags from the base. */
13813 TYPE_HAS_NEW_OPERATOR (ref)
13814 |= TYPE_HAS_NEW_OPERATOR (basetype);
13815 TYPE_HAS_ARRAY_NEW_OPERATOR (ref)
13816 |= TYPE_HAS_ARRAY_NEW_OPERATOR (basetype);
13817 TYPE_GETS_DELETE (ref) |= TYPE_GETS_DELETE (basetype);
13818 TYPE_HAS_CONVERSION (ref) |= TYPE_HAS_CONVERSION (basetype);
13819 CLASSTYPE_DIAMOND_SHAPED_P (ref)
13820 |= CLASSTYPE_DIAMOND_SHAPED_P (basetype);
13821 CLASSTYPE_REPEATED_BASE_P (ref)
13822 |= CLASSTYPE_REPEATED_BASE_P (basetype);
13825 /* We must do this test after we've seen through a typedef
13826 type. */
13827 if (TYPE_MARKED_P (basetype))
13829 if (basetype == ref)
13830 error ("recursive type %qT undefined", basetype);
13831 else
13832 error ("duplicate base type %qT invalid", basetype);
13833 goto dropped_base;
13836 if (PACK_EXPANSION_P (TREE_VALUE (base_list)))
13837 /* Regenerate the pack expansion for the bases. */
13838 basetype = make_pack_expansion (basetype);
13840 TYPE_MARKED_P (basetype) = 1;
13842 base_binfo = copy_binfo (base_binfo, basetype, ref,
13843 &igo_prev, via_virtual);
13844 if (!BINFO_INHERITANCE_CHAIN (base_binfo))
13845 BINFO_INHERITANCE_CHAIN (base_binfo) = binfo;
13847 BINFO_BASE_APPEND (binfo, base_binfo);
13848 BINFO_BASE_ACCESS_APPEND (binfo, access);
13849 continue;
13851 dropped_base:
13852 /* Update max_vbases to reflect the reality that we are dropping
13853 this base: if it reaches zero we want to undo the vec_alloc
13854 above to avoid inconsistencies during error-recovery: eg, in
13855 build_special_member_call, CLASSTYPE_VBASECLASSES non null
13856 and vtt null (c++/27952). */
13857 if (via_virtual)
13858 max_vbases--;
13859 if (CLASS_TYPE_P (basetype))
13860 max_vbases
13861 -= vec_safe_length (CLASSTYPE_VBASECLASSES (basetype));
13864 if (CLASSTYPE_VBASECLASSES (ref)
13865 && max_vbases == 0)
13866 vec_free (CLASSTYPE_VBASECLASSES (ref));
13868 if (vec_safe_length (CLASSTYPE_VBASECLASSES (ref)) < max_vbases)
13869 /* If we didn't get max_vbases vbases, we must have shared at
13870 least one of them, and are therefore diamond shaped. */
13871 CLASSTYPE_DIAMOND_SHAPED_P (ref) = 1;
13873 /* Unmark all the types. */
13874 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
13875 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
13876 TYPE_MARKED_P (ref) = 0;
13878 /* Now see if we have a repeated base type. */
13879 if (!CLASSTYPE_REPEATED_BASE_P (ref))
13881 for (base_binfo = binfo; base_binfo;
13882 base_binfo = TREE_CHAIN (base_binfo))
13884 if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
13886 CLASSTYPE_REPEATED_BASE_P (ref) = 1;
13887 break;
13889 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 1;
13891 for (base_binfo = binfo; base_binfo;
13892 base_binfo = TREE_CHAIN (base_binfo))
13893 if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
13894 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
13895 else
13896 break;
13901 /* Copies the enum-related properties from type SRC to type DST.
13902 Used with the underlying type of an enum and the enum itself. */
13903 static void
13904 copy_type_enum (tree dst, tree src)
13906 tree t;
13907 for (t = dst; t; t = TYPE_NEXT_VARIANT (t))
13909 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (src);
13910 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (src);
13911 TYPE_SIZE (t) = TYPE_SIZE (src);
13912 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (src);
13913 SET_TYPE_MODE (dst, TYPE_MODE (src));
13914 TYPE_PRECISION (t) = TYPE_PRECISION (src);
13915 unsigned valign = TYPE_ALIGN (src);
13916 if (TYPE_USER_ALIGN (t))
13917 valign = MAX (valign, TYPE_ALIGN (t));
13918 else
13919 TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (src);
13920 SET_TYPE_ALIGN (t, valign);
13921 TYPE_UNSIGNED (t) = TYPE_UNSIGNED (src);
13925 /* Begin compiling the definition of an enumeration type.
13926 NAME is its name,
13928 if ENUMTYPE is not NULL_TREE then the type has alredy been found.
13930 UNDERLYING_TYPE is the type that will be used as the storage for
13931 the enumeration type. This should be NULL_TREE if no storage type
13932 was specified.
13934 ATTRIBUTES are any attributes specified after the enum-key.
13936 SCOPED_ENUM_P is true if this is a scoped enumeration type.
13938 if IS_NEW is not NULL, gets TRUE iff a new type is created.
13940 Returns the type object, as yet incomplete.
13941 Also records info about it so that build_enumerator
13942 may be used to declare the individual values as they are read. */
13944 tree
13945 start_enum (tree name, tree enumtype, tree underlying_type,
13946 tree attributes, bool scoped_enum_p, bool *is_new)
13948 tree prevtype = NULL_TREE;
13949 gcc_assert (identifier_p (name));
13951 if (is_new)
13952 *is_new = false;
13953 /* [C++0x dcl.enum]p5:
13955 If not explicitly specified, the underlying type of a scoped
13956 enumeration type is int. */
13957 if (!underlying_type && scoped_enum_p)
13958 underlying_type = integer_type_node;
13960 if (underlying_type)
13961 underlying_type = cv_unqualified (underlying_type);
13963 /* If this is the real definition for a previous forward reference,
13964 fill in the contents in the same object that used to be the
13965 forward reference. */
13966 if (!enumtype)
13967 enumtype = lookup_and_check_tag (enum_type, name,
13968 /*tag_scope=*/ts_current,
13969 /*template_header_p=*/false);
13971 /* In case of a template_decl, the only check that should be deferred
13972 to instantiation time is the comparison of underlying types. */
13973 if (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE)
13975 if (scoped_enum_p != SCOPED_ENUM_P (enumtype))
13977 error_at (input_location, "scoped/unscoped mismatch "
13978 "in enum %q#T", enumtype);
13979 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
13980 "previous definition here");
13981 enumtype = error_mark_node;
13983 else if (ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) != !! underlying_type)
13985 error_at (input_location, "underlying type mismatch "
13986 "in enum %q#T", enumtype);
13987 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
13988 "previous definition here");
13989 enumtype = error_mark_node;
13991 else if (underlying_type && ENUM_UNDERLYING_TYPE (enumtype)
13992 && !dependent_type_p (underlying_type)
13993 && !dependent_type_p (ENUM_UNDERLYING_TYPE (enumtype))
13994 && !same_type_p (underlying_type,
13995 ENUM_UNDERLYING_TYPE (enumtype)))
13997 error_at (input_location, "different underlying type "
13998 "in enum %q#T", enumtype);
13999 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
14000 "previous definition here");
14001 underlying_type = NULL_TREE;
14005 if (!enumtype || TREE_CODE (enumtype) != ENUMERAL_TYPE
14006 || processing_template_decl)
14008 /* In case of error, make a dummy enum to allow parsing to
14009 continue. */
14010 if (enumtype == error_mark_node)
14012 name = make_anon_name ();
14013 enumtype = NULL_TREE;
14016 /* enumtype may be an ENUMERAL_TYPE if this is a redefinition
14017 of an opaque enum, or an opaque enum of an already defined
14018 enumeration (C++0x only).
14019 In any other case, it'll be NULL_TREE. */
14020 if (!enumtype)
14022 if (is_new)
14023 *is_new = true;
14025 prevtype = enumtype;
14027 /* Do not push the decl more than once, unless we need to
14028 compare underlying types at instantiation time */
14029 if (!enumtype
14030 || TREE_CODE (enumtype) != ENUMERAL_TYPE
14031 || (underlying_type
14032 && dependent_type_p (underlying_type))
14033 || (ENUM_UNDERLYING_TYPE (enumtype)
14034 && dependent_type_p (ENUM_UNDERLYING_TYPE (enumtype))))
14036 enumtype = cxx_make_type (ENUMERAL_TYPE);
14037 enumtype = pushtag (name, enumtype, /*tag_scope=*/ts_current);
14039 /* std::byte aliases anything. */
14040 if (enumtype != error_mark_node
14041 && TYPE_CONTEXT (enumtype) == std_node
14042 && !strcmp ("byte", TYPE_NAME_STRING (enumtype)))
14043 TYPE_ALIAS_SET (enumtype) = 0;
14045 else
14046 enumtype = xref_tag (enum_type, name, /*tag_scope=*/ts_current,
14047 false);
14049 if (enumtype == error_mark_node)
14050 return error_mark_node;
14052 /* The enum is considered opaque until the opening '{' of the
14053 enumerator list. */
14054 SET_OPAQUE_ENUM_P (enumtype, true);
14055 ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) = !! underlying_type;
14058 SET_SCOPED_ENUM_P (enumtype, scoped_enum_p);
14060 cplus_decl_attributes (&enumtype, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
14062 if (underlying_type)
14064 if (ENUM_UNDERLYING_TYPE (enumtype))
14065 /* We already checked that it matches, don't change it to a different
14066 typedef variant. */;
14067 else if (CP_INTEGRAL_TYPE_P (underlying_type))
14069 copy_type_enum (enumtype, underlying_type);
14070 ENUM_UNDERLYING_TYPE (enumtype) = underlying_type;
14072 else if (dependent_type_p (underlying_type))
14073 ENUM_UNDERLYING_TYPE (enumtype) = underlying_type;
14074 else
14075 error ("underlying type %qT of %qT must be an integral type",
14076 underlying_type, enumtype);
14079 /* If into a template class, the returned enum is always the first
14080 declaration (opaque or not) seen. This way all the references to
14081 this type will be to the same declaration. The following ones are used
14082 only to check for definition errors. */
14083 if (prevtype && processing_template_decl)
14084 return prevtype;
14085 else
14086 return enumtype;
14089 /* After processing and defining all the values of an enumeration type,
14090 install their decls in the enumeration type.
14091 ENUMTYPE is the type object. */
14093 void
14094 finish_enum_value_list (tree enumtype)
14096 tree values;
14097 tree underlying_type;
14098 tree decl;
14099 tree value;
14100 tree minnode, maxnode;
14101 tree t;
14103 bool fixed_underlying_type_p
14104 = ENUM_UNDERLYING_TYPE (enumtype) != NULL_TREE;
14106 /* We built up the VALUES in reverse order. */
14107 TYPE_VALUES (enumtype) = nreverse (TYPE_VALUES (enumtype));
14109 /* For an enum defined in a template, just set the type of the values;
14110 all further processing is postponed until the template is
14111 instantiated. We need to set the type so that tsubst of a CONST_DECL
14112 works. */
14113 if (processing_template_decl)
14115 for (values = TYPE_VALUES (enumtype);
14116 values;
14117 values = TREE_CHAIN (values))
14118 TREE_TYPE (TREE_VALUE (values)) = enumtype;
14119 return;
14122 /* Determine the minimum and maximum values of the enumerators. */
14123 if (TYPE_VALUES (enumtype))
14125 minnode = maxnode = NULL_TREE;
14127 for (values = TYPE_VALUES (enumtype);
14128 values;
14129 values = TREE_CHAIN (values))
14131 decl = TREE_VALUE (values);
14133 /* [dcl.enum]: Following the closing brace of an enum-specifier,
14134 each enumerator has the type of its enumeration. Prior to the
14135 closing brace, the type of each enumerator is the type of its
14136 initializing value. */
14137 TREE_TYPE (decl) = enumtype;
14139 /* Update the minimum and maximum values, if appropriate. */
14140 value = DECL_INITIAL (decl);
14141 if (value == error_mark_node)
14142 value = integer_zero_node;
14143 /* Figure out what the minimum and maximum values of the
14144 enumerators are. */
14145 if (!minnode)
14146 minnode = maxnode = value;
14147 else if (tree_int_cst_lt (maxnode, value))
14148 maxnode = value;
14149 else if (tree_int_cst_lt (value, minnode))
14150 minnode = value;
14153 else
14154 /* [dcl.enum]
14156 If the enumerator-list is empty, the underlying type is as if
14157 the enumeration had a single enumerator with value 0. */
14158 minnode = maxnode = integer_zero_node;
14160 if (!fixed_underlying_type_p)
14162 /* Compute the number of bits require to represent all values of the
14163 enumeration. We must do this before the type of MINNODE and
14164 MAXNODE are transformed, since tree_int_cst_min_precision relies
14165 on the TREE_TYPE of the value it is passed. */
14166 signop sgn = tree_int_cst_sgn (minnode) >= 0 ? UNSIGNED : SIGNED;
14167 int lowprec = tree_int_cst_min_precision (minnode, sgn);
14168 int highprec = tree_int_cst_min_precision (maxnode, sgn);
14169 int precision = MAX (lowprec, highprec);
14170 unsigned int itk;
14171 bool use_short_enum;
14173 /* Determine the underlying type of the enumeration.
14175 [dcl.enum]
14177 The underlying type of an enumeration is an integral type that
14178 can represent all the enumerator values defined in the
14179 enumeration. It is implementation-defined which integral type is
14180 used as the underlying type for an enumeration except that the
14181 underlying type shall not be larger than int unless the value of
14182 an enumerator cannot fit in an int or unsigned int.
14184 We use "int" or an "unsigned int" as the underlying type, even if
14185 a smaller integral type would work, unless the user has
14186 explicitly requested that we use the smallest possible type. The
14187 user can request that for all enumerations with a command line
14188 flag, or for just one enumeration with an attribute. */
14190 use_short_enum = flag_short_enums
14191 || lookup_attribute ("packed", TYPE_ATTRIBUTES (enumtype));
14193 /* If the precision of the type was specified with an attribute and it
14194 was too small, give an error. Otherwise, use it. */
14195 if (TYPE_PRECISION (enumtype))
14197 if (precision > TYPE_PRECISION (enumtype))
14198 error ("specified mode too small for enumeral values");
14199 else
14201 use_short_enum = true;
14202 precision = TYPE_PRECISION (enumtype);
14206 for (itk = (use_short_enum ? itk_char : itk_int);
14207 itk != itk_none;
14208 itk++)
14210 underlying_type = integer_types[itk];
14211 if (underlying_type != NULL_TREE
14212 && TYPE_PRECISION (underlying_type) >= precision
14213 && TYPE_SIGN (underlying_type) == sgn)
14214 break;
14216 if (itk == itk_none)
14218 /* DR 377
14220 IF no integral type can represent all the enumerator values, the
14221 enumeration is ill-formed. */
14222 error ("no integral type can represent all of the enumerator values "
14223 "for %qT", enumtype);
14224 precision = TYPE_PRECISION (long_long_integer_type_node);
14225 underlying_type = integer_types[itk_unsigned_long_long];
14228 /* [dcl.enum]
14230 The value of sizeof() applied to an enumeration type, an object
14231 of an enumeration type, or an enumerator, is the value of sizeof()
14232 applied to the underlying type. */
14233 copy_type_enum (enumtype, underlying_type);
14235 /* Compute the minimum and maximum values for the type.
14237 [dcl.enum]
14239 For an enumeration where emin is the smallest enumerator and emax
14240 is the largest, the values of the enumeration are the values of the
14241 underlying type in the range bmin to bmax, where bmin and bmax are,
14242 respectively, the smallest and largest values of the smallest bit-
14243 field that can store emin and emax. */
14245 /* The middle-end currently assumes that types with TYPE_PRECISION
14246 narrower than their underlying type are suitably zero or sign
14247 extended to fill their mode. Similarly, it assumes that the front
14248 end assures that a value of a particular type must be within
14249 TYPE_MIN_VALUE and TYPE_MAX_VALUE.
14251 We used to set these fields based on bmin and bmax, but that led
14252 to invalid assumptions like optimizing away bounds checking. So
14253 now we just set the TYPE_PRECISION, TYPE_MIN_VALUE, and
14254 TYPE_MAX_VALUE to the values for the mode above and only restrict
14255 the ENUM_UNDERLYING_TYPE for the benefit of diagnostics. */
14256 ENUM_UNDERLYING_TYPE (enumtype)
14257 = build_distinct_type_copy (underlying_type);
14258 TYPE_PRECISION (ENUM_UNDERLYING_TYPE (enumtype)) = precision;
14259 set_min_and_max_values_for_integral_type
14260 (ENUM_UNDERLYING_TYPE (enumtype), precision, sgn);
14262 /* If -fstrict-enums, still constrain TYPE_MIN/MAX_VALUE. */
14263 if (flag_strict_enums)
14264 set_min_and_max_values_for_integral_type (enumtype, precision, sgn);
14266 else
14267 underlying_type = ENUM_UNDERLYING_TYPE (enumtype);
14269 /* Convert each of the enumerators to the type of the underlying
14270 type of the enumeration. */
14271 for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values))
14273 location_t saved_location;
14275 decl = TREE_VALUE (values);
14276 saved_location = input_location;
14277 input_location = DECL_SOURCE_LOCATION (decl);
14278 if (fixed_underlying_type_p)
14279 /* If the enumeration type has a fixed underlying type, we
14280 already checked all of the enumerator values. */
14281 value = DECL_INITIAL (decl);
14282 else
14283 value = perform_implicit_conversion (underlying_type,
14284 DECL_INITIAL (decl),
14285 tf_warning_or_error);
14286 input_location = saved_location;
14288 /* Do not clobber shared ints. */
14289 if (value != error_mark_node)
14291 value = copy_node (value);
14293 TREE_TYPE (value) = enumtype;
14295 DECL_INITIAL (decl) = value;
14298 /* Fix up all variant types of this enum type. */
14299 for (t = TYPE_MAIN_VARIANT (enumtype); t; t = TYPE_NEXT_VARIANT (t))
14300 TYPE_VALUES (t) = TYPE_VALUES (enumtype);
14302 if (at_class_scope_p ()
14303 && COMPLETE_TYPE_P (current_class_type)
14304 && UNSCOPED_ENUM_P (enumtype))
14306 insert_late_enum_def_into_classtype_sorted_fields (enumtype,
14307 current_class_type);
14308 fixup_type_variants (current_class_type);
14311 /* Finish debugging output for this type. */
14312 rest_of_type_compilation (enumtype, namespace_bindings_p ());
14314 /* Each enumerator now has the type of its enumeration. Clear the cache
14315 so that this change in types doesn't confuse us later on. */
14316 clear_cv_and_fold_caches ();
14319 /* Finishes the enum type. This is called only the first time an
14320 enumeration is seen, be it opaque or odinary.
14321 ENUMTYPE is the type object. */
14323 void
14324 finish_enum (tree enumtype)
14326 if (processing_template_decl)
14328 if (at_function_scope_p ())
14329 add_stmt (build_min (TAG_DEFN, enumtype));
14330 return;
14333 /* If this is a forward declaration, there should not be any variants,
14334 though we can get a variant in the middle of an enum-specifier with
14335 wacky code like 'enum E { e = sizeof(const E*) };' */
14336 gcc_assert (enumtype == TYPE_MAIN_VARIANT (enumtype)
14337 && (TYPE_VALUES (enumtype)
14338 || !TYPE_NEXT_VARIANT (enumtype)));
14341 /* Build and install a CONST_DECL for an enumeration constant of the
14342 enumeration type ENUMTYPE whose NAME and VALUE (if any) are provided.
14343 Apply ATTRIBUTES if available. LOC is the location of NAME.
14344 Assignment of sequential values by default is handled here. */
14346 void
14347 build_enumerator (tree name, tree value, tree enumtype, tree attributes,
14348 location_t loc)
14350 tree decl;
14351 tree context;
14352 tree type;
14354 /* scalar_constant_value will pull out this expression, so make sure
14355 it's folded as appropriate. */
14356 if (processing_template_decl)
14357 value = fold_non_dependent_expr (value);
14359 /* If the VALUE was erroneous, pretend it wasn't there; that will
14360 result in the enum being assigned the next value in sequence. */
14361 if (value == error_mark_node)
14362 value = NULL_TREE;
14364 /* Remove no-op casts from the value. */
14365 if (value)
14366 STRIP_TYPE_NOPS (value);
14368 if (! processing_template_decl)
14370 /* Validate and default VALUE. */
14371 if (value != NULL_TREE)
14373 if (!ENUM_UNDERLYING_TYPE (enumtype))
14375 tree tmp_value = build_expr_type_conversion (WANT_INT | WANT_ENUM,
14376 value, true);
14377 if (tmp_value)
14378 value = tmp_value;
14380 else if (! INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P
14381 (TREE_TYPE (value)))
14382 value = perform_implicit_conversion_flags
14383 (ENUM_UNDERLYING_TYPE (enumtype), value, tf_warning_or_error,
14384 LOOKUP_IMPLICIT | LOOKUP_NO_NARROWING);
14386 if (value == error_mark_node)
14387 value = NULL_TREE;
14389 if (value != NULL_TREE)
14391 if (! INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P
14392 (TREE_TYPE (value)))
14394 error ("enumerator value for %qD must have integral or "
14395 "unscoped enumeration type", name);
14396 value = NULL_TREE;
14398 else
14400 value = cxx_constant_value (value);
14402 if (TREE_CODE (value) != INTEGER_CST)
14404 error ("enumerator value for %qD is not an integer "
14405 "constant", name);
14406 value = NULL_TREE;
14412 /* Default based on previous value. */
14413 if (value == NULL_TREE)
14415 if (TYPE_VALUES (enumtype))
14417 tree prev_value;
14418 bool overflowed;
14420 /* C++03 7.2/4: If no initializer is specified for the first
14421 enumerator, the type is an unspecified integral
14422 type. Otherwise the type is the same as the type of the
14423 initializing value of the preceding enumerator unless the
14424 incremented value is not representable in that type, in
14425 which case the type is an unspecified integral type
14426 sufficient to contain the incremented value. */
14427 prev_value = DECL_INITIAL (TREE_VALUE (TYPE_VALUES (enumtype)));
14428 if (error_operand_p (prev_value))
14429 value = error_mark_node;
14430 else
14432 tree type = TREE_TYPE (prev_value);
14433 signop sgn = TYPE_SIGN (type);
14434 widest_int wi = wi::add (wi::to_widest (prev_value), 1, sgn,
14435 &overflowed);
14436 if (!overflowed)
14438 bool pos = !wi::neg_p (wi, sgn);
14439 if (!wi::fits_to_tree_p (wi, type))
14441 unsigned int itk;
14442 for (itk = itk_int; itk != itk_none; itk++)
14444 type = integer_types[itk];
14445 if (type != NULL_TREE
14446 && (pos || !TYPE_UNSIGNED (type))
14447 && wi::fits_to_tree_p (wi, type))
14448 break;
14450 if (type && cxx_dialect < cxx11
14451 && itk > itk_unsigned_long)
14452 pedwarn (input_location, OPT_Wlong_long,
14453 pos ? G_("\
14454 incremented enumerator value is too large for %<unsigned long%>") : G_("\
14455 incremented enumerator value is too large for %<long%>"));
14457 if (type == NULL_TREE)
14458 overflowed = true;
14459 else
14460 value = wide_int_to_tree (type, wi);
14463 if (overflowed)
14465 error ("overflow in enumeration values at %qD", name);
14466 value = error_mark_node;
14470 else
14471 value = integer_zero_node;
14474 /* Remove no-op casts from the value. */
14475 STRIP_TYPE_NOPS (value);
14477 /* If the underlying type of the enum is fixed, check whether
14478 the enumerator values fits in the underlying type. If it
14479 does not fit, the program is ill-formed [C++0x dcl.enum]. */
14480 if (ENUM_UNDERLYING_TYPE (enumtype)
14481 && value
14482 && TREE_CODE (value) == INTEGER_CST)
14484 if (!int_fits_type_p (value, ENUM_UNDERLYING_TYPE (enumtype)))
14485 error ("enumerator value %qE is outside the range of underlying "
14486 "type %qT", value, ENUM_UNDERLYING_TYPE (enumtype));
14488 /* Convert the value to the appropriate type. */
14489 value = fold_convert (ENUM_UNDERLYING_TYPE (enumtype), value);
14493 /* C++ associates enums with global, function, or class declarations. */
14494 context = current_scope ();
14496 /* Build the actual enumeration constant. Note that the enumeration
14497 constants have the underlying type of the enum (if it is fixed)
14498 or the type of their initializer (if the underlying type of the
14499 enum is not fixed):
14501 [ C++0x dcl.enum ]
14503 If the underlying type is fixed, the type of each enumerator
14504 prior to the closing brace is the underlying type; if the
14505 initializing value of an enumerator cannot be represented by
14506 the underlying type, the program is ill-formed. If the
14507 underlying type is not fixed, the type of each enumerator is
14508 the type of its initializing value.
14510 If the underlying type is not fixed, it will be computed by
14511 finish_enum and we will reset the type of this enumerator. Of
14512 course, if we're processing a template, there may be no value. */
14513 type = value ? TREE_TYPE (value) : NULL_TREE;
14515 decl = build_decl (loc, CONST_DECL, name, type);
14517 DECL_CONTEXT (decl) = enumtype;
14518 TREE_CONSTANT (decl) = 1;
14519 TREE_READONLY (decl) = 1;
14520 DECL_INITIAL (decl) = value;
14522 if (attributes)
14523 cplus_decl_attributes (&decl, attributes, 0);
14525 if (context && context == current_class_type && !SCOPED_ENUM_P (enumtype))
14527 /* In something like `struct S { enum E { i = 7 }; };' we put `i'
14528 on the TYPE_FIELDS list for `S'. (That's so that you can say
14529 things like `S::i' later.) */
14531 /* The enumerator may be getting declared outside of its enclosing
14532 class, like so:
14534 class S { public: enum E : int; }; enum S::E : int { i = 7; };
14536 For which case we need to make sure that the access of `S::i'
14537 matches the access of `S::E'. */
14538 tree saved_cas = current_access_specifier;
14539 if (TREE_PRIVATE (TYPE_NAME (enumtype)))
14540 current_access_specifier = access_private_node;
14541 else if (TREE_PROTECTED (TYPE_NAME (enumtype)))
14542 current_access_specifier = access_protected_node;
14543 else
14544 current_access_specifier = access_public_node;
14546 finish_member_declaration (decl);
14548 current_access_specifier = saved_cas;
14550 else
14551 pushdecl (decl);
14553 /* Add this enumeration constant to the list for this type. */
14554 TYPE_VALUES (enumtype) = tree_cons (name, decl, TYPE_VALUES (enumtype));
14557 /* Look for an enumerator with the given NAME within the enumeration
14558 type ENUMTYPE. This routine is used primarily for qualified name
14559 lookup into an enumerator in C++0x, e.g.,
14561 enum class Color { Red, Green, Blue };
14563 Color color = Color::Red;
14565 Returns the value corresponding to the enumerator, or
14566 NULL_TREE if no such enumerator was found. */
14567 tree
14568 lookup_enumerator (tree enumtype, tree name)
14570 tree e;
14571 gcc_assert (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE);
14573 e = purpose_member (name, TYPE_VALUES (enumtype));
14574 return e? TREE_VALUE (e) : NULL_TREE;
14578 /* We're defining DECL. Make sure that its type is OK. */
14580 static void
14581 check_function_type (tree decl, tree current_function_parms)
14583 tree fntype = TREE_TYPE (decl);
14584 tree return_type = complete_type (TREE_TYPE (fntype));
14586 /* In a function definition, arg types must be complete. */
14587 require_complete_types_for_parms (current_function_parms);
14589 if (dependent_type_p (return_type)
14590 || type_uses_auto (return_type))
14591 return;
14592 if (!COMPLETE_OR_VOID_TYPE_P (return_type))
14594 tree args = TYPE_ARG_TYPES (fntype);
14596 error ("return type %q#T is incomplete", return_type);
14598 /* Make it return void instead. */
14599 if (TREE_CODE (fntype) == METHOD_TYPE)
14600 fntype = build_method_type_directly (TREE_TYPE (TREE_VALUE (args)),
14601 void_type_node,
14602 TREE_CHAIN (args));
14603 else
14604 fntype = build_function_type (void_type_node, args);
14605 fntype
14606 = build_exception_variant (fntype,
14607 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl)));
14608 fntype = (cp_build_type_attribute_variant
14609 (fntype, TYPE_ATTRIBUTES (TREE_TYPE (decl))));
14610 TREE_TYPE (decl) = fntype;
14612 else
14614 abstract_virtuals_error (decl, TREE_TYPE (fntype));
14615 maybe_warn_parm_abi (TREE_TYPE (fntype),
14616 DECL_SOURCE_LOCATION (decl));
14620 /* True iff FN is an implicitly-defined default constructor. */
14622 static bool
14623 implicit_default_ctor_p (tree fn)
14625 return (DECL_CONSTRUCTOR_P (fn)
14626 && !user_provided_p (fn)
14627 && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn)));
14630 /* Clobber the contents of *this to let the back end know that the object
14631 storage is dead when we enter the constructor or leave the destructor. */
14633 static tree
14634 build_clobber_this ()
14636 /* Clobbering an empty base is pointless, and harmful if its one byte
14637 TYPE_SIZE overlays real data. */
14638 if (is_empty_class (current_class_type))
14639 return void_node;
14641 /* If we have virtual bases, clobber the whole object, but only if we're in
14642 charge. If we don't have virtual bases, clobber the as-base type so we
14643 don't mess with tail padding. */
14644 bool vbases = CLASSTYPE_VBASECLASSES (current_class_type);
14646 tree ctype = current_class_type;
14647 if (!vbases)
14648 ctype = CLASSTYPE_AS_BASE (ctype);
14650 tree clobber = build_constructor (ctype, NULL);
14651 TREE_THIS_VOLATILE (clobber) = true;
14653 tree thisref = current_class_ref;
14654 if (ctype != current_class_type)
14656 thisref = build_nop (build_reference_type (ctype), current_class_ptr);
14657 thisref = convert_from_reference (thisref);
14660 tree exprstmt = build2 (MODIFY_EXPR, void_type_node, thisref, clobber);
14661 if (vbases)
14662 exprstmt = build_if_in_charge (exprstmt);
14664 return exprstmt;
14667 /* Create the FUNCTION_DECL for a function definition.
14668 DECLSPECS and DECLARATOR are the parts of the declaration;
14669 they describe the function's name and the type it returns,
14670 but twisted together in a fashion that parallels the syntax of C.
14672 FLAGS is a bitwise or of SF_PRE_PARSED (indicating that the
14673 DECLARATOR is really the DECL for the function we are about to
14674 process and that DECLSPECS should be ignored), SF_INCLASS_INLINE
14675 indicating that the function is an inline defined in-class.
14677 This function creates a binding context for the function body
14678 as well as setting up the FUNCTION_DECL in current_function_decl.
14680 For C++, we must first check whether that datum makes any sense.
14681 For example, "class A local_a(1,2);" means that variable local_a
14682 is an aggregate of type A, which should have a constructor
14683 applied to it with the argument list [1, 2].
14685 On entry, DECL_INITIAL (decl1) should be NULL_TREE or error_mark_node,
14686 or may be a BLOCK if the function has been defined previously
14687 in this translation unit. On exit, DECL_INITIAL (decl1) will be
14688 error_mark_node if the function has never been defined, or
14689 a BLOCK if the function has been defined somewhere. */
14691 bool
14692 start_preparsed_function (tree decl1, tree attrs, int flags)
14694 tree ctype = NULL_TREE;
14695 tree fntype;
14696 tree restype;
14697 int doing_friend = 0;
14698 cp_binding_level *bl;
14699 tree current_function_parms;
14700 struct c_fileinfo *finfo
14701 = get_fileinfo (LOCATION_FILE (DECL_SOURCE_LOCATION (decl1)));
14702 bool honor_interface;
14704 /* Sanity check. */
14705 gcc_assert (VOID_TYPE_P (TREE_VALUE (void_list_node)));
14706 gcc_assert (TREE_CHAIN (void_list_node) == NULL_TREE);
14708 fntype = TREE_TYPE (decl1);
14709 if (TREE_CODE (fntype) == METHOD_TYPE)
14710 ctype = TYPE_METHOD_BASETYPE (fntype);
14712 /* ISO C++ 11.4/5. A friend function defined in a class is in
14713 the (lexical) scope of the class in which it is defined. */
14714 if (!ctype && DECL_FRIEND_P (decl1))
14716 ctype = DECL_FRIEND_CONTEXT (decl1);
14718 /* CTYPE could be null here if we're dealing with a template;
14719 for example, `inline friend float foo()' inside a template
14720 will have no CTYPE set. */
14721 if (ctype && TREE_CODE (ctype) != RECORD_TYPE)
14722 ctype = NULL_TREE;
14723 else
14724 doing_friend = 1;
14727 if (DECL_DECLARED_INLINE_P (decl1)
14728 && lookup_attribute ("noinline", attrs))
14729 warning_at (DECL_SOURCE_LOCATION (decl1), 0,
14730 "inline function %qD given attribute noinline", decl1);
14732 /* Handle gnu_inline attribute. */
14733 if (GNU_INLINE_P (decl1))
14735 DECL_EXTERNAL (decl1) = 1;
14736 DECL_NOT_REALLY_EXTERN (decl1) = 0;
14737 DECL_INTERFACE_KNOWN (decl1) = 1;
14738 DECL_DISREGARD_INLINE_LIMITS (decl1) = 1;
14741 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl1))
14742 /* This is a constructor, we must ensure that any default args
14743 introduced by this definition are propagated to the clones
14744 now. The clones are used directly in overload resolution. */
14745 adjust_clone_args (decl1);
14747 /* Sometimes we don't notice that a function is a static member, and
14748 build a METHOD_TYPE for it. Fix that up now. */
14749 gcc_assert (!(ctype != NULL_TREE && DECL_STATIC_FUNCTION_P (decl1)
14750 && TREE_CODE (TREE_TYPE (decl1)) == METHOD_TYPE));
14752 /* Set up current_class_type, and enter the scope of the class, if
14753 appropriate. */
14754 if (ctype)
14755 push_nested_class (ctype);
14756 else if (DECL_STATIC_FUNCTION_P (decl1))
14757 push_nested_class (DECL_CONTEXT (decl1));
14759 /* Now that we have entered the scope of the class, we must restore
14760 the bindings for any template parameters surrounding DECL1, if it
14761 is an inline member template. (Order is important; consider the
14762 case where a template parameter has the same name as a field of
14763 the class.) It is not until after this point that
14764 PROCESSING_TEMPLATE_DECL is guaranteed to be set up correctly. */
14765 if (flags & SF_INCLASS_INLINE)
14766 maybe_begin_member_template_processing (decl1);
14768 /* Effective C++ rule 15. */
14769 if (warn_ecpp
14770 && DECL_OVERLOADED_OPERATOR_P (decl1) == NOP_EXPR
14771 && VOID_TYPE_P (TREE_TYPE (fntype)))
14772 warning (OPT_Weffc__, "%<operator=%> should return a reference to %<*this%>");
14774 /* Make the init_value nonzero so pushdecl knows this is not tentative.
14775 error_mark_node is replaced below (in poplevel) with the BLOCK. */
14776 if (!DECL_INITIAL (decl1))
14777 DECL_INITIAL (decl1) = error_mark_node;
14779 /* This function exists in static storage.
14780 (This does not mean `static' in the C sense!) */
14781 TREE_STATIC (decl1) = 1;
14783 /* We must call push_template_decl after current_class_type is set
14784 up. (If we are processing inline definitions after exiting a
14785 class scope, current_class_type will be NULL_TREE until set above
14786 by push_nested_class.) */
14787 if (processing_template_decl)
14789 tree newdecl1 = push_template_decl (decl1);
14790 if (newdecl1 == error_mark_node)
14792 if (ctype || DECL_STATIC_FUNCTION_P (decl1))
14793 pop_nested_class ();
14794 return false;
14796 decl1 = newdecl1;
14799 /* Make sure the parameter and return types are reasonable. When
14800 you declare a function, these types can be incomplete, but they
14801 must be complete when you define the function. */
14802 check_function_type (decl1, DECL_ARGUMENTS (decl1));
14804 /* Build the return declaration for the function. */
14805 restype = TREE_TYPE (fntype);
14807 if (DECL_RESULT (decl1) == NULL_TREE)
14809 tree resdecl;
14811 resdecl = build_decl (input_location, RESULT_DECL, 0, restype);
14812 DECL_ARTIFICIAL (resdecl) = 1;
14813 DECL_IGNORED_P (resdecl) = 1;
14814 DECL_RESULT (decl1) = resdecl;
14816 cp_apply_type_quals_to_decl (cp_type_quals (restype), resdecl);
14819 /* Record the decl so that the function name is defined.
14820 If we already have a decl for this name, and it is a FUNCTION_DECL,
14821 use the old decl. */
14822 if (!processing_template_decl && !(flags & SF_PRE_PARSED))
14824 /* A specialization is not used to guide overload resolution. */
14825 if (!DECL_FUNCTION_MEMBER_P (decl1)
14826 && !(DECL_USE_TEMPLATE (decl1) &&
14827 PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl1))))
14829 tree olddecl = pushdecl (decl1);
14831 if (olddecl == error_mark_node)
14832 /* If something went wrong when registering the declaration,
14833 use DECL1; we have to have a FUNCTION_DECL to use when
14834 parsing the body of the function. */
14836 else
14838 /* Otherwise, OLDDECL is either a previous declaration
14839 of the same function or DECL1 itself. */
14841 if (warn_missing_declarations
14842 && olddecl == decl1
14843 && !DECL_MAIN_P (decl1)
14844 && TREE_PUBLIC (decl1)
14845 && !DECL_DECLARED_INLINE_P (decl1))
14847 tree context;
14849 /* Check whether DECL1 is in an anonymous
14850 namespace. */
14851 for (context = DECL_CONTEXT (decl1);
14852 context;
14853 context = DECL_CONTEXT (context))
14855 if (TREE_CODE (context) == NAMESPACE_DECL
14856 && DECL_NAME (context) == NULL_TREE)
14857 break;
14860 if (context == NULL)
14861 warning_at (DECL_SOURCE_LOCATION (decl1),
14862 OPT_Wmissing_declarations,
14863 "no previous declaration for %qD", decl1);
14866 decl1 = olddecl;
14869 else
14871 /* We need to set the DECL_CONTEXT. */
14872 if (!DECL_CONTEXT (decl1) && DECL_TEMPLATE_INFO (decl1))
14873 DECL_CONTEXT (decl1) = DECL_CONTEXT (DECL_TI_TEMPLATE (decl1));
14875 fntype = TREE_TYPE (decl1);
14876 restype = TREE_TYPE (fntype);
14878 /* If #pragma weak applies, mark the decl appropriately now.
14879 The pragma only applies to global functions. Because
14880 determining whether or not the #pragma applies involves
14881 computing the mangled name for the declaration, we cannot
14882 apply the pragma until after we have merged this declaration
14883 with any previous declarations; if the original declaration
14884 has a linkage specification, that specification applies to
14885 the definition as well, and may affect the mangled name. */
14886 if (DECL_FILE_SCOPE_P (decl1))
14887 maybe_apply_pragma_weak (decl1);
14890 /* We are now in the scope of the function being defined. */
14891 current_function_decl = decl1;
14893 /* Save the parm names or decls from this function's declarator
14894 where store_parm_decls will find them. */
14895 current_function_parms = DECL_ARGUMENTS (decl1);
14897 /* Let the user know we're compiling this function. */
14898 announce_function (decl1);
14900 gcc_assert (DECL_INITIAL (decl1));
14902 /* This function may already have been parsed, in which case just
14903 return; our caller will skip over the body without parsing. */
14904 if (DECL_INITIAL (decl1) != error_mark_node)
14905 return true;
14907 /* Initialize RTL machinery. We cannot do this until
14908 CURRENT_FUNCTION_DECL and DECL_RESULT are set up. We do this
14909 even when processing a template; this is how we get
14910 CFUN set up, and our per-function variables initialized.
14911 FIXME factor out the non-RTL stuff. */
14912 bl = current_binding_level;
14913 allocate_struct_function (decl1, processing_template_decl);
14915 /* Initialize the language data structures. Whenever we start
14916 a new function, we destroy temporaries in the usual way. */
14917 cfun->language = ggc_cleared_alloc<language_function> ();
14918 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
14919 current_binding_level = bl;
14921 if (!processing_template_decl && type_uses_auto (restype))
14923 FNDECL_USED_AUTO (decl1) = true;
14924 current_function_auto_return_pattern = restype;
14927 /* Start the statement-tree, start the tree now. */
14928 DECL_SAVED_TREE (decl1) = push_stmt_list ();
14930 /* If we are (erroneously) defining a function that we have already
14931 defined before, wipe out what we knew before. */
14932 if (!DECL_PENDING_INLINE_P (decl1))
14933 DECL_SAVED_FUNCTION_DATA (decl1) = NULL;
14935 if (ctype && !doing_friend && !DECL_STATIC_FUNCTION_P (decl1))
14937 /* We know that this was set up by `grokclassfn'. We do not
14938 wait until `store_parm_decls', since evil parse errors may
14939 never get us to that point. Here we keep the consistency
14940 between `current_class_type' and `current_class_ptr'. */
14941 tree t = DECL_ARGUMENTS (decl1);
14943 gcc_assert (t != NULL_TREE && TREE_CODE (t) == PARM_DECL);
14944 gcc_assert (TYPE_PTR_P (TREE_TYPE (t)));
14946 cp_function_chain->x_current_class_ref
14947 = cp_build_indirect_ref (t, RO_NULL, tf_warning_or_error);
14948 /* Set this second to avoid shortcut in cp_build_indirect_ref. */
14949 cp_function_chain->x_current_class_ptr = t;
14951 /* Constructors and destructors need to know whether they're "in
14952 charge" of initializing virtual base classes. */
14953 t = DECL_CHAIN (t);
14954 if (DECL_HAS_IN_CHARGE_PARM_P (decl1))
14956 current_in_charge_parm = t;
14957 t = DECL_CHAIN (t);
14959 if (DECL_HAS_VTT_PARM_P (decl1))
14961 gcc_assert (DECL_NAME (t) == vtt_parm_identifier);
14962 current_vtt_parm = t;
14966 honor_interface = (!DECL_TEMPLATE_INSTANTIATION (decl1)
14967 /* Implicitly-defined methods (like the
14968 destructor for a class in which no destructor
14969 is explicitly declared) must not be defined
14970 until their definition is needed. So, we
14971 ignore interface specifications for
14972 compiler-generated functions. */
14973 && !DECL_ARTIFICIAL (decl1));
14975 if (processing_template_decl)
14976 /* Don't mess with interface flags. */;
14977 else if (DECL_INTERFACE_KNOWN (decl1))
14979 tree ctx = decl_function_context (decl1);
14981 if (DECL_NOT_REALLY_EXTERN (decl1))
14982 DECL_EXTERNAL (decl1) = 0;
14984 if (ctx != NULL_TREE && vague_linkage_p (ctx))
14985 /* This is a function in a local class in an extern inline
14986 or template function. */
14987 comdat_linkage (decl1);
14989 /* If this function belongs to an interface, it is public.
14990 If it belongs to someone else's interface, it is also external.
14991 This only affects inlines and template instantiations. */
14992 else if (!finfo->interface_unknown && honor_interface)
14994 if (DECL_DECLARED_INLINE_P (decl1)
14995 || DECL_TEMPLATE_INSTANTIATION (decl1))
14997 DECL_EXTERNAL (decl1)
14998 = (finfo->interface_only
14999 || (DECL_DECLARED_INLINE_P (decl1)
15000 && ! flag_implement_inlines
15001 && !DECL_VINDEX (decl1)));
15003 /* For WIN32 we also want to put these in linkonce sections. */
15004 maybe_make_one_only (decl1);
15006 else
15007 DECL_EXTERNAL (decl1) = 0;
15008 DECL_INTERFACE_KNOWN (decl1) = 1;
15009 /* If this function is in an interface implemented in this file,
15010 make sure that the back end knows to emit this function
15011 here. */
15012 if (!DECL_EXTERNAL (decl1))
15013 mark_needed (decl1);
15015 else if (finfo->interface_unknown && finfo->interface_only
15016 && honor_interface)
15018 /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma
15019 interface, we will have both finfo->interface_unknown and
15020 finfo->interface_only set. In that case, we don't want to
15021 use the normal heuristics because someone will supply a
15022 #pragma implementation elsewhere, and deducing it here would
15023 produce a conflict. */
15024 comdat_linkage (decl1);
15025 DECL_EXTERNAL (decl1) = 0;
15026 DECL_INTERFACE_KNOWN (decl1) = 1;
15027 DECL_DEFER_OUTPUT (decl1) = 1;
15029 else
15031 /* This is a definition, not a reference.
15032 So clear DECL_EXTERNAL, unless this is a GNU extern inline. */
15033 if (!GNU_INLINE_P (decl1))
15034 DECL_EXTERNAL (decl1) = 0;
15036 if ((DECL_DECLARED_INLINE_P (decl1)
15037 || DECL_TEMPLATE_INSTANTIATION (decl1))
15038 && ! DECL_INTERFACE_KNOWN (decl1))
15039 DECL_DEFER_OUTPUT (decl1) = 1;
15040 else
15041 DECL_INTERFACE_KNOWN (decl1) = 1;
15044 /* Determine the ELF visibility attribute for the function. We must not
15045 do this before calling "pushdecl", as we must allow "duplicate_decls"
15046 to merge any attributes appropriately. We also need to wait until
15047 linkage is set. */
15048 if (!DECL_CLONED_FUNCTION_P (decl1))
15049 determine_visibility (decl1);
15051 if (!processing_template_decl)
15052 maybe_instantiate_noexcept (decl1);
15054 begin_scope (sk_function_parms, decl1);
15056 ++function_depth;
15058 if (DECL_DESTRUCTOR_P (decl1)
15059 || (DECL_CONSTRUCTOR_P (decl1)
15060 && targetm.cxx.cdtor_returns_this ()))
15061 cdtor_label = create_artificial_label (input_location);
15063 start_fname_decls ();
15065 store_parm_decls (current_function_parms);
15067 if (!processing_template_decl
15068 && (flag_lifetime_dse > 1)
15069 && DECL_CONSTRUCTOR_P (decl1)
15070 && !DECL_CLONED_FUNCTION_P (decl1)
15071 /* Clobbering an empty base is harmful if it overlays real data. */
15072 && !is_empty_class (current_class_type)
15073 /* We can't clobber safely for an implicitly-defined default constructor
15074 because part of the initialization might happen before we enter the
15075 constructor, via AGGR_INIT_ZERO_FIRST (c++/68006). */
15076 && !implicit_default_ctor_p (decl1))
15077 finish_expr_stmt (build_clobber_this ());
15079 if (!processing_template_decl
15080 && DECL_CONSTRUCTOR_P (decl1)
15081 && sanitize_flags_p (SANITIZE_VPTR)
15082 && !DECL_CLONED_FUNCTION_P (decl1)
15083 && !implicit_default_ctor_p (decl1))
15084 cp_ubsan_maybe_initialize_vtbl_ptrs (current_class_ptr);
15086 return true;
15090 /* Like start_preparsed_function, except that instead of a
15091 FUNCTION_DECL, this function takes DECLSPECS and DECLARATOR.
15093 Returns true on success. If the DECLARATOR is not suitable
15094 for a function, we return false, which tells the parser to
15095 skip the entire function. */
15097 bool
15098 start_function (cp_decl_specifier_seq *declspecs,
15099 const cp_declarator *declarator,
15100 tree attrs)
15102 tree decl1;
15104 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, &attrs);
15105 invoke_plugin_callbacks (PLUGIN_START_PARSE_FUNCTION, decl1);
15106 if (decl1 == error_mark_node)
15107 return false;
15108 /* If the declarator is not suitable for a function definition,
15109 cause a syntax error. */
15110 if (decl1 == NULL_TREE || TREE_CODE (decl1) != FUNCTION_DECL)
15112 error ("invalid function declaration");
15113 return false;
15116 if (DECL_MAIN_P (decl1))
15117 /* main must return int. grokfndecl should have corrected it
15118 (and issued a diagnostic) if the user got it wrong. */
15119 gcc_assert (same_type_p (TREE_TYPE (TREE_TYPE (decl1)),
15120 integer_type_node));
15122 return start_preparsed_function (decl1, attrs, /*flags=*/SF_DEFAULT);
15125 /* Returns true iff an EH_SPEC_BLOCK should be created in the body of
15126 FN. */
15128 static bool
15129 use_eh_spec_block (tree fn)
15131 return (flag_exceptions && flag_enforce_eh_specs
15132 && !processing_template_decl
15133 && !type_throw_all_p (TREE_TYPE (fn))
15134 /* We insert the EH_SPEC_BLOCK only in the original
15135 function; then, it is copied automatically to the
15136 clones. */
15137 && !DECL_CLONED_FUNCTION_P (fn)
15138 /* Implicitly-generated constructors and destructors have
15139 exception specifications. However, those specifications
15140 are the union of the possible exceptions specified by the
15141 constructors/destructors for bases and members, so no
15142 unallowed exception will ever reach this function. By
15143 not creating the EH_SPEC_BLOCK we save a little memory,
15144 and we avoid spurious warnings about unreachable
15145 code. */
15146 && !DECL_DEFAULTED_FN (fn));
15149 /* Store the parameter declarations into the current function declaration.
15150 This is called after parsing the parameter declarations, before
15151 digesting the body of the function.
15153 Also install to binding contour return value identifier, if any. */
15155 static void
15156 store_parm_decls (tree current_function_parms)
15158 tree fndecl = current_function_decl;
15159 tree parm;
15161 /* This is a chain of any other decls that came in among the parm
15162 declarations. If a parm is declared with enum {foo, bar} x;
15163 then CONST_DECLs for foo and bar are put here. */
15164 tree nonparms = NULL_TREE;
15166 if (current_function_parms)
15168 /* This case is when the function was defined with an ANSI prototype.
15169 The parms already have decls, so we need not do anything here
15170 except record them as in effect
15171 and complain if any redundant old-style parm decls were written. */
15173 tree specparms = current_function_parms;
15174 tree next;
15176 /* Must clear this because it might contain TYPE_DECLs declared
15177 at class level. */
15178 current_binding_level->names = NULL;
15180 /* If we're doing semantic analysis, then we'll call pushdecl
15181 for each of these. We must do them in reverse order so that
15182 they end in the correct forward order. */
15183 specparms = nreverse (specparms);
15185 for (parm = specparms; parm; parm = next)
15187 next = DECL_CHAIN (parm);
15188 if (TREE_CODE (parm) == PARM_DECL)
15189 pushdecl (parm);
15190 else
15192 /* If we find an enum constant or a type tag,
15193 put it aside for the moment. */
15194 TREE_CHAIN (parm) = NULL_TREE;
15195 nonparms = chainon (nonparms, parm);
15199 /* Get the decls in their original chain order and record in the
15200 function. This is all and only the PARM_DECLs that were
15201 pushed into scope by the loop above. */
15202 DECL_ARGUMENTS (fndecl) = get_local_decls ();
15204 else
15205 DECL_ARGUMENTS (fndecl) = NULL_TREE;
15207 /* Now store the final chain of decls for the arguments
15208 as the decl-chain of the current lexical scope.
15209 Put the enumerators in as well, at the front so that
15210 DECL_ARGUMENTS is not modified. */
15211 current_binding_level->names = chainon (nonparms, DECL_ARGUMENTS (fndecl));
15213 if (use_eh_spec_block (current_function_decl))
15214 current_eh_spec_block = begin_eh_spec_block ();
15218 /* We have finished doing semantic analysis on DECL, but have not yet
15219 generated RTL for its body. Save away our current state, so that
15220 when we want to generate RTL later we know what to do. */
15222 static void
15223 save_function_data (tree decl)
15225 struct language_function *f;
15227 /* Save the language-specific per-function data so that we can
15228 get it back when we really expand this function. */
15229 gcc_assert (!DECL_PENDING_INLINE_P (decl));
15231 /* Make a copy. */
15232 f = ggc_alloc<language_function> ();
15233 memcpy (f, cp_function_chain, sizeof (struct language_function));
15234 DECL_SAVED_FUNCTION_DATA (decl) = f;
15236 /* Clear out the bits we don't need. */
15237 f->base.x_stmt_tree.x_cur_stmt_list = NULL;
15238 f->bindings = NULL;
15239 f->x_local_names = NULL;
15240 f->base.local_typedefs = NULL;
15244 /* Set the return value of the constructor (if present). */
15246 static void
15247 finish_constructor_body (void)
15249 tree val;
15250 tree exprstmt;
15252 if (targetm.cxx.cdtor_returns_this ())
15254 /* Any return from a constructor will end up here. */
15255 add_stmt (build_stmt (input_location, LABEL_EXPR, cdtor_label));
15257 val = DECL_ARGUMENTS (current_function_decl);
15258 val = build2 (MODIFY_EXPR, TREE_TYPE (val),
15259 DECL_RESULT (current_function_decl), val);
15260 /* Return the address of the object. */
15261 exprstmt = build_stmt (input_location, RETURN_EXPR, val);
15262 add_stmt (exprstmt);
15266 /* Do all the processing for the beginning of a destructor; set up the
15267 vtable pointers and cleanups for bases and members. */
15269 static void
15270 begin_destructor_body (void)
15272 tree compound_stmt;
15274 /* If the CURRENT_CLASS_TYPE is incomplete, we will have already
15275 issued an error message. We still want to try to process the
15276 body of the function, but initialize_vtbl_ptrs will crash if
15277 TYPE_BINFO is NULL. */
15278 if (COMPLETE_TYPE_P (current_class_type))
15280 compound_stmt = begin_compound_stmt (0);
15281 /* Make all virtual function table pointers in non-virtual base
15282 classes point to CURRENT_CLASS_TYPE's virtual function
15283 tables. */
15284 initialize_vtbl_ptrs (current_class_ptr);
15285 finish_compound_stmt (compound_stmt);
15287 if (flag_lifetime_dse
15288 /* Clobbering an empty base is harmful if it overlays real data. */
15289 && !is_empty_class (current_class_type))
15290 finish_decl_cleanup (NULL_TREE, build_clobber_this ());
15292 /* And insert cleanups for our bases and members so that they
15293 will be properly destroyed if we throw. */
15294 push_base_cleanups ();
15298 /* At the end of every destructor we generate code to delete the object if
15299 necessary. Do that now. */
15301 static void
15302 finish_destructor_body (void)
15304 tree exprstmt;
15306 /* Any return from a destructor will end up here; that way all base
15307 and member cleanups will be run when the function returns. */
15308 add_stmt (build_stmt (input_location, LABEL_EXPR, cdtor_label));
15310 if (targetm.cxx.cdtor_returns_this ())
15312 tree val;
15314 val = DECL_ARGUMENTS (current_function_decl);
15315 val = build2 (MODIFY_EXPR, TREE_TYPE (val),
15316 DECL_RESULT (current_function_decl), val);
15317 /* Return the address of the object. */
15318 exprstmt = build_stmt (input_location, RETURN_EXPR, val);
15319 add_stmt (exprstmt);
15323 /* Do the necessary processing for the beginning of a function body, which
15324 in this case includes member-initializers, but not the catch clauses of
15325 a function-try-block. Currently, this means opening a binding level
15326 for the member-initializers (in a ctor), member cleanups (in a dtor),
15327 and capture proxies (in a lambda operator()). */
15329 tree
15330 begin_function_body (void)
15332 tree stmt;
15334 if (! FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
15335 return NULL_TREE;
15337 if (processing_template_decl)
15338 /* Do nothing now. */;
15339 else
15340 /* Always keep the BLOCK node associated with the outermost pair of
15341 curly braces of a function. These are needed for correct
15342 operation of dwarfout.c. */
15343 keep_next_level (true);
15345 stmt = begin_compound_stmt (BCS_FN_BODY);
15347 if (processing_template_decl)
15348 /* Do nothing now. */;
15349 else if (DECL_DESTRUCTOR_P (current_function_decl))
15350 begin_destructor_body ();
15352 return stmt;
15355 /* Do the processing for the end of a function body. Currently, this means
15356 closing out the cleanups for fully-constructed bases and members, and in
15357 the case of the destructor, deleting the object if desired. Again, this
15358 is only meaningful for [cd]tors, since they are the only functions where
15359 there is a significant distinction between the main body and any
15360 function catch clauses. Handling, say, main() return semantics here
15361 would be wrong, as flowing off the end of a function catch clause for
15362 main() would also need to return 0. */
15364 void
15365 finish_function_body (tree compstmt)
15367 if (compstmt == NULL_TREE)
15368 return;
15370 /* Close the block. */
15371 finish_compound_stmt (compstmt);
15373 if (processing_template_decl)
15374 /* Do nothing now. */;
15375 else if (DECL_CONSTRUCTOR_P (current_function_decl))
15376 finish_constructor_body ();
15377 else if (DECL_DESTRUCTOR_P (current_function_decl))
15378 finish_destructor_body ();
15381 /* Given a function, returns the BLOCK corresponding to the outermost level
15382 of curly braces, skipping the artificial block created for constructor
15383 initializers. */
15385 tree
15386 outer_curly_brace_block (tree fndecl)
15388 tree block = DECL_INITIAL (fndecl);
15389 if (BLOCK_OUTER_CURLY_BRACE_P (block))
15390 return block;
15391 block = BLOCK_SUBBLOCKS (block);
15392 if (BLOCK_OUTER_CURLY_BRACE_P (block))
15393 return block;
15394 block = BLOCK_SUBBLOCKS (block);
15395 gcc_assert (BLOCK_OUTER_CURLY_BRACE_P (block));
15396 return block;
15399 /* If FNDECL is a class's key method, add the class to the list of
15400 keyed classes that should be emitted. */
15402 static void
15403 record_key_method_defined (tree fndecl)
15405 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fndecl)
15406 && DECL_VIRTUAL_P (fndecl)
15407 && !processing_template_decl)
15409 tree fnclass = DECL_CONTEXT (fndecl);
15410 if (fndecl == CLASSTYPE_KEY_METHOD (fnclass))
15411 vec_safe_push (keyed_classes, fnclass);
15415 /* Subroutine of finish_function.
15416 Save the body of constexpr functions for possible
15417 future compile time evaluation. */
15419 static void
15420 maybe_save_function_definition (tree fun)
15422 if (!processing_template_decl
15423 && DECL_DECLARED_CONSTEXPR_P (fun)
15424 && !cp_function_chain->invalid_constexpr
15425 && !DECL_CLONED_FUNCTION_P (fun))
15426 register_constexpr_fundef (fun, DECL_SAVED_TREE (fun));
15429 /* Finish up a function declaration and compile that function
15430 all the way to assembler language output. The free the storage
15431 for the function definition.
15433 FLAGS is a bitwise or of the following values:
15434 2 - INCLASS_INLINE
15435 We just finished processing the body of an in-class inline
15436 function definition. (This processing will have taken place
15437 after the class definition is complete.) */
15439 tree
15440 finish_function (int flags)
15442 tree fndecl = current_function_decl;
15443 tree fntype, ctype = NULL_TREE;
15444 int inclass_inline = (flags & 2) != 0;
15446 /* When we get some parse errors, we can end up without a
15447 current_function_decl, so cope. */
15448 if (fndecl == NULL_TREE)
15449 return error_mark_node;
15451 if (c_dialect_objc ())
15452 objc_finish_function ();
15454 record_key_method_defined (fndecl);
15456 fntype = TREE_TYPE (fndecl);
15458 /* TREE_READONLY (fndecl) = 1;
15459 This caused &foo to be of type ptr-to-const-function
15460 which then got a warning when stored in a ptr-to-function variable. */
15462 gcc_assert (building_stmt_list_p ());
15463 /* The current function is being defined, so its DECL_INITIAL should
15464 be set, and unless there's a multiple definition, it should be
15465 error_mark_node. */
15466 gcc_assert (DECL_INITIAL (fndecl) == error_mark_node);
15468 /* For a cloned function, we've already got all the code we need;
15469 there's no need to add any extra bits. */
15470 if (!DECL_CLONED_FUNCTION_P (fndecl))
15472 /* Make it so that `main' always returns 0 by default. */
15473 if (DECL_MAIN_P (current_function_decl))
15474 finish_return_stmt (integer_zero_node);
15476 if (use_eh_spec_block (current_function_decl))
15477 finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
15478 (TREE_TYPE (current_function_decl)),
15479 current_eh_spec_block);
15482 /* If we're saving up tree structure, tie off the function now. */
15483 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
15485 if (fn_contains_cilk_spawn_p (cfun) && !processing_template_decl)
15486 cfun->cilk_frame_decl = insert_cilk_frame (fndecl);
15488 finish_fname_decls ();
15490 /* If this function can't throw any exceptions, remember that. */
15491 if (!processing_template_decl
15492 && !cp_function_chain->can_throw
15493 && !flag_non_call_exceptions
15494 && !decl_replaceable_p (fndecl))
15495 TREE_NOTHROW (fndecl) = 1;
15497 /* This must come after expand_function_end because cleanups might
15498 have declarations (from inline functions) that need to go into
15499 this function's blocks. */
15501 /* If the current binding level isn't the outermost binding level
15502 for this function, either there is a bug, or we have experienced
15503 syntax errors and the statement tree is malformed. */
15504 if (current_binding_level->kind != sk_function_parms)
15506 /* Make sure we have already experienced errors. */
15507 gcc_assert (errorcount);
15509 /* Throw away the broken statement tree and extra binding
15510 levels. */
15511 DECL_SAVED_TREE (fndecl) = alloc_stmt_list ();
15513 while (current_binding_level->kind != sk_function_parms)
15515 if (current_binding_level->kind == sk_class)
15516 pop_nested_class ();
15517 else
15518 poplevel (0, 0, 0);
15521 poplevel (1, 0, 1);
15523 /* Statements should always be full-expressions at the outermost set
15524 of curly braces for a function. */
15525 gcc_assert (stmts_are_full_exprs_p ());
15527 /* If there are no return statements in a function with auto return type,
15528 the return type is void. But if the declared type is something like
15529 auto*, this is an error. */
15530 if (!processing_template_decl && FNDECL_USED_AUTO (fndecl)
15531 && TREE_TYPE (fntype) == current_function_auto_return_pattern)
15533 if (is_auto (current_function_auto_return_pattern))
15535 apply_deduced_return_type (fndecl, void_type_node);
15536 fntype = TREE_TYPE (fndecl);
15538 else if (!current_function_returns_value
15539 && !current_function_returns_null)
15541 error ("no return statements in function returning %qT",
15542 current_function_auto_return_pattern);
15543 inform (input_location, "only plain %<auto%> return type can be "
15544 "deduced to %<void%>");
15548 // If this is a concept, check that the definition is reasonable.
15549 if (DECL_DECLARED_CONCEPT_P (fndecl))
15550 check_function_concept (fndecl);
15552 /* Lambda closure members are implicitly constexpr if possible. */
15553 if (cxx_dialect >= cxx1z
15554 && LAMBDA_TYPE_P (CP_DECL_CONTEXT (fndecl))
15555 && (processing_template_decl
15556 || is_valid_constexpr_fn (fndecl, /*complain*/false))
15557 && potential_constant_expression (DECL_SAVED_TREE (fndecl)))
15558 DECL_DECLARED_CONSTEXPR_P (fndecl) = true;
15560 /* Save constexpr function body before it gets munged by
15561 the NRV transformation. */
15562 maybe_save_function_definition (fndecl);
15564 /* Invoke the pre-genericize plugin before we start munging things. */
15565 if (!processing_template_decl)
15566 invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE, fndecl);
15568 /* Perform delayed folding before NRV transformation. */
15569 if (!processing_template_decl)
15570 cp_fold_function (fndecl);
15572 /* Set up the named return value optimization, if we can. Candidate
15573 variables are selected in check_return_expr. */
15574 if (current_function_return_value)
15576 tree r = current_function_return_value;
15577 tree outer;
15579 if (r != error_mark_node
15580 /* This is only worth doing for fns that return in memory--and
15581 simpler, since we don't have to worry about promoted modes. */
15582 && aggregate_value_p (TREE_TYPE (TREE_TYPE (fndecl)), fndecl)
15583 /* Only allow this for variables declared in the outer scope of
15584 the function so we know that their lifetime always ends with a
15585 return; see g++.dg/opt/nrv6.C. We could be more flexible if
15586 we were to do this optimization in tree-ssa. */
15587 && (outer = outer_curly_brace_block (fndecl))
15588 && chain_member (r, BLOCK_VARS (outer)))
15589 finalize_nrv (&DECL_SAVED_TREE (fndecl), r, DECL_RESULT (fndecl));
15591 current_function_return_value = NULL_TREE;
15594 /* Remember that we were in class scope. */
15595 if (current_class_name)
15596 ctype = current_class_type;
15598 /* Must mark the RESULT_DECL as being in this function. */
15599 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
15601 /* Set the BLOCK_SUPERCONTEXT of the outermost function scope to point
15602 to the FUNCTION_DECL node itself. */
15603 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
15605 /* Save away current state, if appropriate. */
15606 if (!processing_template_decl)
15607 save_function_data (fndecl);
15609 /* Complain if there's just no return statement. */
15610 if (warn_return_type
15611 && !VOID_TYPE_P (TREE_TYPE (fntype))
15612 && !dependent_type_p (TREE_TYPE (fntype))
15613 && !current_function_returns_value && !current_function_returns_null
15614 /* Don't complain if we abort or throw. */
15615 && !current_function_returns_abnormally
15616 /* Don't complain if there's an infinite loop. */
15617 && !current_function_infinite_loop
15618 /* Don't complain if we are declared noreturn. */
15619 && !TREE_THIS_VOLATILE (fndecl)
15620 && !DECL_NAME (DECL_RESULT (fndecl))
15621 && !TREE_NO_WARNING (fndecl)
15622 /* Structor return values (if any) are set by the compiler. */
15623 && !DECL_CONSTRUCTOR_P (fndecl)
15624 && !DECL_DESTRUCTOR_P (fndecl)
15625 && targetm.warn_func_return (fndecl))
15627 warning (OPT_Wreturn_type,
15628 "no return statement in function returning non-void");
15629 TREE_NO_WARNING (fndecl) = 1;
15632 /* Store the end of the function, so that we get good line number
15633 info for the epilogue. */
15634 cfun->function_end_locus = input_location;
15636 /* Complain about parameters that are only set, but never otherwise used. */
15637 if (warn_unused_but_set_parameter
15638 && !processing_template_decl
15639 && errorcount == unused_but_set_errorcount
15640 && !DECL_CLONED_FUNCTION_P (fndecl))
15642 tree decl;
15644 for (decl = DECL_ARGUMENTS (fndecl);
15645 decl;
15646 decl = DECL_CHAIN (decl))
15647 if (TREE_USED (decl)
15648 && TREE_CODE (decl) == PARM_DECL
15649 && !DECL_READ_P (decl)
15650 && DECL_NAME (decl)
15651 && !DECL_ARTIFICIAL (decl)
15652 && !TREE_NO_WARNING (decl)
15653 && !DECL_IN_SYSTEM_HEADER (decl)
15654 && TREE_TYPE (decl) != error_mark_node
15655 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
15656 && (!CLASS_TYPE_P (TREE_TYPE (decl))
15657 || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl))))
15658 warning_at (DECL_SOURCE_LOCATION (decl),
15659 OPT_Wunused_but_set_parameter,
15660 "parameter %qD set but not used", decl);
15661 unused_but_set_errorcount = errorcount;
15664 /* Complain about locally defined typedefs that are not used in this
15665 function. */
15666 maybe_warn_unused_local_typedefs ();
15668 /* Possibly warn about unused parameters. */
15669 if (warn_unused_parameter
15670 && !processing_template_decl
15671 && !DECL_CLONED_FUNCTION_P (fndecl))
15672 do_warn_unused_parameter (fndecl);
15674 /* Genericize before inlining. */
15675 if (!processing_template_decl)
15677 struct language_function *f = DECL_SAVED_FUNCTION_DATA (fndecl);
15678 cp_genericize (fndecl);
15679 /* Clear out the bits we don't need. */
15680 f->x_current_class_ptr = NULL;
15681 f->x_current_class_ref = NULL;
15682 f->x_eh_spec_block = NULL;
15683 f->x_in_charge_parm = NULL;
15684 f->x_vtt_parm = NULL;
15685 f->x_return_value = NULL;
15686 f->bindings = NULL;
15687 f->extern_decl_map = NULL;
15688 f->infinite_loops = NULL;
15690 /* Clear out the bits we don't need. */
15691 local_names = NULL;
15693 /* We're leaving the context of this function, so zap cfun. It's still in
15694 DECL_STRUCT_FUNCTION, and we'll restore it in tree_rest_of_compilation. */
15695 set_cfun (NULL);
15696 current_function_decl = NULL;
15698 /* If this is an in-class inline definition, we may have to pop the
15699 bindings for the template parameters that we added in
15700 maybe_begin_member_template_processing when start_function was
15701 called. */
15702 if (inclass_inline)
15703 maybe_end_member_template_processing ();
15705 /* Leave the scope of the class. */
15706 if (ctype)
15707 pop_nested_class ();
15709 --function_depth;
15711 /* Clean up. */
15712 current_function_decl = NULL_TREE;
15714 invoke_plugin_callbacks (PLUGIN_FINISH_PARSE_FUNCTION, fndecl);
15715 return fndecl;
15718 /* Create the FUNCTION_DECL for a function definition.
15719 DECLSPECS and DECLARATOR are the parts of the declaration;
15720 they describe the return type and the name of the function,
15721 but twisted together in a fashion that parallels the syntax of C.
15723 This function creates a binding context for the function body
15724 as well as setting up the FUNCTION_DECL in current_function_decl.
15726 Returns a FUNCTION_DECL on success.
15728 If the DECLARATOR is not suitable for a function (it defines a datum
15729 instead), we return 0, which tells yyparse to report a parse error.
15731 May return void_type_node indicating that this method is actually
15732 a friend. See grokfield for more details.
15734 Came here with a `.pushlevel' .
15736 DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
15737 CHANGES TO CODE IN `grokfield'. */
15739 tree
15740 grokmethod (cp_decl_specifier_seq *declspecs,
15741 const cp_declarator *declarator, tree attrlist)
15743 tree fndecl = grokdeclarator (declarator, declspecs, MEMFUNCDEF, 0,
15744 &attrlist);
15746 if (fndecl == error_mark_node)
15747 return error_mark_node;
15749 if (fndecl == NULL || TREE_CODE (fndecl) != FUNCTION_DECL)
15751 error ("invalid member function declaration");
15752 return error_mark_node;
15755 if (attrlist)
15756 cplus_decl_attributes (&fndecl, attrlist, 0);
15758 /* Pass friends other than inline friend functions back. */
15759 if (fndecl == void_type_node)
15760 return fndecl;
15762 if (DECL_IN_AGGR_P (fndecl))
15764 if (DECL_CLASS_SCOPE_P (fndecl))
15765 error ("%qD is already defined in class %qT", fndecl,
15766 DECL_CONTEXT (fndecl));
15767 return error_mark_node;
15770 check_template_shadow (fndecl);
15772 if (TREE_PUBLIC (fndecl))
15773 DECL_COMDAT (fndecl) = 1;
15774 DECL_DECLARED_INLINE_P (fndecl) = 1;
15775 DECL_NO_INLINE_WARNING_P (fndecl) = 1;
15777 /* We process method specializations in finish_struct_1. */
15778 if (processing_template_decl && !DECL_TEMPLATE_SPECIALIZATION (fndecl))
15780 fndecl = push_template_decl (fndecl);
15781 if (fndecl == error_mark_node)
15782 return fndecl;
15785 if (! DECL_FRIEND_P (fndecl))
15787 if (DECL_CHAIN (fndecl))
15789 fndecl = copy_node (fndecl);
15790 TREE_CHAIN (fndecl) = NULL_TREE;
15794 cp_finish_decl (fndecl, NULL_TREE, false, NULL_TREE, 0);
15796 DECL_IN_AGGR_P (fndecl) = 1;
15797 return fndecl;
15801 /* VAR is a VAR_DECL. If its type is incomplete, remember VAR so that
15802 we can lay it out later, when and if its type becomes complete.
15804 Also handle constexpr variables where the initializer involves
15805 an unlowered PTRMEM_CST because the class isn't complete yet. */
15807 void
15808 maybe_register_incomplete_var (tree var)
15810 gcc_assert (VAR_P (var));
15812 /* Keep track of variables with incomplete types. */
15813 if (!processing_template_decl && TREE_TYPE (var) != error_mark_node
15814 && DECL_EXTERNAL (var))
15816 tree inner_type = TREE_TYPE (var);
15818 while (TREE_CODE (inner_type) == ARRAY_TYPE)
15819 inner_type = TREE_TYPE (inner_type);
15820 inner_type = TYPE_MAIN_VARIANT (inner_type);
15822 if ((!COMPLETE_TYPE_P (inner_type) && CLASS_TYPE_P (inner_type))
15823 /* RTTI TD entries are created while defining the type_info. */
15824 || (TYPE_LANG_SPECIFIC (inner_type)
15825 && TYPE_BEING_DEFINED (inner_type)))
15827 incomplete_var iv = {var, inner_type};
15828 vec_safe_push (incomplete_vars, iv);
15830 else if (!(DECL_LANG_SPECIFIC (var) && DECL_TEMPLATE_INFO (var))
15831 && decl_constant_var_p (var)
15832 && (TYPE_PTRMEM_P (inner_type) || CLASS_TYPE_P (inner_type)))
15834 /* When the outermost open class is complete we can resolve any
15835 pointers-to-members. */
15836 tree context = outermost_open_class ();
15837 incomplete_var iv = {var, context};
15838 vec_safe_push (incomplete_vars, iv);
15843 /* Called when a class type (given by TYPE) is defined. If there are
15844 any existing VAR_DECLs whose type has been completed by this
15845 declaration, update them now. */
15847 void
15848 complete_vars (tree type)
15850 unsigned ix;
15851 incomplete_var *iv;
15853 for (ix = 0; vec_safe_iterate (incomplete_vars, ix, &iv); )
15855 if (same_type_p (type, iv->incomplete_type))
15857 tree var = iv->decl;
15858 tree type = TREE_TYPE (var);
15860 if (type != error_mark_node
15861 && (TYPE_MAIN_VARIANT (strip_array_types (type))
15862 == iv->incomplete_type))
15864 /* Complete the type of the variable. The VAR_DECL itself
15865 will be laid out in expand_expr. */
15866 complete_type (type);
15867 cp_apply_type_quals_to_decl (cp_type_quals (type), var);
15870 /* Remove this entry from the list. */
15871 incomplete_vars->unordered_remove (ix);
15873 else
15874 ix++;
15877 /* Check for pending declarations which may have abstract type. */
15878 complete_type_check_abstract (type);
15881 /* If DECL is of a type which needs a cleanup, build and return an
15882 expression to perform that cleanup here. Return NULL_TREE if no
15883 cleanup need be done. DECL can also be a _REF when called from
15884 split_nonconstant_init_1. */
15886 tree
15887 cxx_maybe_build_cleanup (tree decl, tsubst_flags_t complain)
15889 tree type;
15890 tree attr;
15891 tree cleanup;
15893 /* Assume no cleanup is required. */
15894 cleanup = NULL_TREE;
15896 if (error_operand_p (decl))
15897 return cleanup;
15899 /* Handle "__attribute__((cleanup))". We run the cleanup function
15900 before the destructor since the destructor is what actually
15901 terminates the lifetime of the object. */
15902 if (DECL_P (decl))
15903 attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
15904 else
15905 attr = NULL_TREE;
15906 if (attr)
15908 tree id;
15909 tree fn;
15910 tree arg;
15912 /* Get the name specified by the user for the cleanup function. */
15913 id = TREE_VALUE (TREE_VALUE (attr));
15914 /* Look up the name to find the cleanup function to call. It is
15915 important to use lookup_name here because that is what is
15916 used in c-common.c:handle_cleanup_attribute when performing
15917 initial checks on the attribute. Note that those checks
15918 include ensuring that the function found is not an overloaded
15919 function, or an object with an overloaded call operator,
15920 etc.; we can rely on the fact that the function found is an
15921 ordinary FUNCTION_DECL. */
15922 fn = lookup_name (id);
15923 arg = build_address (decl);
15924 if (!mark_used (decl, complain) && !(complain & tf_error))
15925 return error_mark_node;
15926 cleanup = cp_build_function_call_nary (fn, complain, arg, NULL_TREE);
15927 if (cleanup == error_mark_node)
15928 return error_mark_node;
15930 /* Handle ordinary C++ destructors. */
15931 type = TREE_TYPE (decl);
15932 if (type_build_dtor_call (type))
15934 int flags = LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR;
15935 tree addr;
15936 tree call;
15938 if (TREE_CODE (type) == ARRAY_TYPE)
15939 addr = decl;
15940 else
15941 addr = build_address (decl);
15943 call = build_delete (TREE_TYPE (addr), addr,
15944 sfk_complete_destructor, flags, 0, complain);
15945 if (call == error_mark_node)
15946 cleanup = error_mark_node;
15947 else if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
15948 /* Discard the call. */;
15949 else if (cleanup)
15950 cleanup = cp_build_compound_expr (cleanup, call, complain);
15951 else
15952 cleanup = call;
15955 /* build_delete sets the location of the destructor call to the
15956 current location, even though the destructor is going to be
15957 called later, at the end of the current scope. This can lead to
15958 a "jumpy" behavior for users of debuggers when they step around
15959 the end of the block. So let's unset the location of the
15960 destructor call instead. */
15961 protected_set_expr_location (cleanup, UNKNOWN_LOCATION);
15963 if (cleanup
15964 && DECL_P (decl)
15965 && !lookup_attribute ("warn_unused", TYPE_ATTRIBUTES (TREE_TYPE (decl)))
15966 /* Treat objects with destructors as used; the destructor may do
15967 something substantive. */
15968 && !mark_used (decl, complain) && !(complain & tf_error))
15969 return error_mark_node;
15971 return cleanup;
15975 /* Return the FUNCTION_TYPE that corresponds to MEMFNTYPE, which can be a
15976 FUNCTION_DECL, METHOD_TYPE, FUNCTION_TYPE, pointer or reference to
15977 METHOD_TYPE or FUNCTION_TYPE, or pointer to member function. */
15979 tree
15980 static_fn_type (tree memfntype)
15982 tree fntype;
15983 tree args;
15985 if (TYPE_PTRMEMFUNC_P (memfntype))
15986 memfntype = TYPE_PTRMEMFUNC_FN_TYPE (memfntype);
15987 if (POINTER_TYPE_P (memfntype)
15988 || TREE_CODE (memfntype) == FUNCTION_DECL)
15989 memfntype = TREE_TYPE (memfntype);
15990 if (TREE_CODE (memfntype) == FUNCTION_TYPE)
15991 return memfntype;
15992 gcc_assert (TREE_CODE (memfntype) == METHOD_TYPE);
15993 args = TYPE_ARG_TYPES (memfntype);
15994 cp_ref_qualifier rqual = type_memfn_rqual (memfntype);
15995 fntype = build_function_type (TREE_TYPE (memfntype), TREE_CHAIN (args));
15996 fntype = apply_memfn_quals (fntype, type_memfn_quals (memfntype), rqual);
15997 fntype = (cp_build_type_attribute_variant
15998 (fntype, TYPE_ATTRIBUTES (memfntype)));
15999 fntype = (build_exception_variant
16000 (fntype, TYPE_RAISES_EXCEPTIONS (memfntype)));
16001 if (TYPE_HAS_LATE_RETURN_TYPE (memfntype))
16002 TYPE_HAS_LATE_RETURN_TYPE (fntype) = 1;
16003 return fntype;
16006 /* DECL was originally constructed as a non-static member function,
16007 but turned out to be static. Update it accordingly. */
16009 void
16010 revert_static_member_fn (tree decl)
16012 tree stype = static_fn_type (decl);
16013 cp_cv_quals quals = type_memfn_quals (stype);
16014 cp_ref_qualifier rqual = type_memfn_rqual (stype);
16016 if (quals != TYPE_UNQUALIFIED || rqual != REF_QUAL_NONE)
16017 stype = apply_memfn_quals (stype, TYPE_UNQUALIFIED, REF_QUAL_NONE);
16019 TREE_TYPE (decl) = stype;
16021 if (DECL_ARGUMENTS (decl))
16022 DECL_ARGUMENTS (decl) = DECL_CHAIN (DECL_ARGUMENTS (decl));
16023 DECL_STATIC_FUNCTION_P (decl) = 1;
16026 /* Return which tree structure is used by T, or TS_CP_GENERIC if T is
16027 one of the language-independent trees. */
16029 enum cp_tree_node_structure_enum
16030 cp_tree_node_structure (union lang_tree_node * t)
16032 switch (TREE_CODE (&t->generic))
16034 case DEFAULT_ARG: return TS_CP_DEFAULT_ARG;
16035 case DEFERRED_NOEXCEPT: return TS_CP_DEFERRED_NOEXCEPT;
16036 case IDENTIFIER_NODE: return TS_CP_IDENTIFIER;
16037 case OVERLOAD: return TS_CP_OVERLOAD;
16038 case TEMPLATE_PARM_INDEX: return TS_CP_TPI;
16039 case PTRMEM_CST: return TS_CP_PTRMEM;
16040 case BASELINK: return TS_CP_BASELINK;
16041 case TEMPLATE_DECL: return TS_CP_TEMPLATE_DECL;
16042 case STATIC_ASSERT: return TS_CP_STATIC_ASSERT;
16043 case ARGUMENT_PACK_SELECT: return TS_CP_ARGUMENT_PACK_SELECT;
16044 case TRAIT_EXPR: return TS_CP_TRAIT_EXPR;
16045 case LAMBDA_EXPR: return TS_CP_LAMBDA_EXPR;
16046 case TEMPLATE_INFO: return TS_CP_TEMPLATE_INFO;
16047 case CONSTRAINT_INFO: return TS_CP_CONSTRAINT_INFO;
16048 case USERDEF_LITERAL: return TS_CP_USERDEF_LITERAL;
16049 default: return TS_CP_GENERIC;
16053 /* Build the void_list_node (void_type_node having been created). */
16054 tree
16055 build_void_list_node (void)
16057 tree t = build_tree_list (NULL_TREE, void_type_node);
16058 return t;
16061 bool
16062 cp_missing_noreturn_ok_p (tree decl)
16064 /* A missing noreturn is ok for the `main' function. */
16065 return DECL_MAIN_P (decl);
16068 /* Return the decl used to identify the COMDAT group into which DECL should
16069 be placed. */
16071 tree
16072 cxx_comdat_group (tree decl)
16074 /* Virtual tables, construction virtual tables, and virtual table
16075 tables all go in a single COMDAT group, named after the primary
16076 virtual table. */
16077 if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl))
16078 decl = CLASSTYPE_VTABLES (DECL_CONTEXT (decl));
16079 /* For all other DECLs, the COMDAT group is the mangled name of the
16080 declaration itself. */
16081 else
16083 while (DECL_THUNK_P (decl))
16085 /* If TARGET_USE_LOCAL_THUNK_ALIAS_P, use_thunk puts the thunk
16086 into the same section as the target function. In that case
16087 we must return target's name. */
16088 tree target = THUNK_TARGET (decl);
16089 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (target)
16090 && DECL_SECTION_NAME (target) != NULL
16091 && DECL_ONE_ONLY (target))
16092 decl = target;
16093 else
16094 break;
16098 return decl;
16101 /* Returns the return type for FN as written by the user, which may include
16102 a placeholder for a deduced return type. */
16104 tree
16105 fndecl_declared_return_type (tree fn)
16107 fn = STRIP_TEMPLATE (fn);
16108 if (FNDECL_USED_AUTO (fn))
16110 struct language_function *f = NULL;
16111 if (DECL_STRUCT_FUNCTION (fn))
16112 f = DECL_STRUCT_FUNCTION (fn)->language;
16113 if (f == NULL)
16114 f = DECL_SAVED_FUNCTION_DATA (fn);
16115 return f->x_auto_return_pattern;
16117 return TREE_TYPE (TREE_TYPE (fn));
16120 /* Returns true iff DECL was declared with an auto type and it has
16121 not yet been deduced to a real type. */
16123 bool
16124 undeduced_auto_decl (tree decl)
16126 if (cxx_dialect < cxx11)
16127 return false;
16128 return type_uses_auto (TREE_TYPE (decl));
16131 /* Complain if DECL has an undeduced return type. */
16133 bool
16134 require_deduced_type (tree decl, tsubst_flags_t complain)
16136 if (undeduced_auto_decl (decl))
16138 if (complain & tf_error)
16139 error ("use of %qD before deduction of %<auto%>", decl);
16140 return false;
16142 return true;
16145 #include "gt-cp-decl.h"