syscall, internal/syscall/unix: Fix getrandom, clone on sparc64
[official-gcc.git] / gcc / cp / decl.c
blob2d11aefbd692815a301c9f517800b7bf7f1cd4a5
1 /* Process declarations and variables for C++ compiler.
2 Copyright (C) 1988-2016 Free Software Foundation, Inc.
3 Contributed by Michael Tiemann (tiemann@cygnus.com)
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 /* Process declarations and symbol lookup for C++ front end.
23 Also constructs types; the standard scalar types at initialization,
24 and structure, union, array and enum types when they are declared. */
26 /* ??? not all decl nodes are given the most useful possible
27 line numbers. For example, the CONST_DECLs for enum values. */
29 #include "config.h"
30 #include "system.h"
31 #include "coretypes.h"
32 #include "target.h"
33 #include "c-family/c-target.h"
34 #include "cp-tree.h"
35 #include "timevar.h"
36 #include "stringpool.h"
37 #include "cgraph.h"
38 #include "stor-layout.h"
39 #include "varasm.h"
40 #include "attribs.h"
41 #include "flags.h"
42 #include "tree-iterator.h"
43 #include "decl.h"
44 #include "intl.h"
45 #include "toplev.h"
46 #include "c-family/c-objc.h"
47 #include "c-family/c-pragma.h"
48 #include "c-family/c-ubsan.h"
49 #include "debug.h"
50 #include "plugin.h"
51 #include "cilk.h"
52 #include "builtins.h"
54 /* Possible cases of bad specifiers type used by bad_specifiers. */
55 enum bad_spec_place {
56 BSP_VAR, /* variable */
57 BSP_PARM, /* parameter */
58 BSP_TYPE, /* type */
59 BSP_FIELD /* field */
62 static const char *redeclaration_error_message (tree, tree);
64 static int decl_jump_unsafe (tree);
65 static void require_complete_types_for_parms (tree);
66 static int ambi_op_p (enum tree_code);
67 static int unary_op_p (enum tree_code);
68 static void push_local_name (tree);
69 static tree grok_reference_init (tree, tree, tree, int);
70 static tree grokvardecl (tree, tree, tree, const cp_decl_specifier_seq *,
71 int, int, int, tree);
72 static int check_static_variable_definition (tree, tree);
73 static void record_unknown_type (tree, const char *);
74 static tree builtin_function_1 (tree, tree, bool);
75 static int member_function_or_else (tree, tree, enum overload_flags);
76 static void bad_specifiers (tree, enum bad_spec_place, int, int, int, int,
77 int);
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 int walk_namespaces_r (tree, walk_namespaces_fn, void *);
83 static void maybe_deduce_size_from_array_init (tree, tree);
84 static void layout_var_decl (tree);
85 static tree check_initializer (tree, tree, int, vec<tree, va_gc> **);
86 static void make_rtl_for_nonlocal_decl (tree, tree, const char *);
87 static void save_function_data (tree);
88 static void copy_type_enum (tree , tree);
89 static void check_function_type (tree, tree);
90 static void finish_constructor_body (void);
91 static void begin_destructor_body (void);
92 static void finish_destructor_body (void);
93 static void record_key_method_defined (tree);
94 static tree create_array_type_for_decl (tree, tree, tree);
95 static tree get_atexit_node (void);
96 static tree get_dso_handle_node (void);
97 static tree start_cleanup_fn (void);
98 static void end_cleanup_fn (void);
99 static tree cp_make_fname_decl (location_t, tree, int);
100 static void initialize_predefined_identifiers (void);
101 static tree check_special_function_return_type
102 (special_function_kind, tree, tree, int, const location_t*);
103 static tree push_cp_library_fn (enum tree_code, tree, int);
104 static tree build_cp_library_fn (tree, enum tree_code, tree, int);
105 static void store_parm_decls (tree);
106 static void initialize_local_var (tree, tree);
107 static void expand_static_init (tree, tree);
109 /* The following symbols are subsumed in the cp_global_trees array, and
110 listed here individually for documentation purposes.
112 C++ extensions
113 tree wchar_decl_node;
115 tree vtable_entry_type;
116 tree delta_type_node;
117 tree __t_desc_type_node;
119 tree class_type_node;
120 tree unknown_type_node;
122 Array type `vtable_entry_type[]'
124 tree vtbl_type_node;
125 tree vtbl_ptr_type_node;
127 Namespaces,
129 tree std_node;
130 tree abi_node;
132 A FUNCTION_DECL which can call `abort'. Not necessarily the
133 one that the user will declare, but sufficient to be called
134 by routines that want to abort the program.
136 tree abort_fndecl;
138 Used by RTTI
139 tree type_info_type_node, tinfo_decl_id, tinfo_decl_type;
140 tree tinfo_var_id; */
142 tree cp_global_trees[CPTI_MAX];
144 /* Indicates that there is a type value in some namespace, although
145 that is not necessarily in scope at the moment. */
147 tree global_type_node;
149 /* The node that holds the "name" of the global scope. */
150 tree global_scope_name;
152 #define local_names cp_function_chain->x_local_names
154 /* A list of objects which have constructors or destructors
155 which reside in the global scope. The decl is stored in
156 the TREE_VALUE slot and the initializer is stored
157 in the TREE_PURPOSE slot. */
158 tree static_aggregates;
160 /* Like static_aggregates, but for thread_local variables. */
161 tree tls_aggregates;
163 /* -- end of C++ */
165 /* A node for the integer constant 2. */
167 tree integer_two_node;
169 /* Used only for jumps to as-yet undefined labels, since jumps to
170 defined labels can have their validity checked immediately. */
172 struct GTY((chain_next ("%h.next"))) named_label_use_entry {
173 struct named_label_use_entry *next;
174 /* The binding level to which this entry is *currently* attached.
175 This is initially the binding level in which the goto appeared,
176 but is modified as scopes are closed. */
177 cp_binding_level *binding_level;
178 /* The head of the names list that was current when the goto appeared,
179 or the inner scope popped. These are the decls that will *not* be
180 skipped when jumping to the label. */
181 tree names_in_scope;
182 /* The location of the goto, for error reporting. */
183 location_t o_goto_locus;
184 /* True if an OpenMP structured block scope has been closed since
185 the goto appeared. This means that the branch from the label will
186 illegally exit an OpenMP scope. */
187 bool in_omp_scope;
190 /* A list of all LABEL_DECLs in the function that have names. Here so
191 we can clear out their names' definitions at the end of the
192 function, and so we can check the validity of jumps to these labels. */
194 struct GTY((for_user)) named_label_entry {
195 /* The decl itself. */
196 tree label_decl;
198 /* The binding level to which the label is *currently* attached.
199 This is initially set to the binding level in which the label
200 is defined, but is modified as scopes are closed. */
201 cp_binding_level *binding_level;
202 /* The head of the names list that was current when the label was
203 defined, or the inner scope popped. These are the decls that will
204 be skipped when jumping to the label. */
205 tree names_in_scope;
206 /* A vector of all decls from all binding levels that would be
207 crossed by a backward branch to the label. */
208 vec<tree, va_gc> *bad_decls;
210 /* A list of uses of the label, before the label is defined. */
211 struct named_label_use_entry *uses;
213 /* The following bits are set after the label is defined, and are
214 updated as scopes are popped. They indicate that a backward jump
215 to the label will illegally enter a scope of the given flavor. */
216 bool in_try_scope;
217 bool in_catch_scope;
218 bool in_omp_scope;
219 bool in_transaction_scope;
220 bool in_constexpr_if;
223 #define named_labels cp_function_chain->x_named_labels
225 /* The number of function bodies which we are currently processing.
226 (Zero if we are at namespace scope, one inside the body of a
227 function, two inside the body of a function in a local class, etc.) */
228 int function_depth;
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);
591 if (current_binding_level->kind == sk_cleanup)
592 functionbody = 0;
593 subblocks = functionbody >= 0 ? current_binding_level->blocks : 0;
595 gcc_assert (!vec_safe_length (current_binding_level->class_shadowed));
597 /* We used to use KEEP == 2 to indicate that the new block should go
598 at the beginning of the list of blocks at this binding level,
599 rather than the end. This hack is no longer used. */
600 gcc_assert (keep == 0 || keep == 1);
602 if (current_binding_level->keep)
603 keep = 1;
605 /* Any uses of undefined labels, and any defined labels, now operate
606 under constraints of next binding contour. */
607 if (cfun && !functionbody && named_labels)
608 named_labels->traverse<cp_binding_level *, poplevel_named_label_1>
609 (current_binding_level);
611 /* Get the decls in the order they were written.
612 Usually current_binding_level->names is in reverse order.
613 But parameter decls were previously put in forward order. */
615 if (reverse)
616 current_binding_level->names
617 = decls = nreverse (current_binding_level->names);
618 else
619 decls = current_binding_level->names;
621 /* If there were any declarations or structure tags in that level,
622 or if this level is a function body,
623 create a BLOCK to record them for the life of this function. */
624 block = NULL_TREE;
625 /* Avoid function body block if possible. */
626 if (functionbody && subblocks && BLOCK_CHAIN (subblocks) == NULL_TREE)
627 keep = 0;
628 else if (keep == 1 || functionbody)
629 block = make_node (BLOCK);
630 if (block != NULL_TREE)
632 BLOCK_VARS (block) = decls;
633 BLOCK_SUBBLOCKS (block) = subblocks;
636 /* In each subblock, record that this is its superior. */
637 if (keep >= 0)
638 for (link = subblocks; link; link = BLOCK_CHAIN (link))
639 BLOCK_SUPERCONTEXT (link) = block;
641 /* We still support the old for-scope rules, whereby the variables
642 in a init statement were in scope after the for-statement ended.
643 We only use the new rules if flag_new_for_scope is nonzero. */
644 leaving_for_scope
645 = current_binding_level->kind == sk_for && flag_new_for_scope == 1;
647 /* Before we remove the declarations first check for unused variables. */
648 if ((warn_unused_variable || warn_unused_but_set_variable)
649 && current_binding_level->kind != sk_template_parms
650 && !processing_template_decl)
651 for (tree d = getdecls (); d; d = TREE_CHAIN (d))
653 /* There are cases where D itself is a TREE_LIST. See in
654 push_local_binding where the list of decls returned by
655 getdecls is built. */
656 decl = TREE_CODE (d) == TREE_LIST ? TREE_VALUE (d) : d;
657 tree type = TREE_TYPE (decl);
658 if (VAR_P (decl)
659 && (! TREE_USED (decl) || !DECL_READ_P (decl))
660 && ! DECL_IN_SYSTEM_HEADER (decl)
661 && DECL_NAME (decl) && ! DECL_ARTIFICIAL (decl)
662 && type != error_mark_node
663 && (!CLASS_TYPE_P (type)
664 || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
665 || lookup_attribute ("warn_unused",
666 TYPE_ATTRIBUTES (TREE_TYPE (decl)))))
668 if (! TREE_USED (decl))
669 warning_at (DECL_SOURCE_LOCATION (decl),
670 OPT_Wunused_variable, "unused variable %qD", decl);
671 else if (DECL_CONTEXT (decl) == current_function_decl
672 // For -Wunused-but-set-variable leave references alone.
673 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
674 && errorcount == unused_but_set_errorcount)
676 warning_at (DECL_SOURCE_LOCATION (decl),
677 OPT_Wunused_but_set_variable,
678 "variable %qD set but not used", decl);
679 unused_but_set_errorcount = errorcount;
684 /* Remove declarations for all the DECLs in this level. */
685 for (link = decls; link; link = TREE_CHAIN (link))
687 if (leaving_for_scope && VAR_P (link)
688 /* It's hard to make this ARM compatibility hack play nicely with
689 lambdas, and it really isn't necessary in C++11 mode. */
690 && cxx_dialect < cxx11
691 && DECL_NAME (link))
693 tree name = DECL_NAME (link);
694 cxx_binding *ob;
695 tree ns_binding;
697 ob = outer_binding (name,
698 IDENTIFIER_BINDING (name),
699 /*class_p=*/true);
700 if (!ob)
701 ns_binding = IDENTIFIER_NAMESPACE_VALUE (name);
702 else
703 ns_binding = NULL_TREE;
705 if (ob && ob->scope == current_binding_level->level_chain)
706 /* We have something like:
708 int i;
709 for (int i; ;);
711 and we are leaving the `for' scope. There's no reason to
712 keep the binding of the inner `i' in this case. */
713 pop_binding (name, link);
714 else if ((ob && (TREE_CODE (ob->value) == TYPE_DECL))
715 || (ns_binding && TREE_CODE (ns_binding) == TYPE_DECL))
716 /* Here, we have something like:
718 typedef int I;
720 void f () {
721 for (int I; ;);
724 We must pop the for-scope binding so we know what's a
725 type and what isn't. */
726 pop_binding (name, link);
727 else
729 /* Mark this VAR_DECL as dead so that we can tell we left it
730 there only for backward compatibility. */
731 DECL_DEAD_FOR_LOCAL (link) = 1;
733 /* Keep track of what should have happened when we
734 popped the binding. */
735 if (ob && ob->value)
737 SET_DECL_SHADOWED_FOR_VAR (link, ob->value);
738 DECL_HAS_SHADOWED_FOR_VAR_P (link) = 1;
741 /* Add it to the list of dead variables in the next
742 outermost binding to that we can remove these when we
743 leave that binding. */
744 vec_safe_push (
745 current_binding_level->level_chain->dead_vars_from_for,
746 link);
748 /* Although we don't pop the cxx_binding, we do clear
749 its SCOPE since the scope is going away now. */
750 IDENTIFIER_BINDING (name)->scope
751 = current_binding_level->level_chain;
754 else
756 tree name;
758 /* Remove the binding. */
759 decl = link;
761 if (TREE_CODE (decl) == TREE_LIST)
762 decl = TREE_VALUE (decl);
763 name = decl;
765 if (TREE_CODE (name) == OVERLOAD)
766 name = OVL_FUNCTION (name);
768 gcc_assert (DECL_P (name));
769 pop_binding (DECL_NAME (name), decl);
773 /* Remove declarations for any `for' variables from inner scopes
774 that we kept around. */
775 FOR_EACH_VEC_SAFE_ELT_REVERSE (current_binding_level->dead_vars_from_for,
776 ix, decl)
777 pop_binding (DECL_NAME (decl), decl);
779 /* Restore the IDENTIFIER_TYPE_VALUEs. */
780 for (link = current_binding_level->type_shadowed;
781 link; link = TREE_CHAIN (link))
782 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link), TREE_VALUE (link));
784 /* Restore the IDENTIFIER_LABEL_VALUEs for local labels. */
785 FOR_EACH_VEC_SAFE_ELT_REVERSE (current_binding_level->shadowed_labels,
786 ix, label_bind)
787 pop_local_label (label_bind->label, label_bind->prev_value);
789 /* There may be OVERLOADs (wrapped in TREE_LISTs) on the BLOCK_VARs
790 list if a `using' declaration put them there. The debugging
791 back ends won't understand OVERLOAD, so we remove them here.
792 Because the BLOCK_VARS are (temporarily) shared with
793 CURRENT_BINDING_LEVEL->NAMES we must do this fixup after we have
794 popped all the bindings. */
795 if (block)
797 tree* d;
799 for (d = &BLOCK_VARS (block); *d; )
801 if (TREE_CODE (*d) == TREE_LIST)
802 *d = TREE_CHAIN (*d);
803 else
804 d = &DECL_CHAIN (*d);
808 /* If the level being exited is the top level of a function,
809 check over all the labels. */
810 if (functionbody)
812 if (block)
814 /* Since this is the top level block of a function, the vars are
815 the function's parameters. Don't leave them in the BLOCK
816 because they are found in the FUNCTION_DECL instead. */
817 BLOCK_VARS (block) = 0;
818 pop_labels (block);
820 else
821 pop_labels (subblocks);
824 kind = current_binding_level->kind;
825 if (kind == sk_cleanup)
827 tree stmt;
829 /* If this is a temporary binding created for a cleanup, then we'll
830 have pushed a statement list level. Pop that, create a new
831 BIND_EXPR for the block, and insert it into the stream. */
832 stmt = pop_stmt_list (current_binding_level->statement_list);
833 stmt = c_build_bind_expr (input_location, block, stmt);
834 add_stmt (stmt);
837 leave_scope ();
838 if (functionbody)
840 /* The current function is being defined, so its DECL_INITIAL
841 should be error_mark_node. */
842 gcc_assert (DECL_INITIAL (current_function_decl) == error_mark_node);
843 DECL_INITIAL (current_function_decl) = block ? block : subblocks;
844 if (subblocks)
846 if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
848 if (BLOCK_SUBBLOCKS (subblocks))
849 BLOCK_OUTER_CURLY_BRACE_P (BLOCK_SUBBLOCKS (subblocks)) = 1;
851 else
852 BLOCK_OUTER_CURLY_BRACE_P (subblocks) = 1;
855 else if (block)
856 current_binding_level->blocks
857 = block_chainon (current_binding_level->blocks, block);
859 /* If we did not make a block for the level just exited,
860 any blocks made for inner levels
861 (since they cannot be recorded as subblocks in that level)
862 must be carried forward so they will later become subblocks
863 of something else. */
864 else if (subblocks)
865 current_binding_level->blocks
866 = block_chainon (current_binding_level->blocks, subblocks);
868 /* Each and every BLOCK node created here in `poplevel' is important
869 (e.g. for proper debugging information) so if we created one
870 earlier, mark it as "used". */
871 if (block)
872 TREE_USED (block) = 1;
874 /* All temporary bindings created for cleanups are popped silently. */
875 if (kind == sk_cleanup)
876 goto restart;
878 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
879 return block;
882 /* Walk all the namespaces contained NAMESPACE, including NAMESPACE
883 itself, calling F for each. The DATA is passed to F as well. */
885 static int
886 walk_namespaces_r (tree name_space, walk_namespaces_fn f, void* data)
888 int result = 0;
889 tree current = NAMESPACE_LEVEL (name_space)->namespaces;
891 result |= (*f) (name_space, data);
893 for (; current; current = DECL_CHAIN (current))
894 result |= walk_namespaces_r (current, f, data);
896 return result;
899 /* Walk all the namespaces, calling F for each. The DATA is passed to
900 F as well. */
903 walk_namespaces (walk_namespaces_fn f, void* data)
905 return walk_namespaces_r (global_namespace, f, data);
908 /* Call wrapup_globals_declarations for the globals in NAMESPACE. */
911 wrapup_globals_for_namespace (tree name_space, void* data ATTRIBUTE_UNUSED)
913 cp_binding_level *level = NAMESPACE_LEVEL (name_space);
914 vec<tree, va_gc> *statics = level->static_decls;
915 tree *vec = statics->address ();
916 int len = statics->length ();
918 if (warn_unused_function)
920 tree decl;
921 unsigned int i;
922 FOR_EACH_VEC_SAFE_ELT (statics, i, decl)
923 if (TREE_CODE (decl) == FUNCTION_DECL
924 && DECL_INITIAL (decl) == 0
925 && DECL_EXTERNAL (decl)
926 && !TREE_PUBLIC (decl)
927 && !DECL_ARTIFICIAL (decl)
928 && !TREE_NO_WARNING (decl))
930 warning_at (DECL_SOURCE_LOCATION (decl),
931 OPT_Wunused_function,
932 "%qF declared %<static%> but never defined", decl);
933 TREE_NO_WARNING (decl) = 1;
937 /* Write out any globals that need to be output. */
938 return wrapup_global_declarations (vec, len);
941 /* In C++, you don't have to write `struct S' to refer to `S'; you
942 can just use `S'. We accomplish this by creating a TYPE_DECL as
943 if the user had written `typedef struct S S'. Create and return
944 the TYPE_DECL for TYPE. */
946 tree
947 create_implicit_typedef (tree name, tree type)
949 tree decl;
951 decl = build_decl (input_location, TYPE_DECL, name, type);
952 DECL_ARTIFICIAL (decl) = 1;
953 /* There are other implicit type declarations, like the one *within*
954 a class that allows you to write `S::S'. We must distinguish
955 amongst these. */
956 SET_DECL_IMPLICIT_TYPEDEF_P (decl);
957 TYPE_NAME (type) = decl;
958 TYPE_STUB_DECL (type) = decl;
960 return decl;
963 /* Remember a local name for name-mangling purposes. */
965 static void
966 push_local_name (tree decl)
968 size_t i, nelts;
969 tree t, name;
971 timevar_start (TV_NAME_LOOKUP);
973 name = DECL_NAME (decl);
975 nelts = vec_safe_length (local_names);
976 for (i = 0; i < nelts; i++)
978 t = (*local_names)[i];
979 if (DECL_NAME (t) == name)
981 if (!DECL_LANG_SPECIFIC (decl))
982 retrofit_lang_decl (decl);
983 DECL_LANG_SPECIFIC (decl)->u.base.u2sel = 1;
984 if (DECL_DISCRIMINATOR_SET_P (t))
985 DECL_DISCRIMINATOR (decl) = DECL_DISCRIMINATOR (t) + 1;
986 else
987 DECL_DISCRIMINATOR (decl) = 1;
989 (*local_names)[i] = decl;
990 timevar_stop (TV_NAME_LOOKUP);
991 return;
995 vec_safe_push (local_names, decl);
996 timevar_stop (TV_NAME_LOOKUP);
999 /* Subroutine of duplicate_decls: return truthvalue of whether
1000 or not types of these decls match.
1002 For C++, we must compare the parameter list so that `int' can match
1003 `int&' in a parameter position, but `int&' is not confused with
1004 `const int&'. */
1007 decls_match (tree newdecl, tree olddecl)
1009 int types_match;
1011 if (newdecl == olddecl)
1012 return 1;
1014 if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
1015 /* If the two DECLs are not even the same kind of thing, we're not
1016 interested in their types. */
1017 return 0;
1019 gcc_assert (DECL_P (newdecl));
1021 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1023 tree f1 = TREE_TYPE (newdecl);
1024 tree f2 = TREE_TYPE (olddecl);
1025 tree p1 = TYPE_ARG_TYPES (f1);
1026 tree p2 = TYPE_ARG_TYPES (f2);
1027 tree r2;
1029 /* Specializations of different templates are different functions
1030 even if they have the same type. */
1031 tree t1 = (DECL_USE_TEMPLATE (newdecl)
1032 ? DECL_TI_TEMPLATE (newdecl)
1033 : NULL_TREE);
1034 tree t2 = (DECL_USE_TEMPLATE (olddecl)
1035 ? DECL_TI_TEMPLATE (olddecl)
1036 : NULL_TREE);
1037 if (t1 != t2)
1038 return 0;
1040 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
1041 && ! (DECL_EXTERN_C_P (newdecl)
1042 && DECL_EXTERN_C_P (olddecl)))
1043 return 0;
1045 /* A new declaration doesn't match a built-in one unless it
1046 is also extern "C". */
1047 if (DECL_IS_BUILTIN (olddecl)
1048 && DECL_EXTERN_C_P (olddecl) && !DECL_EXTERN_C_P (newdecl))
1049 return 0;
1051 if (TREE_CODE (f1) != TREE_CODE (f2))
1052 return 0;
1054 /* A declaration with deduced return type should use its pre-deduction
1055 type for declaration matching. */
1056 r2 = fndecl_declared_return_type (olddecl);
1058 if (same_type_p (TREE_TYPE (f1), r2))
1060 if (!prototype_p (f2) && DECL_EXTERN_C_P (olddecl)
1061 && (DECL_BUILT_IN (olddecl)
1062 #ifndef NO_IMPLICIT_EXTERN_C
1063 || (DECL_IN_SYSTEM_HEADER (newdecl) && !DECL_CLASS_SCOPE_P (newdecl))
1064 || (DECL_IN_SYSTEM_HEADER (olddecl) && !DECL_CLASS_SCOPE_P (olddecl))
1065 #endif
1068 types_match = self_promoting_args_p (p1);
1069 if (p1 == void_list_node)
1070 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
1072 #ifndef NO_IMPLICIT_EXTERN_C
1073 else if (!prototype_p (f1)
1074 && (DECL_EXTERN_C_P (olddecl)
1075 && DECL_IN_SYSTEM_HEADER (olddecl)
1076 && !DECL_CLASS_SCOPE_P (olddecl))
1077 && (DECL_EXTERN_C_P (newdecl)
1078 && DECL_IN_SYSTEM_HEADER (newdecl)
1079 && !DECL_CLASS_SCOPE_P (newdecl)))
1081 types_match = self_promoting_args_p (p2);
1082 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
1084 #endif
1085 else
1086 types_match =
1087 compparms (p1, p2)
1088 && type_memfn_rqual (f1) == type_memfn_rqual (f2)
1089 && (TYPE_ATTRIBUTES (TREE_TYPE (newdecl)) == NULL_TREE
1090 || comp_type_attributes (TREE_TYPE (newdecl),
1091 TREE_TYPE (olddecl)) != 0);
1093 else
1094 types_match = 0;
1096 /* The decls dont match if they correspond to two different versions
1097 of the same function. Disallow extern "C" functions to be
1098 versions for now. */
1099 if (types_match
1100 && !DECL_EXTERN_C_P (newdecl)
1101 && !DECL_EXTERN_C_P (olddecl)
1102 && targetm.target_option.function_versions (newdecl, olddecl))
1104 /* Mark functions as versions if necessary. Modify the mangled decl
1105 name if necessary. */
1106 if (DECL_FUNCTION_VERSIONED (newdecl)
1107 && DECL_FUNCTION_VERSIONED (olddecl))
1108 return 0;
1109 if (!DECL_FUNCTION_VERSIONED (newdecl))
1111 DECL_FUNCTION_VERSIONED (newdecl) = 1;
1112 if (DECL_ASSEMBLER_NAME_SET_P (newdecl))
1113 mangle_decl (newdecl);
1115 if (!DECL_FUNCTION_VERSIONED (olddecl))
1117 DECL_FUNCTION_VERSIONED (olddecl) = 1;
1118 if (DECL_ASSEMBLER_NAME_SET_P (olddecl))
1119 mangle_decl (olddecl);
1121 cgraph_node::record_function_versions (olddecl, newdecl);
1122 return 0;
1125 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1127 tree oldres = DECL_TEMPLATE_RESULT (olddecl);
1128 tree newres = DECL_TEMPLATE_RESULT (newdecl);
1130 if (TREE_CODE (newres) != TREE_CODE (oldres))
1131 return 0;
1133 if (!comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
1134 DECL_TEMPLATE_PARMS (olddecl)))
1135 return 0;
1137 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
1138 types_match = (same_type_p (TREE_TYPE (oldres), TREE_TYPE (newres))
1139 && equivalently_constrained (olddecl, newdecl));
1140 else
1141 // We don't need to check equivalently_constrained for variable and
1142 // function templates because we check it on the results.
1143 types_match = decls_match (oldres, newres);
1145 else
1147 /* Need to check scope for variable declaration (VAR_DECL).
1148 For typedef (TYPE_DECL), scope is ignored. */
1149 if (VAR_P (newdecl)
1150 && CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
1151 /* [dcl.link]
1152 Two declarations for an object with C language linkage
1153 with the same name (ignoring the namespace that qualify
1154 it) that appear in different namespace scopes refer to
1155 the same object. */
1156 && !(DECL_EXTERN_C_P (olddecl) && DECL_EXTERN_C_P (newdecl)))
1157 return 0;
1159 if (TREE_TYPE (newdecl) == error_mark_node)
1160 types_match = TREE_TYPE (olddecl) == error_mark_node;
1161 else if (TREE_TYPE (olddecl) == NULL_TREE)
1162 types_match = TREE_TYPE (newdecl) == NULL_TREE;
1163 else if (TREE_TYPE (newdecl) == NULL_TREE)
1164 types_match = 0;
1165 else
1166 types_match = comptypes (TREE_TYPE (newdecl),
1167 TREE_TYPE (olddecl),
1168 COMPARE_REDECLARATION);
1171 // Normal functions can be constrained, as can variable partial
1172 // specializations.
1173 if (types_match && VAR_OR_FUNCTION_DECL_P (newdecl))
1174 types_match = equivalently_constrained (newdecl, olddecl);
1176 return types_match;
1179 /* If NEWDECL is `static' and an `extern' was seen previously,
1180 warn about it. OLDDECL is the previous declaration.
1182 Note that this does not apply to the C++ case of declaring
1183 a variable `extern const' and then later `const'.
1185 Don't complain about built-in functions, since they are beyond
1186 the user's control. */
1188 void
1189 warn_extern_redeclared_static (tree newdecl, tree olddecl)
1191 if (TREE_CODE (newdecl) == TYPE_DECL
1192 || TREE_CODE (newdecl) == TEMPLATE_DECL
1193 || TREE_CODE (newdecl) == CONST_DECL
1194 || TREE_CODE (newdecl) == NAMESPACE_DECL)
1195 return;
1197 /* Don't get confused by static member functions; that's a different
1198 use of `static'. */
1199 if (TREE_CODE (newdecl) == FUNCTION_DECL
1200 && DECL_STATIC_FUNCTION_P (newdecl))
1201 return;
1203 /* If the old declaration was `static', or the new one isn't, then
1204 everything is OK. */
1205 if (DECL_THIS_STATIC (olddecl) || !DECL_THIS_STATIC (newdecl))
1206 return;
1208 /* It's OK to declare a builtin function as `static'. */
1209 if (TREE_CODE (olddecl) == FUNCTION_DECL
1210 && DECL_ARTIFICIAL (olddecl))
1211 return;
1213 if (permerror (DECL_SOURCE_LOCATION (newdecl),
1214 "%qD was declared %<extern%> and later %<static%>", newdecl))
1215 inform (DECL_SOURCE_LOCATION (olddecl),
1216 "previous declaration of %qD", olddecl);
1219 /* NEW_DECL is a redeclaration of OLD_DECL; both are functions or
1220 function templates. If their exception specifications do not
1221 match, issue a diagnostic. */
1223 static void
1224 check_redeclaration_exception_specification (tree new_decl,
1225 tree old_decl)
1227 tree new_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (new_decl));
1228 tree old_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old_decl));
1230 /* Two default specs are equivalent, don't force evaluation. */
1231 if (UNEVALUATED_NOEXCEPT_SPEC_P (new_exceptions)
1232 && UNEVALUATED_NOEXCEPT_SPEC_P (old_exceptions))
1233 return;
1235 maybe_instantiate_noexcept (new_decl);
1236 maybe_instantiate_noexcept (old_decl);
1237 new_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (new_decl));
1238 old_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old_decl));
1240 /* [except.spec]
1242 If any declaration of a function has an exception-specification,
1243 all declarations, including the definition and an explicit
1244 specialization, of that function shall have an
1245 exception-specification with the same set of type-ids. */
1246 if (! DECL_IS_BUILTIN (old_decl)
1247 && !comp_except_specs (new_exceptions, old_exceptions, ce_normal))
1249 const char *msg
1250 = "declaration of %qF has a different exception specifier";
1251 bool complained = true;
1252 location_t new_loc = DECL_SOURCE_LOCATION (new_decl);
1253 if (DECL_IN_SYSTEM_HEADER (old_decl))
1254 complained = pedwarn (new_loc, OPT_Wsystem_headers, msg, new_decl);
1255 else if (!flag_exceptions)
1256 /* We used to silently permit mismatched eh specs with
1257 -fno-exceptions, so make them a pedwarn now. */
1258 complained = pedwarn (new_loc, OPT_Wpedantic, msg, new_decl);
1259 else
1260 error_at (new_loc, msg, new_decl);
1261 if (complained)
1262 inform (DECL_SOURCE_LOCATION (old_decl),
1263 "from previous declaration %qF", old_decl);
1267 /* Return true if OLD_DECL and NEW_DECL agree on constexprness.
1268 Otherwise issue diagnostics. */
1270 static bool
1271 validate_constexpr_redeclaration (tree old_decl, tree new_decl)
1273 old_decl = STRIP_TEMPLATE (old_decl);
1274 new_decl = STRIP_TEMPLATE (new_decl);
1275 if (!VAR_OR_FUNCTION_DECL_P (old_decl)
1276 || !VAR_OR_FUNCTION_DECL_P (new_decl))
1277 return true;
1278 if (DECL_DECLARED_CONSTEXPR_P (old_decl)
1279 == DECL_DECLARED_CONSTEXPR_P (new_decl))
1280 return true;
1281 if (TREE_CODE (old_decl) == FUNCTION_DECL)
1283 if (DECL_BUILT_IN (old_decl))
1285 /* Hide a built-in declaration. */
1286 DECL_DECLARED_CONSTEXPR_P (old_decl)
1287 = DECL_DECLARED_CONSTEXPR_P (new_decl);
1288 return true;
1290 /* 7.1.5 [dcl.constexpr]
1291 Note: An explicit specialization can differ from the template
1292 declaration with respect to the constexpr specifier. */
1293 if (! DECL_TEMPLATE_SPECIALIZATION (old_decl)
1294 && DECL_TEMPLATE_SPECIALIZATION (new_decl))
1295 return true;
1297 error_at (DECL_SOURCE_LOCATION (new_decl),
1298 "redeclaration %qD differs in %<constexpr%> "
1299 "from previous declaration", new_decl);
1300 inform (DECL_SOURCE_LOCATION (old_decl),
1301 "previous declaration %qD", old_decl);
1302 return false;
1304 return true;
1307 // If OLDDECL and NEWDECL are concept declarations with the same type
1308 // (i.e., and template parameters), but different requirements,
1309 // emit diagnostics and return true. Otherwise, return false.
1310 static inline bool
1311 check_concept_refinement (tree olddecl, tree newdecl)
1313 if (!DECL_DECLARED_CONCEPT_P (olddecl) || !DECL_DECLARED_CONCEPT_P (newdecl))
1314 return false;
1316 tree d1 = DECL_TEMPLATE_RESULT (olddecl);
1317 tree d2 = DECL_TEMPLATE_RESULT (newdecl);
1318 if (TREE_CODE (d1) != TREE_CODE (d2))
1319 return false;
1321 tree t1 = TREE_TYPE (d1);
1322 tree t2 = TREE_TYPE (d2);
1323 if (TREE_CODE (d1) == FUNCTION_DECL)
1325 if (compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2))
1326 && comp_template_parms (DECL_TEMPLATE_PARMS (olddecl),
1327 DECL_TEMPLATE_PARMS (newdecl))
1328 && !equivalently_constrained (olddecl, newdecl))
1330 error ("cannot specialize concept %q#D", olddecl);
1331 return true;
1334 return false;
1337 /* DECL is a redeclaration of a function or function template. If
1338 it does have default arguments issue a diagnostic. Note: this
1339 function is used to enforce the requirements in C++11 8.3.6 about
1340 no default arguments in redeclarations. */
1342 static void
1343 check_redeclaration_no_default_args (tree decl)
1345 gcc_assert (DECL_DECLARES_FUNCTION_P (decl));
1347 for (tree t = FUNCTION_FIRST_USER_PARMTYPE (decl);
1348 t && t != void_list_node; t = TREE_CHAIN (t))
1349 if (TREE_PURPOSE (t))
1351 permerror (DECL_SOURCE_LOCATION (decl),
1352 "redeclaration of %q#D may not have default "
1353 "arguments", decl);
1354 return;
1358 #define GNU_INLINE_P(fn) (DECL_DECLARED_INLINE_P (fn) \
1359 && lookup_attribute ("gnu_inline", \
1360 DECL_ATTRIBUTES (fn)))
1362 /* If NEWDECL is a redeclaration of OLDDECL, merge the declarations.
1363 If the redeclaration is invalid, a diagnostic is issued, and the
1364 error_mark_node is returned. Otherwise, OLDDECL is returned.
1366 If NEWDECL is not a redeclaration of OLDDECL, NULL_TREE is
1367 returned.
1369 NEWDECL_IS_FRIEND is true if NEWDECL was declared as a friend. */
1371 tree
1372 duplicate_decls (tree newdecl, tree olddecl, bool newdecl_is_friend)
1374 unsigned olddecl_uid = DECL_UID (olddecl);
1375 int olddecl_friend = 0, types_match = 0, hidden_friend = 0;
1376 int new_defines_function = 0;
1377 tree new_template_info;
1379 if (newdecl == olddecl)
1380 return olddecl;
1382 types_match = decls_match (newdecl, olddecl);
1384 /* If either the type of the new decl or the type of the old decl is an
1385 error_mark_node, then that implies that we have already issued an
1386 error (earlier) for some bogus type specification, and in that case,
1387 it is rather pointless to harass the user with yet more error message
1388 about the same declaration, so just pretend the types match here. */
1389 if (TREE_TYPE (newdecl) == error_mark_node
1390 || TREE_TYPE (olddecl) == error_mark_node)
1391 return error_mark_node;
1393 if (UDLIT_OPER_P (DECL_NAME (newdecl))
1394 && UDLIT_OPER_P (DECL_NAME (olddecl)))
1396 if (TREE_CODE (newdecl) == TEMPLATE_DECL
1397 && TREE_CODE (olddecl) != TEMPLATE_DECL
1398 && check_raw_literal_operator (olddecl))
1399 error ("literal operator template %q+D conflicts with"
1400 " raw literal operator %qD", newdecl, olddecl);
1401 else if (TREE_CODE (newdecl) != TEMPLATE_DECL
1402 && TREE_CODE (olddecl) == TEMPLATE_DECL
1403 && check_raw_literal_operator (newdecl))
1404 error ("raw literal operator %q+D conflicts with"
1405 " literal operator template %qD", newdecl, olddecl);
1408 if (DECL_P (olddecl)
1409 && TREE_CODE (newdecl) == FUNCTION_DECL
1410 && TREE_CODE (olddecl) == FUNCTION_DECL
1411 && diagnose_mismatched_attributes (olddecl, newdecl))
1413 if (DECL_INITIAL (olddecl))
1414 inform (DECL_SOURCE_LOCATION (olddecl),
1415 "previous definition of %qD was here", olddecl);
1416 else
1417 inform (DECL_SOURCE_LOCATION (olddecl),
1418 "previous declaration of %qD was here", olddecl);
1421 /* Check for redeclaration and other discrepancies. */
1422 if (TREE_CODE (olddecl) == FUNCTION_DECL
1423 && DECL_ARTIFICIAL (olddecl))
1425 gcc_assert (!DECL_HIDDEN_FRIEND_P (olddecl));
1426 if (TREE_CODE (newdecl) != FUNCTION_DECL)
1428 /* Avoid warnings redeclaring built-ins which have not been
1429 explicitly declared. */
1430 if (DECL_ANTICIPATED (olddecl))
1431 return NULL_TREE;
1433 /* If you declare a built-in or predefined function name as static,
1434 the old definition is overridden, but optionally warn this was a
1435 bad choice of name. */
1436 if (! TREE_PUBLIC (newdecl))
1438 warning (OPT_Wshadow,
1439 DECL_BUILT_IN (olddecl)
1440 ? G_("shadowing built-in function %q#D")
1441 : G_("shadowing library function %q#D"), olddecl);
1442 /* Discard the old built-in function. */
1443 return NULL_TREE;
1445 /* If the built-in is not ansi, then programs can override
1446 it even globally without an error. */
1447 else if (! DECL_BUILT_IN (olddecl))
1448 warning_at (DECL_SOURCE_LOCATION (newdecl), 0,
1449 "library function %q#D redeclared as non-function %q#D",
1450 olddecl, newdecl);
1451 else
1452 error ("declaration of %q+#D conflicts with built-in "
1453 "declaration %q#D", newdecl, olddecl);
1454 return NULL_TREE;
1456 else if (DECL_OMP_DECLARE_REDUCTION_P (olddecl))
1458 gcc_assert (DECL_OMP_DECLARE_REDUCTION_P (newdecl));
1459 error_at (DECL_SOURCE_LOCATION (newdecl),
1460 "redeclaration of %<pragma omp declare reduction%>");
1461 inform (DECL_SOURCE_LOCATION (olddecl),
1462 "previous %<pragma omp declare reduction%> declaration");
1463 return error_mark_node;
1465 else if (!types_match)
1467 /* Avoid warnings redeclaring built-ins which have not been
1468 explicitly declared. */
1469 if (DECL_ANTICIPATED (olddecl))
1471 /* Deal with fileptr_type_node. FILE type is not known
1472 at the time we create the builtins. */
1473 tree t1, t2;
1475 for (t1 = TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1476 t2 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1477 t1 || t2;
1478 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1479 if (!t1 || !t2)
1480 break;
1481 else if (TREE_VALUE (t2) == fileptr_type_node)
1483 tree t = TREE_VALUE (t1);
1485 if (TYPE_PTR_P (t)
1486 && TYPE_IDENTIFIER (TREE_TYPE (t))
1487 == get_identifier ("FILE")
1488 && compparms (TREE_CHAIN (t1), TREE_CHAIN (t2)))
1490 tree oldargs = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1492 TYPE_ARG_TYPES (TREE_TYPE (olddecl))
1493 = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
1494 types_match = decls_match (newdecl, olddecl);
1495 if (types_match)
1496 return duplicate_decls (newdecl, olddecl,
1497 newdecl_is_friend);
1498 TYPE_ARG_TYPES (TREE_TYPE (olddecl)) = oldargs;
1501 else if (! same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1502 break;
1504 else if ((DECL_EXTERN_C_P (newdecl)
1505 && DECL_EXTERN_C_P (olddecl))
1506 || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1507 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
1509 /* A near match; override the builtin. */
1511 if (TREE_PUBLIC (newdecl))
1512 warning_at (DECL_SOURCE_LOCATION (newdecl), 0,
1513 "new declaration %q#D ambiguates built-in "
1514 "declaration %q#D", newdecl, olddecl);
1515 else
1516 warning (OPT_Wshadow,
1517 DECL_BUILT_IN (olddecl)
1518 ? G_("shadowing built-in function %q#D")
1519 : G_("shadowing library function %q#D"), olddecl);
1521 else
1522 /* Discard the old built-in function. */
1523 return NULL_TREE;
1525 /* Replace the old RTL to avoid problems with inlining. */
1526 COPY_DECL_RTL (newdecl, olddecl);
1528 /* Even if the types match, prefer the new declarations type for
1529 built-ins which have not been explicitly declared, for
1530 exception lists, etc... */
1531 else if (DECL_IS_BUILTIN (olddecl))
1533 tree type = TREE_TYPE (newdecl);
1534 tree attribs = (*targetm.merge_type_attributes)
1535 (TREE_TYPE (olddecl), type);
1537 type = cp_build_type_attribute_variant (type, attribs);
1538 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = type;
1541 /* If a function is explicitly declared "throw ()", propagate that to
1542 the corresponding builtin. */
1543 if (DECL_BUILT_IN_CLASS (olddecl) == BUILT_IN_NORMAL
1544 && DECL_ANTICIPATED (olddecl)
1545 && TREE_NOTHROW (newdecl)
1546 && !TREE_NOTHROW (olddecl))
1548 enum built_in_function fncode = DECL_FUNCTION_CODE (olddecl);
1549 tree tmpdecl = builtin_decl_explicit (fncode);
1550 if (tmpdecl && tmpdecl != olddecl && types_match)
1551 TREE_NOTHROW (tmpdecl) = 1;
1554 /* Whether or not the builtin can throw exceptions has no
1555 bearing on this declarator. */
1556 TREE_NOTHROW (olddecl) = 0;
1558 if (DECL_THIS_STATIC (newdecl) && !DECL_THIS_STATIC (olddecl))
1560 /* If a builtin function is redeclared as `static', merge
1561 the declarations, but make the original one static. */
1562 DECL_THIS_STATIC (olddecl) = 1;
1563 TREE_PUBLIC (olddecl) = 0;
1565 /* Make the old declaration consistent with the new one so
1566 that all remnants of the builtin-ness of this function
1567 will be banished. */
1568 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
1569 COPY_DECL_RTL (newdecl, olddecl);
1572 else if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1574 /* C++ Standard, 3.3, clause 4:
1575 "[Note: a namespace name or a class template name must be unique
1576 in its declarative region (7.3.2, clause 14). ]" */
1577 if (TREE_CODE (olddecl) != NAMESPACE_DECL
1578 && TREE_CODE (newdecl) != NAMESPACE_DECL
1579 && (TREE_CODE (olddecl) != TEMPLATE_DECL
1580 || TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) != TYPE_DECL)
1581 && (TREE_CODE (newdecl) != TEMPLATE_DECL
1582 || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) != TYPE_DECL))
1584 if ((TREE_CODE (olddecl) == TYPE_DECL && DECL_ARTIFICIAL (olddecl)
1585 && TREE_CODE (newdecl) != TYPE_DECL)
1586 || (TREE_CODE (newdecl) == TYPE_DECL && DECL_ARTIFICIAL (newdecl)
1587 && TREE_CODE (olddecl) != TYPE_DECL))
1589 /* We do nothing special here, because C++ does such nasty
1590 things with TYPE_DECLs. Instead, just let the TYPE_DECL
1591 get shadowed, and know that if we need to find a TYPE_DECL
1592 for a given name, we can look in the IDENTIFIER_TYPE_VALUE
1593 slot of the identifier. */
1594 return NULL_TREE;
1597 if ((TREE_CODE (newdecl) == FUNCTION_DECL
1598 && DECL_FUNCTION_TEMPLATE_P (olddecl))
1599 || (TREE_CODE (olddecl) == FUNCTION_DECL
1600 && DECL_FUNCTION_TEMPLATE_P (newdecl)))
1601 return NULL_TREE;
1604 error ("%q#D redeclared as different kind of symbol", newdecl);
1605 if (TREE_CODE (olddecl) == TREE_LIST)
1606 olddecl = TREE_VALUE (olddecl);
1607 inform (DECL_SOURCE_LOCATION (olddecl),
1608 "previous declaration %q#D", olddecl);
1610 return error_mark_node;
1612 else if (!types_match)
1614 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl))
1615 /* These are certainly not duplicate declarations; they're
1616 from different scopes. */
1617 return NULL_TREE;
1619 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1621 /* The name of a class template may not be declared to refer to
1622 any other template, class, function, object, namespace, value,
1623 or type in the same scope. */
1624 if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == TYPE_DECL
1625 || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
1627 error ("conflicting declaration of template %q+#D", newdecl);
1628 inform (DECL_SOURCE_LOCATION (olddecl),
1629 "previous declaration %q#D", olddecl);
1630 return error_mark_node;
1632 else if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == FUNCTION_DECL
1633 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL
1634 && compparms (TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl))),
1635 TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl))))
1636 && comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
1637 DECL_TEMPLATE_PARMS (olddecl))
1638 /* Template functions can be disambiguated by
1639 return type. */
1640 && same_type_p (TREE_TYPE (TREE_TYPE (newdecl)),
1641 TREE_TYPE (TREE_TYPE (olddecl)))
1642 // Template functions can also be disambiguated by
1643 // constraints.
1644 && equivalently_constrained (olddecl, newdecl))
1646 error ("ambiguating new declaration %q+#D", newdecl);
1647 inform (DECL_SOURCE_LOCATION (olddecl),
1648 "old declaration %q#D", olddecl);
1650 else if (check_concept_refinement (olddecl, newdecl))
1651 return error_mark_node;
1652 return NULL_TREE;
1654 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1656 if (DECL_EXTERN_C_P (newdecl) && DECL_EXTERN_C_P (olddecl))
1658 error ("conflicting declaration of C function %q+#D",
1659 newdecl);
1660 inform (DECL_SOURCE_LOCATION (olddecl),
1661 "previous declaration %q#D", olddecl);
1662 return NULL_TREE;
1664 /* For function versions, params and types match, but they
1665 are not ambiguous. */
1666 else if ((!DECL_FUNCTION_VERSIONED (newdecl)
1667 && !DECL_FUNCTION_VERSIONED (olddecl))
1668 // The functions have the same parameter types.
1669 && compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1670 TYPE_ARG_TYPES (TREE_TYPE (olddecl)))
1671 // And the same constraints.
1672 && equivalently_constrained (newdecl, olddecl))
1674 error ("ambiguating new declaration of %q+#D", newdecl);
1675 inform (DECL_SOURCE_LOCATION (olddecl),
1676 "old declaration %q#D", olddecl);
1677 return error_mark_node;
1679 else
1680 return NULL_TREE;
1682 else
1684 error ("conflicting declaration %q+#D", newdecl);
1685 inform (DECL_SOURCE_LOCATION (olddecl),
1686 "previous declaration as %q#D", olddecl);
1687 return error_mark_node;
1690 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1691 && ((DECL_TEMPLATE_SPECIALIZATION (olddecl)
1692 && (!DECL_TEMPLATE_INFO (newdecl)
1693 || (DECL_TI_TEMPLATE (newdecl)
1694 != DECL_TI_TEMPLATE (olddecl))))
1695 || (DECL_TEMPLATE_SPECIALIZATION (newdecl)
1696 && (!DECL_TEMPLATE_INFO (olddecl)
1697 || (DECL_TI_TEMPLATE (olddecl)
1698 != DECL_TI_TEMPLATE (newdecl))))))
1699 /* It's OK to have a template specialization and a non-template
1700 with the same type, or to have specializations of two
1701 different templates with the same type. Note that if one is a
1702 specialization, and the other is an instantiation of the same
1703 template, that we do not exit at this point. That situation
1704 can occur if we instantiate a template class, and then
1705 specialize one of its methods. This situation is valid, but
1706 the declarations must be merged in the usual way. */
1707 return NULL_TREE;
1708 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1709 && ((DECL_TEMPLATE_INSTANTIATION (olddecl)
1710 && !DECL_USE_TEMPLATE (newdecl))
1711 || (DECL_TEMPLATE_INSTANTIATION (newdecl)
1712 && !DECL_USE_TEMPLATE (olddecl))))
1713 /* One of the declarations is a template instantiation, and the
1714 other is not a template at all. That's OK. */
1715 return NULL_TREE;
1716 else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
1718 /* In [namespace.alias] we have:
1720 In a declarative region, a namespace-alias-definition can be
1721 used to redefine a namespace-alias declared in that declarative
1722 region to refer only to the namespace to which it already
1723 refers.
1725 Therefore, if we encounter a second alias directive for the same
1726 alias, we can just ignore the second directive. */
1727 if (DECL_NAMESPACE_ALIAS (newdecl)
1728 && (DECL_NAMESPACE_ALIAS (newdecl)
1729 == DECL_NAMESPACE_ALIAS (olddecl)))
1730 return olddecl;
1731 /* [namespace.alias]
1733 A namespace-name or namespace-alias shall not be declared as
1734 the name of any other entity in the same declarative region.
1735 A namespace-name defined at global scope shall not be
1736 declared as the name of any other entity in any global scope
1737 of the program. */
1738 error ("conflicting declaration of namespace %q+D", newdecl);
1739 inform (DECL_SOURCE_LOCATION (olddecl),
1740 "previous declaration of namespace %qD here", olddecl);
1741 return error_mark_node;
1743 else
1745 const char *errmsg = redeclaration_error_message (newdecl, olddecl);
1746 if (errmsg)
1748 error_at (DECL_SOURCE_LOCATION (newdecl), errmsg, newdecl);
1749 if (DECL_NAME (olddecl) != NULL_TREE)
1750 inform (DECL_SOURCE_LOCATION (olddecl),
1751 (DECL_INITIAL (olddecl) && namespace_bindings_p ())
1752 ? G_("%q#D previously defined here")
1753 : G_("%q#D previously declared here"), olddecl);
1754 return error_mark_node;
1756 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1757 && DECL_INITIAL (olddecl) != NULL_TREE
1758 && !prototype_p (TREE_TYPE (olddecl))
1759 && prototype_p (TREE_TYPE (newdecl)))
1761 /* Prototype decl follows defn w/o prototype. */
1762 if (warning_at (DECL_SOURCE_LOCATION (newdecl), 0,
1763 "prototype specified for %q#D", newdecl))
1764 inform (DECL_SOURCE_LOCATION (olddecl),
1765 "previous non-prototype definition here");
1767 else if (VAR_OR_FUNCTION_DECL_P (olddecl)
1768 && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl))
1770 /* [dcl.link]
1771 If two declarations of the same function or object
1772 specify different linkage-specifications ..., the program
1773 is ill-formed.... Except for functions with C++ linkage,
1774 a function declaration without a linkage specification
1775 shall not precede the first linkage specification for
1776 that function. A function can be declared without a
1777 linkage specification after an explicit linkage
1778 specification has been seen; the linkage explicitly
1779 specified in the earlier declaration is not affected by
1780 such a function declaration.
1782 DR 563 raises the question why the restrictions on
1783 functions should not also apply to objects. Older
1784 versions of G++ silently ignore the linkage-specification
1785 for this example:
1787 namespace N {
1788 extern int i;
1789 extern "C" int i;
1792 which is clearly wrong. Therefore, we now treat objects
1793 like functions. */
1794 if (current_lang_depth () == 0)
1796 /* There is no explicit linkage-specification, so we use
1797 the linkage from the previous declaration. */
1798 if (!DECL_LANG_SPECIFIC (newdecl))
1799 retrofit_lang_decl (newdecl);
1800 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
1802 else
1804 error ("conflicting declaration of %q+#D with %qL linkage",
1805 newdecl, DECL_LANGUAGE (newdecl));
1806 inform (DECL_SOURCE_LOCATION (olddecl),
1807 "previous declaration with %qL linkage",
1808 DECL_LANGUAGE (olddecl));
1812 if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl))
1814 else if (TREE_CODE (olddecl) == FUNCTION_DECL)
1816 /* Note: free functions, as TEMPLATE_DECLs, are handled below. */
1817 if (DECL_FUNCTION_MEMBER_P (olddecl)
1818 && (/* grokfndecl passes member function templates too
1819 as FUNCTION_DECLs. */
1820 DECL_TEMPLATE_INFO (olddecl)
1821 /* C++11 8.3.6/6.
1822 Default arguments for a member function of a class
1823 template shall be specified on the initial declaration
1824 of the member function within the class template. */
1825 || CLASSTYPE_TEMPLATE_INFO (CP_DECL_CONTEXT (olddecl))))
1826 check_redeclaration_no_default_args (newdecl);
1827 else
1829 tree t1 = FUNCTION_FIRST_USER_PARMTYPE (olddecl);
1830 tree t2 = FUNCTION_FIRST_USER_PARMTYPE (newdecl);
1831 int i = 1;
1833 for (; t1 && t1 != void_list_node;
1834 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++)
1835 if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
1837 if (1 == simple_cst_equal (TREE_PURPOSE (t1),
1838 TREE_PURPOSE (t2)))
1840 if (permerror (input_location,
1841 "default argument given for parameter "
1842 "%d of %q#D", i, newdecl))
1843 inform (DECL_SOURCE_LOCATION (olddecl),
1844 "previous specification in %q#D here",
1845 olddecl);
1847 else
1849 error ("default argument given for parameter %d "
1850 "of %q#D", i, newdecl);
1851 inform (DECL_SOURCE_LOCATION (olddecl),
1852 "previous specification in %q#D here",
1853 olddecl);
1860 /* Do not merge an implicit typedef with an explicit one. In:
1862 class A;
1864 typedef class A A __attribute__ ((foo));
1866 the attribute should apply only to the typedef. */
1867 if (TREE_CODE (olddecl) == TYPE_DECL
1868 && (DECL_IMPLICIT_TYPEDEF_P (olddecl)
1869 || DECL_IMPLICIT_TYPEDEF_P (newdecl)))
1870 return NULL_TREE;
1872 /* If new decl is `static' and an `extern' was seen previously,
1873 warn about it. */
1874 warn_extern_redeclared_static (newdecl, olddecl);
1876 if (!validate_constexpr_redeclaration (olddecl, newdecl))
1877 return error_mark_node;
1879 /* We have committed to returning 1 at this point. */
1880 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1882 /* Now that functions must hold information normally held
1883 by field decls, there is extra work to do so that
1884 declaration information does not get destroyed during
1885 definition. */
1886 if (DECL_VINDEX (olddecl))
1887 DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl);
1888 if (DECL_CONTEXT (olddecl))
1889 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1890 DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl);
1891 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1892 DECL_PURE_VIRTUAL_P (newdecl) |= DECL_PURE_VIRTUAL_P (olddecl);
1893 DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl);
1894 DECL_INVALID_OVERRIDER_P (newdecl) |= DECL_INVALID_OVERRIDER_P (olddecl);
1895 DECL_FINAL_P (newdecl) |= DECL_FINAL_P (olddecl);
1896 DECL_OVERRIDE_P (newdecl) |= DECL_OVERRIDE_P (olddecl);
1897 DECL_THIS_STATIC (newdecl) |= DECL_THIS_STATIC (olddecl);
1898 if (DECL_OVERLOADED_OPERATOR_P (olddecl) != ERROR_MARK)
1899 SET_OVERLOADED_OPERATOR_CODE
1900 (newdecl, DECL_OVERLOADED_OPERATOR_P (olddecl));
1901 new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE;
1903 /* Optionally warn about more than one declaration for the same
1904 name, but don't warn about a function declaration followed by a
1905 definition. */
1906 if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl)
1907 && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE)
1908 /* Don't warn about extern decl followed by definition. */
1909 && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl))
1910 /* Don't warn about friends, let add_friend take care of it. */
1911 && ! (newdecl_is_friend || DECL_FRIEND_P (olddecl))
1912 /* Don't warn about declaration followed by specialization. */
1913 && (! DECL_TEMPLATE_SPECIALIZATION (newdecl)
1914 || DECL_TEMPLATE_SPECIALIZATION (olddecl)))
1916 if (warning_at (DECL_SOURCE_LOCATION (newdecl),
1917 OPT_Wredundant_decls,
1918 "redundant redeclaration of %qD in same scope",
1919 newdecl))
1920 inform (DECL_SOURCE_LOCATION (olddecl),
1921 "previous declaration of %qD", olddecl);
1924 if (!(DECL_TEMPLATE_INSTANTIATION (olddecl)
1925 && DECL_TEMPLATE_SPECIALIZATION (newdecl)))
1927 if (DECL_DELETED_FN (newdecl))
1929 error ("deleted definition of %q+D", newdecl);
1930 inform (DECL_SOURCE_LOCATION (olddecl),
1931 "previous declaration of %qD", olddecl);
1933 DECL_DELETED_FN (newdecl) |= DECL_DELETED_FN (olddecl);
1937 /* Deal with C++: must preserve virtual function table size. */
1938 if (TREE_CODE (olddecl) == TYPE_DECL)
1940 tree newtype = TREE_TYPE (newdecl);
1941 tree oldtype = TREE_TYPE (olddecl);
1943 if (newtype != error_mark_node && oldtype != error_mark_node
1944 && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype))
1945 CLASSTYPE_FRIEND_CLASSES (newtype)
1946 = CLASSTYPE_FRIEND_CLASSES (oldtype);
1948 DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl);
1951 /* Copy all the DECL_... slots specified in the new decl
1952 except for any that we copy here from the old type. */
1953 DECL_ATTRIBUTES (newdecl)
1954 = (*targetm.merge_decl_attributes) (olddecl, newdecl);
1956 if (DECL_DECLARES_FUNCTION_P (olddecl) && DECL_DECLARES_FUNCTION_P (newdecl))
1958 olddecl_friend = DECL_FRIEND_P (olddecl);
1959 hidden_friend = (DECL_ANTICIPATED (olddecl)
1960 && DECL_HIDDEN_FRIEND_P (olddecl)
1961 && newdecl_is_friend);
1962 if (!hidden_friend)
1964 DECL_ANTICIPATED (olddecl) = 0;
1965 DECL_HIDDEN_FRIEND_P (olddecl) = 0;
1969 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1971 tree old_result;
1972 tree new_result;
1973 old_result = DECL_TEMPLATE_RESULT (olddecl);
1974 new_result = DECL_TEMPLATE_RESULT (newdecl);
1975 TREE_TYPE (olddecl) = TREE_TYPE (old_result);
1976 DECL_TEMPLATE_SPECIALIZATIONS (olddecl)
1977 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (olddecl),
1978 DECL_TEMPLATE_SPECIALIZATIONS (newdecl));
1980 DECL_ATTRIBUTES (old_result)
1981 = (*targetm.merge_decl_attributes) (old_result, new_result);
1983 if (DECL_FUNCTION_TEMPLATE_P (newdecl))
1985 /* Per C++11 8.3.6/4, default arguments cannot be added in later
1986 declarations of a function template. */
1987 if (DECL_SOURCE_LOCATION (newdecl)
1988 != DECL_SOURCE_LOCATION (olddecl))
1989 check_redeclaration_no_default_args (newdecl);
1991 check_default_args (newdecl);
1993 if (GNU_INLINE_P (old_result) != GNU_INLINE_P (new_result)
1994 && DECL_INITIAL (new_result))
1996 if (DECL_INITIAL (old_result))
1997 DECL_UNINLINABLE (old_result) = 1;
1998 else
1999 DECL_UNINLINABLE (old_result) = DECL_UNINLINABLE (new_result);
2000 DECL_EXTERNAL (old_result) = DECL_EXTERNAL (new_result);
2001 DECL_NOT_REALLY_EXTERN (old_result)
2002 = DECL_NOT_REALLY_EXTERN (new_result);
2003 DECL_INTERFACE_KNOWN (old_result)
2004 = DECL_INTERFACE_KNOWN (new_result);
2005 DECL_DECLARED_INLINE_P (old_result)
2006 = DECL_DECLARED_INLINE_P (new_result);
2007 DECL_DISREGARD_INLINE_LIMITS (old_result)
2008 |= DECL_DISREGARD_INLINE_LIMITS (new_result);
2011 else
2013 DECL_DECLARED_INLINE_P (old_result)
2014 |= DECL_DECLARED_INLINE_P (new_result);
2015 DECL_DISREGARD_INLINE_LIMITS (old_result)
2016 |= DECL_DISREGARD_INLINE_LIMITS (new_result);
2017 check_redeclaration_exception_specification (newdecl, olddecl);
2021 /* If the new declaration is a definition, update the file and
2022 line information on the declaration, and also make
2023 the old declaration the same definition. */
2024 if (DECL_INITIAL (new_result) != NULL_TREE)
2026 DECL_SOURCE_LOCATION (olddecl)
2027 = DECL_SOURCE_LOCATION (old_result)
2028 = DECL_SOURCE_LOCATION (newdecl);
2029 DECL_INITIAL (old_result) = DECL_INITIAL (new_result);
2030 if (DECL_FUNCTION_TEMPLATE_P (newdecl))
2032 tree parm;
2033 DECL_ARGUMENTS (old_result)
2034 = DECL_ARGUMENTS (new_result);
2035 for (parm = DECL_ARGUMENTS (old_result); parm;
2036 parm = DECL_CHAIN (parm))
2037 DECL_CONTEXT (parm) = old_result;
2041 return olddecl;
2044 if (types_match)
2046 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2047 check_redeclaration_exception_specification (newdecl, olddecl);
2049 /* Automatically handles default parameters. */
2050 tree oldtype = TREE_TYPE (olddecl);
2051 tree newtype;
2053 /* For typedefs use the old type, as the new type's DECL_NAME points
2054 at newdecl, which will be ggc_freed. */
2055 if (TREE_CODE (newdecl) == TYPE_DECL)
2057 /* But NEWTYPE might have an attribute, honor that. */
2058 tree tem = TREE_TYPE (newdecl);
2059 newtype = oldtype;
2061 if (TYPE_USER_ALIGN (tem))
2063 if (TYPE_ALIGN (tem) > TYPE_ALIGN (newtype))
2064 SET_TYPE_ALIGN (newtype, TYPE_ALIGN (tem));
2065 TYPE_USER_ALIGN (newtype) = true;
2068 /* And remove the new type from the variants list. */
2069 if (TYPE_NAME (TREE_TYPE (newdecl)) == newdecl)
2071 tree remove = TREE_TYPE (newdecl);
2072 for (tree t = TYPE_MAIN_VARIANT (remove); ;
2073 t = TYPE_NEXT_VARIANT (t))
2074 if (TYPE_NEXT_VARIANT (t) == remove)
2076 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (remove);
2077 break;
2081 else
2082 /* Merge the data types specified in the two decls. */
2083 newtype = merge_types (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
2085 if (VAR_P (newdecl))
2087 DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl);
2088 /* For already initialized vars, TREE_READONLY could have been
2089 cleared in cp_finish_decl, because the var needs runtime
2090 initialization or destruction. Make sure not to set
2091 TREE_READONLY on it again. */
2092 if (DECL_INITIALIZED_P (olddecl)
2093 && !DECL_EXTERNAL (olddecl)
2094 && !TREE_READONLY (olddecl))
2095 TREE_READONLY (newdecl) = 0;
2096 DECL_INITIALIZED_P (newdecl) |= DECL_INITIALIZED_P (olddecl);
2097 DECL_NONTRIVIALLY_INITIALIZED_P (newdecl)
2098 |= DECL_NONTRIVIALLY_INITIALIZED_P (olddecl);
2099 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (newdecl)
2100 |= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (olddecl);
2102 /* Merge the threadprivate attribute from OLDDECL into NEWDECL. */
2103 if (DECL_LANG_SPECIFIC (olddecl)
2104 && CP_DECL_THREADPRIVATE_P (olddecl))
2106 /* Allocate a LANG_SPECIFIC structure for NEWDECL, if needed. */
2107 if (!DECL_LANG_SPECIFIC (newdecl))
2108 retrofit_lang_decl (newdecl);
2110 CP_DECL_THREADPRIVATE_P (newdecl) = 1;
2114 /* An explicit specialization of a function template or of a member
2115 function of a class template can be declared transaction_safe
2116 independently of whether the corresponding template entity is declared
2117 transaction_safe. */
2118 if (flag_tm && TREE_CODE (newdecl) == FUNCTION_DECL
2119 && DECL_TEMPLATE_INSTANTIATION (olddecl)
2120 && DECL_TEMPLATE_SPECIALIZATION (newdecl)
2121 && tx_safe_fn_type_p (newtype)
2122 && !tx_safe_fn_type_p (TREE_TYPE (newdecl)))
2123 newtype = tx_unsafe_fn_variant (newtype);
2125 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype;
2127 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2128 check_default_args (newdecl);
2130 /* Lay the type out, unless already done. */
2131 if (! same_type_p (newtype, oldtype)
2132 && TREE_TYPE (newdecl) != error_mark_node
2133 && !(processing_template_decl && uses_template_parms (newdecl)))
2134 layout_type (TREE_TYPE (newdecl));
2136 if ((VAR_P (newdecl)
2137 || TREE_CODE (newdecl) == PARM_DECL
2138 || TREE_CODE (newdecl) == RESULT_DECL
2139 || TREE_CODE (newdecl) == FIELD_DECL
2140 || TREE_CODE (newdecl) == TYPE_DECL)
2141 && !(processing_template_decl && uses_template_parms (newdecl)))
2142 layout_decl (newdecl, 0);
2144 /* Merge the type qualifiers. */
2145 if (TREE_READONLY (newdecl))
2146 TREE_READONLY (olddecl) = 1;
2147 if (TREE_THIS_VOLATILE (newdecl))
2148 TREE_THIS_VOLATILE (olddecl) = 1;
2149 if (TREE_NOTHROW (newdecl))
2150 TREE_NOTHROW (olddecl) = 1;
2152 /* Merge deprecatedness. */
2153 if (TREE_DEPRECATED (newdecl))
2154 TREE_DEPRECATED (olddecl) = 1;
2156 /* Preserve function specific target and optimization options */
2157 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2159 if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
2160 && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
2161 DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
2162 = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
2164 if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
2165 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
2166 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
2167 = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
2170 /* Merge the initialization information. */
2171 if (DECL_INITIAL (newdecl) == NULL_TREE
2172 && DECL_INITIAL (olddecl) != NULL_TREE)
2174 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2175 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2176 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2178 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
2179 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
2183 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2185 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
2186 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
2187 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
2188 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
2189 TREE_NOTHROW (newdecl) |= TREE_NOTHROW (olddecl);
2190 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
2191 DECL_IS_OPERATOR_NEW (newdecl) |= DECL_IS_OPERATOR_NEW (olddecl);
2192 DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
2193 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
2194 DECL_LOOPING_CONST_OR_PURE_P (newdecl)
2195 |= DECL_LOOPING_CONST_OR_PURE_P (olddecl);
2196 /* Keep the old RTL. */
2197 COPY_DECL_RTL (olddecl, newdecl);
2199 else if (VAR_P (newdecl)
2200 && (DECL_SIZE (olddecl) || !DECL_SIZE (newdecl)))
2202 /* Keep the old RTL. We cannot keep the old RTL if the old
2203 declaration was for an incomplete object and the new
2204 declaration is not since many attributes of the RTL will
2205 change. */
2206 COPY_DECL_RTL (olddecl, newdecl);
2209 /* If cannot merge, then use the new type and qualifiers,
2210 and don't preserve the old rtl. */
2211 else
2213 /* Clean out any memory we had of the old declaration. */
2214 tree oldstatic = value_member (olddecl, static_aggregates);
2215 if (oldstatic)
2216 TREE_VALUE (oldstatic) = error_mark_node;
2218 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
2219 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
2220 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
2221 TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
2224 /* Merge the storage class information. */
2225 merge_weak (newdecl, olddecl);
2227 DECL_DEFER_OUTPUT (newdecl) |= DECL_DEFER_OUTPUT (olddecl);
2228 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
2229 TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl);
2230 if (! DECL_EXTERNAL (olddecl))
2231 DECL_EXTERNAL (newdecl) = 0;
2232 if (! DECL_COMDAT (olddecl))
2233 DECL_COMDAT (newdecl) = 0;
2235 new_template_info = NULL_TREE;
2236 if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl))
2238 bool new_redefines_gnu_inline = false;
2240 if (new_defines_function
2241 && ((DECL_INTERFACE_KNOWN (olddecl)
2242 && TREE_CODE (olddecl) == FUNCTION_DECL)
2243 || (TREE_CODE (olddecl) == TEMPLATE_DECL
2244 && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl))
2245 == FUNCTION_DECL))))
2247 tree fn = olddecl;
2249 if (TREE_CODE (fn) == TEMPLATE_DECL)
2250 fn = DECL_TEMPLATE_RESULT (olddecl);
2252 new_redefines_gnu_inline = GNU_INLINE_P (fn) && DECL_INITIAL (fn);
2255 if (!new_redefines_gnu_inline)
2257 DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl);
2258 DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl);
2259 DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl);
2261 DECL_TEMPLATE_INSTANTIATED (newdecl)
2262 |= DECL_TEMPLATE_INSTANTIATED (olddecl);
2263 DECL_ODR_USED (newdecl) |= DECL_ODR_USED (olddecl);
2265 /* If the OLDDECL is an instantiation and/or specialization,
2266 then the NEWDECL must be too. But, it may not yet be marked
2267 as such if the caller has created NEWDECL, but has not yet
2268 figured out that it is a redeclaration. */
2269 if (!DECL_USE_TEMPLATE (newdecl))
2270 DECL_USE_TEMPLATE (newdecl) = DECL_USE_TEMPLATE (olddecl);
2272 /* Don't really know how much of the language-specific
2273 values we should copy from old to new. */
2274 DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl);
2275 DECL_REPO_AVAILABLE_P (newdecl) = DECL_REPO_AVAILABLE_P (olddecl);
2276 DECL_INITIALIZED_IN_CLASS_P (newdecl)
2277 |= DECL_INITIALIZED_IN_CLASS_P (olddecl);
2279 if (LANG_DECL_HAS_MIN (newdecl))
2281 DECL_LANG_SPECIFIC (newdecl)->u.min.u2 =
2282 DECL_LANG_SPECIFIC (olddecl)->u.min.u2;
2283 if (DECL_TEMPLATE_INFO (newdecl))
2285 new_template_info = DECL_TEMPLATE_INFO (newdecl);
2286 if (DECL_TEMPLATE_INSTANTIATION (olddecl)
2287 && DECL_TEMPLATE_SPECIALIZATION (newdecl))
2288 /* Remember the presence of explicit specialization args. */
2289 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (olddecl))
2290 = TINFO_USED_TEMPLATE_ID (new_template_info);
2292 DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl);
2294 /* Only functions have these fields. */
2295 if (DECL_DECLARES_FUNCTION_P (newdecl))
2297 DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl);
2298 DECL_BEFRIENDING_CLASSES (newdecl)
2299 = chainon (DECL_BEFRIENDING_CLASSES (newdecl),
2300 DECL_BEFRIENDING_CLASSES (olddecl));
2301 /* DECL_THUNKS is only valid for virtual functions,
2302 otherwise it is a DECL_FRIEND_CONTEXT. */
2303 if (DECL_VIRTUAL_P (newdecl))
2304 SET_DECL_THUNKS (newdecl, DECL_THUNKS (olddecl));
2306 /* Only variables have this field. */
2307 else if (VAR_P (newdecl)
2308 && VAR_HAD_UNKNOWN_BOUND (olddecl))
2309 SET_VAR_HAD_UNKNOWN_BOUND (newdecl);
2312 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2314 tree parm;
2316 /* Merge parameter attributes. */
2317 tree oldarg, newarg;
2318 for (oldarg = DECL_ARGUMENTS(olddecl),
2319 newarg = DECL_ARGUMENTS(newdecl);
2320 oldarg && newarg;
2321 oldarg = DECL_CHAIN(oldarg), newarg = DECL_CHAIN(newarg)) {
2322 DECL_ATTRIBUTES (newarg)
2323 = (*targetm.merge_decl_attributes) (oldarg, newarg);
2324 DECL_ATTRIBUTES (oldarg) = DECL_ATTRIBUTES (newarg);
2327 if (DECL_TEMPLATE_INSTANTIATION (olddecl)
2328 && !DECL_TEMPLATE_INSTANTIATION (newdecl))
2330 /* If newdecl is not a specialization, then it is not a
2331 template-related function at all. And that means that we
2332 should have exited above, returning 0. */
2333 gcc_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl));
2335 if (DECL_ODR_USED (olddecl))
2336 /* From [temp.expl.spec]:
2338 If a template, a member template or the member of a class
2339 template is explicitly specialized then that
2340 specialization shall be declared before the first use of
2341 that specialization that would cause an implicit
2342 instantiation to take place, in every translation unit in
2343 which such a use occurs. */
2344 error ("explicit specialization of %qD after first use",
2345 olddecl);
2347 SET_DECL_TEMPLATE_SPECIALIZATION (olddecl);
2348 DECL_COMDAT (newdecl) = (TREE_PUBLIC (newdecl)
2349 && DECL_DECLARED_INLINE_P (newdecl));
2351 /* Don't propagate visibility from the template to the
2352 specialization here. We'll do that in determine_visibility if
2353 appropriate. */
2354 DECL_VISIBILITY_SPECIFIED (olddecl) = 0;
2356 /* [temp.expl.spec/14] We don't inline explicit specialization
2357 just because the primary template says so. */
2359 /* But still keep DECL_DISREGARD_INLINE_LIMITS in sync with
2360 the always_inline attribute. */
2361 if (DECL_DISREGARD_INLINE_LIMITS (olddecl)
2362 && !DECL_DISREGARD_INLINE_LIMITS (newdecl))
2364 if (DECL_DECLARED_INLINE_P (newdecl))
2365 DECL_DISREGARD_INLINE_LIMITS (newdecl) = true;
2366 else
2367 DECL_ATTRIBUTES (newdecl)
2368 = remove_attribute ("always_inline",
2369 DECL_ATTRIBUTES (newdecl));
2372 else if (new_defines_function && DECL_INITIAL (olddecl))
2374 /* Never inline re-defined extern inline functions.
2375 FIXME: this could be better handled by keeping both
2376 function as separate declarations. */
2377 DECL_UNINLINABLE (newdecl) = 1;
2379 else
2381 if (DECL_PENDING_INLINE_P (olddecl))
2383 DECL_PENDING_INLINE_P (newdecl) = 1;
2384 DECL_PENDING_INLINE_INFO (newdecl)
2385 = DECL_PENDING_INLINE_INFO (olddecl);
2387 else if (DECL_PENDING_INLINE_P (newdecl))
2389 else if (DECL_SAVED_FUNCTION_DATA (newdecl) == NULL)
2390 DECL_SAVED_FUNCTION_DATA (newdecl)
2391 = DECL_SAVED_FUNCTION_DATA (olddecl);
2393 DECL_DECLARED_INLINE_P (newdecl) |= DECL_DECLARED_INLINE_P (olddecl);
2395 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
2396 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
2398 DECL_DISREGARD_INLINE_LIMITS (newdecl)
2399 = DECL_DISREGARD_INLINE_LIMITS (olddecl)
2400 = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
2401 || DECL_DISREGARD_INLINE_LIMITS (olddecl));
2404 /* Preserve abstractness on cloned [cd]tors. */
2405 DECL_ABSTRACT_P (newdecl) = DECL_ABSTRACT_P (olddecl);
2407 /* Update newdecl's parms to point at olddecl. */
2408 for (parm = DECL_ARGUMENTS (newdecl); parm;
2409 parm = DECL_CHAIN (parm))
2410 DECL_CONTEXT (parm) = olddecl;
2412 if (! types_match)
2414 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
2415 COPY_DECL_ASSEMBLER_NAME (newdecl, olddecl);
2416 COPY_DECL_RTL (newdecl, olddecl);
2418 if (! types_match || new_defines_function)
2420 /* These need to be copied so that the names are available.
2421 Note that if the types do match, we'll preserve inline
2422 info and other bits, but if not, we won't. */
2423 DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl);
2424 DECL_RESULT (olddecl) = DECL_RESULT (newdecl);
2426 /* If redeclaring a builtin function, it stays built in
2427 if newdecl is a gnu_inline definition, or if newdecl is just
2428 a declaration. */
2429 if (DECL_BUILT_IN (olddecl)
2430 && (new_defines_function ? GNU_INLINE_P (newdecl) : types_match))
2432 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
2433 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
2434 /* If we're keeping the built-in definition, keep the rtl,
2435 regardless of declaration matches. */
2436 COPY_DECL_RTL (olddecl, newdecl);
2437 if (DECL_BUILT_IN_CLASS (newdecl) == BUILT_IN_NORMAL)
2439 enum built_in_function fncode = DECL_FUNCTION_CODE (newdecl);
2440 switch (fncode)
2442 /* If a compatible prototype of these builtin functions
2443 is seen, assume the runtime implements it with the
2444 expected semantics. */
2445 case BUILT_IN_STPCPY:
2446 if (builtin_decl_explicit_p (fncode))
2447 set_builtin_decl_implicit_p (fncode, true);
2448 break;
2449 default:
2450 if (builtin_decl_explicit_p (fncode))
2451 set_builtin_decl_declared_p (fncode, true);
2452 break;
2456 if (new_defines_function)
2457 /* If defining a function declared with other language
2458 linkage, use the previously declared language linkage. */
2459 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
2460 else if (types_match)
2462 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
2463 /* Don't clear out the arguments if we're just redeclaring a
2464 function. */
2465 if (DECL_ARGUMENTS (olddecl))
2466 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
2469 else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
2470 NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl);
2472 /* Now preserve various other info from the definition. */
2473 TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl);
2474 TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl);
2475 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
2476 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
2478 /* Warn about conflicting visibility specifications. */
2479 if (DECL_VISIBILITY_SPECIFIED (olddecl)
2480 && DECL_VISIBILITY_SPECIFIED (newdecl)
2481 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
2483 if (warning_at (DECL_SOURCE_LOCATION (newdecl), OPT_Wattributes,
2484 "%qD: visibility attribute ignored because it "
2485 "conflicts with previous declaration", newdecl))
2486 inform (DECL_SOURCE_LOCATION (olddecl),
2487 "previous declaration of %qD", olddecl);
2489 /* Choose the declaration which specified visibility. */
2490 if (DECL_VISIBILITY_SPECIFIED (olddecl))
2492 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
2493 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
2495 /* Init priority used to be merged from newdecl to olddecl by the memcpy,
2496 so keep this behavior. */
2497 if (VAR_P (newdecl) && DECL_HAS_INIT_PRIORITY_P (newdecl))
2499 SET_DECL_INIT_PRIORITY (olddecl, DECL_INIT_PRIORITY (newdecl));
2500 DECL_HAS_INIT_PRIORITY_P (olddecl) = 1;
2502 /* Likewise for DECL_ALIGN, DECL_USER_ALIGN and DECL_PACKED. */
2503 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
2505 SET_DECL_ALIGN (newdecl, DECL_ALIGN (olddecl));
2506 DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
2508 DECL_USER_ALIGN (olddecl) = DECL_USER_ALIGN (newdecl);
2509 if (TREE_CODE (newdecl) == FIELD_DECL)
2510 DECL_PACKED (olddecl) = DECL_PACKED (newdecl);
2512 /* The DECL_LANG_SPECIFIC information in OLDDECL will be replaced
2513 with that from NEWDECL below. */
2514 if (DECL_LANG_SPECIFIC (olddecl))
2516 gcc_assert (DECL_LANG_SPECIFIC (olddecl)
2517 != DECL_LANG_SPECIFIC (newdecl));
2518 ggc_free (DECL_LANG_SPECIFIC (olddecl));
2521 /* Merge the USED information. */
2522 if (TREE_USED (olddecl))
2523 TREE_USED (newdecl) = 1;
2524 else if (TREE_USED (newdecl))
2525 TREE_USED (olddecl) = 1;
2526 if (VAR_P (newdecl))
2528 if (DECL_READ_P (olddecl))
2529 DECL_READ_P (newdecl) = 1;
2530 else if (DECL_READ_P (newdecl))
2531 DECL_READ_P (olddecl) = 1;
2533 if (DECL_PRESERVE_P (olddecl))
2534 DECL_PRESERVE_P (newdecl) = 1;
2535 else if (DECL_PRESERVE_P (newdecl))
2536 DECL_PRESERVE_P (olddecl) = 1;
2538 /* Merge the DECL_FUNCTION_VERSIONED information. newdecl will be copied
2539 to olddecl and deleted. */
2540 if (TREE_CODE (newdecl) == FUNCTION_DECL
2541 && DECL_FUNCTION_VERSIONED (olddecl))
2543 /* Set the flag for newdecl so that it gets copied to olddecl. */
2544 DECL_FUNCTION_VERSIONED (newdecl) = 1;
2545 /* newdecl will be purged after copying to olddecl and is no longer
2546 a version. */
2547 cgraph_node::delete_function_version (newdecl);
2550 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2552 int function_size;
2553 struct symtab_node *snode = symtab_node::get (olddecl);
2555 function_size = sizeof (struct tree_decl_common);
2557 memcpy ((char *) olddecl + sizeof (struct tree_common),
2558 (char *) newdecl + sizeof (struct tree_common),
2559 function_size - sizeof (struct tree_common));
2561 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2562 (char *) newdecl + sizeof (struct tree_decl_common),
2563 sizeof (struct tree_function_decl) - sizeof (struct tree_decl_common));
2565 /* Preserve symtab node mapping. */
2566 olddecl->decl_with_vis.symtab_node = snode;
2568 if (new_template_info)
2569 /* If newdecl is a template instantiation, it is possible that
2570 the following sequence of events has occurred:
2572 o A friend function was declared in a class template. The
2573 class template was instantiated.
2575 o The instantiation of the friend declaration was
2576 recorded on the instantiation list, and is newdecl.
2578 o Later, however, instantiate_class_template called pushdecl
2579 on the newdecl to perform name injection. But, pushdecl in
2580 turn called duplicate_decls when it discovered that another
2581 declaration of a global function with the same name already
2582 existed.
2584 o Here, in duplicate_decls, we decided to clobber newdecl.
2586 If we're going to do that, we'd better make sure that
2587 olddecl, and not newdecl, is on the list of
2588 instantiations so that if we try to do the instantiation
2589 again we won't get the clobbered declaration. */
2590 reregister_specialization (newdecl,
2591 new_template_info,
2592 olddecl);
2594 else
2596 size_t size = tree_code_size (TREE_CODE (newdecl));
2598 memcpy ((char *) olddecl + sizeof (struct tree_common),
2599 (char *) newdecl + sizeof (struct tree_common),
2600 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
2601 switch (TREE_CODE (newdecl))
2603 case LABEL_DECL:
2604 case VAR_DECL:
2605 case RESULT_DECL:
2606 case PARM_DECL:
2607 case FIELD_DECL:
2608 case TYPE_DECL:
2609 case CONST_DECL:
2611 struct symtab_node *snode = NULL;
2613 if (VAR_P (newdecl)
2614 && (TREE_STATIC (olddecl) || TREE_PUBLIC (olddecl)
2615 || DECL_EXTERNAL (olddecl)))
2616 snode = symtab_node::get (olddecl);
2617 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2618 (char *) newdecl + sizeof (struct tree_decl_common),
2619 size - sizeof (struct tree_decl_common)
2620 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
2621 if (VAR_P (newdecl))
2622 olddecl->decl_with_vis.symtab_node = snode;
2624 break;
2625 default:
2626 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2627 (char *) newdecl + sizeof (struct tree_decl_common),
2628 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common)
2629 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
2630 break;
2634 if (VAR_OR_FUNCTION_DECL_P (newdecl))
2636 if (DECL_EXTERNAL (olddecl)
2637 || TREE_PUBLIC (olddecl)
2638 || TREE_STATIC (olddecl))
2640 /* Merge the section attribute.
2641 We want to issue an error if the sections conflict but that must be
2642 done later in decl_attributes since we are called before attributes
2643 are assigned. */
2644 if (DECL_SECTION_NAME (newdecl) != NULL)
2645 set_decl_section_name (olddecl, DECL_SECTION_NAME (newdecl));
2647 if (DECL_ONE_ONLY (newdecl))
2649 struct symtab_node *oldsym, *newsym;
2650 if (TREE_CODE (olddecl) == FUNCTION_DECL)
2651 oldsym = cgraph_node::get_create (olddecl);
2652 else
2653 oldsym = varpool_node::get_create (olddecl);
2654 newsym = symtab_node::get (newdecl);
2655 oldsym->set_comdat_group (newsym->get_comdat_group ());
2659 if (VAR_P (newdecl)
2660 && CP_DECL_THREAD_LOCAL_P (newdecl))
2662 CP_DECL_THREAD_LOCAL_P (olddecl) = true;
2663 if (!processing_template_decl)
2664 set_decl_tls_model (olddecl, DECL_TLS_MODEL (newdecl));
2668 DECL_UID (olddecl) = olddecl_uid;
2669 if (olddecl_friend)
2670 DECL_FRIEND_P (olddecl) = 1;
2671 if (hidden_friend)
2673 DECL_ANTICIPATED (olddecl) = 1;
2674 DECL_HIDDEN_FRIEND_P (olddecl) = 1;
2677 /* NEWDECL contains the merged attribute lists.
2678 Update OLDDECL to be the same. */
2679 DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
2681 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
2682 so that encode_section_info has a chance to look at the new decl
2683 flags and attributes. */
2684 if (DECL_RTL_SET_P (olddecl)
2685 && (TREE_CODE (olddecl) == FUNCTION_DECL
2686 || (VAR_P (olddecl)
2687 && TREE_STATIC (olddecl))))
2688 make_decl_rtl (olddecl);
2690 /* The NEWDECL will no longer be needed. Because every out-of-class
2691 declaration of a member results in a call to duplicate_decls,
2692 freeing these nodes represents in a significant savings.
2694 Before releasing the node, be sore to remove function from symbol
2695 table that might have been inserted there to record comdat group.
2696 Be sure to however do not free DECL_STRUCT_FUNCTION because this
2697 structure is shared in between newdecl and oldecl. */
2698 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2699 DECL_STRUCT_FUNCTION (newdecl) = NULL;
2700 if (VAR_OR_FUNCTION_DECL_P (newdecl))
2702 struct symtab_node *snode = symtab_node::get (newdecl);
2703 if (snode)
2704 snode->remove ();
2707 /* Remove the associated constraints for newdecl, if any, before
2708 reclaiming memory. */
2709 if (flag_concepts)
2710 remove_constraints (newdecl);
2712 ggc_free (newdecl);
2714 return olddecl;
2717 /* Return zero if the declaration NEWDECL is valid
2718 when the declaration OLDDECL (assumed to be for the same name)
2719 has already been seen.
2720 Otherwise return an error message format string with a %s
2721 where the identifier should go. */
2723 static const char *
2724 redeclaration_error_message (tree newdecl, tree olddecl)
2726 if (TREE_CODE (newdecl) == TYPE_DECL)
2728 /* Because C++ can put things into name space for free,
2729 constructs like "typedef struct foo { ... } foo"
2730 would look like an erroneous redeclaration. */
2731 if (same_type_p (TREE_TYPE (newdecl), TREE_TYPE (olddecl)))
2732 return NULL;
2733 else
2734 return G_("redefinition of %q#D");
2736 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
2738 /* If this is a pure function, its olddecl will actually be
2739 the original initialization to `0' (which we force to call
2740 abort()). Don't complain about redefinition in this case. */
2741 if (DECL_LANG_SPECIFIC (olddecl) && DECL_PURE_VIRTUAL_P (olddecl)
2742 && DECL_INITIAL (olddecl) == NULL_TREE)
2743 return NULL;
2745 /* If both functions come from different namespaces, this is not
2746 a redeclaration - this is a conflict with a used function. */
2747 if (DECL_NAMESPACE_SCOPE_P (olddecl)
2748 && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl)
2749 && ! decls_match (olddecl, newdecl))
2750 return G_("%qD conflicts with used function");
2752 /* We'll complain about linkage mismatches in
2753 warn_extern_redeclared_static. */
2755 /* Defining the same name twice is no good. */
2756 if (DECL_INITIAL (olddecl) != NULL_TREE
2757 && DECL_INITIAL (newdecl) != NULL_TREE)
2759 if (DECL_NAME (olddecl) == NULL_TREE)
2760 return G_("%q#D not declared in class");
2761 else if (!GNU_INLINE_P (olddecl)
2762 || GNU_INLINE_P (newdecl))
2763 return G_("redefinition of %q#D");
2766 if (DECL_DECLARED_INLINE_P (olddecl) && DECL_DECLARED_INLINE_P (newdecl))
2768 bool olda = GNU_INLINE_P (olddecl);
2769 bool newa = GNU_INLINE_P (newdecl);
2771 if (olda != newa)
2773 if (newa)
2774 return G_("%q+D redeclared inline with "
2775 "%<gnu_inline%> attribute");
2776 else
2777 return G_("%q+D redeclared inline without "
2778 "%<gnu_inline%> attribute");
2782 check_abi_tag_redeclaration
2783 (olddecl, lookup_attribute ("abi_tag", DECL_ATTRIBUTES (olddecl)),
2784 lookup_attribute ("abi_tag", DECL_ATTRIBUTES (newdecl)));
2786 return NULL;
2788 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2790 tree nt, ot;
2792 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
2794 if (COMPLETE_TYPE_P (TREE_TYPE (newdecl))
2795 && COMPLETE_TYPE_P (TREE_TYPE (olddecl)))
2796 return G_("redefinition of %q#D");
2797 return NULL;
2800 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) != FUNCTION_DECL
2801 || (DECL_TEMPLATE_RESULT (newdecl)
2802 == DECL_TEMPLATE_RESULT (olddecl)))
2803 return NULL;
2805 nt = DECL_TEMPLATE_RESULT (newdecl);
2806 if (DECL_TEMPLATE_INFO (nt))
2807 nt = DECL_TEMPLATE_RESULT (template_for_substitution (nt));
2808 ot = DECL_TEMPLATE_RESULT (olddecl);
2809 if (DECL_TEMPLATE_INFO (ot))
2810 ot = DECL_TEMPLATE_RESULT (template_for_substitution (ot));
2811 if (DECL_INITIAL (nt) && DECL_INITIAL (ot)
2812 && (!GNU_INLINE_P (ot) || GNU_INLINE_P (nt)))
2813 return G_("redefinition of %q#D");
2815 if (DECL_DECLARED_INLINE_P (ot) && DECL_DECLARED_INLINE_P (nt))
2817 bool olda = GNU_INLINE_P (ot);
2818 bool newa = GNU_INLINE_P (nt);
2820 if (olda != newa)
2822 if (newa)
2823 return G_("%q+D redeclared inline with "
2824 "%<gnu_inline%> attribute");
2825 else
2826 return G_("%q+D redeclared inline without "
2827 "%<gnu_inline%> attribute");
2831 /* Core issue #226 (C++0x):
2833 If a friend function template declaration specifies a
2834 default template-argument, that declaration shall be a
2835 definition and shall be the only declaration of the
2836 function template in the translation unit. */
2837 if ((cxx_dialect != cxx98)
2838 && TREE_CODE (ot) == FUNCTION_DECL && DECL_FRIEND_P (ot)
2839 && !check_default_tmpl_args (nt, DECL_TEMPLATE_PARMS (newdecl),
2840 /*is_primary=*/true,
2841 /*is_partial=*/false,
2842 /*is_friend_decl=*/2))
2843 return G_("redeclaration of friend %q#D "
2844 "may not have default template arguments");
2846 return NULL;
2848 else if (VAR_P (newdecl)
2849 && CP_DECL_THREAD_LOCAL_P (newdecl) != CP_DECL_THREAD_LOCAL_P (olddecl)
2850 && (! DECL_LANG_SPECIFIC (olddecl)
2851 || ! CP_DECL_THREADPRIVATE_P (olddecl)
2852 || CP_DECL_THREAD_LOCAL_P (newdecl)))
2854 /* Only variables can be thread-local, and all declarations must
2855 agree on this property. */
2856 if (CP_DECL_THREAD_LOCAL_P (newdecl))
2857 return G_("thread-local declaration of %q#D follows "
2858 "non-thread-local declaration");
2859 else
2860 return G_("non-thread-local declaration of %q#D follows "
2861 "thread-local declaration");
2863 else if (toplevel_bindings_p () || DECL_NAMESPACE_SCOPE_P (newdecl))
2865 /* The objects have been declared at namespace scope. If either
2866 is a member of an anonymous union, then this is an invalid
2867 redeclaration. For example:
2869 int i;
2870 union { int i; };
2872 is invalid. */
2873 if ((VAR_P (newdecl) && DECL_ANON_UNION_VAR_P (newdecl))
2874 || (VAR_P (olddecl) && DECL_ANON_UNION_VAR_P (olddecl)))
2875 return G_("redeclaration of %q#D");
2876 /* If at least one declaration is a reference, there is no
2877 conflict. For example:
2879 int i = 3;
2880 extern int i;
2882 is valid. */
2883 if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
2884 return NULL;
2885 /* Reject two definitions. */
2886 return G_("redefinition of %q#D");
2888 else
2890 /* Objects declared with block scope: */
2891 /* Reject two definitions, and reject a definition
2892 together with an external reference. */
2893 if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl)))
2894 return G_("redeclaration of %q#D");
2895 return NULL;
2899 /* Hash and equality functions for the named_label table. */
2901 hashval_t
2902 named_label_hasher::hash (named_label_entry *ent)
2904 return DECL_UID (ent->label_decl);
2907 bool
2908 named_label_hasher::equal (named_label_entry *a, named_label_entry *b)
2910 return a->label_decl == b->label_decl;
2913 /* Create a new label, named ID. */
2915 static tree
2916 make_label_decl (tree id, int local_p)
2918 struct named_label_entry *ent;
2919 tree decl;
2921 decl = build_decl (input_location, LABEL_DECL, id, void_type_node);
2923 DECL_CONTEXT (decl) = current_function_decl;
2924 DECL_MODE (decl) = VOIDmode;
2925 C_DECLARED_LABEL_FLAG (decl) = local_p;
2927 /* Say where one reference is to the label, for the sake of the
2928 error if it is not defined. */
2929 DECL_SOURCE_LOCATION (decl) = input_location;
2931 /* Record the fact that this identifier is bound to this label. */
2932 SET_IDENTIFIER_LABEL_VALUE (id, decl);
2934 /* Create the label htab for the function on demand. */
2935 if (!named_labels)
2936 named_labels = hash_table<named_label_hasher>::create_ggc (13);
2938 /* Record this label on the list of labels used in this function.
2939 We do this before calling make_label_decl so that we get the
2940 IDENTIFIER_LABEL_VALUE before the new label is declared. */
2941 ent = ggc_cleared_alloc<named_label_entry> ();
2942 ent->label_decl = decl;
2944 named_label_entry **slot = named_labels->find_slot (ent, INSERT);
2945 gcc_assert (*slot == NULL);
2946 *slot = ent;
2948 return decl;
2951 /* Look for a label named ID in the current function. If one cannot
2952 be found, create one. (We keep track of used, but undefined,
2953 labels, and complain about them at the end of a function.) */
2955 static tree
2956 lookup_label_1 (tree id)
2958 tree decl;
2960 /* You can't use labels at global scope. */
2961 if (current_function_decl == NULL_TREE)
2963 error ("label %qE referenced outside of any function", id);
2964 return NULL_TREE;
2967 /* See if we've already got this label. */
2968 decl = IDENTIFIER_LABEL_VALUE (id);
2969 if (decl != NULL_TREE && DECL_CONTEXT (decl) == current_function_decl)
2970 return decl;
2972 decl = make_label_decl (id, /*local_p=*/0);
2973 return decl;
2976 /* Wrapper for lookup_label_1. */
2978 tree
2979 lookup_label (tree id)
2981 tree ret;
2982 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
2983 ret = lookup_label_1 (id);
2984 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
2985 return ret;
2988 /* Declare a local label named ID. */
2990 tree
2991 declare_local_label (tree id)
2993 tree decl;
2994 cp_label_binding bind;
2996 /* Add a new entry to the SHADOWED_LABELS list so that when we leave
2997 this scope we can restore the old value of IDENTIFIER_TYPE_VALUE. */
2998 bind.prev_value = IDENTIFIER_LABEL_VALUE (id);
3000 decl = make_label_decl (id, /*local_p=*/1);
3001 bind.label = decl;
3002 vec_safe_push (current_binding_level->shadowed_labels, bind);
3004 return decl;
3007 /* Returns nonzero if it is ill-formed to jump past the declaration of
3008 DECL. Returns 2 if it's also a real problem. */
3010 static int
3011 decl_jump_unsafe (tree decl)
3013 /* [stmt.dcl]/3: A program that jumps from a point where a local variable
3014 with automatic storage duration is not in scope to a point where it is
3015 in scope is ill-formed unless the variable has scalar type, class type
3016 with a trivial default constructor and a trivial destructor, a
3017 cv-qualified version of one of these types, or an array of one of the
3018 preceding types and is declared without an initializer (8.5). */
3019 tree type = TREE_TYPE (decl);
3021 if (!VAR_P (decl) || TREE_STATIC (decl)
3022 || type == error_mark_node)
3023 return 0;
3025 if (DECL_NONTRIVIALLY_INITIALIZED_P (decl)
3026 || variably_modified_type_p (type, NULL_TREE))
3027 return 2;
3029 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3030 return 1;
3032 return 0;
3035 /* A subroutine of check_previous_goto_1 and check_goto to identify a branch
3036 to the user. */
3038 static bool
3039 identify_goto (tree decl, location_t loc, const location_t *locus,
3040 diagnostic_t diag_kind)
3042 bool complained
3043 = (decl ? emit_diagnostic (diag_kind, loc, 0, "jump to label %qD", decl)
3044 : emit_diagnostic (diag_kind, loc, 0, "jump to case label"));
3045 if (complained && locus)
3046 inform (*locus, " from here");
3047 return complained;
3050 /* Check that a single previously seen jump to a newly defined label
3051 is OK. DECL is the LABEL_DECL or 0; LEVEL is the binding_level for
3052 the jump context; NAMES are the names in scope in LEVEL at the jump
3053 context; LOCUS is the source position of the jump or 0. Returns
3054 true if all is well. */
3056 static bool
3057 check_previous_goto_1 (tree decl, cp_binding_level* level, tree names,
3058 bool exited_omp, const location_t *locus)
3060 cp_binding_level *b;
3061 bool complained = false;
3062 int identified = 0;
3063 bool saw_eh = false, saw_omp = false, saw_tm = false, saw_cxif = false;
3065 if (exited_omp)
3067 complained = identify_goto (decl, input_location, locus, DK_ERROR);
3068 if (complained)
3069 inform (input_location, " exits OpenMP structured block");
3070 saw_omp = true;
3071 identified = 2;
3074 for (b = current_binding_level; b ; b = b->level_chain)
3076 tree new_decls, old_decls = (b == level ? names : NULL_TREE);
3078 for (new_decls = b->names; new_decls != old_decls;
3079 new_decls = (DECL_P (new_decls) ? DECL_CHAIN (new_decls)
3080 : TREE_CHAIN (new_decls)))
3082 int problem = decl_jump_unsafe (new_decls);
3083 if (! problem)
3084 continue;
3086 if (!identified)
3088 complained = identify_goto (decl, input_location, locus,
3089 DK_PERMERROR);
3090 identified = 1;
3092 if (complained)
3094 if (problem > 1)
3095 inform (DECL_SOURCE_LOCATION (new_decls),
3096 " crosses initialization of %q#D", new_decls);
3097 else
3098 inform (DECL_SOURCE_LOCATION (new_decls),
3099 " enters scope of %q#D which has "
3100 "non-trivial destructor", new_decls);
3104 if (b == level)
3105 break;
3106 if ((b->kind == sk_try || b->kind == sk_catch) && !saw_eh)
3108 if (identified < 2)
3110 complained = identify_goto (decl, input_location, locus,
3111 DK_ERROR);
3112 identified = 2;
3114 if (complained)
3116 if (b->kind == sk_try)
3117 inform (input_location, " enters try block");
3118 else
3119 inform (input_location, " enters catch block");
3121 saw_eh = true;
3123 if (b->kind == sk_omp && !saw_omp)
3125 if (identified < 2)
3127 complained = identify_goto (decl, input_location, locus,
3128 DK_ERROR);
3129 identified = 2;
3131 if (complained)
3132 inform (input_location, " enters OpenMP structured block");
3133 saw_omp = true;
3135 if (b->kind == sk_transaction && !saw_tm)
3137 if (identified < 2)
3139 complained = identify_goto (decl, input_location, locus,
3140 DK_ERROR);
3141 identified = 2;
3143 if (complained)
3144 inform (input_location,
3145 " enters synchronized or atomic statement");
3146 saw_tm = true;
3148 if (!saw_cxif && b->kind == sk_block
3149 && level_for_constexpr_if (b->level_chain))
3151 if (identified < 2)
3153 complained = identify_goto (decl, input_location, locus,
3154 DK_ERROR);
3155 identified = 2;
3157 if (complained)
3158 inform (EXPR_LOCATION (b->level_chain->this_entity),
3159 " enters constexpr if statement");
3160 saw_cxif = true;
3164 return !identified;
3167 static void
3168 check_previous_goto (tree decl, struct named_label_use_entry *use)
3170 check_previous_goto_1 (decl, use->binding_level,
3171 use->names_in_scope, use->in_omp_scope,
3172 &use->o_goto_locus);
3175 static bool
3176 check_switch_goto (cp_binding_level* level)
3178 return check_previous_goto_1 (NULL_TREE, level, level->names, false, NULL);
3181 /* Check that a new jump to a label DECL is OK. Called by
3182 finish_goto_stmt. */
3184 void
3185 check_goto (tree decl)
3187 struct named_label_entry *ent, dummy;
3188 bool saw_catch = false, complained = false;
3189 int identified = 0;
3190 tree bad;
3191 unsigned ix;
3193 /* We can't know where a computed goto is jumping.
3194 So we assume that it's OK. */
3195 if (TREE_CODE (decl) != LABEL_DECL)
3196 return;
3198 /* We didn't record any information about this label when we created it,
3199 and there's not much point since it's trivial to analyze as a return. */
3200 if (decl == cdtor_label)
3201 return;
3203 dummy.label_decl = decl;
3204 ent = named_labels->find (&dummy);
3205 gcc_assert (ent != NULL);
3207 /* If the label hasn't been defined yet, defer checking. */
3208 if (! DECL_INITIAL (decl))
3210 struct named_label_use_entry *new_use;
3212 /* Don't bother creating another use if the last goto had the
3213 same data, and will therefore create the same set of errors. */
3214 if (ent->uses
3215 && ent->uses->names_in_scope == current_binding_level->names)
3216 return;
3218 new_use = ggc_alloc<named_label_use_entry> ();
3219 new_use->binding_level = current_binding_level;
3220 new_use->names_in_scope = current_binding_level->names;
3221 new_use->o_goto_locus = input_location;
3222 new_use->in_omp_scope = false;
3224 new_use->next = ent->uses;
3225 ent->uses = new_use;
3226 return;
3229 if (ent->in_try_scope || ent->in_catch_scope || ent->in_transaction_scope
3230 || ent->in_constexpr_if
3231 || ent->in_omp_scope || !vec_safe_is_empty (ent->bad_decls))
3233 diagnostic_t diag_kind = DK_PERMERROR;
3234 if (ent->in_try_scope || ent->in_catch_scope || ent->in_constexpr_if
3235 || ent->in_transaction_scope || ent->in_omp_scope)
3236 diag_kind = DK_ERROR;
3237 complained = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
3238 &input_location, diag_kind);
3239 identified = 1 + (diag_kind == DK_ERROR);
3242 FOR_EACH_VEC_SAFE_ELT (ent->bad_decls, ix, bad)
3244 int u = decl_jump_unsafe (bad);
3246 if (u > 1 && DECL_ARTIFICIAL (bad))
3248 /* Can't skip init of __exception_info. */
3249 if (identified == 1)
3251 complained = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
3252 &input_location, DK_ERROR);
3253 identified = 2;
3255 if (complained)
3256 inform (DECL_SOURCE_LOCATION (bad), " enters catch block");
3257 saw_catch = true;
3259 else if (complained)
3261 if (u > 1)
3262 inform (DECL_SOURCE_LOCATION (bad),
3263 " skips initialization of %q#D", bad);
3264 else
3265 inform (DECL_SOURCE_LOCATION (bad),
3266 " enters scope of %q#D which has "
3267 "non-trivial destructor", bad);
3271 if (complained)
3273 if (ent->in_try_scope)
3274 inform (input_location, " enters try block");
3275 else if (ent->in_catch_scope && !saw_catch)
3276 inform (input_location, " enters catch block");
3277 else if (ent->in_transaction_scope)
3278 inform (input_location, " enters synchronized or atomic statement");
3279 else if (ent->in_constexpr_if)
3280 inform (input_location, " enters constexpr if statement");
3283 if (ent->in_omp_scope)
3285 if (complained)
3286 inform (input_location, " enters OpenMP structured block");
3288 else if (flag_openmp)
3290 cp_binding_level *b;
3291 for (b = current_binding_level; b ; b = b->level_chain)
3293 if (b == ent->binding_level)
3294 break;
3295 if (b->kind == sk_omp)
3297 if (identified < 2)
3299 complained = identify_goto (decl,
3300 DECL_SOURCE_LOCATION (decl),
3301 &input_location, DK_ERROR);
3302 identified = 2;
3304 if (complained)
3305 inform (input_location, " exits OpenMP structured block");
3306 break;
3312 /* Check that a return is ok wrt OpenMP structured blocks.
3313 Called by finish_return_stmt. Returns true if all is well. */
3315 bool
3316 check_omp_return (void)
3318 cp_binding_level *b;
3319 for (b = current_binding_level; b ; b = b->level_chain)
3320 if (b->kind == sk_omp)
3322 error ("invalid exit from OpenMP structured block");
3323 return false;
3325 else if (b->kind == sk_function_parms)
3326 break;
3327 return true;
3330 /* Define a label, specifying the location in the source file.
3331 Return the LABEL_DECL node for the label. */
3333 static tree
3334 define_label_1 (location_t location, tree name)
3336 struct named_label_entry *ent, dummy;
3337 cp_binding_level *p;
3338 tree decl;
3340 decl = lookup_label (name);
3342 dummy.label_decl = decl;
3343 ent = named_labels->find (&dummy);
3344 gcc_assert (ent != NULL);
3346 /* After labels, make any new cleanups in the function go into their
3347 own new (temporary) binding contour. */
3348 for (p = current_binding_level;
3349 p->kind != sk_function_parms;
3350 p = p->level_chain)
3351 p->more_cleanups_ok = 0;
3353 if (name == get_identifier ("wchar_t"))
3354 permerror (input_location, "label named wchar_t");
3356 if (DECL_INITIAL (decl) != NULL_TREE)
3358 error ("duplicate label %qD", decl);
3359 return error_mark_node;
3361 else
3363 struct named_label_use_entry *use;
3365 /* Mark label as having been defined. */
3366 DECL_INITIAL (decl) = error_mark_node;
3367 /* Say where in the source. */
3368 DECL_SOURCE_LOCATION (decl) = location;
3370 ent->binding_level = current_binding_level;
3371 ent->names_in_scope = current_binding_level->names;
3373 for (use = ent->uses; use ; use = use->next)
3374 check_previous_goto (decl, use);
3375 ent->uses = NULL;
3378 return decl;
3381 /* Wrapper for define_label_1. */
3383 tree
3384 define_label (location_t location, tree name)
3386 tree ret;
3387 bool running = timevar_cond_start (TV_NAME_LOOKUP);
3388 ret = define_label_1 (location, name);
3389 timevar_cond_stop (TV_NAME_LOOKUP, running);
3390 return ret;
3394 struct cp_switch
3396 cp_binding_level *level;
3397 struct cp_switch *next;
3398 /* The SWITCH_STMT being built. */
3399 tree switch_stmt;
3400 /* A splay-tree mapping the low element of a case range to the high
3401 element, or NULL_TREE if there is no high element. Used to
3402 determine whether or not a new case label duplicates an old case
3403 label. We need a tree, rather than simply a hash table, because
3404 of the GNU case range extension. */
3405 splay_tree cases;
3406 /* Remember whether there was a case value that is outside the
3407 range of the original type of the controlling expression. */
3408 bool outside_range_p;
3411 /* A stack of the currently active switch statements. The innermost
3412 switch statement is on the top of the stack. There is no need to
3413 mark the stack for garbage collection because it is only active
3414 during the processing of the body of a function, and we never
3415 collect at that point. */
3417 static struct cp_switch *switch_stack;
3419 /* Called right after a switch-statement condition is parsed.
3420 SWITCH_STMT is the switch statement being parsed. */
3422 void
3423 push_switch (tree switch_stmt)
3425 struct cp_switch *p = XNEW (struct cp_switch);
3426 p->level = current_binding_level;
3427 p->next = switch_stack;
3428 p->switch_stmt = switch_stmt;
3429 p->cases = splay_tree_new (case_compare, NULL, NULL);
3430 p->outside_range_p = false;
3431 switch_stack = p;
3434 void
3435 pop_switch (void)
3437 struct cp_switch *cs = switch_stack;
3438 location_t switch_location;
3440 /* Emit warnings as needed. */
3441 switch_location = EXPR_LOC_OR_LOC (cs->switch_stmt, input_location);
3442 const bool bool_cond_p
3443 = (SWITCH_STMT_TYPE (cs->switch_stmt)
3444 && TREE_CODE (SWITCH_STMT_TYPE (cs->switch_stmt)) == BOOLEAN_TYPE);
3445 if (!processing_template_decl)
3446 c_do_switch_warnings (cs->cases, switch_location,
3447 SWITCH_STMT_TYPE (cs->switch_stmt),
3448 SWITCH_STMT_COND (cs->switch_stmt),
3449 bool_cond_p, cs->outside_range_p);
3451 splay_tree_delete (cs->cases);
3452 switch_stack = switch_stack->next;
3453 free (cs);
3456 /* Convert a case constant VALUE in a switch to the type TYPE of the switch
3457 condition. Note that if TYPE and VALUE are already integral we don't
3458 really do the conversion because the language-independent
3459 warning/optimization code will work better that way. */
3461 static tree
3462 case_conversion (tree type, tree value)
3464 if (value == NULL_TREE)
3465 return value;
3467 if (cxx_dialect >= cxx11
3468 && (SCOPED_ENUM_P (type)
3469 || !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (value))))
3471 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
3472 type = type_promotes_to (type);
3473 value = (perform_implicit_conversion_flags
3474 (type, value, tf_warning_or_error,
3475 LOOKUP_IMPLICIT | LOOKUP_NO_NON_INTEGRAL));
3477 return cxx_constant_value (value);
3480 /* Note that we've seen a definition of a case label, and complain if this
3481 is a bad place for one. */
3483 tree
3484 finish_case_label (location_t loc, tree low_value, tree high_value)
3486 tree cond, r;
3487 cp_binding_level *p;
3488 tree type;
3490 if (processing_template_decl)
3492 tree label;
3494 /* For templates, just add the case label; we'll do semantic
3495 analysis at instantiation-time. */
3496 label = build_decl (loc, LABEL_DECL, NULL_TREE, NULL_TREE);
3497 return add_stmt (build_case_label (low_value, high_value, label));
3500 /* Find the condition on which this switch statement depends. */
3501 cond = SWITCH_STMT_COND (switch_stack->switch_stmt);
3502 if (cond && TREE_CODE (cond) == TREE_LIST)
3503 cond = TREE_VALUE (cond);
3505 if (!check_switch_goto (switch_stack->level))
3506 return error_mark_node;
3508 type = SWITCH_STMT_TYPE (switch_stack->switch_stmt);
3510 low_value = case_conversion (type, low_value);
3511 high_value = case_conversion (type, high_value);
3513 r = c_add_case_label (loc, switch_stack->cases, cond, type,
3514 low_value, high_value,
3515 &switch_stack->outside_range_p);
3517 /* After labels, make any new cleanups in the function go into their
3518 own new (temporary) binding contour. */
3519 for (p = current_binding_level;
3520 p->kind != sk_function_parms;
3521 p = p->level_chain)
3522 p->more_cleanups_ok = 0;
3524 return r;
3527 struct typename_info {
3528 tree scope;
3529 tree name;
3530 tree template_id;
3531 bool enum_p;
3532 bool class_p;
3535 struct typename_hasher : ggc_ptr_hash<tree_node>
3537 typedef typename_info *compare_type;
3539 /* Hash a TYPENAME_TYPE. */
3541 static hashval_t
3542 hash (tree t)
3544 hashval_t hash;
3546 hash = (htab_hash_pointer (TYPE_CONTEXT (t))
3547 ^ htab_hash_pointer (TYPE_IDENTIFIER (t)));
3549 return hash;
3552 /* Compare two TYPENAME_TYPEs. */
3554 static bool
3555 equal (tree t1, const typename_info *t2)
3557 return (TYPE_IDENTIFIER (t1) == t2->name
3558 && TYPE_CONTEXT (t1) == t2->scope
3559 && TYPENAME_TYPE_FULLNAME (t1) == t2->template_id
3560 && TYPENAME_IS_ENUM_P (t1) == t2->enum_p
3561 && TYPENAME_IS_CLASS_P (t1) == t2->class_p);
3565 /* Build a TYPENAME_TYPE. If the type is `typename T::t', CONTEXT is
3566 the type of `T', NAME is the IDENTIFIER_NODE for `t'.
3568 Returns the new TYPENAME_TYPE. */
3570 static GTY (()) hash_table<typename_hasher> *typename_htab;
3572 static tree
3573 build_typename_type (tree context, tree name, tree fullname,
3574 enum tag_types tag_type)
3576 tree t;
3577 tree d;
3578 typename_info ti;
3579 tree *e;
3580 hashval_t hash;
3582 if (typename_htab == NULL)
3583 typename_htab = hash_table<typename_hasher>::create_ggc (61);
3585 ti.scope = FROB_CONTEXT (context);
3586 ti.name = name;
3587 ti.template_id = fullname;
3588 ti.enum_p = tag_type == enum_type;
3589 ti.class_p = (tag_type == class_type
3590 || tag_type == record_type
3591 || tag_type == union_type);
3592 hash = (htab_hash_pointer (ti.scope)
3593 ^ htab_hash_pointer (ti.name));
3595 /* See if we already have this type. */
3596 e = typename_htab->find_slot_with_hash (&ti, hash, INSERT);
3597 if (*e)
3598 t = *e;
3599 else
3601 /* Build the TYPENAME_TYPE. */
3602 t = cxx_make_type (TYPENAME_TYPE);
3603 TYPE_CONTEXT (t) = ti.scope;
3604 TYPENAME_TYPE_FULLNAME (t) = ti.template_id;
3605 TYPENAME_IS_ENUM_P (t) = ti.enum_p;
3606 TYPENAME_IS_CLASS_P (t) = ti.class_p;
3608 /* Build the corresponding TYPE_DECL. */
3609 d = build_decl (input_location, TYPE_DECL, name, t);
3610 TYPE_NAME (TREE_TYPE (d)) = d;
3611 TYPE_STUB_DECL (TREE_TYPE (d)) = d;
3612 DECL_CONTEXT (d) = FROB_CONTEXT (context);
3613 DECL_ARTIFICIAL (d) = 1;
3615 /* Store it in the hash table. */
3616 *e = t;
3618 /* TYPENAME_TYPEs must always be compared structurally, because
3619 they may or may not resolve down to another type depending on
3620 the currently open classes. */
3621 SET_TYPE_STRUCTURAL_EQUALITY (t);
3624 return t;
3627 /* Resolve `typename CONTEXT::NAME'. TAG_TYPE indicates the tag
3628 provided to name the type. Returns an appropriate type, unless an
3629 error occurs, in which case error_mark_node is returned. If we
3630 locate a non-artificial TYPE_DECL and TF_KEEP_TYPE_DECL is set, we
3631 return that, rather than the _TYPE it corresponds to, in other
3632 cases we look through the type decl. If TF_ERROR is set, complain
3633 about errors, otherwise be quiet. */
3635 tree
3636 make_typename_type (tree context, tree name, enum tag_types tag_type,
3637 tsubst_flags_t complain)
3639 tree fullname;
3640 tree t;
3641 bool want_template;
3643 if (name == error_mark_node
3644 || context == NULL_TREE
3645 || context == error_mark_node)
3646 return error_mark_node;
3648 if (TYPE_P (name))
3650 if (!(TYPE_LANG_SPECIFIC (name)
3651 && (CLASSTYPE_IS_TEMPLATE (name)
3652 || CLASSTYPE_USE_TEMPLATE (name))))
3653 name = TYPE_IDENTIFIER (name);
3654 else
3655 /* Create a TEMPLATE_ID_EXPR for the type. */
3656 name = build_nt (TEMPLATE_ID_EXPR,
3657 CLASSTYPE_TI_TEMPLATE (name),
3658 CLASSTYPE_TI_ARGS (name));
3660 else if (TREE_CODE (name) == TYPE_DECL)
3661 name = DECL_NAME (name);
3663 fullname = name;
3665 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3667 name = TREE_OPERAND (name, 0);
3668 if (DECL_TYPE_TEMPLATE_P (name))
3669 name = TREE_OPERAND (fullname, 0) = DECL_NAME (name);
3670 if (TREE_CODE (name) != IDENTIFIER_NODE)
3672 if (complain & tf_error)
3673 error ("%qD is not a type", name);
3674 return error_mark_node;
3677 if (TREE_CODE (name) == TEMPLATE_DECL)
3679 if (complain & tf_error)
3680 error ("%qD used without template parameters", name);
3681 return error_mark_node;
3683 gcc_assert (identifier_p (name));
3684 gcc_assert (TYPE_P (context));
3686 if (!MAYBE_CLASS_TYPE_P (context))
3688 if (complain & tf_error)
3689 error ("%q#T is not a class", context);
3690 return error_mark_node;
3693 /* When the CONTEXT is a dependent type, NAME could refer to a
3694 dependent base class of CONTEXT. But look inside it anyway
3695 if CONTEXT is a currently open scope, in case it refers to a
3696 member of the current instantiation or a non-dependent base;
3697 lookup will stop when we hit a dependent base. */
3698 if (!dependent_scope_p (context))
3699 /* We should only set WANT_TYPE when we're a nested typename type.
3700 Then we can give better diagnostics if we find a non-type. */
3701 t = lookup_field (context, name, 2, /*want_type=*/true);
3702 else
3703 t = NULL_TREE;
3705 if ((!t || TREE_CODE (t) == TREE_LIST) && dependent_type_p (context))
3706 return build_typename_type (context, name, fullname, tag_type);
3708 want_template = TREE_CODE (fullname) == TEMPLATE_ID_EXPR;
3710 if (!t)
3712 if (complain & tf_error)
3713 error (want_template ? G_("no class template named %q#T in %q#T")
3714 : G_("no type named %q#T in %q#T"), name, context);
3715 return error_mark_node;
3718 /* Pull out the template from an injected-class-name (or multiple). */
3719 if (want_template)
3720 t = maybe_get_template_decl_from_type_decl (t);
3722 if (TREE_CODE (t) == TREE_LIST)
3724 if (complain & tf_error)
3726 error ("lookup of %qT in %qT is ambiguous", name, context);
3727 print_candidates (t);
3729 return error_mark_node;
3732 if (want_template && !DECL_TYPE_TEMPLATE_P (t))
3734 if (complain & tf_error)
3735 error ("%<typename %T::%D%> names %q#T, which is not a class template",
3736 context, name, t);
3737 return error_mark_node;
3739 if (!want_template && TREE_CODE (t) != TYPE_DECL)
3741 if (complain & tf_error)
3742 error ("%<typename %T::%D%> names %q#T, which is not a type",
3743 context, name, t);
3744 return error_mark_node;
3747 if (!perform_or_defer_access_check (TYPE_BINFO (context), t, t, complain))
3748 return error_mark_node;
3750 /* If we are currently parsing a template and if T is a typedef accessed
3751 through CONTEXT then we need to remember and check access of T at
3752 template instantiation time. */
3753 add_typedef_to_current_template_for_access_check (t, context, input_location);
3755 if (want_template)
3756 return lookup_template_class (t, TREE_OPERAND (fullname, 1),
3757 NULL_TREE, context,
3758 /*entering_scope=*/0,
3759 complain | tf_user);
3761 if (DECL_ARTIFICIAL (t) || !(complain & tf_keep_type_decl))
3762 t = TREE_TYPE (t);
3764 maybe_record_typedef_use (t);
3766 return t;
3769 /* Resolve `CONTEXT::template NAME'. Returns a TEMPLATE_DECL if the name
3770 can be resolved or an UNBOUND_CLASS_TEMPLATE, unless an error occurs,
3771 in which case error_mark_node is returned.
3773 If PARM_LIST is non-NULL, also make sure that the template parameter
3774 list of TEMPLATE_DECL matches.
3776 If COMPLAIN zero, don't complain about any errors that occur. */
3778 tree
3779 make_unbound_class_template (tree context, tree name, tree parm_list,
3780 tsubst_flags_t complain)
3782 tree t;
3783 tree d;
3785 if (TYPE_P (name))
3786 name = TYPE_IDENTIFIER (name);
3787 else if (DECL_P (name))
3788 name = DECL_NAME (name);
3789 gcc_assert (identifier_p (name));
3791 if (!dependent_type_p (context)
3792 || currently_open_class (context))
3794 tree tmpl = NULL_TREE;
3796 if (MAYBE_CLASS_TYPE_P (context))
3797 tmpl = lookup_field (context, name, 0, false);
3799 if (tmpl && TREE_CODE (tmpl) == TYPE_DECL)
3800 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
3802 if (!tmpl || !DECL_TYPE_TEMPLATE_P (tmpl))
3804 if (complain & tf_error)
3805 error ("no class template named %q#T in %q#T", name, context);
3806 return error_mark_node;
3809 if (parm_list
3810 && !comp_template_parms (DECL_TEMPLATE_PARMS (tmpl), parm_list))
3812 if (complain & tf_error)
3814 error ("template parameters do not match template %qD", tmpl);
3815 inform (DECL_SOURCE_LOCATION (tmpl),
3816 "%qD declared here", tmpl);
3818 return error_mark_node;
3821 if (!perform_or_defer_access_check (TYPE_BINFO (context), tmpl, tmpl,
3822 complain))
3823 return error_mark_node;
3825 return tmpl;
3828 /* Build the UNBOUND_CLASS_TEMPLATE. */
3829 t = cxx_make_type (UNBOUND_CLASS_TEMPLATE);
3830 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
3831 TREE_TYPE (t) = NULL_TREE;
3832 SET_TYPE_STRUCTURAL_EQUALITY (t);
3834 /* Build the corresponding TEMPLATE_DECL. */
3835 d = build_decl (input_location, TEMPLATE_DECL, name, t);
3836 TYPE_NAME (TREE_TYPE (d)) = d;
3837 TYPE_STUB_DECL (TREE_TYPE (d)) = d;
3838 DECL_CONTEXT (d) = FROB_CONTEXT (context);
3839 DECL_ARTIFICIAL (d) = 1;
3840 DECL_TEMPLATE_PARMS (d) = parm_list;
3842 return t;
3847 /* Push the declarations of builtin types into the namespace.
3848 RID_INDEX is the index of the builtin type in the array
3849 RID_POINTERS. NAME is the name used when looking up the builtin
3850 type. TYPE is the _TYPE node for the builtin type. */
3852 void
3853 record_builtin_type (enum rid rid_index,
3854 const char* name,
3855 tree type)
3857 tree rname = NULL_TREE, tname = NULL_TREE;
3858 tree tdecl = NULL_TREE;
3860 if ((int) rid_index < (int) RID_MAX)
3861 rname = ridpointers[(int) rid_index];
3862 if (name)
3863 tname = get_identifier (name);
3865 /* The calls to SET_IDENTIFIER_GLOBAL_VALUE below should be
3866 eliminated. Built-in types should not be looked up name; their
3867 names are keywords that the parser can recognize. However, there
3868 is code in c-common.c that uses identifier_global_value to look
3869 up built-in types by name. */
3870 if (tname)
3872 tdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, tname, type);
3873 DECL_ARTIFICIAL (tdecl) = 1;
3874 SET_IDENTIFIER_GLOBAL_VALUE (tname, tdecl);
3876 if (rname)
3878 if (!tdecl)
3880 tdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, rname, type);
3881 DECL_ARTIFICIAL (tdecl) = 1;
3883 SET_IDENTIFIER_GLOBAL_VALUE (rname, tdecl);
3886 if (!TYPE_NAME (type))
3887 TYPE_NAME (type) = tdecl;
3889 if (tdecl)
3890 debug_hooks->type_decl (tdecl, 0);
3893 /* Push a type into the namespace so that the back ends ignore it. */
3895 static void
3896 record_unknown_type (tree type, const char* name)
3898 tree decl = pushdecl (build_decl (UNKNOWN_LOCATION,
3899 TYPE_DECL, get_identifier (name), type));
3900 /* Make sure the "unknown type" typedecl gets ignored for debug info. */
3901 DECL_IGNORED_P (decl) = 1;
3902 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
3903 TYPE_SIZE (type) = TYPE_SIZE (void_type_node);
3904 SET_TYPE_ALIGN (type, 1);
3905 TYPE_USER_ALIGN (type) = 0;
3906 SET_TYPE_MODE (type, TYPE_MODE (void_type_node));
3909 /* A string for which we should create an IDENTIFIER_NODE at
3910 startup. */
3912 struct predefined_identifier
3914 /* The name of the identifier. */
3915 const char *const name;
3916 /* The place where the IDENTIFIER_NODE should be stored. */
3917 tree *const node;
3918 /* Nonzero if this is the name of a constructor or destructor. */
3919 const int ctor_or_dtor_p;
3922 /* Create all the predefined identifiers. */
3924 static void
3925 initialize_predefined_identifiers (void)
3927 const predefined_identifier *pid;
3929 /* A table of identifiers to create at startup. */
3930 static const predefined_identifier predefined_identifiers[] = {
3931 { "C++", &lang_name_cplusplus, 0 },
3932 { "C", &lang_name_c, 0 },
3933 /* Some of these names have a trailing space so that it is
3934 impossible for them to conflict with names written by users. */
3935 { "__ct ", &ctor_identifier, 1 },
3936 { "__base_ctor ", &base_ctor_identifier, 1 },
3937 { "__comp_ctor ", &complete_ctor_identifier, 1 },
3938 { "__dt ", &dtor_identifier, 1 },
3939 { "__comp_dtor ", &complete_dtor_identifier, 1 },
3940 { "__base_dtor ", &base_dtor_identifier, 1 },
3941 { "__deleting_dtor ", &deleting_dtor_identifier, 1 },
3942 { IN_CHARGE_NAME, &in_charge_identifier, 0 },
3943 { "nelts", &nelts_identifier, 0 },
3944 { THIS_NAME, &this_identifier, 0 },
3945 { VTABLE_DELTA_NAME, &delta_identifier, 0 },
3946 { VTABLE_PFN_NAME, &pfn_identifier, 0 },
3947 { "_vptr", &vptr_identifier, 0 },
3948 { "__vtt_parm", &vtt_parm_identifier, 0 },
3949 { "::", &global_scope_name, 0 },
3950 { "std", &std_identifier, 0 },
3951 { NULL, NULL, 0 }
3954 for (pid = predefined_identifiers; pid->name; ++pid)
3956 *pid->node = get_identifier (pid->name);
3957 if (pid->ctor_or_dtor_p)
3958 IDENTIFIER_CTOR_OR_DTOR_P (*pid->node) = 1;
3962 /* Create the predefined scalar types of C,
3963 and some nodes representing standard constants (0, 1, (void *)0).
3964 Initialize the global binding level.
3965 Make definitions for built-in primitive functions. */
3967 void
3968 cxx_init_decl_processing (void)
3970 tree void_ftype;
3971 tree void_ftype_ptr;
3973 /* Create all the identifiers we need. */
3974 initialize_predefined_identifiers ();
3976 /* Create the global variables. */
3977 push_to_top_level ();
3979 current_function_decl = NULL_TREE;
3980 current_binding_level = NULL;
3981 /* Enter the global namespace. */
3982 gcc_assert (global_namespace == NULL_TREE);
3983 global_namespace = build_lang_decl (NAMESPACE_DECL, global_scope_name,
3984 void_type_node);
3985 DECL_CONTEXT (global_namespace) = build_translation_unit_decl (NULL_TREE);
3986 debug_hooks->register_main_translation_unit
3987 (DECL_CONTEXT (global_namespace));
3988 TREE_PUBLIC (global_namespace) = 1;
3989 begin_scope (sk_namespace, global_namespace);
3991 if (flag_visibility_ms_compat)
3992 default_visibility = VISIBILITY_HIDDEN;
3994 /* Initially, C. */
3995 current_lang_name = lang_name_c;
3997 /* Create the `std' namespace. */
3998 push_namespace (std_identifier);
3999 std_node = current_namespace;
4000 pop_namespace ();
4002 c_common_nodes_and_builtins ();
4004 integer_two_node = build_int_cst (NULL_TREE, 2);
4006 record_builtin_type (RID_BOOL, "bool", boolean_type_node);
4007 truthvalue_type_node = boolean_type_node;
4008 truthvalue_false_node = boolean_false_node;
4009 truthvalue_true_node = boolean_true_node;
4011 empty_except_spec = build_tree_list (NULL_TREE, NULL_TREE);
4012 noexcept_true_spec = build_tree_list (boolean_true_node, NULL_TREE);
4013 noexcept_false_spec = build_tree_list (boolean_false_node, NULL_TREE);
4015 #if 0
4016 record_builtin_type (RID_MAX, NULL, string_type_node);
4017 #endif
4019 delta_type_node = ptrdiff_type_node;
4020 vtable_index_type = ptrdiff_type_node;
4022 vtt_parm_type = build_pointer_type (const_ptr_type_node);
4023 void_ftype = build_function_type_list (void_type_node, NULL_TREE);
4024 void_ftype_ptr = build_function_type_list (void_type_node,
4025 ptr_type_node, NULL_TREE);
4026 void_ftype_ptr
4027 = build_exception_variant (void_ftype_ptr, empty_except_spec);
4029 /* C++ extensions */
4031 unknown_type_node = make_node (LANG_TYPE);
4032 record_unknown_type (unknown_type_node, "unknown type");
4034 /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node. */
4035 TREE_TYPE (unknown_type_node) = unknown_type_node;
4037 /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same
4038 result. */
4039 TYPE_POINTER_TO (unknown_type_node) = unknown_type_node;
4040 TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node;
4042 init_list_type_node = make_node (LANG_TYPE);
4043 record_unknown_type (init_list_type_node, "init list");
4046 /* Make sure we get a unique function type, so we can give
4047 its pointer type a name. (This wins for gdb.) */
4048 tree vfunc_type = make_node (FUNCTION_TYPE);
4049 TREE_TYPE (vfunc_type) = integer_type_node;
4050 TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
4051 layout_type (vfunc_type);
4053 vtable_entry_type = build_pointer_type (vfunc_type);
4055 record_builtin_type (RID_MAX, VTBL_PTR_TYPE, vtable_entry_type);
4057 vtbl_type_node
4058 = build_cplus_array_type (vtable_entry_type, NULL_TREE);
4059 layout_type (vtbl_type_node);
4060 vtbl_type_node = cp_build_qualified_type (vtbl_type_node, TYPE_QUAL_CONST);
4061 record_builtin_type (RID_MAX, NULL, vtbl_type_node);
4062 vtbl_ptr_type_node = build_pointer_type (vtable_entry_type);
4063 layout_type (vtbl_ptr_type_node);
4064 record_builtin_type (RID_MAX, NULL, vtbl_ptr_type_node);
4066 push_namespace (get_identifier ("__cxxabiv1"));
4067 abi_node = current_namespace;
4068 pop_namespace ();
4070 global_type_node = make_node (LANG_TYPE);
4071 record_unknown_type (global_type_node, "global type");
4073 /* Now, C++. */
4074 current_lang_name = lang_name_cplusplus;
4076 if (aligned_new_threshold > 1
4077 && !pow2p_hwi (aligned_new_threshold))
4079 error ("-faligned-new=%d is not a power of two", aligned_new_threshold);
4080 aligned_new_threshold = 1;
4082 if (aligned_new_threshold == -1)
4083 aligned_new_threshold = (cxx_dialect >= cxx1z) ? 1 : 0;
4084 if (aligned_new_threshold == 1)
4085 aligned_new_threshold = max_align_t_align () / BITS_PER_UNIT;
4088 tree newattrs, extvisattr;
4089 tree newtype, deltype;
4090 tree ptr_ftype_sizetype;
4091 tree new_eh_spec;
4093 ptr_ftype_sizetype
4094 = build_function_type_list (ptr_type_node, size_type_node, NULL_TREE);
4095 if (cxx_dialect == cxx98)
4097 tree bad_alloc_id;
4098 tree bad_alloc_type_node;
4099 tree bad_alloc_decl;
4101 push_namespace (std_identifier);
4102 bad_alloc_id = get_identifier ("bad_alloc");
4103 bad_alloc_type_node = make_class_type (RECORD_TYPE);
4104 TYPE_CONTEXT (bad_alloc_type_node) = current_namespace;
4105 bad_alloc_decl
4106 = create_implicit_typedef (bad_alloc_id, bad_alloc_type_node);
4107 DECL_CONTEXT (bad_alloc_decl) = current_namespace;
4108 pop_namespace ();
4110 new_eh_spec
4111 = add_exception_specifier (NULL_TREE, bad_alloc_type_node, -1);
4113 else
4114 new_eh_spec = noexcept_false_spec;
4116 /* Ensure attribs.c is initialized. */
4117 init_attributes ();
4119 /* Ensure constraint.cc is initialized. */
4120 init_constraint_processing ();
4122 extvisattr = build_tree_list (get_identifier ("externally_visible"),
4123 NULL_TREE);
4124 newattrs = tree_cons (get_identifier ("alloc_size"),
4125 build_tree_list (NULL_TREE, integer_one_node),
4126 extvisattr);
4127 newtype = cp_build_type_attribute_variant (ptr_ftype_sizetype, newattrs);
4128 newtype = build_exception_variant (newtype, new_eh_spec);
4129 deltype = cp_build_type_attribute_variant (void_ftype_ptr, extvisattr);
4130 deltype = build_exception_variant (deltype, empty_except_spec);
4131 tree opnew = push_cp_library_fn (NEW_EXPR, newtype, 0);
4132 DECL_IS_MALLOC (opnew) = 1;
4133 DECL_IS_OPERATOR_NEW (opnew) = 1;
4134 opnew = push_cp_library_fn (VEC_NEW_EXPR, newtype, 0);
4135 DECL_IS_MALLOC (opnew) = 1;
4136 DECL_IS_OPERATOR_NEW (opnew) = 1;
4137 push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
4138 push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
4139 if (flag_sized_deallocation)
4141 /* Also push the sized deallocation variants:
4142 void operator delete(void*, std::size_t) throw();
4143 void operator delete[](void*, std::size_t) throw(); */
4144 tree void_ftype_ptr_size
4145 = build_function_type_list (void_type_node, ptr_type_node,
4146 size_type_node, NULL_TREE);
4147 deltype = cp_build_type_attribute_variant (void_ftype_ptr_size,
4148 extvisattr);
4149 deltype = build_exception_variant (deltype, empty_except_spec);
4150 push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
4151 push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
4154 if (aligned_new_threshold)
4156 push_namespace (std_identifier);
4157 tree align_id = get_identifier ("align_val_t");
4158 align_type_node = start_enum (align_id, NULL_TREE, size_type_node,
4159 NULL_TREE, /*scoped*/true, NULL);
4160 pop_namespace ();
4162 /* operator new (size_t, align_val_t); */
4163 newtype = build_function_type_list (ptr_type_node, size_type_node,
4164 align_type_node, NULL_TREE);
4165 newtype = cp_build_type_attribute_variant (newtype, newattrs);
4166 newtype = build_exception_variant (newtype, new_eh_spec);
4167 opnew = push_cp_library_fn (NEW_EXPR, newtype, 0);
4168 DECL_IS_MALLOC (opnew) = 1;
4169 DECL_IS_OPERATOR_NEW (opnew) = 1;
4170 opnew = push_cp_library_fn (VEC_NEW_EXPR, newtype, 0);
4171 DECL_IS_MALLOC (opnew) = 1;
4172 DECL_IS_OPERATOR_NEW (opnew) = 1;
4174 /* operator delete (void *, align_val_t); */
4175 deltype = build_function_type_list (void_type_node, ptr_type_node,
4176 align_type_node, NULL_TREE);
4177 deltype = cp_build_type_attribute_variant (deltype, extvisattr);
4178 deltype = build_exception_variant (deltype, empty_except_spec);
4179 push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
4180 push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
4182 if (flag_sized_deallocation)
4184 /* operator delete (void *, size_t, align_val_t); */
4185 deltype = build_function_type_list (void_type_node, ptr_type_node,
4186 size_type_node, align_type_node,
4187 NULL_TREE);
4188 deltype = cp_build_type_attribute_variant (deltype, extvisattr);
4189 deltype = build_exception_variant (deltype, empty_except_spec);
4190 push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
4191 push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
4195 nullptr_type_node = make_node (NULLPTR_TYPE);
4196 TYPE_SIZE (nullptr_type_node) = bitsize_int (GET_MODE_BITSIZE (ptr_mode));
4197 TYPE_SIZE_UNIT (nullptr_type_node) = size_int (GET_MODE_SIZE (ptr_mode));
4198 TYPE_UNSIGNED (nullptr_type_node) = 1;
4199 TYPE_PRECISION (nullptr_type_node) = GET_MODE_BITSIZE (ptr_mode);
4200 if (abi_version_at_least (9))
4201 SET_TYPE_ALIGN (nullptr_type_node, GET_MODE_ALIGNMENT (ptr_mode));
4202 SET_TYPE_MODE (nullptr_type_node, ptr_mode);
4203 record_builtin_type (RID_MAX, "decltype(nullptr)", nullptr_type_node);
4204 nullptr_node = build_int_cst (nullptr_type_node, 0);
4207 abort_fndecl
4208 = build_library_fn_ptr ("__cxa_pure_virtual", void_ftype,
4209 ECF_NORETURN | ECF_NOTHROW);
4211 /* Perform other language dependent initializations. */
4212 init_class_processing ();
4213 init_rtti_processing ();
4214 init_template_processing ();
4216 if (flag_exceptions)
4217 init_exception_processing ();
4219 if (! supports_one_only ())
4220 flag_weak = 0;
4222 make_fname_decl = cp_make_fname_decl;
4223 start_fname_decls ();
4225 /* Show we use EH for cleanups. */
4226 if (flag_exceptions)
4227 using_eh_for_cleanups ();
4230 /* Generate an initializer for a function naming variable from
4231 NAME. NAME may be NULL, to indicate a dependent name. TYPE_P is
4232 filled in with the type of the init. */
4234 tree
4235 cp_fname_init (const char* name, tree *type_p)
4237 tree domain = NULL_TREE;
4238 tree type;
4239 tree init = NULL_TREE;
4240 size_t length = 0;
4242 if (name)
4244 length = strlen (name);
4245 domain = build_index_type (size_int (length));
4246 init = build_string (length + 1, name);
4249 type = cp_build_qualified_type (char_type_node, TYPE_QUAL_CONST);
4250 type = build_cplus_array_type (type, domain);
4252 *type_p = type;
4254 if (init)
4255 TREE_TYPE (init) = type;
4256 else
4257 init = error_mark_node;
4259 return init;
4262 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give
4263 the decl, LOC is the location to give the decl, NAME is the
4264 initialization string and TYPE_DEP indicates whether NAME depended
4265 on the type of the function. We make use of that to detect
4266 __PRETTY_FUNCTION__ inside a template fn. This is being done lazily
4267 at the point of first use, so we mustn't push the decl now. */
4269 static tree
4270 cp_make_fname_decl (location_t loc, tree id, int type_dep)
4272 const char *const name = (type_dep && processing_template_decl
4273 ? NULL : fname_as_string (type_dep));
4274 tree type;
4275 tree init = cp_fname_init (name, &type);
4276 tree decl = build_decl (loc, VAR_DECL, id, type);
4278 if (name)
4279 free (CONST_CAST (char *, name));
4281 /* As we're using pushdecl_with_scope, we must set the context. */
4282 DECL_CONTEXT (decl) = current_function_decl;
4284 TREE_STATIC (decl) = 1;
4285 TREE_READONLY (decl) = 1;
4286 DECL_ARTIFICIAL (decl) = 1;
4288 TREE_USED (decl) = 1;
4290 if (current_function_decl)
4292 cp_binding_level *b = current_binding_level;
4293 if (b->kind == sk_function_parms)
4294 return error_mark_node;
4295 while (b->level_chain->kind != sk_function_parms)
4296 b = b->level_chain;
4297 pushdecl_with_scope (decl, b, /*is_friend=*/false);
4298 cp_finish_decl (decl, init, /*init_const_expr_p=*/false, NULL_TREE,
4299 LOOKUP_ONLYCONVERTING);
4301 else
4303 DECL_THIS_STATIC (decl) = true;
4304 pushdecl_top_level_and_finish (decl, init);
4307 return decl;
4310 static tree
4311 builtin_function_1 (tree decl, tree context, bool is_global)
4313 tree id = DECL_NAME (decl);
4314 const char *name = IDENTIFIER_POINTER (id);
4316 retrofit_lang_decl (decl);
4318 DECL_ARTIFICIAL (decl) = 1;
4319 SET_OVERLOADED_OPERATOR_CODE (decl, ERROR_MARK);
4320 SET_DECL_LANGUAGE (decl, lang_c);
4321 /* Runtime library routines are, by definition, available in an
4322 external shared object. */
4323 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
4324 DECL_VISIBILITY_SPECIFIED (decl) = 1;
4326 DECL_CONTEXT (decl) = context;
4328 if (is_global)
4329 pushdecl_top_level (decl);
4330 else
4331 pushdecl (decl);
4333 /* A function in the user's namespace should have an explicit
4334 declaration before it is used. Mark the built-in function as
4335 anticipated but not actually declared. */
4336 if (name[0] != '_' || name[1] != '_')
4337 DECL_ANTICIPATED (decl) = 1;
4338 else if (strncmp (name + 2, "builtin_", strlen ("builtin_")) != 0)
4340 size_t len = strlen (name);
4342 /* Treat __*_chk fortification functions as anticipated as well,
4343 unless they are __builtin_*. */
4344 if (len > strlen ("___chk")
4345 && memcmp (name + len - strlen ("_chk"),
4346 "_chk", strlen ("_chk") + 1) == 0)
4347 DECL_ANTICIPATED (decl) = 1;
4350 return decl;
4353 tree
4354 cxx_builtin_function (tree decl)
4356 tree id = DECL_NAME (decl);
4357 const char *name = IDENTIFIER_POINTER (id);
4358 /* All builtins that don't begin with an '_' should additionally
4359 go in the 'std' namespace. */
4360 if (name[0] != '_')
4362 tree decl2 = copy_node(decl);
4363 push_namespace (std_identifier);
4364 builtin_function_1 (decl2, std_node, false);
4365 pop_namespace ();
4368 return builtin_function_1 (decl, NULL_TREE, false);
4371 /* Like cxx_builtin_function, but guarantee the function is added to the global
4372 scope. This is to allow function specific options to add new machine
4373 dependent builtins when the target ISA changes via attribute((target(...)))
4374 which saves space on program startup if the program does not use non-generic
4375 ISAs. */
4377 tree
4378 cxx_builtin_function_ext_scope (tree decl)
4381 tree id = DECL_NAME (decl);
4382 const char *name = IDENTIFIER_POINTER (id);
4383 /* All builtins that don't begin with an '_' should additionally
4384 go in the 'std' namespace. */
4385 if (name[0] != '_')
4387 tree decl2 = copy_node(decl);
4388 push_namespace (std_identifier);
4389 builtin_function_1 (decl2, std_node, true);
4390 pop_namespace ();
4393 return builtin_function_1 (decl, NULL_TREE, true);
4396 /* Generate a FUNCTION_DECL with the typical flags for a runtime library
4397 function. Not called directly. */
4399 static tree
4400 build_library_fn (tree name, enum tree_code operator_code, tree type,
4401 int ecf_flags)
4403 tree fn = build_lang_decl (FUNCTION_DECL, name, type);
4404 DECL_EXTERNAL (fn) = 1;
4405 TREE_PUBLIC (fn) = 1;
4406 DECL_ARTIFICIAL (fn) = 1;
4407 SET_OVERLOADED_OPERATOR_CODE (fn, operator_code);
4408 SET_DECL_LANGUAGE (fn, lang_c);
4409 /* Runtime library routines are, by definition, available in an
4410 external shared object. */
4411 DECL_VISIBILITY (fn) = VISIBILITY_DEFAULT;
4412 DECL_VISIBILITY_SPECIFIED (fn) = 1;
4413 set_call_expr_flags (fn, ecf_flags);
4414 return fn;
4417 /* Returns the _DECL for a library function with C++ linkage. */
4419 static tree
4420 build_cp_library_fn (tree name, enum tree_code operator_code, tree type,
4421 int ecf_flags)
4423 tree fn = build_library_fn (name, operator_code, type, ecf_flags);
4424 DECL_CONTEXT (fn) = FROB_CONTEXT (current_namespace);
4425 SET_DECL_LANGUAGE (fn, lang_cplusplus);
4426 return fn;
4429 /* Like build_library_fn, but takes a C string instead of an
4430 IDENTIFIER_NODE. */
4432 tree
4433 build_library_fn_ptr (const char* name, tree type, int ecf_flags)
4435 return build_library_fn (get_identifier (name), ERROR_MARK, type, ecf_flags);
4438 /* Like build_cp_library_fn, but takes a C string instead of an
4439 IDENTIFIER_NODE. */
4441 tree
4442 build_cp_library_fn_ptr (const char* name, tree type, int ecf_flags)
4444 return build_cp_library_fn (get_identifier (name), ERROR_MARK, type,
4445 ecf_flags);
4448 /* Like build_library_fn, but also pushes the function so that we will
4449 be able to find it via IDENTIFIER_GLOBAL_VALUE. Also, the function
4450 may throw exceptions listed in RAISES. */
4452 tree
4453 push_library_fn (tree name, tree type, tree raises, int ecf_flags)
4455 tree fn;
4457 if (raises)
4458 type = build_exception_variant (type, raises);
4460 fn = build_library_fn (name, ERROR_MARK, type, ecf_flags);
4461 pushdecl_top_level (fn);
4462 return fn;
4465 /* Like build_cp_library_fn, but also pushes the function so that it
4466 will be found by normal lookup. */
4468 static tree
4469 push_cp_library_fn (enum tree_code operator_code, tree type,
4470 int ecf_flags)
4472 tree fn = build_cp_library_fn (ansi_opname (operator_code),
4473 operator_code,
4474 type, ecf_flags);
4475 pushdecl (fn);
4476 if (flag_tm)
4477 apply_tm_attr (fn, get_identifier ("transaction_safe"));
4478 return fn;
4481 /* Like push_library_fn, but takes a TREE_LIST of parm types rather than
4482 a FUNCTION_TYPE. */
4484 tree
4485 push_void_library_fn (tree name, tree parmtypes, int ecf_flags)
4487 tree type = build_function_type (void_type_node, parmtypes);
4488 return push_library_fn (name, type, NULL_TREE, ecf_flags);
4491 /* Like push_library_fn, but also note that this function throws
4492 and does not return. Used for __throw_foo and the like. */
4494 tree
4495 push_throw_library_fn (tree name, tree type)
4497 tree fn = push_library_fn (name, type, NULL_TREE, ECF_NORETURN);
4498 return fn;
4501 /* When we call finish_struct for an anonymous union, we create
4502 default copy constructors and such. But, an anonymous union
4503 shouldn't have such things; this function undoes the damage to the
4504 anonymous union type T.
4506 (The reason that we create the synthesized methods is that we don't
4507 distinguish `union { int i; }' from `typedef union { int i; } U'.
4508 The first is an anonymous union; the second is just an ordinary
4509 union type.) */
4511 void
4512 fixup_anonymous_aggr (tree t)
4514 tree *q;
4516 /* Wipe out memory of synthesized methods. */
4517 TYPE_HAS_USER_CONSTRUCTOR (t) = 0;
4518 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0;
4519 TYPE_HAS_COPY_CTOR (t) = 0;
4520 TYPE_HAS_CONST_COPY_CTOR (t) = 0;
4521 TYPE_HAS_COPY_ASSIGN (t) = 0;
4522 TYPE_HAS_CONST_COPY_ASSIGN (t) = 0;
4524 /* Splice the implicitly generated functions out of the TYPE_METHODS
4525 list. */
4526 q = &TYPE_METHODS (t);
4527 while (*q)
4529 if (DECL_ARTIFICIAL (*q))
4530 *q = TREE_CHAIN (*q);
4531 else
4532 q = &DECL_CHAIN (*q);
4535 /* ISO C++ 9.5.3. Anonymous unions may not have function members. */
4536 if (TYPE_METHODS (t))
4538 tree decl = TYPE_MAIN_DECL (t);
4540 if (TREE_CODE (t) != UNION_TYPE)
4541 error_at (DECL_SOURCE_LOCATION (decl),
4542 "an anonymous struct cannot have function members");
4543 else
4544 error_at (DECL_SOURCE_LOCATION (decl),
4545 "an anonymous union cannot have function members");
4548 /* Anonymous aggregates cannot have fields with ctors, dtors or complex
4549 assignment operators (because they cannot have these methods themselves).
4550 For anonymous unions this is already checked because they are not allowed
4551 in any union, otherwise we have to check it. */
4552 if (TREE_CODE (t) != UNION_TYPE)
4554 tree field, type;
4556 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
4557 if (TREE_CODE (field) == FIELD_DECL)
4559 type = TREE_TYPE (field);
4560 if (CLASS_TYPE_P (type))
4562 if (TYPE_NEEDS_CONSTRUCTING (type))
4563 error ("member %q+#D with constructor not allowed "
4564 "in anonymous aggregate", field);
4565 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
4566 error ("member %q+#D with destructor not allowed "
4567 "in anonymous aggregate", field);
4568 if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type))
4569 error ("member %q+#D with copy assignment operator "
4570 "not allowed in anonymous aggregate", field);
4576 /* Warn for an attribute located at LOCATION that appertains to the
4577 class type CLASS_TYPE that has not been properly placed after its
4578 class-key, in it class-specifier. */
4580 void
4581 warn_misplaced_attr_for_class_type (source_location location,
4582 tree class_type)
4584 gcc_assert (OVERLOAD_TYPE_P (class_type));
4586 if (warning_at (location, OPT_Wattributes,
4587 "attribute ignored in declaration "
4588 "of %q#T", class_type))
4589 inform (location,
4590 "attribute for %q#T must follow the %qs keyword",
4591 class_type, class_key_or_enum_as_string (class_type));
4594 /* Make sure that a declaration with no declarator is well-formed, i.e.
4595 just declares a tagged type or anonymous union.
4597 Returns the type declared; or NULL_TREE if none. */
4599 tree
4600 check_tag_decl (cp_decl_specifier_seq *declspecs,
4601 bool explicit_type_instantiation_p)
4603 int saw_friend = decl_spec_seq_has_spec_p (declspecs, ds_friend);
4604 int saw_typedef = decl_spec_seq_has_spec_p (declspecs, ds_typedef);
4605 /* If a class, struct, or enum type is declared by the DECLSPECS
4606 (i.e, if a class-specifier, enum-specifier, or non-typename
4607 elaborated-type-specifier appears in the DECLSPECS),
4608 DECLARED_TYPE is set to the corresponding type. */
4609 tree declared_type = NULL_TREE;
4610 bool error_p = false;
4612 if (declspecs->multiple_types_p)
4613 error ("multiple types in one declaration");
4614 else if (declspecs->redefined_builtin_type)
4616 if (!in_system_header_at (input_location))
4617 permerror (declspecs->locations[ds_redefined_builtin_type_spec],
4618 "redeclaration of C++ built-in type %qT",
4619 declspecs->redefined_builtin_type);
4620 return NULL_TREE;
4623 if (declspecs->type
4624 && TYPE_P (declspecs->type)
4625 && ((TREE_CODE (declspecs->type) != TYPENAME_TYPE
4626 && MAYBE_CLASS_TYPE_P (declspecs->type))
4627 || TREE_CODE (declspecs->type) == ENUMERAL_TYPE))
4628 declared_type = declspecs->type;
4629 else if (declspecs->type == error_mark_node)
4630 error_p = true;
4631 if (declared_type == NULL_TREE && ! saw_friend && !error_p)
4632 permerror (input_location, "declaration does not declare anything");
4633 else if (declared_type != NULL_TREE && type_uses_auto (declared_type))
4635 error_at (declspecs->locations[ds_type_spec],
4636 "%<auto%> can only be specified for variables "
4637 "or function declarations");
4638 return error_mark_node;
4640 /* Check for an anonymous union. */
4641 else if (declared_type && RECORD_OR_UNION_CODE_P (TREE_CODE (declared_type))
4642 && TYPE_UNNAMED_P (declared_type))
4644 /* 7/3 In a simple-declaration, the optional init-declarator-list
4645 can be omitted only when declaring a class (clause 9) or
4646 enumeration (7.2), that is, when the decl-specifier-seq contains
4647 either a class-specifier, an elaborated-type-specifier with
4648 a class-key (9.1), or an enum-specifier. In these cases and
4649 whenever a class-specifier or enum-specifier is present in the
4650 decl-specifier-seq, the identifiers in these specifiers are among
4651 the names being declared by the declaration (as class-name,
4652 enum-names, or enumerators, depending on the syntax). In such
4653 cases, and except for the declaration of an unnamed bit-field (9.6),
4654 the decl-specifier-seq shall introduce one or more names into the
4655 program, or shall redeclare a name introduced by a previous
4656 declaration. [Example:
4657 enum { }; // ill-formed
4658 typedef class { }; // ill-formed
4659 --end example] */
4660 if (saw_typedef)
4662 error ("missing type-name in typedef-declaration");
4663 return NULL_TREE;
4665 /* Anonymous unions are objects, so they can have specifiers. */;
4666 SET_ANON_AGGR_TYPE_P (declared_type);
4668 if (TREE_CODE (declared_type) != UNION_TYPE
4669 && !in_system_header_at (input_location))
4670 pedwarn (input_location, OPT_Wpedantic, "ISO C++ prohibits anonymous structs");
4673 else
4675 if (decl_spec_seq_has_spec_p (declspecs, ds_inline))
4676 error_at (declspecs->locations[ds_inline],
4677 "%<inline%> can only be specified for functions");
4678 else if (decl_spec_seq_has_spec_p (declspecs, ds_virtual))
4679 error_at (declspecs->locations[ds_virtual],
4680 "%<virtual%> can only be specified for functions");
4681 else if (saw_friend
4682 && (!current_class_type
4683 || current_scope () != current_class_type))
4684 error_at (declspecs->locations[ds_friend],
4685 "%<friend%> can only be specified inside a class");
4686 else if (decl_spec_seq_has_spec_p (declspecs, ds_explicit))
4687 error_at (declspecs->locations[ds_explicit],
4688 "%<explicit%> can only be specified for constructors");
4689 else if (declspecs->storage_class)
4690 error_at (declspecs->locations[ds_storage_class],
4691 "a storage class can only be specified for objects "
4692 "and functions");
4693 else if (decl_spec_seq_has_spec_p (declspecs, ds_const))
4694 error_at (declspecs->locations[ds_const],
4695 "%<const%> can only be specified for objects and "
4696 "functions");
4697 else if (decl_spec_seq_has_spec_p (declspecs, ds_volatile))
4698 error_at (declspecs->locations[ds_volatile],
4699 "%<volatile%> can only be specified for objects and "
4700 "functions");
4701 else if (decl_spec_seq_has_spec_p (declspecs, ds_restrict))
4702 error_at (declspecs->locations[ds_restrict],
4703 "%<__restrict%> can only be specified for objects and "
4704 "functions");
4705 else if (decl_spec_seq_has_spec_p (declspecs, ds_thread))
4706 error_at (declspecs->locations[ds_thread],
4707 "%<__thread%> can only be specified for objects "
4708 "and functions");
4709 else if (saw_typedef)
4710 warning_at (declspecs->locations[ds_typedef], 0,
4711 "%<typedef%> was ignored in this declaration");
4712 else if (decl_spec_seq_has_spec_p (declspecs, ds_constexpr))
4713 error_at (declspecs->locations[ds_constexpr],
4714 "%<constexpr%> cannot be used for type declarations");
4717 if (declspecs->attributes && warn_attributes && declared_type)
4719 location_t loc;
4720 if (!CLASS_TYPE_P (declared_type)
4721 || !CLASSTYPE_TEMPLATE_INSTANTIATION (declared_type))
4722 /* For a non-template class, use the name location. */
4723 loc = location_of (declared_type);
4724 else
4725 /* For a template class (an explicit instantiation), use the
4726 current location. */
4727 loc = input_location;
4729 if (explicit_type_instantiation_p)
4730 /* [dcl.attr.grammar]/4:
4732 No attribute-specifier-seq shall appertain to an explicit
4733 instantiation. */
4735 if (warning_at (loc, OPT_Wattributes,
4736 "attribute ignored in explicit instantiation %q#T",
4737 declared_type))
4738 inform (loc,
4739 "no attribute can be applied to "
4740 "an explicit instantiation");
4742 else
4743 warn_misplaced_attr_for_class_type (loc, declared_type);
4746 return declared_type;
4749 /* Called when a declaration is seen that contains no names to declare.
4750 If its type is a reference to a structure, union or enum inherited
4751 from a containing scope, shadow that tag name for the current scope
4752 with a forward reference.
4753 If its type defines a new named structure or union
4754 or defines an enum, it is valid but we need not do anything here.
4755 Otherwise, it is an error.
4757 C++: may have to grok the declspecs to learn about static,
4758 complain for anonymous unions.
4760 Returns the TYPE declared -- or NULL_TREE if none. */
4762 tree
4763 shadow_tag (cp_decl_specifier_seq *declspecs)
4765 tree t = check_tag_decl (declspecs,
4766 /*explicit_type_instantiation_p=*/false);
4768 if (!t)
4769 return NULL_TREE;
4771 if (maybe_process_partial_specialization (t) == error_mark_node)
4772 return NULL_TREE;
4774 /* This is where the variables in an anonymous union are
4775 declared. An anonymous union declaration looks like:
4776 union { ... } ;
4777 because there is no declarator after the union, the parser
4778 sends that declaration here. */
4779 if (ANON_AGGR_TYPE_P (t))
4781 fixup_anonymous_aggr (t);
4783 if (TYPE_FIELDS (t))
4785 tree decl = grokdeclarator (/*declarator=*/NULL,
4786 declspecs, NORMAL, 0, NULL);
4787 finish_anon_union (decl);
4791 return t;
4794 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
4796 tree
4797 groktypename (cp_decl_specifier_seq *type_specifiers,
4798 const cp_declarator *declarator,
4799 bool is_template_arg)
4801 tree attrs;
4802 tree type;
4803 enum decl_context context
4804 = is_template_arg ? TEMPLATE_TYPE_ARG : TYPENAME;
4805 attrs = type_specifiers->attributes;
4806 type_specifiers->attributes = NULL_TREE;
4807 type = grokdeclarator (declarator, type_specifiers, context, 0, &attrs);
4808 if (attrs && type != error_mark_node)
4810 if (CLASS_TYPE_P (type))
4811 warning (OPT_Wattributes, "ignoring attributes applied to class type %qT "
4812 "outside of definition", type);
4813 else if (MAYBE_CLASS_TYPE_P (type))
4814 /* A template type parameter or other dependent type. */
4815 warning (OPT_Wattributes, "ignoring attributes applied to dependent "
4816 "type %qT without an associated declaration", type);
4817 else
4818 cplus_decl_attributes (&type, attrs, 0);
4820 return type;
4823 /* Process a DECLARATOR for a function-scope variable declaration,
4824 namespace-scope variable declaration, or function declaration.
4825 (Function definitions go through start_function; class member
4826 declarations appearing in the body of the class go through
4827 grokfield.) The DECL corresponding to the DECLARATOR is returned.
4828 If an error occurs, the error_mark_node is returned instead.
4830 DECLSPECS are the decl-specifiers for the declaration. INITIALIZED is
4831 SD_INITIALIZED if an explicit initializer is present, or SD_DEFAULTED
4832 for an explicitly defaulted function, or SD_DELETED for an explicitly
4833 deleted function, but 0 (SD_UNINITIALIZED) if this is a variable
4834 implicitly initialized via a default constructor. ATTRIBUTES and
4835 PREFIX_ATTRIBUTES are GNU attributes associated with this declaration.
4837 The scope represented by the context of the returned DECL is pushed
4838 (if it is not the global namespace) and is assigned to
4839 *PUSHED_SCOPE_P. The caller is then responsible for calling
4840 pop_scope on *PUSHED_SCOPE_P if it is set. */
4842 tree
4843 start_decl (const cp_declarator *declarator,
4844 cp_decl_specifier_seq *declspecs,
4845 int initialized,
4846 tree attributes,
4847 tree prefix_attributes,
4848 tree *pushed_scope_p)
4850 tree decl;
4851 tree context;
4852 bool was_public;
4853 int flags;
4854 bool alias;
4856 *pushed_scope_p = NULL_TREE;
4858 /* An object declared as __attribute__((deprecated)) suppresses
4859 warnings of uses of other deprecated items. */
4860 if (lookup_attribute ("deprecated", attributes))
4861 deprecated_state = DEPRECATED_SUPPRESS;
4863 attributes = chainon (attributes, prefix_attributes);
4865 decl = grokdeclarator (declarator, declspecs, NORMAL, initialized,
4866 &attributes);
4868 deprecated_state = DEPRECATED_NORMAL;
4870 if (decl == NULL_TREE || VOID_TYPE_P (decl)
4871 || decl == error_mark_node)
4872 return error_mark_node;
4874 context = CP_DECL_CONTEXT (decl);
4875 if (context != global_namespace)
4876 *pushed_scope_p = push_scope (context);
4878 /* Is it valid for this decl to have an initializer at all?
4879 If not, set INITIALIZED to zero, which will indirectly
4880 tell `cp_finish_decl' to ignore the initializer once it is parsed. */
4881 if (initialized
4882 && TREE_CODE (decl) == TYPE_DECL)
4884 error ("typedef %qD is initialized (use decltype instead)", decl);
4885 return error_mark_node;
4888 if (initialized)
4890 if (! toplevel_bindings_p ()
4891 && DECL_EXTERNAL (decl))
4892 warning (0, "declaration of %q#D has %<extern%> and is initialized",
4893 decl);
4894 DECL_EXTERNAL (decl) = 0;
4895 if (toplevel_bindings_p ())
4896 TREE_STATIC (decl) = 1;
4898 alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl)) != 0;
4900 if (alias && TREE_CODE (decl) == FUNCTION_DECL)
4901 record_key_method_defined (decl);
4903 /* If this is a typedef that names the class for linkage purposes
4904 (7.1.3p8), apply any attributes directly to the type. */
4905 if (TREE_CODE (decl) == TYPE_DECL
4906 && OVERLOAD_TYPE_P (TREE_TYPE (decl))
4907 && decl == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (decl))))
4908 flags = ATTR_FLAG_TYPE_IN_PLACE;
4909 else
4910 flags = 0;
4912 /* Set attributes here so if duplicate decl, will have proper attributes. */
4913 cplus_decl_attributes (&decl, attributes, flags);
4915 /* Dllimported symbols cannot be defined. Static data members (which
4916 can be initialized in-class and dllimported) go through grokfield,
4917 not here, so we don't need to exclude those decls when checking for
4918 a definition. */
4919 if (initialized && DECL_DLLIMPORT_P (decl))
4921 error ("definition of %q#D is marked %<dllimport%>", decl);
4922 DECL_DLLIMPORT_P (decl) = 0;
4925 /* If #pragma weak was used, mark the decl weak now. */
4926 if (!processing_template_decl)
4927 maybe_apply_pragma_weak (decl);
4929 if (TREE_CODE (decl) == FUNCTION_DECL
4930 && DECL_DECLARED_INLINE_P (decl)
4931 && DECL_UNINLINABLE (decl)
4932 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
4933 warning_at (DECL_SOURCE_LOCATION (decl), 0,
4934 "inline function %qD given attribute noinline", decl);
4936 if (TYPE_P (context) && COMPLETE_TYPE_P (complete_type (context)))
4938 bool this_tmpl = (processing_template_decl
4939 > template_class_depth (context));
4940 if (VAR_P (decl))
4942 tree field = lookup_field (context, DECL_NAME (decl), 0, false);
4943 if (field == NULL_TREE
4944 || !(VAR_P (field) || variable_template_p (field)))
4945 error ("%q+#D is not a static data member of %q#T", decl, context);
4946 else if (variable_template_p (field) && !this_tmpl)
4948 if (DECL_LANG_SPECIFIC (decl)
4949 && DECL_TEMPLATE_SPECIALIZATION (decl))
4950 /* OK, specialization was already checked. */;
4951 else
4953 error_at (DECL_SOURCE_LOCATION (decl),
4954 "non-member-template declaration of %qD", decl);
4955 inform (DECL_SOURCE_LOCATION (field), "does not match "
4956 "member template declaration here");
4957 return error_mark_node;
4960 else
4962 if (variable_template_p (field))
4963 field = DECL_TEMPLATE_RESULT (field);
4965 if (DECL_CONTEXT (field) != context)
4967 if (!same_type_p (DECL_CONTEXT (field), context))
4968 permerror (input_location, "ISO C++ does not permit %<%T::%D%> "
4969 "to be defined as %<%T::%D%>",
4970 DECL_CONTEXT (field), DECL_NAME (decl),
4971 context, DECL_NAME (decl));
4972 DECL_CONTEXT (decl) = DECL_CONTEXT (field);
4974 /* Static data member are tricky; an in-class initialization
4975 still doesn't provide a definition, so the in-class
4976 declaration will have DECL_EXTERNAL set, but will have an
4977 initialization. Thus, duplicate_decls won't warn
4978 about this situation, and so we check here. */
4979 if (initialized && DECL_INITIALIZED_IN_CLASS_P (field))
4980 error ("duplicate initialization of %qD", decl);
4981 if (duplicate_decls (decl, field, /*newdecl_is_friend=*/false))
4982 decl = field;
4983 if (decl_spec_seq_has_spec_p (declspecs, ds_constexpr)
4984 && !DECL_DECLARED_CONSTEXPR_P (field))
4985 error ("%qD declared %<constexpr%> outside its class", field);
4988 else
4990 tree field = check_classfn (context, decl,
4991 this_tmpl
4992 ? current_template_parms
4993 : NULL_TREE);
4994 if (field && field != error_mark_node
4995 && duplicate_decls (decl, field,
4996 /*newdecl_is_friend=*/false))
4997 decl = field;
5000 /* cp_finish_decl sets DECL_EXTERNAL if DECL_IN_AGGR_P is set. */
5001 DECL_IN_AGGR_P (decl) = 0;
5002 /* Do not mark DECL as an explicit specialization if it was not
5003 already marked as an instantiation; a declaration should
5004 never be marked as a specialization unless we know what
5005 template is being specialized. */
5006 if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
5008 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
5009 if (TREE_CODE (decl) == FUNCTION_DECL)
5010 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
5011 && DECL_DECLARED_INLINE_P (decl));
5012 else
5013 DECL_COMDAT (decl) = false;
5015 /* [temp.expl.spec] An explicit specialization of a static data
5016 member of a template is a definition if the declaration
5017 includes an initializer; otherwise, it is a declaration.
5019 We check for processing_specialization so this only applies
5020 to the new specialization syntax. */
5021 if (!initialized && processing_specialization)
5022 DECL_EXTERNAL (decl) = 1;
5025 if (DECL_EXTERNAL (decl) && ! DECL_TEMPLATE_SPECIALIZATION (decl)
5026 /* Aliases are definitions. */
5027 && !alias)
5028 permerror (input_location, "declaration of %q#D outside of class is not definition",
5029 decl);
5032 was_public = TREE_PUBLIC (decl);
5034 /* Enter this declaration into the symbol table. Don't push the plain
5035 VAR_DECL for a variable template. */
5036 if (!template_parm_scope_p ()
5037 || !VAR_P (decl))
5038 decl = maybe_push_decl (decl);
5040 if (processing_template_decl)
5041 decl = push_template_decl (decl);
5042 if (decl == error_mark_node)
5043 return error_mark_node;
5045 if (VAR_P (decl)
5046 && DECL_NAMESPACE_SCOPE_P (decl) && !TREE_PUBLIC (decl) && !was_public
5047 && !DECL_THIS_STATIC (decl) && !DECL_ARTIFICIAL (decl))
5049 /* This is a const variable with implicit 'static'. Set
5050 DECL_THIS_STATIC so we can tell it from variables that are
5051 !TREE_PUBLIC because of the anonymous namespace. */
5052 gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (decl)) || errorcount);
5053 DECL_THIS_STATIC (decl) = 1;
5056 if (current_function_decl && VAR_P (decl)
5057 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
5059 bool ok = false;
5060 if (CP_DECL_THREAD_LOCAL_P (decl))
5061 error ("%qD declared %<thread_local%> in %<constexpr%> function",
5062 decl);
5063 else if (TREE_STATIC (decl))
5064 error ("%qD declared %<static%> in %<constexpr%> function", decl);
5065 else
5066 ok = true;
5067 if (!ok)
5068 cp_function_chain->invalid_constexpr = true;
5071 if (!processing_template_decl && VAR_P (decl))
5072 start_decl_1 (decl, initialized);
5074 return decl;
5077 /* Process the declaration of a variable DECL. INITIALIZED is true
5078 iff DECL is explicitly initialized. (INITIALIZED is false if the
5079 variable is initialized via an implicitly-called constructor.)
5080 This function must be called for ordinary variables (including, for
5081 example, implicit instantiations of templates), but must not be
5082 called for template declarations. */
5084 void
5085 start_decl_1 (tree decl, bool initialized)
5087 tree type;
5088 bool complete_p;
5089 bool aggregate_definition_p;
5091 gcc_assert (!processing_template_decl);
5093 if (error_operand_p (decl))
5094 return;
5096 gcc_assert (VAR_P (decl));
5098 type = TREE_TYPE (decl);
5099 complete_p = COMPLETE_TYPE_P (type);
5100 aggregate_definition_p = MAYBE_CLASS_TYPE_P (type) && !DECL_EXTERNAL (decl);
5102 /* If an explicit initializer is present, or if this is a definition
5103 of an aggregate, then we need a complete type at this point.
5104 (Scalars are always complete types, so there is nothing to
5105 check.) This code just sets COMPLETE_P; errors (if necessary)
5106 are issued below. */
5107 if ((initialized || aggregate_definition_p)
5108 && !complete_p
5109 && COMPLETE_TYPE_P (complete_type (type)))
5111 complete_p = true;
5112 /* We will not yet have set TREE_READONLY on DECL if the type
5113 was "const", but incomplete, before this point. But, now, we
5114 have a complete type, so we can try again. */
5115 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
5118 if (initialized)
5119 /* Is it valid for this decl to have an initializer at all? */
5121 /* Don't allow initializations for incomplete types except for
5122 arrays which might be completed by the initialization. */
5123 if (complete_p)
5124 ; /* A complete type is ok. */
5125 else if (type_uses_auto (type))
5126 ; /* An auto type is ok. */
5127 else if (TREE_CODE (type) != ARRAY_TYPE)
5129 error ("variable %q#D has initializer but incomplete type", decl);
5130 type = TREE_TYPE (decl) = error_mark_node;
5132 else if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
5134 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
5135 error ("elements of array %q#D have incomplete type", decl);
5136 /* else we already gave an error in start_decl. */
5139 else if (aggregate_definition_p && !complete_p)
5141 if (type_uses_auto (type))
5142 gcc_unreachable ();
5143 else
5144 error ("aggregate %q#D has incomplete type and cannot be defined",
5145 decl);
5146 /* Change the type so that assemble_variable will give
5147 DECL an rtl we can live with: (mem (const_int 0)). */
5148 type = TREE_TYPE (decl) = error_mark_node;
5151 /* Create a new scope to hold this declaration if necessary.
5152 Whether or not a new scope is necessary cannot be determined
5153 until after the type has been completed; if the type is a
5154 specialization of a class template it is not until after
5155 instantiation has occurred that TYPE_HAS_NONTRIVIAL_DESTRUCTOR
5156 will be set correctly. */
5157 maybe_push_cleanup_level (type);
5160 /* Handle initialization of references. DECL, TYPE, and INIT have the
5161 same meaning as in cp_finish_decl. *CLEANUP must be NULL on entry,
5162 but will be set to a new CLEANUP_STMT if a temporary is created
5163 that must be destroyed subsequently.
5165 Returns an initializer expression to use to initialize DECL, or
5166 NULL if the initialization can be performed statically.
5168 Quotes on semantics can be found in ARM 8.4.3. */
5170 static tree
5171 grok_reference_init (tree decl, tree type, tree init, int flags)
5173 if (init == NULL_TREE)
5175 if ((DECL_LANG_SPECIFIC (decl) == 0
5176 || DECL_IN_AGGR_P (decl) == 0)
5177 && ! DECL_THIS_EXTERN (decl))
5178 error ("%qD declared as reference but not initialized", decl);
5179 return NULL_TREE;
5182 if (TREE_CODE (init) == TREE_LIST)
5183 init = build_x_compound_expr_from_list (init, ELK_INIT,
5184 tf_warning_or_error);
5186 tree ttype = TREE_TYPE (type);
5187 if (TREE_CODE (ttype) != ARRAY_TYPE
5188 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
5189 /* Note: default conversion is only called in very special cases. */
5190 init = decay_conversion (init, tf_warning_or_error);
5192 /* check_initializer handles this for non-reference variables, but for
5193 references we need to do it here or the initializer will get the
5194 incomplete array type and confuse later calls to
5195 cp_complete_array_type. */
5196 if (TREE_CODE (ttype) == ARRAY_TYPE
5197 && TYPE_DOMAIN (ttype) == NULL_TREE
5198 && (BRACE_ENCLOSED_INITIALIZER_P (init)
5199 || TREE_CODE (init) == STRING_CST))
5201 cp_complete_array_type (&ttype, init, false);
5202 if (ttype != TREE_TYPE (type))
5203 type = cp_build_reference_type (ttype, TYPE_REF_IS_RVALUE (type));
5206 /* Convert INIT to the reference type TYPE. This may involve the
5207 creation of a temporary, whose lifetime must be the same as that
5208 of the reference. If so, a DECL_EXPR for the temporary will be
5209 added just after the DECL_EXPR for DECL. That's why we don't set
5210 DECL_INITIAL for local references (instead assigning to them
5211 explicitly); we need to allow the temporary to be initialized
5212 first. */
5213 return initialize_reference (type, init, flags,
5214 tf_warning_or_error);
5217 /* Designated initializers in arrays are not supported in GNU C++.
5218 The parser cannot detect this error since it does not know whether
5219 a given brace-enclosed initializer is for a class type or for an
5220 array. This function checks that CE does not use a designated
5221 initializer. If it does, an error is issued. Returns true if CE
5222 is valid, i.e., does not have a designated initializer. */
5224 static bool
5225 check_array_designated_initializer (constructor_elt *ce,
5226 unsigned HOST_WIDE_INT index)
5228 /* Designated initializers for array elements are not supported. */
5229 if (ce->index)
5231 /* The parser only allows identifiers as designated
5232 initializers. */
5233 if (ce->index == error_mark_node)
5235 error ("name used in a GNU-style designated "
5236 "initializer for an array");
5237 return false;
5239 else if (identifier_p (ce->index))
5241 error ("name %qD used in a GNU-style designated "
5242 "initializer for an array", ce->index);
5243 return false;
5246 tree ce_index = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5247 ce->index, true);
5248 if (ce_index
5249 && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (ce_index))
5250 && (TREE_CODE (ce_index = maybe_constant_value (ce_index))
5251 == INTEGER_CST))
5253 /* A C99 designator is OK if it matches the current index. */
5254 if (wi::eq_p (ce_index, index))
5255 return true;
5256 else
5257 sorry ("non-trivial designated initializers not supported");
5259 else
5260 error ("C99 designator %qE is not an integral constant-expression",
5261 ce->index);
5263 return false;
5266 return true;
5269 /* When parsing `int a[] = {1, 2};' we don't know the size of the
5270 array until we finish parsing the initializer. If that's the
5271 situation we're in, update DECL accordingly. */
5273 static void
5274 maybe_deduce_size_from_array_init (tree decl, tree init)
5276 tree type = TREE_TYPE (decl);
5278 if (TREE_CODE (type) == ARRAY_TYPE
5279 && TYPE_DOMAIN (type) == NULL_TREE
5280 && TREE_CODE (decl) != TYPE_DECL)
5282 /* do_default is really a C-ism to deal with tentative definitions.
5283 But let's leave it here to ease the eventual merge. */
5284 int do_default = !DECL_EXTERNAL (decl);
5285 tree initializer = init ? init : DECL_INITIAL (decl);
5286 int failure = 0;
5288 /* Check that there are no designated initializers in INIT, as
5289 those are not supported in GNU C++, and as the middle-end
5290 will crash if presented with a non-numeric designated
5291 initializer. */
5292 if (initializer && BRACE_ENCLOSED_INITIALIZER_P (initializer))
5294 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initializer);
5295 constructor_elt *ce;
5296 HOST_WIDE_INT i;
5297 FOR_EACH_VEC_SAFE_ELT (v, i, ce)
5298 if (!check_array_designated_initializer (ce, i))
5299 failure = 1;
5302 if (!failure)
5304 failure = cp_complete_array_type (&TREE_TYPE (decl), initializer,
5305 do_default);
5306 if (failure == 1)
5308 error_at (EXPR_LOC_OR_LOC (initializer,
5309 DECL_SOURCE_LOCATION (decl)),
5310 "initializer fails to determine size of %qD", decl);
5312 else if (failure == 2)
5314 if (do_default)
5316 error_at (DECL_SOURCE_LOCATION (decl),
5317 "array size missing in %qD", decl);
5319 /* If a `static' var's size isn't known, make it extern as
5320 well as static, so it does not get allocated. If it's not
5321 `static', then don't mark it extern; finish_incomplete_decl
5322 will give it a default size and it will get allocated. */
5323 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
5324 DECL_EXTERNAL (decl) = 1;
5326 else if (failure == 3)
5328 error_at (DECL_SOURCE_LOCATION (decl),
5329 "zero-size array %qD", decl);
5333 cp_apply_type_quals_to_decl (cp_type_quals (TREE_TYPE (decl)), decl);
5335 relayout_decl (decl);
5339 /* Set DECL_SIZE, DECL_ALIGN, etc. for DECL (a VAR_DECL), and issue
5340 any appropriate error messages regarding the layout. */
5342 static void
5343 layout_var_decl (tree decl)
5345 tree type;
5347 type = TREE_TYPE (decl);
5348 if (type == error_mark_node)
5349 return;
5351 /* If we haven't already laid out this declaration, do so now.
5352 Note that we must not call complete type for an external object
5353 because it's type might involve templates that we are not
5354 supposed to instantiate yet. (And it's perfectly valid to say
5355 `extern X x' for some incomplete type `X'.) */
5356 if (!DECL_EXTERNAL (decl))
5357 complete_type (type);
5358 if (!DECL_SIZE (decl)
5359 && TREE_TYPE (decl) != error_mark_node
5360 && complete_or_array_type_p (type))
5361 layout_decl (decl, 0);
5363 if (!DECL_EXTERNAL (decl) && DECL_SIZE (decl) == NULL_TREE)
5365 /* An automatic variable with an incomplete type: that is an error.
5366 Don't talk about array types here, since we took care of that
5367 message in grokdeclarator. */
5368 error_at (DECL_SOURCE_LOCATION (decl),
5369 "storage size of %qD isn%'t known", decl);
5370 TREE_TYPE (decl) = error_mark_node;
5372 #if 0
5373 /* Keep this code around in case we later want to control debug info
5374 based on whether a type is "used". (jason 1999-11-11) */
5376 else if (!DECL_EXTERNAL (decl) && MAYBE_CLASS_TYPE_P (ttype))
5377 /* Let debugger know it should output info for this type. */
5378 note_debug_info_needed (ttype);
5380 if (TREE_STATIC (decl) && DECL_CLASS_SCOPE_P (decl))
5381 note_debug_info_needed (DECL_CONTEXT (decl));
5382 #endif
5384 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
5385 && DECL_SIZE (decl) != NULL_TREE
5386 && ! TREE_CONSTANT (DECL_SIZE (decl)))
5388 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
5389 constant_expression_warning (DECL_SIZE (decl));
5390 else
5392 error_at (DECL_SOURCE_LOCATION (decl),
5393 "storage size of %qD isn%'t constant", decl);
5394 TREE_TYPE (decl) = error_mark_node;
5399 /* If a local static variable is declared in an inline function, or if
5400 we have a weak definition, we must endeavor to create only one
5401 instance of the variable at link-time. */
5403 void
5404 maybe_commonize_var (tree decl)
5406 /* Static data in a function with comdat linkage also has comdat
5407 linkage. */
5408 if (TREE_STATIC (decl)
5409 /* Don't mess with __FUNCTION__. */
5410 && ! DECL_ARTIFICIAL (decl)
5411 && DECL_FUNCTION_SCOPE_P (decl)
5412 && vague_linkage_p (DECL_CONTEXT (decl)))
5414 if (flag_weak)
5416 /* With weak symbols, we simply make the variable COMDAT;
5417 that will cause copies in multiple translations units to
5418 be merged. */
5419 comdat_linkage (decl);
5421 else
5423 if (DECL_INITIAL (decl) == NULL_TREE
5424 || DECL_INITIAL (decl) == error_mark_node)
5426 /* Without weak symbols, we can use COMMON to merge
5427 uninitialized variables. */
5428 TREE_PUBLIC (decl) = 1;
5429 DECL_COMMON (decl) = 1;
5431 else
5433 /* While for initialized variables, we must use internal
5434 linkage -- which means that multiple copies will not
5435 be merged. */
5436 TREE_PUBLIC (decl) = 0;
5437 DECL_COMMON (decl) = 0;
5438 if (warning_at (DECL_SOURCE_LOCATION (decl), 0,
5439 "sorry: semantics of inline function static "
5440 "data %q#D are wrong (you%'ll wind up "
5441 "with multiple copies)", decl))
5442 inform (DECL_SOURCE_LOCATION (decl),
5443 "you can work around this by removing the initializer");
5449 /* Issue an error message if DECL is an uninitialized const variable. */
5451 static void
5452 check_for_uninitialized_const_var (tree decl)
5454 tree type = strip_array_types (TREE_TYPE (decl));
5456 /* ``Unless explicitly declared extern, a const object does not have
5457 external linkage and must be initialized. ($8.4; $12.1)'' ARM
5458 7.1.6 */
5459 if (VAR_P (decl)
5460 && TREE_CODE (type) != REFERENCE_TYPE
5461 && (CP_TYPE_CONST_P (type) || var_in_constexpr_fn (decl))
5462 && !DECL_INITIAL (decl))
5464 tree field = default_init_uninitialized_part (type);
5465 if (!field)
5466 return;
5468 if (CP_TYPE_CONST_P (type))
5469 permerror (DECL_SOURCE_LOCATION (decl),
5470 "uninitialized const %qD", decl);
5471 else
5473 error_at (DECL_SOURCE_LOCATION (decl),
5474 "uninitialized variable %qD in %<constexpr%> function",
5475 decl);
5476 cp_function_chain->invalid_constexpr = true;
5479 if (CLASS_TYPE_P (type))
5481 tree defaulted_ctor;
5483 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
5484 "%q#T has no user-provided default constructor", type);
5485 defaulted_ctor = in_class_defaulted_default_constructor (type);
5486 if (defaulted_ctor)
5487 inform (DECL_SOURCE_LOCATION (defaulted_ctor),
5488 "constructor is not user-provided because it is "
5489 "explicitly defaulted in the class body");
5490 inform (DECL_SOURCE_LOCATION (field),
5491 "and the implicitly-defined constructor does not "
5492 "initialize %q#D", field);
5497 /* Structure holding the current initializer being processed by reshape_init.
5498 CUR is a pointer to the current element being processed, END is a pointer
5499 after the last element present in the initializer. */
5500 struct reshape_iter
5502 constructor_elt *cur;
5503 constructor_elt *end;
5506 static tree reshape_init_r (tree, reshape_iter *, bool, tsubst_flags_t);
5508 /* FIELD is a FIELD_DECL or NULL. In the former case, the value
5509 returned is the next FIELD_DECL (possibly FIELD itself) that can be
5510 initialized. If there are no more such fields, the return value
5511 will be NULL. */
5513 tree
5514 next_initializable_field (tree field)
5516 while (field
5517 && (TREE_CODE (field) != FIELD_DECL
5518 || (DECL_C_BIT_FIELD (field) && !DECL_NAME (field))
5519 || DECL_ARTIFICIAL (field)))
5520 field = DECL_CHAIN (field);
5522 return field;
5525 /* Return true for [dcl.init.list] direct-list-initialization from
5526 single element of enumeration with a fixed underlying type. */
5528 bool
5529 is_direct_enum_init (tree type, tree init)
5531 if (cxx_dialect >= cxx1z
5532 && TREE_CODE (type) == ENUMERAL_TYPE
5533 && ENUM_FIXED_UNDERLYING_TYPE_P (type)
5534 && TREE_CODE (init) == CONSTRUCTOR
5535 && CONSTRUCTOR_IS_DIRECT_INIT (init)
5536 && CONSTRUCTOR_NELTS (init) == 1)
5537 return true;
5538 return false;
5541 /* Subroutine of reshape_init_array and reshape_init_vector, which does
5542 the actual work. ELT_TYPE is the element type of the array. MAX_INDEX is an
5543 INTEGER_CST representing the size of the array minus one (the maximum index),
5544 or NULL_TREE if the array was declared without specifying the size. D is
5545 the iterator within the constructor. */
5547 static tree
5548 reshape_init_array_1 (tree elt_type, tree max_index, reshape_iter *d,
5549 tsubst_flags_t complain)
5551 tree new_init;
5552 bool sized_array_p = (max_index && TREE_CONSTANT (max_index));
5553 unsigned HOST_WIDE_INT max_index_cst = 0;
5554 unsigned HOST_WIDE_INT index;
5556 /* The initializer for an array is always a CONSTRUCTOR. */
5557 new_init = build_constructor (init_list_type_node, NULL);
5559 if (sized_array_p)
5561 /* Minus 1 is used for zero sized arrays. */
5562 if (integer_all_onesp (max_index))
5563 return new_init;
5565 if (tree_fits_uhwi_p (max_index))
5566 max_index_cst = tree_to_uhwi (max_index);
5567 /* sizetype is sign extended, not zero extended. */
5568 else
5569 max_index_cst = tree_to_uhwi (fold_convert (size_type_node, max_index));
5572 /* Loop until there are no more initializers. */
5573 for (index = 0;
5574 d->cur != d->end && (!sized_array_p || index <= max_index_cst);
5575 ++index)
5577 tree elt_init;
5578 constructor_elt *old_cur = d->cur;
5580 check_array_designated_initializer (d->cur, index);
5581 elt_init = reshape_init_r (elt_type, d, /*first_initializer_p=*/false,
5582 complain);
5583 if (elt_init == error_mark_node)
5584 return error_mark_node;
5585 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init),
5586 size_int (index), elt_init);
5587 if (!TREE_CONSTANT (elt_init))
5588 TREE_CONSTANT (new_init) = false;
5590 /* This can happen with an invalid initializer (c++/54501). */
5591 if (d->cur == old_cur && !sized_array_p)
5592 break;
5595 return new_init;
5598 /* Subroutine of reshape_init_r, processes the initializers for arrays.
5599 Parameters are the same of reshape_init_r. */
5601 static tree
5602 reshape_init_array (tree type, reshape_iter *d, tsubst_flags_t complain)
5604 tree max_index = NULL_TREE;
5606 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
5608 if (TYPE_DOMAIN (type))
5609 max_index = array_type_nelts (type);
5611 return reshape_init_array_1 (TREE_TYPE (type), max_index, d, complain);
5614 /* Subroutine of reshape_init_r, processes the initializers for vectors.
5615 Parameters are the same of reshape_init_r. */
5617 static tree
5618 reshape_init_vector (tree type, reshape_iter *d, tsubst_flags_t complain)
5620 tree max_index = NULL_TREE;
5622 gcc_assert (VECTOR_TYPE_P (type));
5624 if (COMPOUND_LITERAL_P (d->cur->value))
5626 tree value = d->cur->value;
5627 if (!same_type_p (TREE_TYPE (value), type))
5629 if (complain & tf_error)
5630 error ("invalid type %qT as initializer for a vector of type %qT",
5631 TREE_TYPE (d->cur->value), type);
5632 value = error_mark_node;
5634 ++d->cur;
5635 return value;
5638 /* For a vector, we initialize it as an array of the appropriate size. */
5639 if (VECTOR_TYPE_P (type))
5640 max_index = size_int (TYPE_VECTOR_SUBPARTS (type) - 1);
5642 return reshape_init_array_1 (TREE_TYPE (type), max_index, d, complain);
5645 /* Subroutine of reshape_init_r, processes the initializers for classes
5646 or union. Parameters are the same of reshape_init_r. */
5648 static tree
5649 reshape_init_class (tree type, reshape_iter *d, bool first_initializer_p,
5650 tsubst_flags_t complain)
5652 tree field;
5653 tree new_init;
5655 gcc_assert (CLASS_TYPE_P (type));
5657 /* The initializer for a class is always a CONSTRUCTOR. */
5658 new_init = build_constructor (init_list_type_node, NULL);
5659 field = next_initializable_field (TYPE_FIELDS (type));
5661 if (!field)
5663 /* [dcl.init.aggr]
5665 An initializer for an aggregate member that is an
5666 empty class shall have the form of an empty
5667 initializer-list {}. */
5668 if (!first_initializer_p)
5670 if (complain & tf_error)
5671 error ("initializer for %qT must be brace-enclosed", type);
5672 return error_mark_node;
5674 return new_init;
5677 /* Loop through the initializable fields, gathering initializers. */
5678 while (d->cur != d->end)
5680 tree field_init;
5681 constructor_elt *old_cur = d->cur;
5683 /* Handle designated initializers, as an extension. */
5684 if (d->cur->index)
5686 if (d->cur->index == error_mark_node)
5687 return error_mark_node;
5689 if (TREE_CODE (d->cur->index) == FIELD_DECL)
5690 /* We already reshaped this. */
5691 gcc_assert (d->cur->index == field);
5692 else if (TREE_CODE (d->cur->index) == IDENTIFIER_NODE)
5693 field = lookup_field_1 (type, d->cur->index, /*want_type=*/false);
5694 else
5696 if (complain & tf_error)
5697 error ("%<[%E] =%> used in a GNU-style designated initializer"
5698 " for class %qT", d->cur->index, type);
5699 return error_mark_node;
5702 if (!field || TREE_CODE (field) != FIELD_DECL)
5704 if (complain & tf_error)
5705 error ("%qT has no non-static data member named %qD", type,
5706 d->cur->index);
5707 return error_mark_node;
5711 /* If we processed all the member of the class, we are done. */
5712 if (!field)
5713 break;
5715 field_init = reshape_init_r (TREE_TYPE (field), d,
5716 /*first_initializer_p=*/false, complain);
5717 if (field_init == error_mark_node)
5718 return error_mark_node;
5720 if (d->cur == old_cur && d->cur->index)
5722 /* This can happen with an invalid initializer for a flexible
5723 array member (c++/54441). */
5724 if (complain & tf_error)
5725 error ("invalid initializer for %q#D", field);
5726 return error_mark_node;
5729 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init), field, field_init);
5731 /* [dcl.init.aggr]
5733 When a union is initialized with a brace-enclosed
5734 initializer, the braces shall only contain an
5735 initializer for the first member of the union. */
5736 if (TREE_CODE (type) == UNION_TYPE)
5737 break;
5739 field = next_initializable_field (DECL_CHAIN (field));
5742 return new_init;
5745 /* Subroutine of reshape_init_r. We're in a context where C99 initializer
5746 designators are not valid; either complain or return true to indicate
5747 that reshape_init_r should return error_mark_node. */
5749 static bool
5750 has_designator_problem (reshape_iter *d, tsubst_flags_t complain)
5752 if (d->cur->index)
5754 if (complain & tf_error)
5755 error ("C99 designator %qE outside aggregate initializer",
5756 d->cur->index);
5757 else
5758 return true;
5760 return false;
5763 /* Subroutine of reshape_init, which processes a single initializer (part of
5764 a CONSTRUCTOR). TYPE is the type of the variable being initialized, D is the
5765 iterator within the CONSTRUCTOR which points to the initializer to process.
5766 FIRST_INITIALIZER_P is true if this is the first initializer of the
5767 outermost CONSTRUCTOR node. */
5769 static tree
5770 reshape_init_r (tree type, reshape_iter *d, bool first_initializer_p,
5771 tsubst_flags_t complain)
5773 tree init = d->cur->value;
5775 if (error_operand_p (init))
5776 return error_mark_node;
5778 if (first_initializer_p && !CP_AGGREGATE_TYPE_P (type)
5779 && has_designator_problem (d, complain))
5780 return error_mark_node;
5782 if (TREE_CODE (type) == COMPLEX_TYPE)
5784 /* A complex type can be initialized from one or two initializers,
5785 but braces are not elided. */
5786 d->cur++;
5787 if (BRACE_ENCLOSED_INITIALIZER_P (init))
5789 if (CONSTRUCTOR_NELTS (init) > 2)
5791 if (complain & tf_error)
5792 error ("too many initializers for %qT", type);
5793 else
5794 return error_mark_node;
5797 else if (first_initializer_p && d->cur != d->end)
5799 vec<constructor_elt, va_gc> *v = 0;
5800 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, init);
5801 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, d->cur->value);
5802 if (has_designator_problem (d, complain))
5803 return error_mark_node;
5804 d->cur++;
5805 init = build_constructor (init_list_type_node, v);
5807 return init;
5810 /* A non-aggregate type is always initialized with a single
5811 initializer. */
5812 if (!CP_AGGREGATE_TYPE_P (type))
5814 /* It is invalid to initialize a non-aggregate type with a
5815 brace-enclosed initializer before C++0x.
5816 We need to check for BRACE_ENCLOSED_INITIALIZER_P here because
5817 of g++.old-deja/g++.mike/p7626.C: a pointer-to-member constant is
5818 a CONSTRUCTOR (with a record type). */
5819 if (TREE_CODE (init) == CONSTRUCTOR
5820 /* Don't complain about a capture-init. */
5821 && !CONSTRUCTOR_IS_DIRECT_INIT (init)
5822 && BRACE_ENCLOSED_INITIALIZER_P (init)) /* p7626.C */
5824 if (SCALAR_TYPE_P (type))
5826 if (cxx_dialect < cxx11
5827 /* Isn't value-initialization. */
5828 || CONSTRUCTOR_NELTS (init) > 0)
5830 if (complain & tf_error)
5831 error ("braces around scalar initializer for type %qT",
5832 type);
5833 init = error_mark_node;
5836 else
5837 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5840 d->cur++;
5841 return init;
5844 /* "If T is a class type and the initializer list has a single element of
5845 type cv U, where U is T or a class derived from T, the object is
5846 initialized from that element." Even if T is an aggregate. */
5847 if (cxx_dialect >= cxx11 && (CLASS_TYPE_P (type) || VECTOR_TYPE_P (type))
5848 && first_initializer_p
5849 && d->end - d->cur == 1
5850 && reference_related_p (type, TREE_TYPE (init)))
5852 d->cur++;
5853 return init;
5856 /* [dcl.init.aggr]
5858 All implicit type conversions (clause _conv_) are considered when
5859 initializing the aggregate member with an initializer from an
5860 initializer-list. If the initializer can initialize a member,
5861 the member is initialized. Otherwise, if the member is itself a
5862 non-empty subaggregate, brace elision is assumed and the
5863 initializer is considered for the initialization of the first
5864 member of the subaggregate. */
5865 if (TREE_CODE (init) != CONSTRUCTOR
5866 /* But don't try this for the first initializer, since that would be
5867 looking through the outermost braces; A a2 = { a1 }; is not a
5868 valid aggregate initialization. */
5869 && !first_initializer_p
5870 && (same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (init))
5871 || can_convert_arg (type, TREE_TYPE (init), init, LOOKUP_NORMAL,
5872 complain)))
5874 d->cur++;
5875 return init;
5878 /* [dcl.init.string]
5880 A char array (whether plain char, signed char, or unsigned char)
5881 can be initialized by a string-literal (optionally enclosed in
5882 braces); a wchar_t array can be initialized by a wide
5883 string-literal (optionally enclosed in braces). */
5884 if (TREE_CODE (type) == ARRAY_TYPE
5885 && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type))))
5887 tree str_init = init;
5889 /* Strip one level of braces if and only if they enclose a single
5890 element (as allowed by [dcl.init.string]). */
5891 if (!first_initializer_p
5892 && TREE_CODE (str_init) == CONSTRUCTOR
5893 && CONSTRUCTOR_NELTS (str_init) == 1)
5895 str_init = (*CONSTRUCTOR_ELTS (str_init))[0].value;
5898 /* If it's a string literal, then it's the initializer for the array
5899 as a whole. Otherwise, continue with normal initialization for
5900 array types (one value per array element). */
5901 if (TREE_CODE (str_init) == STRING_CST)
5903 if (has_designator_problem (d, complain))
5904 return error_mark_node;
5905 d->cur++;
5906 return str_init;
5910 /* The following cases are about aggregates. If we are not within a full
5911 initializer already, and there is not a CONSTRUCTOR, it means that there
5912 is a missing set of braces (that is, we are processing the case for
5913 which reshape_init exists). */
5914 if (!first_initializer_p)
5916 if (TREE_CODE (init) == CONSTRUCTOR)
5918 if (TREE_TYPE (init) && TYPE_PTRMEMFUNC_P (TREE_TYPE (init)))
5919 /* There is no need to reshape pointer-to-member function
5920 initializers, as they are always constructed correctly
5921 by the front end. */
5923 else if (COMPOUND_LITERAL_P (init))
5924 /* For a nested compound literal, there is no need to reshape since
5925 brace elision is not allowed. Even if we decided to allow it,
5926 we should add a call to reshape_init in finish_compound_literal,
5927 before calling digest_init, so changing this code would still
5928 not be necessary. */
5929 gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (init));
5930 else
5932 ++d->cur;
5933 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
5934 return reshape_init (type, init, complain);
5938 if (complain & tf_warning)
5939 warning (OPT_Wmissing_braces,
5940 "missing braces around initializer for %qT",
5941 type);
5944 /* Dispatch to specialized routines. */
5945 if (CLASS_TYPE_P (type))
5946 return reshape_init_class (type, d, first_initializer_p, complain);
5947 else if (TREE_CODE (type) == ARRAY_TYPE)
5948 return reshape_init_array (type, d, complain);
5949 else if (VECTOR_TYPE_P (type))
5950 return reshape_init_vector (type, d, complain);
5951 else
5952 gcc_unreachable();
5955 /* Undo the brace-elision allowed by [dcl.init.aggr] in a
5956 brace-enclosed aggregate initializer.
5958 INIT is the CONSTRUCTOR containing the list of initializers describing
5959 a brace-enclosed initializer for an entity of the indicated aggregate TYPE.
5960 It may not presently match the shape of the TYPE; for example:
5962 struct S { int a; int b; };
5963 struct S a[] = { 1, 2, 3, 4 };
5965 Here INIT will hold a vector of four elements, rather than a
5966 vector of two elements, each itself a vector of two elements. This
5967 routine transforms INIT from the former form into the latter. The
5968 revised CONSTRUCTOR node is returned. */
5970 tree
5971 reshape_init (tree type, tree init, tsubst_flags_t complain)
5973 vec<constructor_elt, va_gc> *v;
5974 reshape_iter d;
5975 tree new_init;
5977 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
5979 v = CONSTRUCTOR_ELTS (init);
5981 /* An empty constructor does not need reshaping, and it is always a valid
5982 initializer. */
5983 if (vec_safe_is_empty (v))
5984 return init;
5986 /* Handle [dcl.init.list] direct-list-initialization from
5987 single element of enumeration with a fixed underlying type. */
5988 if (is_direct_enum_init (type, init))
5990 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
5991 if (check_narrowing (ENUM_UNDERLYING_TYPE (type), elt, complain))
5992 return cp_build_c_cast (type, elt, tf_warning_or_error);
5993 else
5994 return error_mark_node;
5997 /* Recurse on this CONSTRUCTOR. */
5998 d.cur = &(*v)[0];
5999 d.end = d.cur + v->length ();
6001 new_init = reshape_init_r (type, &d, true, complain);
6002 if (new_init == error_mark_node)
6003 return error_mark_node;
6005 /* Make sure all the element of the constructor were used. Otherwise,
6006 issue an error about exceeding initializers. */
6007 if (d.cur != d.end)
6009 if (complain & tf_error)
6010 error ("too many initializers for %qT", type);
6011 else
6012 return error_mark_node;
6015 return new_init;
6018 /* Verify array initializer. Returns true if errors have been reported. */
6020 bool
6021 check_array_initializer (tree decl, tree type, tree init)
6023 tree element_type = TREE_TYPE (type);
6025 /* The array type itself need not be complete, because the
6026 initializer may tell us how many elements are in the array.
6027 But, the elements of the array must be complete. */
6028 if (!COMPLETE_TYPE_P (complete_type (element_type)))
6030 if (decl)
6031 error_at (DECL_SOURCE_LOCATION (decl),
6032 "elements of array %q#D have incomplete type", decl);
6033 else
6034 error ("elements of array %q#T have incomplete type", type);
6035 return true;
6037 /* A compound literal can't have variable size. */
6038 if (init && !decl
6039 && ((COMPLETE_TYPE_P (type) && !TREE_CONSTANT (TYPE_SIZE (type)))
6040 || !TREE_CONSTANT (TYPE_SIZE (element_type))))
6042 error ("variable-sized compound literal");
6043 return true;
6045 return false;
6048 /* Subroutine of check_initializer; args are passed down from that function.
6049 Set stmts_are_full_exprs_p to 1 across a call to build_aggr_init. */
6051 static tree
6052 build_aggr_init_full_exprs (tree decl, tree init, int flags)
6055 gcc_assert (stmts_are_full_exprs_p ());
6056 return build_aggr_init (decl, init, flags, tf_warning_or_error);
6059 /* Verify INIT (the initializer for DECL), and record the
6060 initialization in DECL_INITIAL, if appropriate. CLEANUP is as for
6061 grok_reference_init.
6063 If the return value is non-NULL, it is an expression that must be
6064 evaluated dynamically to initialize DECL. */
6066 static tree
6067 check_initializer (tree decl, tree init, int flags, vec<tree, va_gc> **cleanups)
6069 tree type = TREE_TYPE (decl);
6070 tree init_code = NULL;
6071 tree core_type;
6073 /* Things that are going to be initialized need to have complete
6074 type. */
6075 TREE_TYPE (decl) = type = complete_type (TREE_TYPE (decl));
6077 if (DECL_HAS_VALUE_EXPR_P (decl))
6079 /* A variable with DECL_HAS_VALUE_EXPR_P set is just a placeholder,
6080 it doesn't have storage to be initialized. */
6081 gcc_assert (init == NULL_TREE);
6082 return NULL_TREE;
6085 if (type == error_mark_node)
6086 /* We will have already complained. */
6087 return NULL_TREE;
6089 if (TREE_CODE (type) == ARRAY_TYPE)
6091 if (check_array_initializer (decl, type, init))
6092 return NULL_TREE;
6094 else if (!COMPLETE_TYPE_P (type))
6096 error_at (DECL_SOURCE_LOCATION (decl),
6097 "%q#D has incomplete type", decl);
6098 TREE_TYPE (decl) = error_mark_node;
6099 return NULL_TREE;
6101 else
6102 /* There is no way to make a variable-sized class type in GNU C++. */
6103 gcc_assert (TREE_CONSTANT (TYPE_SIZE (type)));
6105 if (init && BRACE_ENCLOSED_INITIALIZER_P (init))
6107 int init_len = CONSTRUCTOR_NELTS (init);
6108 if (SCALAR_TYPE_P (type))
6110 if (init_len == 0)
6112 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6113 init = build_zero_init (type, NULL_TREE, false);
6115 else if (init_len != 1 && TREE_CODE (type) != COMPLEX_TYPE)
6117 error_at (EXPR_LOC_OR_LOC (init, DECL_SOURCE_LOCATION (decl)),
6118 "scalar object %qD requires one element in "
6119 "initializer", decl);
6120 TREE_TYPE (decl) = error_mark_node;
6121 return NULL_TREE;
6126 if (TREE_CODE (decl) == CONST_DECL)
6128 gcc_assert (TREE_CODE (type) != REFERENCE_TYPE);
6130 DECL_INITIAL (decl) = init;
6132 gcc_assert (init != NULL_TREE);
6133 init = NULL_TREE;
6135 else if (!init && DECL_REALLY_EXTERN (decl))
6137 else if (init || type_build_ctor_call (type)
6138 || TREE_CODE (type) == REFERENCE_TYPE)
6140 if (TREE_CODE (type) == REFERENCE_TYPE)
6142 init = grok_reference_init (decl, type, init, flags);
6143 flags |= LOOKUP_ALREADY_DIGESTED;
6145 else if (!init)
6146 check_for_uninitialized_const_var (decl);
6147 /* Do not reshape constructors of vectors (they don't need to be
6148 reshaped. */
6149 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
6151 if (is_std_init_list (type))
6153 init = perform_implicit_conversion (type, init,
6154 tf_warning_or_error);
6155 flags |= LOOKUP_ALREADY_DIGESTED;
6157 else if (TYPE_NON_AGGREGATE_CLASS (type))
6159 /* Don't reshape if the class has constructors. */
6160 if (cxx_dialect == cxx98)
6161 error_at (EXPR_LOC_OR_LOC (init, DECL_SOURCE_LOCATION (decl)),
6162 "in C++98 %qD must be initialized by "
6163 "constructor, not by %<{...}%>",
6164 decl);
6166 else if (VECTOR_TYPE_P (type) && TYPE_VECTOR_OPAQUE (type))
6168 error ("opaque vector types cannot be initialized");
6169 init = error_mark_node;
6171 else
6173 init = reshape_init (type, init, tf_warning_or_error);
6174 flags |= LOOKUP_NO_NARROWING;
6177 else if (TREE_CODE (init) == TREE_LIST
6178 && TREE_TYPE (init) != unknown_type_node
6179 && !MAYBE_CLASS_TYPE_P (type))
6181 gcc_assert (TREE_CODE (decl) != RESULT_DECL);
6183 /* We get here with code like `int a (2);' */
6184 init = build_x_compound_expr_from_list (init, ELK_INIT,
6185 tf_warning_or_error);
6188 /* If DECL has an array type without a specific bound, deduce the
6189 array size from the initializer. */
6190 maybe_deduce_size_from_array_init (decl, init);
6191 type = TREE_TYPE (decl);
6192 if (type == error_mark_node)
6193 return NULL_TREE;
6195 if ((type_build_ctor_call (type) || CLASS_TYPE_P (type))
6196 && !(flags & LOOKUP_ALREADY_DIGESTED)
6197 && !(init && BRACE_ENCLOSED_INITIALIZER_P (init)
6198 && CP_AGGREGATE_TYPE_P (type)
6199 && (CLASS_TYPE_P (type)
6200 || !TYPE_NEEDS_CONSTRUCTING (type)
6201 || type_has_extended_temps (type))))
6203 init_code = build_aggr_init_full_exprs (decl, init, flags);
6205 /* A constructor call is a non-trivial initializer even if
6206 it isn't explicitly written. */
6207 if (TREE_SIDE_EFFECTS (init_code))
6208 DECL_NONTRIVIALLY_INITIALIZED_P (decl) = true;
6210 /* If this is a constexpr initializer, expand_default_init will
6211 have returned an INIT_EXPR rather than a CALL_EXPR. In that
6212 case, pull the initializer back out and pass it down into
6213 store_init_value. */
6214 while (TREE_CODE (init_code) == EXPR_STMT
6215 || TREE_CODE (init_code) == CONVERT_EXPR)
6216 init_code = TREE_OPERAND (init_code, 0);
6217 if (TREE_CODE (init_code) == INIT_EXPR)
6219 init = TREE_OPERAND (init_code, 1);
6220 init_code = NULL_TREE;
6221 /* Don't call digest_init; it's unnecessary and will complain
6222 about aggregate initialization of non-aggregate classes. */
6223 flags |= LOOKUP_ALREADY_DIGESTED;
6225 else if (DECL_DECLARED_CONSTEXPR_P (decl))
6227 /* Declared constexpr, but no suitable initializer; massage
6228 init appropriately so we can pass it into store_init_value
6229 for the error. */
6230 if (CLASS_TYPE_P (type)
6231 && (!init || TREE_CODE (init) == TREE_LIST))
6233 init = build_functional_cast (type, init, tf_none);
6234 if (TREE_CODE (init) == TARGET_EXPR)
6235 TARGET_EXPR_DIRECT_INIT_P (init) = true;
6237 init_code = NULL_TREE;
6239 else
6240 init = NULL_TREE;
6243 if (init && TREE_CODE (init) != TREE_VEC)
6245 /* In aggregate initialization of a variable, each element
6246 initialization is a full-expression because there is no
6247 enclosing expression. */
6248 gcc_assert (stmts_are_full_exprs_p ());
6250 init_code = store_init_value (decl, init, cleanups, flags);
6252 if (pedantic && TREE_CODE (type) == ARRAY_TYPE
6253 && DECL_INITIAL (decl)
6254 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
6255 && PAREN_STRING_LITERAL_P (DECL_INITIAL (decl)))
6256 warning_at (EXPR_LOC_OR_LOC (DECL_INITIAL (decl),
6257 DECL_SOURCE_LOCATION (decl)),
6258 0, "array %qD initialized by parenthesized "
6259 "string literal %qE",
6260 decl, DECL_INITIAL (decl));
6261 init = NULL;
6264 else
6266 if (CLASS_TYPE_P (core_type = strip_array_types (type))
6267 && (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type)
6268 || CLASSTYPE_REF_FIELDS_NEED_INIT (core_type)))
6269 diagnose_uninitialized_cst_or_ref_member (core_type, /*using_new=*/false,
6270 /*complain=*/true);
6272 check_for_uninitialized_const_var (decl);
6275 if (init && init != error_mark_node)
6276 init_code = build2 (INIT_EXPR, type, decl, init);
6278 if (init_code)
6280 /* We might have set these in cp_finish_decl. */
6281 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = false;
6282 TREE_CONSTANT (decl) = false;
6285 if (init_code && DECL_IN_AGGR_P (decl))
6287 static int explained = 0;
6289 if (cxx_dialect < cxx11)
6290 error ("initializer invalid for static member with constructor");
6291 else
6292 error ("non-constant in-class initialization invalid for static "
6293 "member %qD", decl);
6294 if (!explained)
6296 inform (input_location,
6297 "(an out of class initialization is required)");
6298 explained = 1;
6300 return NULL_TREE;
6303 return init_code;
6306 /* If DECL is not a local variable, give it RTL. */
6308 static void
6309 make_rtl_for_nonlocal_decl (tree decl, tree init, const char* asmspec)
6311 int toplev = toplevel_bindings_p ();
6312 int defer_p;
6314 /* Set the DECL_ASSEMBLER_NAME for the object. */
6315 if (asmspec)
6317 /* The `register' keyword, when used together with an
6318 asm-specification, indicates that the variable should be
6319 placed in a particular register. */
6320 if (VAR_P (decl) && DECL_REGISTER (decl))
6322 set_user_assembler_name (decl, asmspec);
6323 DECL_HARD_REGISTER (decl) = 1;
6325 else
6327 if (TREE_CODE (decl) == FUNCTION_DECL
6328 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
6329 set_builtin_user_assembler_name (decl, asmspec);
6330 set_user_assembler_name (decl, asmspec);
6334 /* Handle non-variables up front. */
6335 if (!VAR_P (decl))
6337 rest_of_decl_compilation (decl, toplev, at_eof);
6338 return;
6341 /* If we see a class member here, it should be a static data
6342 member. */
6343 if (DECL_LANG_SPECIFIC (decl) && DECL_IN_AGGR_P (decl))
6345 gcc_assert (TREE_STATIC (decl));
6346 /* An in-class declaration of a static data member should be
6347 external; it is only a declaration, and not a definition. */
6348 if (init == NULL_TREE)
6349 gcc_assert (DECL_EXTERNAL (decl) || !TREE_PUBLIC (decl));
6352 /* We don't create any RTL for local variables. */
6353 if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
6354 return;
6356 /* We defer emission of local statics until the corresponding
6357 DECL_EXPR is expanded. But with constexpr its function might never
6358 be expanded, so go ahead and tell cgraph about the variable now. */
6359 defer_p = ((DECL_FUNCTION_SCOPE_P (decl)
6360 && !var_in_maybe_constexpr_fn (decl))
6361 || DECL_VIRTUAL_P (decl));
6363 /* Defer template instantiations. */
6364 if (DECL_LANG_SPECIFIC (decl)
6365 && DECL_IMPLICIT_INSTANTIATION (decl))
6366 defer_p = 1;
6368 /* If we're not deferring, go ahead and assemble the variable. */
6369 if (!defer_p)
6370 rest_of_decl_compilation (decl, toplev, at_eof);
6373 /* walk_tree helper for wrap_temporary_cleanups, below. */
6375 static tree
6376 wrap_cleanups_r (tree *stmt_p, int *walk_subtrees, void *data)
6378 /* Stop at types or full-expression boundaries. */
6379 if (TYPE_P (*stmt_p)
6380 || TREE_CODE (*stmt_p) == CLEANUP_POINT_EXPR)
6382 *walk_subtrees = 0;
6383 return NULL_TREE;
6386 if (TREE_CODE (*stmt_p) == TARGET_EXPR)
6388 tree guard = (tree)data;
6389 tree tcleanup = TARGET_EXPR_CLEANUP (*stmt_p);
6391 tcleanup = build2 (TRY_CATCH_EXPR, void_type_node, tcleanup, guard);
6392 /* Tell honor_protect_cleanup_actions to handle this as a separate
6393 cleanup. */
6394 TRY_CATCH_IS_CLEANUP (tcleanup) = 1;
6396 TARGET_EXPR_CLEANUP (*stmt_p) = tcleanup;
6399 return NULL_TREE;
6402 /* We're initializing a local variable which has a cleanup GUARD. If there
6403 are any temporaries used in the initializer INIT of this variable, we
6404 need to wrap their cleanups with TRY_CATCH_EXPR (, GUARD) so that the
6405 variable will be cleaned up properly if one of them throws.
6407 Unfortunately, there's no way to express this properly in terms of
6408 nesting, as the regions for the temporaries overlap the region for the
6409 variable itself; if there are two temporaries, the variable needs to be
6410 the first thing destroyed if either of them throws. However, we only
6411 want to run the variable's cleanup if it actually got constructed. So
6412 we need to guard the temporary cleanups with the variable's cleanup if
6413 they are run on the normal path, but not if they are run on the
6414 exceptional path. We implement this by telling
6415 honor_protect_cleanup_actions to strip the variable cleanup from the
6416 exceptional path. */
6418 static void
6419 wrap_temporary_cleanups (tree init, tree guard)
6421 cp_walk_tree_without_duplicates (&init, wrap_cleanups_r, (void *)guard);
6424 /* Generate code to initialize DECL (a local variable). */
6426 static void
6427 initialize_local_var (tree decl, tree init)
6429 tree type = TREE_TYPE (decl);
6430 tree cleanup;
6431 int already_used;
6433 gcc_assert (VAR_P (decl)
6434 || TREE_CODE (decl) == RESULT_DECL);
6435 gcc_assert (!TREE_STATIC (decl));
6437 if (DECL_SIZE (decl) == NULL_TREE)
6439 /* If we used it already as memory, it must stay in memory. */
6440 DECL_INITIAL (decl) = NULL_TREE;
6441 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
6442 return;
6445 if (type == error_mark_node)
6446 return;
6448 /* Compute and store the initial value. */
6449 already_used = TREE_USED (decl) || TREE_USED (type);
6450 if (TREE_USED (type))
6451 DECL_READ_P (decl) = 1;
6453 /* Generate a cleanup, if necessary. */
6454 cleanup = cxx_maybe_build_cleanup (decl, tf_warning_or_error);
6456 /* Perform the initialization. */
6457 if (init)
6459 tree rinit = (TREE_CODE (init) == INIT_EXPR
6460 ? TREE_OPERAND (init, 1) : NULL_TREE);
6461 if (rinit && !TREE_SIDE_EFFECTS (rinit))
6463 /* Stick simple initializers in DECL_INITIAL so that
6464 -Wno-init-self works (c++/34772). */
6465 gcc_assert (TREE_OPERAND (init, 0) == decl);
6466 DECL_INITIAL (decl) = rinit;
6468 if (warn_init_self && TREE_CODE (type) == REFERENCE_TYPE)
6470 STRIP_NOPS (rinit);
6471 if (rinit == decl)
6472 warning_at (DECL_SOURCE_LOCATION (decl),
6473 OPT_Winit_self,
6474 "reference %qD is initialized with itself", decl);
6477 else
6479 int saved_stmts_are_full_exprs_p;
6481 /* If we're only initializing a single object, guard the
6482 destructors of any temporaries used in its initializer with
6483 its destructor. This isn't right for arrays because each
6484 element initialization is a full-expression. */
6485 if (cleanup && TREE_CODE (type) != ARRAY_TYPE)
6486 wrap_temporary_cleanups (init, cleanup);
6488 gcc_assert (building_stmt_list_p ());
6489 saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
6490 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
6491 finish_expr_stmt (init);
6492 current_stmt_tree ()->stmts_are_full_exprs_p =
6493 saved_stmts_are_full_exprs_p;
6497 /* Set this to 0 so we can tell whether an aggregate which was
6498 initialized was ever used. Don't do this if it has a
6499 destructor, so we don't complain about the 'resource
6500 allocation is initialization' idiom. Now set
6501 attribute((unused)) on types so decls of that type will be
6502 marked used. (see TREE_USED, above.) */
6503 if (TYPE_NEEDS_CONSTRUCTING (type)
6504 && ! already_used
6505 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type)
6506 && DECL_NAME (decl))
6507 TREE_USED (decl) = 0;
6508 else if (already_used)
6509 TREE_USED (decl) = 1;
6511 if (cleanup)
6512 finish_decl_cleanup (decl, cleanup);
6515 /* DECL is a VAR_DECL for a compiler-generated variable with static
6516 storage duration (like a virtual table) whose initializer is a
6517 compile-time constant. Initialize the variable and provide it to the
6518 back end. */
6520 void
6521 initialize_artificial_var (tree decl, vec<constructor_elt, va_gc> *v)
6523 tree init;
6524 gcc_assert (DECL_ARTIFICIAL (decl));
6525 init = build_constructor (TREE_TYPE (decl), v);
6526 gcc_assert (TREE_CODE (init) == CONSTRUCTOR);
6527 DECL_INITIAL (decl) = init;
6528 DECL_INITIALIZED_P (decl) = 1;
6529 determine_visibility (decl);
6530 layout_var_decl (decl);
6531 maybe_commonize_var (decl);
6532 make_rtl_for_nonlocal_decl (decl, init, /*asmspec=*/NULL);
6535 /* INIT is the initializer for a variable, as represented by the
6536 parser. Returns true iff INIT is type-dependent. */
6538 static bool
6539 type_dependent_init_p (tree init)
6541 if (TREE_CODE (init) == TREE_LIST)
6542 /* A parenthesized initializer, e.g.: int i (3, 2); ? */
6543 return any_type_dependent_elements_p (init);
6544 else if (TREE_CODE (init) == CONSTRUCTOR)
6545 /* A brace-enclosed initializer, e.g.: int i = { 3 }; ? */
6547 vec<constructor_elt, va_gc> *elts;
6548 size_t nelts;
6549 size_t i;
6551 elts = CONSTRUCTOR_ELTS (init);
6552 nelts = vec_safe_length (elts);
6553 for (i = 0; i < nelts; ++i)
6554 if (type_dependent_init_p ((*elts)[i].value))
6555 return true;
6557 else
6558 /* It must be a simple expression, e.g., int i = 3; */
6559 return type_dependent_expression_p (init);
6561 return false;
6564 /* INIT is the initializer for a variable, as represented by the
6565 parser. Returns true iff INIT is value-dependent. */
6567 static bool
6568 value_dependent_init_p (tree init)
6570 if (TREE_CODE (init) == TREE_LIST)
6571 /* A parenthesized initializer, e.g.: int i (3, 2); ? */
6572 return any_value_dependent_elements_p (init);
6573 else if (TREE_CODE (init) == CONSTRUCTOR)
6574 /* A brace-enclosed initializer, e.g.: int i = { 3 }; ? */
6576 vec<constructor_elt, va_gc> *elts;
6577 size_t nelts;
6578 size_t i;
6580 elts = CONSTRUCTOR_ELTS (init);
6581 nelts = vec_safe_length (elts);
6582 for (i = 0; i < nelts; ++i)
6583 if (value_dependent_init_p ((*elts)[i].value))
6584 return true;
6586 else
6587 /* It must be a simple expression, e.g., int i = 3; */
6588 return value_dependent_expression_p (init);
6590 return false;
6593 // Returns true if a DECL is VAR_DECL with the concept specifier.
6594 static inline bool
6595 is_concept_var (tree decl)
6597 return (VAR_P (decl)
6598 // Not all variables have DECL_LANG_SPECIFIC.
6599 && DECL_LANG_SPECIFIC (decl)
6600 && DECL_DECLARED_CONCEPT_P (decl));
6603 /* A helper function to be called via walk_tree. If any label exists
6604 under *TP, it is (going to be) forced. Set has_forced_label_in_static. */
6606 static tree
6607 notice_forced_label_r (tree *tp, int *walk_subtrees, void *)
6609 if (TYPE_P (*tp))
6610 *walk_subtrees = 0;
6611 if (TREE_CODE (*tp) == LABEL_DECL)
6612 cfun->has_forced_label_in_static = 1;
6613 return NULL_TREE;
6616 /* Finish processing of a declaration;
6617 install its line number and initial value.
6618 If the length of an array type is not known before,
6619 it must be determined now, from the initial value, or it is an error.
6621 INIT is the initializer (if any) for DECL. If INIT_CONST_EXPR_P is
6622 true, then INIT is an integral constant expression.
6624 FLAGS is LOOKUP_ONLYCONVERTING if the = init syntax was used, else 0
6625 if the (init) syntax was used. */
6627 void
6628 cp_finish_decl (tree decl, tree init, bool init_const_expr_p,
6629 tree asmspec_tree, int flags)
6631 tree type;
6632 vec<tree, va_gc> *cleanups = NULL;
6633 const char *asmspec = NULL;
6634 int was_readonly = 0;
6635 bool var_definition_p = false;
6636 tree auto_node;
6638 if (decl == error_mark_node)
6639 return;
6640 else if (! decl)
6642 if (init)
6643 error ("assignment (not initialization) in declaration");
6644 return;
6647 gcc_assert (TREE_CODE (decl) != RESULT_DECL);
6648 /* Parameters are handled by store_parm_decls, not cp_finish_decl. */
6649 gcc_assert (TREE_CODE (decl) != PARM_DECL);
6651 type = TREE_TYPE (decl);
6652 if (type == error_mark_node)
6653 return;
6655 /* Warn about register storage specifiers except when in GNU global
6656 or local register variable extension. */
6657 if (VAR_P (decl) && DECL_REGISTER (decl) && asmspec_tree == NULL_TREE)
6659 if (cxx_dialect >= cxx1z)
6660 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
6661 "ISO C++1z does not allow %<register%> storage "
6662 "class specifier");
6663 else
6664 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
6665 "%<register%> storage class specifier used");
6668 /* If a name was specified, get the string. */
6669 if (at_namespace_scope_p ())
6670 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
6671 if (asmspec_tree && asmspec_tree != error_mark_node)
6672 asmspec = TREE_STRING_POINTER (asmspec_tree);
6674 if (current_class_type
6675 && CP_DECL_CONTEXT (decl) == current_class_type
6676 && TYPE_BEING_DEFINED (current_class_type)
6677 && !CLASSTYPE_TEMPLATE_INSTANTIATION (current_class_type)
6678 && (DECL_INITIAL (decl) || init))
6679 DECL_INITIALIZED_IN_CLASS_P (decl) = 1;
6681 if (TREE_CODE (decl) != FUNCTION_DECL
6682 && (auto_node = type_uses_auto (type)))
6684 tree d_init;
6685 if (init == NULL_TREE)
6687 if (DECL_LANG_SPECIFIC (decl)
6688 && DECL_TEMPLATE_INSTANTIATION (decl)
6689 && !DECL_TEMPLATE_INSTANTIATED (decl))
6691 /* init is null because we're deferring instantiating the
6692 initializer until we need it. Well, we need it now. */
6693 instantiate_decl (decl, /*defer_ok*/true, /*expl*/false);
6694 return;
6697 gcc_unreachable ();
6699 d_init = init;
6700 if (TREE_CODE (d_init) == TREE_LIST
6701 && !CLASS_PLACEHOLDER_TEMPLATE (auto_node))
6702 d_init = build_x_compound_expr_from_list (d_init, ELK_INIT,
6703 tf_warning_or_error);
6704 d_init = resolve_nondeduced_context (d_init, tf_warning_or_error);
6705 type = TREE_TYPE (decl) = do_auto_deduction (type, d_init,
6706 auto_node,
6707 tf_warning_or_error,
6708 adc_variable_type);
6709 if (type == error_mark_node)
6710 return;
6711 if (TREE_CODE (type) == FUNCTION_TYPE)
6713 error ("initializer for %<decltype(auto) %D%> has function type "
6714 "(did you forget the %<()%> ?)", decl);
6715 TREE_TYPE (decl) = error_mark_node;
6716 return;
6718 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
6721 if (!ensure_literal_type_for_constexpr_object (decl))
6722 DECL_DECLARED_CONSTEXPR_P (decl) = 0;
6724 if (VAR_P (decl)
6725 && DECL_CLASS_SCOPE_P (decl)
6726 && DECL_INITIALIZED_IN_CLASS_P (decl))
6727 check_static_variable_definition (decl, type);
6729 if (init && TREE_CODE (decl) == FUNCTION_DECL)
6731 tree clone;
6732 if (init == ridpointers[(int)RID_DELETE])
6734 /* FIXME check this is 1st decl. */
6735 DECL_DELETED_FN (decl) = 1;
6736 DECL_DECLARED_INLINE_P (decl) = 1;
6737 DECL_INITIAL (decl) = error_mark_node;
6738 FOR_EACH_CLONE (clone, decl)
6740 DECL_DELETED_FN (clone) = 1;
6741 DECL_DECLARED_INLINE_P (clone) = 1;
6742 DECL_INITIAL (clone) = error_mark_node;
6744 init = NULL_TREE;
6746 else if (init == ridpointers[(int)RID_DEFAULT])
6748 if (defaultable_fn_check (decl))
6749 DECL_DEFAULTED_FN (decl) = 1;
6750 else
6751 DECL_INITIAL (decl) = NULL_TREE;
6755 if (init && VAR_P (decl))
6757 DECL_NONTRIVIALLY_INITIALIZED_P (decl) = 1;
6758 /* If DECL is a reference, then we want to know whether init is a
6759 reference constant; init_const_expr_p as passed tells us whether
6760 it's an rvalue constant. */
6761 if (TREE_CODE (type) == REFERENCE_TYPE)
6762 init_const_expr_p = potential_constant_expression (init);
6763 if (init_const_expr_p)
6765 /* Set these flags now for templates. We'll update the flags in
6766 store_init_value for instantiations. */
6767 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
6768 if (decl_maybe_constant_var_p (decl))
6769 TREE_CONSTANT (decl) = 1;
6773 if (processing_template_decl)
6775 bool type_dependent_p;
6777 /* Add this declaration to the statement-tree. */
6778 if (at_function_scope_p ())
6779 add_decl_expr (decl);
6781 type_dependent_p = dependent_type_p (type);
6783 if (check_for_bare_parameter_packs (init))
6785 init = NULL_TREE;
6786 DECL_INITIAL (decl) = NULL_TREE;
6789 /* Generally, initializers in templates are expanded when the
6790 template is instantiated. But, if DECL is a variable constant
6791 then it can be used in future constant expressions, so its value
6792 must be available. */
6794 if (!VAR_P (decl) || type_dependent_p)
6795 /* We can't do anything if the decl has dependent type. */;
6796 else if (init
6797 && init_const_expr_p
6798 && TREE_CODE (type) != REFERENCE_TYPE
6799 && decl_maybe_constant_var_p (decl)
6800 && !type_dependent_init_p (init)
6801 && !value_dependent_init_p (init))
6803 /* This variable seems to be a non-dependent constant, so process
6804 its initializer. If check_initializer returns non-null the
6805 initialization wasn't constant after all. */
6806 tree init_code;
6807 cleanups = make_tree_vector ();
6808 init_code = check_initializer (decl, init, flags, &cleanups);
6809 if (init_code == NULL_TREE)
6810 init = NULL_TREE;
6811 release_tree_vector (cleanups);
6813 else if (!init && is_concept_var (decl))
6814 error ("variable concept has no initializer");
6815 else if (!DECL_PRETTY_FUNCTION_P (decl))
6817 /* Deduce array size even if the initializer is dependent. */
6818 maybe_deduce_size_from_array_init (decl, init);
6819 /* And complain about multiple initializers. */
6820 if (init && TREE_CODE (init) == TREE_LIST && TREE_CHAIN (init)
6821 && !MAYBE_CLASS_TYPE_P (type))
6822 init = build_x_compound_expr_from_list (init, ELK_INIT,
6823 tf_warning_or_error);
6826 if (init)
6827 DECL_INITIAL (decl) = init;
6828 return;
6831 /* Just store non-static data member initializers for later. */
6832 if (init && TREE_CODE (decl) == FIELD_DECL)
6833 DECL_INITIAL (decl) = init;
6835 /* Take care of TYPE_DECLs up front. */
6836 if (TREE_CODE (decl) == TYPE_DECL)
6838 if (type != error_mark_node
6839 && MAYBE_CLASS_TYPE_P (type) && DECL_NAME (decl))
6841 if (TREE_TYPE (DECL_NAME (decl)) && TREE_TYPE (decl) != type)
6842 warning (0, "shadowing previous type declaration of %q#D", decl);
6843 set_identifier_type_value (DECL_NAME (decl), decl);
6846 /* If we have installed this as the canonical typedef for this
6847 type, and that type has not been defined yet, delay emitting
6848 the debug information for it, as we will emit it later. */
6849 if (TYPE_MAIN_DECL (TREE_TYPE (decl)) == decl
6850 && !COMPLETE_TYPE_P (TREE_TYPE (decl)))
6851 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
6853 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl),
6854 at_eof);
6855 return;
6858 /* A reference will be modified here, as it is initialized. */
6859 if (! DECL_EXTERNAL (decl)
6860 && TREE_READONLY (decl)
6861 && TREE_CODE (type) == REFERENCE_TYPE)
6863 was_readonly = 1;
6864 TREE_READONLY (decl) = 0;
6867 if (VAR_P (decl))
6869 /* If this is a local variable that will need a mangled name,
6870 register it now. We must do this before processing the
6871 initializer for the variable, since the initialization might
6872 require a guard variable, and since the mangled name of the
6873 guard variable will depend on the mangled name of this
6874 variable. */
6875 if (DECL_FUNCTION_SCOPE_P (decl)
6876 && TREE_STATIC (decl)
6877 && !DECL_ARTIFICIAL (decl))
6879 push_local_name (decl);
6880 /* Normally has_forced_label_in_static is set during GIMPLE
6881 lowering, but [cd]tors are never actually compiled directly.
6882 We need to set this early so we can deal with the label
6883 address extension. */
6884 if ((DECL_CONSTRUCTOR_P (current_function_decl)
6885 || DECL_DESTRUCTOR_P (current_function_decl))
6886 && init)
6888 walk_tree (&init, notice_forced_label_r, NULL, NULL);
6889 add_local_decl (cfun, decl);
6891 /* And make sure it's in the symbol table for
6892 c_parse_final_cleanups to find. */
6893 varpool_node::get_create (decl);
6896 /* Convert the initializer to the type of DECL, if we have not
6897 already initialized DECL. */
6898 if (!DECL_INITIALIZED_P (decl)
6899 /* If !DECL_EXTERNAL then DECL is being defined. In the
6900 case of a static data member initialized inside the
6901 class-specifier, there can be an initializer even if DECL
6902 is *not* defined. */
6903 && (!DECL_EXTERNAL (decl) || init))
6905 cleanups = make_tree_vector ();
6906 init = check_initializer (decl, init, flags, &cleanups);
6908 /* Handle:
6910 [dcl.init]
6912 The memory occupied by any object of static storage
6913 duration is zero-initialized at program startup before
6914 any other initialization takes place.
6916 We cannot create an appropriate initializer until after
6917 the type of DECL is finalized. If DECL_INITIAL is set,
6918 then the DECL is statically initialized, and any
6919 necessary zero-initialization has already been performed. */
6920 if (TREE_STATIC (decl) && !DECL_INITIAL (decl))
6921 DECL_INITIAL (decl) = build_zero_init (TREE_TYPE (decl),
6922 /*nelts=*/NULL_TREE,
6923 /*static_storage_p=*/true);
6924 /* Remember that the initialization for this variable has
6925 taken place. */
6926 DECL_INITIALIZED_P (decl) = 1;
6927 /* This declaration is the definition of this variable,
6928 unless we are initializing a static data member within
6929 the class specifier. */
6930 if (!DECL_EXTERNAL (decl))
6931 var_definition_p = true;
6933 /* If the variable has an array type, lay out the type, even if
6934 there is no initializer. It is valid to index through the
6935 array, and we must get TYPE_ALIGN set correctly on the array
6936 type. */
6937 else if (TREE_CODE (type) == ARRAY_TYPE)
6938 layout_type (type);
6940 if (TREE_STATIC (decl)
6941 && !at_function_scope_p ()
6942 && current_function_decl == NULL)
6943 /* So decl is a global variable or a static member of a
6944 non local class. Record the types it uses
6945 so that we can decide later to emit debug info for them. */
6946 record_types_used_by_current_var_decl (decl);
6949 /* Add this declaration to the statement-tree. This needs to happen
6950 after the call to check_initializer so that the DECL_EXPR for a
6951 reference temp is added before the DECL_EXPR for the reference itself. */
6952 if (DECL_FUNCTION_SCOPE_P (decl))
6954 /* If we're building a variable sized type, and we might be
6955 reachable other than via the top of the current binding
6956 level, then create a new BIND_EXPR so that we deallocate
6957 the object at the right time. */
6958 if (VAR_P (decl)
6959 && DECL_SIZE (decl)
6960 && !TREE_CONSTANT (DECL_SIZE (decl))
6961 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
6963 tree bind;
6964 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
6965 TREE_SIDE_EFFECTS (bind) = 1;
6966 add_stmt (bind);
6967 BIND_EXPR_BODY (bind) = push_stmt_list ();
6969 add_decl_expr (decl);
6972 /* Let the middle end know about variables and functions -- but not
6973 static data members in uninstantiated class templates. */
6974 if (VAR_OR_FUNCTION_DECL_P (decl))
6976 if (VAR_P (decl))
6978 layout_var_decl (decl);
6979 maybe_commonize_var (decl);
6982 /* This needs to happen after the linkage is set. */
6983 determine_visibility (decl);
6985 if (var_definition_p && TREE_STATIC (decl))
6987 /* If a TREE_READONLY variable needs initialization
6988 at runtime, it is no longer readonly and we need to
6989 avoid MEM_READONLY_P being set on RTL created for it. */
6990 if (init)
6992 if (TREE_READONLY (decl))
6993 TREE_READONLY (decl) = 0;
6994 was_readonly = 0;
6996 else if (was_readonly)
6997 TREE_READONLY (decl) = 1;
6999 /* Likewise if it needs destruction. */
7000 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
7001 TREE_READONLY (decl) = 0;
7004 make_rtl_for_nonlocal_decl (decl, init, asmspec);
7006 /* Check for abstractness of the type. Notice that there is no
7007 need to strip array types here since the check for those types
7008 is already done within create_array_type_for_decl. */
7009 abstract_virtuals_error (decl, type);
7011 if (TREE_TYPE (decl) == error_mark_node)
7012 /* No initialization required. */
7014 else if (TREE_CODE (decl) == FUNCTION_DECL)
7016 if (init)
7018 if (init == ridpointers[(int)RID_DEFAULT])
7020 /* An out-of-class default definition is defined at
7021 the point where it is explicitly defaulted. */
7022 if (DECL_DELETED_FN (decl))
7023 maybe_explain_implicit_delete (decl);
7024 else if (DECL_INITIAL (decl) == error_mark_node)
7025 synthesize_method (decl);
7027 else
7028 error ("function %q#D is initialized like a variable", decl);
7030 /* else no initialization required. */
7032 else if (DECL_EXTERNAL (decl)
7033 && ! (DECL_LANG_SPECIFIC (decl)
7034 && DECL_NOT_REALLY_EXTERN (decl)))
7036 if (init)
7037 DECL_INITIAL (decl) = init;
7039 /* A variable definition. */
7040 else if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
7041 /* Initialize the local variable. */
7042 initialize_local_var (decl, init);
7044 /* If a variable is defined, and then a subsequent
7045 definition with external linkage is encountered, we will
7046 get here twice for the same variable. We want to avoid
7047 calling expand_static_init more than once. For variables
7048 that are not static data members, we can call
7049 expand_static_init only when we actually process the
7050 initializer. It is not legal to redeclare a static data
7051 member, so this issue does not arise in that case. */
7052 else if (var_definition_p && TREE_STATIC (decl))
7053 expand_static_init (decl, init);
7056 /* If a CLEANUP_STMT was created to destroy a temporary bound to a
7057 reference, insert it in the statement-tree now. */
7058 if (cleanups)
7060 unsigned i; tree t;
7061 FOR_EACH_VEC_ELT (*cleanups, i, t)
7062 push_cleanup (decl, t, false);
7063 release_tree_vector (cleanups);
7066 if (was_readonly)
7067 TREE_READONLY (decl) = 1;
7069 invoke_plugin_callbacks (PLUGIN_FINISH_DECL, decl);
7072 /* Returns a declaration for a VAR_DECL as if:
7074 extern "C" TYPE NAME;
7076 had been seen. Used to create compiler-generated global
7077 variables. */
7079 static tree
7080 declare_global_var (tree name, tree type)
7082 tree decl;
7084 push_to_top_level ();
7085 decl = build_decl (input_location, VAR_DECL, name, type);
7086 TREE_PUBLIC (decl) = 1;
7087 DECL_EXTERNAL (decl) = 1;
7088 DECL_ARTIFICIAL (decl) = 1;
7089 /* If the user has explicitly declared this variable (perhaps
7090 because the code we are compiling is part of a low-level runtime
7091 library), then it is possible that our declaration will be merged
7092 with theirs by pushdecl. */
7093 decl = pushdecl (decl);
7094 cp_finish_decl (decl, NULL_TREE, false, NULL_TREE, 0);
7095 pop_from_top_level ();
7097 return decl;
7100 /* Returns the type for the argument to "__cxa_atexit" (or "atexit",
7101 if "__cxa_atexit" is not being used) corresponding to the function
7102 to be called when the program exits. */
7104 static tree
7105 get_atexit_fn_ptr_type (void)
7107 tree fn_type;
7109 if (!atexit_fn_ptr_type_node)
7111 tree arg_type;
7112 if (flag_use_cxa_atexit
7113 && !targetm.cxx.use_atexit_for_cxa_atexit ())
7114 /* The parameter to "__cxa_atexit" is "void (*)(void *)". */
7115 arg_type = ptr_type_node;
7116 else
7117 /* The parameter to "atexit" is "void (*)(void)". */
7118 arg_type = NULL_TREE;
7120 fn_type = build_function_type_list (void_type_node,
7121 arg_type, NULL_TREE);
7122 atexit_fn_ptr_type_node = build_pointer_type (fn_type);
7125 return atexit_fn_ptr_type_node;
7128 /* Returns a pointer to the `atexit' function. Note that if
7129 FLAG_USE_CXA_ATEXIT is nonzero, then this will actually be the new
7130 `__cxa_atexit' function specified in the IA64 C++ ABI. */
7132 static tree
7133 get_atexit_node (void)
7135 tree atexit_fndecl;
7136 tree fn_type;
7137 tree fn_ptr_type;
7138 const char *name;
7139 bool use_aeabi_atexit;
7141 if (atexit_node)
7142 return atexit_node;
7144 if (flag_use_cxa_atexit && !targetm.cxx.use_atexit_for_cxa_atexit ())
7146 /* The declaration for `__cxa_atexit' is:
7148 int __cxa_atexit (void (*)(void *), void *, void *)
7150 We build up the argument types and then the function type
7151 itself. */
7152 tree argtype0, argtype1, argtype2;
7154 use_aeabi_atexit = targetm.cxx.use_aeabi_atexit ();
7155 /* First, build the pointer-to-function type for the first
7156 argument. */
7157 fn_ptr_type = get_atexit_fn_ptr_type ();
7158 /* Then, build the rest of the argument types. */
7159 argtype2 = ptr_type_node;
7160 if (use_aeabi_atexit)
7162 argtype1 = fn_ptr_type;
7163 argtype0 = ptr_type_node;
7165 else
7167 argtype1 = ptr_type_node;
7168 argtype0 = fn_ptr_type;
7170 /* And the final __cxa_atexit type. */
7171 fn_type = build_function_type_list (integer_type_node,
7172 argtype0, argtype1, argtype2,
7173 NULL_TREE);
7174 if (use_aeabi_atexit)
7175 name = "__aeabi_atexit";
7176 else
7177 name = "__cxa_atexit";
7179 else
7181 /* The declaration for `atexit' is:
7183 int atexit (void (*)());
7185 We build up the argument types and then the function type
7186 itself. */
7187 fn_ptr_type = get_atexit_fn_ptr_type ();
7188 /* Build the final atexit type. */
7189 fn_type = build_function_type_list (integer_type_node,
7190 fn_ptr_type, NULL_TREE);
7191 name = "atexit";
7194 /* Now, build the function declaration. */
7195 push_lang_context (lang_name_c);
7196 atexit_fndecl = build_library_fn_ptr (name, fn_type, ECF_LEAF | ECF_NOTHROW);
7197 mark_used (atexit_fndecl);
7198 pop_lang_context ();
7199 atexit_node = decay_conversion (atexit_fndecl, tf_warning_or_error);
7201 return atexit_node;
7204 /* Like get_atexit_node, but for thread-local cleanups. */
7206 static tree
7207 get_thread_atexit_node (void)
7209 /* The declaration for `__cxa_thread_atexit' is:
7211 int __cxa_thread_atexit (void (*)(void *), void *, void *) */
7212 tree fn_type = build_function_type_list (integer_type_node,
7213 get_atexit_fn_ptr_type (),
7214 ptr_type_node, ptr_type_node,
7215 NULL_TREE);
7217 /* Now, build the function declaration. */
7218 tree atexit_fndecl = build_library_fn_ptr ("__cxa_thread_atexit", fn_type,
7219 ECF_LEAF | ECF_NOTHROW);
7220 return decay_conversion (atexit_fndecl, tf_warning_or_error);
7223 /* Returns the __dso_handle VAR_DECL. */
7225 static tree
7226 get_dso_handle_node (void)
7228 if (dso_handle_node)
7229 return dso_handle_node;
7231 /* Declare the variable. */
7232 dso_handle_node = declare_global_var (get_identifier ("__dso_handle"),
7233 ptr_type_node);
7235 #ifdef HAVE_GAS_HIDDEN
7236 if (dso_handle_node != error_mark_node)
7238 DECL_VISIBILITY (dso_handle_node) = VISIBILITY_HIDDEN;
7239 DECL_VISIBILITY_SPECIFIED (dso_handle_node) = 1;
7241 #endif
7243 return dso_handle_node;
7246 /* Begin a new function with internal linkage whose job will be simply
7247 to destroy some particular variable. */
7249 static GTY(()) int start_cleanup_cnt;
7251 static tree
7252 start_cleanup_fn (void)
7254 char name[32];
7255 tree fntype;
7256 tree fndecl;
7257 bool use_cxa_atexit = flag_use_cxa_atexit
7258 && !targetm.cxx.use_atexit_for_cxa_atexit ();
7260 push_to_top_level ();
7262 /* No need to mangle this. */
7263 push_lang_context (lang_name_c);
7265 /* Build the name of the function. */
7266 sprintf (name, "__tcf_%d", start_cleanup_cnt++);
7267 /* Build the function declaration. */
7268 fntype = TREE_TYPE (get_atexit_fn_ptr_type ());
7269 fndecl = build_lang_decl (FUNCTION_DECL, get_identifier (name), fntype);
7270 /* It's a function with internal linkage, generated by the
7271 compiler. */
7272 TREE_PUBLIC (fndecl) = 0;
7273 DECL_ARTIFICIAL (fndecl) = 1;
7274 /* Make the function `inline' so that it is only emitted if it is
7275 actually needed. It is unlikely that it will be inlined, since
7276 it is only called via a function pointer, but we avoid unnecessary
7277 emissions this way. */
7278 DECL_DECLARED_INLINE_P (fndecl) = 1;
7279 DECL_INTERFACE_KNOWN (fndecl) = 1;
7280 /* Build the parameter. */
7281 if (use_cxa_atexit)
7283 tree parmdecl;
7285 parmdecl = cp_build_parm_decl (NULL_TREE, ptr_type_node);
7286 DECL_CONTEXT (parmdecl) = fndecl;
7287 TREE_USED (parmdecl) = 1;
7288 DECL_READ_P (parmdecl) = 1;
7289 DECL_ARGUMENTS (fndecl) = parmdecl;
7292 pushdecl (fndecl);
7293 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
7295 pop_lang_context ();
7297 return current_function_decl;
7300 /* Finish the cleanup function begun by start_cleanup_fn. */
7302 static void
7303 end_cleanup_fn (void)
7305 expand_or_defer_fn (finish_function (0));
7307 pop_from_top_level ();
7310 /* Generate code to handle the destruction of DECL, an object with
7311 static storage duration. */
7313 tree
7314 register_dtor_fn (tree decl)
7316 tree cleanup;
7317 tree addr;
7318 tree compound_stmt;
7319 tree fcall;
7320 tree type;
7321 bool ob_parm, dso_parm, use_dtor;
7322 tree arg0, arg1, arg2;
7323 tree atex_node;
7325 type = TREE_TYPE (decl);
7326 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
7327 return void_node;
7329 /* If we're using "__cxa_atexit" (or "__cxa_thread_atexit" or
7330 "__aeabi_atexit"), and DECL is a class object, we can just pass the
7331 destructor to "__cxa_atexit"; we don't have to build a temporary
7332 function to do the cleanup. */
7333 dso_parm = (flag_use_cxa_atexit
7334 && !targetm.cxx.use_atexit_for_cxa_atexit ());
7335 ob_parm = (CP_DECL_THREAD_LOCAL_P (decl) || dso_parm);
7336 use_dtor = ob_parm && CLASS_TYPE_P (type);
7337 if (use_dtor)
7339 int idx;
7341 /* Find the destructor. */
7342 idx = lookup_fnfields_1 (type, complete_dtor_identifier);
7343 gcc_assert (idx >= 0);
7344 cleanup = (*CLASSTYPE_METHOD_VEC (type))[idx];
7345 /* Make sure it is accessible. */
7346 perform_or_defer_access_check (TYPE_BINFO (type), cleanup, cleanup,
7347 tf_warning_or_error);
7349 else
7351 /* Call build_cleanup before we enter the anonymous function so
7352 that any access checks will be done relative to the current
7353 scope, rather than the scope of the anonymous function. */
7354 build_cleanup (decl);
7356 /* Now start the function. */
7357 cleanup = start_cleanup_fn ();
7359 /* Now, recompute the cleanup. It may contain SAVE_EXPRs that refer
7360 to the original function, rather than the anonymous one. That
7361 will make the back end think that nested functions are in use,
7362 which causes confusion. */
7363 push_deferring_access_checks (dk_no_check);
7364 fcall = build_cleanup (decl);
7365 pop_deferring_access_checks ();
7367 /* Create the body of the anonymous function. */
7368 compound_stmt = begin_compound_stmt (BCS_FN_BODY);
7369 finish_expr_stmt (fcall);
7370 finish_compound_stmt (compound_stmt);
7371 end_cleanup_fn ();
7374 /* Call atexit with the cleanup function. */
7375 mark_used (cleanup);
7376 cleanup = build_address (cleanup);
7378 if (CP_DECL_THREAD_LOCAL_P (decl))
7379 atex_node = get_thread_atexit_node ();
7380 else
7381 atex_node = get_atexit_node ();
7383 if (use_dtor)
7385 /* We must convert CLEANUP to the type that "__cxa_atexit"
7386 expects. */
7387 cleanup = build_nop (get_atexit_fn_ptr_type (), cleanup);
7388 /* "__cxa_atexit" will pass the address of DECL to the
7389 cleanup function. */
7390 mark_used (decl);
7391 addr = build_address (decl);
7392 /* The declared type of the parameter to "__cxa_atexit" is
7393 "void *". For plain "T*", we could just let the
7394 machinery in cp_build_function_call convert it -- but if the
7395 type is "cv-qualified T *", then we need to convert it
7396 before passing it in, to avoid spurious errors. */
7397 addr = build_nop (ptr_type_node, addr);
7399 else
7400 /* Since the cleanup functions we build ignore the address
7401 they're given, there's no reason to pass the actual address
7402 in, and, in general, it's cheaper to pass NULL than any
7403 other value. */
7404 addr = null_pointer_node;
7406 if (dso_parm)
7407 arg2 = cp_build_addr_expr (get_dso_handle_node (),
7408 tf_warning_or_error);
7409 else if (ob_parm)
7410 /* Just pass NULL to the dso handle parm if we don't actually
7411 have a DSO handle on this target. */
7412 arg2 = null_pointer_node;
7413 else
7414 arg2 = NULL_TREE;
7416 if (ob_parm)
7418 if (!CP_DECL_THREAD_LOCAL_P (decl)
7419 && targetm.cxx.use_aeabi_atexit ())
7421 arg1 = cleanup;
7422 arg0 = addr;
7424 else
7426 arg1 = addr;
7427 arg0 = cleanup;
7430 else
7432 arg0 = cleanup;
7433 arg1 = NULL_TREE;
7435 return cp_build_function_call_nary (atex_node, tf_warning_or_error,
7436 arg0, arg1, arg2, NULL_TREE);
7439 /* DECL is a VAR_DECL with static storage duration. INIT, if present,
7440 is its initializer. Generate code to handle the construction
7441 and destruction of DECL. */
7443 static void
7444 expand_static_init (tree decl, tree init)
7446 gcc_assert (VAR_P (decl));
7447 gcc_assert (TREE_STATIC (decl));
7449 /* Some variables require no dynamic initialization. */
7450 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
7452 /* Make sure the destructor is callable. */
7453 cxx_maybe_build_cleanup (decl, tf_warning_or_error);
7454 if (!init)
7455 return;
7458 if (CP_DECL_THREAD_LOCAL_P (decl) && DECL_GNU_TLS_P (decl)
7459 && !DECL_FUNCTION_SCOPE_P (decl))
7461 if (init)
7462 error ("non-local variable %qD declared %<__thread%> "
7463 "needs dynamic initialization", decl);
7464 else
7465 error ("non-local variable %qD declared %<__thread%> "
7466 "has a non-trivial destructor", decl);
7467 static bool informed;
7468 if (!informed)
7470 inform (DECL_SOURCE_LOCATION (decl),
7471 "C++11 %<thread_local%> allows dynamic initialization "
7472 "and destruction");
7473 informed = true;
7475 return;
7478 if (DECL_FUNCTION_SCOPE_P (decl))
7480 /* Emit code to perform this initialization but once. */
7481 tree if_stmt = NULL_TREE, inner_if_stmt = NULL_TREE;
7482 tree then_clause = NULL_TREE, inner_then_clause = NULL_TREE;
7483 tree guard, guard_addr;
7484 tree flag, begin;
7485 /* We don't need thread-safety code for thread-local vars. */
7486 bool thread_guard = (flag_threadsafe_statics
7487 && !CP_DECL_THREAD_LOCAL_P (decl));
7489 /* Emit code to perform this initialization but once. This code
7490 looks like:
7492 static <type> guard;
7493 if (!__atomic_load (guard.first_byte)) {
7494 if (__cxa_guard_acquire (&guard)) {
7495 bool flag = false;
7496 try {
7497 // Do initialization.
7498 flag = true; __cxa_guard_release (&guard);
7499 // Register variable for destruction at end of program.
7500 } catch {
7501 if (!flag) __cxa_guard_abort (&guard);
7505 Note that the `flag' variable is only set to 1 *after* the
7506 initialization is complete. This ensures that an exception,
7507 thrown during the construction, will cause the variable to
7508 reinitialized when we pass through this code again, as per:
7510 [stmt.dcl]
7512 If the initialization exits by throwing an exception, the
7513 initialization is not complete, so it will be tried again
7514 the next time control enters the declaration.
7516 This process should be thread-safe, too; multiple threads
7517 should not be able to initialize the variable more than
7518 once. */
7520 /* Create the guard variable. */
7521 guard = get_guard (decl);
7523 /* Begin the conditional initialization. */
7524 if_stmt = begin_if_stmt ();
7526 finish_if_stmt_cond (get_guard_cond (guard, thread_guard), if_stmt);
7527 then_clause = begin_compound_stmt (BCS_NO_SCOPE);
7529 if (thread_guard)
7531 tree vfntype = NULL_TREE;
7532 tree acquire_name, release_name, abort_name;
7533 tree acquire_fn, release_fn, abort_fn;
7534 guard_addr = build_address (guard);
7536 acquire_name = get_identifier ("__cxa_guard_acquire");
7537 release_name = get_identifier ("__cxa_guard_release");
7538 abort_name = get_identifier ("__cxa_guard_abort");
7539 acquire_fn = identifier_global_value (acquire_name);
7540 release_fn = identifier_global_value (release_name);
7541 abort_fn = identifier_global_value (abort_name);
7542 if (!acquire_fn)
7543 acquire_fn = push_library_fn
7544 (acquire_name, build_function_type_list (integer_type_node,
7545 TREE_TYPE (guard_addr),
7546 NULL_TREE),
7547 NULL_TREE, ECF_NOTHROW | ECF_LEAF);
7548 if (!release_fn || !abort_fn)
7549 vfntype = build_function_type_list (void_type_node,
7550 TREE_TYPE (guard_addr),
7551 NULL_TREE);
7552 if (!release_fn)
7553 release_fn = push_library_fn (release_name, vfntype, NULL_TREE,
7554 ECF_NOTHROW | ECF_LEAF);
7555 if (!abort_fn)
7556 abort_fn = push_library_fn (abort_name, vfntype, NULL_TREE,
7557 ECF_NOTHROW | ECF_LEAF);
7559 inner_if_stmt = begin_if_stmt ();
7560 finish_if_stmt_cond (build_call_n (acquire_fn, 1, guard_addr),
7561 inner_if_stmt);
7563 inner_then_clause = begin_compound_stmt (BCS_NO_SCOPE);
7564 begin = get_target_expr (boolean_false_node);
7565 flag = TARGET_EXPR_SLOT (begin);
7567 TARGET_EXPR_CLEANUP (begin)
7568 = build3 (COND_EXPR, void_type_node, flag,
7569 void_node,
7570 build_call_n (abort_fn, 1, guard_addr));
7571 CLEANUP_EH_ONLY (begin) = 1;
7573 /* Do the initialization itself. */
7574 init = add_stmt_to_compound (begin, init);
7575 init = add_stmt_to_compound
7576 (init, build2 (MODIFY_EXPR, void_type_node, flag, boolean_true_node));
7577 init = add_stmt_to_compound
7578 (init, build_call_n (release_fn, 1, guard_addr));
7580 else
7581 init = add_stmt_to_compound (init, set_guard (guard));
7583 /* Use atexit to register a function for destroying this static
7584 variable. */
7585 init = add_stmt_to_compound (init, register_dtor_fn (decl));
7587 finish_expr_stmt (init);
7589 if (thread_guard)
7591 finish_compound_stmt (inner_then_clause);
7592 finish_then_clause (inner_if_stmt);
7593 finish_if_stmt (inner_if_stmt);
7596 finish_compound_stmt (then_clause);
7597 finish_then_clause (if_stmt);
7598 finish_if_stmt (if_stmt);
7600 else if (CP_DECL_THREAD_LOCAL_P (decl))
7601 tls_aggregates = tree_cons (init, decl, tls_aggregates);
7602 else
7603 static_aggregates = tree_cons (init, decl, static_aggregates);
7607 /* Make TYPE a complete type based on INITIAL_VALUE.
7608 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
7609 2 if there was no information (in which case assume 0 if DO_DEFAULT),
7610 3 if the initializer list is empty (in pedantic mode). */
7613 cp_complete_array_type (tree *ptype, tree initial_value, bool do_default)
7615 int failure;
7616 tree type, elt_type;
7618 /* Don't get confused by a CONSTRUCTOR for some other type. */
7619 if (initial_value && TREE_CODE (initial_value) == CONSTRUCTOR
7620 && !BRACE_ENCLOSED_INITIALIZER_P (initial_value)
7621 && TREE_CODE (TREE_TYPE (initial_value)) != ARRAY_TYPE)
7622 return 1;
7624 if (initial_value)
7626 unsigned HOST_WIDE_INT i;
7627 tree value;
7629 /* An array of character type can be initialized from a
7630 brace-enclosed string constant.
7632 FIXME: this code is duplicated from reshape_init. Probably
7633 we should just call reshape_init here? */
7634 if (char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (*ptype)))
7635 && TREE_CODE (initial_value) == CONSTRUCTOR
7636 && !vec_safe_is_empty (CONSTRUCTOR_ELTS (initial_value)))
7638 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value);
7639 tree value = (*v)[0].value;
7641 if (TREE_CODE (value) == STRING_CST
7642 && v->length () == 1)
7643 initial_value = value;
7646 /* If any of the elements are parameter packs, we can't actually
7647 complete this type now because the array size is dependent. */
7648 if (TREE_CODE (initial_value) == CONSTRUCTOR)
7650 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (initial_value),
7651 i, value)
7653 if (PACK_EXPANSION_P (value))
7654 return 0;
7659 failure = complete_array_type (ptype, initial_value, do_default);
7661 /* We can create the array before the element type is complete, which
7662 means that we didn't have these two bits set in the original type
7663 either. In completing the type, we are expected to propagate these
7664 bits. See also complete_type which does the same thing for arrays
7665 of fixed size. */
7666 type = *ptype;
7667 if (TYPE_DOMAIN (type))
7669 elt_type = TREE_TYPE (type);
7670 TYPE_NEEDS_CONSTRUCTING (type) = TYPE_NEEDS_CONSTRUCTING (elt_type);
7671 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
7672 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (elt_type);
7675 return failure;
7678 /* As above, but either give an error or reject zero-size arrays, depending
7679 on COMPLAIN. */
7682 cp_complete_array_type_or_error (tree *ptype, tree initial_value,
7683 bool do_default, tsubst_flags_t complain)
7685 int failure;
7686 bool sfinae = !(complain & tf_error);
7687 /* In SFINAE context we can't be lenient about zero-size arrays. */
7688 if (sfinae)
7689 ++pedantic;
7690 failure = cp_complete_array_type (ptype, initial_value, do_default);
7691 if (sfinae)
7692 --pedantic;
7693 if (failure)
7695 if (sfinae)
7696 /* Not an error. */;
7697 else if (failure == 1)
7698 error ("initializer fails to determine size of %qT", *ptype);
7699 else if (failure == 2)
7701 if (do_default)
7702 error ("array size missing in %qT", *ptype);
7704 else if (failure == 3)
7705 error ("zero-size array %qT", *ptype);
7706 *ptype = error_mark_node;
7708 return failure;
7711 /* Return zero if something is declared to be a member of type
7712 CTYPE when in the context of CUR_TYPE. STRING is the error
7713 message to print in that case. Otherwise, quietly return 1. */
7715 static int
7716 member_function_or_else (tree ctype, tree cur_type, enum overload_flags flags)
7718 if (ctype && ctype != cur_type)
7720 if (flags == DTOR_FLAG)
7721 error ("destructor for alien class %qT cannot be a member", ctype);
7722 else
7723 error ("constructor for alien class %qT cannot be a member", ctype);
7724 return 0;
7726 return 1;
7729 /* Subroutine of `grokdeclarator'. */
7731 /* Generate errors possibly applicable for a given set of specifiers.
7732 This is for ARM $7.1.2. */
7734 static void
7735 bad_specifiers (tree object,
7736 enum bad_spec_place type,
7737 int virtualp,
7738 int quals,
7739 int inlinep,
7740 int friendp,
7741 int raises)
7743 switch (type)
7745 case BSP_VAR:
7746 if (virtualp)
7747 error ("%qD declared as a %<virtual%> variable", object);
7748 if (inlinep)
7749 error ("%qD declared as an %<inline%> variable", object);
7750 if (quals)
7751 error ("%<const%> and %<volatile%> function specifiers on "
7752 "%qD invalid in variable declaration", object);
7753 break;
7754 case BSP_PARM:
7755 if (virtualp)
7756 error ("%qD declared as a %<virtual%> parameter", object);
7757 if (inlinep)
7758 error ("%qD declared as an %<inline%> parameter", object);
7759 if (quals)
7760 error ("%<const%> and %<volatile%> function specifiers on "
7761 "%qD invalid in parameter declaration", object);
7762 break;
7763 case BSP_TYPE:
7764 if (virtualp)
7765 error ("%qD declared as a %<virtual%> type", object);
7766 if (inlinep)
7767 error ("%qD declared as an %<inline%> type", object);
7768 if (quals)
7769 error ("%<const%> and %<volatile%> function specifiers on "
7770 "%qD invalid in type declaration", object);
7771 break;
7772 case BSP_FIELD:
7773 if (virtualp)
7774 error ("%qD declared as a %<virtual%> field", object);
7775 if (inlinep)
7776 error ("%qD declared as an %<inline%> field", object);
7777 if (quals)
7778 error ("%<const%> and %<volatile%> function specifiers on "
7779 "%qD invalid in field declaration", object);
7780 break;
7781 default:
7782 gcc_unreachable();
7784 if (friendp)
7785 error ("%q+D declared as a friend", object);
7786 if (raises
7787 && (TREE_CODE (object) == TYPE_DECL
7788 || (!TYPE_PTRFN_P (TREE_TYPE (object))
7789 && !TYPE_REFFN_P (TREE_TYPE (object))
7790 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (object)))))
7791 error ("%q+D declared with an exception specification", object);
7794 /* DECL is a member function or static data member and is presently
7795 being defined. Check that the definition is taking place in a
7796 valid namespace. */
7798 static void
7799 check_class_member_definition_namespace (tree decl)
7801 /* These checks only apply to member functions and static data
7802 members. */
7803 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
7804 /* We check for problems with specializations in pt.c in
7805 check_specialization_namespace, where we can issue better
7806 diagnostics. */
7807 if (processing_specialization)
7808 return;
7809 /* There are no restrictions on the placement of
7810 explicit instantiations. */
7811 if (processing_explicit_instantiation)
7812 return;
7813 /* [class.mfct]
7815 A member function definition that appears outside of the
7816 class definition shall appear in a namespace scope enclosing
7817 the class definition.
7819 [class.static.data]
7821 The definition for a static data member shall appear in a
7822 namespace scope enclosing the member's class definition. */
7823 if (!is_ancestor (current_namespace, DECL_CONTEXT (decl)))
7824 permerror (input_location, "definition of %qD is not in namespace enclosing %qT",
7825 decl, DECL_CONTEXT (decl));
7828 /* Build a PARM_DECL for the "this" parameter. TYPE is the
7829 METHOD_TYPE for a non-static member function; QUALS are the
7830 cv-qualifiers that apply to the function. */
7832 tree
7833 build_this_parm (tree type, cp_cv_quals quals)
7835 tree this_type;
7836 tree qual_type;
7837 tree parm;
7838 cp_cv_quals this_quals;
7840 if (CLASS_TYPE_P (type))
7842 this_type
7843 = cp_build_qualified_type (type, quals & ~TYPE_QUAL_RESTRICT);
7844 this_type = build_pointer_type (this_type);
7846 else
7847 this_type = type_of_this_parm (type);
7848 /* The `this' parameter is implicitly `const'; it cannot be
7849 assigned to. */
7850 this_quals = (quals & TYPE_QUAL_RESTRICT) | TYPE_QUAL_CONST;
7851 qual_type = cp_build_qualified_type (this_type, this_quals);
7852 parm = build_artificial_parm (this_identifier, qual_type);
7853 cp_apply_type_quals_to_decl (this_quals, parm);
7854 return parm;
7857 /* DECL is a static member function. Complain if it was declared
7858 with function-cv-quals. */
7860 static void
7861 check_static_quals (tree decl, cp_cv_quals quals)
7863 if (quals != TYPE_UNQUALIFIED)
7864 error ("static member function %q#D declared with type qualifiers",
7865 decl);
7868 // Check that FN takes no arguments and returns bool.
7869 static void
7870 check_concept_fn (tree fn)
7872 // A constraint is nullary.
7873 if (DECL_ARGUMENTS (fn))
7874 error ("concept %q#D declared with function parameters", fn);
7876 // The declared return type of the concept shall be bool, and
7877 // it shall not be deduced from it definition.
7878 tree type = TREE_TYPE (TREE_TYPE (fn));
7879 if (is_auto (type))
7880 error ("concept %q#D declared with a deduced return type", fn);
7881 else if (type != boolean_type_node)
7882 error ("concept %q#D with non-%<bool%> return type %qT", fn, type);
7885 /* Helper function. Replace the temporary this parameter injected
7886 during cp_finish_omp_declare_simd with the real this parameter. */
7888 static tree
7889 declare_simd_adjust_this (tree *tp, int *walk_subtrees, void *data)
7891 tree this_parm = (tree) data;
7892 if (TREE_CODE (*tp) == PARM_DECL
7893 && DECL_NAME (*tp) == this_identifier
7894 && *tp != this_parm)
7895 *tp = this_parm;
7896 else if (TYPE_P (*tp))
7897 *walk_subtrees = 0;
7898 return NULL_TREE;
7901 /* CTYPE is class type, or null if non-class.
7902 TYPE is type this FUNCTION_DECL should have, either FUNCTION_TYPE
7903 or METHOD_TYPE.
7904 DECLARATOR is the function's name.
7905 PARMS is a chain of PARM_DECLs for the function.
7906 VIRTUALP is truthvalue of whether the function is virtual or not.
7907 FLAGS are to be passed through to `grokclassfn'.
7908 QUALS are qualifiers indicating whether the function is `const'
7909 or `volatile'.
7910 RAISES is a list of exceptions that this function can raise.
7911 CHECK is 1 if we must find this method in CTYPE, 0 if we should
7912 not look, and -1 if we should not call `grokclassfn' at all.
7914 SFK is the kind of special function (if any) for the new function.
7916 Returns `NULL_TREE' if something goes wrong, after issuing
7917 applicable error messages. */
7919 static tree
7920 grokfndecl (tree ctype,
7921 tree type,
7922 tree declarator,
7923 tree parms,
7924 tree orig_declarator,
7925 tree decl_reqs,
7926 int virtualp,
7927 enum overload_flags flags,
7928 cp_cv_quals quals,
7929 cp_ref_qualifier rqual,
7930 tree raises,
7931 int check,
7932 int friendp,
7933 int publicp,
7934 int inlinep,
7935 bool deletedp,
7936 special_function_kind sfk,
7937 bool funcdef_flag,
7938 int template_count,
7939 tree in_namespace,
7940 tree* attrlist,
7941 location_t location)
7943 tree decl;
7944 int staticp = ctype && TREE_CODE (type) == FUNCTION_TYPE;
7945 tree t;
7947 // Was the concept specifier present?
7948 bool concept_p = inlinep & 4;
7950 // Concept declarations must have a corresponding definition.
7951 if (concept_p && !funcdef_flag)
7953 error ("concept %qD has no definition", declarator);
7954 return NULL_TREE;
7957 if (rqual)
7958 type = build_ref_qualified_type (type, rqual);
7959 if (raises)
7960 type = build_exception_variant (type, raises);
7962 decl = build_lang_decl (FUNCTION_DECL, declarator, type);
7964 /* Set the constraints on the declaration. */
7965 if (flag_concepts)
7967 tree tmpl_reqs = NULL_TREE;
7968 if (processing_template_decl > template_class_depth (ctype))
7969 tmpl_reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
7971 /* Adjust the required expression into a constraint. */
7972 if (decl_reqs)
7973 decl_reqs = normalize_expression (decl_reqs);
7975 tree ci = build_constraints (tmpl_reqs, decl_reqs);
7976 set_constraints (decl, ci);
7979 /* If we have an explicit location, use it, otherwise use whatever
7980 build_lang_decl used (probably input_location). */
7981 if (location != UNKNOWN_LOCATION)
7982 DECL_SOURCE_LOCATION (decl) = location;
7984 if (TREE_CODE (type) == METHOD_TYPE)
7986 tree parm;
7987 parm = build_this_parm (type, quals);
7988 DECL_CHAIN (parm) = parms;
7989 parms = parm;
7991 /* Allocate space to hold the vptr bit if needed. */
7992 SET_DECL_ALIGN (decl, MINIMUM_METHOD_BOUNDARY);
7994 DECL_ARGUMENTS (decl) = parms;
7995 for (t = parms; t; t = DECL_CHAIN (t))
7996 DECL_CONTEXT (t) = decl;
7997 /* Propagate volatile out from type to decl. */
7998 if (TYPE_VOLATILE (type))
7999 TREE_THIS_VOLATILE (decl) = 1;
8001 /* Setup decl according to sfk. */
8002 switch (sfk)
8004 case sfk_constructor:
8005 case sfk_copy_constructor:
8006 case sfk_move_constructor:
8007 DECL_CONSTRUCTOR_P (decl) = 1;
8008 break;
8009 case sfk_destructor:
8010 DECL_DESTRUCTOR_P (decl) = 1;
8011 break;
8012 default:
8013 break;
8016 if (friendp
8017 && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)
8019 if (funcdef_flag)
8020 error
8021 ("defining explicit specialization %qD in friend declaration",
8022 orig_declarator);
8023 else
8025 tree fns = TREE_OPERAND (orig_declarator, 0);
8026 tree args = TREE_OPERAND (orig_declarator, 1);
8028 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8030 /* Something like `template <class T> friend void f<T>()'. */
8031 error ("invalid use of template-id %qD in declaration "
8032 "of primary template",
8033 orig_declarator);
8034 return NULL_TREE;
8038 /* A friend declaration of the form friend void f<>(). Record
8039 the information in the TEMPLATE_ID_EXPR. */
8040 SET_DECL_IMPLICIT_INSTANTIATION (decl);
8042 gcc_assert (identifier_p (fns) || TREE_CODE (fns) == OVERLOAD);
8043 DECL_TEMPLATE_INFO (decl) = build_template_info (fns, args);
8045 for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t))
8046 if (TREE_PURPOSE (t)
8047 && TREE_CODE (TREE_PURPOSE (t)) == DEFAULT_ARG)
8049 error ("default arguments are not allowed in declaration "
8050 "of friend template specialization %qD",
8051 decl);
8052 return NULL_TREE;
8055 if (inlinep & 1)
8057 error ("%<inline%> is not allowed in declaration of friend "
8058 "template specialization %qD",
8059 decl);
8060 return NULL_TREE;
8065 /* If this decl has namespace scope, set that up. */
8066 if (in_namespace)
8067 set_decl_namespace (decl, in_namespace, friendp);
8068 else if (!ctype)
8069 DECL_CONTEXT (decl) = FROB_CONTEXT (current_decl_namespace ());
8071 /* `main' and builtins have implicit 'C' linkage. */
8072 if ((MAIN_NAME_P (declarator)
8073 || (IDENTIFIER_LENGTH (declarator) > 10
8074 && IDENTIFIER_POINTER (declarator)[0] == '_'
8075 && IDENTIFIER_POINTER (declarator)[1] == '_'
8076 && strncmp (IDENTIFIER_POINTER (declarator)+2, "builtin_", 8) == 0)
8077 || (targetcm.cxx_implicit_extern_c
8078 && targetcm.cxx_implicit_extern_c(IDENTIFIER_POINTER (declarator))))
8079 && current_lang_name == lang_name_cplusplus
8080 && ctype == NULL_TREE
8081 && DECL_FILE_SCOPE_P (decl))
8082 SET_DECL_LANGUAGE (decl, lang_c);
8084 /* Should probably propagate const out from type to decl I bet (mrs). */
8085 if (staticp)
8087 DECL_STATIC_FUNCTION_P (decl) = 1;
8088 DECL_CONTEXT (decl) = ctype;
8091 if (deletedp)
8092 DECL_DELETED_FN (decl) = 1;
8094 if (ctype)
8096 DECL_CONTEXT (decl) = ctype;
8097 if (funcdef_flag)
8098 check_class_member_definition_namespace (decl);
8101 if (ctype == NULL_TREE && DECL_MAIN_P (decl))
8103 if (PROCESSING_REAL_TEMPLATE_DECL_P())
8104 error ("cannot declare %<::main%> to be a template");
8105 if (inlinep & 1)
8106 error ("cannot declare %<::main%> to be inline");
8107 if (inlinep & 2)
8108 error ("cannot declare %<::main%> to be constexpr");
8109 if (!publicp)
8110 error ("cannot declare %<::main%> to be static");
8111 inlinep = 0;
8112 publicp = 1;
8115 /* Members of anonymous types and local classes have no linkage; make
8116 them internal. If a typedef is made later, this will be changed. */
8117 if (ctype && (!TREE_PUBLIC (TYPE_MAIN_DECL (ctype))
8118 || decl_function_context (TYPE_MAIN_DECL (ctype))))
8119 publicp = 0;
8121 if (publicp && cxx_dialect == cxx98)
8123 /* [basic.link]: A name with no linkage (notably, the name of a class
8124 or enumeration declared in a local scope) shall not be used to
8125 declare an entity with linkage.
8127 DR 757 relaxes this restriction for C++0x. */
8128 no_linkage_error (decl);
8131 TREE_PUBLIC (decl) = publicp;
8132 if (! publicp)
8134 DECL_INTERFACE_KNOWN (decl) = 1;
8135 DECL_NOT_REALLY_EXTERN (decl) = 1;
8138 /* If the declaration was declared inline, mark it as such. */
8139 if (inlinep)
8141 DECL_DECLARED_INLINE_P (decl) = 1;
8142 if (publicp)
8143 DECL_COMDAT (decl) = 1;
8145 if (inlinep & 2)
8146 DECL_DECLARED_CONSTEXPR_P (decl) = true;
8148 // If the concept declaration specifier was found, check
8149 // that the declaration satisfies the necessary requirements.
8150 if (concept_p)
8152 DECL_DECLARED_CONCEPT_P (decl) = true;
8153 check_concept_fn (decl);
8156 DECL_EXTERNAL (decl) = 1;
8157 if (TREE_CODE (type) == FUNCTION_TYPE)
8159 if (quals || rqual)
8160 TREE_TYPE (decl) = apply_memfn_quals (TREE_TYPE (decl),
8161 TYPE_UNQUALIFIED,
8162 REF_QUAL_NONE);
8164 if (quals)
8166 error (ctype
8167 ? G_("static member function %qD cannot have cv-qualifier")
8168 : G_("non-member function %qD cannot have cv-qualifier"),
8169 decl);
8170 quals = TYPE_UNQUALIFIED;
8173 if (rqual)
8175 error (ctype
8176 ? G_("static member function %qD cannot have ref-qualifier")
8177 : G_("non-member function %qD cannot have ref-qualifier"),
8178 decl);
8179 rqual = REF_QUAL_NONE;
8183 if (deduction_guide_p (decl))
8185 if (!DECL_NAMESPACE_SCOPE_P (decl))
8187 error_at (location, "deduction guide %qD must be declared at "
8188 "namespace scope", decl);
8189 return NULL_TREE;
8191 tree type = TREE_TYPE (DECL_NAME (decl));
8192 if (CP_DECL_CONTEXT (decl) != CP_TYPE_CONTEXT (type))
8194 error_at (location, "deduction guide %qD must be declared in the "
8195 "same scope as %qT", decl, type);
8196 inform (location_of (type), " declared here");
8197 return NULL_TREE;
8199 if (funcdef_flag)
8200 error_at (location,
8201 "deduction guide %qD must not have a function body", decl);
8203 else if (IDENTIFIER_OPNAME_P (DECL_NAME (decl))
8204 && !grok_op_properties (decl, /*complain=*/true))
8205 return NULL_TREE;
8206 else if (UDLIT_OPER_P (DECL_NAME (decl)))
8208 bool long_long_unsigned_p;
8209 bool long_double_p;
8210 const char *suffix = NULL;
8211 /* [over.literal]/6: Literal operators shall not have C linkage. */
8212 if (DECL_LANGUAGE (decl) == lang_c)
8214 error ("literal operator with C linkage");
8215 return NULL_TREE;
8218 if (DECL_NAMESPACE_SCOPE_P (decl))
8220 if (!check_literal_operator_args (decl, &long_long_unsigned_p,
8221 &long_double_p))
8223 error ("%qD has invalid argument list", decl);
8224 return NULL_TREE;
8227 suffix = UDLIT_OP_SUFFIX (DECL_NAME (decl));
8228 if (long_long_unsigned_p)
8230 if (cpp_interpret_int_suffix (parse_in, suffix, strlen (suffix)))
8231 warning (0, "integer suffix %<%s%>"
8232 " shadowed by implementation", suffix);
8234 else if (long_double_p)
8236 if (cpp_interpret_float_suffix (parse_in, suffix, strlen (suffix)))
8237 warning (0, "floating point suffix %<%s%>"
8238 " shadowed by implementation", suffix);
8241 else
8243 error ("%qD must be a non-member function", decl);
8244 return NULL_TREE;
8248 if (funcdef_flag)
8249 /* Make the init_value nonzero so pushdecl knows this is not
8250 tentative. error_mark_node is replaced later with the BLOCK. */
8251 DECL_INITIAL (decl) = error_mark_node;
8253 if (TYPE_NOTHROW_P (type) || nothrow_libfn_p (decl))
8254 TREE_NOTHROW (decl) = 1;
8256 if (flag_openmp || flag_openmp_simd || flag_cilkplus)
8258 /* Adjust "omp declare simd" attributes. */
8259 tree ods = lookup_attribute ("omp declare simd", *attrlist);
8260 if (ods)
8262 tree attr;
8263 for (attr = ods; attr;
8264 attr = lookup_attribute ("omp declare simd", TREE_CHAIN (attr)))
8266 if (TREE_CODE (type) == METHOD_TYPE)
8267 walk_tree (&TREE_VALUE (attr), declare_simd_adjust_this,
8268 DECL_ARGUMENTS (decl), NULL);
8269 if (TREE_VALUE (attr) != NULL_TREE)
8271 tree cl = TREE_VALUE (TREE_VALUE (attr));
8272 cl = c_omp_declare_simd_clauses_to_numbers
8273 (DECL_ARGUMENTS (decl), cl);
8274 if (cl)
8275 TREE_VALUE (TREE_VALUE (attr)) = cl;
8276 else
8277 TREE_VALUE (attr) = NULL_TREE;
8283 /* Caller will do the rest of this. */
8284 if (check < 0)
8285 return decl;
8287 if (ctype != NULL_TREE)
8288 grokclassfn (ctype, decl, flags);
8290 /* 12.4/3 */
8291 if (cxx_dialect >= cxx11
8292 && DECL_DESTRUCTOR_P (decl)
8293 && !TYPE_BEING_DEFINED (DECL_CONTEXT (decl))
8294 && !processing_template_decl)
8295 deduce_noexcept_on_destructor (decl);
8297 decl = check_explicit_specialization (orig_declarator, decl,
8298 template_count,
8299 2 * funcdef_flag +
8300 4 * (friendp != 0) +
8301 8 * concept_p);
8302 if (decl == error_mark_node)
8303 return NULL_TREE;
8305 if (DECL_STATIC_FUNCTION_P (decl))
8306 check_static_quals (decl, quals);
8308 if (attrlist)
8310 cplus_decl_attributes (&decl, *attrlist, 0);
8311 *attrlist = NULL_TREE;
8314 /* Check main's type after attributes have been applied. */
8315 if (ctype == NULL_TREE && DECL_MAIN_P (decl))
8317 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
8318 integer_type_node))
8320 tree oldtypeargs = TYPE_ARG_TYPES (TREE_TYPE (decl));
8321 tree newtype;
8322 error ("%<::main%> must return %<int%>");
8323 newtype = build_function_type (integer_type_node, oldtypeargs);
8324 TREE_TYPE (decl) = newtype;
8326 if (warn_main)
8327 check_main_parameter_types (decl);
8330 if (ctype != NULL_TREE && check)
8332 tree old_decl = check_classfn (ctype, decl,
8333 (processing_template_decl
8334 > template_class_depth (ctype))
8335 ? current_template_parms
8336 : NULL_TREE);
8338 if (old_decl == error_mark_node)
8339 return NULL_TREE;
8341 if (old_decl)
8343 tree ok;
8344 tree pushed_scope;
8346 if (TREE_CODE (old_decl) == TEMPLATE_DECL)
8347 /* Because grokfndecl is always supposed to return a
8348 FUNCTION_DECL, we pull out the DECL_TEMPLATE_RESULT
8349 here. We depend on our callers to figure out that its
8350 really a template that's being returned. */
8351 old_decl = DECL_TEMPLATE_RESULT (old_decl);
8353 if (DECL_STATIC_FUNCTION_P (old_decl)
8354 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
8356 /* Remove the `this' parm added by grokclassfn. */
8357 revert_static_member_fn (decl);
8358 check_static_quals (decl, quals);
8360 if (DECL_ARTIFICIAL (old_decl))
8362 error ("definition of implicitly-declared %qD", old_decl);
8363 return NULL_TREE;
8365 else if (DECL_DEFAULTED_FN (old_decl))
8367 error ("definition of explicitly-defaulted %q+D", decl);
8368 inform (DECL_SOURCE_LOCATION (old_decl),
8369 "%q#D explicitly defaulted here", old_decl);
8370 return NULL_TREE;
8373 /* Since we've smashed OLD_DECL to its
8374 DECL_TEMPLATE_RESULT, we must do the same to DECL. */
8375 if (TREE_CODE (decl) == TEMPLATE_DECL)
8376 decl = DECL_TEMPLATE_RESULT (decl);
8378 /* Attempt to merge the declarations. This can fail, in
8379 the case of some invalid specialization declarations. */
8380 pushed_scope = push_scope (ctype);
8381 ok = duplicate_decls (decl, old_decl, friendp);
8382 if (pushed_scope)
8383 pop_scope (pushed_scope);
8384 if (!ok)
8386 error ("no %q#D member function declared in class %qT",
8387 decl, ctype);
8388 return NULL_TREE;
8390 if (ok == error_mark_node)
8391 return NULL_TREE;
8392 return old_decl;
8396 if (DECL_CONSTRUCTOR_P (decl) && !grok_ctor_properties (ctype, decl))
8397 return NULL_TREE;
8399 if (ctype == NULL_TREE || check)
8400 return decl;
8402 if (virtualp)
8403 DECL_VIRTUAL_P (decl) = 1;
8405 return decl;
8408 /* decl is a FUNCTION_DECL.
8409 specifiers are the parsed virt-specifiers.
8411 Set flags to reflect the virt-specifiers.
8413 Returns decl. */
8415 static tree
8416 set_virt_specifiers (tree decl, cp_virt_specifiers specifiers)
8418 if (decl == NULL_TREE)
8419 return decl;
8420 if (specifiers & VIRT_SPEC_OVERRIDE)
8421 DECL_OVERRIDE_P (decl) = 1;
8422 if (specifiers & VIRT_SPEC_FINAL)
8423 DECL_FINAL_P (decl) = 1;
8424 return decl;
8427 /* DECL is a VAR_DECL for a static data member. Set flags to reflect
8428 the linkage that DECL will receive in the object file. */
8430 static void
8431 set_linkage_for_static_data_member (tree decl)
8433 /* A static data member always has static storage duration and
8434 external linkage. Note that static data members are forbidden in
8435 local classes -- the only situation in which a class has
8436 non-external linkage. */
8437 TREE_PUBLIC (decl) = 1;
8438 TREE_STATIC (decl) = 1;
8439 /* For non-template classes, static data members are always put
8440 out in exactly those files where they are defined, just as
8441 with ordinary namespace-scope variables. */
8442 if (!processing_template_decl)
8443 DECL_INTERFACE_KNOWN (decl) = 1;
8446 /* Create a VAR_DECL named NAME with the indicated TYPE.
8448 If SCOPE is non-NULL, it is the class type or namespace containing
8449 the variable. If SCOPE is NULL, the variable should is created in
8450 the innermost enclosing scope. */
8452 static tree
8453 grokvardecl (tree type,
8454 tree name,
8455 tree orig_declarator,
8456 const cp_decl_specifier_seq *declspecs,
8457 int initialized,
8458 int flags,
8459 int template_count,
8460 tree scope)
8462 tree decl;
8463 tree explicit_scope;
8465 gcc_assert (!name || identifier_p (name));
8467 bool constp = flags&1;
8468 bool conceptp = flags&2;
8470 /* Compute the scope in which to place the variable, but remember
8471 whether or not that scope was explicitly specified by the user. */
8472 explicit_scope = scope;
8473 if (!scope)
8475 /* An explicit "extern" specifier indicates a namespace-scope
8476 variable. */
8477 if (declspecs->storage_class == sc_extern)
8478 scope = current_decl_namespace ();
8479 else if (!at_function_scope_p ())
8480 scope = current_scope ();
8483 if (scope
8484 && (/* If the variable is a namespace-scope variable declared in a
8485 template, we need DECL_LANG_SPECIFIC. */
8486 (TREE_CODE (scope) == NAMESPACE_DECL && processing_template_decl)
8487 /* Similarly for namespace-scope variables with language linkage
8488 other than C++. */
8489 || (TREE_CODE (scope) == NAMESPACE_DECL
8490 && current_lang_name != lang_name_cplusplus)
8491 /* Similarly for static data members. */
8492 || TYPE_P (scope)
8493 /* Similarly for explicit specializations. */
8494 || (orig_declarator
8495 && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)))
8496 decl = build_lang_decl (VAR_DECL, name, type);
8497 else
8498 decl = build_decl (input_location, VAR_DECL, name, type);
8500 if (explicit_scope && TREE_CODE (explicit_scope) == NAMESPACE_DECL)
8501 set_decl_namespace (decl, explicit_scope, 0);
8502 else
8503 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
8505 if (declspecs->storage_class == sc_extern)
8507 DECL_THIS_EXTERN (decl) = 1;
8508 DECL_EXTERNAL (decl) = !initialized;
8511 if (DECL_CLASS_SCOPE_P (decl))
8513 set_linkage_for_static_data_member (decl);
8514 /* This function is only called with out-of-class definitions. */
8515 DECL_EXTERNAL (decl) = 0;
8516 check_class_member_definition_namespace (decl);
8518 /* At top level, either `static' or no s.c. makes a definition
8519 (perhaps tentative), and absence of `static' makes it public. */
8520 else if (toplevel_bindings_p ())
8522 TREE_PUBLIC (decl) = (declspecs->storage_class != sc_static
8523 && (DECL_THIS_EXTERN (decl) || ! constp));
8524 TREE_STATIC (decl) = ! DECL_EXTERNAL (decl);
8526 /* Not at top level, only `static' makes a static definition. */
8527 else
8529 TREE_STATIC (decl) = declspecs->storage_class == sc_static;
8530 TREE_PUBLIC (decl) = DECL_EXTERNAL (decl);
8533 if (decl_spec_seq_has_spec_p (declspecs, ds_thread))
8535 if (DECL_EXTERNAL (decl) || TREE_STATIC (decl))
8537 CP_DECL_THREAD_LOCAL_P (decl) = true;
8538 if (!processing_template_decl)
8539 set_decl_tls_model (decl, decl_default_tls_model (decl));
8541 if (declspecs->gnu_thread_keyword_p)
8542 SET_DECL_GNU_TLS_P (decl);
8545 /* If the type of the decl has no linkage, make sure that we'll
8546 notice that in mark_used. */
8547 if (cxx_dialect > cxx98
8548 && decl_linkage (decl) != lk_none
8549 && DECL_LANG_SPECIFIC (decl) == NULL
8550 && !DECL_EXTERN_C_P (decl)
8551 && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
8552 retrofit_lang_decl (decl);
8554 if (TREE_PUBLIC (decl))
8556 /* [basic.link]: A name with no linkage (notably, the name of a class
8557 or enumeration declared in a local scope) shall not be used to
8558 declare an entity with linkage.
8560 DR 757 relaxes this restriction for C++0x. */
8561 if (cxx_dialect < cxx11)
8562 no_linkage_error (decl);
8564 else
8565 DECL_INTERFACE_KNOWN (decl) = 1;
8567 if (DECL_NAME (decl)
8568 && MAIN_NAME_P (DECL_NAME (decl))
8569 && scope == global_namespace)
8570 error ("cannot declare %<::main%> to be a global variable");
8572 /* Check that the variable can be safely declared as a concept.
8573 Note that this also forbids explicit specializations. */
8574 if (conceptp)
8576 if (!processing_template_decl)
8578 error ("a non-template variable cannot be %<concept%>");
8579 return NULL_TREE;
8581 else
8582 DECL_DECLARED_CONCEPT_P (decl) = true;
8583 if (!same_type_ignoring_top_level_qualifiers_p (type, boolean_type_node))
8584 error_at (declspecs->locations[ds_type_spec],
8585 "concept must have type %<bool%>");
8587 else if (flag_concepts
8588 && processing_template_decl > template_class_depth (scope))
8590 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
8591 tree ci = build_constraints (reqs, NULL_TREE);
8592 set_constraints (decl, ci);
8595 // Handle explicit specializations and instantiations of variable templates.
8596 if (orig_declarator)
8597 decl = check_explicit_specialization (orig_declarator, decl,
8598 template_count, conceptp * 8);
8600 return decl != error_mark_node ? decl : NULL_TREE;
8603 /* Create and return a canonical pointer to member function type, for
8604 TYPE, which is a POINTER_TYPE to a METHOD_TYPE. */
8606 tree
8607 build_ptrmemfunc_type (tree type)
8609 tree field, fields;
8610 tree t;
8612 if (type == error_mark_node)
8613 return type;
8615 /* Make sure that we always have the unqualified pointer-to-member
8616 type first. */
8617 if (cp_cv_quals quals = cp_type_quals (type))
8619 tree unqual = build_ptrmemfunc_type (TYPE_MAIN_VARIANT (type));
8620 return cp_build_qualified_type (unqual, quals);
8623 /* If a canonical type already exists for this type, use it. We use
8624 this method instead of type_hash_canon, because it only does a
8625 simple equality check on the list of field members. */
8627 if ((t = TYPE_GET_PTRMEMFUNC_TYPE (type)))
8628 return t;
8630 t = make_node (RECORD_TYPE);
8632 /* Let the front end know this is a pointer to member function. */
8633 TYPE_PTRMEMFUNC_FLAG (t) = 1;
8635 field = build_decl (input_location, FIELD_DECL, pfn_identifier, type);
8636 fields = field;
8638 field = build_decl (input_location, FIELD_DECL, delta_identifier,
8639 delta_type_node);
8640 DECL_CHAIN (field) = fields;
8641 fields = field;
8643 finish_builtin_struct (t, "__ptrmemfunc_type", fields, ptr_type_node);
8645 /* Zap out the name so that the back end will give us the debugging
8646 information for this anonymous RECORD_TYPE. */
8647 TYPE_NAME (t) = NULL_TREE;
8649 /* Cache this pointer-to-member type so that we can find it again
8650 later. */
8651 TYPE_SET_PTRMEMFUNC_TYPE (type, t);
8653 if (TYPE_STRUCTURAL_EQUALITY_P (type))
8654 SET_TYPE_STRUCTURAL_EQUALITY (t);
8655 else if (TYPE_CANONICAL (type) != type)
8656 TYPE_CANONICAL (t) = build_ptrmemfunc_type (TYPE_CANONICAL (type));
8658 return t;
8661 /* Create and return a pointer to data member type. */
8663 tree
8664 build_ptrmem_type (tree class_type, tree member_type)
8666 if (TREE_CODE (member_type) == METHOD_TYPE)
8668 cp_cv_quals quals = type_memfn_quals (member_type);
8669 cp_ref_qualifier rqual = type_memfn_rqual (member_type);
8670 member_type = build_memfn_type (member_type, class_type, quals, rqual);
8671 return build_ptrmemfunc_type (build_pointer_type (member_type));
8673 else
8675 gcc_assert (TREE_CODE (member_type) != FUNCTION_TYPE);
8676 return build_offset_type (class_type, member_type);
8680 /* DECL is a VAR_DECL defined in-class, whose TYPE is also given.
8681 Check to see that the definition is valid. Issue appropriate error
8682 messages. Return 1 if the definition is particularly bad, or 0
8683 otherwise. */
8685 static int
8686 check_static_variable_definition (tree decl, tree type)
8688 /* Avoid redundant diagnostics on out-of-class definitions. */
8689 if (!current_class_type || !TYPE_BEING_DEFINED (current_class_type))
8690 return 0;
8691 /* Can't check yet if we don't know the type. */
8692 if (dependent_type_p (type))
8693 return 0;
8694 /* If DECL is declared constexpr, we'll do the appropriate checks
8695 in check_initializer. */
8696 if (DECL_P (decl) && DECL_DECLARED_CONSTEXPR_P (decl))
8697 return 0;
8698 else if (cxx_dialect >= cxx11 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
8700 if (!COMPLETE_TYPE_P (type))
8701 error_at (DECL_SOURCE_LOCATION (decl),
8702 "in-class initialization of static data member %q#D of "
8703 "incomplete type", decl);
8704 else if (literal_type_p (type))
8705 permerror (DECL_SOURCE_LOCATION (decl),
8706 "%<constexpr%> needed for in-class initialization of "
8707 "static data member %q#D of non-integral type", decl);
8708 else
8709 error_at (DECL_SOURCE_LOCATION (decl),
8710 "in-class initialization of static data member %q#D of "
8711 "non-literal type", decl);
8712 return 1;
8715 /* Motion 10 at San Diego: If a static const integral data member is
8716 initialized with an integral constant expression, the initializer
8717 may appear either in the declaration (within the class), or in
8718 the definition, but not both. If it appears in the class, the
8719 member is a member constant. The file-scope definition is always
8720 required. */
8721 if (!ARITHMETIC_TYPE_P (type) && TREE_CODE (type) != ENUMERAL_TYPE)
8723 error_at (DECL_SOURCE_LOCATION (decl),
8724 "invalid in-class initialization of static data member "
8725 "of non-integral type %qT",
8726 type);
8727 return 1;
8729 else if (!CP_TYPE_CONST_P (type))
8730 error_at (DECL_SOURCE_LOCATION (decl),
8731 "ISO C++ forbids in-class initialization of non-const "
8732 "static member %qD",
8733 decl);
8734 else if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type))
8735 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
8736 "ISO C++ forbids initialization of member constant "
8737 "%qD of non-integral type %qT", decl, type);
8739 return 0;
8742 /* *expr_p is part of the TYPE_SIZE of a variably-sized array. If any
8743 SAVE_EXPRs in *expr_p wrap expressions with side-effects, break those
8744 expressions out into temporary variables so that walk_tree doesn't
8745 step into them (c++/15764). */
8747 static tree
8748 stabilize_save_expr_r (tree *expr_p, int *walk_subtrees, void *data)
8750 hash_set<tree> *pset = (hash_set<tree> *)data;
8751 tree expr = *expr_p;
8752 if (TREE_CODE (expr) == SAVE_EXPR)
8754 tree op = TREE_OPERAND (expr, 0);
8755 cp_walk_tree (&op, stabilize_save_expr_r, data, pset);
8756 if (TREE_SIDE_EFFECTS (op))
8757 TREE_OPERAND (expr, 0) = get_temp_regvar (TREE_TYPE (op), op);
8758 *walk_subtrees = 0;
8760 else if (!EXPR_P (expr) || !TREE_SIDE_EFFECTS (expr))
8761 *walk_subtrees = 0;
8762 return NULL;
8765 /* Entry point for the above. */
8767 static void
8768 stabilize_vla_size (tree size)
8770 hash_set<tree> pset;
8771 /* Break out any function calls into temporary variables. */
8772 cp_walk_tree (&size, stabilize_save_expr_r, &pset, &pset);
8775 /* Reduce a SIZEOF_EXPR to its value. */
8777 tree
8778 fold_sizeof_expr (tree t)
8780 tree r;
8781 if (SIZEOF_EXPR_TYPE_P (t))
8782 r = cxx_sizeof_or_alignof_type (TREE_TYPE (TREE_OPERAND (t, 0)),
8783 SIZEOF_EXPR, false);
8784 else if (TYPE_P (TREE_OPERAND (t, 0)))
8785 r = cxx_sizeof_or_alignof_type (TREE_OPERAND (t, 0), SIZEOF_EXPR,
8786 false);
8787 else
8788 r = cxx_sizeof_or_alignof_expr (TREE_OPERAND (t, 0), SIZEOF_EXPR,
8789 false);
8790 if (r == error_mark_node)
8791 r = size_one_node;
8792 return r;
8795 /* Given the SIZE (i.e., number of elements) in an array, compute
8796 an appropriate index type for the array. If non-NULL, NAME is
8797 the name of the entity being declared. */
8799 tree
8800 compute_array_index_type (tree name, tree size, tsubst_flags_t complain)
8802 tree itype;
8803 tree osize = size;
8805 if (error_operand_p (size))
8806 return error_mark_node;
8808 if (!type_dependent_expression_p (size))
8810 tree type = TREE_TYPE (size);
8812 mark_rvalue_use (size);
8814 if (cxx_dialect < cxx11 && TREE_CODE (size) == NOP_EXPR
8815 && TREE_SIDE_EFFECTS (size))
8816 /* In C++98, we mark a non-constant array bound with a magic
8817 NOP_EXPR with TREE_SIDE_EFFECTS; don't fold in that case. */;
8818 else
8820 size = instantiate_non_dependent_expr_sfinae (size, complain);
8822 if (CLASS_TYPE_P (type)
8823 && CLASSTYPE_LITERAL_P (type))
8825 size = build_expr_type_conversion (WANT_INT, size, true);
8826 if (!size)
8828 if (!(complain & tf_error))
8829 return error_mark_node;
8830 if (name)
8831 error ("size of array %qD has non-integral type %qT",
8832 name, type);
8833 else
8834 error ("size of array has non-integral type %qT", type);
8835 size = integer_one_node;
8837 if (size == error_mark_node)
8838 return error_mark_node;
8839 type = TREE_TYPE (size);
8842 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
8843 size = maybe_constant_value (size);
8845 if (!TREE_CONSTANT (size))
8846 size = osize;
8849 if (error_operand_p (size))
8850 return error_mark_node;
8852 /* The array bound must be an integer type. */
8853 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
8855 if (!(complain & tf_error))
8856 return error_mark_node;
8857 if (name)
8858 error ("size of array %qD has non-integral type %qT", name, type);
8859 else
8860 error ("size of array has non-integral type %qT", type);
8861 size = integer_one_node;
8862 type = TREE_TYPE (size);
8866 /* A type is dependent if it is...an array type constructed from any
8867 dependent type or whose size is specified by a constant expression
8868 that is value-dependent. */
8869 /* We can only call value_dependent_expression_p on integral constant
8870 expressions; treat non-constant expressions as dependent, too. */
8871 if (processing_template_decl
8872 && (type_dependent_expression_p (size)
8873 || !TREE_CONSTANT (size) || value_dependent_expression_p (size)))
8875 /* We cannot do any checking for a SIZE that isn't known to be
8876 constant. Just build the index type and mark that it requires
8877 structural equality checks. */
8878 itype = build_index_type (build_min (MINUS_EXPR, sizetype,
8879 size, size_one_node));
8880 TYPE_DEPENDENT_P (itype) = 1;
8881 TYPE_DEPENDENT_P_VALID (itype) = 1;
8882 SET_TYPE_STRUCTURAL_EQUALITY (itype);
8883 return itype;
8886 if (TREE_CODE (size) != INTEGER_CST)
8888 tree folded = cp_fully_fold (size);
8889 if (TREE_CODE (folded) == INTEGER_CST)
8890 pedwarn (location_of (size), OPT_Wpedantic,
8891 "size of array is not an integral constant-expression");
8892 /* Use the folded result for VLAs, too; it will have resolved
8893 SIZEOF_EXPR. */
8894 size = folded;
8897 /* Normally, the array-bound will be a constant. */
8898 if (TREE_CODE (size) == INTEGER_CST)
8900 /* Check to see if the array bound overflowed. Make that an
8901 error, no matter how generous we're being. */
8902 constant_expression_error (size);
8904 /* An array must have a positive number of elements. */
8905 if (tree_int_cst_lt (size, integer_zero_node))
8907 if (!(complain & tf_error))
8908 return error_mark_node;
8909 if (name)
8910 error ("size of array %qD is negative", name);
8911 else
8912 error ("size of array is negative");
8913 size = integer_one_node;
8915 /* As an extension we allow zero-sized arrays. */
8916 else if (integer_zerop (size))
8918 if (!(complain & tf_error))
8919 /* We must fail if performing argument deduction (as
8920 indicated by the state of complain), so that
8921 another substitution can be found. */
8922 return error_mark_node;
8923 else if (in_system_header_at (input_location))
8924 /* Allow them in system headers because glibc uses them. */;
8925 else if (name)
8926 pedwarn (input_location, OPT_Wpedantic, "ISO C++ forbids zero-size array %qD", name);
8927 else
8928 pedwarn (input_location, OPT_Wpedantic, "ISO C++ forbids zero-size array");
8931 else if (TREE_CONSTANT (size)
8932 /* We don't allow VLAs at non-function scopes, or during
8933 tentative template substitution. */
8934 || !at_function_scope_p ()
8935 || !(complain & tf_error))
8937 if (!(complain & tf_error))
8938 return error_mark_node;
8939 /* `(int) &fn' is not a valid array bound. */
8940 if (name)
8941 error ("size of array %qD is not an integral constant-expression",
8942 name);
8943 else
8944 error ("size of array is not an integral constant-expression");
8945 size = integer_one_node;
8947 else if (pedantic && warn_vla != 0)
8949 if (name)
8950 pedwarn (input_location, OPT_Wvla, "ISO C++ forbids variable length array %qD", name);
8951 else
8952 pedwarn (input_location, OPT_Wvla, "ISO C++ forbids variable length array");
8954 else if (warn_vla > 0)
8956 if (name)
8957 warning (OPT_Wvla,
8958 "variable length array %qD is used", name);
8959 else
8960 warning (OPT_Wvla,
8961 "variable length array is used");
8964 if (processing_template_decl && !TREE_CONSTANT (size))
8965 /* A variable sized array. */
8966 itype = build_min (MINUS_EXPR, sizetype, size, integer_one_node);
8967 else
8969 HOST_WIDE_INT saved_processing_template_decl;
8971 /* Compute the index of the largest element in the array. It is
8972 one less than the number of elements in the array. We save
8973 and restore PROCESSING_TEMPLATE_DECL so that computations in
8974 cp_build_binary_op will be appropriately folded. */
8975 saved_processing_template_decl = processing_template_decl;
8976 processing_template_decl = 0;
8977 itype = cp_build_binary_op (input_location,
8978 MINUS_EXPR,
8979 cp_convert (ssizetype, size, complain),
8980 cp_convert (ssizetype, integer_one_node,
8981 complain),
8982 complain);
8983 itype = maybe_constant_value (itype);
8984 processing_template_decl = saved_processing_template_decl;
8986 if (!TREE_CONSTANT (itype))
8988 /* A variable sized array. */
8989 itype = variable_size (itype);
8991 stabilize_vla_size (itype);
8993 if (flag_sanitize & SANITIZE_VLA
8994 && do_ubsan_in_current_function ())
8996 /* We have to add 1 -- in the ubsan routine we generate
8997 LE_EXPR rather than LT_EXPR. */
8998 tree t = fold_build2 (PLUS_EXPR, TREE_TYPE (itype), itype,
8999 build_one_cst (TREE_TYPE (itype)));
9000 t = ubsan_instrument_vla (input_location, t);
9001 finish_expr_stmt (t);
9004 /* Make sure that there was no overflow when creating to a signed
9005 index type. (For example, on a 32-bit machine, an array with
9006 size 2^32 - 1 is too big.) */
9007 else if (TREE_CODE (itype) == INTEGER_CST
9008 && TREE_OVERFLOW (itype))
9010 if (!(complain & tf_error))
9011 return error_mark_node;
9012 error ("overflow in array dimension");
9013 TREE_OVERFLOW (itype) = 0;
9017 /* Create and return the appropriate index type. */
9018 itype = build_index_type (itype);
9020 /* If the index type were dependent, we would have returned early, so
9021 remember that it isn't. */
9022 TYPE_DEPENDENT_P (itype) = 0;
9023 TYPE_DEPENDENT_P_VALID (itype) = 1;
9024 return itype;
9027 /* Returns the scope (if any) in which the entity declared by
9028 DECLARATOR will be located. If the entity was declared with an
9029 unqualified name, NULL_TREE is returned. */
9031 tree
9032 get_scope_of_declarator (const cp_declarator *declarator)
9034 while (declarator && declarator->kind != cdk_id)
9035 declarator = declarator->declarator;
9037 /* If the declarator-id is a SCOPE_REF, the scope in which the
9038 declaration occurs is the first operand. */
9039 if (declarator
9040 && declarator->u.id.qualifying_scope)
9041 return declarator->u.id.qualifying_scope;
9043 /* Otherwise, the declarator is not a qualified name; the entity will
9044 be declared in the current scope. */
9045 return NULL_TREE;
9048 /* Returns an ARRAY_TYPE for an array with SIZE elements of the
9049 indicated TYPE. If non-NULL, NAME is the NAME of the declaration
9050 with this type. */
9052 static tree
9053 create_array_type_for_decl (tree name, tree type, tree size)
9055 tree itype = NULL_TREE;
9057 /* If things have already gone awry, bail now. */
9058 if (type == error_mark_node || size == error_mark_node)
9059 return error_mark_node;
9061 /* 8.3.4/1: If the type of the identifier of D contains the auto
9062 type-specifier, the program is ill-formed. */
9063 if (type_uses_auto (type))
9065 error ("%qD declared as array of %qT", name, type);
9066 return error_mark_node;
9069 /* If there are some types which cannot be array elements,
9070 issue an error-message and return. */
9071 switch (TREE_CODE (type))
9073 case VOID_TYPE:
9074 if (name)
9075 error ("declaration of %qD as array of void", name);
9076 else
9077 error ("creating array of void");
9078 return error_mark_node;
9080 case FUNCTION_TYPE:
9081 if (name)
9082 error ("declaration of %qD as array of functions", name);
9083 else
9084 error ("creating array of functions");
9085 return error_mark_node;
9087 case REFERENCE_TYPE:
9088 if (name)
9089 error ("declaration of %qD as array of references", name);
9090 else
9091 error ("creating array of references");
9092 return error_mark_node;
9094 case METHOD_TYPE:
9095 if (name)
9096 error ("declaration of %qD as array of function members", name);
9097 else
9098 error ("creating array of function members");
9099 return error_mark_node;
9101 default:
9102 break;
9105 /* [dcl.array]
9107 The constant expressions that specify the bounds of the arrays
9108 can be omitted only for the first member of the sequence. */
9109 if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type))
9111 if (name)
9112 error ("declaration of %qD as multidimensional array must "
9113 "have bounds for all dimensions except the first",
9114 name);
9115 else
9116 error ("multidimensional array must have bounds for all "
9117 "dimensions except the first");
9119 return error_mark_node;
9122 /* Figure out the index type for the array. */
9123 if (size)
9124 itype = compute_array_index_type (name, size, tf_warning_or_error);
9126 /* [dcl.array]
9127 T is called the array element type; this type shall not be [...] an
9128 abstract class type. */
9129 abstract_virtuals_error (name, type);
9131 return build_cplus_array_type (type, itype);
9134 /* Returns the smallest location != UNKNOWN_LOCATION among the
9135 three stored in LOCATIONS[ds_const], LOCATIONS[ds_volatile],
9136 and LOCATIONS[ds_restrict]. */
9138 static location_t
9139 smallest_type_quals_location (int type_quals, const location_t* locations)
9141 location_t loc = UNKNOWN_LOCATION;
9143 if (type_quals & TYPE_QUAL_CONST)
9144 loc = locations[ds_const];
9146 if ((type_quals & TYPE_QUAL_VOLATILE)
9147 && (loc == UNKNOWN_LOCATION || locations[ds_volatile] < loc))
9148 loc = locations[ds_volatile];
9150 if ((type_quals & TYPE_QUAL_RESTRICT)
9151 && (loc == UNKNOWN_LOCATION || locations[ds_restrict] < loc))
9152 loc = locations[ds_restrict];
9154 return loc;
9157 /* Check that it's OK to declare a function with the indicated TYPE
9158 and TYPE_QUALS. SFK indicates the kind of special function (if any)
9159 that this function is. OPTYPE is the type given in a conversion
9160 operator declaration, or the class type for a constructor/destructor.
9161 Returns the actual return type of the function; that may be different
9162 than TYPE if an error occurs, or for certain special functions. */
9164 static tree
9165 check_special_function_return_type (special_function_kind sfk,
9166 tree type,
9167 tree optype,
9168 int type_quals,
9169 const location_t* locations)
9171 switch (sfk)
9173 case sfk_constructor:
9174 if (type)
9175 error ("return type specification for constructor invalid");
9176 else if (type_quals != TYPE_UNQUALIFIED)
9177 error_at (smallest_type_quals_location (type_quals, locations),
9178 "qualifiers are not allowed on constructor declaration");
9180 if (targetm.cxx.cdtor_returns_this ())
9181 type = build_pointer_type (optype);
9182 else
9183 type = void_type_node;
9184 break;
9186 case sfk_destructor:
9187 if (type)
9188 error ("return type specification for destructor invalid");
9189 else if (type_quals != TYPE_UNQUALIFIED)
9190 error_at (smallest_type_quals_location (type_quals, locations),
9191 "qualifiers are not allowed on destructor declaration");
9193 /* We can't use the proper return type here because we run into
9194 problems with ambiguous bases and covariant returns. */
9195 if (targetm.cxx.cdtor_returns_this ())
9196 type = build_pointer_type (void_type_node);
9197 else
9198 type = void_type_node;
9199 break;
9201 case sfk_conversion:
9202 if (type)
9203 error ("return type specified for %<operator %T%>", optype);
9204 else if (type_quals != TYPE_UNQUALIFIED)
9205 error_at (smallest_type_quals_location (type_quals, locations),
9206 "qualifiers are not allowed on declaration of "
9207 "%<operator %T%>", optype);
9209 type = optype;
9210 break;
9212 default:
9213 gcc_unreachable ();
9216 return type;
9219 /* A variable or data member (whose unqualified name is IDENTIFIER)
9220 has been declared with the indicated TYPE. If the TYPE is not
9221 acceptable, issue an error message and return a type to use for
9222 error-recovery purposes. */
9224 tree
9225 check_var_type (tree identifier, tree type)
9227 if (VOID_TYPE_P (type))
9229 if (!identifier)
9230 error ("unnamed variable or field declared void");
9231 else if (identifier_p (identifier))
9233 gcc_assert (!IDENTIFIER_OPNAME_P (identifier));
9234 error ("variable or field %qE declared void", identifier);
9236 else
9237 error ("variable or field declared void");
9238 type = error_mark_node;
9241 return type;
9244 /* Given declspecs and a declarator (abstract or otherwise), determine
9245 the name and type of the object declared and construct a DECL node
9246 for it.
9248 DECLSPECS points to the representation of declaration-specifier
9249 sequence that precedes declarator.
9251 DECL_CONTEXT says which syntactic context this declaration is in:
9252 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
9253 FUNCDEF for a function definition. Like NORMAL but a few different
9254 error messages in each case. Return value may be zero meaning
9255 this definition is too screwy to try to parse.
9256 MEMFUNCDEF for a function definition. Like FUNCDEF but prepares to
9257 handle member functions (which have FIELD context).
9258 Return value may be zero meaning this definition is too screwy to
9259 try to parse.
9260 PARM for a parameter declaration (either within a function prototype
9261 or before a function body). Make a PARM_DECL, or return void_type_node.
9262 TPARM for a template parameter declaration.
9263 CATCHPARM for a parameter declaration before a catch clause.
9264 TYPENAME if for a typename (in a cast or sizeof).
9265 Don't make a DECL node; just return the ..._TYPE node.
9266 FIELD for a struct or union field; make a FIELD_DECL.
9267 BITFIELD for a field with specified width.
9269 INITIALIZED is as for start_decl.
9271 ATTRLIST is a pointer to the list of attributes, which may be NULL
9272 if there are none; *ATTRLIST may be modified if attributes from inside
9273 the declarator should be applied to the declaration.
9275 When this function is called, scoping variables (such as
9276 CURRENT_CLASS_TYPE) should reflect the scope in which the
9277 declaration occurs, not the scope in which the new declaration will
9278 be placed. For example, on:
9280 void S::f() { ... }
9282 when grokdeclarator is called for `S::f', the CURRENT_CLASS_TYPE
9283 should not be `S'.
9285 Returns a DECL (if a declarator is present), a TYPE (if there is no
9286 declarator, in cases like "struct S;"), or the ERROR_MARK_NODE if an
9287 error occurs. */
9289 tree
9290 grokdeclarator (const cp_declarator *declarator,
9291 cp_decl_specifier_seq *declspecs,
9292 enum decl_context decl_context,
9293 int initialized,
9294 tree* attrlist)
9296 tree type = NULL_TREE;
9297 int longlong = 0;
9298 int explicit_intN = 0;
9299 int virtualp, explicitp, friendp, inlinep, staticp;
9300 int explicit_int = 0;
9301 int explicit_char = 0;
9302 int defaulted_int = 0;
9304 tree typedef_decl = NULL_TREE;
9305 const char *name = NULL;
9306 tree typedef_type = NULL_TREE;
9307 /* True if this declarator is a function definition. */
9308 bool funcdef_flag = false;
9309 cp_declarator_kind innermost_code = cdk_error;
9310 int bitfield = 0;
9311 #if 0
9312 /* See the code below that used this. */
9313 tree decl_attr = NULL_TREE;
9314 #endif
9316 /* Keep track of what sort of function is being processed
9317 so that we can warn about default return values, or explicit
9318 return values which do not match prescribed defaults. */
9319 special_function_kind sfk = sfk_none;
9321 tree dname = NULL_TREE;
9322 tree ctor_return_type = NULL_TREE;
9323 enum overload_flags flags = NO_SPECIAL;
9324 /* cv-qualifiers that apply to the declarator, for a declaration of
9325 a member function. */
9326 cp_cv_quals memfn_quals = TYPE_UNQUALIFIED;
9327 /* virt-specifiers that apply to the declarator, for a declaration of
9328 a member function. */
9329 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
9330 /* ref-qualifier that applies to the declarator, for a declaration of
9331 a member function. */
9332 cp_ref_qualifier rqual = REF_QUAL_NONE;
9333 /* cv-qualifiers that apply to the type specified by the DECLSPECS. */
9334 int type_quals = TYPE_UNQUALIFIED;
9335 tree raises = NULL_TREE;
9336 int template_count = 0;
9337 tree returned_attrs = NULL_TREE;
9338 tree parms = NULL_TREE;
9339 const cp_declarator *id_declarator;
9340 /* The unqualified name of the declarator; either an
9341 IDENTIFIER_NODE, BIT_NOT_EXPR, or TEMPLATE_ID_EXPR. */
9342 tree unqualified_id;
9343 /* The class type, if any, in which this entity is located,
9344 or NULL_TREE if none. Note that this value may be different from
9345 the current class type; for example if an attempt is made to declare
9346 "A::f" inside "B", this value will be "A". */
9347 tree ctype = current_class_type;
9348 /* The NAMESPACE_DECL for the namespace in which this entity is
9349 located. If an unqualified name is used to declare the entity,
9350 this value will be NULL_TREE, even if the entity is located at
9351 namespace scope. */
9352 tree in_namespace = NULL_TREE;
9353 cp_storage_class storage_class;
9354 bool unsigned_p, signed_p, short_p, long_p, thread_p;
9355 bool type_was_error_mark_node = false;
9356 bool parameter_pack_p = declarator? declarator->parameter_pack_p : false;
9357 bool template_type_arg = false;
9358 bool template_parm_flag = false;
9359 bool typedef_p = decl_spec_seq_has_spec_p (declspecs, ds_typedef);
9360 bool constexpr_p = decl_spec_seq_has_spec_p (declspecs, ds_constexpr);
9361 bool late_return_type_p = false;
9362 bool array_parameter_p = false;
9363 source_location saved_loc = input_location;
9364 tree reqs = NULL_TREE;
9366 signed_p = decl_spec_seq_has_spec_p (declspecs, ds_signed);
9367 unsigned_p = decl_spec_seq_has_spec_p (declspecs, ds_unsigned);
9368 short_p = decl_spec_seq_has_spec_p (declspecs, ds_short);
9369 long_p = decl_spec_seq_has_spec_p (declspecs, ds_long);
9370 longlong = decl_spec_seq_has_spec_p (declspecs, ds_long_long);
9371 explicit_intN = declspecs->explicit_intN_p;
9372 thread_p = decl_spec_seq_has_spec_p (declspecs, ds_thread);
9374 // Was concept_p specified? Note that ds_concept
9375 // implies ds_constexpr!
9376 bool concept_p = decl_spec_seq_has_spec_p (declspecs, ds_concept);
9377 if (concept_p)
9378 constexpr_p = true;
9380 if (decl_spec_seq_has_spec_p (declspecs, ds_const))
9381 type_quals |= TYPE_QUAL_CONST;
9382 if (decl_spec_seq_has_spec_p (declspecs, ds_volatile))
9383 type_quals |= TYPE_QUAL_VOLATILE;
9384 if (decl_spec_seq_has_spec_p (declspecs, ds_restrict))
9385 type_quals |= TYPE_QUAL_RESTRICT;
9387 if (decl_context == FUNCDEF)
9388 funcdef_flag = true, decl_context = NORMAL;
9389 else if (decl_context == MEMFUNCDEF)
9390 funcdef_flag = true, decl_context = FIELD;
9391 else if (decl_context == BITFIELD)
9392 bitfield = 1, decl_context = FIELD;
9393 else if (decl_context == TEMPLATE_TYPE_ARG)
9394 template_type_arg = true, decl_context = TYPENAME;
9395 else if (decl_context == TPARM)
9396 template_parm_flag = true, decl_context = PARM;
9398 if (initialized > 1)
9399 funcdef_flag = true;
9401 /* Look inside a declarator for the name being declared
9402 and get it as a string, for an error message. */
9403 for (id_declarator = declarator;
9404 id_declarator;
9405 id_declarator = id_declarator->declarator)
9407 if (id_declarator->kind != cdk_id)
9408 innermost_code = id_declarator->kind;
9410 switch (id_declarator->kind)
9412 case cdk_function:
9413 if (id_declarator->declarator
9414 && id_declarator->declarator->kind == cdk_id)
9416 sfk = id_declarator->declarator->u.id.sfk;
9417 if (sfk == sfk_destructor)
9418 flags = DTOR_FLAG;
9420 break;
9422 case cdk_id:
9424 tree qualifying_scope = id_declarator->u.id.qualifying_scope;
9425 tree decl = id_declarator->u.id.unqualified_name;
9426 if (!decl)
9427 break;
9428 if (qualifying_scope)
9430 if (at_function_scope_p ())
9432 /* [dcl.meaning]
9434 A declarator-id shall not be qualified except
9435 for ...
9437 None of the cases are permitted in block
9438 scope. */
9439 if (qualifying_scope == global_namespace)
9440 error ("invalid use of qualified-name %<::%D%>",
9441 decl);
9442 else if (TYPE_P (qualifying_scope))
9443 error ("invalid use of qualified-name %<%T::%D%>",
9444 qualifying_scope, decl);
9445 else
9446 error ("invalid use of qualified-name %<%D::%D%>",
9447 qualifying_scope, decl);
9448 return error_mark_node;
9450 else if (TYPE_P (qualifying_scope))
9452 ctype = qualifying_scope;
9453 if (!MAYBE_CLASS_TYPE_P (ctype))
9455 error ("%q#T is not a class or a namespace", ctype);
9456 ctype = NULL_TREE;
9458 else if (innermost_code != cdk_function
9459 && current_class_type
9460 && !uniquely_derived_from_p (ctype,
9461 current_class_type))
9463 error ("invalid use of qualified-name %<%T::%D%>",
9464 qualifying_scope, decl);
9465 return error_mark_node;
9468 else if (TREE_CODE (qualifying_scope) == NAMESPACE_DECL)
9469 in_namespace = qualifying_scope;
9471 switch (TREE_CODE (decl))
9473 case BIT_NOT_EXPR:
9475 tree type;
9477 if (innermost_code != cdk_function)
9479 error ("declaration of %qD as non-function", decl);
9480 return error_mark_node;
9482 else if (!qualifying_scope
9483 && !(current_class_type && at_class_scope_p ()))
9485 error ("declaration of %qD as non-member", decl);
9486 return error_mark_node;
9489 type = TREE_OPERAND (decl, 0);
9490 if (TYPE_P (type))
9491 type = constructor_name (type);
9492 name = identifier_to_locale (IDENTIFIER_POINTER (type));
9493 dname = decl;
9495 break;
9497 case TEMPLATE_ID_EXPR:
9499 tree fns = TREE_OPERAND (decl, 0);
9501 dname = fns;
9502 if (!identifier_p (dname))
9504 if (variable_template_p (dname))
9505 dname = DECL_NAME (dname);
9506 else
9508 gcc_assert (is_overloaded_fn (dname));
9509 dname = DECL_NAME (get_first_fn (dname));
9513 /* Fall through. */
9515 case IDENTIFIER_NODE:
9516 if (identifier_p (decl))
9517 dname = decl;
9519 if (C_IS_RESERVED_WORD (dname))
9521 error ("declarator-id missing; using reserved word %qD",
9522 dname);
9523 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
9525 else if (!IDENTIFIER_TYPENAME_P (dname))
9526 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
9527 else
9529 gcc_assert (flags == NO_SPECIAL);
9530 flags = TYPENAME_FLAG;
9531 ctor_return_type = TREE_TYPE (dname);
9532 sfk = sfk_conversion;
9533 if (is_typename_at_global_scope (dname))
9534 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
9535 else
9536 name = "<invalid operator>";
9538 break;
9540 default:
9541 gcc_unreachable ();
9543 break;
9546 case cdk_array:
9547 case cdk_pointer:
9548 case cdk_reference:
9549 case cdk_ptrmem:
9550 break;
9552 case cdk_error:
9553 return error_mark_node;
9555 default:
9556 gcc_unreachable ();
9558 if (id_declarator->kind == cdk_id)
9559 break;
9562 /* [dcl.fct.edf]
9564 The declarator in a function-definition shall have the form
9565 D1 ( parameter-declaration-clause) ... */
9566 if (funcdef_flag && innermost_code != cdk_function)
9568 error ("function definition does not declare parameters");
9569 return error_mark_node;
9572 if (flags == TYPENAME_FLAG
9573 && innermost_code != cdk_function
9574 && ! (ctype && !declspecs->any_specifiers_p))
9576 error ("declaration of %qD as non-function", dname);
9577 return error_mark_node;
9580 if (dname
9581 && identifier_p (dname)
9582 && UDLIT_OPER_P (dname)
9583 && innermost_code != cdk_function)
9585 error ("declaration of %qD as non-function", dname);
9586 return error_mark_node;
9589 if (dname && IDENTIFIER_OPNAME_P (dname))
9591 if (typedef_p)
9593 error ("declaration of %qD as %<typedef%>", dname);
9594 return error_mark_node;
9596 else if (decl_context == PARM || decl_context == CATCHPARM)
9598 error ("declaration of %qD as parameter", dname);
9599 return error_mark_node;
9603 /* Anything declared one level down from the top level
9604 must be one of the parameters of a function
9605 (because the body is at least two levels down). */
9607 /* This heuristic cannot be applied to C++ nodes! Fixed, however,
9608 by not allowing C++ class definitions to specify their parameters
9609 with xdecls (must be spec.d in the parmlist).
9611 Since we now wait to push a class scope until we are sure that
9612 we are in a legitimate method context, we must set oldcname
9613 explicitly (since current_class_name is not yet alive).
9615 We also want to avoid calling this a PARM if it is in a namespace. */
9617 if (decl_context == NORMAL && !toplevel_bindings_p ())
9619 cp_binding_level *b = current_binding_level;
9620 current_binding_level = b->level_chain;
9621 if (current_binding_level != 0 && toplevel_bindings_p ())
9622 decl_context = PARM;
9623 current_binding_level = b;
9626 if (name == NULL)
9627 name = decl_context == PARM ? "parameter" : "type name";
9629 if (concept_p && typedef_p)
9631 error ("%<concept%> cannot appear in a typedef declaration");
9632 return error_mark_node;
9635 if (constexpr_p && typedef_p)
9637 error ("%<constexpr%> cannot appear in a typedef declaration");
9638 return error_mark_node;
9641 /* If there were multiple types specified in the decl-specifier-seq,
9642 issue an error message. */
9643 if (declspecs->multiple_types_p)
9645 error ("two or more data types in declaration of %qs", name);
9646 return error_mark_node;
9649 if (declspecs->conflicting_specifiers_p)
9651 error ("conflicting specifiers in declaration of %qs", name);
9652 return error_mark_node;
9655 /* Extract the basic type from the decl-specifier-seq. */
9656 type = declspecs->type;
9657 if (type == error_mark_node)
9659 type = NULL_TREE;
9660 type_was_error_mark_node = true;
9662 /* If the entire declaration is itself tagged as deprecated then
9663 suppress reports of deprecated items. */
9664 if (type && TREE_DEPRECATED (type)
9665 && deprecated_state != DEPRECATED_SUPPRESS)
9666 warn_deprecated_use (type, NULL_TREE);
9667 if (type && TREE_CODE (type) == TYPE_DECL)
9669 typedef_decl = type;
9670 type = TREE_TYPE (typedef_decl);
9671 if (TREE_DEPRECATED (type)
9672 && DECL_ARTIFICIAL (typedef_decl)
9673 && deprecated_state != DEPRECATED_SUPPRESS)
9674 warn_deprecated_use (type, NULL_TREE);
9676 /* No type at all: default to `int', and set DEFAULTED_INT
9677 because it was not a user-defined typedef. */
9678 if (type == NULL_TREE)
9680 if (signed_p || unsigned_p || long_p || short_p)
9682 /* These imply 'int'. */
9683 type = integer_type_node;
9684 defaulted_int = 1;
9686 /* If we just have "complex", it is equivalent to "complex double". */
9687 else if (!longlong && !explicit_intN
9688 && decl_spec_seq_has_spec_p (declspecs, ds_complex))
9690 type = double_type_node;
9691 pedwarn (declspecs->locations[ds_complex], OPT_Wpedantic,
9692 "ISO C++ does not support plain %<complex%> meaning "
9693 "%<double complex%>");
9696 /* Gather flags. */
9697 explicit_int = declspecs->explicit_int_p;
9698 explicit_char = declspecs->explicit_char_p;
9700 #if 0
9701 /* See the code below that used this. */
9702 if (typedef_decl)
9703 decl_attr = DECL_ATTRIBUTES (typedef_decl);
9704 #endif
9705 typedef_type = type;
9708 if (sfk != sfk_conversion)
9709 ctor_return_type = ctype;
9711 if (sfk != sfk_none)
9713 type = check_special_function_return_type (sfk, type,
9714 ctor_return_type,
9715 type_quals,
9716 declspecs->locations);
9717 type_quals = TYPE_UNQUALIFIED;
9719 else if (type == NULL_TREE)
9721 int is_main;
9723 explicit_int = -1;
9725 /* We handle `main' specially here, because 'main () { }' is so
9726 common. With no options, it is allowed. With -Wreturn-type,
9727 it is a warning. It is only an error with -pedantic-errors. */
9728 is_main = (funcdef_flag
9729 && dname && identifier_p (dname)
9730 && MAIN_NAME_P (dname)
9731 && ctype == NULL_TREE
9732 && in_namespace == NULL_TREE
9733 && current_namespace == global_namespace);
9735 if (type_was_error_mark_node)
9736 /* We've already issued an error, don't complain more. */;
9737 else if (in_system_header_at (input_location) || flag_ms_extensions)
9738 /* Allow it, sigh. */;
9739 else if (! is_main)
9740 permerror (input_location, "ISO C++ forbids declaration of %qs with no type", name);
9741 else if (pedantic)
9742 pedwarn (input_location, OPT_Wpedantic,
9743 "ISO C++ forbids declaration of %qs with no type", name);
9744 else
9745 warning (OPT_Wreturn_type,
9746 "ISO C++ forbids declaration of %qs with no type", name);
9748 if (type_was_error_mark_node && template_parm_flag)
9749 /* FIXME we should be able to propagate the error_mark_node as is
9750 for other contexts too. */
9751 type = error_mark_node;
9752 else
9753 type = integer_type_node;
9756 ctype = NULL_TREE;
9758 if (explicit_intN)
9760 if (! int_n_enabled_p[declspecs->int_n_idx])
9762 error ("%<__int%d%> is not supported by this target",
9763 int_n_data[declspecs->int_n_idx].bitsize);
9764 explicit_intN = false;
9766 else if (pedantic && ! in_system_header_at (input_location))
9767 pedwarn (input_location, OPT_Wpedantic,
9768 "ISO C++ does not support %<__int%d%> for %qs",
9769 int_n_data[declspecs->int_n_idx].bitsize, name);
9772 /* Now process the modifiers that were specified
9773 and check for invalid combinations. */
9775 /* Long double is a special combination. */
9776 if (long_p && !longlong && TYPE_MAIN_VARIANT (type) == double_type_node)
9778 long_p = false;
9779 type = cp_build_qualified_type (long_double_type_node,
9780 cp_type_quals (type));
9783 /* Check all other uses of type modifiers. */
9785 if (unsigned_p || signed_p || long_p || short_p)
9787 int ok = 0;
9789 if ((signed_p || unsigned_p) && TREE_CODE (type) != INTEGER_TYPE)
9790 error ("%<signed%> or %<unsigned%> invalid for %qs", name);
9791 else if (signed_p && unsigned_p)
9792 error ("%<signed%> and %<unsigned%> specified together for %qs", name);
9793 else if (longlong && TREE_CODE (type) != INTEGER_TYPE)
9794 error ("%<long long%> invalid for %qs", name);
9795 else if (long_p && TREE_CODE (type) == REAL_TYPE)
9796 error ("%<long%> invalid for %qs", name);
9797 else if (short_p && TREE_CODE (type) == REAL_TYPE)
9798 error ("%<short%> invalid for %qs", name);
9799 else if ((long_p || short_p) && TREE_CODE (type) != INTEGER_TYPE)
9800 error ("%<long%> or %<short%> invalid for %qs", name);
9801 else if ((long_p || short_p || explicit_char || explicit_int) && explicit_intN)
9802 error ("%<long%>, %<int%>, %<short%>, or %<char%> invalid for %qs", name);
9803 else if ((long_p || short_p) && explicit_char)
9804 error ("%<long%> or %<short%> specified with char for %qs", name);
9805 else if (long_p && short_p)
9806 error ("%<long%> and %<short%> specified together for %qs", name);
9807 else if (type == char16_type_node || type == char32_type_node)
9809 if (signed_p || unsigned_p)
9810 error ("%<signed%> or %<unsigned%> invalid for %qs", name);
9811 else if (short_p || long_p)
9812 error ("%<short%> or %<long%> invalid for %qs", name);
9814 else
9816 ok = 1;
9817 if (!explicit_int && !defaulted_int && !explicit_char && !explicit_intN && pedantic)
9819 pedwarn (input_location, OPT_Wpedantic,
9820 "long, short, signed or unsigned used invalidly for %qs",
9821 name);
9822 if (flag_pedantic_errors)
9823 ok = 0;
9827 /* Discard the type modifiers if they are invalid. */
9828 if (! ok)
9830 unsigned_p = false;
9831 signed_p = false;
9832 long_p = false;
9833 short_p = false;
9834 longlong = 0;
9838 /* Decide whether an integer type is signed or not.
9839 Optionally treat bitfields as signed by default. */
9840 if (unsigned_p
9841 /* [class.bit]
9843 It is implementation-defined whether a plain (neither
9844 explicitly signed or unsigned) char, short, int, or long
9845 bit-field is signed or unsigned.
9847 Naturally, we extend this to long long as well. Note that
9848 this does not include wchar_t. */
9849 || (bitfield && !flag_signed_bitfields
9850 && !signed_p
9851 /* A typedef for plain `int' without `signed' can be
9852 controlled just like plain `int', but a typedef for
9853 `signed int' cannot be so controlled. */
9854 && !(typedef_decl
9855 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl))
9856 && TREE_CODE (type) == INTEGER_TYPE
9857 && !same_type_p (TYPE_MAIN_VARIANT (type), wchar_type_node)))
9859 if (explicit_intN)
9860 type = int_n_trees[declspecs->int_n_idx].unsigned_type;
9861 else if (longlong)
9862 type = long_long_unsigned_type_node;
9863 else if (long_p)
9864 type = long_unsigned_type_node;
9865 else if (short_p)
9866 type = short_unsigned_type_node;
9867 else if (type == char_type_node)
9868 type = unsigned_char_type_node;
9869 else if (typedef_decl)
9870 type = unsigned_type_for (type);
9871 else
9872 type = unsigned_type_node;
9874 else if (signed_p && type == char_type_node)
9875 type = signed_char_type_node;
9876 else if (explicit_intN)
9877 type = int_n_trees[declspecs->int_n_idx].signed_type;
9878 else if (longlong)
9879 type = long_long_integer_type_node;
9880 else if (long_p)
9881 type = long_integer_type_node;
9882 else if (short_p)
9883 type = short_integer_type_node;
9885 if (decl_spec_seq_has_spec_p (declspecs, ds_complex))
9887 if (TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
9888 error ("complex invalid for %qs", name);
9889 /* If a modifier is specified, the resulting complex is the complex
9890 form of TYPE. E.g, "complex short" is "complex short int". */
9891 else if (type == integer_type_node)
9892 type = complex_integer_type_node;
9893 else if (type == float_type_node)
9894 type = complex_float_type_node;
9895 else if (type == double_type_node)
9896 type = complex_double_type_node;
9897 else if (type == long_double_type_node)
9898 type = complex_long_double_type_node;
9899 else
9900 type = build_complex_type (type);
9903 /* If we're using the injected-class-name to form a compound type or a
9904 declaration, replace it with the underlying class so we don't get
9905 redundant typedefs in the debug output. But if we are returning the
9906 type unchanged, leave it alone so that it's available to
9907 maybe_get_template_decl_from_type_decl. */
9908 if (CLASS_TYPE_P (type)
9909 && DECL_SELF_REFERENCE_P (TYPE_NAME (type))
9910 && type == TREE_TYPE (TYPE_NAME (type))
9911 && (declarator || type_quals))
9912 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
9914 type_quals |= cp_type_quals (type);
9915 type = cp_build_qualified_type_real
9916 (type, type_quals, ((((typedef_decl && !DECL_ARTIFICIAL (typedef_decl))
9917 || declspecs->decltype_p)
9918 ? tf_ignore_bad_quals : 0) | tf_warning_or_error));
9919 /* We might have ignored or rejected some of the qualifiers. */
9920 type_quals = cp_type_quals (type);
9922 staticp = 0;
9923 inlinep = decl_spec_seq_has_spec_p (declspecs, ds_inline);
9924 virtualp = decl_spec_seq_has_spec_p (declspecs, ds_virtual);
9925 explicitp = decl_spec_seq_has_spec_p (declspecs, ds_explicit);
9927 storage_class = declspecs->storage_class;
9928 if (storage_class == sc_static)
9929 staticp = 1 + (decl_context == FIELD);
9931 if (virtualp)
9933 if (staticp == 2)
9935 error ("member %qD cannot be declared both %<virtual%> "
9936 "and %<static%>", dname);
9937 storage_class = sc_none;
9938 staticp = 0;
9940 if (constexpr_p)
9941 error ("member %qD cannot be declared both %<virtual%> "
9942 "and %<constexpr%>", dname);
9944 friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
9946 /* Issue errors about use of storage classes for parameters. */
9947 if (decl_context == PARM)
9949 if (typedef_p)
9951 error ("typedef declaration invalid in parameter declaration");
9952 return error_mark_node;
9954 else if (template_parm_flag && storage_class != sc_none)
9956 error ("storage class specified for template parameter %qs", name);
9957 return error_mark_node;
9959 else if (storage_class == sc_static
9960 || storage_class == sc_extern
9961 || thread_p)
9962 error ("storage class specifiers invalid in parameter declarations");
9964 /* Function parameters cannot be concept. */
9965 if (concept_p)
9966 error ("a parameter cannot be declared %<concept%>");
9967 /* Function parameters cannot be constexpr. If we saw one, moan
9968 and pretend it wasn't there. */
9969 else if (constexpr_p)
9971 error ("a parameter cannot be declared %<constexpr%>");
9972 constexpr_p = 0;
9976 /* Give error if `virtual' is used outside of class declaration. */
9977 if (virtualp
9978 && (current_class_name == NULL_TREE || decl_context != FIELD))
9980 error_at (declspecs->locations[ds_virtual],
9981 "%<virtual%> outside class declaration");
9982 virtualp = 0;
9985 /* Static anonymous unions are dealt with here. */
9986 if (staticp && decl_context == TYPENAME
9987 && declspecs->type
9988 && ANON_AGGR_TYPE_P (declspecs->type))
9989 decl_context = FIELD;
9991 /* Warn about storage classes that are invalid for certain
9992 kinds of declarations (parameters, typenames, etc.). */
9993 if (thread_p
9994 && ((storage_class
9995 && storage_class != sc_extern
9996 && storage_class != sc_static)
9997 || typedef_p))
9999 error ("multiple storage classes in declaration of %qs", name);
10000 thread_p = false;
10002 if (decl_context != NORMAL
10003 && ((storage_class != sc_none
10004 && storage_class != sc_mutable)
10005 || thread_p))
10007 if ((decl_context == PARM || decl_context == CATCHPARM)
10008 && (storage_class == sc_register
10009 || storage_class == sc_auto))
10011 else if (typedef_p)
10013 else if (decl_context == FIELD
10014 /* C++ allows static class elements. */
10015 && storage_class == sc_static)
10016 /* C++ also allows inlines and signed and unsigned elements,
10017 but in those cases we don't come in here. */
10019 else
10021 if (decl_context == FIELD)
10022 error ("storage class specified for %qs", name);
10023 else
10025 if (decl_context == PARM || decl_context == CATCHPARM)
10026 error ("storage class specified for parameter %qs", name);
10027 else
10028 error ("storage class specified for typename");
10030 if (storage_class == sc_register
10031 || storage_class == sc_auto
10032 || storage_class == sc_extern
10033 || thread_p)
10034 storage_class = sc_none;
10037 else if (storage_class == sc_extern && funcdef_flag
10038 && ! toplevel_bindings_p ())
10039 error ("nested function %qs declared %<extern%>", name);
10040 else if (toplevel_bindings_p ())
10042 if (storage_class == sc_auto)
10043 error ("top-level declaration of %qs specifies %<auto%>", name);
10045 else if (thread_p
10046 && storage_class != sc_extern
10047 && storage_class != sc_static)
10049 if (declspecs->gnu_thread_keyword_p)
10050 pedwarn (input_location, 0, "function-scope %qs implicitly auto and "
10051 "declared %<__thread%>", name);
10053 /* When thread_local is applied to a variable of block scope the
10054 storage-class-specifier static is implied if it does not appear
10055 explicitly. */
10056 storage_class = declspecs->storage_class = sc_static;
10057 staticp = 1;
10060 if (storage_class && friendp)
10062 error ("storage class specifiers invalid in friend function declarations");
10063 storage_class = sc_none;
10064 staticp = 0;
10067 if (!id_declarator)
10068 unqualified_id = NULL_TREE;
10069 else
10071 unqualified_id = id_declarator->u.id.unqualified_name;
10072 switch (TREE_CODE (unqualified_id))
10074 case BIT_NOT_EXPR:
10075 unqualified_id = TREE_OPERAND (unqualified_id, 0);
10076 if (TYPE_P (unqualified_id))
10077 unqualified_id = constructor_name (unqualified_id);
10078 break;
10080 case IDENTIFIER_NODE:
10081 case TEMPLATE_ID_EXPR:
10082 break;
10084 default:
10085 gcc_unreachable ();
10089 if (declspecs->std_attributes)
10091 /* Apply the c++11 attributes to the type preceding them. */
10092 input_location = declspecs->locations[ds_std_attribute];
10093 decl_attributes (&type, declspecs->std_attributes, 0);
10094 input_location = saved_loc;
10097 /* Determine the type of the entity declared by recurring on the
10098 declarator. */
10099 for (; declarator; declarator = declarator->declarator)
10101 const cp_declarator *inner_declarator;
10102 tree attrs;
10104 if (type == error_mark_node)
10105 return error_mark_node;
10107 attrs = declarator->attributes;
10108 if (attrs)
10110 int attr_flags;
10112 attr_flags = 0;
10113 if (declarator == NULL || declarator->kind == cdk_id)
10114 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
10115 if (declarator->kind == cdk_function)
10116 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
10117 if (declarator->kind == cdk_array)
10118 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
10119 returned_attrs = decl_attributes (&type,
10120 chainon (returned_attrs, attrs),
10121 attr_flags);
10124 if (declarator->kind == cdk_id)
10125 break;
10127 inner_declarator = declarator->declarator;
10129 switch (declarator->kind)
10131 case cdk_array:
10132 type = create_array_type_for_decl (dname, type,
10133 declarator->u.array.bounds);
10134 if (!valid_array_size_p (input_location, type, dname))
10135 type = error_mark_node;
10137 if (declarator->std_attributes)
10138 /* [dcl.array]/1:
10140 The optional attribute-specifier-seq appertains to the
10141 array. */
10142 returned_attrs = chainon (returned_attrs,
10143 declarator->std_attributes);
10144 break;
10146 case cdk_function:
10148 tree arg_types;
10149 int funcdecl_p;
10151 /* Declaring a function type.
10152 Make sure we have a valid type for the function to return. */
10154 if (type_quals != TYPE_UNQUALIFIED)
10156 if (SCALAR_TYPE_P (type) || VOID_TYPE_P (type))
10158 location_t loc;
10159 loc = smallest_type_quals_location (type_quals,
10160 declspecs->locations);
10161 if (loc == UNKNOWN_LOCATION)
10162 loc = declspecs->locations[ds_type_spec];
10163 warning_at (loc, OPT_Wignored_qualifiers, "type "
10164 "qualifiers ignored on function return type");
10166 /* We now know that the TYPE_QUALS don't apply to the
10167 decl, but to its return type. */
10168 type_quals = TYPE_UNQUALIFIED;
10171 /* Error about some types functions can't return. */
10173 if (TREE_CODE (type) == FUNCTION_TYPE)
10175 error ("%qs declared as function returning a function", name);
10176 return error_mark_node;
10178 if (TREE_CODE (type) == ARRAY_TYPE)
10180 error ("%qs declared as function returning an array", name);
10181 return error_mark_node;
10184 input_location = declspecs->locations[ds_type_spec];
10185 abstract_virtuals_error (ACU_RETURN, type);
10186 input_location = saved_loc;
10188 /* Pick up type qualifiers which should be applied to `this'. */
10189 memfn_quals = declarator->u.function.qualifiers;
10190 /* Pick up virt-specifiers. */
10191 virt_specifiers = declarator->u.function.virt_specifiers;
10192 /* And ref-qualifier, too */
10193 rqual = declarator->u.function.ref_qualifier;
10194 /* And tx-qualifier. */
10195 tree tx_qual = declarator->u.function.tx_qualifier;
10196 /* Pick up the exception specifications. */
10197 raises = declarator->u.function.exception_specification;
10198 /* If the exception-specification is ill-formed, let's pretend
10199 there wasn't one. */
10200 if (raises == error_mark_node)
10201 raises = NULL_TREE;
10203 if (reqs)
10204 error_at (location_of (reqs), "requires-clause on return type");
10205 reqs = declarator->u.function.requires_clause;
10207 /* Say it's a definition only for the CALL_EXPR
10208 closest to the identifier. */
10209 funcdecl_p = inner_declarator && inner_declarator->kind == cdk_id;
10211 /* Handle a late-specified return type. */
10212 if (funcdecl_p)
10214 if (type_uses_auto (type))
10216 if (!declarator->u.function.late_return_type)
10218 if (current_class_type
10219 && LAMBDA_TYPE_P (current_class_type))
10220 /* OK for C++11 lambdas. */;
10221 else if (cxx_dialect < cxx14)
10223 error ("%qs function uses "
10224 "%<auto%> type specifier without trailing "
10225 "return type", name);
10226 inform (input_location, "deduced return type "
10227 "only available with -std=c++14 or "
10228 "-std=gnu++14");
10230 else if (virtualp)
10232 error ("virtual function cannot "
10233 "have deduced return type");
10234 virtualp = false;
10237 else if (!is_auto (type) && sfk != sfk_conversion)
10239 error ("%qs function with trailing return type has"
10240 " %qT as its type rather than plain %<auto%>",
10241 name, type);
10242 return error_mark_node;
10245 else if (declarator->u.function.late_return_type
10246 && sfk != sfk_conversion)
10248 if (cxx_dialect < cxx11)
10249 /* Not using maybe_warn_cpp0x because this should
10250 always be an error. */
10251 error ("trailing return type only available with "
10252 "-std=c++11 or -std=gnu++11");
10253 else
10254 error ("%qs function with trailing return type not "
10255 "declared with %<auto%> type specifier", name);
10256 return error_mark_node;
10259 type = splice_late_return_type
10260 (type, declarator->u.function.late_return_type);
10261 if (type == error_mark_node)
10262 return error_mark_node;
10264 if (declarator->u.function.late_return_type)
10265 late_return_type_p = true;
10267 if (ctype == NULL_TREE
10268 && decl_context == FIELD
10269 && funcdecl_p
10270 && friendp == 0)
10271 ctype = current_class_type;
10273 if (ctype && (sfk == sfk_constructor
10274 || sfk == sfk_destructor))
10276 /* We are within a class's scope. If our declarator name
10277 is the same as the class name, and we are defining
10278 a function, then it is a constructor/destructor, and
10279 therefore returns a void type. */
10281 /* ISO C++ 12.4/2. A destructor may not be declared
10282 const or volatile. A destructor may not be static.
10283 A destructor may not be declared with ref-qualifier.
10285 ISO C++ 12.1. A constructor may not be declared
10286 const or volatile. A constructor may not be
10287 virtual. A constructor may not be static.
10288 A constructor may not be declared with ref-qualifier. */
10289 if (staticp == 2)
10290 error ((flags == DTOR_FLAG)
10291 ? G_("destructor cannot be static member function")
10292 : G_("constructor cannot be static member function"));
10293 if (memfn_quals)
10295 error ((flags == DTOR_FLAG)
10296 ? G_("destructors may not be cv-qualified")
10297 : G_("constructors may not be cv-qualified"));
10298 memfn_quals = TYPE_UNQUALIFIED;
10301 if (rqual)
10303 maybe_warn_cpp0x (CPP0X_REF_QUALIFIER);
10304 error ((flags == DTOR_FLAG)
10305 ? "destructors may not be ref-qualified"
10306 : "constructors may not be ref-qualified");
10307 rqual = REF_QUAL_NONE;
10310 if (decl_context == FIELD
10311 && !member_function_or_else (ctype,
10312 current_class_type,
10313 flags))
10314 return error_mark_node;
10316 if (flags != DTOR_FLAG)
10318 /* It's a constructor. */
10319 if (explicitp == 1)
10320 explicitp = 2;
10321 if (virtualp)
10323 permerror (input_location,
10324 "constructors cannot be declared %<virtual%>");
10325 virtualp = 0;
10327 if (decl_context == FIELD
10328 && sfk != sfk_constructor)
10329 return error_mark_node;
10331 if (decl_context == FIELD)
10332 staticp = 0;
10334 else if (friendp)
10336 if (virtualp)
10338 /* Cannot be both friend and virtual. */
10339 error ("virtual functions cannot be friends");
10340 friendp = 0;
10342 if (decl_context == NORMAL)
10343 error ("friend declaration not in class definition");
10344 if (current_function_decl && funcdef_flag)
10345 error ("can%'t define friend function %qs in a local "
10346 "class definition",
10347 name);
10349 else if (ctype && sfk == sfk_conversion)
10351 if (explicitp == 1)
10353 maybe_warn_cpp0x (CPP0X_EXPLICIT_CONVERSION);
10354 explicitp = 2;
10356 if (late_return_type_p)
10357 error ("a conversion function cannot have a trailing return type");
10360 arg_types = grokparms (declarator->u.function.parameters,
10361 &parms);
10363 if (inner_declarator
10364 && inner_declarator->kind == cdk_id
10365 && inner_declarator->u.id.sfk == sfk_destructor
10366 && arg_types != void_list_node)
10368 error ("destructors may not have parameters");
10369 arg_types = void_list_node;
10370 parms = NULL_TREE;
10373 type = build_function_type (type, arg_types);
10375 tree attrs = declarator->std_attributes;
10376 if (tx_qual)
10378 tree att = build_tree_list (tx_qual, NULL_TREE);
10379 /* transaction_safe applies to the type, but
10380 transaction_safe_dynamic applies to the function. */
10381 if (is_attribute_p ("transaction_safe", tx_qual))
10382 attrs = chainon (attrs, att);
10383 else
10384 returned_attrs = chainon (returned_attrs, att);
10386 if (attrs)
10387 /* [dcl.fct]/2:
10389 The optional attribute-specifier-seq appertains to
10390 the function type. */
10391 decl_attributes (&type, attrs, 0);
10393 break;
10395 case cdk_pointer:
10396 case cdk_reference:
10397 case cdk_ptrmem:
10398 /* Filter out pointers-to-references and references-to-references.
10399 We can get these if a TYPE_DECL is used. */
10401 if (TREE_CODE (type) == REFERENCE_TYPE)
10403 if (declarator->kind != cdk_reference)
10405 error ("cannot declare pointer to %q#T", type);
10406 type = TREE_TYPE (type);
10409 /* In C++0x, we allow reference to reference declarations
10410 that occur indirectly through typedefs [7.1.3/8 dcl.typedef]
10411 and template type arguments [14.3.1/4 temp.arg.type]. The
10412 check for direct reference to reference declarations, which
10413 are still forbidden, occurs below. Reasoning behind the change
10414 can be found in DR106, DR540, and the rvalue reference
10415 proposals. */
10416 else if (cxx_dialect == cxx98)
10418 error ("cannot declare reference to %q#T", type);
10419 type = TREE_TYPE (type);
10422 else if (VOID_TYPE_P (type))
10424 if (declarator->kind == cdk_reference)
10425 error ("cannot declare reference to %q#T", type);
10426 else if (declarator->kind == cdk_ptrmem)
10427 error ("cannot declare pointer to %q#T member", type);
10430 /* We now know that the TYPE_QUALS don't apply to the decl,
10431 but to the target of the pointer. */
10432 type_quals = TYPE_UNQUALIFIED;
10434 /* This code used to handle METHOD_TYPE, but I don't think it's
10435 possible to get it here anymore. */
10436 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
10437 if (declarator->kind == cdk_ptrmem
10438 && TREE_CODE (type) == FUNCTION_TYPE)
10440 memfn_quals |= type_memfn_quals (type);
10441 type = build_memfn_type (type,
10442 declarator->u.pointer.class_type,
10443 memfn_quals,
10444 rqual);
10445 if (type == error_mark_node)
10446 return error_mark_node;
10448 rqual = REF_QUAL_NONE;
10449 memfn_quals = TYPE_UNQUALIFIED;
10452 if (TREE_CODE (type) == FUNCTION_TYPE
10453 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
10454 || type_memfn_rqual (type) != REF_QUAL_NONE))
10455 error (declarator->kind == cdk_reference
10456 ? G_("cannot declare reference to qualified function type %qT")
10457 : G_("cannot declare pointer to qualified function type %qT"),
10458 type);
10460 /* When the pointed-to type involves components of variable size,
10461 care must be taken to ensure that the size evaluation code is
10462 emitted early enough to dominate all the possible later uses
10463 and late enough for the variables on which it depends to have
10464 been assigned.
10466 This is expected to happen automatically when the pointed-to
10467 type has a name/declaration of it's own, but special attention
10468 is required if the type is anonymous.
10470 We handle the NORMAL and FIELD contexts here by inserting a
10471 dummy statement that just evaluates the size at a safe point
10472 and ensures it is not deferred until e.g. within a deeper
10473 conditional context (c++/43555).
10475 We expect nothing to be needed here for PARM or TYPENAME.
10476 Evaluating the size at this point for TYPENAME would
10477 actually be incorrect, as we might be in the middle of an
10478 expression with side effects on the pointed-to type size
10479 "arguments" prior to the pointer declaration point and the
10480 size evaluation could end up prior to the side effects. */
10482 if (!TYPE_NAME (type)
10483 && (decl_context == NORMAL || decl_context == FIELD)
10484 && at_function_scope_p ()
10485 && variably_modified_type_p (type, NULL_TREE))
10487 TYPE_NAME (type) = build_decl (UNKNOWN_LOCATION, TYPE_DECL,
10488 NULL_TREE, type);
10489 add_decl_expr (TYPE_NAME (type));
10492 if (declarator->kind == cdk_reference)
10494 /* In C++0x, the type we are creating a reference to might be
10495 a typedef which is itself a reference type. In that case,
10496 we follow the reference collapsing rules in
10497 [7.1.3/8 dcl.typedef] to create the final reference type:
10499 "If a typedef TD names a type that is a reference to a type
10500 T, an attempt to create the type 'lvalue reference to cv TD'
10501 creates the type 'lvalue reference to T,' while an attempt
10502 to create the type "rvalue reference to cv TD' creates the
10503 type TD."
10505 if (VOID_TYPE_P (type))
10506 /* We already gave an error. */;
10507 else if (TREE_CODE (type) == REFERENCE_TYPE)
10509 if (declarator->u.reference.rvalue_ref)
10510 /* Leave type alone. */;
10511 else
10512 type = cp_build_reference_type (TREE_TYPE (type), false);
10514 else
10515 type = cp_build_reference_type
10516 (type, declarator->u.reference.rvalue_ref);
10518 /* In C++0x, we need this check for direct reference to
10519 reference declarations, which are forbidden by
10520 [8.3.2/5 dcl.ref]. Reference to reference declarations
10521 are only allowed indirectly through typedefs and template
10522 type arguments. Example:
10524 void foo(int & &); // invalid ref-to-ref decl
10526 typedef int & int_ref;
10527 void foo(int_ref &); // valid ref-to-ref decl
10529 if (inner_declarator && inner_declarator->kind == cdk_reference)
10530 error ("cannot declare reference to %q#T, which is not "
10531 "a typedef or a template type argument", type);
10533 else if (TREE_CODE (type) == METHOD_TYPE)
10534 type = build_ptrmemfunc_type (build_pointer_type (type));
10535 else if (declarator->kind == cdk_ptrmem)
10537 gcc_assert (TREE_CODE (declarator->u.pointer.class_type)
10538 != NAMESPACE_DECL);
10539 if (declarator->u.pointer.class_type == error_mark_node)
10540 /* We will already have complained. */
10541 type = error_mark_node;
10542 else
10543 type = build_ptrmem_type (declarator->u.pointer.class_type,
10544 type);
10546 else
10547 type = build_pointer_type (type);
10549 /* Process a list of type modifier keywords (such as
10550 const or volatile) that were given inside the `*' or `&'. */
10552 if (declarator->u.pointer.qualifiers)
10554 type
10555 = cp_build_qualified_type (type,
10556 declarator->u.pointer.qualifiers);
10557 type_quals = cp_type_quals (type);
10560 /* Apply C++11 attributes to the pointer, and not to the
10561 type pointed to. This is unlike what is done for GNU
10562 attributes above. It is to comply with [dcl.ptr]/1:
10564 [the optional attribute-specifier-seq (7.6.1) appertains
10565 to the pointer and not to the object pointed to]. */
10566 if (declarator->std_attributes)
10567 decl_attributes (&type, declarator->std_attributes,
10570 ctype = NULL_TREE;
10571 break;
10573 case cdk_error:
10574 break;
10576 default:
10577 gcc_unreachable ();
10581 /* A `constexpr' specifier used in an object declaration declares
10582 the object as `const'. */
10583 if (constexpr_p && innermost_code != cdk_function)
10585 /* DR1688 says that a `constexpr' specifier in combination with
10586 `volatile' is valid. */
10588 if (TREE_CODE (type) != REFERENCE_TYPE)
10590 type_quals |= TYPE_QUAL_CONST;
10591 type = cp_build_qualified_type (type, type_quals);
10595 if (unqualified_id && TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR
10596 && TREE_CODE (type) != FUNCTION_TYPE
10597 && TREE_CODE (type) != METHOD_TYPE
10598 && !variable_template_p (TREE_OPERAND (unqualified_id, 0)))
10600 error ("template-id %qD used as a declarator",
10601 unqualified_id);
10602 unqualified_id = dname;
10605 /* If TYPE is a FUNCTION_TYPE, but the function name was explicitly
10606 qualified with a class-name, turn it into a METHOD_TYPE, unless
10607 we know that the function is static. We take advantage of this
10608 opportunity to do other processing that pertains to entities
10609 explicitly declared to be class members. Note that if DECLARATOR
10610 is non-NULL, we know it is a cdk_id declarator; otherwise, we
10611 would not have exited the loop above. */
10612 if (declarator
10613 && declarator->u.id.qualifying_scope
10614 && MAYBE_CLASS_TYPE_P (declarator->u.id.qualifying_scope))
10616 ctype = declarator->u.id.qualifying_scope;
10617 ctype = TYPE_MAIN_VARIANT (ctype);
10618 template_count = num_template_headers_for_class (ctype);
10620 if (ctype == current_class_type)
10622 if (friendp)
10624 permerror (input_location, "member functions are implicitly friends of their class");
10625 friendp = 0;
10627 else
10628 permerror (declarator->id_loc,
10629 "extra qualification %<%T::%> on member %qs",
10630 ctype, name);
10632 else if (/* If the qualifying type is already complete, then we
10633 can skip the following checks. */
10634 !COMPLETE_TYPE_P (ctype)
10635 && (/* If the function is being defined, then
10636 qualifying type must certainly be complete. */
10637 funcdef_flag
10638 /* A friend declaration of "T::f" is OK, even if
10639 "T" is a template parameter. But, if this
10640 function is not a friend, the qualifying type
10641 must be a class. */
10642 || (!friendp && !CLASS_TYPE_P (ctype))
10643 /* For a declaration, the type need not be
10644 complete, if either it is dependent (since there
10645 is no meaningful definition of complete in that
10646 case) or the qualifying class is currently being
10647 defined. */
10648 || !(dependent_type_p (ctype)
10649 || currently_open_class (ctype)))
10650 /* Check that the qualifying type is complete. */
10651 && !complete_type_or_else (ctype, NULL_TREE))
10652 return error_mark_node;
10653 else if (TREE_CODE (type) == FUNCTION_TYPE)
10655 if (current_class_type
10656 && (!friendp || funcdef_flag))
10658 error (funcdef_flag
10659 ? G_("cannot define member function %<%T::%s%> "
10660 "within %<%T%>")
10661 : G_("cannot declare member function %<%T::%s%> "
10662 "within %<%T%>"),
10663 ctype, name, current_class_type);
10664 return error_mark_node;
10667 else if (typedef_p && current_class_type)
10669 error ("cannot declare member %<%T::%s%> within %qT",
10670 ctype, name, current_class_type);
10671 return error_mark_node;
10675 if (ctype == NULL_TREE && decl_context == FIELD && friendp == 0)
10676 ctype = current_class_type;
10678 /* Now TYPE has the actual type. */
10680 if (returned_attrs)
10682 if (attrlist)
10683 *attrlist = chainon (returned_attrs, *attrlist);
10684 else
10685 attrlist = &returned_attrs;
10688 if (declarator
10689 && declarator->kind == cdk_id
10690 && declarator->std_attributes)
10691 /* [dcl.meaning]/1: The optional attribute-specifier-seq following
10692 a declarator-id appertains to the entity that is declared. */
10693 *attrlist = chainon (*attrlist, declarator->std_attributes);
10695 /* Handle parameter packs. */
10696 if (parameter_pack_p)
10698 if (decl_context == PARM)
10699 /* Turn the type into a pack expansion.*/
10700 type = make_pack_expansion (type);
10701 else
10702 error ("non-parameter %qs cannot be a parameter pack", name);
10705 if ((decl_context == FIELD || decl_context == PARM)
10706 && !processing_template_decl
10707 && variably_modified_type_p (type, NULL_TREE))
10709 if (decl_context == FIELD)
10710 error ("data member may not have variably modified type %qT", type);
10711 else
10712 error ("parameter may not have variably modified type %qT", type);
10713 type = error_mark_node;
10716 if (explicitp == 1 || (explicitp && friendp))
10718 /* [dcl.fct.spec] (C++11) The explicit specifier shall be used only
10719 in the declaration of a constructor or conversion function within
10720 a class definition. */
10721 if (!current_class_type)
10722 error_at (declspecs->locations[ds_explicit],
10723 "%<explicit%> outside class declaration");
10724 else if (friendp)
10725 error_at (declspecs->locations[ds_explicit],
10726 "%<explicit%> in friend declaration");
10727 else
10728 error_at (declspecs->locations[ds_explicit],
10729 "only declarations of constructors and conversion operators "
10730 "can be %<explicit%>");
10731 explicitp = 0;
10734 if (storage_class == sc_mutable)
10736 if (decl_context != FIELD || friendp)
10738 error ("non-member %qs cannot be declared %<mutable%>", name);
10739 storage_class = sc_none;
10741 else if (decl_context == TYPENAME || typedef_p)
10743 error ("non-object member %qs cannot be declared %<mutable%>", name);
10744 storage_class = sc_none;
10746 else if (TREE_CODE (type) == FUNCTION_TYPE
10747 || TREE_CODE (type) == METHOD_TYPE)
10749 error ("function %qs cannot be declared %<mutable%>", name);
10750 storage_class = sc_none;
10752 else if (staticp)
10754 error ("static %qs cannot be declared %<mutable%>", name);
10755 storage_class = sc_none;
10757 else if (type_quals & TYPE_QUAL_CONST)
10759 error ("const %qs cannot be declared %<mutable%>", name);
10760 storage_class = sc_none;
10762 else if (TREE_CODE (type) == REFERENCE_TYPE)
10764 permerror (input_location, "reference %qs cannot be declared "
10765 "%<mutable%>", name);
10766 storage_class = sc_none;
10770 /* If this is declaring a typedef name, return a TYPE_DECL. */
10771 if (typedef_p && decl_context != TYPENAME)
10773 tree decl;
10775 /* This declaration:
10777 typedef void f(int) const;
10779 declares a function type which is not a member of any
10780 particular class, but which is cv-qualified; for
10781 example "f S::*" declares a pointer to a const-qualified
10782 member function of S. We record the cv-qualification in the
10783 function type. */
10784 if ((rqual || memfn_quals) && TREE_CODE (type) == FUNCTION_TYPE)
10786 type = apply_memfn_quals (type, memfn_quals, rqual);
10788 /* We have now dealt with these qualifiers. */
10789 memfn_quals = TYPE_UNQUALIFIED;
10790 rqual = REF_QUAL_NONE;
10793 if (type_uses_auto (type))
10795 error ("typedef declared %<auto%>");
10796 type = error_mark_node;
10799 if (reqs)
10800 error_at (location_of (reqs), "requires-clause on typedef");
10802 if (decl_context == FIELD)
10803 decl = build_lang_decl (TYPE_DECL, unqualified_id, type);
10804 else
10805 decl = build_decl (input_location, TYPE_DECL, unqualified_id, type);
10806 if (id_declarator && declarator->u.id.qualifying_scope) {
10807 error_at (DECL_SOURCE_LOCATION (decl),
10808 "typedef name may not be a nested-name-specifier");
10809 TREE_TYPE (decl) = error_mark_node;
10812 if (decl_context != FIELD)
10814 if (!current_function_decl)
10815 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
10816 else if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (current_function_decl)
10817 || (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P
10818 (current_function_decl)))
10819 /* The TYPE_DECL is "abstract" because there will be
10820 clones of this constructor/destructor, and there will
10821 be copies of this TYPE_DECL generated in those
10822 clones. The decloning optimization (for space) may
10823 revert this subsequently if it determines that
10824 the clones should share a common implementation. */
10825 DECL_ABSTRACT_P (decl) = true;
10827 else if (current_class_type
10828 && constructor_name_p (unqualified_id, current_class_type))
10829 permerror (input_location, "ISO C++ forbids nested type %qD with same name "
10830 "as enclosing class",
10831 unqualified_id);
10833 /* If the user declares "typedef struct {...} foo" then the
10834 struct will have an anonymous name. Fill that name in now.
10835 Nothing can refer to it, so nothing needs know about the name
10836 change. */
10837 if (type != error_mark_node
10838 && unqualified_id
10839 && TYPE_NAME (type)
10840 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10841 && TYPE_UNNAMED_P (type)
10842 && declspecs->type_definition_p
10843 && attributes_naming_typedef_ok (*attrlist)
10844 && cp_type_quals (type) == TYPE_UNQUALIFIED)
10846 tree t;
10848 /* Replace the anonymous name with the real name everywhere. */
10849 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
10851 if (anon_aggrname_p (TYPE_IDENTIFIER (t)))
10852 /* We do not rename the debug info representing the
10853 unnamed tagged type because the standard says in
10854 [dcl.typedef] that the naming applies only for
10855 linkage purposes. */
10856 /*debug_hooks->set_name (t, decl);*/
10857 TYPE_NAME (t) = decl;
10860 if (TYPE_LANG_SPECIFIC (type))
10861 TYPE_WAS_UNNAMED (type) = 1;
10863 /* If this is a typedef within a template class, the nested
10864 type is a (non-primary) template. The name for the
10865 template needs updating as well. */
10866 if (TYPE_LANG_SPECIFIC (type) && CLASSTYPE_TEMPLATE_INFO (type))
10867 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type))
10868 = TYPE_IDENTIFIER (type);
10870 /* Adjust linkage now that we aren't unnamed anymore. */
10871 reset_type_linkage (type);
10873 /* FIXME remangle member functions; member functions of a
10874 type with external linkage have external linkage. */
10877 if (signed_p
10878 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
10879 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
10881 bad_specifiers (decl, BSP_TYPE, virtualp,
10882 memfn_quals != TYPE_UNQUALIFIED,
10883 inlinep, friendp, raises != NULL_TREE);
10885 if (decl_spec_seq_has_spec_p (declspecs, ds_alias))
10886 /* Acknowledge that this was written:
10887 `using analias = atype;'. */
10888 TYPE_DECL_ALIAS_P (decl) = 1;
10890 return decl;
10893 /* Detect the case of an array type of unspecified size
10894 which came, as such, direct from a typedef name.
10895 We must copy the type, so that the array's domain can be
10896 individually set by the object's initializer. */
10898 if (type && typedef_type
10899 && TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
10900 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
10901 type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
10903 /* Detect where we're using a typedef of function type to declare a
10904 function. PARMS will not be set, so we must create it now. */
10906 if (type == typedef_type && TREE_CODE (type) == FUNCTION_TYPE)
10908 tree decls = NULL_TREE;
10909 tree args;
10911 for (args = TYPE_ARG_TYPES (type);
10912 args && args != void_list_node;
10913 args = TREE_CHAIN (args))
10915 tree decl = cp_build_parm_decl (NULL_TREE, TREE_VALUE (args));
10917 DECL_CHAIN (decl) = decls;
10918 decls = decl;
10921 parms = nreverse (decls);
10923 if (decl_context != TYPENAME)
10925 /* The qualifiers on the function type become the qualifiers on
10926 the non-static member function. */
10927 memfn_quals |= type_memfn_quals (type);
10928 rqual = type_memfn_rqual (type);
10929 type_quals = TYPE_UNQUALIFIED;
10933 /* If this is a type name (such as, in a cast or sizeof),
10934 compute the type and return it now. */
10936 if (decl_context == TYPENAME)
10938 /* Note that here we don't care about type_quals. */
10940 /* Special case: "friend class foo" looks like a TYPENAME context. */
10941 if (friendp)
10943 if (inlinep)
10945 error ("%<inline%> specified for friend class declaration");
10946 inlinep = 0;
10949 if (!current_aggr)
10951 /* Don't allow friend declaration without a class-key. */
10952 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
10953 permerror (input_location, "template parameters cannot be friends");
10954 else if (TREE_CODE (type) == TYPENAME_TYPE)
10955 permerror (input_location, "friend declaration requires class-key, "
10956 "i.e. %<friend class %T::%D%>",
10957 TYPE_CONTEXT (type), TYPENAME_TYPE_FULLNAME (type));
10958 else
10959 permerror (input_location, "friend declaration requires class-key, "
10960 "i.e. %<friend %#T%>",
10961 type);
10964 /* Only try to do this stuff if we didn't already give up. */
10965 if (type != integer_type_node)
10967 /* A friendly class? */
10968 if (current_class_type)
10969 make_friend_class (current_class_type, TYPE_MAIN_VARIANT (type),
10970 /*complain=*/true);
10971 else
10972 error ("trying to make class %qT a friend of global scope",
10973 type);
10975 type = void_type_node;
10978 else if (memfn_quals || rqual)
10980 if (ctype == NULL_TREE
10981 && TREE_CODE (type) == METHOD_TYPE)
10982 ctype = TYPE_METHOD_BASETYPE (type);
10984 if (ctype)
10985 type = build_memfn_type (type, ctype, memfn_quals, rqual);
10986 /* Core issue #547: need to allow this in template type args.
10987 Allow it in general in C++11 for alias-declarations. */
10988 else if ((template_type_arg || cxx_dialect >= cxx11)
10989 && TREE_CODE (type) == FUNCTION_TYPE)
10990 type = apply_memfn_quals (type, memfn_quals, rqual);
10991 else
10992 error ("invalid qualifiers on non-member function type");
10995 if (reqs)
10996 error_at (location_of (reqs), "requires-clause on type-id");
10998 return type;
11000 else if (unqualified_id == NULL_TREE && decl_context != PARM
11001 && decl_context != CATCHPARM
11002 && TREE_CODE (type) != UNION_TYPE
11003 && ! bitfield)
11005 error ("abstract declarator %qT used as declaration", type);
11006 return error_mark_node;
11009 /* Only functions may be declared using an operator-function-id. */
11010 if (unqualified_id
11011 && IDENTIFIER_OPNAME_P (unqualified_id)
11012 && TREE_CODE (type) != FUNCTION_TYPE
11013 && TREE_CODE (type) != METHOD_TYPE)
11015 error ("declaration of %qD as non-function", unqualified_id);
11016 return error_mark_node;
11019 if (reqs
11020 && TREE_CODE (type) != FUNCTION_TYPE
11021 && TREE_CODE (type) != METHOD_TYPE)
11022 error_at (location_of (reqs),
11023 "requires-clause on declaration of non-function type %qT",
11024 type);
11026 /* We don't check parameter types here because we can emit a better
11027 error message later. */
11028 if (decl_context != PARM)
11030 type = check_var_type (unqualified_id, type);
11031 if (type == error_mark_node)
11032 return error_mark_node;
11035 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
11036 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
11038 if (decl_context == PARM || decl_context == CATCHPARM)
11040 if (ctype || in_namespace)
11041 error ("cannot use %<::%> in parameter declaration");
11043 if (type_uses_auto (type))
11045 if (cxx_dialect >= cxx14)
11046 error ("%<auto%> parameter not permitted in this context");
11047 else
11048 error ("parameter declared %<auto%>");
11049 type = error_mark_node;
11052 /* A parameter declared as an array of T is really a pointer to T.
11053 One declared as a function is really a pointer to a function.
11054 One declared as a member is really a pointer to member. */
11056 if (TREE_CODE (type) == ARRAY_TYPE)
11058 /* Transfer const-ness of array into that of type pointed to. */
11059 type = build_pointer_type (TREE_TYPE (type));
11060 type_quals = TYPE_UNQUALIFIED;
11061 array_parameter_p = true;
11063 else if (TREE_CODE (type) == FUNCTION_TYPE)
11064 type = build_pointer_type (type);
11067 if (ctype && TREE_CODE (type) == FUNCTION_TYPE && staticp < 2
11068 && !NEW_DELETE_OPNAME_P (unqualified_id))
11070 cp_cv_quals real_quals = memfn_quals;
11071 if (cxx_dialect < cxx14 && constexpr_p
11072 && sfk != sfk_constructor && sfk != sfk_destructor)
11073 real_quals |= TYPE_QUAL_CONST;
11074 type = build_memfn_type (type, ctype, real_quals, rqual);
11078 tree decl = NULL_TREE;
11080 if (decl_context == PARM)
11082 decl = cp_build_parm_decl (unqualified_id, type);
11083 DECL_ARRAY_PARAMETER_P (decl) = array_parameter_p;
11085 bad_specifiers (decl, BSP_PARM, virtualp,
11086 memfn_quals != TYPE_UNQUALIFIED,
11087 inlinep, friendp, raises != NULL_TREE);
11089 else if (decl_context == FIELD)
11091 if (!staticp && !friendp && TREE_CODE (type) != METHOD_TYPE)
11092 if (tree auto_node = type_uses_auto (type))
11094 location_t loc = declspecs->locations[ds_type_spec];
11095 if (CLASS_PLACEHOLDER_TEMPLATE (auto_node))
11096 error_at (loc, "invalid use of template-name %qE without an "
11097 "argument list",
11098 CLASS_PLACEHOLDER_TEMPLATE (auto_node));
11099 else
11100 error_at (loc, "non-static data member declared with "
11101 "placeholder %qT", auto_node);
11102 type = error_mark_node;
11105 /* The C99 flexible array extension. */
11106 if (!staticp && TREE_CODE (type) == ARRAY_TYPE
11107 && TYPE_DOMAIN (type) == NULL_TREE)
11109 if (ctype
11110 && (TREE_CODE (ctype) == UNION_TYPE
11111 || TREE_CODE (ctype) == QUAL_UNION_TYPE))
11113 error ("flexible array member in union");
11114 type = error_mark_node;
11116 else
11118 /* Flexible array member has a null domain. */
11119 type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
11123 if (type == error_mark_node)
11125 /* Happens when declaring arrays of sizes which
11126 are error_mark_node, for example. */
11127 decl = NULL_TREE;
11129 else if (in_namespace && !friendp)
11131 /* Something like struct S { int N::j; }; */
11132 error ("invalid use of %<::%>");
11133 return error_mark_node;
11135 else if (TREE_CODE (type) == FUNCTION_TYPE
11136 || TREE_CODE (type) == METHOD_TYPE)
11138 int publicp = 0;
11139 tree function_context;
11141 if (friendp == 0)
11143 /* This should never happen in pure C++ (the check
11144 could be an assert). It could happen in
11145 Objective-C++ if someone writes invalid code that
11146 uses a function declaration for an instance
11147 variable or property (instance variables and
11148 properties are parsed as FIELD_DECLs, but they are
11149 part of an Objective-C class, not a C++ class).
11150 That code is invalid and is caught by this
11151 check. */
11152 if (!ctype)
11154 error ("declaration of function %qD in invalid context",
11155 unqualified_id);
11156 return error_mark_node;
11159 /* ``A union may [ ... ] not [ have ] virtual functions.''
11160 ARM 9.5 */
11161 if (virtualp && TREE_CODE (ctype) == UNION_TYPE)
11163 error ("function %qD declared %<virtual%> inside a union",
11164 unqualified_id);
11165 return error_mark_node;
11168 if (NEW_DELETE_OPNAME_P (unqualified_id))
11170 if (virtualp)
11172 error ("%qD cannot be declared %<virtual%>, since it "
11173 "is always static",
11174 unqualified_id);
11175 virtualp = 0;
11180 /* Check that the name used for a destructor makes sense. */
11181 if (sfk == sfk_destructor)
11183 tree uqname = id_declarator->u.id.unqualified_name;
11185 if (!ctype)
11187 gcc_assert (friendp);
11188 error ("expected qualified name in friend declaration "
11189 "for destructor %qD", uqname);
11190 return error_mark_node;
11193 if (!check_dtor_name (ctype, TREE_OPERAND (uqname, 0)))
11195 error ("declaration of %qD as member of %qT",
11196 uqname, ctype);
11197 return error_mark_node;
11199 if (concept_p)
11201 error ("a destructor cannot be %<concept%>");
11202 return error_mark_node;
11204 if (constexpr_p)
11206 error ("a destructor cannot be %<constexpr%>");
11207 return error_mark_node;
11210 else if (sfk == sfk_constructor && friendp && !ctype)
11212 error ("expected qualified name in friend declaration "
11213 "for constructor %qD",
11214 id_declarator->u.id.unqualified_name);
11215 return error_mark_node;
11217 if (sfk == sfk_constructor)
11218 if (concept_p)
11220 error ("a constructor cannot be %<concept%>");
11221 return error_mark_node;
11223 if (concept_p)
11225 error ("a concept cannot be a member function");
11226 concept_p = false;
11229 if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
11231 tree tmpl = TREE_OPERAND (unqualified_id, 0);
11232 if (variable_template_p (tmpl))
11234 error ("specialization of variable template %qD "
11235 "declared as function", tmpl);
11236 inform (DECL_SOURCE_LOCATION (tmpl),
11237 "variable template declared here");
11238 return error_mark_node;
11242 /* Tell grokfndecl if it needs to set TREE_PUBLIC on the node. */
11243 function_context = (ctype != NULL_TREE) ?
11244 decl_function_context (TYPE_MAIN_DECL (ctype)) : NULL_TREE;
11245 publicp = (! friendp || ! staticp)
11246 && function_context == NULL_TREE;
11248 if (late_return_type_p)
11249 TYPE_HAS_LATE_RETURN_TYPE (type) = 1;
11251 decl = grokfndecl (ctype, type,
11252 TREE_CODE (unqualified_id) != TEMPLATE_ID_EXPR
11253 ? unqualified_id : dname,
11254 parms,
11255 unqualified_id,
11256 reqs,
11257 virtualp, flags, memfn_quals, rqual, raises,
11258 friendp ? -1 : 0, friendp, publicp,
11259 inlinep | (2 * constexpr_p) | (4 * concept_p),
11260 initialized == SD_DELETED, sfk,
11261 funcdef_flag, template_count, in_namespace,
11262 attrlist, declarator->id_loc);
11263 decl = set_virt_specifiers (decl, virt_specifiers);
11264 if (decl == NULL_TREE)
11265 return error_mark_node;
11266 #if 0
11267 /* This clobbers the attrs stored in `decl' from `attrlist'. */
11268 /* The decl and setting of decl_attr is also turned off. */
11269 decl = build_decl_attribute_variant (decl, decl_attr);
11270 #endif
11272 /* [class.conv.ctor]
11274 A constructor declared without the function-specifier
11275 explicit that can be called with a single parameter
11276 specifies a conversion from the type of its first
11277 parameter to the type of its class. Such a constructor
11278 is called a converting constructor. */
11279 if (explicitp == 2)
11280 DECL_NONCONVERTING_P (decl) = 1;
11282 else if (!staticp && !dependent_type_p (type)
11283 && !COMPLETE_TYPE_P (complete_type (type))
11284 && (!complete_or_array_type_p (type)
11285 || initialized == 0))
11287 if (TREE_CODE (type) != ARRAY_TYPE
11288 || !COMPLETE_TYPE_P (TREE_TYPE (type)))
11290 if (unqualified_id)
11292 error ("field %qD has incomplete type %qT",
11293 unqualified_id, type);
11294 cxx_incomplete_type_inform (strip_array_types (type));
11296 else
11297 error ("name %qT has incomplete type", type);
11299 type = error_mark_node;
11300 decl = NULL_TREE;
11303 else
11305 if (friendp)
11307 error ("%qE is neither function nor member function; "
11308 "cannot be declared friend", unqualified_id);
11309 friendp = 0;
11311 decl = NULL_TREE;
11314 if (friendp)
11316 /* Friends are treated specially. */
11317 if (ctype == current_class_type)
11318 ; /* We already issued a permerror. */
11319 else if (decl && DECL_NAME (decl))
11321 if (template_class_depth (current_class_type) == 0)
11323 decl = check_explicit_specialization
11324 (unqualified_id, decl, template_count,
11325 2 * funcdef_flag + 4);
11326 if (decl == error_mark_node)
11327 return error_mark_node;
11330 decl = do_friend (ctype, unqualified_id, decl,
11331 *attrlist, flags,
11332 funcdef_flag);
11333 return decl;
11335 else
11336 return error_mark_node;
11339 /* Structure field. It may not be a function, except for C++. */
11341 if (decl == NULL_TREE)
11343 if (staticp)
11345 /* C++ allows static class members. All other work
11346 for this is done by grokfield. */
11347 decl = build_lang_decl_loc (declarator
11348 ? declarator->id_loc
11349 : input_location,
11350 VAR_DECL, unqualified_id, type);
11351 set_linkage_for_static_data_member (decl);
11352 /* Even if there is an in-class initialization, DECL
11353 is considered undefined until an out-of-class
11354 definition is provided. */
11355 DECL_EXTERNAL (decl) = 1;
11357 if (thread_p)
11359 CP_DECL_THREAD_LOCAL_P (decl) = true;
11360 if (!processing_template_decl)
11361 set_decl_tls_model (decl, decl_default_tls_model (decl));
11362 if (declspecs->gnu_thread_keyword_p)
11363 SET_DECL_GNU_TLS_P (decl);
11365 if (concept_p)
11366 error ("static data member %qE declared %<concept%>",
11367 unqualified_id);
11368 else if (constexpr_p && !initialized)
11370 error ("constexpr static data member %qD must have an "
11371 "initializer", decl);
11372 constexpr_p = false;
11375 else
11377 if (concept_p)
11378 error ("non-static data member %qE declared %<concept%>",
11379 unqualified_id);
11380 else if (constexpr_p)
11382 error ("non-static data member %qE declared %<constexpr%>",
11383 unqualified_id);
11384 constexpr_p = false;
11386 decl = build_decl (input_location,
11387 FIELD_DECL, unqualified_id, type);
11388 DECL_NONADDRESSABLE_P (decl) = bitfield;
11389 if (bitfield && !unqualified_id)
11390 TREE_NO_WARNING (decl) = 1;
11392 if (storage_class == sc_mutable)
11394 DECL_MUTABLE_P (decl) = 1;
11395 storage_class = sc_none;
11398 if (initialized)
11400 /* An attempt is being made to initialize a non-static
11401 member. This is new in C++11. */
11402 maybe_warn_cpp0x (CPP0X_NSDMI);
11404 /* If this has been parsed with static storage class, but
11405 errors forced staticp to be cleared, ensure NSDMI is
11406 not present. */
11407 if (declspecs->storage_class == sc_static)
11408 DECL_INITIAL (decl) = error_mark_node;
11412 bad_specifiers (decl, BSP_FIELD, virtualp,
11413 memfn_quals != TYPE_UNQUALIFIED,
11414 inlinep, friendp, raises != NULL_TREE);
11417 else if (TREE_CODE (type) == FUNCTION_TYPE
11418 || TREE_CODE (type) == METHOD_TYPE)
11420 tree original_name;
11421 int publicp = 0;
11423 if (!unqualified_id)
11424 return error_mark_node;
11426 if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
11427 original_name = dname;
11428 else
11429 original_name = unqualified_id;
11431 if (storage_class == sc_auto)
11432 error ("storage class %<auto%> invalid for function %qs", name);
11433 else if (storage_class == sc_register)
11434 error ("storage class %<register%> invalid for function %qs", name);
11435 else if (thread_p)
11437 if (declspecs->gnu_thread_keyword_p)
11438 error ("storage class %<__thread%> invalid for function %qs",
11439 name);
11440 else
11441 error ("storage class %<thread_local%> invalid for function %qs",
11442 name);
11445 if (virt_specifiers)
11446 error ("virt-specifiers in %qs not allowed outside a class definition", name);
11447 /* Function declaration not at top level.
11448 Storage classes other than `extern' are not allowed
11449 and `extern' makes no difference. */
11450 if (! toplevel_bindings_p ()
11451 && (storage_class == sc_static
11452 || decl_spec_seq_has_spec_p (declspecs, ds_inline))
11453 && pedantic)
11455 if (storage_class == sc_static)
11456 pedwarn (input_location, OPT_Wpedantic,
11457 "%<static%> specifier invalid for function %qs "
11458 "declared out of global scope", name);
11459 else
11460 pedwarn (input_location, OPT_Wpedantic,
11461 "%<inline%> specifier invalid for function %qs "
11462 "declared out of global scope", name);
11465 if (ctype == NULL_TREE)
11467 if (virtualp)
11469 error ("virtual non-class function %qs", name);
11470 virtualp = 0;
11472 else if (sfk == sfk_constructor
11473 || sfk == sfk_destructor)
11475 error (funcdef_flag
11476 ? G_("%qs defined in a non-class scope")
11477 : G_("%qs declared in a non-class scope"), name);
11478 sfk = sfk_none;
11482 /* Record whether the function is public. */
11483 publicp = (ctype != NULL_TREE
11484 || storage_class != sc_static);
11486 if (late_return_type_p)
11487 TYPE_HAS_LATE_RETURN_TYPE (type) = 1;
11489 decl = grokfndecl (ctype, type, original_name, parms, unqualified_id,
11490 reqs, virtualp, flags, memfn_quals, rqual, raises,
11491 1, friendp,
11492 publicp,
11493 inlinep | (2 * constexpr_p) | (4 * concept_p),
11494 initialized == SD_DELETED,
11495 sfk,
11496 funcdef_flag,
11497 template_count, in_namespace, attrlist,
11498 declarator->id_loc);
11499 if (decl == NULL_TREE)
11500 return error_mark_node;
11502 if (staticp == 1)
11504 int invalid_static = 0;
11506 /* Don't allow a static member function in a class, and forbid
11507 declaring main to be static. */
11508 if (TREE_CODE (type) == METHOD_TYPE)
11510 permerror (input_location, "cannot declare member function %qD to have "
11511 "static linkage", decl);
11512 invalid_static = 1;
11514 else if (current_function_decl)
11516 /* 7.1.1: There can be no static function declarations within a
11517 block. */
11518 error ("cannot declare static function inside another function");
11519 invalid_static = 1;
11522 if (invalid_static)
11524 staticp = 0;
11525 storage_class = sc_none;
11529 else
11531 /* It's a variable. */
11533 /* An uninitialized decl with `extern' is a reference. */
11534 decl = grokvardecl (type, dname, unqualified_id,
11535 declspecs,
11536 initialized,
11537 ((type_quals & TYPE_QUAL_CONST) != 0) | (2 * concept_p),
11538 template_count,
11539 ctype ? ctype : in_namespace);
11540 if (decl == NULL_TREE)
11541 return error_mark_node;
11543 bad_specifiers (decl, BSP_VAR, virtualp,
11544 memfn_quals != TYPE_UNQUALIFIED,
11545 inlinep, friendp, raises != NULL_TREE);
11547 if (ctype)
11549 DECL_CONTEXT (decl) = ctype;
11550 if (staticp == 1)
11552 permerror (input_location, "%<static%> may not be used when defining "
11553 "(as opposed to declaring) a static data member");
11554 staticp = 0;
11555 storage_class = sc_none;
11557 if (storage_class == sc_register && TREE_STATIC (decl))
11559 error ("static member %qD declared %<register%>", decl);
11560 storage_class = sc_none;
11562 if (storage_class == sc_extern && pedantic)
11564 pedwarn (input_location, OPT_Wpedantic,
11565 "cannot explicitly declare member %q#D to have "
11566 "extern linkage", decl);
11567 storage_class = sc_none;
11570 else if (constexpr_p && DECL_EXTERNAL (decl))
11572 error ("declaration of constexpr variable %qD is not a definition",
11573 decl);
11574 constexpr_p = false;
11578 if (VAR_P (decl) && !initialized)
11579 if (tree auto_node = type_uses_auto (type))
11581 location_t loc = declspecs->locations[ds_type_spec];
11582 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
11584 error_at (loc, "invalid use of template-name %qE without an "
11585 "argument list", tmpl);
11586 inform (loc, "class template argument deduction "
11587 "requires an initializer");
11589 else
11590 error_at (loc, "declaration of %q#D has no initializer", decl);
11591 TREE_TYPE (decl) = error_mark_node;
11594 if (storage_class == sc_extern && initialized && !funcdef_flag)
11596 if (toplevel_bindings_p ())
11598 /* It's common practice (and completely valid) to have a const
11599 be initialized and declared extern. */
11600 if (!(type_quals & TYPE_QUAL_CONST))
11601 warning (0, "%qs initialized and declared %<extern%>", name);
11603 else
11605 error ("%qs has both %<extern%> and initializer", name);
11606 return error_mark_node;
11610 /* Record `register' declaration for warnings on &
11611 and in case doing stupid register allocation. */
11613 if (storage_class == sc_register)
11615 DECL_REGISTER (decl) = 1;
11616 /* Warn about register storage specifiers on PARM_DECLs. */
11617 if (TREE_CODE (decl) == PARM_DECL)
11619 if (cxx_dialect >= cxx1z)
11620 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
11621 "ISO C++1z does not allow %<register%> storage "
11622 "class specifier");
11623 else
11624 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
11625 "%<register%> storage class specifier used");
11628 else if (storage_class == sc_extern)
11629 DECL_THIS_EXTERN (decl) = 1;
11630 else if (storage_class == sc_static)
11631 DECL_THIS_STATIC (decl) = 1;
11633 /* Set constexpr flag on vars (functions got it in grokfndecl). */
11634 if (constexpr_p && VAR_P (decl))
11635 DECL_DECLARED_CONSTEXPR_P (decl) = true;
11637 /* Record constancy and volatility on the DECL itself . There's
11638 no need to do this when processing a template; we'll do this
11639 for the instantiated declaration based on the type of DECL. */
11640 if (!processing_template_decl)
11641 cp_apply_type_quals_to_decl (type_quals, decl);
11643 return decl;
11647 /* Subroutine of start_function. Ensure that each of the parameter
11648 types (as listed in PARMS) is complete, as is required for a
11649 function definition. */
11651 static void
11652 require_complete_types_for_parms (tree parms)
11654 for (; parms; parms = DECL_CHAIN (parms))
11656 if (dependent_type_p (TREE_TYPE (parms)))
11657 continue;
11658 if (!VOID_TYPE_P (TREE_TYPE (parms))
11659 && complete_type_or_else (TREE_TYPE (parms), parms))
11661 relayout_decl (parms);
11662 DECL_ARG_TYPE (parms) = type_passed_as (TREE_TYPE (parms));
11664 else
11665 /* grokparms or complete_type_or_else will have already issued
11666 an error. */
11667 TREE_TYPE (parms) = error_mark_node;
11671 /* Returns nonzero if T is a local variable. */
11674 local_variable_p (const_tree t)
11676 if ((VAR_P (t)
11677 /* A VAR_DECL with a context that is a _TYPE is a static data
11678 member. */
11679 && !TYPE_P (CP_DECL_CONTEXT (t))
11680 /* Any other non-local variable must be at namespace scope. */
11681 && !DECL_NAMESPACE_SCOPE_P (t))
11682 || (TREE_CODE (t) == PARM_DECL))
11683 return 1;
11685 return 0;
11688 /* Like local_variable_p, but suitable for use as a tree-walking
11689 function. */
11691 static tree
11692 local_variable_p_walkfn (tree *tp, int *walk_subtrees,
11693 void * /*data*/)
11695 if (local_variable_p (*tp)
11696 && (!DECL_ARTIFICIAL (*tp) || DECL_NAME (*tp) == this_identifier))
11697 return *tp;
11698 else if (TYPE_P (*tp))
11699 *walk_subtrees = 0;
11701 return NULL_TREE;
11704 /* Check that ARG, which is a default-argument expression for a
11705 parameter DECL, is valid. Returns ARG, or ERROR_MARK_NODE, if
11706 something goes wrong. DECL may also be a _TYPE node, rather than a
11707 DECL, if there is no DECL available. */
11709 tree
11710 check_default_argument (tree decl, tree arg, tsubst_flags_t complain)
11712 tree var;
11713 tree decl_type;
11715 if (TREE_CODE (arg) == DEFAULT_ARG)
11716 /* We get a DEFAULT_ARG when looking at an in-class declaration
11717 with a default argument. Ignore the argument for now; we'll
11718 deal with it after the class is complete. */
11719 return arg;
11721 if (TYPE_P (decl))
11723 decl_type = decl;
11724 decl = NULL_TREE;
11726 else
11727 decl_type = TREE_TYPE (decl);
11729 if (arg == error_mark_node
11730 || decl == error_mark_node
11731 || TREE_TYPE (arg) == error_mark_node
11732 || decl_type == error_mark_node)
11733 /* Something already went wrong. There's no need to check
11734 further. */
11735 return error_mark_node;
11737 /* [dcl.fct.default]
11739 A default argument expression is implicitly converted to the
11740 parameter type. */
11741 ++cp_unevaluated_operand;
11742 perform_implicit_conversion_flags (decl_type, arg, complain,
11743 LOOKUP_IMPLICIT);
11744 --cp_unevaluated_operand;
11746 /* Avoid redundant -Wzero-as-null-pointer-constant warnings at
11747 the call sites. */
11748 if (TYPE_PTR_OR_PTRMEM_P (decl_type)
11749 && null_ptr_cst_p (arg))
11750 return nullptr_node;
11752 /* [dcl.fct.default]
11754 Local variables shall not be used in default argument
11755 expressions.
11757 The keyword `this' shall not be used in a default argument of a
11758 member function. */
11759 var = cp_walk_tree_without_duplicates (&arg, local_variable_p_walkfn, NULL);
11760 if (var)
11762 if (complain & tf_warning_or_error)
11764 if (DECL_NAME (var) == this_identifier)
11765 permerror (input_location, "default argument %qE uses %qD",
11766 arg, var);
11767 else
11768 error ("default argument %qE uses local variable %qD", arg, var);
11770 return error_mark_node;
11773 /* All is well. */
11774 return arg;
11777 /* Returns a deprecated type used within TYPE, or NULL_TREE if none. */
11779 static tree
11780 type_is_deprecated (tree type)
11782 enum tree_code code;
11783 if (TREE_DEPRECATED (type))
11784 return type;
11785 if (TYPE_NAME (type))
11787 if (TREE_DEPRECATED (TYPE_NAME (type)))
11788 return type;
11789 else
11790 return NULL_TREE;
11793 /* Do warn about using typedefs to a deprecated class. */
11794 if (OVERLOAD_TYPE_P (type) && type != TYPE_MAIN_VARIANT (type))
11795 return type_is_deprecated (TYPE_MAIN_VARIANT (type));
11797 code = TREE_CODE (type);
11799 if (code == POINTER_TYPE || code == REFERENCE_TYPE
11800 || code == OFFSET_TYPE || code == FUNCTION_TYPE
11801 || code == METHOD_TYPE || code == ARRAY_TYPE)
11802 return type_is_deprecated (TREE_TYPE (type));
11804 if (TYPE_PTRMEMFUNC_P (type))
11805 return type_is_deprecated
11806 (TREE_TYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type))));
11808 return NULL_TREE;
11811 /* Decode the list of parameter types for a function type.
11812 Given the list of things declared inside the parens,
11813 return a list of types.
11815 If this parameter does not end with an ellipsis, we append
11816 void_list_node.
11818 *PARMS is set to the chain of PARM_DECLs created. */
11820 tree
11821 grokparms (tree parmlist, tree *parms)
11823 tree result = NULL_TREE;
11824 tree decls = NULL_TREE;
11825 tree parm;
11826 int any_error = 0;
11828 for (parm = parmlist; parm != NULL_TREE; parm = TREE_CHAIN (parm))
11830 tree type = NULL_TREE;
11831 tree init = TREE_PURPOSE (parm);
11832 tree decl = TREE_VALUE (parm);
11834 if (parm == void_list_node)
11835 break;
11837 if (! decl || TREE_TYPE (decl) == error_mark_node)
11838 continue;
11840 type = TREE_TYPE (decl);
11841 if (VOID_TYPE_P (type))
11843 if (same_type_p (type, void_type_node)
11844 && !init
11845 && !DECL_NAME (decl) && !result
11846 && TREE_CHAIN (parm) == void_list_node)
11847 /* DR 577: A parameter list consisting of a single
11848 unnamed parameter of non-dependent type 'void'. */
11849 break;
11850 else if (cv_qualified_p (type))
11851 error_at (DECL_SOURCE_LOCATION (decl),
11852 "invalid use of cv-qualified type %qT in "
11853 "parameter declaration", type);
11854 else
11855 error_at (DECL_SOURCE_LOCATION (decl),
11856 "invalid use of type %<void%> in parameter "
11857 "declaration");
11858 /* It's not a good idea to actually create parameters of
11859 type `void'; other parts of the compiler assume that a
11860 void type terminates the parameter list. */
11861 type = error_mark_node;
11862 TREE_TYPE (decl) = error_mark_node;
11865 if (type != error_mark_node)
11867 if (deprecated_state != DEPRECATED_SUPPRESS)
11869 tree deptype = type_is_deprecated (type);
11870 if (deptype)
11871 warn_deprecated_use (deptype, NULL_TREE);
11874 /* Top-level qualifiers on the parameters are
11875 ignored for function types. */
11876 type = cp_build_qualified_type (type, 0);
11877 if (TREE_CODE (type) == METHOD_TYPE)
11879 error ("parameter %qD invalidly declared method type", decl);
11880 type = build_pointer_type (type);
11881 TREE_TYPE (decl) = type;
11883 else if (abstract_virtuals_error (decl, type))
11884 any_error = 1; /* Seems like a good idea. */
11885 else if (POINTER_TYPE_P (type))
11887 /* [dcl.fct]/6, parameter types cannot contain pointers
11888 (references) to arrays of unknown bound. */
11889 tree t = TREE_TYPE (type);
11890 int ptr = TYPE_PTR_P (type);
11892 while (1)
11894 if (TYPE_PTR_P (t))
11895 ptr = 1;
11896 else if (TREE_CODE (t) != ARRAY_TYPE)
11897 break;
11898 else if (!TYPE_DOMAIN (t))
11899 break;
11900 t = TREE_TYPE (t);
11902 if (TREE_CODE (t) == ARRAY_TYPE)
11903 error (ptr
11904 ? G_("parameter %qD includes pointer to array of "
11905 "unknown bound %qT")
11906 : G_("parameter %qD includes reference to array of "
11907 "unknown bound %qT"),
11908 decl, t);
11911 if (any_error)
11912 init = NULL_TREE;
11913 else if (init && !processing_template_decl)
11914 init = check_default_argument (decl, init, tf_warning_or_error);
11917 DECL_CHAIN (decl) = decls;
11918 decls = decl;
11919 result = tree_cons (init, type, result);
11921 decls = nreverse (decls);
11922 result = nreverse (result);
11923 if (parm)
11924 result = chainon (result, void_list_node);
11925 *parms = decls;
11927 return result;
11931 /* D is a constructor or overloaded `operator='.
11933 Let T be the class in which D is declared. Then, this function
11934 returns:
11936 -1 if D's is an ill-formed constructor or copy assignment operator
11937 whose first parameter is of type `T'.
11938 0 if D is not a copy constructor or copy assignment
11939 operator.
11940 1 if D is a copy constructor or copy assignment operator whose
11941 first parameter is a reference to non-const qualified T.
11942 2 if D is a copy constructor or copy assignment operator whose
11943 first parameter is a reference to const qualified T.
11945 This function can be used as a predicate. Positive values indicate
11946 a copy constructor and nonzero values indicate a copy assignment
11947 operator. */
11950 copy_fn_p (const_tree d)
11952 tree args;
11953 tree arg_type;
11954 int result = 1;
11956 gcc_assert (DECL_FUNCTION_MEMBER_P (d));
11958 if (TREE_CODE (d) == TEMPLATE_DECL
11959 || (DECL_TEMPLATE_INFO (d)
11960 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d))))
11961 /* Instantiations of template member functions are never copy
11962 functions. Note that member functions of templated classes are
11963 represented as template functions internally, and we must
11964 accept those as copy functions. */
11965 return 0;
11967 args = FUNCTION_FIRST_USER_PARMTYPE (d);
11968 if (!args)
11969 return 0;
11971 arg_type = TREE_VALUE (args);
11972 if (arg_type == error_mark_node)
11973 return 0;
11975 if (TYPE_MAIN_VARIANT (arg_type) == DECL_CONTEXT (d))
11977 /* Pass by value copy assignment operator. */
11978 result = -1;
11980 else if (TREE_CODE (arg_type) == REFERENCE_TYPE
11981 && !TYPE_REF_IS_RVALUE (arg_type)
11982 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)) == DECL_CONTEXT (d))
11984 if (CP_TYPE_CONST_P (TREE_TYPE (arg_type)))
11985 result = 2;
11987 else
11988 return 0;
11990 args = TREE_CHAIN (args);
11992 if (args && args != void_list_node && !TREE_PURPOSE (args))
11993 /* There are more non-optional args. */
11994 return 0;
11996 return result;
11999 /* D is a constructor or overloaded `operator='.
12001 Let T be the class in which D is declared. Then, this function
12002 returns true when D is a move constructor or move assignment
12003 operator, false otherwise. */
12005 bool
12006 move_fn_p (const_tree d)
12008 gcc_assert (DECL_FUNCTION_MEMBER_P (d));
12010 if (cxx_dialect == cxx98)
12011 /* There are no move constructors if we are in C++98 mode. */
12012 return false;
12014 if (TREE_CODE (d) == TEMPLATE_DECL
12015 || (DECL_TEMPLATE_INFO (d)
12016 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d))))
12017 /* Instantiations of template member functions are never move
12018 functions. Note that member functions of templated classes are
12019 represented as template functions internally, and we must
12020 accept those as move functions. */
12021 return 0;
12023 return move_signature_fn_p (d);
12026 /* D is a constructor or overloaded `operator='.
12028 Then, this function returns true when D has the same signature as a move
12029 constructor or move assignment operator (because either it is such a
12030 ctor/op= or it is a template specialization with the same signature),
12031 false otherwise. */
12033 bool
12034 move_signature_fn_p (const_tree d)
12036 tree args;
12037 tree arg_type;
12038 bool result = false;
12040 args = FUNCTION_FIRST_USER_PARMTYPE (d);
12041 if (!args)
12042 return 0;
12044 arg_type = TREE_VALUE (args);
12045 if (arg_type == error_mark_node)
12046 return 0;
12048 if (TREE_CODE (arg_type) == REFERENCE_TYPE
12049 && TYPE_REF_IS_RVALUE (arg_type)
12050 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)),
12051 DECL_CONTEXT (d)))
12052 result = true;
12054 args = TREE_CHAIN (args);
12056 if (args && args != void_list_node && !TREE_PURPOSE (args))
12057 /* There are more non-optional args. */
12058 return false;
12060 return result;
12063 /* Remember any special properties of member function DECL. */
12065 void
12066 grok_special_member_properties (tree decl)
12068 tree class_type;
12070 if (!DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
12071 return;
12073 class_type = DECL_CONTEXT (decl);
12074 if (DECL_CONSTRUCTOR_P (decl))
12076 int ctor = copy_fn_p (decl);
12078 if (!DECL_ARTIFICIAL (decl))
12079 TYPE_HAS_USER_CONSTRUCTOR (class_type) = 1;
12081 if (ctor > 0)
12083 /* [class.copy]
12085 A non-template constructor for class X is a copy
12086 constructor if its first parameter is of type X&, const
12087 X&, volatile X& or const volatile X&, and either there
12088 are no other parameters or else all other parameters have
12089 default arguments. */
12090 TYPE_HAS_COPY_CTOR (class_type) = 1;
12091 if (user_provided_p (decl))
12092 TYPE_HAS_COMPLEX_COPY_CTOR (class_type) = 1;
12093 if (ctor > 1)
12094 TYPE_HAS_CONST_COPY_CTOR (class_type) = 1;
12096 else if (sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (decl)))
12097 TYPE_HAS_DEFAULT_CONSTRUCTOR (class_type) = 1;
12098 else if (move_fn_p (decl) && user_provided_p (decl))
12099 TYPE_HAS_COMPLEX_MOVE_CTOR (class_type) = 1;
12100 else if (is_list_ctor (decl))
12101 TYPE_HAS_LIST_CTOR (class_type) = 1;
12103 if (DECL_DECLARED_CONSTEXPR_P (decl)
12104 && !copy_fn_p (decl) && !move_fn_p (decl))
12105 TYPE_HAS_CONSTEXPR_CTOR (class_type) = 1;
12107 else if (DECL_OVERLOADED_OPERATOR_P (decl) == NOP_EXPR)
12109 /* [class.copy]
12111 A non-template assignment operator for class X is a copy
12112 assignment operator if its parameter is of type X, X&, const
12113 X&, volatile X& or const volatile X&. */
12115 int assop = copy_fn_p (decl);
12117 if (assop)
12119 TYPE_HAS_COPY_ASSIGN (class_type) = 1;
12120 if (user_provided_p (decl))
12121 TYPE_HAS_COMPLEX_COPY_ASSIGN (class_type) = 1;
12122 if (assop != 1)
12123 TYPE_HAS_CONST_COPY_ASSIGN (class_type) = 1;
12125 else if (move_fn_p (decl) && user_provided_p (decl))
12126 TYPE_HAS_COMPLEX_MOVE_ASSIGN (class_type) = 1;
12128 /* Destructors are handled in check_methods. */
12131 /* Check a constructor DECL has the correct form. Complains
12132 if the class has a constructor of the form X(X). */
12135 grok_ctor_properties (const_tree ctype, const_tree decl)
12137 int ctor_parm = copy_fn_p (decl);
12139 if (ctor_parm < 0)
12141 /* [class.copy]
12143 A declaration of a constructor for a class X is ill-formed if
12144 its first parameter is of type (optionally cv-qualified) X
12145 and either there are no other parameters or else all other
12146 parameters have default arguments.
12148 We *don't* complain about member template instantiations that
12149 have this form, though; they can occur as we try to decide
12150 what constructor to use during overload resolution. Since
12151 overload resolution will never prefer such a constructor to
12152 the non-template copy constructor (which is either explicitly
12153 or implicitly defined), there's no need to worry about their
12154 existence. Theoretically, they should never even be
12155 instantiated, but that's hard to forestall. */
12156 error ("invalid constructor; you probably meant %<%T (const %T&)%>",
12157 ctype, ctype);
12158 return 0;
12161 return 1;
12164 /* An operator with this code is unary, but can also be binary. */
12166 static int
12167 ambi_op_p (enum tree_code code)
12169 return (code == INDIRECT_REF
12170 || code == ADDR_EXPR
12171 || code == UNARY_PLUS_EXPR
12172 || code == NEGATE_EXPR
12173 || code == PREINCREMENT_EXPR
12174 || code == PREDECREMENT_EXPR);
12177 /* An operator with this name can only be unary. */
12179 static int
12180 unary_op_p (enum tree_code code)
12182 return (code == TRUTH_NOT_EXPR
12183 || code == BIT_NOT_EXPR
12184 || code == COMPONENT_REF
12185 || code == TYPE_EXPR);
12188 /* DECL is a declaration for an overloaded operator. If COMPLAIN is true,
12189 errors are issued for invalid declarations. */
12191 bool
12192 grok_op_properties (tree decl, bool complain)
12194 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
12195 tree argtype;
12196 int methodp = (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE);
12197 tree name = DECL_NAME (decl);
12198 enum tree_code operator_code;
12199 int arity;
12200 bool ellipsis_p;
12201 tree class_type;
12203 /* Count the number of arguments and check for ellipsis. */
12204 for (argtype = argtypes, arity = 0;
12205 argtype && argtype != void_list_node;
12206 argtype = TREE_CHAIN (argtype))
12207 ++arity;
12208 ellipsis_p = !argtype;
12210 class_type = DECL_CONTEXT (decl);
12211 if (class_type && !CLASS_TYPE_P (class_type))
12212 class_type = NULL_TREE;
12214 if (DECL_CONV_FN_P (decl))
12215 operator_code = TYPE_EXPR;
12216 else
12219 #define DEF_OPERATOR(NAME, CODE, MANGLING, ARITY, ASSN_P) \
12220 if (ansi_opname (CODE) == name) \
12222 operator_code = (CODE); \
12223 break; \
12225 else if (ansi_assopname (CODE) == name) \
12227 operator_code = (CODE); \
12228 DECL_ASSIGNMENT_OPERATOR_P (decl) = 1; \
12229 break; \
12232 #include "operators.def"
12233 #undef DEF_OPERATOR
12235 gcc_unreachable ();
12237 while (0);
12238 gcc_assert (operator_code != MAX_TREE_CODES);
12239 SET_OVERLOADED_OPERATOR_CODE (decl, operator_code);
12241 if (class_type)
12242 switch (operator_code)
12244 case NEW_EXPR:
12245 TYPE_HAS_NEW_OPERATOR (class_type) = 1;
12246 break;
12248 case DELETE_EXPR:
12249 TYPE_GETS_DELETE (class_type) |= 1;
12250 break;
12252 case VEC_NEW_EXPR:
12253 TYPE_HAS_ARRAY_NEW_OPERATOR (class_type) = 1;
12254 break;
12256 case VEC_DELETE_EXPR:
12257 TYPE_GETS_DELETE (class_type) |= 2;
12258 break;
12260 default:
12261 break;
12264 /* [basic.std.dynamic.allocation]/1:
12266 A program is ill-formed if an allocation function is declared
12267 in a namespace scope other than global scope or declared static
12268 in global scope.
12270 The same also holds true for deallocation functions. */
12271 if (operator_code == NEW_EXPR || operator_code == VEC_NEW_EXPR
12272 || operator_code == DELETE_EXPR || operator_code == VEC_DELETE_EXPR)
12274 if (DECL_NAMESPACE_SCOPE_P (decl))
12276 if (CP_DECL_CONTEXT (decl) != global_namespace)
12278 error ("%qD may not be declared within a namespace", decl);
12279 return false;
12281 else if (!TREE_PUBLIC (decl))
12283 error ("%qD may not be declared as static", decl);
12284 return false;
12289 if (operator_code == NEW_EXPR || operator_code == VEC_NEW_EXPR)
12291 TREE_TYPE (decl) = coerce_new_type (TREE_TYPE (decl));
12292 DECL_IS_OPERATOR_NEW (decl) = 1;
12294 else if (operator_code == DELETE_EXPR || operator_code == VEC_DELETE_EXPR)
12295 TREE_TYPE (decl) = coerce_delete_type (TREE_TYPE (decl));
12296 else
12298 /* An operator function must either be a non-static member function
12299 or have at least one parameter of a class, a reference to a class,
12300 an enumeration, or a reference to an enumeration. 13.4.0.6 */
12301 if (! methodp || DECL_STATIC_FUNCTION_P (decl))
12303 if (operator_code == TYPE_EXPR
12304 || operator_code == CALL_EXPR
12305 || operator_code == COMPONENT_REF
12306 || operator_code == ARRAY_REF
12307 || operator_code == NOP_EXPR)
12309 error ("%qD must be a nonstatic member function", decl);
12310 return false;
12312 else
12314 tree p;
12316 if (DECL_STATIC_FUNCTION_P (decl))
12318 error ("%qD must be either a non-static member "
12319 "function or a non-member function", decl);
12320 return false;
12323 for (p = argtypes; p && p != void_list_node; p = TREE_CHAIN (p))
12325 tree arg = non_reference (TREE_VALUE (p));
12326 if (arg == error_mark_node)
12327 return false;
12329 /* MAYBE_CLASS_TYPE_P, rather than CLASS_TYPE_P, is used
12330 because these checks are performed even on
12331 template functions. */
12332 if (MAYBE_CLASS_TYPE_P (arg)
12333 || TREE_CODE (arg) == ENUMERAL_TYPE)
12334 break;
12337 if (!p || p == void_list_node)
12339 if (complain)
12340 error ("%qD must have an argument of class or "
12341 "enumerated type", decl);
12342 return false;
12347 /* There are no restrictions on the arguments to an overloaded
12348 "operator ()". */
12349 if (operator_code == CALL_EXPR)
12350 return true;
12352 /* Warn about conversion operators that will never be used. */
12353 if (IDENTIFIER_TYPENAME_P (name)
12354 && ! DECL_TEMPLATE_INFO (decl)
12355 && warn_conversion
12356 /* Warn only declaring the function; there is no need to
12357 warn again about out-of-class definitions. */
12358 && class_type == current_class_type)
12360 tree t = TREE_TYPE (name);
12361 int ref = (TREE_CODE (t) == REFERENCE_TYPE);
12363 if (ref)
12364 t = TYPE_MAIN_VARIANT (TREE_TYPE (t));
12366 if (VOID_TYPE_P (t))
12367 warning (OPT_Wconversion,
12369 ? G_("conversion to a reference to void "
12370 "will never use a type conversion operator")
12371 : G_("conversion to void "
12372 "will never use a type conversion operator"));
12373 else if (class_type)
12375 if (t == class_type)
12376 warning (OPT_Wconversion,
12378 ? G_("conversion to a reference to the same type "
12379 "will never use a type conversion operator")
12380 : G_("conversion to the same type "
12381 "will never use a type conversion operator"));
12382 /* Don't force t to be complete here. */
12383 else if (MAYBE_CLASS_TYPE_P (t)
12384 && COMPLETE_TYPE_P (t)
12385 && DERIVED_FROM_P (t, class_type))
12386 warning (OPT_Wconversion,
12388 ? G_("conversion to a reference to a base class "
12389 "will never use a type conversion operator")
12390 : G_("conversion to a base class "
12391 "will never use a type conversion operator"));
12396 if (operator_code == COND_EXPR)
12398 /* 13.4.0.3 */
12399 error ("ISO C++ prohibits overloading operator ?:");
12400 return false;
12402 else if (ellipsis_p)
12404 error ("%qD must not have variable number of arguments", decl);
12405 return false;
12407 else if (ambi_op_p (operator_code))
12409 if (arity == 1)
12410 /* We pick the one-argument operator codes by default, so
12411 we don't have to change anything. */
12413 else if (arity == 2)
12415 /* If we thought this was a unary operator, we now know
12416 it to be a binary operator. */
12417 switch (operator_code)
12419 case INDIRECT_REF:
12420 operator_code = MULT_EXPR;
12421 break;
12423 case ADDR_EXPR:
12424 operator_code = BIT_AND_EXPR;
12425 break;
12427 case UNARY_PLUS_EXPR:
12428 operator_code = PLUS_EXPR;
12429 break;
12431 case NEGATE_EXPR:
12432 operator_code = MINUS_EXPR;
12433 break;
12435 case PREINCREMENT_EXPR:
12436 operator_code = POSTINCREMENT_EXPR;
12437 break;
12439 case PREDECREMENT_EXPR:
12440 operator_code = POSTDECREMENT_EXPR;
12441 break;
12443 default:
12444 gcc_unreachable ();
12447 SET_OVERLOADED_OPERATOR_CODE (decl, operator_code);
12449 if ((operator_code == POSTINCREMENT_EXPR
12450 || operator_code == POSTDECREMENT_EXPR)
12451 && ! processing_template_decl
12452 && ! same_type_p (TREE_VALUE (TREE_CHAIN (argtypes)), integer_type_node))
12454 if (methodp)
12455 error ("postfix %qD must take %<int%> as its argument",
12456 decl);
12457 else
12458 error ("postfix %qD must take %<int%> as its second "
12459 "argument", decl);
12460 return false;
12463 else
12465 if (methodp)
12466 error ("%qD must take either zero or one argument", decl);
12467 else
12468 error ("%qD must take either one or two arguments", decl);
12469 return false;
12472 /* More Effective C++ rule 6. */
12473 if (warn_ecpp
12474 && (operator_code == POSTINCREMENT_EXPR
12475 || operator_code == POSTDECREMENT_EXPR
12476 || operator_code == PREINCREMENT_EXPR
12477 || operator_code == PREDECREMENT_EXPR))
12479 tree arg = TREE_VALUE (argtypes);
12480 tree ret = TREE_TYPE (TREE_TYPE (decl));
12481 if (methodp || TREE_CODE (arg) == REFERENCE_TYPE)
12482 arg = TREE_TYPE (arg);
12483 arg = TYPE_MAIN_VARIANT (arg);
12484 if (operator_code == PREINCREMENT_EXPR
12485 || operator_code == PREDECREMENT_EXPR)
12487 if (TREE_CODE (ret) != REFERENCE_TYPE
12488 || !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (ret)),
12489 arg))
12490 warning (OPT_Weffc__, "prefix %qD should return %qT", decl,
12491 build_reference_type (arg));
12493 else
12495 if (!same_type_p (TYPE_MAIN_VARIANT (ret), arg))
12496 warning (OPT_Weffc__, "postfix %qD should return %qT", decl, arg);
12500 else if (unary_op_p (operator_code))
12502 if (arity != 1)
12504 if (methodp)
12505 error ("%qD must take %<void%>", decl);
12506 else
12507 error ("%qD must take exactly one argument", decl);
12508 return false;
12511 else /* if (binary_op_p (operator_code)) */
12513 if (arity != 2)
12515 if (methodp)
12516 error ("%qD must take exactly one argument", decl);
12517 else
12518 error ("%qD must take exactly two arguments", decl);
12519 return false;
12522 /* More Effective C++ rule 7. */
12523 if (warn_ecpp
12524 && (operator_code == TRUTH_ANDIF_EXPR
12525 || operator_code == TRUTH_ORIF_EXPR
12526 || operator_code == COMPOUND_EXPR))
12527 warning (OPT_Weffc__, "user-defined %qD always evaluates both arguments",
12528 decl);
12531 /* Effective C++ rule 23. */
12532 if (warn_ecpp
12533 && arity == 2
12534 && !DECL_ASSIGNMENT_OPERATOR_P (decl)
12535 && (operator_code == PLUS_EXPR
12536 || operator_code == MINUS_EXPR
12537 || operator_code == TRUNC_DIV_EXPR
12538 || operator_code == MULT_EXPR
12539 || operator_code == TRUNC_MOD_EXPR)
12540 && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == REFERENCE_TYPE)
12541 warning (OPT_Weffc__, "%qD should return by value", decl);
12543 /* [over.oper]/8 */
12544 for (; argtypes && argtypes != void_list_node;
12545 argtypes = TREE_CHAIN (argtypes))
12546 if (TREE_PURPOSE (argtypes))
12548 TREE_PURPOSE (argtypes) = NULL_TREE;
12549 if (operator_code == POSTINCREMENT_EXPR
12550 || operator_code == POSTDECREMENT_EXPR)
12552 pedwarn (input_location, OPT_Wpedantic, "%qD cannot have default arguments",
12553 decl);
12555 else
12557 error ("%qD cannot have default arguments", decl);
12558 return false;
12562 return true;
12565 /* Return a string giving the keyword associate with CODE. */
12567 static const char *
12568 tag_name (enum tag_types code)
12570 switch (code)
12572 case record_type:
12573 return "struct";
12574 case class_type:
12575 return "class";
12576 case union_type:
12577 return "union";
12578 case enum_type:
12579 return "enum";
12580 case typename_type:
12581 return "typename";
12582 default:
12583 gcc_unreachable ();
12587 /* Name lookup in an elaborated-type-specifier (after the keyword
12588 indicated by TAG_CODE) has found the TYPE_DECL DECL. If the
12589 elaborated-type-specifier is invalid, issue a diagnostic and return
12590 error_mark_node; otherwise, return the *_TYPE to which it referred.
12591 If ALLOW_TEMPLATE_P is true, TYPE may be a class template. */
12593 tree
12594 check_elaborated_type_specifier (enum tag_types tag_code,
12595 tree decl,
12596 bool allow_template_p)
12598 tree type;
12600 /* In the case of:
12602 struct S { struct S *p; };
12604 name lookup will find the TYPE_DECL for the implicit "S::S"
12605 typedef. Adjust for that here. */
12606 if (DECL_SELF_REFERENCE_P (decl))
12607 decl = TYPE_NAME (TREE_TYPE (decl));
12609 type = TREE_TYPE (decl);
12611 /* Check TEMPLATE_TYPE_PARM first because DECL_IMPLICIT_TYPEDEF_P
12612 is false for this case as well. */
12613 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
12615 error ("using template type parameter %qT after %qs",
12616 type, tag_name (tag_code));
12617 return error_mark_node;
12619 /* Accept template template parameters. */
12620 else if (allow_template_p
12621 && (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM
12622 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM))
12624 /* [dcl.type.elab]
12626 If the identifier resolves to a typedef-name or the
12627 simple-template-id resolves to an alias template
12628 specialization, the elaborated-type-specifier is ill-formed.
12630 In other words, the only legitimate declaration to use in the
12631 elaborated type specifier is the implicit typedef created when
12632 the type is declared. */
12633 else if (!DECL_IMPLICIT_TYPEDEF_P (decl)
12634 && !DECL_SELF_REFERENCE_P (decl)
12635 && tag_code != typename_type)
12637 if (alias_template_specialization_p (type))
12638 error ("using alias template specialization %qT after %qs",
12639 type, tag_name (tag_code));
12640 else
12641 error ("using typedef-name %qD after %qs", decl, tag_name (tag_code));
12642 inform (DECL_SOURCE_LOCATION (decl),
12643 "%qD has a previous declaration here", decl);
12644 return error_mark_node;
12646 else if (TREE_CODE (type) != RECORD_TYPE
12647 && TREE_CODE (type) != UNION_TYPE
12648 && tag_code != enum_type
12649 && tag_code != typename_type)
12651 error ("%qT referred to as %qs", type, tag_name (tag_code));
12652 inform (location_of (type), "%qT has a previous declaration here", type);
12653 return error_mark_node;
12655 else if (TREE_CODE (type) != ENUMERAL_TYPE
12656 && tag_code == enum_type)
12658 error ("%qT referred to as enum", type);
12659 inform (location_of (type), "%qT has a previous declaration here", type);
12660 return error_mark_node;
12662 else if (!allow_template_p
12663 && TREE_CODE (type) == RECORD_TYPE
12664 && CLASSTYPE_IS_TEMPLATE (type))
12666 /* If a class template appears as elaborated type specifier
12667 without a template header such as:
12669 template <class T> class C {};
12670 void f(class C); // No template header here
12672 then the required template argument is missing. */
12673 error ("template argument required for %<%s %T%>",
12674 tag_name (tag_code),
12675 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)));
12676 return error_mark_node;
12679 return type;
12682 /* Lookup NAME in elaborate type specifier in scope according to
12683 SCOPE and issue diagnostics if necessary.
12684 Return *_TYPE node upon success, NULL_TREE when the NAME is not
12685 found, and ERROR_MARK_NODE for type error. */
12687 static tree
12688 lookup_and_check_tag (enum tag_types tag_code, tree name,
12689 tag_scope scope, bool template_header_p)
12691 tree t;
12692 tree decl;
12693 if (scope == ts_global)
12695 /* First try ordinary name lookup, ignoring hidden class name
12696 injected via friend declaration. */
12697 decl = lookup_name_prefer_type (name, 2);
12698 decl = strip_using_decl (decl);
12699 /* If that fails, the name will be placed in the smallest
12700 non-class, non-function-prototype scope according to 3.3.1/5.
12701 We may already have a hidden name declared as friend in this
12702 scope. So lookup again but not ignoring hidden names.
12703 If we find one, that name will be made visible rather than
12704 creating a new tag. */
12705 if (!decl)
12706 decl = lookup_type_scope (name, ts_within_enclosing_non_class);
12708 else
12709 decl = lookup_type_scope (name, scope);
12711 if (decl
12712 && (DECL_CLASS_TEMPLATE_P (decl)
12713 /* If scope is ts_current we're defining a class, so ignore a
12714 template template parameter. */
12715 || (scope != ts_current
12716 && DECL_TEMPLATE_TEMPLATE_PARM_P (decl))))
12717 decl = DECL_TEMPLATE_RESULT (decl);
12719 if (decl && TREE_CODE (decl) == TYPE_DECL)
12721 /* Look for invalid nested type:
12722 class C {
12723 class C {};
12724 }; */
12725 if (scope == ts_current && DECL_SELF_REFERENCE_P (decl))
12727 error ("%qD has the same name as the class in which it is "
12728 "declared",
12729 decl);
12730 return error_mark_node;
12733 /* Two cases we need to consider when deciding if a class
12734 template is allowed as an elaborated type specifier:
12735 1. It is a self reference to its own class.
12736 2. It comes with a template header.
12738 For example:
12740 template <class T> class C {
12741 class C *c1; // DECL_SELF_REFERENCE_P is true
12742 class D;
12744 template <class U> class C; // template_header_p is true
12745 template <class T> class C<T>::D {
12746 class C *c2; // DECL_SELF_REFERENCE_P is true
12747 }; */
12749 t = check_elaborated_type_specifier (tag_code,
12750 decl,
12751 template_header_p
12752 | DECL_SELF_REFERENCE_P (decl));
12753 if (template_header_p && t && CLASS_TYPE_P (t)
12754 && (!CLASSTYPE_TEMPLATE_INFO (t)
12755 || (!PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)))))
12757 error ("%qT is not a template", t);
12758 inform (location_of (t), "previous declaration here");
12759 if (TYPE_CLASS_SCOPE_P (t)
12760 && CLASSTYPE_TEMPLATE_INFO (TYPE_CONTEXT (t)))
12761 inform (input_location,
12762 "perhaps you want to explicitly add %<%T::%>",
12763 TYPE_CONTEXT (t));
12764 t = error_mark_node;
12767 return t;
12769 else if (decl && TREE_CODE (decl) == TREE_LIST)
12771 error ("reference to %qD is ambiguous", name);
12772 print_candidates (decl);
12773 return error_mark_node;
12775 else
12776 return NULL_TREE;
12779 /* Get the struct, enum or union (TAG_CODE says which) with tag NAME.
12780 Define the tag as a forward-reference if it is not defined.
12782 If a declaration is given, process it here, and report an error if
12783 multiple declarations are not identical.
12785 SCOPE is TS_CURRENT when this is also a definition. Only look in
12786 the current frame for the name (since C++ allows new names in any
12787 scope.) It is TS_WITHIN_ENCLOSING_NON_CLASS if this is a friend
12788 declaration. Only look beginning from the current scope outward up
12789 till the nearest non-class scope. Otherwise it is TS_GLOBAL.
12791 TEMPLATE_HEADER_P is true when this declaration is preceded by
12792 a set of template parameters. */
12794 static tree
12795 xref_tag_1 (enum tag_types tag_code, tree name,
12796 tag_scope orig_scope, bool template_header_p)
12798 enum tree_code code;
12799 tree t;
12800 tree context = NULL_TREE;
12801 tag_scope scope;
12803 gcc_assert (identifier_p (name));
12805 switch (tag_code)
12807 case record_type:
12808 case class_type:
12809 code = RECORD_TYPE;
12810 break;
12811 case union_type:
12812 code = UNION_TYPE;
12813 break;
12814 case enum_type:
12815 code = ENUMERAL_TYPE;
12816 break;
12817 default:
12818 gcc_unreachable ();
12821 if (orig_scope == ts_lambda)
12822 scope = ts_current;
12823 else
12824 scope = orig_scope;
12826 /* In case of anonymous name, xref_tag is only called to
12827 make type node and push name. Name lookup is not required. */
12828 if (anon_aggrname_p (name))
12829 t = NULL_TREE;
12830 else
12831 t = lookup_and_check_tag (tag_code, name,
12832 scope, template_header_p);
12834 if (t == error_mark_node)
12835 return error_mark_node;
12837 if (scope != ts_current && t && current_class_type
12838 && template_class_depth (current_class_type)
12839 && template_header_p)
12841 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
12842 return t;
12844 /* Since SCOPE is not TS_CURRENT, we are not looking at a
12845 definition of this tag. Since, in addition, we are currently
12846 processing a (member) template declaration of a template
12847 class, we must be very careful; consider:
12849 template <class X>
12850 struct S1
12852 template <class U>
12853 struct S2
12854 { template <class V>
12855 friend struct S1; };
12857 Here, the S2::S1 declaration should not be confused with the
12858 outer declaration. In particular, the inner version should
12859 have a template parameter of level 2, not level 1. This
12860 would be particularly important if the member declaration
12861 were instead:
12863 template <class V = U> friend struct S1;
12865 say, when we should tsubst into `U' when instantiating
12866 S2. On the other hand, when presented with:
12868 template <class T>
12869 struct S1 {
12870 template <class U>
12871 struct S2 {};
12872 template <class U>
12873 friend struct S2;
12876 we must find the inner binding eventually. We
12877 accomplish this by making sure that the new type we
12878 create to represent this declaration has the right
12879 TYPE_CONTEXT. */
12880 context = TYPE_CONTEXT (t);
12881 t = NULL_TREE;
12884 if (! t)
12886 /* If no such tag is yet defined, create a forward-reference node
12887 and record it as the "definition".
12888 When a real declaration of this type is found,
12889 the forward-reference will be altered into a real type. */
12890 if (code == ENUMERAL_TYPE)
12892 error ("use of enum %q#D without previous declaration", name);
12893 return error_mark_node;
12895 else
12897 t = make_class_type (code);
12898 TYPE_CONTEXT (t) = context;
12899 if (orig_scope == ts_lambda)
12900 /* Remember that we're declaring a lambda to avoid bogus errors
12901 in push_template_decl. */
12902 CLASSTYPE_LAMBDA_EXPR (t) = error_mark_node;
12903 t = pushtag (name, t, scope);
12906 else
12908 if (template_header_p && MAYBE_CLASS_TYPE_P (t))
12910 /* Check that we aren't trying to overload a class with different
12911 constraints. */
12912 tree constr = NULL_TREE;
12913 if (current_template_parms)
12915 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
12916 constr = build_constraints (reqs, NULL_TREE);
12918 if (!redeclare_class_template (t, current_template_parms, constr))
12919 return error_mark_node;
12921 else if (!processing_template_decl
12922 && CLASS_TYPE_P (t)
12923 && CLASSTYPE_IS_TEMPLATE (t))
12925 error ("redeclaration of %qT as a non-template", t);
12926 inform (location_of (t), "previous declaration %qD", t);
12927 return error_mark_node;
12930 /* Make injected friend class visible. */
12931 if (scope != ts_within_enclosing_non_class
12932 && hidden_name_p (TYPE_NAME (t)))
12934 DECL_ANTICIPATED (TYPE_NAME (t)) = 0;
12935 DECL_FRIEND_P (TYPE_NAME (t)) = 0;
12937 if (TYPE_TEMPLATE_INFO (t))
12939 DECL_ANTICIPATED (TYPE_TI_TEMPLATE (t)) = 0;
12940 DECL_FRIEND_P (TYPE_TI_TEMPLATE (t)) = 0;
12945 return t;
12948 /* Wrapper for xref_tag_1. */
12950 tree
12951 xref_tag (enum tag_types tag_code, tree name,
12952 tag_scope scope, bool template_header_p)
12954 tree ret;
12955 bool subtime;
12956 subtime = timevar_cond_start (TV_NAME_LOOKUP);
12957 ret = xref_tag_1 (tag_code, name, scope, template_header_p);
12958 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
12959 return ret;
12963 tree
12964 xref_tag_from_type (tree old, tree id, tag_scope scope)
12966 enum tag_types tag_kind;
12968 if (TREE_CODE (old) == RECORD_TYPE)
12969 tag_kind = (CLASSTYPE_DECLARED_CLASS (old) ? class_type : record_type);
12970 else
12971 tag_kind = union_type;
12973 if (id == NULL_TREE)
12974 id = TYPE_IDENTIFIER (old);
12976 return xref_tag (tag_kind, id, scope, false);
12979 /* Create the binfo hierarchy for REF with (possibly NULL) base list
12980 BASE_LIST. For each element on BASE_LIST the TREE_PURPOSE is an
12981 access_* node, and the TREE_VALUE is the type of the base-class.
12982 Non-NULL TREE_TYPE indicates virtual inheritance. */
12984 void
12985 xref_basetypes (tree ref, tree base_list)
12987 tree *basep;
12988 tree binfo, base_binfo;
12989 unsigned max_vbases = 0; /* Maximum direct & indirect virtual bases. */
12990 unsigned max_bases = 0; /* Maximum direct bases. */
12991 unsigned max_dvbases = 0; /* Maximum direct virtual bases. */
12992 int i;
12993 tree default_access;
12994 tree igo_prev; /* Track Inheritance Graph Order. */
12996 if (ref == error_mark_node)
12997 return;
12999 /* The base of a derived class is private by default, all others are
13000 public. */
13001 default_access = (TREE_CODE (ref) == RECORD_TYPE
13002 && CLASSTYPE_DECLARED_CLASS (ref)
13003 ? access_private_node : access_public_node);
13005 /* First, make sure that any templates in base-classes are
13006 instantiated. This ensures that if we call ourselves recursively
13007 we do not get confused about which classes are marked and which
13008 are not. */
13009 basep = &base_list;
13010 while (*basep)
13012 tree basetype = TREE_VALUE (*basep);
13014 /* The dependent_type_p call below should really be dependent_scope_p
13015 so that we give a hard error about using an incomplete type as a
13016 base, but we allow it with a pedwarn for backward
13017 compatibility. */
13018 if (processing_template_decl
13019 && CLASS_TYPE_P (basetype) && TYPE_BEING_DEFINED (basetype))
13020 cxx_incomplete_type_diagnostic (NULL_TREE, basetype, DK_PEDWARN);
13021 if (!dependent_type_p (basetype)
13022 && !complete_type_or_else (basetype, NULL))
13023 /* An incomplete type. Remove it from the list. */
13024 *basep = TREE_CHAIN (*basep);
13025 else
13027 max_bases++;
13028 if (TREE_TYPE (*basep))
13029 max_dvbases++;
13030 if (CLASS_TYPE_P (basetype))
13031 max_vbases += vec_safe_length (CLASSTYPE_VBASECLASSES (basetype));
13032 basep = &TREE_CHAIN (*basep);
13035 max_vbases += max_dvbases;
13037 TYPE_MARKED_P (ref) = 1;
13039 /* The binfo slot should be empty, unless this is an (ill-formed)
13040 redefinition. */
13041 gcc_assert (!TYPE_BINFO (ref) || TYPE_SIZE (ref));
13043 gcc_assert (TYPE_MAIN_VARIANT (ref) == ref);
13045 binfo = make_tree_binfo (max_bases);
13047 TYPE_BINFO (ref) = binfo;
13048 BINFO_OFFSET (binfo) = size_zero_node;
13049 BINFO_TYPE (binfo) = ref;
13051 /* Apply base-class info set up to the variants of this type. */
13052 fixup_type_variants (ref);
13054 if (max_bases)
13056 vec_alloc (BINFO_BASE_ACCESSES (binfo), max_bases);
13057 /* An aggregate cannot have baseclasses. */
13058 CLASSTYPE_NON_AGGREGATE (ref) = 1;
13060 if (TREE_CODE (ref) == UNION_TYPE)
13061 error ("derived union %qT invalid", ref);
13064 if (max_bases > 1)
13065 warning (OPT_Wmultiple_inheritance,
13066 "%qT defined with multiple direct bases", ref);
13068 if (max_vbases)
13070 vec_alloc (CLASSTYPE_VBASECLASSES (ref), max_vbases);
13072 if (max_dvbases)
13073 warning (OPT_Wvirtual_inheritance,
13074 "%qT defined with direct virtual base", ref);
13077 for (igo_prev = binfo; base_list; base_list = TREE_CHAIN (base_list))
13079 tree access = TREE_PURPOSE (base_list);
13080 int via_virtual = TREE_TYPE (base_list) != NULL_TREE;
13081 tree basetype = TREE_VALUE (base_list);
13083 if (access == access_default_node)
13084 access = default_access;
13086 if (PACK_EXPANSION_P (basetype))
13087 basetype = PACK_EXPANSION_PATTERN (basetype);
13088 if (TREE_CODE (basetype) == TYPE_DECL)
13089 basetype = TREE_TYPE (basetype);
13090 if (!MAYBE_CLASS_TYPE_P (basetype) || TREE_CODE (basetype) == UNION_TYPE)
13092 error ("base type %qT fails to be a struct or class type",
13093 basetype);
13094 goto dropped_base;
13097 base_binfo = NULL_TREE;
13098 if (CLASS_TYPE_P (basetype) && !dependent_scope_p (basetype))
13100 base_binfo = TYPE_BINFO (basetype);
13101 /* The original basetype could have been a typedef'd type. */
13102 basetype = BINFO_TYPE (base_binfo);
13104 /* Inherit flags from the base. */
13105 TYPE_HAS_NEW_OPERATOR (ref)
13106 |= TYPE_HAS_NEW_OPERATOR (basetype);
13107 TYPE_HAS_ARRAY_NEW_OPERATOR (ref)
13108 |= TYPE_HAS_ARRAY_NEW_OPERATOR (basetype);
13109 TYPE_GETS_DELETE (ref) |= TYPE_GETS_DELETE (basetype);
13110 TYPE_HAS_CONVERSION (ref) |= TYPE_HAS_CONVERSION (basetype);
13111 CLASSTYPE_DIAMOND_SHAPED_P (ref)
13112 |= CLASSTYPE_DIAMOND_SHAPED_P (basetype);
13113 CLASSTYPE_REPEATED_BASE_P (ref)
13114 |= CLASSTYPE_REPEATED_BASE_P (basetype);
13117 /* We must do this test after we've seen through a typedef
13118 type. */
13119 if (TYPE_MARKED_P (basetype))
13121 if (basetype == ref)
13122 error ("recursive type %qT undefined", basetype);
13123 else
13124 error ("duplicate base type %qT invalid", basetype);
13125 goto dropped_base;
13128 if (PACK_EXPANSION_P (TREE_VALUE (base_list)))
13129 /* Regenerate the pack expansion for the bases. */
13130 basetype = make_pack_expansion (basetype);
13132 TYPE_MARKED_P (basetype) = 1;
13134 base_binfo = copy_binfo (base_binfo, basetype, ref,
13135 &igo_prev, via_virtual);
13136 if (!BINFO_INHERITANCE_CHAIN (base_binfo))
13137 BINFO_INHERITANCE_CHAIN (base_binfo) = binfo;
13139 BINFO_BASE_APPEND (binfo, base_binfo);
13140 BINFO_BASE_ACCESS_APPEND (binfo, access);
13141 continue;
13143 dropped_base:
13144 /* Update max_vbases to reflect the reality that we are dropping
13145 this base: if it reaches zero we want to undo the vec_alloc
13146 above to avoid inconsistencies during error-recovery: eg, in
13147 build_special_member_call, CLASSTYPE_VBASECLASSES non null
13148 and vtt null (c++/27952). */
13149 if (via_virtual)
13150 max_vbases--;
13151 if (CLASS_TYPE_P (basetype))
13152 max_vbases
13153 -= vec_safe_length (CLASSTYPE_VBASECLASSES (basetype));
13156 if (CLASSTYPE_VBASECLASSES (ref)
13157 && max_vbases == 0)
13158 vec_free (CLASSTYPE_VBASECLASSES (ref));
13160 if (vec_safe_length (CLASSTYPE_VBASECLASSES (ref)) < max_vbases)
13161 /* If we didn't get max_vbases vbases, we must have shared at
13162 least one of them, and are therefore diamond shaped. */
13163 CLASSTYPE_DIAMOND_SHAPED_P (ref) = 1;
13165 /* Unmark all the types. */
13166 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
13167 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
13168 TYPE_MARKED_P (ref) = 0;
13170 /* Now see if we have a repeated base type. */
13171 if (!CLASSTYPE_REPEATED_BASE_P (ref))
13173 for (base_binfo = binfo; base_binfo;
13174 base_binfo = TREE_CHAIN (base_binfo))
13176 if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
13178 CLASSTYPE_REPEATED_BASE_P (ref) = 1;
13179 break;
13181 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 1;
13183 for (base_binfo = binfo; base_binfo;
13184 base_binfo = TREE_CHAIN (base_binfo))
13185 if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
13186 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
13187 else
13188 break;
13193 /* Copies the enum-related properties from type SRC to type DST.
13194 Used with the underlying type of an enum and the enum itself. */
13195 static void
13196 copy_type_enum (tree dst, tree src)
13198 tree t;
13199 for (t = dst; t; t = TYPE_NEXT_VARIANT (t))
13201 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (src);
13202 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (src);
13203 TYPE_SIZE (t) = TYPE_SIZE (src);
13204 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (src);
13205 SET_TYPE_MODE (dst, TYPE_MODE (src));
13206 TYPE_PRECISION (t) = TYPE_PRECISION (src);
13207 unsigned valign = TYPE_ALIGN (src);
13208 if (TYPE_USER_ALIGN (t))
13209 valign = MAX (valign, TYPE_ALIGN (t));
13210 else
13211 TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (src);
13212 SET_TYPE_ALIGN (t, valign);
13213 TYPE_UNSIGNED (t) = TYPE_UNSIGNED (src);
13217 /* Begin compiling the definition of an enumeration type.
13218 NAME is its name,
13220 if ENUMTYPE is not NULL_TREE then the type has alredy been found.
13222 UNDERLYING_TYPE is the type that will be used as the storage for
13223 the enumeration type. This should be NULL_TREE if no storage type
13224 was specified.
13226 ATTRIBUTES are any attributes specified after the enum-key.
13228 SCOPED_ENUM_P is true if this is a scoped enumeration type.
13230 if IS_NEW is not NULL, gets TRUE iff a new type is created.
13232 Returns the type object, as yet incomplete.
13233 Also records info about it so that build_enumerator
13234 may be used to declare the individual values as they are read. */
13236 tree
13237 start_enum (tree name, tree enumtype, tree underlying_type,
13238 tree attributes, bool scoped_enum_p, bool *is_new)
13240 tree prevtype = NULL_TREE;
13241 gcc_assert (identifier_p (name));
13243 if (is_new)
13244 *is_new = false;
13245 /* [C++0x dcl.enum]p5:
13247 If not explicitly specified, the underlying type of a scoped
13248 enumeration type is int. */
13249 if (!underlying_type && scoped_enum_p)
13250 underlying_type = integer_type_node;
13252 if (underlying_type)
13253 underlying_type = cv_unqualified (underlying_type);
13255 /* If this is the real definition for a previous forward reference,
13256 fill in the contents in the same object that used to be the
13257 forward reference. */
13258 if (!enumtype)
13259 enumtype = lookup_and_check_tag (enum_type, name,
13260 /*tag_scope=*/ts_current,
13261 /*template_header_p=*/false);
13263 /* In case of a template_decl, the only check that should be deferred
13264 to instantiation time is the comparison of underlying types. */
13265 if (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE)
13267 if (scoped_enum_p != SCOPED_ENUM_P (enumtype))
13269 error_at (input_location, "scoped/unscoped mismatch "
13270 "in enum %q#T", enumtype);
13271 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
13272 "previous definition here");
13273 enumtype = error_mark_node;
13275 else if (ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) != !! underlying_type)
13277 error_at (input_location, "underlying type mismatch "
13278 "in enum %q#T", enumtype);
13279 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
13280 "previous definition here");
13281 enumtype = error_mark_node;
13283 else if (underlying_type && ENUM_UNDERLYING_TYPE (enumtype)
13284 && !dependent_type_p (underlying_type)
13285 && !dependent_type_p (ENUM_UNDERLYING_TYPE (enumtype))
13286 && !same_type_p (underlying_type,
13287 ENUM_UNDERLYING_TYPE (enumtype)))
13289 error_at (input_location, "different underlying type "
13290 "in enum %q#T", enumtype);
13291 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
13292 "previous definition here");
13293 underlying_type = NULL_TREE;
13297 if (!enumtype || TREE_CODE (enumtype) != ENUMERAL_TYPE
13298 || processing_template_decl)
13300 /* In case of error, make a dummy enum to allow parsing to
13301 continue. */
13302 if (enumtype == error_mark_node)
13304 name = make_anon_name ();
13305 enumtype = NULL_TREE;
13308 /* enumtype may be an ENUMERAL_TYPE if this is a redefinition
13309 of an opaque enum, or an opaque enum of an already defined
13310 enumeration (C++0x only).
13311 In any other case, it'll be NULL_TREE. */
13312 if (!enumtype)
13314 if (is_new)
13315 *is_new = true;
13317 prevtype = enumtype;
13319 /* Do not push the decl more than once, unless we need to
13320 compare underlying types at instantiation time */
13321 if (!enumtype
13322 || TREE_CODE (enumtype) != ENUMERAL_TYPE
13323 || (underlying_type
13324 && dependent_type_p (underlying_type))
13325 || (ENUM_UNDERLYING_TYPE (enumtype)
13326 && dependent_type_p (ENUM_UNDERLYING_TYPE (enumtype))))
13328 enumtype = cxx_make_type (ENUMERAL_TYPE);
13329 enumtype = pushtag (name, enumtype, /*tag_scope=*/ts_current);
13331 else
13332 enumtype = xref_tag (enum_type, name, /*tag_scope=*/ts_current,
13333 false);
13335 if (enumtype == error_mark_node)
13336 return error_mark_node;
13338 /* The enum is considered opaque until the opening '{' of the
13339 enumerator list. */
13340 SET_OPAQUE_ENUM_P (enumtype, true);
13341 ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) = !! underlying_type;
13344 SET_SCOPED_ENUM_P (enumtype, scoped_enum_p);
13346 cplus_decl_attributes (&enumtype, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
13348 if (underlying_type)
13350 if (ENUM_UNDERLYING_TYPE (enumtype))
13351 /* We already checked that it matches, don't change it to a different
13352 typedef variant. */;
13353 else if (CP_INTEGRAL_TYPE_P (underlying_type))
13355 copy_type_enum (enumtype, underlying_type);
13356 ENUM_UNDERLYING_TYPE (enumtype) = underlying_type;
13358 else if (dependent_type_p (underlying_type))
13359 ENUM_UNDERLYING_TYPE (enumtype) = underlying_type;
13360 else
13361 error ("underlying type %<%T%> of %<%T%> must be an integral type",
13362 underlying_type, enumtype);
13365 /* If into a template class, the returned enum is always the first
13366 declaration (opaque or not) seen. This way all the references to
13367 this type will be to the same declaration. The following ones are used
13368 only to check for definition errors. */
13369 if (prevtype && processing_template_decl)
13370 return prevtype;
13371 else
13372 return enumtype;
13375 /* After processing and defining all the values of an enumeration type,
13376 install their decls in the enumeration type.
13377 ENUMTYPE is the type object. */
13379 void
13380 finish_enum_value_list (tree enumtype)
13382 tree values;
13383 tree underlying_type;
13384 tree decl;
13385 tree value;
13386 tree minnode, maxnode;
13387 tree t;
13389 bool fixed_underlying_type_p
13390 = ENUM_UNDERLYING_TYPE (enumtype) != NULL_TREE;
13392 /* We built up the VALUES in reverse order. */
13393 TYPE_VALUES (enumtype) = nreverse (TYPE_VALUES (enumtype));
13395 /* For an enum defined in a template, just set the type of the values;
13396 all further processing is postponed until the template is
13397 instantiated. We need to set the type so that tsubst of a CONST_DECL
13398 works. */
13399 if (processing_template_decl)
13401 for (values = TYPE_VALUES (enumtype);
13402 values;
13403 values = TREE_CHAIN (values))
13404 TREE_TYPE (TREE_VALUE (values)) = enumtype;
13405 return;
13408 /* Determine the minimum and maximum values of the enumerators. */
13409 if (TYPE_VALUES (enumtype))
13411 minnode = maxnode = NULL_TREE;
13413 for (values = TYPE_VALUES (enumtype);
13414 values;
13415 values = TREE_CHAIN (values))
13417 decl = TREE_VALUE (values);
13419 /* [dcl.enum]: Following the closing brace of an enum-specifier,
13420 each enumerator has the type of its enumeration. Prior to the
13421 closing brace, the type of each enumerator is the type of its
13422 initializing value. */
13423 TREE_TYPE (decl) = enumtype;
13425 /* Update the minimum and maximum values, if appropriate. */
13426 value = DECL_INITIAL (decl);
13427 if (value == error_mark_node)
13428 value = integer_zero_node;
13429 /* Figure out what the minimum and maximum values of the
13430 enumerators are. */
13431 if (!minnode)
13432 minnode = maxnode = value;
13433 else if (tree_int_cst_lt (maxnode, value))
13434 maxnode = value;
13435 else if (tree_int_cst_lt (value, minnode))
13436 minnode = value;
13439 else
13440 /* [dcl.enum]
13442 If the enumerator-list is empty, the underlying type is as if
13443 the enumeration had a single enumerator with value 0. */
13444 minnode = maxnode = integer_zero_node;
13446 if (!fixed_underlying_type_p)
13448 /* Compute the number of bits require to represent all values of the
13449 enumeration. We must do this before the type of MINNODE and
13450 MAXNODE are transformed, since tree_int_cst_min_precision relies
13451 on the TREE_TYPE of the value it is passed. */
13452 signop sgn = tree_int_cst_sgn (minnode) >= 0 ? UNSIGNED : SIGNED;
13453 int lowprec = tree_int_cst_min_precision (minnode, sgn);
13454 int highprec = tree_int_cst_min_precision (maxnode, sgn);
13455 int precision = MAX (lowprec, highprec);
13456 unsigned int itk;
13457 bool use_short_enum;
13459 /* Determine the underlying type of the enumeration.
13461 [dcl.enum]
13463 The underlying type of an enumeration is an integral type that
13464 can represent all the enumerator values defined in the
13465 enumeration. It is implementation-defined which integral type is
13466 used as the underlying type for an enumeration except that the
13467 underlying type shall not be larger than int unless the value of
13468 an enumerator cannot fit in an int or unsigned int.
13470 We use "int" or an "unsigned int" as the underlying type, even if
13471 a smaller integral type would work, unless the user has
13472 explicitly requested that we use the smallest possible type. The
13473 user can request that for all enumerations with a command line
13474 flag, or for just one enumeration with an attribute. */
13476 use_short_enum = flag_short_enums
13477 || lookup_attribute ("packed", TYPE_ATTRIBUTES (enumtype));
13479 /* If the precision of the type was specified with an attribute and it
13480 was too small, give an error. Otherwise, use it. */
13481 if (TYPE_PRECISION (enumtype))
13483 if (precision > TYPE_PRECISION (enumtype))
13484 error ("specified mode too small for enumeral values");
13485 else
13487 use_short_enum = true;
13488 precision = TYPE_PRECISION (enumtype);
13492 for (itk = (use_short_enum ? itk_char : itk_int);
13493 itk != itk_none;
13494 itk++)
13496 underlying_type = integer_types[itk];
13497 if (underlying_type != NULL_TREE
13498 && TYPE_PRECISION (underlying_type) >= precision
13499 && TYPE_SIGN (underlying_type) == sgn)
13500 break;
13502 if (itk == itk_none)
13504 /* DR 377
13506 IF no integral type can represent all the enumerator values, the
13507 enumeration is ill-formed. */
13508 error ("no integral type can represent all of the enumerator values "
13509 "for %qT", enumtype);
13510 precision = TYPE_PRECISION (long_long_integer_type_node);
13511 underlying_type = integer_types[itk_unsigned_long_long];
13514 /* [dcl.enum]
13516 The value of sizeof() applied to an enumeration type, an object
13517 of an enumeration type, or an enumerator, is the value of sizeof()
13518 applied to the underlying type. */
13519 copy_type_enum (enumtype, underlying_type);
13521 /* Compute the minimum and maximum values for the type.
13523 [dcl.enum]
13525 For an enumeration where emin is the smallest enumerator and emax
13526 is the largest, the values of the enumeration are the values of the
13527 underlying type in the range bmin to bmax, where bmin and bmax are,
13528 respectively, the smallest and largest values of the smallest bit-
13529 field that can store emin and emax. */
13531 /* The middle-end currently assumes that types with TYPE_PRECISION
13532 narrower than their underlying type are suitably zero or sign
13533 extended to fill their mode. Similarly, it assumes that the front
13534 end assures that a value of a particular type must be within
13535 TYPE_MIN_VALUE and TYPE_MAX_VALUE.
13537 We used to set these fields based on bmin and bmax, but that led
13538 to invalid assumptions like optimizing away bounds checking. So
13539 now we just set the TYPE_PRECISION, TYPE_MIN_VALUE, and
13540 TYPE_MAX_VALUE to the values for the mode above and only restrict
13541 the ENUM_UNDERLYING_TYPE for the benefit of diagnostics. */
13542 ENUM_UNDERLYING_TYPE (enumtype)
13543 = build_distinct_type_copy (underlying_type);
13544 TYPE_PRECISION (ENUM_UNDERLYING_TYPE (enumtype)) = precision;
13545 set_min_and_max_values_for_integral_type
13546 (ENUM_UNDERLYING_TYPE (enumtype), precision, sgn);
13548 /* If -fstrict-enums, still constrain TYPE_MIN/MAX_VALUE. */
13549 if (flag_strict_enums)
13550 set_min_and_max_values_for_integral_type (enumtype, precision, sgn);
13552 else
13553 underlying_type = ENUM_UNDERLYING_TYPE (enumtype);
13555 /* Convert each of the enumerators to the type of the underlying
13556 type of the enumeration. */
13557 for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values))
13559 location_t saved_location;
13561 decl = TREE_VALUE (values);
13562 saved_location = input_location;
13563 input_location = DECL_SOURCE_LOCATION (decl);
13564 if (fixed_underlying_type_p)
13565 /* If the enumeration type has a fixed underlying type, we
13566 already checked all of the enumerator values. */
13567 value = DECL_INITIAL (decl);
13568 else
13569 value = perform_implicit_conversion (underlying_type,
13570 DECL_INITIAL (decl),
13571 tf_warning_or_error);
13572 input_location = saved_location;
13574 /* Do not clobber shared ints. */
13575 value = copy_node (value);
13577 TREE_TYPE (value) = enumtype;
13578 DECL_INITIAL (decl) = value;
13581 /* Fix up all variant types of this enum type. */
13582 for (t = TYPE_MAIN_VARIANT (enumtype); t; t = TYPE_NEXT_VARIANT (t))
13583 TYPE_VALUES (t) = TYPE_VALUES (enumtype);
13585 if (at_class_scope_p ()
13586 && COMPLETE_TYPE_P (current_class_type)
13587 && UNSCOPED_ENUM_P (enumtype))
13588 insert_late_enum_def_into_classtype_sorted_fields (enumtype,
13589 current_class_type);
13591 /* Finish debugging output for this type. */
13592 rest_of_type_compilation (enumtype, namespace_bindings_p ());
13594 /* Each enumerator now has the type of its enumeration. Clear the cache
13595 so that this change in types doesn't confuse us later on. */
13596 clear_cv_and_fold_caches ();
13599 /* Finishes the enum type. This is called only the first time an
13600 enumeration is seen, be it opaque or odinary.
13601 ENUMTYPE is the type object. */
13603 void
13604 finish_enum (tree enumtype)
13606 if (processing_template_decl)
13608 if (at_function_scope_p ())
13609 add_stmt (build_min (TAG_DEFN, enumtype));
13610 return;
13613 /* If this is a forward declaration, there should not be any variants,
13614 though we can get a variant in the middle of an enum-specifier with
13615 wacky code like 'enum E { e = sizeof(const E*) };' */
13616 gcc_assert (enumtype == TYPE_MAIN_VARIANT (enumtype)
13617 && (TYPE_VALUES (enumtype)
13618 || !TYPE_NEXT_VARIANT (enumtype)));
13621 /* Build and install a CONST_DECL for an enumeration constant of the
13622 enumeration type ENUMTYPE whose NAME and VALUE (if any) are provided.
13623 Apply ATTRIBUTES if available. LOC is the location of NAME.
13624 Assignment of sequential values by default is handled here. */
13626 void
13627 build_enumerator (tree name, tree value, tree enumtype, tree attributes,
13628 location_t loc)
13630 tree decl;
13631 tree context;
13632 tree type;
13634 /* scalar_constant_value will pull out this expression, so make sure
13635 it's folded as appropriate. */
13636 if (processing_template_decl)
13637 value = fold_non_dependent_expr (value);
13639 /* If the VALUE was erroneous, pretend it wasn't there; that will
13640 result in the enum being assigned the next value in sequence. */
13641 if (value == error_mark_node)
13642 value = NULL_TREE;
13644 /* Remove no-op casts from the value. */
13645 if (value)
13646 STRIP_TYPE_NOPS (value);
13648 if (! processing_template_decl)
13650 /* Validate and default VALUE. */
13651 if (value != NULL_TREE)
13653 if (!ENUM_UNDERLYING_TYPE (enumtype))
13655 tree tmp_value = build_expr_type_conversion (WANT_INT | WANT_ENUM,
13656 value, true);
13657 if (tmp_value)
13658 value = tmp_value;
13660 else if (! INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P
13661 (TREE_TYPE (value)))
13662 value = perform_implicit_conversion_flags
13663 (ENUM_UNDERLYING_TYPE (enumtype), value, tf_warning_or_error,
13664 LOOKUP_IMPLICIT | LOOKUP_NO_NARROWING);
13666 if (value == error_mark_node)
13667 value = NULL_TREE;
13669 if (value != NULL_TREE)
13671 if (! INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P
13672 (TREE_TYPE (value)))
13674 error ("enumerator value for %qD must have integral or "
13675 "unscoped enumeration type", name);
13676 value = NULL_TREE;
13678 else
13680 value = cxx_constant_value (value);
13682 if (TREE_CODE (value) != INTEGER_CST)
13684 error ("enumerator value for %qD is not an integer "
13685 "constant", name);
13686 value = NULL_TREE;
13692 /* Default based on previous value. */
13693 if (value == NULL_TREE)
13695 if (TYPE_VALUES (enumtype))
13697 tree prev_value;
13698 bool overflowed;
13700 /* C++03 7.2/4: If no initializer is specified for the first
13701 enumerator, the type is an unspecified integral
13702 type. Otherwise the type is the same as the type of the
13703 initializing value of the preceding enumerator unless the
13704 incremented value is not representable in that type, in
13705 which case the type is an unspecified integral type
13706 sufficient to contain the incremented value. */
13707 prev_value = DECL_INITIAL (TREE_VALUE (TYPE_VALUES (enumtype)));
13708 if (error_operand_p (prev_value))
13709 value = error_mark_node;
13710 else
13712 tree type = TREE_TYPE (prev_value);
13713 signop sgn = TYPE_SIGN (type);
13714 widest_int wi = wi::add (wi::to_widest (prev_value), 1, sgn,
13715 &overflowed);
13716 if (!overflowed)
13718 bool pos = !wi::neg_p (wi, sgn);
13719 if (!wi::fits_to_tree_p (wi, type))
13721 unsigned int itk;
13722 for (itk = itk_int; itk != itk_none; itk++)
13724 type = integer_types[itk];
13725 if (type != NULL_TREE
13726 && (pos || !TYPE_UNSIGNED (type))
13727 && wi::fits_to_tree_p (wi, type))
13728 break;
13730 if (type && cxx_dialect < cxx11
13731 && itk > itk_unsigned_long)
13732 pedwarn (input_location, OPT_Wlong_long, pos ? "\
13733 incremented enumerator value is too large for %<unsigned long%>" : "\
13734 incremented enumerator value is too large for %<long%>");
13736 if (type == NULL_TREE)
13737 overflowed = true;
13738 else
13739 value = wide_int_to_tree (type, wi);
13742 if (overflowed)
13744 error ("overflow in enumeration values at %qD", name);
13745 value = error_mark_node;
13749 else
13750 value = integer_zero_node;
13753 /* Remove no-op casts from the value. */
13754 STRIP_TYPE_NOPS (value);
13756 /* If the underlying type of the enum is fixed, check whether
13757 the enumerator values fits in the underlying type. If it
13758 does not fit, the program is ill-formed [C++0x dcl.enum]. */
13759 if (ENUM_UNDERLYING_TYPE (enumtype)
13760 && value
13761 && TREE_CODE (value) == INTEGER_CST)
13763 if (!int_fits_type_p (value, ENUM_UNDERLYING_TYPE (enumtype)))
13764 error ("enumerator value %E is outside the range of underlying "
13765 "type %<%T%>", value, ENUM_UNDERLYING_TYPE (enumtype));
13767 /* Convert the value to the appropriate type. */
13768 value = fold_convert (ENUM_UNDERLYING_TYPE (enumtype), value);
13772 /* C++ associates enums with global, function, or class declarations. */
13773 context = current_scope ();
13775 /* Build the actual enumeration constant. Note that the enumeration
13776 constants have the underlying type of the enum (if it is fixed)
13777 or the type of their initializer (if the underlying type of the
13778 enum is not fixed):
13780 [ C++0x dcl.enum ]
13782 If the underlying type is fixed, the type of each enumerator
13783 prior to the closing brace is the underlying type; if the
13784 initializing value of an enumerator cannot be represented by
13785 the underlying type, the program is ill-formed. If the
13786 underlying type is not fixed, the type of each enumerator is
13787 the type of its initializing value.
13789 If the underlying type is not fixed, it will be computed by
13790 finish_enum and we will reset the type of this enumerator. Of
13791 course, if we're processing a template, there may be no value. */
13792 type = value ? TREE_TYPE (value) : NULL_TREE;
13794 decl = build_decl (loc, CONST_DECL, name, type);
13796 DECL_CONTEXT (decl) = enumtype;
13797 TREE_CONSTANT (decl) = 1;
13798 TREE_READONLY (decl) = 1;
13799 DECL_INITIAL (decl) = value;
13801 if (attributes)
13802 cplus_decl_attributes (&decl, attributes, 0);
13804 if (context && context == current_class_type && !SCOPED_ENUM_P (enumtype))
13806 /* In something like `struct S { enum E { i = 7 }; };' we put `i'
13807 on the TYPE_FIELDS list for `S'. (That's so that you can say
13808 things like `S::i' later.) */
13810 /* The enumerator may be getting declared outside of its enclosing
13811 class, like so:
13813 class S { public: enum E : int; }; enum S::E : int { i = 7; };
13815 For which case we need to make sure that the access of `S::i'
13816 matches the access of `S::E'. */
13817 tree saved_cas = current_access_specifier;
13818 if (TREE_PRIVATE (TYPE_NAME (enumtype)))
13819 current_access_specifier = access_private_node;
13820 else if (TREE_PROTECTED (TYPE_NAME (enumtype)))
13821 current_access_specifier = access_protected_node;
13822 else
13823 current_access_specifier = access_public_node;
13825 finish_member_declaration (decl);
13827 current_access_specifier = saved_cas;
13829 else
13830 pushdecl (decl);
13832 /* Add this enumeration constant to the list for this type. */
13833 TYPE_VALUES (enumtype) = tree_cons (name, decl, TYPE_VALUES (enumtype));
13836 /* Look for an enumerator with the given NAME within the enumeration
13837 type ENUMTYPE. This routine is used primarily for qualified name
13838 lookup into an enumerator in C++0x, e.g.,
13840 enum class Color { Red, Green, Blue };
13842 Color color = Color::Red;
13844 Returns the value corresponding to the enumerator, or
13845 NULL_TREE if no such enumerator was found. */
13846 tree
13847 lookup_enumerator (tree enumtype, tree name)
13849 tree e;
13850 gcc_assert (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE);
13852 e = purpose_member (name, TYPE_VALUES (enumtype));
13853 return e? TREE_VALUE (e) : NULL_TREE;
13857 /* We're defining DECL. Make sure that its type is OK. */
13859 static void
13860 check_function_type (tree decl, tree current_function_parms)
13862 tree fntype = TREE_TYPE (decl);
13863 tree return_type = complete_type (TREE_TYPE (fntype));
13865 /* In a function definition, arg types must be complete. */
13866 require_complete_types_for_parms (current_function_parms);
13868 if (dependent_type_p (return_type)
13869 || type_uses_auto (return_type))
13870 return;
13871 if (!COMPLETE_OR_VOID_TYPE_P (return_type))
13873 tree args = TYPE_ARG_TYPES (fntype);
13875 error ("return type %q#T is incomplete", return_type);
13877 /* Make it return void instead. */
13878 if (TREE_CODE (fntype) == METHOD_TYPE)
13879 fntype = build_method_type_directly (TREE_TYPE (TREE_VALUE (args)),
13880 void_type_node,
13881 TREE_CHAIN (args));
13882 else
13883 fntype = build_function_type (void_type_node, args);
13884 fntype
13885 = build_exception_variant (fntype,
13886 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl)));
13887 fntype = (cp_build_type_attribute_variant
13888 (fntype, TYPE_ATTRIBUTES (TREE_TYPE (decl))));
13889 TREE_TYPE (decl) = fntype;
13891 else
13892 abstract_virtuals_error (decl, TREE_TYPE (fntype));
13895 /* True iff FN is an implicitly-defined default constructor. */
13897 static bool
13898 implicit_default_ctor_p (tree fn)
13900 return (DECL_CONSTRUCTOR_P (fn)
13901 && !user_provided_p (fn)
13902 && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn)));
13905 /* Clobber the contents of *this to let the back end know that the object
13906 storage is dead when we enter the constructor or leave the destructor. */
13908 static tree
13909 build_clobber_this ()
13911 /* Clobbering an empty base is pointless, and harmful if its one byte
13912 TYPE_SIZE overlays real data. */
13913 if (is_empty_class (current_class_type))
13914 return void_node;
13916 /* If we have virtual bases, clobber the whole object, but only if we're in
13917 charge. If we don't have virtual bases, clobber the as-base type so we
13918 don't mess with tail padding. */
13919 bool vbases = CLASSTYPE_VBASECLASSES (current_class_type);
13921 tree ctype = current_class_type;
13922 if (!vbases)
13923 ctype = CLASSTYPE_AS_BASE (ctype);
13925 tree clobber = build_constructor (ctype, NULL);
13926 TREE_THIS_VOLATILE (clobber) = true;
13928 tree thisref = current_class_ref;
13929 if (ctype != current_class_type)
13931 thisref = build_nop (build_reference_type (ctype), current_class_ptr);
13932 thisref = convert_from_reference (thisref);
13935 tree exprstmt = build2 (MODIFY_EXPR, void_type_node, thisref, clobber);
13936 if (vbases)
13937 exprstmt = build_if_in_charge (exprstmt);
13939 return exprstmt;
13942 /* Create the FUNCTION_DECL for a function definition.
13943 DECLSPECS and DECLARATOR are the parts of the declaration;
13944 they describe the function's name and the type it returns,
13945 but twisted together in a fashion that parallels the syntax of C.
13947 FLAGS is a bitwise or of SF_PRE_PARSED (indicating that the
13948 DECLARATOR is really the DECL for the function we are about to
13949 process and that DECLSPECS should be ignored), SF_INCLASS_INLINE
13950 indicating that the function is an inline defined in-class.
13952 This function creates a binding context for the function body
13953 as well as setting up the FUNCTION_DECL in current_function_decl.
13955 For C++, we must first check whether that datum makes any sense.
13956 For example, "class A local_a(1,2);" means that variable local_a
13957 is an aggregate of type A, which should have a constructor
13958 applied to it with the argument list [1, 2].
13960 On entry, DECL_INITIAL (decl1) should be NULL_TREE or error_mark_node,
13961 or may be a BLOCK if the function has been defined previously
13962 in this translation unit. On exit, DECL_INITIAL (decl1) will be
13963 error_mark_node if the function has never been defined, or
13964 a BLOCK if the function has been defined somewhere. */
13966 bool
13967 start_preparsed_function (tree decl1, tree attrs, int flags)
13969 tree ctype = NULL_TREE;
13970 tree fntype;
13971 tree restype;
13972 int doing_friend = 0;
13973 cp_binding_level *bl;
13974 tree current_function_parms;
13975 struct c_fileinfo *finfo
13976 = get_fileinfo (LOCATION_FILE (DECL_SOURCE_LOCATION (decl1)));
13977 bool honor_interface;
13979 /* Sanity check. */
13980 gcc_assert (VOID_TYPE_P (TREE_VALUE (void_list_node)));
13981 gcc_assert (TREE_CHAIN (void_list_node) == NULL_TREE);
13983 fntype = TREE_TYPE (decl1);
13984 if (TREE_CODE (fntype) == METHOD_TYPE)
13985 ctype = TYPE_METHOD_BASETYPE (fntype);
13987 /* ISO C++ 11.4/5. A friend function defined in a class is in
13988 the (lexical) scope of the class in which it is defined. */
13989 if (!ctype && DECL_FRIEND_P (decl1))
13991 ctype = DECL_FRIEND_CONTEXT (decl1);
13993 /* CTYPE could be null here if we're dealing with a template;
13994 for example, `inline friend float foo()' inside a template
13995 will have no CTYPE set. */
13996 if (ctype && TREE_CODE (ctype) != RECORD_TYPE)
13997 ctype = NULL_TREE;
13998 else
13999 doing_friend = 1;
14002 if (DECL_DECLARED_INLINE_P (decl1)
14003 && lookup_attribute ("noinline", attrs))
14004 warning_at (DECL_SOURCE_LOCATION (decl1), 0,
14005 "inline function %qD given attribute noinline", decl1);
14007 /* Handle gnu_inline attribute. */
14008 if (GNU_INLINE_P (decl1))
14010 DECL_EXTERNAL (decl1) = 1;
14011 DECL_NOT_REALLY_EXTERN (decl1) = 0;
14012 DECL_INTERFACE_KNOWN (decl1) = 1;
14013 DECL_DISREGARD_INLINE_LIMITS (decl1) = 1;
14016 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl1))
14017 /* This is a constructor, we must ensure that any default args
14018 introduced by this definition are propagated to the clones
14019 now. The clones are used directly in overload resolution. */
14020 adjust_clone_args (decl1);
14022 /* Sometimes we don't notice that a function is a static member, and
14023 build a METHOD_TYPE for it. Fix that up now. */
14024 gcc_assert (!(ctype != NULL_TREE && DECL_STATIC_FUNCTION_P (decl1)
14025 && TREE_CODE (TREE_TYPE (decl1)) == METHOD_TYPE));
14027 /* Set up current_class_type, and enter the scope of the class, if
14028 appropriate. */
14029 if (ctype)
14030 push_nested_class (ctype);
14031 else if (DECL_STATIC_FUNCTION_P (decl1))
14032 push_nested_class (DECL_CONTEXT (decl1));
14034 /* Now that we have entered the scope of the class, we must restore
14035 the bindings for any template parameters surrounding DECL1, if it
14036 is an inline member template. (Order is important; consider the
14037 case where a template parameter has the same name as a field of
14038 the class.) It is not until after this point that
14039 PROCESSING_TEMPLATE_DECL is guaranteed to be set up correctly. */
14040 if (flags & SF_INCLASS_INLINE)
14041 maybe_begin_member_template_processing (decl1);
14043 /* Effective C++ rule 15. */
14044 if (warn_ecpp
14045 && DECL_OVERLOADED_OPERATOR_P (decl1) == NOP_EXPR
14046 && VOID_TYPE_P (TREE_TYPE (fntype)))
14047 warning (OPT_Weffc__, "%<operator=%> should return a reference to %<*this%>");
14049 /* Make the init_value nonzero so pushdecl knows this is not tentative.
14050 error_mark_node is replaced below (in poplevel) with the BLOCK. */
14051 if (!DECL_INITIAL (decl1))
14052 DECL_INITIAL (decl1) = error_mark_node;
14054 /* This function exists in static storage.
14055 (This does not mean `static' in the C sense!) */
14056 TREE_STATIC (decl1) = 1;
14058 /* We must call push_template_decl after current_class_type is set
14059 up. (If we are processing inline definitions after exiting a
14060 class scope, current_class_type will be NULL_TREE until set above
14061 by push_nested_class.) */
14062 if (processing_template_decl)
14064 tree newdecl1 = push_template_decl (decl1);
14065 if (newdecl1 == error_mark_node)
14067 if (ctype || DECL_STATIC_FUNCTION_P (decl1))
14068 pop_nested_class ();
14069 return false;
14071 decl1 = newdecl1;
14074 /* We are now in the scope of the function being defined. */
14075 current_function_decl = decl1;
14077 /* Save the parm names or decls from this function's declarator
14078 where store_parm_decls will find them. */
14079 current_function_parms = DECL_ARGUMENTS (decl1);
14081 /* Make sure the parameter and return types are reasonable. When
14082 you declare a function, these types can be incomplete, but they
14083 must be complete when you define the function. */
14084 check_function_type (decl1, current_function_parms);
14086 /* Build the return declaration for the function. */
14087 restype = TREE_TYPE (fntype);
14089 if (DECL_RESULT (decl1) == NULL_TREE)
14091 tree resdecl;
14093 resdecl = build_decl (input_location, RESULT_DECL, 0, restype);
14094 DECL_ARTIFICIAL (resdecl) = 1;
14095 DECL_IGNORED_P (resdecl) = 1;
14096 DECL_RESULT (decl1) = resdecl;
14098 cp_apply_type_quals_to_decl (cp_type_quals (restype), resdecl);
14101 /* Let the user know we're compiling this function. */
14102 announce_function (decl1);
14104 /* Record the decl so that the function name is defined.
14105 If we already have a decl for this name, and it is a FUNCTION_DECL,
14106 use the old decl. */
14107 if (!processing_template_decl && !(flags & SF_PRE_PARSED))
14109 /* A specialization is not used to guide overload resolution. */
14110 if (!DECL_FUNCTION_MEMBER_P (decl1)
14111 && !(DECL_USE_TEMPLATE (decl1) &&
14112 PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl1))))
14114 tree olddecl = pushdecl (decl1);
14116 if (olddecl == error_mark_node)
14117 /* If something went wrong when registering the declaration,
14118 use DECL1; we have to have a FUNCTION_DECL to use when
14119 parsing the body of the function. */
14121 else
14123 /* Otherwise, OLDDECL is either a previous declaration
14124 of the same function or DECL1 itself. */
14126 if (warn_missing_declarations
14127 && olddecl == decl1
14128 && !DECL_MAIN_P (decl1)
14129 && TREE_PUBLIC (decl1)
14130 && !DECL_DECLARED_INLINE_P (decl1))
14132 tree context;
14134 /* Check whether DECL1 is in an anonymous
14135 namespace. */
14136 for (context = DECL_CONTEXT (decl1);
14137 context;
14138 context = DECL_CONTEXT (context))
14140 if (TREE_CODE (context) == NAMESPACE_DECL
14141 && DECL_NAME (context) == NULL_TREE)
14142 break;
14145 if (context == NULL)
14146 warning_at (DECL_SOURCE_LOCATION (decl1),
14147 OPT_Wmissing_declarations,
14148 "no previous declaration for %qD", decl1);
14151 decl1 = olddecl;
14154 else
14156 /* We need to set the DECL_CONTEXT. */
14157 if (!DECL_CONTEXT (decl1) && DECL_TEMPLATE_INFO (decl1))
14158 DECL_CONTEXT (decl1) = DECL_CONTEXT (DECL_TI_TEMPLATE (decl1));
14160 fntype = TREE_TYPE (decl1);
14161 restype = TREE_TYPE (fntype);
14163 /* If #pragma weak applies, mark the decl appropriately now.
14164 The pragma only applies to global functions. Because
14165 determining whether or not the #pragma applies involves
14166 computing the mangled name for the declaration, we cannot
14167 apply the pragma until after we have merged this declaration
14168 with any previous declarations; if the original declaration
14169 has a linkage specification, that specification applies to
14170 the definition as well, and may affect the mangled name. */
14171 if (DECL_FILE_SCOPE_P (decl1))
14172 maybe_apply_pragma_weak (decl1);
14175 /* Reset this in case the call to pushdecl changed it. */
14176 current_function_decl = decl1;
14178 gcc_assert (DECL_INITIAL (decl1));
14180 /* This function may already have been parsed, in which case just
14181 return; our caller will skip over the body without parsing. */
14182 if (DECL_INITIAL (decl1) != error_mark_node)
14183 return true;
14185 /* Initialize RTL machinery. We cannot do this until
14186 CURRENT_FUNCTION_DECL and DECL_RESULT are set up. We do this
14187 even when processing a template; this is how we get
14188 CFUN set up, and our per-function variables initialized.
14189 FIXME factor out the non-RTL stuff. */
14190 bl = current_binding_level;
14191 allocate_struct_function (decl1, processing_template_decl);
14193 /* Initialize the language data structures. Whenever we start
14194 a new function, we destroy temporaries in the usual way. */
14195 cfun->language = ggc_cleared_alloc<language_function> ();
14196 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
14197 current_binding_level = bl;
14199 if (!processing_template_decl && type_uses_auto (restype))
14201 FNDECL_USED_AUTO (decl1) = true;
14202 current_function_auto_return_pattern = restype;
14205 /* Start the statement-tree, start the tree now. */
14206 DECL_SAVED_TREE (decl1) = push_stmt_list ();
14208 /* If we are (erroneously) defining a function that we have already
14209 defined before, wipe out what we knew before. */
14210 if (!DECL_PENDING_INLINE_P (decl1))
14211 DECL_SAVED_FUNCTION_DATA (decl1) = NULL;
14213 if (ctype && !doing_friend && !DECL_STATIC_FUNCTION_P (decl1))
14215 /* We know that this was set up by `grokclassfn'. We do not
14216 wait until `store_parm_decls', since evil parse errors may
14217 never get us to that point. Here we keep the consistency
14218 between `current_class_type' and `current_class_ptr'. */
14219 tree t = DECL_ARGUMENTS (decl1);
14221 gcc_assert (t != NULL_TREE && TREE_CODE (t) == PARM_DECL);
14222 gcc_assert (TYPE_PTR_P (TREE_TYPE (t)));
14224 cp_function_chain->x_current_class_ref
14225 = cp_build_indirect_ref (t, RO_NULL, tf_warning_or_error);
14226 /* Set this second to avoid shortcut in cp_build_indirect_ref. */
14227 cp_function_chain->x_current_class_ptr = t;
14229 /* Constructors and destructors need to know whether they're "in
14230 charge" of initializing virtual base classes. */
14231 t = DECL_CHAIN (t);
14232 if (DECL_HAS_IN_CHARGE_PARM_P (decl1))
14234 current_in_charge_parm = t;
14235 t = DECL_CHAIN (t);
14237 if (DECL_HAS_VTT_PARM_P (decl1))
14239 gcc_assert (DECL_NAME (t) == vtt_parm_identifier);
14240 current_vtt_parm = t;
14244 honor_interface = (!DECL_TEMPLATE_INSTANTIATION (decl1)
14245 /* Implicitly-defined methods (like the
14246 destructor for a class in which no destructor
14247 is explicitly declared) must not be defined
14248 until their definition is needed. So, we
14249 ignore interface specifications for
14250 compiler-generated functions. */
14251 && !DECL_ARTIFICIAL (decl1));
14253 if (processing_template_decl)
14254 /* Don't mess with interface flags. */;
14255 else if (DECL_INTERFACE_KNOWN (decl1))
14257 tree ctx = decl_function_context (decl1);
14259 if (DECL_NOT_REALLY_EXTERN (decl1))
14260 DECL_EXTERNAL (decl1) = 0;
14262 if (ctx != NULL_TREE && vague_linkage_p (ctx))
14263 /* This is a function in a local class in an extern inline
14264 or template function. */
14265 comdat_linkage (decl1);
14267 /* If this function belongs to an interface, it is public.
14268 If it belongs to someone else's interface, it is also external.
14269 This only affects inlines and template instantiations. */
14270 else if (!finfo->interface_unknown && honor_interface)
14272 if (DECL_DECLARED_INLINE_P (decl1)
14273 || DECL_TEMPLATE_INSTANTIATION (decl1))
14275 DECL_EXTERNAL (decl1)
14276 = (finfo->interface_only
14277 || (DECL_DECLARED_INLINE_P (decl1)
14278 && ! flag_implement_inlines
14279 && !DECL_VINDEX (decl1)));
14281 /* For WIN32 we also want to put these in linkonce sections. */
14282 maybe_make_one_only (decl1);
14284 else
14285 DECL_EXTERNAL (decl1) = 0;
14286 DECL_INTERFACE_KNOWN (decl1) = 1;
14287 /* If this function is in an interface implemented in this file,
14288 make sure that the back end knows to emit this function
14289 here. */
14290 if (!DECL_EXTERNAL (decl1))
14291 mark_needed (decl1);
14293 else if (finfo->interface_unknown && finfo->interface_only
14294 && honor_interface)
14296 /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma
14297 interface, we will have both finfo->interface_unknown and
14298 finfo->interface_only set. In that case, we don't want to
14299 use the normal heuristics because someone will supply a
14300 #pragma implementation elsewhere, and deducing it here would
14301 produce a conflict. */
14302 comdat_linkage (decl1);
14303 DECL_EXTERNAL (decl1) = 0;
14304 DECL_INTERFACE_KNOWN (decl1) = 1;
14305 DECL_DEFER_OUTPUT (decl1) = 1;
14307 else
14309 /* This is a definition, not a reference.
14310 So clear DECL_EXTERNAL, unless this is a GNU extern inline. */
14311 if (!GNU_INLINE_P (decl1))
14312 DECL_EXTERNAL (decl1) = 0;
14314 if ((DECL_DECLARED_INLINE_P (decl1)
14315 || DECL_TEMPLATE_INSTANTIATION (decl1))
14316 && ! DECL_INTERFACE_KNOWN (decl1))
14317 DECL_DEFER_OUTPUT (decl1) = 1;
14318 else
14319 DECL_INTERFACE_KNOWN (decl1) = 1;
14322 /* Determine the ELF visibility attribute for the function. We must not
14323 do this before calling "pushdecl", as we must allow "duplicate_decls"
14324 to merge any attributes appropriately. We also need to wait until
14325 linkage is set. */
14326 if (!DECL_CLONED_FUNCTION_P (decl1))
14327 determine_visibility (decl1);
14329 if (!processing_template_decl)
14330 maybe_instantiate_noexcept (decl1);
14332 begin_scope (sk_function_parms, decl1);
14334 ++function_depth;
14336 if (DECL_DESTRUCTOR_P (decl1)
14337 || (DECL_CONSTRUCTOR_P (decl1)
14338 && targetm.cxx.cdtor_returns_this ()))
14339 cdtor_label = create_artificial_label (input_location);
14341 start_fname_decls ();
14343 store_parm_decls (current_function_parms);
14345 if (!processing_template_decl
14346 && (flag_lifetime_dse > 1)
14347 && DECL_CONSTRUCTOR_P (decl1)
14348 && !DECL_CLONED_FUNCTION_P (decl1)
14349 /* Clobbering an empty base is harmful if it overlays real data. */
14350 && !is_empty_class (current_class_type)
14351 /* We can't clobber safely for an implicitly-defined default constructor
14352 because part of the initialization might happen before we enter the
14353 constructor, via AGGR_INIT_ZERO_FIRST (c++/68006). */
14354 && !implicit_default_ctor_p (decl1))
14355 finish_expr_stmt (build_clobber_this ());
14357 if (!processing_template_decl
14358 && DECL_CONSTRUCTOR_P (decl1)
14359 && (flag_sanitize & SANITIZE_VPTR)
14360 && !DECL_CLONED_FUNCTION_P (decl1)
14361 && !implicit_default_ctor_p (decl1))
14362 cp_ubsan_maybe_initialize_vtbl_ptrs (current_class_ptr);
14364 return true;
14368 /* Like start_preparsed_function, except that instead of a
14369 FUNCTION_DECL, this function takes DECLSPECS and DECLARATOR.
14371 Returns true on success. If the DECLARATOR is not suitable
14372 for a function, we return false, which tells the parser to
14373 skip the entire function. */
14375 bool
14376 start_function (cp_decl_specifier_seq *declspecs,
14377 const cp_declarator *declarator,
14378 tree attrs)
14380 tree decl1;
14382 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, &attrs);
14383 invoke_plugin_callbacks (PLUGIN_START_PARSE_FUNCTION, decl1);
14384 if (decl1 == error_mark_node)
14385 return false;
14386 /* If the declarator is not suitable for a function definition,
14387 cause a syntax error. */
14388 if (decl1 == NULL_TREE || TREE_CODE (decl1) != FUNCTION_DECL)
14390 error ("invalid function declaration");
14391 return false;
14394 if (DECL_MAIN_P (decl1))
14395 /* main must return int. grokfndecl should have corrected it
14396 (and issued a diagnostic) if the user got it wrong. */
14397 gcc_assert (same_type_p (TREE_TYPE (TREE_TYPE (decl1)),
14398 integer_type_node));
14400 return start_preparsed_function (decl1, attrs, /*flags=*/SF_DEFAULT);
14403 /* Returns true iff an EH_SPEC_BLOCK should be created in the body of
14404 FN. */
14406 static bool
14407 use_eh_spec_block (tree fn)
14409 return (flag_exceptions && flag_enforce_eh_specs
14410 && !processing_template_decl
14411 && !type_throw_all_p (TREE_TYPE (fn))
14412 /* We insert the EH_SPEC_BLOCK only in the original
14413 function; then, it is copied automatically to the
14414 clones. */
14415 && !DECL_CLONED_FUNCTION_P (fn)
14416 /* Implicitly-generated constructors and destructors have
14417 exception specifications. However, those specifications
14418 are the union of the possible exceptions specified by the
14419 constructors/destructors for bases and members, so no
14420 unallowed exception will ever reach this function. By
14421 not creating the EH_SPEC_BLOCK we save a little memory,
14422 and we avoid spurious warnings about unreachable
14423 code. */
14424 && !DECL_DEFAULTED_FN (fn));
14427 /* Store the parameter declarations into the current function declaration.
14428 This is called after parsing the parameter declarations, before
14429 digesting the body of the function.
14431 Also install to binding contour return value identifier, if any. */
14433 static void
14434 store_parm_decls (tree current_function_parms)
14436 tree fndecl = current_function_decl;
14437 tree parm;
14439 /* This is a chain of any other decls that came in among the parm
14440 declarations. If a parm is declared with enum {foo, bar} x;
14441 then CONST_DECLs for foo and bar are put here. */
14442 tree nonparms = NULL_TREE;
14444 if (current_function_parms)
14446 /* This case is when the function was defined with an ANSI prototype.
14447 The parms already have decls, so we need not do anything here
14448 except record them as in effect
14449 and complain if any redundant old-style parm decls were written. */
14451 tree specparms = current_function_parms;
14452 tree next;
14454 /* Must clear this because it might contain TYPE_DECLs declared
14455 at class level. */
14456 current_binding_level->names = NULL;
14458 /* If we're doing semantic analysis, then we'll call pushdecl
14459 for each of these. We must do them in reverse order so that
14460 they end in the correct forward order. */
14461 specparms = nreverse (specparms);
14463 for (parm = specparms; parm; parm = next)
14465 next = DECL_CHAIN (parm);
14466 if (TREE_CODE (parm) == PARM_DECL)
14467 pushdecl (parm);
14468 else
14470 /* If we find an enum constant or a type tag,
14471 put it aside for the moment. */
14472 TREE_CHAIN (parm) = NULL_TREE;
14473 nonparms = chainon (nonparms, parm);
14477 /* Get the decls in their original chain order and record in the
14478 function. This is all and only the PARM_DECLs that were
14479 pushed into scope by the loop above. */
14480 DECL_ARGUMENTS (fndecl) = getdecls ();
14482 else
14483 DECL_ARGUMENTS (fndecl) = NULL_TREE;
14485 /* Now store the final chain of decls for the arguments
14486 as the decl-chain of the current lexical scope.
14487 Put the enumerators in as well, at the front so that
14488 DECL_ARGUMENTS is not modified. */
14489 current_binding_level->names = chainon (nonparms, DECL_ARGUMENTS (fndecl));
14491 if (use_eh_spec_block (current_function_decl))
14492 current_eh_spec_block = begin_eh_spec_block ();
14496 /* We have finished doing semantic analysis on DECL, but have not yet
14497 generated RTL for its body. Save away our current state, so that
14498 when we want to generate RTL later we know what to do. */
14500 static void
14501 save_function_data (tree decl)
14503 struct language_function *f;
14505 /* Save the language-specific per-function data so that we can
14506 get it back when we really expand this function. */
14507 gcc_assert (!DECL_PENDING_INLINE_P (decl));
14509 /* Make a copy. */
14510 f = ggc_alloc<language_function> ();
14511 memcpy (f, cp_function_chain, sizeof (struct language_function));
14512 DECL_SAVED_FUNCTION_DATA (decl) = f;
14514 /* Clear out the bits we don't need. */
14515 f->base.x_stmt_tree.x_cur_stmt_list = NULL;
14516 f->bindings = NULL;
14517 f->x_local_names = NULL;
14518 f->base.local_typedefs = NULL;
14522 /* Set the return value of the constructor (if present). */
14524 static void
14525 finish_constructor_body (void)
14527 tree val;
14528 tree exprstmt;
14530 if (targetm.cxx.cdtor_returns_this ())
14532 /* Any return from a constructor will end up here. */
14533 add_stmt (build_stmt (input_location, LABEL_EXPR, cdtor_label));
14535 val = DECL_ARGUMENTS (current_function_decl);
14536 val = build2 (MODIFY_EXPR, TREE_TYPE (val),
14537 DECL_RESULT (current_function_decl), val);
14538 /* Return the address of the object. */
14539 exprstmt = build_stmt (input_location, RETURN_EXPR, val);
14540 add_stmt (exprstmt);
14544 /* Do all the processing for the beginning of a destructor; set up the
14545 vtable pointers and cleanups for bases and members. */
14547 static void
14548 begin_destructor_body (void)
14550 tree compound_stmt;
14552 /* If the CURRENT_CLASS_TYPE is incomplete, we will have already
14553 issued an error message. We still want to try to process the
14554 body of the function, but initialize_vtbl_ptrs will crash if
14555 TYPE_BINFO is NULL. */
14556 if (COMPLETE_TYPE_P (current_class_type))
14558 compound_stmt = begin_compound_stmt (0);
14559 /* Make all virtual function table pointers in non-virtual base
14560 classes point to CURRENT_CLASS_TYPE's virtual function
14561 tables. */
14562 initialize_vtbl_ptrs (current_class_ptr);
14563 finish_compound_stmt (compound_stmt);
14565 if (flag_lifetime_dse
14566 /* Clobbering an empty base is harmful if it overlays real data. */
14567 && !is_empty_class (current_class_type))
14568 finish_decl_cleanup (NULL_TREE, build_clobber_this ());
14570 /* And insert cleanups for our bases and members so that they
14571 will be properly destroyed if we throw. */
14572 push_base_cleanups ();
14576 /* At the end of every destructor we generate code to delete the object if
14577 necessary. Do that now. */
14579 static void
14580 finish_destructor_body (void)
14582 tree exprstmt;
14584 /* Any return from a destructor will end up here; that way all base
14585 and member cleanups will be run when the function returns. */
14586 add_stmt (build_stmt (input_location, LABEL_EXPR, cdtor_label));
14588 if (targetm.cxx.cdtor_returns_this ())
14590 tree val;
14592 val = DECL_ARGUMENTS (current_function_decl);
14593 val = build2 (MODIFY_EXPR, TREE_TYPE (val),
14594 DECL_RESULT (current_function_decl), val);
14595 /* Return the address of the object. */
14596 exprstmt = build_stmt (input_location, RETURN_EXPR, val);
14597 add_stmt (exprstmt);
14601 /* Do the necessary processing for the beginning of a function body, which
14602 in this case includes member-initializers, but not the catch clauses of
14603 a function-try-block. Currently, this means opening a binding level
14604 for the member-initializers (in a ctor), member cleanups (in a dtor),
14605 and capture proxies (in a lambda operator()). */
14607 tree
14608 begin_function_body (void)
14610 tree stmt;
14612 if (! FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
14613 return NULL_TREE;
14615 if (processing_template_decl)
14616 /* Do nothing now. */;
14617 else
14618 /* Always keep the BLOCK node associated with the outermost pair of
14619 curly braces of a function. These are needed for correct
14620 operation of dwarfout.c. */
14621 keep_next_level (true);
14623 stmt = begin_compound_stmt (BCS_FN_BODY);
14625 if (processing_template_decl)
14626 /* Do nothing now. */;
14627 else if (DECL_DESTRUCTOR_P (current_function_decl))
14628 begin_destructor_body ();
14630 return stmt;
14633 /* Do the processing for the end of a function body. Currently, this means
14634 closing out the cleanups for fully-constructed bases and members, and in
14635 the case of the destructor, deleting the object if desired. Again, this
14636 is only meaningful for [cd]tors, since they are the only functions where
14637 there is a significant distinction between the main body and any
14638 function catch clauses. Handling, say, main() return semantics here
14639 would be wrong, as flowing off the end of a function catch clause for
14640 main() would also need to return 0. */
14642 void
14643 finish_function_body (tree compstmt)
14645 if (compstmt == NULL_TREE)
14646 return;
14648 /* Close the block. */
14649 finish_compound_stmt (compstmt);
14651 if (processing_template_decl)
14652 /* Do nothing now. */;
14653 else if (DECL_CONSTRUCTOR_P (current_function_decl))
14654 finish_constructor_body ();
14655 else if (DECL_DESTRUCTOR_P (current_function_decl))
14656 finish_destructor_body ();
14659 /* Given a function, returns the BLOCK corresponding to the outermost level
14660 of curly braces, skipping the artificial block created for constructor
14661 initializers. */
14663 tree
14664 outer_curly_brace_block (tree fndecl)
14666 tree block = DECL_INITIAL (fndecl);
14667 if (BLOCK_OUTER_CURLY_BRACE_P (block))
14668 return block;
14669 block = BLOCK_SUBBLOCKS (block);
14670 if (BLOCK_OUTER_CURLY_BRACE_P (block))
14671 return block;
14672 block = BLOCK_SUBBLOCKS (block);
14673 gcc_assert (BLOCK_OUTER_CURLY_BRACE_P (block));
14674 return block;
14677 /* If FNDECL is a class's key method, add the class to the list of
14678 keyed classes that should be emitted. */
14680 static void
14681 record_key_method_defined (tree fndecl)
14683 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fndecl)
14684 && DECL_VIRTUAL_P (fndecl)
14685 && !processing_template_decl)
14687 tree fnclass = DECL_CONTEXT (fndecl);
14688 if (fndecl == CLASSTYPE_KEY_METHOD (fnclass))
14689 keyed_classes = tree_cons (NULL_TREE, fnclass, keyed_classes);
14693 /* Subroutine of finish_function.
14694 Save the body of constexpr functions for possible
14695 future compile time evaluation. */
14697 static void
14698 maybe_save_function_definition (tree fun)
14700 if (!processing_template_decl
14701 && DECL_DECLARED_CONSTEXPR_P (fun)
14702 && !cp_function_chain->invalid_constexpr
14703 && !DECL_CLONED_FUNCTION_P (fun))
14704 register_constexpr_fundef (fun, DECL_SAVED_TREE (fun));
14707 /* Finish up a function declaration and compile that function
14708 all the way to assembler language output. The free the storage
14709 for the function definition.
14711 FLAGS is a bitwise or of the following values:
14712 2 - INCLASS_INLINE
14713 We just finished processing the body of an in-class inline
14714 function definition. (This processing will have taken place
14715 after the class definition is complete.) */
14717 tree
14718 finish_function (int flags)
14720 tree fndecl = current_function_decl;
14721 tree fntype, ctype = NULL_TREE;
14722 int inclass_inline = (flags & 2) != 0;
14724 /* When we get some parse errors, we can end up without a
14725 current_function_decl, so cope. */
14726 if (fndecl == NULL_TREE)
14727 return error_mark_node;
14729 if (c_dialect_objc ())
14730 objc_finish_function ();
14732 record_key_method_defined (fndecl);
14734 fntype = TREE_TYPE (fndecl);
14736 /* TREE_READONLY (fndecl) = 1;
14737 This caused &foo to be of type ptr-to-const-function
14738 which then got a warning when stored in a ptr-to-function variable. */
14740 gcc_assert (building_stmt_list_p ());
14741 /* The current function is being defined, so its DECL_INITIAL should
14742 be set, and unless there's a multiple definition, it should be
14743 error_mark_node. */
14744 gcc_assert (DECL_INITIAL (fndecl) == error_mark_node);
14746 /* For a cloned function, we've already got all the code we need;
14747 there's no need to add any extra bits. */
14748 if (!DECL_CLONED_FUNCTION_P (fndecl))
14750 /* Make it so that `main' always returns 0 by default. */
14751 if (DECL_MAIN_P (current_function_decl))
14752 finish_return_stmt (integer_zero_node);
14754 if (use_eh_spec_block (current_function_decl))
14755 finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
14756 (TREE_TYPE (current_function_decl)),
14757 current_eh_spec_block);
14760 /* If we're saving up tree structure, tie off the function now. */
14761 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
14763 if (fn_contains_cilk_spawn_p (cfun) && !processing_template_decl)
14764 cfun->cilk_frame_decl = insert_cilk_frame (fndecl);
14766 finish_fname_decls ();
14768 /* If this function can't throw any exceptions, remember that. */
14769 if (!processing_template_decl
14770 && !cp_function_chain->can_throw
14771 && !flag_non_call_exceptions
14772 && !decl_replaceable_p (fndecl))
14773 TREE_NOTHROW (fndecl) = 1;
14775 /* This must come after expand_function_end because cleanups might
14776 have declarations (from inline functions) that need to go into
14777 this function's blocks. */
14779 /* If the current binding level isn't the outermost binding level
14780 for this function, either there is a bug, or we have experienced
14781 syntax errors and the statement tree is malformed. */
14782 if (current_binding_level->kind != sk_function_parms)
14784 /* Make sure we have already experienced errors. */
14785 gcc_assert (errorcount);
14787 /* Throw away the broken statement tree and extra binding
14788 levels. */
14789 DECL_SAVED_TREE (fndecl) = alloc_stmt_list ();
14791 while (current_binding_level->kind != sk_function_parms)
14793 if (current_binding_level->kind == sk_class)
14794 pop_nested_class ();
14795 else
14796 poplevel (0, 0, 0);
14799 poplevel (1, 0, 1);
14801 /* Statements should always be full-expressions at the outermost set
14802 of curly braces for a function. */
14803 gcc_assert (stmts_are_full_exprs_p ());
14805 /* If there are no return statements in a function with auto return type,
14806 the return type is void. But if the declared type is something like
14807 auto*, this is an error. */
14808 if (!processing_template_decl && FNDECL_USED_AUTO (fndecl)
14809 && TREE_TYPE (fntype) == current_function_auto_return_pattern)
14811 if (!is_auto (current_function_auto_return_pattern)
14812 && !current_function_returns_value && !current_function_returns_null)
14814 error ("no return statements in function returning %qT",
14815 current_function_auto_return_pattern);
14816 inform (input_location, "only plain %<auto%> return type can be "
14817 "deduced to %<void%>");
14819 apply_deduced_return_type (fndecl, void_type_node);
14820 fntype = TREE_TYPE (fndecl);
14823 // If this is a concept, check that the definition is reasonable.
14824 if (DECL_DECLARED_CONCEPT_P (fndecl))
14825 check_function_concept (fndecl);
14827 /* Lambda closure members are implicitly constexpr if possible. */
14828 if (cxx_dialect >= cxx1z
14829 && LAMBDA_TYPE_P (CP_DECL_CONTEXT (fndecl))
14830 && (processing_template_decl
14831 || is_valid_constexpr_fn (fndecl, /*complain*/false))
14832 && potential_constant_expression (DECL_SAVED_TREE (fndecl)))
14833 DECL_DECLARED_CONSTEXPR_P (fndecl) = true;
14835 /* Save constexpr function body before it gets munged by
14836 the NRV transformation. */
14837 maybe_save_function_definition (fndecl);
14839 /* Invoke the pre-genericize plugin before we start munging things. */
14840 if (!processing_template_decl)
14841 invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE, fndecl);
14843 /* Perform delayed folding before NRV transformation. */
14844 if (!processing_template_decl)
14845 cp_fold_function (fndecl);
14847 /* Set up the named return value optimization, if we can. Candidate
14848 variables are selected in check_return_expr. */
14849 if (current_function_return_value)
14851 tree r = current_function_return_value;
14852 tree outer;
14854 if (r != error_mark_node
14855 /* This is only worth doing for fns that return in memory--and
14856 simpler, since we don't have to worry about promoted modes. */
14857 && aggregate_value_p (TREE_TYPE (TREE_TYPE (fndecl)), fndecl)
14858 /* Only allow this for variables declared in the outer scope of
14859 the function so we know that their lifetime always ends with a
14860 return; see g++.dg/opt/nrv6.C. We could be more flexible if
14861 we were to do this optimization in tree-ssa. */
14862 && (outer = outer_curly_brace_block (fndecl))
14863 && chain_member (r, BLOCK_VARS (outer)))
14864 finalize_nrv (&DECL_SAVED_TREE (fndecl), r, DECL_RESULT (fndecl));
14866 current_function_return_value = NULL_TREE;
14869 /* Remember that we were in class scope. */
14870 if (current_class_name)
14871 ctype = current_class_type;
14873 /* Must mark the RESULT_DECL as being in this function. */
14874 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
14876 /* Set the BLOCK_SUPERCONTEXT of the outermost function scope to point
14877 to the FUNCTION_DECL node itself. */
14878 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
14880 /* Save away current state, if appropriate. */
14881 if (!processing_template_decl)
14882 save_function_data (fndecl);
14884 /* Complain if there's just no return statement. */
14885 if (warn_return_type
14886 && !VOID_TYPE_P (TREE_TYPE (fntype))
14887 && !dependent_type_p (TREE_TYPE (fntype))
14888 && !current_function_returns_value && !current_function_returns_null
14889 /* Don't complain if we abort or throw. */
14890 && !current_function_returns_abnormally
14891 /* Don't complain if there's an infinite loop. */
14892 && !current_function_infinite_loop
14893 /* Don't complain if we are declared noreturn. */
14894 && !TREE_THIS_VOLATILE (fndecl)
14895 && !DECL_NAME (DECL_RESULT (fndecl))
14896 && !TREE_NO_WARNING (fndecl)
14897 /* Structor return values (if any) are set by the compiler. */
14898 && !DECL_CONSTRUCTOR_P (fndecl)
14899 && !DECL_DESTRUCTOR_P (fndecl)
14900 && targetm.warn_func_return (fndecl))
14902 warning (OPT_Wreturn_type,
14903 "no return statement in function returning non-void");
14904 TREE_NO_WARNING (fndecl) = 1;
14907 /* Store the end of the function, so that we get good line number
14908 info for the epilogue. */
14909 cfun->function_end_locus = input_location;
14911 /* Complain about parameters that are only set, but never otherwise used. */
14912 if (warn_unused_but_set_parameter
14913 && !processing_template_decl
14914 && errorcount == unused_but_set_errorcount
14915 && !DECL_CLONED_FUNCTION_P (fndecl))
14917 tree decl;
14919 for (decl = DECL_ARGUMENTS (fndecl);
14920 decl;
14921 decl = DECL_CHAIN (decl))
14922 if (TREE_USED (decl)
14923 && TREE_CODE (decl) == PARM_DECL
14924 && !DECL_READ_P (decl)
14925 && DECL_NAME (decl)
14926 && !DECL_ARTIFICIAL (decl)
14927 && !TREE_NO_WARNING (decl)
14928 && !DECL_IN_SYSTEM_HEADER (decl)
14929 && TREE_TYPE (decl) != error_mark_node
14930 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
14931 && (!CLASS_TYPE_P (TREE_TYPE (decl))
14932 || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl))))
14933 warning_at (DECL_SOURCE_LOCATION (decl),
14934 OPT_Wunused_but_set_parameter,
14935 "parameter %qD set but not used", decl);
14936 unused_but_set_errorcount = errorcount;
14939 /* Complain about locally defined typedefs that are not used in this
14940 function. */
14941 maybe_warn_unused_local_typedefs ();
14943 /* Possibly warn about unused parameters. */
14944 if (warn_unused_parameter
14945 && !processing_template_decl
14946 && !DECL_CLONED_FUNCTION_P (fndecl))
14947 do_warn_unused_parameter (fndecl);
14949 /* Genericize before inlining. */
14950 if (!processing_template_decl)
14952 struct language_function *f = DECL_SAVED_FUNCTION_DATA (fndecl);
14953 cp_genericize (fndecl);
14954 /* Clear out the bits we don't need. */
14955 f->x_current_class_ptr = NULL;
14956 f->x_current_class_ref = NULL;
14957 f->x_eh_spec_block = NULL;
14958 f->x_in_charge_parm = NULL;
14959 f->x_vtt_parm = NULL;
14960 f->x_return_value = NULL;
14961 f->bindings = NULL;
14962 f->extern_decl_map = NULL;
14963 f->infinite_loops = NULL;
14965 /* Clear out the bits we don't need. */
14966 local_names = NULL;
14968 /* We're leaving the context of this function, so zap cfun. It's still in
14969 DECL_STRUCT_FUNCTION, and we'll restore it in tree_rest_of_compilation. */
14970 set_cfun (NULL);
14971 current_function_decl = NULL;
14973 /* If this is an in-class inline definition, we may have to pop the
14974 bindings for the template parameters that we added in
14975 maybe_begin_member_template_processing when start_function was
14976 called. */
14977 if (inclass_inline)
14978 maybe_end_member_template_processing ();
14980 /* Leave the scope of the class. */
14981 if (ctype)
14982 pop_nested_class ();
14984 --function_depth;
14986 /* Clean up. */
14987 current_function_decl = NULL_TREE;
14989 invoke_plugin_callbacks (PLUGIN_FINISH_PARSE_FUNCTION, fndecl);
14990 return fndecl;
14993 /* Create the FUNCTION_DECL for a function definition.
14994 DECLSPECS and DECLARATOR are the parts of the declaration;
14995 they describe the return type and the name of the function,
14996 but twisted together in a fashion that parallels the syntax of C.
14998 This function creates a binding context for the function body
14999 as well as setting up the FUNCTION_DECL in current_function_decl.
15001 Returns a FUNCTION_DECL on success.
15003 If the DECLARATOR is not suitable for a function (it defines a datum
15004 instead), we return 0, which tells yyparse to report a parse error.
15006 May return void_type_node indicating that this method is actually
15007 a friend. See grokfield for more details.
15009 Came here with a `.pushlevel' .
15011 DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
15012 CHANGES TO CODE IN `grokfield'. */
15014 tree
15015 grokmethod (cp_decl_specifier_seq *declspecs,
15016 const cp_declarator *declarator, tree attrlist)
15018 tree fndecl = grokdeclarator (declarator, declspecs, MEMFUNCDEF, 0,
15019 &attrlist);
15021 if (fndecl == error_mark_node)
15022 return error_mark_node;
15024 if (fndecl == NULL || TREE_CODE (fndecl) != FUNCTION_DECL)
15026 error ("invalid member function declaration");
15027 return error_mark_node;
15030 if (attrlist)
15031 cplus_decl_attributes (&fndecl, attrlist, 0);
15033 /* Pass friends other than inline friend functions back. */
15034 if (fndecl == void_type_node)
15035 return fndecl;
15037 if (DECL_IN_AGGR_P (fndecl))
15039 if (DECL_CLASS_SCOPE_P (fndecl))
15040 error ("%qD is already defined in class %qT", fndecl,
15041 DECL_CONTEXT (fndecl));
15042 return error_mark_node;
15045 check_template_shadow (fndecl);
15047 if (TREE_PUBLIC (fndecl))
15048 DECL_COMDAT (fndecl) = 1;
15049 DECL_DECLARED_INLINE_P (fndecl) = 1;
15050 DECL_NO_INLINE_WARNING_P (fndecl) = 1;
15052 /* We process method specializations in finish_struct_1. */
15053 if (processing_template_decl && !DECL_TEMPLATE_SPECIALIZATION (fndecl))
15055 fndecl = push_template_decl (fndecl);
15056 if (fndecl == error_mark_node)
15057 return fndecl;
15060 if (! DECL_FRIEND_P (fndecl))
15062 if (DECL_CHAIN (fndecl))
15064 fndecl = copy_node (fndecl);
15065 TREE_CHAIN (fndecl) = NULL_TREE;
15069 cp_finish_decl (fndecl, NULL_TREE, false, NULL_TREE, 0);
15071 DECL_IN_AGGR_P (fndecl) = 1;
15072 return fndecl;
15076 /* VAR is a VAR_DECL. If its type is incomplete, remember VAR so that
15077 we can lay it out later, when and if its type becomes complete.
15079 Also handle constexpr variables where the initializer involves
15080 an unlowered PTRMEM_CST because the class isn't complete yet. */
15082 void
15083 maybe_register_incomplete_var (tree var)
15085 gcc_assert (VAR_P (var));
15087 /* Keep track of variables with incomplete types. */
15088 if (!processing_template_decl && TREE_TYPE (var) != error_mark_node
15089 && DECL_EXTERNAL (var))
15091 tree inner_type = TREE_TYPE (var);
15093 while (TREE_CODE (inner_type) == ARRAY_TYPE)
15094 inner_type = TREE_TYPE (inner_type);
15095 inner_type = TYPE_MAIN_VARIANT (inner_type);
15097 if ((!COMPLETE_TYPE_P (inner_type) && CLASS_TYPE_P (inner_type))
15098 /* RTTI TD entries are created while defining the type_info. */
15099 || (TYPE_LANG_SPECIFIC (inner_type)
15100 && TYPE_BEING_DEFINED (inner_type)))
15102 incomplete_var iv = {var, inner_type};
15103 vec_safe_push (incomplete_vars, iv);
15105 else if (!(DECL_LANG_SPECIFIC (var) && DECL_TEMPLATE_INFO (var))
15106 && decl_constant_var_p (var)
15107 && (TYPE_PTRMEM_P (inner_type) || CLASS_TYPE_P (inner_type)))
15109 /* When the outermost open class is complete we can resolve any
15110 pointers-to-members. */
15111 tree context = outermost_open_class ();
15112 incomplete_var iv = {var, context};
15113 vec_safe_push (incomplete_vars, iv);
15118 /* Called when a class type (given by TYPE) is defined. If there are
15119 any existing VAR_DECLs whose type has been completed by this
15120 declaration, update them now. */
15122 void
15123 complete_vars (tree type)
15125 unsigned ix;
15126 incomplete_var *iv;
15128 for (ix = 0; vec_safe_iterate (incomplete_vars, ix, &iv); )
15130 if (same_type_p (type, iv->incomplete_type))
15132 tree var = iv->decl;
15133 tree type = TREE_TYPE (var);
15135 if (type != error_mark_node
15136 && (TYPE_MAIN_VARIANT (strip_array_types (type))
15137 == iv->incomplete_type))
15139 /* Complete the type of the variable. The VAR_DECL itself
15140 will be laid out in expand_expr. */
15141 complete_type (type);
15142 cp_apply_type_quals_to_decl (cp_type_quals (type), var);
15145 /* Remove this entry from the list. */
15146 incomplete_vars->unordered_remove (ix);
15148 else
15149 ix++;
15152 /* Check for pending declarations which may have abstract type. */
15153 complete_type_check_abstract (type);
15156 /* If DECL is of a type which needs a cleanup, build and return an
15157 expression to perform that cleanup here. Return NULL_TREE if no
15158 cleanup need be done. DECL can also be a _REF when called from
15159 split_nonconstant_init_1. */
15161 tree
15162 cxx_maybe_build_cleanup (tree decl, tsubst_flags_t complain)
15164 tree type;
15165 tree attr;
15166 tree cleanup;
15168 /* Assume no cleanup is required. */
15169 cleanup = NULL_TREE;
15171 if (error_operand_p (decl))
15172 return cleanup;
15174 /* Handle "__attribute__((cleanup))". We run the cleanup function
15175 before the destructor since the destructor is what actually
15176 terminates the lifetime of the object. */
15177 if (DECL_P (decl))
15178 attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
15179 else
15180 attr = NULL_TREE;
15181 if (attr)
15183 tree id;
15184 tree fn;
15185 tree arg;
15187 /* Get the name specified by the user for the cleanup function. */
15188 id = TREE_VALUE (TREE_VALUE (attr));
15189 /* Look up the name to find the cleanup function to call. It is
15190 important to use lookup_name here because that is what is
15191 used in c-common.c:handle_cleanup_attribute when performing
15192 initial checks on the attribute. Note that those checks
15193 include ensuring that the function found is not an overloaded
15194 function, or an object with an overloaded call operator,
15195 etc.; we can rely on the fact that the function found is an
15196 ordinary FUNCTION_DECL. */
15197 fn = lookup_name (id);
15198 arg = build_address (decl);
15199 if (!mark_used (decl, complain) && !(complain & tf_error))
15200 return error_mark_node;
15201 cleanup = cp_build_function_call_nary (fn, complain, arg, NULL_TREE);
15202 if (cleanup == error_mark_node)
15203 return error_mark_node;
15205 /* Handle ordinary C++ destructors. */
15206 type = TREE_TYPE (decl);
15207 if (type_build_dtor_call (type))
15209 int flags = LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR;
15210 tree addr;
15211 tree call;
15213 if (TREE_CODE (type) == ARRAY_TYPE)
15214 addr = decl;
15215 else
15216 addr = build_address (decl);
15218 call = build_delete (TREE_TYPE (addr), addr,
15219 sfk_complete_destructor, flags, 0, complain);
15220 if (call == error_mark_node)
15221 cleanup = error_mark_node;
15222 else if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
15223 /* Discard the call. */;
15224 else if (cleanup)
15225 cleanup = cp_build_compound_expr (cleanup, call, complain);
15226 else
15227 cleanup = call;
15230 /* build_delete sets the location of the destructor call to the
15231 current location, even though the destructor is going to be
15232 called later, at the end of the current scope. This can lead to
15233 a "jumpy" behavior for users of debuggers when they step around
15234 the end of the block. So let's unset the location of the
15235 destructor call instead. */
15236 protected_set_expr_location (cleanup, UNKNOWN_LOCATION);
15238 if (cleanup
15239 && DECL_P (decl)
15240 && !lookup_attribute ("warn_unused", TYPE_ATTRIBUTES (TREE_TYPE (decl)))
15241 /* Treat objects with destructors as used; the destructor may do
15242 something substantive. */
15243 && !mark_used (decl, complain) && !(complain & tf_error))
15244 return error_mark_node;
15246 return cleanup;
15250 /* Return the FUNCTION_TYPE that corresponds to MEMFNTYPE, which can be a
15251 FUNCTION_DECL, METHOD_TYPE, FUNCTION_TYPE, pointer or reference to
15252 METHOD_TYPE or FUNCTION_TYPE, or pointer to member function. */
15254 tree
15255 static_fn_type (tree memfntype)
15257 tree fntype;
15258 tree args;
15260 if (TYPE_PTRMEMFUNC_P (memfntype))
15261 memfntype = TYPE_PTRMEMFUNC_FN_TYPE (memfntype);
15262 if (POINTER_TYPE_P (memfntype)
15263 || TREE_CODE (memfntype) == FUNCTION_DECL)
15264 memfntype = TREE_TYPE (memfntype);
15265 if (TREE_CODE (memfntype) == FUNCTION_TYPE)
15266 return memfntype;
15267 gcc_assert (TREE_CODE (memfntype) == METHOD_TYPE);
15268 args = TYPE_ARG_TYPES (memfntype);
15269 cp_ref_qualifier rqual = type_memfn_rqual (memfntype);
15270 fntype = build_function_type (TREE_TYPE (memfntype), TREE_CHAIN (args));
15271 fntype = apply_memfn_quals (fntype, type_memfn_quals (memfntype), rqual);
15272 fntype = (cp_build_type_attribute_variant
15273 (fntype, TYPE_ATTRIBUTES (memfntype)));
15274 fntype = (build_exception_variant
15275 (fntype, TYPE_RAISES_EXCEPTIONS (memfntype)));
15276 if (TYPE_HAS_LATE_RETURN_TYPE (memfntype))
15277 TYPE_HAS_LATE_RETURN_TYPE (fntype) = 1;
15278 return fntype;
15281 /* DECL was originally constructed as a non-static member function,
15282 but turned out to be static. Update it accordingly. */
15284 void
15285 revert_static_member_fn (tree decl)
15287 tree stype = static_fn_type (decl);
15288 cp_cv_quals quals = type_memfn_quals (stype);
15289 cp_ref_qualifier rqual = type_memfn_rqual (stype);
15291 if (quals != TYPE_UNQUALIFIED || rqual != REF_QUAL_NONE)
15292 stype = apply_memfn_quals (stype, TYPE_UNQUALIFIED, REF_QUAL_NONE);
15294 TREE_TYPE (decl) = stype;
15296 if (DECL_ARGUMENTS (decl))
15297 DECL_ARGUMENTS (decl) = DECL_CHAIN (DECL_ARGUMENTS (decl));
15298 DECL_STATIC_FUNCTION_P (decl) = 1;
15301 /* Return which tree structure is used by T, or TS_CP_GENERIC if T is
15302 one of the language-independent trees. */
15304 enum cp_tree_node_structure_enum
15305 cp_tree_node_structure (union lang_tree_node * t)
15307 switch (TREE_CODE (&t->generic))
15309 case DEFAULT_ARG: return TS_CP_DEFAULT_ARG;
15310 case DEFERRED_NOEXCEPT: return TS_CP_DEFERRED_NOEXCEPT;
15311 case IDENTIFIER_NODE: return TS_CP_IDENTIFIER;
15312 case OVERLOAD: return TS_CP_OVERLOAD;
15313 case TEMPLATE_PARM_INDEX: return TS_CP_TPI;
15314 case PTRMEM_CST: return TS_CP_PTRMEM;
15315 case BASELINK: return TS_CP_BASELINK;
15316 case TEMPLATE_DECL: return TS_CP_TEMPLATE_DECL;
15317 case STATIC_ASSERT: return TS_CP_STATIC_ASSERT;
15318 case ARGUMENT_PACK_SELECT: return TS_CP_ARGUMENT_PACK_SELECT;
15319 case TRAIT_EXPR: return TS_CP_TRAIT_EXPR;
15320 case LAMBDA_EXPR: return TS_CP_LAMBDA_EXPR;
15321 case TEMPLATE_INFO: return TS_CP_TEMPLATE_INFO;
15322 case CONSTRAINT_INFO: return TS_CP_CONSTRAINT_INFO;
15323 case USERDEF_LITERAL: return TS_CP_USERDEF_LITERAL;
15324 default: return TS_CP_GENERIC;
15328 /* Build the void_list_node (void_type_node having been created). */
15329 tree
15330 build_void_list_node (void)
15332 tree t = build_tree_list (NULL_TREE, void_type_node);
15333 return t;
15336 bool
15337 cp_missing_noreturn_ok_p (tree decl)
15339 /* A missing noreturn is ok for the `main' function. */
15340 return DECL_MAIN_P (decl);
15343 /* Return the decl used to identify the COMDAT group into which DECL should
15344 be placed. */
15346 tree
15347 cxx_comdat_group (tree decl)
15349 /* Virtual tables, construction virtual tables, and virtual table
15350 tables all go in a single COMDAT group, named after the primary
15351 virtual table. */
15352 if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl))
15353 decl = CLASSTYPE_VTABLES (DECL_CONTEXT (decl));
15354 /* For all other DECLs, the COMDAT group is the mangled name of the
15355 declaration itself. */
15356 else
15358 while (DECL_THUNK_P (decl))
15360 /* If TARGET_USE_LOCAL_THUNK_ALIAS_P, use_thunk puts the thunk
15361 into the same section as the target function. In that case
15362 we must return target's name. */
15363 tree target = THUNK_TARGET (decl);
15364 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (target)
15365 && DECL_SECTION_NAME (target) != NULL
15366 && DECL_ONE_ONLY (target))
15367 decl = target;
15368 else
15369 break;
15373 return decl;
15376 /* Returns the return type for FN as written by the user, which may include
15377 a placeholder for a deduced return type. */
15379 tree
15380 fndecl_declared_return_type (tree fn)
15382 fn = STRIP_TEMPLATE (fn);
15383 if (FNDECL_USED_AUTO (fn))
15385 struct language_function *f = NULL;
15386 if (DECL_STRUCT_FUNCTION (fn))
15387 f = DECL_STRUCT_FUNCTION (fn)->language;
15388 if (f == NULL)
15389 f = DECL_SAVED_FUNCTION_DATA (fn);
15390 return f->x_auto_return_pattern;
15392 return TREE_TYPE (TREE_TYPE (fn));
15395 /* Returns true iff DECL was declared with an auto return type and it has
15396 not yet been deduced to a real type. */
15398 bool
15399 undeduced_auto_decl (tree decl)
15401 if (cxx_dialect < cxx14)
15402 return false;
15403 return type_uses_auto (TREE_TYPE (decl));
15406 /* Complain if DECL has an undeduced return type. */
15408 void
15409 require_deduced_type (tree decl)
15411 if (undeduced_auto_decl (decl))
15412 error ("use of %qD before deduction of %<auto%>", decl);
15415 #include "gt-cp-decl.h"