* doc/extend.texi (Java Exceptions): Remove.
[official-gcc.git] / gcc / cp / decl.c
blobc8f766618d137e4c7e4c5b16c9dfb0ba2fbf8ef1
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 for-init statement were in scope after the for-statement
643 ended. We only use the new rules if flag_new_for_scope is
644 nonzero. */
645 leaving_for_scope
646 = current_binding_level->kind == sk_for && flag_new_for_scope == 1;
648 /* Before we remove the declarations first check for unused variables. */
649 if ((warn_unused_variable || warn_unused_but_set_variable)
650 && current_binding_level->kind != sk_template_parms
651 && !processing_template_decl)
652 for (tree d = getdecls (); d; d = TREE_CHAIN (d))
654 /* There are cases where D itself is a TREE_LIST. See in
655 push_local_binding where the list of decls returned by
656 getdecls is built. */
657 decl = TREE_CODE (d) == TREE_LIST ? TREE_VALUE (d) : d;
658 tree type = TREE_TYPE (decl);
659 if (VAR_P (decl)
660 && (! TREE_USED (decl) || !DECL_READ_P (decl))
661 && ! DECL_IN_SYSTEM_HEADER (decl)
662 && DECL_NAME (decl) && ! DECL_ARTIFICIAL (decl)
663 && type != error_mark_node
664 && (!CLASS_TYPE_P (type)
665 || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
666 || lookup_attribute ("warn_unused",
667 TYPE_ATTRIBUTES (TREE_TYPE (decl)))))
669 if (! TREE_USED (decl))
670 warning_at (DECL_SOURCE_LOCATION (decl),
671 OPT_Wunused_variable, "unused variable %qD", decl);
672 else if (DECL_CONTEXT (decl) == current_function_decl
673 // For -Wunused-but-set-variable leave references alone.
674 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
675 && errorcount == unused_but_set_errorcount)
677 warning_at (DECL_SOURCE_LOCATION (decl),
678 OPT_Wunused_but_set_variable,
679 "variable %qD set but not used", decl);
680 unused_but_set_errorcount = errorcount;
685 /* Remove declarations for all the DECLs in this level. */
686 for (link = decls; link; link = TREE_CHAIN (link))
688 if (leaving_for_scope && VAR_P (link)
689 /* It's hard to make this ARM compatibility hack play nicely with
690 lambdas, and it really isn't necessary in C++11 mode. */
691 && cxx_dialect < cxx11
692 && DECL_NAME (link))
694 tree name = DECL_NAME (link);
695 cxx_binding *ob;
696 tree ns_binding;
698 ob = outer_binding (name,
699 IDENTIFIER_BINDING (name),
700 /*class_p=*/true);
701 if (!ob)
702 ns_binding = IDENTIFIER_NAMESPACE_VALUE (name);
703 else
704 ns_binding = NULL_TREE;
706 if (ob && ob->scope == current_binding_level->level_chain)
707 /* We have something like:
709 int i;
710 for (int i; ;);
712 and we are leaving the `for' scope. There's no reason to
713 keep the binding of the inner `i' in this case. */
714 pop_binding (name, link);
715 else if ((ob && (TREE_CODE (ob->value) == TYPE_DECL))
716 || (ns_binding && TREE_CODE (ns_binding) == TYPE_DECL))
717 /* Here, we have something like:
719 typedef int I;
721 void f () {
722 for (int I; ;);
725 We must pop the for-scope binding so we know what's a
726 type and what isn't. */
727 pop_binding (name, link);
728 else
730 /* Mark this VAR_DECL as dead so that we can tell we left it
731 there only for backward compatibility. */
732 DECL_DEAD_FOR_LOCAL (link) = 1;
734 /* Keep track of what should have happened when we
735 popped the binding. */
736 if (ob && ob->value)
738 SET_DECL_SHADOWED_FOR_VAR (link, ob->value);
739 DECL_HAS_SHADOWED_FOR_VAR_P (link) = 1;
742 /* Add it to the list of dead variables in the next
743 outermost binding to that we can remove these when we
744 leave that binding. */
745 vec_safe_push (
746 current_binding_level->level_chain->dead_vars_from_for,
747 link);
749 /* Although we don't pop the cxx_binding, we do clear
750 its SCOPE since the scope is going away now. */
751 IDENTIFIER_BINDING (name)->scope
752 = current_binding_level->level_chain;
755 else
757 tree name;
759 /* Remove the binding. */
760 decl = link;
762 if (TREE_CODE (decl) == TREE_LIST)
763 decl = TREE_VALUE (decl);
764 name = decl;
766 if (TREE_CODE (name) == OVERLOAD)
767 name = OVL_FUNCTION (name);
769 gcc_assert (DECL_P (name));
770 pop_binding (DECL_NAME (name), decl);
774 /* Remove declarations for any `for' variables from inner scopes
775 that we kept around. */
776 FOR_EACH_VEC_SAFE_ELT_REVERSE (current_binding_level->dead_vars_from_for,
777 ix, decl)
778 pop_binding (DECL_NAME (decl), decl);
780 /* Restore the IDENTIFIER_TYPE_VALUEs. */
781 for (link = current_binding_level->type_shadowed;
782 link; link = TREE_CHAIN (link))
783 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link), TREE_VALUE (link));
785 /* Restore the IDENTIFIER_LABEL_VALUEs for local labels. */
786 FOR_EACH_VEC_SAFE_ELT_REVERSE (current_binding_level->shadowed_labels,
787 ix, label_bind)
788 pop_local_label (label_bind->label, label_bind->prev_value);
790 /* There may be OVERLOADs (wrapped in TREE_LISTs) on the BLOCK_VARs
791 list if a `using' declaration put them there. The debugging
792 back ends won't understand OVERLOAD, so we remove them here.
793 Because the BLOCK_VARS are (temporarily) shared with
794 CURRENT_BINDING_LEVEL->NAMES we must do this fixup after we have
795 popped all the bindings. */
796 if (block)
798 tree* d;
800 for (d = &BLOCK_VARS (block); *d; )
802 if (TREE_CODE (*d) == TREE_LIST)
803 *d = TREE_CHAIN (*d);
804 else
805 d = &DECL_CHAIN (*d);
809 /* If the level being exited is the top level of a function,
810 check over all the labels. */
811 if (functionbody)
813 if (block)
815 /* Since this is the top level block of a function, the vars are
816 the function's parameters. Don't leave them in the BLOCK
817 because they are found in the FUNCTION_DECL instead. */
818 BLOCK_VARS (block) = 0;
819 pop_labels (block);
821 else
822 pop_labels (subblocks);
825 kind = current_binding_level->kind;
826 if (kind == sk_cleanup)
828 tree stmt;
830 /* If this is a temporary binding created for a cleanup, then we'll
831 have pushed a statement list level. Pop that, create a new
832 BIND_EXPR for the block, and insert it into the stream. */
833 stmt = pop_stmt_list (current_binding_level->statement_list);
834 stmt = c_build_bind_expr (input_location, block, stmt);
835 add_stmt (stmt);
838 leave_scope ();
839 if (functionbody)
841 /* The current function is being defined, so its DECL_INITIAL
842 should be error_mark_node. */
843 gcc_assert (DECL_INITIAL (current_function_decl) == error_mark_node);
844 DECL_INITIAL (current_function_decl) = block ? block : subblocks;
845 if (subblocks)
847 if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
849 if (BLOCK_SUBBLOCKS (subblocks))
850 BLOCK_OUTER_CURLY_BRACE_P (BLOCK_SUBBLOCKS (subblocks)) = 1;
852 else
853 BLOCK_OUTER_CURLY_BRACE_P (subblocks) = 1;
856 else if (block)
857 current_binding_level->blocks
858 = block_chainon (current_binding_level->blocks, block);
860 /* If we did not make a block for the level just exited,
861 any blocks made for inner levels
862 (since they cannot be recorded as subblocks in that level)
863 must be carried forward so they will later become subblocks
864 of something else. */
865 else if (subblocks)
866 current_binding_level->blocks
867 = block_chainon (current_binding_level->blocks, subblocks);
869 /* Each and every BLOCK node created here in `poplevel' is important
870 (e.g. for proper debugging information) so if we created one
871 earlier, mark it as "used". */
872 if (block)
873 TREE_USED (block) = 1;
875 /* All temporary bindings created for cleanups are popped silently. */
876 if (kind == sk_cleanup)
877 goto restart;
879 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
880 return block;
883 /* Walk all the namespaces contained NAMESPACE, including NAMESPACE
884 itself, calling F for each. The DATA is passed to F as well. */
886 static int
887 walk_namespaces_r (tree name_space, walk_namespaces_fn f, void* data)
889 int result = 0;
890 tree current = NAMESPACE_LEVEL (name_space)->namespaces;
892 result |= (*f) (name_space, data);
894 for (; current; current = DECL_CHAIN (current))
895 result |= walk_namespaces_r (current, f, data);
897 return result;
900 /* Walk all the namespaces, calling F for each. The DATA is passed to
901 F as well. */
904 walk_namespaces (walk_namespaces_fn f, void* data)
906 return walk_namespaces_r (global_namespace, f, data);
909 /* Call wrapup_globals_declarations for the globals in NAMESPACE. */
912 wrapup_globals_for_namespace (tree name_space, void* data ATTRIBUTE_UNUSED)
914 cp_binding_level *level = NAMESPACE_LEVEL (name_space);
915 vec<tree, va_gc> *statics = level->static_decls;
916 tree *vec = statics->address ();
917 int len = statics->length ();
919 if (warn_unused_function)
921 tree decl;
922 unsigned int i;
923 FOR_EACH_VEC_SAFE_ELT (statics, i, decl)
924 if (TREE_CODE (decl) == FUNCTION_DECL
925 && DECL_INITIAL (decl) == 0
926 && DECL_EXTERNAL (decl)
927 && !TREE_PUBLIC (decl)
928 && !DECL_ARTIFICIAL (decl)
929 && !TREE_NO_WARNING (decl))
931 warning_at (DECL_SOURCE_LOCATION (decl),
932 OPT_Wunused_function,
933 "%qF declared %<static%> but never defined", decl);
934 TREE_NO_WARNING (decl) = 1;
938 /* Write out any globals that need to be output. */
939 return wrapup_global_declarations (vec, len);
942 /* In C++, you don't have to write `struct S' to refer to `S'; you
943 can just use `S'. We accomplish this by creating a TYPE_DECL as
944 if the user had written `typedef struct S S'. Create and return
945 the TYPE_DECL for TYPE. */
947 tree
948 create_implicit_typedef (tree name, tree type)
950 tree decl;
952 decl = build_decl (input_location, TYPE_DECL, name, type);
953 DECL_ARTIFICIAL (decl) = 1;
954 /* There are other implicit type declarations, like the one *within*
955 a class that allows you to write `S::S'. We must distinguish
956 amongst these. */
957 SET_DECL_IMPLICIT_TYPEDEF_P (decl);
958 TYPE_NAME (type) = decl;
959 TYPE_STUB_DECL (type) = decl;
961 return decl;
964 /* Remember a local name for name-mangling purposes. */
966 static void
967 push_local_name (tree decl)
969 size_t i, nelts;
970 tree t, name;
972 timevar_start (TV_NAME_LOOKUP);
974 name = DECL_NAME (decl);
976 nelts = vec_safe_length (local_names);
977 for (i = 0; i < nelts; i++)
979 t = (*local_names)[i];
980 if (DECL_NAME (t) == name)
982 if (!DECL_LANG_SPECIFIC (decl))
983 retrofit_lang_decl (decl);
984 DECL_LANG_SPECIFIC (decl)->u.base.u2sel = 1;
985 if (DECL_DISCRIMINATOR_SET_P (t))
986 DECL_DISCRIMINATOR (decl) = DECL_DISCRIMINATOR (t) + 1;
987 else
988 DECL_DISCRIMINATOR (decl) = 1;
990 (*local_names)[i] = decl;
991 timevar_stop (TV_NAME_LOOKUP);
992 return;
996 vec_safe_push (local_names, decl);
997 timevar_stop (TV_NAME_LOOKUP);
1000 /* Subroutine of duplicate_decls: return truthvalue of whether
1001 or not types of these decls match.
1003 For C++, we must compare the parameter list so that `int' can match
1004 `int&' in a parameter position, but `int&' is not confused with
1005 `const int&'. */
1008 decls_match (tree newdecl, tree olddecl)
1010 int types_match;
1012 if (newdecl == olddecl)
1013 return 1;
1015 if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
1016 /* If the two DECLs are not even the same kind of thing, we're not
1017 interested in their types. */
1018 return 0;
1020 gcc_assert (DECL_P (newdecl));
1022 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1024 tree f1 = TREE_TYPE (newdecl);
1025 tree f2 = TREE_TYPE (olddecl);
1026 tree p1 = TYPE_ARG_TYPES (f1);
1027 tree p2 = TYPE_ARG_TYPES (f2);
1028 tree r2;
1030 /* Specializations of different templates are different functions
1031 even if they have the same type. */
1032 tree t1 = (DECL_USE_TEMPLATE (newdecl)
1033 ? DECL_TI_TEMPLATE (newdecl)
1034 : NULL_TREE);
1035 tree t2 = (DECL_USE_TEMPLATE (olddecl)
1036 ? DECL_TI_TEMPLATE (olddecl)
1037 : NULL_TREE);
1038 if (t1 != t2)
1039 return 0;
1041 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
1042 && ! (DECL_EXTERN_C_P (newdecl)
1043 && DECL_EXTERN_C_P (olddecl)))
1044 return 0;
1046 /* A new declaration doesn't match a built-in one unless it
1047 is also extern "C". */
1048 if (DECL_IS_BUILTIN (olddecl)
1049 && DECL_EXTERN_C_P (olddecl) && !DECL_EXTERN_C_P (newdecl))
1050 return 0;
1052 if (TREE_CODE (f1) != TREE_CODE (f2))
1053 return 0;
1055 /* A declaration with deduced return type should use its pre-deduction
1056 type for declaration matching. */
1057 r2 = fndecl_declared_return_type (olddecl);
1059 if (same_type_p (TREE_TYPE (f1), r2))
1061 if (!prototype_p (f2) && DECL_EXTERN_C_P (olddecl)
1062 && (DECL_BUILT_IN (olddecl)
1063 #ifndef NO_IMPLICIT_EXTERN_C
1064 || (DECL_IN_SYSTEM_HEADER (newdecl) && !DECL_CLASS_SCOPE_P (newdecl))
1065 || (DECL_IN_SYSTEM_HEADER (olddecl) && !DECL_CLASS_SCOPE_P (olddecl))
1066 #endif
1069 types_match = self_promoting_args_p (p1);
1070 if (p1 == void_list_node)
1071 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
1073 #ifndef NO_IMPLICIT_EXTERN_C
1074 else if (!prototype_p (f1)
1075 && (DECL_EXTERN_C_P (olddecl)
1076 && DECL_IN_SYSTEM_HEADER (olddecl)
1077 && !DECL_CLASS_SCOPE_P (olddecl))
1078 && (DECL_EXTERN_C_P (newdecl)
1079 && DECL_IN_SYSTEM_HEADER (newdecl)
1080 && !DECL_CLASS_SCOPE_P (newdecl)))
1082 types_match = self_promoting_args_p (p2);
1083 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
1085 #endif
1086 else
1087 types_match =
1088 compparms (p1, p2)
1089 && type_memfn_rqual (f1) == type_memfn_rqual (f2)
1090 && (TYPE_ATTRIBUTES (TREE_TYPE (newdecl)) == NULL_TREE
1091 || comp_type_attributes (TREE_TYPE (newdecl),
1092 TREE_TYPE (olddecl)) != 0);
1094 else
1095 types_match = 0;
1097 /* The decls dont match if they correspond to two different versions
1098 of the same function. Disallow extern "C" functions to be
1099 versions for now. */
1100 if (types_match
1101 && !DECL_EXTERN_C_P (newdecl)
1102 && !DECL_EXTERN_C_P (olddecl)
1103 && targetm.target_option.function_versions (newdecl, olddecl))
1105 /* Mark functions as versions if necessary. Modify the mangled decl
1106 name if necessary. */
1107 if (DECL_FUNCTION_VERSIONED (newdecl)
1108 && DECL_FUNCTION_VERSIONED (olddecl))
1109 return 0;
1110 if (!DECL_FUNCTION_VERSIONED (newdecl))
1112 DECL_FUNCTION_VERSIONED (newdecl) = 1;
1113 if (DECL_ASSEMBLER_NAME_SET_P (newdecl))
1114 mangle_decl (newdecl);
1116 if (!DECL_FUNCTION_VERSIONED (olddecl))
1118 DECL_FUNCTION_VERSIONED (olddecl) = 1;
1119 if (DECL_ASSEMBLER_NAME_SET_P (olddecl))
1120 mangle_decl (olddecl);
1122 cgraph_node::record_function_versions (olddecl, newdecl);
1123 return 0;
1126 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1128 tree oldres = DECL_TEMPLATE_RESULT (olddecl);
1129 tree newres = DECL_TEMPLATE_RESULT (newdecl);
1131 if (TREE_CODE (newres) != TREE_CODE (oldres))
1132 return 0;
1134 if (!comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
1135 DECL_TEMPLATE_PARMS (olddecl)))
1136 return 0;
1138 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
1139 types_match = (same_type_p (TREE_TYPE (oldres), TREE_TYPE (newres))
1140 && equivalently_constrained (olddecl, newdecl));
1141 else
1142 // We don't need to check equivalently_constrained for variable and
1143 // function templates because we check it on the results.
1144 types_match = decls_match (oldres, newres);
1146 else
1148 /* Need to check scope for variable declaration (VAR_DECL).
1149 For typedef (TYPE_DECL), scope is ignored. */
1150 if (VAR_P (newdecl)
1151 && CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
1152 /* [dcl.link]
1153 Two declarations for an object with C language linkage
1154 with the same name (ignoring the namespace that qualify
1155 it) that appear in different namespace scopes refer to
1156 the same object. */
1157 && !(DECL_EXTERN_C_P (olddecl) && DECL_EXTERN_C_P (newdecl)))
1158 return 0;
1160 if (TREE_TYPE (newdecl) == error_mark_node)
1161 types_match = TREE_TYPE (olddecl) == error_mark_node;
1162 else if (TREE_TYPE (olddecl) == NULL_TREE)
1163 types_match = TREE_TYPE (newdecl) == NULL_TREE;
1164 else if (TREE_TYPE (newdecl) == NULL_TREE)
1165 types_match = 0;
1166 else
1167 types_match = comptypes (TREE_TYPE (newdecl),
1168 TREE_TYPE (olddecl),
1169 COMPARE_REDECLARATION);
1172 // Normal functions can be constrained, as can variable partial
1173 // specializations.
1174 if (types_match && VAR_OR_FUNCTION_DECL_P (newdecl))
1175 types_match = equivalently_constrained (newdecl, olddecl);
1177 return types_match;
1180 /* If NEWDECL is `static' and an `extern' was seen previously,
1181 warn about it. OLDDECL is the previous declaration.
1183 Note that this does not apply to the C++ case of declaring
1184 a variable `extern const' and then later `const'.
1186 Don't complain about built-in functions, since they are beyond
1187 the user's control. */
1189 void
1190 warn_extern_redeclared_static (tree newdecl, tree olddecl)
1192 if (TREE_CODE (newdecl) == TYPE_DECL
1193 || TREE_CODE (newdecl) == TEMPLATE_DECL
1194 || TREE_CODE (newdecl) == CONST_DECL
1195 || TREE_CODE (newdecl) == NAMESPACE_DECL)
1196 return;
1198 /* Don't get confused by static member functions; that's a different
1199 use of `static'. */
1200 if (TREE_CODE (newdecl) == FUNCTION_DECL
1201 && DECL_STATIC_FUNCTION_P (newdecl))
1202 return;
1204 /* If the old declaration was `static', or the new one isn't, then
1205 everything is OK. */
1206 if (DECL_THIS_STATIC (olddecl) || !DECL_THIS_STATIC (newdecl))
1207 return;
1209 /* It's OK to declare a builtin function as `static'. */
1210 if (TREE_CODE (olddecl) == FUNCTION_DECL
1211 && DECL_ARTIFICIAL (olddecl))
1212 return;
1214 if (permerror (DECL_SOURCE_LOCATION (newdecl),
1215 "%qD was declared %<extern%> and later %<static%>", newdecl))
1216 inform (DECL_SOURCE_LOCATION (olddecl),
1217 "previous declaration of %qD", olddecl);
1220 /* NEW_DECL is a redeclaration of OLD_DECL; both are functions or
1221 function templates. If their exception specifications do not
1222 match, issue a diagnostic. */
1224 static void
1225 check_redeclaration_exception_specification (tree new_decl,
1226 tree old_decl)
1228 tree new_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (new_decl));
1229 tree old_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old_decl));
1231 /* Two default specs are equivalent, don't force evaluation. */
1232 if (UNEVALUATED_NOEXCEPT_SPEC_P (new_exceptions)
1233 && UNEVALUATED_NOEXCEPT_SPEC_P (old_exceptions))
1234 return;
1236 maybe_instantiate_noexcept (new_decl);
1237 maybe_instantiate_noexcept (old_decl);
1238 new_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (new_decl));
1239 old_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old_decl));
1241 /* [except.spec]
1243 If any declaration of a function has an exception-specification,
1244 all declarations, including the definition and an explicit
1245 specialization, of that function shall have an
1246 exception-specification with the same set of type-ids. */
1247 if (! DECL_IS_BUILTIN (old_decl)
1248 && !comp_except_specs (new_exceptions, old_exceptions, ce_normal))
1250 const char *msg
1251 = "declaration of %qF has a different exception specifier";
1252 bool complained = true;
1253 location_t new_loc = DECL_SOURCE_LOCATION (new_decl);
1254 if (DECL_IN_SYSTEM_HEADER (old_decl))
1255 complained = pedwarn (new_loc, OPT_Wsystem_headers, msg, new_decl);
1256 else if (!flag_exceptions)
1257 /* We used to silently permit mismatched eh specs with
1258 -fno-exceptions, so make them a pedwarn now. */
1259 complained = pedwarn (new_loc, OPT_Wpedantic, msg, new_decl);
1260 else
1261 error_at (new_loc, msg, new_decl);
1262 if (complained)
1263 inform (DECL_SOURCE_LOCATION (old_decl),
1264 "from previous declaration %qF", old_decl);
1268 /* Return true if OLD_DECL and NEW_DECL agree on constexprness.
1269 Otherwise issue diagnostics. */
1271 static bool
1272 validate_constexpr_redeclaration (tree old_decl, tree new_decl)
1274 old_decl = STRIP_TEMPLATE (old_decl);
1275 new_decl = STRIP_TEMPLATE (new_decl);
1276 if (!VAR_OR_FUNCTION_DECL_P (old_decl)
1277 || !VAR_OR_FUNCTION_DECL_P (new_decl))
1278 return true;
1279 if (DECL_DECLARED_CONSTEXPR_P (old_decl)
1280 == DECL_DECLARED_CONSTEXPR_P (new_decl))
1281 return true;
1282 if (TREE_CODE (old_decl) == FUNCTION_DECL)
1284 if (DECL_BUILT_IN (old_decl))
1286 /* Hide a built-in declaration. */
1287 DECL_DECLARED_CONSTEXPR_P (old_decl)
1288 = DECL_DECLARED_CONSTEXPR_P (new_decl);
1289 return true;
1291 /* 7.1.5 [dcl.constexpr]
1292 Note: An explicit specialization can differ from the template
1293 declaration with respect to the constexpr specifier. */
1294 if (! DECL_TEMPLATE_SPECIALIZATION (old_decl)
1295 && DECL_TEMPLATE_SPECIALIZATION (new_decl))
1296 return true;
1298 error_at (DECL_SOURCE_LOCATION (new_decl),
1299 "redeclaration %qD differs in %<constexpr%> "
1300 "from previous declaration", new_decl);
1301 inform (DECL_SOURCE_LOCATION (old_decl),
1302 "previous declaration %qD", old_decl);
1303 return false;
1305 return true;
1308 // If OLDDECL and NEWDECL are concept declarations with the same type
1309 // (i.e., and template parameters), but different requirements,
1310 // emit diagnostics and return true. Otherwise, return false.
1311 static inline bool
1312 check_concept_refinement (tree olddecl, tree newdecl)
1314 if (!DECL_DECLARED_CONCEPT_P (olddecl) || !DECL_DECLARED_CONCEPT_P (newdecl))
1315 return false;
1317 tree d1 = DECL_TEMPLATE_RESULT (olddecl);
1318 tree d2 = DECL_TEMPLATE_RESULT (newdecl);
1319 if (TREE_CODE (d1) != TREE_CODE (d2))
1320 return false;
1322 tree t1 = TREE_TYPE (d1);
1323 tree t2 = TREE_TYPE (d2);
1324 if (TREE_CODE (d1) == FUNCTION_DECL)
1326 if (compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2))
1327 && comp_template_parms (DECL_TEMPLATE_PARMS (olddecl),
1328 DECL_TEMPLATE_PARMS (newdecl))
1329 && !equivalently_constrained (olddecl, newdecl))
1331 error ("cannot specialize concept %q#D", olddecl);
1332 return true;
1335 return false;
1338 /* DECL is a redeclaration of a function or function template. If
1339 it does have default arguments issue a diagnostic. Note: this
1340 function is used to enforce the requirements in C++11 8.3.6 about
1341 no default arguments in redeclarations. */
1343 static void
1344 check_redeclaration_no_default_args (tree decl)
1346 gcc_assert (DECL_DECLARES_FUNCTION_P (decl));
1348 for (tree t = FUNCTION_FIRST_USER_PARMTYPE (decl);
1349 t && t != void_list_node; t = TREE_CHAIN (t))
1350 if (TREE_PURPOSE (t))
1352 permerror (DECL_SOURCE_LOCATION (decl),
1353 "redeclaration of %q#D may not have default "
1354 "arguments", decl);
1355 return;
1359 #define GNU_INLINE_P(fn) (DECL_DECLARED_INLINE_P (fn) \
1360 && lookup_attribute ("gnu_inline", \
1361 DECL_ATTRIBUTES (fn)))
1363 /* If NEWDECL is a redeclaration of OLDDECL, merge the declarations.
1364 If the redeclaration is invalid, a diagnostic is issued, and the
1365 error_mark_node is returned. Otherwise, OLDDECL is returned.
1367 If NEWDECL is not a redeclaration of OLDDECL, NULL_TREE is
1368 returned.
1370 NEWDECL_IS_FRIEND is true if NEWDECL was declared as a friend. */
1372 tree
1373 duplicate_decls (tree newdecl, tree olddecl, bool newdecl_is_friend)
1375 unsigned olddecl_uid = DECL_UID (olddecl);
1376 int olddecl_friend = 0, types_match = 0, hidden_friend = 0;
1377 int new_defines_function = 0;
1378 tree new_template_info;
1380 if (newdecl == olddecl)
1381 return olddecl;
1383 types_match = decls_match (newdecl, olddecl);
1385 /* If either the type of the new decl or the type of the old decl is an
1386 error_mark_node, then that implies that we have already issued an
1387 error (earlier) for some bogus type specification, and in that case,
1388 it is rather pointless to harass the user with yet more error message
1389 about the same declaration, so just pretend the types match here. */
1390 if (TREE_TYPE (newdecl) == error_mark_node
1391 || TREE_TYPE (olddecl) == error_mark_node)
1392 return error_mark_node;
1394 if (UDLIT_OPER_P (DECL_NAME (newdecl))
1395 && UDLIT_OPER_P (DECL_NAME (olddecl)))
1397 if (TREE_CODE (newdecl) == TEMPLATE_DECL
1398 && TREE_CODE (olddecl) != TEMPLATE_DECL
1399 && check_raw_literal_operator (olddecl))
1400 error ("literal operator template %q+D conflicts with"
1401 " raw literal operator %qD", newdecl, olddecl);
1402 else if (TREE_CODE (newdecl) != TEMPLATE_DECL
1403 && TREE_CODE (olddecl) == TEMPLATE_DECL
1404 && check_raw_literal_operator (newdecl))
1405 error ("raw literal operator %q+D conflicts with"
1406 " literal operator template %qD", newdecl, olddecl);
1409 if (DECL_P (olddecl)
1410 && TREE_CODE (newdecl) == FUNCTION_DECL
1411 && TREE_CODE (olddecl) == FUNCTION_DECL
1412 && diagnose_mismatched_attributes (olddecl, newdecl))
1414 if (DECL_INITIAL (olddecl))
1415 inform (DECL_SOURCE_LOCATION (olddecl),
1416 "previous definition of %qD was here", olddecl);
1417 else
1418 inform (DECL_SOURCE_LOCATION (olddecl),
1419 "previous declaration of %qD was here", olddecl);
1422 /* Check for redeclaration and other discrepancies. */
1423 if (TREE_CODE (olddecl) == FUNCTION_DECL
1424 && DECL_ARTIFICIAL (olddecl))
1426 gcc_assert (!DECL_HIDDEN_FRIEND_P (olddecl));
1427 if (TREE_CODE (newdecl) != FUNCTION_DECL)
1429 /* Avoid warnings redeclaring built-ins which have not been
1430 explicitly declared. */
1431 if (DECL_ANTICIPATED (olddecl))
1432 return NULL_TREE;
1434 /* If you declare a built-in or predefined function name as static,
1435 the old definition is overridden, but optionally warn this was a
1436 bad choice of name. */
1437 if (! TREE_PUBLIC (newdecl))
1439 warning (OPT_Wshadow,
1440 DECL_BUILT_IN (olddecl)
1441 ? G_("shadowing built-in function %q#D")
1442 : G_("shadowing library function %q#D"), olddecl);
1443 /* Discard the old built-in function. */
1444 return NULL_TREE;
1446 /* If the built-in is not ansi, then programs can override
1447 it even globally without an error. */
1448 else if (! DECL_BUILT_IN (olddecl))
1449 warning_at (DECL_SOURCE_LOCATION (newdecl), 0,
1450 "library function %q#D redeclared as non-function %q#D",
1451 olddecl, newdecl);
1452 else
1453 error ("declaration of %q+#D conflicts with built-in "
1454 "declaration %q#D", newdecl, olddecl);
1455 return NULL_TREE;
1457 else if (DECL_OMP_DECLARE_REDUCTION_P (olddecl))
1459 gcc_assert (DECL_OMP_DECLARE_REDUCTION_P (newdecl));
1460 error_at (DECL_SOURCE_LOCATION (newdecl),
1461 "redeclaration of %<pragma omp declare reduction%>");
1462 inform (DECL_SOURCE_LOCATION (olddecl),
1463 "previous %<pragma omp declare reduction%> declaration");
1464 return error_mark_node;
1466 else if (!types_match)
1468 /* Avoid warnings redeclaring built-ins which have not been
1469 explicitly declared. */
1470 if (DECL_ANTICIPATED (olddecl))
1472 /* Deal with fileptr_type_node. FILE type is not known
1473 at the time we create the builtins. */
1474 tree t1, t2;
1476 for (t1 = TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1477 t2 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1478 t1 || t2;
1479 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1480 if (!t1 || !t2)
1481 break;
1482 else if (TREE_VALUE (t2) == fileptr_type_node)
1484 tree t = TREE_VALUE (t1);
1486 if (TYPE_PTR_P (t)
1487 && TYPE_IDENTIFIER (TREE_TYPE (t))
1488 == get_identifier ("FILE")
1489 && compparms (TREE_CHAIN (t1), TREE_CHAIN (t2)))
1491 tree oldargs = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1493 TYPE_ARG_TYPES (TREE_TYPE (olddecl))
1494 = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
1495 types_match = decls_match (newdecl, olddecl);
1496 if (types_match)
1497 return duplicate_decls (newdecl, olddecl,
1498 newdecl_is_friend);
1499 TYPE_ARG_TYPES (TREE_TYPE (olddecl)) = oldargs;
1502 else if (! same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1503 break;
1505 else if ((DECL_EXTERN_C_P (newdecl)
1506 && DECL_EXTERN_C_P (olddecl))
1507 || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1508 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
1510 /* A near match; override the builtin. */
1512 if (TREE_PUBLIC (newdecl))
1513 warning_at (DECL_SOURCE_LOCATION (newdecl), 0,
1514 "new declaration %q#D ambiguates built-in "
1515 "declaration %q#D", newdecl, olddecl);
1516 else
1517 warning (OPT_Wshadow,
1518 DECL_BUILT_IN (olddecl)
1519 ? G_("shadowing built-in function %q#D")
1520 : G_("shadowing library function %q#D"), olddecl);
1522 else
1523 /* Discard the old built-in function. */
1524 return NULL_TREE;
1526 /* Replace the old RTL to avoid problems with inlining. */
1527 COPY_DECL_RTL (newdecl, olddecl);
1529 /* Even if the types match, prefer the new declarations type for
1530 built-ins which have not been explicitly declared, for
1531 exception lists, etc... */
1532 else if (DECL_IS_BUILTIN (olddecl))
1534 tree type = TREE_TYPE (newdecl);
1535 tree attribs = (*targetm.merge_type_attributes)
1536 (TREE_TYPE (olddecl), type);
1538 type = cp_build_type_attribute_variant (type, attribs);
1539 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = type;
1542 /* If a function is explicitly declared "throw ()", propagate that to
1543 the corresponding builtin. */
1544 if (DECL_BUILT_IN_CLASS (olddecl) == BUILT_IN_NORMAL
1545 && DECL_ANTICIPATED (olddecl)
1546 && TREE_NOTHROW (newdecl)
1547 && !TREE_NOTHROW (olddecl))
1549 enum built_in_function fncode = DECL_FUNCTION_CODE (olddecl);
1550 tree tmpdecl = builtin_decl_explicit (fncode);
1551 if (tmpdecl && tmpdecl != olddecl && types_match)
1552 TREE_NOTHROW (tmpdecl) = 1;
1555 /* Whether or not the builtin can throw exceptions has no
1556 bearing on this declarator. */
1557 TREE_NOTHROW (olddecl) = 0;
1559 if (DECL_THIS_STATIC (newdecl) && !DECL_THIS_STATIC (olddecl))
1561 /* If a builtin function is redeclared as `static', merge
1562 the declarations, but make the original one static. */
1563 DECL_THIS_STATIC (olddecl) = 1;
1564 TREE_PUBLIC (olddecl) = 0;
1566 /* Make the old declaration consistent with the new one so
1567 that all remnants of the builtin-ness of this function
1568 will be banished. */
1569 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
1570 COPY_DECL_RTL (newdecl, olddecl);
1573 else if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1575 /* C++ Standard, 3.3, clause 4:
1576 "[Note: a namespace name or a class template name must be unique
1577 in its declarative region (7.3.2, clause 14). ]" */
1578 if (TREE_CODE (olddecl) != NAMESPACE_DECL
1579 && TREE_CODE (newdecl) != NAMESPACE_DECL
1580 && (TREE_CODE (olddecl) != TEMPLATE_DECL
1581 || TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) != TYPE_DECL)
1582 && (TREE_CODE (newdecl) != TEMPLATE_DECL
1583 || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) != TYPE_DECL))
1585 if ((TREE_CODE (olddecl) == TYPE_DECL && DECL_ARTIFICIAL (olddecl)
1586 && TREE_CODE (newdecl) != TYPE_DECL)
1587 || (TREE_CODE (newdecl) == TYPE_DECL && DECL_ARTIFICIAL (newdecl)
1588 && TREE_CODE (olddecl) != TYPE_DECL))
1590 /* We do nothing special here, because C++ does such nasty
1591 things with TYPE_DECLs. Instead, just let the TYPE_DECL
1592 get shadowed, and know that if we need to find a TYPE_DECL
1593 for a given name, we can look in the IDENTIFIER_TYPE_VALUE
1594 slot of the identifier. */
1595 return NULL_TREE;
1598 if ((TREE_CODE (newdecl) == FUNCTION_DECL
1599 && DECL_FUNCTION_TEMPLATE_P (olddecl))
1600 || (TREE_CODE (olddecl) == FUNCTION_DECL
1601 && DECL_FUNCTION_TEMPLATE_P (newdecl)))
1602 return NULL_TREE;
1605 error ("%q#D redeclared as different kind of symbol", newdecl);
1606 if (TREE_CODE (olddecl) == TREE_LIST)
1607 olddecl = TREE_VALUE (olddecl);
1608 inform (DECL_SOURCE_LOCATION (olddecl),
1609 "previous declaration %q#D", olddecl);
1611 return error_mark_node;
1613 else if (!types_match)
1615 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl))
1616 /* These are certainly not duplicate declarations; they're
1617 from different scopes. */
1618 return NULL_TREE;
1620 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1622 /* The name of a class template may not be declared to refer to
1623 any other template, class, function, object, namespace, value,
1624 or type in the same scope. */
1625 if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == TYPE_DECL
1626 || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
1628 error ("conflicting declaration of template %q+#D", newdecl);
1629 inform (DECL_SOURCE_LOCATION (olddecl),
1630 "previous declaration %q#D", olddecl);
1631 return error_mark_node;
1633 else if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == FUNCTION_DECL
1634 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL
1635 && compparms (TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl))),
1636 TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl))))
1637 && comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
1638 DECL_TEMPLATE_PARMS (olddecl))
1639 /* Template functions can be disambiguated by
1640 return type. */
1641 && same_type_p (TREE_TYPE (TREE_TYPE (newdecl)),
1642 TREE_TYPE (TREE_TYPE (olddecl)))
1643 // Template functions can also be disambiguated by
1644 // constraints.
1645 && equivalently_constrained (olddecl, newdecl))
1647 error ("ambiguating new declaration %q+#D", newdecl);
1648 inform (DECL_SOURCE_LOCATION (olddecl),
1649 "old declaration %q#D", olddecl);
1651 else if (check_concept_refinement (olddecl, newdecl))
1652 return error_mark_node;
1653 return NULL_TREE;
1655 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1657 if (DECL_EXTERN_C_P (newdecl) && DECL_EXTERN_C_P (olddecl))
1659 error ("conflicting declaration of C function %q+#D",
1660 newdecl);
1661 inform (DECL_SOURCE_LOCATION (olddecl),
1662 "previous declaration %q#D", olddecl);
1663 return NULL_TREE;
1665 /* For function versions, params and types match, but they
1666 are not ambiguous. */
1667 else if ((!DECL_FUNCTION_VERSIONED (newdecl)
1668 && !DECL_FUNCTION_VERSIONED (olddecl))
1669 // The functions have the same parameter types.
1670 && compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1671 TYPE_ARG_TYPES (TREE_TYPE (olddecl)))
1672 // And the same constraints.
1673 && equivalently_constrained (newdecl, olddecl))
1675 error ("ambiguating new declaration of %q+#D", newdecl);
1676 inform (DECL_SOURCE_LOCATION (olddecl),
1677 "old declaration %q#D", olddecl);
1678 return error_mark_node;
1680 else
1681 return NULL_TREE;
1683 else
1685 error ("conflicting declaration %q+#D", newdecl);
1686 inform (DECL_SOURCE_LOCATION (olddecl),
1687 "previous declaration as %q#D", olddecl);
1688 return error_mark_node;
1691 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1692 && ((DECL_TEMPLATE_SPECIALIZATION (olddecl)
1693 && (!DECL_TEMPLATE_INFO (newdecl)
1694 || (DECL_TI_TEMPLATE (newdecl)
1695 != DECL_TI_TEMPLATE (olddecl))))
1696 || (DECL_TEMPLATE_SPECIALIZATION (newdecl)
1697 && (!DECL_TEMPLATE_INFO (olddecl)
1698 || (DECL_TI_TEMPLATE (olddecl)
1699 != DECL_TI_TEMPLATE (newdecl))))))
1700 /* It's OK to have a template specialization and a non-template
1701 with the same type, or to have specializations of two
1702 different templates with the same type. Note that if one is a
1703 specialization, and the other is an instantiation of the same
1704 template, that we do not exit at this point. That situation
1705 can occur if we instantiate a template class, and then
1706 specialize one of its methods. This situation is valid, but
1707 the declarations must be merged in the usual way. */
1708 return NULL_TREE;
1709 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1710 && ((DECL_TEMPLATE_INSTANTIATION (olddecl)
1711 && !DECL_USE_TEMPLATE (newdecl))
1712 || (DECL_TEMPLATE_INSTANTIATION (newdecl)
1713 && !DECL_USE_TEMPLATE (olddecl))))
1714 /* One of the declarations is a template instantiation, and the
1715 other is not a template at all. That's OK. */
1716 return NULL_TREE;
1717 else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
1719 /* In [namespace.alias] we have:
1721 In a declarative region, a namespace-alias-definition can be
1722 used to redefine a namespace-alias declared in that declarative
1723 region to refer only to the namespace to which it already
1724 refers.
1726 Therefore, if we encounter a second alias directive for the same
1727 alias, we can just ignore the second directive. */
1728 if (DECL_NAMESPACE_ALIAS (newdecl)
1729 && (DECL_NAMESPACE_ALIAS (newdecl)
1730 == DECL_NAMESPACE_ALIAS (olddecl)))
1731 return olddecl;
1732 /* [namespace.alias]
1734 A namespace-name or namespace-alias shall not be declared as
1735 the name of any other entity in the same declarative region.
1736 A namespace-name defined at global scope shall not be
1737 declared as the name of any other entity in any global scope
1738 of the program. */
1739 error ("conflicting declaration of namespace %q+D", newdecl);
1740 inform (DECL_SOURCE_LOCATION (olddecl),
1741 "previous declaration of namespace %qD here", olddecl);
1742 return error_mark_node;
1744 else
1746 const char *errmsg = redeclaration_error_message (newdecl, olddecl);
1747 if (errmsg)
1749 error_at (DECL_SOURCE_LOCATION (newdecl), errmsg, newdecl);
1750 if (DECL_NAME (olddecl) != NULL_TREE)
1751 inform (DECL_SOURCE_LOCATION (olddecl),
1752 (DECL_INITIAL (olddecl) && namespace_bindings_p ())
1753 ? G_("%q#D previously defined here")
1754 : G_("%q#D previously declared here"), olddecl);
1755 return error_mark_node;
1757 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1758 && DECL_INITIAL (olddecl) != NULL_TREE
1759 && !prototype_p (TREE_TYPE (olddecl))
1760 && prototype_p (TREE_TYPE (newdecl)))
1762 /* Prototype decl follows defn w/o prototype. */
1763 if (warning_at (DECL_SOURCE_LOCATION (newdecl), 0,
1764 "prototype specified for %q#D", newdecl))
1765 inform (DECL_SOURCE_LOCATION (olddecl),
1766 "previous non-prototype definition here");
1768 else if (VAR_OR_FUNCTION_DECL_P (olddecl)
1769 && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl))
1771 /* [dcl.link]
1772 If two declarations of the same function or object
1773 specify different linkage-specifications ..., the program
1774 is ill-formed.... Except for functions with C++ linkage,
1775 a function declaration without a linkage specification
1776 shall not precede the first linkage specification for
1777 that function. A function can be declared without a
1778 linkage specification after an explicit linkage
1779 specification has been seen; the linkage explicitly
1780 specified in the earlier declaration is not affected by
1781 such a function declaration.
1783 DR 563 raises the question why the restrictions on
1784 functions should not also apply to objects. Older
1785 versions of G++ silently ignore the linkage-specification
1786 for this example:
1788 namespace N {
1789 extern int i;
1790 extern "C" int i;
1793 which is clearly wrong. Therefore, we now treat objects
1794 like functions. */
1795 if (current_lang_depth () == 0)
1797 /* There is no explicit linkage-specification, so we use
1798 the linkage from the previous declaration. */
1799 if (!DECL_LANG_SPECIFIC (newdecl))
1800 retrofit_lang_decl (newdecl);
1801 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
1803 else
1805 error ("conflicting declaration of %q+#D with %qL linkage",
1806 newdecl, DECL_LANGUAGE (newdecl));
1807 inform (DECL_SOURCE_LOCATION (olddecl),
1808 "previous declaration with %qL linkage",
1809 DECL_LANGUAGE (olddecl));
1813 if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl))
1815 else if (TREE_CODE (olddecl) == FUNCTION_DECL)
1817 /* Note: free functions, as TEMPLATE_DECLs, are handled below. */
1818 if (DECL_FUNCTION_MEMBER_P (olddecl)
1819 && (/* grokfndecl passes member function templates too
1820 as FUNCTION_DECLs. */
1821 DECL_TEMPLATE_INFO (olddecl)
1822 /* C++11 8.3.6/6.
1823 Default arguments for a member function of a class
1824 template shall be specified on the initial declaration
1825 of the member function within the class template. */
1826 || CLASSTYPE_TEMPLATE_INFO (CP_DECL_CONTEXT (olddecl))))
1827 check_redeclaration_no_default_args (newdecl);
1828 else
1830 tree t1 = FUNCTION_FIRST_USER_PARMTYPE (olddecl);
1831 tree t2 = FUNCTION_FIRST_USER_PARMTYPE (newdecl);
1832 int i = 1;
1834 for (; t1 && t1 != void_list_node;
1835 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++)
1836 if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
1838 if (1 == simple_cst_equal (TREE_PURPOSE (t1),
1839 TREE_PURPOSE (t2)))
1841 if (permerror (input_location,
1842 "default argument given for parameter "
1843 "%d of %q#D", i, newdecl))
1844 inform (DECL_SOURCE_LOCATION (olddecl),
1845 "previous specification in %q#D here",
1846 olddecl);
1848 else
1850 error ("default argument given for parameter %d "
1851 "of %q#D", i, newdecl);
1852 inform (DECL_SOURCE_LOCATION (olddecl),
1853 "previous specification in %q#D here",
1854 olddecl);
1861 /* Do not merge an implicit typedef with an explicit one. In:
1863 class A;
1865 typedef class A A __attribute__ ((foo));
1867 the attribute should apply only to the typedef. */
1868 if (TREE_CODE (olddecl) == TYPE_DECL
1869 && (DECL_IMPLICIT_TYPEDEF_P (olddecl)
1870 || DECL_IMPLICIT_TYPEDEF_P (newdecl)))
1871 return NULL_TREE;
1873 /* If new decl is `static' and an `extern' was seen previously,
1874 warn about it. */
1875 warn_extern_redeclared_static (newdecl, olddecl);
1877 if (!validate_constexpr_redeclaration (olddecl, newdecl))
1878 return error_mark_node;
1880 /* We have committed to returning 1 at this point. */
1881 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1883 /* Now that functions must hold information normally held
1884 by field decls, there is extra work to do so that
1885 declaration information does not get destroyed during
1886 definition. */
1887 if (DECL_VINDEX (olddecl))
1888 DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl);
1889 if (DECL_CONTEXT (olddecl))
1890 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1891 DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl);
1892 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1893 DECL_PURE_VIRTUAL_P (newdecl) |= DECL_PURE_VIRTUAL_P (olddecl);
1894 DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl);
1895 DECL_INVALID_OVERRIDER_P (newdecl) |= DECL_INVALID_OVERRIDER_P (olddecl);
1896 DECL_FINAL_P (newdecl) |= DECL_FINAL_P (olddecl);
1897 DECL_OVERRIDE_P (newdecl) |= DECL_OVERRIDE_P (olddecl);
1898 DECL_THIS_STATIC (newdecl) |= DECL_THIS_STATIC (olddecl);
1899 if (DECL_OVERLOADED_OPERATOR_P (olddecl) != ERROR_MARK)
1900 SET_OVERLOADED_OPERATOR_CODE
1901 (newdecl, DECL_OVERLOADED_OPERATOR_P (olddecl));
1902 new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE;
1904 /* Optionally warn about more than one declaration for the same
1905 name, but don't warn about a function declaration followed by a
1906 definition. */
1907 if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl)
1908 && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE)
1909 /* Don't warn about extern decl followed by definition. */
1910 && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl))
1911 /* Don't warn about friends, let add_friend take care of it. */
1912 && ! (newdecl_is_friend || DECL_FRIEND_P (olddecl))
1913 /* Don't warn about declaration followed by specialization. */
1914 && (! DECL_TEMPLATE_SPECIALIZATION (newdecl)
1915 || DECL_TEMPLATE_SPECIALIZATION (olddecl)))
1917 if (warning_at (DECL_SOURCE_LOCATION (newdecl),
1918 OPT_Wredundant_decls,
1919 "redundant redeclaration of %qD in same scope",
1920 newdecl))
1921 inform (DECL_SOURCE_LOCATION (olddecl),
1922 "previous declaration of %qD", olddecl);
1925 if (!(DECL_TEMPLATE_INSTANTIATION (olddecl)
1926 && DECL_TEMPLATE_SPECIALIZATION (newdecl)))
1928 if (DECL_DELETED_FN (newdecl))
1930 error ("deleted definition of %q+D", newdecl);
1931 inform (DECL_SOURCE_LOCATION (olddecl),
1932 "previous declaration of %qD", olddecl);
1934 DECL_DELETED_FN (newdecl) |= DECL_DELETED_FN (olddecl);
1938 /* Deal with C++: must preserve virtual function table size. */
1939 if (TREE_CODE (olddecl) == TYPE_DECL)
1941 tree newtype = TREE_TYPE (newdecl);
1942 tree oldtype = TREE_TYPE (olddecl);
1944 if (newtype != error_mark_node && oldtype != error_mark_node
1945 && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype))
1946 CLASSTYPE_FRIEND_CLASSES (newtype)
1947 = CLASSTYPE_FRIEND_CLASSES (oldtype);
1949 DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl);
1952 /* Copy all the DECL_... slots specified in the new decl
1953 except for any that we copy here from the old type. */
1954 DECL_ATTRIBUTES (newdecl)
1955 = (*targetm.merge_decl_attributes) (olddecl, newdecl);
1957 if (DECL_DECLARES_FUNCTION_P (olddecl) && DECL_DECLARES_FUNCTION_P (newdecl))
1959 olddecl_friend = DECL_FRIEND_P (olddecl);
1960 hidden_friend = (DECL_ANTICIPATED (olddecl)
1961 && DECL_HIDDEN_FRIEND_P (olddecl)
1962 && newdecl_is_friend);
1963 if (!hidden_friend)
1965 DECL_ANTICIPATED (olddecl) = 0;
1966 DECL_HIDDEN_FRIEND_P (olddecl) = 0;
1970 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1972 tree old_result;
1973 tree new_result;
1974 old_result = DECL_TEMPLATE_RESULT (olddecl);
1975 new_result = DECL_TEMPLATE_RESULT (newdecl);
1976 TREE_TYPE (olddecl) = TREE_TYPE (old_result);
1977 DECL_TEMPLATE_SPECIALIZATIONS (olddecl)
1978 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (olddecl),
1979 DECL_TEMPLATE_SPECIALIZATIONS (newdecl));
1981 DECL_ATTRIBUTES (old_result)
1982 = (*targetm.merge_decl_attributes) (old_result, new_result);
1984 if (DECL_FUNCTION_TEMPLATE_P (newdecl))
1986 /* Per C++11 8.3.6/4, default arguments cannot be added in later
1987 declarations of a function template. */
1988 if (DECL_SOURCE_LOCATION (newdecl)
1989 != DECL_SOURCE_LOCATION (olddecl))
1990 check_redeclaration_no_default_args (newdecl);
1992 check_default_args (newdecl);
1994 if (GNU_INLINE_P (old_result) != GNU_INLINE_P (new_result)
1995 && DECL_INITIAL (new_result))
1997 if (DECL_INITIAL (old_result))
1998 DECL_UNINLINABLE (old_result) = 1;
1999 else
2000 DECL_UNINLINABLE (old_result) = DECL_UNINLINABLE (new_result);
2001 DECL_EXTERNAL (old_result) = DECL_EXTERNAL (new_result);
2002 DECL_NOT_REALLY_EXTERN (old_result)
2003 = DECL_NOT_REALLY_EXTERN (new_result);
2004 DECL_INTERFACE_KNOWN (old_result)
2005 = DECL_INTERFACE_KNOWN (new_result);
2006 DECL_DECLARED_INLINE_P (old_result)
2007 = DECL_DECLARED_INLINE_P (new_result);
2008 DECL_DISREGARD_INLINE_LIMITS (old_result)
2009 |= DECL_DISREGARD_INLINE_LIMITS (new_result);
2012 else
2014 DECL_DECLARED_INLINE_P (old_result)
2015 |= DECL_DECLARED_INLINE_P (new_result);
2016 DECL_DISREGARD_INLINE_LIMITS (old_result)
2017 |= DECL_DISREGARD_INLINE_LIMITS (new_result);
2018 check_redeclaration_exception_specification (newdecl, olddecl);
2022 /* If the new declaration is a definition, update the file and
2023 line information on the declaration, and also make
2024 the old declaration the same definition. */
2025 if (DECL_INITIAL (new_result) != NULL_TREE)
2027 DECL_SOURCE_LOCATION (olddecl)
2028 = DECL_SOURCE_LOCATION (old_result)
2029 = DECL_SOURCE_LOCATION (newdecl);
2030 DECL_INITIAL (old_result) = DECL_INITIAL (new_result);
2031 if (DECL_FUNCTION_TEMPLATE_P (newdecl))
2033 tree parm;
2034 DECL_ARGUMENTS (old_result)
2035 = DECL_ARGUMENTS (new_result);
2036 for (parm = DECL_ARGUMENTS (old_result); parm;
2037 parm = DECL_CHAIN (parm))
2038 DECL_CONTEXT (parm) = old_result;
2042 return olddecl;
2045 if (types_match)
2047 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2048 check_redeclaration_exception_specification (newdecl, olddecl);
2050 /* Automatically handles default parameters. */
2051 tree oldtype = TREE_TYPE (olddecl);
2052 tree newtype;
2054 /* For typedefs use the old type, as the new type's DECL_NAME points
2055 at newdecl, which will be ggc_freed. */
2056 if (TREE_CODE (newdecl) == TYPE_DECL)
2058 /* But NEWTYPE might have an attribute, honor that. */
2059 tree tem = TREE_TYPE (newdecl);
2060 newtype = oldtype;
2062 if (TYPE_USER_ALIGN (tem))
2064 if (TYPE_ALIGN (tem) > TYPE_ALIGN (newtype))
2065 SET_TYPE_ALIGN (newtype, TYPE_ALIGN (tem));
2066 TYPE_USER_ALIGN (newtype) = true;
2069 /* And remove the new type from the variants list. */
2070 if (TYPE_NAME (TREE_TYPE (newdecl)) == newdecl)
2072 tree remove = TREE_TYPE (newdecl);
2073 for (tree t = TYPE_MAIN_VARIANT (remove); ;
2074 t = TYPE_NEXT_VARIANT (t))
2075 if (TYPE_NEXT_VARIANT (t) == remove)
2077 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (remove);
2078 break;
2082 else
2083 /* Merge the data types specified in the two decls. */
2084 newtype = merge_types (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
2086 if (VAR_P (newdecl))
2088 DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl);
2089 /* For already initialized vars, TREE_READONLY could have been
2090 cleared in cp_finish_decl, because the var needs runtime
2091 initialization or destruction. Make sure not to set
2092 TREE_READONLY on it again. */
2093 if (DECL_INITIALIZED_P (olddecl)
2094 && !DECL_EXTERNAL (olddecl)
2095 && !TREE_READONLY (olddecl))
2096 TREE_READONLY (newdecl) = 0;
2097 DECL_INITIALIZED_P (newdecl) |= DECL_INITIALIZED_P (olddecl);
2098 DECL_NONTRIVIALLY_INITIALIZED_P (newdecl)
2099 |= DECL_NONTRIVIALLY_INITIALIZED_P (olddecl);
2100 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (newdecl)
2101 |= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (olddecl);
2103 /* Merge the threadprivate attribute from OLDDECL into NEWDECL. */
2104 if (DECL_LANG_SPECIFIC (olddecl)
2105 && CP_DECL_THREADPRIVATE_P (olddecl))
2107 /* Allocate a LANG_SPECIFIC structure for NEWDECL, if needed. */
2108 if (!DECL_LANG_SPECIFIC (newdecl))
2109 retrofit_lang_decl (newdecl);
2111 CP_DECL_THREADPRIVATE_P (newdecl) = 1;
2115 /* An explicit specialization of a function template or of a member
2116 function of a class template can be declared transaction_safe
2117 independently of whether the corresponding template entity is declared
2118 transaction_safe. */
2119 if (flag_tm && TREE_CODE (newdecl) == FUNCTION_DECL
2120 && DECL_TEMPLATE_INSTANTIATION (olddecl)
2121 && DECL_TEMPLATE_SPECIALIZATION (newdecl)
2122 && tx_safe_fn_type_p (newtype)
2123 && !tx_safe_fn_type_p (TREE_TYPE (newdecl)))
2124 newtype = tx_unsafe_fn_variant (newtype);
2126 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype;
2128 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2129 check_default_args (newdecl);
2131 /* Lay the type out, unless already done. */
2132 if (! same_type_p (newtype, oldtype)
2133 && TREE_TYPE (newdecl) != error_mark_node
2134 && !(processing_template_decl && uses_template_parms (newdecl)))
2135 layout_type (TREE_TYPE (newdecl));
2137 if ((VAR_P (newdecl)
2138 || TREE_CODE (newdecl) == PARM_DECL
2139 || TREE_CODE (newdecl) == RESULT_DECL
2140 || TREE_CODE (newdecl) == FIELD_DECL
2141 || TREE_CODE (newdecl) == TYPE_DECL)
2142 && !(processing_template_decl && uses_template_parms (newdecl)))
2143 layout_decl (newdecl, 0);
2145 /* Merge the type qualifiers. */
2146 if (TREE_READONLY (newdecl))
2147 TREE_READONLY (olddecl) = 1;
2148 if (TREE_THIS_VOLATILE (newdecl))
2149 TREE_THIS_VOLATILE (olddecl) = 1;
2150 if (TREE_NOTHROW (newdecl))
2151 TREE_NOTHROW (olddecl) = 1;
2153 /* Merge deprecatedness. */
2154 if (TREE_DEPRECATED (newdecl))
2155 TREE_DEPRECATED (olddecl) = 1;
2157 /* Preserve function specific target and optimization options */
2158 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2160 if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
2161 && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
2162 DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
2163 = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
2165 if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
2166 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
2167 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
2168 = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
2171 /* Merge the initialization information. */
2172 if (DECL_INITIAL (newdecl) == NULL_TREE
2173 && DECL_INITIAL (olddecl) != NULL_TREE)
2175 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2176 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2177 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2179 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
2180 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
2184 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2186 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
2187 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
2188 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
2189 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
2190 TREE_NOTHROW (newdecl) |= TREE_NOTHROW (olddecl);
2191 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
2192 DECL_IS_OPERATOR_NEW (newdecl) |= DECL_IS_OPERATOR_NEW (olddecl);
2193 DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
2194 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
2195 DECL_LOOPING_CONST_OR_PURE_P (newdecl)
2196 |= DECL_LOOPING_CONST_OR_PURE_P (olddecl);
2197 /* Keep the old RTL. */
2198 COPY_DECL_RTL (olddecl, newdecl);
2200 else if (VAR_P (newdecl)
2201 && (DECL_SIZE (olddecl) || !DECL_SIZE (newdecl)))
2203 /* Keep the old RTL. We cannot keep the old RTL if the old
2204 declaration was for an incomplete object and the new
2205 declaration is not since many attributes of the RTL will
2206 change. */
2207 COPY_DECL_RTL (olddecl, newdecl);
2210 /* If cannot merge, then use the new type and qualifiers,
2211 and don't preserve the old rtl. */
2212 else
2214 /* Clean out any memory we had of the old declaration. */
2215 tree oldstatic = value_member (olddecl, static_aggregates);
2216 if (oldstatic)
2217 TREE_VALUE (oldstatic) = error_mark_node;
2219 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
2220 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
2221 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
2222 TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
2225 /* Merge the storage class information. */
2226 merge_weak (newdecl, olddecl);
2228 DECL_DEFER_OUTPUT (newdecl) |= DECL_DEFER_OUTPUT (olddecl);
2229 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
2230 TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl);
2231 if (! DECL_EXTERNAL (olddecl))
2232 DECL_EXTERNAL (newdecl) = 0;
2233 if (! DECL_COMDAT (olddecl))
2234 DECL_COMDAT (newdecl) = 0;
2236 new_template_info = NULL_TREE;
2237 if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl))
2239 bool new_redefines_gnu_inline = false;
2241 if (new_defines_function
2242 && ((DECL_INTERFACE_KNOWN (olddecl)
2243 && TREE_CODE (olddecl) == FUNCTION_DECL)
2244 || (TREE_CODE (olddecl) == TEMPLATE_DECL
2245 && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl))
2246 == FUNCTION_DECL))))
2248 tree fn = olddecl;
2250 if (TREE_CODE (fn) == TEMPLATE_DECL)
2251 fn = DECL_TEMPLATE_RESULT (olddecl);
2253 new_redefines_gnu_inline = GNU_INLINE_P (fn) && DECL_INITIAL (fn);
2256 if (!new_redefines_gnu_inline)
2258 DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl);
2259 DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl);
2260 DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl);
2262 DECL_TEMPLATE_INSTANTIATED (newdecl)
2263 |= DECL_TEMPLATE_INSTANTIATED (olddecl);
2264 DECL_ODR_USED (newdecl) |= DECL_ODR_USED (olddecl);
2266 /* If the OLDDECL is an instantiation and/or specialization,
2267 then the NEWDECL must be too. But, it may not yet be marked
2268 as such if the caller has created NEWDECL, but has not yet
2269 figured out that it is a redeclaration. */
2270 if (!DECL_USE_TEMPLATE (newdecl))
2271 DECL_USE_TEMPLATE (newdecl) = DECL_USE_TEMPLATE (olddecl);
2273 /* Don't really know how much of the language-specific
2274 values we should copy from old to new. */
2275 DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl);
2276 DECL_REPO_AVAILABLE_P (newdecl) = DECL_REPO_AVAILABLE_P (olddecl);
2277 DECL_INITIALIZED_IN_CLASS_P (newdecl)
2278 |= DECL_INITIALIZED_IN_CLASS_P (olddecl);
2280 if (LANG_DECL_HAS_MIN (newdecl))
2282 DECL_LANG_SPECIFIC (newdecl)->u.min.u2 =
2283 DECL_LANG_SPECIFIC (olddecl)->u.min.u2;
2284 if (DECL_TEMPLATE_INFO (newdecl))
2286 new_template_info = DECL_TEMPLATE_INFO (newdecl);
2287 if (DECL_TEMPLATE_INSTANTIATION (olddecl)
2288 && DECL_TEMPLATE_SPECIALIZATION (newdecl))
2289 /* Remember the presence of explicit specialization args. */
2290 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (olddecl))
2291 = TINFO_USED_TEMPLATE_ID (new_template_info);
2293 DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl);
2295 /* Only functions have these fields. */
2296 if (DECL_DECLARES_FUNCTION_P (newdecl))
2298 DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl);
2299 DECL_BEFRIENDING_CLASSES (newdecl)
2300 = chainon (DECL_BEFRIENDING_CLASSES (newdecl),
2301 DECL_BEFRIENDING_CLASSES (olddecl));
2302 /* DECL_THUNKS is only valid for virtual functions,
2303 otherwise it is a DECL_FRIEND_CONTEXT. */
2304 if (DECL_VIRTUAL_P (newdecl))
2305 SET_DECL_THUNKS (newdecl, DECL_THUNKS (olddecl));
2307 /* Only variables have this field. */
2308 else if (VAR_P (newdecl)
2309 && VAR_HAD_UNKNOWN_BOUND (olddecl))
2310 SET_VAR_HAD_UNKNOWN_BOUND (newdecl);
2313 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2315 tree parm;
2317 /* Merge parameter attributes. */
2318 tree oldarg, newarg;
2319 for (oldarg = DECL_ARGUMENTS(olddecl),
2320 newarg = DECL_ARGUMENTS(newdecl);
2321 oldarg && newarg;
2322 oldarg = DECL_CHAIN(oldarg), newarg = DECL_CHAIN(newarg)) {
2323 DECL_ATTRIBUTES (newarg)
2324 = (*targetm.merge_decl_attributes) (oldarg, newarg);
2325 DECL_ATTRIBUTES (oldarg) = DECL_ATTRIBUTES (newarg);
2328 if (DECL_TEMPLATE_INSTANTIATION (olddecl)
2329 && !DECL_TEMPLATE_INSTANTIATION (newdecl))
2331 /* If newdecl is not a specialization, then it is not a
2332 template-related function at all. And that means that we
2333 should have exited above, returning 0. */
2334 gcc_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl));
2336 if (DECL_ODR_USED (olddecl))
2337 /* From [temp.expl.spec]:
2339 If a template, a member template or the member of a class
2340 template is explicitly specialized then that
2341 specialization shall be declared before the first use of
2342 that specialization that would cause an implicit
2343 instantiation to take place, in every translation unit in
2344 which such a use occurs. */
2345 error ("explicit specialization of %qD after first use",
2346 olddecl);
2348 SET_DECL_TEMPLATE_SPECIALIZATION (olddecl);
2349 DECL_COMDAT (newdecl) = (TREE_PUBLIC (newdecl)
2350 && DECL_DECLARED_INLINE_P (newdecl));
2352 /* Don't propagate visibility from the template to the
2353 specialization here. We'll do that in determine_visibility if
2354 appropriate. */
2355 DECL_VISIBILITY_SPECIFIED (olddecl) = 0;
2357 /* [temp.expl.spec/14] We don't inline explicit specialization
2358 just because the primary template says so. */
2360 /* But still keep DECL_DISREGARD_INLINE_LIMITS in sync with
2361 the always_inline attribute. */
2362 if (DECL_DISREGARD_INLINE_LIMITS (olddecl)
2363 && !DECL_DISREGARD_INLINE_LIMITS (newdecl))
2365 if (DECL_DECLARED_INLINE_P (newdecl))
2366 DECL_DISREGARD_INLINE_LIMITS (newdecl) = true;
2367 else
2368 DECL_ATTRIBUTES (newdecl)
2369 = remove_attribute ("always_inline",
2370 DECL_ATTRIBUTES (newdecl));
2373 else if (new_defines_function && DECL_INITIAL (olddecl))
2375 /* Never inline re-defined extern inline functions.
2376 FIXME: this could be better handled by keeping both
2377 function as separate declarations. */
2378 DECL_UNINLINABLE (newdecl) = 1;
2380 else
2382 if (DECL_PENDING_INLINE_P (olddecl))
2384 DECL_PENDING_INLINE_P (newdecl) = 1;
2385 DECL_PENDING_INLINE_INFO (newdecl)
2386 = DECL_PENDING_INLINE_INFO (olddecl);
2388 else if (DECL_PENDING_INLINE_P (newdecl))
2390 else if (DECL_SAVED_FUNCTION_DATA (newdecl) == NULL)
2391 DECL_SAVED_FUNCTION_DATA (newdecl)
2392 = DECL_SAVED_FUNCTION_DATA (olddecl);
2394 DECL_DECLARED_INLINE_P (newdecl) |= DECL_DECLARED_INLINE_P (olddecl);
2396 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
2397 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
2399 DECL_DISREGARD_INLINE_LIMITS (newdecl)
2400 = DECL_DISREGARD_INLINE_LIMITS (olddecl)
2401 = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
2402 || DECL_DISREGARD_INLINE_LIMITS (olddecl));
2405 /* Preserve abstractness on cloned [cd]tors. */
2406 DECL_ABSTRACT_P (newdecl) = DECL_ABSTRACT_P (olddecl);
2408 /* Update newdecl's parms to point at olddecl. */
2409 for (parm = DECL_ARGUMENTS (newdecl); parm;
2410 parm = DECL_CHAIN (parm))
2411 DECL_CONTEXT (parm) = olddecl;
2413 if (! types_match)
2415 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
2416 COPY_DECL_ASSEMBLER_NAME (newdecl, olddecl);
2417 COPY_DECL_RTL (newdecl, olddecl);
2419 if (! types_match || new_defines_function)
2421 /* These need to be copied so that the names are available.
2422 Note that if the types do match, we'll preserve inline
2423 info and other bits, but if not, we won't. */
2424 DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl);
2425 DECL_RESULT (olddecl) = DECL_RESULT (newdecl);
2427 /* If redeclaring a builtin function, it stays built in
2428 if newdecl is a gnu_inline definition, or if newdecl is just
2429 a declaration. */
2430 if (DECL_BUILT_IN (olddecl)
2431 && (new_defines_function ? GNU_INLINE_P (newdecl) : types_match))
2433 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
2434 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
2435 /* If we're keeping the built-in definition, keep the rtl,
2436 regardless of declaration matches. */
2437 COPY_DECL_RTL (olddecl, newdecl);
2438 if (DECL_BUILT_IN_CLASS (newdecl) == BUILT_IN_NORMAL)
2440 enum built_in_function fncode = DECL_FUNCTION_CODE (newdecl);
2441 switch (fncode)
2443 /* If a compatible prototype of these builtin functions
2444 is seen, assume the runtime implements it with the
2445 expected semantics. */
2446 case BUILT_IN_STPCPY:
2447 if (builtin_decl_explicit_p (fncode))
2448 set_builtin_decl_implicit_p (fncode, true);
2449 break;
2450 default:
2451 if (builtin_decl_explicit_p (fncode))
2452 set_builtin_decl_declared_p (fncode, true);
2453 break;
2457 if (new_defines_function)
2458 /* If defining a function declared with other language
2459 linkage, use the previously declared language linkage. */
2460 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
2461 else if (types_match)
2463 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
2464 /* Don't clear out the arguments if we're just redeclaring a
2465 function. */
2466 if (DECL_ARGUMENTS (olddecl))
2467 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
2470 else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
2471 NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl);
2473 /* Now preserve various other info from the definition. */
2474 TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl);
2475 TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl);
2476 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
2477 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
2479 /* Warn about conflicting visibility specifications. */
2480 if (DECL_VISIBILITY_SPECIFIED (olddecl)
2481 && DECL_VISIBILITY_SPECIFIED (newdecl)
2482 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
2484 if (warning_at (DECL_SOURCE_LOCATION (newdecl), OPT_Wattributes,
2485 "%qD: visibility attribute ignored because it "
2486 "conflicts with previous declaration", newdecl))
2487 inform (DECL_SOURCE_LOCATION (olddecl),
2488 "previous declaration of %qD", olddecl);
2490 /* Choose the declaration which specified visibility. */
2491 if (DECL_VISIBILITY_SPECIFIED (olddecl))
2493 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
2494 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
2496 /* Init priority used to be merged from newdecl to olddecl by the memcpy,
2497 so keep this behavior. */
2498 if (VAR_P (newdecl) && DECL_HAS_INIT_PRIORITY_P (newdecl))
2500 SET_DECL_INIT_PRIORITY (olddecl, DECL_INIT_PRIORITY (newdecl));
2501 DECL_HAS_INIT_PRIORITY_P (olddecl) = 1;
2503 /* Likewise for DECL_ALIGN, DECL_USER_ALIGN and DECL_PACKED. */
2504 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
2506 SET_DECL_ALIGN (newdecl, DECL_ALIGN (olddecl));
2507 DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
2509 DECL_USER_ALIGN (olddecl) = DECL_USER_ALIGN (newdecl);
2510 if (TREE_CODE (newdecl) == FIELD_DECL)
2511 DECL_PACKED (olddecl) = DECL_PACKED (newdecl);
2513 /* The DECL_LANG_SPECIFIC information in OLDDECL will be replaced
2514 with that from NEWDECL below. */
2515 if (DECL_LANG_SPECIFIC (olddecl))
2517 gcc_assert (DECL_LANG_SPECIFIC (olddecl)
2518 != DECL_LANG_SPECIFIC (newdecl));
2519 ggc_free (DECL_LANG_SPECIFIC (olddecl));
2522 /* Merge the USED information. */
2523 if (TREE_USED (olddecl))
2524 TREE_USED (newdecl) = 1;
2525 else if (TREE_USED (newdecl))
2526 TREE_USED (olddecl) = 1;
2527 if (VAR_P (newdecl))
2529 if (DECL_READ_P (olddecl))
2530 DECL_READ_P (newdecl) = 1;
2531 else if (DECL_READ_P (newdecl))
2532 DECL_READ_P (olddecl) = 1;
2534 if (DECL_PRESERVE_P (olddecl))
2535 DECL_PRESERVE_P (newdecl) = 1;
2536 else if (DECL_PRESERVE_P (newdecl))
2537 DECL_PRESERVE_P (olddecl) = 1;
2539 /* Merge the DECL_FUNCTION_VERSIONED information. newdecl will be copied
2540 to olddecl and deleted. */
2541 if (TREE_CODE (newdecl) == FUNCTION_DECL
2542 && DECL_FUNCTION_VERSIONED (olddecl))
2544 /* Set the flag for newdecl so that it gets copied to olddecl. */
2545 DECL_FUNCTION_VERSIONED (newdecl) = 1;
2546 /* newdecl will be purged after copying to olddecl and is no longer
2547 a version. */
2548 cgraph_node::delete_function_version (newdecl);
2551 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2553 int function_size;
2554 struct symtab_node *snode = symtab_node::get (olddecl);
2556 function_size = sizeof (struct tree_decl_common);
2558 memcpy ((char *) olddecl + sizeof (struct tree_common),
2559 (char *) newdecl + sizeof (struct tree_common),
2560 function_size - sizeof (struct tree_common));
2562 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2563 (char *) newdecl + sizeof (struct tree_decl_common),
2564 sizeof (struct tree_function_decl) - sizeof (struct tree_decl_common));
2566 /* Preserve symtab node mapping. */
2567 olddecl->decl_with_vis.symtab_node = snode;
2569 if (new_template_info)
2570 /* If newdecl is a template instantiation, it is possible that
2571 the following sequence of events has occurred:
2573 o A friend function was declared in a class template. The
2574 class template was instantiated.
2576 o The instantiation of the friend declaration was
2577 recorded on the instantiation list, and is newdecl.
2579 o Later, however, instantiate_class_template called pushdecl
2580 on the newdecl to perform name injection. But, pushdecl in
2581 turn called duplicate_decls when it discovered that another
2582 declaration of a global function with the same name already
2583 existed.
2585 o Here, in duplicate_decls, we decided to clobber newdecl.
2587 If we're going to do that, we'd better make sure that
2588 olddecl, and not newdecl, is on the list of
2589 instantiations so that if we try to do the instantiation
2590 again we won't get the clobbered declaration. */
2591 reregister_specialization (newdecl,
2592 new_template_info,
2593 olddecl);
2595 else
2597 size_t size = tree_code_size (TREE_CODE (newdecl));
2599 memcpy ((char *) olddecl + sizeof (struct tree_common),
2600 (char *) newdecl + sizeof (struct tree_common),
2601 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
2602 switch (TREE_CODE (newdecl))
2604 case LABEL_DECL:
2605 case VAR_DECL:
2606 case RESULT_DECL:
2607 case PARM_DECL:
2608 case FIELD_DECL:
2609 case TYPE_DECL:
2610 case CONST_DECL:
2612 struct symtab_node *snode = NULL;
2614 if (VAR_P (newdecl)
2615 && (TREE_STATIC (olddecl) || TREE_PUBLIC (olddecl)
2616 || DECL_EXTERNAL (olddecl)))
2617 snode = symtab_node::get (olddecl);
2618 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2619 (char *) newdecl + sizeof (struct tree_decl_common),
2620 size - sizeof (struct tree_decl_common)
2621 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
2622 if (VAR_P (newdecl))
2623 olddecl->decl_with_vis.symtab_node = snode;
2625 break;
2626 default:
2627 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2628 (char *) newdecl + sizeof (struct tree_decl_common),
2629 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common)
2630 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
2631 break;
2635 if (VAR_OR_FUNCTION_DECL_P (newdecl))
2637 if (DECL_EXTERNAL (olddecl)
2638 || TREE_PUBLIC (olddecl)
2639 || TREE_STATIC (olddecl))
2641 /* Merge the section attribute.
2642 We want to issue an error if the sections conflict but that must be
2643 done later in decl_attributes since we are called before attributes
2644 are assigned. */
2645 if (DECL_SECTION_NAME (newdecl) != NULL)
2646 set_decl_section_name (olddecl, DECL_SECTION_NAME (newdecl));
2648 if (DECL_ONE_ONLY (newdecl))
2650 struct symtab_node *oldsym, *newsym;
2651 if (TREE_CODE (olddecl) == FUNCTION_DECL)
2652 oldsym = cgraph_node::get_create (olddecl);
2653 else
2654 oldsym = varpool_node::get_create (olddecl);
2655 newsym = symtab_node::get (newdecl);
2656 oldsym->set_comdat_group (newsym->get_comdat_group ());
2660 if (VAR_P (newdecl)
2661 && CP_DECL_THREAD_LOCAL_P (newdecl))
2663 CP_DECL_THREAD_LOCAL_P (olddecl) = true;
2664 if (!processing_template_decl)
2665 set_decl_tls_model (olddecl, DECL_TLS_MODEL (newdecl));
2669 DECL_UID (olddecl) = olddecl_uid;
2670 if (olddecl_friend)
2671 DECL_FRIEND_P (olddecl) = 1;
2672 if (hidden_friend)
2674 DECL_ANTICIPATED (olddecl) = 1;
2675 DECL_HIDDEN_FRIEND_P (olddecl) = 1;
2678 /* NEWDECL contains the merged attribute lists.
2679 Update OLDDECL to be the same. */
2680 DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
2682 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
2683 so that encode_section_info has a chance to look at the new decl
2684 flags and attributes. */
2685 if (DECL_RTL_SET_P (olddecl)
2686 && (TREE_CODE (olddecl) == FUNCTION_DECL
2687 || (VAR_P (olddecl)
2688 && TREE_STATIC (olddecl))))
2689 make_decl_rtl (olddecl);
2691 /* The NEWDECL will no longer be needed. Because every out-of-class
2692 declaration of a member results in a call to duplicate_decls,
2693 freeing these nodes represents in a significant savings.
2695 Before releasing the node, be sore to remove function from symbol
2696 table that might have been inserted there to record comdat group.
2697 Be sure to however do not free DECL_STRUCT_FUNCTION because this
2698 structure is shared in between newdecl and oldecl. */
2699 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2700 DECL_STRUCT_FUNCTION (newdecl) = NULL;
2701 if (VAR_OR_FUNCTION_DECL_P (newdecl))
2703 struct symtab_node *snode = symtab_node::get (newdecl);
2704 if (snode)
2705 snode->remove ();
2708 /* Remove the associated constraints for newdecl, if any, before
2709 reclaiming memory. */
2710 if (flag_concepts)
2711 remove_constraints (newdecl);
2713 ggc_free (newdecl);
2715 return olddecl;
2718 /* Return zero if the declaration NEWDECL is valid
2719 when the declaration OLDDECL (assumed to be for the same name)
2720 has already been seen.
2721 Otherwise return an error message format string with a %s
2722 where the identifier should go. */
2724 static const char *
2725 redeclaration_error_message (tree newdecl, tree olddecl)
2727 if (TREE_CODE (newdecl) == TYPE_DECL)
2729 /* Because C++ can put things into name space for free,
2730 constructs like "typedef struct foo { ... } foo"
2731 would look like an erroneous redeclaration. */
2732 if (same_type_p (TREE_TYPE (newdecl), TREE_TYPE (olddecl)))
2733 return NULL;
2734 else
2735 return G_("redefinition of %q#D");
2737 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
2739 /* If this is a pure function, its olddecl will actually be
2740 the original initialization to `0' (which we force to call
2741 abort()). Don't complain about redefinition in this case. */
2742 if (DECL_LANG_SPECIFIC (olddecl) && DECL_PURE_VIRTUAL_P (olddecl)
2743 && DECL_INITIAL (olddecl) == NULL_TREE)
2744 return NULL;
2746 /* If both functions come from different namespaces, this is not
2747 a redeclaration - this is a conflict with a used function. */
2748 if (DECL_NAMESPACE_SCOPE_P (olddecl)
2749 && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl)
2750 && ! decls_match (olddecl, newdecl))
2751 return G_("%qD conflicts with used function");
2753 /* We'll complain about linkage mismatches in
2754 warn_extern_redeclared_static. */
2756 /* Defining the same name twice is no good. */
2757 if (DECL_INITIAL (olddecl) != NULL_TREE
2758 && DECL_INITIAL (newdecl) != NULL_TREE)
2760 if (DECL_NAME (olddecl) == NULL_TREE)
2761 return G_("%q#D not declared in class");
2762 else if (!GNU_INLINE_P (olddecl)
2763 || GNU_INLINE_P (newdecl))
2764 return G_("redefinition of %q#D");
2767 if (DECL_DECLARED_INLINE_P (olddecl) && DECL_DECLARED_INLINE_P (newdecl))
2769 bool olda = GNU_INLINE_P (olddecl);
2770 bool newa = GNU_INLINE_P (newdecl);
2772 if (olda != newa)
2774 if (newa)
2775 return G_("%q+D redeclared inline with "
2776 "%<gnu_inline%> attribute");
2777 else
2778 return G_("%q+D redeclared inline without "
2779 "%<gnu_inline%> attribute");
2783 check_abi_tag_redeclaration
2784 (olddecl, lookup_attribute ("abi_tag", DECL_ATTRIBUTES (olddecl)),
2785 lookup_attribute ("abi_tag", DECL_ATTRIBUTES (newdecl)));
2787 return NULL;
2789 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2791 tree nt, ot;
2793 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
2795 if (COMPLETE_TYPE_P (TREE_TYPE (newdecl))
2796 && COMPLETE_TYPE_P (TREE_TYPE (olddecl)))
2797 return G_("redefinition of %q#D");
2798 return NULL;
2801 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) != FUNCTION_DECL
2802 || (DECL_TEMPLATE_RESULT (newdecl)
2803 == DECL_TEMPLATE_RESULT (olddecl)))
2804 return NULL;
2806 nt = DECL_TEMPLATE_RESULT (newdecl);
2807 if (DECL_TEMPLATE_INFO (nt))
2808 nt = DECL_TEMPLATE_RESULT (template_for_substitution (nt));
2809 ot = DECL_TEMPLATE_RESULT (olddecl);
2810 if (DECL_TEMPLATE_INFO (ot))
2811 ot = DECL_TEMPLATE_RESULT (template_for_substitution (ot));
2812 if (DECL_INITIAL (nt) && DECL_INITIAL (ot)
2813 && (!GNU_INLINE_P (ot) || GNU_INLINE_P (nt)))
2814 return G_("redefinition of %q#D");
2816 if (DECL_DECLARED_INLINE_P (ot) && DECL_DECLARED_INLINE_P (nt))
2818 bool olda = GNU_INLINE_P (ot);
2819 bool newa = GNU_INLINE_P (nt);
2821 if (olda != newa)
2823 if (newa)
2824 return G_("%q+D redeclared inline with "
2825 "%<gnu_inline%> attribute");
2826 else
2827 return G_("%q+D redeclared inline without "
2828 "%<gnu_inline%> attribute");
2832 /* Core issue #226 (C++0x):
2834 If a friend function template declaration specifies a
2835 default template-argument, that declaration shall be a
2836 definition and shall be the only declaration of the
2837 function template in the translation unit. */
2838 if ((cxx_dialect != cxx98)
2839 && TREE_CODE (ot) == FUNCTION_DECL && DECL_FRIEND_P (ot)
2840 && !check_default_tmpl_args (nt, DECL_TEMPLATE_PARMS (newdecl),
2841 /*is_primary=*/true,
2842 /*is_partial=*/false,
2843 /*is_friend_decl=*/2))
2844 return G_("redeclaration of friend %q#D "
2845 "may not have default template arguments");
2847 return NULL;
2849 else if (VAR_P (newdecl)
2850 && CP_DECL_THREAD_LOCAL_P (newdecl) != CP_DECL_THREAD_LOCAL_P (olddecl)
2851 && (! DECL_LANG_SPECIFIC (olddecl)
2852 || ! CP_DECL_THREADPRIVATE_P (olddecl)
2853 || CP_DECL_THREAD_LOCAL_P (newdecl)))
2855 /* Only variables can be thread-local, and all declarations must
2856 agree on this property. */
2857 if (CP_DECL_THREAD_LOCAL_P (newdecl))
2858 return G_("thread-local declaration of %q#D follows "
2859 "non-thread-local declaration");
2860 else
2861 return G_("non-thread-local declaration of %q#D follows "
2862 "thread-local declaration");
2864 else if (toplevel_bindings_p () || DECL_NAMESPACE_SCOPE_P (newdecl))
2866 /* The objects have been declared at namespace scope. If either
2867 is a member of an anonymous union, then this is an invalid
2868 redeclaration. For example:
2870 int i;
2871 union { int i; };
2873 is invalid. */
2874 if ((VAR_P (newdecl) && DECL_ANON_UNION_VAR_P (newdecl))
2875 || (VAR_P (olddecl) && DECL_ANON_UNION_VAR_P (olddecl)))
2876 return G_("redeclaration of %q#D");
2877 /* If at least one declaration is a reference, there is no
2878 conflict. For example:
2880 int i = 3;
2881 extern int i;
2883 is valid. */
2884 if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
2885 return NULL;
2886 /* Reject two definitions. */
2887 return G_("redefinition of %q#D");
2889 else
2891 /* Objects declared with block scope: */
2892 /* Reject two definitions, and reject a definition
2893 together with an external reference. */
2894 if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl)))
2895 return G_("redeclaration of %q#D");
2896 return NULL;
2900 /* Hash and equality functions for the named_label table. */
2902 hashval_t
2903 named_label_hasher::hash (named_label_entry *ent)
2905 return DECL_UID (ent->label_decl);
2908 bool
2909 named_label_hasher::equal (named_label_entry *a, named_label_entry *b)
2911 return a->label_decl == b->label_decl;
2914 /* Create a new label, named ID. */
2916 static tree
2917 make_label_decl (tree id, int local_p)
2919 struct named_label_entry *ent;
2920 tree decl;
2922 decl = build_decl (input_location, LABEL_DECL, id, void_type_node);
2924 DECL_CONTEXT (decl) = current_function_decl;
2925 DECL_MODE (decl) = VOIDmode;
2926 C_DECLARED_LABEL_FLAG (decl) = local_p;
2928 /* Say where one reference is to the label, for the sake of the
2929 error if it is not defined. */
2930 DECL_SOURCE_LOCATION (decl) = input_location;
2932 /* Record the fact that this identifier is bound to this label. */
2933 SET_IDENTIFIER_LABEL_VALUE (id, decl);
2935 /* Create the label htab for the function on demand. */
2936 if (!named_labels)
2937 named_labels = hash_table<named_label_hasher>::create_ggc (13);
2939 /* Record this label on the list of labels used in this function.
2940 We do this before calling make_label_decl so that we get the
2941 IDENTIFIER_LABEL_VALUE before the new label is declared. */
2942 ent = ggc_cleared_alloc<named_label_entry> ();
2943 ent->label_decl = decl;
2945 named_label_entry **slot = named_labels->find_slot (ent, INSERT);
2946 gcc_assert (*slot == NULL);
2947 *slot = ent;
2949 return decl;
2952 /* Look for a label named ID in the current function. If one cannot
2953 be found, create one. (We keep track of used, but undefined,
2954 labels, and complain about them at the end of a function.) */
2956 static tree
2957 lookup_label_1 (tree id)
2959 tree decl;
2961 /* You can't use labels at global scope. */
2962 if (current_function_decl == NULL_TREE)
2964 error ("label %qE referenced outside of any function", id);
2965 return NULL_TREE;
2968 /* See if we've already got this label. */
2969 decl = IDENTIFIER_LABEL_VALUE (id);
2970 if (decl != NULL_TREE && DECL_CONTEXT (decl) == current_function_decl)
2971 return decl;
2973 decl = make_label_decl (id, /*local_p=*/0);
2974 return decl;
2977 /* Wrapper for lookup_label_1. */
2979 tree
2980 lookup_label (tree id)
2982 tree ret;
2983 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
2984 ret = lookup_label_1 (id);
2985 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
2986 return ret;
2989 /* Declare a local label named ID. */
2991 tree
2992 declare_local_label (tree id)
2994 tree decl;
2995 cp_label_binding bind;
2997 /* Add a new entry to the SHADOWED_LABELS list so that when we leave
2998 this scope we can restore the old value of IDENTIFIER_TYPE_VALUE. */
2999 bind.prev_value = IDENTIFIER_LABEL_VALUE (id);
3001 decl = make_label_decl (id, /*local_p=*/1);
3002 bind.label = decl;
3003 vec_safe_push (current_binding_level->shadowed_labels, bind);
3005 return decl;
3008 /* Returns nonzero if it is ill-formed to jump past the declaration of
3009 DECL. Returns 2 if it's also a real problem. */
3011 static int
3012 decl_jump_unsafe (tree decl)
3014 /* [stmt.dcl]/3: A program that jumps from a point where a local variable
3015 with automatic storage duration is not in scope to a point where it is
3016 in scope is ill-formed unless the variable has scalar type, class type
3017 with a trivial default constructor and a trivial destructor, a
3018 cv-qualified version of one of these types, or an array of one of the
3019 preceding types and is declared without an initializer (8.5). */
3020 tree type = TREE_TYPE (decl);
3022 if (!VAR_P (decl) || TREE_STATIC (decl)
3023 || type == error_mark_node)
3024 return 0;
3026 if (DECL_NONTRIVIALLY_INITIALIZED_P (decl)
3027 || variably_modified_type_p (type, NULL_TREE))
3028 return 2;
3030 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3031 return 1;
3033 return 0;
3036 /* A subroutine of check_previous_goto_1 and check_goto to identify a branch
3037 to the user. */
3039 static bool
3040 identify_goto (tree decl, location_t loc, const location_t *locus,
3041 diagnostic_t diag_kind)
3043 bool complained
3044 = (decl ? emit_diagnostic (diag_kind, loc, 0, "jump to label %qD", decl)
3045 : emit_diagnostic (diag_kind, loc, 0, "jump to case label"));
3046 if (complained && locus)
3047 inform (*locus, " from here");
3048 return complained;
3051 /* Check that a single previously seen jump to a newly defined label
3052 is OK. DECL is the LABEL_DECL or 0; LEVEL is the binding_level for
3053 the jump context; NAMES are the names in scope in LEVEL at the jump
3054 context; LOCUS is the source position of the jump or 0. Returns
3055 true if all is well. */
3057 static bool
3058 check_previous_goto_1 (tree decl, cp_binding_level* level, tree names,
3059 bool exited_omp, const location_t *locus)
3061 cp_binding_level *b;
3062 bool complained = false;
3063 int identified = 0;
3064 bool saw_eh = false, saw_omp = false, saw_tm = false, saw_cxif = false;
3066 if (exited_omp)
3068 complained = identify_goto (decl, input_location, locus, DK_ERROR);
3069 if (complained)
3070 inform (input_location, " exits OpenMP structured block");
3071 saw_omp = true;
3072 identified = 2;
3075 for (b = current_binding_level; b ; b = b->level_chain)
3077 tree new_decls, old_decls = (b == level ? names : NULL_TREE);
3079 for (new_decls = b->names; new_decls != old_decls;
3080 new_decls = (DECL_P (new_decls) ? DECL_CHAIN (new_decls)
3081 : TREE_CHAIN (new_decls)))
3083 int problem = decl_jump_unsafe (new_decls);
3084 if (! problem)
3085 continue;
3087 if (!identified)
3089 complained = identify_goto (decl, input_location, locus,
3090 DK_PERMERROR);
3091 identified = 1;
3093 if (complained)
3095 if (problem > 1)
3096 inform (DECL_SOURCE_LOCATION (new_decls),
3097 " crosses initialization of %q#D", new_decls);
3098 else
3099 inform (DECL_SOURCE_LOCATION (new_decls),
3100 " enters scope of %q#D which has "
3101 "non-trivial destructor", new_decls);
3105 if (b == level)
3106 break;
3107 if ((b->kind == sk_try || b->kind == sk_catch) && !saw_eh)
3109 if (identified < 2)
3111 complained = identify_goto (decl, input_location, locus,
3112 DK_ERROR);
3113 identified = 2;
3115 if (complained)
3117 if (b->kind == sk_try)
3118 inform (input_location, " enters try block");
3119 else
3120 inform (input_location, " enters catch block");
3122 saw_eh = true;
3124 if (b->kind == sk_omp && !saw_omp)
3126 if (identified < 2)
3128 complained = identify_goto (decl, input_location, locus,
3129 DK_ERROR);
3130 identified = 2;
3132 if (complained)
3133 inform (input_location, " enters OpenMP structured block");
3134 saw_omp = true;
3136 if (b->kind == sk_transaction && !saw_tm)
3138 if (identified < 2)
3140 complained = identify_goto (decl, input_location, locus,
3141 DK_ERROR);
3142 identified = 2;
3144 if (complained)
3145 inform (input_location,
3146 " enters synchronized or atomic statement");
3147 saw_tm = true;
3149 if (!saw_cxif && b->kind == sk_block
3150 && level_for_constexpr_if (b->level_chain))
3152 if (identified < 2)
3154 complained = identify_goto (decl, input_location, locus,
3155 DK_ERROR);
3156 identified = 2;
3158 if (complained)
3159 inform (EXPR_LOCATION (b->level_chain->this_entity),
3160 " enters constexpr if statement");
3161 saw_cxif = true;
3165 return !identified;
3168 static void
3169 check_previous_goto (tree decl, struct named_label_use_entry *use)
3171 check_previous_goto_1 (decl, use->binding_level,
3172 use->names_in_scope, use->in_omp_scope,
3173 &use->o_goto_locus);
3176 static bool
3177 check_switch_goto (cp_binding_level* level)
3179 return check_previous_goto_1 (NULL_TREE, level, level->names, false, NULL);
3182 /* Check that a new jump to a label DECL is OK. Called by
3183 finish_goto_stmt. */
3185 void
3186 check_goto (tree decl)
3188 struct named_label_entry *ent, dummy;
3189 bool saw_catch = false, complained = false;
3190 int identified = 0;
3191 tree bad;
3192 unsigned ix;
3194 /* We can't know where a computed goto is jumping.
3195 So we assume that it's OK. */
3196 if (TREE_CODE (decl) != LABEL_DECL)
3197 return;
3199 /* We didn't record any information about this label when we created it,
3200 and there's not much point since it's trivial to analyze as a return. */
3201 if (decl == cdtor_label)
3202 return;
3204 dummy.label_decl = decl;
3205 ent = named_labels->find (&dummy);
3206 gcc_assert (ent != NULL);
3208 /* If the label hasn't been defined yet, defer checking. */
3209 if (! DECL_INITIAL (decl))
3211 struct named_label_use_entry *new_use;
3213 /* Don't bother creating another use if the last goto had the
3214 same data, and will therefore create the same set of errors. */
3215 if (ent->uses
3216 && ent->uses->names_in_scope == current_binding_level->names)
3217 return;
3219 new_use = ggc_alloc<named_label_use_entry> ();
3220 new_use->binding_level = current_binding_level;
3221 new_use->names_in_scope = current_binding_level->names;
3222 new_use->o_goto_locus = input_location;
3223 new_use->in_omp_scope = false;
3225 new_use->next = ent->uses;
3226 ent->uses = new_use;
3227 return;
3230 if (ent->in_try_scope || ent->in_catch_scope || ent->in_transaction_scope
3231 || ent->in_constexpr_if
3232 || ent->in_omp_scope || !vec_safe_is_empty (ent->bad_decls))
3234 diagnostic_t diag_kind = DK_PERMERROR;
3235 if (ent->in_try_scope || ent->in_catch_scope || ent->in_constexpr_if
3236 || ent->in_transaction_scope || ent->in_omp_scope)
3237 diag_kind = DK_ERROR;
3238 complained = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
3239 &input_location, diag_kind);
3240 identified = 1 + (diag_kind == DK_ERROR);
3243 FOR_EACH_VEC_SAFE_ELT (ent->bad_decls, ix, bad)
3245 int u = decl_jump_unsafe (bad);
3247 if (u > 1 && DECL_ARTIFICIAL (bad))
3249 /* Can't skip init of __exception_info. */
3250 if (identified == 1)
3252 complained = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
3253 &input_location, DK_ERROR);
3254 identified = 2;
3256 if (complained)
3257 inform (DECL_SOURCE_LOCATION (bad), " enters catch block");
3258 saw_catch = true;
3260 else if (complained)
3262 if (u > 1)
3263 inform (DECL_SOURCE_LOCATION (bad),
3264 " skips initialization of %q#D", bad);
3265 else
3266 inform (DECL_SOURCE_LOCATION (bad),
3267 " enters scope of %q#D which has "
3268 "non-trivial destructor", bad);
3272 if (complained)
3274 if (ent->in_try_scope)
3275 inform (input_location, " enters try block");
3276 else if (ent->in_catch_scope && !saw_catch)
3277 inform (input_location, " enters catch block");
3278 else if (ent->in_transaction_scope)
3279 inform (input_location, " enters synchronized or atomic statement");
3280 else if (ent->in_constexpr_if)
3281 inform (input_location, " enters constexpr if statement");
3284 if (ent->in_omp_scope)
3286 if (complained)
3287 inform (input_location, " enters OpenMP structured block");
3289 else if (flag_openmp)
3291 cp_binding_level *b;
3292 for (b = current_binding_level; b ; b = b->level_chain)
3294 if (b == ent->binding_level)
3295 break;
3296 if (b->kind == sk_omp)
3298 if (identified < 2)
3300 complained = identify_goto (decl,
3301 DECL_SOURCE_LOCATION (decl),
3302 &input_location, DK_ERROR);
3303 identified = 2;
3305 if (complained)
3306 inform (input_location, " exits OpenMP structured block");
3307 break;
3313 /* Check that a return is ok wrt OpenMP structured blocks.
3314 Called by finish_return_stmt. Returns true if all is well. */
3316 bool
3317 check_omp_return (void)
3319 cp_binding_level *b;
3320 for (b = current_binding_level; b ; b = b->level_chain)
3321 if (b->kind == sk_omp)
3323 error ("invalid exit from OpenMP structured block");
3324 return false;
3326 else if (b->kind == sk_function_parms)
3327 break;
3328 return true;
3331 /* Define a label, specifying the location in the source file.
3332 Return the LABEL_DECL node for the label. */
3334 static tree
3335 define_label_1 (location_t location, tree name)
3337 struct named_label_entry *ent, dummy;
3338 cp_binding_level *p;
3339 tree decl;
3341 decl = lookup_label (name);
3343 dummy.label_decl = decl;
3344 ent = named_labels->find (&dummy);
3345 gcc_assert (ent != NULL);
3347 /* After labels, make any new cleanups in the function go into their
3348 own new (temporary) binding contour. */
3349 for (p = current_binding_level;
3350 p->kind != sk_function_parms;
3351 p = p->level_chain)
3352 p->more_cleanups_ok = 0;
3354 if (name == get_identifier ("wchar_t"))
3355 permerror (input_location, "label named wchar_t");
3357 if (DECL_INITIAL (decl) != NULL_TREE)
3359 error ("duplicate label %qD", decl);
3360 return error_mark_node;
3362 else
3364 struct named_label_use_entry *use;
3366 /* Mark label as having been defined. */
3367 DECL_INITIAL (decl) = error_mark_node;
3368 /* Say where in the source. */
3369 DECL_SOURCE_LOCATION (decl) = location;
3371 ent->binding_level = current_binding_level;
3372 ent->names_in_scope = current_binding_level->names;
3374 for (use = ent->uses; use ; use = use->next)
3375 check_previous_goto (decl, use);
3376 ent->uses = NULL;
3379 return decl;
3382 /* Wrapper for define_label_1. */
3384 tree
3385 define_label (location_t location, tree name)
3387 tree ret;
3388 bool running = timevar_cond_start (TV_NAME_LOOKUP);
3389 ret = define_label_1 (location, name);
3390 timevar_cond_stop (TV_NAME_LOOKUP, running);
3391 return ret;
3395 struct cp_switch
3397 cp_binding_level *level;
3398 struct cp_switch *next;
3399 /* The SWITCH_STMT being built. */
3400 tree switch_stmt;
3401 /* A splay-tree mapping the low element of a case range to the high
3402 element, or NULL_TREE if there is no high element. Used to
3403 determine whether or not a new case label duplicates an old case
3404 label. We need a tree, rather than simply a hash table, because
3405 of the GNU case range extension. */
3406 splay_tree cases;
3407 /* Remember whether there was a case value that is outside the
3408 range of the original type of the controlling expression. */
3409 bool outside_range_p;
3412 /* A stack of the currently active switch statements. The innermost
3413 switch statement is on the top of the stack. There is no need to
3414 mark the stack for garbage collection because it is only active
3415 during the processing of the body of a function, and we never
3416 collect at that point. */
3418 static struct cp_switch *switch_stack;
3420 /* Called right after a switch-statement condition is parsed.
3421 SWITCH_STMT is the switch statement being parsed. */
3423 void
3424 push_switch (tree switch_stmt)
3426 struct cp_switch *p = XNEW (struct cp_switch);
3427 p->level = current_binding_level;
3428 p->next = switch_stack;
3429 p->switch_stmt = switch_stmt;
3430 p->cases = splay_tree_new (case_compare, NULL, NULL);
3431 p->outside_range_p = false;
3432 switch_stack = p;
3435 void
3436 pop_switch (void)
3438 struct cp_switch *cs = switch_stack;
3439 location_t switch_location;
3441 /* Emit warnings as needed. */
3442 switch_location = EXPR_LOC_OR_LOC (cs->switch_stmt, input_location);
3443 const bool bool_cond_p
3444 = (SWITCH_STMT_TYPE (cs->switch_stmt)
3445 && TREE_CODE (SWITCH_STMT_TYPE (cs->switch_stmt)) == BOOLEAN_TYPE);
3446 if (!processing_template_decl)
3447 c_do_switch_warnings (cs->cases, switch_location,
3448 SWITCH_STMT_TYPE (cs->switch_stmt),
3449 SWITCH_STMT_COND (cs->switch_stmt),
3450 bool_cond_p, cs->outside_range_p);
3452 splay_tree_delete (cs->cases);
3453 switch_stack = switch_stack->next;
3454 free (cs);
3457 /* Convert a case constant VALUE in a switch to the type TYPE of the switch
3458 condition. Note that if TYPE and VALUE are already integral we don't
3459 really do the conversion because the language-independent
3460 warning/optimization code will work better that way. */
3462 static tree
3463 case_conversion (tree type, tree value)
3465 if (value == NULL_TREE)
3466 return value;
3468 if (cxx_dialect >= cxx11
3469 && (SCOPED_ENUM_P (type)
3470 || !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (value))))
3472 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
3473 type = type_promotes_to (type);
3474 value = (perform_implicit_conversion_flags
3475 (type, value, tf_warning_or_error,
3476 LOOKUP_IMPLICIT | LOOKUP_NO_NON_INTEGRAL));
3478 return cxx_constant_value (value);
3481 /* Note that we've seen a definition of a case label, and complain if this
3482 is a bad place for one. */
3484 tree
3485 finish_case_label (location_t loc, tree low_value, tree high_value)
3487 tree cond, r;
3488 cp_binding_level *p;
3489 tree type;
3491 if (processing_template_decl)
3493 tree label;
3495 /* For templates, just add the case label; we'll do semantic
3496 analysis at instantiation-time. */
3497 label = build_decl (loc, LABEL_DECL, NULL_TREE, NULL_TREE);
3498 return add_stmt (build_case_label (low_value, high_value, label));
3501 /* Find the condition on which this switch statement depends. */
3502 cond = SWITCH_STMT_COND (switch_stack->switch_stmt);
3503 if (cond && TREE_CODE (cond) == TREE_LIST)
3504 cond = TREE_VALUE (cond);
3506 if (!check_switch_goto (switch_stack->level))
3507 return error_mark_node;
3509 type = SWITCH_STMT_TYPE (switch_stack->switch_stmt);
3511 low_value = case_conversion (type, low_value);
3512 high_value = case_conversion (type, high_value);
3514 r = c_add_case_label (loc, switch_stack->cases, cond, type,
3515 low_value, high_value,
3516 &switch_stack->outside_range_p);
3518 /* After labels, make any new cleanups in the function go into their
3519 own new (temporary) binding contour. */
3520 for (p = current_binding_level;
3521 p->kind != sk_function_parms;
3522 p = p->level_chain)
3523 p->more_cleanups_ok = 0;
3525 return r;
3528 struct typename_info {
3529 tree scope;
3530 tree name;
3531 tree template_id;
3532 bool enum_p;
3533 bool class_p;
3536 struct typename_hasher : ggc_ptr_hash<tree_node>
3538 typedef typename_info *compare_type;
3540 /* Hash a TYPENAME_TYPE. */
3542 static hashval_t
3543 hash (tree t)
3545 hashval_t hash;
3547 hash = (htab_hash_pointer (TYPE_CONTEXT (t))
3548 ^ htab_hash_pointer (TYPE_IDENTIFIER (t)));
3550 return hash;
3553 /* Compare two TYPENAME_TYPEs. */
3555 static bool
3556 equal (tree t1, const typename_info *t2)
3558 return (TYPE_IDENTIFIER (t1) == t2->name
3559 && TYPE_CONTEXT (t1) == t2->scope
3560 && TYPENAME_TYPE_FULLNAME (t1) == t2->template_id
3561 && TYPENAME_IS_ENUM_P (t1) == t2->enum_p
3562 && TYPENAME_IS_CLASS_P (t1) == t2->class_p);
3566 /* Build a TYPENAME_TYPE. If the type is `typename T::t', CONTEXT is
3567 the type of `T', NAME is the IDENTIFIER_NODE for `t'.
3569 Returns the new TYPENAME_TYPE. */
3571 static GTY (()) hash_table<typename_hasher> *typename_htab;
3573 static tree
3574 build_typename_type (tree context, tree name, tree fullname,
3575 enum tag_types tag_type)
3577 tree t;
3578 tree d;
3579 typename_info ti;
3580 tree *e;
3581 hashval_t hash;
3583 if (typename_htab == NULL)
3584 typename_htab = hash_table<typename_hasher>::create_ggc (61);
3586 ti.scope = FROB_CONTEXT (context);
3587 ti.name = name;
3588 ti.template_id = fullname;
3589 ti.enum_p = tag_type == enum_type;
3590 ti.class_p = (tag_type == class_type
3591 || tag_type == record_type
3592 || tag_type == union_type);
3593 hash = (htab_hash_pointer (ti.scope)
3594 ^ htab_hash_pointer (ti.name));
3596 /* See if we already have this type. */
3597 e = typename_htab->find_slot_with_hash (&ti, hash, INSERT);
3598 if (*e)
3599 t = *e;
3600 else
3602 /* Build the TYPENAME_TYPE. */
3603 t = cxx_make_type (TYPENAME_TYPE);
3604 TYPE_CONTEXT (t) = ti.scope;
3605 TYPENAME_TYPE_FULLNAME (t) = ti.template_id;
3606 TYPENAME_IS_ENUM_P (t) = ti.enum_p;
3607 TYPENAME_IS_CLASS_P (t) = ti.class_p;
3609 /* Build the corresponding TYPE_DECL. */
3610 d = build_decl (input_location, TYPE_DECL, name, t);
3611 TYPE_NAME (TREE_TYPE (d)) = d;
3612 TYPE_STUB_DECL (TREE_TYPE (d)) = d;
3613 DECL_CONTEXT (d) = FROB_CONTEXT (context);
3614 DECL_ARTIFICIAL (d) = 1;
3616 /* Store it in the hash table. */
3617 *e = t;
3619 /* TYPENAME_TYPEs must always be compared structurally, because
3620 they may or may not resolve down to another type depending on
3621 the currently open classes. */
3622 SET_TYPE_STRUCTURAL_EQUALITY (t);
3625 return t;
3628 /* Resolve `typename CONTEXT::NAME'. TAG_TYPE indicates the tag
3629 provided to name the type. Returns an appropriate type, unless an
3630 error occurs, in which case error_mark_node is returned. If we
3631 locate a non-artificial TYPE_DECL and TF_KEEP_TYPE_DECL is set, we
3632 return that, rather than the _TYPE it corresponds to, in other
3633 cases we look through the type decl. If TF_ERROR is set, complain
3634 about errors, otherwise be quiet. */
3636 tree
3637 make_typename_type (tree context, tree name, enum tag_types tag_type,
3638 tsubst_flags_t complain)
3640 tree fullname;
3641 tree t;
3642 bool want_template;
3644 if (name == error_mark_node
3645 || context == NULL_TREE
3646 || context == error_mark_node)
3647 return error_mark_node;
3649 if (TYPE_P (name))
3651 if (!(TYPE_LANG_SPECIFIC (name)
3652 && (CLASSTYPE_IS_TEMPLATE (name)
3653 || CLASSTYPE_USE_TEMPLATE (name))))
3654 name = TYPE_IDENTIFIER (name);
3655 else
3656 /* Create a TEMPLATE_ID_EXPR for the type. */
3657 name = build_nt (TEMPLATE_ID_EXPR,
3658 CLASSTYPE_TI_TEMPLATE (name),
3659 CLASSTYPE_TI_ARGS (name));
3661 else if (TREE_CODE (name) == TYPE_DECL)
3662 name = DECL_NAME (name);
3664 fullname = name;
3666 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3668 name = TREE_OPERAND (name, 0);
3669 if (DECL_TYPE_TEMPLATE_P (name))
3670 name = TREE_OPERAND (fullname, 0) = DECL_NAME (name);
3671 if (TREE_CODE (name) != IDENTIFIER_NODE)
3673 if (complain & tf_error)
3674 error ("%qD is not a type", name);
3675 return error_mark_node;
3678 if (TREE_CODE (name) == TEMPLATE_DECL)
3680 if (complain & tf_error)
3681 error ("%qD used without template parameters", name);
3682 return error_mark_node;
3684 gcc_assert (identifier_p (name));
3685 gcc_assert (TYPE_P (context));
3687 if (!MAYBE_CLASS_TYPE_P (context))
3689 if (complain & tf_error)
3690 error ("%q#T is not a class", context);
3691 return error_mark_node;
3694 /* When the CONTEXT is a dependent type, NAME could refer to a
3695 dependent base class of CONTEXT. But look inside it anyway
3696 if CONTEXT is a currently open scope, in case it refers to a
3697 member of the current instantiation or a non-dependent base;
3698 lookup will stop when we hit a dependent base. */
3699 if (!dependent_scope_p (context))
3700 /* We should only set WANT_TYPE when we're a nested typename type.
3701 Then we can give better diagnostics if we find a non-type. */
3702 t = lookup_field (context, name, 2, /*want_type=*/true);
3703 else
3704 t = NULL_TREE;
3706 if ((!t || TREE_CODE (t) == TREE_LIST) && dependent_type_p (context))
3707 return build_typename_type (context, name, fullname, tag_type);
3709 want_template = TREE_CODE (fullname) == TEMPLATE_ID_EXPR;
3711 if (!t)
3713 if (complain & tf_error)
3714 error (want_template ? G_("no class template named %q#T in %q#T")
3715 : G_("no type named %q#T in %q#T"), name, context);
3716 return error_mark_node;
3719 /* Pull out the template from an injected-class-name (or multiple). */
3720 if (want_template)
3721 t = maybe_get_template_decl_from_type_decl (t);
3723 if (TREE_CODE (t) == TREE_LIST)
3725 if (complain & tf_error)
3727 error ("lookup of %qT in %qT is ambiguous", name, context);
3728 print_candidates (t);
3730 return error_mark_node;
3733 if (want_template && !DECL_TYPE_TEMPLATE_P (t))
3735 if (complain & tf_error)
3736 error ("%<typename %T::%D%> names %q#T, which is not a class template",
3737 context, name, t);
3738 return error_mark_node;
3740 if (!want_template && TREE_CODE (t) != TYPE_DECL)
3742 if (complain & tf_error)
3743 error ("%<typename %T::%D%> names %q#T, which is not a type",
3744 context, name, t);
3745 return error_mark_node;
3748 if (!perform_or_defer_access_check (TYPE_BINFO (context), t, t, complain))
3749 return error_mark_node;
3751 /* If we are currently parsing a template and if T is a typedef accessed
3752 through CONTEXT then we need to remember and check access of T at
3753 template instantiation time. */
3754 add_typedef_to_current_template_for_access_check (t, context, input_location);
3756 if (want_template)
3757 return lookup_template_class (t, TREE_OPERAND (fullname, 1),
3758 NULL_TREE, context,
3759 /*entering_scope=*/0,
3760 complain | tf_user);
3762 if (DECL_ARTIFICIAL (t) || !(complain & tf_keep_type_decl))
3763 t = TREE_TYPE (t);
3765 maybe_record_typedef_use (t);
3767 return t;
3770 /* Resolve `CONTEXT::template NAME'. Returns a TEMPLATE_DECL if the name
3771 can be resolved or an UNBOUND_CLASS_TEMPLATE, unless an error occurs,
3772 in which case error_mark_node is returned.
3774 If PARM_LIST is non-NULL, also make sure that the template parameter
3775 list of TEMPLATE_DECL matches.
3777 If COMPLAIN zero, don't complain about any errors that occur. */
3779 tree
3780 make_unbound_class_template (tree context, tree name, tree parm_list,
3781 tsubst_flags_t complain)
3783 tree t;
3784 tree d;
3786 if (TYPE_P (name))
3787 name = TYPE_IDENTIFIER (name);
3788 else if (DECL_P (name))
3789 name = DECL_NAME (name);
3790 gcc_assert (identifier_p (name));
3792 if (!dependent_type_p (context)
3793 || currently_open_class (context))
3795 tree tmpl = NULL_TREE;
3797 if (MAYBE_CLASS_TYPE_P (context))
3798 tmpl = lookup_field (context, name, 0, false);
3800 if (tmpl && TREE_CODE (tmpl) == TYPE_DECL)
3801 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
3803 if (!tmpl || !DECL_TYPE_TEMPLATE_P (tmpl))
3805 if (complain & tf_error)
3806 error ("no class template named %q#T in %q#T", name, context);
3807 return error_mark_node;
3810 if (parm_list
3811 && !comp_template_parms (DECL_TEMPLATE_PARMS (tmpl), parm_list))
3813 if (complain & tf_error)
3815 error ("template parameters do not match template %qD", tmpl);
3816 inform (DECL_SOURCE_LOCATION (tmpl),
3817 "%qD declared here", tmpl);
3819 return error_mark_node;
3822 if (!perform_or_defer_access_check (TYPE_BINFO (context), tmpl, tmpl,
3823 complain))
3824 return error_mark_node;
3826 return tmpl;
3829 /* Build the UNBOUND_CLASS_TEMPLATE. */
3830 t = cxx_make_type (UNBOUND_CLASS_TEMPLATE);
3831 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
3832 TREE_TYPE (t) = NULL_TREE;
3833 SET_TYPE_STRUCTURAL_EQUALITY (t);
3835 /* Build the corresponding TEMPLATE_DECL. */
3836 d = build_decl (input_location, TEMPLATE_DECL, name, t);
3837 TYPE_NAME (TREE_TYPE (d)) = d;
3838 TYPE_STUB_DECL (TREE_TYPE (d)) = d;
3839 DECL_CONTEXT (d) = FROB_CONTEXT (context);
3840 DECL_ARTIFICIAL (d) = 1;
3841 DECL_TEMPLATE_PARMS (d) = parm_list;
3843 return t;
3848 /* Push the declarations of builtin types into the namespace.
3849 RID_INDEX is the index of the builtin type in the array
3850 RID_POINTERS. NAME is the name used when looking up the builtin
3851 type. TYPE is the _TYPE node for the builtin type. */
3853 void
3854 record_builtin_type (enum rid rid_index,
3855 const char* name,
3856 tree type)
3858 tree rname = NULL_TREE, tname = NULL_TREE;
3859 tree tdecl = NULL_TREE;
3861 if ((int) rid_index < (int) RID_MAX)
3862 rname = ridpointers[(int) rid_index];
3863 if (name)
3864 tname = get_identifier (name);
3866 /* The calls to SET_IDENTIFIER_GLOBAL_VALUE below should be
3867 eliminated. Built-in types should not be looked up name; their
3868 names are keywords that the parser can recognize. However, there
3869 is code in c-common.c that uses identifier_global_value to look
3870 up built-in types by name. */
3871 if (tname)
3873 tdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, tname, type);
3874 DECL_ARTIFICIAL (tdecl) = 1;
3875 SET_IDENTIFIER_GLOBAL_VALUE (tname, tdecl);
3877 if (rname)
3879 if (!tdecl)
3881 tdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, rname, type);
3882 DECL_ARTIFICIAL (tdecl) = 1;
3884 SET_IDENTIFIER_GLOBAL_VALUE (rname, tdecl);
3887 if (!TYPE_NAME (type))
3888 TYPE_NAME (type) = tdecl;
3890 if (tdecl)
3891 debug_hooks->type_decl (tdecl, 0);
3894 /* Push a type into the namespace so that the back ends ignore it. */
3896 static void
3897 record_unknown_type (tree type, const char* name)
3899 tree decl = pushdecl (build_decl (UNKNOWN_LOCATION,
3900 TYPE_DECL, get_identifier (name), type));
3901 /* Make sure the "unknown type" typedecl gets ignored for debug info. */
3902 DECL_IGNORED_P (decl) = 1;
3903 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
3904 TYPE_SIZE (type) = TYPE_SIZE (void_type_node);
3905 SET_TYPE_ALIGN (type, 1);
3906 TYPE_USER_ALIGN (type) = 0;
3907 SET_TYPE_MODE (type, TYPE_MODE (void_type_node));
3910 /* A string for which we should create an IDENTIFIER_NODE at
3911 startup. */
3913 struct predefined_identifier
3915 /* The name of the identifier. */
3916 const char *const name;
3917 /* The place where the IDENTIFIER_NODE should be stored. */
3918 tree *const node;
3919 /* Nonzero if this is the name of a constructor or destructor. */
3920 const int ctor_or_dtor_p;
3923 /* Create all the predefined identifiers. */
3925 static void
3926 initialize_predefined_identifiers (void)
3928 const predefined_identifier *pid;
3930 /* A table of identifiers to create at startup. */
3931 static const predefined_identifier predefined_identifiers[] = {
3932 { "C++", &lang_name_cplusplus, 0 },
3933 { "C", &lang_name_c, 0 },
3934 /* Some of these names have a trailing space so that it is
3935 impossible for them to conflict with names written by users. */
3936 { "__ct ", &ctor_identifier, 1 },
3937 { "__base_ctor ", &base_ctor_identifier, 1 },
3938 { "__comp_ctor ", &complete_ctor_identifier, 1 },
3939 { "__dt ", &dtor_identifier, 1 },
3940 { "__comp_dtor ", &complete_dtor_identifier, 1 },
3941 { "__base_dtor ", &base_dtor_identifier, 1 },
3942 { "__deleting_dtor ", &deleting_dtor_identifier, 1 },
3943 { IN_CHARGE_NAME, &in_charge_identifier, 0 },
3944 { "nelts", &nelts_identifier, 0 },
3945 { THIS_NAME, &this_identifier, 0 },
3946 { VTABLE_DELTA_NAME, &delta_identifier, 0 },
3947 { VTABLE_PFN_NAME, &pfn_identifier, 0 },
3948 { "_vptr", &vptr_identifier, 0 },
3949 { "__vtt_parm", &vtt_parm_identifier, 0 },
3950 { "::", &global_scope_name, 0 },
3951 { "std", &std_identifier, 0 },
3952 { NULL, NULL, 0 }
3955 for (pid = predefined_identifiers; pid->name; ++pid)
3957 *pid->node = get_identifier (pid->name);
3958 if (pid->ctor_or_dtor_p)
3959 IDENTIFIER_CTOR_OR_DTOR_P (*pid->node) = 1;
3963 /* Create the predefined scalar types of C,
3964 and some nodes representing standard constants (0, 1, (void *)0).
3965 Initialize the global binding level.
3966 Make definitions for built-in primitive functions. */
3968 void
3969 cxx_init_decl_processing (void)
3971 tree void_ftype;
3972 tree void_ftype_ptr;
3974 /* Create all the identifiers we need. */
3975 initialize_predefined_identifiers ();
3977 /* Create the global variables. */
3978 push_to_top_level ();
3980 current_function_decl = NULL_TREE;
3981 current_binding_level = NULL;
3982 /* Enter the global namespace. */
3983 gcc_assert (global_namespace == NULL_TREE);
3984 global_namespace = build_lang_decl (NAMESPACE_DECL, global_scope_name,
3985 void_type_node);
3986 DECL_CONTEXT (global_namespace) = build_translation_unit_decl (NULL_TREE);
3987 debug_hooks->register_main_translation_unit
3988 (DECL_CONTEXT (global_namespace));
3989 TREE_PUBLIC (global_namespace) = 1;
3990 begin_scope (sk_namespace, global_namespace);
3992 if (flag_visibility_ms_compat)
3993 default_visibility = VISIBILITY_HIDDEN;
3995 /* Initially, C. */
3996 current_lang_name = lang_name_c;
3998 /* Create the `std' namespace. */
3999 push_namespace (std_identifier);
4000 std_node = current_namespace;
4001 pop_namespace ();
4003 c_common_nodes_and_builtins ();
4005 integer_two_node = build_int_cst (NULL_TREE, 2);
4007 record_builtin_type (RID_BOOL, "bool", boolean_type_node);
4008 truthvalue_type_node = boolean_type_node;
4009 truthvalue_false_node = boolean_false_node;
4010 truthvalue_true_node = boolean_true_node;
4012 empty_except_spec = build_tree_list (NULL_TREE, NULL_TREE);
4013 noexcept_true_spec = build_tree_list (boolean_true_node, NULL_TREE);
4014 noexcept_false_spec = build_tree_list (boolean_false_node, NULL_TREE);
4016 #if 0
4017 record_builtin_type (RID_MAX, NULL, string_type_node);
4018 #endif
4020 delta_type_node = ptrdiff_type_node;
4021 vtable_index_type = ptrdiff_type_node;
4023 vtt_parm_type = build_pointer_type (const_ptr_type_node);
4024 void_ftype = build_function_type_list (void_type_node, NULL_TREE);
4025 void_ftype_ptr = build_function_type_list (void_type_node,
4026 ptr_type_node, NULL_TREE);
4027 void_ftype_ptr
4028 = build_exception_variant (void_ftype_ptr, empty_except_spec);
4030 /* C++ extensions */
4032 unknown_type_node = make_node (LANG_TYPE);
4033 record_unknown_type (unknown_type_node, "unknown type");
4035 /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node. */
4036 TREE_TYPE (unknown_type_node) = unknown_type_node;
4038 /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same
4039 result. */
4040 TYPE_POINTER_TO (unknown_type_node) = unknown_type_node;
4041 TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node;
4043 init_list_type_node = make_node (LANG_TYPE);
4044 record_unknown_type (init_list_type_node, "init list");
4047 /* Make sure we get a unique function type, so we can give
4048 its pointer type a name. (This wins for gdb.) */
4049 tree vfunc_type = make_node (FUNCTION_TYPE);
4050 TREE_TYPE (vfunc_type) = integer_type_node;
4051 TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
4052 layout_type (vfunc_type);
4054 vtable_entry_type = build_pointer_type (vfunc_type);
4056 record_builtin_type (RID_MAX, VTBL_PTR_TYPE, vtable_entry_type);
4058 vtbl_type_node
4059 = build_cplus_array_type (vtable_entry_type, NULL_TREE);
4060 layout_type (vtbl_type_node);
4061 vtbl_type_node = cp_build_qualified_type (vtbl_type_node, TYPE_QUAL_CONST);
4062 record_builtin_type (RID_MAX, NULL, vtbl_type_node);
4063 vtbl_ptr_type_node = build_pointer_type (vtable_entry_type);
4064 layout_type (vtbl_ptr_type_node);
4065 record_builtin_type (RID_MAX, NULL, vtbl_ptr_type_node);
4067 push_namespace (get_identifier ("__cxxabiv1"));
4068 abi_node = current_namespace;
4069 pop_namespace ();
4071 global_type_node = make_node (LANG_TYPE);
4072 record_unknown_type (global_type_node, "global type");
4074 /* Now, C++. */
4075 current_lang_name = lang_name_cplusplus;
4077 if (aligned_new_threshold > 1
4078 && !pow2p_hwi (aligned_new_threshold))
4080 error ("-faligned-new=%d is not a power of two", aligned_new_threshold);
4081 aligned_new_threshold = 1;
4083 if (aligned_new_threshold == -1)
4084 aligned_new_threshold = (cxx_dialect >= cxx1z) ? 1 : 0;
4085 if (aligned_new_threshold == 1)
4086 aligned_new_threshold = max_align_t_align () / BITS_PER_UNIT;
4089 tree newattrs, extvisattr;
4090 tree newtype, deltype;
4091 tree ptr_ftype_sizetype;
4092 tree new_eh_spec;
4094 ptr_ftype_sizetype
4095 = build_function_type_list (ptr_type_node, size_type_node, NULL_TREE);
4096 if (cxx_dialect == cxx98)
4098 tree bad_alloc_id;
4099 tree bad_alloc_type_node;
4100 tree bad_alloc_decl;
4102 push_namespace (std_identifier);
4103 bad_alloc_id = get_identifier ("bad_alloc");
4104 bad_alloc_type_node = make_class_type (RECORD_TYPE);
4105 TYPE_CONTEXT (bad_alloc_type_node) = current_namespace;
4106 bad_alloc_decl
4107 = create_implicit_typedef (bad_alloc_id, bad_alloc_type_node);
4108 DECL_CONTEXT (bad_alloc_decl) = current_namespace;
4109 pop_namespace ();
4111 new_eh_spec
4112 = add_exception_specifier (NULL_TREE, bad_alloc_type_node, -1);
4114 else
4115 new_eh_spec = noexcept_false_spec;
4117 /* Ensure attribs.c is initialized. */
4118 init_attributes ();
4120 /* Ensure constraint.cc is initialized. */
4121 init_constraint_processing ();
4123 extvisattr = build_tree_list (get_identifier ("externally_visible"),
4124 NULL_TREE);
4125 newattrs = tree_cons (get_identifier ("alloc_size"),
4126 build_tree_list (NULL_TREE, integer_one_node),
4127 extvisattr);
4128 newtype = cp_build_type_attribute_variant (ptr_ftype_sizetype, newattrs);
4129 newtype = build_exception_variant (newtype, new_eh_spec);
4130 deltype = cp_build_type_attribute_variant (void_ftype_ptr, extvisattr);
4131 deltype = build_exception_variant (deltype, empty_except_spec);
4132 tree opnew = push_cp_library_fn (NEW_EXPR, newtype, 0);
4133 DECL_IS_MALLOC (opnew) = 1;
4134 DECL_IS_OPERATOR_NEW (opnew) = 1;
4135 opnew = push_cp_library_fn (VEC_NEW_EXPR, newtype, 0);
4136 DECL_IS_MALLOC (opnew) = 1;
4137 DECL_IS_OPERATOR_NEW (opnew) = 1;
4138 push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
4139 push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
4140 if (flag_sized_deallocation)
4142 /* Also push the sized deallocation variants:
4143 void operator delete(void*, std::size_t) throw();
4144 void operator delete[](void*, std::size_t) throw(); */
4145 tree void_ftype_ptr_size
4146 = build_function_type_list (void_type_node, ptr_type_node,
4147 size_type_node, NULL_TREE);
4148 deltype = cp_build_type_attribute_variant (void_ftype_ptr_size,
4149 extvisattr);
4150 deltype = build_exception_variant (deltype, empty_except_spec);
4151 push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
4152 push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
4155 if (aligned_new_threshold)
4157 push_namespace (std_identifier);
4158 tree align_id = get_identifier ("align_val_t");
4159 align_type_node = start_enum (align_id, NULL_TREE, size_type_node,
4160 NULL_TREE, /*scoped*/true, NULL);
4161 pop_namespace ();
4163 /* operator new (size_t, align_val_t); */
4164 newtype = build_function_type_list (ptr_type_node, size_type_node,
4165 align_type_node, NULL_TREE);
4166 newtype = cp_build_type_attribute_variant (newtype, newattrs);
4167 newtype = build_exception_variant (newtype, new_eh_spec);
4168 opnew = push_cp_library_fn (NEW_EXPR, newtype, 0);
4169 DECL_IS_MALLOC (opnew) = 1;
4170 DECL_IS_OPERATOR_NEW (opnew) = 1;
4171 opnew = push_cp_library_fn (VEC_NEW_EXPR, newtype, 0);
4172 DECL_IS_MALLOC (opnew) = 1;
4173 DECL_IS_OPERATOR_NEW (opnew) = 1;
4175 /* operator delete (void *, align_val_t); */
4176 deltype = build_function_type_list (void_type_node, ptr_type_node,
4177 align_type_node, NULL_TREE);
4178 deltype = cp_build_type_attribute_variant (deltype, extvisattr);
4179 deltype = build_exception_variant (deltype, empty_except_spec);
4180 push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
4181 push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
4183 if (flag_sized_deallocation)
4185 /* operator delete (void *, size_t, align_val_t); */
4186 deltype = build_function_type_list (void_type_node, ptr_type_node,
4187 size_type_node, align_type_node,
4188 NULL_TREE);
4189 deltype = cp_build_type_attribute_variant (deltype, extvisattr);
4190 deltype = build_exception_variant (deltype, empty_except_spec);
4191 push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
4192 push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
4196 nullptr_type_node = make_node (NULLPTR_TYPE);
4197 TYPE_SIZE (nullptr_type_node) = bitsize_int (GET_MODE_BITSIZE (ptr_mode));
4198 TYPE_SIZE_UNIT (nullptr_type_node) = size_int (GET_MODE_SIZE (ptr_mode));
4199 TYPE_UNSIGNED (nullptr_type_node) = 1;
4200 TYPE_PRECISION (nullptr_type_node) = GET_MODE_BITSIZE (ptr_mode);
4201 if (abi_version_at_least (9))
4202 SET_TYPE_ALIGN (nullptr_type_node, GET_MODE_ALIGNMENT (ptr_mode));
4203 SET_TYPE_MODE (nullptr_type_node, ptr_mode);
4204 record_builtin_type (RID_MAX, "decltype(nullptr)", nullptr_type_node);
4205 nullptr_node = build_int_cst (nullptr_type_node, 0);
4208 abort_fndecl
4209 = build_library_fn_ptr ("__cxa_pure_virtual", void_ftype,
4210 ECF_NORETURN | ECF_NOTHROW);
4212 /* Perform other language dependent initializations. */
4213 init_class_processing ();
4214 init_rtti_processing ();
4215 init_template_processing ();
4217 if (flag_exceptions)
4218 init_exception_processing ();
4220 if (! supports_one_only ())
4221 flag_weak = 0;
4223 make_fname_decl = cp_make_fname_decl;
4224 start_fname_decls ();
4226 /* Show we use EH for cleanups. */
4227 if (flag_exceptions)
4228 using_eh_for_cleanups ();
4231 /* Generate an initializer for a function naming variable from
4232 NAME. NAME may be NULL, to indicate a dependent name. TYPE_P is
4233 filled in with the type of the init. */
4235 tree
4236 cp_fname_init (const char* name, tree *type_p)
4238 tree domain = NULL_TREE;
4239 tree type;
4240 tree init = NULL_TREE;
4241 size_t length = 0;
4243 if (name)
4245 length = strlen (name);
4246 domain = build_index_type (size_int (length));
4247 init = build_string (length + 1, name);
4250 type = cp_build_qualified_type (char_type_node, TYPE_QUAL_CONST);
4251 type = build_cplus_array_type (type, domain);
4253 *type_p = type;
4255 if (init)
4256 TREE_TYPE (init) = type;
4257 else
4258 init = error_mark_node;
4260 return init;
4263 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give
4264 the decl, LOC is the location to give the decl, NAME is the
4265 initialization string and TYPE_DEP indicates whether NAME depended
4266 on the type of the function. We make use of that to detect
4267 __PRETTY_FUNCTION__ inside a template fn. This is being done lazily
4268 at the point of first use, so we mustn't push the decl now. */
4270 static tree
4271 cp_make_fname_decl (location_t loc, tree id, int type_dep)
4273 const char *const name = (type_dep && processing_template_decl
4274 ? NULL : fname_as_string (type_dep));
4275 tree type;
4276 tree init = cp_fname_init (name, &type);
4277 tree decl = build_decl (loc, VAR_DECL, id, type);
4279 if (name)
4280 free (CONST_CAST (char *, name));
4282 /* As we're using pushdecl_with_scope, we must set the context. */
4283 DECL_CONTEXT (decl) = current_function_decl;
4285 TREE_STATIC (decl) = 1;
4286 TREE_READONLY (decl) = 1;
4287 DECL_ARTIFICIAL (decl) = 1;
4289 TREE_USED (decl) = 1;
4291 if (current_function_decl)
4293 cp_binding_level *b = current_binding_level;
4294 if (b->kind == sk_function_parms)
4295 return error_mark_node;
4296 while (b->level_chain->kind != sk_function_parms)
4297 b = b->level_chain;
4298 pushdecl_with_scope (decl, b, /*is_friend=*/false);
4299 cp_finish_decl (decl, init, /*init_const_expr_p=*/false, NULL_TREE,
4300 LOOKUP_ONLYCONVERTING);
4302 else
4304 DECL_THIS_STATIC (decl) = true;
4305 pushdecl_top_level_and_finish (decl, init);
4308 return decl;
4311 static tree
4312 builtin_function_1 (tree decl, tree context, bool is_global)
4314 tree id = DECL_NAME (decl);
4315 const char *name = IDENTIFIER_POINTER (id);
4317 retrofit_lang_decl (decl);
4319 DECL_ARTIFICIAL (decl) = 1;
4320 SET_OVERLOADED_OPERATOR_CODE (decl, ERROR_MARK);
4321 SET_DECL_LANGUAGE (decl, lang_c);
4322 /* Runtime library routines are, by definition, available in an
4323 external shared object. */
4324 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
4325 DECL_VISIBILITY_SPECIFIED (decl) = 1;
4327 DECL_CONTEXT (decl) = context;
4329 if (is_global)
4330 pushdecl_top_level (decl);
4331 else
4332 pushdecl (decl);
4334 /* A function in the user's namespace should have an explicit
4335 declaration before it is used. Mark the built-in function as
4336 anticipated but not actually declared. */
4337 if (name[0] != '_' || name[1] != '_')
4338 DECL_ANTICIPATED (decl) = 1;
4339 else if (strncmp (name + 2, "builtin_", strlen ("builtin_")) != 0)
4341 size_t len = strlen (name);
4343 /* Treat __*_chk fortification functions as anticipated as well,
4344 unless they are __builtin_*. */
4345 if (len > strlen ("___chk")
4346 && memcmp (name + len - strlen ("_chk"),
4347 "_chk", strlen ("_chk") + 1) == 0)
4348 DECL_ANTICIPATED (decl) = 1;
4351 return decl;
4354 tree
4355 cxx_builtin_function (tree decl)
4357 tree id = DECL_NAME (decl);
4358 const char *name = IDENTIFIER_POINTER (id);
4359 /* All builtins that don't begin with an '_' should additionally
4360 go in the 'std' namespace. */
4361 if (name[0] != '_')
4363 tree decl2 = copy_node(decl);
4364 push_namespace (std_identifier);
4365 builtin_function_1 (decl2, std_node, false);
4366 pop_namespace ();
4369 return builtin_function_1 (decl, NULL_TREE, false);
4372 /* Like cxx_builtin_function, but guarantee the function is added to the global
4373 scope. This is to allow function specific options to add new machine
4374 dependent builtins when the target ISA changes via attribute((target(...)))
4375 which saves space on program startup if the program does not use non-generic
4376 ISAs. */
4378 tree
4379 cxx_builtin_function_ext_scope (tree decl)
4382 tree id = DECL_NAME (decl);
4383 const char *name = IDENTIFIER_POINTER (id);
4384 /* All builtins that don't begin with an '_' should additionally
4385 go in the 'std' namespace. */
4386 if (name[0] != '_')
4388 tree decl2 = copy_node(decl);
4389 push_namespace (std_identifier);
4390 builtin_function_1 (decl2, std_node, true);
4391 pop_namespace ();
4394 return builtin_function_1 (decl, NULL_TREE, true);
4397 /* Generate a FUNCTION_DECL with the typical flags for a runtime library
4398 function. Not called directly. */
4400 static tree
4401 build_library_fn (tree name, enum tree_code operator_code, tree type,
4402 int ecf_flags)
4404 tree fn = build_lang_decl (FUNCTION_DECL, name, type);
4405 DECL_EXTERNAL (fn) = 1;
4406 TREE_PUBLIC (fn) = 1;
4407 DECL_ARTIFICIAL (fn) = 1;
4408 SET_OVERLOADED_OPERATOR_CODE (fn, operator_code);
4409 SET_DECL_LANGUAGE (fn, lang_c);
4410 /* Runtime library routines are, by definition, available in an
4411 external shared object. */
4412 DECL_VISIBILITY (fn) = VISIBILITY_DEFAULT;
4413 DECL_VISIBILITY_SPECIFIED (fn) = 1;
4414 set_call_expr_flags (fn, ecf_flags);
4415 return fn;
4418 /* Returns the _DECL for a library function with C++ linkage. */
4420 static tree
4421 build_cp_library_fn (tree name, enum tree_code operator_code, tree type,
4422 int ecf_flags)
4424 tree fn = build_library_fn (name, operator_code, type, ecf_flags);
4425 DECL_CONTEXT (fn) = FROB_CONTEXT (current_namespace);
4426 SET_DECL_LANGUAGE (fn, lang_cplusplus);
4427 return fn;
4430 /* Like build_library_fn, but takes a C string instead of an
4431 IDENTIFIER_NODE. */
4433 tree
4434 build_library_fn_ptr (const char* name, tree type, int ecf_flags)
4436 return build_library_fn (get_identifier (name), ERROR_MARK, type, ecf_flags);
4439 /* Like build_cp_library_fn, but takes a C string instead of an
4440 IDENTIFIER_NODE. */
4442 tree
4443 build_cp_library_fn_ptr (const char* name, tree type, int ecf_flags)
4445 return build_cp_library_fn (get_identifier (name), ERROR_MARK, type,
4446 ecf_flags);
4449 /* Like build_library_fn, but also pushes the function so that we will
4450 be able to find it via IDENTIFIER_GLOBAL_VALUE. Also, the function
4451 may throw exceptions listed in RAISES. */
4453 tree
4454 push_library_fn (tree name, tree type, tree raises, int ecf_flags)
4456 tree fn;
4458 if (raises)
4459 type = build_exception_variant (type, raises);
4461 fn = build_library_fn (name, ERROR_MARK, type, ecf_flags);
4462 pushdecl_top_level (fn);
4463 return fn;
4466 /* Like build_cp_library_fn, but also pushes the function so that it
4467 will be found by normal lookup. */
4469 static tree
4470 push_cp_library_fn (enum tree_code operator_code, tree type,
4471 int ecf_flags)
4473 tree fn = build_cp_library_fn (ansi_opname (operator_code),
4474 operator_code,
4475 type, ecf_flags);
4476 pushdecl (fn);
4477 if (flag_tm)
4478 apply_tm_attr (fn, get_identifier ("transaction_safe"));
4479 return fn;
4482 /* Like push_library_fn, but takes a TREE_LIST of parm types rather than
4483 a FUNCTION_TYPE. */
4485 tree
4486 push_void_library_fn (tree name, tree parmtypes, int ecf_flags)
4488 tree type = build_function_type (void_type_node, parmtypes);
4489 return push_library_fn (name, type, NULL_TREE, ecf_flags);
4492 /* Like push_library_fn, but also note that this function throws
4493 and does not return. Used for __throw_foo and the like. */
4495 tree
4496 push_throw_library_fn (tree name, tree type)
4498 tree fn = push_library_fn (name, type, NULL_TREE, ECF_NORETURN);
4499 return fn;
4502 /* When we call finish_struct for an anonymous union, we create
4503 default copy constructors and such. But, an anonymous union
4504 shouldn't have such things; this function undoes the damage to the
4505 anonymous union type T.
4507 (The reason that we create the synthesized methods is that we don't
4508 distinguish `union { int i; }' from `typedef union { int i; } U'.
4509 The first is an anonymous union; the second is just an ordinary
4510 union type.) */
4512 void
4513 fixup_anonymous_aggr (tree t)
4515 tree *q;
4517 /* Wipe out memory of synthesized methods. */
4518 TYPE_HAS_USER_CONSTRUCTOR (t) = 0;
4519 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0;
4520 TYPE_HAS_COPY_CTOR (t) = 0;
4521 TYPE_HAS_CONST_COPY_CTOR (t) = 0;
4522 TYPE_HAS_COPY_ASSIGN (t) = 0;
4523 TYPE_HAS_CONST_COPY_ASSIGN (t) = 0;
4525 /* Splice the implicitly generated functions out of the TYPE_METHODS
4526 list. */
4527 q = &TYPE_METHODS (t);
4528 while (*q)
4530 if (DECL_ARTIFICIAL (*q))
4531 *q = TREE_CHAIN (*q);
4532 else
4533 q = &DECL_CHAIN (*q);
4536 /* ISO C++ 9.5.3. Anonymous unions may not have function members. */
4537 if (TYPE_METHODS (t))
4539 tree decl = TYPE_MAIN_DECL (t);
4541 if (TREE_CODE (t) != UNION_TYPE)
4542 error_at (DECL_SOURCE_LOCATION (decl),
4543 "an anonymous struct cannot have function members");
4544 else
4545 error_at (DECL_SOURCE_LOCATION (decl),
4546 "an anonymous union cannot have function members");
4549 /* Anonymous aggregates cannot have fields with ctors, dtors or complex
4550 assignment operators (because they cannot have these methods themselves).
4551 For anonymous unions this is already checked because they are not allowed
4552 in any union, otherwise we have to check it. */
4553 if (TREE_CODE (t) != UNION_TYPE)
4555 tree field, type;
4557 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
4558 if (TREE_CODE (field) == FIELD_DECL)
4560 type = TREE_TYPE (field);
4561 if (CLASS_TYPE_P (type))
4563 if (TYPE_NEEDS_CONSTRUCTING (type))
4564 error ("member %q+#D with constructor not allowed "
4565 "in anonymous aggregate", field);
4566 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
4567 error ("member %q+#D with destructor not allowed "
4568 "in anonymous aggregate", field);
4569 if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type))
4570 error ("member %q+#D with copy assignment operator "
4571 "not allowed in anonymous aggregate", field);
4577 /* Warn for an attribute located at LOCATION that appertains to the
4578 class type CLASS_TYPE that has not been properly placed after its
4579 class-key, in it class-specifier. */
4581 void
4582 warn_misplaced_attr_for_class_type (source_location location,
4583 tree class_type)
4585 gcc_assert (OVERLOAD_TYPE_P (class_type));
4587 if (warning_at (location, OPT_Wattributes,
4588 "attribute ignored in declaration "
4589 "of %q#T", class_type))
4590 inform (location,
4591 "attribute for %q#T must follow the %qs keyword",
4592 class_type, class_key_or_enum_as_string (class_type));
4595 /* Make sure that a declaration with no declarator is well-formed, i.e.
4596 just declares a tagged type or anonymous union.
4598 Returns the type declared; or NULL_TREE if none. */
4600 tree
4601 check_tag_decl (cp_decl_specifier_seq *declspecs,
4602 bool explicit_type_instantiation_p)
4604 int saw_friend = decl_spec_seq_has_spec_p (declspecs, ds_friend);
4605 int saw_typedef = decl_spec_seq_has_spec_p (declspecs, ds_typedef);
4606 /* If a class, struct, or enum type is declared by the DECLSPECS
4607 (i.e, if a class-specifier, enum-specifier, or non-typename
4608 elaborated-type-specifier appears in the DECLSPECS),
4609 DECLARED_TYPE is set to the corresponding type. */
4610 tree declared_type = NULL_TREE;
4611 bool error_p = false;
4613 if (declspecs->multiple_types_p)
4614 error ("multiple types in one declaration");
4615 else if (declspecs->redefined_builtin_type)
4617 if (!in_system_header_at (input_location))
4618 permerror (declspecs->locations[ds_redefined_builtin_type_spec],
4619 "redeclaration of C++ built-in type %qT",
4620 declspecs->redefined_builtin_type);
4621 return NULL_TREE;
4624 if (declspecs->type
4625 && TYPE_P (declspecs->type)
4626 && ((TREE_CODE (declspecs->type) != TYPENAME_TYPE
4627 && MAYBE_CLASS_TYPE_P (declspecs->type))
4628 || TREE_CODE (declspecs->type) == ENUMERAL_TYPE))
4629 declared_type = declspecs->type;
4630 else if (declspecs->type == error_mark_node)
4631 error_p = true;
4632 if (declared_type == NULL_TREE && ! saw_friend && !error_p)
4633 permerror (input_location, "declaration does not declare anything");
4634 else if (declared_type != NULL_TREE && type_uses_auto (declared_type))
4636 error_at (declspecs->locations[ds_type_spec],
4637 "%<auto%> can only be specified for variables "
4638 "or function declarations");
4639 return error_mark_node;
4641 /* Check for an anonymous union. */
4642 else if (declared_type && RECORD_OR_UNION_CODE_P (TREE_CODE (declared_type))
4643 && TYPE_UNNAMED_P (declared_type))
4645 /* 7/3 In a simple-declaration, the optional init-declarator-list
4646 can be omitted only when declaring a class (clause 9) or
4647 enumeration (7.2), that is, when the decl-specifier-seq contains
4648 either a class-specifier, an elaborated-type-specifier with
4649 a class-key (9.1), or an enum-specifier. In these cases and
4650 whenever a class-specifier or enum-specifier is present in the
4651 decl-specifier-seq, the identifiers in these specifiers are among
4652 the names being declared by the declaration (as class-name,
4653 enum-names, or enumerators, depending on the syntax). In such
4654 cases, and except for the declaration of an unnamed bit-field (9.6),
4655 the decl-specifier-seq shall introduce one or more names into the
4656 program, or shall redeclare a name introduced by a previous
4657 declaration. [Example:
4658 enum { }; // ill-formed
4659 typedef class { }; // ill-formed
4660 --end example] */
4661 if (saw_typedef)
4663 error ("missing type-name in typedef-declaration");
4664 return NULL_TREE;
4666 /* Anonymous unions are objects, so they can have specifiers. */;
4667 SET_ANON_AGGR_TYPE_P (declared_type);
4669 if (TREE_CODE (declared_type) != UNION_TYPE
4670 && !in_system_header_at (input_location))
4671 pedwarn (input_location, OPT_Wpedantic, "ISO C++ prohibits anonymous structs");
4674 else
4676 if (decl_spec_seq_has_spec_p (declspecs, ds_inline))
4677 error_at (declspecs->locations[ds_inline],
4678 "%<inline%> can only be specified for functions");
4679 else if (decl_spec_seq_has_spec_p (declspecs, ds_virtual))
4680 error_at (declspecs->locations[ds_virtual],
4681 "%<virtual%> can only be specified for functions");
4682 else if (saw_friend
4683 && (!current_class_type
4684 || current_scope () != current_class_type))
4685 error_at (declspecs->locations[ds_friend],
4686 "%<friend%> can only be specified inside a class");
4687 else if (decl_spec_seq_has_spec_p (declspecs, ds_explicit))
4688 error_at (declspecs->locations[ds_explicit],
4689 "%<explicit%> can only be specified for constructors");
4690 else if (declspecs->storage_class)
4691 error_at (declspecs->locations[ds_storage_class],
4692 "a storage class can only be specified for objects "
4693 "and functions");
4694 else if (decl_spec_seq_has_spec_p (declspecs, ds_const))
4695 error_at (declspecs->locations[ds_const],
4696 "%<const%> can only be specified for objects and "
4697 "functions");
4698 else if (decl_spec_seq_has_spec_p (declspecs, ds_volatile))
4699 error_at (declspecs->locations[ds_volatile],
4700 "%<volatile%> can only be specified for objects and "
4701 "functions");
4702 else if (decl_spec_seq_has_spec_p (declspecs, ds_restrict))
4703 error_at (declspecs->locations[ds_restrict],
4704 "%<__restrict%> can only be specified for objects and "
4705 "functions");
4706 else if (decl_spec_seq_has_spec_p (declspecs, ds_thread))
4707 error_at (declspecs->locations[ds_thread],
4708 "%<__thread%> can only be specified for objects "
4709 "and functions");
4710 else if (saw_typedef)
4711 warning_at (declspecs->locations[ds_typedef], 0,
4712 "%<typedef%> was ignored in this declaration");
4713 else if (decl_spec_seq_has_spec_p (declspecs, ds_constexpr))
4714 error_at (declspecs->locations[ds_constexpr],
4715 "%<constexpr%> cannot be used for type declarations");
4718 if (declspecs->attributes && warn_attributes && declared_type)
4720 location_t loc;
4721 if (!CLASS_TYPE_P (declared_type)
4722 || !CLASSTYPE_TEMPLATE_INSTANTIATION (declared_type))
4723 /* For a non-template class, use the name location. */
4724 loc = location_of (declared_type);
4725 else
4726 /* For a template class (an explicit instantiation), use the
4727 current location. */
4728 loc = input_location;
4730 if (explicit_type_instantiation_p)
4731 /* [dcl.attr.grammar]/4:
4733 No attribute-specifier-seq shall appertain to an explicit
4734 instantiation. */
4736 if (warning_at (loc, OPT_Wattributes,
4737 "attribute ignored in explicit instantiation %q#T",
4738 declared_type))
4739 inform (loc,
4740 "no attribute can be applied to "
4741 "an explicit instantiation");
4743 else
4744 warn_misplaced_attr_for_class_type (loc, declared_type);
4747 return declared_type;
4750 /* Called when a declaration is seen that contains no names to declare.
4751 If its type is a reference to a structure, union or enum inherited
4752 from a containing scope, shadow that tag name for the current scope
4753 with a forward reference.
4754 If its type defines a new named structure or union
4755 or defines an enum, it is valid but we need not do anything here.
4756 Otherwise, it is an error.
4758 C++: may have to grok the declspecs to learn about static,
4759 complain for anonymous unions.
4761 Returns the TYPE declared -- or NULL_TREE if none. */
4763 tree
4764 shadow_tag (cp_decl_specifier_seq *declspecs)
4766 tree t = check_tag_decl (declspecs,
4767 /*explicit_type_instantiation_p=*/false);
4769 if (!t)
4770 return NULL_TREE;
4772 if (maybe_process_partial_specialization (t) == error_mark_node)
4773 return NULL_TREE;
4775 /* This is where the variables in an anonymous union are
4776 declared. An anonymous union declaration looks like:
4777 union { ... } ;
4778 because there is no declarator after the union, the parser
4779 sends that declaration here. */
4780 if (ANON_AGGR_TYPE_P (t))
4782 fixup_anonymous_aggr (t);
4784 if (TYPE_FIELDS (t))
4786 tree decl = grokdeclarator (/*declarator=*/NULL,
4787 declspecs, NORMAL, 0, NULL);
4788 finish_anon_union (decl);
4792 return t;
4795 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
4797 tree
4798 groktypename (cp_decl_specifier_seq *type_specifiers,
4799 const cp_declarator *declarator,
4800 bool is_template_arg)
4802 tree attrs;
4803 tree type;
4804 enum decl_context context
4805 = is_template_arg ? TEMPLATE_TYPE_ARG : TYPENAME;
4806 attrs = type_specifiers->attributes;
4807 type_specifiers->attributes = NULL_TREE;
4808 type = grokdeclarator (declarator, type_specifiers, context, 0, &attrs);
4809 if (attrs && type != error_mark_node)
4811 if (CLASS_TYPE_P (type))
4812 warning (OPT_Wattributes, "ignoring attributes applied to class type %qT "
4813 "outside of definition", type);
4814 else if (MAYBE_CLASS_TYPE_P (type))
4815 /* A template type parameter or other dependent type. */
4816 warning (OPT_Wattributes, "ignoring attributes applied to dependent "
4817 "type %qT without an associated declaration", type);
4818 else
4819 cplus_decl_attributes (&type, attrs, 0);
4821 return type;
4824 /* Process a DECLARATOR for a function-scope variable declaration,
4825 namespace-scope variable declaration, or function declaration.
4826 (Function definitions go through start_function; class member
4827 declarations appearing in the body of the class go through
4828 grokfield.) The DECL corresponding to the DECLARATOR is returned.
4829 If an error occurs, the error_mark_node is returned instead.
4831 DECLSPECS are the decl-specifiers for the declaration. INITIALIZED is
4832 SD_INITIALIZED if an explicit initializer is present, or SD_DEFAULTED
4833 for an explicitly defaulted function, or SD_DELETED for an explicitly
4834 deleted function, but 0 (SD_UNINITIALIZED) if this is a variable
4835 implicitly initialized via a default constructor. ATTRIBUTES and
4836 PREFIX_ATTRIBUTES are GNU attributes associated with this declaration.
4838 The scope represented by the context of the returned DECL is pushed
4839 (if it is not the global namespace) and is assigned to
4840 *PUSHED_SCOPE_P. The caller is then responsible for calling
4841 pop_scope on *PUSHED_SCOPE_P if it is set. */
4843 tree
4844 start_decl (const cp_declarator *declarator,
4845 cp_decl_specifier_seq *declspecs,
4846 int initialized,
4847 tree attributes,
4848 tree prefix_attributes,
4849 tree *pushed_scope_p)
4851 tree decl;
4852 tree context;
4853 bool was_public;
4854 int flags;
4855 bool alias;
4857 *pushed_scope_p = NULL_TREE;
4859 /* An object declared as __attribute__((deprecated)) suppresses
4860 warnings of uses of other deprecated items. */
4861 if (lookup_attribute ("deprecated", attributes))
4862 deprecated_state = DEPRECATED_SUPPRESS;
4864 attributes = chainon (attributes, prefix_attributes);
4866 decl = grokdeclarator (declarator, declspecs, NORMAL, initialized,
4867 &attributes);
4869 deprecated_state = DEPRECATED_NORMAL;
4871 if (decl == NULL_TREE || VOID_TYPE_P (decl)
4872 || decl == error_mark_node)
4873 return error_mark_node;
4875 context = CP_DECL_CONTEXT (decl);
4876 if (context != global_namespace)
4877 *pushed_scope_p = push_scope (context);
4879 /* Is it valid for this decl to have an initializer at all?
4880 If not, set INITIALIZED to zero, which will indirectly
4881 tell `cp_finish_decl' to ignore the initializer once it is parsed. */
4882 if (initialized
4883 && TREE_CODE (decl) == TYPE_DECL)
4885 error ("typedef %qD is initialized (use decltype instead)", decl);
4886 return error_mark_node;
4889 if (initialized)
4891 if (! toplevel_bindings_p ()
4892 && DECL_EXTERNAL (decl))
4893 warning (0, "declaration of %q#D has %<extern%> and is initialized",
4894 decl);
4895 DECL_EXTERNAL (decl) = 0;
4896 if (toplevel_bindings_p ())
4897 TREE_STATIC (decl) = 1;
4899 alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl)) != 0;
4901 if (alias && TREE_CODE (decl) == FUNCTION_DECL)
4902 record_key_method_defined (decl);
4904 /* If this is a typedef that names the class for linkage purposes
4905 (7.1.3p8), apply any attributes directly to the type. */
4906 if (TREE_CODE (decl) == TYPE_DECL
4907 && OVERLOAD_TYPE_P (TREE_TYPE (decl))
4908 && decl == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (decl))))
4909 flags = ATTR_FLAG_TYPE_IN_PLACE;
4910 else
4911 flags = 0;
4913 /* Set attributes here so if duplicate decl, will have proper attributes. */
4914 cplus_decl_attributes (&decl, attributes, flags);
4916 /* Dllimported symbols cannot be defined. Static data members (which
4917 can be initialized in-class and dllimported) go through grokfield,
4918 not here, so we don't need to exclude those decls when checking for
4919 a definition. */
4920 if (initialized && DECL_DLLIMPORT_P (decl))
4922 error ("definition of %q#D is marked %<dllimport%>", decl);
4923 DECL_DLLIMPORT_P (decl) = 0;
4926 /* If #pragma weak was used, mark the decl weak now. */
4927 if (!processing_template_decl)
4928 maybe_apply_pragma_weak (decl);
4930 if (TREE_CODE (decl) == FUNCTION_DECL
4931 && DECL_DECLARED_INLINE_P (decl)
4932 && DECL_UNINLINABLE (decl)
4933 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
4934 warning_at (DECL_SOURCE_LOCATION (decl), 0,
4935 "inline function %qD given attribute noinline", decl);
4937 if (TYPE_P (context) && COMPLETE_TYPE_P (complete_type (context)))
4939 bool this_tmpl = (processing_template_decl
4940 > template_class_depth (context));
4941 if (VAR_P (decl))
4943 tree field = lookup_field (context, DECL_NAME (decl), 0, false);
4944 if (field == NULL_TREE
4945 || !(VAR_P (field) || variable_template_p (field)))
4946 error ("%q+#D is not a static data member of %q#T", decl, context);
4947 else if (variable_template_p (field) && !this_tmpl)
4949 if (DECL_LANG_SPECIFIC (decl)
4950 && DECL_TEMPLATE_SPECIALIZATION (decl))
4951 /* OK, specialization was already checked. */;
4952 else
4954 error_at (DECL_SOURCE_LOCATION (decl),
4955 "non-member-template declaration of %qD", decl);
4956 inform (DECL_SOURCE_LOCATION (field), "does not match "
4957 "member template declaration here");
4958 return error_mark_node;
4961 else
4963 if (variable_template_p (field))
4964 field = DECL_TEMPLATE_RESULT (field);
4966 if (DECL_CONTEXT (field) != context)
4968 if (!same_type_p (DECL_CONTEXT (field), context))
4969 permerror (input_location, "ISO C++ does not permit %<%T::%D%> "
4970 "to be defined as %<%T::%D%>",
4971 DECL_CONTEXT (field), DECL_NAME (decl),
4972 context, DECL_NAME (decl));
4973 DECL_CONTEXT (decl) = DECL_CONTEXT (field);
4975 /* Static data member are tricky; an in-class initialization
4976 still doesn't provide a definition, so the in-class
4977 declaration will have DECL_EXTERNAL set, but will have an
4978 initialization. Thus, duplicate_decls won't warn
4979 about this situation, and so we check here. */
4980 if (initialized && DECL_INITIALIZED_IN_CLASS_P (field))
4981 error ("duplicate initialization of %qD", decl);
4982 if (duplicate_decls (decl, field, /*newdecl_is_friend=*/false))
4983 decl = field;
4984 if (decl_spec_seq_has_spec_p (declspecs, ds_constexpr)
4985 && !DECL_DECLARED_CONSTEXPR_P (field))
4986 error ("%qD declared %<constexpr%> outside its class", field);
4989 else
4991 tree field = check_classfn (context, decl,
4992 this_tmpl
4993 ? current_template_parms
4994 : NULL_TREE);
4995 if (field && field != error_mark_node
4996 && duplicate_decls (decl, field,
4997 /*newdecl_is_friend=*/false))
4998 decl = field;
5001 /* cp_finish_decl sets DECL_EXTERNAL if DECL_IN_AGGR_P is set. */
5002 DECL_IN_AGGR_P (decl) = 0;
5003 /* Do not mark DECL as an explicit specialization if it was not
5004 already marked as an instantiation; a declaration should
5005 never be marked as a specialization unless we know what
5006 template is being specialized. */
5007 if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
5009 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
5010 if (TREE_CODE (decl) == FUNCTION_DECL)
5011 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
5012 && DECL_DECLARED_INLINE_P (decl));
5013 else
5014 DECL_COMDAT (decl) = false;
5016 /* [temp.expl.spec] An explicit specialization of a static data
5017 member of a template is a definition if the declaration
5018 includes an initializer; otherwise, it is a declaration.
5020 We check for processing_specialization so this only applies
5021 to the new specialization syntax. */
5022 if (!initialized && processing_specialization)
5023 DECL_EXTERNAL (decl) = 1;
5026 if (DECL_EXTERNAL (decl) && ! DECL_TEMPLATE_SPECIALIZATION (decl)
5027 /* Aliases are definitions. */
5028 && !alias)
5029 permerror (input_location, "declaration of %q#D outside of class is not definition",
5030 decl);
5033 was_public = TREE_PUBLIC (decl);
5035 /* Enter this declaration into the symbol table. Don't push the plain
5036 VAR_DECL for a variable template. */
5037 if (!template_parm_scope_p ()
5038 || !VAR_P (decl))
5039 decl = maybe_push_decl (decl);
5041 if (processing_template_decl)
5042 decl = push_template_decl (decl);
5043 if (decl == error_mark_node)
5044 return error_mark_node;
5046 if (VAR_P (decl)
5047 && DECL_NAMESPACE_SCOPE_P (decl) && !TREE_PUBLIC (decl) && !was_public
5048 && !DECL_THIS_STATIC (decl) && !DECL_ARTIFICIAL (decl))
5050 /* This is a const variable with implicit 'static'. Set
5051 DECL_THIS_STATIC so we can tell it from variables that are
5052 !TREE_PUBLIC because of the anonymous namespace. */
5053 gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (decl)) || errorcount);
5054 DECL_THIS_STATIC (decl) = 1;
5057 if (current_function_decl && VAR_P (decl)
5058 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
5060 bool ok = false;
5061 if (CP_DECL_THREAD_LOCAL_P (decl))
5062 error ("%qD declared %<thread_local%> in %<constexpr%> function",
5063 decl);
5064 else if (TREE_STATIC (decl))
5065 error ("%qD declared %<static%> in %<constexpr%> function", decl);
5066 else
5067 ok = true;
5068 if (!ok)
5069 cp_function_chain->invalid_constexpr = true;
5072 if (!processing_template_decl && VAR_P (decl))
5073 start_decl_1 (decl, initialized);
5075 return decl;
5078 /* Process the declaration of a variable DECL. INITIALIZED is true
5079 iff DECL is explicitly initialized. (INITIALIZED is false if the
5080 variable is initialized via an implicitly-called constructor.)
5081 This function must be called for ordinary variables (including, for
5082 example, implicit instantiations of templates), but must not be
5083 called for template declarations. */
5085 void
5086 start_decl_1 (tree decl, bool initialized)
5088 tree type;
5089 bool complete_p;
5090 bool aggregate_definition_p;
5092 gcc_assert (!processing_template_decl);
5094 if (error_operand_p (decl))
5095 return;
5097 gcc_assert (VAR_P (decl));
5099 type = TREE_TYPE (decl);
5100 complete_p = COMPLETE_TYPE_P (type);
5101 aggregate_definition_p = MAYBE_CLASS_TYPE_P (type) && !DECL_EXTERNAL (decl);
5103 /* If an explicit initializer is present, or if this is a definition
5104 of an aggregate, then we need a complete type at this point.
5105 (Scalars are always complete types, so there is nothing to
5106 check.) This code just sets COMPLETE_P; errors (if necessary)
5107 are issued below. */
5108 if ((initialized || aggregate_definition_p)
5109 && !complete_p
5110 && COMPLETE_TYPE_P (complete_type (type)))
5112 complete_p = true;
5113 /* We will not yet have set TREE_READONLY on DECL if the type
5114 was "const", but incomplete, before this point. But, now, we
5115 have a complete type, so we can try again. */
5116 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
5119 if (initialized)
5120 /* Is it valid for this decl to have an initializer at all? */
5122 /* Don't allow initializations for incomplete types except for
5123 arrays which might be completed by the initialization. */
5124 if (complete_p)
5125 ; /* A complete type is ok. */
5126 else if (type_uses_auto (type))
5127 ; /* An auto type is ok. */
5128 else if (TREE_CODE (type) != ARRAY_TYPE)
5130 error ("variable %q#D has initializer but incomplete type", decl);
5131 type = TREE_TYPE (decl) = error_mark_node;
5133 else if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
5135 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
5136 error ("elements of array %q#D have incomplete type", decl);
5137 /* else we already gave an error in start_decl. */
5140 else if (aggregate_definition_p && !complete_p)
5142 if (type_uses_auto (type))
5143 error ("declaration of %q#D has no initializer", decl);
5144 else
5145 error ("aggregate %q#D has incomplete type and cannot be defined",
5146 decl);
5147 /* Change the type so that assemble_variable will give
5148 DECL an rtl we can live with: (mem (const_int 0)). */
5149 type = TREE_TYPE (decl) = error_mark_node;
5152 /* Create a new scope to hold this declaration if necessary.
5153 Whether or not a new scope is necessary cannot be determined
5154 until after the type has been completed; if the type is a
5155 specialization of a class template it is not until after
5156 instantiation has occurred that TYPE_HAS_NONTRIVIAL_DESTRUCTOR
5157 will be set correctly. */
5158 maybe_push_cleanup_level (type);
5161 /* Handle initialization of references. DECL, TYPE, and INIT have the
5162 same meaning as in cp_finish_decl. *CLEANUP must be NULL on entry,
5163 but will be set to a new CLEANUP_STMT if a temporary is created
5164 that must be destroyed subsequently.
5166 Returns an initializer expression to use to initialize DECL, or
5167 NULL if the initialization can be performed statically.
5169 Quotes on semantics can be found in ARM 8.4.3. */
5171 static tree
5172 grok_reference_init (tree decl, tree type, tree init, int flags)
5174 if (init == NULL_TREE)
5176 if ((DECL_LANG_SPECIFIC (decl) == 0
5177 || DECL_IN_AGGR_P (decl) == 0)
5178 && ! DECL_THIS_EXTERN (decl))
5179 error ("%qD declared as reference but not initialized", decl);
5180 return NULL_TREE;
5183 if (TREE_CODE (init) == TREE_LIST)
5184 init = build_x_compound_expr_from_list (init, ELK_INIT,
5185 tf_warning_or_error);
5187 tree ttype = TREE_TYPE (type);
5188 if (TREE_CODE (ttype) != ARRAY_TYPE
5189 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
5190 /* Note: default conversion is only called in very special cases. */
5191 init = decay_conversion (init, tf_warning_or_error);
5193 /* check_initializer handles this for non-reference variables, but for
5194 references we need to do it here or the initializer will get the
5195 incomplete array type and confuse later calls to
5196 cp_complete_array_type. */
5197 if (TREE_CODE (ttype) == ARRAY_TYPE
5198 && TYPE_DOMAIN (ttype) == NULL_TREE
5199 && (BRACE_ENCLOSED_INITIALIZER_P (init)
5200 || TREE_CODE (init) == STRING_CST))
5202 cp_complete_array_type (&ttype, init, false);
5203 if (ttype != TREE_TYPE (type))
5204 type = cp_build_reference_type (ttype, TYPE_REF_IS_RVALUE (type));
5207 /* Convert INIT to the reference type TYPE. This may involve the
5208 creation of a temporary, whose lifetime must be the same as that
5209 of the reference. If so, a DECL_EXPR for the temporary will be
5210 added just after the DECL_EXPR for DECL. That's why we don't set
5211 DECL_INITIAL for local references (instead assigning to them
5212 explicitly); we need to allow the temporary to be initialized
5213 first. */
5214 return initialize_reference (type, init, flags,
5215 tf_warning_or_error);
5218 /* Designated initializers in arrays are not supported in GNU C++.
5219 The parser cannot detect this error since it does not know whether
5220 a given brace-enclosed initializer is for a class type or for an
5221 array. This function checks that CE does not use a designated
5222 initializer. If it does, an error is issued. Returns true if CE
5223 is valid, i.e., does not have a designated initializer. */
5225 static bool
5226 check_array_designated_initializer (constructor_elt *ce,
5227 unsigned HOST_WIDE_INT index)
5229 /* Designated initializers for array elements are not supported. */
5230 if (ce->index)
5232 /* The parser only allows identifiers as designated
5233 initializers. */
5234 if (ce->index == error_mark_node)
5236 error ("name used in a GNU-style designated "
5237 "initializer for an array");
5238 return false;
5240 else if (identifier_p (ce->index))
5242 error ("name %qD used in a GNU-style designated "
5243 "initializer for an array", ce->index);
5244 return false;
5247 tree ce_index = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5248 ce->index, true);
5249 if (ce_index
5250 && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (ce_index))
5251 && (TREE_CODE (ce_index = maybe_constant_value (ce_index))
5252 == INTEGER_CST))
5254 /* A C99 designator is OK if it matches the current index. */
5255 if (wi::eq_p (ce_index, index))
5256 return true;
5257 else
5258 sorry ("non-trivial designated initializers not supported");
5260 else
5261 error ("C99 designator %qE is not an integral constant-expression",
5262 ce->index);
5264 return false;
5267 return true;
5270 /* When parsing `int a[] = {1, 2};' we don't know the size of the
5271 array until we finish parsing the initializer. If that's the
5272 situation we're in, update DECL accordingly. */
5274 static void
5275 maybe_deduce_size_from_array_init (tree decl, tree init)
5277 tree type = TREE_TYPE (decl);
5279 if (TREE_CODE (type) == ARRAY_TYPE
5280 && TYPE_DOMAIN (type) == NULL_TREE
5281 && TREE_CODE (decl) != TYPE_DECL)
5283 /* do_default is really a C-ism to deal with tentative definitions.
5284 But let's leave it here to ease the eventual merge. */
5285 int do_default = !DECL_EXTERNAL (decl);
5286 tree initializer = init ? init : DECL_INITIAL (decl);
5287 int failure = 0;
5289 /* Check that there are no designated initializers in INIT, as
5290 those are not supported in GNU C++, and as the middle-end
5291 will crash if presented with a non-numeric designated
5292 initializer. */
5293 if (initializer && BRACE_ENCLOSED_INITIALIZER_P (initializer))
5295 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initializer);
5296 constructor_elt *ce;
5297 HOST_WIDE_INT i;
5298 FOR_EACH_VEC_SAFE_ELT (v, i, ce)
5299 if (!check_array_designated_initializer (ce, i))
5300 failure = 1;
5303 if (!failure)
5305 failure = cp_complete_array_type (&TREE_TYPE (decl), initializer,
5306 do_default);
5307 if (failure == 1)
5309 error_at (EXPR_LOC_OR_LOC (initializer,
5310 DECL_SOURCE_LOCATION (decl)),
5311 "initializer fails to determine size of %qD", decl);
5313 else if (failure == 2)
5315 if (do_default)
5317 error_at (DECL_SOURCE_LOCATION (decl),
5318 "array size missing in %qD", decl);
5320 /* If a `static' var's size isn't known, make it extern as
5321 well as static, so it does not get allocated. If it's not
5322 `static', then don't mark it extern; finish_incomplete_decl
5323 will give it a default size and it will get allocated. */
5324 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
5325 DECL_EXTERNAL (decl) = 1;
5327 else if (failure == 3)
5329 error_at (DECL_SOURCE_LOCATION (decl),
5330 "zero-size array %qD", decl);
5334 cp_apply_type_quals_to_decl (cp_type_quals (TREE_TYPE (decl)), decl);
5336 relayout_decl (decl);
5340 /* Set DECL_SIZE, DECL_ALIGN, etc. for DECL (a VAR_DECL), and issue
5341 any appropriate error messages regarding the layout. */
5343 static void
5344 layout_var_decl (tree decl)
5346 tree type;
5348 type = TREE_TYPE (decl);
5349 if (type == error_mark_node)
5350 return;
5352 /* If we haven't already laid out this declaration, do so now.
5353 Note that we must not call complete type for an external object
5354 because it's type might involve templates that we are not
5355 supposed to instantiate yet. (And it's perfectly valid to say
5356 `extern X x' for some incomplete type `X'.) */
5357 if (!DECL_EXTERNAL (decl))
5358 complete_type (type);
5359 if (!DECL_SIZE (decl)
5360 && TREE_TYPE (decl) != error_mark_node
5361 && complete_or_array_type_p (type))
5362 layout_decl (decl, 0);
5364 if (!DECL_EXTERNAL (decl) && DECL_SIZE (decl) == NULL_TREE)
5366 /* An automatic variable with an incomplete type: that is an error.
5367 Don't talk about array types here, since we took care of that
5368 message in grokdeclarator. */
5369 error_at (DECL_SOURCE_LOCATION (decl),
5370 "storage size of %qD isn%'t known", decl);
5371 TREE_TYPE (decl) = error_mark_node;
5373 #if 0
5374 /* Keep this code around in case we later want to control debug info
5375 based on whether a type is "used". (jason 1999-11-11) */
5377 else if (!DECL_EXTERNAL (decl) && MAYBE_CLASS_TYPE_P (ttype))
5378 /* Let debugger know it should output info for this type. */
5379 note_debug_info_needed (ttype);
5381 if (TREE_STATIC (decl) && DECL_CLASS_SCOPE_P (decl))
5382 note_debug_info_needed (DECL_CONTEXT (decl));
5383 #endif
5385 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
5386 && DECL_SIZE (decl) != NULL_TREE
5387 && ! TREE_CONSTANT (DECL_SIZE (decl)))
5389 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
5390 constant_expression_warning (DECL_SIZE (decl));
5391 else
5393 error_at (DECL_SOURCE_LOCATION (decl),
5394 "storage size of %qD isn%'t constant", decl);
5395 TREE_TYPE (decl) = error_mark_node;
5400 /* If a local static variable is declared in an inline function, or if
5401 we have a weak definition, we must endeavor to create only one
5402 instance of the variable at link-time. */
5404 void
5405 maybe_commonize_var (tree decl)
5407 /* Static data in a function with comdat linkage also has comdat
5408 linkage. */
5409 if (TREE_STATIC (decl)
5410 /* Don't mess with __FUNCTION__. */
5411 && ! DECL_ARTIFICIAL (decl)
5412 && DECL_FUNCTION_SCOPE_P (decl)
5413 && vague_linkage_p (DECL_CONTEXT (decl)))
5415 if (flag_weak)
5417 /* With weak symbols, we simply make the variable COMDAT;
5418 that will cause copies in multiple translations units to
5419 be merged. */
5420 comdat_linkage (decl);
5422 else
5424 if (DECL_INITIAL (decl) == NULL_TREE
5425 || DECL_INITIAL (decl) == error_mark_node)
5427 /* Without weak symbols, we can use COMMON to merge
5428 uninitialized variables. */
5429 TREE_PUBLIC (decl) = 1;
5430 DECL_COMMON (decl) = 1;
5432 else
5434 /* While for initialized variables, we must use internal
5435 linkage -- which means that multiple copies will not
5436 be merged. */
5437 TREE_PUBLIC (decl) = 0;
5438 DECL_COMMON (decl) = 0;
5439 if (warning_at (DECL_SOURCE_LOCATION (decl), 0,
5440 "sorry: semantics of inline function static "
5441 "data %q#D are wrong (you%'ll wind up "
5442 "with multiple copies)", decl))
5443 inform (DECL_SOURCE_LOCATION (decl),
5444 "you can work around this by removing the initializer");
5450 /* Issue an error message if DECL is an uninitialized const variable. */
5452 static void
5453 check_for_uninitialized_const_var (tree decl)
5455 tree type = strip_array_types (TREE_TYPE (decl));
5457 /* ``Unless explicitly declared extern, a const object does not have
5458 external linkage and must be initialized. ($8.4; $12.1)'' ARM
5459 7.1.6 */
5460 if (VAR_P (decl)
5461 && TREE_CODE (type) != REFERENCE_TYPE
5462 && (CP_TYPE_CONST_P (type) || var_in_constexpr_fn (decl))
5463 && !DECL_INITIAL (decl))
5465 tree field = default_init_uninitialized_part (type);
5466 if (!field)
5467 return;
5469 if (CP_TYPE_CONST_P (type))
5470 permerror (DECL_SOURCE_LOCATION (decl),
5471 "uninitialized const %qD", decl);
5472 else
5474 error_at (DECL_SOURCE_LOCATION (decl),
5475 "uninitialized variable %qD in %<constexpr%> function",
5476 decl);
5477 cp_function_chain->invalid_constexpr = true;
5480 if (CLASS_TYPE_P (type))
5482 tree defaulted_ctor;
5484 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
5485 "%q#T has no user-provided default constructor", type);
5486 defaulted_ctor = in_class_defaulted_default_constructor (type);
5487 if (defaulted_ctor)
5488 inform (DECL_SOURCE_LOCATION (defaulted_ctor),
5489 "constructor is not user-provided because it is "
5490 "explicitly defaulted in the class body");
5491 inform (DECL_SOURCE_LOCATION (field),
5492 "and the implicitly-defined constructor does not "
5493 "initialize %q#D", field);
5498 /* Structure holding the current initializer being processed by reshape_init.
5499 CUR is a pointer to the current element being processed, END is a pointer
5500 after the last element present in the initializer. */
5501 struct reshape_iter
5503 constructor_elt *cur;
5504 constructor_elt *end;
5507 static tree reshape_init_r (tree, reshape_iter *, bool, tsubst_flags_t);
5509 /* FIELD is a FIELD_DECL or NULL. In the former case, the value
5510 returned is the next FIELD_DECL (possibly FIELD itself) that can be
5511 initialized. If there are no more such fields, the return value
5512 will be NULL. */
5514 tree
5515 next_initializable_field (tree field)
5517 while (field
5518 && (TREE_CODE (field) != FIELD_DECL
5519 || (DECL_C_BIT_FIELD (field) && !DECL_NAME (field))
5520 || DECL_ARTIFICIAL (field)))
5521 field = DECL_CHAIN (field);
5523 return field;
5526 /* Return true for [dcl.init.list] direct-list-initialization from
5527 single element of enumeration with a fixed underlying type. */
5529 bool
5530 is_direct_enum_init (tree type, tree init)
5532 if (cxx_dialect >= cxx1z
5533 && TREE_CODE (type) == ENUMERAL_TYPE
5534 && ENUM_FIXED_UNDERLYING_TYPE_P (type)
5535 && TREE_CODE (init) == CONSTRUCTOR
5536 && CONSTRUCTOR_IS_DIRECT_INIT (init)
5537 && CONSTRUCTOR_NELTS (init) == 1)
5538 return true;
5539 return false;
5542 /* Subroutine of reshape_init_array and reshape_init_vector, which does
5543 the actual work. ELT_TYPE is the element type of the array. MAX_INDEX is an
5544 INTEGER_CST representing the size of the array minus one (the maximum index),
5545 or NULL_TREE if the array was declared without specifying the size. D is
5546 the iterator within the constructor. */
5548 static tree
5549 reshape_init_array_1 (tree elt_type, tree max_index, reshape_iter *d,
5550 tsubst_flags_t complain)
5552 tree new_init;
5553 bool sized_array_p = (max_index && TREE_CONSTANT (max_index));
5554 unsigned HOST_WIDE_INT max_index_cst = 0;
5555 unsigned HOST_WIDE_INT index;
5557 /* The initializer for an array is always a CONSTRUCTOR. */
5558 new_init = build_constructor (init_list_type_node, NULL);
5560 if (sized_array_p)
5562 /* Minus 1 is used for zero sized arrays. */
5563 if (integer_all_onesp (max_index))
5564 return new_init;
5566 if (tree_fits_uhwi_p (max_index))
5567 max_index_cst = tree_to_uhwi (max_index);
5568 /* sizetype is sign extended, not zero extended. */
5569 else
5570 max_index_cst = tree_to_uhwi (fold_convert (size_type_node, max_index));
5573 /* Loop until there are no more initializers. */
5574 for (index = 0;
5575 d->cur != d->end && (!sized_array_p || index <= max_index_cst);
5576 ++index)
5578 tree elt_init;
5579 constructor_elt *old_cur = d->cur;
5581 check_array_designated_initializer (d->cur, index);
5582 elt_init = reshape_init_r (elt_type, d, /*first_initializer_p=*/false,
5583 complain);
5584 if (elt_init == error_mark_node)
5585 return error_mark_node;
5586 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init),
5587 size_int (index), elt_init);
5588 if (!TREE_CONSTANT (elt_init))
5589 TREE_CONSTANT (new_init) = false;
5591 /* This can happen with an invalid initializer (c++/54501). */
5592 if (d->cur == old_cur && !sized_array_p)
5593 break;
5596 return new_init;
5599 /* Subroutine of reshape_init_r, processes the initializers for arrays.
5600 Parameters are the same of reshape_init_r. */
5602 static tree
5603 reshape_init_array (tree type, reshape_iter *d, tsubst_flags_t complain)
5605 tree max_index = NULL_TREE;
5607 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
5609 if (TYPE_DOMAIN (type))
5610 max_index = array_type_nelts (type);
5612 return reshape_init_array_1 (TREE_TYPE (type), max_index, d, complain);
5615 /* Subroutine of reshape_init_r, processes the initializers for vectors.
5616 Parameters are the same of reshape_init_r. */
5618 static tree
5619 reshape_init_vector (tree type, reshape_iter *d, tsubst_flags_t complain)
5621 tree max_index = NULL_TREE;
5623 gcc_assert (VECTOR_TYPE_P (type));
5625 if (COMPOUND_LITERAL_P (d->cur->value))
5627 tree value = d->cur->value;
5628 if (!same_type_p (TREE_TYPE (value), type))
5630 if (complain & tf_error)
5631 error ("invalid type %qT as initializer for a vector of type %qT",
5632 TREE_TYPE (d->cur->value), type);
5633 value = error_mark_node;
5635 ++d->cur;
5636 return value;
5639 /* For a vector, we initialize it as an array of the appropriate size. */
5640 if (VECTOR_TYPE_P (type))
5641 max_index = size_int (TYPE_VECTOR_SUBPARTS (type) - 1);
5643 return reshape_init_array_1 (TREE_TYPE (type), max_index, d, complain);
5646 /* Subroutine of reshape_init_r, processes the initializers for classes
5647 or union. Parameters are the same of reshape_init_r. */
5649 static tree
5650 reshape_init_class (tree type, reshape_iter *d, bool first_initializer_p,
5651 tsubst_flags_t complain)
5653 tree field;
5654 tree new_init;
5656 gcc_assert (CLASS_TYPE_P (type));
5658 /* The initializer for a class is always a CONSTRUCTOR. */
5659 new_init = build_constructor (init_list_type_node, NULL);
5660 field = next_initializable_field (TYPE_FIELDS (type));
5662 if (!field)
5664 /* [dcl.init.aggr]
5666 An initializer for an aggregate member that is an
5667 empty class shall have the form of an empty
5668 initializer-list {}. */
5669 if (!first_initializer_p)
5671 if (complain & tf_error)
5672 error ("initializer for %qT must be brace-enclosed", type);
5673 return error_mark_node;
5675 return new_init;
5678 /* Loop through the initializable fields, gathering initializers. */
5679 while (d->cur != d->end)
5681 tree field_init;
5682 constructor_elt *old_cur = d->cur;
5684 /* Handle designated initializers, as an extension. */
5685 if (d->cur->index)
5687 if (d->cur->index == error_mark_node)
5688 return error_mark_node;
5690 if (TREE_CODE (d->cur->index) == FIELD_DECL)
5691 /* We already reshaped this. */
5692 gcc_assert (d->cur->index == field);
5693 else if (TREE_CODE (d->cur->index) == IDENTIFIER_NODE)
5694 field = lookup_field_1 (type, d->cur->index, /*want_type=*/false);
5695 else
5697 if (complain & tf_error)
5698 error ("%<[%E] =%> used in a GNU-style designated initializer"
5699 " for class %qT", d->cur->index, type);
5700 return error_mark_node;
5703 if (!field || TREE_CODE (field) != FIELD_DECL)
5705 if (complain & tf_error)
5706 error ("%qT has no non-static data member named %qD", type,
5707 d->cur->index);
5708 return error_mark_node;
5712 /* If we processed all the member of the class, we are done. */
5713 if (!field)
5714 break;
5716 field_init = reshape_init_r (TREE_TYPE (field), d,
5717 /*first_initializer_p=*/false, complain);
5718 if (field_init == error_mark_node)
5719 return error_mark_node;
5721 if (d->cur == old_cur && d->cur->index)
5723 /* This can happen with an invalid initializer for a flexible
5724 array member (c++/54441). */
5725 if (complain & tf_error)
5726 error ("invalid initializer for %q#D", field);
5727 return error_mark_node;
5730 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init), field, field_init);
5732 /* [dcl.init.aggr]
5734 When a union is initialized with a brace-enclosed
5735 initializer, the braces shall only contain an
5736 initializer for the first member of the union. */
5737 if (TREE_CODE (type) == UNION_TYPE)
5738 break;
5740 field = next_initializable_field (DECL_CHAIN (field));
5743 return new_init;
5746 /* Subroutine of reshape_init_r. We're in a context where C99 initializer
5747 designators are not valid; either complain or return true to indicate
5748 that reshape_init_r should return error_mark_node. */
5750 static bool
5751 has_designator_problem (reshape_iter *d, tsubst_flags_t complain)
5753 if (d->cur->index)
5755 if (complain & tf_error)
5756 error ("C99 designator %qE outside aggregate initializer",
5757 d->cur->index);
5758 else
5759 return true;
5761 return false;
5764 /* Subroutine of reshape_init, which processes a single initializer (part of
5765 a CONSTRUCTOR). TYPE is the type of the variable being initialized, D is the
5766 iterator within the CONSTRUCTOR which points to the initializer to process.
5767 FIRST_INITIALIZER_P is true if this is the first initializer of the
5768 outermost CONSTRUCTOR node. */
5770 static tree
5771 reshape_init_r (tree type, reshape_iter *d, bool first_initializer_p,
5772 tsubst_flags_t complain)
5774 tree init = d->cur->value;
5776 if (error_operand_p (init))
5777 return error_mark_node;
5779 if (first_initializer_p && !CP_AGGREGATE_TYPE_P (type)
5780 && has_designator_problem (d, complain))
5781 return error_mark_node;
5783 if (TREE_CODE (type) == COMPLEX_TYPE)
5785 /* A complex type can be initialized from one or two initializers,
5786 but braces are not elided. */
5787 d->cur++;
5788 if (BRACE_ENCLOSED_INITIALIZER_P (init))
5790 if (CONSTRUCTOR_NELTS (init) > 2)
5792 if (complain & tf_error)
5793 error ("too many initializers for %qT", type);
5794 else
5795 return error_mark_node;
5798 else if (first_initializer_p && d->cur != d->end)
5800 vec<constructor_elt, va_gc> *v = 0;
5801 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, init);
5802 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, d->cur->value);
5803 if (has_designator_problem (d, complain))
5804 return error_mark_node;
5805 d->cur++;
5806 init = build_constructor (init_list_type_node, v);
5808 return init;
5811 /* A non-aggregate type is always initialized with a single
5812 initializer. */
5813 if (!CP_AGGREGATE_TYPE_P (type))
5815 /* It is invalid to initialize a non-aggregate type with a
5816 brace-enclosed initializer before C++0x.
5817 We need to check for BRACE_ENCLOSED_INITIALIZER_P here because
5818 of g++.old-deja/g++.mike/p7626.C: a pointer-to-member constant is
5819 a CONSTRUCTOR (with a record type). */
5820 if (TREE_CODE (init) == CONSTRUCTOR
5821 /* Don't complain about a capture-init. */
5822 && !CONSTRUCTOR_IS_DIRECT_INIT (init)
5823 && BRACE_ENCLOSED_INITIALIZER_P (init)) /* p7626.C */
5825 if (SCALAR_TYPE_P (type))
5827 if (cxx_dialect < cxx11
5828 /* Isn't value-initialization. */
5829 || CONSTRUCTOR_NELTS (init) > 0)
5831 if (complain & tf_error)
5832 error ("braces around scalar initializer for type %qT",
5833 type);
5834 init = error_mark_node;
5837 else
5838 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5841 d->cur++;
5842 return init;
5845 /* "If T is a class type and the initializer list has a single element of
5846 type cv U, where U is T or a class derived from T, the object is
5847 initialized from that element." Even if T is an aggregate. */
5848 if (cxx_dialect >= cxx11 && (CLASS_TYPE_P (type) || VECTOR_TYPE_P (type))
5849 && first_initializer_p
5850 && d->end - d->cur == 1
5851 && reference_related_p (type, TREE_TYPE (init)))
5853 d->cur++;
5854 return init;
5857 /* [dcl.init.aggr]
5859 All implicit type conversions (clause _conv_) are considered when
5860 initializing the aggregate member with an initializer from an
5861 initializer-list. If the initializer can initialize a member,
5862 the member is initialized. Otherwise, if the member is itself a
5863 non-empty subaggregate, brace elision is assumed and the
5864 initializer is considered for the initialization of the first
5865 member of the subaggregate. */
5866 if (TREE_CODE (init) != CONSTRUCTOR
5867 /* But don't try this for the first initializer, since that would be
5868 looking through the outermost braces; A a2 = { a1 }; is not a
5869 valid aggregate initialization. */
5870 && !first_initializer_p
5871 && (same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (init))
5872 || can_convert_arg (type, TREE_TYPE (init), init, LOOKUP_NORMAL,
5873 complain)))
5875 d->cur++;
5876 return init;
5879 /* [dcl.init.string]
5881 A char array (whether plain char, signed char, or unsigned char)
5882 can be initialized by a string-literal (optionally enclosed in
5883 braces); a wchar_t array can be initialized by a wide
5884 string-literal (optionally enclosed in braces). */
5885 if (TREE_CODE (type) == ARRAY_TYPE
5886 && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type))))
5888 tree str_init = init;
5890 /* Strip one level of braces if and only if they enclose a single
5891 element (as allowed by [dcl.init.string]). */
5892 if (!first_initializer_p
5893 && TREE_CODE (str_init) == CONSTRUCTOR
5894 && CONSTRUCTOR_NELTS (str_init) == 1)
5896 str_init = (*CONSTRUCTOR_ELTS (str_init))[0].value;
5899 /* If it's a string literal, then it's the initializer for the array
5900 as a whole. Otherwise, continue with normal initialization for
5901 array types (one value per array element). */
5902 if (TREE_CODE (str_init) == STRING_CST)
5904 if (has_designator_problem (d, complain))
5905 return error_mark_node;
5906 d->cur++;
5907 return str_init;
5911 /* The following cases are about aggregates. If we are not within a full
5912 initializer already, and there is not a CONSTRUCTOR, it means that there
5913 is a missing set of braces (that is, we are processing the case for
5914 which reshape_init exists). */
5915 if (!first_initializer_p)
5917 if (TREE_CODE (init) == CONSTRUCTOR)
5919 if (TREE_TYPE (init) && TYPE_PTRMEMFUNC_P (TREE_TYPE (init)))
5920 /* There is no need to reshape pointer-to-member function
5921 initializers, as they are always constructed correctly
5922 by the front end. */
5924 else if (COMPOUND_LITERAL_P (init))
5925 /* For a nested compound literal, there is no need to reshape since
5926 brace elision is not allowed. Even if we decided to allow it,
5927 we should add a call to reshape_init in finish_compound_literal,
5928 before calling digest_init, so changing this code would still
5929 not be necessary. */
5930 gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (init));
5931 else
5933 ++d->cur;
5934 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
5935 return reshape_init (type, init, complain);
5939 if (complain & tf_warning)
5940 warning (OPT_Wmissing_braces,
5941 "missing braces around initializer for %qT",
5942 type);
5945 /* Dispatch to specialized routines. */
5946 if (CLASS_TYPE_P (type))
5947 return reshape_init_class (type, d, first_initializer_p, complain);
5948 else if (TREE_CODE (type) == ARRAY_TYPE)
5949 return reshape_init_array (type, d, complain);
5950 else if (VECTOR_TYPE_P (type))
5951 return reshape_init_vector (type, d, complain);
5952 else
5953 gcc_unreachable();
5956 /* Undo the brace-elision allowed by [dcl.init.aggr] in a
5957 brace-enclosed aggregate initializer.
5959 INIT is the CONSTRUCTOR containing the list of initializers describing
5960 a brace-enclosed initializer for an entity of the indicated aggregate TYPE.
5961 It may not presently match the shape of the TYPE; for example:
5963 struct S { int a; int b; };
5964 struct S a[] = { 1, 2, 3, 4 };
5966 Here INIT will hold a vector of four elements, rather than a
5967 vector of two elements, each itself a vector of two elements. This
5968 routine transforms INIT from the former form into the latter. The
5969 revised CONSTRUCTOR node is returned. */
5971 tree
5972 reshape_init (tree type, tree init, tsubst_flags_t complain)
5974 vec<constructor_elt, va_gc> *v;
5975 reshape_iter d;
5976 tree new_init;
5978 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
5980 v = CONSTRUCTOR_ELTS (init);
5982 /* An empty constructor does not need reshaping, and it is always a valid
5983 initializer. */
5984 if (vec_safe_is_empty (v))
5985 return init;
5987 /* Handle [dcl.init.list] direct-list-initialization from
5988 single element of enumeration with a fixed underlying type. */
5989 if (is_direct_enum_init (type, init))
5991 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
5992 if (check_narrowing (ENUM_UNDERLYING_TYPE (type), elt, complain))
5993 return cp_build_c_cast (type, elt, tf_warning_or_error);
5994 else
5995 return error_mark_node;
5998 /* Recurse on this CONSTRUCTOR. */
5999 d.cur = &(*v)[0];
6000 d.end = d.cur + v->length ();
6002 new_init = reshape_init_r (type, &d, true, complain);
6003 if (new_init == error_mark_node)
6004 return error_mark_node;
6006 /* Make sure all the element of the constructor were used. Otherwise,
6007 issue an error about exceeding initializers. */
6008 if (d.cur != d.end)
6010 if (complain & tf_error)
6011 error ("too many initializers for %qT", type);
6012 else
6013 return error_mark_node;
6016 return new_init;
6019 /* Verify array initializer. Returns true if errors have been reported. */
6021 bool
6022 check_array_initializer (tree decl, tree type, tree init)
6024 tree element_type = TREE_TYPE (type);
6026 /* The array type itself need not be complete, because the
6027 initializer may tell us how many elements are in the array.
6028 But, the elements of the array must be complete. */
6029 if (!COMPLETE_TYPE_P (complete_type (element_type)))
6031 if (decl)
6032 error_at (DECL_SOURCE_LOCATION (decl),
6033 "elements of array %q#D have incomplete type", decl);
6034 else
6035 error ("elements of array %q#T have incomplete type", type);
6036 return true;
6038 /* A compound literal can't have variable size. */
6039 if (init && !decl
6040 && ((COMPLETE_TYPE_P (type) && !TREE_CONSTANT (TYPE_SIZE (type)))
6041 || !TREE_CONSTANT (TYPE_SIZE (element_type))))
6043 error ("variable-sized compound literal");
6044 return true;
6046 return false;
6049 /* Subroutine of check_initializer; args are passed down from that function.
6050 Set stmts_are_full_exprs_p to 1 across a call to build_aggr_init. */
6052 static tree
6053 build_aggr_init_full_exprs (tree decl, tree init, int flags)
6056 gcc_assert (stmts_are_full_exprs_p ());
6057 return build_aggr_init (decl, init, flags, tf_warning_or_error);
6060 /* Verify INIT (the initializer for DECL), and record the
6061 initialization in DECL_INITIAL, if appropriate. CLEANUP is as for
6062 grok_reference_init.
6064 If the return value is non-NULL, it is an expression that must be
6065 evaluated dynamically to initialize DECL. */
6067 static tree
6068 check_initializer (tree decl, tree init, int flags, vec<tree, va_gc> **cleanups)
6070 tree type = TREE_TYPE (decl);
6071 tree init_code = NULL;
6072 tree core_type;
6074 /* Things that are going to be initialized need to have complete
6075 type. */
6076 TREE_TYPE (decl) = type = complete_type (TREE_TYPE (decl));
6078 if (DECL_HAS_VALUE_EXPR_P (decl))
6080 /* A variable with DECL_HAS_VALUE_EXPR_P set is just a placeholder,
6081 it doesn't have storage to be initialized. */
6082 gcc_assert (init == NULL_TREE);
6083 return NULL_TREE;
6086 if (type == error_mark_node)
6087 /* We will have already complained. */
6088 return NULL_TREE;
6090 if (TREE_CODE (type) == ARRAY_TYPE)
6092 if (check_array_initializer (decl, type, init))
6093 return NULL_TREE;
6095 else if (!COMPLETE_TYPE_P (type))
6097 error_at (DECL_SOURCE_LOCATION (decl),
6098 "%q#D has incomplete type", decl);
6099 TREE_TYPE (decl) = error_mark_node;
6100 return NULL_TREE;
6102 else
6103 /* There is no way to make a variable-sized class type in GNU C++. */
6104 gcc_assert (TREE_CONSTANT (TYPE_SIZE (type)));
6106 if (init && BRACE_ENCLOSED_INITIALIZER_P (init))
6108 int init_len = CONSTRUCTOR_NELTS (init);
6109 if (SCALAR_TYPE_P (type))
6111 if (init_len == 0)
6113 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6114 init = build_zero_init (type, NULL_TREE, false);
6116 else if (init_len != 1 && TREE_CODE (type) != COMPLEX_TYPE)
6118 error_at (EXPR_LOC_OR_LOC (init, DECL_SOURCE_LOCATION (decl)),
6119 "scalar object %qD requires one element in "
6120 "initializer", decl);
6121 TREE_TYPE (decl) = error_mark_node;
6122 return NULL_TREE;
6127 if (TREE_CODE (decl) == CONST_DECL)
6129 gcc_assert (TREE_CODE (type) != REFERENCE_TYPE);
6131 DECL_INITIAL (decl) = init;
6133 gcc_assert (init != NULL_TREE);
6134 init = NULL_TREE;
6136 else if (!init && DECL_REALLY_EXTERN (decl))
6138 else if (init || type_build_ctor_call (type)
6139 || TREE_CODE (type) == REFERENCE_TYPE)
6141 if (TREE_CODE (type) == REFERENCE_TYPE)
6143 init = grok_reference_init (decl, type, init, flags);
6144 flags |= LOOKUP_ALREADY_DIGESTED;
6146 else if (!init)
6147 check_for_uninitialized_const_var (decl);
6148 /* Do not reshape constructors of vectors (they don't need to be
6149 reshaped. */
6150 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
6152 if (is_std_init_list (type))
6154 init = perform_implicit_conversion (type, init,
6155 tf_warning_or_error);
6156 flags |= LOOKUP_ALREADY_DIGESTED;
6158 else if (TYPE_NON_AGGREGATE_CLASS (type))
6160 /* Don't reshape if the class has constructors. */
6161 if (cxx_dialect == cxx98)
6162 error_at (EXPR_LOC_OR_LOC (init, DECL_SOURCE_LOCATION (decl)),
6163 "in C++98 %qD must be initialized by "
6164 "constructor, not by %<{...}%>",
6165 decl);
6167 else if (VECTOR_TYPE_P (type) && TYPE_VECTOR_OPAQUE (type))
6169 error ("opaque vector types cannot be initialized");
6170 init = error_mark_node;
6172 else
6174 init = reshape_init (type, init, tf_warning_or_error);
6175 flags |= LOOKUP_NO_NARROWING;
6178 else if (TREE_CODE (init) == TREE_LIST
6179 && TREE_TYPE (init) != unknown_type_node
6180 && !MAYBE_CLASS_TYPE_P (type))
6182 gcc_assert (TREE_CODE (decl) != RESULT_DECL);
6184 /* We get here with code like `int a (2);' */
6185 init = build_x_compound_expr_from_list (init, ELK_INIT,
6186 tf_warning_or_error);
6189 /* If DECL has an array type without a specific bound, deduce the
6190 array size from the initializer. */
6191 maybe_deduce_size_from_array_init (decl, init);
6192 type = TREE_TYPE (decl);
6193 if (type == error_mark_node)
6194 return NULL_TREE;
6196 if ((type_build_ctor_call (type) || CLASS_TYPE_P (type))
6197 && !(flags & LOOKUP_ALREADY_DIGESTED)
6198 && !(init && BRACE_ENCLOSED_INITIALIZER_P (init)
6199 && CP_AGGREGATE_TYPE_P (type)
6200 && (CLASS_TYPE_P (type)
6201 || !TYPE_NEEDS_CONSTRUCTING (type)
6202 || type_has_extended_temps (type))))
6204 init_code = build_aggr_init_full_exprs (decl, init, flags);
6206 /* A constructor call is a non-trivial initializer even if
6207 it isn't explicitly written. */
6208 if (TREE_SIDE_EFFECTS (init_code))
6209 DECL_NONTRIVIALLY_INITIALIZED_P (decl) = true;
6211 /* If this is a constexpr initializer, expand_default_init will
6212 have returned an INIT_EXPR rather than a CALL_EXPR. In that
6213 case, pull the initializer back out and pass it down into
6214 store_init_value. */
6215 while (TREE_CODE (init_code) == EXPR_STMT
6216 || TREE_CODE (init_code) == CONVERT_EXPR)
6217 init_code = TREE_OPERAND (init_code, 0);
6218 if (TREE_CODE (init_code) == INIT_EXPR)
6220 init = TREE_OPERAND (init_code, 1);
6221 init_code = NULL_TREE;
6222 /* Don't call digest_init; it's unnecessary and will complain
6223 about aggregate initialization of non-aggregate classes. */
6224 flags |= LOOKUP_ALREADY_DIGESTED;
6226 else if (DECL_DECLARED_CONSTEXPR_P (decl))
6228 /* Declared constexpr, but no suitable initializer; massage
6229 init appropriately so we can pass it into store_init_value
6230 for the error. */
6231 if (CLASS_TYPE_P (type)
6232 && (!init || TREE_CODE (init) == TREE_LIST))
6234 init = build_functional_cast (type, init, tf_none);
6235 if (TREE_CODE (init) == TARGET_EXPR)
6236 TARGET_EXPR_DIRECT_INIT_P (init) = true;
6238 init_code = NULL_TREE;
6240 else
6241 init = NULL_TREE;
6244 if (init && TREE_CODE (init) != TREE_VEC)
6246 /* In aggregate initialization of a variable, each element
6247 initialization is a full-expression because there is no
6248 enclosing expression. */
6249 gcc_assert (stmts_are_full_exprs_p ());
6251 init_code = store_init_value (decl, init, cleanups, flags);
6253 if (pedantic && TREE_CODE (type) == ARRAY_TYPE
6254 && DECL_INITIAL (decl)
6255 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
6256 && PAREN_STRING_LITERAL_P (DECL_INITIAL (decl)))
6257 warning_at (EXPR_LOC_OR_LOC (DECL_INITIAL (decl),
6258 DECL_SOURCE_LOCATION (decl)),
6259 0, "array %qD initialized by parenthesized "
6260 "string literal %qE",
6261 decl, DECL_INITIAL (decl));
6262 init = NULL;
6265 else
6267 if (CLASS_TYPE_P (core_type = strip_array_types (type))
6268 && (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type)
6269 || CLASSTYPE_REF_FIELDS_NEED_INIT (core_type)))
6270 diagnose_uninitialized_cst_or_ref_member (core_type, /*using_new=*/false,
6271 /*complain=*/true);
6273 check_for_uninitialized_const_var (decl);
6276 if (init && init != error_mark_node)
6277 init_code = build2 (INIT_EXPR, type, decl, init);
6279 if (init_code)
6281 /* We might have set these in cp_finish_decl. */
6282 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = false;
6283 TREE_CONSTANT (decl) = false;
6286 if (init_code && DECL_IN_AGGR_P (decl))
6288 static int explained = 0;
6290 if (cxx_dialect < cxx11)
6291 error ("initializer invalid for static member with constructor");
6292 else
6293 error ("non-constant in-class initialization invalid for static "
6294 "member %qD", decl);
6295 if (!explained)
6297 inform (input_location,
6298 "(an out of class initialization is required)");
6299 explained = 1;
6301 return NULL_TREE;
6304 return init_code;
6307 /* If DECL is not a local variable, give it RTL. */
6309 static void
6310 make_rtl_for_nonlocal_decl (tree decl, tree init, const char* asmspec)
6312 int toplev = toplevel_bindings_p ();
6313 int defer_p;
6315 /* Set the DECL_ASSEMBLER_NAME for the object. */
6316 if (asmspec)
6318 /* The `register' keyword, when used together with an
6319 asm-specification, indicates that the variable should be
6320 placed in a particular register. */
6321 if (VAR_P (decl) && DECL_REGISTER (decl))
6323 set_user_assembler_name (decl, asmspec);
6324 DECL_HARD_REGISTER (decl) = 1;
6326 else
6328 if (TREE_CODE (decl) == FUNCTION_DECL
6329 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
6330 set_builtin_user_assembler_name (decl, asmspec);
6331 set_user_assembler_name (decl, asmspec);
6335 /* Handle non-variables up front. */
6336 if (!VAR_P (decl))
6338 rest_of_decl_compilation (decl, toplev, at_eof);
6339 return;
6342 /* If we see a class member here, it should be a static data
6343 member. */
6344 if (DECL_LANG_SPECIFIC (decl) && DECL_IN_AGGR_P (decl))
6346 gcc_assert (TREE_STATIC (decl));
6347 /* An in-class declaration of a static data member should be
6348 external; it is only a declaration, and not a definition. */
6349 if (init == NULL_TREE)
6350 gcc_assert (DECL_EXTERNAL (decl) || !TREE_PUBLIC (decl));
6353 /* We don't create any RTL for local variables. */
6354 if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
6355 return;
6357 /* We defer emission of local statics until the corresponding
6358 DECL_EXPR is expanded. But with constexpr its function might never
6359 be expanded, so go ahead and tell cgraph about the variable now. */
6360 defer_p = ((DECL_FUNCTION_SCOPE_P (decl)
6361 && !var_in_maybe_constexpr_fn (decl))
6362 || DECL_VIRTUAL_P (decl));
6364 /* Defer template instantiations. */
6365 if (DECL_LANG_SPECIFIC (decl)
6366 && DECL_IMPLICIT_INSTANTIATION (decl))
6367 defer_p = 1;
6369 /* If we're not deferring, go ahead and assemble the variable. */
6370 if (!defer_p)
6371 rest_of_decl_compilation (decl, toplev, at_eof);
6374 /* walk_tree helper for wrap_temporary_cleanups, below. */
6376 static tree
6377 wrap_cleanups_r (tree *stmt_p, int *walk_subtrees, void *data)
6379 /* Stop at types or full-expression boundaries. */
6380 if (TYPE_P (*stmt_p)
6381 || TREE_CODE (*stmt_p) == CLEANUP_POINT_EXPR)
6383 *walk_subtrees = 0;
6384 return NULL_TREE;
6387 if (TREE_CODE (*stmt_p) == TARGET_EXPR)
6389 tree guard = (tree)data;
6390 tree tcleanup = TARGET_EXPR_CLEANUP (*stmt_p);
6392 tcleanup = build2 (TRY_CATCH_EXPR, void_type_node, tcleanup, guard);
6393 /* Tell honor_protect_cleanup_actions to handle this as a separate
6394 cleanup. */
6395 TRY_CATCH_IS_CLEANUP (tcleanup) = 1;
6397 TARGET_EXPR_CLEANUP (*stmt_p) = tcleanup;
6400 return NULL_TREE;
6403 /* We're initializing a local variable which has a cleanup GUARD. If there
6404 are any temporaries used in the initializer INIT of this variable, we
6405 need to wrap their cleanups with TRY_CATCH_EXPR (, GUARD) so that the
6406 variable will be cleaned up properly if one of them throws.
6408 Unfortunately, there's no way to express this properly in terms of
6409 nesting, as the regions for the temporaries overlap the region for the
6410 variable itself; if there are two temporaries, the variable needs to be
6411 the first thing destroyed if either of them throws. However, we only
6412 want to run the variable's cleanup if it actually got constructed. So
6413 we need to guard the temporary cleanups with the variable's cleanup if
6414 they are run on the normal path, but not if they are run on the
6415 exceptional path. We implement this by telling
6416 honor_protect_cleanup_actions to strip the variable cleanup from the
6417 exceptional path. */
6419 static void
6420 wrap_temporary_cleanups (tree init, tree guard)
6422 cp_walk_tree_without_duplicates (&init, wrap_cleanups_r, (void *)guard);
6425 /* Generate code to initialize DECL (a local variable). */
6427 static void
6428 initialize_local_var (tree decl, tree init)
6430 tree type = TREE_TYPE (decl);
6431 tree cleanup;
6432 int already_used;
6434 gcc_assert (VAR_P (decl)
6435 || TREE_CODE (decl) == RESULT_DECL);
6436 gcc_assert (!TREE_STATIC (decl));
6438 if (DECL_SIZE (decl) == NULL_TREE)
6440 /* If we used it already as memory, it must stay in memory. */
6441 DECL_INITIAL (decl) = NULL_TREE;
6442 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
6443 return;
6446 if (type == error_mark_node)
6447 return;
6449 /* Compute and store the initial value. */
6450 already_used = TREE_USED (decl) || TREE_USED (type);
6451 if (TREE_USED (type))
6452 DECL_READ_P (decl) = 1;
6454 /* Generate a cleanup, if necessary. */
6455 cleanup = cxx_maybe_build_cleanup (decl, tf_warning_or_error);
6457 /* Perform the initialization. */
6458 if (init)
6460 tree rinit = (TREE_CODE (init) == INIT_EXPR
6461 ? TREE_OPERAND (init, 1) : NULL_TREE);
6462 if (rinit && !TREE_SIDE_EFFECTS (rinit))
6464 /* Stick simple initializers in DECL_INITIAL so that
6465 -Wno-init-self works (c++/34772). */
6466 gcc_assert (TREE_OPERAND (init, 0) == decl);
6467 DECL_INITIAL (decl) = rinit;
6469 if (warn_init_self && TREE_CODE (type) == REFERENCE_TYPE)
6471 STRIP_NOPS (rinit);
6472 if (rinit == decl)
6473 warning_at (DECL_SOURCE_LOCATION (decl),
6474 OPT_Winit_self,
6475 "reference %qD is initialized with itself", decl);
6478 else
6480 int saved_stmts_are_full_exprs_p;
6482 /* If we're only initializing a single object, guard the
6483 destructors of any temporaries used in its initializer with
6484 its destructor. This isn't right for arrays because each
6485 element initialization is a full-expression. */
6486 if (cleanup && TREE_CODE (type) != ARRAY_TYPE)
6487 wrap_temporary_cleanups (init, cleanup);
6489 gcc_assert (building_stmt_list_p ());
6490 saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
6491 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
6492 finish_expr_stmt (init);
6493 current_stmt_tree ()->stmts_are_full_exprs_p =
6494 saved_stmts_are_full_exprs_p;
6498 /* Set this to 0 so we can tell whether an aggregate which was
6499 initialized was ever used. Don't do this if it has a
6500 destructor, so we don't complain about the 'resource
6501 allocation is initialization' idiom. Now set
6502 attribute((unused)) on types so decls of that type will be
6503 marked used. (see TREE_USED, above.) */
6504 if (TYPE_NEEDS_CONSTRUCTING (type)
6505 && ! already_used
6506 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type)
6507 && DECL_NAME (decl))
6508 TREE_USED (decl) = 0;
6509 else if (already_used)
6510 TREE_USED (decl) = 1;
6512 if (cleanup)
6513 finish_decl_cleanup (decl, cleanup);
6516 /* DECL is a VAR_DECL for a compiler-generated variable with static
6517 storage duration (like a virtual table) whose initializer is a
6518 compile-time constant. Initialize the variable and provide it to the
6519 back end. */
6521 void
6522 initialize_artificial_var (tree decl, vec<constructor_elt, va_gc> *v)
6524 tree init;
6525 gcc_assert (DECL_ARTIFICIAL (decl));
6526 init = build_constructor (TREE_TYPE (decl), v);
6527 gcc_assert (TREE_CODE (init) == CONSTRUCTOR);
6528 DECL_INITIAL (decl) = init;
6529 DECL_INITIALIZED_P (decl) = 1;
6530 determine_visibility (decl);
6531 layout_var_decl (decl);
6532 maybe_commonize_var (decl);
6533 make_rtl_for_nonlocal_decl (decl, init, /*asmspec=*/NULL);
6536 /* INIT is the initializer for a variable, as represented by the
6537 parser. Returns true iff INIT is type-dependent. */
6539 static bool
6540 type_dependent_init_p (tree init)
6542 if (TREE_CODE (init) == TREE_LIST)
6543 /* A parenthesized initializer, e.g.: int i (3, 2); ? */
6544 return any_type_dependent_elements_p (init);
6545 else if (TREE_CODE (init) == CONSTRUCTOR)
6546 /* A brace-enclosed initializer, e.g.: int i = { 3 }; ? */
6548 vec<constructor_elt, va_gc> *elts;
6549 size_t nelts;
6550 size_t i;
6552 elts = CONSTRUCTOR_ELTS (init);
6553 nelts = vec_safe_length (elts);
6554 for (i = 0; i < nelts; ++i)
6555 if (type_dependent_init_p ((*elts)[i].value))
6556 return true;
6558 else
6559 /* It must be a simple expression, e.g., int i = 3; */
6560 return type_dependent_expression_p (init);
6562 return false;
6565 /* INIT is the initializer for a variable, as represented by the
6566 parser. Returns true iff INIT is value-dependent. */
6568 static bool
6569 value_dependent_init_p (tree init)
6571 if (TREE_CODE (init) == TREE_LIST)
6572 /* A parenthesized initializer, e.g.: int i (3, 2); ? */
6573 return any_value_dependent_elements_p (init);
6574 else if (TREE_CODE (init) == CONSTRUCTOR)
6575 /* A brace-enclosed initializer, e.g.: int i = { 3 }; ? */
6577 vec<constructor_elt, va_gc> *elts;
6578 size_t nelts;
6579 size_t i;
6581 elts = CONSTRUCTOR_ELTS (init);
6582 nelts = vec_safe_length (elts);
6583 for (i = 0; i < nelts; ++i)
6584 if (value_dependent_init_p ((*elts)[i].value))
6585 return true;
6587 else
6588 /* It must be a simple expression, e.g., int i = 3; */
6589 return value_dependent_expression_p (init);
6591 return false;
6594 // Returns true if a DECL is VAR_DECL with the concept specifier.
6595 static inline bool
6596 is_concept_var (tree decl)
6598 return (VAR_P (decl)
6599 // Not all variables have DECL_LANG_SPECIFIC.
6600 && DECL_LANG_SPECIFIC (decl)
6601 && DECL_DECLARED_CONCEPT_P (decl));
6604 /* A helper function to be called via walk_tree. If any label exists
6605 under *TP, it is (going to be) forced. Set has_forced_label_in_static. */
6607 static tree
6608 notice_forced_label_r (tree *tp, int *walk_subtrees, void *)
6610 if (TYPE_P (*tp))
6611 *walk_subtrees = 0;
6612 if (TREE_CODE (*tp) == LABEL_DECL)
6613 cfun->has_forced_label_in_static = 1;
6614 return NULL_TREE;
6617 /* Finish processing of a declaration;
6618 install its line number and initial value.
6619 If the length of an array type is not known before,
6620 it must be determined now, from the initial value, or it is an error.
6622 INIT is the initializer (if any) for DECL. If INIT_CONST_EXPR_P is
6623 true, then INIT is an integral constant expression.
6625 FLAGS is LOOKUP_ONLYCONVERTING if the = init syntax was used, else 0
6626 if the (init) syntax was used. */
6628 void
6629 cp_finish_decl (tree decl, tree init, bool init_const_expr_p,
6630 tree asmspec_tree, int flags)
6632 tree type;
6633 vec<tree, va_gc> *cleanups = NULL;
6634 const char *asmspec = NULL;
6635 int was_readonly = 0;
6636 bool var_definition_p = false;
6637 tree auto_node;
6639 if (decl == error_mark_node)
6640 return;
6641 else if (! decl)
6643 if (init)
6644 error ("assignment (not initialization) in declaration");
6645 return;
6648 gcc_assert (TREE_CODE (decl) != RESULT_DECL);
6649 /* Parameters are handled by store_parm_decls, not cp_finish_decl. */
6650 gcc_assert (TREE_CODE (decl) != PARM_DECL);
6652 type = TREE_TYPE (decl);
6653 if (type == error_mark_node)
6654 return;
6656 /* Warn about register storage specifiers except when in GNU global
6657 or local register variable extension. */
6658 if (VAR_P (decl) && DECL_REGISTER (decl) && asmspec_tree == NULL_TREE)
6660 if (cxx_dialect >= cxx1z)
6661 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
6662 "ISO C++1z does not allow %<register%> storage "
6663 "class specifier");
6664 else
6665 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
6666 "%<register%> storage class specifier used");
6669 /* If a name was specified, get the string. */
6670 if (at_namespace_scope_p ())
6671 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
6672 if (asmspec_tree && asmspec_tree != error_mark_node)
6673 asmspec = TREE_STRING_POINTER (asmspec_tree);
6675 if (current_class_type
6676 && CP_DECL_CONTEXT (decl) == current_class_type
6677 && TYPE_BEING_DEFINED (current_class_type)
6678 && !CLASSTYPE_TEMPLATE_INSTANTIATION (current_class_type)
6679 && (DECL_INITIAL (decl) || init))
6680 DECL_INITIALIZED_IN_CLASS_P (decl) = 1;
6682 if (TREE_CODE (decl) != FUNCTION_DECL
6683 && (auto_node = type_uses_auto (type)))
6685 tree d_init;
6686 if (init == NULL_TREE)
6688 if (DECL_LANG_SPECIFIC (decl)
6689 && DECL_TEMPLATE_INSTANTIATION (decl)
6690 && !DECL_TEMPLATE_INSTANTIATED (decl))
6692 /* init is null because we're deferring instantiating the
6693 initializer until we need it. Well, we need it now. */
6694 instantiate_decl (decl, /*defer_ok*/true, /*expl*/false);
6695 return;
6698 error ("declaration of %q#D has no initializer", decl);
6699 TREE_TYPE (decl) = error_mark_node;
6700 return;
6702 d_init = init;
6703 if (TREE_CODE (d_init) == TREE_LIST)
6704 d_init = build_x_compound_expr_from_list (d_init, ELK_INIT,
6705 tf_warning_or_error);
6706 d_init = resolve_nondeduced_context (d_init, tf_warning_or_error);
6707 type = TREE_TYPE (decl) = do_auto_deduction (type, d_init,
6708 auto_node,
6709 tf_warning_or_error,
6710 adc_variable_type);
6711 if (type == error_mark_node)
6712 return;
6713 if (TREE_CODE (type) == FUNCTION_TYPE)
6715 error ("initializer for %<decltype(auto) %D%> has function type "
6716 "(did you forget the %<()%> ?)", decl);
6717 TREE_TYPE (decl) = error_mark_node;
6718 return;
6720 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
6723 if (!ensure_literal_type_for_constexpr_object (decl))
6724 DECL_DECLARED_CONSTEXPR_P (decl) = 0;
6726 if (VAR_P (decl)
6727 && DECL_CLASS_SCOPE_P (decl)
6728 && DECL_INITIALIZED_IN_CLASS_P (decl))
6729 check_static_variable_definition (decl, type);
6731 if (init && TREE_CODE (decl) == FUNCTION_DECL)
6733 tree clone;
6734 if (init == ridpointers[(int)RID_DELETE])
6736 /* FIXME check this is 1st decl. */
6737 DECL_DELETED_FN (decl) = 1;
6738 DECL_DECLARED_INLINE_P (decl) = 1;
6739 DECL_INITIAL (decl) = error_mark_node;
6740 FOR_EACH_CLONE (clone, decl)
6742 DECL_DELETED_FN (clone) = 1;
6743 DECL_DECLARED_INLINE_P (clone) = 1;
6744 DECL_INITIAL (clone) = error_mark_node;
6746 init = NULL_TREE;
6748 else if (init == ridpointers[(int)RID_DEFAULT])
6750 if (defaultable_fn_check (decl))
6751 DECL_DEFAULTED_FN (decl) = 1;
6752 else
6753 DECL_INITIAL (decl) = NULL_TREE;
6757 if (init && VAR_P (decl))
6759 DECL_NONTRIVIALLY_INITIALIZED_P (decl) = 1;
6760 /* If DECL is a reference, then we want to know whether init is a
6761 reference constant; init_const_expr_p as passed tells us whether
6762 it's an rvalue constant. */
6763 if (TREE_CODE (type) == REFERENCE_TYPE)
6764 init_const_expr_p = potential_constant_expression (init);
6765 if (init_const_expr_p)
6767 /* Set these flags now for templates. We'll update the flags in
6768 store_init_value for instantiations. */
6769 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
6770 if (decl_maybe_constant_var_p (decl))
6771 TREE_CONSTANT (decl) = 1;
6775 if (processing_template_decl)
6777 bool type_dependent_p;
6779 /* Add this declaration to the statement-tree. */
6780 if (at_function_scope_p ())
6781 add_decl_expr (decl);
6783 type_dependent_p = dependent_type_p (type);
6785 if (check_for_bare_parameter_packs (init))
6787 init = NULL_TREE;
6788 DECL_INITIAL (decl) = NULL_TREE;
6791 /* Generally, initializers in templates are expanded when the
6792 template is instantiated. But, if DECL is a variable constant
6793 then it can be used in future constant expressions, so its value
6794 must be available. */
6796 if (!VAR_P (decl) || type_dependent_p)
6797 /* We can't do anything if the decl has dependent type. */;
6798 else if (init
6799 && init_const_expr_p
6800 && TREE_CODE (type) != REFERENCE_TYPE
6801 && decl_maybe_constant_var_p (decl)
6802 && !type_dependent_init_p (init)
6803 && !value_dependent_init_p (init))
6805 /* This variable seems to be a non-dependent constant, so process
6806 its initializer. If check_initializer returns non-null the
6807 initialization wasn't constant after all. */
6808 tree init_code;
6809 cleanups = make_tree_vector ();
6810 init_code = check_initializer (decl, init, flags, &cleanups);
6811 if (init_code == NULL_TREE)
6812 init = NULL_TREE;
6813 release_tree_vector (cleanups);
6815 else if (!init && is_concept_var (decl))
6816 error ("variable concept has no initializer");
6817 else if (!DECL_PRETTY_FUNCTION_P (decl))
6819 /* Deduce array size even if the initializer is dependent. */
6820 maybe_deduce_size_from_array_init (decl, init);
6821 /* And complain about multiple initializers. */
6822 if (init && TREE_CODE (init) == TREE_LIST && TREE_CHAIN (init)
6823 && !MAYBE_CLASS_TYPE_P (type))
6824 init = build_x_compound_expr_from_list (init, ELK_INIT,
6825 tf_warning_or_error);
6828 if (init)
6829 DECL_INITIAL (decl) = init;
6830 return;
6833 /* Just store non-static data member initializers for later. */
6834 if (init && TREE_CODE (decl) == FIELD_DECL)
6835 DECL_INITIAL (decl) = init;
6837 /* Take care of TYPE_DECLs up front. */
6838 if (TREE_CODE (decl) == TYPE_DECL)
6840 if (type != error_mark_node
6841 && MAYBE_CLASS_TYPE_P (type) && DECL_NAME (decl))
6843 if (TREE_TYPE (DECL_NAME (decl)) && TREE_TYPE (decl) != type)
6844 warning (0, "shadowing previous type declaration of %q#D", decl);
6845 set_identifier_type_value (DECL_NAME (decl), decl);
6848 /* If we have installed this as the canonical typedef for this
6849 type, and that type has not been defined yet, delay emitting
6850 the debug information for it, as we will emit it later. */
6851 if (TYPE_MAIN_DECL (TREE_TYPE (decl)) == decl
6852 && !COMPLETE_TYPE_P (TREE_TYPE (decl)))
6853 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
6855 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl),
6856 at_eof);
6857 return;
6860 /* A reference will be modified here, as it is initialized. */
6861 if (! DECL_EXTERNAL (decl)
6862 && TREE_READONLY (decl)
6863 && TREE_CODE (type) == REFERENCE_TYPE)
6865 was_readonly = 1;
6866 TREE_READONLY (decl) = 0;
6869 if (VAR_P (decl))
6871 /* If this is a local variable that will need a mangled name,
6872 register it now. We must do this before processing the
6873 initializer for the variable, since the initialization might
6874 require a guard variable, and since the mangled name of the
6875 guard variable will depend on the mangled name of this
6876 variable. */
6877 if (DECL_FUNCTION_SCOPE_P (decl)
6878 && TREE_STATIC (decl)
6879 && !DECL_ARTIFICIAL (decl))
6881 push_local_name (decl);
6882 /* Normally has_forced_label_in_static is set during GIMPLE
6883 lowering, but [cd]tors are never actually compiled directly.
6884 We need to set this early so we can deal with the label
6885 address extension. */
6886 if ((DECL_CONSTRUCTOR_P (current_function_decl)
6887 || DECL_DESTRUCTOR_P (current_function_decl))
6888 && init)
6890 walk_tree (&init, notice_forced_label_r, NULL, NULL);
6891 add_local_decl (cfun, decl);
6893 /* And make sure it's in the symbol table for
6894 c_parse_final_cleanups to find. */
6895 varpool_node::get_create (decl);
6898 /* Convert the initializer to the type of DECL, if we have not
6899 already initialized DECL. */
6900 if (!DECL_INITIALIZED_P (decl)
6901 /* If !DECL_EXTERNAL then DECL is being defined. In the
6902 case of a static data member initialized inside the
6903 class-specifier, there can be an initializer even if DECL
6904 is *not* defined. */
6905 && (!DECL_EXTERNAL (decl) || init))
6907 cleanups = make_tree_vector ();
6908 init = check_initializer (decl, init, flags, &cleanups);
6910 /* Handle:
6912 [dcl.init]
6914 The memory occupied by any object of static storage
6915 duration is zero-initialized at program startup before
6916 any other initialization takes place.
6918 We cannot create an appropriate initializer until after
6919 the type of DECL is finalized. If DECL_INITIAL is set,
6920 then the DECL is statically initialized, and any
6921 necessary zero-initialization has already been performed. */
6922 if (TREE_STATIC (decl) && !DECL_INITIAL (decl))
6923 DECL_INITIAL (decl) = build_zero_init (TREE_TYPE (decl),
6924 /*nelts=*/NULL_TREE,
6925 /*static_storage_p=*/true);
6926 /* Remember that the initialization for this variable has
6927 taken place. */
6928 DECL_INITIALIZED_P (decl) = 1;
6929 /* This declaration is the definition of this variable,
6930 unless we are initializing a static data member within
6931 the class specifier. */
6932 if (!DECL_EXTERNAL (decl))
6933 var_definition_p = true;
6935 /* If the variable has an array type, lay out the type, even if
6936 there is no initializer. It is valid to index through the
6937 array, and we must get TYPE_ALIGN set correctly on the array
6938 type. */
6939 else if (TREE_CODE (type) == ARRAY_TYPE)
6940 layout_type (type);
6942 if (TREE_STATIC (decl)
6943 && !at_function_scope_p ()
6944 && current_function_decl == NULL)
6945 /* So decl is a global variable or a static member of a
6946 non local class. Record the types it uses
6947 so that we can decide later to emit debug info for them. */
6948 record_types_used_by_current_var_decl (decl);
6951 /* Add this declaration to the statement-tree. This needs to happen
6952 after the call to check_initializer so that the DECL_EXPR for a
6953 reference temp is added before the DECL_EXPR for the reference itself. */
6954 if (DECL_FUNCTION_SCOPE_P (decl))
6956 /* If we're building a variable sized type, and we might be
6957 reachable other than via the top of the current binding
6958 level, then create a new BIND_EXPR so that we deallocate
6959 the object at the right time. */
6960 if (VAR_P (decl)
6961 && DECL_SIZE (decl)
6962 && !TREE_CONSTANT (DECL_SIZE (decl))
6963 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
6965 tree bind;
6966 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
6967 TREE_SIDE_EFFECTS (bind) = 1;
6968 add_stmt (bind);
6969 BIND_EXPR_BODY (bind) = push_stmt_list ();
6971 add_decl_expr (decl);
6974 /* Let the middle end know about variables and functions -- but not
6975 static data members in uninstantiated class templates. */
6976 if (VAR_OR_FUNCTION_DECL_P (decl))
6978 if (VAR_P (decl))
6980 layout_var_decl (decl);
6981 maybe_commonize_var (decl);
6984 /* This needs to happen after the linkage is set. */
6985 determine_visibility (decl);
6987 if (var_definition_p && TREE_STATIC (decl))
6989 /* If a TREE_READONLY variable needs initialization
6990 at runtime, it is no longer readonly and we need to
6991 avoid MEM_READONLY_P being set on RTL created for it. */
6992 if (init)
6994 if (TREE_READONLY (decl))
6995 TREE_READONLY (decl) = 0;
6996 was_readonly = 0;
6998 else if (was_readonly)
6999 TREE_READONLY (decl) = 1;
7001 /* Likewise if it needs destruction. */
7002 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
7003 TREE_READONLY (decl) = 0;
7006 make_rtl_for_nonlocal_decl (decl, init, asmspec);
7008 /* Check for abstractness of the type. Notice that there is no
7009 need to strip array types here since the check for those types
7010 is already done within create_array_type_for_decl. */
7011 abstract_virtuals_error (decl, type);
7013 if (TREE_TYPE (decl) == error_mark_node)
7014 /* No initialization required. */
7016 else if (TREE_CODE (decl) == FUNCTION_DECL)
7018 if (init)
7020 if (init == ridpointers[(int)RID_DEFAULT])
7022 /* An out-of-class default definition is defined at
7023 the point where it is explicitly defaulted. */
7024 if (DECL_DELETED_FN (decl))
7025 maybe_explain_implicit_delete (decl);
7026 else if (DECL_INITIAL (decl) == error_mark_node)
7027 synthesize_method (decl);
7029 else
7030 error ("function %q#D is initialized like a variable", decl);
7032 /* else no initialization required. */
7034 else if (DECL_EXTERNAL (decl)
7035 && ! (DECL_LANG_SPECIFIC (decl)
7036 && DECL_NOT_REALLY_EXTERN (decl)))
7038 if (init)
7039 DECL_INITIAL (decl) = init;
7041 /* A variable definition. */
7042 else if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
7043 /* Initialize the local variable. */
7044 initialize_local_var (decl, init);
7046 /* If a variable is defined, and then a subsequent
7047 definition with external linkage is encountered, we will
7048 get here twice for the same variable. We want to avoid
7049 calling expand_static_init more than once. For variables
7050 that are not static data members, we can call
7051 expand_static_init only when we actually process the
7052 initializer. It is not legal to redeclare a static data
7053 member, so this issue does not arise in that case. */
7054 else if (var_definition_p && TREE_STATIC (decl))
7055 expand_static_init (decl, init);
7058 /* If a CLEANUP_STMT was created to destroy a temporary bound to a
7059 reference, insert it in the statement-tree now. */
7060 if (cleanups)
7062 unsigned i; tree t;
7063 FOR_EACH_VEC_ELT (*cleanups, i, t)
7064 push_cleanup (decl, t, false);
7065 release_tree_vector (cleanups);
7068 if (was_readonly)
7069 TREE_READONLY (decl) = 1;
7071 invoke_plugin_callbacks (PLUGIN_FINISH_DECL, decl);
7074 /* Returns a declaration for a VAR_DECL as if:
7076 extern "C" TYPE NAME;
7078 had been seen. Used to create compiler-generated global
7079 variables. */
7081 static tree
7082 declare_global_var (tree name, tree type)
7084 tree decl;
7086 push_to_top_level ();
7087 decl = build_decl (input_location, VAR_DECL, name, type);
7088 TREE_PUBLIC (decl) = 1;
7089 DECL_EXTERNAL (decl) = 1;
7090 DECL_ARTIFICIAL (decl) = 1;
7091 /* If the user has explicitly declared this variable (perhaps
7092 because the code we are compiling is part of a low-level runtime
7093 library), then it is possible that our declaration will be merged
7094 with theirs by pushdecl. */
7095 decl = pushdecl (decl);
7096 cp_finish_decl (decl, NULL_TREE, false, NULL_TREE, 0);
7097 pop_from_top_level ();
7099 return decl;
7102 /* Returns the type for the argument to "__cxa_atexit" (or "atexit",
7103 if "__cxa_atexit" is not being used) corresponding to the function
7104 to be called when the program exits. */
7106 static tree
7107 get_atexit_fn_ptr_type (void)
7109 tree fn_type;
7111 if (!atexit_fn_ptr_type_node)
7113 tree arg_type;
7114 if (flag_use_cxa_atexit
7115 && !targetm.cxx.use_atexit_for_cxa_atexit ())
7116 /* The parameter to "__cxa_atexit" is "void (*)(void *)". */
7117 arg_type = ptr_type_node;
7118 else
7119 /* The parameter to "atexit" is "void (*)(void)". */
7120 arg_type = NULL_TREE;
7122 fn_type = build_function_type_list (void_type_node,
7123 arg_type, NULL_TREE);
7124 atexit_fn_ptr_type_node = build_pointer_type (fn_type);
7127 return atexit_fn_ptr_type_node;
7130 /* Returns a pointer to the `atexit' function. Note that if
7131 FLAG_USE_CXA_ATEXIT is nonzero, then this will actually be the new
7132 `__cxa_atexit' function specified in the IA64 C++ ABI. */
7134 static tree
7135 get_atexit_node (void)
7137 tree atexit_fndecl;
7138 tree fn_type;
7139 tree fn_ptr_type;
7140 const char *name;
7141 bool use_aeabi_atexit;
7143 if (atexit_node)
7144 return atexit_node;
7146 if (flag_use_cxa_atexit && !targetm.cxx.use_atexit_for_cxa_atexit ())
7148 /* The declaration for `__cxa_atexit' is:
7150 int __cxa_atexit (void (*)(void *), void *, void *)
7152 We build up the argument types and then the function type
7153 itself. */
7154 tree argtype0, argtype1, argtype2;
7156 use_aeabi_atexit = targetm.cxx.use_aeabi_atexit ();
7157 /* First, build the pointer-to-function type for the first
7158 argument. */
7159 fn_ptr_type = get_atexit_fn_ptr_type ();
7160 /* Then, build the rest of the argument types. */
7161 argtype2 = ptr_type_node;
7162 if (use_aeabi_atexit)
7164 argtype1 = fn_ptr_type;
7165 argtype0 = ptr_type_node;
7167 else
7169 argtype1 = ptr_type_node;
7170 argtype0 = fn_ptr_type;
7172 /* And the final __cxa_atexit type. */
7173 fn_type = build_function_type_list (integer_type_node,
7174 argtype0, argtype1, argtype2,
7175 NULL_TREE);
7176 if (use_aeabi_atexit)
7177 name = "__aeabi_atexit";
7178 else
7179 name = "__cxa_atexit";
7181 else
7183 /* The declaration for `atexit' is:
7185 int atexit (void (*)());
7187 We build up the argument types and then the function type
7188 itself. */
7189 fn_ptr_type = get_atexit_fn_ptr_type ();
7190 /* Build the final atexit type. */
7191 fn_type = build_function_type_list (integer_type_node,
7192 fn_ptr_type, NULL_TREE);
7193 name = "atexit";
7196 /* Now, build the function declaration. */
7197 push_lang_context (lang_name_c);
7198 atexit_fndecl = build_library_fn_ptr (name, fn_type, ECF_LEAF | ECF_NOTHROW);
7199 mark_used (atexit_fndecl);
7200 pop_lang_context ();
7201 atexit_node = decay_conversion (atexit_fndecl, tf_warning_or_error);
7203 return atexit_node;
7206 /* Like get_atexit_node, but for thread-local cleanups. */
7208 static tree
7209 get_thread_atexit_node (void)
7211 /* The declaration for `__cxa_thread_atexit' is:
7213 int __cxa_thread_atexit (void (*)(void *), void *, void *) */
7214 tree fn_type = build_function_type_list (integer_type_node,
7215 get_atexit_fn_ptr_type (),
7216 ptr_type_node, ptr_type_node,
7217 NULL_TREE);
7219 /* Now, build the function declaration. */
7220 tree atexit_fndecl = build_library_fn_ptr ("__cxa_thread_atexit", fn_type,
7221 ECF_LEAF | ECF_NOTHROW);
7222 return decay_conversion (atexit_fndecl, tf_warning_or_error);
7225 /* Returns the __dso_handle VAR_DECL. */
7227 static tree
7228 get_dso_handle_node (void)
7230 if (dso_handle_node)
7231 return dso_handle_node;
7233 /* Declare the variable. */
7234 dso_handle_node = declare_global_var (get_identifier ("__dso_handle"),
7235 ptr_type_node);
7237 #ifdef HAVE_GAS_HIDDEN
7238 if (dso_handle_node != error_mark_node)
7240 DECL_VISIBILITY (dso_handle_node) = VISIBILITY_HIDDEN;
7241 DECL_VISIBILITY_SPECIFIED (dso_handle_node) = 1;
7243 #endif
7245 return dso_handle_node;
7248 /* Begin a new function with internal linkage whose job will be simply
7249 to destroy some particular variable. */
7251 static GTY(()) int start_cleanup_cnt;
7253 static tree
7254 start_cleanup_fn (void)
7256 char name[32];
7257 tree fntype;
7258 tree fndecl;
7259 bool use_cxa_atexit = flag_use_cxa_atexit
7260 && !targetm.cxx.use_atexit_for_cxa_atexit ();
7262 push_to_top_level ();
7264 /* No need to mangle this. */
7265 push_lang_context (lang_name_c);
7267 /* Build the name of the function. */
7268 sprintf (name, "__tcf_%d", start_cleanup_cnt++);
7269 /* Build the function declaration. */
7270 fntype = TREE_TYPE (get_atexit_fn_ptr_type ());
7271 fndecl = build_lang_decl (FUNCTION_DECL, get_identifier (name), fntype);
7272 /* It's a function with internal linkage, generated by the
7273 compiler. */
7274 TREE_PUBLIC (fndecl) = 0;
7275 DECL_ARTIFICIAL (fndecl) = 1;
7276 /* Make the function `inline' so that it is only emitted if it is
7277 actually needed. It is unlikely that it will be inlined, since
7278 it is only called via a function pointer, but we avoid unnecessary
7279 emissions this way. */
7280 DECL_DECLARED_INLINE_P (fndecl) = 1;
7281 DECL_INTERFACE_KNOWN (fndecl) = 1;
7282 /* Build the parameter. */
7283 if (use_cxa_atexit)
7285 tree parmdecl;
7287 parmdecl = cp_build_parm_decl (NULL_TREE, ptr_type_node);
7288 DECL_CONTEXT (parmdecl) = fndecl;
7289 TREE_USED (parmdecl) = 1;
7290 DECL_READ_P (parmdecl) = 1;
7291 DECL_ARGUMENTS (fndecl) = parmdecl;
7294 pushdecl (fndecl);
7295 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
7297 pop_lang_context ();
7299 return current_function_decl;
7302 /* Finish the cleanup function begun by start_cleanup_fn. */
7304 static void
7305 end_cleanup_fn (void)
7307 expand_or_defer_fn (finish_function (0));
7309 pop_from_top_level ();
7312 /* Generate code to handle the destruction of DECL, an object with
7313 static storage duration. */
7315 tree
7316 register_dtor_fn (tree decl)
7318 tree cleanup;
7319 tree addr;
7320 tree compound_stmt;
7321 tree fcall;
7322 tree type;
7323 bool ob_parm, dso_parm, use_dtor;
7324 tree arg0, arg1, arg2;
7325 tree atex_node;
7327 type = TREE_TYPE (decl);
7328 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
7329 return void_node;
7331 /* If we're using "__cxa_atexit" (or "__cxa_thread_atexit" or
7332 "__aeabi_atexit"), and DECL is a class object, we can just pass the
7333 destructor to "__cxa_atexit"; we don't have to build a temporary
7334 function to do the cleanup. */
7335 dso_parm = (flag_use_cxa_atexit
7336 && !targetm.cxx.use_atexit_for_cxa_atexit ());
7337 ob_parm = (CP_DECL_THREAD_LOCAL_P (decl) || dso_parm);
7338 use_dtor = ob_parm && CLASS_TYPE_P (type);
7339 if (use_dtor)
7341 int idx;
7343 /* Find the destructor. */
7344 idx = lookup_fnfields_1 (type, complete_dtor_identifier);
7345 gcc_assert (idx >= 0);
7346 cleanup = (*CLASSTYPE_METHOD_VEC (type))[idx];
7347 /* Make sure it is accessible. */
7348 perform_or_defer_access_check (TYPE_BINFO (type), cleanup, cleanup,
7349 tf_warning_or_error);
7351 else
7353 /* Call build_cleanup before we enter the anonymous function so
7354 that any access checks will be done relative to the current
7355 scope, rather than the scope of the anonymous function. */
7356 build_cleanup (decl);
7358 /* Now start the function. */
7359 cleanup = start_cleanup_fn ();
7361 /* Now, recompute the cleanup. It may contain SAVE_EXPRs that refer
7362 to the original function, rather than the anonymous one. That
7363 will make the back end think that nested functions are in use,
7364 which causes confusion. */
7365 push_deferring_access_checks (dk_no_check);
7366 fcall = build_cleanup (decl);
7367 pop_deferring_access_checks ();
7369 /* Create the body of the anonymous function. */
7370 compound_stmt = begin_compound_stmt (BCS_FN_BODY);
7371 finish_expr_stmt (fcall);
7372 finish_compound_stmt (compound_stmt);
7373 end_cleanup_fn ();
7376 /* Call atexit with the cleanup function. */
7377 mark_used (cleanup);
7378 cleanup = build_address (cleanup);
7380 if (CP_DECL_THREAD_LOCAL_P (decl))
7381 atex_node = get_thread_atexit_node ();
7382 else
7383 atex_node = get_atexit_node ();
7385 if (use_dtor)
7387 /* We must convert CLEANUP to the type that "__cxa_atexit"
7388 expects. */
7389 cleanup = build_nop (get_atexit_fn_ptr_type (), cleanup);
7390 /* "__cxa_atexit" will pass the address of DECL to the
7391 cleanup function. */
7392 mark_used (decl);
7393 addr = build_address (decl);
7394 /* The declared type of the parameter to "__cxa_atexit" is
7395 "void *". For plain "T*", we could just let the
7396 machinery in cp_build_function_call convert it -- but if the
7397 type is "cv-qualified T *", then we need to convert it
7398 before passing it in, to avoid spurious errors. */
7399 addr = build_nop (ptr_type_node, addr);
7401 else
7402 /* Since the cleanup functions we build ignore the address
7403 they're given, there's no reason to pass the actual address
7404 in, and, in general, it's cheaper to pass NULL than any
7405 other value. */
7406 addr = null_pointer_node;
7408 if (dso_parm)
7409 arg2 = cp_build_addr_expr (get_dso_handle_node (),
7410 tf_warning_or_error);
7411 else if (ob_parm)
7412 /* Just pass NULL to the dso handle parm if we don't actually
7413 have a DSO handle on this target. */
7414 arg2 = null_pointer_node;
7415 else
7416 arg2 = NULL_TREE;
7418 if (ob_parm)
7420 if (!CP_DECL_THREAD_LOCAL_P (decl)
7421 && targetm.cxx.use_aeabi_atexit ())
7423 arg1 = cleanup;
7424 arg0 = addr;
7426 else
7428 arg1 = addr;
7429 arg0 = cleanup;
7432 else
7434 arg0 = cleanup;
7435 arg1 = NULL_TREE;
7437 return cp_build_function_call_nary (atex_node, tf_warning_or_error,
7438 arg0, arg1, arg2, NULL_TREE);
7441 /* DECL is a VAR_DECL with static storage duration. INIT, if present,
7442 is its initializer. Generate code to handle the construction
7443 and destruction of DECL. */
7445 static void
7446 expand_static_init (tree decl, tree init)
7448 gcc_assert (VAR_P (decl));
7449 gcc_assert (TREE_STATIC (decl));
7451 /* Some variables require no dynamic initialization. */
7452 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
7454 /* Make sure the destructor is callable. */
7455 cxx_maybe_build_cleanup (decl, tf_warning_or_error);
7456 if (!init)
7457 return;
7460 if (CP_DECL_THREAD_LOCAL_P (decl) && DECL_GNU_TLS_P (decl)
7461 && !DECL_FUNCTION_SCOPE_P (decl))
7463 if (init)
7464 error ("non-local variable %qD declared %<__thread%> "
7465 "needs dynamic initialization", decl);
7466 else
7467 error ("non-local variable %qD declared %<__thread%> "
7468 "has a non-trivial destructor", decl);
7469 static bool informed;
7470 if (!informed)
7472 inform (DECL_SOURCE_LOCATION (decl),
7473 "C++11 %<thread_local%> allows dynamic initialization "
7474 "and destruction");
7475 informed = true;
7477 return;
7480 if (DECL_FUNCTION_SCOPE_P (decl))
7482 /* Emit code to perform this initialization but once. */
7483 tree if_stmt = NULL_TREE, inner_if_stmt = NULL_TREE;
7484 tree then_clause = NULL_TREE, inner_then_clause = NULL_TREE;
7485 tree guard, guard_addr;
7486 tree flag, begin;
7487 /* We don't need thread-safety code for thread-local vars. */
7488 bool thread_guard = (flag_threadsafe_statics
7489 && !CP_DECL_THREAD_LOCAL_P (decl));
7491 /* Emit code to perform this initialization but once. This code
7492 looks like:
7494 static <type> guard;
7495 if (!__atomic_load (guard.first_byte)) {
7496 if (__cxa_guard_acquire (&guard)) {
7497 bool flag = false;
7498 try {
7499 // Do initialization.
7500 flag = true; __cxa_guard_release (&guard);
7501 // Register variable for destruction at end of program.
7502 } catch {
7503 if (!flag) __cxa_guard_abort (&guard);
7507 Note that the `flag' variable is only set to 1 *after* the
7508 initialization is complete. This ensures that an exception,
7509 thrown during the construction, will cause the variable to
7510 reinitialized when we pass through this code again, as per:
7512 [stmt.dcl]
7514 If the initialization exits by throwing an exception, the
7515 initialization is not complete, so it will be tried again
7516 the next time control enters the declaration.
7518 This process should be thread-safe, too; multiple threads
7519 should not be able to initialize the variable more than
7520 once. */
7522 /* Create the guard variable. */
7523 guard = get_guard (decl);
7525 /* Begin the conditional initialization. */
7526 if_stmt = begin_if_stmt ();
7528 finish_if_stmt_cond (get_guard_cond (guard, thread_guard), if_stmt);
7529 then_clause = begin_compound_stmt (BCS_NO_SCOPE);
7531 if (thread_guard)
7533 tree vfntype = NULL_TREE;
7534 tree acquire_name, release_name, abort_name;
7535 tree acquire_fn, release_fn, abort_fn;
7536 guard_addr = build_address (guard);
7538 acquire_name = get_identifier ("__cxa_guard_acquire");
7539 release_name = get_identifier ("__cxa_guard_release");
7540 abort_name = get_identifier ("__cxa_guard_abort");
7541 acquire_fn = identifier_global_value (acquire_name);
7542 release_fn = identifier_global_value (release_name);
7543 abort_fn = identifier_global_value (abort_name);
7544 if (!acquire_fn)
7545 acquire_fn = push_library_fn
7546 (acquire_name, build_function_type_list (integer_type_node,
7547 TREE_TYPE (guard_addr),
7548 NULL_TREE),
7549 NULL_TREE, ECF_NOTHROW | ECF_LEAF);
7550 if (!release_fn || !abort_fn)
7551 vfntype = build_function_type_list (void_type_node,
7552 TREE_TYPE (guard_addr),
7553 NULL_TREE);
7554 if (!release_fn)
7555 release_fn = push_library_fn (release_name, vfntype, NULL_TREE,
7556 ECF_NOTHROW | ECF_LEAF);
7557 if (!abort_fn)
7558 abort_fn = push_library_fn (abort_name, vfntype, NULL_TREE,
7559 ECF_NOTHROW | ECF_LEAF);
7561 inner_if_stmt = begin_if_stmt ();
7562 finish_if_stmt_cond (build_call_n (acquire_fn, 1, guard_addr),
7563 inner_if_stmt);
7565 inner_then_clause = begin_compound_stmt (BCS_NO_SCOPE);
7566 begin = get_target_expr (boolean_false_node);
7567 flag = TARGET_EXPR_SLOT (begin);
7569 TARGET_EXPR_CLEANUP (begin)
7570 = build3 (COND_EXPR, void_type_node, flag,
7571 void_node,
7572 build_call_n (abort_fn, 1, guard_addr));
7573 CLEANUP_EH_ONLY (begin) = 1;
7575 /* Do the initialization itself. */
7576 init = add_stmt_to_compound (begin, init);
7577 init = add_stmt_to_compound
7578 (init, build2 (MODIFY_EXPR, void_type_node, flag, boolean_true_node));
7579 init = add_stmt_to_compound
7580 (init, build_call_n (release_fn, 1, guard_addr));
7582 else
7583 init = add_stmt_to_compound (init, set_guard (guard));
7585 /* Use atexit to register a function for destroying this static
7586 variable. */
7587 init = add_stmt_to_compound (init, register_dtor_fn (decl));
7589 finish_expr_stmt (init);
7591 if (thread_guard)
7593 finish_compound_stmt (inner_then_clause);
7594 finish_then_clause (inner_if_stmt);
7595 finish_if_stmt (inner_if_stmt);
7598 finish_compound_stmt (then_clause);
7599 finish_then_clause (if_stmt);
7600 finish_if_stmt (if_stmt);
7602 else if (CP_DECL_THREAD_LOCAL_P (decl))
7603 tls_aggregates = tree_cons (init, decl, tls_aggregates);
7604 else
7605 static_aggregates = tree_cons (init, decl, static_aggregates);
7609 /* Make TYPE a complete type based on INITIAL_VALUE.
7610 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
7611 2 if there was no information (in which case assume 0 if DO_DEFAULT),
7612 3 if the initializer list is empty (in pedantic mode). */
7615 cp_complete_array_type (tree *ptype, tree initial_value, bool do_default)
7617 int failure;
7618 tree type, elt_type;
7620 /* Don't get confused by a CONSTRUCTOR for some other type. */
7621 if (initial_value && TREE_CODE (initial_value) == CONSTRUCTOR
7622 && !BRACE_ENCLOSED_INITIALIZER_P (initial_value)
7623 && TREE_CODE (TREE_TYPE (initial_value)) != ARRAY_TYPE)
7624 return 1;
7626 if (initial_value)
7628 unsigned HOST_WIDE_INT i;
7629 tree value;
7631 /* An array of character type can be initialized from a
7632 brace-enclosed string constant.
7634 FIXME: this code is duplicated from reshape_init. Probably
7635 we should just call reshape_init here? */
7636 if (char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (*ptype)))
7637 && TREE_CODE (initial_value) == CONSTRUCTOR
7638 && !vec_safe_is_empty (CONSTRUCTOR_ELTS (initial_value)))
7640 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value);
7641 tree value = (*v)[0].value;
7643 if (TREE_CODE (value) == STRING_CST
7644 && v->length () == 1)
7645 initial_value = value;
7648 /* If any of the elements are parameter packs, we can't actually
7649 complete this type now because the array size is dependent. */
7650 if (TREE_CODE (initial_value) == CONSTRUCTOR)
7652 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (initial_value),
7653 i, value)
7655 if (PACK_EXPANSION_P (value))
7656 return 0;
7661 failure = complete_array_type (ptype, initial_value, do_default);
7663 /* We can create the array before the element type is complete, which
7664 means that we didn't have these two bits set in the original type
7665 either. In completing the type, we are expected to propagate these
7666 bits. See also complete_type which does the same thing for arrays
7667 of fixed size. */
7668 type = *ptype;
7669 if (TYPE_DOMAIN (type))
7671 elt_type = TREE_TYPE (type);
7672 TYPE_NEEDS_CONSTRUCTING (type) = TYPE_NEEDS_CONSTRUCTING (elt_type);
7673 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
7674 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (elt_type);
7677 return failure;
7680 /* As above, but either give an error or reject zero-size arrays, depending
7681 on COMPLAIN. */
7684 cp_complete_array_type_or_error (tree *ptype, tree initial_value,
7685 bool do_default, tsubst_flags_t complain)
7687 int failure;
7688 bool sfinae = !(complain & tf_error);
7689 /* In SFINAE context we can't be lenient about zero-size arrays. */
7690 if (sfinae)
7691 ++pedantic;
7692 failure = cp_complete_array_type (ptype, initial_value, do_default);
7693 if (sfinae)
7694 --pedantic;
7695 if (failure)
7697 if (sfinae)
7698 /* Not an error. */;
7699 else if (failure == 1)
7700 error ("initializer fails to determine size of %qT", *ptype);
7701 else if (failure == 2)
7703 if (do_default)
7704 error ("array size missing in %qT", *ptype);
7706 else if (failure == 3)
7707 error ("zero-size array %qT", *ptype);
7708 *ptype = error_mark_node;
7710 return failure;
7713 /* Return zero if something is declared to be a member of type
7714 CTYPE when in the context of CUR_TYPE. STRING is the error
7715 message to print in that case. Otherwise, quietly return 1. */
7717 static int
7718 member_function_or_else (tree ctype, tree cur_type, enum overload_flags flags)
7720 if (ctype && ctype != cur_type)
7722 if (flags == DTOR_FLAG)
7723 error ("destructor for alien class %qT cannot be a member", ctype);
7724 else
7725 error ("constructor for alien class %qT cannot be a member", ctype);
7726 return 0;
7728 return 1;
7731 /* Subroutine of `grokdeclarator'. */
7733 /* Generate errors possibly applicable for a given set of specifiers.
7734 This is for ARM $7.1.2. */
7736 static void
7737 bad_specifiers (tree object,
7738 enum bad_spec_place type,
7739 int virtualp,
7740 int quals,
7741 int inlinep,
7742 int friendp,
7743 int raises)
7745 switch (type)
7747 case BSP_VAR:
7748 if (virtualp)
7749 error ("%qD declared as a %<virtual%> variable", object);
7750 if (inlinep)
7751 error ("%qD declared as an %<inline%> variable", object);
7752 if (quals)
7753 error ("%<const%> and %<volatile%> function specifiers on "
7754 "%qD invalid in variable declaration", object);
7755 break;
7756 case BSP_PARM:
7757 if (virtualp)
7758 error ("%qD declared as a %<virtual%> parameter", object);
7759 if (inlinep)
7760 error ("%qD declared as an %<inline%> parameter", object);
7761 if (quals)
7762 error ("%<const%> and %<volatile%> function specifiers on "
7763 "%qD invalid in parameter declaration", object);
7764 break;
7765 case BSP_TYPE:
7766 if (virtualp)
7767 error ("%qD declared as a %<virtual%> type", object);
7768 if (inlinep)
7769 error ("%qD declared as an %<inline%> type", object);
7770 if (quals)
7771 error ("%<const%> and %<volatile%> function specifiers on "
7772 "%qD invalid in type declaration", object);
7773 break;
7774 case BSP_FIELD:
7775 if (virtualp)
7776 error ("%qD declared as a %<virtual%> field", object);
7777 if (inlinep)
7778 error ("%qD declared as an %<inline%> field", object);
7779 if (quals)
7780 error ("%<const%> and %<volatile%> function specifiers on "
7781 "%qD invalid in field declaration", object);
7782 break;
7783 default:
7784 gcc_unreachable();
7786 if (friendp)
7787 error ("%q+D declared as a friend", object);
7788 if (raises
7789 && (TREE_CODE (object) == TYPE_DECL
7790 || (!TYPE_PTRFN_P (TREE_TYPE (object))
7791 && !TYPE_REFFN_P (TREE_TYPE (object))
7792 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (object)))))
7793 error ("%q+D declared with an exception specification", object);
7796 /* DECL is a member function or static data member and is presently
7797 being defined. Check that the definition is taking place in a
7798 valid namespace. */
7800 static void
7801 check_class_member_definition_namespace (tree decl)
7803 /* These checks only apply to member functions and static data
7804 members. */
7805 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
7806 /* We check for problems with specializations in pt.c in
7807 check_specialization_namespace, where we can issue better
7808 diagnostics. */
7809 if (processing_specialization)
7810 return;
7811 /* There are no restrictions on the placement of
7812 explicit instantiations. */
7813 if (processing_explicit_instantiation)
7814 return;
7815 /* [class.mfct]
7817 A member function definition that appears outside of the
7818 class definition shall appear in a namespace scope enclosing
7819 the class definition.
7821 [class.static.data]
7823 The definition for a static data member shall appear in a
7824 namespace scope enclosing the member's class definition. */
7825 if (!is_ancestor (current_namespace, DECL_CONTEXT (decl)))
7826 permerror (input_location, "definition of %qD is not in namespace enclosing %qT",
7827 decl, DECL_CONTEXT (decl));
7830 /* Build a PARM_DECL for the "this" parameter. TYPE is the
7831 METHOD_TYPE for a non-static member function; QUALS are the
7832 cv-qualifiers that apply to the function. */
7834 tree
7835 build_this_parm (tree type, cp_cv_quals quals)
7837 tree this_type;
7838 tree qual_type;
7839 tree parm;
7840 cp_cv_quals this_quals;
7842 if (CLASS_TYPE_P (type))
7844 this_type
7845 = cp_build_qualified_type (type, quals & ~TYPE_QUAL_RESTRICT);
7846 this_type = build_pointer_type (this_type);
7848 else
7849 this_type = type_of_this_parm (type);
7850 /* The `this' parameter is implicitly `const'; it cannot be
7851 assigned to. */
7852 this_quals = (quals & TYPE_QUAL_RESTRICT) | TYPE_QUAL_CONST;
7853 qual_type = cp_build_qualified_type (this_type, this_quals);
7854 parm = build_artificial_parm (this_identifier, qual_type);
7855 cp_apply_type_quals_to_decl (this_quals, parm);
7856 return parm;
7859 /* DECL is a static member function. Complain if it was declared
7860 with function-cv-quals. */
7862 static void
7863 check_static_quals (tree decl, cp_cv_quals quals)
7865 if (quals != TYPE_UNQUALIFIED)
7866 error ("static member function %q#D declared with type qualifiers",
7867 decl);
7870 // Check that FN takes no arguments and returns bool.
7871 static void
7872 check_concept_fn (tree fn)
7874 // A constraint is nullary.
7875 if (DECL_ARGUMENTS (fn))
7876 error ("concept %q#D declared with function parameters", fn);
7878 // The declared return type of the concept shall be bool, and
7879 // it shall not be deduced from it definition.
7880 tree type = TREE_TYPE (TREE_TYPE (fn));
7881 if (is_auto (type))
7882 error ("concept %q#D declared with a deduced return type", fn);
7883 else if (type != boolean_type_node)
7884 error ("concept %q#D with non-%<bool%> return type %qT", fn, type);
7887 /* Helper function. Replace the temporary this parameter injected
7888 during cp_finish_omp_declare_simd with the real this parameter. */
7890 static tree
7891 declare_simd_adjust_this (tree *tp, int *walk_subtrees, void *data)
7893 tree this_parm = (tree) data;
7894 if (TREE_CODE (*tp) == PARM_DECL
7895 && DECL_NAME (*tp) == this_identifier
7896 && *tp != this_parm)
7897 *tp = this_parm;
7898 else if (TYPE_P (*tp))
7899 *walk_subtrees = 0;
7900 return NULL_TREE;
7903 /* CTYPE is class type, or null if non-class.
7904 TYPE is type this FUNCTION_DECL should have, either FUNCTION_TYPE
7905 or METHOD_TYPE.
7906 DECLARATOR is the function's name.
7907 PARMS is a chain of PARM_DECLs for the function.
7908 VIRTUALP is truthvalue of whether the function is virtual or not.
7909 FLAGS are to be passed through to `grokclassfn'.
7910 QUALS are qualifiers indicating whether the function is `const'
7911 or `volatile'.
7912 RAISES is a list of exceptions that this function can raise.
7913 CHECK is 1 if we must find this method in CTYPE, 0 if we should
7914 not look, and -1 if we should not call `grokclassfn' at all.
7916 SFK is the kind of special function (if any) for the new function.
7918 Returns `NULL_TREE' if something goes wrong, after issuing
7919 applicable error messages. */
7921 static tree
7922 grokfndecl (tree ctype,
7923 tree type,
7924 tree declarator,
7925 tree parms,
7926 tree orig_declarator,
7927 tree decl_reqs,
7928 int virtualp,
7929 enum overload_flags flags,
7930 cp_cv_quals quals,
7931 cp_ref_qualifier rqual,
7932 tree raises,
7933 int check,
7934 int friendp,
7935 int publicp,
7936 int inlinep,
7937 bool deletedp,
7938 special_function_kind sfk,
7939 bool funcdef_flag,
7940 int template_count,
7941 tree in_namespace,
7942 tree* attrlist,
7943 location_t location)
7945 tree decl;
7946 int staticp = ctype && TREE_CODE (type) == FUNCTION_TYPE;
7947 tree t;
7949 // Was the concept specifier present?
7950 bool concept_p = inlinep & 4;
7952 // Concept declarations must have a corresponding definition.
7953 if (concept_p && !funcdef_flag)
7955 error ("concept %qD has no definition", declarator);
7956 return NULL_TREE;
7959 if (rqual)
7960 type = build_ref_qualified_type (type, rqual);
7961 if (raises)
7962 type = build_exception_variant (type, raises);
7964 decl = build_lang_decl (FUNCTION_DECL, declarator, type);
7966 /* Set the constraints on the declaration. */
7967 if (flag_concepts)
7969 tree tmpl_reqs = NULL_TREE;
7970 if (processing_template_decl > template_class_depth (ctype))
7971 tmpl_reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
7973 /* Adjust the required expression into a constraint. */
7974 if (decl_reqs)
7975 decl_reqs = normalize_expression (decl_reqs);
7977 tree ci = build_constraints (tmpl_reqs, decl_reqs);
7978 set_constraints (decl, ci);
7981 /* If we have an explicit location, use it, otherwise use whatever
7982 build_lang_decl used (probably input_location). */
7983 if (location != UNKNOWN_LOCATION)
7984 DECL_SOURCE_LOCATION (decl) = location;
7986 if (TREE_CODE (type) == METHOD_TYPE)
7988 tree parm;
7989 parm = build_this_parm (type, quals);
7990 DECL_CHAIN (parm) = parms;
7991 parms = parm;
7993 /* Allocate space to hold the vptr bit if needed. */
7994 SET_DECL_ALIGN (decl, MINIMUM_METHOD_BOUNDARY);
7996 DECL_ARGUMENTS (decl) = parms;
7997 for (t = parms; t; t = DECL_CHAIN (t))
7998 DECL_CONTEXT (t) = decl;
7999 /* Propagate volatile out from type to decl. */
8000 if (TYPE_VOLATILE (type))
8001 TREE_THIS_VOLATILE (decl) = 1;
8003 /* Setup decl according to sfk. */
8004 switch (sfk)
8006 case sfk_constructor:
8007 case sfk_copy_constructor:
8008 case sfk_move_constructor:
8009 DECL_CONSTRUCTOR_P (decl) = 1;
8010 break;
8011 case sfk_destructor:
8012 DECL_DESTRUCTOR_P (decl) = 1;
8013 break;
8014 default:
8015 break;
8018 if (friendp
8019 && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)
8021 if (funcdef_flag)
8022 error
8023 ("defining explicit specialization %qD in friend declaration",
8024 orig_declarator);
8025 else
8027 tree fns = TREE_OPERAND (orig_declarator, 0);
8028 tree args = TREE_OPERAND (orig_declarator, 1);
8030 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8032 /* Something like `template <class T> friend void f<T>()'. */
8033 error ("invalid use of template-id %qD in declaration "
8034 "of primary template",
8035 orig_declarator);
8036 return NULL_TREE;
8040 /* A friend declaration of the form friend void f<>(). Record
8041 the information in the TEMPLATE_ID_EXPR. */
8042 SET_DECL_IMPLICIT_INSTANTIATION (decl);
8044 gcc_assert (identifier_p (fns) || TREE_CODE (fns) == OVERLOAD);
8045 DECL_TEMPLATE_INFO (decl) = build_template_info (fns, args);
8047 for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t))
8048 if (TREE_PURPOSE (t)
8049 && TREE_CODE (TREE_PURPOSE (t)) == DEFAULT_ARG)
8051 error ("default arguments are not allowed in declaration "
8052 "of friend template specialization %qD",
8053 decl);
8054 return NULL_TREE;
8057 if (inlinep & 1)
8059 error ("%<inline%> is not allowed in declaration of friend "
8060 "template specialization %qD",
8061 decl);
8062 return NULL_TREE;
8067 /* If this decl has namespace scope, set that up. */
8068 if (in_namespace)
8069 set_decl_namespace (decl, in_namespace, friendp);
8070 else if (!ctype)
8071 DECL_CONTEXT (decl) = FROB_CONTEXT (current_decl_namespace ());
8073 /* `main' and builtins have implicit 'C' linkage. */
8074 if ((MAIN_NAME_P (declarator)
8075 || (IDENTIFIER_LENGTH (declarator) > 10
8076 && IDENTIFIER_POINTER (declarator)[0] == '_'
8077 && IDENTIFIER_POINTER (declarator)[1] == '_'
8078 && strncmp (IDENTIFIER_POINTER (declarator)+2, "builtin_", 8) == 0)
8079 || (targetcm.cxx_implicit_extern_c
8080 && targetcm.cxx_implicit_extern_c(IDENTIFIER_POINTER (declarator))))
8081 && current_lang_name == lang_name_cplusplus
8082 && ctype == NULL_TREE
8083 && DECL_FILE_SCOPE_P (decl))
8084 SET_DECL_LANGUAGE (decl, lang_c);
8086 /* Should probably propagate const out from type to decl I bet (mrs). */
8087 if (staticp)
8089 DECL_STATIC_FUNCTION_P (decl) = 1;
8090 DECL_CONTEXT (decl) = ctype;
8093 if (deletedp)
8094 DECL_DELETED_FN (decl) = 1;
8096 if (ctype)
8098 DECL_CONTEXT (decl) = ctype;
8099 if (funcdef_flag)
8100 check_class_member_definition_namespace (decl);
8103 if (ctype == NULL_TREE && DECL_MAIN_P (decl))
8105 if (PROCESSING_REAL_TEMPLATE_DECL_P())
8106 error ("cannot declare %<::main%> to be a template");
8107 if (inlinep & 1)
8108 error ("cannot declare %<::main%> to be inline");
8109 if (inlinep & 2)
8110 error ("cannot declare %<::main%> to be constexpr");
8111 if (!publicp)
8112 error ("cannot declare %<::main%> to be static");
8113 inlinep = 0;
8114 publicp = 1;
8117 /* Members of anonymous types and local classes have no linkage; make
8118 them internal. If a typedef is made later, this will be changed. */
8119 if (ctype && (!TREE_PUBLIC (TYPE_MAIN_DECL (ctype))
8120 || decl_function_context (TYPE_MAIN_DECL (ctype))))
8121 publicp = 0;
8123 if (publicp && cxx_dialect == cxx98)
8125 /* [basic.link]: A name with no linkage (notably, the name of a class
8126 or enumeration declared in a local scope) shall not be used to
8127 declare an entity with linkage.
8129 DR 757 relaxes this restriction for C++0x. */
8130 no_linkage_error (decl);
8133 TREE_PUBLIC (decl) = publicp;
8134 if (! publicp)
8136 DECL_INTERFACE_KNOWN (decl) = 1;
8137 DECL_NOT_REALLY_EXTERN (decl) = 1;
8140 /* If the declaration was declared inline, mark it as such. */
8141 if (inlinep)
8143 DECL_DECLARED_INLINE_P (decl) = 1;
8144 if (publicp)
8145 DECL_COMDAT (decl) = 1;
8147 if (inlinep & 2)
8148 DECL_DECLARED_CONSTEXPR_P (decl) = true;
8150 // If the concept declaration specifier was found, check
8151 // that the declaration satisfies the necessary requirements.
8152 if (concept_p)
8154 DECL_DECLARED_CONCEPT_P (decl) = true;
8155 check_concept_fn (decl);
8158 DECL_EXTERNAL (decl) = 1;
8159 if (TREE_CODE (type) == FUNCTION_TYPE)
8161 if (quals || rqual)
8162 TREE_TYPE (decl) = apply_memfn_quals (TREE_TYPE (decl),
8163 TYPE_UNQUALIFIED,
8164 REF_QUAL_NONE);
8166 if (quals)
8168 error (ctype
8169 ? G_("static member function %qD cannot have cv-qualifier")
8170 : G_("non-member function %qD cannot have cv-qualifier"),
8171 decl);
8172 quals = TYPE_UNQUALIFIED;
8175 if (rqual)
8177 error (ctype
8178 ? G_("static member function %qD cannot have ref-qualifier")
8179 : G_("non-member function %qD cannot have ref-qualifier"),
8180 decl);
8181 rqual = REF_QUAL_NONE;
8185 if (IDENTIFIER_OPNAME_P (DECL_NAME (decl))
8186 && !grok_op_properties (decl, /*complain=*/true))
8187 return NULL_TREE;
8188 else if (UDLIT_OPER_P (DECL_NAME (decl)))
8190 bool long_long_unsigned_p;
8191 bool long_double_p;
8192 const char *suffix = NULL;
8193 /* [over.literal]/6: Literal operators shall not have C linkage. */
8194 if (DECL_LANGUAGE (decl) == lang_c)
8196 error ("literal operator with C linkage");
8197 return NULL_TREE;
8200 if (DECL_NAMESPACE_SCOPE_P (decl))
8202 if (!check_literal_operator_args (decl, &long_long_unsigned_p,
8203 &long_double_p))
8205 error ("%qD has invalid argument list", decl);
8206 return NULL_TREE;
8209 suffix = UDLIT_OP_SUFFIX (DECL_NAME (decl));
8210 if (long_long_unsigned_p)
8212 if (cpp_interpret_int_suffix (parse_in, suffix, strlen (suffix)))
8213 warning (0, "integer suffix %<%s%>"
8214 " shadowed by implementation", suffix);
8216 else if (long_double_p)
8218 if (cpp_interpret_float_suffix (parse_in, suffix, strlen (suffix)))
8219 warning (0, "floating point suffix %<%s%>"
8220 " shadowed by implementation", suffix);
8223 else
8225 error ("%qD must be a non-member function", decl);
8226 return NULL_TREE;
8230 if (funcdef_flag)
8231 /* Make the init_value nonzero so pushdecl knows this is not
8232 tentative. error_mark_node is replaced later with the BLOCK. */
8233 DECL_INITIAL (decl) = error_mark_node;
8235 if (TYPE_NOTHROW_P (type) || nothrow_libfn_p (decl))
8236 TREE_NOTHROW (decl) = 1;
8238 if (flag_openmp || flag_openmp_simd || flag_cilkplus)
8240 /* Adjust "omp declare simd" attributes. */
8241 tree ods = lookup_attribute ("omp declare simd", *attrlist);
8242 if (ods)
8244 tree attr;
8245 for (attr = ods; attr;
8246 attr = lookup_attribute ("omp declare simd", TREE_CHAIN (attr)))
8248 if (TREE_CODE (type) == METHOD_TYPE)
8249 walk_tree (&TREE_VALUE (attr), declare_simd_adjust_this,
8250 DECL_ARGUMENTS (decl), NULL);
8251 if (TREE_VALUE (attr) != NULL_TREE)
8253 tree cl = TREE_VALUE (TREE_VALUE (attr));
8254 cl = c_omp_declare_simd_clauses_to_numbers
8255 (DECL_ARGUMENTS (decl), cl);
8256 if (cl)
8257 TREE_VALUE (TREE_VALUE (attr)) = cl;
8258 else
8259 TREE_VALUE (attr) = NULL_TREE;
8265 /* Caller will do the rest of this. */
8266 if (check < 0)
8267 return decl;
8269 if (ctype != NULL_TREE)
8270 grokclassfn (ctype, decl, flags);
8272 /* 12.4/3 */
8273 if (cxx_dialect >= cxx11
8274 && DECL_DESTRUCTOR_P (decl)
8275 && !TYPE_BEING_DEFINED (DECL_CONTEXT (decl))
8276 && !processing_template_decl)
8277 deduce_noexcept_on_destructor (decl);
8279 decl = check_explicit_specialization (orig_declarator, decl,
8280 template_count,
8281 2 * funcdef_flag +
8282 4 * (friendp != 0) +
8283 8 * concept_p);
8284 if (decl == error_mark_node)
8285 return NULL_TREE;
8287 if (DECL_STATIC_FUNCTION_P (decl))
8288 check_static_quals (decl, quals);
8290 if (attrlist)
8292 cplus_decl_attributes (&decl, *attrlist, 0);
8293 *attrlist = NULL_TREE;
8296 /* Check main's type after attributes have been applied. */
8297 if (ctype == NULL_TREE && DECL_MAIN_P (decl))
8299 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
8300 integer_type_node))
8302 tree oldtypeargs = TYPE_ARG_TYPES (TREE_TYPE (decl));
8303 tree newtype;
8304 error ("%<::main%> must return %<int%>");
8305 newtype = build_function_type (integer_type_node, oldtypeargs);
8306 TREE_TYPE (decl) = newtype;
8308 if (warn_main)
8309 check_main_parameter_types (decl);
8312 if (ctype != NULL_TREE && check)
8314 tree old_decl = check_classfn (ctype, decl,
8315 (processing_template_decl
8316 > template_class_depth (ctype))
8317 ? current_template_parms
8318 : NULL_TREE);
8320 if (old_decl == error_mark_node)
8321 return NULL_TREE;
8323 if (old_decl)
8325 tree ok;
8326 tree pushed_scope;
8328 if (TREE_CODE (old_decl) == TEMPLATE_DECL)
8329 /* Because grokfndecl is always supposed to return a
8330 FUNCTION_DECL, we pull out the DECL_TEMPLATE_RESULT
8331 here. We depend on our callers to figure out that its
8332 really a template that's being returned. */
8333 old_decl = DECL_TEMPLATE_RESULT (old_decl);
8335 if (DECL_STATIC_FUNCTION_P (old_decl)
8336 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
8338 /* Remove the `this' parm added by grokclassfn. */
8339 revert_static_member_fn (decl);
8340 check_static_quals (decl, quals);
8342 if (DECL_ARTIFICIAL (old_decl))
8344 error ("definition of implicitly-declared %qD", old_decl);
8345 return NULL_TREE;
8347 else if (DECL_DEFAULTED_FN (old_decl))
8349 error ("definition of explicitly-defaulted %q+D", decl);
8350 inform (DECL_SOURCE_LOCATION (old_decl),
8351 "%q#D explicitly defaulted here", old_decl);
8352 return NULL_TREE;
8355 /* Since we've smashed OLD_DECL to its
8356 DECL_TEMPLATE_RESULT, we must do the same to DECL. */
8357 if (TREE_CODE (decl) == TEMPLATE_DECL)
8358 decl = DECL_TEMPLATE_RESULT (decl);
8360 /* Attempt to merge the declarations. This can fail, in
8361 the case of some invalid specialization declarations. */
8362 pushed_scope = push_scope (ctype);
8363 ok = duplicate_decls (decl, old_decl, friendp);
8364 if (pushed_scope)
8365 pop_scope (pushed_scope);
8366 if (!ok)
8368 error ("no %q#D member function declared in class %qT",
8369 decl, ctype);
8370 return NULL_TREE;
8372 if (ok == error_mark_node)
8373 return NULL_TREE;
8374 return old_decl;
8378 if (DECL_CONSTRUCTOR_P (decl) && !grok_ctor_properties (ctype, decl))
8379 return NULL_TREE;
8381 if (ctype == NULL_TREE || check)
8382 return decl;
8384 if (virtualp)
8385 DECL_VIRTUAL_P (decl) = 1;
8387 return decl;
8390 /* decl is a FUNCTION_DECL.
8391 specifiers are the parsed virt-specifiers.
8393 Set flags to reflect the virt-specifiers.
8395 Returns decl. */
8397 static tree
8398 set_virt_specifiers (tree decl, cp_virt_specifiers specifiers)
8400 if (decl == NULL_TREE)
8401 return decl;
8402 if (specifiers & VIRT_SPEC_OVERRIDE)
8403 DECL_OVERRIDE_P (decl) = 1;
8404 if (specifiers & VIRT_SPEC_FINAL)
8405 DECL_FINAL_P (decl) = 1;
8406 return decl;
8409 /* DECL is a VAR_DECL for a static data member. Set flags to reflect
8410 the linkage that DECL will receive in the object file. */
8412 static void
8413 set_linkage_for_static_data_member (tree decl)
8415 /* A static data member always has static storage duration and
8416 external linkage. Note that static data members are forbidden in
8417 local classes -- the only situation in which a class has
8418 non-external linkage. */
8419 TREE_PUBLIC (decl) = 1;
8420 TREE_STATIC (decl) = 1;
8421 /* For non-template classes, static data members are always put
8422 out in exactly those files where they are defined, just as
8423 with ordinary namespace-scope variables. */
8424 if (!processing_template_decl)
8425 DECL_INTERFACE_KNOWN (decl) = 1;
8428 /* Create a VAR_DECL named NAME with the indicated TYPE.
8430 If SCOPE is non-NULL, it is the class type or namespace containing
8431 the variable. If SCOPE is NULL, the variable should is created in
8432 the innermost enclosing scope. */
8434 static tree
8435 grokvardecl (tree type,
8436 tree name,
8437 tree orig_declarator,
8438 const cp_decl_specifier_seq *declspecs,
8439 int initialized,
8440 int flags,
8441 int template_count,
8442 tree scope)
8444 tree decl;
8445 tree explicit_scope;
8447 gcc_assert (!name || identifier_p (name));
8449 bool constp = flags&1;
8450 bool conceptp = flags&2;
8452 /* Compute the scope in which to place the variable, but remember
8453 whether or not that scope was explicitly specified by the user. */
8454 explicit_scope = scope;
8455 if (!scope)
8457 /* An explicit "extern" specifier indicates a namespace-scope
8458 variable. */
8459 if (declspecs->storage_class == sc_extern)
8460 scope = current_decl_namespace ();
8461 else if (!at_function_scope_p ())
8462 scope = current_scope ();
8465 if (scope
8466 && (/* If the variable is a namespace-scope variable declared in a
8467 template, we need DECL_LANG_SPECIFIC. */
8468 (TREE_CODE (scope) == NAMESPACE_DECL && processing_template_decl)
8469 /* Similarly for namespace-scope variables with language linkage
8470 other than C++. */
8471 || (TREE_CODE (scope) == NAMESPACE_DECL
8472 && current_lang_name != lang_name_cplusplus)
8473 /* Similarly for static data members. */
8474 || TYPE_P (scope)
8475 /* Similarly for explicit specializations. */
8476 || (orig_declarator
8477 && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)))
8478 decl = build_lang_decl (VAR_DECL, name, type);
8479 else
8480 decl = build_decl (input_location, VAR_DECL, name, type);
8482 if (explicit_scope && TREE_CODE (explicit_scope) == NAMESPACE_DECL)
8483 set_decl_namespace (decl, explicit_scope, 0);
8484 else
8485 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
8487 if (declspecs->storage_class == sc_extern)
8489 DECL_THIS_EXTERN (decl) = 1;
8490 DECL_EXTERNAL (decl) = !initialized;
8493 if (DECL_CLASS_SCOPE_P (decl))
8495 set_linkage_for_static_data_member (decl);
8496 /* This function is only called with out-of-class definitions. */
8497 DECL_EXTERNAL (decl) = 0;
8498 check_class_member_definition_namespace (decl);
8500 /* At top level, either `static' or no s.c. makes a definition
8501 (perhaps tentative), and absence of `static' makes it public. */
8502 else if (toplevel_bindings_p ())
8504 TREE_PUBLIC (decl) = (declspecs->storage_class != sc_static
8505 && (DECL_THIS_EXTERN (decl) || ! constp));
8506 TREE_STATIC (decl) = ! DECL_EXTERNAL (decl);
8508 /* Not at top level, only `static' makes a static definition. */
8509 else
8511 TREE_STATIC (decl) = declspecs->storage_class == sc_static;
8512 TREE_PUBLIC (decl) = DECL_EXTERNAL (decl);
8515 if (decl_spec_seq_has_spec_p (declspecs, ds_thread))
8517 if (DECL_EXTERNAL (decl) || TREE_STATIC (decl))
8519 CP_DECL_THREAD_LOCAL_P (decl) = true;
8520 if (!processing_template_decl)
8521 set_decl_tls_model (decl, decl_default_tls_model (decl));
8523 if (declspecs->gnu_thread_keyword_p)
8524 SET_DECL_GNU_TLS_P (decl);
8527 /* If the type of the decl has no linkage, make sure that we'll
8528 notice that in mark_used. */
8529 if (cxx_dialect > cxx98
8530 && decl_linkage (decl) != lk_none
8531 && DECL_LANG_SPECIFIC (decl) == NULL
8532 && !DECL_EXTERN_C_P (decl)
8533 && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
8534 retrofit_lang_decl (decl);
8536 if (TREE_PUBLIC (decl))
8538 /* [basic.link]: A name with no linkage (notably, the name of a class
8539 or enumeration declared in a local scope) shall not be used to
8540 declare an entity with linkage.
8542 DR 757 relaxes this restriction for C++0x. */
8543 if (cxx_dialect < cxx11)
8544 no_linkage_error (decl);
8546 else
8547 DECL_INTERFACE_KNOWN (decl) = 1;
8549 if (DECL_NAME (decl)
8550 && MAIN_NAME_P (DECL_NAME (decl))
8551 && scope == global_namespace)
8552 error ("cannot declare %<::main%> to be a global variable");
8554 /* Check that the variable can be safely declared as a concept.
8555 Note that this also forbids explicit specializations. */
8556 if (conceptp)
8558 if (!processing_template_decl)
8560 error ("a non-template variable cannot be %<concept%>");
8561 return NULL_TREE;
8563 else
8564 DECL_DECLARED_CONCEPT_P (decl) = true;
8565 if (!same_type_ignoring_top_level_qualifiers_p (type, boolean_type_node))
8566 error_at (declspecs->locations[ds_type_spec],
8567 "concept must have type %<bool%>");
8569 else if (flag_concepts
8570 && processing_template_decl > template_class_depth (scope))
8572 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
8573 tree ci = build_constraints (reqs, NULL_TREE);
8574 set_constraints (decl, ci);
8577 // Handle explicit specializations and instantiations of variable templates.
8578 if (orig_declarator)
8579 decl = check_explicit_specialization (orig_declarator, decl,
8580 template_count, conceptp * 8);
8582 return decl != error_mark_node ? decl : NULL_TREE;
8585 /* Create and return a canonical pointer to member function type, for
8586 TYPE, which is a POINTER_TYPE to a METHOD_TYPE. */
8588 tree
8589 build_ptrmemfunc_type (tree type)
8591 tree field, fields;
8592 tree t;
8594 if (type == error_mark_node)
8595 return type;
8597 /* Make sure that we always have the unqualified pointer-to-member
8598 type first. */
8599 if (cp_cv_quals quals = cp_type_quals (type))
8601 tree unqual = build_ptrmemfunc_type (TYPE_MAIN_VARIANT (type));
8602 return cp_build_qualified_type (unqual, quals);
8605 /* If a canonical type already exists for this type, use it. We use
8606 this method instead of type_hash_canon, because it only does a
8607 simple equality check on the list of field members. */
8609 if ((t = TYPE_GET_PTRMEMFUNC_TYPE (type)))
8610 return t;
8612 t = make_node (RECORD_TYPE);
8614 /* Let the front end know this is a pointer to member function. */
8615 TYPE_PTRMEMFUNC_FLAG (t) = 1;
8617 field = build_decl (input_location, FIELD_DECL, pfn_identifier, type);
8618 fields = field;
8620 field = build_decl (input_location, FIELD_DECL, delta_identifier,
8621 delta_type_node);
8622 DECL_CHAIN (field) = fields;
8623 fields = field;
8625 finish_builtin_struct (t, "__ptrmemfunc_type", fields, ptr_type_node);
8627 /* Zap out the name so that the back end will give us the debugging
8628 information for this anonymous RECORD_TYPE. */
8629 TYPE_NAME (t) = NULL_TREE;
8631 /* Cache this pointer-to-member type so that we can find it again
8632 later. */
8633 TYPE_SET_PTRMEMFUNC_TYPE (type, t);
8635 if (TYPE_STRUCTURAL_EQUALITY_P (type))
8636 SET_TYPE_STRUCTURAL_EQUALITY (t);
8637 else if (TYPE_CANONICAL (type) != type)
8638 TYPE_CANONICAL (t) = build_ptrmemfunc_type (TYPE_CANONICAL (type));
8640 return t;
8643 /* Create and return a pointer to data member type. */
8645 tree
8646 build_ptrmem_type (tree class_type, tree member_type)
8648 if (TREE_CODE (member_type) == METHOD_TYPE)
8650 cp_cv_quals quals = type_memfn_quals (member_type);
8651 cp_ref_qualifier rqual = type_memfn_rqual (member_type);
8652 member_type = build_memfn_type (member_type, class_type, quals, rqual);
8653 return build_ptrmemfunc_type (build_pointer_type (member_type));
8655 else
8657 gcc_assert (TREE_CODE (member_type) != FUNCTION_TYPE);
8658 return build_offset_type (class_type, member_type);
8662 /* DECL is a VAR_DECL defined in-class, whose TYPE is also given.
8663 Check to see that the definition is valid. Issue appropriate error
8664 messages. Return 1 if the definition is particularly bad, or 0
8665 otherwise. */
8667 static int
8668 check_static_variable_definition (tree decl, tree type)
8670 /* Avoid redundant diagnostics on out-of-class definitions. */
8671 if (!current_class_type || !TYPE_BEING_DEFINED (current_class_type))
8672 return 0;
8673 /* Can't check yet if we don't know the type. */
8674 if (dependent_type_p (type))
8675 return 0;
8676 /* If DECL is declared constexpr, we'll do the appropriate checks
8677 in check_initializer. */
8678 if (DECL_P (decl) && DECL_DECLARED_CONSTEXPR_P (decl))
8679 return 0;
8680 else if (cxx_dialect >= cxx11 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
8682 if (!COMPLETE_TYPE_P (type))
8683 error_at (DECL_SOURCE_LOCATION (decl),
8684 "in-class initialization of static data member %q#D of "
8685 "incomplete type", decl);
8686 else if (literal_type_p (type))
8687 permerror (DECL_SOURCE_LOCATION (decl),
8688 "%<constexpr%> needed for in-class initialization of "
8689 "static data member %q#D of non-integral type", decl);
8690 else
8691 error_at (DECL_SOURCE_LOCATION (decl),
8692 "in-class initialization of static data member %q#D of "
8693 "non-literal type", decl);
8694 return 1;
8697 /* Motion 10 at San Diego: If a static const integral data member is
8698 initialized with an integral constant expression, the initializer
8699 may appear either in the declaration (within the class), or in
8700 the definition, but not both. If it appears in the class, the
8701 member is a member constant. The file-scope definition is always
8702 required. */
8703 if (!ARITHMETIC_TYPE_P (type) && TREE_CODE (type) != ENUMERAL_TYPE)
8705 error_at (DECL_SOURCE_LOCATION (decl),
8706 "invalid in-class initialization of static data member "
8707 "of non-integral type %qT",
8708 type);
8709 return 1;
8711 else if (!CP_TYPE_CONST_P (type))
8712 error_at (DECL_SOURCE_LOCATION (decl),
8713 "ISO C++ forbids in-class initialization of non-const "
8714 "static member %qD",
8715 decl);
8716 else if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type))
8717 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
8718 "ISO C++ forbids initialization of member constant "
8719 "%qD of non-integral type %qT", decl, type);
8721 return 0;
8724 /* *expr_p is part of the TYPE_SIZE of a variably-sized array. If any
8725 SAVE_EXPRs in *expr_p wrap expressions with side-effects, break those
8726 expressions out into temporary variables so that walk_tree doesn't
8727 step into them (c++/15764). */
8729 static tree
8730 stabilize_save_expr_r (tree *expr_p, int *walk_subtrees, void *data)
8732 hash_set<tree> *pset = (hash_set<tree> *)data;
8733 tree expr = *expr_p;
8734 if (TREE_CODE (expr) == SAVE_EXPR)
8736 tree op = TREE_OPERAND (expr, 0);
8737 cp_walk_tree (&op, stabilize_save_expr_r, data, pset);
8738 if (TREE_SIDE_EFFECTS (op))
8739 TREE_OPERAND (expr, 0) = get_temp_regvar (TREE_TYPE (op), op);
8740 *walk_subtrees = 0;
8742 else if (!EXPR_P (expr) || !TREE_SIDE_EFFECTS (expr))
8743 *walk_subtrees = 0;
8744 return NULL;
8747 /* Entry point for the above. */
8749 static void
8750 stabilize_vla_size (tree size)
8752 hash_set<tree> pset;
8753 /* Break out any function calls into temporary variables. */
8754 cp_walk_tree (&size, stabilize_save_expr_r, &pset, &pset);
8757 /* Reduce a SIZEOF_EXPR to its value. */
8759 tree
8760 fold_sizeof_expr (tree t)
8762 tree r;
8763 if (SIZEOF_EXPR_TYPE_P (t))
8764 r = cxx_sizeof_or_alignof_type (TREE_TYPE (TREE_OPERAND (t, 0)),
8765 SIZEOF_EXPR, false);
8766 else if (TYPE_P (TREE_OPERAND (t, 0)))
8767 r = cxx_sizeof_or_alignof_type (TREE_OPERAND (t, 0), SIZEOF_EXPR,
8768 false);
8769 else
8770 r = cxx_sizeof_or_alignof_expr (TREE_OPERAND (t, 0), SIZEOF_EXPR,
8771 false);
8772 if (r == error_mark_node)
8773 r = size_one_node;
8774 return r;
8777 /* Given the SIZE (i.e., number of elements) in an array, compute
8778 an appropriate index type for the array. If non-NULL, NAME is
8779 the name of the entity being declared. */
8781 tree
8782 compute_array_index_type (tree name, tree size, tsubst_flags_t complain)
8784 tree itype;
8785 tree osize = size;
8787 if (error_operand_p (size))
8788 return error_mark_node;
8790 if (!type_dependent_expression_p (size))
8792 tree type = TREE_TYPE (size);
8794 mark_rvalue_use (size);
8796 if (cxx_dialect < cxx11 && TREE_CODE (size) == NOP_EXPR
8797 && TREE_SIDE_EFFECTS (size))
8798 /* In C++98, we mark a non-constant array bound with a magic
8799 NOP_EXPR with TREE_SIDE_EFFECTS; don't fold in that case. */;
8800 else
8802 size = instantiate_non_dependent_expr_sfinae (size, complain);
8804 if (CLASS_TYPE_P (type)
8805 && CLASSTYPE_LITERAL_P (type))
8807 size = build_expr_type_conversion (WANT_INT, size, true);
8808 if (!size)
8810 if (!(complain & tf_error))
8811 return error_mark_node;
8812 if (name)
8813 error ("size of array %qD has non-integral type %qT",
8814 name, type);
8815 else
8816 error ("size of array has non-integral type %qT", type);
8817 size = integer_one_node;
8819 if (size == error_mark_node)
8820 return error_mark_node;
8821 type = TREE_TYPE (size);
8824 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
8825 size = maybe_constant_value (size);
8827 if (!TREE_CONSTANT (size))
8828 size = osize;
8831 if (error_operand_p (size))
8832 return error_mark_node;
8834 /* The array bound must be an integer type. */
8835 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
8837 if (!(complain & tf_error))
8838 return error_mark_node;
8839 if (name)
8840 error ("size of array %qD has non-integral type %qT", name, type);
8841 else
8842 error ("size of array has non-integral type %qT", type);
8843 size = integer_one_node;
8844 type = TREE_TYPE (size);
8848 /* A type is dependent if it is...an array type constructed from any
8849 dependent type or whose size is specified by a constant expression
8850 that is value-dependent. */
8851 /* We can only call value_dependent_expression_p on integral constant
8852 expressions; treat non-constant expressions as dependent, too. */
8853 if (processing_template_decl
8854 && (type_dependent_expression_p (size)
8855 || !TREE_CONSTANT (size) || value_dependent_expression_p (size)))
8857 /* We cannot do any checking for a SIZE that isn't known to be
8858 constant. Just build the index type and mark that it requires
8859 structural equality checks. */
8860 itype = build_index_type (build_min (MINUS_EXPR, sizetype,
8861 size, size_one_node));
8862 TYPE_DEPENDENT_P (itype) = 1;
8863 TYPE_DEPENDENT_P_VALID (itype) = 1;
8864 SET_TYPE_STRUCTURAL_EQUALITY (itype);
8865 return itype;
8868 if (TREE_CODE (size) != INTEGER_CST)
8870 tree folded = cp_fully_fold (size);
8871 if (TREE_CODE (folded) == INTEGER_CST)
8872 pedwarn (location_of (size), OPT_Wpedantic,
8873 "size of array is not an integral constant-expression");
8874 /* Use the folded result for VLAs, too; it will have resolved
8875 SIZEOF_EXPR. */
8876 size = folded;
8879 /* Normally, the array-bound will be a constant. */
8880 if (TREE_CODE (size) == INTEGER_CST)
8882 /* Check to see if the array bound overflowed. Make that an
8883 error, no matter how generous we're being. */
8884 constant_expression_error (size);
8886 /* An array must have a positive number of elements. */
8887 if (tree_int_cst_lt (size, integer_zero_node))
8889 if (!(complain & tf_error))
8890 return error_mark_node;
8891 if (name)
8892 error ("size of array %qD is negative", name);
8893 else
8894 error ("size of array is negative");
8895 size = integer_one_node;
8897 /* As an extension we allow zero-sized arrays. */
8898 else if (integer_zerop (size))
8900 if (!(complain & tf_error))
8901 /* We must fail if performing argument deduction (as
8902 indicated by the state of complain), so that
8903 another substitution can be found. */
8904 return error_mark_node;
8905 else if (in_system_header_at (input_location))
8906 /* Allow them in system headers because glibc uses them. */;
8907 else if (name)
8908 pedwarn (input_location, OPT_Wpedantic, "ISO C++ forbids zero-size array %qD", name);
8909 else
8910 pedwarn (input_location, OPT_Wpedantic, "ISO C++ forbids zero-size array");
8913 else if (TREE_CONSTANT (size)
8914 /* We don't allow VLAs at non-function scopes, or during
8915 tentative template substitution. */
8916 || !at_function_scope_p ()
8917 || !(complain & tf_error))
8919 if (!(complain & tf_error))
8920 return error_mark_node;
8921 /* `(int) &fn' is not a valid array bound. */
8922 if (name)
8923 error ("size of array %qD is not an integral constant-expression",
8924 name);
8925 else
8926 error ("size of array is not an integral constant-expression");
8927 size = integer_one_node;
8929 else if (pedantic && warn_vla != 0)
8931 if (name)
8932 pedwarn (input_location, OPT_Wvla, "ISO C++ forbids variable length array %qD", name);
8933 else
8934 pedwarn (input_location, OPT_Wvla, "ISO C++ forbids variable length array");
8936 else if (warn_vla > 0)
8938 if (name)
8939 warning (OPT_Wvla,
8940 "variable length array %qD is used", name);
8941 else
8942 warning (OPT_Wvla,
8943 "variable length array is used");
8946 if (processing_template_decl && !TREE_CONSTANT (size))
8947 /* A variable sized array. */
8948 itype = build_min (MINUS_EXPR, sizetype, size, integer_one_node);
8949 else
8951 HOST_WIDE_INT saved_processing_template_decl;
8953 /* Compute the index of the largest element in the array. It is
8954 one less than the number of elements in the array. We save
8955 and restore PROCESSING_TEMPLATE_DECL so that computations in
8956 cp_build_binary_op will be appropriately folded. */
8957 saved_processing_template_decl = processing_template_decl;
8958 processing_template_decl = 0;
8959 itype = cp_build_binary_op (input_location,
8960 MINUS_EXPR,
8961 cp_convert (ssizetype, size, complain),
8962 cp_convert (ssizetype, integer_one_node,
8963 complain),
8964 complain);
8965 itype = maybe_constant_value (itype);
8966 processing_template_decl = saved_processing_template_decl;
8968 if (!TREE_CONSTANT (itype))
8970 /* A variable sized array. */
8971 itype = variable_size (itype);
8973 stabilize_vla_size (itype);
8975 if (flag_sanitize & SANITIZE_VLA
8976 && do_ubsan_in_current_function ())
8978 /* We have to add 1 -- in the ubsan routine we generate
8979 LE_EXPR rather than LT_EXPR. */
8980 tree t = fold_build2 (PLUS_EXPR, TREE_TYPE (itype), itype,
8981 build_one_cst (TREE_TYPE (itype)));
8982 t = ubsan_instrument_vla (input_location, t);
8983 finish_expr_stmt (t);
8986 /* Make sure that there was no overflow when creating to a signed
8987 index type. (For example, on a 32-bit machine, an array with
8988 size 2^32 - 1 is too big.) */
8989 else if (TREE_CODE (itype) == INTEGER_CST
8990 && TREE_OVERFLOW (itype))
8992 if (!(complain & tf_error))
8993 return error_mark_node;
8994 error ("overflow in array dimension");
8995 TREE_OVERFLOW (itype) = 0;
8999 /* Create and return the appropriate index type. */
9000 itype = build_index_type (itype);
9002 /* If the index type were dependent, we would have returned early, so
9003 remember that it isn't. */
9004 TYPE_DEPENDENT_P (itype) = 0;
9005 TYPE_DEPENDENT_P_VALID (itype) = 1;
9006 return itype;
9009 /* Returns the scope (if any) in which the entity declared by
9010 DECLARATOR will be located. If the entity was declared with an
9011 unqualified name, NULL_TREE is returned. */
9013 tree
9014 get_scope_of_declarator (const cp_declarator *declarator)
9016 while (declarator && declarator->kind != cdk_id)
9017 declarator = declarator->declarator;
9019 /* If the declarator-id is a SCOPE_REF, the scope in which the
9020 declaration occurs is the first operand. */
9021 if (declarator
9022 && declarator->u.id.qualifying_scope)
9023 return declarator->u.id.qualifying_scope;
9025 /* Otherwise, the declarator is not a qualified name; the entity will
9026 be declared in the current scope. */
9027 return NULL_TREE;
9030 /* Returns an ARRAY_TYPE for an array with SIZE elements of the
9031 indicated TYPE. If non-NULL, NAME is the NAME of the declaration
9032 with this type. */
9034 static tree
9035 create_array_type_for_decl (tree name, tree type, tree size)
9037 tree itype = NULL_TREE;
9039 /* If things have already gone awry, bail now. */
9040 if (type == error_mark_node || size == error_mark_node)
9041 return error_mark_node;
9043 /* 8.3.4/1: If the type of the identifier of D contains the auto
9044 type-specifier, the program is ill-formed. */
9045 if (type_uses_auto (type))
9047 error ("%qD declared as array of %qT", name, type);
9048 return error_mark_node;
9051 /* If there are some types which cannot be array elements,
9052 issue an error-message and return. */
9053 switch (TREE_CODE (type))
9055 case VOID_TYPE:
9056 if (name)
9057 error ("declaration of %qD as array of void", name);
9058 else
9059 error ("creating array of void");
9060 return error_mark_node;
9062 case FUNCTION_TYPE:
9063 if (name)
9064 error ("declaration of %qD as array of functions", name);
9065 else
9066 error ("creating array of functions");
9067 return error_mark_node;
9069 case REFERENCE_TYPE:
9070 if (name)
9071 error ("declaration of %qD as array of references", name);
9072 else
9073 error ("creating array of references");
9074 return error_mark_node;
9076 case METHOD_TYPE:
9077 if (name)
9078 error ("declaration of %qD as array of function members", name);
9079 else
9080 error ("creating array of function members");
9081 return error_mark_node;
9083 default:
9084 break;
9087 /* [dcl.array]
9089 The constant expressions that specify the bounds of the arrays
9090 can be omitted only for the first member of the sequence. */
9091 if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type))
9093 if (name)
9094 error ("declaration of %qD as multidimensional array must "
9095 "have bounds for all dimensions except the first",
9096 name);
9097 else
9098 error ("multidimensional array must have bounds for all "
9099 "dimensions except the first");
9101 return error_mark_node;
9104 /* Figure out the index type for the array. */
9105 if (size)
9106 itype = compute_array_index_type (name, size, tf_warning_or_error);
9108 /* [dcl.array]
9109 T is called the array element type; this type shall not be [...] an
9110 abstract class type. */
9111 abstract_virtuals_error (name, type);
9113 return build_cplus_array_type (type, itype);
9116 /* Returns the smallest location != UNKNOWN_LOCATION among the
9117 three stored in LOCATIONS[ds_const], LOCATIONS[ds_volatile],
9118 and LOCATIONS[ds_restrict]. */
9120 static location_t
9121 smallest_type_quals_location (int type_quals, const location_t* locations)
9123 location_t loc = UNKNOWN_LOCATION;
9125 if (type_quals & TYPE_QUAL_CONST)
9126 loc = locations[ds_const];
9128 if ((type_quals & TYPE_QUAL_VOLATILE)
9129 && (loc == UNKNOWN_LOCATION || locations[ds_volatile] < loc))
9130 loc = locations[ds_volatile];
9132 if ((type_quals & TYPE_QUAL_RESTRICT)
9133 && (loc == UNKNOWN_LOCATION || locations[ds_restrict] < loc))
9134 loc = locations[ds_restrict];
9136 return loc;
9139 /* Check that it's OK to declare a function with the indicated TYPE
9140 and TYPE_QUALS. SFK indicates the kind of special function (if any)
9141 that this function is. OPTYPE is the type given in a conversion
9142 operator declaration, or the class type for a constructor/destructor.
9143 Returns the actual return type of the function; that may be different
9144 than TYPE if an error occurs, or for certain special functions. */
9146 static tree
9147 check_special_function_return_type (special_function_kind sfk,
9148 tree type,
9149 tree optype,
9150 int type_quals,
9151 const location_t* locations)
9153 switch (sfk)
9155 case sfk_constructor:
9156 if (type)
9157 error ("return type specification for constructor invalid");
9158 else if (type_quals != TYPE_UNQUALIFIED)
9159 error_at (smallest_type_quals_location (type_quals, locations),
9160 "qualifiers are not allowed on constructor declaration");
9162 if (targetm.cxx.cdtor_returns_this ())
9163 type = build_pointer_type (optype);
9164 else
9165 type = void_type_node;
9166 break;
9168 case sfk_destructor:
9169 if (type)
9170 error ("return type specification for destructor invalid");
9171 else if (type_quals != TYPE_UNQUALIFIED)
9172 error_at (smallest_type_quals_location (type_quals, locations),
9173 "qualifiers are not allowed on destructor declaration");
9175 /* We can't use the proper return type here because we run into
9176 problems with ambiguous bases and covariant returns. */
9177 if (targetm.cxx.cdtor_returns_this ())
9178 type = build_pointer_type (void_type_node);
9179 else
9180 type = void_type_node;
9181 break;
9183 case sfk_conversion:
9184 if (type)
9185 error ("return type specified for %<operator %T%>", optype);
9186 else if (type_quals != TYPE_UNQUALIFIED)
9187 error_at (smallest_type_quals_location (type_quals, locations),
9188 "qualifiers are not allowed on declaration of "
9189 "%<operator %T%>", optype);
9191 type = optype;
9192 break;
9194 default:
9195 gcc_unreachable ();
9198 return type;
9201 /* A variable or data member (whose unqualified name is IDENTIFIER)
9202 has been declared with the indicated TYPE. If the TYPE is not
9203 acceptable, issue an error message and return a type to use for
9204 error-recovery purposes. */
9206 tree
9207 check_var_type (tree identifier, tree type)
9209 if (VOID_TYPE_P (type))
9211 if (!identifier)
9212 error ("unnamed variable or field declared void");
9213 else if (identifier_p (identifier))
9215 gcc_assert (!IDENTIFIER_OPNAME_P (identifier));
9216 error ("variable or field %qE declared void", identifier);
9218 else
9219 error ("variable or field declared void");
9220 type = error_mark_node;
9223 return type;
9226 /* Given declspecs and a declarator (abstract or otherwise), determine
9227 the name and type of the object declared and construct a DECL node
9228 for it.
9230 DECLSPECS points to the representation of declaration-specifier
9231 sequence that precedes declarator.
9233 DECL_CONTEXT says which syntactic context this declaration is in:
9234 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
9235 FUNCDEF for a function definition. Like NORMAL but a few different
9236 error messages in each case. Return value may be zero meaning
9237 this definition is too screwy to try to parse.
9238 MEMFUNCDEF for a function definition. Like FUNCDEF but prepares to
9239 handle member functions (which have FIELD context).
9240 Return value may be zero meaning this definition is too screwy to
9241 try to parse.
9242 PARM for a parameter declaration (either within a function prototype
9243 or before a function body). Make a PARM_DECL, or return void_type_node.
9244 TPARM for a template parameter declaration.
9245 CATCHPARM for a parameter declaration before a catch clause.
9246 TYPENAME if for a typename (in a cast or sizeof).
9247 Don't make a DECL node; just return the ..._TYPE node.
9248 FIELD for a struct or union field; make a FIELD_DECL.
9249 BITFIELD for a field with specified width.
9251 INITIALIZED is as for start_decl.
9253 ATTRLIST is a pointer to the list of attributes, which may be NULL
9254 if there are none; *ATTRLIST may be modified if attributes from inside
9255 the declarator should be applied to the declaration.
9257 When this function is called, scoping variables (such as
9258 CURRENT_CLASS_TYPE) should reflect the scope in which the
9259 declaration occurs, not the scope in which the new declaration will
9260 be placed. For example, on:
9262 void S::f() { ... }
9264 when grokdeclarator is called for `S::f', the CURRENT_CLASS_TYPE
9265 should not be `S'.
9267 Returns a DECL (if a declarator is present), a TYPE (if there is no
9268 declarator, in cases like "struct S;"), or the ERROR_MARK_NODE if an
9269 error occurs. */
9271 tree
9272 grokdeclarator (const cp_declarator *declarator,
9273 cp_decl_specifier_seq *declspecs,
9274 enum decl_context decl_context,
9275 int initialized,
9276 tree* attrlist)
9278 tree type = NULL_TREE;
9279 int longlong = 0;
9280 int explicit_intN = 0;
9281 int virtualp, explicitp, friendp, inlinep, staticp;
9282 int explicit_int = 0;
9283 int explicit_char = 0;
9284 int defaulted_int = 0;
9286 tree typedef_decl = NULL_TREE;
9287 const char *name = NULL;
9288 tree typedef_type = NULL_TREE;
9289 /* True if this declarator is a function definition. */
9290 bool funcdef_flag = false;
9291 cp_declarator_kind innermost_code = cdk_error;
9292 int bitfield = 0;
9293 #if 0
9294 /* See the code below that used this. */
9295 tree decl_attr = NULL_TREE;
9296 #endif
9298 /* Keep track of what sort of function is being processed
9299 so that we can warn about default return values, or explicit
9300 return values which do not match prescribed defaults. */
9301 special_function_kind sfk = sfk_none;
9303 tree dname = NULL_TREE;
9304 tree ctor_return_type = NULL_TREE;
9305 enum overload_flags flags = NO_SPECIAL;
9306 /* cv-qualifiers that apply to the declarator, for a declaration of
9307 a member function. */
9308 cp_cv_quals memfn_quals = TYPE_UNQUALIFIED;
9309 /* virt-specifiers that apply to the declarator, for a declaration of
9310 a member function. */
9311 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
9312 /* ref-qualifier that applies to the declarator, for a declaration of
9313 a member function. */
9314 cp_ref_qualifier rqual = REF_QUAL_NONE;
9315 /* cv-qualifiers that apply to the type specified by the DECLSPECS. */
9316 int type_quals = TYPE_UNQUALIFIED;
9317 tree raises = NULL_TREE;
9318 int template_count = 0;
9319 tree returned_attrs = NULL_TREE;
9320 tree parms = NULL_TREE;
9321 const cp_declarator *id_declarator;
9322 /* The unqualified name of the declarator; either an
9323 IDENTIFIER_NODE, BIT_NOT_EXPR, or TEMPLATE_ID_EXPR. */
9324 tree unqualified_id;
9325 /* The class type, if any, in which this entity is located,
9326 or NULL_TREE if none. Note that this value may be different from
9327 the current class type; for example if an attempt is made to declare
9328 "A::f" inside "B", this value will be "A". */
9329 tree ctype = current_class_type;
9330 /* The NAMESPACE_DECL for the namespace in which this entity is
9331 located. If an unqualified name is used to declare the entity,
9332 this value will be NULL_TREE, even if the entity is located at
9333 namespace scope. */
9334 tree in_namespace = NULL_TREE;
9335 cp_storage_class storage_class;
9336 bool unsigned_p, signed_p, short_p, long_p, thread_p;
9337 bool type_was_error_mark_node = false;
9338 bool parameter_pack_p = declarator? declarator->parameter_pack_p : false;
9339 bool template_type_arg = false;
9340 bool template_parm_flag = false;
9341 bool typedef_p = decl_spec_seq_has_spec_p (declspecs, ds_typedef);
9342 bool constexpr_p = decl_spec_seq_has_spec_p (declspecs, ds_constexpr);
9343 bool late_return_type_p = false;
9344 bool array_parameter_p = false;
9345 source_location saved_loc = input_location;
9346 tree reqs = NULL_TREE;
9348 signed_p = decl_spec_seq_has_spec_p (declspecs, ds_signed);
9349 unsigned_p = decl_spec_seq_has_spec_p (declspecs, ds_unsigned);
9350 short_p = decl_spec_seq_has_spec_p (declspecs, ds_short);
9351 long_p = decl_spec_seq_has_spec_p (declspecs, ds_long);
9352 longlong = decl_spec_seq_has_spec_p (declspecs, ds_long_long);
9353 explicit_intN = declspecs->explicit_intN_p;
9354 thread_p = decl_spec_seq_has_spec_p (declspecs, ds_thread);
9356 // Was concept_p specified? Note that ds_concept
9357 // implies ds_constexpr!
9358 bool concept_p = decl_spec_seq_has_spec_p (declspecs, ds_concept);
9359 if (concept_p)
9360 constexpr_p = true;
9362 if (decl_spec_seq_has_spec_p (declspecs, ds_const))
9363 type_quals |= TYPE_QUAL_CONST;
9364 if (decl_spec_seq_has_spec_p (declspecs, ds_volatile))
9365 type_quals |= TYPE_QUAL_VOLATILE;
9366 if (decl_spec_seq_has_spec_p (declspecs, ds_restrict))
9367 type_quals |= TYPE_QUAL_RESTRICT;
9369 if (decl_context == FUNCDEF)
9370 funcdef_flag = true, decl_context = NORMAL;
9371 else if (decl_context == MEMFUNCDEF)
9372 funcdef_flag = true, decl_context = FIELD;
9373 else if (decl_context == BITFIELD)
9374 bitfield = 1, decl_context = FIELD;
9375 else if (decl_context == TEMPLATE_TYPE_ARG)
9376 template_type_arg = true, decl_context = TYPENAME;
9377 else if (decl_context == TPARM)
9378 template_parm_flag = true, decl_context = PARM;
9380 if (initialized > 1)
9381 funcdef_flag = true;
9383 /* Look inside a declarator for the name being declared
9384 and get it as a string, for an error message. */
9385 for (id_declarator = declarator;
9386 id_declarator;
9387 id_declarator = id_declarator->declarator)
9389 if (id_declarator->kind != cdk_id)
9390 innermost_code = id_declarator->kind;
9392 switch (id_declarator->kind)
9394 case cdk_function:
9395 if (id_declarator->declarator
9396 && id_declarator->declarator->kind == cdk_id)
9398 sfk = id_declarator->declarator->u.id.sfk;
9399 if (sfk == sfk_destructor)
9400 flags = DTOR_FLAG;
9402 break;
9404 case cdk_id:
9406 tree qualifying_scope = id_declarator->u.id.qualifying_scope;
9407 tree decl = id_declarator->u.id.unqualified_name;
9408 if (!decl)
9409 break;
9410 if (qualifying_scope)
9412 if (at_function_scope_p ())
9414 /* [dcl.meaning]
9416 A declarator-id shall not be qualified except
9417 for ...
9419 None of the cases are permitted in block
9420 scope. */
9421 if (qualifying_scope == global_namespace)
9422 error ("invalid use of qualified-name %<::%D%>",
9423 decl);
9424 else if (TYPE_P (qualifying_scope))
9425 error ("invalid use of qualified-name %<%T::%D%>",
9426 qualifying_scope, decl);
9427 else
9428 error ("invalid use of qualified-name %<%D::%D%>",
9429 qualifying_scope, decl);
9430 return error_mark_node;
9432 else if (TYPE_P (qualifying_scope))
9434 ctype = qualifying_scope;
9435 if (!MAYBE_CLASS_TYPE_P (ctype))
9437 error ("%q#T is not a class or a namespace", ctype);
9438 ctype = NULL_TREE;
9440 else if (innermost_code != cdk_function
9441 && current_class_type
9442 && !uniquely_derived_from_p (ctype,
9443 current_class_type))
9445 error ("invalid use of qualified-name %<%T::%D%>",
9446 qualifying_scope, decl);
9447 return error_mark_node;
9450 else if (TREE_CODE (qualifying_scope) == NAMESPACE_DECL)
9451 in_namespace = qualifying_scope;
9453 switch (TREE_CODE (decl))
9455 case BIT_NOT_EXPR:
9457 tree type;
9459 if (innermost_code != cdk_function)
9461 error ("declaration of %qD as non-function", decl);
9462 return error_mark_node;
9464 else if (!qualifying_scope
9465 && !(current_class_type && at_class_scope_p ()))
9467 error ("declaration of %qD as non-member", decl);
9468 return error_mark_node;
9471 type = TREE_OPERAND (decl, 0);
9472 if (TYPE_P (type))
9473 type = constructor_name (type);
9474 name = identifier_to_locale (IDENTIFIER_POINTER (type));
9475 dname = decl;
9477 break;
9479 case TEMPLATE_ID_EXPR:
9481 tree fns = TREE_OPERAND (decl, 0);
9483 dname = fns;
9484 if (!identifier_p (dname))
9486 if (variable_template_p (dname))
9487 dname = DECL_NAME (dname);
9488 else
9490 gcc_assert (is_overloaded_fn (dname));
9491 dname = DECL_NAME (get_first_fn (dname));
9495 /* Fall through. */
9497 case IDENTIFIER_NODE:
9498 if (identifier_p (decl))
9499 dname = decl;
9501 if (C_IS_RESERVED_WORD (dname))
9503 error ("declarator-id missing; using reserved word %qD",
9504 dname);
9505 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
9507 else if (!IDENTIFIER_TYPENAME_P (dname))
9508 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
9509 else
9511 gcc_assert (flags == NO_SPECIAL);
9512 flags = TYPENAME_FLAG;
9513 ctor_return_type = TREE_TYPE (dname);
9514 sfk = sfk_conversion;
9515 if (is_typename_at_global_scope (dname))
9516 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
9517 else
9518 name = "<invalid operator>";
9520 break;
9522 default:
9523 gcc_unreachable ();
9525 break;
9528 case cdk_array:
9529 case cdk_pointer:
9530 case cdk_reference:
9531 case cdk_ptrmem:
9532 break;
9534 case cdk_error:
9535 return error_mark_node;
9537 default:
9538 gcc_unreachable ();
9540 if (id_declarator->kind == cdk_id)
9541 break;
9544 /* [dcl.fct.edf]
9546 The declarator in a function-definition shall have the form
9547 D1 ( parameter-declaration-clause) ... */
9548 if (funcdef_flag && innermost_code != cdk_function)
9550 error ("function definition does not declare parameters");
9551 return error_mark_node;
9554 if (flags == TYPENAME_FLAG
9555 && innermost_code != cdk_function
9556 && ! (ctype && !declspecs->any_specifiers_p))
9558 error ("declaration of %qD as non-function", dname);
9559 return error_mark_node;
9562 if (dname
9563 && identifier_p (dname)
9564 && UDLIT_OPER_P (dname)
9565 && innermost_code != cdk_function)
9567 error ("declaration of %qD as non-function", dname);
9568 return error_mark_node;
9571 if (dname && IDENTIFIER_OPNAME_P (dname))
9573 if (typedef_p)
9575 error ("declaration of %qD as %<typedef%>", dname);
9576 return error_mark_node;
9578 else if (decl_context == PARM || decl_context == CATCHPARM)
9580 error ("declaration of %qD as parameter", dname);
9581 return error_mark_node;
9585 /* Anything declared one level down from the top level
9586 must be one of the parameters of a function
9587 (because the body is at least two levels down). */
9589 /* This heuristic cannot be applied to C++ nodes! Fixed, however,
9590 by not allowing C++ class definitions to specify their parameters
9591 with xdecls (must be spec.d in the parmlist).
9593 Since we now wait to push a class scope until we are sure that
9594 we are in a legitimate method context, we must set oldcname
9595 explicitly (since current_class_name is not yet alive).
9597 We also want to avoid calling this a PARM if it is in a namespace. */
9599 if (decl_context == NORMAL && !toplevel_bindings_p ())
9601 cp_binding_level *b = current_binding_level;
9602 current_binding_level = b->level_chain;
9603 if (current_binding_level != 0 && toplevel_bindings_p ())
9604 decl_context = PARM;
9605 current_binding_level = b;
9608 if (name == NULL)
9609 name = decl_context == PARM ? "parameter" : "type name";
9611 if (concept_p && typedef_p)
9613 error ("%<concept%> cannot appear in a typedef declaration");
9614 return error_mark_node;
9617 if (constexpr_p && typedef_p)
9619 error ("%<constexpr%> cannot appear in a typedef declaration");
9620 return error_mark_node;
9623 /* If there were multiple types specified in the decl-specifier-seq,
9624 issue an error message. */
9625 if (declspecs->multiple_types_p)
9627 error ("two or more data types in declaration of %qs", name);
9628 return error_mark_node;
9631 if (declspecs->conflicting_specifiers_p)
9633 error ("conflicting specifiers in declaration of %qs", name);
9634 return error_mark_node;
9637 /* Extract the basic type from the decl-specifier-seq. */
9638 type = declspecs->type;
9639 if (type == error_mark_node)
9641 type = NULL_TREE;
9642 type_was_error_mark_node = true;
9644 /* If the entire declaration is itself tagged as deprecated then
9645 suppress reports of deprecated items. */
9646 if (type && TREE_DEPRECATED (type)
9647 && deprecated_state != DEPRECATED_SUPPRESS)
9648 warn_deprecated_use (type, NULL_TREE);
9649 if (type && TREE_CODE (type) == TYPE_DECL)
9651 typedef_decl = type;
9652 type = TREE_TYPE (typedef_decl);
9653 if (TREE_DEPRECATED (type)
9654 && DECL_ARTIFICIAL (typedef_decl)
9655 && deprecated_state != DEPRECATED_SUPPRESS)
9656 warn_deprecated_use (type, NULL_TREE);
9658 /* No type at all: default to `int', and set DEFAULTED_INT
9659 because it was not a user-defined typedef. */
9660 if (type == NULL_TREE)
9662 if (signed_p || unsigned_p || long_p || short_p)
9664 /* These imply 'int'. */
9665 type = integer_type_node;
9666 defaulted_int = 1;
9668 /* If we just have "complex", it is equivalent to "complex double". */
9669 else if (!longlong && !explicit_intN
9670 && decl_spec_seq_has_spec_p (declspecs, ds_complex))
9672 type = double_type_node;
9673 pedwarn (declspecs->locations[ds_complex], OPT_Wpedantic,
9674 "ISO C++ does not support plain %<complex%> meaning "
9675 "%<double complex%>");
9678 /* Gather flags. */
9679 explicit_int = declspecs->explicit_int_p;
9680 explicit_char = declspecs->explicit_char_p;
9682 #if 0
9683 /* See the code below that used this. */
9684 if (typedef_decl)
9685 decl_attr = DECL_ATTRIBUTES (typedef_decl);
9686 #endif
9687 typedef_type = type;
9690 if (sfk != sfk_conversion)
9691 ctor_return_type = ctype;
9693 if (sfk != sfk_none)
9695 type = check_special_function_return_type (sfk, type,
9696 ctor_return_type,
9697 type_quals,
9698 declspecs->locations);
9699 type_quals = TYPE_UNQUALIFIED;
9701 else if (type == NULL_TREE)
9703 int is_main;
9705 explicit_int = -1;
9707 /* We handle `main' specially here, because 'main () { }' is so
9708 common. With no options, it is allowed. With -Wreturn-type,
9709 it is a warning. It is only an error with -pedantic-errors. */
9710 is_main = (funcdef_flag
9711 && dname && identifier_p (dname)
9712 && MAIN_NAME_P (dname)
9713 && ctype == NULL_TREE
9714 && in_namespace == NULL_TREE
9715 && current_namespace == global_namespace);
9717 if (type_was_error_mark_node)
9718 /* We've already issued an error, don't complain more. */;
9719 else if (in_system_header_at (input_location) || flag_ms_extensions)
9720 /* Allow it, sigh. */;
9721 else if (! is_main)
9722 permerror (input_location, "ISO C++ forbids declaration of %qs with no type", name);
9723 else if (pedantic)
9724 pedwarn (input_location, OPT_Wpedantic,
9725 "ISO C++ forbids declaration of %qs with no type", name);
9726 else
9727 warning (OPT_Wreturn_type,
9728 "ISO C++ forbids declaration of %qs with no type", name);
9730 if (type_was_error_mark_node && template_parm_flag)
9731 /* FIXME we should be able to propagate the error_mark_node as is
9732 for other contexts too. */
9733 type = error_mark_node;
9734 else
9735 type = integer_type_node;
9738 ctype = NULL_TREE;
9740 if (explicit_intN)
9742 if (! int_n_enabled_p[declspecs->int_n_idx])
9744 error ("%<__int%d%> is not supported by this target",
9745 int_n_data[declspecs->int_n_idx].bitsize);
9746 explicit_intN = false;
9748 else if (pedantic && ! in_system_header_at (input_location))
9749 pedwarn (input_location, OPT_Wpedantic,
9750 "ISO C++ does not support %<__int%d%> for %qs",
9751 int_n_data[declspecs->int_n_idx].bitsize, name);
9754 /* Now process the modifiers that were specified
9755 and check for invalid combinations. */
9757 /* Long double is a special combination. */
9758 if (long_p && !longlong && TYPE_MAIN_VARIANT (type) == double_type_node)
9760 long_p = false;
9761 type = cp_build_qualified_type (long_double_type_node,
9762 cp_type_quals (type));
9765 /* Check all other uses of type modifiers. */
9767 if (unsigned_p || signed_p || long_p || short_p)
9769 int ok = 0;
9771 if ((signed_p || unsigned_p) && TREE_CODE (type) != INTEGER_TYPE)
9772 error ("%<signed%> or %<unsigned%> invalid for %qs", name);
9773 else if (signed_p && unsigned_p)
9774 error ("%<signed%> and %<unsigned%> specified together for %qs", name);
9775 else if (longlong && TREE_CODE (type) != INTEGER_TYPE)
9776 error ("%<long long%> invalid for %qs", name);
9777 else if (long_p && TREE_CODE (type) == REAL_TYPE)
9778 error ("%<long%> invalid for %qs", name);
9779 else if (short_p && TREE_CODE (type) == REAL_TYPE)
9780 error ("%<short%> invalid for %qs", name);
9781 else if ((long_p || short_p) && TREE_CODE (type) != INTEGER_TYPE)
9782 error ("%<long%> or %<short%> invalid for %qs", name);
9783 else if ((long_p || short_p || explicit_char || explicit_int) && explicit_intN)
9784 error ("%<long%>, %<int%>, %<short%>, or %<char%> invalid for %qs", name);
9785 else if ((long_p || short_p) && explicit_char)
9786 error ("%<long%> or %<short%> specified with char for %qs", name);
9787 else if (long_p && short_p)
9788 error ("%<long%> and %<short%> specified together for %qs", name);
9789 else if (type == char16_type_node || type == char32_type_node)
9791 if (signed_p || unsigned_p)
9792 error ("%<signed%> or %<unsigned%> invalid for %qs", name);
9793 else if (short_p || long_p)
9794 error ("%<short%> or %<long%> invalid for %qs", name);
9796 else
9798 ok = 1;
9799 if (!explicit_int && !defaulted_int && !explicit_char && !explicit_intN && pedantic)
9801 pedwarn (input_location, OPT_Wpedantic,
9802 "long, short, signed or unsigned used invalidly for %qs",
9803 name);
9804 if (flag_pedantic_errors)
9805 ok = 0;
9809 /* Discard the type modifiers if they are invalid. */
9810 if (! ok)
9812 unsigned_p = false;
9813 signed_p = false;
9814 long_p = false;
9815 short_p = false;
9816 longlong = 0;
9820 /* Decide whether an integer type is signed or not.
9821 Optionally treat bitfields as signed by default. */
9822 if (unsigned_p
9823 /* [class.bit]
9825 It is implementation-defined whether a plain (neither
9826 explicitly signed or unsigned) char, short, int, or long
9827 bit-field is signed or unsigned.
9829 Naturally, we extend this to long long as well. Note that
9830 this does not include wchar_t. */
9831 || (bitfield && !flag_signed_bitfields
9832 && !signed_p
9833 /* A typedef for plain `int' without `signed' can be
9834 controlled just like plain `int', but a typedef for
9835 `signed int' cannot be so controlled. */
9836 && !(typedef_decl
9837 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl))
9838 && TREE_CODE (type) == INTEGER_TYPE
9839 && !same_type_p (TYPE_MAIN_VARIANT (type), wchar_type_node)))
9841 if (explicit_intN)
9842 type = int_n_trees[declspecs->int_n_idx].unsigned_type;
9843 else if (longlong)
9844 type = long_long_unsigned_type_node;
9845 else if (long_p)
9846 type = long_unsigned_type_node;
9847 else if (short_p)
9848 type = short_unsigned_type_node;
9849 else if (type == char_type_node)
9850 type = unsigned_char_type_node;
9851 else if (typedef_decl)
9852 type = unsigned_type_for (type);
9853 else
9854 type = unsigned_type_node;
9856 else if (signed_p && type == char_type_node)
9857 type = signed_char_type_node;
9858 else if (explicit_intN)
9859 type = int_n_trees[declspecs->int_n_idx].signed_type;
9860 else if (longlong)
9861 type = long_long_integer_type_node;
9862 else if (long_p)
9863 type = long_integer_type_node;
9864 else if (short_p)
9865 type = short_integer_type_node;
9867 if (decl_spec_seq_has_spec_p (declspecs, ds_complex))
9869 if (TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
9870 error ("complex invalid for %qs", name);
9871 /* If a modifier is specified, the resulting complex is the complex
9872 form of TYPE. E.g, "complex short" is "complex short int". */
9873 else if (type == integer_type_node)
9874 type = complex_integer_type_node;
9875 else if (type == float_type_node)
9876 type = complex_float_type_node;
9877 else if (type == double_type_node)
9878 type = complex_double_type_node;
9879 else if (type == long_double_type_node)
9880 type = complex_long_double_type_node;
9881 else
9882 type = build_complex_type (type);
9885 /* If we're using the injected-class-name to form a compound type or a
9886 declaration, replace it with the underlying class so we don't get
9887 redundant typedefs in the debug output. But if we are returning the
9888 type unchanged, leave it alone so that it's available to
9889 maybe_get_template_decl_from_type_decl. */
9890 if (CLASS_TYPE_P (type)
9891 && DECL_SELF_REFERENCE_P (TYPE_NAME (type))
9892 && type == TREE_TYPE (TYPE_NAME (type))
9893 && (declarator || type_quals))
9894 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
9896 type_quals |= cp_type_quals (type);
9897 type = cp_build_qualified_type_real
9898 (type, type_quals, ((((typedef_decl && !DECL_ARTIFICIAL (typedef_decl))
9899 || declspecs->decltype_p)
9900 ? tf_ignore_bad_quals : 0) | tf_warning_or_error));
9901 /* We might have ignored or rejected some of the qualifiers. */
9902 type_quals = cp_type_quals (type);
9904 staticp = 0;
9905 inlinep = decl_spec_seq_has_spec_p (declspecs, ds_inline);
9906 virtualp = decl_spec_seq_has_spec_p (declspecs, ds_virtual);
9907 explicitp = decl_spec_seq_has_spec_p (declspecs, ds_explicit);
9909 storage_class = declspecs->storage_class;
9910 if (storage_class == sc_static)
9911 staticp = 1 + (decl_context == FIELD);
9913 if (virtualp)
9915 if (staticp == 2)
9917 error ("member %qD cannot be declared both %<virtual%> "
9918 "and %<static%>", dname);
9919 storage_class = sc_none;
9920 staticp = 0;
9922 if (constexpr_p)
9923 error ("member %qD cannot be declared both %<virtual%> "
9924 "and %<constexpr%>", dname);
9926 friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
9928 /* Issue errors about use of storage classes for parameters. */
9929 if (decl_context == PARM)
9931 if (typedef_p)
9933 error ("typedef declaration invalid in parameter declaration");
9934 return error_mark_node;
9936 else if (template_parm_flag && storage_class != sc_none)
9938 error ("storage class specified for template parameter %qs", name);
9939 return error_mark_node;
9941 else if (storage_class == sc_static
9942 || storage_class == sc_extern
9943 || thread_p)
9944 error ("storage class specifiers invalid in parameter declarations");
9946 /* Function parameters cannot be concept. */
9947 if (concept_p)
9948 error ("a parameter cannot be declared %<concept%>");
9949 /* Function parameters cannot be constexpr. If we saw one, moan
9950 and pretend it wasn't there. */
9951 else if (constexpr_p)
9953 error ("a parameter cannot be declared %<constexpr%>");
9954 constexpr_p = 0;
9958 /* Give error if `virtual' is used outside of class declaration. */
9959 if (virtualp
9960 && (current_class_name == NULL_TREE || decl_context != FIELD))
9962 error_at (declspecs->locations[ds_virtual],
9963 "%<virtual%> outside class declaration");
9964 virtualp = 0;
9967 /* Static anonymous unions are dealt with here. */
9968 if (staticp && decl_context == TYPENAME
9969 && declspecs->type
9970 && ANON_AGGR_TYPE_P (declspecs->type))
9971 decl_context = FIELD;
9973 /* Warn about storage classes that are invalid for certain
9974 kinds of declarations (parameters, typenames, etc.). */
9975 if (thread_p
9976 && ((storage_class
9977 && storage_class != sc_extern
9978 && storage_class != sc_static)
9979 || typedef_p))
9981 error ("multiple storage classes in declaration of %qs", name);
9982 thread_p = false;
9984 if (decl_context != NORMAL
9985 && ((storage_class != sc_none
9986 && storage_class != sc_mutable)
9987 || thread_p))
9989 if ((decl_context == PARM || decl_context == CATCHPARM)
9990 && (storage_class == sc_register
9991 || storage_class == sc_auto))
9993 else if (typedef_p)
9995 else if (decl_context == FIELD
9996 /* C++ allows static class elements. */
9997 && storage_class == sc_static)
9998 /* C++ also allows inlines and signed and unsigned elements,
9999 but in those cases we don't come in here. */
10001 else
10003 if (decl_context == FIELD)
10004 error ("storage class specified for %qs", name);
10005 else
10007 if (decl_context == PARM || decl_context == CATCHPARM)
10008 error ("storage class specified for parameter %qs", name);
10009 else
10010 error ("storage class specified for typename");
10012 if (storage_class == sc_register
10013 || storage_class == sc_auto
10014 || storage_class == sc_extern
10015 || thread_p)
10016 storage_class = sc_none;
10019 else if (storage_class == sc_extern && funcdef_flag
10020 && ! toplevel_bindings_p ())
10021 error ("nested function %qs declared %<extern%>", name);
10022 else if (toplevel_bindings_p ())
10024 if (storage_class == sc_auto)
10025 error ("top-level declaration of %qs specifies %<auto%>", name);
10027 else if (thread_p
10028 && storage_class != sc_extern
10029 && storage_class != sc_static)
10031 if (declspecs->gnu_thread_keyword_p)
10032 pedwarn (input_location, 0, "function-scope %qs implicitly auto and "
10033 "declared %<__thread%>", name);
10035 /* When thread_local is applied to a variable of block scope the
10036 storage-class-specifier static is implied if it does not appear
10037 explicitly. */
10038 storage_class = declspecs->storage_class = sc_static;
10039 staticp = 1;
10042 if (storage_class && friendp)
10044 error ("storage class specifiers invalid in friend function declarations");
10045 storage_class = sc_none;
10046 staticp = 0;
10049 if (!id_declarator)
10050 unqualified_id = NULL_TREE;
10051 else
10053 unqualified_id = id_declarator->u.id.unqualified_name;
10054 switch (TREE_CODE (unqualified_id))
10056 case BIT_NOT_EXPR:
10057 unqualified_id = TREE_OPERAND (unqualified_id, 0);
10058 if (TYPE_P (unqualified_id))
10059 unqualified_id = constructor_name (unqualified_id);
10060 break;
10062 case IDENTIFIER_NODE:
10063 case TEMPLATE_ID_EXPR:
10064 break;
10066 default:
10067 gcc_unreachable ();
10071 if (declspecs->std_attributes)
10073 /* Apply the c++11 attributes to the type preceding them. */
10074 input_location = declspecs->locations[ds_std_attribute];
10075 decl_attributes (&type, declspecs->std_attributes, 0);
10076 input_location = saved_loc;
10079 /* Determine the type of the entity declared by recurring on the
10080 declarator. */
10081 for (; declarator; declarator = declarator->declarator)
10083 const cp_declarator *inner_declarator;
10084 tree attrs;
10086 if (type == error_mark_node)
10087 return error_mark_node;
10089 attrs = declarator->attributes;
10090 if (attrs)
10092 int attr_flags;
10094 attr_flags = 0;
10095 if (declarator == NULL || declarator->kind == cdk_id)
10096 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
10097 if (declarator->kind == cdk_function)
10098 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
10099 if (declarator->kind == cdk_array)
10100 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
10101 returned_attrs = decl_attributes (&type,
10102 chainon (returned_attrs, attrs),
10103 attr_flags);
10106 if (declarator->kind == cdk_id)
10107 break;
10109 inner_declarator = declarator->declarator;
10111 switch (declarator->kind)
10113 case cdk_array:
10114 type = create_array_type_for_decl (dname, type,
10115 declarator->u.array.bounds);
10116 if (!valid_array_size_p (input_location, type, dname))
10117 type = error_mark_node;
10119 if (declarator->std_attributes)
10120 /* [dcl.array]/1:
10122 The optional attribute-specifier-seq appertains to the
10123 array. */
10124 returned_attrs = chainon (returned_attrs,
10125 declarator->std_attributes);
10126 break;
10128 case cdk_function:
10130 tree arg_types;
10131 int funcdecl_p;
10133 /* Declaring a function type.
10134 Make sure we have a valid type for the function to return. */
10136 if (type_quals != TYPE_UNQUALIFIED)
10138 if (SCALAR_TYPE_P (type) || VOID_TYPE_P (type))
10139 warning (OPT_Wignored_qualifiers,
10140 "type qualifiers ignored on function return type");
10141 /* We now know that the TYPE_QUALS don't apply to the
10142 decl, but to its return type. */
10143 type_quals = TYPE_UNQUALIFIED;
10146 /* Error about some types functions can't return. */
10148 if (TREE_CODE (type) == FUNCTION_TYPE)
10150 error ("%qs declared as function returning a function", name);
10151 return error_mark_node;
10153 if (TREE_CODE (type) == ARRAY_TYPE)
10155 error ("%qs declared as function returning an array", name);
10156 return error_mark_node;
10159 input_location = declspecs->locations[ds_type_spec];
10160 abstract_virtuals_error (ACU_RETURN, type);
10161 input_location = saved_loc;
10163 /* Pick up type qualifiers which should be applied to `this'. */
10164 memfn_quals = declarator->u.function.qualifiers;
10165 /* Pick up virt-specifiers. */
10166 virt_specifiers = declarator->u.function.virt_specifiers;
10167 /* And ref-qualifier, too */
10168 rqual = declarator->u.function.ref_qualifier;
10169 /* And tx-qualifier. */
10170 tree tx_qual = declarator->u.function.tx_qualifier;
10171 /* Pick up the exception specifications. */
10172 raises = declarator->u.function.exception_specification;
10173 /* If the exception-specification is ill-formed, let's pretend
10174 there wasn't one. */
10175 if (raises == error_mark_node)
10176 raises = NULL_TREE;
10178 if (reqs)
10179 error_at (location_of (reqs), "requires-clause on return type");
10180 reqs = declarator->u.function.requires_clause;
10182 /* Say it's a definition only for the CALL_EXPR
10183 closest to the identifier. */
10184 funcdecl_p = inner_declarator && inner_declarator->kind == cdk_id;
10186 /* Handle a late-specified return type. */
10187 if (funcdecl_p)
10189 if (type_uses_auto (type))
10191 if (!declarator->u.function.late_return_type)
10193 if (current_class_type
10194 && LAMBDA_TYPE_P (current_class_type))
10195 /* OK for C++11 lambdas. */;
10196 else if (cxx_dialect < cxx14)
10198 error ("%qs function uses "
10199 "%<auto%> type specifier without trailing "
10200 "return type", name);
10201 inform (input_location, "deduced return type "
10202 "only available with -std=c++14 or "
10203 "-std=gnu++14");
10205 else if (virtualp)
10207 error ("virtual function cannot "
10208 "have deduced return type");
10209 virtualp = false;
10212 else if (!is_auto (type) && sfk != sfk_conversion)
10214 error ("%qs function with trailing return type has"
10215 " %qT as its type rather than plain %<auto%>",
10216 name, type);
10217 return error_mark_node;
10220 else if (declarator->u.function.late_return_type
10221 && sfk != sfk_conversion)
10223 if (cxx_dialect < cxx11)
10224 /* Not using maybe_warn_cpp0x because this should
10225 always be an error. */
10226 error ("trailing return type only available with "
10227 "-std=c++11 or -std=gnu++11");
10228 else
10229 error ("%qs function with trailing return type not "
10230 "declared with %<auto%> type specifier", name);
10231 return error_mark_node;
10234 type = splice_late_return_type
10235 (type, declarator->u.function.late_return_type);
10236 if (type == error_mark_node)
10237 return error_mark_node;
10239 if (declarator->u.function.late_return_type)
10240 late_return_type_p = true;
10242 if (ctype == NULL_TREE
10243 && decl_context == FIELD
10244 && funcdecl_p
10245 && friendp == 0)
10246 ctype = current_class_type;
10248 if (ctype && (sfk == sfk_constructor
10249 || sfk == sfk_destructor))
10251 /* We are within a class's scope. If our declarator name
10252 is the same as the class name, and we are defining
10253 a function, then it is a constructor/destructor, and
10254 therefore returns a void type. */
10256 /* ISO C++ 12.4/2. A destructor may not be declared
10257 const or volatile. A destructor may not be static.
10258 A destructor may not be declared with ref-qualifier.
10260 ISO C++ 12.1. A constructor may not be declared
10261 const or volatile. A constructor may not be
10262 virtual. A constructor may not be static.
10263 A constructor may not be declared with ref-qualifier. */
10264 if (staticp == 2)
10265 error ((flags == DTOR_FLAG)
10266 ? G_("destructor cannot be static member function")
10267 : G_("constructor cannot be static member function"));
10268 if (memfn_quals)
10270 error ((flags == DTOR_FLAG)
10271 ? G_("destructors may not be cv-qualified")
10272 : G_("constructors may not be cv-qualified"));
10273 memfn_quals = TYPE_UNQUALIFIED;
10276 if (rqual)
10278 maybe_warn_cpp0x (CPP0X_REF_QUALIFIER);
10279 error ((flags == DTOR_FLAG)
10280 ? "destructors may not be ref-qualified"
10281 : "constructors may not be ref-qualified");
10282 rqual = REF_QUAL_NONE;
10285 if (decl_context == FIELD
10286 && !member_function_or_else (ctype,
10287 current_class_type,
10288 flags))
10289 return error_mark_node;
10291 if (flags != DTOR_FLAG)
10293 /* It's a constructor. */
10294 if (explicitp == 1)
10295 explicitp = 2;
10296 if (virtualp)
10298 permerror (input_location,
10299 "constructors cannot be declared %<virtual%>");
10300 virtualp = 0;
10302 if (decl_context == FIELD
10303 && sfk != sfk_constructor)
10304 return error_mark_node;
10306 if (decl_context == FIELD)
10307 staticp = 0;
10309 else if (friendp)
10311 if (virtualp)
10313 /* Cannot be both friend and virtual. */
10314 error ("virtual functions cannot be friends");
10315 friendp = 0;
10317 if (decl_context == NORMAL)
10318 error ("friend declaration not in class definition");
10319 if (current_function_decl && funcdef_flag)
10320 error ("can%'t define friend function %qs in a local "
10321 "class definition",
10322 name);
10324 else if (ctype && sfk == sfk_conversion)
10326 if (explicitp == 1)
10328 maybe_warn_cpp0x (CPP0X_EXPLICIT_CONVERSION);
10329 explicitp = 2;
10331 if (late_return_type_p)
10332 error ("a conversion function cannot have a trailing return type");
10335 arg_types = grokparms (declarator->u.function.parameters,
10336 &parms);
10338 if (inner_declarator
10339 && inner_declarator->kind == cdk_id
10340 && inner_declarator->u.id.sfk == sfk_destructor
10341 && arg_types != void_list_node)
10343 error ("destructors may not have parameters");
10344 arg_types = void_list_node;
10345 parms = NULL_TREE;
10348 type = build_function_type (type, arg_types);
10350 tree attrs = declarator->std_attributes;
10351 if (tx_qual)
10353 tree att = build_tree_list (tx_qual, NULL_TREE);
10354 /* transaction_safe applies to the type, but
10355 transaction_safe_dynamic applies to the function. */
10356 if (is_attribute_p ("transaction_safe", tx_qual))
10357 attrs = chainon (attrs, att);
10358 else
10359 returned_attrs = chainon (returned_attrs, att);
10361 if (attrs)
10362 /* [dcl.fct]/2:
10364 The optional attribute-specifier-seq appertains to
10365 the function type. */
10366 decl_attributes (&type, attrs, 0);
10368 break;
10370 case cdk_pointer:
10371 case cdk_reference:
10372 case cdk_ptrmem:
10373 /* Filter out pointers-to-references and references-to-references.
10374 We can get these if a TYPE_DECL is used. */
10376 if (TREE_CODE (type) == REFERENCE_TYPE)
10378 if (declarator->kind != cdk_reference)
10380 error ("cannot declare pointer to %q#T", type);
10381 type = TREE_TYPE (type);
10384 /* In C++0x, we allow reference to reference declarations
10385 that occur indirectly through typedefs [7.1.3/8 dcl.typedef]
10386 and template type arguments [14.3.1/4 temp.arg.type]. The
10387 check for direct reference to reference declarations, which
10388 are still forbidden, occurs below. Reasoning behind the change
10389 can be found in DR106, DR540, and the rvalue reference
10390 proposals. */
10391 else if (cxx_dialect == cxx98)
10393 error ("cannot declare reference to %q#T", type);
10394 type = TREE_TYPE (type);
10397 else if (VOID_TYPE_P (type))
10399 if (declarator->kind == cdk_reference)
10400 error ("cannot declare reference to %q#T", type);
10401 else if (declarator->kind == cdk_ptrmem)
10402 error ("cannot declare pointer to %q#T member", type);
10405 /* We now know that the TYPE_QUALS don't apply to the decl,
10406 but to the target of the pointer. */
10407 type_quals = TYPE_UNQUALIFIED;
10409 /* This code used to handle METHOD_TYPE, but I don't think it's
10410 possible to get it here anymore. */
10411 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
10412 if (declarator->kind == cdk_ptrmem
10413 && TREE_CODE (type) == FUNCTION_TYPE)
10415 memfn_quals |= type_memfn_quals (type);
10416 type = build_memfn_type (type,
10417 declarator->u.pointer.class_type,
10418 memfn_quals,
10419 rqual);
10420 if (type == error_mark_node)
10421 return error_mark_node;
10423 rqual = REF_QUAL_NONE;
10424 memfn_quals = TYPE_UNQUALIFIED;
10427 if (TREE_CODE (type) == FUNCTION_TYPE
10428 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
10429 || type_memfn_rqual (type) != REF_QUAL_NONE))
10430 error (declarator->kind == cdk_reference
10431 ? G_("cannot declare reference to qualified function type %qT")
10432 : G_("cannot declare pointer to qualified function type %qT"),
10433 type);
10435 /* When the pointed-to type involves components of variable size,
10436 care must be taken to ensure that the size evaluation code is
10437 emitted early enough to dominate all the possible later uses
10438 and late enough for the variables on which it depends to have
10439 been assigned.
10441 This is expected to happen automatically when the pointed-to
10442 type has a name/declaration of it's own, but special attention
10443 is required if the type is anonymous.
10445 We handle the NORMAL and FIELD contexts here by inserting a
10446 dummy statement that just evaluates the size at a safe point
10447 and ensures it is not deferred until e.g. within a deeper
10448 conditional context (c++/43555).
10450 We expect nothing to be needed here for PARM or TYPENAME.
10451 Evaluating the size at this point for TYPENAME would
10452 actually be incorrect, as we might be in the middle of an
10453 expression with side effects on the pointed-to type size
10454 "arguments" prior to the pointer declaration point and the
10455 size evaluation could end up prior to the side effects. */
10457 if (!TYPE_NAME (type)
10458 && (decl_context == NORMAL || decl_context == FIELD)
10459 && at_function_scope_p ()
10460 && variably_modified_type_p (type, NULL_TREE))
10462 TYPE_NAME (type) = build_decl (UNKNOWN_LOCATION, TYPE_DECL,
10463 NULL_TREE, type);
10464 add_decl_expr (TYPE_NAME (type));
10467 if (declarator->kind == cdk_reference)
10469 /* In C++0x, the type we are creating a reference to might be
10470 a typedef which is itself a reference type. In that case,
10471 we follow the reference collapsing rules in
10472 [7.1.3/8 dcl.typedef] to create the final reference type:
10474 "If a typedef TD names a type that is a reference to a type
10475 T, an attempt to create the type 'lvalue reference to cv TD'
10476 creates the type 'lvalue reference to T,' while an attempt
10477 to create the type "rvalue reference to cv TD' creates the
10478 type TD."
10480 if (VOID_TYPE_P (type))
10481 /* We already gave an error. */;
10482 else if (TREE_CODE (type) == REFERENCE_TYPE)
10484 if (declarator->u.reference.rvalue_ref)
10485 /* Leave type alone. */;
10486 else
10487 type = cp_build_reference_type (TREE_TYPE (type), false);
10489 else
10490 type = cp_build_reference_type
10491 (type, declarator->u.reference.rvalue_ref);
10493 /* In C++0x, we need this check for direct reference to
10494 reference declarations, which are forbidden by
10495 [8.3.2/5 dcl.ref]. Reference to reference declarations
10496 are only allowed indirectly through typedefs and template
10497 type arguments. Example:
10499 void foo(int & &); // invalid ref-to-ref decl
10501 typedef int & int_ref;
10502 void foo(int_ref &); // valid ref-to-ref decl
10504 if (inner_declarator && inner_declarator->kind == cdk_reference)
10505 error ("cannot declare reference to %q#T, which is not "
10506 "a typedef or a template type argument", type);
10508 else if (TREE_CODE (type) == METHOD_TYPE)
10509 type = build_ptrmemfunc_type (build_pointer_type (type));
10510 else if (declarator->kind == cdk_ptrmem)
10512 gcc_assert (TREE_CODE (declarator->u.pointer.class_type)
10513 != NAMESPACE_DECL);
10514 if (declarator->u.pointer.class_type == error_mark_node)
10515 /* We will already have complained. */
10516 type = error_mark_node;
10517 else
10518 type = build_ptrmem_type (declarator->u.pointer.class_type,
10519 type);
10521 else
10522 type = build_pointer_type (type);
10524 /* Process a list of type modifier keywords (such as
10525 const or volatile) that were given inside the `*' or `&'. */
10527 if (declarator->u.pointer.qualifiers)
10529 type
10530 = cp_build_qualified_type (type,
10531 declarator->u.pointer.qualifiers);
10532 type_quals = cp_type_quals (type);
10535 /* Apply C++11 attributes to the pointer, and not to the
10536 type pointed to. This is unlike what is done for GNU
10537 attributes above. It is to comply with [dcl.ptr]/1:
10539 [the optional attribute-specifier-seq (7.6.1) appertains
10540 to the pointer and not to the object pointed to]. */
10541 if (declarator->std_attributes)
10542 decl_attributes (&type, declarator->std_attributes,
10545 ctype = NULL_TREE;
10546 break;
10548 case cdk_error:
10549 break;
10551 default:
10552 gcc_unreachable ();
10556 /* A `constexpr' specifier used in an object declaration declares
10557 the object as `const'. */
10558 if (constexpr_p && innermost_code != cdk_function)
10560 /* DR1688 says that a `constexpr' specifier in combination with
10561 `volatile' is valid. */
10563 if (TREE_CODE (type) != REFERENCE_TYPE)
10565 type_quals |= TYPE_QUAL_CONST;
10566 type = cp_build_qualified_type (type, type_quals);
10570 if (unqualified_id && TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR
10571 && TREE_CODE (type) != FUNCTION_TYPE
10572 && TREE_CODE (type) != METHOD_TYPE
10573 && !variable_template_p (TREE_OPERAND (unqualified_id, 0)))
10575 error ("template-id %qD used as a declarator",
10576 unqualified_id);
10577 unqualified_id = dname;
10580 /* If TYPE is a FUNCTION_TYPE, but the function name was explicitly
10581 qualified with a class-name, turn it into a METHOD_TYPE, unless
10582 we know that the function is static. We take advantage of this
10583 opportunity to do other processing that pertains to entities
10584 explicitly declared to be class members. Note that if DECLARATOR
10585 is non-NULL, we know it is a cdk_id declarator; otherwise, we
10586 would not have exited the loop above. */
10587 if (declarator
10588 && declarator->u.id.qualifying_scope
10589 && MAYBE_CLASS_TYPE_P (declarator->u.id.qualifying_scope))
10591 ctype = declarator->u.id.qualifying_scope;
10592 ctype = TYPE_MAIN_VARIANT (ctype);
10593 template_count = num_template_headers_for_class (ctype);
10595 if (ctype == current_class_type)
10597 if (friendp)
10599 permerror (input_location, "member functions are implicitly friends of their class");
10600 friendp = 0;
10602 else
10603 permerror (declarator->id_loc,
10604 "extra qualification %<%T::%> on member %qs",
10605 ctype, name);
10607 else if (/* If the qualifying type is already complete, then we
10608 can skip the following checks. */
10609 !COMPLETE_TYPE_P (ctype)
10610 && (/* If the function is being defined, then
10611 qualifying type must certainly be complete. */
10612 funcdef_flag
10613 /* A friend declaration of "T::f" is OK, even if
10614 "T" is a template parameter. But, if this
10615 function is not a friend, the qualifying type
10616 must be a class. */
10617 || (!friendp && !CLASS_TYPE_P (ctype))
10618 /* For a declaration, the type need not be
10619 complete, if either it is dependent (since there
10620 is no meaningful definition of complete in that
10621 case) or the qualifying class is currently being
10622 defined. */
10623 || !(dependent_type_p (ctype)
10624 || currently_open_class (ctype)))
10625 /* Check that the qualifying type is complete. */
10626 && !complete_type_or_else (ctype, NULL_TREE))
10627 return error_mark_node;
10628 else if (TREE_CODE (type) == FUNCTION_TYPE)
10630 if (current_class_type
10631 && (!friendp || funcdef_flag))
10633 error (funcdef_flag
10634 ? G_("cannot define member function %<%T::%s%> "
10635 "within %<%T%>")
10636 : G_("cannot declare member function %<%T::%s%> "
10637 "within %<%T%>"),
10638 ctype, name, current_class_type);
10639 return error_mark_node;
10642 else if (typedef_p && current_class_type)
10644 error ("cannot declare member %<%T::%s%> within %qT",
10645 ctype, name, current_class_type);
10646 return error_mark_node;
10650 if (ctype == NULL_TREE && decl_context == FIELD && friendp == 0)
10651 ctype = current_class_type;
10653 /* Now TYPE has the actual type. */
10655 if (returned_attrs)
10657 if (attrlist)
10658 *attrlist = chainon (returned_attrs, *attrlist);
10659 else
10660 attrlist = &returned_attrs;
10663 if (declarator
10664 && declarator->kind == cdk_id
10665 && declarator->std_attributes)
10666 /* [dcl.meaning]/1: The optional attribute-specifier-seq following
10667 a declarator-id appertains to the entity that is declared. */
10668 *attrlist = chainon (*attrlist, declarator->std_attributes);
10670 /* Handle parameter packs. */
10671 if (parameter_pack_p)
10673 if (decl_context == PARM)
10674 /* Turn the type into a pack expansion.*/
10675 type = make_pack_expansion (type);
10676 else
10677 error ("non-parameter %qs cannot be a parameter pack", name);
10680 if ((decl_context == FIELD || decl_context == PARM)
10681 && !processing_template_decl
10682 && variably_modified_type_p (type, NULL_TREE))
10684 if (decl_context == FIELD)
10685 error ("data member may not have variably modified type %qT", type);
10686 else
10687 error ("parameter may not have variably modified type %qT", type);
10688 type = error_mark_node;
10691 if (explicitp == 1 || (explicitp && friendp))
10693 /* [dcl.fct.spec] (C++11) The explicit specifier shall be used only
10694 in the declaration of a constructor or conversion function within
10695 a class definition. */
10696 if (!current_class_type)
10697 error_at (declspecs->locations[ds_explicit],
10698 "%<explicit%> outside class declaration");
10699 else if (friendp)
10700 error_at (declspecs->locations[ds_explicit],
10701 "%<explicit%> in friend declaration");
10702 else
10703 error_at (declspecs->locations[ds_explicit],
10704 "only declarations of constructors and conversion operators "
10705 "can be %<explicit%>");
10706 explicitp = 0;
10709 if (storage_class == sc_mutable)
10711 if (decl_context != FIELD || friendp)
10713 error ("non-member %qs cannot be declared %<mutable%>", name);
10714 storage_class = sc_none;
10716 else if (decl_context == TYPENAME || typedef_p)
10718 error ("non-object member %qs cannot be declared %<mutable%>", name);
10719 storage_class = sc_none;
10721 else if (TREE_CODE (type) == FUNCTION_TYPE
10722 || TREE_CODE (type) == METHOD_TYPE)
10724 error ("function %qs cannot be declared %<mutable%>", name);
10725 storage_class = sc_none;
10727 else if (staticp)
10729 error ("static %qs cannot be declared %<mutable%>", name);
10730 storage_class = sc_none;
10732 else if (type_quals & TYPE_QUAL_CONST)
10734 error ("const %qs cannot be declared %<mutable%>", name);
10735 storage_class = sc_none;
10737 else if (TREE_CODE (type) == REFERENCE_TYPE)
10739 permerror (input_location, "reference %qs cannot be declared "
10740 "%<mutable%>", name);
10741 storage_class = sc_none;
10745 /* If this is declaring a typedef name, return a TYPE_DECL. */
10746 if (typedef_p && decl_context != TYPENAME)
10748 tree decl;
10750 /* This declaration:
10752 typedef void f(int) const;
10754 declares a function type which is not a member of any
10755 particular class, but which is cv-qualified; for
10756 example "f S::*" declares a pointer to a const-qualified
10757 member function of S. We record the cv-qualification in the
10758 function type. */
10759 if ((rqual || memfn_quals) && TREE_CODE (type) == FUNCTION_TYPE)
10761 type = apply_memfn_quals (type, memfn_quals, rqual);
10763 /* We have now dealt with these qualifiers. */
10764 memfn_quals = TYPE_UNQUALIFIED;
10765 rqual = REF_QUAL_NONE;
10768 if (type_uses_auto (type))
10770 error ("typedef declared %<auto%>");
10771 type = error_mark_node;
10774 if (reqs)
10775 error_at (location_of (reqs), "requires-clause on typedef");
10777 if (decl_context == FIELD)
10778 decl = build_lang_decl (TYPE_DECL, unqualified_id, type);
10779 else
10780 decl = build_decl (input_location, TYPE_DECL, unqualified_id, type);
10781 if (id_declarator && declarator->u.id.qualifying_scope) {
10782 error_at (DECL_SOURCE_LOCATION (decl),
10783 "typedef name may not be a nested-name-specifier");
10784 TREE_TYPE (decl) = error_mark_node;
10787 if (decl_context != FIELD)
10789 if (!current_function_decl)
10790 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
10791 else if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (current_function_decl)
10792 || (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P
10793 (current_function_decl)))
10794 /* The TYPE_DECL is "abstract" because there will be
10795 clones of this constructor/destructor, and there will
10796 be copies of this TYPE_DECL generated in those
10797 clones. The decloning optimization (for space) may
10798 revert this subsequently if it determines that
10799 the clones should share a common implementation. */
10800 DECL_ABSTRACT_P (decl) = true;
10802 else if (current_class_type
10803 && constructor_name_p (unqualified_id, current_class_type))
10804 permerror (input_location, "ISO C++ forbids nested type %qD with same name "
10805 "as enclosing class",
10806 unqualified_id);
10808 /* If the user declares "typedef struct {...} foo" then the
10809 struct will have an anonymous name. Fill that name in now.
10810 Nothing can refer to it, so nothing needs know about the name
10811 change. */
10812 if (type != error_mark_node
10813 && unqualified_id
10814 && TYPE_NAME (type)
10815 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10816 && TYPE_UNNAMED_P (type)
10817 && declspecs->type_definition_p
10818 && attributes_naming_typedef_ok (*attrlist)
10819 && cp_type_quals (type) == TYPE_UNQUALIFIED)
10821 tree t;
10823 /* Replace the anonymous name with the real name everywhere. */
10824 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
10826 if (anon_aggrname_p (TYPE_IDENTIFIER (t)))
10827 /* We do not rename the debug info representing the
10828 unnamed tagged type because the standard says in
10829 [dcl.typedef] that the naming applies only for
10830 linkage purposes. */
10831 /*debug_hooks->set_name (t, decl);*/
10832 TYPE_NAME (t) = decl;
10835 if (TYPE_LANG_SPECIFIC (type))
10836 TYPE_WAS_UNNAMED (type) = 1;
10838 /* If this is a typedef within a template class, the nested
10839 type is a (non-primary) template. The name for the
10840 template needs updating as well. */
10841 if (TYPE_LANG_SPECIFIC (type) && CLASSTYPE_TEMPLATE_INFO (type))
10842 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type))
10843 = TYPE_IDENTIFIER (type);
10845 /* Adjust linkage now that we aren't unnamed anymore. */
10846 reset_type_linkage (type);
10848 /* FIXME remangle member functions; member functions of a
10849 type with external linkage have external linkage. */
10852 if (signed_p
10853 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
10854 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
10856 bad_specifiers (decl, BSP_TYPE, virtualp,
10857 memfn_quals != TYPE_UNQUALIFIED,
10858 inlinep, friendp, raises != NULL_TREE);
10860 if (decl_spec_seq_has_spec_p (declspecs, ds_alias))
10861 /* Acknowledge that this was written:
10862 `using analias = atype;'. */
10863 TYPE_DECL_ALIAS_P (decl) = 1;
10865 return decl;
10868 /* Detect the case of an array type of unspecified size
10869 which came, as such, direct from a typedef name.
10870 We must copy the type, so that the array's domain can be
10871 individually set by the object's initializer. */
10873 if (type && typedef_type
10874 && TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
10875 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
10876 type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
10878 /* Detect where we're using a typedef of function type to declare a
10879 function. PARMS will not be set, so we must create it now. */
10881 if (type == typedef_type && TREE_CODE (type) == FUNCTION_TYPE)
10883 tree decls = NULL_TREE;
10884 tree args;
10886 for (args = TYPE_ARG_TYPES (type);
10887 args && args != void_list_node;
10888 args = TREE_CHAIN (args))
10890 tree decl = cp_build_parm_decl (NULL_TREE, TREE_VALUE (args));
10892 DECL_CHAIN (decl) = decls;
10893 decls = decl;
10896 parms = nreverse (decls);
10898 if (decl_context != TYPENAME)
10900 /* The qualifiers on the function type become the qualifiers on
10901 the non-static member function. */
10902 memfn_quals |= type_memfn_quals (type);
10903 rqual = type_memfn_rqual (type);
10904 type_quals = TYPE_UNQUALIFIED;
10908 /* If this is a type name (such as, in a cast or sizeof),
10909 compute the type and return it now. */
10911 if (decl_context == TYPENAME)
10913 /* Note that here we don't care about type_quals. */
10915 /* Special case: "friend class foo" looks like a TYPENAME context. */
10916 if (friendp)
10918 if (inlinep)
10920 error ("%<inline%> specified for friend class declaration");
10921 inlinep = 0;
10924 if (!current_aggr)
10926 /* Don't allow friend declaration without a class-key. */
10927 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
10928 permerror (input_location, "template parameters cannot be friends");
10929 else if (TREE_CODE (type) == TYPENAME_TYPE)
10930 permerror (input_location, "friend declaration requires class-key, "
10931 "i.e. %<friend class %T::%D%>",
10932 TYPE_CONTEXT (type), TYPENAME_TYPE_FULLNAME (type));
10933 else
10934 permerror (input_location, "friend declaration requires class-key, "
10935 "i.e. %<friend %#T%>",
10936 type);
10939 /* Only try to do this stuff if we didn't already give up. */
10940 if (type != integer_type_node)
10942 /* A friendly class? */
10943 if (current_class_type)
10944 make_friend_class (current_class_type, TYPE_MAIN_VARIANT (type),
10945 /*complain=*/true);
10946 else
10947 error ("trying to make class %qT a friend of global scope",
10948 type);
10950 type = void_type_node;
10953 else if (memfn_quals || rqual)
10955 if (ctype == NULL_TREE
10956 && TREE_CODE (type) == METHOD_TYPE)
10957 ctype = TYPE_METHOD_BASETYPE (type);
10959 if (ctype)
10960 type = build_memfn_type (type, ctype, memfn_quals, rqual);
10961 /* Core issue #547: need to allow this in template type args.
10962 Allow it in general in C++11 for alias-declarations. */
10963 else if ((template_type_arg || cxx_dialect >= cxx11)
10964 && TREE_CODE (type) == FUNCTION_TYPE)
10965 type = apply_memfn_quals (type, memfn_quals, rqual);
10966 else
10967 error ("invalid qualifiers on non-member function type");
10970 if (reqs)
10971 error_at (location_of (reqs), "requires-clause on type-id");
10973 return type;
10975 else if (unqualified_id == NULL_TREE && decl_context != PARM
10976 && decl_context != CATCHPARM
10977 && TREE_CODE (type) != UNION_TYPE
10978 && ! bitfield)
10980 error ("abstract declarator %qT used as declaration", type);
10981 return error_mark_node;
10984 /* Only functions may be declared using an operator-function-id. */
10985 if (unqualified_id
10986 && IDENTIFIER_OPNAME_P (unqualified_id)
10987 && TREE_CODE (type) != FUNCTION_TYPE
10988 && TREE_CODE (type) != METHOD_TYPE)
10990 error ("declaration of %qD as non-function", unqualified_id);
10991 return error_mark_node;
10994 if (reqs
10995 && TREE_CODE (type) != FUNCTION_TYPE
10996 && TREE_CODE (type) != METHOD_TYPE)
10997 error_at (location_of (reqs),
10998 "requires-clause on declaration of non-function type %qT",
10999 type);
11001 /* We don't check parameter types here because we can emit a better
11002 error message later. */
11003 if (decl_context != PARM)
11005 type = check_var_type (unqualified_id, type);
11006 if (type == error_mark_node)
11007 return error_mark_node;
11010 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
11011 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
11013 if (decl_context == PARM || decl_context == CATCHPARM)
11015 if (ctype || in_namespace)
11016 error ("cannot use %<::%> in parameter declaration");
11018 if (type_uses_auto (type))
11020 if (cxx_dialect >= cxx14)
11021 error ("%<auto%> parameter not permitted in this context");
11022 else
11023 error ("parameter declared %<auto%>");
11024 type = error_mark_node;
11027 /* A parameter declared as an array of T is really a pointer to T.
11028 One declared as a function is really a pointer to a function.
11029 One declared as a member is really a pointer to member. */
11031 if (TREE_CODE (type) == ARRAY_TYPE)
11033 /* Transfer const-ness of array into that of type pointed to. */
11034 type = build_pointer_type (TREE_TYPE (type));
11035 type_quals = TYPE_UNQUALIFIED;
11036 array_parameter_p = true;
11038 else if (TREE_CODE (type) == FUNCTION_TYPE)
11039 type = build_pointer_type (type);
11042 if (ctype && TREE_CODE (type) == FUNCTION_TYPE && staticp < 2
11043 && !NEW_DELETE_OPNAME_P (unqualified_id))
11045 cp_cv_quals real_quals = memfn_quals;
11046 if (cxx_dialect < cxx14 && constexpr_p
11047 && sfk != sfk_constructor && sfk != sfk_destructor)
11048 real_quals |= TYPE_QUAL_CONST;
11049 type = build_memfn_type (type, ctype, real_quals, rqual);
11053 tree decl = NULL_TREE;
11055 if (decl_context == PARM)
11057 decl = cp_build_parm_decl (unqualified_id, type);
11058 DECL_ARRAY_PARAMETER_P (decl) = array_parameter_p;
11060 bad_specifiers (decl, BSP_PARM, virtualp,
11061 memfn_quals != TYPE_UNQUALIFIED,
11062 inlinep, friendp, raises != NULL_TREE);
11064 else if (decl_context == FIELD)
11066 if (!staticp && !friendp && TREE_CODE (type) != METHOD_TYPE
11067 && type_uses_auto (type))
11069 error ("non-static data member declared %<auto%>");
11070 type = error_mark_node;
11073 /* The C99 flexible array extension. */
11074 if (!staticp && TREE_CODE (type) == ARRAY_TYPE
11075 && TYPE_DOMAIN (type) == NULL_TREE)
11077 if (ctype
11078 && (TREE_CODE (ctype) == UNION_TYPE
11079 || TREE_CODE (ctype) == QUAL_UNION_TYPE))
11081 error ("flexible array member in union");
11082 type = error_mark_node;
11084 else
11086 /* Flexible array member has a null domain. */
11087 type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
11091 if (type == error_mark_node)
11093 /* Happens when declaring arrays of sizes which
11094 are error_mark_node, for example. */
11095 decl = NULL_TREE;
11097 else if (in_namespace && !friendp)
11099 /* Something like struct S { int N::j; }; */
11100 error ("invalid use of %<::%>");
11101 return error_mark_node;
11103 else if (TREE_CODE (type) == FUNCTION_TYPE
11104 || TREE_CODE (type) == METHOD_TYPE)
11106 int publicp = 0;
11107 tree function_context;
11109 if (friendp == 0)
11111 /* This should never happen in pure C++ (the check
11112 could be an assert). It could happen in
11113 Objective-C++ if someone writes invalid code that
11114 uses a function declaration for an instance
11115 variable or property (instance variables and
11116 properties are parsed as FIELD_DECLs, but they are
11117 part of an Objective-C class, not a C++ class).
11118 That code is invalid and is caught by this
11119 check. */
11120 if (!ctype)
11122 error ("declaration of function %qD in invalid context",
11123 unqualified_id);
11124 return error_mark_node;
11127 /* ``A union may [ ... ] not [ have ] virtual functions.''
11128 ARM 9.5 */
11129 if (virtualp && TREE_CODE (ctype) == UNION_TYPE)
11131 error ("function %qD declared %<virtual%> inside a union",
11132 unqualified_id);
11133 return error_mark_node;
11136 if (NEW_DELETE_OPNAME_P (unqualified_id))
11138 if (virtualp)
11140 error ("%qD cannot be declared %<virtual%>, since it "
11141 "is always static",
11142 unqualified_id);
11143 virtualp = 0;
11148 /* Check that the name used for a destructor makes sense. */
11149 if (sfk == sfk_destructor)
11151 tree uqname = id_declarator->u.id.unqualified_name;
11153 if (!ctype)
11155 gcc_assert (friendp);
11156 error ("expected qualified name in friend declaration "
11157 "for destructor %qD", uqname);
11158 return error_mark_node;
11161 if (!check_dtor_name (ctype, TREE_OPERAND (uqname, 0)))
11163 error ("declaration of %qD as member of %qT",
11164 uqname, ctype);
11165 return error_mark_node;
11167 if (concept_p)
11169 error ("a destructor cannot be %<concept%>");
11170 return error_mark_node;
11172 if (constexpr_p)
11174 error ("a destructor cannot be %<constexpr%>");
11175 return error_mark_node;
11178 else if (sfk == sfk_constructor && friendp && !ctype)
11180 error ("expected qualified name in friend declaration "
11181 "for constructor %qD",
11182 id_declarator->u.id.unqualified_name);
11183 return error_mark_node;
11185 if (sfk == sfk_constructor)
11186 if (concept_p)
11188 error ("a constructor cannot be %<concept%>");
11189 return error_mark_node;
11191 if (concept_p)
11193 error ("a concept cannot be a member function");
11194 concept_p = false;
11197 if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
11199 tree tmpl = TREE_OPERAND (unqualified_id, 0);
11200 if (variable_template_p (tmpl))
11202 error ("specialization of variable template %qD "
11203 "declared as function", tmpl);
11204 inform (DECL_SOURCE_LOCATION (tmpl),
11205 "variable template declared here");
11206 return error_mark_node;
11210 /* Tell grokfndecl if it needs to set TREE_PUBLIC on the node. */
11211 function_context = (ctype != NULL_TREE) ?
11212 decl_function_context (TYPE_MAIN_DECL (ctype)) : NULL_TREE;
11213 publicp = (! friendp || ! staticp)
11214 && function_context == NULL_TREE;
11216 if (late_return_type_p)
11217 TYPE_HAS_LATE_RETURN_TYPE (type) = 1;
11219 decl = grokfndecl (ctype, type,
11220 TREE_CODE (unqualified_id) != TEMPLATE_ID_EXPR
11221 ? unqualified_id : dname,
11222 parms,
11223 unqualified_id,
11224 reqs,
11225 virtualp, flags, memfn_quals, rqual, raises,
11226 friendp ? -1 : 0, friendp, publicp,
11227 inlinep | (2 * constexpr_p) | (4 * concept_p),
11228 initialized == SD_DELETED, sfk,
11229 funcdef_flag, template_count, in_namespace,
11230 attrlist, declarator->id_loc);
11231 decl = set_virt_specifiers (decl, virt_specifiers);
11232 if (decl == NULL_TREE)
11233 return error_mark_node;
11234 #if 0
11235 /* This clobbers the attrs stored in `decl' from `attrlist'. */
11236 /* The decl and setting of decl_attr is also turned off. */
11237 decl = build_decl_attribute_variant (decl, decl_attr);
11238 #endif
11240 /* [class.conv.ctor]
11242 A constructor declared without the function-specifier
11243 explicit that can be called with a single parameter
11244 specifies a conversion from the type of its first
11245 parameter to the type of its class. Such a constructor
11246 is called a converting constructor. */
11247 if (explicitp == 2)
11248 DECL_NONCONVERTING_P (decl) = 1;
11250 else if (!staticp && !dependent_type_p (type)
11251 && !COMPLETE_TYPE_P (complete_type (type))
11252 && (!complete_or_array_type_p (type)
11253 || initialized == 0))
11255 if (TREE_CODE (type) != ARRAY_TYPE
11256 || !COMPLETE_TYPE_P (TREE_TYPE (type)))
11258 if (unqualified_id)
11260 error ("field %qD has incomplete type %qT",
11261 unqualified_id, type);
11262 cxx_incomplete_type_inform (strip_array_types (type));
11264 else
11265 error ("name %qT has incomplete type", type);
11267 type = error_mark_node;
11268 decl = NULL_TREE;
11271 else
11273 if (friendp)
11275 error ("%qE is neither function nor member function; "
11276 "cannot be declared friend", unqualified_id);
11277 friendp = 0;
11279 decl = NULL_TREE;
11282 if (friendp)
11284 /* Friends are treated specially. */
11285 if (ctype == current_class_type)
11286 ; /* We already issued a permerror. */
11287 else if (decl && DECL_NAME (decl))
11289 if (template_class_depth (current_class_type) == 0)
11291 decl = check_explicit_specialization
11292 (unqualified_id, decl, template_count,
11293 2 * funcdef_flag + 4);
11294 if (decl == error_mark_node)
11295 return error_mark_node;
11298 decl = do_friend (ctype, unqualified_id, decl,
11299 *attrlist, flags,
11300 funcdef_flag);
11301 return decl;
11303 else
11304 return error_mark_node;
11307 /* Structure field. It may not be a function, except for C++. */
11309 if (decl == NULL_TREE)
11311 if (staticp)
11313 /* C++ allows static class members. All other work
11314 for this is done by grokfield. */
11315 decl = build_lang_decl_loc (declarator
11316 ? declarator->id_loc
11317 : input_location,
11318 VAR_DECL, unqualified_id, type);
11319 set_linkage_for_static_data_member (decl);
11320 /* Even if there is an in-class initialization, DECL
11321 is considered undefined until an out-of-class
11322 definition is provided. */
11323 DECL_EXTERNAL (decl) = 1;
11325 if (thread_p)
11327 CP_DECL_THREAD_LOCAL_P (decl) = true;
11328 if (!processing_template_decl)
11329 set_decl_tls_model (decl, decl_default_tls_model (decl));
11330 if (declspecs->gnu_thread_keyword_p)
11331 SET_DECL_GNU_TLS_P (decl);
11333 if (concept_p)
11334 error ("static data member %qE declared %<concept%>",
11335 unqualified_id);
11336 else if (constexpr_p && !initialized)
11338 error ("constexpr static data member %qD must have an "
11339 "initializer", decl);
11340 constexpr_p = false;
11343 else
11345 if (concept_p)
11346 error ("non-static data member %qE declared %<concept%>",
11347 unqualified_id);
11348 else if (constexpr_p)
11350 error ("non-static data member %qE declared %<constexpr%>",
11351 unqualified_id);
11352 constexpr_p = false;
11354 decl = build_decl (input_location,
11355 FIELD_DECL, unqualified_id, type);
11356 DECL_NONADDRESSABLE_P (decl) = bitfield;
11357 if (bitfield && !unqualified_id)
11358 TREE_NO_WARNING (decl) = 1;
11360 if (storage_class == sc_mutable)
11362 DECL_MUTABLE_P (decl) = 1;
11363 storage_class = sc_none;
11366 if (initialized)
11368 /* An attempt is being made to initialize a non-static
11369 member. This is new in C++11. */
11370 maybe_warn_cpp0x (CPP0X_NSDMI);
11372 /* If this has been parsed with static storage class, but
11373 errors forced staticp to be cleared, ensure NSDMI is
11374 not present. */
11375 if (declspecs->storage_class == sc_static)
11376 DECL_INITIAL (decl) = error_mark_node;
11380 bad_specifiers (decl, BSP_FIELD, virtualp,
11381 memfn_quals != TYPE_UNQUALIFIED,
11382 inlinep, friendp, raises != NULL_TREE);
11385 else if (TREE_CODE (type) == FUNCTION_TYPE
11386 || TREE_CODE (type) == METHOD_TYPE)
11388 tree original_name;
11389 int publicp = 0;
11391 if (!unqualified_id)
11392 return error_mark_node;
11394 if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
11395 original_name = dname;
11396 else
11397 original_name = unqualified_id;
11399 if (storage_class == sc_auto)
11400 error ("storage class %<auto%> invalid for function %qs", name);
11401 else if (storage_class == sc_register)
11402 error ("storage class %<register%> invalid for function %qs", name);
11403 else if (thread_p)
11405 if (declspecs->gnu_thread_keyword_p)
11406 error ("storage class %<__thread%> invalid for function %qs",
11407 name);
11408 else
11409 error ("storage class %<thread_local%> invalid for function %qs",
11410 name);
11413 if (virt_specifiers)
11414 error ("virt-specifiers in %qs not allowed outside a class definition", name);
11415 /* Function declaration not at top level.
11416 Storage classes other than `extern' are not allowed
11417 and `extern' makes no difference. */
11418 if (! toplevel_bindings_p ()
11419 && (storage_class == sc_static
11420 || decl_spec_seq_has_spec_p (declspecs, ds_inline))
11421 && pedantic)
11423 if (storage_class == sc_static)
11424 pedwarn (input_location, OPT_Wpedantic,
11425 "%<static%> specifier invalid for function %qs "
11426 "declared out of global scope", name);
11427 else
11428 pedwarn (input_location, OPT_Wpedantic,
11429 "%<inline%> specifier invalid for function %qs "
11430 "declared out of global scope", name);
11433 if (ctype == NULL_TREE)
11435 if (virtualp)
11437 error ("virtual non-class function %qs", name);
11438 virtualp = 0;
11440 else if (sfk == sfk_constructor
11441 || sfk == sfk_destructor)
11443 error (funcdef_flag
11444 ? G_("%qs defined in a non-class scope")
11445 : G_("%qs declared in a non-class scope"), name);
11446 sfk = sfk_none;
11450 /* Record whether the function is public. */
11451 publicp = (ctype != NULL_TREE
11452 || storage_class != sc_static);
11454 if (late_return_type_p)
11455 TYPE_HAS_LATE_RETURN_TYPE (type) = 1;
11457 decl = grokfndecl (ctype, type, original_name, parms, unqualified_id,
11458 reqs, virtualp, flags, memfn_quals, rqual, raises,
11459 1, friendp,
11460 publicp,
11461 inlinep | (2 * constexpr_p) | (4 * concept_p),
11462 initialized == SD_DELETED,
11463 sfk,
11464 funcdef_flag,
11465 template_count, in_namespace, attrlist,
11466 declarator->id_loc);
11467 if (decl == NULL_TREE)
11468 return error_mark_node;
11470 if (staticp == 1)
11472 int invalid_static = 0;
11474 /* Don't allow a static member function in a class, and forbid
11475 declaring main to be static. */
11476 if (TREE_CODE (type) == METHOD_TYPE)
11478 permerror (input_location, "cannot declare member function %qD to have "
11479 "static linkage", decl);
11480 invalid_static = 1;
11482 else if (current_function_decl)
11484 /* 7.1.1: There can be no static function declarations within a
11485 block. */
11486 error ("cannot declare static function inside another function");
11487 invalid_static = 1;
11490 if (invalid_static)
11492 staticp = 0;
11493 storage_class = sc_none;
11497 else
11499 /* It's a variable. */
11501 /* An uninitialized decl with `extern' is a reference. */
11502 decl = grokvardecl (type, dname, unqualified_id,
11503 declspecs,
11504 initialized,
11505 ((type_quals & TYPE_QUAL_CONST) != 0) | (2 * concept_p),
11506 template_count,
11507 ctype ? ctype : in_namespace);
11508 if (decl == NULL_TREE)
11509 return error_mark_node;
11511 bad_specifiers (decl, BSP_VAR, virtualp,
11512 memfn_quals != TYPE_UNQUALIFIED,
11513 inlinep, friendp, raises != NULL_TREE);
11515 if (ctype)
11517 DECL_CONTEXT (decl) = ctype;
11518 if (staticp == 1)
11520 permerror (input_location, "%<static%> may not be used when defining "
11521 "(as opposed to declaring) a static data member");
11522 staticp = 0;
11523 storage_class = sc_none;
11525 if (storage_class == sc_register && TREE_STATIC (decl))
11527 error ("static member %qD declared %<register%>", decl);
11528 storage_class = sc_none;
11530 if (storage_class == sc_extern && pedantic)
11532 pedwarn (input_location, OPT_Wpedantic,
11533 "cannot explicitly declare member %q#D to have "
11534 "extern linkage", decl);
11535 storage_class = sc_none;
11538 else if (constexpr_p && DECL_EXTERNAL (decl))
11540 error ("declaration of constexpr variable %qD is not a definition",
11541 decl);
11542 constexpr_p = false;
11546 if (storage_class == sc_extern && initialized && !funcdef_flag)
11548 if (toplevel_bindings_p ())
11550 /* It's common practice (and completely valid) to have a const
11551 be initialized and declared extern. */
11552 if (!(type_quals & TYPE_QUAL_CONST))
11553 warning (0, "%qs initialized and declared %<extern%>", name);
11555 else
11557 error ("%qs has both %<extern%> and initializer", name);
11558 return error_mark_node;
11562 /* Record `register' declaration for warnings on &
11563 and in case doing stupid register allocation. */
11565 if (storage_class == sc_register)
11567 DECL_REGISTER (decl) = 1;
11568 /* Warn about register storage specifiers on PARM_DECLs. */
11569 if (TREE_CODE (decl) == PARM_DECL)
11571 if (cxx_dialect >= cxx1z)
11572 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
11573 "ISO C++1z does not allow %<register%> storage "
11574 "class specifier");
11575 else
11576 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
11577 "%<register%> storage class specifier used");
11580 else if (storage_class == sc_extern)
11581 DECL_THIS_EXTERN (decl) = 1;
11582 else if (storage_class == sc_static)
11583 DECL_THIS_STATIC (decl) = 1;
11585 /* Set constexpr flag on vars (functions got it in grokfndecl). */
11586 if (constexpr_p && VAR_P (decl))
11587 DECL_DECLARED_CONSTEXPR_P (decl) = true;
11589 /* Record constancy and volatility on the DECL itself . There's
11590 no need to do this when processing a template; we'll do this
11591 for the instantiated declaration based on the type of DECL. */
11592 if (!processing_template_decl)
11593 cp_apply_type_quals_to_decl (type_quals, decl);
11595 return decl;
11599 /* Subroutine of start_function. Ensure that each of the parameter
11600 types (as listed in PARMS) is complete, as is required for a
11601 function definition. */
11603 static void
11604 require_complete_types_for_parms (tree parms)
11606 for (; parms; parms = DECL_CHAIN (parms))
11608 if (dependent_type_p (TREE_TYPE (parms)))
11609 continue;
11610 if (!VOID_TYPE_P (TREE_TYPE (parms))
11611 && complete_type_or_else (TREE_TYPE (parms), parms))
11613 relayout_decl (parms);
11614 DECL_ARG_TYPE (parms) = type_passed_as (TREE_TYPE (parms));
11616 else
11617 /* grokparms or complete_type_or_else will have already issued
11618 an error. */
11619 TREE_TYPE (parms) = error_mark_node;
11623 /* Returns nonzero if T is a local variable. */
11626 local_variable_p (const_tree t)
11628 if ((VAR_P (t)
11629 /* A VAR_DECL with a context that is a _TYPE is a static data
11630 member. */
11631 && !TYPE_P (CP_DECL_CONTEXT (t))
11632 /* Any other non-local variable must be at namespace scope. */
11633 && !DECL_NAMESPACE_SCOPE_P (t))
11634 || (TREE_CODE (t) == PARM_DECL))
11635 return 1;
11637 return 0;
11640 /* Like local_variable_p, but suitable for use as a tree-walking
11641 function. */
11643 static tree
11644 local_variable_p_walkfn (tree *tp, int *walk_subtrees,
11645 void * /*data*/)
11647 if (local_variable_p (*tp)
11648 && (!DECL_ARTIFICIAL (*tp) || DECL_NAME (*tp) == this_identifier))
11649 return *tp;
11650 else if (TYPE_P (*tp))
11651 *walk_subtrees = 0;
11653 return NULL_TREE;
11656 /* Check that ARG, which is a default-argument expression for a
11657 parameter DECL, is valid. Returns ARG, or ERROR_MARK_NODE, if
11658 something goes wrong. DECL may also be a _TYPE node, rather than a
11659 DECL, if there is no DECL available. */
11661 tree
11662 check_default_argument (tree decl, tree arg, tsubst_flags_t complain)
11664 tree var;
11665 tree decl_type;
11667 if (TREE_CODE (arg) == DEFAULT_ARG)
11668 /* We get a DEFAULT_ARG when looking at an in-class declaration
11669 with a default argument. Ignore the argument for now; we'll
11670 deal with it after the class is complete. */
11671 return arg;
11673 if (TYPE_P (decl))
11675 decl_type = decl;
11676 decl = NULL_TREE;
11678 else
11679 decl_type = TREE_TYPE (decl);
11681 if (arg == error_mark_node
11682 || decl == error_mark_node
11683 || TREE_TYPE (arg) == error_mark_node
11684 || decl_type == error_mark_node)
11685 /* Something already went wrong. There's no need to check
11686 further. */
11687 return error_mark_node;
11689 /* [dcl.fct.default]
11691 A default argument expression is implicitly converted to the
11692 parameter type. */
11693 ++cp_unevaluated_operand;
11694 perform_implicit_conversion_flags (decl_type, arg, complain,
11695 LOOKUP_IMPLICIT);
11696 --cp_unevaluated_operand;
11698 /* Avoid redundant -Wzero-as-null-pointer-constant warnings at
11699 the call sites. */
11700 if (TYPE_PTR_OR_PTRMEM_P (decl_type)
11701 && null_ptr_cst_p (arg))
11702 return nullptr_node;
11704 /* [dcl.fct.default]
11706 Local variables shall not be used in default argument
11707 expressions.
11709 The keyword `this' shall not be used in a default argument of a
11710 member function. */
11711 var = cp_walk_tree_without_duplicates (&arg, local_variable_p_walkfn, NULL);
11712 if (var)
11714 if (complain & tf_warning_or_error)
11716 if (DECL_NAME (var) == this_identifier)
11717 permerror (input_location, "default argument %qE uses %qD",
11718 arg, var);
11719 else
11720 error ("default argument %qE uses local variable %qD", arg, var);
11722 return error_mark_node;
11725 /* All is well. */
11726 return arg;
11729 /* Returns a deprecated type used within TYPE, or NULL_TREE if none. */
11731 static tree
11732 type_is_deprecated (tree type)
11734 enum tree_code code;
11735 if (TREE_DEPRECATED (type))
11736 return type;
11737 if (TYPE_NAME (type))
11739 if (TREE_DEPRECATED (TYPE_NAME (type)))
11740 return type;
11741 else
11742 return NULL_TREE;
11745 /* Do warn about using typedefs to a deprecated class. */
11746 if (OVERLOAD_TYPE_P (type) && type != TYPE_MAIN_VARIANT (type))
11747 return type_is_deprecated (TYPE_MAIN_VARIANT (type));
11749 code = TREE_CODE (type);
11751 if (code == POINTER_TYPE || code == REFERENCE_TYPE
11752 || code == OFFSET_TYPE || code == FUNCTION_TYPE
11753 || code == METHOD_TYPE || code == ARRAY_TYPE)
11754 return type_is_deprecated (TREE_TYPE (type));
11756 if (TYPE_PTRMEMFUNC_P (type))
11757 return type_is_deprecated
11758 (TREE_TYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type))));
11760 return NULL_TREE;
11763 /* Decode the list of parameter types for a function type.
11764 Given the list of things declared inside the parens,
11765 return a list of types.
11767 If this parameter does not end with an ellipsis, we append
11768 void_list_node.
11770 *PARMS is set to the chain of PARM_DECLs created. */
11772 tree
11773 grokparms (tree parmlist, tree *parms)
11775 tree result = NULL_TREE;
11776 tree decls = NULL_TREE;
11777 tree parm;
11778 int any_error = 0;
11780 for (parm = parmlist; parm != NULL_TREE; parm = TREE_CHAIN (parm))
11782 tree type = NULL_TREE;
11783 tree init = TREE_PURPOSE (parm);
11784 tree decl = TREE_VALUE (parm);
11786 if (parm == void_list_node)
11787 break;
11789 if (! decl || TREE_TYPE (decl) == error_mark_node)
11790 continue;
11792 type = TREE_TYPE (decl);
11793 if (VOID_TYPE_P (type))
11795 if (same_type_p (type, void_type_node)
11796 && !init
11797 && !DECL_NAME (decl) && !result
11798 && TREE_CHAIN (parm) == void_list_node)
11799 /* DR 577: A parameter list consisting of a single
11800 unnamed parameter of non-dependent type 'void'. */
11801 break;
11802 else if (cv_qualified_p (type))
11803 error_at (DECL_SOURCE_LOCATION (decl),
11804 "invalid use of cv-qualified type %qT in "
11805 "parameter declaration", type);
11806 else
11807 error_at (DECL_SOURCE_LOCATION (decl),
11808 "invalid use of type %<void%> in parameter "
11809 "declaration");
11810 /* It's not a good idea to actually create parameters of
11811 type `void'; other parts of the compiler assume that a
11812 void type terminates the parameter list. */
11813 type = error_mark_node;
11814 TREE_TYPE (decl) = error_mark_node;
11817 if (type != error_mark_node)
11819 if (deprecated_state != DEPRECATED_SUPPRESS)
11821 tree deptype = type_is_deprecated (type);
11822 if (deptype)
11823 warn_deprecated_use (deptype, NULL_TREE);
11826 /* Top-level qualifiers on the parameters are
11827 ignored for function types. */
11828 type = cp_build_qualified_type (type, 0);
11829 if (TREE_CODE (type) == METHOD_TYPE)
11831 error ("parameter %qD invalidly declared method type", decl);
11832 type = build_pointer_type (type);
11833 TREE_TYPE (decl) = type;
11835 else if (abstract_virtuals_error (decl, type))
11836 any_error = 1; /* Seems like a good idea. */
11837 else if (POINTER_TYPE_P (type))
11839 /* [dcl.fct]/6, parameter types cannot contain pointers
11840 (references) to arrays of unknown bound. */
11841 tree t = TREE_TYPE (type);
11842 int ptr = TYPE_PTR_P (type);
11844 while (1)
11846 if (TYPE_PTR_P (t))
11847 ptr = 1;
11848 else if (TREE_CODE (t) != ARRAY_TYPE)
11849 break;
11850 else if (!TYPE_DOMAIN (t))
11851 break;
11852 t = TREE_TYPE (t);
11854 if (TREE_CODE (t) == ARRAY_TYPE)
11855 error (ptr
11856 ? G_("parameter %qD includes pointer to array of "
11857 "unknown bound %qT")
11858 : G_("parameter %qD includes reference to array of "
11859 "unknown bound %qT"),
11860 decl, t);
11863 if (any_error)
11864 init = NULL_TREE;
11865 else if (init && !processing_template_decl)
11866 init = check_default_argument (decl, init, tf_warning_or_error);
11869 DECL_CHAIN (decl) = decls;
11870 decls = decl;
11871 result = tree_cons (init, type, result);
11873 decls = nreverse (decls);
11874 result = nreverse (result);
11875 if (parm)
11876 result = chainon (result, void_list_node);
11877 *parms = decls;
11879 return result;
11883 /* D is a constructor or overloaded `operator='.
11885 Let T be the class in which D is declared. Then, this function
11886 returns:
11888 -1 if D's is an ill-formed constructor or copy assignment operator
11889 whose first parameter is of type `T'.
11890 0 if D is not a copy constructor or copy assignment
11891 operator.
11892 1 if D is a copy constructor or copy assignment operator whose
11893 first parameter is a reference to non-const qualified T.
11894 2 if D is a copy constructor or copy assignment operator whose
11895 first parameter is a reference to const qualified T.
11897 This function can be used as a predicate. Positive values indicate
11898 a copy constructor and nonzero values indicate a copy assignment
11899 operator. */
11902 copy_fn_p (const_tree d)
11904 tree args;
11905 tree arg_type;
11906 int result = 1;
11908 gcc_assert (DECL_FUNCTION_MEMBER_P (d));
11910 if (TREE_CODE (d) == TEMPLATE_DECL
11911 || (DECL_TEMPLATE_INFO (d)
11912 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d))))
11913 /* Instantiations of template member functions are never copy
11914 functions. Note that member functions of templated classes are
11915 represented as template functions internally, and we must
11916 accept those as copy functions. */
11917 return 0;
11919 args = FUNCTION_FIRST_USER_PARMTYPE (d);
11920 if (!args)
11921 return 0;
11923 arg_type = TREE_VALUE (args);
11924 if (arg_type == error_mark_node)
11925 return 0;
11927 if (TYPE_MAIN_VARIANT (arg_type) == DECL_CONTEXT (d))
11929 /* Pass by value copy assignment operator. */
11930 result = -1;
11932 else if (TREE_CODE (arg_type) == REFERENCE_TYPE
11933 && !TYPE_REF_IS_RVALUE (arg_type)
11934 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)) == DECL_CONTEXT (d))
11936 if (CP_TYPE_CONST_P (TREE_TYPE (arg_type)))
11937 result = 2;
11939 else
11940 return 0;
11942 args = TREE_CHAIN (args);
11944 if (args && args != void_list_node && !TREE_PURPOSE (args))
11945 /* There are more non-optional args. */
11946 return 0;
11948 return result;
11951 /* D is a constructor or overloaded `operator='.
11953 Let T be the class in which D is declared. Then, this function
11954 returns true when D is a move constructor or move assignment
11955 operator, false otherwise. */
11957 bool
11958 move_fn_p (const_tree d)
11960 gcc_assert (DECL_FUNCTION_MEMBER_P (d));
11962 if (cxx_dialect == cxx98)
11963 /* There are no move constructors if we are in C++98 mode. */
11964 return false;
11966 if (TREE_CODE (d) == TEMPLATE_DECL
11967 || (DECL_TEMPLATE_INFO (d)
11968 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d))))
11969 /* Instantiations of template member functions are never move
11970 functions. Note that member functions of templated classes are
11971 represented as template functions internally, and we must
11972 accept those as move functions. */
11973 return 0;
11975 return move_signature_fn_p (d);
11978 /* D is a constructor or overloaded `operator='.
11980 Then, this function returns true when D has the same signature as a move
11981 constructor or move assignment operator (because either it is such a
11982 ctor/op= or it is a template specialization with the same signature),
11983 false otherwise. */
11985 bool
11986 move_signature_fn_p (const_tree d)
11988 tree args;
11989 tree arg_type;
11990 bool result = false;
11992 args = FUNCTION_FIRST_USER_PARMTYPE (d);
11993 if (!args)
11994 return 0;
11996 arg_type = TREE_VALUE (args);
11997 if (arg_type == error_mark_node)
11998 return 0;
12000 if (TREE_CODE (arg_type) == REFERENCE_TYPE
12001 && TYPE_REF_IS_RVALUE (arg_type)
12002 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)),
12003 DECL_CONTEXT (d)))
12004 result = true;
12006 args = TREE_CHAIN (args);
12008 if (args && args != void_list_node && !TREE_PURPOSE (args))
12009 /* There are more non-optional args. */
12010 return false;
12012 return result;
12015 /* Remember any special properties of member function DECL. */
12017 void
12018 grok_special_member_properties (tree decl)
12020 tree class_type;
12022 if (!DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
12023 return;
12025 class_type = DECL_CONTEXT (decl);
12026 if (DECL_CONSTRUCTOR_P (decl))
12028 int ctor = copy_fn_p (decl);
12030 if (!DECL_ARTIFICIAL (decl))
12031 TYPE_HAS_USER_CONSTRUCTOR (class_type) = 1;
12033 if (ctor > 0)
12035 /* [class.copy]
12037 A non-template constructor for class X is a copy
12038 constructor if its first parameter is of type X&, const
12039 X&, volatile X& or const volatile X&, and either there
12040 are no other parameters or else all other parameters have
12041 default arguments. */
12042 TYPE_HAS_COPY_CTOR (class_type) = 1;
12043 if (user_provided_p (decl))
12044 TYPE_HAS_COMPLEX_COPY_CTOR (class_type) = 1;
12045 if (ctor > 1)
12046 TYPE_HAS_CONST_COPY_CTOR (class_type) = 1;
12048 else if (sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (decl)))
12049 TYPE_HAS_DEFAULT_CONSTRUCTOR (class_type) = 1;
12050 else if (move_fn_p (decl) && user_provided_p (decl))
12051 TYPE_HAS_COMPLEX_MOVE_CTOR (class_type) = 1;
12052 else if (is_list_ctor (decl))
12053 TYPE_HAS_LIST_CTOR (class_type) = 1;
12055 if (DECL_DECLARED_CONSTEXPR_P (decl)
12056 && !copy_fn_p (decl) && !move_fn_p (decl))
12057 TYPE_HAS_CONSTEXPR_CTOR (class_type) = 1;
12059 else if (DECL_OVERLOADED_OPERATOR_P (decl) == NOP_EXPR)
12061 /* [class.copy]
12063 A non-template assignment operator for class X is a copy
12064 assignment operator if its parameter is of type X, X&, const
12065 X&, volatile X& or const volatile X&. */
12067 int assop = copy_fn_p (decl);
12069 if (assop)
12071 TYPE_HAS_COPY_ASSIGN (class_type) = 1;
12072 if (user_provided_p (decl))
12073 TYPE_HAS_COMPLEX_COPY_ASSIGN (class_type) = 1;
12074 if (assop != 1)
12075 TYPE_HAS_CONST_COPY_ASSIGN (class_type) = 1;
12077 else if (move_fn_p (decl) && user_provided_p (decl))
12078 TYPE_HAS_COMPLEX_MOVE_ASSIGN (class_type) = 1;
12080 /* Destructors are handled in check_methods. */
12083 /* Check a constructor DECL has the correct form. Complains
12084 if the class has a constructor of the form X(X). */
12087 grok_ctor_properties (const_tree ctype, const_tree decl)
12089 int ctor_parm = copy_fn_p (decl);
12091 if (ctor_parm < 0)
12093 /* [class.copy]
12095 A declaration of a constructor for a class X is ill-formed if
12096 its first parameter is of type (optionally cv-qualified) X
12097 and either there are no other parameters or else all other
12098 parameters have default arguments.
12100 We *don't* complain about member template instantiations that
12101 have this form, though; they can occur as we try to decide
12102 what constructor to use during overload resolution. Since
12103 overload resolution will never prefer such a constructor to
12104 the non-template copy constructor (which is either explicitly
12105 or implicitly defined), there's no need to worry about their
12106 existence. Theoretically, they should never even be
12107 instantiated, but that's hard to forestall. */
12108 error ("invalid constructor; you probably meant %<%T (const %T&)%>",
12109 ctype, ctype);
12110 return 0;
12113 return 1;
12116 /* An operator with this code is unary, but can also be binary. */
12118 static int
12119 ambi_op_p (enum tree_code code)
12121 return (code == INDIRECT_REF
12122 || code == ADDR_EXPR
12123 || code == UNARY_PLUS_EXPR
12124 || code == NEGATE_EXPR
12125 || code == PREINCREMENT_EXPR
12126 || code == PREDECREMENT_EXPR);
12129 /* An operator with this name can only be unary. */
12131 static int
12132 unary_op_p (enum tree_code code)
12134 return (code == TRUTH_NOT_EXPR
12135 || code == BIT_NOT_EXPR
12136 || code == COMPONENT_REF
12137 || code == TYPE_EXPR);
12140 /* DECL is a declaration for an overloaded operator. If COMPLAIN is true,
12141 errors are issued for invalid declarations. */
12143 bool
12144 grok_op_properties (tree decl, bool complain)
12146 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
12147 tree argtype;
12148 int methodp = (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE);
12149 tree name = DECL_NAME (decl);
12150 enum tree_code operator_code;
12151 int arity;
12152 bool ellipsis_p;
12153 tree class_type;
12155 /* Count the number of arguments and check for ellipsis. */
12156 for (argtype = argtypes, arity = 0;
12157 argtype && argtype != void_list_node;
12158 argtype = TREE_CHAIN (argtype))
12159 ++arity;
12160 ellipsis_p = !argtype;
12162 class_type = DECL_CONTEXT (decl);
12163 if (class_type && !CLASS_TYPE_P (class_type))
12164 class_type = NULL_TREE;
12166 if (DECL_CONV_FN_P (decl))
12167 operator_code = TYPE_EXPR;
12168 else
12171 #define DEF_OPERATOR(NAME, CODE, MANGLING, ARITY, ASSN_P) \
12172 if (ansi_opname (CODE) == name) \
12174 operator_code = (CODE); \
12175 break; \
12177 else if (ansi_assopname (CODE) == name) \
12179 operator_code = (CODE); \
12180 DECL_ASSIGNMENT_OPERATOR_P (decl) = 1; \
12181 break; \
12184 #include "operators.def"
12185 #undef DEF_OPERATOR
12187 gcc_unreachable ();
12189 while (0);
12190 gcc_assert (operator_code != MAX_TREE_CODES);
12191 SET_OVERLOADED_OPERATOR_CODE (decl, operator_code);
12193 if (class_type)
12194 switch (operator_code)
12196 case NEW_EXPR:
12197 TYPE_HAS_NEW_OPERATOR (class_type) = 1;
12198 break;
12200 case DELETE_EXPR:
12201 TYPE_GETS_DELETE (class_type) |= 1;
12202 break;
12204 case VEC_NEW_EXPR:
12205 TYPE_HAS_ARRAY_NEW_OPERATOR (class_type) = 1;
12206 break;
12208 case VEC_DELETE_EXPR:
12209 TYPE_GETS_DELETE (class_type) |= 2;
12210 break;
12212 default:
12213 break;
12216 /* [basic.std.dynamic.allocation]/1:
12218 A program is ill-formed if an allocation function is declared
12219 in a namespace scope other than global scope or declared static
12220 in global scope.
12222 The same also holds true for deallocation functions. */
12223 if (operator_code == NEW_EXPR || operator_code == VEC_NEW_EXPR
12224 || operator_code == DELETE_EXPR || operator_code == VEC_DELETE_EXPR)
12226 if (DECL_NAMESPACE_SCOPE_P (decl))
12228 if (CP_DECL_CONTEXT (decl) != global_namespace)
12230 error ("%qD may not be declared within a namespace", decl);
12231 return false;
12233 else if (!TREE_PUBLIC (decl))
12235 error ("%qD may not be declared as static", decl);
12236 return false;
12241 if (operator_code == NEW_EXPR || operator_code == VEC_NEW_EXPR)
12243 TREE_TYPE (decl) = coerce_new_type (TREE_TYPE (decl));
12244 DECL_IS_OPERATOR_NEW (decl) = 1;
12246 else if (operator_code == DELETE_EXPR || operator_code == VEC_DELETE_EXPR)
12247 TREE_TYPE (decl) = coerce_delete_type (TREE_TYPE (decl));
12248 else
12250 /* An operator function must either be a non-static member function
12251 or have at least one parameter of a class, a reference to a class,
12252 an enumeration, or a reference to an enumeration. 13.4.0.6 */
12253 if (! methodp || DECL_STATIC_FUNCTION_P (decl))
12255 if (operator_code == TYPE_EXPR
12256 || operator_code == CALL_EXPR
12257 || operator_code == COMPONENT_REF
12258 || operator_code == ARRAY_REF
12259 || operator_code == NOP_EXPR)
12261 error ("%qD must be a nonstatic member function", decl);
12262 return false;
12264 else
12266 tree p;
12268 if (DECL_STATIC_FUNCTION_P (decl))
12270 error ("%qD must be either a non-static member "
12271 "function or a non-member function", decl);
12272 return false;
12275 for (p = argtypes; p && p != void_list_node; p = TREE_CHAIN (p))
12277 tree arg = non_reference (TREE_VALUE (p));
12278 if (arg == error_mark_node)
12279 return false;
12281 /* MAYBE_CLASS_TYPE_P, rather than CLASS_TYPE_P, is used
12282 because these checks are performed even on
12283 template functions. */
12284 if (MAYBE_CLASS_TYPE_P (arg)
12285 || TREE_CODE (arg) == ENUMERAL_TYPE)
12286 break;
12289 if (!p || p == void_list_node)
12291 if (complain)
12292 error ("%qD must have an argument of class or "
12293 "enumerated type", decl);
12294 return false;
12299 /* There are no restrictions on the arguments to an overloaded
12300 "operator ()". */
12301 if (operator_code == CALL_EXPR)
12302 return true;
12304 /* Warn about conversion operators that will never be used. */
12305 if (IDENTIFIER_TYPENAME_P (name)
12306 && ! DECL_TEMPLATE_INFO (decl)
12307 && warn_conversion
12308 /* Warn only declaring the function; there is no need to
12309 warn again about out-of-class definitions. */
12310 && class_type == current_class_type)
12312 tree t = TREE_TYPE (name);
12313 int ref = (TREE_CODE (t) == REFERENCE_TYPE);
12315 if (ref)
12316 t = TYPE_MAIN_VARIANT (TREE_TYPE (t));
12318 if (VOID_TYPE_P (t))
12319 warning (OPT_Wconversion,
12321 ? G_("conversion to a reference to void "
12322 "will never use a type conversion operator")
12323 : G_("conversion to void "
12324 "will never use a type conversion operator"));
12325 else if (class_type)
12327 if (t == class_type)
12328 warning (OPT_Wconversion,
12330 ? G_("conversion to a reference to the same type "
12331 "will never use a type conversion operator")
12332 : G_("conversion to the same type "
12333 "will never use a type conversion operator"));
12334 /* Don't force t to be complete here. */
12335 else if (MAYBE_CLASS_TYPE_P (t)
12336 && COMPLETE_TYPE_P (t)
12337 && DERIVED_FROM_P (t, class_type))
12338 warning (OPT_Wconversion,
12340 ? G_("conversion to a reference to a base class "
12341 "will never use a type conversion operator")
12342 : G_("conversion to a base class "
12343 "will never use a type conversion operator"));
12348 if (operator_code == COND_EXPR)
12350 /* 13.4.0.3 */
12351 error ("ISO C++ prohibits overloading operator ?:");
12352 return false;
12354 else if (ellipsis_p)
12356 error ("%qD must not have variable number of arguments", decl);
12357 return false;
12359 else if (ambi_op_p (operator_code))
12361 if (arity == 1)
12362 /* We pick the one-argument operator codes by default, so
12363 we don't have to change anything. */
12365 else if (arity == 2)
12367 /* If we thought this was a unary operator, we now know
12368 it to be a binary operator. */
12369 switch (operator_code)
12371 case INDIRECT_REF:
12372 operator_code = MULT_EXPR;
12373 break;
12375 case ADDR_EXPR:
12376 operator_code = BIT_AND_EXPR;
12377 break;
12379 case UNARY_PLUS_EXPR:
12380 operator_code = PLUS_EXPR;
12381 break;
12383 case NEGATE_EXPR:
12384 operator_code = MINUS_EXPR;
12385 break;
12387 case PREINCREMENT_EXPR:
12388 operator_code = POSTINCREMENT_EXPR;
12389 break;
12391 case PREDECREMENT_EXPR:
12392 operator_code = POSTDECREMENT_EXPR;
12393 break;
12395 default:
12396 gcc_unreachable ();
12399 SET_OVERLOADED_OPERATOR_CODE (decl, operator_code);
12401 if ((operator_code == POSTINCREMENT_EXPR
12402 || operator_code == POSTDECREMENT_EXPR)
12403 && ! processing_template_decl
12404 && ! same_type_p (TREE_VALUE (TREE_CHAIN (argtypes)), integer_type_node))
12406 if (methodp)
12407 error ("postfix %qD must take %<int%> as its argument",
12408 decl);
12409 else
12410 error ("postfix %qD must take %<int%> as its second "
12411 "argument", decl);
12412 return false;
12415 else
12417 if (methodp)
12418 error ("%qD must take either zero or one argument", decl);
12419 else
12420 error ("%qD must take either one or two arguments", decl);
12421 return false;
12424 /* More Effective C++ rule 6. */
12425 if (warn_ecpp
12426 && (operator_code == POSTINCREMENT_EXPR
12427 || operator_code == POSTDECREMENT_EXPR
12428 || operator_code == PREINCREMENT_EXPR
12429 || operator_code == PREDECREMENT_EXPR))
12431 tree arg = TREE_VALUE (argtypes);
12432 tree ret = TREE_TYPE (TREE_TYPE (decl));
12433 if (methodp || TREE_CODE (arg) == REFERENCE_TYPE)
12434 arg = TREE_TYPE (arg);
12435 arg = TYPE_MAIN_VARIANT (arg);
12436 if (operator_code == PREINCREMENT_EXPR
12437 || operator_code == PREDECREMENT_EXPR)
12439 if (TREE_CODE (ret) != REFERENCE_TYPE
12440 || !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (ret)),
12441 arg))
12442 warning (OPT_Weffc__, "prefix %qD should return %qT", decl,
12443 build_reference_type (arg));
12445 else
12447 if (!same_type_p (TYPE_MAIN_VARIANT (ret), arg))
12448 warning (OPT_Weffc__, "postfix %qD should return %qT", decl, arg);
12452 else if (unary_op_p (operator_code))
12454 if (arity != 1)
12456 if (methodp)
12457 error ("%qD must take %<void%>", decl);
12458 else
12459 error ("%qD must take exactly one argument", decl);
12460 return false;
12463 else /* if (binary_op_p (operator_code)) */
12465 if (arity != 2)
12467 if (methodp)
12468 error ("%qD must take exactly one argument", decl);
12469 else
12470 error ("%qD must take exactly two arguments", decl);
12471 return false;
12474 /* More Effective C++ rule 7. */
12475 if (warn_ecpp
12476 && (operator_code == TRUTH_ANDIF_EXPR
12477 || operator_code == TRUTH_ORIF_EXPR
12478 || operator_code == COMPOUND_EXPR))
12479 warning (OPT_Weffc__, "user-defined %qD always evaluates both arguments",
12480 decl);
12483 /* Effective C++ rule 23. */
12484 if (warn_ecpp
12485 && arity == 2
12486 && !DECL_ASSIGNMENT_OPERATOR_P (decl)
12487 && (operator_code == PLUS_EXPR
12488 || operator_code == MINUS_EXPR
12489 || operator_code == TRUNC_DIV_EXPR
12490 || operator_code == MULT_EXPR
12491 || operator_code == TRUNC_MOD_EXPR)
12492 && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == REFERENCE_TYPE)
12493 warning (OPT_Weffc__, "%qD should return by value", decl);
12495 /* [over.oper]/8 */
12496 for (; argtypes && argtypes != void_list_node;
12497 argtypes = TREE_CHAIN (argtypes))
12498 if (TREE_PURPOSE (argtypes))
12500 TREE_PURPOSE (argtypes) = NULL_TREE;
12501 if (operator_code == POSTINCREMENT_EXPR
12502 || operator_code == POSTDECREMENT_EXPR)
12504 pedwarn (input_location, OPT_Wpedantic, "%qD cannot have default arguments",
12505 decl);
12507 else
12509 error ("%qD cannot have default arguments", decl);
12510 return false;
12514 return true;
12517 /* Return a string giving the keyword associate with CODE. */
12519 static const char *
12520 tag_name (enum tag_types code)
12522 switch (code)
12524 case record_type:
12525 return "struct";
12526 case class_type:
12527 return "class";
12528 case union_type:
12529 return "union";
12530 case enum_type:
12531 return "enum";
12532 case typename_type:
12533 return "typename";
12534 default:
12535 gcc_unreachable ();
12539 /* Name lookup in an elaborated-type-specifier (after the keyword
12540 indicated by TAG_CODE) has found the TYPE_DECL DECL. If the
12541 elaborated-type-specifier is invalid, issue a diagnostic and return
12542 error_mark_node; otherwise, return the *_TYPE to which it referred.
12543 If ALLOW_TEMPLATE_P is true, TYPE may be a class template. */
12545 tree
12546 check_elaborated_type_specifier (enum tag_types tag_code,
12547 tree decl,
12548 bool allow_template_p)
12550 tree type;
12552 /* In the case of:
12554 struct S { struct S *p; };
12556 name lookup will find the TYPE_DECL for the implicit "S::S"
12557 typedef. Adjust for that here. */
12558 if (DECL_SELF_REFERENCE_P (decl))
12559 decl = TYPE_NAME (TREE_TYPE (decl));
12561 type = TREE_TYPE (decl);
12563 /* Check TEMPLATE_TYPE_PARM first because DECL_IMPLICIT_TYPEDEF_P
12564 is false for this case as well. */
12565 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
12567 error ("using template type parameter %qT after %qs",
12568 type, tag_name (tag_code));
12569 return error_mark_node;
12571 /* Accept template template parameters. */
12572 else if (allow_template_p
12573 && (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM
12574 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM))
12576 /* [dcl.type.elab]
12578 If the identifier resolves to a typedef-name or the
12579 simple-template-id resolves to an alias template
12580 specialization, the elaborated-type-specifier is ill-formed.
12582 In other words, the only legitimate declaration to use in the
12583 elaborated type specifier is the implicit typedef created when
12584 the type is declared. */
12585 else if (!DECL_IMPLICIT_TYPEDEF_P (decl)
12586 && !DECL_SELF_REFERENCE_P (decl)
12587 && tag_code != typename_type)
12589 if (alias_template_specialization_p (type))
12590 error ("using alias template specialization %qT after %qs",
12591 type, tag_name (tag_code));
12592 else
12593 error ("using typedef-name %qD after %qs", decl, tag_name (tag_code));
12594 inform (DECL_SOURCE_LOCATION (decl),
12595 "%qD has a previous declaration here", decl);
12596 return error_mark_node;
12598 else if (TREE_CODE (type) != RECORD_TYPE
12599 && TREE_CODE (type) != UNION_TYPE
12600 && tag_code != enum_type
12601 && tag_code != typename_type)
12603 error ("%qT referred to as %qs", type, tag_name (tag_code));
12604 inform (location_of (type), "%qT has a previous declaration here", type);
12605 return error_mark_node;
12607 else if (TREE_CODE (type) != ENUMERAL_TYPE
12608 && tag_code == enum_type)
12610 error ("%qT referred to as enum", type);
12611 inform (location_of (type), "%qT has a previous declaration here", type);
12612 return error_mark_node;
12614 else if (!allow_template_p
12615 && TREE_CODE (type) == RECORD_TYPE
12616 && CLASSTYPE_IS_TEMPLATE (type))
12618 /* If a class template appears as elaborated type specifier
12619 without a template header such as:
12621 template <class T> class C {};
12622 void f(class C); // No template header here
12624 then the required template argument is missing. */
12625 error ("template argument required for %<%s %T%>",
12626 tag_name (tag_code),
12627 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)));
12628 return error_mark_node;
12631 return type;
12634 /* Lookup NAME in elaborate type specifier in scope according to
12635 SCOPE and issue diagnostics if necessary.
12636 Return *_TYPE node upon success, NULL_TREE when the NAME is not
12637 found, and ERROR_MARK_NODE for type error. */
12639 static tree
12640 lookup_and_check_tag (enum tag_types tag_code, tree name,
12641 tag_scope scope, bool template_header_p)
12643 tree t;
12644 tree decl;
12645 if (scope == ts_global)
12647 /* First try ordinary name lookup, ignoring hidden class name
12648 injected via friend declaration. */
12649 decl = lookup_name_prefer_type (name, 2);
12650 decl = strip_using_decl (decl);
12651 /* If that fails, the name will be placed in the smallest
12652 non-class, non-function-prototype scope according to 3.3.1/5.
12653 We may already have a hidden name declared as friend in this
12654 scope. So lookup again but not ignoring hidden names.
12655 If we find one, that name will be made visible rather than
12656 creating a new tag. */
12657 if (!decl)
12658 decl = lookup_type_scope (name, ts_within_enclosing_non_class);
12660 else
12661 decl = lookup_type_scope (name, scope);
12663 if (decl
12664 && (DECL_CLASS_TEMPLATE_P (decl)
12665 /* If scope is ts_current we're defining a class, so ignore a
12666 template template parameter. */
12667 || (scope != ts_current
12668 && DECL_TEMPLATE_TEMPLATE_PARM_P (decl))))
12669 decl = DECL_TEMPLATE_RESULT (decl);
12671 if (decl && TREE_CODE (decl) == TYPE_DECL)
12673 /* Look for invalid nested type:
12674 class C {
12675 class C {};
12676 }; */
12677 if (scope == ts_current && DECL_SELF_REFERENCE_P (decl))
12679 error ("%qD has the same name as the class in which it is "
12680 "declared",
12681 decl);
12682 return error_mark_node;
12685 /* Two cases we need to consider when deciding if a class
12686 template is allowed as an elaborated type specifier:
12687 1. It is a self reference to its own class.
12688 2. It comes with a template header.
12690 For example:
12692 template <class T> class C {
12693 class C *c1; // DECL_SELF_REFERENCE_P is true
12694 class D;
12696 template <class U> class C; // template_header_p is true
12697 template <class T> class C<T>::D {
12698 class C *c2; // DECL_SELF_REFERENCE_P is true
12699 }; */
12701 t = check_elaborated_type_specifier (tag_code,
12702 decl,
12703 template_header_p
12704 | DECL_SELF_REFERENCE_P (decl));
12705 if (template_header_p && t && CLASS_TYPE_P (t)
12706 && (!CLASSTYPE_TEMPLATE_INFO (t)
12707 || (!PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)))))
12709 error ("%qT is not a template", t);
12710 inform (location_of (t), "previous declaration here");
12711 if (TYPE_CLASS_SCOPE_P (t)
12712 && CLASSTYPE_TEMPLATE_INFO (TYPE_CONTEXT (t)))
12713 inform (input_location,
12714 "perhaps you want to explicitly add %<%T::%>",
12715 TYPE_CONTEXT (t));
12716 t = error_mark_node;
12719 return t;
12721 else if (decl && TREE_CODE (decl) == TREE_LIST)
12723 error ("reference to %qD is ambiguous", name);
12724 print_candidates (decl);
12725 return error_mark_node;
12727 else
12728 return NULL_TREE;
12731 /* Get the struct, enum or union (TAG_CODE says which) with tag NAME.
12732 Define the tag as a forward-reference if it is not defined.
12734 If a declaration is given, process it here, and report an error if
12735 multiple declarations are not identical.
12737 SCOPE is TS_CURRENT when this is also a definition. Only look in
12738 the current frame for the name (since C++ allows new names in any
12739 scope.) It is TS_WITHIN_ENCLOSING_NON_CLASS if this is a friend
12740 declaration. Only look beginning from the current scope outward up
12741 till the nearest non-class scope. Otherwise it is TS_GLOBAL.
12743 TEMPLATE_HEADER_P is true when this declaration is preceded by
12744 a set of template parameters. */
12746 static tree
12747 xref_tag_1 (enum tag_types tag_code, tree name,
12748 tag_scope orig_scope, bool template_header_p)
12750 enum tree_code code;
12751 tree t;
12752 tree context = NULL_TREE;
12753 tag_scope scope;
12755 gcc_assert (identifier_p (name));
12757 switch (tag_code)
12759 case record_type:
12760 case class_type:
12761 code = RECORD_TYPE;
12762 break;
12763 case union_type:
12764 code = UNION_TYPE;
12765 break;
12766 case enum_type:
12767 code = ENUMERAL_TYPE;
12768 break;
12769 default:
12770 gcc_unreachable ();
12773 if (orig_scope == ts_lambda)
12774 scope = ts_current;
12775 else
12776 scope = orig_scope;
12778 /* In case of anonymous name, xref_tag is only called to
12779 make type node and push name. Name lookup is not required. */
12780 if (anon_aggrname_p (name))
12781 t = NULL_TREE;
12782 else
12783 t = lookup_and_check_tag (tag_code, name,
12784 scope, template_header_p);
12786 if (t == error_mark_node)
12787 return error_mark_node;
12789 if (scope != ts_current && t && current_class_type
12790 && template_class_depth (current_class_type)
12791 && template_header_p)
12793 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
12794 return t;
12796 /* Since SCOPE is not TS_CURRENT, we are not looking at a
12797 definition of this tag. Since, in addition, we are currently
12798 processing a (member) template declaration of a template
12799 class, we must be very careful; consider:
12801 template <class X>
12802 struct S1
12804 template <class U>
12805 struct S2
12806 { template <class V>
12807 friend struct S1; };
12809 Here, the S2::S1 declaration should not be confused with the
12810 outer declaration. In particular, the inner version should
12811 have a template parameter of level 2, not level 1. This
12812 would be particularly important if the member declaration
12813 were instead:
12815 template <class V = U> friend struct S1;
12817 say, when we should tsubst into `U' when instantiating
12818 S2. On the other hand, when presented with:
12820 template <class T>
12821 struct S1 {
12822 template <class U>
12823 struct S2 {};
12824 template <class U>
12825 friend struct S2;
12828 we must find the inner binding eventually. We
12829 accomplish this by making sure that the new type we
12830 create to represent this declaration has the right
12831 TYPE_CONTEXT. */
12832 context = TYPE_CONTEXT (t);
12833 t = NULL_TREE;
12836 if (! t)
12838 /* If no such tag is yet defined, create a forward-reference node
12839 and record it as the "definition".
12840 When a real declaration of this type is found,
12841 the forward-reference will be altered into a real type. */
12842 if (code == ENUMERAL_TYPE)
12844 error ("use of enum %q#D without previous declaration", name);
12845 return error_mark_node;
12847 else
12849 t = make_class_type (code);
12850 TYPE_CONTEXT (t) = context;
12851 if (orig_scope == ts_lambda)
12852 /* Remember that we're declaring a lambda to avoid bogus errors
12853 in push_template_decl. */
12854 CLASSTYPE_LAMBDA_EXPR (t) = error_mark_node;
12855 t = pushtag (name, t, scope);
12858 else
12860 if (template_header_p && MAYBE_CLASS_TYPE_P (t))
12862 /* Check that we aren't trying to overload a class with different
12863 constraints. */
12864 tree constr = NULL_TREE;
12865 if (current_template_parms)
12867 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
12868 constr = build_constraints (reqs, NULL_TREE);
12870 if (!redeclare_class_template (t, current_template_parms, constr))
12871 return error_mark_node;
12873 else if (!processing_template_decl
12874 && CLASS_TYPE_P (t)
12875 && CLASSTYPE_IS_TEMPLATE (t))
12877 error ("redeclaration of %qT as a non-template", t);
12878 inform (location_of (t), "previous declaration %qD", t);
12879 return error_mark_node;
12882 /* Make injected friend class visible. */
12883 if (scope != ts_within_enclosing_non_class
12884 && hidden_name_p (TYPE_NAME (t)))
12886 DECL_ANTICIPATED (TYPE_NAME (t)) = 0;
12887 DECL_FRIEND_P (TYPE_NAME (t)) = 0;
12889 if (TYPE_TEMPLATE_INFO (t))
12891 DECL_ANTICIPATED (TYPE_TI_TEMPLATE (t)) = 0;
12892 DECL_FRIEND_P (TYPE_TI_TEMPLATE (t)) = 0;
12897 return t;
12900 /* Wrapper for xref_tag_1. */
12902 tree
12903 xref_tag (enum tag_types tag_code, tree name,
12904 tag_scope scope, bool template_header_p)
12906 tree ret;
12907 bool subtime;
12908 subtime = timevar_cond_start (TV_NAME_LOOKUP);
12909 ret = xref_tag_1 (tag_code, name, scope, template_header_p);
12910 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
12911 return ret;
12915 tree
12916 xref_tag_from_type (tree old, tree id, tag_scope scope)
12918 enum tag_types tag_kind;
12920 if (TREE_CODE (old) == RECORD_TYPE)
12921 tag_kind = (CLASSTYPE_DECLARED_CLASS (old) ? class_type : record_type);
12922 else
12923 tag_kind = union_type;
12925 if (id == NULL_TREE)
12926 id = TYPE_IDENTIFIER (old);
12928 return xref_tag (tag_kind, id, scope, false);
12931 /* Create the binfo hierarchy for REF with (possibly NULL) base list
12932 BASE_LIST. For each element on BASE_LIST the TREE_PURPOSE is an
12933 access_* node, and the TREE_VALUE is the type of the base-class.
12934 Non-NULL TREE_TYPE indicates virtual inheritance. */
12936 void
12937 xref_basetypes (tree ref, tree base_list)
12939 tree *basep;
12940 tree binfo, base_binfo;
12941 unsigned max_vbases = 0; /* Maximum direct & indirect virtual bases. */
12942 unsigned max_bases = 0; /* Maximum direct bases. */
12943 unsigned max_dvbases = 0; /* Maximum direct virtual bases. */
12944 int i;
12945 tree default_access;
12946 tree igo_prev; /* Track Inheritance Graph Order. */
12948 if (ref == error_mark_node)
12949 return;
12951 /* The base of a derived class is private by default, all others are
12952 public. */
12953 default_access = (TREE_CODE (ref) == RECORD_TYPE
12954 && CLASSTYPE_DECLARED_CLASS (ref)
12955 ? access_private_node : access_public_node);
12957 /* First, make sure that any templates in base-classes are
12958 instantiated. This ensures that if we call ourselves recursively
12959 we do not get confused about which classes are marked and which
12960 are not. */
12961 basep = &base_list;
12962 while (*basep)
12964 tree basetype = TREE_VALUE (*basep);
12966 /* The dependent_type_p call below should really be dependent_scope_p
12967 so that we give a hard error about using an incomplete type as a
12968 base, but we allow it with a pedwarn for backward
12969 compatibility. */
12970 if (processing_template_decl
12971 && CLASS_TYPE_P (basetype) && TYPE_BEING_DEFINED (basetype))
12972 cxx_incomplete_type_diagnostic (NULL_TREE, basetype, DK_PEDWARN);
12973 if (!dependent_type_p (basetype)
12974 && !complete_type_or_else (basetype, NULL))
12975 /* An incomplete type. Remove it from the list. */
12976 *basep = TREE_CHAIN (*basep);
12977 else
12979 max_bases++;
12980 if (TREE_TYPE (*basep))
12981 max_dvbases++;
12982 if (CLASS_TYPE_P (basetype))
12983 max_vbases += vec_safe_length (CLASSTYPE_VBASECLASSES (basetype));
12984 basep = &TREE_CHAIN (*basep);
12987 max_vbases += max_dvbases;
12989 TYPE_MARKED_P (ref) = 1;
12991 /* The binfo slot should be empty, unless this is an (ill-formed)
12992 redefinition. */
12993 gcc_assert (!TYPE_BINFO (ref) || TYPE_SIZE (ref));
12995 gcc_assert (TYPE_MAIN_VARIANT (ref) == ref);
12997 binfo = make_tree_binfo (max_bases);
12999 TYPE_BINFO (ref) = binfo;
13000 BINFO_OFFSET (binfo) = size_zero_node;
13001 BINFO_TYPE (binfo) = ref;
13003 /* Apply base-class info set up to the variants of this type. */
13004 fixup_type_variants (ref);
13006 if (max_bases)
13008 vec_alloc (BINFO_BASE_ACCESSES (binfo), max_bases);
13009 /* An aggregate cannot have baseclasses. */
13010 CLASSTYPE_NON_AGGREGATE (ref) = 1;
13012 if (TREE_CODE (ref) == UNION_TYPE)
13013 error ("derived union %qT invalid", ref);
13016 if (max_bases > 1)
13017 warning (OPT_Wmultiple_inheritance,
13018 "%qT defined with multiple direct bases", ref);
13020 if (max_vbases)
13022 vec_alloc (CLASSTYPE_VBASECLASSES (ref), max_vbases);
13024 if (max_dvbases)
13025 warning (OPT_Wvirtual_inheritance,
13026 "%qT defined with direct virtual base", ref);
13029 for (igo_prev = binfo; base_list; base_list = TREE_CHAIN (base_list))
13031 tree access = TREE_PURPOSE (base_list);
13032 int via_virtual = TREE_TYPE (base_list) != NULL_TREE;
13033 tree basetype = TREE_VALUE (base_list);
13035 if (access == access_default_node)
13036 access = default_access;
13038 if (PACK_EXPANSION_P (basetype))
13039 basetype = PACK_EXPANSION_PATTERN (basetype);
13040 if (TREE_CODE (basetype) == TYPE_DECL)
13041 basetype = TREE_TYPE (basetype);
13042 if (!MAYBE_CLASS_TYPE_P (basetype) || TREE_CODE (basetype) == UNION_TYPE)
13044 error ("base type %qT fails to be a struct or class type",
13045 basetype);
13046 goto dropped_base;
13049 base_binfo = NULL_TREE;
13050 if (CLASS_TYPE_P (basetype) && !dependent_scope_p (basetype))
13052 base_binfo = TYPE_BINFO (basetype);
13053 /* The original basetype could have been a typedef'd type. */
13054 basetype = BINFO_TYPE (base_binfo);
13056 /* Inherit flags from the base. */
13057 TYPE_HAS_NEW_OPERATOR (ref)
13058 |= TYPE_HAS_NEW_OPERATOR (basetype);
13059 TYPE_HAS_ARRAY_NEW_OPERATOR (ref)
13060 |= TYPE_HAS_ARRAY_NEW_OPERATOR (basetype);
13061 TYPE_GETS_DELETE (ref) |= TYPE_GETS_DELETE (basetype);
13062 TYPE_HAS_CONVERSION (ref) |= TYPE_HAS_CONVERSION (basetype);
13063 CLASSTYPE_DIAMOND_SHAPED_P (ref)
13064 |= CLASSTYPE_DIAMOND_SHAPED_P (basetype);
13065 CLASSTYPE_REPEATED_BASE_P (ref)
13066 |= CLASSTYPE_REPEATED_BASE_P (basetype);
13069 /* We must do this test after we've seen through a typedef
13070 type. */
13071 if (TYPE_MARKED_P (basetype))
13073 if (basetype == ref)
13074 error ("recursive type %qT undefined", basetype);
13075 else
13076 error ("duplicate base type %qT invalid", basetype);
13077 goto dropped_base;
13080 if (PACK_EXPANSION_P (TREE_VALUE (base_list)))
13081 /* Regenerate the pack expansion for the bases. */
13082 basetype = make_pack_expansion (basetype);
13084 TYPE_MARKED_P (basetype) = 1;
13086 base_binfo = copy_binfo (base_binfo, basetype, ref,
13087 &igo_prev, via_virtual);
13088 if (!BINFO_INHERITANCE_CHAIN (base_binfo))
13089 BINFO_INHERITANCE_CHAIN (base_binfo) = binfo;
13091 BINFO_BASE_APPEND (binfo, base_binfo);
13092 BINFO_BASE_ACCESS_APPEND (binfo, access);
13093 continue;
13095 dropped_base:
13096 /* Update max_vbases to reflect the reality that we are dropping
13097 this base: if it reaches zero we want to undo the vec_alloc
13098 above to avoid inconsistencies during error-recovery: eg, in
13099 build_special_member_call, CLASSTYPE_VBASECLASSES non null
13100 and vtt null (c++/27952). */
13101 if (via_virtual)
13102 max_vbases--;
13103 if (CLASS_TYPE_P (basetype))
13104 max_vbases
13105 -= vec_safe_length (CLASSTYPE_VBASECLASSES (basetype));
13108 if (CLASSTYPE_VBASECLASSES (ref)
13109 && max_vbases == 0)
13110 vec_free (CLASSTYPE_VBASECLASSES (ref));
13112 if (vec_safe_length (CLASSTYPE_VBASECLASSES (ref)) < max_vbases)
13113 /* If we didn't get max_vbases vbases, we must have shared at
13114 least one of them, and are therefore diamond shaped. */
13115 CLASSTYPE_DIAMOND_SHAPED_P (ref) = 1;
13117 /* Unmark all the types. */
13118 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
13119 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
13120 TYPE_MARKED_P (ref) = 0;
13122 /* Now see if we have a repeated base type. */
13123 if (!CLASSTYPE_REPEATED_BASE_P (ref))
13125 for (base_binfo = binfo; base_binfo;
13126 base_binfo = TREE_CHAIN (base_binfo))
13128 if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
13130 CLASSTYPE_REPEATED_BASE_P (ref) = 1;
13131 break;
13133 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 1;
13135 for (base_binfo = binfo; base_binfo;
13136 base_binfo = TREE_CHAIN (base_binfo))
13137 if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
13138 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
13139 else
13140 break;
13145 /* Copies the enum-related properties from type SRC to type DST.
13146 Used with the underlying type of an enum and the enum itself. */
13147 static void
13148 copy_type_enum (tree dst, tree src)
13150 tree t;
13151 for (t = dst; t; t = TYPE_NEXT_VARIANT (t))
13153 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (src);
13154 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (src);
13155 TYPE_SIZE (t) = TYPE_SIZE (src);
13156 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (src);
13157 SET_TYPE_MODE (dst, TYPE_MODE (src));
13158 TYPE_PRECISION (t) = TYPE_PRECISION (src);
13159 unsigned valign = TYPE_ALIGN (src);
13160 if (TYPE_USER_ALIGN (t))
13161 valign = MAX (valign, TYPE_ALIGN (t));
13162 else
13163 TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (src);
13164 SET_TYPE_ALIGN (t, valign);
13165 TYPE_UNSIGNED (t) = TYPE_UNSIGNED (src);
13169 /* Begin compiling the definition of an enumeration type.
13170 NAME is its name,
13172 if ENUMTYPE is not NULL_TREE then the type has alredy been found.
13174 UNDERLYING_TYPE is the type that will be used as the storage for
13175 the enumeration type. This should be NULL_TREE if no storage type
13176 was specified.
13178 ATTRIBUTES are any attributes specified after the enum-key.
13180 SCOPED_ENUM_P is true if this is a scoped enumeration type.
13182 if IS_NEW is not NULL, gets TRUE iff a new type is created.
13184 Returns the type object, as yet incomplete.
13185 Also records info about it so that build_enumerator
13186 may be used to declare the individual values as they are read. */
13188 tree
13189 start_enum (tree name, tree enumtype, tree underlying_type,
13190 tree attributes, bool scoped_enum_p, bool *is_new)
13192 tree prevtype = NULL_TREE;
13193 gcc_assert (identifier_p (name));
13195 if (is_new)
13196 *is_new = false;
13197 /* [C++0x dcl.enum]p5:
13199 If not explicitly specified, the underlying type of a scoped
13200 enumeration type is int. */
13201 if (!underlying_type && scoped_enum_p)
13202 underlying_type = integer_type_node;
13204 if (underlying_type)
13205 underlying_type = cv_unqualified (underlying_type);
13207 /* If this is the real definition for a previous forward reference,
13208 fill in the contents in the same object that used to be the
13209 forward reference. */
13210 if (!enumtype)
13211 enumtype = lookup_and_check_tag (enum_type, name,
13212 /*tag_scope=*/ts_current,
13213 /*template_header_p=*/false);
13215 /* In case of a template_decl, the only check that should be deferred
13216 to instantiation time is the comparison of underlying types. */
13217 if (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE)
13219 if (scoped_enum_p != SCOPED_ENUM_P (enumtype))
13221 error_at (input_location, "scoped/unscoped mismatch "
13222 "in enum %q#T", enumtype);
13223 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
13224 "previous definition here");
13225 enumtype = error_mark_node;
13227 else if (ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) != !! underlying_type)
13229 error_at (input_location, "underlying type mismatch "
13230 "in enum %q#T", enumtype);
13231 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
13232 "previous definition here");
13233 enumtype = error_mark_node;
13235 else if (underlying_type && ENUM_UNDERLYING_TYPE (enumtype)
13236 && !dependent_type_p (underlying_type)
13237 && !dependent_type_p (ENUM_UNDERLYING_TYPE (enumtype))
13238 && !same_type_p (underlying_type,
13239 ENUM_UNDERLYING_TYPE (enumtype)))
13241 error_at (input_location, "different underlying type "
13242 "in enum %q#T", enumtype);
13243 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
13244 "previous definition here");
13245 underlying_type = NULL_TREE;
13249 if (!enumtype || TREE_CODE (enumtype) != ENUMERAL_TYPE
13250 || processing_template_decl)
13252 /* In case of error, make a dummy enum to allow parsing to
13253 continue. */
13254 if (enumtype == error_mark_node)
13256 name = make_anon_name ();
13257 enumtype = NULL_TREE;
13260 /* enumtype may be an ENUMERAL_TYPE if this is a redefinition
13261 of an opaque enum, or an opaque enum of an already defined
13262 enumeration (C++0x only).
13263 In any other case, it'll be NULL_TREE. */
13264 if (!enumtype)
13266 if (is_new)
13267 *is_new = true;
13269 prevtype = enumtype;
13271 /* Do not push the decl more than once, unless we need to
13272 compare underlying types at instantiation time */
13273 if (!enumtype
13274 || TREE_CODE (enumtype) != ENUMERAL_TYPE
13275 || (underlying_type
13276 && dependent_type_p (underlying_type))
13277 || (ENUM_UNDERLYING_TYPE (enumtype)
13278 && dependent_type_p (ENUM_UNDERLYING_TYPE (enumtype))))
13280 enumtype = cxx_make_type (ENUMERAL_TYPE);
13281 enumtype = pushtag (name, enumtype, /*tag_scope=*/ts_current);
13283 else
13284 enumtype = xref_tag (enum_type, name, /*tag_scope=*/ts_current,
13285 false);
13287 if (enumtype == error_mark_node)
13288 return error_mark_node;
13290 /* The enum is considered opaque until the opening '{' of the
13291 enumerator list. */
13292 SET_OPAQUE_ENUM_P (enumtype, true);
13293 ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) = !! underlying_type;
13296 SET_SCOPED_ENUM_P (enumtype, scoped_enum_p);
13298 cplus_decl_attributes (&enumtype, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
13300 if (underlying_type)
13302 if (ENUM_UNDERLYING_TYPE (enumtype))
13303 /* We already checked that it matches, don't change it to a different
13304 typedef variant. */;
13305 else if (CP_INTEGRAL_TYPE_P (underlying_type))
13307 copy_type_enum (enumtype, underlying_type);
13308 ENUM_UNDERLYING_TYPE (enumtype) = underlying_type;
13310 else if (dependent_type_p (underlying_type))
13311 ENUM_UNDERLYING_TYPE (enumtype) = underlying_type;
13312 else
13313 error ("underlying type %<%T%> of %<%T%> must be an integral type",
13314 underlying_type, enumtype);
13317 /* If into a template class, the returned enum is always the first
13318 declaration (opaque or not) seen. This way all the references to
13319 this type will be to the same declaration. The following ones are used
13320 only to check for definition errors. */
13321 if (prevtype && processing_template_decl)
13322 return prevtype;
13323 else
13324 return enumtype;
13327 /* After processing and defining all the values of an enumeration type,
13328 install their decls in the enumeration type.
13329 ENUMTYPE is the type object. */
13331 void
13332 finish_enum_value_list (tree enumtype)
13334 tree values;
13335 tree underlying_type;
13336 tree decl;
13337 tree value;
13338 tree minnode, maxnode;
13339 tree t;
13341 bool fixed_underlying_type_p
13342 = ENUM_UNDERLYING_TYPE (enumtype) != NULL_TREE;
13344 /* We built up the VALUES in reverse order. */
13345 TYPE_VALUES (enumtype) = nreverse (TYPE_VALUES (enumtype));
13347 /* For an enum defined in a template, just set the type of the values;
13348 all further processing is postponed until the template is
13349 instantiated. We need to set the type so that tsubst of a CONST_DECL
13350 works. */
13351 if (processing_template_decl)
13353 for (values = TYPE_VALUES (enumtype);
13354 values;
13355 values = TREE_CHAIN (values))
13356 TREE_TYPE (TREE_VALUE (values)) = enumtype;
13357 return;
13360 /* Determine the minimum and maximum values of the enumerators. */
13361 if (TYPE_VALUES (enumtype))
13363 minnode = maxnode = NULL_TREE;
13365 for (values = TYPE_VALUES (enumtype);
13366 values;
13367 values = TREE_CHAIN (values))
13369 decl = TREE_VALUE (values);
13371 /* [dcl.enum]: Following the closing brace of an enum-specifier,
13372 each enumerator has the type of its enumeration. Prior to the
13373 closing brace, the type of each enumerator is the type of its
13374 initializing value. */
13375 TREE_TYPE (decl) = enumtype;
13377 /* Update the minimum and maximum values, if appropriate. */
13378 value = DECL_INITIAL (decl);
13379 if (value == error_mark_node)
13380 value = integer_zero_node;
13381 /* Figure out what the minimum and maximum values of the
13382 enumerators are. */
13383 if (!minnode)
13384 minnode = maxnode = value;
13385 else if (tree_int_cst_lt (maxnode, value))
13386 maxnode = value;
13387 else if (tree_int_cst_lt (value, minnode))
13388 minnode = value;
13391 else
13392 /* [dcl.enum]
13394 If the enumerator-list is empty, the underlying type is as if
13395 the enumeration had a single enumerator with value 0. */
13396 minnode = maxnode = integer_zero_node;
13398 if (!fixed_underlying_type_p)
13400 /* Compute the number of bits require to represent all values of the
13401 enumeration. We must do this before the type of MINNODE and
13402 MAXNODE are transformed, since tree_int_cst_min_precision relies
13403 on the TREE_TYPE of the value it is passed. */
13404 signop sgn = tree_int_cst_sgn (minnode) >= 0 ? UNSIGNED : SIGNED;
13405 int lowprec = tree_int_cst_min_precision (minnode, sgn);
13406 int highprec = tree_int_cst_min_precision (maxnode, sgn);
13407 int precision = MAX (lowprec, highprec);
13408 unsigned int itk;
13409 bool use_short_enum;
13411 /* Determine the underlying type of the enumeration.
13413 [dcl.enum]
13415 The underlying type of an enumeration is an integral type that
13416 can represent all the enumerator values defined in the
13417 enumeration. It is implementation-defined which integral type is
13418 used as the underlying type for an enumeration except that the
13419 underlying type shall not be larger than int unless the value of
13420 an enumerator cannot fit in an int or unsigned int.
13422 We use "int" or an "unsigned int" as the underlying type, even if
13423 a smaller integral type would work, unless the user has
13424 explicitly requested that we use the smallest possible type. The
13425 user can request that for all enumerations with a command line
13426 flag, or for just one enumeration with an attribute. */
13428 use_short_enum = flag_short_enums
13429 || lookup_attribute ("packed", TYPE_ATTRIBUTES (enumtype));
13431 /* If the precision of the type was specified with an attribute and it
13432 was too small, give an error. Otherwise, use it. */
13433 if (TYPE_PRECISION (enumtype))
13435 if (precision > TYPE_PRECISION (enumtype))
13436 error ("specified mode too small for enumeral values");
13437 else
13439 use_short_enum = true;
13440 precision = TYPE_PRECISION (enumtype);
13444 for (itk = (use_short_enum ? itk_char : itk_int);
13445 itk != itk_none;
13446 itk++)
13448 underlying_type = integer_types[itk];
13449 if (underlying_type != NULL_TREE
13450 && TYPE_PRECISION (underlying_type) >= precision
13451 && TYPE_SIGN (underlying_type) == sgn)
13452 break;
13454 if (itk == itk_none)
13456 /* DR 377
13458 IF no integral type can represent all the enumerator values, the
13459 enumeration is ill-formed. */
13460 error ("no integral type can represent all of the enumerator values "
13461 "for %qT", enumtype);
13462 precision = TYPE_PRECISION (long_long_integer_type_node);
13463 underlying_type = integer_types[itk_unsigned_long_long];
13466 /* [dcl.enum]
13468 The value of sizeof() applied to an enumeration type, an object
13469 of an enumeration type, or an enumerator, is the value of sizeof()
13470 applied to the underlying type. */
13471 copy_type_enum (enumtype, underlying_type);
13473 /* Compute the minimum and maximum values for the type.
13475 [dcl.enum]
13477 For an enumeration where emin is the smallest enumerator and emax
13478 is the largest, the values of the enumeration are the values of the
13479 underlying type in the range bmin to bmax, where bmin and bmax are,
13480 respectively, the smallest and largest values of the smallest bit-
13481 field that can store emin and emax. */
13483 /* The middle-end currently assumes that types with TYPE_PRECISION
13484 narrower than their underlying type are suitably zero or sign
13485 extended to fill their mode. Similarly, it assumes that the front
13486 end assures that a value of a particular type must be within
13487 TYPE_MIN_VALUE and TYPE_MAX_VALUE.
13489 We used to set these fields based on bmin and bmax, but that led
13490 to invalid assumptions like optimizing away bounds checking. So
13491 now we just set the TYPE_PRECISION, TYPE_MIN_VALUE, and
13492 TYPE_MAX_VALUE to the values for the mode above and only restrict
13493 the ENUM_UNDERLYING_TYPE for the benefit of diagnostics. */
13494 ENUM_UNDERLYING_TYPE (enumtype)
13495 = build_distinct_type_copy (underlying_type);
13496 TYPE_PRECISION (ENUM_UNDERLYING_TYPE (enumtype)) = precision;
13497 set_min_and_max_values_for_integral_type
13498 (ENUM_UNDERLYING_TYPE (enumtype), precision, sgn);
13500 /* If -fstrict-enums, still constrain TYPE_MIN/MAX_VALUE. */
13501 if (flag_strict_enums)
13502 set_min_and_max_values_for_integral_type (enumtype, precision, sgn);
13504 else
13505 underlying_type = ENUM_UNDERLYING_TYPE (enumtype);
13507 /* Convert each of the enumerators to the type of the underlying
13508 type of the enumeration. */
13509 for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values))
13511 location_t saved_location;
13513 decl = TREE_VALUE (values);
13514 saved_location = input_location;
13515 input_location = DECL_SOURCE_LOCATION (decl);
13516 if (fixed_underlying_type_p)
13517 /* If the enumeration type has a fixed underlying type, we
13518 already checked all of the enumerator values. */
13519 value = DECL_INITIAL (decl);
13520 else
13521 value = perform_implicit_conversion (underlying_type,
13522 DECL_INITIAL (decl),
13523 tf_warning_or_error);
13524 input_location = saved_location;
13526 /* Do not clobber shared ints. */
13527 value = copy_node (value);
13529 TREE_TYPE (value) = enumtype;
13530 DECL_INITIAL (decl) = value;
13533 /* Fix up all variant types of this enum type. */
13534 for (t = TYPE_MAIN_VARIANT (enumtype); t; t = TYPE_NEXT_VARIANT (t))
13535 TYPE_VALUES (t) = TYPE_VALUES (enumtype);
13537 if (at_class_scope_p ()
13538 && COMPLETE_TYPE_P (current_class_type)
13539 && UNSCOPED_ENUM_P (enumtype))
13540 insert_late_enum_def_into_classtype_sorted_fields (enumtype,
13541 current_class_type);
13543 /* Finish debugging output for this type. */
13544 rest_of_type_compilation (enumtype, namespace_bindings_p ());
13546 /* Each enumerator now has the type of its enumeration. Clear the cache
13547 so that this change in types doesn't confuse us later on. */
13548 clear_cv_and_fold_caches ();
13551 /* Finishes the enum type. This is called only the first time an
13552 enumeration is seen, be it opaque or odinary.
13553 ENUMTYPE is the type object. */
13555 void
13556 finish_enum (tree enumtype)
13558 if (processing_template_decl)
13560 if (at_function_scope_p ())
13561 add_stmt (build_min (TAG_DEFN, enumtype));
13562 return;
13565 /* If this is a forward declaration, there should not be any variants,
13566 though we can get a variant in the middle of an enum-specifier with
13567 wacky code like 'enum E { e = sizeof(const E*) };' */
13568 gcc_assert (enumtype == TYPE_MAIN_VARIANT (enumtype)
13569 && (TYPE_VALUES (enumtype)
13570 || !TYPE_NEXT_VARIANT (enumtype)));
13573 /* Build and install a CONST_DECL for an enumeration constant of the
13574 enumeration type ENUMTYPE whose NAME and VALUE (if any) are provided.
13575 Apply ATTRIBUTES if available. LOC is the location of NAME.
13576 Assignment of sequential values by default is handled here. */
13578 void
13579 build_enumerator (tree name, tree value, tree enumtype, tree attributes,
13580 location_t loc)
13582 tree decl;
13583 tree context;
13584 tree type;
13586 /* scalar_constant_value will pull out this expression, so make sure
13587 it's folded as appropriate. */
13588 if (processing_template_decl)
13589 value = fold_non_dependent_expr (value);
13591 /* If the VALUE was erroneous, pretend it wasn't there; that will
13592 result in the enum being assigned the next value in sequence. */
13593 if (value == error_mark_node)
13594 value = NULL_TREE;
13596 /* Remove no-op casts from the value. */
13597 if (value)
13598 STRIP_TYPE_NOPS (value);
13600 if (! processing_template_decl)
13602 /* Validate and default VALUE. */
13603 if (value != NULL_TREE)
13605 if (!ENUM_UNDERLYING_TYPE (enumtype))
13607 tree tmp_value = build_expr_type_conversion (WANT_INT | WANT_ENUM,
13608 value, true);
13609 if (tmp_value)
13610 value = tmp_value;
13612 else if (! INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P
13613 (TREE_TYPE (value)))
13614 value = perform_implicit_conversion_flags
13615 (ENUM_UNDERLYING_TYPE (enumtype), value, tf_warning_or_error,
13616 LOOKUP_IMPLICIT | LOOKUP_NO_NARROWING);
13618 if (value == error_mark_node)
13619 value = NULL_TREE;
13621 if (value != NULL_TREE)
13623 if (! INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P
13624 (TREE_TYPE (value)))
13626 error ("enumerator value for %qD must have integral or "
13627 "unscoped enumeration type", name);
13628 value = NULL_TREE;
13630 else
13632 value = cxx_constant_value (value);
13634 if (TREE_CODE (value) != INTEGER_CST)
13636 error ("enumerator value for %qD is not an integer "
13637 "constant", name);
13638 value = NULL_TREE;
13644 /* Default based on previous value. */
13645 if (value == NULL_TREE)
13647 if (TYPE_VALUES (enumtype))
13649 tree prev_value;
13650 bool overflowed;
13652 /* C++03 7.2/4: If no initializer is specified for the first
13653 enumerator, the type is an unspecified integral
13654 type. Otherwise the type is the same as the type of the
13655 initializing value of the preceding enumerator unless the
13656 incremented value is not representable in that type, in
13657 which case the type is an unspecified integral type
13658 sufficient to contain the incremented value. */
13659 prev_value = DECL_INITIAL (TREE_VALUE (TYPE_VALUES (enumtype)));
13660 if (error_operand_p (prev_value))
13661 value = error_mark_node;
13662 else
13664 tree type = TREE_TYPE (prev_value);
13665 signop sgn = TYPE_SIGN (type);
13666 widest_int wi = wi::add (wi::to_widest (prev_value), 1, sgn,
13667 &overflowed);
13668 if (!overflowed)
13670 bool pos = !wi::neg_p (wi, sgn);
13671 if (!wi::fits_to_tree_p (wi, type))
13673 unsigned int itk;
13674 for (itk = itk_int; itk != itk_none; itk++)
13676 type = integer_types[itk];
13677 if (type != NULL_TREE
13678 && (pos || !TYPE_UNSIGNED (type))
13679 && wi::fits_to_tree_p (wi, type))
13680 break;
13682 if (type && cxx_dialect < cxx11
13683 && itk > itk_unsigned_long)
13684 pedwarn (input_location, OPT_Wlong_long, pos ? "\
13685 incremented enumerator value is too large for %<unsigned long%>" : "\
13686 incremented enumerator value is too large for %<long%>");
13688 if (type == NULL_TREE)
13689 overflowed = true;
13690 else
13691 value = wide_int_to_tree (type, wi);
13694 if (overflowed)
13696 error ("overflow in enumeration values at %qD", name);
13697 value = error_mark_node;
13701 else
13702 value = integer_zero_node;
13705 /* Remove no-op casts from the value. */
13706 STRIP_TYPE_NOPS (value);
13708 /* If the underlying type of the enum is fixed, check whether
13709 the enumerator values fits in the underlying type. If it
13710 does not fit, the program is ill-formed [C++0x dcl.enum]. */
13711 if (ENUM_UNDERLYING_TYPE (enumtype)
13712 && value
13713 && TREE_CODE (value) == INTEGER_CST)
13715 if (!int_fits_type_p (value, ENUM_UNDERLYING_TYPE (enumtype)))
13716 error ("enumerator value %E is outside the range of underlying "
13717 "type %<%T%>", value, ENUM_UNDERLYING_TYPE (enumtype));
13719 /* Convert the value to the appropriate type. */
13720 value = fold_convert (ENUM_UNDERLYING_TYPE (enumtype), value);
13724 /* C++ associates enums with global, function, or class declarations. */
13725 context = current_scope ();
13727 /* Build the actual enumeration constant. Note that the enumeration
13728 constants have the underlying type of the enum (if it is fixed)
13729 or the type of their initializer (if the underlying type of the
13730 enum is not fixed):
13732 [ C++0x dcl.enum ]
13734 If the underlying type is fixed, the type of each enumerator
13735 prior to the closing brace is the underlying type; if the
13736 initializing value of an enumerator cannot be represented by
13737 the underlying type, the program is ill-formed. If the
13738 underlying type is not fixed, the type of each enumerator is
13739 the type of its initializing value.
13741 If the underlying type is not fixed, it will be computed by
13742 finish_enum and we will reset the type of this enumerator. Of
13743 course, if we're processing a template, there may be no value. */
13744 type = value ? TREE_TYPE (value) : NULL_TREE;
13746 decl = build_decl (loc, CONST_DECL, name, type);
13748 DECL_CONTEXT (decl) = enumtype;
13749 TREE_CONSTANT (decl) = 1;
13750 TREE_READONLY (decl) = 1;
13751 DECL_INITIAL (decl) = value;
13753 if (attributes)
13754 cplus_decl_attributes (&decl, attributes, 0);
13756 if (context && context == current_class_type && !SCOPED_ENUM_P (enumtype))
13758 /* In something like `struct S { enum E { i = 7 }; };' we put `i'
13759 on the TYPE_FIELDS list for `S'. (That's so that you can say
13760 things like `S::i' later.) */
13762 /* The enumerator may be getting declared outside of its enclosing
13763 class, like so:
13765 class S { public: enum E : int; }; enum S::E : int { i = 7; };
13767 For which case we need to make sure that the access of `S::i'
13768 matches the access of `S::E'. */
13769 tree saved_cas = current_access_specifier;
13770 if (TREE_PRIVATE (TYPE_NAME (enumtype)))
13771 current_access_specifier = access_private_node;
13772 else if (TREE_PROTECTED (TYPE_NAME (enumtype)))
13773 current_access_specifier = access_protected_node;
13774 else
13775 current_access_specifier = access_public_node;
13777 finish_member_declaration (decl);
13779 current_access_specifier = saved_cas;
13781 else
13782 pushdecl (decl);
13784 /* Add this enumeration constant to the list for this type. */
13785 TYPE_VALUES (enumtype) = tree_cons (name, decl, TYPE_VALUES (enumtype));
13788 /* Look for an enumerator with the given NAME within the enumeration
13789 type ENUMTYPE. This routine is used primarily for qualified name
13790 lookup into an enumerator in C++0x, e.g.,
13792 enum class Color { Red, Green, Blue };
13794 Color color = Color::Red;
13796 Returns the value corresponding to the enumerator, or
13797 NULL_TREE if no such enumerator was found. */
13798 tree
13799 lookup_enumerator (tree enumtype, tree name)
13801 tree e;
13802 gcc_assert (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE);
13804 e = purpose_member (name, TYPE_VALUES (enumtype));
13805 return e? TREE_VALUE (e) : NULL_TREE;
13809 /* We're defining DECL. Make sure that its type is OK. */
13811 static void
13812 check_function_type (tree decl, tree current_function_parms)
13814 tree fntype = TREE_TYPE (decl);
13815 tree return_type = complete_type (TREE_TYPE (fntype));
13817 /* In a function definition, arg types must be complete. */
13818 require_complete_types_for_parms (current_function_parms);
13820 if (dependent_type_p (return_type)
13821 || type_uses_auto (return_type))
13822 return;
13823 if (!COMPLETE_OR_VOID_TYPE_P (return_type))
13825 tree args = TYPE_ARG_TYPES (fntype);
13827 error ("return type %q#T is incomplete", return_type);
13829 /* Make it return void instead. */
13830 if (TREE_CODE (fntype) == METHOD_TYPE)
13831 fntype = build_method_type_directly (TREE_TYPE (TREE_VALUE (args)),
13832 void_type_node,
13833 TREE_CHAIN (args));
13834 else
13835 fntype = build_function_type (void_type_node, args);
13836 fntype
13837 = build_exception_variant (fntype,
13838 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl)));
13839 fntype = (cp_build_type_attribute_variant
13840 (fntype, TYPE_ATTRIBUTES (TREE_TYPE (decl))));
13841 TREE_TYPE (decl) = fntype;
13843 else
13844 abstract_virtuals_error (decl, TREE_TYPE (fntype));
13847 /* True iff FN is an implicitly-defined default constructor. */
13849 static bool
13850 implicit_default_ctor_p (tree fn)
13852 return (DECL_CONSTRUCTOR_P (fn)
13853 && !user_provided_p (fn)
13854 && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn)));
13857 /* Clobber the contents of *this to let the back end know that the object
13858 storage is dead when we enter the constructor or leave the destructor. */
13860 static tree
13861 build_clobber_this ()
13863 /* Clobbering an empty base is pointless, and harmful if its one byte
13864 TYPE_SIZE overlays real data. */
13865 if (is_empty_class (current_class_type))
13866 return void_node;
13868 /* If we have virtual bases, clobber the whole object, but only if we're in
13869 charge. If we don't have virtual bases, clobber the as-base type so we
13870 don't mess with tail padding. */
13871 bool vbases = CLASSTYPE_VBASECLASSES (current_class_type);
13873 tree ctype = current_class_type;
13874 if (!vbases)
13875 ctype = CLASSTYPE_AS_BASE (ctype);
13877 tree clobber = build_constructor (ctype, NULL);
13878 TREE_THIS_VOLATILE (clobber) = true;
13880 tree thisref = current_class_ref;
13881 if (ctype != current_class_type)
13883 thisref = build_nop (build_reference_type (ctype), current_class_ptr);
13884 thisref = convert_from_reference (thisref);
13887 tree exprstmt = build2 (MODIFY_EXPR, void_type_node, thisref, clobber);
13888 if (vbases)
13889 exprstmt = build_if_in_charge (exprstmt);
13891 return exprstmt;
13894 /* Create the FUNCTION_DECL for a function definition.
13895 DECLSPECS and DECLARATOR are the parts of the declaration;
13896 they describe the function's name and the type it returns,
13897 but twisted together in a fashion that parallels the syntax of C.
13899 FLAGS is a bitwise or of SF_PRE_PARSED (indicating that the
13900 DECLARATOR is really the DECL for the function we are about to
13901 process and that DECLSPECS should be ignored), SF_INCLASS_INLINE
13902 indicating that the function is an inline defined in-class.
13904 This function creates a binding context for the function body
13905 as well as setting up the FUNCTION_DECL in current_function_decl.
13907 For C++, we must first check whether that datum makes any sense.
13908 For example, "class A local_a(1,2);" means that variable local_a
13909 is an aggregate of type A, which should have a constructor
13910 applied to it with the argument list [1, 2].
13912 On entry, DECL_INITIAL (decl1) should be NULL_TREE or error_mark_node,
13913 or may be a BLOCK if the function has been defined previously
13914 in this translation unit. On exit, DECL_INITIAL (decl1) will be
13915 error_mark_node if the function has never been defined, or
13916 a BLOCK if the function has been defined somewhere. */
13918 bool
13919 start_preparsed_function (tree decl1, tree attrs, int flags)
13921 tree ctype = NULL_TREE;
13922 tree fntype;
13923 tree restype;
13924 int doing_friend = 0;
13925 cp_binding_level *bl;
13926 tree current_function_parms;
13927 struct c_fileinfo *finfo
13928 = get_fileinfo (LOCATION_FILE (DECL_SOURCE_LOCATION (decl1)));
13929 bool honor_interface;
13931 /* Sanity check. */
13932 gcc_assert (VOID_TYPE_P (TREE_VALUE (void_list_node)));
13933 gcc_assert (TREE_CHAIN (void_list_node) == NULL_TREE);
13935 fntype = TREE_TYPE (decl1);
13936 if (TREE_CODE (fntype) == METHOD_TYPE)
13937 ctype = TYPE_METHOD_BASETYPE (fntype);
13939 /* ISO C++ 11.4/5. A friend function defined in a class is in
13940 the (lexical) scope of the class in which it is defined. */
13941 if (!ctype && DECL_FRIEND_P (decl1))
13943 ctype = DECL_FRIEND_CONTEXT (decl1);
13945 /* CTYPE could be null here if we're dealing with a template;
13946 for example, `inline friend float foo()' inside a template
13947 will have no CTYPE set. */
13948 if (ctype && TREE_CODE (ctype) != RECORD_TYPE)
13949 ctype = NULL_TREE;
13950 else
13951 doing_friend = 1;
13954 if (DECL_DECLARED_INLINE_P (decl1)
13955 && lookup_attribute ("noinline", attrs))
13956 warning_at (DECL_SOURCE_LOCATION (decl1), 0,
13957 "inline function %qD given attribute noinline", decl1);
13959 /* Handle gnu_inline attribute. */
13960 if (GNU_INLINE_P (decl1))
13962 DECL_EXTERNAL (decl1) = 1;
13963 DECL_NOT_REALLY_EXTERN (decl1) = 0;
13964 DECL_INTERFACE_KNOWN (decl1) = 1;
13965 DECL_DISREGARD_INLINE_LIMITS (decl1) = 1;
13968 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl1))
13969 /* This is a constructor, we must ensure that any default args
13970 introduced by this definition are propagated to the clones
13971 now. The clones are used directly in overload resolution. */
13972 adjust_clone_args (decl1);
13974 /* Sometimes we don't notice that a function is a static member, and
13975 build a METHOD_TYPE for it. Fix that up now. */
13976 gcc_assert (!(ctype != NULL_TREE && DECL_STATIC_FUNCTION_P (decl1)
13977 && TREE_CODE (TREE_TYPE (decl1)) == METHOD_TYPE));
13979 /* Set up current_class_type, and enter the scope of the class, if
13980 appropriate. */
13981 if (ctype)
13982 push_nested_class (ctype);
13983 else if (DECL_STATIC_FUNCTION_P (decl1))
13984 push_nested_class (DECL_CONTEXT (decl1));
13986 /* Now that we have entered the scope of the class, we must restore
13987 the bindings for any template parameters surrounding DECL1, if it
13988 is an inline member template. (Order is important; consider the
13989 case where a template parameter has the same name as a field of
13990 the class.) It is not until after this point that
13991 PROCESSING_TEMPLATE_DECL is guaranteed to be set up correctly. */
13992 if (flags & SF_INCLASS_INLINE)
13993 maybe_begin_member_template_processing (decl1);
13995 /* Effective C++ rule 15. */
13996 if (warn_ecpp
13997 && DECL_OVERLOADED_OPERATOR_P (decl1) == NOP_EXPR
13998 && VOID_TYPE_P (TREE_TYPE (fntype)))
13999 warning (OPT_Weffc__, "%<operator=%> should return a reference to %<*this%>");
14001 /* Make the init_value nonzero so pushdecl knows this is not tentative.
14002 error_mark_node is replaced below (in poplevel) with the BLOCK. */
14003 if (!DECL_INITIAL (decl1))
14004 DECL_INITIAL (decl1) = error_mark_node;
14006 /* This function exists in static storage.
14007 (This does not mean `static' in the C sense!) */
14008 TREE_STATIC (decl1) = 1;
14010 /* We must call push_template_decl after current_class_type is set
14011 up. (If we are processing inline definitions after exiting a
14012 class scope, current_class_type will be NULL_TREE until set above
14013 by push_nested_class.) */
14014 if (processing_template_decl)
14016 tree newdecl1 = push_template_decl (decl1);
14017 if (newdecl1 == error_mark_node)
14019 if (ctype || DECL_STATIC_FUNCTION_P (decl1))
14020 pop_nested_class ();
14021 return false;
14023 decl1 = newdecl1;
14026 /* We are now in the scope of the function being defined. */
14027 current_function_decl = decl1;
14029 /* Save the parm names or decls from this function's declarator
14030 where store_parm_decls will find them. */
14031 current_function_parms = DECL_ARGUMENTS (decl1);
14033 /* Make sure the parameter and return types are reasonable. When
14034 you declare a function, these types can be incomplete, but they
14035 must be complete when you define the function. */
14036 check_function_type (decl1, current_function_parms);
14038 /* Build the return declaration for the function. */
14039 restype = TREE_TYPE (fntype);
14041 if (DECL_RESULT (decl1) == NULL_TREE)
14043 tree resdecl;
14045 resdecl = build_decl (input_location, RESULT_DECL, 0, restype);
14046 DECL_ARTIFICIAL (resdecl) = 1;
14047 DECL_IGNORED_P (resdecl) = 1;
14048 DECL_RESULT (decl1) = resdecl;
14050 cp_apply_type_quals_to_decl (cp_type_quals (restype), resdecl);
14053 /* Let the user know we're compiling this function. */
14054 announce_function (decl1);
14056 /* Record the decl so that the function name is defined.
14057 If we already have a decl for this name, and it is a FUNCTION_DECL,
14058 use the old decl. */
14059 if (!processing_template_decl && !(flags & SF_PRE_PARSED))
14061 /* A specialization is not used to guide overload resolution. */
14062 if (!DECL_FUNCTION_MEMBER_P (decl1)
14063 && !(DECL_USE_TEMPLATE (decl1) &&
14064 PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl1))))
14066 tree olddecl = pushdecl (decl1);
14068 if (olddecl == error_mark_node)
14069 /* If something went wrong when registering the declaration,
14070 use DECL1; we have to have a FUNCTION_DECL to use when
14071 parsing the body of the function. */
14073 else
14075 /* Otherwise, OLDDECL is either a previous declaration
14076 of the same function or DECL1 itself. */
14078 if (warn_missing_declarations
14079 && olddecl == decl1
14080 && !DECL_MAIN_P (decl1)
14081 && TREE_PUBLIC (decl1)
14082 && !DECL_DECLARED_INLINE_P (decl1))
14084 tree context;
14086 /* Check whether DECL1 is in an anonymous
14087 namespace. */
14088 for (context = DECL_CONTEXT (decl1);
14089 context;
14090 context = DECL_CONTEXT (context))
14092 if (TREE_CODE (context) == NAMESPACE_DECL
14093 && DECL_NAME (context) == NULL_TREE)
14094 break;
14097 if (context == NULL)
14098 warning_at (DECL_SOURCE_LOCATION (decl1),
14099 OPT_Wmissing_declarations,
14100 "no previous declaration for %qD", decl1);
14103 decl1 = olddecl;
14106 else
14108 /* We need to set the DECL_CONTEXT. */
14109 if (!DECL_CONTEXT (decl1) && DECL_TEMPLATE_INFO (decl1))
14110 DECL_CONTEXT (decl1) = DECL_CONTEXT (DECL_TI_TEMPLATE (decl1));
14112 fntype = TREE_TYPE (decl1);
14113 restype = TREE_TYPE (fntype);
14115 /* If #pragma weak applies, mark the decl appropriately now.
14116 The pragma only applies to global functions. Because
14117 determining whether or not the #pragma applies involves
14118 computing the mangled name for the declaration, we cannot
14119 apply the pragma until after we have merged this declaration
14120 with any previous declarations; if the original declaration
14121 has a linkage specification, that specification applies to
14122 the definition as well, and may affect the mangled name. */
14123 if (DECL_FILE_SCOPE_P (decl1))
14124 maybe_apply_pragma_weak (decl1);
14127 /* Reset this in case the call to pushdecl changed it. */
14128 current_function_decl = decl1;
14130 gcc_assert (DECL_INITIAL (decl1));
14132 /* This function may already have been parsed, in which case just
14133 return; our caller will skip over the body without parsing. */
14134 if (DECL_INITIAL (decl1) != error_mark_node)
14135 return true;
14137 /* Initialize RTL machinery. We cannot do this until
14138 CURRENT_FUNCTION_DECL and DECL_RESULT are set up. We do this
14139 even when processing a template; this is how we get
14140 CFUN set up, and our per-function variables initialized.
14141 FIXME factor out the non-RTL stuff. */
14142 bl = current_binding_level;
14143 allocate_struct_function (decl1, processing_template_decl);
14145 /* Initialize the language data structures. Whenever we start
14146 a new function, we destroy temporaries in the usual way. */
14147 cfun->language = ggc_cleared_alloc<language_function> ();
14148 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
14149 current_binding_level = bl;
14151 if (!processing_template_decl && type_uses_auto (restype))
14153 FNDECL_USED_AUTO (decl1) = true;
14154 current_function_auto_return_pattern = restype;
14157 /* Start the statement-tree, start the tree now. */
14158 DECL_SAVED_TREE (decl1) = push_stmt_list ();
14160 /* If we are (erroneously) defining a function that we have already
14161 defined before, wipe out what we knew before. */
14162 if (!DECL_PENDING_INLINE_P (decl1))
14163 DECL_SAVED_FUNCTION_DATA (decl1) = NULL;
14165 if (ctype && !doing_friend && !DECL_STATIC_FUNCTION_P (decl1))
14167 /* We know that this was set up by `grokclassfn'. We do not
14168 wait until `store_parm_decls', since evil parse errors may
14169 never get us to that point. Here we keep the consistency
14170 between `current_class_type' and `current_class_ptr'. */
14171 tree t = DECL_ARGUMENTS (decl1);
14173 gcc_assert (t != NULL_TREE && TREE_CODE (t) == PARM_DECL);
14174 gcc_assert (TYPE_PTR_P (TREE_TYPE (t)));
14176 cp_function_chain->x_current_class_ref
14177 = cp_build_indirect_ref (t, RO_NULL, tf_warning_or_error);
14178 /* Set this second to avoid shortcut in cp_build_indirect_ref. */
14179 cp_function_chain->x_current_class_ptr = t;
14181 /* Constructors and destructors need to know whether they're "in
14182 charge" of initializing virtual base classes. */
14183 t = DECL_CHAIN (t);
14184 if (DECL_HAS_IN_CHARGE_PARM_P (decl1))
14186 current_in_charge_parm = t;
14187 t = DECL_CHAIN (t);
14189 if (DECL_HAS_VTT_PARM_P (decl1))
14191 gcc_assert (DECL_NAME (t) == vtt_parm_identifier);
14192 current_vtt_parm = t;
14196 honor_interface = (!DECL_TEMPLATE_INSTANTIATION (decl1)
14197 /* Implicitly-defined methods (like the
14198 destructor for a class in which no destructor
14199 is explicitly declared) must not be defined
14200 until their definition is needed. So, we
14201 ignore interface specifications for
14202 compiler-generated functions. */
14203 && !DECL_ARTIFICIAL (decl1));
14205 if (processing_template_decl)
14206 /* Don't mess with interface flags. */;
14207 else if (DECL_INTERFACE_KNOWN (decl1))
14209 tree ctx = decl_function_context (decl1);
14211 if (DECL_NOT_REALLY_EXTERN (decl1))
14212 DECL_EXTERNAL (decl1) = 0;
14214 if (ctx != NULL_TREE && vague_linkage_p (ctx))
14215 /* This is a function in a local class in an extern inline
14216 or template function. */
14217 comdat_linkage (decl1);
14219 /* If this function belongs to an interface, it is public.
14220 If it belongs to someone else's interface, it is also external.
14221 This only affects inlines and template instantiations. */
14222 else if (!finfo->interface_unknown && honor_interface)
14224 if (DECL_DECLARED_INLINE_P (decl1)
14225 || DECL_TEMPLATE_INSTANTIATION (decl1))
14227 DECL_EXTERNAL (decl1)
14228 = (finfo->interface_only
14229 || (DECL_DECLARED_INLINE_P (decl1)
14230 && ! flag_implement_inlines
14231 && !DECL_VINDEX (decl1)));
14233 /* For WIN32 we also want to put these in linkonce sections. */
14234 maybe_make_one_only (decl1);
14236 else
14237 DECL_EXTERNAL (decl1) = 0;
14238 DECL_INTERFACE_KNOWN (decl1) = 1;
14239 /* If this function is in an interface implemented in this file,
14240 make sure that the back end knows to emit this function
14241 here. */
14242 if (!DECL_EXTERNAL (decl1))
14243 mark_needed (decl1);
14245 else if (finfo->interface_unknown && finfo->interface_only
14246 && honor_interface)
14248 /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma
14249 interface, we will have both finfo->interface_unknown and
14250 finfo->interface_only set. In that case, we don't want to
14251 use the normal heuristics because someone will supply a
14252 #pragma implementation elsewhere, and deducing it here would
14253 produce a conflict. */
14254 comdat_linkage (decl1);
14255 DECL_EXTERNAL (decl1) = 0;
14256 DECL_INTERFACE_KNOWN (decl1) = 1;
14257 DECL_DEFER_OUTPUT (decl1) = 1;
14259 else
14261 /* This is a definition, not a reference.
14262 So clear DECL_EXTERNAL, unless this is a GNU extern inline. */
14263 if (!GNU_INLINE_P (decl1))
14264 DECL_EXTERNAL (decl1) = 0;
14266 if ((DECL_DECLARED_INLINE_P (decl1)
14267 || DECL_TEMPLATE_INSTANTIATION (decl1))
14268 && ! DECL_INTERFACE_KNOWN (decl1))
14269 DECL_DEFER_OUTPUT (decl1) = 1;
14270 else
14271 DECL_INTERFACE_KNOWN (decl1) = 1;
14274 /* Determine the ELF visibility attribute for the function. We must not
14275 do this before calling "pushdecl", as we must allow "duplicate_decls"
14276 to merge any attributes appropriately. We also need to wait until
14277 linkage is set. */
14278 if (!DECL_CLONED_FUNCTION_P (decl1))
14279 determine_visibility (decl1);
14281 if (!processing_template_decl)
14282 maybe_instantiate_noexcept (decl1);
14284 begin_scope (sk_function_parms, decl1);
14286 ++function_depth;
14288 if (DECL_DESTRUCTOR_P (decl1)
14289 || (DECL_CONSTRUCTOR_P (decl1)
14290 && targetm.cxx.cdtor_returns_this ()))
14291 cdtor_label = create_artificial_label (input_location);
14293 start_fname_decls ();
14295 store_parm_decls (current_function_parms);
14297 if (!processing_template_decl
14298 && (flag_lifetime_dse > 1)
14299 && DECL_CONSTRUCTOR_P (decl1)
14300 && !DECL_CLONED_FUNCTION_P (decl1)
14301 /* Clobbering an empty base is harmful if it overlays real data. */
14302 && !is_empty_class (current_class_type)
14303 /* We can't clobber safely for an implicitly-defined default constructor
14304 because part of the initialization might happen before we enter the
14305 constructor, via AGGR_INIT_ZERO_FIRST (c++/68006). */
14306 && !implicit_default_ctor_p (decl1))
14307 finish_expr_stmt (build_clobber_this ());
14309 if (!processing_template_decl
14310 && DECL_CONSTRUCTOR_P (decl1)
14311 && (flag_sanitize & SANITIZE_VPTR)
14312 && !DECL_CLONED_FUNCTION_P (decl1)
14313 && !implicit_default_ctor_p (decl1))
14314 cp_ubsan_maybe_initialize_vtbl_ptrs (current_class_ptr);
14316 return true;
14320 /* Like start_preparsed_function, except that instead of a
14321 FUNCTION_DECL, this function takes DECLSPECS and DECLARATOR.
14323 Returns true on success. If the DECLARATOR is not suitable
14324 for a function, we return false, which tells the parser to
14325 skip the entire function. */
14327 bool
14328 start_function (cp_decl_specifier_seq *declspecs,
14329 const cp_declarator *declarator,
14330 tree attrs)
14332 tree decl1;
14334 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, &attrs);
14335 invoke_plugin_callbacks (PLUGIN_START_PARSE_FUNCTION, decl1);
14336 if (decl1 == error_mark_node)
14337 return false;
14338 /* If the declarator is not suitable for a function definition,
14339 cause a syntax error. */
14340 if (decl1 == NULL_TREE || TREE_CODE (decl1) != FUNCTION_DECL)
14342 error ("invalid function declaration");
14343 return false;
14346 if (DECL_MAIN_P (decl1))
14347 /* main must return int. grokfndecl should have corrected it
14348 (and issued a diagnostic) if the user got it wrong. */
14349 gcc_assert (same_type_p (TREE_TYPE (TREE_TYPE (decl1)),
14350 integer_type_node));
14352 return start_preparsed_function (decl1, attrs, /*flags=*/SF_DEFAULT);
14355 /* Returns true iff an EH_SPEC_BLOCK should be created in the body of
14356 FN. */
14358 static bool
14359 use_eh_spec_block (tree fn)
14361 return (flag_exceptions && flag_enforce_eh_specs
14362 && !processing_template_decl
14363 && !type_throw_all_p (TREE_TYPE (fn))
14364 /* We insert the EH_SPEC_BLOCK only in the original
14365 function; then, it is copied automatically to the
14366 clones. */
14367 && !DECL_CLONED_FUNCTION_P (fn)
14368 /* Implicitly-generated constructors and destructors have
14369 exception specifications. However, those specifications
14370 are the union of the possible exceptions specified by the
14371 constructors/destructors for bases and members, so no
14372 unallowed exception will ever reach this function. By
14373 not creating the EH_SPEC_BLOCK we save a little memory,
14374 and we avoid spurious warnings about unreachable
14375 code. */
14376 && !DECL_DEFAULTED_FN (fn));
14379 /* Store the parameter declarations into the current function declaration.
14380 This is called after parsing the parameter declarations, before
14381 digesting the body of the function.
14383 Also install to binding contour return value identifier, if any. */
14385 static void
14386 store_parm_decls (tree current_function_parms)
14388 tree fndecl = current_function_decl;
14389 tree parm;
14391 /* This is a chain of any other decls that came in among the parm
14392 declarations. If a parm is declared with enum {foo, bar} x;
14393 then CONST_DECLs for foo and bar are put here. */
14394 tree nonparms = NULL_TREE;
14396 if (current_function_parms)
14398 /* This case is when the function was defined with an ANSI prototype.
14399 The parms already have decls, so we need not do anything here
14400 except record them as in effect
14401 and complain if any redundant old-style parm decls were written. */
14403 tree specparms = current_function_parms;
14404 tree next;
14406 /* Must clear this because it might contain TYPE_DECLs declared
14407 at class level. */
14408 current_binding_level->names = NULL;
14410 /* If we're doing semantic analysis, then we'll call pushdecl
14411 for each of these. We must do them in reverse order so that
14412 they end in the correct forward order. */
14413 specparms = nreverse (specparms);
14415 for (parm = specparms; parm; parm = next)
14417 next = DECL_CHAIN (parm);
14418 if (TREE_CODE (parm) == PARM_DECL)
14419 pushdecl (parm);
14420 else
14422 /* If we find an enum constant or a type tag,
14423 put it aside for the moment. */
14424 TREE_CHAIN (parm) = NULL_TREE;
14425 nonparms = chainon (nonparms, parm);
14429 /* Get the decls in their original chain order and record in the
14430 function. This is all and only the PARM_DECLs that were
14431 pushed into scope by the loop above. */
14432 DECL_ARGUMENTS (fndecl) = getdecls ();
14434 else
14435 DECL_ARGUMENTS (fndecl) = NULL_TREE;
14437 /* Now store the final chain of decls for the arguments
14438 as the decl-chain of the current lexical scope.
14439 Put the enumerators in as well, at the front so that
14440 DECL_ARGUMENTS is not modified. */
14441 current_binding_level->names = chainon (nonparms, DECL_ARGUMENTS (fndecl));
14443 if (use_eh_spec_block (current_function_decl))
14444 current_eh_spec_block = begin_eh_spec_block ();
14448 /* We have finished doing semantic analysis on DECL, but have not yet
14449 generated RTL for its body. Save away our current state, so that
14450 when we want to generate RTL later we know what to do. */
14452 static void
14453 save_function_data (tree decl)
14455 struct language_function *f;
14457 /* Save the language-specific per-function data so that we can
14458 get it back when we really expand this function. */
14459 gcc_assert (!DECL_PENDING_INLINE_P (decl));
14461 /* Make a copy. */
14462 f = ggc_alloc<language_function> ();
14463 memcpy (f, cp_function_chain, sizeof (struct language_function));
14464 DECL_SAVED_FUNCTION_DATA (decl) = f;
14466 /* Clear out the bits we don't need. */
14467 f->base.x_stmt_tree.x_cur_stmt_list = NULL;
14468 f->bindings = NULL;
14469 f->x_local_names = NULL;
14470 f->base.local_typedefs = NULL;
14474 /* Set the return value of the constructor (if present). */
14476 static void
14477 finish_constructor_body (void)
14479 tree val;
14480 tree exprstmt;
14482 if (targetm.cxx.cdtor_returns_this ())
14484 /* Any return from a constructor will end up here. */
14485 add_stmt (build_stmt (input_location, LABEL_EXPR, cdtor_label));
14487 val = DECL_ARGUMENTS (current_function_decl);
14488 val = build2 (MODIFY_EXPR, TREE_TYPE (val),
14489 DECL_RESULT (current_function_decl), val);
14490 /* Return the address of the object. */
14491 exprstmt = build_stmt (input_location, RETURN_EXPR, val);
14492 add_stmt (exprstmt);
14496 /* Do all the processing for the beginning of a destructor; set up the
14497 vtable pointers and cleanups for bases and members. */
14499 static void
14500 begin_destructor_body (void)
14502 tree compound_stmt;
14504 /* If the CURRENT_CLASS_TYPE is incomplete, we will have already
14505 issued an error message. We still want to try to process the
14506 body of the function, but initialize_vtbl_ptrs will crash if
14507 TYPE_BINFO is NULL. */
14508 if (COMPLETE_TYPE_P (current_class_type))
14510 compound_stmt = begin_compound_stmt (0);
14511 /* Make all virtual function table pointers in non-virtual base
14512 classes point to CURRENT_CLASS_TYPE's virtual function
14513 tables. */
14514 initialize_vtbl_ptrs (current_class_ptr);
14515 finish_compound_stmt (compound_stmt);
14517 if (flag_lifetime_dse
14518 /* Clobbering an empty base is harmful if it overlays real data. */
14519 && !is_empty_class (current_class_type))
14520 finish_decl_cleanup (NULL_TREE, build_clobber_this ());
14522 /* And insert cleanups for our bases and members so that they
14523 will be properly destroyed if we throw. */
14524 push_base_cleanups ();
14528 /* At the end of every destructor we generate code to delete the object if
14529 necessary. Do that now. */
14531 static void
14532 finish_destructor_body (void)
14534 tree exprstmt;
14536 /* Any return from a destructor will end up here; that way all base
14537 and member cleanups will be run when the function returns. */
14538 add_stmt (build_stmt (input_location, LABEL_EXPR, cdtor_label));
14540 if (targetm.cxx.cdtor_returns_this ())
14542 tree val;
14544 val = DECL_ARGUMENTS (current_function_decl);
14545 val = build2 (MODIFY_EXPR, TREE_TYPE (val),
14546 DECL_RESULT (current_function_decl), val);
14547 /* Return the address of the object. */
14548 exprstmt = build_stmt (input_location, RETURN_EXPR, val);
14549 add_stmt (exprstmt);
14553 /* Do the necessary processing for the beginning of a function body, which
14554 in this case includes member-initializers, but not the catch clauses of
14555 a function-try-block. Currently, this means opening a binding level
14556 for the member-initializers (in a ctor), member cleanups (in a dtor),
14557 and capture proxies (in a lambda operator()). */
14559 tree
14560 begin_function_body (void)
14562 tree stmt;
14564 if (! FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
14565 return NULL_TREE;
14567 if (processing_template_decl)
14568 /* Do nothing now. */;
14569 else
14570 /* Always keep the BLOCK node associated with the outermost pair of
14571 curly braces of a function. These are needed for correct
14572 operation of dwarfout.c. */
14573 keep_next_level (true);
14575 stmt = begin_compound_stmt (BCS_FN_BODY);
14577 if (processing_template_decl)
14578 /* Do nothing now. */;
14579 else if (DECL_DESTRUCTOR_P (current_function_decl))
14580 begin_destructor_body ();
14582 return stmt;
14585 /* Do the processing for the end of a function body. Currently, this means
14586 closing out the cleanups for fully-constructed bases and members, and in
14587 the case of the destructor, deleting the object if desired. Again, this
14588 is only meaningful for [cd]tors, since they are the only functions where
14589 there is a significant distinction between the main body and any
14590 function catch clauses. Handling, say, main() return semantics here
14591 would be wrong, as flowing off the end of a function catch clause for
14592 main() would also need to return 0. */
14594 void
14595 finish_function_body (tree compstmt)
14597 if (compstmt == NULL_TREE)
14598 return;
14600 /* Close the block. */
14601 finish_compound_stmt (compstmt);
14603 if (processing_template_decl)
14604 /* Do nothing now. */;
14605 else if (DECL_CONSTRUCTOR_P (current_function_decl))
14606 finish_constructor_body ();
14607 else if (DECL_DESTRUCTOR_P (current_function_decl))
14608 finish_destructor_body ();
14611 /* Given a function, returns the BLOCK corresponding to the outermost level
14612 of curly braces, skipping the artificial block created for constructor
14613 initializers. */
14615 tree
14616 outer_curly_brace_block (tree fndecl)
14618 tree block = DECL_INITIAL (fndecl);
14619 if (BLOCK_OUTER_CURLY_BRACE_P (block))
14620 return block;
14621 block = BLOCK_SUBBLOCKS (block);
14622 if (BLOCK_OUTER_CURLY_BRACE_P (block))
14623 return block;
14624 block = BLOCK_SUBBLOCKS (block);
14625 gcc_assert (BLOCK_OUTER_CURLY_BRACE_P (block));
14626 return block;
14629 /* If FNDECL is a class's key method, add the class to the list of
14630 keyed classes that should be emitted. */
14632 static void
14633 record_key_method_defined (tree fndecl)
14635 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fndecl)
14636 && DECL_VIRTUAL_P (fndecl)
14637 && !processing_template_decl)
14639 tree fnclass = DECL_CONTEXT (fndecl);
14640 if (fndecl == CLASSTYPE_KEY_METHOD (fnclass))
14641 keyed_classes = tree_cons (NULL_TREE, fnclass, keyed_classes);
14645 /* Subroutine of finish_function.
14646 Save the body of constexpr functions for possible
14647 future compile time evaluation. */
14649 static void
14650 maybe_save_function_definition (tree fun)
14652 if (!processing_template_decl
14653 && DECL_DECLARED_CONSTEXPR_P (fun)
14654 && !cp_function_chain->invalid_constexpr
14655 && !DECL_CLONED_FUNCTION_P (fun))
14656 register_constexpr_fundef (fun, DECL_SAVED_TREE (fun));
14659 /* Finish up a function declaration and compile that function
14660 all the way to assembler language output. The free the storage
14661 for the function definition.
14663 FLAGS is a bitwise or of the following values:
14664 2 - INCLASS_INLINE
14665 We just finished processing the body of an in-class inline
14666 function definition. (This processing will have taken place
14667 after the class definition is complete.) */
14669 tree
14670 finish_function (int flags)
14672 tree fndecl = current_function_decl;
14673 tree fntype, ctype = NULL_TREE;
14674 int inclass_inline = (flags & 2) != 0;
14676 /* When we get some parse errors, we can end up without a
14677 current_function_decl, so cope. */
14678 if (fndecl == NULL_TREE)
14679 return error_mark_node;
14681 if (c_dialect_objc ())
14682 objc_finish_function ();
14684 record_key_method_defined (fndecl);
14686 fntype = TREE_TYPE (fndecl);
14688 /* TREE_READONLY (fndecl) = 1;
14689 This caused &foo to be of type ptr-to-const-function
14690 which then got a warning when stored in a ptr-to-function variable. */
14692 gcc_assert (building_stmt_list_p ());
14693 /* The current function is being defined, so its DECL_INITIAL should
14694 be set, and unless there's a multiple definition, it should be
14695 error_mark_node. */
14696 gcc_assert (DECL_INITIAL (fndecl) == error_mark_node);
14698 /* For a cloned function, we've already got all the code we need;
14699 there's no need to add any extra bits. */
14700 if (!DECL_CLONED_FUNCTION_P (fndecl))
14702 /* Make it so that `main' always returns 0 by default. */
14703 if (DECL_MAIN_P (current_function_decl))
14704 finish_return_stmt (integer_zero_node);
14706 if (use_eh_spec_block (current_function_decl))
14707 finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
14708 (TREE_TYPE (current_function_decl)),
14709 current_eh_spec_block);
14712 /* If we're saving up tree structure, tie off the function now. */
14713 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
14715 if (fn_contains_cilk_spawn_p (cfun) && !processing_template_decl)
14716 cfun->cilk_frame_decl = insert_cilk_frame (fndecl);
14718 finish_fname_decls ();
14720 /* If this function can't throw any exceptions, remember that. */
14721 if (!processing_template_decl
14722 && !cp_function_chain->can_throw
14723 && !flag_non_call_exceptions
14724 && !decl_replaceable_p (fndecl))
14725 TREE_NOTHROW (fndecl) = 1;
14727 /* This must come after expand_function_end because cleanups might
14728 have declarations (from inline functions) that need to go into
14729 this function's blocks. */
14731 /* If the current binding level isn't the outermost binding level
14732 for this function, either there is a bug, or we have experienced
14733 syntax errors and the statement tree is malformed. */
14734 if (current_binding_level->kind != sk_function_parms)
14736 /* Make sure we have already experienced errors. */
14737 gcc_assert (errorcount);
14739 /* Throw away the broken statement tree and extra binding
14740 levels. */
14741 DECL_SAVED_TREE (fndecl) = alloc_stmt_list ();
14743 while (current_binding_level->kind != sk_function_parms)
14745 if (current_binding_level->kind == sk_class)
14746 pop_nested_class ();
14747 else
14748 poplevel (0, 0, 0);
14751 poplevel (1, 0, 1);
14753 /* Statements should always be full-expressions at the outermost set
14754 of curly braces for a function. */
14755 gcc_assert (stmts_are_full_exprs_p ());
14757 /* If there are no return statements in a function with auto return type,
14758 the return type is void. But if the declared type is something like
14759 auto*, this is an error. */
14760 if (!processing_template_decl && FNDECL_USED_AUTO (fndecl)
14761 && TREE_TYPE (fntype) == current_function_auto_return_pattern)
14763 if (!is_auto (current_function_auto_return_pattern)
14764 && !current_function_returns_value && !current_function_returns_null)
14766 error ("no return statements in function returning %qT",
14767 current_function_auto_return_pattern);
14768 inform (input_location, "only plain %<auto%> return type can be "
14769 "deduced to %<void%>");
14771 apply_deduced_return_type (fndecl, void_type_node);
14772 fntype = TREE_TYPE (fndecl);
14775 // If this is a concept, check that the definition is reasonable.
14776 if (DECL_DECLARED_CONCEPT_P (fndecl))
14777 check_function_concept (fndecl);
14779 /* Lambda closure members are implicitly constexpr if possible. */
14780 if (cxx_dialect >= cxx1z
14781 && LAMBDA_TYPE_P (CP_DECL_CONTEXT (fndecl))
14782 && (processing_template_decl
14783 || is_valid_constexpr_fn (fndecl, /*complain*/false))
14784 && potential_constant_expression (DECL_SAVED_TREE (fndecl)))
14785 DECL_DECLARED_CONSTEXPR_P (fndecl) = true;
14787 /* Save constexpr function body before it gets munged by
14788 the NRV transformation. */
14789 maybe_save_function_definition (fndecl);
14791 /* Invoke the pre-genericize plugin before we start munging things. */
14792 if (!processing_template_decl)
14793 invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE, fndecl);
14795 /* Perform delayed folding before NRV transformation. */
14796 if (!processing_template_decl)
14797 cp_fold_function (fndecl);
14799 /* Set up the named return value optimization, if we can. Candidate
14800 variables are selected in check_return_expr. */
14801 if (current_function_return_value)
14803 tree r = current_function_return_value;
14804 tree outer;
14806 if (r != error_mark_node
14807 /* This is only worth doing for fns that return in memory--and
14808 simpler, since we don't have to worry about promoted modes. */
14809 && aggregate_value_p (TREE_TYPE (TREE_TYPE (fndecl)), fndecl)
14810 /* Only allow this for variables declared in the outer scope of
14811 the function so we know that their lifetime always ends with a
14812 return; see g++.dg/opt/nrv6.C. We could be more flexible if
14813 we were to do this optimization in tree-ssa. */
14814 && (outer = outer_curly_brace_block (fndecl))
14815 && chain_member (r, BLOCK_VARS (outer)))
14816 finalize_nrv (&DECL_SAVED_TREE (fndecl), r, DECL_RESULT (fndecl));
14818 current_function_return_value = NULL_TREE;
14821 /* Remember that we were in class scope. */
14822 if (current_class_name)
14823 ctype = current_class_type;
14825 /* Must mark the RESULT_DECL as being in this function. */
14826 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
14828 /* Set the BLOCK_SUPERCONTEXT of the outermost function scope to point
14829 to the FUNCTION_DECL node itself. */
14830 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
14832 /* Save away current state, if appropriate. */
14833 if (!processing_template_decl)
14834 save_function_data (fndecl);
14836 /* Complain if there's just no return statement. */
14837 if (warn_return_type
14838 && !VOID_TYPE_P (TREE_TYPE (fntype))
14839 && !dependent_type_p (TREE_TYPE (fntype))
14840 && !current_function_returns_value && !current_function_returns_null
14841 /* Don't complain if we abort or throw. */
14842 && !current_function_returns_abnormally
14843 /* Don't complain if there's an infinite loop. */
14844 && !current_function_infinite_loop
14845 /* Don't complain if we are declared noreturn. */
14846 && !TREE_THIS_VOLATILE (fndecl)
14847 && !DECL_NAME (DECL_RESULT (fndecl))
14848 && !TREE_NO_WARNING (fndecl)
14849 /* Structor return values (if any) are set by the compiler. */
14850 && !DECL_CONSTRUCTOR_P (fndecl)
14851 && !DECL_DESTRUCTOR_P (fndecl)
14852 && targetm.warn_func_return (fndecl))
14854 warning (OPT_Wreturn_type,
14855 "no return statement in function returning non-void");
14856 TREE_NO_WARNING (fndecl) = 1;
14859 /* Store the end of the function, so that we get good line number
14860 info for the epilogue. */
14861 cfun->function_end_locus = input_location;
14863 /* Complain about parameters that are only set, but never otherwise used. */
14864 if (warn_unused_but_set_parameter
14865 && !processing_template_decl
14866 && errorcount == unused_but_set_errorcount
14867 && !DECL_CLONED_FUNCTION_P (fndecl))
14869 tree decl;
14871 for (decl = DECL_ARGUMENTS (fndecl);
14872 decl;
14873 decl = DECL_CHAIN (decl))
14874 if (TREE_USED (decl)
14875 && TREE_CODE (decl) == PARM_DECL
14876 && !DECL_READ_P (decl)
14877 && DECL_NAME (decl)
14878 && !DECL_ARTIFICIAL (decl)
14879 && !TREE_NO_WARNING (decl)
14880 && !DECL_IN_SYSTEM_HEADER (decl)
14881 && TREE_TYPE (decl) != error_mark_node
14882 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
14883 && (!CLASS_TYPE_P (TREE_TYPE (decl))
14884 || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl))))
14885 warning_at (DECL_SOURCE_LOCATION (decl),
14886 OPT_Wunused_but_set_parameter,
14887 "parameter %qD set but not used", decl);
14888 unused_but_set_errorcount = errorcount;
14891 /* Complain about locally defined typedefs that are not used in this
14892 function. */
14893 maybe_warn_unused_local_typedefs ();
14895 /* Possibly warn about unused parameters. */
14896 if (warn_unused_parameter
14897 && !processing_template_decl
14898 && !DECL_CLONED_FUNCTION_P (fndecl))
14899 do_warn_unused_parameter (fndecl);
14901 /* Genericize before inlining. */
14902 if (!processing_template_decl)
14904 struct language_function *f = DECL_SAVED_FUNCTION_DATA (fndecl);
14905 cp_genericize (fndecl);
14906 /* Clear out the bits we don't need. */
14907 f->x_current_class_ptr = NULL;
14908 f->x_current_class_ref = NULL;
14909 f->x_eh_spec_block = NULL;
14910 f->x_in_charge_parm = NULL;
14911 f->x_vtt_parm = NULL;
14912 f->x_return_value = NULL;
14913 f->bindings = NULL;
14914 f->extern_decl_map = NULL;
14915 f->infinite_loops = NULL;
14917 /* Clear out the bits we don't need. */
14918 local_names = NULL;
14920 /* We're leaving the context of this function, so zap cfun. It's still in
14921 DECL_STRUCT_FUNCTION, and we'll restore it in tree_rest_of_compilation. */
14922 set_cfun (NULL);
14923 current_function_decl = NULL;
14925 /* If this is an in-class inline definition, we may have to pop the
14926 bindings for the template parameters that we added in
14927 maybe_begin_member_template_processing when start_function was
14928 called. */
14929 if (inclass_inline)
14930 maybe_end_member_template_processing ();
14932 /* Leave the scope of the class. */
14933 if (ctype)
14934 pop_nested_class ();
14936 --function_depth;
14938 /* Clean up. */
14939 current_function_decl = NULL_TREE;
14941 invoke_plugin_callbacks (PLUGIN_FINISH_PARSE_FUNCTION, fndecl);
14942 return fndecl;
14945 /* Create the FUNCTION_DECL for a function definition.
14946 DECLSPECS and DECLARATOR are the parts of the declaration;
14947 they describe the return type and the name of the function,
14948 but twisted together in a fashion that parallels the syntax of C.
14950 This function creates a binding context for the function body
14951 as well as setting up the FUNCTION_DECL in current_function_decl.
14953 Returns a FUNCTION_DECL on success.
14955 If the DECLARATOR is not suitable for a function (it defines a datum
14956 instead), we return 0, which tells yyparse to report a parse error.
14958 May return void_type_node indicating that this method is actually
14959 a friend. See grokfield for more details.
14961 Came here with a `.pushlevel' .
14963 DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
14964 CHANGES TO CODE IN `grokfield'. */
14966 tree
14967 grokmethod (cp_decl_specifier_seq *declspecs,
14968 const cp_declarator *declarator, tree attrlist)
14970 tree fndecl = grokdeclarator (declarator, declspecs, MEMFUNCDEF, 0,
14971 &attrlist);
14973 if (fndecl == error_mark_node)
14974 return error_mark_node;
14976 if (fndecl == NULL || TREE_CODE (fndecl) != FUNCTION_DECL)
14978 error ("invalid member function declaration");
14979 return error_mark_node;
14982 if (attrlist)
14983 cplus_decl_attributes (&fndecl, attrlist, 0);
14985 /* Pass friends other than inline friend functions back. */
14986 if (fndecl == void_type_node)
14987 return fndecl;
14989 if (DECL_IN_AGGR_P (fndecl))
14991 if (DECL_CLASS_SCOPE_P (fndecl))
14992 error ("%qD is already defined in class %qT", fndecl,
14993 DECL_CONTEXT (fndecl));
14994 return error_mark_node;
14997 check_template_shadow (fndecl);
14999 if (TREE_PUBLIC (fndecl))
15000 DECL_COMDAT (fndecl) = 1;
15001 DECL_DECLARED_INLINE_P (fndecl) = 1;
15002 DECL_NO_INLINE_WARNING_P (fndecl) = 1;
15004 /* We process method specializations in finish_struct_1. */
15005 if (processing_template_decl && !DECL_TEMPLATE_SPECIALIZATION (fndecl))
15007 fndecl = push_template_decl (fndecl);
15008 if (fndecl == error_mark_node)
15009 return fndecl;
15012 if (! DECL_FRIEND_P (fndecl))
15014 if (DECL_CHAIN (fndecl))
15016 fndecl = copy_node (fndecl);
15017 TREE_CHAIN (fndecl) = NULL_TREE;
15021 cp_finish_decl (fndecl, NULL_TREE, false, NULL_TREE, 0);
15023 DECL_IN_AGGR_P (fndecl) = 1;
15024 return fndecl;
15028 /* VAR is a VAR_DECL. If its type is incomplete, remember VAR so that
15029 we can lay it out later, when and if its type becomes complete.
15031 Also handle constexpr variables where the initializer involves
15032 an unlowered PTRMEM_CST because the class isn't complete yet. */
15034 void
15035 maybe_register_incomplete_var (tree var)
15037 gcc_assert (VAR_P (var));
15039 /* Keep track of variables with incomplete types. */
15040 if (!processing_template_decl && TREE_TYPE (var) != error_mark_node
15041 && DECL_EXTERNAL (var))
15043 tree inner_type = TREE_TYPE (var);
15045 while (TREE_CODE (inner_type) == ARRAY_TYPE)
15046 inner_type = TREE_TYPE (inner_type);
15047 inner_type = TYPE_MAIN_VARIANT (inner_type);
15049 if ((!COMPLETE_TYPE_P (inner_type) && CLASS_TYPE_P (inner_type))
15050 /* RTTI TD entries are created while defining the type_info. */
15051 || (TYPE_LANG_SPECIFIC (inner_type)
15052 && TYPE_BEING_DEFINED (inner_type)))
15054 incomplete_var iv = {var, inner_type};
15055 vec_safe_push (incomplete_vars, iv);
15057 else if (!(DECL_LANG_SPECIFIC (var) && DECL_TEMPLATE_INFO (var))
15058 && decl_constant_var_p (var)
15059 && (TYPE_PTRMEM_P (inner_type) || CLASS_TYPE_P (inner_type)))
15061 /* When the outermost open class is complete we can resolve any
15062 pointers-to-members. */
15063 tree context = outermost_open_class ();
15064 incomplete_var iv = {var, context};
15065 vec_safe_push (incomplete_vars, iv);
15070 /* Called when a class type (given by TYPE) is defined. If there are
15071 any existing VAR_DECLs whose type has been completed by this
15072 declaration, update them now. */
15074 void
15075 complete_vars (tree type)
15077 unsigned ix;
15078 incomplete_var *iv;
15080 for (ix = 0; vec_safe_iterate (incomplete_vars, ix, &iv); )
15082 if (same_type_p (type, iv->incomplete_type))
15084 tree var = iv->decl;
15085 tree type = TREE_TYPE (var);
15087 if (type != error_mark_node
15088 && (TYPE_MAIN_VARIANT (strip_array_types (type))
15089 == iv->incomplete_type))
15091 /* Complete the type of the variable. The VAR_DECL itself
15092 will be laid out in expand_expr. */
15093 complete_type (type);
15094 cp_apply_type_quals_to_decl (cp_type_quals (type), var);
15097 /* Remove this entry from the list. */
15098 incomplete_vars->unordered_remove (ix);
15100 else
15101 ix++;
15104 /* Check for pending declarations which may have abstract type. */
15105 complete_type_check_abstract (type);
15108 /* If DECL is of a type which needs a cleanup, build and return an
15109 expression to perform that cleanup here. Return NULL_TREE if no
15110 cleanup need be done. DECL can also be a _REF when called from
15111 split_nonconstant_init_1. */
15113 tree
15114 cxx_maybe_build_cleanup (tree decl, tsubst_flags_t complain)
15116 tree type;
15117 tree attr;
15118 tree cleanup;
15120 /* Assume no cleanup is required. */
15121 cleanup = NULL_TREE;
15123 if (error_operand_p (decl))
15124 return cleanup;
15126 /* Handle "__attribute__((cleanup))". We run the cleanup function
15127 before the destructor since the destructor is what actually
15128 terminates the lifetime of the object. */
15129 if (DECL_P (decl))
15130 attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
15131 else
15132 attr = NULL_TREE;
15133 if (attr)
15135 tree id;
15136 tree fn;
15137 tree arg;
15139 /* Get the name specified by the user for the cleanup function. */
15140 id = TREE_VALUE (TREE_VALUE (attr));
15141 /* Look up the name to find the cleanup function to call. It is
15142 important to use lookup_name here because that is what is
15143 used in c-common.c:handle_cleanup_attribute when performing
15144 initial checks on the attribute. Note that those checks
15145 include ensuring that the function found is not an overloaded
15146 function, or an object with an overloaded call operator,
15147 etc.; we can rely on the fact that the function found is an
15148 ordinary FUNCTION_DECL. */
15149 fn = lookup_name (id);
15150 arg = build_address (decl);
15151 if (!mark_used (decl, complain) && !(complain & tf_error))
15152 return error_mark_node;
15153 cleanup = cp_build_function_call_nary (fn, complain, arg, NULL_TREE);
15154 if (cleanup == error_mark_node)
15155 return error_mark_node;
15157 /* Handle ordinary C++ destructors. */
15158 type = TREE_TYPE (decl);
15159 if (type_build_dtor_call (type))
15161 int flags = LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR;
15162 tree addr;
15163 tree call;
15165 if (TREE_CODE (type) == ARRAY_TYPE)
15166 addr = decl;
15167 else
15168 addr = build_address (decl);
15170 call = build_delete (TREE_TYPE (addr), addr,
15171 sfk_complete_destructor, flags, 0, complain);
15172 if (call == error_mark_node)
15173 cleanup = error_mark_node;
15174 else if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
15175 /* Discard the call. */;
15176 else if (cleanup)
15177 cleanup = cp_build_compound_expr (cleanup, call, complain);
15178 else
15179 cleanup = call;
15182 /* build_delete sets the location of the destructor call to the
15183 current location, even though the destructor is going to be
15184 called later, at the end of the current scope. This can lead to
15185 a "jumpy" behavior for users of debuggers when they step around
15186 the end of the block. So let's unset the location of the
15187 destructor call instead. */
15188 protected_set_expr_location (cleanup, UNKNOWN_LOCATION);
15190 if (cleanup
15191 && DECL_P (decl)
15192 && !lookup_attribute ("warn_unused", TYPE_ATTRIBUTES (TREE_TYPE (decl)))
15193 /* Treat objects with destructors as used; the destructor may do
15194 something substantive. */
15195 && !mark_used (decl, complain) && !(complain & tf_error))
15196 return error_mark_node;
15198 return cleanup;
15202 /* Return the FUNCTION_TYPE that corresponds to MEMFNTYPE, which can be a
15203 FUNCTION_DECL, METHOD_TYPE, FUNCTION_TYPE, pointer or reference to
15204 METHOD_TYPE or FUNCTION_TYPE, or pointer to member function. */
15206 tree
15207 static_fn_type (tree memfntype)
15209 tree fntype;
15210 tree args;
15212 if (TYPE_PTRMEMFUNC_P (memfntype))
15213 memfntype = TYPE_PTRMEMFUNC_FN_TYPE (memfntype);
15214 if (POINTER_TYPE_P (memfntype)
15215 || TREE_CODE (memfntype) == FUNCTION_DECL)
15216 memfntype = TREE_TYPE (memfntype);
15217 if (TREE_CODE (memfntype) == FUNCTION_TYPE)
15218 return memfntype;
15219 gcc_assert (TREE_CODE (memfntype) == METHOD_TYPE);
15220 args = TYPE_ARG_TYPES (memfntype);
15221 cp_ref_qualifier rqual = type_memfn_rqual (memfntype);
15222 fntype = build_function_type (TREE_TYPE (memfntype), TREE_CHAIN (args));
15223 fntype = apply_memfn_quals (fntype, type_memfn_quals (memfntype), rqual);
15224 fntype = (cp_build_type_attribute_variant
15225 (fntype, TYPE_ATTRIBUTES (memfntype)));
15226 fntype = (build_exception_variant
15227 (fntype, TYPE_RAISES_EXCEPTIONS (memfntype)));
15228 if (TYPE_HAS_LATE_RETURN_TYPE (memfntype))
15229 TYPE_HAS_LATE_RETURN_TYPE (fntype) = 1;
15230 return fntype;
15233 /* DECL was originally constructed as a non-static member function,
15234 but turned out to be static. Update it accordingly. */
15236 void
15237 revert_static_member_fn (tree decl)
15239 tree stype = static_fn_type (decl);
15240 cp_cv_quals quals = type_memfn_quals (stype);
15241 cp_ref_qualifier rqual = type_memfn_rqual (stype);
15243 if (quals != TYPE_UNQUALIFIED || rqual != REF_QUAL_NONE)
15244 stype = apply_memfn_quals (stype, TYPE_UNQUALIFIED, REF_QUAL_NONE);
15246 TREE_TYPE (decl) = stype;
15248 if (DECL_ARGUMENTS (decl))
15249 DECL_ARGUMENTS (decl) = DECL_CHAIN (DECL_ARGUMENTS (decl));
15250 DECL_STATIC_FUNCTION_P (decl) = 1;
15253 /* Return which tree structure is used by T, or TS_CP_GENERIC if T is
15254 one of the language-independent trees. */
15256 enum cp_tree_node_structure_enum
15257 cp_tree_node_structure (union lang_tree_node * t)
15259 switch (TREE_CODE (&t->generic))
15261 case DEFAULT_ARG: return TS_CP_DEFAULT_ARG;
15262 case DEFERRED_NOEXCEPT: return TS_CP_DEFERRED_NOEXCEPT;
15263 case IDENTIFIER_NODE: return TS_CP_IDENTIFIER;
15264 case OVERLOAD: return TS_CP_OVERLOAD;
15265 case TEMPLATE_PARM_INDEX: return TS_CP_TPI;
15266 case PTRMEM_CST: return TS_CP_PTRMEM;
15267 case BASELINK: return TS_CP_BASELINK;
15268 case TEMPLATE_DECL: return TS_CP_TEMPLATE_DECL;
15269 case STATIC_ASSERT: return TS_CP_STATIC_ASSERT;
15270 case ARGUMENT_PACK_SELECT: return TS_CP_ARGUMENT_PACK_SELECT;
15271 case TRAIT_EXPR: return TS_CP_TRAIT_EXPR;
15272 case LAMBDA_EXPR: return TS_CP_LAMBDA_EXPR;
15273 case TEMPLATE_INFO: return TS_CP_TEMPLATE_INFO;
15274 case CONSTRAINT_INFO: return TS_CP_CONSTRAINT_INFO;
15275 case USERDEF_LITERAL: return TS_CP_USERDEF_LITERAL;
15276 default: return TS_CP_GENERIC;
15280 /* Build the void_list_node (void_type_node having been created). */
15281 tree
15282 build_void_list_node (void)
15284 tree t = build_tree_list (NULL_TREE, void_type_node);
15285 return t;
15288 bool
15289 cp_missing_noreturn_ok_p (tree decl)
15291 /* A missing noreturn is ok for the `main' function. */
15292 return DECL_MAIN_P (decl);
15295 /* Return the decl used to identify the COMDAT group into which DECL should
15296 be placed. */
15298 tree
15299 cxx_comdat_group (tree decl)
15301 /* Virtual tables, construction virtual tables, and virtual table
15302 tables all go in a single COMDAT group, named after the primary
15303 virtual table. */
15304 if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl))
15305 decl = CLASSTYPE_VTABLES (DECL_CONTEXT (decl));
15306 /* For all other DECLs, the COMDAT group is the mangled name of the
15307 declaration itself. */
15308 else
15310 while (DECL_THUNK_P (decl))
15312 /* If TARGET_USE_LOCAL_THUNK_ALIAS_P, use_thunk puts the thunk
15313 into the same section as the target function. In that case
15314 we must return target's name. */
15315 tree target = THUNK_TARGET (decl);
15316 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (target)
15317 && DECL_SECTION_NAME (target) != NULL
15318 && DECL_ONE_ONLY (target))
15319 decl = target;
15320 else
15321 break;
15325 return decl;
15328 /* Returns the return type for FN as written by the user, which may include
15329 a placeholder for a deduced return type. */
15331 tree
15332 fndecl_declared_return_type (tree fn)
15334 fn = STRIP_TEMPLATE (fn);
15335 if (FNDECL_USED_AUTO (fn))
15337 struct language_function *f = NULL;
15338 if (DECL_STRUCT_FUNCTION (fn))
15339 f = DECL_STRUCT_FUNCTION (fn)->language;
15340 if (f == NULL)
15341 f = DECL_SAVED_FUNCTION_DATA (fn);
15342 return f->x_auto_return_pattern;
15344 return TREE_TYPE (TREE_TYPE (fn));
15347 /* Returns true iff DECL was declared with an auto return type and it has
15348 not yet been deduced to a real type. */
15350 bool
15351 undeduced_auto_decl (tree decl)
15353 if (cxx_dialect < cxx14)
15354 return false;
15355 return type_uses_auto (TREE_TYPE (decl));
15358 /* Complain if DECL has an undeduced return type. */
15360 void
15361 require_deduced_type (tree decl)
15363 if (undeduced_auto_decl (decl))
15364 error ("use of %qD before deduction of %<auto%>", decl);
15367 #include "gt-cp-decl.h"