Merge from trunk
[official-gcc.git] / gcc / cp / decl.c
blobd03622547105f4308f57af5de847e7fc70bdb586
1 /* Process declarations and variables for C++ compiler.
2 Copyright (C) 1988-2015 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 "tm.h"
33 #include "hash-set.h"
34 #include "machmode.h"
35 #include "vec.h"
36 #include "double-int.h"
37 #include "input.h"
38 #include "alias.h"
39 #include "symtab.h"
40 #include "wide-int.h"
41 #include "inchash.h"
42 #include "tree.h"
43 #include "print-tree.h"
44 #include "tree-hasher.h"
45 #include "stringpool.h"
46 #include "stor-layout.h"
47 #include "varasm.h"
48 #include "attribs.h"
49 #include "calls.h"
50 #include "flags.h"
51 #include "cp-tree.h"
52 #include "tree-iterator.h"
53 #include "tree-inline.h"
54 #include "decl.h"
55 #include "intl.h"
56 #include "toplev.h"
57 #include "hashtab.h"
58 #include "tm_p.h"
59 #include "target.h"
60 #include "c-family/c-common.h"
61 #include "c-family/c-objc.h"
62 #include "c-family/c-pragma.h"
63 #include "c-family/c-target.h"
64 #include "c-family/c-ubsan.h"
65 #include "diagnostic.h"
66 #include "intl.h"
67 #include "debug.h"
68 #include "timevar.h"
69 #include "splay-tree.h"
70 #include "plugin.h"
71 #include "hash-map.h"
72 #include "is-a.h"
73 #include "plugin-api.h"
74 #include "hard-reg-set.h"
75 #include "input.h"
76 #include "function.h"
77 #include "ipa-ref.h"
78 #include "cgraph.h"
79 #include "cilk.h"
80 #include "wide-int.h"
81 #include "builtins.h"
83 /* Possible cases of bad specifiers type used by bad_specifiers. */
84 enum bad_spec_place {
85 BSP_VAR, /* variable */
86 BSP_PARM, /* parameter */
87 BSP_TYPE, /* type */
88 BSP_FIELD /* field */
91 static tree grokparms (tree parmlist, tree *);
92 static const char *redeclaration_error_message (tree, tree);
94 static int decl_jump_unsafe (tree);
95 static void require_complete_types_for_parms (tree);
96 static int ambi_op_p (enum tree_code);
97 static int unary_op_p (enum tree_code);
98 static void push_local_name (tree);
99 static tree grok_reference_init (tree, tree, tree, int);
100 static tree grokvardecl (tree, tree, tree, const cp_decl_specifier_seq *,
101 int, int, int, tree);
102 static int check_static_variable_definition (tree, tree);
103 static void record_unknown_type (tree, const char *);
104 static tree builtin_function_1 (tree, tree, bool);
105 static int member_function_or_else (tree, tree, enum overload_flags);
106 static void bad_specifiers (tree, enum bad_spec_place, int, int, int, int,
107 int);
108 static void check_for_uninitialized_const_var (tree);
109 static tree local_variable_p_walkfn (tree *, int *, void *);
110 static tree record_builtin_java_type (const char *, int);
111 static const char *tag_name (enum tag_types);
112 static tree lookup_and_check_tag (enum tag_types, tree, tag_scope, bool);
113 static int walk_namespaces_r (tree, walk_namespaces_fn, void *);
114 static void maybe_deduce_size_from_array_init (tree, tree);
115 static void layout_var_decl (tree);
116 static tree check_initializer (tree, tree, int, vec<tree, va_gc> **);
117 static void make_rtl_for_nonlocal_decl (tree, tree, const char *);
118 static void save_function_data (tree);
119 static void copy_type_enum (tree , tree);
120 static void check_function_type (tree, tree);
121 static void finish_constructor_body (void);
122 static void begin_destructor_body (void);
123 static void finish_destructor_body (void);
124 static void record_key_method_defined (tree);
125 static tree create_array_type_for_decl (tree, tree, tree);
126 static tree get_atexit_node (void);
127 static tree get_dso_handle_node (void);
128 static tree start_cleanup_fn (void);
129 static void end_cleanup_fn (void);
130 static tree cp_make_fname_decl (location_t, tree, int);
131 static void initialize_predefined_identifiers (void);
132 static tree check_special_function_return_type
133 (special_function_kind, tree, tree);
134 static tree push_cp_library_fn (enum tree_code, tree, int);
135 static tree build_cp_library_fn (tree, enum tree_code, tree, int);
136 static void store_parm_decls (tree);
137 static void initialize_local_var (tree, tree);
138 static void expand_static_init (tree, tree);
140 /* The following symbols are subsumed in the cp_global_trees array, and
141 listed here individually for documentation purposes.
143 C++ extensions
144 tree wchar_decl_node;
146 tree vtable_entry_type;
147 tree delta_type_node;
148 tree __t_desc_type_node;
150 tree class_type_node;
151 tree unknown_type_node;
153 Array type `vtable_entry_type[]'
155 tree vtbl_type_node;
156 tree vtbl_ptr_type_node;
158 Namespaces,
160 tree std_node;
161 tree abi_node;
163 A FUNCTION_DECL which can call `abort'. Not necessarily the
164 one that the user will declare, but sufficient to be called
165 by routines that want to abort the program.
167 tree abort_fndecl;
169 Used by RTTI
170 tree type_info_type_node, tinfo_decl_id, tinfo_decl_type;
171 tree tinfo_var_id; */
173 tree cp_global_trees[CPTI_MAX];
175 /* Indicates that there is a type value in some namespace, although
176 that is not necessarily in scope at the moment. */
178 tree global_type_node;
180 /* The node that holds the "name" of the global scope. */
181 tree global_scope_name;
183 #define local_names cp_function_chain->x_local_names
185 /* A list of objects which have constructors or destructors
186 which reside in the global scope. The decl is stored in
187 the TREE_VALUE slot and the initializer is stored
188 in the TREE_PURPOSE slot. */
189 tree static_aggregates;
191 /* Like static_aggregates, but for thread_local variables. */
192 tree tls_aggregates;
194 /* -- end of C++ */
196 /* A node for the integer constant 2. */
198 tree integer_two_node;
200 /* Used only for jumps to as-yet undefined labels, since jumps to
201 defined labels can have their validity checked immediately. */
203 struct GTY((chain_next ("%h.next"))) named_label_use_entry {
204 struct named_label_use_entry *next;
205 /* The binding level to which this entry is *currently* attached.
206 This is initially the binding level in which the goto appeared,
207 but is modified as scopes are closed. */
208 cp_binding_level *binding_level;
209 /* The head of the names list that was current when the goto appeared,
210 or the inner scope popped. These are the decls that will *not* be
211 skipped when jumping to the label. */
212 tree names_in_scope;
213 /* The location of the goto, for error reporting. */
214 location_t o_goto_locus;
215 /* True if an OpenMP structured block scope has been closed since
216 the goto appeared. This means that the branch from the label will
217 illegally exit an OpenMP scope. */
218 bool in_omp_scope;
221 /* A list of all LABEL_DECLs in the function that have names. Here so
222 we can clear out their names' definitions at the end of the
223 function, and so we can check the validity of jumps to these labels. */
225 struct GTY((for_user)) named_label_entry {
226 /* The decl itself. */
227 tree label_decl;
229 /* The binding level to which the label is *currently* attached.
230 This is initially set to the binding level in which the label
231 is defined, but is modified as scopes are closed. */
232 cp_binding_level *binding_level;
233 /* The head of the names list that was current when the label was
234 defined, or the inner scope popped. These are the decls that will
235 be skipped when jumping to the label. */
236 tree names_in_scope;
237 /* A vector of all decls from all binding levels that would be
238 crossed by a backward branch to the label. */
239 vec<tree, va_gc> *bad_decls;
241 /* A list of uses of the label, before the label is defined. */
242 struct named_label_use_entry *uses;
244 /* The following bits are set after the label is defined, and are
245 updated as scopes are popped. They indicate that a backward jump
246 to the label will illegally enter a scope of the given flavor. */
247 bool in_try_scope;
248 bool in_catch_scope;
249 bool in_omp_scope;
252 #define named_labels cp_function_chain->x_named_labels
254 /* The number of function bodies which we are currently processing.
255 (Zero if we are at namespace scope, one inside the body of a
256 function, two inside the body of a function in a local class, etc.) */
257 int function_depth;
259 /* To avoid unwanted recursion, finish_function defers all mark_used calls
260 encountered during its execution until it finishes. */
261 bool defer_mark_used_calls;
262 vec<tree, va_gc> *deferred_mark_used_calls;
264 /* States indicating how grokdeclarator() should handle declspecs marked
265 with __attribute__((deprecated)). An object declared as
266 __attribute__((deprecated)) suppresses warnings of uses of other
267 deprecated items. */
268 enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
271 /* A list of VAR_DECLs whose type was incomplete at the time the
272 variable was declared. */
274 typedef struct GTY(()) incomplete_var_d {
275 tree decl;
276 tree incomplete_type;
277 } incomplete_var;
280 static GTY(()) vec<incomplete_var, va_gc> *incomplete_vars;
282 /* Returns the kind of template specialization we are currently
283 processing, given that it's declaration contained N_CLASS_SCOPES
284 explicit scope qualifications. */
286 tmpl_spec_kind
287 current_tmpl_spec_kind (int n_class_scopes)
289 int n_template_parm_scopes = 0;
290 int seen_specialization_p = 0;
291 int innermost_specialization_p = 0;
292 cp_binding_level *b;
294 /* Scan through the template parameter scopes. */
295 for (b = current_binding_level;
296 b->kind == sk_template_parms;
297 b = b->level_chain)
299 /* If we see a specialization scope inside a parameter scope,
300 then something is wrong. That corresponds to a declaration
301 like:
303 template <class T> template <> ...
305 which is always invalid since [temp.expl.spec] forbids the
306 specialization of a class member template if the enclosing
307 class templates are not explicitly specialized as well. */
308 if (b->explicit_spec_p)
310 if (n_template_parm_scopes == 0)
311 innermost_specialization_p = 1;
312 else
313 seen_specialization_p = 1;
315 else if (seen_specialization_p == 1)
316 return tsk_invalid_member_spec;
318 ++n_template_parm_scopes;
321 /* Handle explicit instantiations. */
322 if (processing_explicit_instantiation)
324 if (n_template_parm_scopes != 0)
325 /* We've seen a template parameter list during an explicit
326 instantiation. For example:
328 template <class T> template void f(int);
330 This is erroneous. */
331 return tsk_invalid_expl_inst;
332 else
333 return tsk_expl_inst;
336 if (n_template_parm_scopes < n_class_scopes)
337 /* We've not seen enough template headers to match all the
338 specialized classes present. For example:
340 template <class T> void R<T>::S<T>::f(int);
342 This is invalid; there needs to be one set of template
343 parameters for each class. */
344 return tsk_insufficient_parms;
345 else if (n_template_parm_scopes == n_class_scopes)
346 /* We're processing a non-template declaration (even though it may
347 be a member of a template class.) For example:
349 template <class T> void S<T>::f(int);
351 The `class T' matches the `S<T>', leaving no template headers
352 corresponding to the `f'. */
353 return tsk_none;
354 else if (n_template_parm_scopes > n_class_scopes + 1)
355 /* We've got too many template headers. For example:
357 template <> template <class T> void f (T);
359 There need to be more enclosing classes. */
360 return tsk_excessive_parms;
361 else
362 /* This must be a template. It's of the form:
364 template <class T> template <class U> void S<T>::f(U);
366 This is a specialization if the innermost level was a
367 specialization; otherwise it's just a definition of the
368 template. */
369 return innermost_specialization_p ? tsk_expl_spec : tsk_template;
372 /* Exit the current scope. */
374 void
375 finish_scope (void)
377 poplevel (0, 0, 0);
380 /* When a label goes out of scope, check to see if that label was used
381 in a valid manner, and issue any appropriate warnings or errors. */
383 static void
384 pop_label (tree label, tree old_value)
386 if (!processing_template_decl)
388 if (DECL_INITIAL (label) == NULL_TREE)
390 location_t location;
392 error ("label %q+D used but not defined", label);
393 location = input_location;
394 /* FIXME want (LOCATION_FILE (input_location), (line)0) */
395 /* Avoid crashing later. */
396 define_label (location, DECL_NAME (label));
398 else
399 warn_for_unused_label (label);
402 SET_IDENTIFIER_LABEL_VALUE (DECL_NAME (label), old_value);
405 /* At the end of a function, all labels declared within the function
406 go out of scope. BLOCK is the top-level block for the
407 function. */
410 pop_labels_1 (named_label_entry **slot, tree block)
412 struct named_label_entry *ent = *slot;
414 pop_label (ent->label_decl, NULL_TREE);
416 /* Put the labels into the "variables" of the top-level block,
417 so debugger can see them. */
418 DECL_CHAIN (ent->label_decl) = BLOCK_VARS (block);
419 BLOCK_VARS (block) = ent->label_decl;
421 named_labels->clear_slot (slot);
423 return 1;
426 static void
427 pop_labels (tree block)
429 if (named_labels)
431 named_labels->traverse<tree, pop_labels_1> (block);
432 named_labels = NULL;
436 /* At the end of a block with local labels, restore the outer definition. */
438 static void
439 pop_local_label (tree label, tree old_value)
441 struct named_label_entry dummy;
443 pop_label (label, old_value);
445 dummy.label_decl = label;
446 named_label_entry **slot = named_labels->find_slot (&dummy, NO_INSERT);
447 named_labels->clear_slot (slot);
450 /* The following two routines are used to interface to Objective-C++.
451 The binding level is purposely treated as an opaque type. */
453 void *
454 objc_get_current_scope (void)
456 return current_binding_level;
459 /* The following routine is used by the NeXT-style SJLJ exceptions;
460 variables get marked 'volatile' so as to not be clobbered by
461 _setjmp()/_longjmp() calls. All variables in the current scope,
462 as well as parent scopes up to (but not including) ENCLOSING_BLK
463 shall be thusly marked. */
465 void
466 objc_mark_locals_volatile (void *enclosing_blk)
468 cp_binding_level *scope;
470 for (scope = current_binding_level;
471 scope && scope != enclosing_blk;
472 scope = scope->level_chain)
474 tree decl;
476 for (decl = scope->names; decl; decl = TREE_CHAIN (decl))
477 objc_volatilize_decl (decl);
479 /* Do not climb up past the current function. */
480 if (scope->kind == sk_function_parms)
481 break;
485 /* Update data for defined and undefined labels when leaving a scope. */
488 poplevel_named_label_1 (named_label_entry **slot, cp_binding_level *bl)
490 named_label_entry *ent = *slot;
491 cp_binding_level *obl = bl->level_chain;
493 if (ent->binding_level == bl)
495 tree decl;
497 /* ENT->NAMES_IN_SCOPE may contain a mixture of DECLs and
498 TREE_LISTs representing OVERLOADs, so be careful. */
499 for (decl = ent->names_in_scope; decl; decl = (DECL_P (decl)
500 ? DECL_CHAIN (decl)
501 : TREE_CHAIN (decl)))
502 if (decl_jump_unsafe (decl))
503 vec_safe_push (ent->bad_decls, decl);
505 ent->binding_level = obl;
506 ent->names_in_scope = obl->names;
507 switch (bl->kind)
509 case sk_try:
510 ent->in_try_scope = true;
511 break;
512 case sk_catch:
513 ent->in_catch_scope = true;
514 break;
515 case sk_omp:
516 ent->in_omp_scope = true;
517 break;
518 default:
519 break;
522 else if (ent->uses)
524 struct named_label_use_entry *use;
526 for (use = ent->uses; use ; use = use->next)
527 if (use->binding_level == bl)
529 use->binding_level = obl;
530 use->names_in_scope = obl->names;
531 if (bl->kind == sk_omp)
532 use->in_omp_scope = true;
536 return 1;
539 /* Saved errorcount to avoid -Wunused-but-set-{parameter,variable} warnings
540 when errors were reported, except for -Werror-unused-but-set-*. */
541 static int unused_but_set_errorcount;
543 /* Exit a binding level.
544 Pop the level off, and restore the state of the identifier-decl mappings
545 that were in effect when this level was entered.
547 If KEEP == 1, this level had explicit declarations, so
548 and create a "block" (a BLOCK node) for the level
549 to record its declarations and subblocks for symbol table output.
551 If FUNCTIONBODY is nonzero, this level is the body of a function,
552 so create a block as if KEEP were set and also clear out all
553 label names.
555 If REVERSE is nonzero, reverse the order of decls before putting
556 them into the BLOCK. */
558 tree
559 poplevel (int keep, int reverse, int functionbody)
561 tree link;
562 /* The chain of decls was accumulated in reverse order.
563 Put it into forward order, just for cleanliness. */
564 tree decls;
565 tree subblocks;
566 tree block;
567 tree decl;
568 int leaving_for_scope;
569 scope_kind kind;
570 unsigned ix;
571 cp_label_binding *label_bind;
573 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
574 restart:
576 block = NULL_TREE;
578 gcc_assert (current_binding_level->kind != sk_class);
580 if (current_binding_level->kind == sk_cleanup)
581 functionbody = 0;
582 subblocks = functionbody >= 0 ? current_binding_level->blocks : 0;
584 gcc_assert (!vec_safe_length (current_binding_level->class_shadowed));
586 /* We used to use KEEP == 2 to indicate that the new block should go
587 at the beginning of the list of blocks at this binding level,
588 rather than the end. This hack is no longer used. */
589 gcc_assert (keep == 0 || keep == 1);
591 if (current_binding_level->keep)
592 keep = 1;
594 /* Any uses of undefined labels, and any defined labels, now operate
595 under constraints of next binding contour. */
596 if (cfun && !functionbody && named_labels)
597 named_labels->traverse<cp_binding_level *, poplevel_named_label_1>
598 (current_binding_level);
600 /* Get the decls in the order they were written.
601 Usually current_binding_level->names is in reverse order.
602 But parameter decls were previously put in forward order. */
604 if (reverse)
605 current_binding_level->names
606 = decls = nreverse (current_binding_level->names);
607 else
608 decls = current_binding_level->names;
610 /* If there were any declarations or structure tags in that level,
611 or if this level is a function body,
612 create a BLOCK to record them for the life of this function. */
613 block = NULL_TREE;
614 /* Avoid function body block if possible. */
615 if (functionbody && subblocks && BLOCK_CHAIN (subblocks) == NULL_TREE)
616 keep = 0;
617 else if (keep == 1 || functionbody)
618 block = make_node (BLOCK);
619 if (block != NULL_TREE)
621 BLOCK_VARS (block) = decls;
622 BLOCK_SUBBLOCKS (block) = subblocks;
625 /* In each subblock, record that this is its superior. */
626 if (keep >= 0)
627 for (link = subblocks; link; link = BLOCK_CHAIN (link))
628 BLOCK_SUPERCONTEXT (link) = block;
630 /* We still support the old for-scope rules, whereby the variables
631 in a for-init statement were in scope after the for-statement
632 ended. We only use the new rules if flag_new_for_scope is
633 nonzero. */
634 leaving_for_scope
635 = current_binding_level->kind == sk_for && flag_new_for_scope == 1;
637 /* Before we remove the declarations first check for unused variables. */
638 if ((warn_unused_variable || warn_unused_but_set_variable)
639 && current_binding_level->kind != sk_template_parms
640 && !processing_template_decl)
641 for (tree d = getdecls (); d; d = TREE_CHAIN (d))
643 /* There are cases where D itself is a TREE_LIST. See in
644 push_local_binding where the list of decls returned by
645 getdecls is built. */
646 decl = TREE_CODE (d) == TREE_LIST ? TREE_VALUE (d) : d;
647 tree type = TREE_TYPE (decl);
648 if (VAR_P (decl)
649 && (! TREE_USED (decl) || !DECL_READ_P (decl))
650 && ! DECL_IN_SYSTEM_HEADER (decl)
651 && DECL_NAME (decl) && ! DECL_ARTIFICIAL (decl)
652 && type != error_mark_node
653 && (!CLASS_TYPE_P (type)
654 || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
655 || lookup_attribute ("warn_unused",
656 TYPE_ATTRIBUTES (TREE_TYPE (decl)))))
658 if (! TREE_USED (decl))
659 warning (OPT_Wunused_variable, "unused variable %q+D", decl);
660 else if (DECL_CONTEXT (decl) == current_function_decl
661 // For -Wunused-but-set-variable leave references alone.
662 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
663 && errorcount == unused_but_set_errorcount)
665 warning (OPT_Wunused_but_set_variable,
666 "variable %q+D set but not used", decl);
667 unused_but_set_errorcount = errorcount;
672 /* Remove declarations for all the DECLs in this level. */
673 for (link = decls; link; link = TREE_CHAIN (link))
675 if (leaving_for_scope && VAR_P (link)
676 /* It's hard to make this ARM compatibility hack play nicely with
677 lambdas, and it really isn't necessary in C++11 mode. */
678 && cxx_dialect < cxx11
679 && DECL_NAME (link))
681 tree name = DECL_NAME (link);
682 cxx_binding *ob;
683 tree ns_binding;
685 ob = outer_binding (name,
686 IDENTIFIER_BINDING (name),
687 /*class_p=*/true);
688 if (!ob)
689 ns_binding = IDENTIFIER_NAMESPACE_VALUE (name);
690 else
691 ns_binding = NULL_TREE;
693 if (ob && ob->scope == current_binding_level->level_chain)
694 /* We have something like:
696 int i;
697 for (int i; ;);
699 and we are leaving the `for' scope. There's no reason to
700 keep the binding of the inner `i' in this case. */
701 pop_binding (name, link);
702 else if ((ob && (TREE_CODE (ob->value) == TYPE_DECL))
703 || (ns_binding && TREE_CODE (ns_binding) == TYPE_DECL))
704 /* Here, we have something like:
706 typedef int I;
708 void f () {
709 for (int I; ;);
712 We must pop the for-scope binding so we know what's a
713 type and what isn't. */
714 pop_binding (name, link);
715 else
717 /* Mark this VAR_DECL as dead so that we can tell we left it
718 there only for backward compatibility. */
719 DECL_DEAD_FOR_LOCAL (link) = 1;
721 /* Keep track of what should have happened when we
722 popped the binding. */
723 if (ob && ob->value)
725 SET_DECL_SHADOWED_FOR_VAR (link, ob->value);
726 DECL_HAS_SHADOWED_FOR_VAR_P (link) = 1;
729 /* Add it to the list of dead variables in the next
730 outermost binding to that we can remove these when we
731 leave that binding. */
732 vec_safe_push (
733 current_binding_level->level_chain->dead_vars_from_for,
734 link);
736 /* Although we don't pop the cxx_binding, we do clear
737 its SCOPE since the scope is going away now. */
738 IDENTIFIER_BINDING (name)->scope
739 = current_binding_level->level_chain;
742 else
744 tree name;
746 /* Remove the binding. */
747 decl = link;
749 if (TREE_CODE (decl) == TREE_LIST)
750 decl = TREE_VALUE (decl);
751 name = decl;
753 if (TREE_CODE (name) == OVERLOAD)
754 name = OVL_FUNCTION (name);
756 gcc_assert (DECL_P (name));
757 pop_binding (DECL_NAME (name), decl);
761 /* Remove declarations for any `for' variables from inner scopes
762 that we kept around. */
763 FOR_EACH_VEC_SAFE_ELT_REVERSE (current_binding_level->dead_vars_from_for,
764 ix, decl)
765 pop_binding (DECL_NAME (decl), decl);
767 /* Restore the IDENTIFIER_TYPE_VALUEs. */
768 for (link = current_binding_level->type_shadowed;
769 link; link = TREE_CHAIN (link))
770 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link), TREE_VALUE (link));
772 /* Restore the IDENTIFIER_LABEL_VALUEs for local labels. */
773 FOR_EACH_VEC_SAFE_ELT_REVERSE (current_binding_level->shadowed_labels,
774 ix, label_bind)
775 pop_local_label (label_bind->label, label_bind->prev_value);
777 /* There may be OVERLOADs (wrapped in TREE_LISTs) on the BLOCK_VARs
778 list if a `using' declaration put them there. The debugging
779 back ends won't understand OVERLOAD, so we remove them here.
780 Because the BLOCK_VARS are (temporarily) shared with
781 CURRENT_BINDING_LEVEL->NAMES we must do this fixup after we have
782 popped all the bindings. */
783 if (block)
785 tree* d;
787 for (d = &BLOCK_VARS (block); *d; )
789 if (TREE_CODE (*d) == TREE_LIST)
790 *d = TREE_CHAIN (*d);
791 else
792 d = &DECL_CHAIN (*d);
796 /* If the level being exited is the top level of a function,
797 check over all the labels. */
798 if (functionbody)
800 if (block)
802 /* Since this is the top level block of a function, the vars are
803 the function's parameters. Don't leave them in the BLOCK
804 because they are found in the FUNCTION_DECL instead. */
805 BLOCK_VARS (block) = 0;
806 pop_labels (block);
808 else
809 pop_labels (subblocks);
812 kind = current_binding_level->kind;
813 if (kind == sk_cleanup)
815 tree stmt;
817 /* If this is a temporary binding created for a cleanup, then we'll
818 have pushed a statement list level. Pop that, create a new
819 BIND_EXPR for the block, and insert it into the stream. */
820 stmt = pop_stmt_list (current_binding_level->statement_list);
821 stmt = c_build_bind_expr (input_location, block, stmt);
822 add_stmt (stmt);
825 leave_scope ();
826 if (functionbody)
828 /* The current function is being defined, so its DECL_INITIAL
829 should be error_mark_node. */
830 gcc_assert (DECL_INITIAL (current_function_decl) == error_mark_node);
831 DECL_INITIAL (current_function_decl) = block ? block : subblocks;
832 if (subblocks)
834 if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
836 if (BLOCK_SUBBLOCKS (subblocks))
837 BLOCK_OUTER_CURLY_BRACE_P (BLOCK_SUBBLOCKS (subblocks)) = 1;
839 else
840 BLOCK_OUTER_CURLY_BRACE_P (subblocks) = 1;
843 else if (block)
844 current_binding_level->blocks
845 = block_chainon (current_binding_level->blocks, block);
847 /* If we did not make a block for the level just exited,
848 any blocks made for inner levels
849 (since they cannot be recorded as subblocks in that level)
850 must be carried forward so they will later become subblocks
851 of something else. */
852 else if (subblocks)
853 current_binding_level->blocks
854 = block_chainon (current_binding_level->blocks, subblocks);
856 /* Each and every BLOCK node created here in `poplevel' is important
857 (e.g. for proper debugging information) so if we created one
858 earlier, mark it as "used". */
859 if (block)
860 TREE_USED (block) = 1;
862 /* All temporary bindings created for cleanups are popped silently. */
863 if (kind == sk_cleanup)
864 goto restart;
866 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
867 return block;
870 /* Walk all the namespaces contained NAMESPACE, including NAMESPACE
871 itself, calling F for each. The DATA is passed to F as well. */
873 static int
874 walk_namespaces_r (tree name_space, walk_namespaces_fn f, void* data)
876 int result = 0;
877 tree current = NAMESPACE_LEVEL (name_space)->namespaces;
879 result |= (*f) (name_space, data);
881 for (; current; current = DECL_CHAIN (current))
882 result |= walk_namespaces_r (current, f, data);
884 return result;
887 /* Walk all the namespaces, calling F for each. The DATA is passed to
888 F as well. */
891 walk_namespaces (walk_namespaces_fn f, void* data)
893 return walk_namespaces_r (global_namespace, f, data);
896 /* Call wrapup_globals_declarations for the globals in NAMESPACE. If
897 DATA is non-NULL, this is the last time we will call
898 wrapup_global_declarations for this NAMESPACE. */
901 wrapup_globals_for_namespace (tree name_space, void* data)
903 cp_binding_level *level = NAMESPACE_LEVEL (name_space);
904 vec<tree, va_gc> *statics = level->static_decls;
905 tree *vec = statics->address ();
906 int len = statics->length ();
907 int last_time = (data != 0);
909 if (last_time)
911 check_global_declarations (vec, len);
912 emit_debug_global_declarations (vec, len);
913 return 0;
916 /* Write out any globals that need to be output. */
917 return wrapup_global_declarations (vec, len);
921 /* In C++, you don't have to write `struct S' to refer to `S'; you
922 can just use `S'. We accomplish this by creating a TYPE_DECL as
923 if the user had written `typedef struct S S'. Create and return
924 the TYPE_DECL for TYPE. */
926 tree
927 create_implicit_typedef (tree name, tree type)
929 tree decl;
931 decl = build_decl (input_location, TYPE_DECL, name, type);
932 DECL_ARTIFICIAL (decl) = 1;
933 /* There are other implicit type declarations, like the one *within*
934 a class that allows you to write `S::S'. We must distinguish
935 amongst these. */
936 SET_DECL_IMPLICIT_TYPEDEF_P (decl);
937 TYPE_NAME (type) = decl;
938 TYPE_STUB_DECL (type) = decl;
940 return decl;
943 /* Remember a local name for name-mangling purposes. */
945 static void
946 push_local_name (tree decl)
948 size_t i, nelts;
949 tree t, name;
951 timevar_start (TV_NAME_LOOKUP);
953 name = DECL_NAME (decl);
955 nelts = vec_safe_length (local_names);
956 for (i = 0; i < nelts; i++)
958 t = (*local_names)[i];
959 if (DECL_NAME (t) == name)
961 if (!DECL_LANG_SPECIFIC (decl))
962 retrofit_lang_decl (decl);
963 DECL_LANG_SPECIFIC (decl)->u.base.u2sel = 1;
964 if (DECL_DISCRIMINATOR_SET_P (t))
965 DECL_DISCRIMINATOR (decl) = DECL_DISCRIMINATOR (t) + 1;
966 else
967 DECL_DISCRIMINATOR (decl) = 1;
969 (*local_names)[i] = decl;
970 timevar_stop (TV_NAME_LOOKUP);
971 return;
975 vec_safe_push (local_names, decl);
976 timevar_stop (TV_NAME_LOOKUP);
979 /* Subroutine of duplicate_decls: return truthvalue of whether
980 or not types of these decls match.
982 For C++, we must compare the parameter list so that `int' can match
983 `int&' in a parameter position, but `int&' is not confused with
984 `const int&'. */
987 decls_match (tree newdecl, tree olddecl)
989 int types_match;
991 if (newdecl == olddecl)
992 return 1;
994 if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
995 /* If the two DECLs are not even the same kind of thing, we're not
996 interested in their types. */
997 return 0;
999 gcc_assert (DECL_P (newdecl));
1001 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1003 tree f1 = TREE_TYPE (newdecl);
1004 tree f2 = TREE_TYPE (olddecl);
1005 tree p1 = TYPE_ARG_TYPES (f1);
1006 tree p2 = TYPE_ARG_TYPES (f2);
1007 tree r2;
1009 /* Specializations of different templates are different functions
1010 even if they have the same type. */
1011 tree t1 = (DECL_USE_TEMPLATE (newdecl)
1012 ? DECL_TI_TEMPLATE (newdecl)
1013 : NULL_TREE);
1014 tree t2 = (DECL_USE_TEMPLATE (olddecl)
1015 ? DECL_TI_TEMPLATE (olddecl)
1016 : NULL_TREE);
1017 if (t1 != t2)
1018 return 0;
1020 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
1021 && ! (DECL_EXTERN_C_P (newdecl)
1022 && DECL_EXTERN_C_P (olddecl)))
1023 return 0;
1025 /* A new declaration doesn't match a built-in one unless it
1026 is also extern "C". */
1027 if (DECL_IS_BUILTIN (olddecl)
1028 && DECL_EXTERN_C_P (olddecl) && !DECL_EXTERN_C_P (newdecl))
1029 return 0;
1031 if (TREE_CODE (f1) != TREE_CODE (f2))
1032 return 0;
1034 /* A declaration with deduced return type should use its pre-deduction
1035 type for declaration matching. */
1036 r2 = fndecl_declared_return_type (olddecl);
1038 // Normal functions can be constrained. Two functions with the
1039 // same type and different constraints are different functions.
1040 tree c1 = get_constraints (newdecl);
1041 tree c2 = get_constraints (olddecl);
1043 if (same_type_p (TREE_TYPE (f1), r2))
1045 if (!prototype_p (f2) && DECL_EXTERN_C_P (olddecl)
1046 && (DECL_BUILT_IN (olddecl)
1047 #ifndef NO_IMPLICIT_EXTERN_C
1048 || (DECL_IN_SYSTEM_HEADER (newdecl) && !DECL_CLASS_SCOPE_P (newdecl))
1049 || (DECL_IN_SYSTEM_HEADER (olddecl) && !DECL_CLASS_SCOPE_P (olddecl))
1050 #endif
1053 types_match = self_promoting_args_p (p1);
1054 if (p1 == void_list_node)
1055 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
1057 #ifndef NO_IMPLICIT_EXTERN_C
1058 else if (!prototype_p (f1)
1059 && (DECL_EXTERN_C_P (olddecl)
1060 && DECL_IN_SYSTEM_HEADER (olddecl)
1061 && !DECL_CLASS_SCOPE_P (olddecl))
1062 && (DECL_EXTERN_C_P (newdecl)
1063 && DECL_IN_SYSTEM_HEADER (newdecl)
1064 && !DECL_CLASS_SCOPE_P (newdecl)))
1066 types_match = self_promoting_args_p (p2);
1067 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
1069 #endif
1070 else
1071 types_match =
1072 compparms (p1, p2)
1073 && type_memfn_rqual (f1) == type_memfn_rqual (f2)
1074 && equivalent_constraints (c1, c2)
1075 && (TYPE_ATTRIBUTES (TREE_TYPE (newdecl)) == NULL_TREE
1076 || comp_type_attributes (TREE_TYPE (newdecl),
1077 TREE_TYPE (olddecl)) != 0);
1079 else
1080 types_match = 0;
1082 /* The decls dont match if they correspond to two different versions
1083 of the same function. Disallow extern "C" functions to be
1084 versions for now. */
1085 if (types_match
1086 && !DECL_EXTERN_C_P (newdecl)
1087 && !DECL_EXTERN_C_P (olddecl)
1088 && targetm.target_option.function_versions (newdecl, olddecl))
1090 /* Mark functions as versions if necessary. Modify the mangled decl
1091 name if necessary. */
1092 if (DECL_FUNCTION_VERSIONED (newdecl)
1093 && DECL_FUNCTION_VERSIONED (olddecl))
1094 return 0;
1095 if (!DECL_FUNCTION_VERSIONED (newdecl))
1097 DECL_FUNCTION_VERSIONED (newdecl) = 1;
1098 if (DECL_ASSEMBLER_NAME_SET_P (newdecl))
1099 mangle_decl (newdecl);
1101 if (!DECL_FUNCTION_VERSIONED (olddecl))
1103 DECL_FUNCTION_VERSIONED (olddecl) = 1;
1104 if (DECL_ASSEMBLER_NAME_SET_P (olddecl))
1105 mangle_decl (olddecl);
1107 cgraph_node::record_function_versions (olddecl, newdecl);
1108 return 0;
1111 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1113 tree oldres = DECL_TEMPLATE_RESULT (olddecl);
1114 tree newres = DECL_TEMPLATE_RESULT (newdecl);
1116 if (TREE_CODE (newres) != TREE_CODE (oldres))
1117 return 0;
1119 if (!comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
1120 DECL_TEMPLATE_PARMS (olddecl)))
1121 return 0;
1123 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
1124 types_match = same_type_p (TREE_TYPE (oldres), TREE_TYPE (newres));
1125 else
1126 types_match = decls_match (oldres, newres);
1128 // If the types of the underlying templates match, compare
1129 // the template constraints. The declarations could differ there.
1130 if (types_match)
1131 types_match = equivalently_constrained (olddecl, newdecl);
1133 else
1135 /* Need to check scope for variable declaration (VAR_DECL).
1136 For typedef (TYPE_DECL), scope is ignored. */
1137 if (VAR_P (newdecl)
1138 && CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
1139 /* [dcl.link]
1140 Two declarations for an object with C language linkage
1141 with the same name (ignoring the namespace that qualify
1142 it) that appear in different namespace scopes refer to
1143 the same object. */
1144 && !(DECL_EXTERN_C_P (olddecl) && DECL_EXTERN_C_P (newdecl)))
1145 return 0;
1147 if (TREE_TYPE (newdecl) == error_mark_node)
1148 types_match = TREE_TYPE (olddecl) == error_mark_node;
1149 else if (TREE_TYPE (olddecl) == NULL_TREE)
1150 types_match = TREE_TYPE (newdecl) == NULL_TREE;
1151 else if (TREE_TYPE (newdecl) == NULL_TREE)
1152 types_match = 0;
1153 else
1154 types_match = comptypes (TREE_TYPE (newdecl),
1155 TREE_TYPE (olddecl),
1156 COMPARE_REDECLARATION);
1159 return types_match;
1162 /* If NEWDECL is `static' and an `extern' was seen previously,
1163 warn about it. OLDDECL is the previous declaration.
1165 Note that this does not apply to the C++ case of declaring
1166 a variable `extern const' and then later `const'.
1168 Don't complain about built-in functions, since they are beyond
1169 the user's control. */
1171 void
1172 warn_extern_redeclared_static (tree newdecl, tree olddecl)
1174 if (TREE_CODE (newdecl) == TYPE_DECL
1175 || TREE_CODE (newdecl) == TEMPLATE_DECL
1176 || TREE_CODE (newdecl) == CONST_DECL
1177 || TREE_CODE (newdecl) == NAMESPACE_DECL)
1178 return;
1180 /* Don't get confused by static member functions; that's a different
1181 use of `static'. */
1182 if (TREE_CODE (newdecl) == FUNCTION_DECL
1183 && DECL_STATIC_FUNCTION_P (newdecl))
1184 return;
1186 /* If the old declaration was `static', or the new one isn't, then
1187 everything is OK. */
1188 if (DECL_THIS_STATIC (olddecl) || !DECL_THIS_STATIC (newdecl))
1189 return;
1191 /* It's OK to declare a builtin function as `static'. */
1192 if (TREE_CODE (olddecl) == FUNCTION_DECL
1193 && DECL_ARTIFICIAL (olddecl))
1194 return;
1196 if (permerror (input_location,
1197 "%qD was declared %<extern%> and later %<static%>", newdecl))
1198 inform (input_location, "previous declaration of %q+D", olddecl);
1201 /* NEW_DECL is a redeclaration of OLD_DECL; both are functions or
1202 function templates. If their exception specifications do not
1203 match, issue a diagnostic. */
1205 static void
1206 check_redeclaration_exception_specification (tree new_decl,
1207 tree old_decl)
1209 tree new_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (new_decl));
1210 tree old_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old_decl));
1212 /* Two default specs are equivalent, don't force evaluation. */
1213 if (UNEVALUATED_NOEXCEPT_SPEC_P (new_exceptions)
1214 && UNEVALUATED_NOEXCEPT_SPEC_P (old_exceptions))
1215 return;
1217 maybe_instantiate_noexcept (new_decl);
1218 maybe_instantiate_noexcept (old_decl);
1219 new_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (new_decl));
1220 old_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old_decl));
1222 /* [except.spec]
1224 If any declaration of a function has an exception-specification,
1225 all declarations, including the definition and an explicit
1226 specialization, of that function shall have an
1227 exception-specification with the same set of type-ids. */
1228 if ((pedantic || ! DECL_IN_SYSTEM_HEADER (old_decl))
1229 && ! DECL_IS_BUILTIN (old_decl)
1230 && flag_exceptions
1231 && !comp_except_specs (new_exceptions, old_exceptions, ce_normal))
1233 error ("declaration of %qF has a different exception specifier",
1234 new_decl);
1235 error ("from previous declaration %q+F", old_decl);
1239 /* Return true if OLD_DECL and NEW_DECL agree on constexprness.
1240 Otherwise issue diagnostics. */
1242 static bool
1243 validate_constexpr_redeclaration (tree old_decl, tree new_decl)
1245 old_decl = STRIP_TEMPLATE (old_decl);
1246 new_decl = STRIP_TEMPLATE (new_decl);
1247 if (!VAR_OR_FUNCTION_DECL_P (old_decl)
1248 || !VAR_OR_FUNCTION_DECL_P (new_decl))
1249 return true;
1250 if (DECL_DECLARED_CONSTEXPR_P (old_decl)
1251 == DECL_DECLARED_CONSTEXPR_P (new_decl))
1252 return true;
1253 if (TREE_CODE (old_decl) == FUNCTION_DECL)
1255 if (DECL_BUILT_IN (old_decl))
1257 /* Hide a built-in declaration. */
1258 DECL_DECLARED_CONSTEXPR_P (old_decl)
1259 = DECL_DECLARED_CONSTEXPR_P (new_decl);
1260 return true;
1262 /* 7.1.5 [dcl.constexpr]
1263 Note: An explicit specialization can differ from the template
1264 declaration with respect to the constexpr specifier. */
1265 if (! DECL_TEMPLATE_SPECIALIZATION (old_decl)
1266 && DECL_TEMPLATE_SPECIALIZATION (new_decl))
1267 return true;
1269 error ("redeclaration %qD differs in %<constexpr%>", new_decl);
1270 error ("from previous declaration %q+D", old_decl);
1271 return false;
1273 return true;
1276 // If OLDDECL and NEWDECL are concept declarations with the same type
1277 // (i.e., and template parameters), but different requirements,
1278 // emit diagnostics and return true. Otherwise, return false.
1279 static inline bool
1280 check_concept_refinement (tree olddecl, tree newdecl)
1282 if (!DECL_DECLARED_CONCEPT_P (olddecl) || !DECL_DECLARED_CONCEPT_P (newdecl))
1283 return false;
1285 tree d1 = DECL_TEMPLATE_RESULT (olddecl);
1286 tree d2 = DECL_TEMPLATE_RESULT (newdecl);
1287 if (TREE_CODE (d1) != TREE_CODE (d2))
1288 return false;
1290 tree t1 = TREE_TYPE (d1);
1291 tree t2 = TREE_TYPE (d2);
1292 if (TREE_CODE (d1) == FUNCTION_DECL)
1294 if (compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2))
1295 && comp_template_parms (DECL_TEMPLATE_PARMS (olddecl),
1296 DECL_TEMPLATE_PARMS (newdecl))
1297 && !equivalently_constrained (olddecl, newdecl))
1299 error ("cannot specialize concept %q#D", olddecl);
1300 return true;
1303 return false;
1307 /* DECL is a redeclaration of a function or function template. If
1308 it does have default arguments issue a diagnostic. Note: this
1309 function is used to enforce the requirements in C++11 8.3.6 about
1310 no default arguments in redeclarations. */
1312 static void
1313 check_redeclaration_no_default_args (tree decl)
1315 gcc_assert (DECL_DECLARES_FUNCTION_P (decl));
1317 for (tree t = FUNCTION_FIRST_USER_PARMTYPE (decl);
1318 t && t != void_list_node; t = TREE_CHAIN (t))
1319 if (TREE_PURPOSE (t))
1321 permerror (input_location,
1322 "redeclaration of %q#D may not have default "
1323 "arguments", decl);
1324 return;
1328 #define GNU_INLINE_P(fn) (DECL_DECLARED_INLINE_P (fn) \
1329 && lookup_attribute ("gnu_inline", \
1330 DECL_ATTRIBUTES (fn)))
1332 /* If NEWDECL is a redeclaration of OLDDECL, merge the declarations.
1333 If the redeclaration is invalid, a diagnostic is issued, and the
1334 error_mark_node is returned. Otherwise, OLDDECL is returned.
1336 If NEWDECL is not a redeclaration of OLDDECL, NULL_TREE is
1337 returned.
1339 NEWDECL_IS_FRIEND is true if NEWDECL was declared as a friend. */
1341 tree
1342 duplicate_decls (tree newdecl, tree olddecl, bool newdecl_is_friend)
1344 unsigned olddecl_uid = DECL_UID (olddecl);
1345 int olddecl_friend = 0, types_match = 0, hidden_friend = 0;
1346 int new_defines_function = 0;
1347 tree new_template_info;
1349 if (newdecl == olddecl)
1350 return olddecl;
1352 types_match = decls_match (newdecl, olddecl);
1354 /* If either the type of the new decl or the type of the old decl is an
1355 error_mark_node, then that implies that we have already issued an
1356 error (earlier) for some bogus type specification, and in that case,
1357 it is rather pointless to harass the user with yet more error message
1358 about the same declaration, so just pretend the types match here. */
1359 if (TREE_TYPE (newdecl) == error_mark_node
1360 || TREE_TYPE (olddecl) == error_mark_node)
1361 return error_mark_node;
1363 if (UDLIT_OPER_P (DECL_NAME (newdecl))
1364 && UDLIT_OPER_P (DECL_NAME (olddecl)))
1366 if (TREE_CODE (newdecl) == TEMPLATE_DECL
1367 && TREE_CODE (olddecl) != TEMPLATE_DECL
1368 && check_raw_literal_operator (olddecl))
1369 error ("literal operator template %q+D conflicts with"
1370 " raw literal operator %qD", newdecl, olddecl);
1371 else if (TREE_CODE (newdecl) != TEMPLATE_DECL
1372 && TREE_CODE (olddecl) == TEMPLATE_DECL
1373 && check_raw_literal_operator (newdecl))
1374 error ("raw literal operator %q+D conflicts with"
1375 " literal operator template %qD", newdecl, olddecl);
1378 if (DECL_P (olddecl)
1379 && TREE_CODE (newdecl) == FUNCTION_DECL
1380 && TREE_CODE (olddecl) == FUNCTION_DECL
1381 && (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl)))
1383 if (DECL_DECLARED_INLINE_P (newdecl)
1384 && DECL_UNINLINABLE (newdecl)
1385 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1386 /* Already warned elsewhere. */;
1387 else if (DECL_DECLARED_INLINE_P (olddecl)
1388 && DECL_UNINLINABLE (olddecl)
1389 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1390 /* Already warned. */;
1391 else if (DECL_DECLARED_INLINE_P (newdecl)
1392 && DECL_UNINLINABLE (olddecl)
1393 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1395 if (warning (OPT_Wattributes, "function %q+D redeclared as inline",
1396 newdecl))
1397 inform (DECL_SOURCE_LOCATION (olddecl),
1398 "previous declaration of %qD with attribute noinline",
1399 olddecl);
1401 else if (DECL_DECLARED_INLINE_P (olddecl)
1402 && DECL_UNINLINABLE (newdecl)
1403 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1405 if (warning (OPT_Wattributes, "function %q+D redeclared with "
1406 "attribute noinline", newdecl))
1407 inform (DECL_SOURCE_LOCATION (olddecl),
1408 "previous declaration of %qD was inline",
1409 olddecl);
1413 /* Check for redeclaration and other discrepancies. */
1414 if (TREE_CODE (olddecl) == FUNCTION_DECL
1415 && DECL_ARTIFICIAL (olddecl))
1417 gcc_assert (!DECL_HIDDEN_FRIEND_P (olddecl));
1418 if (TREE_CODE (newdecl) != FUNCTION_DECL)
1420 /* Avoid warnings redeclaring built-ins which have not been
1421 explicitly declared. */
1422 if (DECL_ANTICIPATED (olddecl))
1423 return NULL_TREE;
1425 /* If you declare a built-in or predefined function name as static,
1426 the old definition is overridden, but optionally warn this was a
1427 bad choice of name. */
1428 if (! TREE_PUBLIC (newdecl))
1430 warning (OPT_Wshadow,
1431 DECL_BUILT_IN (olddecl)
1432 ? G_("shadowing built-in function %q#D")
1433 : G_("shadowing library function %q#D"), olddecl);
1434 /* Discard the old built-in function. */
1435 return NULL_TREE;
1437 /* If the built-in is not ansi, then programs can override
1438 it even globally without an error. */
1439 else if (! DECL_BUILT_IN (olddecl))
1440 warning (0, "library function %q#D redeclared as non-function %q#D",
1441 olddecl, newdecl);
1442 else
1443 error ("declaration of %q#D conflicts with built-in "
1444 "declaration %q#D", newdecl, olddecl);
1445 return NULL_TREE;
1447 else if (DECL_OMP_DECLARE_REDUCTION_P (olddecl))
1449 gcc_assert (DECL_OMP_DECLARE_REDUCTION_P (newdecl));
1450 error_at (DECL_SOURCE_LOCATION (newdecl),
1451 "redeclaration of %<pragma omp declare reduction%>");
1452 inform (DECL_SOURCE_LOCATION (olddecl),
1453 "previous %<pragma omp declare reduction%> declaration");
1454 return error_mark_node;
1456 else if (!types_match)
1458 /* Avoid warnings redeclaring built-ins which have not been
1459 explicitly declared. */
1460 if (DECL_ANTICIPATED (olddecl))
1462 /* Deal with fileptr_type_node. FILE type is not known
1463 at the time we create the builtins. */
1464 tree t1, t2;
1466 for (t1 = TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1467 t2 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1468 t1 || t2;
1469 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1470 if (!t1 || !t2)
1471 break;
1472 else if (TREE_VALUE (t2) == fileptr_type_node)
1474 tree t = TREE_VALUE (t1);
1476 if (TYPE_PTR_P (t)
1477 && TYPE_IDENTIFIER (TREE_TYPE (t))
1478 == get_identifier ("FILE")
1479 && compparms (TREE_CHAIN (t1), TREE_CHAIN (t2)))
1481 tree oldargs = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1483 TYPE_ARG_TYPES (TREE_TYPE (olddecl))
1484 = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
1485 types_match = decls_match (newdecl, olddecl);
1486 if (types_match)
1487 return duplicate_decls (newdecl, olddecl,
1488 newdecl_is_friend);
1489 TYPE_ARG_TYPES (TREE_TYPE (olddecl)) = oldargs;
1492 else if (! same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1493 break;
1495 else if ((DECL_EXTERN_C_P (newdecl)
1496 && DECL_EXTERN_C_P (olddecl))
1497 || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1498 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
1500 /* A near match; override the builtin. */
1502 if (TREE_PUBLIC (newdecl))
1503 warning (0, "new declaration %q#D ambiguates built-in "
1504 "declaration %q#D", newdecl, olddecl);
1505 else
1506 warning (OPT_Wshadow,
1507 DECL_BUILT_IN (olddecl)
1508 ? G_("shadowing built-in function %q#D")
1509 : G_("shadowing library function %q#D"), olddecl);
1511 else
1512 /* Discard the old built-in function. */
1513 return NULL_TREE;
1515 /* Replace the old RTL to avoid problems with inlining. */
1516 COPY_DECL_RTL (newdecl, olddecl);
1518 /* Even if the types match, prefer the new declarations type for
1519 built-ins which have not been explicitly declared, for
1520 exception lists, etc... */
1521 else if (DECL_IS_BUILTIN (olddecl))
1523 tree type = TREE_TYPE (newdecl);
1524 tree attribs = (*targetm.merge_type_attributes)
1525 (TREE_TYPE (olddecl), type);
1527 type = cp_build_type_attribute_variant (type, attribs);
1528 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = type;
1531 /* If a function is explicitly declared "throw ()", propagate that to
1532 the corresponding builtin. */
1533 if (DECL_BUILT_IN_CLASS (olddecl) == BUILT_IN_NORMAL
1534 && DECL_ANTICIPATED (olddecl)
1535 && TREE_NOTHROW (newdecl)
1536 && !TREE_NOTHROW (olddecl))
1538 enum built_in_function fncode = DECL_FUNCTION_CODE (olddecl);
1539 tree tmpdecl = builtin_decl_explicit (fncode);
1540 if (tmpdecl && tmpdecl != olddecl && types_match)
1541 TREE_NOTHROW (tmpdecl) = 1;
1544 /* Whether or not the builtin can throw exceptions has no
1545 bearing on this declarator. */
1546 TREE_NOTHROW (olddecl) = 0;
1548 if (DECL_THIS_STATIC (newdecl) && !DECL_THIS_STATIC (olddecl))
1550 /* If a builtin function is redeclared as `static', merge
1551 the declarations, but make the original one static. */
1552 DECL_THIS_STATIC (olddecl) = 1;
1553 TREE_PUBLIC (olddecl) = 0;
1555 /* Make the old declaration consistent with the new one so
1556 that all remnants of the builtin-ness of this function
1557 will be banished. */
1558 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
1559 COPY_DECL_RTL (newdecl, olddecl);
1562 else if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1564 /* C++ Standard, 3.3, clause 4:
1565 "[Note: a namespace name or a class template name must be unique
1566 in its declarative region (7.3.2, clause 14). ]" */
1567 if (TREE_CODE (olddecl) != NAMESPACE_DECL
1568 && TREE_CODE (newdecl) != NAMESPACE_DECL
1569 && (TREE_CODE (olddecl) != TEMPLATE_DECL
1570 || TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) != TYPE_DECL)
1571 && (TREE_CODE (newdecl) != TEMPLATE_DECL
1572 || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) != TYPE_DECL))
1574 if ((TREE_CODE (olddecl) == TYPE_DECL && DECL_ARTIFICIAL (olddecl)
1575 && TREE_CODE (newdecl) != TYPE_DECL)
1576 || (TREE_CODE (newdecl) == TYPE_DECL && DECL_ARTIFICIAL (newdecl)
1577 && TREE_CODE (olddecl) != TYPE_DECL))
1579 /* We do nothing special here, because C++ does such nasty
1580 things with TYPE_DECLs. Instead, just let the TYPE_DECL
1581 get shadowed, and know that if we need to find a TYPE_DECL
1582 for a given name, we can look in the IDENTIFIER_TYPE_VALUE
1583 slot of the identifier. */
1584 return NULL_TREE;
1587 if ((TREE_CODE (newdecl) == FUNCTION_DECL
1588 && DECL_FUNCTION_TEMPLATE_P (olddecl))
1589 || (TREE_CODE (olddecl) == FUNCTION_DECL
1590 && DECL_FUNCTION_TEMPLATE_P (newdecl)))
1591 return NULL_TREE;
1594 error ("%q#D redeclared as different kind of symbol", newdecl);
1595 if (TREE_CODE (olddecl) == TREE_LIST)
1596 olddecl = TREE_VALUE (olddecl);
1597 inform (DECL_SOURCE_LOCATION (olddecl),
1598 "previous declaration %q#D", olddecl);
1600 return error_mark_node;
1602 else if (!types_match)
1604 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl))
1605 /* These are certainly not duplicate declarations; they're
1606 from different scopes. */
1607 return NULL_TREE;
1609 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1611 /* The name of a class template may not be declared to refer to
1612 any other template, class, function, object, namespace, value,
1613 or type in the same scope. */
1614 if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == TYPE_DECL
1615 || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
1617 error ("conflicting declaration of template %q#D", newdecl);
1618 inform (DECL_SOURCE_LOCATION (olddecl),
1619 "previous declaration %q#D", olddecl);
1620 return error_mark_node;
1622 else if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == FUNCTION_DECL
1623 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL
1624 && compparms (TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl))),
1625 TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl))))
1626 && comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
1627 DECL_TEMPLATE_PARMS (olddecl))
1628 /* Template functions can be disambiguated by
1629 return type. */
1630 && same_type_p (TREE_TYPE (TREE_TYPE (newdecl)),
1631 TREE_TYPE (TREE_TYPE (olddecl)))
1632 // Template functions can also be disambiguated by
1633 // constraints.
1634 && equivalently_constrained (olddecl, newdecl))
1636 error ("ambiguating new declaration %q#D", newdecl);
1637 inform (DECL_SOURCE_LOCATION (olddecl),
1638 "old declaration %q#D", olddecl);
1640 else if (check_concept_refinement (olddecl, newdecl))
1642 return error_mark_node;
1644 return NULL_TREE;
1646 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1648 if (DECL_EXTERN_C_P (newdecl) && DECL_EXTERN_C_P (olddecl))
1650 error ("conflicting declaration of C function %q#D",
1651 newdecl);
1652 inform (DECL_SOURCE_LOCATION (olddecl),
1653 "previous declaration %q#D", olddecl);
1654 return NULL_TREE;
1656 /* For function versions, params and types match, but they
1657 are not ambiguous. */
1658 else if ((!DECL_FUNCTION_VERSIONED (newdecl)
1659 && !DECL_FUNCTION_VERSIONED (olddecl))
1660 // The functions have the same parameter types.
1661 && compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1662 TYPE_ARG_TYPES (TREE_TYPE (olddecl)))
1663 // And the same constraints.
1664 && equivalently_constrained (newdecl, olddecl))
1666 error ("ambiguating new declaration of %q#D", newdecl);
1667 inform (DECL_SOURCE_LOCATION (olddecl),
1668 "old declaration %q#D", olddecl);
1669 return error_mark_node;
1671 else
1672 return NULL_TREE;
1674 else
1676 error ("conflicting declaration %q#D", newdecl);
1677 inform (DECL_SOURCE_LOCATION (olddecl),
1678 "previous declaration as %q#D", olddecl);
1679 return error_mark_node;
1682 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1683 && ((DECL_TEMPLATE_SPECIALIZATION (olddecl)
1684 && (!DECL_TEMPLATE_INFO (newdecl)
1685 || (DECL_TI_TEMPLATE (newdecl)
1686 != DECL_TI_TEMPLATE (olddecl))))
1687 || (DECL_TEMPLATE_SPECIALIZATION (newdecl)
1688 && (!DECL_TEMPLATE_INFO (olddecl)
1689 || (DECL_TI_TEMPLATE (olddecl)
1690 != DECL_TI_TEMPLATE (newdecl))))))
1691 /* It's OK to have a template specialization and a non-template
1692 with the same type, or to have specializations of two
1693 different templates with the same type. Note that if one is a
1694 specialization, and the other is an instantiation of the same
1695 template, that we do not exit at this point. That situation
1696 can occur if we instantiate a template class, and then
1697 specialize one of its methods. This situation is valid, but
1698 the declarations must be merged in the usual way. */
1699 return NULL_TREE;
1700 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1701 && ((DECL_TEMPLATE_INSTANTIATION (olddecl)
1702 && !DECL_USE_TEMPLATE (newdecl))
1703 || (DECL_TEMPLATE_INSTANTIATION (newdecl)
1704 && !DECL_USE_TEMPLATE (olddecl))))
1705 /* One of the declarations is a template instantiation, and the
1706 other is not a template at all. That's OK. */
1707 return NULL_TREE;
1708 else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
1710 /* In [namespace.alias] we have:
1712 In a declarative region, a namespace-alias-definition can be
1713 used to redefine a namespace-alias declared in that declarative
1714 region to refer only to the namespace to which it already
1715 refers.
1717 Therefore, if we encounter a second alias directive for the same
1718 alias, we can just ignore the second directive. */
1719 if (DECL_NAMESPACE_ALIAS (newdecl)
1720 && (DECL_NAMESPACE_ALIAS (newdecl)
1721 == DECL_NAMESPACE_ALIAS (olddecl)))
1722 return olddecl;
1723 /* [namespace.alias]
1725 A namespace-name or namespace-alias shall not be declared as
1726 the name of any other entity in the same declarative region.
1727 A namespace-name defined at global scope shall not be
1728 declared as the name of any other entity in any global scope
1729 of the program. */
1730 error ("conflicting declaration of namespace %qD", newdecl);
1731 inform (DECL_SOURCE_LOCATION (olddecl),
1732 "previous declaration of namespace %qD here", olddecl);
1733 return error_mark_node;
1735 else
1737 const char *errmsg = redeclaration_error_message (newdecl, olddecl);
1738 if (errmsg)
1740 error_at (DECL_SOURCE_LOCATION (newdecl), errmsg, newdecl);
1741 if (DECL_NAME (olddecl) != NULL_TREE)
1742 inform (input_location,
1743 (DECL_INITIAL (olddecl) && namespace_bindings_p ())
1744 ? G_("%q+#D previously defined here")
1745 : G_("%q+#D previously declared here"), olddecl);
1746 return error_mark_node;
1748 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1749 && DECL_INITIAL (olddecl) != NULL_TREE
1750 && !prototype_p (TREE_TYPE (olddecl))
1751 && prototype_p (TREE_TYPE (newdecl)))
1753 /* Prototype decl follows defn w/o prototype. */
1754 if (warning_at (DECL_SOURCE_LOCATION (newdecl), 0,
1755 "prototype specified for %q#D", newdecl))
1756 inform (DECL_SOURCE_LOCATION (olddecl),
1757 "previous non-prototype definition here");
1759 else if (VAR_OR_FUNCTION_DECL_P (olddecl)
1760 && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl))
1762 /* [dcl.link]
1763 If two declarations of the same function or object
1764 specify different linkage-specifications ..., the program
1765 is ill-formed.... Except for functions with C++ linkage,
1766 a function declaration without a linkage specification
1767 shall not precede the first linkage specification for
1768 that function. A function can be declared without a
1769 linkage specification after an explicit linkage
1770 specification has been seen; the linkage explicitly
1771 specified in the earlier declaration is not affected by
1772 such a function declaration.
1774 DR 563 raises the question why the restrictions on
1775 functions should not also apply to objects. Older
1776 versions of G++ silently ignore the linkage-specification
1777 for this example:
1779 namespace N {
1780 extern int i;
1781 extern "C" int i;
1784 which is clearly wrong. Therefore, we now treat objects
1785 like functions. */
1786 if (current_lang_depth () == 0)
1788 /* There is no explicit linkage-specification, so we use
1789 the linkage from the previous declaration. */
1790 if (!DECL_LANG_SPECIFIC (newdecl))
1791 retrofit_lang_decl (newdecl);
1792 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
1794 else
1796 error ("conflicting declaration of %q#D with %qL linkage",
1797 newdecl, DECL_LANGUAGE (newdecl));
1798 inform (DECL_SOURCE_LOCATION (olddecl),
1799 "previous declaration with %qL linkage",
1800 DECL_LANGUAGE (olddecl));
1804 if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl))
1806 else if (TREE_CODE (olddecl) == FUNCTION_DECL)
1808 /* Note: free functions, as TEMPLATE_DECLs, are handled below. */
1809 if (DECL_FUNCTION_MEMBER_P (olddecl)
1810 && (/* grokfndecl passes member function templates too
1811 as FUNCTION_DECLs. */
1812 DECL_TEMPLATE_INFO (olddecl)
1813 /* C++11 8.3.6/6.
1814 Default arguments for a member function of a class
1815 template shall be specified on the initial declaration
1816 of the member function within the class template. */
1817 || CLASSTYPE_TEMPLATE_INFO (CP_DECL_CONTEXT (olddecl))))
1818 check_redeclaration_no_default_args (newdecl);
1819 else
1821 tree t1 = FUNCTION_FIRST_USER_PARMTYPE (olddecl);
1822 tree t2 = FUNCTION_FIRST_USER_PARMTYPE (newdecl);
1823 int i = 1;
1825 for (; t1 && t1 != void_list_node;
1826 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++)
1827 if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
1829 if (1 == simple_cst_equal (TREE_PURPOSE (t1),
1830 TREE_PURPOSE (t2)))
1832 if (permerror (input_location,
1833 "default argument given for parameter "
1834 "%d of %q#D", i, newdecl))
1835 inform (DECL_SOURCE_LOCATION (olddecl),
1836 "previous specification in %q#D here",
1837 olddecl);
1839 else
1841 error ("default argument given for parameter %d "
1842 "of %q#D", i, newdecl);
1843 inform (DECL_SOURCE_LOCATION (olddecl),
1844 "previous specification in %q#D here",
1845 olddecl);
1852 /* Do not merge an implicit typedef with an explicit one. In:
1854 class A;
1856 typedef class A A __attribute__ ((foo));
1858 the attribute should apply only to the typedef. */
1859 if (TREE_CODE (olddecl) == TYPE_DECL
1860 && (DECL_IMPLICIT_TYPEDEF_P (olddecl)
1861 || DECL_IMPLICIT_TYPEDEF_P (newdecl)))
1862 return NULL_TREE;
1864 /* If new decl is `static' and an `extern' was seen previously,
1865 warn about it. */
1866 warn_extern_redeclared_static (newdecl, olddecl);
1868 if (!validate_constexpr_redeclaration (olddecl, newdecl))
1869 return error_mark_node;
1871 /* We have committed to returning 1 at this point. */
1872 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1874 /* Now that functions must hold information normally held
1875 by field decls, there is extra work to do so that
1876 declaration information does not get destroyed during
1877 definition. */
1878 if (DECL_VINDEX (olddecl))
1879 DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl);
1880 if (DECL_CONTEXT (olddecl))
1881 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1882 DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl);
1883 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1884 DECL_PURE_VIRTUAL_P (newdecl) |= DECL_PURE_VIRTUAL_P (olddecl);
1885 DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl);
1886 DECL_INVALID_OVERRIDER_P (newdecl) |= DECL_INVALID_OVERRIDER_P (olddecl);
1887 DECL_FINAL_P (newdecl) |= DECL_FINAL_P (olddecl);
1888 DECL_OVERRIDE_P (newdecl) |= DECL_OVERRIDE_P (olddecl);
1889 DECL_THIS_STATIC (newdecl) |= DECL_THIS_STATIC (olddecl);
1890 if (DECL_OVERLOADED_OPERATOR_P (olddecl) != ERROR_MARK)
1891 SET_OVERLOADED_OPERATOR_CODE
1892 (newdecl, DECL_OVERLOADED_OPERATOR_P (olddecl));
1893 new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE;
1895 /* Optionally warn about more than one declaration for the same
1896 name, but don't warn about a function declaration followed by a
1897 definition. */
1898 if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl)
1899 && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE)
1900 /* Don't warn about extern decl followed by definition. */
1901 && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl))
1902 /* Don't warn about friends, let add_friend take care of it. */
1903 && ! (newdecl_is_friend || DECL_FRIEND_P (olddecl))
1904 /* Don't warn about declaration followed by specialization. */
1905 && (! DECL_TEMPLATE_SPECIALIZATION (newdecl)
1906 || DECL_TEMPLATE_SPECIALIZATION (olddecl)))
1908 if (warning (OPT_Wredundant_decls,
1909 "redundant redeclaration of %qD in same scope",
1910 newdecl))
1911 inform (DECL_SOURCE_LOCATION (olddecl),
1912 "previous declaration of %qD", olddecl);
1915 if (!(DECL_TEMPLATE_INSTANTIATION (olddecl)
1916 && DECL_TEMPLATE_SPECIALIZATION (newdecl)))
1918 if (DECL_DELETED_FN (newdecl))
1920 error ("deleted definition of %qD", newdecl);
1921 inform (DECL_SOURCE_LOCATION (olddecl),
1922 "previous declaration of %qD", olddecl);
1924 DECL_DELETED_FN (newdecl) |= DECL_DELETED_FN (olddecl);
1928 /* Deal with C++: must preserve virtual function table size. */
1929 if (TREE_CODE (olddecl) == TYPE_DECL)
1931 tree newtype = TREE_TYPE (newdecl);
1932 tree oldtype = TREE_TYPE (olddecl);
1934 if (newtype != error_mark_node && oldtype != error_mark_node
1935 && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype))
1936 CLASSTYPE_FRIEND_CLASSES (newtype)
1937 = CLASSTYPE_FRIEND_CLASSES (oldtype);
1939 DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl);
1942 /* Copy all the DECL_... slots specified in the new decl
1943 except for any that we copy here from the old type. */
1944 DECL_ATTRIBUTES (newdecl)
1945 = (*targetm.merge_decl_attributes) (olddecl, newdecl);
1947 if (DECL_DECLARES_FUNCTION_P (olddecl) && DECL_DECLARES_FUNCTION_P (newdecl))
1949 olddecl_friend = DECL_FRIEND_P (olddecl);
1950 hidden_friend = (DECL_ANTICIPATED (olddecl)
1951 && DECL_HIDDEN_FRIEND_P (olddecl)
1952 && newdecl_is_friend);
1953 if (!hidden_friend)
1955 DECL_ANTICIPATED (olddecl) = 0;
1956 DECL_HIDDEN_FRIEND_P (olddecl) = 0;
1960 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1962 tree old_result;
1963 tree new_result;
1964 old_result = DECL_TEMPLATE_RESULT (olddecl);
1965 new_result = DECL_TEMPLATE_RESULT (newdecl);
1966 TREE_TYPE (olddecl) = TREE_TYPE (old_result);
1967 DECL_TEMPLATE_SPECIALIZATIONS (olddecl)
1968 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (olddecl),
1969 DECL_TEMPLATE_SPECIALIZATIONS (newdecl));
1971 DECL_ATTRIBUTES (old_result)
1972 = (*targetm.merge_decl_attributes) (old_result, new_result);
1974 if (DECL_FUNCTION_TEMPLATE_P (newdecl))
1976 /* Per C++11 8.3.6/4, default arguments cannot be added in later
1977 declarations of a function template. */
1978 if (DECL_SOURCE_LOCATION (newdecl)
1979 != DECL_SOURCE_LOCATION (olddecl))
1980 check_redeclaration_no_default_args (newdecl);
1982 check_default_args (newdecl);
1984 if (GNU_INLINE_P (old_result) != GNU_INLINE_P (new_result)
1985 && DECL_INITIAL (new_result))
1987 if (DECL_INITIAL (old_result))
1988 DECL_UNINLINABLE (old_result) = 1;
1989 else
1990 DECL_UNINLINABLE (old_result) = DECL_UNINLINABLE (new_result);
1991 DECL_EXTERNAL (old_result) = DECL_EXTERNAL (new_result);
1992 DECL_NOT_REALLY_EXTERN (old_result)
1993 = DECL_NOT_REALLY_EXTERN (new_result);
1994 DECL_INTERFACE_KNOWN (old_result)
1995 = DECL_INTERFACE_KNOWN (new_result);
1996 DECL_DECLARED_INLINE_P (old_result)
1997 = DECL_DECLARED_INLINE_P (new_result);
1998 DECL_DISREGARD_INLINE_LIMITS (old_result)
1999 |= DECL_DISREGARD_INLINE_LIMITS (new_result);
2002 else
2004 DECL_DECLARED_INLINE_P (old_result)
2005 |= DECL_DECLARED_INLINE_P (new_result);
2006 DECL_DISREGARD_INLINE_LIMITS (old_result)
2007 |= DECL_DISREGARD_INLINE_LIMITS (new_result);
2008 check_redeclaration_exception_specification (newdecl, olddecl);
2012 /* If the new declaration is a definition, update the file and
2013 line information on the declaration, and also make
2014 the old declaration the same definition. */
2015 if (DECL_INITIAL (new_result) != NULL_TREE)
2017 DECL_SOURCE_LOCATION (olddecl)
2018 = DECL_SOURCE_LOCATION (old_result)
2019 = DECL_SOURCE_LOCATION (newdecl);
2020 DECL_INITIAL (old_result) = DECL_INITIAL (new_result);
2021 if (DECL_FUNCTION_TEMPLATE_P (newdecl))
2023 tree parm;
2024 DECL_ARGUMENTS (old_result)
2025 = DECL_ARGUMENTS (new_result);
2026 for (parm = DECL_ARGUMENTS (old_result); parm;
2027 parm = DECL_CHAIN (parm))
2028 DECL_CONTEXT (parm) = old_result;
2032 return olddecl;
2035 if (types_match)
2037 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2038 check_redeclaration_exception_specification (newdecl, olddecl);
2040 /* Automatically handles default parameters. */
2041 tree oldtype = TREE_TYPE (olddecl);
2042 tree newtype;
2044 /* For typedefs use the old type, as the new type's DECL_NAME points
2045 at newdecl, which will be ggc_freed. */
2046 if (TREE_CODE (newdecl) == TYPE_DECL)
2047 newtype = oldtype;
2048 else
2049 /* Merge the data types specified in the two decls. */
2050 newtype = merge_types (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
2052 if (VAR_P (newdecl))
2054 DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl);
2055 DECL_INITIALIZED_P (newdecl) |= DECL_INITIALIZED_P (olddecl);
2056 DECL_NONTRIVIALLY_INITIALIZED_P (newdecl)
2057 |= DECL_NONTRIVIALLY_INITIALIZED_P (olddecl);
2058 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (newdecl)
2059 |= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (olddecl);
2061 /* Merge the threadprivate attribute from OLDDECL into NEWDECL. */
2062 if (DECL_LANG_SPECIFIC (olddecl)
2063 && CP_DECL_THREADPRIVATE_P (olddecl))
2065 /* Allocate a LANG_SPECIFIC structure for NEWDECL, if needed. */
2066 if (!DECL_LANG_SPECIFIC (newdecl))
2067 retrofit_lang_decl (newdecl);
2069 CP_DECL_THREADPRIVATE_P (newdecl) = 1;
2073 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype;
2075 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2076 check_default_args (newdecl);
2078 /* Lay the type out, unless already done. */
2079 if (! same_type_p (newtype, oldtype)
2080 && TREE_TYPE (newdecl) != error_mark_node
2081 && !(processing_template_decl && uses_template_parms (newdecl)))
2082 layout_type (TREE_TYPE (newdecl));
2084 if ((VAR_P (newdecl)
2085 || TREE_CODE (newdecl) == PARM_DECL
2086 || TREE_CODE (newdecl) == RESULT_DECL
2087 || TREE_CODE (newdecl) == FIELD_DECL
2088 || TREE_CODE (newdecl) == TYPE_DECL)
2089 && !(processing_template_decl && uses_template_parms (newdecl)))
2090 layout_decl (newdecl, 0);
2092 /* Merge the type qualifiers. */
2093 if (TREE_READONLY (newdecl))
2094 TREE_READONLY (olddecl) = 1;
2095 if (TREE_THIS_VOLATILE (newdecl))
2096 TREE_THIS_VOLATILE (olddecl) = 1;
2097 if (TREE_NOTHROW (newdecl))
2098 TREE_NOTHROW (olddecl) = 1;
2100 /* Merge deprecatedness. */
2101 if (TREE_DEPRECATED (newdecl))
2102 TREE_DEPRECATED (olddecl) = 1;
2104 /* Preserve function specific target and optimization options */
2105 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2107 if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
2108 && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
2109 DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
2110 = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
2112 if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
2113 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
2114 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
2115 = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
2118 /* Merge the initialization information. */
2119 if (DECL_INITIAL (newdecl) == NULL_TREE
2120 && DECL_INITIAL (olddecl) != NULL_TREE)
2122 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2123 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2124 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2126 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
2127 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
2131 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2133 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
2134 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
2135 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
2136 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
2137 TREE_NOTHROW (newdecl) |= TREE_NOTHROW (olddecl);
2138 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
2139 DECL_IS_OPERATOR_NEW (newdecl) |= DECL_IS_OPERATOR_NEW (olddecl);
2140 DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
2141 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
2142 DECL_LOOPING_CONST_OR_PURE_P (newdecl)
2143 |= DECL_LOOPING_CONST_OR_PURE_P (olddecl);
2144 /* Keep the old RTL. */
2145 COPY_DECL_RTL (olddecl, newdecl);
2147 else if (VAR_P (newdecl)
2148 && (DECL_SIZE (olddecl) || !DECL_SIZE (newdecl)))
2150 /* Keep the old RTL. We cannot keep the old RTL if the old
2151 declaration was for an incomplete object and the new
2152 declaration is not since many attributes of the RTL will
2153 change. */
2154 COPY_DECL_RTL (olddecl, newdecl);
2157 /* If cannot merge, then use the new type and qualifiers,
2158 and don't preserve the old rtl. */
2159 else
2161 /* Clean out any memory we had of the old declaration. */
2162 tree oldstatic = value_member (olddecl, static_aggregates);
2163 if (oldstatic)
2164 TREE_VALUE (oldstatic) = error_mark_node;
2166 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
2167 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
2168 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
2169 TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
2172 /* Merge the storage class information. */
2173 merge_weak (newdecl, olddecl);
2175 DECL_DEFER_OUTPUT (newdecl) |= DECL_DEFER_OUTPUT (olddecl);
2176 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
2177 TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl);
2178 if (! DECL_EXTERNAL (olddecl))
2179 DECL_EXTERNAL (newdecl) = 0;
2181 new_template_info = NULL_TREE;
2182 if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl))
2184 bool new_redefines_gnu_inline = false;
2186 if (new_defines_function
2187 && ((DECL_INTERFACE_KNOWN (olddecl)
2188 && TREE_CODE (olddecl) == FUNCTION_DECL)
2189 || (TREE_CODE (olddecl) == TEMPLATE_DECL
2190 && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl))
2191 == FUNCTION_DECL))))
2193 tree fn = olddecl;
2195 if (TREE_CODE (fn) == TEMPLATE_DECL)
2196 fn = DECL_TEMPLATE_RESULT (olddecl);
2198 new_redefines_gnu_inline = GNU_INLINE_P (fn) && DECL_INITIAL (fn);
2201 if (!new_redefines_gnu_inline)
2203 DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl);
2204 DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl);
2205 DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl);
2207 DECL_TEMPLATE_INSTANTIATED (newdecl)
2208 |= DECL_TEMPLATE_INSTANTIATED (olddecl);
2209 DECL_ODR_USED (newdecl) |= DECL_ODR_USED (olddecl);
2211 /* If the OLDDECL is an instantiation and/or specialization,
2212 then the NEWDECL must be too. But, it may not yet be marked
2213 as such if the caller has created NEWDECL, but has not yet
2214 figured out that it is a redeclaration. */
2215 if (!DECL_USE_TEMPLATE (newdecl))
2216 DECL_USE_TEMPLATE (newdecl) = DECL_USE_TEMPLATE (olddecl);
2218 /* Don't really know how much of the language-specific
2219 values we should copy from old to new. */
2220 DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl);
2221 DECL_REPO_AVAILABLE_P (newdecl) = DECL_REPO_AVAILABLE_P (olddecl);
2222 DECL_INITIALIZED_IN_CLASS_P (newdecl)
2223 |= DECL_INITIALIZED_IN_CLASS_P (olddecl);
2225 if (LANG_DECL_HAS_MIN (newdecl))
2227 DECL_LANG_SPECIFIC (newdecl)->u.min.u2 =
2228 DECL_LANG_SPECIFIC (olddecl)->u.min.u2;
2229 if (DECL_TEMPLATE_INFO (newdecl))
2231 new_template_info = DECL_TEMPLATE_INFO (newdecl);
2232 if (DECL_TEMPLATE_INSTANTIATION (olddecl)
2233 && DECL_TEMPLATE_SPECIALIZATION (newdecl))
2234 /* Remember the presence of explicit specialization args. */
2235 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (olddecl))
2236 = TINFO_USED_TEMPLATE_ID (new_template_info);
2238 DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl);
2240 /* Only functions have these fields. */
2241 if (DECL_DECLARES_FUNCTION_P (newdecl))
2243 DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl);
2244 DECL_BEFRIENDING_CLASSES (newdecl)
2245 = chainon (DECL_BEFRIENDING_CLASSES (newdecl),
2246 DECL_BEFRIENDING_CLASSES (olddecl));
2247 /* DECL_THUNKS is only valid for virtual functions,
2248 otherwise it is a DECL_FRIEND_CONTEXT. */
2249 if (DECL_VIRTUAL_P (newdecl))
2250 SET_DECL_THUNKS (newdecl, DECL_THUNKS (olddecl));
2252 /* Only variables have this field. */
2253 else if (VAR_P (newdecl)
2254 && VAR_HAD_UNKNOWN_BOUND (olddecl))
2255 SET_VAR_HAD_UNKNOWN_BOUND (newdecl);
2258 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2260 tree parm;
2262 /* Merge parameter attributes. */
2263 tree oldarg, newarg;
2264 for (oldarg = DECL_ARGUMENTS(olddecl),
2265 newarg = DECL_ARGUMENTS(newdecl);
2266 oldarg && newarg;
2267 oldarg = DECL_CHAIN(oldarg), newarg = DECL_CHAIN(newarg)) {
2268 DECL_ATTRIBUTES (newarg)
2269 = (*targetm.merge_decl_attributes) (oldarg, newarg);
2270 DECL_ATTRIBUTES (oldarg) = DECL_ATTRIBUTES (newarg);
2273 if (DECL_TEMPLATE_INSTANTIATION (olddecl)
2274 && !DECL_TEMPLATE_INSTANTIATION (newdecl))
2276 /* If newdecl is not a specialization, then it is not a
2277 template-related function at all. And that means that we
2278 should have exited above, returning 0. */
2279 gcc_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl));
2281 if (DECL_ODR_USED (olddecl))
2282 /* From [temp.expl.spec]:
2284 If a template, a member template or the member of a class
2285 template is explicitly specialized then that
2286 specialization shall be declared before the first use of
2287 that specialization that would cause an implicit
2288 instantiation to take place, in every translation unit in
2289 which such a use occurs. */
2290 error ("explicit specialization of %qD after first use",
2291 olddecl);
2293 SET_DECL_TEMPLATE_SPECIALIZATION (olddecl);
2294 DECL_COMDAT (newdecl) = (TREE_PUBLIC (newdecl)
2295 && DECL_DECLARED_INLINE_P (newdecl));
2297 /* Don't propagate visibility from the template to the
2298 specialization here. We'll do that in determine_visibility if
2299 appropriate. */
2300 DECL_VISIBILITY_SPECIFIED (olddecl) = 0;
2302 /* [temp.expl.spec/14] We don't inline explicit specialization
2303 just because the primary template says so. */
2305 /* But still keep DECL_DISREGARD_INLINE_LIMITS in sync with
2306 the always_inline attribute. */
2307 if (DECL_DISREGARD_INLINE_LIMITS (olddecl)
2308 && !DECL_DISREGARD_INLINE_LIMITS (newdecl))
2310 if (DECL_DECLARED_INLINE_P (newdecl))
2311 DECL_DISREGARD_INLINE_LIMITS (newdecl) = true;
2312 else
2313 DECL_ATTRIBUTES (newdecl)
2314 = remove_attribute ("always_inline",
2315 DECL_ATTRIBUTES (newdecl));
2318 else if (new_defines_function && DECL_INITIAL (olddecl))
2320 /* Never inline re-defined extern inline functions.
2321 FIXME: this could be better handled by keeping both
2322 function as separate declarations. */
2323 DECL_UNINLINABLE (newdecl) = 1;
2325 else
2327 if (DECL_PENDING_INLINE_INFO (newdecl) == 0)
2328 DECL_PENDING_INLINE_INFO (newdecl) = DECL_PENDING_INLINE_INFO (olddecl);
2330 DECL_DECLARED_INLINE_P (newdecl) |= DECL_DECLARED_INLINE_P (olddecl);
2332 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
2333 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
2335 DECL_DISREGARD_INLINE_LIMITS (newdecl)
2336 = DECL_DISREGARD_INLINE_LIMITS (olddecl)
2337 = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
2338 || DECL_DISREGARD_INLINE_LIMITS (olddecl));
2341 /* Preserve abstractness on cloned [cd]tors. */
2342 DECL_ABSTRACT_P (newdecl) = DECL_ABSTRACT_P (olddecl);
2344 /* Update newdecl's parms to point at olddecl. */
2345 for (parm = DECL_ARGUMENTS (newdecl); parm;
2346 parm = DECL_CHAIN (parm))
2347 DECL_CONTEXT (parm) = olddecl;
2349 if (! types_match)
2351 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
2352 COPY_DECL_ASSEMBLER_NAME (newdecl, olddecl);
2353 COPY_DECL_RTL (newdecl, olddecl);
2355 if (! types_match || new_defines_function)
2357 /* These need to be copied so that the names are available.
2358 Note that if the types do match, we'll preserve inline
2359 info and other bits, but if not, we won't. */
2360 DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl);
2361 DECL_RESULT (olddecl) = DECL_RESULT (newdecl);
2363 /* If redeclaring a builtin function, it stays built in
2364 if newdecl is a gnu_inline definition, or if newdecl is just
2365 a declaration. */
2366 if (DECL_BUILT_IN (olddecl)
2367 && (new_defines_function ? GNU_INLINE_P (newdecl) : types_match))
2369 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
2370 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
2371 /* If we're keeping the built-in definition, keep the rtl,
2372 regardless of declaration matches. */
2373 COPY_DECL_RTL (olddecl, newdecl);
2374 if (DECL_BUILT_IN_CLASS (newdecl) == BUILT_IN_NORMAL)
2376 enum built_in_function fncode = DECL_FUNCTION_CODE (newdecl);
2377 switch (fncode)
2379 /* If a compatible prototype of these builtin functions
2380 is seen, assume the runtime implements it with the
2381 expected semantics. */
2382 case BUILT_IN_STPCPY:
2383 if (builtin_decl_explicit_p (fncode))
2384 set_builtin_decl_implicit_p (fncode, true);
2385 break;
2386 default:
2387 if (builtin_decl_explicit_p (fncode))
2388 set_builtin_decl_declared_p (fncode, true);
2389 break;
2393 if (new_defines_function)
2394 /* If defining a function declared with other language
2395 linkage, use the previously declared language linkage. */
2396 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
2397 else if (types_match)
2399 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
2400 /* Don't clear out the arguments if we're just redeclaring a
2401 function. */
2402 if (DECL_ARGUMENTS (olddecl))
2403 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
2406 else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
2407 NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl);
2409 /* Now preserve various other info from the definition. */
2410 TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl);
2411 TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl);
2412 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
2413 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
2415 /* Warn about conflicting visibility specifications. */
2416 if (DECL_VISIBILITY_SPECIFIED (olddecl)
2417 && DECL_VISIBILITY_SPECIFIED (newdecl)
2418 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
2420 if (warning_at (DECL_SOURCE_LOCATION (newdecl), OPT_Wattributes,
2421 "%qD: visibility attribute ignored because it "
2422 "conflicts with previous declaration", newdecl))
2423 inform (DECL_SOURCE_LOCATION (olddecl),
2424 "previous declaration of %qD", olddecl);
2426 /* Choose the declaration which specified visibility. */
2427 if (DECL_VISIBILITY_SPECIFIED (olddecl))
2429 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
2430 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
2432 /* Init priority used to be merged from newdecl to olddecl by the memcpy,
2433 so keep this behavior. */
2434 if (VAR_P (newdecl) && DECL_HAS_INIT_PRIORITY_P (newdecl))
2436 SET_DECL_INIT_PRIORITY (olddecl, DECL_INIT_PRIORITY (newdecl));
2437 DECL_HAS_INIT_PRIORITY_P (olddecl) = 1;
2439 /* Likewise for DECL_ALIGN, DECL_USER_ALIGN and DECL_PACKED. */
2440 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
2442 DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
2443 DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
2445 DECL_USER_ALIGN (olddecl) = DECL_USER_ALIGN (newdecl);
2446 if (TREE_CODE (newdecl) == FIELD_DECL)
2447 DECL_PACKED (olddecl) = DECL_PACKED (newdecl);
2449 /* The DECL_LANG_SPECIFIC information in OLDDECL will be replaced
2450 with that from NEWDECL below. */
2451 if (DECL_LANG_SPECIFIC (olddecl))
2453 gcc_assert (DECL_LANG_SPECIFIC (olddecl)
2454 != DECL_LANG_SPECIFIC (newdecl));
2455 ggc_free (DECL_LANG_SPECIFIC (olddecl));
2458 /* Merge the USED information. */
2459 if (TREE_USED (olddecl))
2460 TREE_USED (newdecl) = 1;
2461 else if (TREE_USED (newdecl))
2462 TREE_USED (olddecl) = 1;
2463 if (VAR_P (newdecl))
2465 if (DECL_READ_P (olddecl))
2466 DECL_READ_P (newdecl) = 1;
2467 else if (DECL_READ_P (newdecl))
2468 DECL_READ_P (olddecl) = 1;
2470 if (DECL_PRESERVE_P (olddecl))
2471 DECL_PRESERVE_P (newdecl) = 1;
2472 else if (DECL_PRESERVE_P (newdecl))
2473 DECL_PRESERVE_P (olddecl) = 1;
2475 /* Merge the DECL_FUNCTION_VERSIONED information. newdecl will be copied
2476 to olddecl and deleted. */
2477 if (TREE_CODE (newdecl) == FUNCTION_DECL
2478 && DECL_FUNCTION_VERSIONED (olddecl))
2480 /* Set the flag for newdecl so that it gets copied to olddecl. */
2481 DECL_FUNCTION_VERSIONED (newdecl) = 1;
2482 /* newdecl will be purged after copying to olddecl and is no longer
2483 a version. */
2484 cgraph_node::delete_function_version (newdecl);
2487 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2489 int function_size;
2490 struct symtab_node *snode = symtab_node::get (olddecl);
2492 function_size = sizeof (struct tree_decl_common);
2494 memcpy ((char *) olddecl + sizeof (struct tree_common),
2495 (char *) newdecl + sizeof (struct tree_common),
2496 function_size - sizeof (struct tree_common));
2498 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2499 (char *) newdecl + sizeof (struct tree_decl_common),
2500 sizeof (struct tree_function_decl) - sizeof (struct tree_decl_common));
2502 /* Preserve symtab node mapping. */
2503 olddecl->decl_with_vis.symtab_node = snode;
2505 if (new_template_info)
2506 /* If newdecl is a template instantiation, it is possible that
2507 the following sequence of events has occurred:
2509 o A friend function was declared in a class template. The
2510 class template was instantiated.
2512 o The instantiation of the friend declaration was
2513 recorded on the instantiation list, and is newdecl.
2515 o Later, however, instantiate_class_template called pushdecl
2516 on the newdecl to perform name injection. But, pushdecl in
2517 turn called duplicate_decls when it discovered that another
2518 declaration of a global function with the same name already
2519 existed.
2521 o Here, in duplicate_decls, we decided to clobber newdecl.
2523 If we're going to do that, we'd better make sure that
2524 olddecl, and not newdecl, is on the list of
2525 instantiations so that if we try to do the instantiation
2526 again we won't get the clobbered declaration. */
2527 reregister_specialization (newdecl,
2528 new_template_info,
2529 olddecl);
2531 else
2533 size_t size = tree_code_size (TREE_CODE (newdecl));
2535 memcpy ((char *) olddecl + sizeof (struct tree_common),
2536 (char *) newdecl + sizeof (struct tree_common),
2537 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
2538 switch (TREE_CODE (newdecl))
2540 case LABEL_DECL:
2541 case VAR_DECL:
2542 case RESULT_DECL:
2543 case PARM_DECL:
2544 case FIELD_DECL:
2545 case TYPE_DECL:
2546 case CONST_DECL:
2548 struct symtab_node *snode = NULL;
2550 if (TREE_CODE (newdecl) == VAR_DECL
2551 && (TREE_STATIC (olddecl) || TREE_PUBLIC (olddecl) || DECL_EXTERNAL (olddecl)))
2552 snode = symtab_node::get (olddecl);
2553 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2554 (char *) newdecl + sizeof (struct tree_decl_common),
2555 size - sizeof (struct tree_decl_common)
2556 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
2557 if (TREE_CODE (newdecl) == VAR_DECL)
2558 olddecl->decl_with_vis.symtab_node = snode;
2560 break;
2561 default:
2562 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2563 (char *) newdecl + sizeof (struct tree_decl_common),
2564 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common)
2565 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
2566 break;
2570 if (TREE_CODE (newdecl) == FUNCTION_DECL
2571 || TREE_CODE (newdecl) == VAR_DECL)
2573 if (DECL_EXTERNAL (olddecl)
2574 || TREE_PUBLIC (olddecl)
2575 || TREE_STATIC (olddecl))
2577 /* Merge the section attribute.
2578 We want to issue an error if the sections conflict but that must be
2579 done later in decl_attributes since we are called before attributes
2580 are assigned. */
2581 if (DECL_SECTION_NAME (newdecl) != NULL)
2582 set_decl_section_name (olddecl, DECL_SECTION_NAME (newdecl));
2584 if (DECL_ONE_ONLY (newdecl))
2586 struct symtab_node *oldsym, *newsym;
2587 if (TREE_CODE (olddecl) == FUNCTION_DECL)
2588 oldsym = cgraph_node::get_create (olddecl);
2589 else
2590 oldsym = varpool_node::get_create (olddecl);
2591 newsym = symtab_node::get (newdecl);
2592 oldsym->set_comdat_group (newsym->get_comdat_group ());
2596 if (TREE_CODE (newdecl) == VAR_DECL
2597 && DECL_THREAD_LOCAL_P (newdecl))
2598 set_decl_tls_model (olddecl, DECL_TLS_MODEL (newdecl));
2601 DECL_UID (olddecl) = olddecl_uid;
2602 if (olddecl_friend)
2603 DECL_FRIEND_P (olddecl) = 1;
2604 if (hidden_friend)
2606 DECL_ANTICIPATED (olddecl) = 1;
2607 DECL_HIDDEN_FRIEND_P (olddecl) = 1;
2610 /* NEWDECL contains the merged attribute lists.
2611 Update OLDDECL to be the same. */
2612 DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
2614 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
2615 so that encode_section_info has a chance to look at the new decl
2616 flags and attributes. */
2617 if (DECL_RTL_SET_P (olddecl)
2618 && (TREE_CODE (olddecl) == FUNCTION_DECL
2619 || (VAR_P (olddecl)
2620 && TREE_STATIC (olddecl))))
2621 make_decl_rtl (olddecl);
2623 /* The NEWDECL will no longer be needed. Because every out-of-class
2624 declaration of a member results in a call to duplicate_decls,
2625 freeing these nodes represents in a significant savings.
2627 Before releasing the node, be sore to remove function from symbol
2628 table that might have been inserted there to record comdat group.
2629 Be sure to however do not free DECL_STRUCT_FUNCTION becuase this
2630 structure is shared in between newdecl and oldecl. */
2631 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2632 DECL_STRUCT_FUNCTION (newdecl) = NULL;
2633 if (TREE_CODE (newdecl) == FUNCTION_DECL
2634 || TREE_CODE (newdecl) == VAR_DECL)
2636 struct symtab_node *snode = symtab_node::get (newdecl);
2637 if (snode)
2638 snode->remove ();
2641 /* Remove the associated constraints for newdecl, if any, before
2642 reclaiming memory. */
2643 if (flag_concepts)
2644 remove_constraints (newdecl);
2646 ggc_free (newdecl);
2648 return olddecl;
2651 /* Return zero if the declaration NEWDECL is valid
2652 when the declaration OLDDECL (assumed to be for the same name)
2653 has already been seen.
2654 Otherwise return an error message format string with a %s
2655 where the identifier should go. */
2657 static const char *
2658 redeclaration_error_message (tree newdecl, tree olddecl)
2660 if (TREE_CODE (newdecl) == TYPE_DECL)
2662 /* Because C++ can put things into name space for free,
2663 constructs like "typedef struct foo { ... } foo"
2664 would look like an erroneous redeclaration. */
2665 if (same_type_p (TREE_TYPE (newdecl), TREE_TYPE (olddecl)))
2666 return NULL;
2667 else
2668 return G_("redefinition of %q#D");
2670 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
2672 /* If this is a pure function, its olddecl will actually be
2673 the original initialization to `0' (which we force to call
2674 abort()). Don't complain about redefinition in this case. */
2675 if (DECL_LANG_SPECIFIC (olddecl) && DECL_PURE_VIRTUAL_P (olddecl)
2676 && DECL_INITIAL (olddecl) == NULL_TREE)
2677 return NULL;
2679 /* If both functions come from different namespaces, this is not
2680 a redeclaration - this is a conflict with a used function. */
2681 if (DECL_NAMESPACE_SCOPE_P (olddecl)
2682 && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl)
2683 && ! decls_match (olddecl, newdecl))
2684 return G_("%qD conflicts with used function");
2686 /* We'll complain about linkage mismatches in
2687 warn_extern_redeclared_static. */
2689 /* Defining the same name twice is no good. */
2690 if (DECL_INITIAL (olddecl) != NULL_TREE
2691 && DECL_INITIAL (newdecl) != NULL_TREE)
2693 if (DECL_NAME (olddecl) == NULL_TREE)
2694 return G_("%q#D not declared in class");
2695 else if (!GNU_INLINE_P (olddecl)
2696 || GNU_INLINE_P (newdecl))
2697 return G_("redefinition of %q#D");
2700 if (DECL_DECLARED_INLINE_P (olddecl) && DECL_DECLARED_INLINE_P (newdecl))
2702 bool olda = GNU_INLINE_P (olddecl);
2703 bool newa = GNU_INLINE_P (newdecl);
2705 if (olda != newa)
2707 if (newa)
2708 return G_("%q+D redeclared inline with "
2709 "%<gnu_inline%> attribute");
2710 else
2711 return G_("%q+D redeclared inline without "
2712 "%<gnu_inline%> attribute");
2716 check_abi_tag_redeclaration
2717 (olddecl, lookup_attribute ("abi_tag", DECL_ATTRIBUTES (olddecl)),
2718 lookup_attribute ("abi_tag", DECL_ATTRIBUTES (newdecl)));
2720 return NULL;
2722 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2724 tree nt, ot;
2726 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
2728 if (COMPLETE_TYPE_P (TREE_TYPE (newdecl))
2729 && COMPLETE_TYPE_P (TREE_TYPE (olddecl)))
2730 return G_("redefinition of %q#D");
2731 return NULL;
2734 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) != FUNCTION_DECL
2735 || (DECL_TEMPLATE_RESULT (newdecl)
2736 == DECL_TEMPLATE_RESULT (olddecl)))
2737 return NULL;
2739 nt = DECL_TEMPLATE_RESULT (newdecl);
2740 if (DECL_TEMPLATE_INFO (nt))
2741 nt = DECL_TEMPLATE_RESULT (template_for_substitution (nt));
2742 ot = DECL_TEMPLATE_RESULT (olddecl);
2743 if (DECL_TEMPLATE_INFO (ot))
2744 ot = DECL_TEMPLATE_RESULT (template_for_substitution (ot));
2745 if (DECL_INITIAL (nt) && DECL_INITIAL (ot)
2746 && (!GNU_INLINE_P (ot) || GNU_INLINE_P (nt)))
2747 return G_("redefinition of %q#D");
2749 if (DECL_DECLARED_INLINE_P (ot) && DECL_DECLARED_INLINE_P (nt))
2751 bool olda = GNU_INLINE_P (ot);
2752 bool newa = GNU_INLINE_P (nt);
2754 if (olda != newa)
2756 if (newa)
2757 return G_("%q+D redeclared inline with "
2758 "%<gnu_inline%> attribute");
2759 else
2760 return G_("%q+D redeclared inline without "
2761 "%<gnu_inline%> attribute");
2765 /* Core issue #226 (C++0x):
2767 If a friend function template declaration specifies a
2768 default template-argument, that declaration shall be a
2769 definition and shall be the only declaration of the
2770 function template in the translation unit. */
2771 if ((cxx_dialect != cxx98)
2772 && TREE_CODE (ot) == FUNCTION_DECL && DECL_FRIEND_P (ot)
2773 && !check_default_tmpl_args (nt, DECL_TEMPLATE_PARMS (newdecl),
2774 /*is_primary=*/true,
2775 /*is_partial=*/false,
2776 /*is_friend_decl=*/2))
2777 return G_("redeclaration of friend %q#D "
2778 "may not have default template arguments");
2780 return NULL;
2782 else if (VAR_P (newdecl)
2783 && DECL_THREAD_LOCAL_P (newdecl) != DECL_THREAD_LOCAL_P (olddecl)
2784 && (! DECL_LANG_SPECIFIC (olddecl)
2785 || ! CP_DECL_THREADPRIVATE_P (olddecl)
2786 || DECL_THREAD_LOCAL_P (newdecl)))
2788 /* Only variables can be thread-local, and all declarations must
2789 agree on this property. */
2790 if (DECL_THREAD_LOCAL_P (newdecl))
2791 return G_("thread-local declaration of %q#D follows "
2792 "non-thread-local declaration");
2793 else
2794 return G_("non-thread-local declaration of %q#D follows "
2795 "thread-local declaration");
2797 else if (toplevel_bindings_p () || DECL_NAMESPACE_SCOPE_P (newdecl))
2799 /* The objects have been declared at namespace scope. If either
2800 is a member of an anonymous union, then this is an invalid
2801 redeclaration. For example:
2803 int i;
2804 union { int i; };
2806 is invalid. */
2807 if ((VAR_P (newdecl) && DECL_ANON_UNION_VAR_P (newdecl))
2808 || (VAR_P (olddecl) && DECL_ANON_UNION_VAR_P (olddecl)))
2809 return G_("redeclaration of %q#D");
2810 /* If at least one declaration is a reference, there is no
2811 conflict. For example:
2813 int i = 3;
2814 extern int i;
2816 is valid. */
2817 if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
2818 return NULL;
2819 /* Reject two definitions. */
2820 return G_("redefinition of %q#D");
2822 else
2824 /* Objects declared with block scope: */
2825 /* Reject two definitions, and reject a definition
2826 together with an external reference. */
2827 if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl)))
2828 return G_("redeclaration of %q#D");
2829 return NULL;
2833 /* Hash and equality functions for the named_label table. */
2835 hashval_t
2836 named_label_hasher::hash (named_label_entry *ent)
2838 return DECL_UID (ent->label_decl);
2841 bool
2842 named_label_hasher::equal (named_label_entry *a, named_label_entry *b)
2844 return a->label_decl == b->label_decl;
2847 /* Create a new label, named ID. */
2849 static tree
2850 make_label_decl (tree id, int local_p)
2852 struct named_label_entry *ent;
2853 tree decl;
2855 decl = build_decl (input_location, LABEL_DECL, id, void_type_node);
2857 DECL_CONTEXT (decl) = current_function_decl;
2858 DECL_MODE (decl) = VOIDmode;
2859 C_DECLARED_LABEL_FLAG (decl) = local_p;
2861 /* Say where one reference is to the label, for the sake of the
2862 error if it is not defined. */
2863 DECL_SOURCE_LOCATION (decl) = input_location;
2865 /* Record the fact that this identifier is bound to this label. */
2866 SET_IDENTIFIER_LABEL_VALUE (id, decl);
2868 /* Create the label htab for the function on demand. */
2869 if (!named_labels)
2870 named_labels = hash_table<named_label_hasher>::create_ggc (13);
2872 /* Record this label on the list of labels used in this function.
2873 We do this before calling make_label_decl so that we get the
2874 IDENTIFIER_LABEL_VALUE before the new label is declared. */
2875 ent = ggc_cleared_alloc<named_label_entry> ();
2876 ent->label_decl = decl;
2878 named_label_entry **slot = named_labels->find_slot (ent, INSERT);
2879 gcc_assert (*slot == NULL);
2880 *slot = ent;
2882 return decl;
2885 /* Look for a label named ID in the current function. If one cannot
2886 be found, create one. (We keep track of used, but undefined,
2887 labels, and complain about them at the end of a function.) */
2889 static tree
2890 lookup_label_1 (tree id)
2892 tree decl;
2894 /* You can't use labels at global scope. */
2895 if (current_function_decl == NULL_TREE)
2897 error ("label %qE referenced outside of any function", id);
2898 return NULL_TREE;
2901 /* See if we've already got this label. */
2902 decl = IDENTIFIER_LABEL_VALUE (id);
2903 if (decl != NULL_TREE && DECL_CONTEXT (decl) == current_function_decl)
2904 return decl;
2906 decl = make_label_decl (id, /*local_p=*/0);
2907 return decl;
2910 /* Wrapper for lookup_label_1. */
2912 tree
2913 lookup_label (tree id)
2915 tree ret;
2916 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
2917 ret = lookup_label_1 (id);
2918 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
2919 return ret;
2922 /* Declare a local label named ID. */
2924 tree
2925 declare_local_label (tree id)
2927 tree decl;
2928 cp_label_binding bind;
2930 /* Add a new entry to the SHADOWED_LABELS list so that when we leave
2931 this scope we can restore the old value of IDENTIFIER_TYPE_VALUE. */
2932 bind.prev_value = IDENTIFIER_LABEL_VALUE (id);
2934 decl = make_label_decl (id, /*local_p=*/1);
2935 bind.label = decl;
2936 vec_safe_push (current_binding_level->shadowed_labels, bind);
2938 return decl;
2941 /* Returns nonzero if it is ill-formed to jump past the declaration of
2942 DECL. Returns 2 if it's also a real problem. */
2944 static int
2945 decl_jump_unsafe (tree decl)
2947 /* [stmt.dcl]/3: A program that jumps from a point where a local variable
2948 with automatic storage duration is not in scope to a point where it is
2949 in scope is ill-formed unless the variable has scalar type, class type
2950 with a trivial default constructor and a trivial destructor, a
2951 cv-qualified version of one of these types, or an array of one of the
2952 preceding types and is declared without an initializer (8.5). */
2953 tree type = TREE_TYPE (decl);
2955 if (!VAR_P (decl) || TREE_STATIC (decl)
2956 || type == error_mark_node)
2957 return 0;
2959 if (DECL_NONTRIVIALLY_INITIALIZED_P (decl)
2960 || variably_modified_type_p (type, NULL_TREE))
2961 return 2;
2963 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
2964 return 1;
2966 return 0;
2969 /* A subroutine of check_previous_goto_1 to identify a branch to the user. */
2971 static bool
2972 identify_goto (tree decl, const location_t *locus)
2974 bool complained = (decl
2975 ? permerror (input_location, "jump to label %qD", decl)
2976 : permerror (input_location, "jump to case label"));
2977 if (complained && locus)
2978 inform (*locus, " from here");
2979 return complained;
2982 /* Check that a single previously seen jump to a newly defined label
2983 is OK. DECL is the LABEL_DECL or 0; LEVEL is the binding_level for
2984 the jump context; NAMES are the names in scope in LEVEL at the jump
2985 context; LOCUS is the source position of the jump or 0. Returns
2986 true if all is well. */
2988 static bool
2989 check_previous_goto_1 (tree decl, cp_binding_level* level, tree names,
2990 bool exited_omp, const location_t *locus)
2992 cp_binding_level *b;
2993 bool identified = false, complained = false;
2994 bool saw_eh = false, saw_omp = false;
2996 if (exited_omp)
2998 complained = identify_goto (decl, locus);
2999 if (complained)
3000 inform (input_location, " exits OpenMP structured block");
3001 identified = saw_omp = true;
3004 for (b = current_binding_level; b ; b = b->level_chain)
3006 tree new_decls, old_decls = (b == level ? names : NULL_TREE);
3008 for (new_decls = b->names; new_decls != old_decls;
3009 new_decls = (DECL_P (new_decls) ? DECL_CHAIN (new_decls)
3010 : TREE_CHAIN (new_decls)))
3012 int problem = decl_jump_unsafe (new_decls);
3013 if (! problem)
3014 continue;
3016 if (!identified)
3018 complained = identify_goto (decl, locus);
3019 identified = true;
3021 if (complained)
3023 if (problem > 1)
3024 inform (input_location,
3025 " crosses initialization of %q+#D", new_decls);
3026 else
3027 inform (input_location, " enters scope of %q+#D which has "
3028 "non-trivial destructor", new_decls);
3032 if (b == level)
3033 break;
3034 if ((b->kind == sk_try || b->kind == sk_catch) && !saw_eh)
3036 if (!identified)
3038 complained = identify_goto (decl, locus);
3039 identified = true;
3041 if (complained)
3043 if (b->kind == sk_try)
3044 inform (input_location, " enters try block");
3045 else
3046 inform (input_location, " enters catch block");
3048 saw_eh = true;
3050 if (b->kind == sk_omp && !saw_omp)
3052 if (!identified)
3054 complained = identify_goto (decl, locus);
3055 identified = true;
3057 if (complained)
3058 inform (input_location, " enters OpenMP structured block");
3059 saw_omp = true;
3063 return !identified;
3066 static void
3067 check_previous_goto (tree decl, struct named_label_use_entry *use)
3069 check_previous_goto_1 (decl, use->binding_level,
3070 use->names_in_scope, use->in_omp_scope,
3071 &use->o_goto_locus);
3074 static bool
3075 check_switch_goto (cp_binding_level* level)
3077 return check_previous_goto_1 (NULL_TREE, level, level->names, false, NULL);
3080 /* Check that a new jump to a label DECL is OK. Called by
3081 finish_goto_stmt. */
3083 void
3084 check_goto (tree decl)
3086 struct named_label_entry *ent, dummy;
3087 bool saw_catch = false, identified = false, complained = false;
3088 tree bad;
3089 unsigned ix;
3091 /* We can't know where a computed goto is jumping.
3092 So we assume that it's OK. */
3093 if (TREE_CODE (decl) != LABEL_DECL)
3094 return;
3096 /* We didn't record any information about this label when we created it,
3097 and there's not much point since it's trivial to analyze as a return. */
3098 if (decl == cdtor_label)
3099 return;
3101 dummy.label_decl = decl;
3102 ent = named_labels->find (&dummy);
3103 gcc_assert (ent != NULL);
3105 /* If the label hasn't been defined yet, defer checking. */
3106 if (! DECL_INITIAL (decl))
3108 struct named_label_use_entry *new_use;
3110 /* Don't bother creating another use if the last goto had the
3111 same data, and will therefore create the same set of errors. */
3112 if (ent->uses
3113 && ent->uses->names_in_scope == current_binding_level->names)
3114 return;
3116 new_use = ggc_alloc<named_label_use_entry> ();
3117 new_use->binding_level = current_binding_level;
3118 new_use->names_in_scope = current_binding_level->names;
3119 new_use->o_goto_locus = input_location;
3120 new_use->in_omp_scope = false;
3122 new_use->next = ent->uses;
3123 ent->uses = new_use;
3124 return;
3127 if (ent->in_try_scope || ent->in_catch_scope
3128 || ent->in_omp_scope || !vec_safe_is_empty (ent->bad_decls))
3130 complained = permerror (input_location, "jump to label %q+D", decl);
3131 if (complained)
3132 inform (input_location, " from here");
3133 identified = true;
3136 FOR_EACH_VEC_SAFE_ELT (ent->bad_decls, ix, bad)
3138 int u = decl_jump_unsafe (bad);
3140 if (u > 1 && DECL_ARTIFICIAL (bad))
3142 /* Can't skip init of __exception_info. */
3143 if (complained)
3144 inform (DECL_SOURCE_LOCATION (bad), " enters catch block");
3145 saw_catch = true;
3147 else if (complained)
3149 if (u > 1)
3150 inform (input_location, " skips initialization of %q+#D", bad);
3151 else
3152 inform (input_location, " enters scope of %q+#D which has "
3153 "non-trivial destructor", bad);
3157 if (complained)
3159 if (ent->in_try_scope)
3160 inform (input_location, " enters try block");
3161 else if (ent->in_catch_scope && !saw_catch)
3162 inform (input_location, " enters catch block");
3165 if (ent->in_omp_scope)
3167 if (complained)
3168 inform (input_location, " enters OpenMP structured block");
3170 else if (flag_openmp)
3172 cp_binding_level *b;
3173 for (b = current_binding_level; b ; b = b->level_chain)
3175 if (b == ent->binding_level)
3176 break;
3177 if (b->kind == sk_omp)
3179 if (!identified)
3181 complained = permerror (input_location,
3182 "jump to label %q+D", decl);
3183 if (complained)
3184 inform (input_location, " from here");
3185 identified = true;
3187 if (complained)
3188 inform (input_location, " exits OpenMP structured block");
3189 break;
3195 /* Check that a return is ok wrt OpenMP structured blocks.
3196 Called by finish_return_stmt. Returns true if all is well. */
3198 bool
3199 check_omp_return (void)
3201 cp_binding_level *b;
3202 for (b = current_binding_level; b ; b = b->level_chain)
3203 if (b->kind == sk_omp)
3205 error ("invalid exit from OpenMP structured block");
3206 return false;
3208 else if (b->kind == sk_function_parms)
3209 break;
3210 return true;
3213 /* Define a label, specifying the location in the source file.
3214 Return the LABEL_DECL node for the label. */
3216 static tree
3217 define_label_1 (location_t location, tree name)
3219 struct named_label_entry *ent, dummy;
3220 cp_binding_level *p;
3221 tree decl;
3223 decl = lookup_label (name);
3225 dummy.label_decl = decl;
3226 ent = named_labels->find (&dummy);
3227 gcc_assert (ent != NULL);
3229 /* After labels, make any new cleanups in the function go into their
3230 own new (temporary) binding contour. */
3231 for (p = current_binding_level;
3232 p->kind != sk_function_parms;
3233 p = p->level_chain)
3234 p->more_cleanups_ok = 0;
3236 if (name == get_identifier ("wchar_t"))
3237 permerror (input_location, "label named wchar_t");
3239 if (DECL_INITIAL (decl) != NULL_TREE)
3241 error ("duplicate label %qD", decl);
3242 return error_mark_node;
3244 else
3246 struct named_label_use_entry *use;
3248 /* Mark label as having been defined. */
3249 DECL_INITIAL (decl) = error_mark_node;
3250 /* Say where in the source. */
3251 DECL_SOURCE_LOCATION (decl) = location;
3253 ent->binding_level = current_binding_level;
3254 ent->names_in_scope = current_binding_level->names;
3256 for (use = ent->uses; use ; use = use->next)
3257 check_previous_goto (decl, use);
3258 ent->uses = NULL;
3261 return decl;
3264 /* Wrapper for define_label_1. */
3266 tree
3267 define_label (location_t location, tree name)
3269 tree ret;
3270 bool running = timevar_cond_start (TV_NAME_LOOKUP);
3271 ret = define_label_1 (location, name);
3272 timevar_cond_stop (TV_NAME_LOOKUP, running);
3273 return ret;
3277 struct cp_switch
3279 cp_binding_level *level;
3280 struct cp_switch *next;
3281 /* The SWITCH_STMT being built. */
3282 tree switch_stmt;
3283 /* A splay-tree mapping the low element of a case range to the high
3284 element, or NULL_TREE if there is no high element. Used to
3285 determine whether or not a new case label duplicates an old case
3286 label. We need a tree, rather than simply a hash table, because
3287 of the GNU case range extension. */
3288 splay_tree cases;
3291 /* A stack of the currently active switch statements. The innermost
3292 switch statement is on the top of the stack. There is no need to
3293 mark the stack for garbage collection because it is only active
3294 during the processing of the body of a function, and we never
3295 collect at that point. */
3297 static struct cp_switch *switch_stack;
3299 /* Called right after a switch-statement condition is parsed.
3300 SWITCH_STMT is the switch statement being parsed. */
3302 void
3303 push_switch (tree switch_stmt)
3305 struct cp_switch *p = XNEW (struct cp_switch);
3306 p->level = current_binding_level;
3307 p->next = switch_stack;
3308 p->switch_stmt = switch_stmt;
3309 p->cases = splay_tree_new (case_compare, NULL, NULL);
3310 switch_stack = p;
3313 void
3314 pop_switch (void)
3316 struct cp_switch *cs = switch_stack;
3317 location_t switch_location;
3319 /* Emit warnings as needed. */
3320 switch_location = EXPR_LOC_OR_LOC (cs->switch_stmt, input_location);
3321 if (!processing_template_decl)
3322 c_do_switch_warnings (cs->cases, switch_location,
3323 SWITCH_STMT_TYPE (cs->switch_stmt),
3324 SWITCH_STMT_COND (cs->switch_stmt));
3326 splay_tree_delete (cs->cases);
3327 switch_stack = switch_stack->next;
3328 free (cs);
3331 /* Convert a case constant VALUE in a switch to the type TYPE of the switch
3332 condition. Note that if TYPE and VALUE are already integral we don't
3333 really do the conversion because the language-independent
3334 warning/optimization code will work better that way. */
3336 static tree
3337 case_conversion (tree type, tree value)
3339 if (value == NULL_TREE)
3340 return value;
3342 if (cxx_dialect >= cxx11
3343 && (SCOPED_ENUM_P (type)
3344 || !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (value))))
3346 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
3347 type = type_promotes_to (type);
3348 value = (perform_implicit_conversion_flags
3349 (type, value, tf_warning_or_error,
3350 LOOKUP_IMPLICIT | LOOKUP_NO_NON_INTEGRAL));
3352 return cxx_constant_value (value);
3355 /* Note that we've seen a definition of a case label, and complain if this
3356 is a bad place for one. */
3358 tree
3359 finish_case_label (location_t loc, tree low_value, tree high_value)
3361 tree cond, r;
3362 cp_binding_level *p;
3363 tree type;
3365 if (processing_template_decl)
3367 tree label;
3369 /* For templates, just add the case label; we'll do semantic
3370 analysis at instantiation-time. */
3371 label = build_decl (loc, LABEL_DECL, NULL_TREE, NULL_TREE);
3372 return add_stmt (build_case_label (low_value, high_value, label));
3375 /* Find the condition on which this switch statement depends. */
3376 cond = SWITCH_STMT_COND (switch_stack->switch_stmt);
3377 if (cond && TREE_CODE (cond) == TREE_LIST)
3378 cond = TREE_VALUE (cond);
3380 if (!check_switch_goto (switch_stack->level))
3381 return error_mark_node;
3383 type = SWITCH_STMT_TYPE (switch_stack->switch_stmt);
3385 low_value = case_conversion (type, low_value);
3386 high_value = case_conversion (type, high_value);
3388 r = c_add_case_label (loc, switch_stack->cases, cond, type,
3389 low_value, high_value);
3391 /* After labels, make any new cleanups in the function go into their
3392 own new (temporary) binding contour. */
3393 for (p = current_binding_level;
3394 p->kind != sk_function_parms;
3395 p = p->level_chain)
3396 p->more_cleanups_ok = 0;
3398 return r;
3401 struct typename_info {
3402 tree scope;
3403 tree name;
3404 tree template_id;
3405 bool enum_p;
3406 bool class_p;
3409 struct typename_hasher : ggc_hasher<tree>
3411 typedef typename_info *compare_type;
3413 /* Hash a TYPENAME_TYPE. */
3415 static hashval_t
3416 hash (tree t)
3418 hashval_t hash;
3420 hash = (htab_hash_pointer (TYPE_CONTEXT (t))
3421 ^ htab_hash_pointer (TYPE_IDENTIFIER (t)));
3423 return hash;
3426 /* Compare two TYPENAME_TYPEs. */
3428 static bool
3429 equal (tree t1, const typename_info *t2)
3431 return (TYPE_IDENTIFIER (t1) == t2->name
3432 && TYPE_CONTEXT (t1) == t2->scope
3433 && TYPENAME_TYPE_FULLNAME (t1) == t2->template_id
3434 && TYPENAME_IS_ENUM_P (t1) == t2->enum_p
3435 && TYPENAME_IS_CLASS_P (t1) == t2->class_p);
3439 /* Build a TYPENAME_TYPE. If the type is `typename T::t', CONTEXT is
3440 the type of `T', NAME is the IDENTIFIER_NODE for `t'.
3442 Returns the new TYPENAME_TYPE. */
3444 static GTY (()) hash_table<typename_hasher> *typename_htab;
3446 static tree
3447 build_typename_type (tree context, tree name, tree fullname,
3448 enum tag_types tag_type)
3450 tree t;
3451 tree d;
3452 typename_info ti;
3453 tree *e;
3454 hashval_t hash;
3456 if (typename_htab == NULL)
3457 typename_htab = hash_table<typename_hasher>::create_ggc (61);
3459 ti.scope = FROB_CONTEXT (context);
3460 ti.name = name;
3461 ti.template_id = fullname;
3462 ti.enum_p = tag_type == enum_type;
3463 ti.class_p = (tag_type == class_type
3464 || tag_type == record_type
3465 || tag_type == union_type);
3466 hash = (htab_hash_pointer (ti.scope)
3467 ^ htab_hash_pointer (ti.name));
3469 /* See if we already have this type. */
3470 e = typename_htab->find_slot_with_hash (&ti, hash, INSERT);
3471 if (*e)
3472 t = *e;
3473 else
3475 /* Build the TYPENAME_TYPE. */
3476 t = cxx_make_type (TYPENAME_TYPE);
3477 TYPE_CONTEXT (t) = ti.scope;
3478 TYPENAME_TYPE_FULLNAME (t) = ti.template_id;
3479 TYPENAME_IS_ENUM_P (t) = ti.enum_p;
3480 TYPENAME_IS_CLASS_P (t) = ti.class_p;
3482 /* Build the corresponding TYPE_DECL. */
3483 d = build_decl (input_location, TYPE_DECL, name, t);
3484 TYPE_NAME (TREE_TYPE (d)) = d;
3485 TYPE_STUB_DECL (TREE_TYPE (d)) = d;
3486 DECL_CONTEXT (d) = FROB_CONTEXT (context);
3487 DECL_ARTIFICIAL (d) = 1;
3489 /* Store it in the hash table. */
3490 *e = t;
3492 /* TYPENAME_TYPEs must always be compared structurally, because
3493 they may or may not resolve down to another type depending on
3494 the currently open classes. */
3495 SET_TYPE_STRUCTURAL_EQUALITY (t);
3498 return t;
3501 /* Resolve `typename CONTEXT::NAME'. TAG_TYPE indicates the tag
3502 provided to name the type. Returns an appropriate type, unless an
3503 error occurs, in which case error_mark_node is returned. If we
3504 locate a non-artificial TYPE_DECL and TF_KEEP_TYPE_DECL is set, we
3505 return that, rather than the _TYPE it corresponds to, in other
3506 cases we look through the type decl. If TF_ERROR is set, complain
3507 about errors, otherwise be quiet. */
3509 tree
3510 make_typename_type (tree context, tree name, enum tag_types tag_type,
3511 tsubst_flags_t complain)
3513 tree fullname;
3514 tree t;
3515 bool want_template;
3517 if (name == error_mark_node
3518 || context == NULL_TREE
3519 || context == error_mark_node)
3520 return error_mark_node;
3522 if (TYPE_P (name))
3524 if (!(TYPE_LANG_SPECIFIC (name)
3525 && (CLASSTYPE_IS_TEMPLATE (name)
3526 || CLASSTYPE_USE_TEMPLATE (name))))
3527 name = TYPE_IDENTIFIER (name);
3528 else
3529 /* Create a TEMPLATE_ID_EXPR for the type. */
3530 name = build_nt (TEMPLATE_ID_EXPR,
3531 CLASSTYPE_TI_TEMPLATE (name),
3532 CLASSTYPE_TI_ARGS (name));
3534 else if (TREE_CODE (name) == TYPE_DECL)
3535 name = DECL_NAME (name);
3537 fullname = name;
3539 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3541 name = TREE_OPERAND (name, 0);
3542 if (TREE_CODE (name) == TEMPLATE_DECL)
3543 name = TREE_OPERAND (fullname, 0) = DECL_NAME (name);
3544 else if (TREE_CODE (name) == OVERLOAD)
3546 if (complain & tf_error)
3547 error ("%qD is not a type", name);
3548 return error_mark_node;
3551 if (TREE_CODE (name) == TEMPLATE_DECL)
3553 if (complain & tf_error)
3554 error ("%qD used without template parameters", name);
3555 return error_mark_node;
3557 gcc_assert (identifier_p (name));
3558 gcc_assert (TYPE_P (context));
3560 if (!MAYBE_CLASS_TYPE_P (context))
3562 if (complain & tf_error)
3563 error ("%q#T is not a class", context);
3564 return error_mark_node;
3567 /* When the CONTEXT is a dependent type, NAME could refer to a
3568 dependent base class of CONTEXT. But look inside it anyway
3569 if CONTEXT is a currently open scope, in case it refers to a
3570 member of the current instantiation or a non-dependent base;
3571 lookup will stop when we hit a dependent base. */
3572 if (!dependent_scope_p (context))
3573 /* We should only set WANT_TYPE when we're a nested typename type.
3574 Then we can give better diagnostics if we find a non-type. */
3575 t = lookup_field (context, name, 2, /*want_type=*/true);
3576 else
3577 t = NULL_TREE;
3579 if ((!t || TREE_CODE (t) == TREE_LIST) && dependent_type_p (context))
3580 return build_typename_type (context, name, fullname, tag_type);
3582 want_template = TREE_CODE (fullname) == TEMPLATE_ID_EXPR;
3584 if (!t)
3586 if (complain & tf_error)
3587 error (want_template ? G_("no class template named %q#T in %q#T")
3588 : G_("no type named %q#T in %q#T"), name, context);
3589 return error_mark_node;
3592 /* Pull out the template from an injected-class-name (or multiple). */
3593 if (want_template)
3594 t = maybe_get_template_decl_from_type_decl (t);
3596 if (TREE_CODE (t) == TREE_LIST)
3598 if (complain & tf_error)
3600 error ("lookup of %qT in %qT is ambiguous", name, context);
3601 print_candidates (t);
3603 return error_mark_node;
3606 if (want_template && !DECL_TYPE_TEMPLATE_P (t))
3608 if (complain & tf_error)
3609 error ("%<typename %T::%D%> names %q#T, which is not a class template",
3610 context, name, t);
3611 return error_mark_node;
3613 if (!want_template && TREE_CODE (t) != TYPE_DECL)
3615 if (complain & tf_error)
3616 error ("%<typename %T::%D%> names %q#T, which is not a type",
3617 context, name, t);
3618 return error_mark_node;
3621 if (!perform_or_defer_access_check (TYPE_BINFO (context), t, t, complain))
3622 return error_mark_node;
3624 /* If we are currently parsing a template and if T is a typedef accessed
3625 through CONTEXT then we need to remember and check access of T at
3626 template instantiation time. */
3627 add_typedef_to_current_template_for_access_check (t, context, input_location);
3629 if (want_template)
3630 return lookup_template_class (t, TREE_OPERAND (fullname, 1),
3631 NULL_TREE, context,
3632 /*entering_scope=*/0,
3633 complain | tf_user);
3635 if (DECL_ARTIFICIAL (t) || !(complain & tf_keep_type_decl))
3636 t = TREE_TYPE (t);
3638 maybe_record_typedef_use (t);
3640 return t;
3643 /* Resolve `CONTEXT::template NAME'. Returns a TEMPLATE_DECL if the name
3644 can be resolved or an UNBOUND_CLASS_TEMPLATE, unless an error occurs,
3645 in which case error_mark_node is returned.
3647 If PARM_LIST is non-NULL, also make sure that the template parameter
3648 list of TEMPLATE_DECL matches.
3650 If COMPLAIN zero, don't complain about any errors that occur. */
3652 tree
3653 make_unbound_class_template (tree context, tree name, tree parm_list,
3654 tsubst_flags_t complain)
3656 tree t;
3657 tree d;
3659 if (TYPE_P (name))
3660 name = TYPE_IDENTIFIER (name);
3661 else if (DECL_P (name))
3662 name = DECL_NAME (name);
3663 gcc_assert (identifier_p (name));
3665 if (!dependent_type_p (context)
3666 || currently_open_class (context))
3668 tree tmpl = NULL_TREE;
3670 if (MAYBE_CLASS_TYPE_P (context))
3671 tmpl = lookup_field (context, name, 0, false);
3673 if (tmpl && TREE_CODE (tmpl) == TYPE_DECL)
3674 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
3676 if (!tmpl || !DECL_TYPE_TEMPLATE_P (tmpl))
3678 if (complain & tf_error)
3679 error ("no class template named %q#T in %q#T", name, context);
3680 return error_mark_node;
3683 if (parm_list
3684 && !comp_template_parms (DECL_TEMPLATE_PARMS (tmpl), parm_list))
3686 if (complain & tf_error)
3688 error ("template parameters do not match template %qD", tmpl);
3689 inform (DECL_SOURCE_LOCATION (tmpl),
3690 "%qD declared here", tmpl);
3692 return error_mark_node;
3695 if (!perform_or_defer_access_check (TYPE_BINFO (context), tmpl, tmpl,
3696 complain))
3697 return error_mark_node;
3699 return tmpl;
3702 /* Build the UNBOUND_CLASS_TEMPLATE. */
3703 t = cxx_make_type (UNBOUND_CLASS_TEMPLATE);
3704 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
3705 TREE_TYPE (t) = NULL_TREE;
3706 SET_TYPE_STRUCTURAL_EQUALITY (t);
3708 /* Build the corresponding TEMPLATE_DECL. */
3709 d = build_decl (input_location, TEMPLATE_DECL, name, t);
3710 TYPE_NAME (TREE_TYPE (d)) = d;
3711 TYPE_STUB_DECL (TREE_TYPE (d)) = d;
3712 DECL_CONTEXT (d) = FROB_CONTEXT (context);
3713 DECL_ARTIFICIAL (d) = 1;
3714 DECL_TEMPLATE_PARMS (d) = parm_list;
3716 return t;
3721 /* Push the declarations of builtin types into the namespace.
3722 RID_INDEX is the index of the builtin type in the array
3723 RID_POINTERS. NAME is the name used when looking up the builtin
3724 type. TYPE is the _TYPE node for the builtin type. */
3726 void
3727 record_builtin_type (enum rid rid_index,
3728 const char* name,
3729 tree type)
3731 tree rname = NULL_TREE, tname = NULL_TREE;
3732 tree tdecl = NULL_TREE;
3734 if ((int) rid_index < (int) RID_MAX)
3735 rname = ridpointers[(int) rid_index];
3736 if (name)
3737 tname = get_identifier (name);
3739 /* The calls to SET_IDENTIFIER_GLOBAL_VALUE below should be
3740 eliminated. Built-in types should not be looked up name; their
3741 names are keywords that the parser can recognize. However, there
3742 is code in c-common.c that uses identifier_global_value to look
3743 up built-in types by name. */
3744 if (tname)
3746 tdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, tname, type);
3747 DECL_ARTIFICIAL (tdecl) = 1;
3748 SET_IDENTIFIER_GLOBAL_VALUE (tname, tdecl);
3750 if (rname)
3752 if (!tdecl)
3754 tdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, rname, type);
3755 DECL_ARTIFICIAL (tdecl) = 1;
3757 SET_IDENTIFIER_GLOBAL_VALUE (rname, tdecl);
3760 if (!TYPE_NAME (type))
3761 TYPE_NAME (type) = tdecl;
3763 if (tdecl)
3764 debug_hooks->type_decl (tdecl, 0);
3767 /* Record one of the standard Java types.
3768 * Declare it as having the given NAME.
3769 * If SIZE > 0, it is the size of one of the integral types;
3770 * otherwise it is the negative of the size of one of the other types. */
3772 static tree
3773 record_builtin_java_type (const char* name, int size)
3775 tree type, decl;
3776 if (size > 0)
3778 type = build_nonstandard_integer_type (size, 0);
3779 type = build_distinct_type_copy (type);
3781 else if (size > -32)
3783 tree stype;
3784 /* "__java_char" or ""__java_boolean". */
3785 type = build_nonstandard_integer_type (-size, 1);
3786 type = build_distinct_type_copy (type);
3787 /* Get the signed type cached and attached to the unsigned type,
3788 so it doesn't get garbage-collected at "random" times,
3789 causing potential codegen differences out of different UIDs
3790 and different alias set numbers. */
3791 stype = build_nonstandard_integer_type (-size, 0);
3792 stype = build_distinct_type_copy (stype);
3793 TREE_CHAIN (type) = stype;
3794 /*if (size == -1) TREE_SET_CODE (type, BOOLEAN_TYPE);*/
3796 else
3797 { /* "__java_float" or ""__java_double". */
3798 type = make_node (REAL_TYPE);
3799 TYPE_PRECISION (type) = - size;
3800 layout_type (type);
3802 record_builtin_type (RID_MAX, name, type);
3803 decl = TYPE_NAME (type);
3805 /* Suppress generate debug symbol entries for these types,
3806 since for normal C++ they are just clutter.
3807 However, push_lang_context undoes this if extern "Java" is seen. */
3808 DECL_IGNORED_P (decl) = 1;
3810 TYPE_FOR_JAVA (type) = 1;
3811 return type;
3814 /* Push a type into the namespace so that the back ends ignore it. */
3816 static void
3817 record_unknown_type (tree type, const char* name)
3819 tree decl = pushdecl (build_decl (UNKNOWN_LOCATION,
3820 TYPE_DECL, get_identifier (name), type));
3821 /* Make sure the "unknown type" typedecl gets ignored for debug info. */
3822 DECL_IGNORED_P (decl) = 1;
3823 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
3824 TYPE_SIZE (type) = TYPE_SIZE (void_type_node);
3825 TYPE_ALIGN (type) = 1;
3826 TYPE_USER_ALIGN (type) = 0;
3827 SET_TYPE_MODE (type, TYPE_MODE (void_type_node));
3830 /* A string for which we should create an IDENTIFIER_NODE at
3831 startup. */
3833 typedef struct predefined_identifier
3835 /* The name of the identifier. */
3836 const char *const name;
3837 /* The place where the IDENTIFIER_NODE should be stored. */
3838 tree *const node;
3839 /* Nonzero if this is the name of a constructor or destructor. */
3840 const int ctor_or_dtor_p;
3841 } predefined_identifier;
3843 /* Create all the predefined identifiers. */
3845 static void
3846 initialize_predefined_identifiers (void)
3848 const predefined_identifier *pid;
3850 /* A table of identifiers to create at startup. */
3851 static const predefined_identifier predefined_identifiers[] = {
3852 { "C++", &lang_name_cplusplus, 0 },
3853 { "C", &lang_name_c, 0 },
3854 { "Java", &lang_name_java, 0 },
3855 /* Some of these names have a trailing space so that it is
3856 impossible for them to conflict with names written by users. */
3857 { "__ct ", &ctor_identifier, 1 },
3858 { "__base_ctor ", &base_ctor_identifier, 1 },
3859 { "__comp_ctor ", &complete_ctor_identifier, 1 },
3860 { "__dt ", &dtor_identifier, 1 },
3861 { "__comp_dtor ", &complete_dtor_identifier, 1 },
3862 { "__base_dtor ", &base_dtor_identifier, 1 },
3863 { "__deleting_dtor ", &deleting_dtor_identifier, 1 },
3864 { IN_CHARGE_NAME, &in_charge_identifier, 0 },
3865 { "nelts", &nelts_identifier, 0 },
3866 { THIS_NAME, &this_identifier, 0 },
3867 { VTABLE_DELTA_NAME, &delta_identifier, 0 },
3868 { VTABLE_PFN_NAME, &pfn_identifier, 0 },
3869 { "_vptr", &vptr_identifier, 0 },
3870 { "__vtt_parm", &vtt_parm_identifier, 0 },
3871 { "::", &global_scope_name, 0 },
3872 { "std", &std_identifier, 0 },
3873 { NULL, NULL, 0 }
3876 for (pid = predefined_identifiers; pid->name; ++pid)
3878 *pid->node = get_identifier (pid->name);
3879 if (pid->ctor_or_dtor_p)
3880 IDENTIFIER_CTOR_OR_DTOR_P (*pid->node) = 1;
3884 /* Create the predefined scalar types of C,
3885 and some nodes representing standard constants (0, 1, (void *)0).
3886 Initialize the global binding level.
3887 Make definitions for built-in primitive functions. */
3889 void
3890 cxx_init_decl_processing (void)
3892 tree void_ftype;
3893 tree void_ftype_ptr;
3895 /* Create all the identifiers we need. */
3896 initialize_predefined_identifiers ();
3898 /* Create the global variables. */
3899 push_to_top_level ();
3901 current_function_decl = NULL_TREE;
3902 current_binding_level = NULL;
3903 /* Enter the global namespace. */
3904 gcc_assert (global_namespace == NULL_TREE);
3905 global_namespace = build_lang_decl (NAMESPACE_DECL, global_scope_name,
3906 void_type_node);
3907 DECL_CONTEXT (global_namespace) = build_translation_unit_decl (NULL_TREE);
3908 TREE_PUBLIC (global_namespace) = 1;
3909 begin_scope (sk_namespace, global_namespace);
3911 if (flag_visibility_ms_compat)
3912 default_visibility = VISIBILITY_HIDDEN;
3914 /* Initially, C. */
3915 current_lang_name = lang_name_c;
3917 /* Create the `std' namespace. */
3918 push_namespace (std_identifier);
3919 std_node = current_namespace;
3920 pop_namespace ();
3922 c_common_nodes_and_builtins ();
3924 java_byte_type_node = record_builtin_java_type ("__java_byte", 8);
3925 java_short_type_node = record_builtin_java_type ("__java_short", 16);
3926 java_int_type_node = record_builtin_java_type ("__java_int", 32);
3927 java_long_type_node = record_builtin_java_type ("__java_long", 64);
3928 java_float_type_node = record_builtin_java_type ("__java_float", -32);
3929 java_double_type_node = record_builtin_java_type ("__java_double", -64);
3930 java_char_type_node = record_builtin_java_type ("__java_char", -16);
3931 java_boolean_type_node = record_builtin_java_type ("__java_boolean", -1);
3933 integer_two_node = build_int_cst (NULL_TREE, 2);
3935 record_builtin_type (RID_BOOL, "bool", boolean_type_node);
3936 truthvalue_type_node = boolean_type_node;
3937 truthvalue_false_node = boolean_false_node;
3938 truthvalue_true_node = boolean_true_node;
3940 empty_except_spec = build_tree_list (NULL_TREE, NULL_TREE);
3941 noexcept_true_spec = build_tree_list (boolean_true_node, NULL_TREE);
3942 noexcept_false_spec = build_tree_list (boolean_false_node, NULL_TREE);
3944 #if 0
3945 record_builtin_type (RID_MAX, NULL, string_type_node);
3946 #endif
3948 delta_type_node = ptrdiff_type_node;
3949 vtable_index_type = ptrdiff_type_node;
3951 vtt_parm_type = build_pointer_type (const_ptr_type_node);
3952 void_ftype = build_function_type_list (void_type_node, NULL_TREE);
3953 void_ftype_ptr = build_function_type_list (void_type_node,
3954 ptr_type_node, NULL_TREE);
3955 void_ftype_ptr
3956 = build_exception_variant (void_ftype_ptr, empty_except_spec);
3958 /* C++ extensions */
3960 unknown_type_node = make_node (LANG_TYPE);
3961 record_unknown_type (unknown_type_node, "unknown type");
3963 /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node. */
3964 TREE_TYPE (unknown_type_node) = unknown_type_node;
3966 /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same
3967 result. */
3968 TYPE_POINTER_TO (unknown_type_node) = unknown_type_node;
3969 TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node;
3971 init_list_type_node = make_node (LANG_TYPE);
3972 record_unknown_type (init_list_type_node, "init list");
3975 /* Make sure we get a unique function type, so we can give
3976 its pointer type a name. (This wins for gdb.) */
3977 tree vfunc_type = make_node (FUNCTION_TYPE);
3978 TREE_TYPE (vfunc_type) = integer_type_node;
3979 TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
3980 layout_type (vfunc_type);
3982 vtable_entry_type = build_pointer_type (vfunc_type);
3984 record_builtin_type (RID_MAX, VTBL_PTR_TYPE, vtable_entry_type);
3986 vtbl_type_node
3987 = build_cplus_array_type (vtable_entry_type, NULL_TREE);
3988 layout_type (vtbl_type_node);
3989 vtbl_type_node = cp_build_qualified_type (vtbl_type_node, TYPE_QUAL_CONST);
3990 record_builtin_type (RID_MAX, NULL, vtbl_type_node);
3991 vtbl_ptr_type_node = build_pointer_type (vtable_entry_type);
3992 layout_type (vtbl_ptr_type_node);
3993 record_builtin_type (RID_MAX, NULL, vtbl_ptr_type_node);
3995 push_namespace (get_identifier ("__cxxabiv1"));
3996 abi_node = current_namespace;
3997 pop_namespace ();
3999 global_type_node = make_node (LANG_TYPE);
4000 record_unknown_type (global_type_node, "global type");
4002 /* Now, C++. */
4003 current_lang_name = lang_name_cplusplus;
4006 tree newattrs, extvisattr;
4007 tree newtype, deltype;
4008 tree ptr_ftype_sizetype;
4009 tree new_eh_spec;
4011 ptr_ftype_sizetype
4012 = build_function_type_list (ptr_type_node, size_type_node, NULL_TREE);
4013 if (cxx_dialect == cxx98)
4015 tree bad_alloc_id;
4016 tree bad_alloc_type_node;
4017 tree bad_alloc_decl;
4019 push_namespace (std_identifier);
4020 bad_alloc_id = get_identifier ("bad_alloc");
4021 bad_alloc_type_node = make_class_type (RECORD_TYPE);
4022 TYPE_CONTEXT (bad_alloc_type_node) = current_namespace;
4023 bad_alloc_decl
4024 = create_implicit_typedef (bad_alloc_id, bad_alloc_type_node);
4025 DECL_CONTEXT (bad_alloc_decl) = current_namespace;
4026 pop_namespace ();
4028 new_eh_spec
4029 = add_exception_specifier (NULL_TREE, bad_alloc_type_node, -1);
4031 else
4032 new_eh_spec = noexcept_false_spec;
4034 /* Ensure attribs.c is initialized. */
4035 init_attributes ();
4037 /* Ensure constraint.cc is initialized. */
4038 init_constraint_processing ();
4040 extvisattr = build_tree_list (get_identifier ("externally_visible"),
4041 NULL_TREE);
4042 newattrs = tree_cons (get_identifier ("alloc_size"),
4043 build_tree_list (NULL_TREE, integer_one_node),
4044 extvisattr);
4045 newtype = cp_build_type_attribute_variant (ptr_ftype_sizetype, newattrs);
4046 newtype = build_exception_variant (newtype, new_eh_spec);
4047 deltype = cp_build_type_attribute_variant (void_ftype_ptr, extvisattr);
4048 deltype = build_exception_variant (deltype, empty_except_spec);
4049 tree opnew = push_cp_library_fn (NEW_EXPR, newtype, 0);
4050 DECL_IS_MALLOC (opnew) = 1;
4051 DECL_IS_OPERATOR_NEW (opnew) = 1;
4052 opnew = push_cp_library_fn (VEC_NEW_EXPR, newtype, 0);
4053 DECL_IS_MALLOC (opnew) = 1;
4054 DECL_IS_OPERATOR_NEW (opnew) = 1;
4055 push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
4056 push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
4057 if (flag_sized_deallocation)
4059 /* Also push the sized deallocation variants:
4060 void operator delete(void*, std::size_t) throw();
4061 void operator delete[](void*, std::size_t) throw(); */
4062 tree void_ftype_ptr_size
4063 = build_function_type_list (void_type_node, ptr_type_node,
4064 size_type_node, NULL_TREE);
4065 deltype = cp_build_type_attribute_variant (void_ftype_ptr_size,
4066 extvisattr);
4067 deltype = build_exception_variant (deltype, empty_except_spec);
4068 push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
4069 push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
4072 nullptr_type_node = make_node (NULLPTR_TYPE);
4073 TYPE_SIZE (nullptr_type_node) = bitsize_int (GET_MODE_BITSIZE (ptr_mode));
4074 TYPE_SIZE_UNIT (nullptr_type_node) = size_int (GET_MODE_SIZE (ptr_mode));
4075 TYPE_UNSIGNED (nullptr_type_node) = 1;
4076 TYPE_PRECISION (nullptr_type_node) = GET_MODE_BITSIZE (ptr_mode);
4077 SET_TYPE_MODE (nullptr_type_node, ptr_mode);
4078 record_builtin_type (RID_MAX, "decltype(nullptr)", nullptr_type_node);
4079 nullptr_node = build_int_cst (nullptr_type_node, 0);
4082 abort_fndecl
4083 = build_library_fn_ptr ("__cxa_pure_virtual", void_ftype,
4084 ECF_NORETURN | ECF_NOTHROW);
4086 /* Perform other language dependent initializations. */
4087 init_class_processing ();
4088 init_rtti_processing ();
4089 init_template_processing ();
4091 if (flag_exceptions)
4092 init_exception_processing ();
4094 if (! supports_one_only ())
4095 flag_weak = 0;
4097 make_fname_decl = cp_make_fname_decl;
4098 start_fname_decls ();
4100 /* Show we use EH for cleanups. */
4101 if (flag_exceptions)
4102 using_eh_for_cleanups ();
4105 /* Generate an initializer for a function naming variable from
4106 NAME. NAME may be NULL, to indicate a dependent name. TYPE_P is
4107 filled in with the type of the init. */
4109 tree
4110 cp_fname_init (const char* name, tree *type_p)
4112 tree domain = NULL_TREE;
4113 tree type;
4114 tree init = NULL_TREE;
4115 size_t length = 0;
4117 if (name)
4119 length = strlen (name);
4120 domain = build_index_type (size_int (length));
4121 init = build_string (length + 1, name);
4124 type = cp_build_qualified_type (char_type_node, TYPE_QUAL_CONST);
4125 type = build_cplus_array_type (type, domain);
4127 *type_p = type;
4129 if (init)
4130 TREE_TYPE (init) = type;
4131 else
4132 init = error_mark_node;
4134 return init;
4137 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give
4138 the decl, LOC is the location to give the decl, NAME is the
4139 initialization string and TYPE_DEP indicates whether NAME depended
4140 on the type of the function. We make use of that to detect
4141 __PRETTY_FUNCTION__ inside a template fn. This is being done lazily
4142 at the point of first use, so we mustn't push the decl now. */
4144 static tree
4145 cp_make_fname_decl (location_t loc, tree id, int type_dep)
4147 const char *const name = (type_dep && processing_template_decl
4148 ? NULL : fname_as_string (type_dep));
4149 tree type;
4150 tree init = cp_fname_init (name, &type);
4151 tree decl = build_decl (loc, VAR_DECL, id, type);
4153 if (name)
4154 free (CONST_CAST (char *, name));
4156 /* As we're using pushdecl_with_scope, we must set the context. */
4157 DECL_CONTEXT (decl) = current_function_decl;
4159 TREE_STATIC (decl) = 1;
4160 TREE_READONLY (decl) = 1;
4161 DECL_ARTIFICIAL (decl) = 1;
4163 TREE_USED (decl) = 1;
4165 if (current_function_decl)
4167 cp_binding_level *b = current_binding_level;
4168 if (b->kind == sk_function_parms)
4169 return error_mark_node;
4170 while (b->level_chain->kind != sk_function_parms)
4171 b = b->level_chain;
4172 pushdecl_with_scope (decl, b, /*is_friend=*/false);
4173 cp_finish_decl (decl, init, /*init_const_expr_p=*/false, NULL_TREE,
4174 LOOKUP_ONLYCONVERTING);
4176 else
4178 DECL_THIS_STATIC (decl) = true;
4179 pushdecl_top_level_and_finish (decl, init);
4182 return decl;
4185 static tree
4186 builtin_function_1 (tree decl, tree context, bool is_global)
4188 tree id = DECL_NAME (decl);
4189 const char *name = IDENTIFIER_POINTER (id);
4191 retrofit_lang_decl (decl);
4193 DECL_ARTIFICIAL (decl) = 1;
4194 SET_OVERLOADED_OPERATOR_CODE (decl, ERROR_MARK);
4195 SET_DECL_LANGUAGE (decl, lang_c);
4196 /* Runtime library routines are, by definition, available in an
4197 external shared object. */
4198 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
4199 DECL_VISIBILITY_SPECIFIED (decl) = 1;
4201 DECL_CONTEXT (decl) = context;
4203 if (is_global)
4204 pushdecl_top_level (decl);
4205 else
4206 pushdecl (decl);
4208 /* A function in the user's namespace should have an explicit
4209 declaration before it is used. Mark the built-in function as
4210 anticipated but not actually declared. */
4211 if (name[0] != '_' || name[1] != '_')
4212 DECL_ANTICIPATED (decl) = 1;
4213 else if (strncmp (name + 2, "builtin_", strlen ("builtin_")) != 0)
4215 size_t len = strlen (name);
4217 /* Treat __*_chk fortification functions as anticipated as well,
4218 unless they are __builtin_*. */
4219 if (len > strlen ("___chk")
4220 && memcmp (name + len - strlen ("_chk"),
4221 "_chk", strlen ("_chk") + 1) == 0)
4222 DECL_ANTICIPATED (decl) = 1;
4225 return decl;
4228 tree
4229 cxx_builtin_function (tree decl)
4231 tree id = DECL_NAME (decl);
4232 const char *name = IDENTIFIER_POINTER (id);
4233 /* All builtins that don't begin with an '_' should additionally
4234 go in the 'std' namespace. */
4235 if (name[0] != '_')
4237 tree decl2 = copy_node(decl);
4238 push_namespace (std_identifier);
4239 builtin_function_1 (decl2, std_node, false);
4240 pop_namespace ();
4243 return builtin_function_1 (decl, NULL_TREE, false);
4246 /* Like cxx_builtin_function, but guarantee the function is added to the global
4247 scope. This is to allow function specific options to add new machine
4248 dependent builtins when the target ISA changes via attribute((target(...)))
4249 which saves space on program startup if the program does not use non-generic
4250 ISAs. */
4252 tree
4253 cxx_builtin_function_ext_scope (tree decl)
4256 tree id = DECL_NAME (decl);
4257 const char *name = IDENTIFIER_POINTER (id);
4258 /* All builtins that don't begin with an '_' should additionally
4259 go in the 'std' namespace. */
4260 if (name[0] != '_')
4262 tree decl2 = copy_node(decl);
4263 push_namespace (std_identifier);
4264 builtin_function_1 (decl2, std_node, true);
4265 pop_namespace ();
4268 return builtin_function_1 (decl, NULL_TREE, true);
4271 /* Generate a FUNCTION_DECL with the typical flags for a runtime library
4272 function. Not called directly. */
4274 static tree
4275 build_library_fn (tree name, enum tree_code operator_code, tree type,
4276 int ecf_flags)
4278 tree fn = build_lang_decl (FUNCTION_DECL, name, type);
4279 DECL_EXTERNAL (fn) = 1;
4280 TREE_PUBLIC (fn) = 1;
4281 DECL_ARTIFICIAL (fn) = 1;
4282 SET_OVERLOADED_OPERATOR_CODE (fn, operator_code);
4283 SET_DECL_LANGUAGE (fn, lang_c);
4284 /* Runtime library routines are, by definition, available in an
4285 external shared object. */
4286 DECL_VISIBILITY (fn) = VISIBILITY_DEFAULT;
4287 DECL_VISIBILITY_SPECIFIED (fn) = 1;
4288 set_call_expr_flags (fn, ecf_flags);
4289 return fn;
4292 /* Returns the _DECL for a library function with C++ linkage. */
4294 static tree
4295 build_cp_library_fn (tree name, enum tree_code operator_code, tree type,
4296 int ecf_flags)
4298 tree fn = build_library_fn (name, operator_code, type, ecf_flags);
4299 DECL_CONTEXT (fn) = FROB_CONTEXT (current_namespace);
4300 SET_DECL_LANGUAGE (fn, lang_cplusplus);
4301 return fn;
4304 /* Like build_library_fn, but takes a C string instead of an
4305 IDENTIFIER_NODE. */
4307 tree
4308 build_library_fn_ptr (const char* name, tree type, int ecf_flags)
4310 return build_library_fn (get_identifier (name), ERROR_MARK, type, ecf_flags);
4313 /* Like build_cp_library_fn, but takes a C string instead of an
4314 IDENTIFIER_NODE. */
4316 tree
4317 build_cp_library_fn_ptr (const char* name, tree type, int ecf_flags)
4319 return build_cp_library_fn (get_identifier (name), ERROR_MARK, type,
4320 ecf_flags);
4323 /* Like build_library_fn, but also pushes the function so that we will
4324 be able to find it via IDENTIFIER_GLOBAL_VALUE. Also, the function
4325 may throw exceptions listed in RAISES. */
4327 tree
4328 push_library_fn (tree name, tree type, tree raises, int ecf_flags)
4330 tree fn;
4332 if (raises)
4333 type = build_exception_variant (type, raises);
4335 fn = build_library_fn (name, ERROR_MARK, type, ecf_flags);
4336 pushdecl_top_level (fn);
4337 return fn;
4340 /* Like build_cp_library_fn, but also pushes the function so that it
4341 will be found by normal lookup. */
4343 static tree
4344 push_cp_library_fn (enum tree_code operator_code, tree type,
4345 int ecf_flags)
4347 tree fn = build_cp_library_fn (ansi_opname (operator_code),
4348 operator_code,
4349 type, ecf_flags);
4350 pushdecl (fn);
4351 if (flag_tm)
4352 apply_tm_attr (fn, get_identifier ("transaction_safe"));
4353 return fn;
4356 /* Like push_library_fn, but takes a TREE_LIST of parm types rather than
4357 a FUNCTION_TYPE. */
4359 tree
4360 push_void_library_fn (tree name, tree parmtypes, int ecf_flags)
4362 tree type = build_function_type (void_type_node, parmtypes);
4363 return push_library_fn (name, type, NULL_TREE, ecf_flags);
4366 /* Like push_library_fn, but also note that this function throws
4367 and does not return. Used for __throw_foo and the like. */
4369 tree
4370 push_throw_library_fn (tree name, tree type)
4372 tree fn = push_library_fn (name, type, NULL_TREE, ECF_NORETURN);
4373 return fn;
4376 /* When we call finish_struct for an anonymous union, we create
4377 default copy constructors and such. But, an anonymous union
4378 shouldn't have such things; this function undoes the damage to the
4379 anonymous union type T.
4381 (The reason that we create the synthesized methods is that we don't
4382 distinguish `union { int i; }' from `typedef union { int i; } U'.
4383 The first is an anonymous union; the second is just an ordinary
4384 union type.) */
4386 void
4387 fixup_anonymous_aggr (tree t)
4389 tree *q;
4391 /* Wipe out memory of synthesized methods. */
4392 TYPE_HAS_USER_CONSTRUCTOR (t) = 0;
4393 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0;
4394 TYPE_HAS_COPY_CTOR (t) = 0;
4395 TYPE_HAS_CONST_COPY_CTOR (t) = 0;
4396 TYPE_HAS_COPY_ASSIGN (t) = 0;
4397 TYPE_HAS_CONST_COPY_ASSIGN (t) = 0;
4399 /* Splice the implicitly generated functions out of the TYPE_METHODS
4400 list. */
4401 q = &TYPE_METHODS (t);
4402 while (*q)
4404 if (DECL_ARTIFICIAL (*q))
4405 *q = TREE_CHAIN (*q);
4406 else
4407 q = &DECL_CHAIN (*q);
4410 /* ISO C++ 9.5.3. Anonymous unions may not have function members. */
4411 if (TYPE_METHODS (t))
4413 tree decl = TYPE_MAIN_DECL (t);
4415 if (TREE_CODE (t) != UNION_TYPE)
4416 error_at (DECL_SOURCE_LOCATION (decl),
4417 "an anonymous struct cannot have function members");
4418 else
4419 error_at (DECL_SOURCE_LOCATION (decl),
4420 "an anonymous union cannot have function members");
4423 /* Anonymous aggregates cannot have fields with ctors, dtors or complex
4424 assignment operators (because they cannot have these methods themselves).
4425 For anonymous unions this is already checked because they are not allowed
4426 in any union, otherwise we have to check it. */
4427 if (TREE_CODE (t) != UNION_TYPE)
4429 tree field, type;
4431 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
4432 if (TREE_CODE (field) == FIELD_DECL)
4434 type = TREE_TYPE (field);
4435 if (CLASS_TYPE_P (type))
4437 if (TYPE_NEEDS_CONSTRUCTING (type))
4438 error ("member %q+#D with constructor not allowed "
4439 "in anonymous aggregate", field);
4440 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
4441 error ("member %q+#D with destructor not allowed "
4442 "in anonymous aggregate", field);
4443 if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type))
4444 error ("member %q+#D with copy assignment operator "
4445 "not allowed in anonymous aggregate", field);
4451 /* Warn for an attribute located at LOCATION that appertains to the
4452 class type CLASS_TYPE that has not been properly placed after its
4453 class-key, in it class-specifier. */
4455 void
4456 warn_misplaced_attr_for_class_type (source_location location,
4457 tree class_type)
4459 gcc_assert (OVERLOAD_TYPE_P (class_type));
4461 if (warning_at (location, OPT_Wattributes,
4462 "attribute ignored in declaration "
4463 "of %q#T", class_type))
4464 inform (location,
4465 "attribute for %q#T must follow the %qs keyword",
4466 class_type, class_key_or_enum_as_string (class_type));
4469 /* Make sure that a declaration with no declarator is well-formed, i.e.
4470 just declares a tagged type or anonymous union.
4472 Returns the type declared; or NULL_TREE if none. */
4474 tree
4475 check_tag_decl (cp_decl_specifier_seq *declspecs,
4476 bool explicit_type_instantiation_p)
4478 int saw_friend = decl_spec_seq_has_spec_p (declspecs, ds_friend);
4479 int saw_typedef = decl_spec_seq_has_spec_p (declspecs, ds_typedef);
4480 /* If a class, struct, or enum type is declared by the DECLSPECS
4481 (i.e, if a class-specifier, enum-specifier, or non-typename
4482 elaborated-type-specifier appears in the DECLSPECS),
4483 DECLARED_TYPE is set to the corresponding type. */
4484 tree declared_type = NULL_TREE;
4485 bool error_p = false;
4487 if (declspecs->multiple_types_p)
4488 error ("multiple types in one declaration");
4489 else if (declspecs->redefined_builtin_type)
4491 if (!in_system_header_at (input_location))
4492 permerror (declspecs->locations[ds_redefined_builtin_type_spec],
4493 "redeclaration of C++ built-in type %qT",
4494 declspecs->redefined_builtin_type);
4495 return NULL_TREE;
4498 if (declspecs->type
4499 && TYPE_P (declspecs->type)
4500 && ((TREE_CODE (declspecs->type) != TYPENAME_TYPE
4501 && MAYBE_CLASS_TYPE_P (declspecs->type))
4502 || TREE_CODE (declspecs->type) == ENUMERAL_TYPE))
4503 declared_type = declspecs->type;
4504 else if (declspecs->type == error_mark_node)
4505 error_p = true;
4506 if (declared_type == NULL_TREE && ! saw_friend && !error_p)
4507 permerror (input_location, "declaration does not declare anything");
4508 else if (declared_type != NULL_TREE && type_uses_auto (declared_type))
4510 error ("%<auto%> can only be specified for variables "
4511 "or function declarations");
4512 return error_mark_node;
4514 /* Check for an anonymous union. */
4515 else if (declared_type && RECORD_OR_UNION_CODE_P (TREE_CODE (declared_type))
4516 && TYPE_ANONYMOUS_P (declared_type))
4518 /* 7/3 In a simple-declaration, the optional init-declarator-list
4519 can be omitted only when declaring a class (clause 9) or
4520 enumeration (7.2), that is, when the decl-specifier-seq contains
4521 either a class-specifier, an elaborated-type-specifier with
4522 a class-key (9.1), or an enum-specifier. In these cases and
4523 whenever a class-specifier or enum-specifier is present in the
4524 decl-specifier-seq, the identifiers in these specifiers are among
4525 the names being declared by the declaration (as class-name,
4526 enum-names, or enumerators, depending on the syntax). In such
4527 cases, and except for the declaration of an unnamed bit-field (9.6),
4528 the decl-specifier-seq shall introduce one or more names into the
4529 program, or shall redeclare a name introduced by a previous
4530 declaration. [Example:
4531 enum { }; // ill-formed
4532 typedef class { }; // ill-formed
4533 --end example] */
4534 if (saw_typedef)
4536 error ("missing type-name in typedef-declaration");
4537 return NULL_TREE;
4539 /* Anonymous unions are objects, so they can have specifiers. */;
4540 SET_ANON_AGGR_TYPE_P (declared_type);
4542 if (TREE_CODE (declared_type) != UNION_TYPE
4543 && !in_system_header_at (input_location))
4544 pedwarn (input_location, OPT_Wpedantic, "ISO C++ prohibits anonymous structs");
4547 else
4549 if (decl_spec_seq_has_spec_p (declspecs, ds_inline)
4550 || decl_spec_seq_has_spec_p (declspecs, ds_virtual))
4551 error ("%qs can only be specified for functions",
4552 decl_spec_seq_has_spec_p (declspecs, ds_inline)
4553 ? "inline" : "virtual");
4554 else if (saw_friend
4555 && (!current_class_type
4556 || current_scope () != current_class_type))
4557 error ("%<friend%> can only be specified inside a class");
4558 else if (decl_spec_seq_has_spec_p (declspecs, ds_explicit))
4559 error ("%<explicit%> can only be specified for constructors");
4560 else if (declspecs->storage_class)
4561 error ("a storage class can only be specified for objects "
4562 "and functions");
4563 else if (decl_spec_seq_has_spec_p (declspecs, ds_const)
4564 || decl_spec_seq_has_spec_p (declspecs, ds_volatile)
4565 || decl_spec_seq_has_spec_p (declspecs, ds_restrict)
4566 || decl_spec_seq_has_spec_p (declspecs, ds_thread))
4567 error ("qualifiers can only be specified for objects "
4568 "and functions");
4569 else if (saw_typedef)
4570 warning (0, "%<typedef%> was ignored in this declaration");
4571 else if (decl_spec_seq_has_spec_p (declspecs, ds_constexpr))
4572 error ("%<constexpr%> cannot be used for type declarations");
4575 if (declspecs->attributes && warn_attributes && declared_type)
4577 location_t loc;
4578 if (!CLASS_TYPE_P (declared_type)
4579 || !CLASSTYPE_TEMPLATE_INSTANTIATION (declared_type))
4580 /* For a non-template class, use the name location. */
4581 loc = location_of (declared_type);
4582 else
4583 /* For a template class (an explicit instantiation), use the
4584 current location. */
4585 loc = input_location;
4587 if (explicit_type_instantiation_p)
4588 /* [dcl.attr.grammar]/4:
4590 No attribute-specifier-seq shall appertain to an explicit
4591 instantiation. */
4593 if (warning_at (loc, OPT_Wattributes,
4594 "attribute ignored in explicit instantiation %q#T",
4595 declared_type))
4596 inform (loc,
4597 "no attribute can be applied to "
4598 "an explicit instantiation");
4600 else
4601 warn_misplaced_attr_for_class_type (loc, declared_type);
4604 return declared_type;
4607 // True if the type T is a class template or a class template
4608 // specialization (either explicit or partial).
4609 static inline bool
4610 is_class_template_or_specialization (tree t)
4612 if (CLASS_TYPE_P (t)
4613 && (CLASSTYPE_IS_TEMPLATE (t) || CLASSTYPE_TEMPLATE_SPECIALIZATION (t)))
4614 return true;
4615 return false;
4618 /* Called when a declaration is seen that contains no names to declare.
4619 If its type is a reference to a structure, union or enum inherited
4620 from a containing scope, shadow that tag name for the current scope
4621 with a forward reference.
4622 If its type defines a new named structure or union
4623 or defines an enum, it is valid but we need not do anything here.
4624 Otherwise, it is an error.
4626 C++: may have to grok the declspecs to learn about static,
4627 complain for anonymous unions.
4629 Returns the TYPE declared -- or NULL_TREE if none. */
4631 tree
4632 shadow_tag (cp_decl_specifier_seq *declspecs)
4634 tree t = check_tag_decl (declspecs,
4635 /*explicit_type_instantiation_p=*/false);
4637 if (!t)
4638 return NULL_TREE;
4640 if (maybe_process_partial_specialization (t) == error_mark_node)
4641 return NULL_TREE;
4643 /* This is where the variables in an anonymous union are
4644 declared. An anonymous union declaration looks like:
4645 union { ... } ;
4646 because there is no declarator after the union, the parser
4647 sends that declaration here. */
4648 if (ANON_AGGR_TYPE_P (t))
4650 fixup_anonymous_aggr (t);
4652 if (TYPE_FIELDS (t))
4654 tree decl = grokdeclarator (/*declarator=*/NULL,
4655 declspecs, NORMAL, 0, NULL);
4656 finish_anon_union (decl);
4660 return t;
4663 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
4665 tree
4666 groktypename (cp_decl_specifier_seq *type_specifiers,
4667 const cp_declarator *declarator,
4668 bool is_template_arg)
4670 tree attrs;
4671 tree type;
4672 enum decl_context context
4673 = is_template_arg ? TEMPLATE_TYPE_ARG : TYPENAME;
4674 attrs = type_specifiers->attributes;
4675 type_specifiers->attributes = NULL_TREE;
4676 type = grokdeclarator (declarator, type_specifiers, context, 0, &attrs);
4677 if (attrs && type != error_mark_node)
4679 if (CLASS_TYPE_P (type))
4680 warning (OPT_Wattributes, "ignoring attributes applied to class type %qT "
4681 "outside of definition", type);
4682 else if (MAYBE_CLASS_TYPE_P (type))
4683 /* A template type parameter or other dependent type. */
4684 warning (OPT_Wattributes, "ignoring attributes applied to dependent "
4685 "type %qT without an associated declaration", type);
4686 else
4687 cplus_decl_attributes (&type, attrs, 0);
4689 return type;
4692 /* Process a DECLARATOR for a function-scope variable declaration,
4693 namespace-scope variable declaration, or function declaration.
4694 (Function definitions go through start_function; class member
4695 declarations appearing in the body of the class go through
4696 grokfield.) The DECL corresponding to the DECLARATOR is returned.
4697 If an error occurs, the error_mark_node is returned instead.
4699 DECLSPECS are the decl-specifiers for the declaration. INITIALIZED is
4700 SD_INITIALIZED if an explicit initializer is present, or SD_DEFAULTED
4701 for an explicitly defaulted function, or SD_DELETED for an explicitly
4702 deleted function, but 0 (SD_UNINITIALIZED) if this is a variable
4703 implicitly initialized via a default constructor. ATTRIBUTES and
4704 PREFIX_ATTRIBUTES are GNU attributes associated with this declaration.
4706 The scope represented by the context of the returned DECL is pushed
4707 (if it is not the global namespace) and is assigned to
4708 *PUSHED_SCOPE_P. The caller is then responsible for calling
4709 pop_scope on *PUSHED_SCOPE_P if it is set. */
4711 tree
4712 start_decl (const cp_declarator *declarator,
4713 cp_decl_specifier_seq *declspecs,
4714 int initialized,
4715 tree attributes,
4716 tree prefix_attributes,
4717 tree *pushed_scope_p)
4719 tree decl;
4720 tree context;
4721 bool was_public;
4722 int flags;
4723 bool alias;
4725 *pushed_scope_p = NULL_TREE;
4727 /* An object declared as __attribute__((deprecated)) suppresses
4728 warnings of uses of other deprecated items. */
4729 if (lookup_attribute ("deprecated", attributes))
4730 deprecated_state = DEPRECATED_SUPPRESS;
4732 attributes = chainon (attributes, prefix_attributes);
4734 decl = grokdeclarator (declarator, declspecs, NORMAL, initialized,
4735 &attributes);
4737 deprecated_state = DEPRECATED_NORMAL;
4739 if (decl == NULL_TREE || VOID_TYPE_P (decl)
4740 || decl == error_mark_node)
4741 return error_mark_node;
4743 context = CP_DECL_CONTEXT (decl);
4744 if (context != global_namespace)
4745 *pushed_scope_p = push_scope (context);
4747 /* Is it valid for this decl to have an initializer at all?
4748 If not, set INITIALIZED to zero, which will indirectly
4749 tell `cp_finish_decl' to ignore the initializer once it is parsed. */
4750 if (initialized
4751 && TREE_CODE (decl) == TYPE_DECL)
4753 error ("typedef %qD is initialized (use decltype instead)", decl);
4754 return error_mark_node;
4757 if (initialized)
4759 if (! toplevel_bindings_p ()
4760 && DECL_EXTERNAL (decl))
4761 warning (0, "declaration of %q#D has %<extern%> and is initialized",
4762 decl);
4763 DECL_EXTERNAL (decl) = 0;
4764 if (toplevel_bindings_p ())
4765 TREE_STATIC (decl) = 1;
4767 alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl)) != 0;
4769 if (alias && TREE_CODE (decl) == FUNCTION_DECL)
4770 record_key_method_defined (decl);
4772 /* If this is a typedef that names the class for linkage purposes
4773 (7.1.3p8), apply any attributes directly to the type. */
4774 if (TREE_CODE (decl) == TYPE_DECL
4775 && OVERLOAD_TYPE_P (TREE_TYPE (decl))
4776 && decl == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (decl))))
4777 flags = ATTR_FLAG_TYPE_IN_PLACE;
4778 else
4779 flags = 0;
4781 /* Set attributes here so if duplicate decl, will have proper attributes. */
4782 cplus_decl_attributes (&decl, attributes, flags);
4784 /* Dllimported symbols cannot be defined. Static data members (which
4785 can be initialized in-class and dllimported) go through grokfield,
4786 not here, so we don't need to exclude those decls when checking for
4787 a definition. */
4788 if (initialized && DECL_DLLIMPORT_P (decl))
4790 error ("definition of %q#D is marked %<dllimport%>", decl);
4791 DECL_DLLIMPORT_P (decl) = 0;
4794 /* If #pragma weak was used, mark the decl weak now. */
4795 if (!processing_template_decl)
4796 maybe_apply_pragma_weak (decl);
4798 if (TREE_CODE (decl) == FUNCTION_DECL
4799 && DECL_DECLARED_INLINE_P (decl)
4800 && DECL_UNINLINABLE (decl)
4801 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
4802 warning (0, "inline function %q+D given attribute noinline", decl);
4804 if (TYPE_P (context) && COMPLETE_TYPE_P (complete_type (context)))
4806 bool this_tmpl = (processing_template_decl
4807 > template_class_depth (context));
4808 if (VAR_P (decl))
4810 tree field = lookup_field (context, DECL_NAME (decl), 0, false);
4811 if (field == NULL_TREE
4812 || !(VAR_P (field) || variable_template_p (field)))
4813 error ("%q+#D is not a static data member of %q#T", decl, context);
4814 else if (variable_template_p (field) && !this_tmpl)
4816 if (DECL_LANG_SPECIFIC (decl)
4817 && DECL_TEMPLATE_SPECIALIZATION (decl))
4818 /* OK, specialization was already checked. */;
4819 else
4821 error_at (DECL_SOURCE_LOCATION (decl),
4822 "non-member-template declaration of %qD", decl);
4823 inform (DECL_SOURCE_LOCATION (field), "does not match "
4824 "member template declaration here");
4825 return error_mark_node;
4828 else
4830 if (variable_template_p (field))
4831 field = DECL_TEMPLATE_RESULT (field);
4833 if (DECL_CONTEXT (field) != context)
4835 if (!same_type_p (DECL_CONTEXT (field), context))
4836 permerror (input_location, "ISO C++ does not permit %<%T::%D%> "
4837 "to be defined as %<%T::%D%>",
4838 DECL_CONTEXT (field), DECL_NAME (decl),
4839 context, DECL_NAME (decl));
4840 DECL_CONTEXT (decl) = DECL_CONTEXT (field);
4842 /* Static data member are tricky; an in-class initialization
4843 still doesn't provide a definition, so the in-class
4844 declaration will have DECL_EXTERNAL set, but will have an
4845 initialization. Thus, duplicate_decls won't warn
4846 about this situation, and so we check here. */
4847 if (initialized && DECL_INITIALIZED_IN_CLASS_P (field))
4848 error ("duplicate initialization of %qD", decl);
4849 if (duplicate_decls (decl, field, /*newdecl_is_friend=*/false))
4850 decl = field;
4851 if (decl_spec_seq_has_spec_p (declspecs, ds_constexpr)
4852 && !DECL_DECLARED_CONSTEXPR_P (field))
4853 error ("%qD declared %<constexpr%> outside its class", field);
4856 else
4858 tree field = check_classfn (context, decl,
4859 this_tmpl
4860 ? current_template_parms
4861 : NULL_TREE);
4862 if (field && field != error_mark_node
4863 && duplicate_decls (decl, field,
4864 /*newdecl_is_friend=*/false))
4865 decl = field;
4868 /* cp_finish_decl sets DECL_EXTERNAL if DECL_IN_AGGR_P is set. */
4869 DECL_IN_AGGR_P (decl) = 0;
4870 /* Do not mark DECL as an explicit specialization if it was not
4871 already marked as an instantiation; a declaration should
4872 never be marked as a specialization unless we know what
4873 template is being specialized. */
4874 if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
4876 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
4877 if (TREE_CODE (decl) == FUNCTION_DECL)
4878 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
4879 && DECL_DECLARED_INLINE_P (decl));
4880 else
4881 DECL_COMDAT (decl) = false;
4883 /* [temp.expl.spec] An explicit specialization of a static data
4884 member of a template is a definition if the declaration
4885 includes an initializer; otherwise, it is a declaration.
4887 We check for processing_specialization so this only applies
4888 to the new specialization syntax. */
4889 if (!initialized && processing_specialization)
4890 DECL_EXTERNAL (decl) = 1;
4893 if (DECL_EXTERNAL (decl) && ! DECL_TEMPLATE_SPECIALIZATION (decl)
4894 /* Aliases are definitions. */
4895 && !alias)
4896 permerror (input_location, "declaration of %q#D outside of class is not definition",
4897 decl);
4900 was_public = TREE_PUBLIC (decl);
4902 /* Enter this declaration into the symbol table. */
4903 decl = maybe_push_decl (decl);
4905 if (processing_template_decl)
4906 decl = push_template_decl (decl);
4907 if (decl == error_mark_node)
4908 return error_mark_node;
4910 if (VAR_P (decl)
4911 && DECL_NAMESPACE_SCOPE_P (decl) && !TREE_PUBLIC (decl) && !was_public
4912 && !DECL_THIS_STATIC (decl) && !DECL_ARTIFICIAL (decl))
4914 /* This is a const variable with implicit 'static'. Set
4915 DECL_THIS_STATIC so we can tell it from variables that are
4916 !TREE_PUBLIC because of the anonymous namespace. */
4917 gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (decl)) || errorcount);
4918 DECL_THIS_STATIC (decl) = 1;
4921 if (current_function_decl && VAR_P (decl)
4922 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
4924 bool ok = false;
4925 if (DECL_THREAD_LOCAL_P (decl))
4926 error ("%qD declared %<thread_local%> in %<constexpr%> function",
4927 decl);
4928 else if (TREE_STATIC (decl))
4929 error ("%qD declared %<static%> in %<constexpr%> function", decl);
4930 else
4931 ok = true;
4932 if (!ok)
4933 cp_function_chain->invalid_constexpr = true;
4936 if (!processing_template_decl && VAR_P (decl))
4937 start_decl_1 (decl, initialized);
4939 return decl;
4942 /* Process the declaration of a variable DECL. INITIALIZED is true
4943 iff DECL is explicitly initialized. (INITIALIZED is false if the
4944 variable is initialized via an implicitly-called constructor.)
4945 This function must be called for ordinary variables (including, for
4946 example, implicit instantiations of templates), but must not be
4947 called for template declarations. */
4949 void
4950 start_decl_1 (tree decl, bool initialized)
4952 tree type;
4953 bool complete_p;
4954 bool aggregate_definition_p;
4956 gcc_assert (!processing_template_decl);
4958 if (error_operand_p (decl))
4959 return;
4961 gcc_assert (VAR_P (decl));
4963 type = TREE_TYPE (decl);
4964 complete_p = COMPLETE_TYPE_P (type);
4965 aggregate_definition_p = MAYBE_CLASS_TYPE_P (type) && !DECL_EXTERNAL (decl);
4967 /* If an explicit initializer is present, or if this is a definition
4968 of an aggregate, then we need a complete type at this point.
4969 (Scalars are always complete types, so there is nothing to
4970 check.) This code just sets COMPLETE_P; errors (if necessary)
4971 are issued below. */
4972 if ((initialized || aggregate_definition_p)
4973 && !complete_p
4974 && COMPLETE_TYPE_P (complete_type (type)))
4976 complete_p = true;
4977 /* We will not yet have set TREE_READONLY on DECL if the type
4978 was "const", but incomplete, before this point. But, now, we
4979 have a complete type, so we can try again. */
4980 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
4983 if (initialized)
4984 /* Is it valid for this decl to have an initializer at all? */
4986 /* Don't allow initializations for incomplete types except for
4987 arrays which might be completed by the initialization. */
4988 if (complete_p)
4989 ; /* A complete type is ok. */
4990 else if (type_uses_auto (type))
4991 ; /* An auto type is ok. */
4992 else if (TREE_CODE (type) != ARRAY_TYPE)
4994 error ("variable %q#D has initializer but incomplete type", decl);
4995 type = TREE_TYPE (decl) = error_mark_node;
4997 else if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
4999 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
5000 error ("elements of array %q#D have incomplete type", decl);
5001 /* else we already gave an error in start_decl. */
5004 else if (aggregate_definition_p && !complete_p)
5006 if (type_uses_auto (type))
5007 error ("declaration of %q#D has no initializer", decl);
5008 else
5009 error ("aggregate %q#D has incomplete type and cannot be defined",
5010 decl);
5011 /* Change the type so that assemble_variable will give
5012 DECL an rtl we can live with: (mem (const_int 0)). */
5013 type = TREE_TYPE (decl) = error_mark_node;
5016 /* Create a new scope to hold this declaration if necessary.
5017 Whether or not a new scope is necessary cannot be determined
5018 until after the type has been completed; if the type is a
5019 specialization of a class template it is not until after
5020 instantiation has occurred that TYPE_HAS_NONTRIVIAL_DESTRUCTOR
5021 will be set correctly. */
5022 maybe_push_cleanup_level (type);
5025 /* Handle initialization of references. DECL, TYPE, and INIT have the
5026 same meaning as in cp_finish_decl. *CLEANUP must be NULL on entry,
5027 but will be set to a new CLEANUP_STMT if a temporary is created
5028 that must be destroyed subsequently.
5030 Returns an initializer expression to use to initialize DECL, or
5031 NULL if the initialization can be performed statically.
5033 Quotes on semantics can be found in ARM 8.4.3. */
5035 static tree
5036 grok_reference_init (tree decl, tree type, tree init, int flags)
5038 if (init == NULL_TREE)
5040 if ((DECL_LANG_SPECIFIC (decl) == 0
5041 || DECL_IN_AGGR_P (decl) == 0)
5042 && ! DECL_THIS_EXTERN (decl))
5043 error ("%qD declared as reference but not initialized", decl);
5044 return NULL_TREE;
5047 if (TREE_CODE (init) == TREE_LIST)
5048 init = build_x_compound_expr_from_list (init, ELK_INIT,
5049 tf_warning_or_error);
5051 tree ttype = TREE_TYPE (type);
5052 if (TREE_CODE (ttype) != ARRAY_TYPE
5053 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
5054 /* Note: default conversion is only called in very special cases. */
5055 init = decay_conversion (init, tf_warning_or_error);
5057 /* check_initializer handles this for non-reference variables, but for
5058 references we need to do it here or the initializer will get the
5059 incomplete array type and confuse later calls to
5060 cp_complete_array_type. */
5061 if (TREE_CODE (ttype) == ARRAY_TYPE
5062 && TYPE_DOMAIN (ttype) == NULL_TREE
5063 && (BRACE_ENCLOSED_INITIALIZER_P (init)
5064 || TREE_CODE (init) == STRING_CST))
5066 cp_complete_array_type (&ttype, init, false);
5067 if (ttype != TREE_TYPE (type))
5068 type = cp_build_reference_type (ttype, TYPE_REF_IS_RVALUE (type));
5071 /* Convert INIT to the reference type TYPE. This may involve the
5072 creation of a temporary, whose lifetime must be the same as that
5073 of the reference. If so, a DECL_EXPR for the temporary will be
5074 added just after the DECL_EXPR for DECL. That's why we don't set
5075 DECL_INITIAL for local references (instead assigning to them
5076 explicitly); we need to allow the temporary to be initialized
5077 first. */
5078 return initialize_reference (type, init, flags,
5079 tf_warning_or_error);
5082 /* Designated initializers in arrays are not supported in GNU C++.
5083 The parser cannot detect this error since it does not know whether
5084 a given brace-enclosed initializer is for a class type or for an
5085 array. This function checks that CE does not use a designated
5086 initializer. If it does, an error is issued. Returns true if CE
5087 is valid, i.e., does not have a designated initializer. */
5089 static bool
5090 check_array_designated_initializer (constructor_elt *ce,
5091 unsigned HOST_WIDE_INT index)
5093 /* Designated initializers for array elements are not supported. */
5094 if (ce->index)
5096 /* The parser only allows identifiers as designated
5097 initializers. */
5098 if (ce->index == error_mark_node)
5100 error ("name used in a GNU-style designated "
5101 "initializer for an array");
5102 return false;
5104 else if (identifier_p (ce->index))
5106 error ("name %qD used in a GNU-style designated "
5107 "initializer for an array", ce->index);
5108 return false;
5111 tree ce_index = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5112 ce->index, true);
5113 if (ce_index
5114 && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (ce_index))
5115 && (TREE_CODE (ce_index = maybe_constant_value (ce_index))
5116 == INTEGER_CST))
5118 /* A C99 designator is OK if it matches the current index. */
5119 if (wi::eq_p (ce_index, index))
5120 return true;
5121 else
5122 sorry ("non-trivial designated initializers not supported");
5124 else
5125 error ("C99 designator %qE is not an integral constant-expression",
5126 ce->index);
5128 return false;
5131 return true;
5134 /* When parsing `int a[] = {1, 2};' we don't know the size of the
5135 array until we finish parsing the initializer. If that's the
5136 situation we're in, update DECL accordingly. */
5138 static void
5139 maybe_deduce_size_from_array_init (tree decl, tree init)
5141 tree type = TREE_TYPE (decl);
5143 if (TREE_CODE (type) == ARRAY_TYPE
5144 && TYPE_DOMAIN (type) == NULL_TREE
5145 && TREE_CODE (decl) != TYPE_DECL)
5147 /* do_default is really a C-ism to deal with tentative definitions.
5148 But let's leave it here to ease the eventual merge. */
5149 int do_default = !DECL_EXTERNAL (decl);
5150 tree initializer = init ? init : DECL_INITIAL (decl);
5151 int failure = 0;
5153 /* Check that there are no designated initializers in INIT, as
5154 those are not supported in GNU C++, and as the middle-end
5155 will crash if presented with a non-numeric designated
5156 initializer. */
5157 if (initializer && BRACE_ENCLOSED_INITIALIZER_P (initializer))
5159 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initializer);
5160 constructor_elt *ce;
5161 HOST_WIDE_INT i;
5162 FOR_EACH_VEC_SAFE_ELT (v, i, ce)
5163 if (!check_array_designated_initializer (ce, i))
5164 failure = 1;
5167 if (!failure)
5169 failure = cp_complete_array_type (&TREE_TYPE (decl), initializer,
5170 do_default);
5171 if (failure == 1)
5173 error ("initializer fails to determine size of %qD", decl);
5175 else if (failure == 2)
5177 if (do_default)
5179 error ("array size missing in %qD", decl);
5181 /* If a `static' var's size isn't known, make it extern as
5182 well as static, so it does not get allocated. If it's not
5183 `static', then don't mark it extern; finish_incomplete_decl
5184 will give it a default size and it will get allocated. */
5185 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
5186 DECL_EXTERNAL (decl) = 1;
5188 else if (failure == 3)
5190 error ("zero-size array %qD", decl);
5194 cp_apply_type_quals_to_decl (cp_type_quals (TREE_TYPE (decl)), decl);
5196 relayout_decl (decl);
5200 /* Set DECL_SIZE, DECL_ALIGN, etc. for DECL (a VAR_DECL), and issue
5201 any appropriate error messages regarding the layout. */
5203 static void
5204 layout_var_decl (tree decl)
5206 tree type;
5208 type = TREE_TYPE (decl);
5209 if (type == error_mark_node)
5210 return;
5212 /* If we haven't already laid out this declaration, do so now.
5213 Note that we must not call complete type for an external object
5214 because it's type might involve templates that we are not
5215 supposed to instantiate yet. (And it's perfectly valid to say
5216 `extern X x' for some incomplete type `X'.) */
5217 if (!DECL_EXTERNAL (decl))
5218 complete_type (type);
5219 if (!DECL_SIZE (decl)
5220 && TREE_TYPE (decl) != error_mark_node
5221 && (COMPLETE_TYPE_P (type)
5222 || (TREE_CODE (type) == ARRAY_TYPE
5223 && !TYPE_DOMAIN (type)
5224 && COMPLETE_TYPE_P (TREE_TYPE (type)))))
5225 layout_decl (decl, 0);
5227 if (!DECL_EXTERNAL (decl) && DECL_SIZE (decl) == NULL_TREE)
5229 /* An automatic variable with an incomplete type: that is an error.
5230 Don't talk about array types here, since we took care of that
5231 message in grokdeclarator. */
5232 error ("storage size of %qD isn%'t known", decl);
5233 TREE_TYPE (decl) = error_mark_node;
5235 #if 0
5236 /* Keep this code around in case we later want to control debug info
5237 based on whether a type is "used". (jason 1999-11-11) */
5239 else if (!DECL_EXTERNAL (decl) && MAYBE_CLASS_TYPE_P (ttype))
5240 /* Let debugger know it should output info for this type. */
5241 note_debug_info_needed (ttype);
5243 if (TREE_STATIC (decl) && DECL_CLASS_SCOPE_P (decl))
5244 note_debug_info_needed (DECL_CONTEXT (decl));
5245 #endif
5247 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
5248 && DECL_SIZE (decl) != NULL_TREE
5249 && ! TREE_CONSTANT (DECL_SIZE (decl)))
5251 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
5252 constant_expression_warning (DECL_SIZE (decl));
5253 else
5255 error ("storage size of %qD isn%'t constant", decl);
5256 TREE_TYPE (decl) = error_mark_node;
5261 /* If a local static variable is declared in an inline function, or if
5262 we have a weak definition, we must endeavor to create only one
5263 instance of the variable at link-time. */
5265 void
5266 maybe_commonize_var (tree decl)
5268 /* Static data in a function with comdat linkage also has comdat
5269 linkage. */
5270 if (TREE_STATIC (decl)
5271 /* Don't mess with __FUNCTION__. */
5272 && ! DECL_ARTIFICIAL (decl)
5273 && DECL_FUNCTION_SCOPE_P (decl)
5274 && vague_linkage_p (DECL_CONTEXT (decl)))
5276 if (flag_weak)
5278 /* With weak symbols, we simply make the variable COMDAT;
5279 that will cause copies in multiple translations units to
5280 be merged. */
5281 comdat_linkage (decl);
5283 else
5285 if (DECL_INITIAL (decl) == NULL_TREE
5286 || DECL_INITIAL (decl) == error_mark_node)
5288 /* Without weak symbols, we can use COMMON to merge
5289 uninitialized variables. */
5290 TREE_PUBLIC (decl) = 1;
5291 DECL_COMMON (decl) = 1;
5293 else
5295 /* While for initialized variables, we must use internal
5296 linkage -- which means that multiple copies will not
5297 be merged. */
5298 TREE_PUBLIC (decl) = 0;
5299 DECL_COMMON (decl) = 0;
5300 if (warning_at (DECL_SOURCE_LOCATION (decl), 0,
5301 "sorry: semantics of inline function static "
5302 "data %q#D are wrong (you%'ll wind up "
5303 "with multiple copies)", decl))
5304 inform (DECL_SOURCE_LOCATION (decl),
5305 "you can work around this by removing the initializer");
5311 /* Issue an error message if DECL is an uninitialized const variable. */
5313 static void
5314 check_for_uninitialized_const_var (tree decl)
5316 tree type = strip_array_types (TREE_TYPE (decl));
5318 /* ``Unless explicitly declared extern, a const object does not have
5319 external linkage and must be initialized. ($8.4; $12.1)'' ARM
5320 7.1.6 */
5321 if (VAR_P (decl)
5322 && TREE_CODE (type) != REFERENCE_TYPE
5323 && (CP_TYPE_CONST_P (type) || var_in_constexpr_fn (decl))
5324 && !DECL_INITIAL (decl))
5326 tree field = default_init_uninitialized_part (type);
5327 if (!field)
5328 return;
5330 if (CP_TYPE_CONST_P (type))
5331 permerror (DECL_SOURCE_LOCATION (decl),
5332 "uninitialized const %qD", decl);
5333 else
5335 error_at (DECL_SOURCE_LOCATION (decl),
5336 "uninitialized variable %qD in %<constexpr%> function",
5337 decl);
5338 cp_function_chain->invalid_constexpr = true;
5341 if (CLASS_TYPE_P (type))
5343 tree defaulted_ctor;
5345 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
5346 "%q#T has no user-provided default constructor", type);
5347 defaulted_ctor = in_class_defaulted_default_constructor (type);
5348 if (defaulted_ctor)
5349 inform (DECL_SOURCE_LOCATION (defaulted_ctor),
5350 "constructor is not user-provided because it is "
5351 "explicitly defaulted in the class body");
5352 inform (0, "and the implicitly-defined constructor does not "
5353 "initialize %q+#D", field);
5358 /* Structure holding the current initializer being processed by reshape_init.
5359 CUR is a pointer to the current element being processed, END is a pointer
5360 after the last element present in the initializer. */
5361 typedef struct reshape_iterator_t
5363 constructor_elt *cur;
5364 constructor_elt *end;
5365 } reshape_iter;
5367 static tree reshape_init_r (tree, reshape_iter *, bool, tsubst_flags_t);
5369 /* FIELD is a FIELD_DECL or NULL. In the former case, the value
5370 returned is the next FIELD_DECL (possibly FIELD itself) that can be
5371 initialized. If there are no more such fields, the return value
5372 will be NULL. */
5374 tree
5375 next_initializable_field (tree field)
5377 while (field
5378 && (TREE_CODE (field) != FIELD_DECL
5379 || (DECL_C_BIT_FIELD (field) && !DECL_NAME (field))
5380 || DECL_ARTIFICIAL (field)))
5381 field = DECL_CHAIN (field);
5383 return field;
5386 /* Subroutine of reshape_init_array and reshape_init_vector, which does
5387 the actual work. ELT_TYPE is the element type of the array. MAX_INDEX is an
5388 INTEGER_CST representing the size of the array minus one (the maximum index),
5389 or NULL_TREE if the array was declared without specifying the size. D is
5390 the iterator within the constructor. */
5392 static tree
5393 reshape_init_array_1 (tree elt_type, tree max_index, reshape_iter *d,
5394 tsubst_flags_t complain)
5396 tree new_init;
5397 bool sized_array_p = (max_index && TREE_CONSTANT (max_index));
5398 unsigned HOST_WIDE_INT max_index_cst = 0;
5399 unsigned HOST_WIDE_INT index;
5401 /* The initializer for an array is always a CONSTRUCTOR. */
5402 new_init = build_constructor (init_list_type_node, NULL);
5404 if (sized_array_p)
5406 /* Minus 1 is used for zero sized arrays. */
5407 if (integer_all_onesp (max_index))
5408 return new_init;
5410 if (tree_fits_uhwi_p (max_index))
5411 max_index_cst = tree_to_uhwi (max_index);
5412 /* sizetype is sign extended, not zero extended. */
5413 else
5414 max_index_cst = tree_to_uhwi (fold_convert (size_type_node, max_index));
5417 /* Loop until there are no more initializers. */
5418 for (index = 0;
5419 d->cur != d->end && (!sized_array_p || index <= max_index_cst);
5420 ++index)
5422 tree elt_init;
5423 constructor_elt *old_cur = d->cur;
5425 check_array_designated_initializer (d->cur, index);
5426 elt_init = reshape_init_r (elt_type, d, /*first_initializer_p=*/false,
5427 complain);
5428 if (elt_init == error_mark_node)
5429 return error_mark_node;
5430 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init),
5431 size_int (index), elt_init);
5432 if (!TREE_CONSTANT (elt_init))
5433 TREE_CONSTANT (new_init) = false;
5435 /* This can happen with an invalid initializer (c++/54501). */
5436 if (d->cur == old_cur && !sized_array_p)
5437 break;
5440 return new_init;
5443 /* Subroutine of reshape_init_r, processes the initializers for arrays.
5444 Parameters are the same of reshape_init_r. */
5446 static tree
5447 reshape_init_array (tree type, reshape_iter *d, tsubst_flags_t complain)
5449 tree max_index = NULL_TREE;
5451 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
5453 if (TYPE_DOMAIN (type))
5454 max_index = array_type_nelts (type);
5456 return reshape_init_array_1 (TREE_TYPE (type), max_index, d, complain);
5459 /* Subroutine of reshape_init_r, processes the initializers for vectors.
5460 Parameters are the same of reshape_init_r. */
5462 static tree
5463 reshape_init_vector (tree type, reshape_iter *d, tsubst_flags_t complain)
5465 tree max_index = NULL_TREE;
5467 gcc_assert (TREE_CODE (type) == VECTOR_TYPE);
5469 if (COMPOUND_LITERAL_P (d->cur->value))
5471 tree value = d->cur->value;
5472 if (!same_type_p (TREE_TYPE (value), type))
5474 if (complain & tf_error)
5475 error ("invalid type %qT as initializer for a vector of type %qT",
5476 TREE_TYPE (d->cur->value), type);
5477 value = error_mark_node;
5479 ++d->cur;
5480 return value;
5483 /* For a vector, we initialize it as an array of the appropriate size. */
5484 if (TREE_CODE (type) == VECTOR_TYPE)
5485 max_index = size_int (TYPE_VECTOR_SUBPARTS (type) - 1);
5487 return reshape_init_array_1 (TREE_TYPE (type), max_index, d, complain);
5490 /* Subroutine of reshape_init_r, processes the initializers for classes
5491 or union. Parameters are the same of reshape_init_r. */
5493 static tree
5494 reshape_init_class (tree type, reshape_iter *d, bool first_initializer_p,
5495 tsubst_flags_t complain)
5497 tree field;
5498 tree new_init;
5500 gcc_assert (CLASS_TYPE_P (type));
5502 /* The initializer for a class is always a CONSTRUCTOR. */
5503 new_init = build_constructor (init_list_type_node, NULL);
5504 field = next_initializable_field (TYPE_FIELDS (type));
5506 if (!field)
5508 /* [dcl.init.aggr]
5510 An initializer for an aggregate member that is an
5511 empty class shall have the form of an empty
5512 initializer-list {}. */
5513 if (!first_initializer_p)
5515 if (complain & tf_error)
5516 error ("initializer for %qT must be brace-enclosed", type);
5517 return error_mark_node;
5519 return new_init;
5522 /* Loop through the initializable fields, gathering initializers. */
5523 while (d->cur != d->end)
5525 tree field_init;
5526 constructor_elt *old_cur = d->cur;
5528 /* Handle designated initializers, as an extension. */
5529 if (d->cur->index)
5531 if (d->cur->index == error_mark_node)
5532 return error_mark_node;
5534 if (TREE_CODE (d->cur->index) == FIELD_DECL)
5535 /* We already reshaped this. */
5536 gcc_assert (d->cur->index == field);
5537 else if (TREE_CODE (d->cur->index) == IDENTIFIER_NODE)
5538 field = lookup_field_1 (type, d->cur->index, /*want_type=*/false);
5539 else
5541 if (complain & tf_error)
5542 error ("%<[%E] =%> used in a GNU-style designated initializer"
5543 " for class %qT", d->cur->index, type);
5544 return error_mark_node;
5547 if (!field || TREE_CODE (field) != FIELD_DECL)
5549 if (complain & tf_error)
5550 error ("%qT has no non-static data member named %qD", type,
5551 d->cur->index);
5552 return error_mark_node;
5556 /* If we processed all the member of the class, we are done. */
5557 if (!field)
5558 break;
5560 field_init = reshape_init_r (TREE_TYPE (field), d,
5561 /*first_initializer_p=*/false, complain);
5562 if (field_init == error_mark_node)
5563 return error_mark_node;
5565 if (d->cur == old_cur && d->cur->index)
5567 /* This can happen with an invalid initializer for a flexible
5568 array member (c++/54441). */
5569 if (complain & tf_error)
5570 error ("invalid initializer for %q#D", field);
5571 return error_mark_node;
5574 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init), field, field_init);
5576 /* [dcl.init.aggr]
5578 When a union is initialized with a brace-enclosed
5579 initializer, the braces shall only contain an
5580 initializer for the first member of the union. */
5581 if (TREE_CODE (type) == UNION_TYPE)
5582 break;
5584 field = next_initializable_field (DECL_CHAIN (field));
5587 return new_init;
5590 /* Subroutine of reshape_init_r. We're in a context where C99 initializer
5591 designators are not valid; either complain or return true to indicate
5592 that reshape_init_r should return error_mark_node. */
5594 static bool
5595 has_designator_problem (reshape_iter *d, tsubst_flags_t complain)
5597 if (d->cur->index)
5599 if (complain & tf_error)
5600 error ("C99 designator %qE outside aggregate initializer",
5601 d->cur->index);
5602 else
5603 return true;
5605 return false;
5608 /* Subroutine of reshape_init, which processes a single initializer (part of
5609 a CONSTRUCTOR). TYPE is the type of the variable being initialized, D is the
5610 iterator within the CONSTRUCTOR which points to the initializer to process.
5611 FIRST_INITIALIZER_P is true if this is the first initializer of the
5612 outermost CONSTRUCTOR node. */
5614 static tree
5615 reshape_init_r (tree type, reshape_iter *d, bool first_initializer_p,
5616 tsubst_flags_t complain)
5618 tree init = d->cur->value;
5620 if (error_operand_p (init))
5621 return error_mark_node;
5623 if (first_initializer_p && !CP_AGGREGATE_TYPE_P (type)
5624 && has_designator_problem (d, complain))
5625 return error_mark_node;
5627 if (TREE_CODE (type) == COMPLEX_TYPE)
5629 /* A complex type can be initialized from one or two initializers,
5630 but braces are not elided. */
5631 d->cur++;
5632 if (BRACE_ENCLOSED_INITIALIZER_P (init))
5634 if (CONSTRUCTOR_NELTS (init) > 2)
5636 if (complain & tf_error)
5637 error ("too many initializers for %qT", type);
5638 else
5639 return error_mark_node;
5642 else if (first_initializer_p && d->cur != d->end)
5644 vec<constructor_elt, va_gc> *v = 0;
5645 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, init);
5646 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, d->cur->value);
5647 if (has_designator_problem (d, complain))
5648 return error_mark_node;
5649 d->cur++;
5650 init = build_constructor (init_list_type_node, v);
5652 return init;
5655 /* A non-aggregate type is always initialized with a single
5656 initializer. */
5657 if (!CP_AGGREGATE_TYPE_P (type))
5659 /* It is invalid to initialize a non-aggregate type with a
5660 brace-enclosed initializer before C++0x.
5661 We need to check for BRACE_ENCLOSED_INITIALIZER_P here because
5662 of g++.old-deja/g++.mike/p7626.C: a pointer-to-member constant is
5663 a CONSTRUCTOR (with a record type). */
5664 if (TREE_CODE (init) == CONSTRUCTOR
5665 /* Don't complain about a capture-init. */
5666 && !CONSTRUCTOR_IS_DIRECT_INIT (init)
5667 && BRACE_ENCLOSED_INITIALIZER_P (init)) /* p7626.C */
5669 if (SCALAR_TYPE_P (type))
5671 if (cxx_dialect < cxx11
5672 /* Isn't value-initialization. */
5673 || CONSTRUCTOR_NELTS (init) > 0)
5675 if (complain & tf_error)
5676 error ("braces around scalar initializer for type %qT",
5677 type);
5678 init = error_mark_node;
5681 else
5682 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5685 d->cur++;
5686 return init;
5689 /* "If T is a class type and the initializer list has a single element of
5690 type cv U, where U is T or a class derived from T, the object is
5691 initialized from that element." Even if T is an aggregate. */
5692 if (cxx_dialect >= cxx11 && (CLASS_TYPE_P (type) || VECTOR_TYPE_P (type))
5693 && first_initializer_p
5694 && d->end - d->cur == 1
5695 && reference_related_p (type, TREE_TYPE (init)))
5697 d->cur++;
5698 return init;
5701 /* [dcl.init.aggr]
5703 All implicit type conversions (clause _conv_) are considered when
5704 initializing the aggregate member with an initializer from an
5705 initializer-list. If the initializer can initialize a member,
5706 the member is initialized. Otherwise, if the member is itself a
5707 non-empty subaggregate, brace elision is assumed and the
5708 initializer is considered for the initialization of the first
5709 member of the subaggregate. */
5710 if (TREE_CODE (init) != CONSTRUCTOR
5711 /* But don't try this for the first initializer, since that would be
5712 looking through the outermost braces; A a2 = { a1 }; is not a
5713 valid aggregate initialization. */
5714 && !first_initializer_p
5715 && (same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (init))
5716 || can_convert_arg (type, TREE_TYPE (init), init, LOOKUP_NORMAL,
5717 complain)))
5719 d->cur++;
5720 return init;
5723 /* [dcl.init.string]
5725 A char array (whether plain char, signed char, or unsigned char)
5726 can be initialized by a string-literal (optionally enclosed in
5727 braces); a wchar_t array can be initialized by a wide
5728 string-literal (optionally enclosed in braces). */
5729 if (TREE_CODE (type) == ARRAY_TYPE
5730 && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type))))
5732 tree str_init = init;
5734 /* Strip one level of braces if and only if they enclose a single
5735 element (as allowed by [dcl.init.string]). */
5736 if (!first_initializer_p
5737 && TREE_CODE (str_init) == CONSTRUCTOR
5738 && vec_safe_length (CONSTRUCTOR_ELTS (str_init)) == 1)
5740 str_init = (*CONSTRUCTOR_ELTS (str_init))[0].value;
5743 /* If it's a string literal, then it's the initializer for the array
5744 as a whole. Otherwise, continue with normal initialization for
5745 array types (one value per array element). */
5746 if (TREE_CODE (str_init) == STRING_CST)
5748 if (has_designator_problem (d, complain))
5749 return error_mark_node;
5750 d->cur++;
5751 return str_init;
5755 /* The following cases are about aggregates. If we are not within a full
5756 initializer already, and there is not a CONSTRUCTOR, it means that there
5757 is a missing set of braces (that is, we are processing the case for
5758 which reshape_init exists). */
5759 if (!first_initializer_p)
5761 if (TREE_CODE (init) == CONSTRUCTOR)
5763 if (TREE_TYPE (init) && TYPE_PTRMEMFUNC_P (TREE_TYPE (init)))
5764 /* There is no need to reshape pointer-to-member function
5765 initializers, as they are always constructed correctly
5766 by the front end. */
5768 else if (COMPOUND_LITERAL_P (init))
5769 /* For a nested compound literal, there is no need to reshape since
5770 brace elision is not allowed. Even if we decided to allow it,
5771 we should add a call to reshape_init in finish_compound_literal,
5772 before calling digest_init, so changing this code would still
5773 not be necessary. */
5774 gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (init));
5775 else
5777 ++d->cur;
5778 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
5779 return reshape_init (type, init, complain);
5783 warning (OPT_Wmissing_braces, "missing braces around initializer for %qT",
5784 type);
5787 /* Dispatch to specialized routines. */
5788 if (CLASS_TYPE_P (type))
5789 return reshape_init_class (type, d, first_initializer_p, complain);
5790 else if (TREE_CODE (type) == ARRAY_TYPE)
5791 return reshape_init_array (type, d, complain);
5792 else if (TREE_CODE (type) == VECTOR_TYPE)
5793 return reshape_init_vector (type, d, complain);
5794 else
5795 gcc_unreachable();
5798 /* Undo the brace-elision allowed by [dcl.init.aggr] in a
5799 brace-enclosed aggregate initializer.
5801 INIT is the CONSTRUCTOR containing the list of initializers describing
5802 a brace-enclosed initializer for an entity of the indicated aggregate TYPE.
5803 It may not presently match the shape of the TYPE; for example:
5805 struct S { int a; int b; };
5806 struct S a[] = { 1, 2, 3, 4 };
5808 Here INIT will hold a vector of four elements, rather than a
5809 vector of two elements, each itself a vector of two elements. This
5810 routine transforms INIT from the former form into the latter. The
5811 revised CONSTRUCTOR node is returned. */
5813 tree
5814 reshape_init (tree type, tree init, tsubst_flags_t complain)
5816 vec<constructor_elt, va_gc> *v;
5817 reshape_iter d;
5818 tree new_init;
5820 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
5822 v = CONSTRUCTOR_ELTS (init);
5824 /* An empty constructor does not need reshaping, and it is always a valid
5825 initializer. */
5826 if (vec_safe_is_empty (v))
5827 return init;
5829 /* Recurse on this CONSTRUCTOR. */
5830 d.cur = &(*v)[0];
5831 d.end = d.cur + v->length ();
5833 new_init = reshape_init_r (type, &d, true, complain);
5834 if (new_init == error_mark_node)
5835 return error_mark_node;
5837 /* Make sure all the element of the constructor were used. Otherwise,
5838 issue an error about exceeding initializers. */
5839 if (d.cur != d.end)
5841 if (complain & tf_error)
5842 error ("too many initializers for %qT", type);
5843 else
5844 return error_mark_node;
5847 return new_init;
5850 /* Verify array initializer. Returns true if errors have been reported. */
5852 bool
5853 check_array_initializer (tree decl, tree type, tree init)
5855 tree element_type = TREE_TYPE (type);
5857 /* The array type itself need not be complete, because the
5858 initializer may tell us how many elements are in the array.
5859 But, the elements of the array must be complete. */
5860 if (!COMPLETE_TYPE_P (complete_type (element_type)))
5862 if (decl)
5863 error ("elements of array %q#D have incomplete type", decl);
5864 else
5865 error ("elements of array %q#T have incomplete type", type);
5866 return true;
5868 /* A compound literal can't have variable size. */
5869 if (init && !decl
5870 && ((COMPLETE_TYPE_P (type) && !TREE_CONSTANT (TYPE_SIZE (type)))
5871 || !TREE_CONSTANT (TYPE_SIZE (element_type))))
5873 error ("variable-sized compound literal");
5874 return true;
5876 return false;
5879 /* Subroutine of check_initializer; args are passed down from that function.
5880 Set stmts_are_full_exprs_p to 1 across a call to build_aggr_init. */
5882 static tree
5883 build_aggr_init_full_exprs (tree decl, tree init, int flags)
5886 gcc_assert (stmts_are_full_exprs_p ());
5887 return build_aggr_init (decl, init, flags, tf_warning_or_error);
5890 /* Verify INIT (the initializer for DECL), and record the
5891 initialization in DECL_INITIAL, if appropriate. CLEANUP is as for
5892 grok_reference_init.
5894 If the return value is non-NULL, it is an expression that must be
5895 evaluated dynamically to initialize DECL. */
5897 static tree
5898 check_initializer (tree decl, tree init, int flags, vec<tree, va_gc> **cleanups)
5900 tree type = TREE_TYPE (decl);
5901 tree init_code = NULL;
5902 tree core_type;
5904 /* Things that are going to be initialized need to have complete
5905 type. */
5906 TREE_TYPE (decl) = type = complete_type (TREE_TYPE (decl));
5908 if (DECL_HAS_VALUE_EXPR_P (decl))
5910 /* A variable with DECL_HAS_VALUE_EXPR_P set is just a placeholder,
5911 it doesn't have storage to be initialized. */
5912 gcc_assert (init == NULL_TREE);
5913 return NULL_TREE;
5916 if (type == error_mark_node)
5917 /* We will have already complained. */
5918 return NULL_TREE;
5920 if (TREE_CODE (type) == ARRAY_TYPE)
5922 if (check_array_initializer (decl, type, init))
5923 return NULL_TREE;
5925 else if (!COMPLETE_TYPE_P (type))
5927 error ("%q#D has incomplete type", decl);
5928 TREE_TYPE (decl) = error_mark_node;
5929 return NULL_TREE;
5931 else
5932 /* There is no way to make a variable-sized class type in GNU C++. */
5933 gcc_assert (TREE_CONSTANT (TYPE_SIZE (type)));
5935 if (init && BRACE_ENCLOSED_INITIALIZER_P (init))
5937 int init_len = vec_safe_length (CONSTRUCTOR_ELTS (init));
5938 if (SCALAR_TYPE_P (type))
5940 if (init_len == 0)
5942 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5943 init = build_zero_init (type, NULL_TREE, false);
5945 else if (init_len != 1 && TREE_CODE (type) != COMPLEX_TYPE)
5947 error ("scalar object %qD requires one element in initializer",
5948 decl);
5949 TREE_TYPE (decl) = error_mark_node;
5950 return NULL_TREE;
5955 if (TREE_CODE (decl) == CONST_DECL)
5957 gcc_assert (TREE_CODE (type) != REFERENCE_TYPE);
5959 DECL_INITIAL (decl) = init;
5961 gcc_assert (init != NULL_TREE);
5962 init = NULL_TREE;
5964 else if (!init && DECL_REALLY_EXTERN (decl))
5966 else if (init || type_build_ctor_call (type)
5967 || TREE_CODE (type) == REFERENCE_TYPE)
5969 if (TREE_CODE (type) == REFERENCE_TYPE)
5971 init = grok_reference_init (decl, type, init, flags);
5972 flags |= LOOKUP_ALREADY_DIGESTED;
5974 else if (!init)
5975 check_for_uninitialized_const_var (decl);
5976 /* Do not reshape constructors of vectors (they don't need to be
5977 reshaped. */
5978 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
5980 if (is_std_init_list (type))
5982 init = perform_implicit_conversion (type, init,
5983 tf_warning_or_error);
5984 flags |= LOOKUP_ALREADY_DIGESTED;
5986 else if (TYPE_NON_AGGREGATE_CLASS (type))
5988 /* Don't reshape if the class has constructors. */
5989 if (cxx_dialect == cxx98)
5990 error ("in C++98 %qD must be initialized by constructor, "
5991 "not by %<{...}%>",
5992 decl);
5994 else if (TREE_CODE (type) == VECTOR_TYPE && TYPE_VECTOR_OPAQUE (type))
5996 error ("opaque vector types cannot be initialized");
5997 init = error_mark_node;
5999 else
6001 init = reshape_init (type, init, tf_warning_or_error);
6002 flags |= LOOKUP_NO_NARROWING;
6005 else if (TREE_CODE (init) == TREE_LIST
6006 && TREE_TYPE (init) != unknown_type_node
6007 && !MAYBE_CLASS_TYPE_P (type))
6009 gcc_assert (TREE_CODE (decl) != RESULT_DECL);
6011 /* We get here with code like `int a (2);' */
6012 init = build_x_compound_expr_from_list (init, ELK_INIT,
6013 tf_warning_or_error);
6016 /* If DECL has an array type without a specific bound, deduce the
6017 array size from the initializer. */
6018 maybe_deduce_size_from_array_init (decl, init);
6019 type = TREE_TYPE (decl);
6020 if (type == error_mark_node)
6021 return NULL_TREE;
6023 if ((type_build_ctor_call (type) || CLASS_TYPE_P (type))
6024 && !(flags & LOOKUP_ALREADY_DIGESTED)
6025 && !(init && BRACE_ENCLOSED_INITIALIZER_P (init)
6026 && CP_AGGREGATE_TYPE_P (type)
6027 && (CLASS_TYPE_P (type)
6028 || !TYPE_NEEDS_CONSTRUCTING (type)
6029 || type_has_extended_temps (type))))
6031 init_code = build_aggr_init_full_exprs (decl, init, flags);
6033 /* A constructor call is a non-trivial initializer even if
6034 it isn't explicitly written. */
6035 if (TREE_SIDE_EFFECTS (init_code))
6036 DECL_NONTRIVIALLY_INITIALIZED_P (decl) = true;
6038 /* If this is a constexpr initializer, expand_default_init will
6039 have returned an INIT_EXPR rather than a CALL_EXPR. In that
6040 case, pull the initializer back out and pass it down into
6041 store_init_value. */
6042 while (TREE_CODE (init_code) == EXPR_STMT
6043 || TREE_CODE (init_code) == CONVERT_EXPR)
6044 init_code = TREE_OPERAND (init_code, 0);
6045 if (TREE_CODE (init_code) == INIT_EXPR)
6047 init = TREE_OPERAND (init_code, 1);
6048 init_code = NULL_TREE;
6049 /* Don't call digest_init; it's unnecessary and will complain
6050 about aggregate initialization of non-aggregate classes. */
6051 flags |= LOOKUP_ALREADY_DIGESTED;
6053 else if (DECL_DECLARED_CONSTEXPR_P (decl))
6055 /* Declared constexpr, but no suitable initializer; massage
6056 init appropriately so we can pass it into store_init_value
6057 for the error. */
6058 if (CLASS_TYPE_P (type)
6059 && (!init || TREE_CODE (init) == TREE_LIST))
6061 init = build_functional_cast (type, init, tf_none);
6062 if (TREE_CODE (init) == TARGET_EXPR)
6063 TARGET_EXPR_DIRECT_INIT_P (init) = true;
6065 init_code = NULL_TREE;
6067 else
6068 init = NULL_TREE;
6071 if (init && TREE_CODE (init) != TREE_VEC)
6073 /* In aggregate initialization of a variable, each element
6074 initialization is a full-expression because there is no
6075 enclosing expression. */
6076 gcc_assert (stmts_are_full_exprs_p ());
6078 init_code = store_init_value (decl, init, cleanups, flags);
6080 if (pedantic && TREE_CODE (type) == ARRAY_TYPE
6081 && DECL_INITIAL (decl)
6082 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
6083 && PAREN_STRING_LITERAL_P (DECL_INITIAL (decl)))
6084 warning (0, "array %qD initialized by parenthesized string literal %qE",
6085 decl, DECL_INITIAL (decl));
6086 init = NULL;
6089 else
6091 if (CLASS_TYPE_P (core_type = strip_array_types (type))
6092 && (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type)
6093 || CLASSTYPE_REF_FIELDS_NEED_INIT (core_type)))
6094 diagnose_uninitialized_cst_or_ref_member (core_type, /*using_new=*/false,
6095 /*complain=*/true);
6097 check_for_uninitialized_const_var (decl);
6100 if (init && init != error_mark_node)
6101 init_code = build2 (INIT_EXPR, type, decl, init);
6103 if (init_code)
6105 /* We might have set these in cp_finish_decl. */
6106 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = false;
6107 TREE_CONSTANT (decl) = false;
6110 if (init_code && DECL_IN_AGGR_P (decl))
6112 static int explained = 0;
6114 if (cxx_dialect < cxx11)
6115 error ("initializer invalid for static member with constructor");
6116 else
6117 error ("non-constant in-class initialization invalid for static "
6118 "member %qD", decl);
6119 if (!explained)
6121 inform (input_location,
6122 "(an out of class initialization is required)");
6123 explained = 1;
6125 return NULL_TREE;
6128 return init_code;
6131 /* If DECL is not a local variable, give it RTL. */
6133 static void
6134 make_rtl_for_nonlocal_decl (tree decl, tree init, const char* asmspec)
6136 int toplev = toplevel_bindings_p ();
6137 int defer_p;
6138 const char *filename;
6140 /* Set the DECL_ASSEMBLER_NAME for the object. */
6141 if (asmspec)
6143 /* The `register' keyword, when used together with an
6144 asm-specification, indicates that the variable should be
6145 placed in a particular register. */
6146 if (VAR_P (decl) && DECL_REGISTER (decl))
6148 set_user_assembler_name (decl, asmspec);
6149 DECL_HARD_REGISTER (decl) = 1;
6151 else
6153 if (TREE_CODE (decl) == FUNCTION_DECL
6154 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
6155 set_builtin_user_assembler_name (decl, asmspec);
6156 set_user_assembler_name (decl, asmspec);
6160 /* Handle non-variables up front. */
6161 if (!VAR_P (decl))
6163 rest_of_decl_compilation (decl, toplev, at_eof);
6164 return;
6167 /* If we see a class member here, it should be a static data
6168 member. */
6169 if (DECL_LANG_SPECIFIC (decl) && DECL_IN_AGGR_P (decl))
6171 gcc_assert (TREE_STATIC (decl));
6172 /* An in-class declaration of a static data member should be
6173 external; it is only a declaration, and not a definition. */
6174 if (init == NULL_TREE)
6175 gcc_assert (DECL_EXTERNAL (decl) || !TREE_PUBLIC (decl));
6178 /* We don't create any RTL for local variables. */
6179 if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
6180 return;
6182 /* We defer emission of local statics until the corresponding
6183 DECL_EXPR is expanded. */
6184 defer_p = DECL_FUNCTION_SCOPE_P (decl) || DECL_VIRTUAL_P (decl);
6186 /* We try to defer namespace-scope static constants so that they are
6187 not emitted into the object file unnecessarily. */
6188 filename = LOCATION_FILE (input_location);
6189 if (!DECL_VIRTUAL_P (decl)
6190 && TREE_READONLY (decl)
6191 && DECL_INITIAL (decl) != NULL_TREE
6192 && DECL_INITIAL (decl) != error_mark_node
6193 && filename != NULL
6194 && ! EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl))
6195 && toplev
6196 && !TREE_PUBLIC (decl))
6198 /* Fool with the linkage of static consts according to #pragma
6199 interface. */
6200 struct c_fileinfo *finfo = get_fileinfo (filename);
6201 if (!finfo->interface_unknown && !TREE_PUBLIC (decl))
6203 TREE_PUBLIC (decl) = 1;
6204 DECL_EXTERNAL (decl) = finfo->interface_only;
6207 defer_p = 1;
6209 /* Likewise for template instantiations. */
6210 else if (DECL_LANG_SPECIFIC (decl)
6211 && DECL_IMPLICIT_INSTANTIATION (decl))
6212 defer_p = 1;
6214 /* If we're not deferring, go ahead and assemble the variable. */
6215 if (!defer_p)
6216 rest_of_decl_compilation (decl, toplev, at_eof);
6219 /* walk_tree helper for wrap_temporary_cleanups, below. */
6221 static tree
6222 wrap_cleanups_r (tree *stmt_p, int *walk_subtrees, void *data)
6224 /* Stop at types or full-expression boundaries. */
6225 if (TYPE_P (*stmt_p)
6226 || TREE_CODE (*stmt_p) == CLEANUP_POINT_EXPR)
6228 *walk_subtrees = 0;
6229 return NULL_TREE;
6232 if (TREE_CODE (*stmt_p) == TARGET_EXPR)
6234 tree guard = (tree)data;
6235 tree tcleanup = TARGET_EXPR_CLEANUP (*stmt_p);
6237 tcleanup = build2 (TRY_CATCH_EXPR, void_type_node, tcleanup, guard);
6238 /* Tell honor_protect_cleanup_actions to handle this as a separate
6239 cleanup. */
6240 TRY_CATCH_IS_CLEANUP (tcleanup) = 1;
6242 TARGET_EXPR_CLEANUP (*stmt_p) = tcleanup;
6245 return NULL_TREE;
6248 /* We're initializing a local variable which has a cleanup GUARD. If there
6249 are any temporaries used in the initializer INIT of this variable, we
6250 need to wrap their cleanups with TRY_CATCH_EXPR (, GUARD) so that the
6251 variable will be cleaned up properly if one of them throws.
6253 Unfortunately, there's no way to express this properly in terms of
6254 nesting, as the regions for the temporaries overlap the region for the
6255 variable itself; if there are two temporaries, the variable needs to be
6256 the first thing destroyed if either of them throws. However, we only
6257 want to run the variable's cleanup if it actually got constructed. So
6258 we need to guard the temporary cleanups with the variable's cleanup if
6259 they are run on the normal path, but not if they are run on the
6260 exceptional path. We implement this by telling
6261 honor_protect_cleanup_actions to strip the variable cleanup from the
6262 exceptional path. */
6264 static void
6265 wrap_temporary_cleanups (tree init, tree guard)
6267 cp_walk_tree_without_duplicates (&init, wrap_cleanups_r, (void *)guard);
6270 /* Generate code to initialize DECL (a local variable). */
6272 static void
6273 initialize_local_var (tree decl, tree init)
6275 tree type = TREE_TYPE (decl);
6276 tree cleanup;
6277 int already_used;
6279 gcc_assert (VAR_P (decl)
6280 || TREE_CODE (decl) == RESULT_DECL);
6281 gcc_assert (!TREE_STATIC (decl));
6283 if (DECL_SIZE (decl) == NULL_TREE)
6285 /* If we used it already as memory, it must stay in memory. */
6286 DECL_INITIAL (decl) = NULL_TREE;
6287 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
6288 return;
6291 if (type == error_mark_node)
6292 return;
6294 /* Compute and store the initial value. */
6295 already_used = TREE_USED (decl) || TREE_USED (type);
6296 if (TREE_USED (type))
6297 DECL_READ_P (decl) = 1;
6299 /* Generate a cleanup, if necessary. */
6300 cleanup = cxx_maybe_build_cleanup (decl, tf_warning_or_error);
6302 /* Perform the initialization. */
6303 if (init)
6305 tree rinit = (TREE_CODE (init) == INIT_EXPR
6306 ? TREE_OPERAND (init, 1) : NULL_TREE);
6307 if (rinit && !TREE_SIDE_EFFECTS (rinit))
6309 /* Stick simple initializers in DECL_INITIAL so that
6310 -Wno-init-self works (c++/34772). */
6311 gcc_assert (TREE_OPERAND (init, 0) == decl);
6312 DECL_INITIAL (decl) = rinit;
6314 if (warn_init_self && TREE_CODE (type) == REFERENCE_TYPE)
6316 STRIP_NOPS (rinit);
6317 if (rinit == decl)
6318 warning_at (DECL_SOURCE_LOCATION (decl),
6319 OPT_Winit_self,
6320 "reference %qD is initialized with itself", decl);
6323 else
6325 int saved_stmts_are_full_exprs_p;
6327 /* If we're only initializing a single object, guard the
6328 destructors of any temporaries used in its initializer with
6329 its destructor. This isn't right for arrays because each
6330 element initialization is a full-expression. */
6331 if (cleanup && TREE_CODE (type) != ARRAY_TYPE)
6332 wrap_temporary_cleanups (init, cleanup);
6334 gcc_assert (building_stmt_list_p ());
6335 saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
6336 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
6337 finish_expr_stmt (init);
6338 current_stmt_tree ()->stmts_are_full_exprs_p =
6339 saved_stmts_are_full_exprs_p;
6343 /* Set this to 0 so we can tell whether an aggregate which was
6344 initialized was ever used. Don't do this if it has a
6345 destructor, so we don't complain about the 'resource
6346 allocation is initialization' idiom. Now set
6347 attribute((unused)) on types so decls of that type will be
6348 marked used. (see TREE_USED, above.) */
6349 if (TYPE_NEEDS_CONSTRUCTING (type)
6350 && ! already_used
6351 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type)
6352 && DECL_NAME (decl))
6353 TREE_USED (decl) = 0;
6354 else if (already_used)
6355 TREE_USED (decl) = 1;
6357 if (cleanup)
6358 finish_decl_cleanup (decl, cleanup);
6361 /* DECL is a VAR_DECL for a compiler-generated variable with static
6362 storage duration (like a virtual table) whose initializer is a
6363 compile-time constant. Initialize the variable and provide it to the
6364 back end. */
6366 void
6367 initialize_artificial_var (tree decl, vec<constructor_elt, va_gc> *v)
6369 tree init;
6370 gcc_assert (DECL_ARTIFICIAL (decl));
6371 init = build_constructor (TREE_TYPE (decl), v);
6372 gcc_assert (TREE_CODE (init) == CONSTRUCTOR);
6373 DECL_INITIAL (decl) = init;
6374 DECL_INITIALIZED_P (decl) = 1;
6375 determine_visibility (decl);
6376 layout_var_decl (decl);
6377 maybe_commonize_var (decl);
6378 make_rtl_for_nonlocal_decl (decl, init, /*asmspec=*/NULL);
6381 /* INIT is the initializer for a variable, as represented by the
6382 parser. Returns true iff INIT is type-dependent. */
6384 static bool
6385 type_dependent_init_p (tree init)
6387 if (TREE_CODE (init) == TREE_LIST)
6388 /* A parenthesized initializer, e.g.: int i (3, 2); ? */
6389 return any_type_dependent_elements_p (init);
6390 else if (TREE_CODE (init) == CONSTRUCTOR)
6391 /* A brace-enclosed initializer, e.g.: int i = { 3 }; ? */
6393 vec<constructor_elt, va_gc> *elts;
6394 size_t nelts;
6395 size_t i;
6397 elts = CONSTRUCTOR_ELTS (init);
6398 nelts = vec_safe_length (elts);
6399 for (i = 0; i < nelts; ++i)
6400 if (type_dependent_init_p ((*elts)[i].value))
6401 return true;
6403 else
6404 /* It must be a simple expression, e.g., int i = 3; */
6405 return type_dependent_expression_p (init);
6407 return false;
6410 /* INIT is the initializer for a variable, as represented by the
6411 parser. Returns true iff INIT is value-dependent. */
6413 static bool
6414 value_dependent_init_p (tree init)
6416 if (TREE_CODE (init) == TREE_LIST)
6417 /* A parenthesized initializer, e.g.: int i (3, 2); ? */
6418 return any_value_dependent_elements_p (init);
6419 else if (TREE_CODE (init) == CONSTRUCTOR)
6420 /* A brace-enclosed initializer, e.g.: int i = { 3 }; ? */
6422 vec<constructor_elt, va_gc> *elts;
6423 size_t nelts;
6424 size_t i;
6426 elts = CONSTRUCTOR_ELTS (init);
6427 nelts = vec_safe_length (elts);
6428 for (i = 0; i < nelts; ++i)
6429 if (value_dependent_init_p ((*elts)[i].value))
6430 return true;
6432 else
6433 /* It must be a simple expression, e.g., int i = 3; */
6434 return value_dependent_expression_p (init);
6436 return false;
6439 // Returns true if a DECL is VAR_DECL with the concept specifier.
6440 static inline bool
6441 is_concept_var (tree decl)
6443 return (VAR_P (decl)
6444 // Not all variables have DECL_LANG_SPECIFIC.
6445 && DECL_LANG_SPECIFIC (decl)
6446 && DECL_DECLARED_CONCEPT_P (decl));
6449 /* Finish processing of a declaration;
6450 install its line number and initial value.
6451 If the length of an array type is not known before,
6452 it must be determined now, from the initial value, or it is an error.
6454 INIT is the initializer (if any) for DECL. If INIT_CONST_EXPR_P is
6455 true, then INIT is an integral constant expression.
6457 FLAGS is LOOKUP_ONLYCONVERTING if the = init syntax was used, else 0
6458 if the (init) syntax was used. */
6460 void
6461 cp_finish_decl (tree decl, tree init, bool init_const_expr_p,
6462 tree asmspec_tree, int flags)
6464 tree type;
6465 vec<tree, va_gc> *cleanups = NULL;
6466 const char *asmspec = NULL;
6467 int was_readonly = 0;
6468 bool var_definition_p = false;
6469 tree auto_node;
6471 if (decl == error_mark_node)
6472 return;
6473 else if (! decl)
6475 if (init)
6476 error ("assignment (not initialization) in declaration");
6477 return;
6480 gcc_assert (TREE_CODE (decl) != RESULT_DECL);
6481 /* Parameters are handled by store_parm_decls, not cp_finish_decl. */
6482 gcc_assert (TREE_CODE (decl) != PARM_DECL);
6484 type = TREE_TYPE (decl);
6485 if (type == error_mark_node)
6486 return;
6488 /* If a name was specified, get the string. */
6489 if (at_namespace_scope_p ())
6490 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
6491 if (asmspec_tree && asmspec_tree != error_mark_node)
6492 asmspec = TREE_STRING_POINTER (asmspec_tree);
6494 if (current_class_type
6495 && CP_DECL_CONTEXT (decl) == current_class_type
6496 && TYPE_BEING_DEFINED (current_class_type)
6497 && !CLASSTYPE_TEMPLATE_INSTANTIATION (current_class_type)
6498 && (DECL_INITIAL (decl) || init))
6499 DECL_INITIALIZED_IN_CLASS_P (decl) = 1;
6501 if (TREE_CODE (decl) != FUNCTION_DECL
6502 && (auto_node = type_uses_auto (type)))
6504 tree d_init;
6505 if (init == NULL_TREE)
6507 if (DECL_LANG_SPECIFIC (decl)
6508 && DECL_TEMPLATE_INSTANTIATION (decl)
6509 && !DECL_TEMPLATE_INSTANTIATED (decl))
6511 /* init is null because we're deferring instantiating the
6512 initializer until we need it. Well, we need it now. */
6513 instantiate_decl (decl, /*defer_ok*/true, /*expl*/false);
6514 return;
6517 error ("declaration of %q#D has no initializer", decl);
6518 TREE_TYPE (decl) = error_mark_node;
6519 return;
6521 d_init = init;
6522 if (TREE_CODE (d_init) == TREE_LIST)
6523 d_init = build_x_compound_expr_from_list (d_init, ELK_INIT,
6524 tf_warning_or_error);
6525 d_init = resolve_nondeduced_context (d_init);
6526 type = TREE_TYPE (decl) = do_auto_deduction (type, d_init,
6527 auto_node);
6528 if (type == error_mark_node)
6529 return;
6530 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
6533 if (!ensure_literal_type_for_constexpr_object (decl))
6534 DECL_DECLARED_CONSTEXPR_P (decl) = 0;
6536 if (VAR_P (decl)
6537 && DECL_CLASS_SCOPE_P (decl)
6538 && DECL_INITIALIZED_IN_CLASS_P (decl))
6539 check_static_variable_definition (decl, type);
6541 if (init && TREE_CODE (decl) == FUNCTION_DECL)
6543 tree clone;
6544 if (init == ridpointers[(int)RID_DELETE])
6546 /* FIXME check this is 1st decl. */
6547 DECL_DELETED_FN (decl) = 1;
6548 DECL_DECLARED_INLINE_P (decl) = 1;
6549 DECL_INITIAL (decl) = error_mark_node;
6550 FOR_EACH_CLONE (clone, decl)
6552 DECL_DELETED_FN (clone) = 1;
6553 DECL_DECLARED_INLINE_P (clone) = 1;
6554 DECL_INITIAL (clone) = error_mark_node;
6556 init = NULL_TREE;
6558 else if (init == ridpointers[(int)RID_DEFAULT])
6560 if (defaultable_fn_check (decl))
6561 DECL_DEFAULTED_FN (decl) = 1;
6562 else
6563 DECL_INITIAL (decl) = NULL_TREE;
6567 if (init && VAR_P (decl))
6569 DECL_NONTRIVIALLY_INITIALIZED_P (decl) = 1;
6570 /* If DECL is a reference, then we want to know whether init is a
6571 reference constant; init_const_expr_p as passed tells us whether
6572 it's an rvalue constant. */
6573 if (TREE_CODE (type) == REFERENCE_TYPE)
6574 init_const_expr_p = potential_constant_expression (init);
6575 if (init_const_expr_p)
6577 /* Set these flags now for templates. We'll update the flags in
6578 store_init_value for instantiations. */
6579 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
6580 if (decl_maybe_constant_var_p (decl))
6581 TREE_CONSTANT (decl) = 1;
6585 if (processing_template_decl)
6587 bool type_dependent_p;
6589 /* Add this declaration to the statement-tree. */
6590 if (at_function_scope_p ())
6591 add_decl_expr (decl);
6593 type_dependent_p = dependent_type_p (type);
6595 if (check_for_bare_parameter_packs (init))
6597 init = NULL_TREE;
6598 DECL_INITIAL (decl) = NULL_TREE;
6601 /* Generally, initializers in templates are expanded when the
6602 template is instantiated. But, if DECL is a variable constant
6603 then it can be used in future constant expressions, so its value
6604 must be available. */
6606 if (!VAR_P (decl) || dependent_type_p (type))
6607 /* We can't do anything if the decl has dependent type. */;
6608 else if (init
6609 && init_const_expr_p
6610 && !type_dependent_p
6611 && TREE_CODE (type) != REFERENCE_TYPE
6612 && decl_maybe_constant_var_p (decl)
6613 && !type_dependent_init_p (init)
6614 && !value_dependent_init_p (init))
6616 /* This variable seems to be a non-dependent constant, so process
6617 its initializer. If check_initializer returns non-null the
6618 initialization wasn't constant after all. */
6619 tree init_code;
6620 cleanups = make_tree_vector ();
6621 init_code = check_initializer (decl, init, flags, &cleanups);
6622 if (init_code == NULL_TREE)
6623 init = NULL_TREE;
6624 release_tree_vector (cleanups);
6626 else if (!init && is_concept_var (decl))
6627 error ("variable concept has no initializer");
6628 else if (!DECL_PRETTY_FUNCTION_P (decl))
6630 /* Deduce array size even if the initializer is dependent. */
6631 maybe_deduce_size_from_array_init (decl, init);
6632 /* And complain about multiple initializers. */
6633 if (init && TREE_CODE (init) == TREE_LIST && TREE_CHAIN (init)
6634 && !MAYBE_CLASS_TYPE_P (type))
6635 init = build_x_compound_expr_from_list (init, ELK_INIT,
6636 tf_warning_or_error);
6639 if (init)
6640 DECL_INITIAL (decl) = init;
6641 return;
6644 /* Just store non-static data member initializers for later. */
6645 if (init && TREE_CODE (decl) == FIELD_DECL)
6646 DECL_INITIAL (decl) = init;
6648 /* Take care of TYPE_DECLs up front. */
6649 if (TREE_CODE (decl) == TYPE_DECL)
6651 if (type != error_mark_node
6652 && MAYBE_CLASS_TYPE_P (type) && DECL_NAME (decl))
6654 if (TREE_TYPE (DECL_NAME (decl)) && TREE_TYPE (decl) != type)
6655 warning (0, "shadowing previous type declaration of %q#D", decl);
6656 set_identifier_type_value (DECL_NAME (decl), decl);
6659 /* If we have installed this as the canonical typedef for this
6660 type, and that type has not been defined yet, delay emitting
6661 the debug information for it, as we will emit it later. */
6662 if (TYPE_MAIN_DECL (TREE_TYPE (decl)) == decl
6663 && !COMPLETE_TYPE_P (TREE_TYPE (decl)))
6664 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
6666 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl),
6667 at_eof);
6668 return;
6671 /* A reference will be modified here, as it is initialized. */
6672 if (! DECL_EXTERNAL (decl)
6673 && TREE_READONLY (decl)
6674 && TREE_CODE (type) == REFERENCE_TYPE)
6676 was_readonly = 1;
6677 TREE_READONLY (decl) = 0;
6680 if (VAR_P (decl))
6682 /* If this is a local variable that will need a mangled name,
6683 register it now. We must do this before processing the
6684 initializer for the variable, since the initialization might
6685 require a guard variable, and since the mangled name of the
6686 guard variable will depend on the mangled name of this
6687 variable. */
6688 if (DECL_FUNCTION_SCOPE_P (decl)
6689 && TREE_STATIC (decl)
6690 && !DECL_ARTIFICIAL (decl))
6692 push_local_name (decl);
6693 if (DECL_CONSTRUCTOR_P (current_function_decl)
6694 || DECL_DESTRUCTOR_P (current_function_decl))
6695 /* Normally local_decls is populated during GIMPLE lowering,
6696 but [cd]tors are never actually compiled directly. We need
6697 to put statics on the list so we can deal with the label
6698 address extension. FIXME. */
6699 add_local_decl (cfun, decl);
6702 /* Convert the initializer to the type of DECL, if we have not
6703 already initialized DECL. */
6704 if (!DECL_INITIALIZED_P (decl)
6705 /* If !DECL_EXTERNAL then DECL is being defined. In the
6706 case of a static data member initialized inside the
6707 class-specifier, there can be an initializer even if DECL
6708 is *not* defined. */
6709 && (!DECL_EXTERNAL (decl) || init))
6711 if (TYPE_FOR_JAVA (type) && MAYBE_CLASS_TYPE_P (type))
6713 tree jclass
6714 = IDENTIFIER_GLOBAL_VALUE (get_identifier ("jclass"));
6715 /* Allow libjava/prims.cc define primitive classes. */
6716 if (init != NULL_TREE
6717 || jclass == NULL_TREE
6718 || TREE_CODE (jclass) != TYPE_DECL
6719 || !POINTER_TYPE_P (TREE_TYPE (jclass))
6720 || !same_type_ignoring_top_level_qualifiers_p
6721 (type, TREE_TYPE (TREE_TYPE (jclass))))
6722 error ("Java object %qD not allocated with %<new%>", decl);
6723 init = NULL_TREE;
6725 cleanups = make_tree_vector ();
6726 init = check_initializer (decl, init, flags, &cleanups);
6728 /* Handle:
6730 [dcl.init]
6732 The memory occupied by any object of static storage
6733 duration is zero-initialized at program startup before
6734 any other initialization takes place.
6736 We cannot create an appropriate initializer until after
6737 the type of DECL is finalized. If DECL_INITIAL is set,
6738 then the DECL is statically initialized, and any
6739 necessary zero-initialization has already been performed. */
6740 if (TREE_STATIC (decl) && !DECL_INITIAL (decl))
6741 DECL_INITIAL (decl) = build_zero_init (TREE_TYPE (decl),
6742 /*nelts=*/NULL_TREE,
6743 /*static_storage_p=*/true);
6744 /* Remember that the initialization for this variable has
6745 taken place. */
6746 DECL_INITIALIZED_P (decl) = 1;
6747 /* This declaration is the definition of this variable,
6748 unless we are initializing a static data member within
6749 the class specifier. */
6750 if (!DECL_EXTERNAL (decl))
6751 var_definition_p = true;
6753 /* If the variable has an array type, lay out the type, even if
6754 there is no initializer. It is valid to index through the
6755 array, and we must get TYPE_ALIGN set correctly on the array
6756 type. */
6757 else if (TREE_CODE (type) == ARRAY_TYPE)
6758 layout_type (type);
6760 if (TREE_STATIC (decl)
6761 && !at_function_scope_p ()
6762 && current_function_decl == NULL)
6763 /* So decl is a global variable or a static member of a
6764 non local class. Record the types it uses
6765 so that we can decide later to emit debug info for them. */
6766 record_types_used_by_current_var_decl (decl);
6768 else if (TREE_CODE (decl) == FIELD_DECL
6769 && TYPE_FOR_JAVA (type) && MAYBE_CLASS_TYPE_P (type))
6770 error ("non-static data member %qD has Java class type", decl);
6772 /* Add this declaration to the statement-tree. This needs to happen
6773 after the call to check_initializer so that the DECL_EXPR for a
6774 reference temp is added before the DECL_EXPR for the reference itself. */
6775 if (DECL_FUNCTION_SCOPE_P (decl))
6777 /* If we're building a variable sized type, and we might be
6778 reachable other than via the top of the current binding
6779 level, then create a new BIND_EXPR so that we deallocate
6780 the object at the right time. */
6781 if (VAR_P (decl)
6782 && DECL_SIZE (decl)
6783 && !TREE_CONSTANT (DECL_SIZE (decl))
6784 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
6786 tree bind;
6787 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
6788 TREE_SIDE_EFFECTS (bind) = 1;
6789 add_stmt (bind);
6790 BIND_EXPR_BODY (bind) = push_stmt_list ();
6792 add_decl_expr (decl);
6795 /* Let the middle end know about variables and functions -- but not
6796 static data members in uninstantiated class templates. */
6797 if (VAR_OR_FUNCTION_DECL_P (decl))
6799 if (VAR_P (decl))
6801 layout_var_decl (decl);
6802 maybe_commonize_var (decl);
6805 /* This needs to happen after the linkage is set. */
6806 determine_visibility (decl);
6808 if (var_definition_p && TREE_STATIC (decl))
6810 /* If a TREE_READONLY variable needs initialization
6811 at runtime, it is no longer readonly and we need to
6812 avoid MEM_READONLY_P being set on RTL created for it. */
6813 if (init)
6815 if (TREE_READONLY (decl))
6816 TREE_READONLY (decl) = 0;
6817 was_readonly = 0;
6819 else if (was_readonly)
6820 TREE_READONLY (decl) = 1;
6822 /* Likewise if it needs destruction. */
6823 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
6824 TREE_READONLY (decl) = 0;
6827 make_rtl_for_nonlocal_decl (decl, init, asmspec);
6829 /* Check for abstractness of the type. Notice that there is no
6830 need to strip array types here since the check for those types
6831 is already done within create_array_type_for_decl. */
6832 abstract_virtuals_error (decl, type);
6834 if (TREE_TYPE (decl) == error_mark_node)
6835 /* No initialization required. */
6837 else if (TREE_CODE (decl) == FUNCTION_DECL)
6839 if (init)
6841 if (init == ridpointers[(int)RID_DEFAULT])
6843 /* An out-of-class default definition is defined at
6844 the point where it is explicitly defaulted. */
6845 if (DECL_DELETED_FN (decl))
6846 maybe_explain_implicit_delete (decl);
6847 else if (DECL_INITIAL (decl) == error_mark_node)
6848 synthesize_method (decl);
6850 else
6851 error ("function %q#D is initialized like a variable", decl);
6853 /* else no initialization required. */
6855 else if (DECL_EXTERNAL (decl)
6856 && ! (DECL_LANG_SPECIFIC (decl)
6857 && DECL_NOT_REALLY_EXTERN (decl)))
6859 if (init)
6860 DECL_INITIAL (decl) = init;
6862 /* A variable definition. */
6863 else if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
6864 /* Initialize the local variable. */
6865 initialize_local_var (decl, init);
6867 /* If a variable is defined, and then a subsequent
6868 definition with external linkage is encountered, we will
6869 get here twice for the same variable. We want to avoid
6870 calling expand_static_init more than once. For variables
6871 that are not static data members, we can call
6872 expand_static_init only when we actually process the
6873 initializer. It is not legal to redeclare a static data
6874 member, so this issue does not arise in that case. */
6875 else if (var_definition_p && TREE_STATIC (decl))
6876 expand_static_init (decl, init);
6879 /* If a CLEANUP_STMT was created to destroy a temporary bound to a
6880 reference, insert it in the statement-tree now. */
6881 if (cleanups)
6883 unsigned i; tree t;
6884 FOR_EACH_VEC_ELT (*cleanups, i, t)
6885 push_cleanup (decl, t, false);
6886 release_tree_vector (cleanups);
6889 if (was_readonly)
6890 TREE_READONLY (decl) = 1;
6892 invoke_plugin_callbacks (PLUGIN_FINISH_DECL, decl);
6895 /* Returns a declaration for a VAR_DECL as if:
6897 extern "C" TYPE NAME;
6899 had been seen. Used to create compiler-generated global
6900 variables. */
6902 static tree
6903 declare_global_var (tree name, tree type)
6905 tree decl;
6907 push_to_top_level ();
6908 decl = build_decl (input_location, VAR_DECL, name, type);
6909 TREE_PUBLIC (decl) = 1;
6910 DECL_EXTERNAL (decl) = 1;
6911 DECL_ARTIFICIAL (decl) = 1;
6912 /* If the user has explicitly declared this variable (perhaps
6913 because the code we are compiling is part of a low-level runtime
6914 library), then it is possible that our declaration will be merged
6915 with theirs by pushdecl. */
6916 decl = pushdecl (decl);
6917 cp_finish_decl (decl, NULL_TREE, false, NULL_TREE, 0);
6918 pop_from_top_level ();
6920 return decl;
6923 /* Returns the type for the argument to "__cxa_atexit" (or "atexit",
6924 if "__cxa_atexit" is not being used) corresponding to the function
6925 to be called when the program exits. */
6927 static tree
6928 get_atexit_fn_ptr_type (void)
6930 tree fn_type;
6932 if (!atexit_fn_ptr_type_node)
6934 tree arg_type;
6935 if (flag_use_cxa_atexit
6936 && !targetm.cxx.use_atexit_for_cxa_atexit ())
6937 /* The parameter to "__cxa_atexit" is "void (*)(void *)". */
6938 arg_type = ptr_type_node;
6939 else
6940 /* The parameter to "atexit" is "void (*)(void)". */
6941 arg_type = NULL_TREE;
6943 fn_type = build_function_type_list (void_type_node,
6944 arg_type, NULL_TREE);
6945 atexit_fn_ptr_type_node = build_pointer_type (fn_type);
6948 return atexit_fn_ptr_type_node;
6951 /* Returns a pointer to the `atexit' function. Note that if
6952 FLAG_USE_CXA_ATEXIT is nonzero, then this will actually be the new
6953 `__cxa_atexit' function specified in the IA64 C++ ABI. */
6955 static tree
6956 get_atexit_node (void)
6958 tree atexit_fndecl;
6959 tree fn_type;
6960 tree fn_ptr_type;
6961 const char *name;
6962 bool use_aeabi_atexit;
6964 if (atexit_node)
6965 return atexit_node;
6967 if (flag_use_cxa_atexit && !targetm.cxx.use_atexit_for_cxa_atexit ())
6969 /* The declaration for `__cxa_atexit' is:
6971 int __cxa_atexit (void (*)(void *), void *, void *)
6973 We build up the argument types and then the function type
6974 itself. */
6975 tree argtype0, argtype1, argtype2;
6977 use_aeabi_atexit = targetm.cxx.use_aeabi_atexit ();
6978 /* First, build the pointer-to-function type for the first
6979 argument. */
6980 fn_ptr_type = get_atexit_fn_ptr_type ();
6981 /* Then, build the rest of the argument types. */
6982 argtype2 = ptr_type_node;
6983 if (use_aeabi_atexit)
6985 argtype1 = fn_ptr_type;
6986 argtype0 = ptr_type_node;
6988 else
6990 argtype1 = ptr_type_node;
6991 argtype0 = fn_ptr_type;
6993 /* And the final __cxa_atexit type. */
6994 fn_type = build_function_type_list (integer_type_node,
6995 argtype0, argtype1, argtype2,
6996 NULL_TREE);
6997 if (use_aeabi_atexit)
6998 name = "__aeabi_atexit";
6999 else
7000 name = "__cxa_atexit";
7002 else
7004 /* The declaration for `atexit' is:
7006 int atexit (void (*)());
7008 We build up the argument types and then the function type
7009 itself. */
7010 fn_ptr_type = get_atexit_fn_ptr_type ();
7011 /* Build the final atexit type. */
7012 fn_type = build_function_type_list (integer_type_node,
7013 fn_ptr_type, NULL_TREE);
7014 name = "atexit";
7017 /* Now, build the function declaration. */
7018 push_lang_context (lang_name_c);
7019 atexit_fndecl = build_library_fn_ptr (name, fn_type, ECF_LEAF | ECF_NOTHROW);
7020 mark_used (atexit_fndecl);
7021 pop_lang_context ();
7022 atexit_node = decay_conversion (atexit_fndecl, tf_warning_or_error);
7024 return atexit_node;
7027 /* Like get_atexit_node, but for thread-local cleanups. */
7029 static tree
7030 get_thread_atexit_node (void)
7032 /* The declaration for `__cxa_thread_atexit' is:
7034 int __cxa_thread_atexit (void (*)(void *), void *, void *) */
7035 tree fn_type = build_function_type_list (integer_type_node,
7036 get_atexit_fn_ptr_type (),
7037 ptr_type_node, ptr_type_node,
7038 NULL_TREE);
7040 /* Now, build the function declaration. */
7041 tree atexit_fndecl = build_library_fn_ptr ("__cxa_thread_atexit", fn_type,
7042 ECF_LEAF | ECF_NOTHROW);
7043 return decay_conversion (atexit_fndecl, tf_warning_or_error);
7046 /* Returns the __dso_handle VAR_DECL. */
7048 static tree
7049 get_dso_handle_node (void)
7051 if (dso_handle_node)
7052 return dso_handle_node;
7054 /* Declare the variable. */
7055 dso_handle_node = declare_global_var (get_identifier ("__dso_handle"),
7056 ptr_type_node);
7058 #ifdef HAVE_GAS_HIDDEN
7059 if (dso_handle_node != error_mark_node)
7061 DECL_VISIBILITY (dso_handle_node) = VISIBILITY_HIDDEN;
7062 DECL_VISIBILITY_SPECIFIED (dso_handle_node) = 1;
7064 #endif
7066 return dso_handle_node;
7069 /* Begin a new function with internal linkage whose job will be simply
7070 to destroy some particular variable. */
7072 static GTY(()) int start_cleanup_cnt;
7074 static tree
7075 start_cleanup_fn (void)
7077 char name[32];
7078 tree fntype;
7079 tree fndecl;
7080 bool use_cxa_atexit = flag_use_cxa_atexit
7081 && !targetm.cxx.use_atexit_for_cxa_atexit ();
7083 push_to_top_level ();
7085 /* No need to mangle this. */
7086 push_lang_context (lang_name_c);
7088 /* Build the name of the function. */
7089 sprintf (name, "__tcf_%d", start_cleanup_cnt++);
7090 /* Build the function declaration. */
7091 fntype = TREE_TYPE (get_atexit_fn_ptr_type ());
7092 fndecl = build_lang_decl (FUNCTION_DECL, get_identifier (name), fntype);
7093 /* It's a function with internal linkage, generated by the
7094 compiler. */
7095 TREE_PUBLIC (fndecl) = 0;
7096 DECL_ARTIFICIAL (fndecl) = 1;
7097 /* Make the function `inline' so that it is only emitted if it is
7098 actually needed. It is unlikely that it will be inlined, since
7099 it is only called via a function pointer, but we avoid unnecessary
7100 emissions this way. */
7101 DECL_DECLARED_INLINE_P (fndecl) = 1;
7102 DECL_INTERFACE_KNOWN (fndecl) = 1;
7103 /* Build the parameter. */
7104 if (use_cxa_atexit)
7106 tree parmdecl;
7108 parmdecl = cp_build_parm_decl (NULL_TREE, ptr_type_node);
7109 DECL_CONTEXT (parmdecl) = fndecl;
7110 TREE_USED (parmdecl) = 1;
7111 DECL_READ_P (parmdecl) = 1;
7112 DECL_ARGUMENTS (fndecl) = parmdecl;
7115 pushdecl (fndecl);
7116 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
7118 pop_lang_context ();
7120 return current_function_decl;
7123 /* Finish the cleanup function begun by start_cleanup_fn. */
7125 static void
7126 end_cleanup_fn (void)
7128 expand_or_defer_fn (finish_function (0));
7130 pop_from_top_level ();
7133 /* Generate code to handle the destruction of DECL, an object with
7134 static storage duration. */
7136 tree
7137 register_dtor_fn (tree decl)
7139 tree cleanup;
7140 tree addr;
7141 tree compound_stmt;
7142 tree fcall;
7143 tree type;
7144 bool ob_parm, dso_parm, use_dtor;
7145 tree arg0, arg1, arg2;
7146 tree atex_node;
7148 type = TREE_TYPE (decl);
7149 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
7150 return void_node;
7152 /* If we're using "__cxa_atexit" (or "__cxa_thread_atexit" or
7153 "__aeabi_atexit"), and DECL is a class object, we can just pass the
7154 destructor to "__cxa_atexit"; we don't have to build a temporary
7155 function to do the cleanup. */
7156 dso_parm = (flag_use_cxa_atexit
7157 && !targetm.cxx.use_atexit_for_cxa_atexit ());
7158 ob_parm = (DECL_THREAD_LOCAL_P (decl) || dso_parm);
7159 use_dtor = ob_parm && CLASS_TYPE_P (type);
7160 if (use_dtor)
7162 int idx;
7164 /* Find the destructor. */
7165 idx = lookup_fnfields_1 (type, complete_dtor_identifier);
7166 gcc_assert (idx >= 0);
7167 cleanup = (*CLASSTYPE_METHOD_VEC (type))[idx];
7168 /* Make sure it is accessible. */
7169 perform_or_defer_access_check (TYPE_BINFO (type), cleanup, cleanup,
7170 tf_warning_or_error);
7172 else
7174 /* Call build_cleanup before we enter the anonymous function so
7175 that any access checks will be done relative to the current
7176 scope, rather than the scope of the anonymous function. */
7177 build_cleanup (decl);
7179 /* Now start the function. */
7180 cleanup = start_cleanup_fn ();
7182 /* Now, recompute the cleanup. It may contain SAVE_EXPRs that refer
7183 to the original function, rather than the anonymous one. That
7184 will make the back end think that nested functions are in use,
7185 which causes confusion. */
7186 push_deferring_access_checks (dk_no_check);
7187 fcall = build_cleanup (decl);
7188 pop_deferring_access_checks ();
7190 /* Create the body of the anonymous function. */
7191 compound_stmt = begin_compound_stmt (BCS_FN_BODY);
7192 finish_expr_stmt (fcall);
7193 finish_compound_stmt (compound_stmt);
7194 end_cleanup_fn ();
7197 /* Call atexit with the cleanup function. */
7198 mark_used (cleanup);
7199 cleanup = build_address (cleanup);
7201 if (DECL_THREAD_LOCAL_P (decl))
7202 atex_node = get_thread_atexit_node ();
7203 else
7204 atex_node = get_atexit_node ();
7206 if (use_dtor)
7208 /* We must convert CLEANUP to the type that "__cxa_atexit"
7209 expects. */
7210 cleanup = build_nop (get_atexit_fn_ptr_type (), cleanup);
7211 /* "__cxa_atexit" will pass the address of DECL to the
7212 cleanup function. */
7213 mark_used (decl);
7214 addr = build_address (decl);
7215 /* The declared type of the parameter to "__cxa_atexit" is
7216 "void *". For plain "T*", we could just let the
7217 machinery in cp_build_function_call convert it -- but if the
7218 type is "cv-qualified T *", then we need to convert it
7219 before passing it in, to avoid spurious errors. */
7220 addr = build_nop (ptr_type_node, addr);
7222 else
7223 /* Since the cleanup functions we build ignore the address
7224 they're given, there's no reason to pass the actual address
7225 in, and, in general, it's cheaper to pass NULL than any
7226 other value. */
7227 addr = null_pointer_node;
7229 if (dso_parm)
7230 arg2 = cp_build_addr_expr (get_dso_handle_node (),
7231 tf_warning_or_error);
7232 else if (ob_parm)
7233 /* Just pass NULL to the dso handle parm if we don't actually
7234 have a DSO handle on this target. */
7235 arg2 = null_pointer_node;
7236 else
7237 arg2 = NULL_TREE;
7239 if (ob_parm)
7241 if (!DECL_THREAD_LOCAL_P (decl)
7242 && targetm.cxx.use_aeabi_atexit ())
7244 arg1 = cleanup;
7245 arg0 = addr;
7247 else
7249 arg1 = addr;
7250 arg0 = cleanup;
7253 else
7255 arg0 = cleanup;
7256 arg1 = NULL_TREE;
7258 return cp_build_function_call_nary (atex_node, tf_warning_or_error,
7259 arg0, arg1, arg2, NULL_TREE);
7262 /* DECL is a VAR_DECL with static storage duration. INIT, if present,
7263 is its initializer. Generate code to handle the construction
7264 and destruction of DECL. */
7266 static void
7267 expand_static_init (tree decl, tree init)
7269 gcc_assert (VAR_P (decl));
7270 gcc_assert (TREE_STATIC (decl));
7272 /* Some variables require no dynamic initialization. */
7273 if (!init
7274 && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
7276 /* Make sure the destructor is callable. */
7277 cxx_maybe_build_cleanup (decl, tf_warning_or_error);
7278 return;
7281 if (DECL_THREAD_LOCAL_P (decl) && DECL_GNU_TLS_P (decl)
7282 && !DECL_FUNCTION_SCOPE_P (decl))
7284 if (init)
7285 error ("non-local variable %qD declared %<__thread%> "
7286 "needs dynamic initialization", decl);
7287 else
7288 error ("non-local variable %qD declared %<__thread%> "
7289 "has a non-trivial destructor", decl);
7290 static bool informed;
7291 if (!informed)
7293 inform (DECL_SOURCE_LOCATION (decl),
7294 "C++11 %<thread_local%> allows dynamic initialization "
7295 "and destruction");
7296 informed = true;
7298 return;
7301 if (DECL_FUNCTION_SCOPE_P (decl))
7303 /* Emit code to perform this initialization but once. */
7304 tree if_stmt = NULL_TREE, inner_if_stmt = NULL_TREE;
7305 tree then_clause = NULL_TREE, inner_then_clause = NULL_TREE;
7306 tree guard, guard_addr;
7307 tree flag, begin;
7308 /* We don't need thread-safety code for thread-local vars. */
7309 bool thread_guard = (flag_threadsafe_statics
7310 && !DECL_THREAD_LOCAL_P (decl));
7312 /* Emit code to perform this initialization but once. This code
7313 looks like:
7315 static <type> guard;
7316 if (!guard.first_byte) {
7317 if (__cxa_guard_acquire (&guard)) {
7318 bool flag = false;
7319 try {
7320 // Do initialization.
7321 flag = true; __cxa_guard_release (&guard);
7322 // Register variable for destruction at end of program.
7323 } catch {
7324 if (!flag) __cxa_guard_abort (&guard);
7328 Note that the `flag' variable is only set to 1 *after* the
7329 initialization is complete. This ensures that an exception,
7330 thrown during the construction, will cause the variable to
7331 reinitialized when we pass through this code again, as per:
7333 [stmt.dcl]
7335 If the initialization exits by throwing an exception, the
7336 initialization is not complete, so it will be tried again
7337 the next time control enters the declaration.
7339 This process should be thread-safe, too; multiple threads
7340 should not be able to initialize the variable more than
7341 once. */
7343 /* Create the guard variable. */
7344 guard = get_guard (decl);
7346 /* This optimization isn't safe on targets with relaxed memory
7347 consistency. On such targets we force synchronization in
7348 __cxa_guard_acquire. */
7349 if (!targetm.relaxed_ordering || !thread_guard)
7351 /* Begin the conditional initialization. */
7352 if_stmt = begin_if_stmt ();
7353 finish_if_stmt_cond (get_guard_cond (guard), if_stmt);
7354 then_clause = begin_compound_stmt (BCS_NO_SCOPE);
7357 if (thread_guard)
7359 tree vfntype = NULL_TREE;
7360 tree acquire_name, release_name, abort_name;
7361 tree acquire_fn, release_fn, abort_fn;
7362 guard_addr = build_address (guard);
7364 acquire_name = get_identifier ("__cxa_guard_acquire");
7365 release_name = get_identifier ("__cxa_guard_release");
7366 abort_name = get_identifier ("__cxa_guard_abort");
7367 acquire_fn = identifier_global_value (acquire_name);
7368 release_fn = identifier_global_value (release_name);
7369 abort_fn = identifier_global_value (abort_name);
7370 if (!acquire_fn)
7371 acquire_fn = push_library_fn
7372 (acquire_name, build_function_type_list (integer_type_node,
7373 TREE_TYPE (guard_addr),
7374 NULL_TREE),
7375 NULL_TREE, ECF_NOTHROW | ECF_LEAF);
7376 if (!release_fn || !abort_fn)
7377 vfntype = build_function_type_list (void_type_node,
7378 TREE_TYPE (guard_addr),
7379 NULL_TREE);
7380 if (!release_fn)
7381 release_fn = push_library_fn (release_name, vfntype, NULL_TREE,
7382 ECF_NOTHROW | ECF_LEAF);
7383 if (!abort_fn)
7384 abort_fn = push_library_fn (abort_name, vfntype, NULL_TREE,
7385 ECF_NOTHROW | ECF_LEAF);
7387 inner_if_stmt = begin_if_stmt ();
7388 finish_if_stmt_cond (build_call_n (acquire_fn, 1, guard_addr),
7389 inner_if_stmt);
7391 inner_then_clause = begin_compound_stmt (BCS_NO_SCOPE);
7392 begin = get_target_expr (boolean_false_node);
7393 flag = TARGET_EXPR_SLOT (begin);
7395 TARGET_EXPR_CLEANUP (begin)
7396 = build3 (COND_EXPR, void_type_node, flag,
7397 void_node,
7398 build_call_n (abort_fn, 1, guard_addr));
7399 CLEANUP_EH_ONLY (begin) = 1;
7401 /* Do the initialization itself. */
7402 init = add_stmt_to_compound (begin, init);
7403 init = add_stmt_to_compound
7404 (init, build2 (MODIFY_EXPR, void_type_node, flag, boolean_true_node));
7405 init = add_stmt_to_compound
7406 (init, build_call_n (release_fn, 1, guard_addr));
7408 else
7409 init = add_stmt_to_compound (init, set_guard (guard));
7411 /* Use atexit to register a function for destroying this static
7412 variable. */
7413 init = add_stmt_to_compound (init, register_dtor_fn (decl));
7415 finish_expr_stmt (init);
7417 if (thread_guard)
7419 finish_compound_stmt (inner_then_clause);
7420 finish_then_clause (inner_if_stmt);
7421 finish_if_stmt (inner_if_stmt);
7424 if (!targetm.relaxed_ordering || !thread_guard)
7426 finish_compound_stmt (then_clause);
7427 finish_then_clause (if_stmt);
7428 finish_if_stmt (if_stmt);
7431 else if (DECL_THREAD_LOCAL_P (decl))
7432 tls_aggregates = tree_cons (init, decl, tls_aggregates);
7433 else
7434 static_aggregates = tree_cons (init, decl, static_aggregates);
7438 /* Make TYPE a complete type based on INITIAL_VALUE.
7439 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
7440 2 if there was no information (in which case assume 0 if DO_DEFAULT),
7441 3 if the initializer list is empty (in pedantic mode). */
7444 cp_complete_array_type (tree *ptype, tree initial_value, bool do_default)
7446 int failure;
7447 tree type, elt_type;
7449 /* Don't get confused by a CONSTRUCTOR for some other type. */
7450 if (initial_value && TREE_CODE (initial_value) == CONSTRUCTOR
7451 && !BRACE_ENCLOSED_INITIALIZER_P (initial_value))
7452 return 1;
7454 if (initial_value)
7456 unsigned HOST_WIDE_INT i;
7457 tree value;
7459 /* An array of character type can be initialized from a
7460 brace-enclosed string constant.
7462 FIXME: this code is duplicated from reshape_init. Probably
7463 we should just call reshape_init here? */
7464 if (char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (*ptype)))
7465 && TREE_CODE (initial_value) == CONSTRUCTOR
7466 && !vec_safe_is_empty (CONSTRUCTOR_ELTS (initial_value)))
7468 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value);
7469 tree value = (*v)[0].value;
7471 if (TREE_CODE (value) == STRING_CST
7472 && v->length () == 1)
7473 initial_value = value;
7476 /* If any of the elements are parameter packs, we can't actually
7477 complete this type now because the array size is dependent. */
7478 if (TREE_CODE (initial_value) == CONSTRUCTOR)
7480 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (initial_value),
7481 i, value)
7483 if (PACK_EXPANSION_P (value))
7484 return 0;
7489 failure = complete_array_type (ptype, initial_value, do_default);
7491 /* We can create the array before the element type is complete, which
7492 means that we didn't have these two bits set in the original type
7493 either. In completing the type, we are expected to propagate these
7494 bits. See also complete_type which does the same thing for arrays
7495 of fixed size. */
7496 type = *ptype;
7497 if (TYPE_DOMAIN (type))
7499 elt_type = TREE_TYPE (type);
7500 TYPE_NEEDS_CONSTRUCTING (type) = TYPE_NEEDS_CONSTRUCTING (elt_type);
7501 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
7502 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (elt_type);
7505 return failure;
7508 /* As above, but either give an error or reject zero-size arrays, depending
7509 on COMPLAIN. */
7512 cp_complete_array_type_or_error (tree *ptype, tree initial_value,
7513 bool do_default, tsubst_flags_t complain)
7515 int failure;
7516 bool sfinae = !(complain & tf_error);
7517 /* In SFINAE context we can't be lenient about zero-size arrays. */
7518 if (sfinae)
7519 ++pedantic;
7520 failure = cp_complete_array_type (ptype, initial_value, do_default);
7521 if (sfinae)
7522 --pedantic;
7523 if (failure)
7525 if (sfinae)
7526 /* Not an error. */;
7527 else if (failure == 1)
7528 error ("initializer fails to determine size of %qT", *ptype);
7529 else if (failure == 2)
7531 if (do_default)
7532 error ("array size missing in %qT", *ptype);
7534 else if (failure == 3)
7535 error ("zero-size array %qT", *ptype);
7536 *ptype = error_mark_node;
7538 return failure;
7541 /* Return zero if something is declared to be a member of type
7542 CTYPE when in the context of CUR_TYPE. STRING is the error
7543 message to print in that case. Otherwise, quietly return 1. */
7545 static int
7546 member_function_or_else (tree ctype, tree cur_type, enum overload_flags flags)
7548 if (ctype && ctype != cur_type)
7550 if (flags == DTOR_FLAG)
7551 error ("destructor for alien class %qT cannot be a member", ctype);
7552 else
7553 error ("constructor for alien class %qT cannot be a member", ctype);
7554 return 0;
7556 return 1;
7559 /* Subroutine of `grokdeclarator'. */
7561 /* Generate errors possibly applicable for a given set of specifiers.
7562 This is for ARM $7.1.2. */
7564 static void
7565 bad_specifiers (tree object,
7566 enum bad_spec_place type,
7567 int virtualp,
7568 int quals,
7569 int inlinep,
7570 int friendp,
7571 int raises)
7573 switch (type)
7575 case BSP_VAR:
7576 if (virtualp)
7577 error ("%qD declared as a %<virtual%> variable", object);
7578 if (inlinep)
7579 error ("%qD declared as an %<inline%> variable", object);
7580 if (quals)
7581 error ("%<const%> and %<volatile%> function specifiers on "
7582 "%qD invalid in variable declaration", object);
7583 break;
7584 case BSP_PARM:
7585 if (virtualp)
7586 error ("%qD declared as a %<virtual%> parameter", object);
7587 if (inlinep)
7588 error ("%qD declared as an %<inline%> parameter", object);
7589 if (quals)
7590 error ("%<const%> and %<volatile%> function specifiers on "
7591 "%qD invalid in parameter declaration", object);
7592 break;
7593 case BSP_TYPE:
7594 if (virtualp)
7595 error ("%qD declared as a %<virtual%> type", object);
7596 if (inlinep)
7597 error ("%qD declared as an %<inline%> type", object);
7598 if (quals)
7599 error ("%<const%> and %<volatile%> function specifiers on "
7600 "%qD invalid in type declaration", object);
7601 break;
7602 case BSP_FIELD:
7603 if (virtualp)
7604 error ("%qD declared as a %<virtual%> field", object);
7605 if (inlinep)
7606 error ("%qD declared as an %<inline%> field", object);
7607 if (quals)
7608 error ("%<const%> and %<volatile%> function specifiers on "
7609 "%qD invalid in field declaration", object);
7610 break;
7611 default:
7612 gcc_unreachable();
7614 if (friendp)
7615 error ("%q+D declared as a friend", object);
7616 if (raises
7617 && (TREE_CODE (object) == TYPE_DECL
7618 || (!TYPE_PTRFN_P (TREE_TYPE (object))
7619 && !TYPE_REFFN_P (TREE_TYPE (object))
7620 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (object)))))
7621 error ("%q+D declared with an exception specification", object);
7624 /* DECL is a member function or static data member and is presently
7625 being defined. Check that the definition is taking place in a
7626 valid namespace. */
7628 static void
7629 check_class_member_definition_namespace (tree decl)
7631 /* These checks only apply to member functions and static data
7632 members. */
7633 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
7634 /* We check for problems with specializations in pt.c in
7635 check_specialization_namespace, where we can issue better
7636 diagnostics. */
7637 if (processing_specialization)
7638 return;
7639 /* There are no restrictions on the placement of
7640 explicit instantiations. */
7641 if (processing_explicit_instantiation)
7642 return;
7643 /* [class.mfct]
7645 A member function definition that appears outside of the
7646 class definition shall appear in a namespace scope enclosing
7647 the class definition.
7649 [class.static.data]
7651 The definition for a static data member shall appear in a
7652 namespace scope enclosing the member's class definition. */
7653 if (!is_ancestor (current_namespace, DECL_CONTEXT (decl)))
7654 permerror (input_location, "definition of %qD is not in namespace enclosing %qT",
7655 decl, DECL_CONTEXT (decl));
7658 /* Build a PARM_DECL for the "this" parameter. TYPE is the
7659 METHOD_TYPE for a non-static member function; QUALS are the
7660 cv-qualifiers that apply to the function. */
7662 tree
7663 build_this_parm (tree type, cp_cv_quals quals)
7665 tree this_type;
7666 tree qual_type;
7667 tree parm;
7668 cp_cv_quals this_quals;
7670 if (CLASS_TYPE_P (type))
7672 this_type
7673 = cp_build_qualified_type (type, quals & ~TYPE_QUAL_RESTRICT);
7674 this_type = build_pointer_type (this_type);
7676 else
7677 this_type = type_of_this_parm (type);
7678 /* The `this' parameter is implicitly `const'; it cannot be
7679 assigned to. */
7680 this_quals = (quals & TYPE_QUAL_RESTRICT) | TYPE_QUAL_CONST;
7681 qual_type = cp_build_qualified_type (this_type, this_quals);
7682 parm = build_artificial_parm (this_identifier, qual_type);
7683 cp_apply_type_quals_to_decl (this_quals, parm);
7684 return parm;
7687 /* DECL is a static member function. Complain if it was declared
7688 with function-cv-quals. */
7690 static void
7691 check_static_quals (tree decl, cp_cv_quals quals)
7693 if (quals != TYPE_UNQUALIFIED)
7694 error ("static member function %q#D declared with type qualifiers",
7695 decl);
7698 // Check that FN takes no arguments and returns bool.
7699 static void
7700 check_concept_fn (tree fn)
7702 // A constraint is nullary.
7703 if (DECL_ARGUMENTS (fn))
7704 error ("concept %q#D declared with function parameters", fn);
7706 // The declared return type of the concept shall be bool, and
7707 // it shall not be deduced from it definition.
7708 tree type = TREE_TYPE (TREE_TYPE (fn));
7709 if (is_auto (type))
7710 error ("concept %q#D declared with a deduced return type", fn);
7711 else if (type != boolean_type_node)
7712 error ("concept %q#D with return type %qT", fn, type);
7715 /* Helper function. Replace the temporary this parameter injected
7716 during cp_finish_omp_declare_simd with the real this parameter. */
7718 static tree
7719 declare_simd_adjust_this (tree *tp, int *walk_subtrees, void *data)
7721 tree this_parm = (tree) data;
7722 if (TREE_CODE (*tp) == PARM_DECL
7723 && DECL_NAME (*tp) == this_identifier
7724 && *tp != this_parm)
7725 *tp = this_parm;
7726 else if (TYPE_P (*tp))
7727 *walk_subtrees = 0;
7728 return NULL_TREE;
7731 /* CTYPE is class type, or null if non-class.
7732 TYPE is type this FUNCTION_DECL should have, either FUNCTION_TYPE
7733 or METHOD_TYPE.
7734 DECLARATOR is the function's name.
7735 PARMS is a chain of PARM_DECLs for the function.
7736 VIRTUALP is truthvalue of whether the function is virtual or not.
7737 FLAGS are to be passed through to `grokclassfn'.
7738 QUALS are qualifiers indicating whether the function is `const'
7739 or `volatile'.
7740 RAISES is a list of exceptions that this function can raise.
7741 CHECK is 1 if we must find this method in CTYPE, 0 if we should
7742 not look, and -1 if we should not call `grokclassfn' at all.
7744 SFK is the kind of special function (if any) for the new function.
7746 Returns `NULL_TREE' if something goes wrong, after issuing
7747 applicable error messages. */
7749 static tree
7750 grokfndecl (tree ctype,
7751 tree type,
7752 tree declarator,
7753 tree parms,
7754 tree orig_declarator,
7755 tree decl_reqs,
7756 int virtualp,
7757 enum overload_flags flags,
7758 cp_cv_quals quals,
7759 cp_ref_qualifier rqual,
7760 tree raises,
7761 int check,
7762 int friendp,
7763 int publicp,
7764 int inlinep,
7765 bool deletedp,
7766 special_function_kind sfk,
7767 bool funcdef_flag,
7768 int template_count,
7769 tree in_namespace,
7770 tree* attrlist,
7771 location_t location)
7773 tree decl;
7774 int staticp = ctype && TREE_CODE (type) == FUNCTION_TYPE;
7775 tree t;
7777 // Was the concept specifier present?
7778 bool concept_p = inlinep & 4;
7780 // Concept declarations must have a corresponding definition.
7781 if (concept_p && !funcdef_flag)
7783 error ("concept %qD has no definition", declarator);
7784 return NULL_TREE;
7787 if (rqual)
7788 type = build_ref_qualified_type (type, rqual);
7789 if (raises)
7790 type = build_exception_variant (type, raises);
7792 decl = build_lang_decl (FUNCTION_DECL, declarator, type);
7794 /* Set the constraints on the declaration. */
7795 if (flag_concepts)
7797 tree tmpl_reqs = NULL_TREE;
7798 if (processing_template_decl > template_class_depth (ctype))
7799 tmpl_reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
7801 /* Adjust the required expression into a constraint. */
7802 if (decl_reqs)
7803 decl_reqs = make_predicate_constraint (decl_reqs);
7805 tree ci = build_constraints (tmpl_reqs, decl_reqs);
7806 set_constraints (decl, ci);
7809 /* If we have an explicit location, use it, otherwise use whatever
7810 build_lang_decl used (probably input_location). */
7811 if (location != UNKNOWN_LOCATION)
7812 DECL_SOURCE_LOCATION (decl) = location;
7814 if (TREE_CODE (type) == METHOD_TYPE)
7816 tree parm;
7817 parm = build_this_parm (type, quals);
7818 DECL_CHAIN (parm) = parms;
7819 parms = parm;
7821 DECL_ARGUMENTS (decl) = parms;
7822 for (t = parms; t; t = DECL_CHAIN (t))
7823 DECL_CONTEXT (t) = decl;
7824 /* Propagate volatile out from type to decl. */
7825 if (TYPE_VOLATILE (type))
7826 TREE_THIS_VOLATILE (decl) = 1;
7828 /* Setup decl according to sfk. */
7829 switch (sfk)
7831 case sfk_constructor:
7832 case sfk_copy_constructor:
7833 case sfk_move_constructor:
7834 DECL_CONSTRUCTOR_P (decl) = 1;
7835 break;
7836 case sfk_destructor:
7837 DECL_DESTRUCTOR_P (decl) = 1;
7838 break;
7839 default:
7840 break;
7843 /* If pointers to member functions use the least significant bit to
7844 indicate whether a function is virtual, ensure a pointer
7845 to this function will have that bit clear. */
7846 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn
7847 && TREE_CODE (type) == METHOD_TYPE
7848 && DECL_ALIGN (decl) < 2 * BITS_PER_UNIT)
7849 DECL_ALIGN (decl) = 2 * BITS_PER_UNIT;
7851 if (friendp
7852 && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)
7854 if (funcdef_flag)
7855 error
7856 ("defining explicit specialization %qD in friend declaration",
7857 orig_declarator);
7858 else
7860 tree fns = TREE_OPERAND (orig_declarator, 0);
7861 tree args = TREE_OPERAND (orig_declarator, 1);
7863 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
7865 /* Something like `template <class T> friend void f<T>()'. */
7866 error ("invalid use of template-id %qD in declaration "
7867 "of primary template",
7868 orig_declarator);
7869 return NULL_TREE;
7872 // Do not allow the declaration of constrained friend template
7873 // specializations. They cannot be instantiated since they
7874 // must match a fully instantiated function, and non-dependent
7875 // functions cannot be constrained.
7877 // FIXME [concepts] This is no longer correct. We do disallow
7878 // constraints on friend function template specializations.
7880 // if (current_template_reqs)
7881 // {
7882 // error ("constraints are not allowed in declaration "
7883 // "of friend template specialization %qD", decl);
7884 // return NULL_TREE;
7885 // }
7887 /* A friend declaration of the form friend void f<>(). Record
7888 the information in the TEMPLATE_ID_EXPR. */
7889 SET_DECL_IMPLICIT_INSTANTIATION (decl);
7891 gcc_assert (identifier_p (fns) || TREE_CODE (fns) == OVERLOAD);
7892 DECL_TEMPLATE_INFO (decl) = build_template_info (fns, args);
7894 for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t))
7895 if (TREE_PURPOSE (t)
7896 && TREE_CODE (TREE_PURPOSE (t)) == DEFAULT_ARG)
7898 error ("default arguments are not allowed in declaration "
7899 "of friend template specialization %qD",
7900 decl);
7901 return NULL_TREE;
7904 if (inlinep & 1)
7905 error ("%<inline%> is not allowed in declaration of friend "
7906 "template specialization %qD",
7907 decl);
7908 if (inlinep & 2)
7909 error ("%<constexpr%> is not allowed in declaration of friend "
7910 "template specialization %qD",
7911 decl);
7912 if (inlinep)
7913 return NULL_TREE;
7917 /* If this decl has namespace scope, set that up. */
7918 if (in_namespace)
7919 set_decl_namespace (decl, in_namespace, friendp);
7920 else if (!ctype)
7921 DECL_CONTEXT (decl) = FROB_CONTEXT (current_decl_namespace ());
7923 /* `main' and builtins have implicit 'C' linkage. */
7924 if ((MAIN_NAME_P (declarator)
7925 || (IDENTIFIER_LENGTH (declarator) > 10
7926 && IDENTIFIER_POINTER (declarator)[0] == '_'
7927 && IDENTIFIER_POINTER (declarator)[1] == '_'
7928 && strncmp (IDENTIFIER_POINTER (declarator)+2, "builtin_", 8) == 0)
7929 || (targetcm.cxx_implicit_extern_c
7930 && targetcm.cxx_implicit_extern_c(IDENTIFIER_POINTER (declarator))))
7931 && current_lang_name == lang_name_cplusplus
7932 && ctype == NULL_TREE
7933 && DECL_FILE_SCOPE_P (decl))
7934 SET_DECL_LANGUAGE (decl, lang_c);
7936 /* Should probably propagate const out from type to decl I bet (mrs). */
7937 if (staticp)
7939 DECL_STATIC_FUNCTION_P (decl) = 1;
7940 DECL_CONTEXT (decl) = ctype;
7943 if (deletedp)
7944 DECL_DELETED_FN (decl) = 1;
7946 if (ctype)
7948 DECL_CONTEXT (decl) = ctype;
7949 if (funcdef_flag)
7950 check_class_member_definition_namespace (decl);
7953 if (ctype == NULL_TREE && DECL_MAIN_P (decl))
7955 if (PROCESSING_REAL_TEMPLATE_DECL_P())
7956 error ("cannot declare %<::main%> to be a template");
7957 if (inlinep & 1)
7958 error ("cannot declare %<::main%> to be inline");
7959 if (inlinep & 2)
7960 error ("cannot declare %<::main%> to be constexpr");
7961 if (!publicp)
7962 error ("cannot declare %<::main%> to be static");
7963 inlinep = 0;
7964 publicp = 1;
7967 /* Members of anonymous types and local classes have no linkage; make
7968 them internal. If a typedef is made later, this will be changed. */
7969 if (ctype && (TYPE_ANONYMOUS_P (ctype)
7970 || decl_function_context (TYPE_MAIN_DECL (ctype))))
7971 publicp = 0;
7973 if (publicp && cxx_dialect == cxx98)
7975 /* [basic.link]: A name with no linkage (notably, the name of a class
7976 or enumeration declared in a local scope) shall not be used to
7977 declare an entity with linkage.
7979 DR 757 relaxes this restriction for C++0x. */
7980 no_linkage_error (decl);
7983 TREE_PUBLIC (decl) = publicp;
7984 if (! publicp)
7986 DECL_INTERFACE_KNOWN (decl) = 1;
7987 DECL_NOT_REALLY_EXTERN (decl) = 1;
7990 /* If the declaration was declared inline, mark it as such. */
7991 if (inlinep)
7993 DECL_DECLARED_INLINE_P (decl) = 1;
7994 if (publicp)
7995 DECL_COMDAT (decl) = 1;
7997 if (inlinep & 2)
7998 DECL_DECLARED_CONSTEXPR_P (decl) = true;
8000 // If the concept declaration specifier was found, check
8001 // that the declaration satisfies the necessary requirements.
8002 if (inlinep & 4)
8004 DECL_DECLARED_CONCEPT_P (decl) = true;
8005 check_concept_fn (decl);
8008 DECL_EXTERNAL (decl) = 1;
8009 if (TREE_CODE (type) == FUNCTION_TYPE)
8011 if (quals)
8013 error (ctype
8014 ? G_("static member function %qD cannot have cv-qualifier")
8015 : G_("non-member function %qD cannot have cv-qualifier"),
8016 decl);
8017 quals = TYPE_UNQUALIFIED;
8020 if (rqual)
8022 error (ctype
8023 ? G_("static member function %qD cannot have ref-qualifier")
8024 : G_("non-member function %qD cannot have ref-qualifier"),
8025 decl);
8026 rqual = REF_QUAL_NONE;
8030 if (IDENTIFIER_OPNAME_P (DECL_NAME (decl))
8031 && !grok_op_properties (decl, /*complain=*/true))
8032 return NULL_TREE;
8033 else if (UDLIT_OPER_P (DECL_NAME (decl)))
8035 bool long_long_unsigned_p;
8036 bool long_double_p;
8037 const char *suffix = NULL;
8038 /* [over.literal]/6: Literal operators shall not have C linkage. */
8039 if (DECL_LANGUAGE (decl) == lang_c)
8041 error ("literal operator with C linkage");
8042 return NULL_TREE;
8045 if (DECL_NAMESPACE_SCOPE_P (decl))
8047 if (!check_literal_operator_args (decl, &long_long_unsigned_p,
8048 &long_double_p))
8050 error ("%qD has invalid argument list", decl);
8051 return NULL_TREE;
8054 suffix = UDLIT_OP_SUFFIX (DECL_NAME (decl));
8055 if (long_long_unsigned_p)
8057 if (cpp_interpret_int_suffix (parse_in, suffix, strlen (suffix)))
8058 warning (0, "integer suffix %<%s%>"
8059 " shadowed by implementation", suffix);
8061 else if (long_double_p)
8063 if (cpp_interpret_float_suffix (parse_in, suffix, strlen (suffix)))
8064 warning (0, "floating point suffix %<%s%>"
8065 " shadowed by implementation", suffix);
8068 else
8070 error ("%qD must be a non-member function", decl);
8071 return NULL_TREE;
8075 if (funcdef_flag)
8076 /* Make the init_value nonzero so pushdecl knows this is not
8077 tentative. error_mark_node is replaced later with the BLOCK. */
8078 DECL_INITIAL (decl) = error_mark_node;
8080 if (TYPE_NOTHROW_P (type) || nothrow_libfn_p (decl))
8081 TREE_NOTHROW (decl) = 1;
8083 if (flag_openmp || flag_cilkplus)
8085 /* Adjust "omp declare simd" attributes. */
8086 tree ods = lookup_attribute ("omp declare simd", *attrlist);
8087 if (ods)
8089 tree attr;
8090 for (attr = ods; attr;
8091 attr = lookup_attribute ("omp declare simd", TREE_CHAIN (attr)))
8093 if (TREE_CODE (type) == METHOD_TYPE)
8094 walk_tree (&TREE_VALUE (attr), declare_simd_adjust_this,
8095 DECL_ARGUMENTS (decl), NULL);
8096 if (TREE_VALUE (attr) != NULL_TREE)
8098 tree cl = TREE_VALUE (TREE_VALUE (attr));
8099 cl = c_omp_declare_simd_clauses_to_numbers
8100 (DECL_ARGUMENTS (decl), cl);
8101 if (cl)
8102 TREE_VALUE (TREE_VALUE (attr)) = cl;
8103 else
8104 TREE_VALUE (attr) = NULL_TREE;
8110 /* Caller will do the rest of this. */
8111 if (check < 0)
8112 return decl;
8114 if (ctype != NULL_TREE)
8115 grokclassfn (ctype, decl, flags);
8117 /* 12.4/3 */
8118 if (cxx_dialect >= cxx11
8119 && DECL_DESTRUCTOR_P (decl)
8120 && !TYPE_BEING_DEFINED (DECL_CONTEXT (decl))
8121 && !processing_template_decl)
8122 deduce_noexcept_on_destructor (decl);
8124 decl = check_explicit_specialization (orig_declarator, decl,
8125 template_count,
8126 2 * funcdef_flag +
8127 4 * (friendp != 0) +
8128 8 * concept_p);
8129 if (decl == error_mark_node)
8130 return NULL_TREE;
8132 if (DECL_STATIC_FUNCTION_P (decl))
8133 check_static_quals (decl, quals);
8135 if (attrlist)
8137 cplus_decl_attributes (&decl, *attrlist, 0);
8138 *attrlist = NULL_TREE;
8141 /* Check main's type after attributes have been applied. */
8142 if (ctype == NULL_TREE && DECL_MAIN_P (decl))
8144 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
8145 integer_type_node))
8147 tree oldtypeargs = TYPE_ARG_TYPES (TREE_TYPE (decl));
8148 tree newtype;
8149 error ("%<::main%> must return %<int%>");
8150 newtype = build_function_type (integer_type_node, oldtypeargs);
8151 TREE_TYPE (decl) = newtype;
8153 if (warn_main)
8154 check_main_parameter_types (decl);
8157 if (ctype != NULL_TREE
8158 && (! TYPE_FOR_JAVA (ctype) || check_java_method (decl))
8159 && check)
8161 tree old_decl = check_classfn (ctype, decl,
8162 (processing_template_decl
8163 > template_class_depth (ctype))
8164 ? current_template_parms
8165 : NULL_TREE);
8167 if (old_decl == error_mark_node)
8168 return NULL_TREE;
8170 if (old_decl)
8172 tree ok;
8173 tree pushed_scope;
8175 if (TREE_CODE (old_decl) == TEMPLATE_DECL)
8176 /* Because grokfndecl is always supposed to return a
8177 FUNCTION_DECL, we pull out the DECL_TEMPLATE_RESULT
8178 here. We depend on our callers to figure out that its
8179 really a template that's being returned. */
8180 old_decl = DECL_TEMPLATE_RESULT (old_decl);
8182 if (DECL_STATIC_FUNCTION_P (old_decl)
8183 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
8185 /* Remove the `this' parm added by grokclassfn. */
8186 revert_static_member_fn (decl);
8187 check_static_quals (decl, quals);
8189 if (DECL_ARTIFICIAL (old_decl))
8191 error ("definition of implicitly-declared %qD", old_decl);
8192 return NULL_TREE;
8194 else if (DECL_DEFAULTED_FN (old_decl))
8196 error ("definition of explicitly-defaulted %q+D", decl);
8197 error ("%q+#D explicitly defaulted here", old_decl);
8198 return NULL_TREE;
8201 /* Since we've smashed OLD_DECL to its
8202 DECL_TEMPLATE_RESULT, we must do the same to DECL. */
8203 if (TREE_CODE (decl) == TEMPLATE_DECL)
8204 decl = DECL_TEMPLATE_RESULT (decl);
8206 /* Attempt to merge the declarations. This can fail, in
8207 the case of some invalid specialization declarations. */
8208 pushed_scope = push_scope (ctype);
8209 ok = duplicate_decls (decl, old_decl, friendp);
8210 if (pushed_scope)
8211 pop_scope (pushed_scope);
8212 if (!ok)
8214 error ("no %q#D member function declared in class %qT",
8215 decl, ctype);
8216 return NULL_TREE;
8218 if (ok == error_mark_node)
8219 return NULL_TREE;
8220 return old_decl;
8224 if (DECL_CONSTRUCTOR_P (decl) && !grok_ctor_properties (ctype, decl))
8225 return NULL_TREE;
8227 if (ctype == NULL_TREE || check)
8228 return decl;
8230 if (virtualp)
8231 DECL_VIRTUAL_P (decl) = 1;
8233 return decl;
8236 /* decl is a FUNCTION_DECL.
8237 specifiers are the parsed virt-specifiers.
8239 Set flags to reflect the virt-specifiers.
8241 Returns decl. */
8243 static tree
8244 set_virt_specifiers (tree decl, cp_virt_specifiers specifiers)
8246 if (decl == NULL_TREE)
8247 return decl;
8248 if (specifiers & VIRT_SPEC_OVERRIDE)
8249 DECL_OVERRIDE_P (decl) = 1;
8250 if (specifiers & VIRT_SPEC_FINAL)
8251 DECL_FINAL_P (decl) = 1;
8252 return decl;
8255 /* DECL is a VAR_DECL for a static data member. Set flags to reflect
8256 the linkage that DECL will receive in the object file. */
8258 static void
8259 set_linkage_for_static_data_member (tree decl)
8261 /* A static data member always has static storage duration and
8262 external linkage. Note that static data members are forbidden in
8263 local classes -- the only situation in which a class has
8264 non-external linkage. */
8265 TREE_PUBLIC (decl) = 1;
8266 TREE_STATIC (decl) = 1;
8267 /* For non-template classes, static data members are always put
8268 out in exactly those files where they are defined, just as
8269 with ordinary namespace-scope variables. */
8270 if (!processing_template_decl)
8271 DECL_INTERFACE_KNOWN (decl) = 1;
8274 /* Create a VAR_DECL named NAME with the indicated TYPE.
8276 If SCOPE is non-NULL, it is the class type or namespace containing
8277 the variable. If SCOPE is NULL, the variable should is created in
8278 the innermost enclosing scope. */
8280 static tree
8281 grokvardecl (tree type,
8282 tree name,
8283 tree orig_declarator,
8284 const cp_decl_specifier_seq *declspecs,
8285 int initialized,
8286 int flags,
8287 int template_count,
8288 tree scope)
8290 tree decl;
8291 tree explicit_scope;
8293 gcc_assert (!name || identifier_p (name));
8295 bool constp = flags&1;
8296 bool conceptp = flags&2;
8298 /* Compute the scope in which to place the variable, but remember
8299 whether or not that scope was explicitly specified by the user. */
8300 explicit_scope = scope;
8301 if (!scope)
8303 /* An explicit "extern" specifier indicates a namespace-scope
8304 variable. */
8305 if (declspecs->storage_class == sc_extern)
8306 scope = current_decl_namespace ();
8307 else if (!at_function_scope_p ())
8308 scope = current_scope ();
8311 if (scope
8312 && (/* If the variable is a namespace-scope variable declared in a
8313 template, we need DECL_LANG_SPECIFIC. */
8314 (TREE_CODE (scope) == NAMESPACE_DECL && processing_template_decl)
8315 /* Similarly for namespace-scope variables with language linkage
8316 other than C++. */
8317 || (TREE_CODE (scope) == NAMESPACE_DECL
8318 && current_lang_name != lang_name_cplusplus)
8319 /* Similarly for static data members. */
8320 || TYPE_P (scope)
8321 /* Similarly for explicit specializations. */
8322 || (orig_declarator
8323 && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)))
8324 decl = build_lang_decl (VAR_DECL, name, type);
8325 else
8326 decl = build_decl (input_location, VAR_DECL, name, type);
8328 if (explicit_scope && TREE_CODE (explicit_scope) == NAMESPACE_DECL)
8329 set_decl_namespace (decl, explicit_scope, 0);
8330 else
8331 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
8333 if (declspecs->storage_class == sc_extern)
8335 DECL_THIS_EXTERN (decl) = 1;
8336 DECL_EXTERNAL (decl) = !initialized;
8339 if (DECL_CLASS_SCOPE_P (decl))
8341 set_linkage_for_static_data_member (decl);
8342 /* This function is only called with out-of-class definitions. */
8343 DECL_EXTERNAL (decl) = 0;
8344 check_class_member_definition_namespace (decl);
8346 /* At top level, either `static' or no s.c. makes a definition
8347 (perhaps tentative), and absence of `static' makes it public. */
8348 else if (toplevel_bindings_p ())
8350 TREE_PUBLIC (decl) = (declspecs->storage_class != sc_static
8351 && (DECL_THIS_EXTERN (decl) || ! constp));
8352 TREE_STATIC (decl) = ! DECL_EXTERNAL (decl);
8354 /* Not at top level, only `static' makes a static definition. */
8355 else
8357 TREE_STATIC (decl) = declspecs->storage_class == sc_static;
8358 TREE_PUBLIC (decl) = DECL_EXTERNAL (decl);
8361 if (decl_spec_seq_has_spec_p (declspecs, ds_thread))
8363 if (DECL_EXTERNAL (decl) || TREE_STATIC (decl))
8364 set_decl_tls_model (decl, decl_default_tls_model (decl));
8365 if (declspecs->gnu_thread_keyword_p)
8366 DECL_GNU_TLS_P (decl) = true;
8369 /* If the type of the decl has no linkage, make sure that we'll
8370 notice that in mark_used. */
8371 if (cxx_dialect > cxx98
8372 && decl_linkage (decl) != lk_none
8373 && DECL_LANG_SPECIFIC (decl) == NULL
8374 && !DECL_EXTERN_C_P (decl)
8375 && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
8376 retrofit_lang_decl (decl);
8378 if (TREE_PUBLIC (decl))
8380 /* [basic.link]: A name with no linkage (notably, the name of a class
8381 or enumeration declared in a local scope) shall not be used to
8382 declare an entity with linkage.
8384 DR 757 relaxes this restriction for C++0x. */
8385 if (cxx_dialect < cxx11)
8386 no_linkage_error (decl);
8388 else
8389 DECL_INTERFACE_KNOWN (decl) = 1;
8391 // Check that the variable can be safely declared as a concept.
8392 if (conceptp)
8394 if (!processing_template_decl)
8396 error ("a non-template variable cannot be %<concept%>");
8397 return NULL_TREE;
8399 else
8400 DECL_DECLARED_CONCEPT_P (decl) = true;
8403 // Handle explicit specializations and instantiations of variable templates.
8404 if (orig_declarator)
8405 decl = check_explicit_specialization (orig_declarator, decl,
8406 template_count, conceptp * 8);
8408 return decl != error_mark_node ? decl : NULL_TREE;
8411 /* Create and return a canonical pointer to member function type, for
8412 TYPE, which is a POINTER_TYPE to a METHOD_TYPE. */
8414 tree
8415 build_ptrmemfunc_type (tree type)
8417 tree field, fields;
8418 tree t;
8420 if (type == error_mark_node)
8421 return type;
8423 /* If a canonical type already exists for this type, use it. We use
8424 this method instead of type_hash_canon, because it only does a
8425 simple equality check on the list of field members. */
8427 if ((t = TYPE_GET_PTRMEMFUNC_TYPE (type)))
8428 return t;
8430 /* Make sure that we always have the unqualified pointer-to-member
8431 type first. */
8432 if (cp_cv_quals quals = cp_type_quals (type))
8434 tree unqual = build_ptrmemfunc_type (TYPE_MAIN_VARIANT (type));
8435 return cp_build_qualified_type (unqual, quals);
8438 t = make_node (RECORD_TYPE);
8440 /* Let the front end know this is a pointer to member function. */
8441 TYPE_PTRMEMFUNC_FLAG (t) = 1;
8443 field = build_decl (input_location, FIELD_DECL, pfn_identifier, type);
8444 fields = field;
8446 field = build_decl (input_location, FIELD_DECL, delta_identifier,
8447 delta_type_node);
8448 DECL_CHAIN (field) = fields;
8449 fields = field;
8451 finish_builtin_struct (t, "__ptrmemfunc_type", fields, ptr_type_node);
8453 /* Zap out the name so that the back end will give us the debugging
8454 information for this anonymous RECORD_TYPE. */
8455 TYPE_NAME (t) = NULL_TREE;
8457 /* Cache this pointer-to-member type so that we can find it again
8458 later. */
8459 TYPE_SET_PTRMEMFUNC_TYPE (type, t);
8461 if (TYPE_STRUCTURAL_EQUALITY_P (type))
8462 SET_TYPE_STRUCTURAL_EQUALITY (t);
8463 else if (TYPE_CANONICAL (type) != type)
8464 TYPE_CANONICAL (t) = build_ptrmemfunc_type (TYPE_CANONICAL (type));
8466 return t;
8469 /* Create and return a pointer to data member type. */
8471 tree
8472 build_ptrmem_type (tree class_type, tree member_type)
8474 if (TREE_CODE (member_type) == METHOD_TYPE)
8476 cp_cv_quals quals = type_memfn_quals (member_type);
8477 cp_ref_qualifier rqual = type_memfn_rqual (member_type);
8478 member_type = build_memfn_type (member_type, class_type, quals, rqual);
8479 return build_ptrmemfunc_type (build_pointer_type (member_type));
8481 else
8483 gcc_assert (TREE_CODE (member_type) != FUNCTION_TYPE);
8484 return build_offset_type (class_type, member_type);
8488 /* DECL is a VAR_DECL defined in-class, whose TYPE is also given.
8489 Check to see that the definition is valid. Issue appropriate error
8490 messages. Return 1 if the definition is particularly bad, or 0
8491 otherwise. */
8493 static int
8494 check_static_variable_definition (tree decl, tree type)
8496 /* Can't check yet if we don't know the type. */
8497 if (dependent_type_p (type))
8498 return 0;
8499 /* If DECL is declared constexpr, we'll do the appropriate checks
8500 in check_initializer. */
8501 if (DECL_P (decl) && DECL_DECLARED_CONSTEXPR_P (decl))
8502 return 0;
8503 else if (cxx_dialect >= cxx11 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
8505 if (!COMPLETE_TYPE_P (type))
8506 error ("in-class initialization of static data member %q#D of "
8507 "incomplete type", decl);
8508 else if (literal_type_p (type))
8509 permerror (input_location,
8510 "%<constexpr%> needed for in-class initialization of "
8511 "static data member %q#D of non-integral type", decl);
8512 else
8513 error ("in-class initialization of static data member %q#D of "
8514 "non-literal type", decl);
8515 return 1;
8518 /* Motion 10 at San Diego: If a static const integral data member is
8519 initialized with an integral constant expression, the initializer
8520 may appear either in the declaration (within the class), or in
8521 the definition, but not both. If it appears in the class, the
8522 member is a member constant. The file-scope definition is always
8523 required. */
8524 if (!ARITHMETIC_TYPE_P (type) && TREE_CODE (type) != ENUMERAL_TYPE)
8526 error ("invalid in-class initialization of static data member "
8527 "of non-integral type %qT",
8528 type);
8529 return 1;
8531 else if (!CP_TYPE_CONST_P (type))
8532 error ("ISO C++ forbids in-class initialization of non-const "
8533 "static member %qD",
8534 decl);
8535 else if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type))
8536 pedwarn (input_location, OPT_Wpedantic, "ISO C++ forbids initialization of member constant "
8537 "%qD of non-integral type %qT", decl, type);
8539 return 0;
8542 /* *expr_p is part of the TYPE_SIZE of a variably-sized array. If any
8543 SAVE_EXPRs in *expr_p wrap expressions with side-effects, break those
8544 expressions out into temporary variables so that walk_tree doesn't
8545 step into them (c++/15764). */
8547 static tree
8548 stabilize_save_expr_r (tree *expr_p, int *walk_subtrees, void *data)
8550 hash_set<tree> *pset = (hash_set<tree> *)data;
8551 tree expr = *expr_p;
8552 if (TREE_CODE (expr) == SAVE_EXPR)
8554 tree op = TREE_OPERAND (expr, 0);
8555 cp_walk_tree (&op, stabilize_save_expr_r, data, pset);
8556 if (TREE_SIDE_EFFECTS (op))
8557 TREE_OPERAND (expr, 0) = get_temp_regvar (TREE_TYPE (op), op);
8558 *walk_subtrees = 0;
8560 else if (!EXPR_P (expr) || !TREE_SIDE_EFFECTS (expr))
8561 *walk_subtrees = 0;
8562 return NULL;
8565 /* Entry point for the above. */
8567 static void
8568 stabilize_vla_size (tree size)
8570 hash_set<tree> pset;
8571 /* Break out any function calls into temporary variables. */
8572 cp_walk_tree (&size, stabilize_save_expr_r, &pset, &pset);
8575 /* Helper function for compute_array_index_type. Look for SIZEOF_EXPR
8576 not inside of SAVE_EXPR and fold them. */
8578 static tree
8579 fold_sizeof_expr_r (tree *expr_p, int *walk_subtrees, void *data)
8581 tree expr = *expr_p;
8582 if (TREE_CODE (expr) == SAVE_EXPR || TYPE_P (expr))
8583 *walk_subtrees = 0;
8584 else if (TREE_CODE (expr) == SIZEOF_EXPR)
8586 *(bool *)data = true;
8587 if (SIZEOF_EXPR_TYPE_P (expr))
8588 expr = cxx_sizeof_or_alignof_type (TREE_TYPE (TREE_OPERAND (expr, 0)),
8589 SIZEOF_EXPR, false);
8590 else if (TYPE_P (TREE_OPERAND (expr, 0)))
8591 expr = cxx_sizeof_or_alignof_type (TREE_OPERAND (expr, 0), SIZEOF_EXPR,
8592 false);
8593 else
8594 expr = cxx_sizeof_or_alignof_expr (TREE_OPERAND (expr, 0), SIZEOF_EXPR,
8595 false);
8596 if (expr == error_mark_node)
8597 expr = size_one_node;
8598 *expr_p = expr;
8599 *walk_subtrees = 0;
8601 return NULL;
8604 /* Given the SIZE (i.e., number of elements) in an array, compute an
8605 appropriate index type for the array. If non-NULL, NAME is the
8606 name of the thing being declared. */
8608 tree
8609 compute_array_index_type (tree name, tree size, tsubst_flags_t complain)
8611 tree itype;
8612 tree osize = size;
8614 if (error_operand_p (size))
8615 return error_mark_node;
8617 if (!type_dependent_expression_p (size))
8619 tree type = TREE_TYPE (size);
8621 mark_rvalue_use (size);
8623 if (cxx_dialect < cxx11 && TREE_CODE (size) == NOP_EXPR
8624 && TREE_SIDE_EFFECTS (size))
8625 /* In C++98, we mark a non-constant array bound with a magic
8626 NOP_EXPR with TREE_SIDE_EFFECTS; don't fold in that case. */;
8627 else
8629 size = instantiate_non_dependent_expr_sfinae (size, complain);
8631 if (CLASS_TYPE_P (type)
8632 && CLASSTYPE_LITERAL_P (type))
8634 size = build_expr_type_conversion (WANT_INT, size, true);
8635 if (!size)
8637 if (!(complain & tf_error))
8638 return error_mark_node;
8639 if (name)
8640 error ("size of array %qD has non-integral type %qT",
8641 name, type);
8642 else
8643 error ("size of array has non-integral type %qT", type);
8644 size = integer_one_node;
8646 if (size == error_mark_node)
8647 return error_mark_node;
8648 type = TREE_TYPE (size);
8651 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
8652 size = maybe_constant_value (size);
8654 if (!TREE_CONSTANT (size))
8655 size = osize;
8658 if (error_operand_p (size))
8659 return error_mark_node;
8661 /* The array bound must be an integer type. */
8662 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
8664 if (!(complain & tf_error))
8665 return error_mark_node;
8666 if (name)
8667 error ("size of array %qD has non-integral type %qT", name, type);
8668 else
8669 error ("size of array has non-integral type %qT", type);
8670 size = integer_one_node;
8671 type = TREE_TYPE (size);
8675 /* A type is dependent if it is...an array type constructed from any
8676 dependent type or whose size is specified by a constant expression
8677 that is value-dependent. */
8678 /* We can only call value_dependent_expression_p on integral constant
8679 expressions; treat non-constant expressions as dependent, too. */
8680 if (processing_template_decl
8681 && (type_dependent_expression_p (size)
8682 || !TREE_CONSTANT (size) || value_dependent_expression_p (size)))
8684 /* We cannot do any checking for a SIZE that isn't known to be
8685 constant. Just build the index type and mark that it requires
8686 structural equality checks. */
8687 itype = build_index_type (build_min (MINUS_EXPR, sizetype,
8688 size, size_one_node));
8689 TYPE_DEPENDENT_P (itype) = 1;
8690 TYPE_DEPENDENT_P_VALID (itype) = 1;
8691 SET_TYPE_STRUCTURAL_EQUALITY (itype);
8692 return itype;
8695 /* Normally, the array-bound will be a constant. */
8696 if (TREE_CODE (size) == INTEGER_CST)
8698 /* Check to see if the array bound overflowed. Make that an
8699 error, no matter how generous we're being. */
8700 constant_expression_error (size);
8702 /* An array must have a positive number of elements. */
8703 if (tree_int_cst_lt (size, integer_zero_node))
8705 if (!(complain & tf_error))
8706 return error_mark_node;
8707 if (name)
8708 error ("size of array %qD is negative", name);
8709 else
8710 error ("size of array is negative");
8711 size = integer_one_node;
8713 /* As an extension we allow zero-sized arrays. */
8714 else if (integer_zerop (size))
8716 if (!(complain & tf_error))
8717 /* We must fail if performing argument deduction (as
8718 indicated by the state of complain), so that
8719 another substitution can be found. */
8720 return error_mark_node;
8721 else if (in_system_header_at (input_location))
8722 /* Allow them in system headers because glibc uses them. */;
8723 else if (name)
8724 pedwarn (input_location, OPT_Wpedantic, "ISO C++ forbids zero-size array %qD", name);
8725 else
8726 pedwarn (input_location, OPT_Wpedantic, "ISO C++ forbids zero-size array");
8729 else if (TREE_CONSTANT (size)
8730 /* We don't allow VLAs at non-function scopes, or during
8731 tentative template substitution. */
8732 || !at_function_scope_p ()
8733 || !(complain & tf_error))
8735 if (!(complain & tf_error))
8736 return error_mark_node;
8737 /* `(int) &fn' is not a valid array bound. */
8738 if (name)
8739 error ("size of array %qD is not an integral constant-expression",
8740 name);
8741 else
8742 error ("size of array is not an integral constant-expression");
8743 size = integer_one_node;
8745 else if (pedantic && warn_vla != 0)
8747 if (name)
8748 pedwarn (input_location, OPT_Wvla, "ISO C++ forbids variable length array %qD", name);
8749 else
8750 pedwarn (input_location, OPT_Wvla, "ISO C++ forbids variable length array");
8752 else if (warn_vla > 0)
8754 if (name)
8755 warning (OPT_Wvla,
8756 "variable length array %qD is used", name);
8757 else
8758 warning (OPT_Wvla,
8759 "variable length array is used");
8762 if (processing_template_decl && !TREE_CONSTANT (size))
8763 /* A variable sized array. */
8764 itype = build_min (MINUS_EXPR, sizetype, size, integer_one_node);
8765 else
8767 HOST_WIDE_INT saved_processing_template_decl;
8769 /* Compute the index of the largest element in the array. It is
8770 one less than the number of elements in the array. We save
8771 and restore PROCESSING_TEMPLATE_DECL so that computations in
8772 cp_build_binary_op will be appropriately folded. */
8773 saved_processing_template_decl = processing_template_decl;
8774 processing_template_decl = 0;
8775 itype = cp_build_binary_op (input_location,
8776 MINUS_EXPR,
8777 cp_convert (ssizetype, size, complain),
8778 cp_convert (ssizetype, integer_one_node,
8779 complain),
8780 complain);
8781 itype = fold (itype);
8782 processing_template_decl = saved_processing_template_decl;
8784 if (!TREE_CONSTANT (itype))
8786 /* A variable sized array. */
8787 itype = variable_size (itype);
8789 if (TREE_CODE (itype) != SAVE_EXPR)
8791 /* Look for SIZEOF_EXPRs in itype and fold them, otherwise
8792 they might survive till gimplification. */
8793 tree newitype = itype;
8794 bool found = false;
8795 cp_walk_tree_without_duplicates (&newitype,
8796 fold_sizeof_expr_r, &found);
8797 if (found)
8798 itype = variable_size (fold (newitype));
8801 stabilize_vla_size (itype);
8803 if (flag_sanitize & SANITIZE_VLA
8804 && do_ubsan_in_current_function ())
8806 /* We have to add 1 -- in the ubsan routine we generate
8807 LE_EXPR rather than LT_EXPR. */
8808 tree t = fold_build2 (PLUS_EXPR, TREE_TYPE (itype), itype,
8809 build_one_cst (TREE_TYPE (itype)));
8810 t = ubsan_instrument_vla (input_location, t);
8811 finish_expr_stmt (t);
8814 /* Make sure that there was no overflow when creating to a signed
8815 index type. (For example, on a 32-bit machine, an array with
8816 size 2^32 - 1 is too big.) */
8817 else if (TREE_CODE (itype) == INTEGER_CST
8818 && TREE_OVERFLOW (itype))
8820 if (!(complain & tf_error))
8821 return error_mark_node;
8822 error ("overflow in array dimension");
8823 TREE_OVERFLOW (itype) = 0;
8827 /* Create and return the appropriate index type. */
8828 itype = build_index_type (itype);
8830 /* If the index type were dependent, we would have returned early, so
8831 remember that it isn't. */
8832 TYPE_DEPENDENT_P (itype) = 0;
8833 TYPE_DEPENDENT_P_VALID (itype) = 1;
8834 return itype;
8837 /* Returns the scope (if any) in which the entity declared by
8838 DECLARATOR will be located. If the entity was declared with an
8839 unqualified name, NULL_TREE is returned. */
8841 tree
8842 get_scope_of_declarator (const cp_declarator *declarator)
8844 while (declarator && declarator->kind != cdk_id)
8845 declarator = declarator->declarator;
8847 /* If the declarator-id is a SCOPE_REF, the scope in which the
8848 declaration occurs is the first operand. */
8849 if (declarator
8850 && declarator->u.id.qualifying_scope)
8851 return declarator->u.id.qualifying_scope;
8853 /* Otherwise, the declarator is not a qualified name; the entity will
8854 be declared in the current scope. */
8855 return NULL_TREE;
8858 /* Returns an ARRAY_TYPE for an array with SIZE elements of the
8859 indicated TYPE. If non-NULL, NAME is the NAME of the declaration
8860 with this type. */
8862 static tree
8863 create_array_type_for_decl (tree name, tree type, tree size)
8865 tree itype = NULL_TREE;
8867 /* If things have already gone awry, bail now. */
8868 if (type == error_mark_node || size == error_mark_node)
8869 return error_mark_node;
8871 /* 8.3.4/1: If the type of the identifier of D contains the auto
8872 type-specifier, the program is ill-formed. */
8873 if (type_uses_auto (type))
8875 error ("%qD declared as array of %qT", name, type);
8876 return error_mark_node;
8879 /* If there are some types which cannot be array elements,
8880 issue an error-message and return. */
8881 switch (TREE_CODE (type))
8883 case VOID_TYPE:
8884 if (name)
8885 error ("declaration of %qD as array of void", name);
8886 else
8887 error ("creating array of void");
8888 return error_mark_node;
8890 case FUNCTION_TYPE:
8891 if (name)
8892 error ("declaration of %qD as array of functions", name);
8893 else
8894 error ("creating array of functions");
8895 return error_mark_node;
8897 case REFERENCE_TYPE:
8898 if (name)
8899 error ("declaration of %qD as array of references", name);
8900 else
8901 error ("creating array of references");
8902 return error_mark_node;
8904 case METHOD_TYPE:
8905 if (name)
8906 error ("declaration of %qD as array of function members", name);
8907 else
8908 error ("creating array of function members");
8909 return error_mark_node;
8911 default:
8912 break;
8915 /* [dcl.array]
8917 The constant expressions that specify the bounds of the arrays
8918 can be omitted only for the first member of the sequence. */
8919 if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type))
8921 if (name)
8922 error ("declaration of %qD as multidimensional array must "
8923 "have bounds for all dimensions except the first",
8924 name);
8925 else
8926 error ("multidimensional array must have bounds for all "
8927 "dimensions except the first");
8929 return error_mark_node;
8932 /* Figure out the index type for the array. */
8933 if (size)
8934 itype = compute_array_index_type (name, size, tf_warning_or_error);
8936 /* [dcl.array]
8937 T is called the array element type; this type shall not be [...] an
8938 abstract class type. */
8939 abstract_virtuals_error (name, type);
8941 return build_cplus_array_type (type, itype);
8944 /* Check that it's OK to declare a function with the indicated TYPE.
8945 SFK indicates the kind of special function (if any) that this
8946 function is. OPTYPE is the type given in a conversion operator
8947 declaration, or the class type for a constructor/destructor.
8948 Returns the actual return type of the function; that
8949 may be different than TYPE if an error occurs, or for certain
8950 special functions. */
8952 static tree
8953 check_special_function_return_type (special_function_kind sfk,
8954 tree type,
8955 tree optype)
8957 switch (sfk)
8959 case sfk_constructor:
8960 if (type)
8961 error ("return type specification for constructor invalid");
8963 if (targetm.cxx.cdtor_returns_this () && !TYPE_FOR_JAVA (optype))
8964 type = build_pointer_type (optype);
8965 else
8966 type = void_type_node;
8967 break;
8969 case sfk_destructor:
8970 if (type)
8971 error ("return type specification for destructor invalid");
8972 /* We can't use the proper return type here because we run into
8973 problems with ambiguous bases and covariant returns.
8974 Java classes are left unchanged because (void *) isn't a valid
8975 Java type, and we don't want to change the Java ABI. */
8976 if (targetm.cxx.cdtor_returns_this () && !TYPE_FOR_JAVA (optype))
8977 type = build_pointer_type (void_type_node);
8978 else
8979 type = void_type_node;
8980 break;
8982 case sfk_conversion:
8983 if (type)
8984 error ("return type specified for %<operator %T%>", optype);
8985 type = optype;
8986 break;
8988 default:
8989 gcc_unreachable ();
8992 return type;
8995 /* A variable or data member (whose unqualified name is IDENTIFIER)
8996 has been declared with the indicated TYPE. If the TYPE is not
8997 acceptable, issue an error message and return a type to use for
8998 error-recovery purposes. */
9000 tree
9001 check_var_type (tree identifier, tree type)
9003 if (VOID_TYPE_P (type))
9005 if (!identifier)
9006 error ("unnamed variable or field declared void");
9007 else if (identifier_p (identifier))
9009 gcc_assert (!IDENTIFIER_OPNAME_P (identifier));
9010 error ("variable or field %qE declared void", identifier);
9012 else
9013 error ("variable or field declared void");
9014 type = error_mark_node;
9017 return type;
9020 // Return a trailing requires clause for a function declarator, or
9021 // NULL_TREE if there is no trailing requires clause or the declarator
9022 // is some other kind.
9023 static inline tree
9024 get_trailing_requires_clause (const cp_declarator *declarator)
9026 if (declarator && declarator->kind == cdk_function)
9027 return declarator->u.function.requires_clause;
9028 else
9029 return NULL_TREE;
9032 /* Given declspecs and a declarator (abstract or otherwise), determine
9033 the name and type of the object declared and construct a DECL node
9034 for it.
9036 DECLSPECS points to the representation of declaration-specifier
9037 sequence that precedes declarator.
9039 DECL_CONTEXT says which syntactic context this declaration is in:
9040 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
9041 FUNCDEF for a function definition. Like NORMAL but a few different
9042 error messages in each case. Return value may be zero meaning
9043 this definition is too screwy to try to parse.
9044 MEMFUNCDEF for a function definition. Like FUNCDEF but prepares to
9045 handle member functions (which have FIELD context).
9046 Return value may be zero meaning this definition is too screwy to
9047 try to parse.
9048 PARM for a parameter declaration (either within a function prototype
9049 or before a function body). Make a PARM_DECL, or return void_type_node.
9050 TPARM for a template parameter declaration.
9051 CATCHPARM for a parameter declaration before a catch clause.
9052 TYPENAME if for a typename (in a cast or sizeof).
9053 Don't make a DECL node; just return the ..._TYPE node.
9054 FIELD for a struct or union field; make a FIELD_DECL.
9055 BITFIELD for a field with specified width.
9057 INITIALIZED is as for start_decl.
9059 ATTRLIST is a pointer to the list of attributes, which may be NULL
9060 if there are none; *ATTRLIST may be modified if attributes from inside
9061 the declarator should be applied to the declaration.
9063 When this function is called, scoping variables (such as
9064 CURRENT_CLASS_TYPE) should reflect the scope in which the
9065 declaration occurs, not the scope in which the new declaration will
9066 be placed. For example, on:
9068 void S::f() { ... }
9070 when grokdeclarator is called for `S::f', the CURRENT_CLASS_TYPE
9071 should not be `S'.
9073 Returns a DECL (if a declarator is present), a TYPE (if there is no
9074 declarator, in cases like "struct S;"), or the ERROR_MARK_NODE if an
9075 error occurs. */
9077 tree
9078 grokdeclarator (const cp_declarator *declarator,
9079 cp_decl_specifier_seq *declspecs,
9080 enum decl_context decl_context,
9081 int initialized,
9082 tree* attrlist)
9084 tree type = NULL_TREE;
9085 int longlong = 0;
9086 int explicit_intN = 0;
9087 int virtualp, explicitp, friendp, inlinep, staticp;
9088 int explicit_int = 0;
9089 int explicit_char = 0;
9090 int defaulted_int = 0;
9092 tree typedef_decl = NULL_TREE;
9093 const char *name = NULL;
9094 tree typedef_type = NULL_TREE;
9095 /* True if this declarator is a function definition. */
9096 bool funcdef_flag = false;
9097 cp_declarator_kind innermost_code = cdk_error;
9098 int bitfield = 0;
9099 #if 0
9100 /* See the code below that used this. */
9101 tree decl_attr = NULL_TREE;
9102 #endif
9104 /* Keep track of what sort of function is being processed
9105 so that we can warn about default return values, or explicit
9106 return values which do not match prescribed defaults. */
9107 special_function_kind sfk = sfk_none;
9109 tree dname = NULL_TREE;
9110 tree ctor_return_type = NULL_TREE;
9111 enum overload_flags flags = NO_SPECIAL;
9112 /* cv-qualifiers that apply to the declarator, for a declaration of
9113 a member function. */
9114 cp_cv_quals memfn_quals = TYPE_UNQUALIFIED;
9115 /* virt-specifiers that apply to the declarator, for a declaration of
9116 a member function. */
9117 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
9118 /* ref-qualifier that applies to the declarator, for a declaration of
9119 a member function. */
9120 cp_ref_qualifier rqual = REF_QUAL_NONE;
9121 /* cv-qualifiers that apply to the type specified by the DECLSPECS. */
9122 int type_quals;
9123 tree raises = NULL_TREE;
9124 int template_count = 0;
9125 tree returned_attrs = NULL_TREE;
9126 tree parms = NULL_TREE;
9127 const cp_declarator *id_declarator;
9128 /* The unqualified name of the declarator; either an
9129 IDENTIFIER_NODE, BIT_NOT_EXPR, or TEMPLATE_ID_EXPR. */
9130 tree unqualified_id;
9131 /* The class type, if any, in which this entity is located,
9132 or NULL_TREE if none. Note that this value may be different from
9133 the current class type; for example if an attempt is made to declare
9134 "A::f" inside "B", this value will be "A". */
9135 tree ctype = current_class_type;
9136 /* The NAMESPACE_DECL for the namespace in which this entity is
9137 located. If an unqualified name is used to declare the entity,
9138 this value will be NULL_TREE, even if the entity is located at
9139 namespace scope. */
9140 tree in_namespace = NULL_TREE;
9141 cp_storage_class storage_class;
9142 bool unsigned_p, signed_p, short_p, long_p, thread_p;
9143 bool type_was_error_mark_node = false;
9144 bool parameter_pack_p = declarator? declarator->parameter_pack_p : false;
9145 bool template_type_arg = false;
9146 bool template_parm_flag = false;
9147 bool typedef_p = decl_spec_seq_has_spec_p (declspecs, ds_typedef);
9148 bool constexpr_p = decl_spec_seq_has_spec_p (declspecs, ds_constexpr);
9149 bool late_return_type_p = false;
9150 bool array_parameter_p = false;
9151 source_location saved_loc = input_location;
9152 const char *errmsg;
9154 signed_p = decl_spec_seq_has_spec_p (declspecs, ds_signed);
9155 unsigned_p = decl_spec_seq_has_spec_p (declspecs, ds_unsigned);
9156 short_p = decl_spec_seq_has_spec_p (declspecs, ds_short);
9157 long_p = decl_spec_seq_has_spec_p (declspecs, ds_long);
9158 longlong = decl_spec_seq_has_spec_p (declspecs, ds_long_long);
9159 explicit_intN = declspecs->explicit_intN_p;
9160 thread_p = decl_spec_seq_has_spec_p (declspecs, ds_thread);
9162 // Get a requires-clause attached to the declarator.
9163 tree reqs = get_trailing_requires_clause (declarator);
9165 // Was concept_p specified? Note that ds_concept
9166 // implies ds_constexpr!
9167 bool concept_p = decl_spec_seq_has_spec_p (declspecs, ds_concept);
9168 if (concept_p)
9169 constexpr_p = true;
9171 if (decl_context == FUNCDEF)
9172 funcdef_flag = true, decl_context = NORMAL;
9173 else if (decl_context == MEMFUNCDEF)
9174 funcdef_flag = true, decl_context = FIELD;
9175 else if (decl_context == BITFIELD)
9176 bitfield = 1, decl_context = FIELD;
9177 else if (decl_context == TEMPLATE_TYPE_ARG)
9178 template_type_arg = true, decl_context = TYPENAME;
9179 else if (decl_context == TPARM)
9180 template_parm_flag = true, decl_context = PARM;
9182 if (initialized > 1)
9183 funcdef_flag = true;
9185 /* Look inside a declarator for the name being declared
9186 and get it as a string, for an error message. */
9187 for (id_declarator = declarator;
9188 id_declarator;
9189 id_declarator = id_declarator->declarator)
9191 if (id_declarator->kind != cdk_id)
9192 innermost_code = id_declarator->kind;
9194 switch (id_declarator->kind)
9196 case cdk_function:
9197 if (id_declarator->declarator
9198 && id_declarator->declarator->kind == cdk_id)
9200 sfk = id_declarator->declarator->u.id.sfk;
9201 if (sfk == sfk_destructor)
9202 flags = DTOR_FLAG;
9204 break;
9206 case cdk_id:
9208 tree qualifying_scope = id_declarator->u.id.qualifying_scope;
9209 tree decl = id_declarator->u.id.unqualified_name;
9210 if (!decl)
9211 break;
9212 if (qualifying_scope)
9214 if (at_function_scope_p ())
9216 /* [dcl.meaning]
9218 A declarator-id shall not be qualified except
9219 for ...
9221 None of the cases are permitted in block
9222 scope. */
9223 if (qualifying_scope == global_namespace)
9224 error ("invalid use of qualified-name %<::%D%>",
9225 decl);
9226 else if (TYPE_P (qualifying_scope))
9227 error ("invalid use of qualified-name %<%T::%D%>",
9228 qualifying_scope, decl);
9229 else
9230 error ("invalid use of qualified-name %<%D::%D%>",
9231 qualifying_scope, decl);
9232 return error_mark_node;
9234 else if (TYPE_P (qualifying_scope))
9236 ctype = qualifying_scope;
9237 if (!MAYBE_CLASS_TYPE_P (ctype))
9239 error ("%q#T is not a class or a namespace", ctype);
9240 ctype = NULL_TREE;
9242 else if (innermost_code != cdk_function
9243 && current_class_type
9244 && !uniquely_derived_from_p (ctype,
9245 current_class_type))
9247 error ("invalid use of qualified-name %<%T::%D%>",
9248 qualifying_scope, decl);
9249 return error_mark_node;
9252 else if (TREE_CODE (qualifying_scope) == NAMESPACE_DECL)
9253 in_namespace = qualifying_scope;
9255 switch (TREE_CODE (decl))
9257 case BIT_NOT_EXPR:
9259 tree type;
9261 if (innermost_code != cdk_function)
9263 error ("declaration of %qD as non-function", decl);
9264 return error_mark_node;
9266 else if (!qualifying_scope
9267 && !(current_class_type && at_class_scope_p ()))
9269 error ("declaration of %qD as non-member", decl);
9270 return error_mark_node;
9273 type = TREE_OPERAND (decl, 0);
9274 if (TYPE_P (type))
9275 type = constructor_name (type);
9276 name = identifier_to_locale (IDENTIFIER_POINTER (type));
9277 dname = decl;
9279 break;
9281 case TEMPLATE_ID_EXPR:
9283 tree fns = TREE_OPERAND (decl, 0);
9285 dname = fns;
9286 if (!identifier_p (dname))
9288 if (variable_template_p (dname))
9289 dname = DECL_NAME (dname);
9290 else
9292 gcc_assert (is_overloaded_fn (dname));
9293 dname = DECL_NAME (get_first_fn (dname));
9297 /* Fall through. */
9299 case IDENTIFIER_NODE:
9300 if (identifier_p (decl))
9301 dname = decl;
9303 if (C_IS_RESERVED_WORD (dname))
9305 error ("declarator-id missing; using reserved word %qD",
9306 dname);
9307 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
9309 else if (!IDENTIFIER_TYPENAME_P (dname))
9310 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
9311 else
9313 gcc_assert (flags == NO_SPECIAL);
9314 flags = TYPENAME_FLAG;
9315 ctor_return_type = TREE_TYPE (dname);
9316 sfk = sfk_conversion;
9317 if (is_typename_at_global_scope (dname))
9318 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
9319 else
9320 name = "<invalid operator>";
9322 break;
9324 default:
9325 gcc_unreachable ();
9327 break;
9330 case cdk_array:
9331 case cdk_pointer:
9332 case cdk_reference:
9333 case cdk_ptrmem:
9334 break;
9336 case cdk_error:
9337 return error_mark_node;
9339 default:
9340 gcc_unreachable ();
9342 if (id_declarator->kind == cdk_id)
9343 break;
9346 /* [dcl.fct.edf]
9348 The declarator in a function-definition shall have the form
9349 D1 ( parameter-declaration-clause) ... */
9350 if (funcdef_flag && innermost_code != cdk_function)
9352 error ("function definition does not declare parameters");
9353 return error_mark_node;
9356 if (flags == TYPENAME_FLAG
9357 && innermost_code != cdk_function
9358 && ! (ctype && !declspecs->any_specifiers_p))
9360 error ("declaration of %qD as non-function", dname);
9361 return error_mark_node;
9364 if (dname
9365 && identifier_p (dname)
9366 && UDLIT_OPER_P (dname)
9367 && innermost_code != cdk_function)
9369 error ("declaration of %qD as non-function", dname);
9370 return error_mark_node;
9373 if (dname && IDENTIFIER_OPNAME_P (dname))
9375 if (typedef_p)
9377 error ("declaration of %qD as %<typedef%>", dname);
9378 return error_mark_node;
9380 else if (decl_context == PARM || decl_context == CATCHPARM)
9382 error ("declaration of %qD as parameter", dname);
9383 return error_mark_node;
9387 /* Anything declared one level down from the top level
9388 must be one of the parameters of a function
9389 (because the body is at least two levels down). */
9391 /* This heuristic cannot be applied to C++ nodes! Fixed, however,
9392 by not allowing C++ class definitions to specify their parameters
9393 with xdecls (must be spec.d in the parmlist).
9395 Since we now wait to push a class scope until we are sure that
9396 we are in a legitimate method context, we must set oldcname
9397 explicitly (since current_class_name is not yet alive).
9399 We also want to avoid calling this a PARM if it is in a namespace. */
9401 if (decl_context == NORMAL && !toplevel_bindings_p ())
9403 cp_binding_level *b = current_binding_level;
9404 current_binding_level = b->level_chain;
9405 if (current_binding_level != 0 && toplevel_bindings_p ())
9406 decl_context = PARM;
9407 current_binding_level = b;
9410 if (name == NULL)
9411 name = decl_context == PARM ? "parameter" : "type name";
9413 if (concept_p && typedef_p)
9415 error ("%<concept%> cannot appear in a typedef declaration");
9416 return error_mark_node;
9419 if (constexpr_p && typedef_p)
9421 error ("%<constexpr%> cannot appear in a typedef declaration");
9422 return error_mark_node;
9425 /* If there were multiple types specified in the decl-specifier-seq,
9426 issue an error message. */
9427 if (declspecs->multiple_types_p)
9429 error ("two or more data types in declaration of %qs", name);
9430 return error_mark_node;
9433 if (declspecs->conflicting_specifiers_p)
9435 error ("conflicting specifiers in declaration of %qs", name);
9436 return error_mark_node;
9439 /* Extract the basic type from the decl-specifier-seq. */
9440 type = declspecs->type;
9441 if (type == error_mark_node)
9443 type = NULL_TREE;
9444 type_was_error_mark_node = true;
9446 /* If the entire declaration is itself tagged as deprecated then
9447 suppress reports of deprecated items. */
9448 if (type && TREE_DEPRECATED (type)
9449 && deprecated_state != DEPRECATED_SUPPRESS)
9450 warn_deprecated_use (type, NULL_TREE);
9451 if (type && TREE_CODE (type) == TYPE_DECL)
9453 typedef_decl = type;
9454 type = TREE_TYPE (typedef_decl);
9455 if (TREE_DEPRECATED (type)
9456 && DECL_ARTIFICIAL (typedef_decl)
9457 && deprecated_state != DEPRECATED_SUPPRESS)
9458 warn_deprecated_use (type, NULL_TREE);
9460 /* No type at all: default to `int', and set DEFAULTED_INT
9461 because it was not a user-defined typedef. */
9462 if (type == NULL_TREE)
9464 if (signed_p || unsigned_p || long_p || short_p)
9466 /* These imply 'int'. */
9467 type = integer_type_node;
9468 defaulted_int = 1;
9470 /* If we just have "complex", it is equivalent to "complex double". */
9471 else if (!longlong && !explicit_intN
9472 && decl_spec_seq_has_spec_p (declspecs, ds_complex))
9474 type = double_type_node;
9475 pedwarn (declspecs->locations[ds_complex], OPT_Wpedantic,
9476 "ISO C++ does not support plain %<complex%> meaning "
9477 "%<double complex%>");
9480 /* Gather flags. */
9481 explicit_int = declspecs->explicit_int_p;
9482 explicit_char = declspecs->explicit_char_p;
9484 #if 0
9485 /* See the code below that used this. */
9486 if (typedef_decl)
9487 decl_attr = DECL_ATTRIBUTES (typedef_decl);
9488 #endif
9489 typedef_type = type;
9492 if (sfk != sfk_conversion)
9493 ctor_return_type = ctype;
9495 if (sfk != sfk_none)
9496 type = check_special_function_return_type (sfk, type,
9497 ctor_return_type);
9498 else if (type == NULL_TREE)
9500 int is_main;
9502 explicit_int = -1;
9504 /* We handle `main' specially here, because 'main () { }' is so
9505 common. With no options, it is allowed. With -Wreturn-type,
9506 it is a warning. It is only an error with -pedantic-errors. */
9507 is_main = (funcdef_flag
9508 && dname && identifier_p (dname)
9509 && MAIN_NAME_P (dname)
9510 && ctype == NULL_TREE
9511 && in_namespace == NULL_TREE
9512 && current_namespace == global_namespace);
9514 if (type_was_error_mark_node)
9515 /* We've already issued an error, don't complain more. */;
9516 else if (in_system_header_at (input_location) || flag_ms_extensions)
9517 /* Allow it, sigh. */;
9518 else if (! is_main)
9519 permerror (input_location, "ISO C++ forbids declaration of %qs with no type", name);
9520 else if (pedantic)
9521 pedwarn (input_location, OPT_Wpedantic,
9522 "ISO C++ forbids declaration of %qs with no type", name);
9523 else
9524 warning (OPT_Wreturn_type,
9525 "ISO C++ forbids declaration of %qs with no type", name);
9527 type = integer_type_node;
9530 ctype = NULL_TREE;
9532 if (explicit_intN)
9534 if (! int_n_enabled_p[declspecs->int_n_idx])
9536 error ("%<__int%d%> is not supported by this target",
9537 int_n_data[declspecs->int_n_idx].bitsize);
9538 explicit_intN = false;
9540 else if (pedantic && ! in_system_header_at (input_location))
9541 pedwarn (input_location, OPT_Wpedantic,
9542 "ISO C++ does not support %<__int%d%> for %qs",
9543 int_n_data[declspecs->int_n_idx].bitsize, name);
9546 /* Now process the modifiers that were specified
9547 and check for invalid combinations. */
9549 /* Long double is a special combination. */
9550 if (long_p && !longlong && TYPE_MAIN_VARIANT (type) == double_type_node)
9552 long_p = false;
9553 type = cp_build_qualified_type (long_double_type_node,
9554 cp_type_quals (type));
9557 /* Check all other uses of type modifiers. */
9559 if (unsigned_p || signed_p || long_p || short_p)
9561 int ok = 0;
9563 if ((signed_p || unsigned_p) && TREE_CODE (type) != INTEGER_TYPE)
9564 error ("%<signed%> or %<unsigned%> invalid for %qs", name);
9565 else if (signed_p && unsigned_p)
9566 error ("%<signed%> and %<unsigned%> specified together for %qs", name);
9567 else if (longlong && TREE_CODE (type) != INTEGER_TYPE)
9568 error ("%<long long%> invalid for %qs", name);
9569 else if (long_p && TREE_CODE (type) == REAL_TYPE)
9570 error ("%<long%> invalid for %qs", name);
9571 else if (short_p && TREE_CODE (type) == REAL_TYPE)
9572 error ("%<short%> invalid for %qs", name);
9573 else if ((long_p || short_p) && TREE_CODE (type) != INTEGER_TYPE)
9574 error ("%<long%> or %<short%> invalid for %qs", name);
9575 else if ((long_p || short_p || explicit_char || explicit_int) && explicit_intN)
9576 error ("%<long%>, %<int%>, %<short%>, or %<char%> invalid for %qs", name);
9577 else if ((long_p || short_p) && explicit_char)
9578 error ("%<long%> or %<short%> specified with char for %qs", name);
9579 else if (long_p && short_p)
9580 error ("%<long%> and %<short%> specified together for %qs", name);
9581 else if (type == char16_type_node || type == char32_type_node)
9583 if (signed_p || unsigned_p)
9584 error ("%<signed%> or %<unsigned%> invalid for %qs", name);
9585 else if (short_p || long_p)
9586 error ("%<short%> or %<long%> invalid for %qs", name);
9588 else
9590 ok = 1;
9591 if (!explicit_int && !defaulted_int && !explicit_char && !explicit_intN && pedantic)
9593 pedwarn (input_location, OPT_Wpedantic,
9594 "long, short, signed or unsigned used invalidly for %qs",
9595 name);
9596 if (flag_pedantic_errors)
9597 ok = 0;
9601 /* Discard the type modifiers if they are invalid. */
9602 if (! ok)
9604 unsigned_p = false;
9605 signed_p = false;
9606 long_p = false;
9607 short_p = false;
9608 longlong = 0;
9612 /* Decide whether an integer type is signed or not.
9613 Optionally treat bitfields as signed by default. */
9614 if (unsigned_p
9615 /* [class.bit]
9617 It is implementation-defined whether a plain (neither
9618 explicitly signed or unsigned) char, short, int, or long
9619 bit-field is signed or unsigned.
9621 Naturally, we extend this to long long as well. Note that
9622 this does not include wchar_t. */
9623 || (bitfield && !flag_signed_bitfields
9624 && !signed_p
9625 /* A typedef for plain `int' without `signed' can be
9626 controlled just like plain `int', but a typedef for
9627 `signed int' cannot be so controlled. */
9628 && !(typedef_decl
9629 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl))
9630 && TREE_CODE (type) == INTEGER_TYPE
9631 && !same_type_p (TYPE_MAIN_VARIANT (type), wchar_type_node)))
9633 if (explicit_intN)
9634 type = int_n_trees[declspecs->int_n_idx].unsigned_type;
9635 else if (longlong)
9636 type = long_long_unsigned_type_node;
9637 else if (long_p)
9638 type = long_unsigned_type_node;
9639 else if (short_p)
9640 type = short_unsigned_type_node;
9641 else if (type == char_type_node)
9642 type = unsigned_char_type_node;
9643 else if (typedef_decl)
9644 type = unsigned_type_for (type);
9645 else
9646 type = unsigned_type_node;
9648 else if (signed_p && type == char_type_node)
9649 type = signed_char_type_node;
9650 else if (explicit_intN)
9651 type = int_n_trees[declspecs->int_n_idx].signed_type;
9652 else if (longlong)
9653 type = long_long_integer_type_node;
9654 else if (long_p)
9655 type = long_integer_type_node;
9656 else if (short_p)
9657 type = short_integer_type_node;
9659 if (decl_spec_seq_has_spec_p (declspecs, ds_complex))
9661 if (TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
9662 error ("complex invalid for %qs", name);
9663 /* If a modifier is specified, the resulting complex is the complex
9664 form of TYPE. E.g, "complex short" is "complex short int". */
9665 else if (type == integer_type_node)
9666 type = complex_integer_type_node;
9667 else if (type == float_type_node)
9668 type = complex_float_type_node;
9669 else if (type == double_type_node)
9670 type = complex_double_type_node;
9671 else if (type == long_double_type_node)
9672 type = complex_long_double_type_node;
9673 else
9674 type = build_complex_type (type);
9677 type_quals = TYPE_UNQUALIFIED;
9678 if (decl_spec_seq_has_spec_p (declspecs, ds_const))
9679 type_quals |= TYPE_QUAL_CONST;
9680 if (decl_spec_seq_has_spec_p (declspecs, ds_volatile))
9681 type_quals |= TYPE_QUAL_VOLATILE;
9682 if (decl_spec_seq_has_spec_p (declspecs, ds_restrict))
9683 type_quals |= TYPE_QUAL_RESTRICT;
9684 if (sfk == sfk_conversion && type_quals != TYPE_UNQUALIFIED)
9685 error ("qualifiers are not allowed on declaration of %<operator %T%>",
9686 ctor_return_type);
9688 /* If we're using the injected-class-name to form a compound type or a
9689 declaration, replace it with the underlying class so we don't get
9690 redundant typedefs in the debug output. But if we are returning the
9691 type unchanged, leave it alone so that it's available to
9692 maybe_get_template_decl_from_type_decl. */
9693 if (CLASS_TYPE_P (type)
9694 && DECL_SELF_REFERENCE_P (TYPE_NAME (type))
9695 && type == TREE_TYPE (TYPE_NAME (type))
9696 && (declarator || type_quals))
9697 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
9699 type_quals |= cp_type_quals (type);
9700 type = cp_build_qualified_type_real
9701 (type, type_quals, ((((typedef_decl && !DECL_ARTIFICIAL (typedef_decl))
9702 || declspecs->decltype_p)
9703 ? tf_ignore_bad_quals : 0) | tf_warning_or_error));
9704 /* We might have ignored or rejected some of the qualifiers. */
9705 type_quals = cp_type_quals (type);
9707 staticp = 0;
9708 inlinep = decl_spec_seq_has_spec_p (declspecs, ds_inline);
9709 virtualp = decl_spec_seq_has_spec_p (declspecs, ds_virtual);
9710 explicitp = decl_spec_seq_has_spec_p (declspecs, ds_explicit);
9712 storage_class = declspecs->storage_class;
9713 if (storage_class == sc_static)
9714 staticp = 1 + (decl_context == FIELD);
9716 if (virtualp && staticp == 2)
9718 error ("member %qD cannot be declared both virtual and static", dname);
9719 storage_class = sc_none;
9720 staticp = 0;
9722 friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
9724 /* Issue errors about use of storage classes for parameters. */
9725 if (decl_context == PARM)
9727 if (typedef_p)
9729 error ("typedef declaration invalid in parameter declaration");
9730 return error_mark_node;
9732 else if (template_parm_flag && storage_class != sc_none)
9734 error ("storage class specified for template parameter %qs", name);
9735 return error_mark_node;
9737 else if (storage_class == sc_static
9738 || storage_class == sc_extern
9739 || thread_p)
9740 error ("storage class specifiers invalid in parameter declarations");
9742 /* Function parameters cannot be concept. */
9743 if (concept_p)
9744 error ("a parameter cannot be declared %<concept%>");
9745 /* Function parameters cannot be constexpr. If we saw one, moan
9746 and pretend it wasn't there. */
9747 else if (constexpr_p)
9749 error ("a parameter cannot be declared %<constexpr%>");
9750 constexpr_p = 0;
9754 /* Give error if `virtual' is used outside of class declaration. */
9755 if (virtualp
9756 && (current_class_name == NULL_TREE || decl_context != FIELD))
9758 error ("%<virtual%> outside class declaration");
9759 virtualp = 0;
9762 /* Static anonymous unions are dealt with here. */
9763 if (staticp && decl_context == TYPENAME
9764 && declspecs->type
9765 && ANON_AGGR_TYPE_P (declspecs->type))
9766 decl_context = FIELD;
9768 /* Warn about storage classes that are invalid for certain
9769 kinds of declarations (parameters, typenames, etc.). */
9770 if (thread_p
9771 && ((storage_class
9772 && storage_class != sc_extern
9773 && storage_class != sc_static)
9774 || typedef_p))
9776 error ("multiple storage classes in declaration of %qs", name);
9777 thread_p = false;
9779 if (decl_context != NORMAL
9780 && ((storage_class != sc_none
9781 && storage_class != sc_mutable)
9782 || thread_p))
9784 if ((decl_context == PARM || decl_context == CATCHPARM)
9785 && (storage_class == sc_register
9786 || storage_class == sc_auto))
9788 else if (typedef_p)
9790 else if (decl_context == FIELD
9791 /* C++ allows static class elements. */
9792 && storage_class == sc_static)
9793 /* C++ also allows inlines and signed and unsigned elements,
9794 but in those cases we don't come in here. */
9796 else
9798 if (decl_context == FIELD)
9799 error ("storage class specified for %qs", name);
9800 else
9802 if (decl_context == PARM || decl_context == CATCHPARM)
9803 error ("storage class specified for parameter %qs", name);
9804 else
9805 error ("storage class specified for typename");
9807 if (storage_class == sc_register
9808 || storage_class == sc_auto
9809 || storage_class == sc_extern
9810 || thread_p)
9811 storage_class = sc_none;
9814 else if (storage_class == sc_extern && funcdef_flag
9815 && ! toplevel_bindings_p ())
9816 error ("nested function %qs declared %<extern%>", name);
9817 else if (toplevel_bindings_p ())
9819 if (storage_class == sc_auto)
9820 error ("top-level declaration of %qs specifies %<auto%>", name);
9822 else if (thread_p
9823 && storage_class != sc_extern
9824 && storage_class != sc_static)
9826 if (declspecs->gnu_thread_keyword_p)
9827 pedwarn (input_location, 0, "function-scope %qs implicitly auto and "
9828 "declared %<__thread%>", name);
9830 /* When thread_local is applied to a variable of block scope the
9831 storage-class-specifier static is implied if it does not appear
9832 explicitly. */
9833 storage_class = declspecs->storage_class = sc_static;
9834 staticp = 1;
9837 if (storage_class && friendp)
9839 error ("storage class specifiers invalid in friend function declarations");
9840 storage_class = sc_none;
9841 staticp = 0;
9844 if (!id_declarator)
9845 unqualified_id = NULL_TREE;
9846 else
9848 unqualified_id = id_declarator->u.id.unqualified_name;
9849 switch (TREE_CODE (unqualified_id))
9851 case BIT_NOT_EXPR:
9852 unqualified_id = TREE_OPERAND (unqualified_id, 0);
9853 if (TYPE_P (unqualified_id))
9854 unqualified_id = constructor_name (unqualified_id);
9855 break;
9857 case IDENTIFIER_NODE:
9858 case TEMPLATE_ID_EXPR:
9859 break;
9861 default:
9862 gcc_unreachable ();
9866 if (declspecs->std_attributes)
9868 /* Apply the c++11 attributes to the type preceding them. */
9869 input_location = declspecs->locations[ds_std_attribute];
9870 decl_attributes (&type, declspecs->std_attributes, 0);
9871 input_location = saved_loc;
9874 /* Determine the type of the entity declared by recurring on the
9875 declarator. */
9876 for (; declarator; declarator = declarator->declarator)
9878 const cp_declarator *inner_declarator;
9879 tree attrs;
9881 if (type == error_mark_node)
9882 return error_mark_node;
9884 attrs = declarator->attributes;
9885 if (attrs)
9887 int attr_flags;
9889 attr_flags = 0;
9890 if (declarator == NULL || declarator->kind == cdk_id)
9891 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
9892 if (declarator->kind == cdk_function)
9893 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
9894 if (declarator->kind == cdk_array)
9895 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
9896 returned_attrs = decl_attributes (&type,
9897 chainon (returned_attrs, attrs),
9898 attr_flags);
9901 if (declarator->kind == cdk_id)
9902 break;
9904 inner_declarator = declarator->declarator;
9906 switch (declarator->kind)
9908 case cdk_array:
9909 type = create_array_type_for_decl (dname, type,
9910 declarator->u.array.bounds);
9911 if (declarator->std_attributes)
9912 /* [dcl.array]/1:
9914 The optional attribute-specifier-seq appertains to the
9915 array. */
9916 returned_attrs = chainon (returned_attrs,
9917 declarator->std_attributes);
9918 break;
9920 case cdk_function:
9922 tree arg_types;
9923 int funcdecl_p;
9925 /* Declaring a function type.
9926 Make sure we have a valid type for the function to return. */
9928 if (type_quals != TYPE_UNQUALIFIED)
9930 if (SCALAR_TYPE_P (type) || VOID_TYPE_P (type))
9931 warning (OPT_Wignored_qualifiers,
9932 "type qualifiers ignored on function return type");
9933 /* We now know that the TYPE_QUALS don't apply to the
9934 decl, but to its return type. */
9935 type_quals = TYPE_UNQUALIFIED;
9937 errmsg = targetm.invalid_return_type (type);
9938 if (errmsg)
9940 error (errmsg);
9941 type = integer_type_node;
9944 /* Error about some types functions can't return. */
9946 if (TREE_CODE (type) == FUNCTION_TYPE)
9948 error ("%qs declared as function returning a function", name);
9949 return error_mark_node;
9951 if (TREE_CODE (type) == ARRAY_TYPE)
9953 error ("%qs declared as function returning an array", name);
9954 return error_mark_node;
9957 input_location = declspecs->locations[ds_type_spec];
9958 abstract_virtuals_error (ACU_RETURN, type);
9959 input_location = saved_loc;
9961 /* Pick up type qualifiers which should be applied to `this'. */
9962 memfn_quals = declarator->u.function.qualifiers;
9963 /* Pick up virt-specifiers. */
9964 virt_specifiers = declarator->u.function.virt_specifiers;
9965 /* And ref-qualifier, too */
9966 rqual = declarator->u.function.ref_qualifier;
9967 /* Pick up the exception specifications. */
9968 raises = declarator->u.function.exception_specification;
9969 /* If the exception-specification is ill-formed, let's pretend
9970 there wasn't one. */
9971 if (raises == error_mark_node)
9972 raises = NULL_TREE;
9974 /* Say it's a definition only for the CALL_EXPR
9975 closest to the identifier. */
9976 funcdecl_p = inner_declarator && inner_declarator->kind == cdk_id;
9978 /* Handle a late-specified return type. */
9979 if (funcdecl_p)
9981 if (type_uses_auto (type))
9983 if (!declarator->u.function.late_return_type)
9985 if (current_class_type
9986 && LAMBDA_TYPE_P (current_class_type))
9987 /* OK for C++11 lambdas. */;
9988 else if (cxx_dialect < cxx14)
9990 error ("%qs function uses "
9991 "%<auto%> type specifier without trailing "
9992 "return type", name);
9993 inform (input_location, "deduced return type "
9994 "only available with -std=c++14 or "
9995 "-std=gnu++14");
9997 else if (virtualp)
9999 error ("virtual function cannot "
10000 "have deduced return type");
10001 virtualp = false;
10004 else if (!is_auto (type))
10006 error ("%qs function with trailing return type has"
10007 " %qT as its type rather than plain %<auto%>",
10008 name, type);
10009 return error_mark_node;
10012 else if (declarator->u.function.late_return_type)
10014 if (cxx_dialect < cxx11)
10015 /* Not using maybe_warn_cpp0x because this should
10016 always be an error. */
10017 error ("trailing return type only available with "
10018 "-std=c++11 or -std=gnu++11");
10019 else
10020 error ("%qs function with trailing return type not "
10021 "declared with %<auto%> type specifier", name);
10022 return error_mark_node;
10025 type = splice_late_return_type
10026 (type, declarator->u.function.late_return_type);
10027 if (type == error_mark_node)
10028 return error_mark_node;
10030 if (declarator->u.function.late_return_type)
10031 late_return_type_p = true;
10033 if (ctype == NULL_TREE
10034 && decl_context == FIELD
10035 && funcdecl_p
10036 && friendp == 0)
10037 ctype = current_class_type;
10039 if (ctype && (sfk == sfk_constructor
10040 || sfk == sfk_destructor))
10042 /* We are within a class's scope. If our declarator name
10043 is the same as the class name, and we are defining
10044 a function, then it is a constructor/destructor, and
10045 therefore returns a void type. */
10047 /* ISO C++ 12.4/2. A destructor may not be declared
10048 const or volatile. A destructor may not be static.
10049 A destructor may not be declared with ref-qualifier.
10051 ISO C++ 12.1. A constructor may not be declared
10052 const or volatile. A constructor may not be
10053 virtual. A constructor may not be static.
10054 A constructor may not be declared with ref-qualifier. */
10055 if (staticp == 2)
10056 error ((flags == DTOR_FLAG)
10057 ? G_("destructor cannot be static member function")
10058 : G_("constructor cannot be static member function"));
10059 if (memfn_quals)
10061 error ((flags == DTOR_FLAG)
10062 ? G_("destructors may not be cv-qualified")
10063 : G_("constructors may not be cv-qualified"));
10064 memfn_quals = TYPE_UNQUALIFIED;
10067 if (rqual)
10069 maybe_warn_cpp0x (CPP0X_REF_QUALIFIER);
10070 error ((flags == DTOR_FLAG)
10071 ? "destructors may not be ref-qualified"
10072 : "constructors may not be ref-qualified");
10073 rqual = REF_QUAL_NONE;
10076 if (decl_context == FIELD
10077 && !member_function_or_else (ctype,
10078 current_class_type,
10079 flags))
10080 return error_mark_node;
10082 if (flags != DTOR_FLAG)
10084 /* It's a constructor. */
10085 if (explicitp == 1)
10086 explicitp = 2;
10087 if (virtualp)
10089 permerror (input_location, "constructors cannot be declared virtual");
10090 virtualp = 0;
10092 if (decl_context == FIELD
10093 && sfk != sfk_constructor)
10094 return error_mark_node;
10096 if (decl_context == FIELD)
10097 staticp = 0;
10099 else if (friendp)
10101 if (virtualp)
10103 /* Cannot be both friend and virtual. */
10104 error ("virtual functions cannot be friends");
10105 friendp = 0;
10107 if (decl_context == NORMAL)
10108 error ("friend declaration not in class definition");
10109 if (current_function_decl && funcdef_flag)
10110 error ("can%'t define friend function %qs in a local "
10111 "class definition",
10112 name);
10114 else if (ctype && sfk == sfk_conversion)
10116 if (explicitp == 1)
10118 maybe_warn_cpp0x (CPP0X_EXPLICIT_CONVERSION);
10119 explicitp = 2;
10123 arg_types = grokparms (declarator->u.function.parameters,
10124 &parms);
10126 if (inner_declarator
10127 && inner_declarator->kind == cdk_id
10128 && inner_declarator->u.id.sfk == sfk_destructor
10129 && arg_types != void_list_node)
10131 error ("destructors may not have parameters");
10132 arg_types = void_list_node;
10133 parms = NULL_TREE;
10136 type = build_function_type (type, arg_types);
10137 if (declarator->std_attributes)
10138 /* [dcl.fct]/2:
10140 The optional attribute-specifier-seq appertains to
10141 the function type. */
10142 decl_attributes (&type, declarator->std_attributes,
10145 break;
10147 case cdk_pointer:
10148 case cdk_reference:
10149 case cdk_ptrmem:
10150 /* Filter out pointers-to-references and references-to-references.
10151 We can get these if a TYPE_DECL is used. */
10153 if (TREE_CODE (type) == REFERENCE_TYPE)
10155 if (declarator->kind != cdk_reference)
10157 error ("cannot declare pointer to %q#T", type);
10158 type = TREE_TYPE (type);
10161 /* In C++0x, we allow reference to reference declarations
10162 that occur indirectly through typedefs [7.1.3/8 dcl.typedef]
10163 and template type arguments [14.3.1/4 temp.arg.type]. The
10164 check for direct reference to reference declarations, which
10165 are still forbidden, occurs below. Reasoning behind the change
10166 can be found in DR106, DR540, and the rvalue reference
10167 proposals. */
10168 else if (cxx_dialect == cxx98)
10170 error ("cannot declare reference to %q#T", type);
10171 type = TREE_TYPE (type);
10174 else if (VOID_TYPE_P (type))
10176 if (declarator->kind == cdk_reference)
10177 error ("cannot declare reference to %q#T", type);
10178 else if (declarator->kind == cdk_ptrmem)
10179 error ("cannot declare pointer to %q#T member", type);
10182 /* We now know that the TYPE_QUALS don't apply to the decl,
10183 but to the target of the pointer. */
10184 type_quals = TYPE_UNQUALIFIED;
10186 /* This code used to handle METHOD_TYPE, but I don't think it's
10187 possible to get it here anymore. */
10188 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
10189 if (declarator->kind == cdk_ptrmem
10190 && TREE_CODE (type) == FUNCTION_TYPE)
10192 memfn_quals |= type_memfn_quals (type);
10193 type = build_memfn_type (type,
10194 declarator->u.pointer.class_type,
10195 memfn_quals,
10196 rqual);
10197 if (type == error_mark_node)
10198 return error_mark_node;
10200 rqual = REF_QUAL_NONE;
10201 memfn_quals = TYPE_UNQUALIFIED;
10204 if (TREE_CODE (type) == FUNCTION_TYPE
10205 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
10206 || type_memfn_rqual (type) != REF_QUAL_NONE))
10207 error (declarator->kind == cdk_reference
10208 ? G_("cannot declare reference to qualified function type %qT")
10209 : G_("cannot declare pointer to qualified function type %qT"),
10210 type);
10212 /* When the pointed-to type involves components of variable size,
10213 care must be taken to ensure that the size evaluation code is
10214 emitted early enough to dominate all the possible later uses
10215 and late enough for the variables on which it depends to have
10216 been assigned.
10218 This is expected to happen automatically when the pointed-to
10219 type has a name/declaration of it's own, but special attention
10220 is required if the type is anonymous.
10222 We handle the NORMAL and FIELD contexts here by inserting a
10223 dummy statement that just evaluates the size at a safe point
10224 and ensures it is not deferred until e.g. within a deeper
10225 conditional context (c++/43555).
10227 We expect nothing to be needed here for PARM or TYPENAME.
10228 Evaluating the size at this point for TYPENAME would
10229 actually be incorrect, as we might be in the middle of an
10230 expression with side effects on the pointed-to type size
10231 "arguments" prior to the pointer declaration point and the
10232 size evaluation could end up prior to the side effects. */
10234 if (!TYPE_NAME (type)
10235 && (decl_context == NORMAL || decl_context == FIELD)
10236 && at_function_scope_p ()
10237 && variably_modified_type_p (type, NULL_TREE))
10238 /* Force evaluation of the SAVE_EXPR. */
10239 finish_expr_stmt (TYPE_SIZE (type));
10241 if (declarator->kind == cdk_reference)
10243 /* In C++0x, the type we are creating a reference to might be
10244 a typedef which is itself a reference type. In that case,
10245 we follow the reference collapsing rules in
10246 [7.1.3/8 dcl.typedef] to create the final reference type:
10248 "If a typedef TD names a type that is a reference to a type
10249 T, an attempt to create the type 'lvalue reference to cv TD'
10250 creates the type 'lvalue reference to T,' while an attempt
10251 to create the type "rvalue reference to cv TD' creates the
10252 type TD."
10254 if (VOID_TYPE_P (type))
10255 /* We already gave an error. */;
10256 else if (TREE_CODE (type) == REFERENCE_TYPE)
10258 if (declarator->u.reference.rvalue_ref)
10259 /* Leave type alone. */;
10260 else
10261 type = cp_build_reference_type (TREE_TYPE (type), false);
10263 else
10264 type = cp_build_reference_type
10265 (type, declarator->u.reference.rvalue_ref);
10267 /* In C++0x, we need this check for direct reference to
10268 reference declarations, which are forbidden by
10269 [8.3.2/5 dcl.ref]. Reference to reference declarations
10270 are only allowed indirectly through typedefs and template
10271 type arguments. Example:
10273 void foo(int & &); // invalid ref-to-ref decl
10275 typedef int & int_ref;
10276 void foo(int_ref &); // valid ref-to-ref decl
10278 if (inner_declarator && inner_declarator->kind == cdk_reference)
10279 error ("cannot declare reference to %q#T, which is not "
10280 "a typedef or a template type argument", type);
10282 else if (TREE_CODE (type) == METHOD_TYPE)
10283 type = build_ptrmemfunc_type (build_pointer_type (type));
10284 else if (declarator->kind == cdk_ptrmem)
10286 gcc_assert (TREE_CODE (declarator->u.pointer.class_type)
10287 != NAMESPACE_DECL);
10288 if (declarator->u.pointer.class_type == error_mark_node)
10289 /* We will already have complained. */
10290 type = error_mark_node;
10291 else
10292 type = build_ptrmem_type (declarator->u.pointer.class_type,
10293 type);
10295 else
10296 type = build_pointer_type (type);
10298 /* Process a list of type modifier keywords (such as
10299 const or volatile) that were given inside the `*' or `&'. */
10301 if (declarator->u.pointer.qualifiers)
10303 type
10304 = cp_build_qualified_type (type,
10305 declarator->u.pointer.qualifiers);
10306 type_quals = cp_type_quals (type);
10309 /* Apply C++11 attributes to the pointer, and not to the
10310 type pointed to. This is unlike what is done for GNU
10311 attributes above. It is to comply with [dcl.ptr]/1:
10313 [the optional attribute-specifier-seq (7.6.1) appertains
10314 to the pointer and not to the object pointed to]. */
10315 if (declarator->std_attributes)
10316 decl_attributes (&type, declarator->std_attributes,
10319 ctype = NULL_TREE;
10320 break;
10322 case cdk_error:
10323 break;
10325 default:
10326 gcc_unreachable ();
10330 /* A `constexpr' specifier used in an object declaration declares
10331 the object as `const'. */
10332 if (constexpr_p && innermost_code != cdk_function)
10334 /* DR1688 says that a `constexpr' specifier in combination with
10335 `volatile' is valid. */
10337 if (TREE_CODE (type) != REFERENCE_TYPE)
10339 type_quals |= TYPE_QUAL_CONST;
10340 type = cp_build_qualified_type (type, type_quals);
10344 if (unqualified_id && TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR
10345 && TREE_CODE (type) != FUNCTION_TYPE
10346 && TREE_CODE (type) != METHOD_TYPE
10347 && !variable_template_p (TREE_OPERAND (unqualified_id, 0)))
10349 error ("template-id %qD used as a declarator",
10350 unqualified_id);
10351 unqualified_id = dname;
10354 /* If TYPE is a FUNCTION_TYPE, but the function name was explicitly
10355 qualified with a class-name, turn it into a METHOD_TYPE, unless
10356 we know that the function is static. We take advantage of this
10357 opportunity to do other processing that pertains to entities
10358 explicitly declared to be class members. Note that if DECLARATOR
10359 is non-NULL, we know it is a cdk_id declarator; otherwise, we
10360 would not have exited the loop above. */
10361 if (declarator
10362 && declarator->u.id.qualifying_scope
10363 && MAYBE_CLASS_TYPE_P (declarator->u.id.qualifying_scope))
10365 ctype = declarator->u.id.qualifying_scope;
10366 ctype = TYPE_MAIN_VARIANT (ctype);
10367 template_count = num_template_headers_for_class (ctype);
10369 if (ctype == current_class_type)
10371 if (friendp)
10373 permerror (input_location, "member functions are implicitly friends of their class");
10374 friendp = 0;
10376 else
10377 permerror (declarator->id_loc,
10378 "extra qualification %<%T::%> on member %qs",
10379 ctype, name);
10381 else if (/* If the qualifying type is already complete, then we
10382 can skip the following checks. */
10383 !COMPLETE_TYPE_P (ctype)
10384 && (/* If the function is being defined, then
10385 qualifying type must certainly be complete. */
10386 funcdef_flag
10387 /* A friend declaration of "T::f" is OK, even if
10388 "T" is a template parameter. But, if this
10389 function is not a friend, the qualifying type
10390 must be a class. */
10391 || (!friendp && !CLASS_TYPE_P (ctype))
10392 /* For a declaration, the type need not be
10393 complete, if either it is dependent (since there
10394 is no meaningful definition of complete in that
10395 case) or the qualifying class is currently being
10396 defined. */
10397 || !(dependent_type_p (ctype)
10398 || currently_open_class (ctype)))
10399 /* Check that the qualifying type is complete. */
10400 && !complete_type_or_else (ctype, NULL_TREE))
10401 return error_mark_node;
10402 else if (TREE_CODE (type) == FUNCTION_TYPE)
10404 if (current_class_type
10405 && (!friendp || funcdef_flag))
10407 error (funcdef_flag
10408 ? G_("cannot define member function %<%T::%s%> "
10409 "within %<%T%>")
10410 : G_("cannot declare member function %<%T::%s%> "
10411 "within %<%T%>"),
10412 ctype, name, current_class_type);
10413 return error_mark_node;
10416 else if (typedef_p && current_class_type)
10418 error ("cannot declare member %<%T::%s%> within %qT",
10419 ctype, name, current_class_type);
10420 return error_mark_node;
10424 if (ctype == NULL_TREE && decl_context == FIELD && friendp == 0)
10425 ctype = current_class_type;
10427 /* Now TYPE has the actual type. */
10429 if (returned_attrs)
10431 if (attrlist)
10432 *attrlist = chainon (returned_attrs, *attrlist);
10433 else
10434 attrlist = &returned_attrs;
10437 if (declarator
10438 && declarator->kind == cdk_id
10439 && declarator->std_attributes)
10440 /* [dcl.meaning]/1: The optional attribute-specifier-seq following
10441 a declarator-id appertains to the entity that is declared. */
10442 *attrlist = chainon (*attrlist, declarator->std_attributes);
10444 /* Handle parameter packs. */
10445 if (parameter_pack_p)
10447 if (decl_context == PARM)
10448 /* Turn the type into a pack expansion.*/
10449 type = make_pack_expansion (type);
10450 else
10451 error ("non-parameter %qs cannot be a parameter pack", name);
10454 /* Did array size calculations overflow or does the array cover more
10455 than half of the address-space? */
10456 if (TREE_CODE (type) == ARRAY_TYPE
10457 && COMPLETE_TYPE_P (type)
10458 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
10459 && ! valid_constant_size_p (TYPE_SIZE_UNIT (type)))
10461 error ("size of array %qs is too large", name);
10462 /* If we proceed with the array type as it is, we'll eventually
10463 crash in tree_to_[su]hwi(). */
10464 type = error_mark_node;
10467 if ((decl_context == FIELD || decl_context == PARM)
10468 && !processing_template_decl
10469 && variably_modified_type_p (type, NULL_TREE))
10471 if (decl_context == FIELD)
10472 error ("data member may not have variably modified type %qT", type);
10473 else
10474 error ("parameter may not have variably modified type %qT", type);
10475 type = error_mark_node;
10478 if (explicitp == 1 || (explicitp && friendp))
10480 /* [dcl.fct.spec] (C++11) The explicit specifier shall be used only
10481 in the declaration of a constructor or conversion function within
10482 a class definition. */
10483 if (!current_class_type)
10484 error ("%<explicit%> outside class declaration");
10485 else if (friendp)
10486 error ("%<explicit%> in friend declaration");
10487 else
10488 error ("only declarations of constructors and conversion operators "
10489 "can be %<explicit%>");
10490 explicitp = 0;
10493 if (storage_class == sc_mutable)
10495 if (decl_context != FIELD || friendp)
10497 error ("non-member %qs cannot be declared %<mutable%>", name);
10498 storage_class = sc_none;
10500 else if (decl_context == TYPENAME || typedef_p)
10502 error ("non-object member %qs cannot be declared %<mutable%>", name);
10503 storage_class = sc_none;
10505 else if (TREE_CODE (type) == FUNCTION_TYPE
10506 || TREE_CODE (type) == METHOD_TYPE)
10508 error ("function %qs cannot be declared %<mutable%>", name);
10509 storage_class = sc_none;
10511 else if (staticp)
10513 error ("static %qs cannot be declared %<mutable%>", name);
10514 storage_class = sc_none;
10516 else if (type_quals & TYPE_QUAL_CONST)
10518 error ("const %qs cannot be declared %<mutable%>", name);
10519 storage_class = sc_none;
10521 else if (TREE_CODE (type) == REFERENCE_TYPE)
10523 permerror (input_location, "reference %qs cannot be declared "
10524 "%<mutable%>", name);
10525 storage_class = sc_none;
10529 /* If this is declaring a typedef name, return a TYPE_DECL. */
10530 if (typedef_p && decl_context != TYPENAME)
10532 tree decl;
10534 /* Note that the grammar rejects storage classes
10535 in typenames, fields or parameters. */
10536 if (current_lang_name == lang_name_java)
10537 TYPE_FOR_JAVA (type) = 1;
10539 /* This declaration:
10541 typedef void f(int) const;
10543 declares a function type which is not a member of any
10544 particular class, but which is cv-qualified; for
10545 example "f S::*" declares a pointer to a const-qualified
10546 member function of S. We record the cv-qualification in the
10547 function type. */
10548 if ((rqual || memfn_quals) && TREE_CODE (type) == FUNCTION_TYPE)
10550 type = apply_memfn_quals (type, memfn_quals, rqual);
10552 /* We have now dealt with these qualifiers. */
10553 memfn_quals = TYPE_UNQUALIFIED;
10554 rqual = REF_QUAL_NONE;
10557 if (type_uses_auto (type))
10559 error ("typedef declared %<auto%>");
10560 type = error_mark_node;
10563 if (decl_context == FIELD)
10564 decl = build_lang_decl (TYPE_DECL, unqualified_id, type);
10565 else
10566 decl = build_decl (input_location, TYPE_DECL, unqualified_id, type);
10567 if (id_declarator && declarator->u.id.qualifying_scope) {
10568 error_at (DECL_SOURCE_LOCATION (decl),
10569 "typedef name may not be a nested-name-specifier");
10570 TREE_TYPE (decl) = error_mark_node;
10573 if (decl_context != FIELD)
10575 if (!current_function_decl)
10576 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
10577 else if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (current_function_decl)
10578 || (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P
10579 (current_function_decl)))
10580 /* The TYPE_DECL is "abstract" because there will be
10581 clones of this constructor/destructor, and there will
10582 be copies of this TYPE_DECL generated in those
10583 clones. The decloning optimization (for space) may
10584 revert this subsequently if it determines that
10585 the clones should share a common implementation. */
10586 DECL_ABSTRACT_P (decl) = true;
10588 else if (current_class_type
10589 && constructor_name_p (unqualified_id, current_class_type))
10590 permerror (input_location, "ISO C++ forbids nested type %qD with same name "
10591 "as enclosing class",
10592 unqualified_id);
10594 /* If the user declares "typedef struct {...} foo" then the
10595 struct will have an anonymous name. Fill that name in now.
10596 Nothing can refer to it, so nothing needs know about the name
10597 change. */
10598 if (type != error_mark_node
10599 && unqualified_id
10600 && TYPE_NAME (type)
10601 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10602 && TYPE_ANONYMOUS_P (type)
10603 && declspecs->type_definition_p
10604 && attributes_naming_typedef_ok (*attrlist)
10605 && cp_type_quals (type) == TYPE_UNQUALIFIED)
10607 tree t;
10609 /* Replace the anonymous name with the real name everywhere. */
10610 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
10612 if (ANON_AGGRNAME_P (TYPE_IDENTIFIER (t)))
10613 /* We do not rename the debug info representing the
10614 anonymous tagged type because the standard says in
10615 [dcl.typedef] that the naming applies only for
10616 linkage purposes. */
10617 /*debug_hooks->set_name (t, decl);*/
10618 TYPE_NAME (t) = decl;
10621 if (TYPE_LANG_SPECIFIC (type))
10622 TYPE_WAS_ANONYMOUS (type) = 1;
10624 /* If this is a typedef within a template class, the nested
10625 type is a (non-primary) template. The name for the
10626 template needs updating as well. */
10627 if (TYPE_LANG_SPECIFIC (type) && CLASSTYPE_TEMPLATE_INFO (type))
10628 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type))
10629 = TYPE_IDENTIFIER (type);
10631 /* Adjust linkage now that we aren't anonymous anymore. */
10632 reset_type_linkage (type);
10634 /* FIXME remangle member functions; member functions of a
10635 type with external linkage have external linkage. */
10638 if (signed_p
10639 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
10640 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
10642 bad_specifiers (decl, BSP_TYPE, virtualp,
10643 memfn_quals != TYPE_UNQUALIFIED,
10644 inlinep, friendp, raises != NULL_TREE);
10646 if (decl_spec_seq_has_spec_p (declspecs, ds_alias))
10647 /* Acknowledge that this was written:
10648 `using analias = atype;'. */
10649 TYPE_DECL_ALIAS_P (decl) = 1;
10651 return decl;
10654 /* Detect the case of an array type of unspecified size
10655 which came, as such, direct from a typedef name.
10656 We must copy the type, so that the array's domain can be
10657 individually set by the object's initializer. */
10659 if (type && typedef_type
10660 && TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
10661 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
10662 type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
10664 /* Detect where we're using a typedef of function type to declare a
10665 function. PARMS will not be set, so we must create it now. */
10667 if (type == typedef_type && TREE_CODE (type) == FUNCTION_TYPE)
10669 tree decls = NULL_TREE;
10670 tree args;
10672 for (args = TYPE_ARG_TYPES (type);
10673 args && args != void_list_node;
10674 args = TREE_CHAIN (args))
10676 tree decl = cp_build_parm_decl (NULL_TREE, TREE_VALUE (args));
10678 DECL_CHAIN (decl) = decls;
10679 decls = decl;
10682 parms = nreverse (decls);
10684 if (decl_context != TYPENAME)
10686 /* The qualifiers on the function type become the qualifiers on
10687 the non-static member function. */
10688 memfn_quals |= type_memfn_quals (type);
10689 rqual = type_memfn_rqual (type);
10690 type_quals = TYPE_UNQUALIFIED;
10694 /* If this is a type name (such as, in a cast or sizeof),
10695 compute the type and return it now. */
10697 if (decl_context == TYPENAME)
10699 /* Note that the grammar rejects storage classes
10700 in typenames, fields or parameters. */
10701 if (type_quals != TYPE_UNQUALIFIED)
10702 type_quals = TYPE_UNQUALIFIED;
10704 /* Special case: "friend class foo" looks like a TYPENAME context. */
10705 if (friendp)
10707 if (type_quals != TYPE_UNQUALIFIED)
10709 error ("type qualifiers specified for friend class declaration");
10710 type_quals = TYPE_UNQUALIFIED;
10712 if (inlinep)
10714 error ("%<inline%> specified for friend class declaration");
10715 inlinep = 0;
10718 if (!current_aggr)
10720 /* Don't allow friend declaration without a class-key. */
10721 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
10722 permerror (input_location, "template parameters cannot be friends");
10723 else if (TREE_CODE (type) == TYPENAME_TYPE)
10724 permerror (input_location, "friend declaration requires class-key, "
10725 "i.e. %<friend class %T::%D%>",
10726 TYPE_CONTEXT (type), TYPENAME_TYPE_FULLNAME (type));
10727 else
10728 permerror (input_location, "friend declaration requires class-key, "
10729 "i.e. %<friend %#T%>",
10730 type);
10733 /* Only try to do this stuff if we didn't already give up. */
10734 if (type != integer_type_node)
10736 /* A friendly class? */
10737 if (current_class_type)
10738 make_friend_class (current_class_type, TYPE_MAIN_VARIANT (type),
10739 /*complain=*/true);
10740 else
10741 error ("trying to make class %qT a friend of global scope",
10742 type);
10744 type = void_type_node;
10747 else if (memfn_quals || rqual)
10749 if (ctype == NULL_TREE
10750 && TREE_CODE (type) == METHOD_TYPE)
10751 ctype = TYPE_METHOD_BASETYPE (type);
10753 if (ctype)
10754 type = build_memfn_type (type, ctype, memfn_quals, rqual);
10755 /* Core issue #547: need to allow this in template type args.
10756 Allow it in general in C++11 for alias-declarations. */
10757 else if ((template_type_arg || cxx_dialect >= cxx11)
10758 && TREE_CODE (type) == FUNCTION_TYPE)
10759 type = apply_memfn_quals (type, memfn_quals, rqual);
10760 else
10761 error ("invalid qualifiers on non-member function type");
10764 return type;
10766 else if (unqualified_id == NULL_TREE && decl_context != PARM
10767 && decl_context != CATCHPARM
10768 && TREE_CODE (type) != UNION_TYPE
10769 && ! bitfield)
10771 error ("abstract declarator %qT used as declaration", type);
10772 return error_mark_node;
10775 /* Only functions may be declared using an operator-function-id. */
10776 if (unqualified_id
10777 && IDENTIFIER_OPNAME_P (unqualified_id)
10778 && TREE_CODE (type) != FUNCTION_TYPE
10779 && TREE_CODE (type) != METHOD_TYPE)
10781 error ("declaration of %qD as non-function", unqualified_id);
10782 return error_mark_node;
10785 /* We don't check parameter types here because we can emit a better
10786 error message later. */
10787 if (decl_context != PARM)
10789 type = check_var_type (unqualified_id, type);
10790 if (type == error_mark_node)
10791 return error_mark_node;
10794 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
10795 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
10797 if (decl_context == PARM || decl_context == CATCHPARM)
10799 if (ctype || in_namespace)
10800 error ("cannot use %<::%> in parameter declaration");
10802 if (type_uses_auto (type))
10804 if (cxx_dialect >= cxx14)
10805 error ("%<auto%> parameter not permitted in this context");
10806 else
10807 error ("parameter declared %<auto%>");
10808 type = error_mark_node;
10811 /* A parameter declared as an array of T is really a pointer to T.
10812 One declared as a function is really a pointer to a function.
10813 One declared as a member is really a pointer to member. */
10815 if (TREE_CODE (type) == ARRAY_TYPE)
10817 /* Transfer const-ness of array into that of type pointed to. */
10818 type = build_pointer_type (TREE_TYPE (type));
10819 type_quals = TYPE_UNQUALIFIED;
10820 array_parameter_p = true;
10822 else if (TREE_CODE (type) == FUNCTION_TYPE)
10823 type = build_pointer_type (type);
10826 if (ctype && TREE_CODE (type) == FUNCTION_TYPE && staticp < 2
10827 && !NEW_DELETE_OPNAME_P (unqualified_id))
10829 cp_cv_quals real_quals = memfn_quals;
10830 if (cxx_dialect < cxx14 && constexpr_p
10831 && sfk != sfk_constructor && sfk != sfk_destructor)
10832 real_quals |= TYPE_QUAL_CONST;
10833 type = build_memfn_type (type, ctype, real_quals, rqual);
10837 tree decl;
10839 if (decl_context == PARM)
10841 decl = cp_build_parm_decl (unqualified_id, type);
10842 DECL_ARRAY_PARAMETER_P (decl) = array_parameter_p;
10844 bad_specifiers (decl, BSP_PARM, virtualp,
10845 memfn_quals != TYPE_UNQUALIFIED,
10846 inlinep, friendp, raises != NULL_TREE);
10848 else if (decl_context == FIELD)
10850 if (!staticp && TREE_CODE (type) != METHOD_TYPE
10851 && type_uses_auto (type))
10853 error ("non-static data member declared %<auto%>");
10854 type = error_mark_node;
10857 /* The C99 flexible array extension. */
10858 if (!staticp && TREE_CODE (type) == ARRAY_TYPE
10859 && TYPE_DOMAIN (type) == NULL_TREE)
10861 tree itype = compute_array_index_type (dname, integer_zero_node,
10862 tf_warning_or_error);
10863 type = build_cplus_array_type (TREE_TYPE (type), itype);
10866 if (type == error_mark_node)
10868 /* Happens when declaring arrays of sizes which
10869 are error_mark_node, for example. */
10870 decl = NULL_TREE;
10872 else if (in_namespace && !friendp)
10874 /* Something like struct S { int N::j; }; */
10875 error ("invalid use of %<::%>");
10876 return error_mark_node;
10878 else if (TREE_CODE (type) == FUNCTION_TYPE
10879 || TREE_CODE (type) == METHOD_TYPE)
10881 int publicp = 0;
10882 tree function_context;
10884 if (friendp == 0)
10886 /* This should never happen in pure C++ (the check
10887 could be an assert). It could happen in
10888 Objective-C++ if someone writes invalid code that
10889 uses a function declaration for an instance
10890 variable or property (instance variables and
10891 properties are parsed as FIELD_DECLs, but they are
10892 part of an Objective-C class, not a C++ class).
10893 That code is invalid and is caught by this
10894 check. */
10895 if (!ctype)
10897 error ("declaration of function %qD in invalid context",
10898 unqualified_id);
10899 return error_mark_node;
10902 /* ``A union may [ ... ] not [ have ] virtual functions.''
10903 ARM 9.5 */
10904 if (virtualp && TREE_CODE (ctype) == UNION_TYPE)
10906 error ("function %qD declared virtual inside a union",
10907 unqualified_id);
10908 return error_mark_node;
10911 if (NEW_DELETE_OPNAME_P (unqualified_id))
10913 if (virtualp)
10915 error ("%qD cannot be declared virtual, since it "
10916 "is always static",
10917 unqualified_id);
10918 virtualp = 0;
10923 /* Check that the name used for a destructor makes sense. */
10924 if (sfk == sfk_destructor)
10926 tree uqname = id_declarator->u.id.unqualified_name;
10928 if (!ctype)
10930 gcc_assert (friendp);
10931 error ("expected qualified name in friend declaration "
10932 "for destructor %qD", uqname);
10933 return error_mark_node;
10936 if (!check_dtor_name (ctype, TREE_OPERAND (uqname, 0)))
10938 error ("declaration of %qD as member of %qT",
10939 uqname, ctype);
10940 return error_mark_node;
10942 if (concept_p)
10944 error ("a destructor cannot be %<concept%>");
10945 return error_mark_node;
10947 if (constexpr_p)
10949 error ("a destructor cannot be %<constexpr%>");
10950 return error_mark_node;
10953 else if (sfk == sfk_constructor && friendp && !ctype)
10955 error ("expected qualified name in friend declaration "
10956 "for constructor %qD",
10957 id_declarator->u.id.unqualified_name);
10958 return error_mark_node;
10960 if (sfk == sfk_constructor)
10961 if (concept_p)
10963 error ("a constructor cannot be %<concept%>");
10964 return error_mark_node;
10966 if (staticp && concept_p)
10967 error ("a concept cannot be a static member function");
10969 if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
10971 tree tmpl = TREE_OPERAND (unqualified_id, 0);
10972 if (variable_template_p (tmpl))
10974 error ("specialization of variable template %qD "
10975 "declared as function", tmpl);
10976 inform (DECL_SOURCE_LOCATION (tmpl),
10977 "variable template declared here");
10978 return error_mark_node;
10982 /* Tell grokfndecl if it needs to set TREE_PUBLIC on the node. */
10983 function_context = (ctype != NULL_TREE) ?
10984 decl_function_context (TYPE_MAIN_DECL (ctype)) : NULL_TREE;
10985 publicp = (! friendp || ! staticp)
10986 && function_context == NULL_TREE;
10988 if (late_return_type_p)
10989 TYPE_HAS_LATE_RETURN_TYPE (type) = 1;
10991 decl = grokfndecl (ctype, type,
10992 TREE_CODE (unqualified_id) != TEMPLATE_ID_EXPR
10993 ? unqualified_id : dname,
10994 parms,
10995 unqualified_id,
10996 reqs,
10997 virtualp, flags, memfn_quals, rqual, raises,
10998 friendp ? -1 : 0, friendp, publicp,
10999 inlinep | (2 * constexpr_p) | (4 * concept_p),
11000 initialized == SD_DELETED, sfk,
11001 funcdef_flag, template_count, in_namespace,
11002 attrlist, declarator->id_loc);
11003 decl = set_virt_specifiers (decl, virt_specifiers);
11004 if (decl == NULL_TREE)
11005 return error_mark_node;
11006 #if 0
11007 /* This clobbers the attrs stored in `decl' from `attrlist'. */
11008 /* The decl and setting of decl_attr is also turned off. */
11009 decl = build_decl_attribute_variant (decl, decl_attr);
11010 #endif
11012 /* [class.conv.ctor]
11014 A constructor declared without the function-specifier
11015 explicit that can be called with a single parameter
11016 specifies a conversion from the type of its first
11017 parameter to the type of its class. Such a constructor
11018 is called a converting constructor. */
11019 if (explicitp == 2)
11020 DECL_NONCONVERTING_P (decl) = 1;
11022 else if (!staticp && !dependent_type_p (type)
11023 && !COMPLETE_TYPE_P (complete_type (type))
11024 && (TREE_CODE (type) != ARRAY_TYPE
11025 || !COMPLETE_TYPE_P (TREE_TYPE (type))
11026 || initialized == 0))
11028 if (unqualified_id)
11030 error ("field %qD has incomplete type %qT",
11031 unqualified_id, type);
11032 cxx_incomplete_type_inform (strip_array_types (type));
11034 else
11035 error ("name %qT has incomplete type", type);
11037 type = error_mark_node;
11038 decl = NULL_TREE;
11040 else
11042 if (friendp)
11044 error ("%qE is neither function nor member function; "
11045 "cannot be declared friend", unqualified_id);
11046 friendp = 0;
11048 decl = NULL_TREE;
11051 if (friendp)
11053 /* Friends are treated specially. */
11054 if (ctype == current_class_type)
11055 ; /* We already issued a permerror. */
11056 else if (decl && DECL_NAME (decl))
11058 if (template_class_depth (current_class_type) == 0)
11060 decl = check_explicit_specialization
11061 (unqualified_id, decl, template_count,
11062 2 * funcdef_flag + 4);
11063 if (decl == error_mark_node)
11064 return error_mark_node;
11067 decl = do_friend (ctype, unqualified_id, decl,
11068 *attrlist, flags,
11069 funcdef_flag);
11070 return decl;
11072 else
11073 return error_mark_node;
11076 /* Structure field. It may not be a function, except for C++. */
11078 if (decl == NULL_TREE)
11080 if (staticp)
11082 /* C++ allows static class members. All other work
11083 for this is done by grokfield. */
11084 decl = build_lang_decl_loc (declarator
11085 ? declarator->id_loc
11086 : input_location,
11087 VAR_DECL, unqualified_id, type);
11088 set_linkage_for_static_data_member (decl);
11089 /* Even if there is an in-class initialization, DECL
11090 is considered undefined until an out-of-class
11091 definition is provided. */
11092 DECL_EXTERNAL (decl) = 1;
11094 if (thread_p)
11096 set_decl_tls_model (decl, decl_default_tls_model (decl));
11097 if (declspecs->gnu_thread_keyword_p)
11098 DECL_GNU_TLS_P (decl) = true;
11100 if (concept_p)
11101 error ("static data member %qE declared %<concept%>",
11102 unqualified_id);
11103 else if (constexpr_p && !initialized)
11105 error ("constexpr static data member %qD must have an "
11106 "initializer", decl);
11107 constexpr_p = false;
11110 else
11112 if (concept_p)
11113 error ("non-static data member %qE declared %<concept%>",
11114 unqualified_id);
11115 else if (constexpr_p)
11117 error ("non-static data member %qE declared %<constexpr%>",
11118 unqualified_id);
11119 constexpr_p = false;
11121 decl = build_decl (input_location,
11122 FIELD_DECL, unqualified_id, type);
11123 DECL_NONADDRESSABLE_P (decl) = bitfield;
11124 if (bitfield && !unqualified_id)
11125 TREE_NO_WARNING (decl) = 1;
11127 if (storage_class == sc_mutable)
11129 DECL_MUTABLE_P (decl) = 1;
11130 storage_class = sc_none;
11133 if (initialized)
11135 /* An attempt is being made to initialize a non-static
11136 member. This is new in C++11. */
11137 maybe_warn_cpp0x (CPP0X_NSDMI);
11139 /* If this has been parsed with static storage class, but
11140 errors forced staticp to be cleared, ensure NSDMI is
11141 not present. */
11142 if (declspecs->storage_class == sc_static)
11143 DECL_INITIAL (decl) = error_mark_node;
11147 bad_specifiers (decl, BSP_FIELD, virtualp,
11148 memfn_quals != TYPE_UNQUALIFIED,
11149 inlinep, friendp, raises != NULL_TREE);
11152 else if (TREE_CODE (type) == FUNCTION_TYPE
11153 || TREE_CODE (type) == METHOD_TYPE)
11155 tree original_name;
11156 int publicp = 0;
11158 if (!unqualified_id)
11159 return error_mark_node;
11161 if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
11162 original_name = dname;
11163 else
11164 original_name = unqualified_id;
11166 if (storage_class == sc_auto)
11167 error ("storage class %<auto%> invalid for function %qs", name);
11168 else if (storage_class == sc_register)
11169 error ("storage class %<register%> invalid for function %qs", name);
11170 else if (thread_p)
11172 if (declspecs->gnu_thread_keyword_p)
11173 error ("storage class %<__thread%> invalid for function %qs",
11174 name);
11175 else
11176 error ("storage class %<thread_local%> invalid for function %qs",
11177 name);
11180 if (virt_specifiers)
11181 error ("virt-specifiers in %qs not allowed outside a class definition", name);
11182 /* Function declaration not at top level.
11183 Storage classes other than `extern' are not allowed
11184 and `extern' makes no difference. */
11185 if (! toplevel_bindings_p ()
11186 && (storage_class == sc_static
11187 || decl_spec_seq_has_spec_p (declspecs, ds_inline))
11188 && pedantic)
11190 if (storage_class == sc_static)
11191 pedwarn (input_location, OPT_Wpedantic,
11192 "%<static%> specified invalid for function %qs "
11193 "declared out of global scope", name);
11194 else
11195 pedwarn (input_location, OPT_Wpedantic,
11196 "%<inline%> specifier invalid for function %qs "
11197 "declared out of global scope", name);
11200 if (ctype == NULL_TREE)
11202 if (virtualp)
11204 error ("virtual non-class function %qs", name);
11205 virtualp = 0;
11207 else if (sfk == sfk_constructor
11208 || sfk == sfk_destructor)
11210 error (funcdef_flag
11211 ? G_("%qs defined in a non-class scope")
11212 : G_("%qs declared in a non-class scope"), name);
11213 sfk = sfk_none;
11217 /* Record whether the function is public. */
11218 publicp = (ctype != NULL_TREE
11219 || storage_class != sc_static);
11221 if (late_return_type_p)
11222 TYPE_HAS_LATE_RETURN_TYPE (type) = 1;
11224 decl = grokfndecl (ctype, type, original_name, parms, unqualified_id,
11225 reqs, virtualp, flags, memfn_quals, rqual, raises,
11226 1, friendp,
11227 publicp,
11228 inlinep | (2 * constexpr_p) | (4 * concept_p),
11229 initialized == SD_DELETED,
11230 sfk,
11231 funcdef_flag,
11232 template_count, in_namespace, attrlist,
11233 declarator->id_loc);
11234 if (decl == NULL_TREE)
11235 return error_mark_node;
11237 if (staticp == 1)
11239 int invalid_static = 0;
11241 /* Don't allow a static member function in a class, and forbid
11242 declaring main to be static. */
11243 if (TREE_CODE (type) == METHOD_TYPE)
11245 permerror (input_location, "cannot declare member function %qD to have "
11246 "static linkage", decl);
11247 invalid_static = 1;
11249 else if (current_function_decl)
11251 /* FIXME need arm citation */
11252 error ("cannot declare static function inside another function");
11253 invalid_static = 1;
11256 if (invalid_static)
11258 staticp = 0;
11259 storage_class = sc_none;
11263 else
11265 /* It's a variable. */
11267 /* An uninitialized decl with `extern' is a reference. */
11268 decl = grokvardecl (type, dname, unqualified_id,
11269 declspecs,
11270 initialized,
11271 ((type_quals & TYPE_QUAL_CONST) != 0) | (2 * concept_p),
11272 template_count,
11273 ctype ? ctype : in_namespace);
11274 if (decl == NULL_TREE)
11275 return error_mark_node;
11277 bad_specifiers (decl, BSP_VAR, virtualp,
11278 memfn_quals != TYPE_UNQUALIFIED,
11279 inlinep, friendp, raises != NULL_TREE);
11281 if (ctype)
11283 DECL_CONTEXT (decl) = ctype;
11284 if (staticp == 1)
11286 permerror (input_location, "%<static%> may not be used when defining "
11287 "(as opposed to declaring) a static data member");
11288 staticp = 0;
11289 storage_class = sc_none;
11291 if (storage_class == sc_register && TREE_STATIC (decl))
11293 error ("static member %qD declared %<register%>", decl);
11294 storage_class = sc_none;
11296 if (storage_class == sc_extern && pedantic)
11298 pedwarn (input_location, OPT_Wpedantic,
11299 "cannot explicitly declare member %q#D to have "
11300 "extern linkage", decl);
11301 storage_class = sc_none;
11304 else if (constexpr_p && DECL_EXTERNAL (decl))
11306 error ("declaration of constexpr variable %qD is not a definition",
11307 decl);
11308 constexpr_p = false;
11312 if (storage_class == sc_extern && initialized && !funcdef_flag)
11314 if (toplevel_bindings_p ())
11316 /* It's common practice (and completely valid) to have a const
11317 be initialized and declared extern. */
11318 if (!(type_quals & TYPE_QUAL_CONST))
11319 warning (0, "%qs initialized and declared %<extern%>", name);
11321 else
11323 error ("%qs has both %<extern%> and initializer", name);
11324 return error_mark_node;
11328 /* Record `register' declaration for warnings on &
11329 and in case doing stupid register allocation. */
11331 if (storage_class == sc_register)
11332 DECL_REGISTER (decl) = 1;
11333 else if (storage_class == sc_extern)
11334 DECL_THIS_EXTERN (decl) = 1;
11335 else if (storage_class == sc_static)
11336 DECL_THIS_STATIC (decl) = 1;
11338 /* Set constexpr flag on vars (functions got it in grokfndecl). */
11339 if (constexpr_p && VAR_P (decl))
11340 DECL_DECLARED_CONSTEXPR_P (decl) = true;
11342 /* Record constancy and volatility on the DECL itself . There's
11343 no need to do this when processing a template; we'll do this
11344 for the instantiated declaration based on the type of DECL. */
11345 if (!processing_template_decl)
11346 cp_apply_type_quals_to_decl (type_quals, decl);
11348 return decl;
11352 /* Subroutine of start_function. Ensure that each of the parameter
11353 types (as listed in PARMS) is complete, as is required for a
11354 function definition. */
11356 static void
11357 require_complete_types_for_parms (tree parms)
11359 for (; parms; parms = DECL_CHAIN (parms))
11361 if (dependent_type_p (TREE_TYPE (parms)))
11362 continue;
11363 if (!VOID_TYPE_P (TREE_TYPE (parms))
11364 && complete_type_or_else (TREE_TYPE (parms), parms))
11366 relayout_decl (parms);
11367 DECL_ARG_TYPE (parms) = type_passed_as (TREE_TYPE (parms));
11369 else
11370 /* grokparms or complete_type_or_else will have already issued
11371 an error. */
11372 TREE_TYPE (parms) = error_mark_node;
11376 /* Returns nonzero if T is a local variable. */
11379 local_variable_p (const_tree t)
11381 if ((VAR_P (t)
11382 /* A VAR_DECL with a context that is a _TYPE is a static data
11383 member. */
11384 && !TYPE_P (CP_DECL_CONTEXT (t))
11385 /* Any other non-local variable must be at namespace scope. */
11386 && !DECL_NAMESPACE_SCOPE_P (t))
11387 || (TREE_CODE (t) == PARM_DECL))
11388 return 1;
11390 return 0;
11393 /* Like local_variable_p, but suitable for use as a tree-walking
11394 function. */
11396 static tree
11397 local_variable_p_walkfn (tree *tp, int *walk_subtrees,
11398 void * /*data*/)
11400 if (local_variable_p (*tp)
11401 && (!DECL_ARTIFICIAL (*tp) || DECL_NAME (*tp) == this_identifier))
11402 return *tp;
11403 else if (TYPE_P (*tp))
11404 *walk_subtrees = 0;
11406 return NULL_TREE;
11409 /* Check that ARG, which is a default-argument expression for a
11410 parameter DECL, is valid. Returns ARG, or ERROR_MARK_NODE, if
11411 something goes wrong. DECL may also be a _TYPE node, rather than a
11412 DECL, if there is no DECL available. */
11414 tree
11415 check_default_argument (tree decl, tree arg, tsubst_flags_t complain)
11417 tree var;
11418 tree decl_type;
11420 if (TREE_CODE (arg) == DEFAULT_ARG)
11421 /* We get a DEFAULT_ARG when looking at an in-class declaration
11422 with a default argument. Ignore the argument for now; we'll
11423 deal with it after the class is complete. */
11424 return arg;
11426 if (TYPE_P (decl))
11428 decl_type = decl;
11429 decl = NULL_TREE;
11431 else
11432 decl_type = TREE_TYPE (decl);
11434 if (arg == error_mark_node
11435 || decl == error_mark_node
11436 || TREE_TYPE (arg) == error_mark_node
11437 || decl_type == error_mark_node)
11438 /* Something already went wrong. There's no need to check
11439 further. */
11440 return error_mark_node;
11442 /* [dcl.fct.default]
11444 A default argument expression is implicitly converted to the
11445 parameter type. */
11446 ++cp_unevaluated_operand;
11447 perform_implicit_conversion_flags (decl_type, arg, complain,
11448 LOOKUP_IMPLICIT);
11449 --cp_unevaluated_operand;
11451 /* Avoid redundant -Wzero-as-null-pointer-constant warnings at
11452 the call sites. */
11453 if (TYPE_PTR_OR_PTRMEM_P (decl_type)
11454 && null_ptr_cst_p (arg))
11455 return nullptr_node;
11457 /* [dcl.fct.default]
11459 Local variables shall not be used in default argument
11460 expressions.
11462 The keyword `this' shall not be used in a default argument of a
11463 member function. */
11464 var = cp_walk_tree_without_duplicates (&arg, local_variable_p_walkfn, NULL);
11465 if (var)
11467 if (complain & tf_warning_or_error)
11469 if (DECL_NAME (var) == this_identifier)
11470 permerror (input_location, "default argument %qE uses %qD",
11471 arg, var);
11472 else
11473 error ("default argument %qE uses local variable %qD", arg, var);
11475 return error_mark_node;
11478 /* All is well. */
11479 return arg;
11482 /* Returns a deprecated type used within TYPE, or NULL_TREE if none. */
11484 static tree
11485 type_is_deprecated (tree type)
11487 enum tree_code code;
11488 if (TREE_DEPRECATED (type))
11489 return type;
11490 if (TYPE_NAME (type)
11491 && TREE_DEPRECATED (TYPE_NAME (type)))
11492 return type;
11494 /* Do warn about using typedefs to a deprecated class. */
11495 if (OVERLOAD_TYPE_P (type) && type != TYPE_MAIN_VARIANT (type))
11496 return type_is_deprecated (TYPE_MAIN_VARIANT (type));
11498 code = TREE_CODE (type);
11500 if (code == POINTER_TYPE || code == REFERENCE_TYPE
11501 || code == OFFSET_TYPE || code == FUNCTION_TYPE
11502 || code == METHOD_TYPE || code == ARRAY_TYPE)
11503 return type_is_deprecated (TREE_TYPE (type));
11505 if (TYPE_PTRMEMFUNC_P (type))
11506 return type_is_deprecated
11507 (TREE_TYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type))));
11509 return NULL_TREE;
11512 /* Decode the list of parameter types for a function type.
11513 Given the list of things declared inside the parens,
11514 return a list of types.
11516 If this parameter does not end with an ellipsis, we append
11517 void_list_node.
11519 *PARMS is set to the chain of PARM_DECLs created. */
11521 static tree
11522 grokparms (tree parmlist, tree *parms)
11524 tree result = NULL_TREE;
11525 tree decls = NULL_TREE;
11526 tree parm;
11527 int any_error = 0;
11529 for (parm = parmlist; parm != NULL_TREE; parm = TREE_CHAIN (parm))
11531 tree type = NULL_TREE;
11532 tree init = TREE_PURPOSE (parm);
11533 tree decl = TREE_VALUE (parm);
11534 const char *errmsg;
11536 if (parm == void_list_node)
11537 break;
11539 if (! decl || TREE_TYPE (decl) == error_mark_node)
11540 continue;
11542 type = TREE_TYPE (decl);
11543 if (VOID_TYPE_P (type))
11545 if (same_type_p (type, void_type_node)
11546 && !init
11547 && !DECL_NAME (decl) && !result
11548 && TREE_CHAIN (parm) == void_list_node)
11549 /* DR 577: A parameter list consisting of a single
11550 unnamed parameter of non-dependent type 'void'. */
11551 break;
11552 else if (cv_qualified_p (type))
11553 error_at (DECL_SOURCE_LOCATION (decl),
11554 "invalid use of cv-qualified type %qT in "
11555 "parameter declaration", type);
11556 else
11557 error_at (DECL_SOURCE_LOCATION (decl),
11558 "invalid use of type %<void%> in parameter "
11559 "declaration");
11560 /* It's not a good idea to actually create parameters of
11561 type `void'; other parts of the compiler assume that a
11562 void type terminates the parameter list. */
11563 type = error_mark_node;
11564 TREE_TYPE (decl) = error_mark_node;
11567 if (type != error_mark_node
11568 && TYPE_FOR_JAVA (type)
11569 && MAYBE_CLASS_TYPE_P (type))
11571 error ("parameter %qD has Java class type", decl);
11572 type = error_mark_node;
11573 TREE_TYPE (decl) = error_mark_node;
11574 init = NULL_TREE;
11577 if (type != error_mark_node
11578 && (errmsg = targetm.invalid_parameter_type (type)))
11580 error (errmsg);
11581 type = error_mark_node;
11582 TREE_TYPE (decl) = error_mark_node;
11585 if (type != error_mark_node)
11587 if (deprecated_state != DEPRECATED_SUPPRESS)
11589 tree deptype = type_is_deprecated (type);
11590 if (deptype)
11591 warn_deprecated_use (deptype, NULL_TREE);
11594 /* Top-level qualifiers on the parameters are
11595 ignored for function types. */
11596 type = cp_build_qualified_type (type, 0);
11597 if (TREE_CODE (type) == METHOD_TYPE)
11599 error ("parameter %qD invalidly declared method type", decl);
11600 type = build_pointer_type (type);
11601 TREE_TYPE (decl) = type;
11603 else if (abstract_virtuals_error (decl, type))
11604 any_error = 1; /* Seems like a good idea. */
11605 else if (POINTER_TYPE_P (type))
11607 /* [dcl.fct]/6, parameter types cannot contain pointers
11608 (references) to arrays of unknown bound. */
11609 tree t = TREE_TYPE (type);
11610 int ptr = TYPE_PTR_P (type);
11612 while (1)
11614 if (TYPE_PTR_P (t))
11615 ptr = 1;
11616 else if (TREE_CODE (t) != ARRAY_TYPE)
11617 break;
11618 else if (!TYPE_DOMAIN (t))
11619 break;
11620 t = TREE_TYPE (t);
11622 if (TREE_CODE (t) == ARRAY_TYPE)
11623 error (ptr
11624 ? G_("parameter %qD includes pointer to array of "
11625 "unknown bound %qT")
11626 : G_("parameter %qD includes reference to array of "
11627 "unknown bound %qT"),
11628 decl, t);
11631 if (any_error)
11632 init = NULL_TREE;
11633 else if (init && !processing_template_decl)
11634 init = check_default_argument (decl, init, tf_warning_or_error);
11637 DECL_CHAIN (decl) = decls;
11638 decls = decl;
11639 result = tree_cons (init, type, result);
11641 decls = nreverse (decls);
11642 result = nreverse (result);
11643 if (parm)
11644 result = chainon (result, void_list_node);
11645 *parms = decls;
11647 return result;
11651 /* D is a constructor or overloaded `operator='.
11653 Let T be the class in which D is declared. Then, this function
11654 returns:
11656 -1 if D's is an ill-formed constructor or copy assignment operator
11657 whose first parameter is of type `T'.
11658 0 if D is not a copy constructor or copy assignment
11659 operator.
11660 1 if D is a copy constructor or copy assignment operator whose
11661 first parameter is a reference to non-const qualified T.
11662 2 if D is a copy constructor or copy assignment operator whose
11663 first parameter is a reference to const qualified T.
11665 This function can be used as a predicate. Positive values indicate
11666 a copy constructor and nonzero values indicate a copy assignment
11667 operator. */
11670 copy_fn_p (const_tree d)
11672 tree args;
11673 tree arg_type;
11674 int result = 1;
11676 gcc_assert (DECL_FUNCTION_MEMBER_P (d));
11678 if (TREE_CODE (d) == TEMPLATE_DECL
11679 || (DECL_TEMPLATE_INFO (d)
11680 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d))))
11681 /* Instantiations of template member functions are never copy
11682 functions. Note that member functions of templated classes are
11683 represented as template functions internally, and we must
11684 accept those as copy functions. */
11685 return 0;
11687 args = FUNCTION_FIRST_USER_PARMTYPE (d);
11688 if (!args)
11689 return 0;
11691 arg_type = TREE_VALUE (args);
11692 if (arg_type == error_mark_node)
11693 return 0;
11695 if (TYPE_MAIN_VARIANT (arg_type) == DECL_CONTEXT (d))
11697 /* Pass by value copy assignment operator. */
11698 result = -1;
11700 else if (TREE_CODE (arg_type) == REFERENCE_TYPE
11701 && !TYPE_REF_IS_RVALUE (arg_type)
11702 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)) == DECL_CONTEXT (d))
11704 if (CP_TYPE_CONST_P (TREE_TYPE (arg_type)))
11705 result = 2;
11707 else
11708 return 0;
11710 args = TREE_CHAIN (args);
11712 if (args && args != void_list_node && !TREE_PURPOSE (args))
11713 /* There are more non-optional args. */
11714 return 0;
11716 return result;
11719 /* D is a constructor or overloaded `operator='.
11721 Let T be the class in which D is declared. Then, this function
11722 returns true when D is a move constructor or move assignment
11723 operator, false otherwise. */
11725 bool
11726 move_fn_p (const_tree d)
11728 gcc_assert (DECL_FUNCTION_MEMBER_P (d));
11730 if (cxx_dialect == cxx98)
11731 /* There are no move constructors if we are in C++98 mode. */
11732 return false;
11734 if (TREE_CODE (d) == TEMPLATE_DECL
11735 || (DECL_TEMPLATE_INFO (d)
11736 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d))))
11737 /* Instantiations of template member functions are never move
11738 functions. Note that member functions of templated classes are
11739 represented as template functions internally, and we must
11740 accept those as move functions. */
11741 return 0;
11743 return move_signature_fn_p (d);
11746 /* D is a constructor or overloaded `operator='.
11748 Then, this function returns true when D has the same signature as a move
11749 constructor or move assignment operator (because either it is such a
11750 ctor/op= or it is a template specialization with the same signature),
11751 false otherwise. */
11753 bool
11754 move_signature_fn_p (const_tree d)
11756 tree args;
11757 tree arg_type;
11758 bool result = false;
11760 args = FUNCTION_FIRST_USER_PARMTYPE (d);
11761 if (!args)
11762 return 0;
11764 arg_type = TREE_VALUE (args);
11765 if (arg_type == error_mark_node)
11766 return 0;
11768 if (TREE_CODE (arg_type) == REFERENCE_TYPE
11769 && TYPE_REF_IS_RVALUE (arg_type)
11770 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)),
11771 DECL_CONTEXT (d)))
11772 result = true;
11774 args = TREE_CHAIN (args);
11776 if (args && args != void_list_node && !TREE_PURPOSE (args))
11777 /* There are more non-optional args. */
11778 return false;
11780 return result;
11783 /* Remember any special properties of member function DECL. */
11785 void
11786 grok_special_member_properties (tree decl)
11788 tree class_type;
11790 if (!DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
11791 return;
11793 class_type = DECL_CONTEXT (decl);
11794 if (DECL_CONSTRUCTOR_P (decl))
11796 int ctor = copy_fn_p (decl);
11798 if (!DECL_ARTIFICIAL (decl))
11799 TYPE_HAS_USER_CONSTRUCTOR (class_type) = 1;
11801 if (ctor > 0)
11803 /* [class.copy]
11805 A non-template constructor for class X is a copy
11806 constructor if its first parameter is of type X&, const
11807 X&, volatile X& or const volatile X&, and either there
11808 are no other parameters or else all other parameters have
11809 default arguments. */
11810 TYPE_HAS_COPY_CTOR (class_type) = 1;
11811 if (user_provided_p (decl))
11812 TYPE_HAS_COMPLEX_COPY_CTOR (class_type) = 1;
11813 if (ctor > 1)
11814 TYPE_HAS_CONST_COPY_CTOR (class_type) = 1;
11816 else if (sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (decl)))
11817 TYPE_HAS_DEFAULT_CONSTRUCTOR (class_type) = 1;
11818 else if (move_fn_p (decl) && user_provided_p (decl))
11819 TYPE_HAS_COMPLEX_MOVE_CTOR (class_type) = 1;
11820 else if (is_list_ctor (decl))
11821 TYPE_HAS_LIST_CTOR (class_type) = 1;
11823 if (DECL_DECLARED_CONSTEXPR_P (decl)
11824 && !copy_fn_p (decl) && !move_fn_p (decl))
11825 TYPE_HAS_CONSTEXPR_CTOR (class_type) = 1;
11827 else if (DECL_OVERLOADED_OPERATOR_P (decl) == NOP_EXPR)
11829 /* [class.copy]
11831 A non-template assignment operator for class X is a copy
11832 assignment operator if its parameter is of type X, X&, const
11833 X&, volatile X& or const volatile X&. */
11835 int assop = copy_fn_p (decl);
11837 if (assop)
11839 TYPE_HAS_COPY_ASSIGN (class_type) = 1;
11840 if (user_provided_p (decl))
11841 TYPE_HAS_COMPLEX_COPY_ASSIGN (class_type) = 1;
11842 if (assop != 1)
11843 TYPE_HAS_CONST_COPY_ASSIGN (class_type) = 1;
11845 else if (move_fn_p (decl) && user_provided_p (decl))
11846 TYPE_HAS_COMPLEX_MOVE_ASSIGN (class_type) = 1;
11848 /* Destructors are handled in check_methods. */
11851 /* Check a constructor DECL has the correct form. Complains
11852 if the class has a constructor of the form X(X). */
11855 grok_ctor_properties (const_tree ctype, const_tree decl)
11857 int ctor_parm = copy_fn_p (decl);
11859 if (ctor_parm < 0)
11861 /* [class.copy]
11863 A declaration of a constructor for a class X is ill-formed if
11864 its first parameter is of type (optionally cv-qualified) X
11865 and either there are no other parameters or else all other
11866 parameters have default arguments.
11868 We *don't* complain about member template instantiations that
11869 have this form, though; they can occur as we try to decide
11870 what constructor to use during overload resolution. Since
11871 overload resolution will never prefer such a constructor to
11872 the non-template copy constructor (which is either explicitly
11873 or implicitly defined), there's no need to worry about their
11874 existence. Theoretically, they should never even be
11875 instantiated, but that's hard to forestall. */
11876 error ("invalid constructor; you probably meant %<%T (const %T&)%>",
11877 ctype, ctype);
11878 return 0;
11881 return 1;
11884 /* An operator with this code is unary, but can also be binary. */
11886 static int
11887 ambi_op_p (enum tree_code code)
11889 return (code == INDIRECT_REF
11890 || code == ADDR_EXPR
11891 || code == UNARY_PLUS_EXPR
11892 || code == NEGATE_EXPR
11893 || code == PREINCREMENT_EXPR
11894 || code == PREDECREMENT_EXPR);
11897 /* An operator with this name can only be unary. */
11899 static int
11900 unary_op_p (enum tree_code code)
11902 return (code == TRUTH_NOT_EXPR
11903 || code == BIT_NOT_EXPR
11904 || code == COMPONENT_REF
11905 || code == TYPE_EXPR);
11908 /* DECL is a declaration for an overloaded operator. If COMPLAIN is true,
11909 errors are issued for invalid declarations. */
11911 bool
11912 grok_op_properties (tree decl, bool complain)
11914 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
11915 tree argtype;
11916 int methodp = (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE);
11917 tree name = DECL_NAME (decl);
11918 enum tree_code operator_code;
11919 int arity;
11920 bool ellipsis_p;
11921 tree class_type;
11923 /* Count the number of arguments and check for ellipsis. */
11924 for (argtype = argtypes, arity = 0;
11925 argtype && argtype != void_list_node;
11926 argtype = TREE_CHAIN (argtype))
11927 ++arity;
11928 ellipsis_p = !argtype;
11930 class_type = DECL_CONTEXT (decl);
11931 if (class_type && !CLASS_TYPE_P (class_type))
11932 class_type = NULL_TREE;
11934 if (DECL_CONV_FN_P (decl))
11935 operator_code = TYPE_EXPR;
11936 else
11939 #define DEF_OPERATOR(NAME, CODE, MANGLING, ARITY, ASSN_P) \
11940 if (ansi_opname (CODE) == name) \
11942 operator_code = (CODE); \
11943 break; \
11945 else if (ansi_assopname (CODE) == name) \
11947 operator_code = (CODE); \
11948 DECL_ASSIGNMENT_OPERATOR_P (decl) = 1; \
11949 break; \
11952 #include "operators.def"
11953 #undef DEF_OPERATOR
11955 gcc_unreachable ();
11957 while (0);
11958 gcc_assert (operator_code != MAX_TREE_CODES);
11959 SET_OVERLOADED_OPERATOR_CODE (decl, operator_code);
11961 if (class_type)
11962 switch (operator_code)
11964 case NEW_EXPR:
11965 TYPE_HAS_NEW_OPERATOR (class_type) = 1;
11966 break;
11968 case DELETE_EXPR:
11969 TYPE_GETS_DELETE (class_type) |= 1;
11970 break;
11972 case VEC_NEW_EXPR:
11973 TYPE_HAS_ARRAY_NEW_OPERATOR (class_type) = 1;
11974 break;
11976 case VEC_DELETE_EXPR:
11977 TYPE_GETS_DELETE (class_type) |= 2;
11978 break;
11980 default:
11981 break;
11984 /* [basic.std.dynamic.allocation]/1:
11986 A program is ill-formed if an allocation function is declared
11987 in a namespace scope other than global scope or declared static
11988 in global scope.
11990 The same also holds true for deallocation functions. */
11991 if (operator_code == NEW_EXPR || operator_code == VEC_NEW_EXPR
11992 || operator_code == DELETE_EXPR || operator_code == VEC_DELETE_EXPR)
11994 if (DECL_NAMESPACE_SCOPE_P (decl))
11996 if (CP_DECL_CONTEXT (decl) != global_namespace)
11998 error ("%qD may not be declared within a namespace", decl);
11999 return false;
12001 else if (!TREE_PUBLIC (decl))
12003 error ("%qD may not be declared as static", decl);
12004 return false;
12006 if (!flag_sized_deallocation && warn_cxx14_compat)
12008 tree parm = FUNCTION_ARG_CHAIN (decl);
12009 if (parm && same_type_p (TREE_VALUE (parm), size_type_node)
12010 && TREE_CHAIN (parm) == void_list_node)
12011 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc__14_compat,
12012 "%qD is a usual (non-placement) deallocation "
12013 "function in C++14 (or with -fsized-deallocation)",
12014 decl);
12019 if (operator_code == NEW_EXPR || operator_code == VEC_NEW_EXPR)
12021 TREE_TYPE (decl) = coerce_new_type (TREE_TYPE (decl));
12022 DECL_IS_OPERATOR_NEW (decl) = 1;
12024 else if (operator_code == DELETE_EXPR || operator_code == VEC_DELETE_EXPR)
12025 TREE_TYPE (decl) = coerce_delete_type (TREE_TYPE (decl));
12026 else
12028 /* An operator function must either be a non-static member function
12029 or have at least one parameter of a class, a reference to a class,
12030 an enumeration, or a reference to an enumeration. 13.4.0.6 */
12031 if (! methodp || DECL_STATIC_FUNCTION_P (decl))
12033 if (operator_code == TYPE_EXPR
12034 || operator_code == CALL_EXPR
12035 || operator_code == COMPONENT_REF
12036 || operator_code == ARRAY_REF
12037 || operator_code == NOP_EXPR)
12039 error ("%qD must be a nonstatic member function", decl);
12040 return false;
12042 else
12044 tree p;
12046 if (DECL_STATIC_FUNCTION_P (decl))
12048 error ("%qD must be either a non-static member "
12049 "function or a non-member function", decl);
12050 return false;
12053 for (p = argtypes; p && p != void_list_node; p = TREE_CHAIN (p))
12055 tree arg = non_reference (TREE_VALUE (p));
12056 if (arg == error_mark_node)
12057 return false;
12059 /* MAYBE_CLASS_TYPE_P, rather than CLASS_TYPE_P, is used
12060 because these checks are performed even on
12061 template functions. */
12062 if (MAYBE_CLASS_TYPE_P (arg)
12063 || TREE_CODE (arg) == ENUMERAL_TYPE)
12064 break;
12067 if (!p || p == void_list_node)
12069 if (complain)
12070 error ("%qD must have an argument of class or "
12071 "enumerated type", decl);
12072 return false;
12077 /* There are no restrictions on the arguments to an overloaded
12078 "operator ()". */
12079 if (operator_code == CALL_EXPR)
12080 return true;
12082 /* Warn about conversion operators that will never be used. */
12083 if (IDENTIFIER_TYPENAME_P (name)
12084 && ! DECL_TEMPLATE_INFO (decl)
12085 && warn_conversion
12086 /* Warn only declaring the function; there is no need to
12087 warn again about out-of-class definitions. */
12088 && class_type == current_class_type)
12090 tree t = TREE_TYPE (name);
12091 int ref = (TREE_CODE (t) == REFERENCE_TYPE);
12093 if (ref)
12094 t = TYPE_MAIN_VARIANT (TREE_TYPE (t));
12096 if (VOID_TYPE_P (t))
12097 warning (OPT_Wconversion,
12099 ? G_("conversion to a reference to void "
12100 "will never use a type conversion operator")
12101 : G_("conversion to void "
12102 "will never use a type conversion operator"));
12103 else if (class_type)
12105 if (t == class_type)
12106 warning (OPT_Wconversion,
12108 ? G_("conversion to a reference to the same type "
12109 "will never use a type conversion operator")
12110 : G_("conversion to the same type "
12111 "will never use a type conversion operator"));
12112 /* Don't force t to be complete here. */
12113 else if (MAYBE_CLASS_TYPE_P (t)
12114 && COMPLETE_TYPE_P (t)
12115 && DERIVED_FROM_P (t, class_type))
12116 warning (OPT_Wconversion,
12118 ? G_("conversion to a reference to a base class "
12119 "will never use a type conversion operator")
12120 : G_("conversion to a base class "
12121 "will never use a type conversion operator"));
12126 if (operator_code == COND_EXPR)
12128 /* 13.4.0.3 */
12129 error ("ISO C++ prohibits overloading operator ?:");
12130 return false;
12132 else if (ellipsis_p)
12134 error ("%qD must not have variable number of arguments", decl);
12135 return false;
12137 else if (ambi_op_p (operator_code))
12139 if (arity == 1)
12140 /* We pick the one-argument operator codes by default, so
12141 we don't have to change anything. */
12143 else if (arity == 2)
12145 /* If we thought this was a unary operator, we now know
12146 it to be a binary operator. */
12147 switch (operator_code)
12149 case INDIRECT_REF:
12150 operator_code = MULT_EXPR;
12151 break;
12153 case ADDR_EXPR:
12154 operator_code = BIT_AND_EXPR;
12155 break;
12157 case UNARY_PLUS_EXPR:
12158 operator_code = PLUS_EXPR;
12159 break;
12161 case NEGATE_EXPR:
12162 operator_code = MINUS_EXPR;
12163 break;
12165 case PREINCREMENT_EXPR:
12166 operator_code = POSTINCREMENT_EXPR;
12167 break;
12169 case PREDECREMENT_EXPR:
12170 operator_code = POSTDECREMENT_EXPR;
12171 break;
12173 default:
12174 gcc_unreachable ();
12177 SET_OVERLOADED_OPERATOR_CODE (decl, operator_code);
12179 if ((operator_code == POSTINCREMENT_EXPR
12180 || operator_code == POSTDECREMENT_EXPR)
12181 && ! processing_template_decl
12182 && ! same_type_p (TREE_VALUE (TREE_CHAIN (argtypes)), integer_type_node))
12184 if (methodp)
12185 error ("postfix %qD must take %<int%> as its argument",
12186 decl);
12187 else
12188 error ("postfix %qD must take %<int%> as its second "
12189 "argument", decl);
12190 return false;
12193 else
12195 if (methodp)
12196 error ("%qD must take either zero or one argument", decl);
12197 else
12198 error ("%qD must take either one or two arguments", decl);
12199 return false;
12202 /* More Effective C++ rule 6. */
12203 if (warn_ecpp
12204 && (operator_code == POSTINCREMENT_EXPR
12205 || operator_code == POSTDECREMENT_EXPR
12206 || operator_code == PREINCREMENT_EXPR
12207 || operator_code == PREDECREMENT_EXPR))
12209 tree arg = TREE_VALUE (argtypes);
12210 tree ret = TREE_TYPE (TREE_TYPE (decl));
12211 if (methodp || TREE_CODE (arg) == REFERENCE_TYPE)
12212 arg = TREE_TYPE (arg);
12213 arg = TYPE_MAIN_VARIANT (arg);
12214 if (operator_code == PREINCREMENT_EXPR
12215 || operator_code == PREDECREMENT_EXPR)
12217 if (TREE_CODE (ret) != REFERENCE_TYPE
12218 || !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (ret)),
12219 arg))
12220 warning (OPT_Weffc__, "prefix %qD should return %qT", decl,
12221 build_reference_type (arg));
12223 else
12225 if (!same_type_p (TYPE_MAIN_VARIANT (ret), arg))
12226 warning (OPT_Weffc__, "postfix %qD should return %qT", decl, arg);
12230 else if (unary_op_p (operator_code))
12232 if (arity != 1)
12234 if (methodp)
12235 error ("%qD must take %<void%>", decl);
12236 else
12237 error ("%qD must take exactly one argument", decl);
12238 return false;
12241 else /* if (binary_op_p (operator_code)) */
12243 if (arity != 2)
12245 if (methodp)
12246 error ("%qD must take exactly one argument", decl);
12247 else
12248 error ("%qD must take exactly two arguments", decl);
12249 return false;
12252 /* More Effective C++ rule 7. */
12253 if (warn_ecpp
12254 && (operator_code == TRUTH_ANDIF_EXPR
12255 || operator_code == TRUTH_ORIF_EXPR
12256 || operator_code == COMPOUND_EXPR))
12257 warning (OPT_Weffc__, "user-defined %qD always evaluates both arguments",
12258 decl);
12261 /* Effective C++ rule 23. */
12262 if (warn_ecpp
12263 && arity == 2
12264 && !DECL_ASSIGNMENT_OPERATOR_P (decl)
12265 && (operator_code == PLUS_EXPR
12266 || operator_code == MINUS_EXPR
12267 || operator_code == TRUNC_DIV_EXPR
12268 || operator_code == MULT_EXPR
12269 || operator_code == TRUNC_MOD_EXPR)
12270 && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == REFERENCE_TYPE)
12271 warning (OPT_Weffc__, "%qD should return by value", decl);
12273 /* [over.oper]/8 */
12274 for (; argtypes && argtypes != void_list_node;
12275 argtypes = TREE_CHAIN (argtypes))
12276 if (TREE_PURPOSE (argtypes))
12278 TREE_PURPOSE (argtypes) = NULL_TREE;
12279 if (operator_code == POSTINCREMENT_EXPR
12280 || operator_code == POSTDECREMENT_EXPR)
12282 pedwarn (input_location, OPT_Wpedantic, "%qD cannot have default arguments",
12283 decl);
12285 else
12287 error ("%qD cannot have default arguments", decl);
12288 return false;
12292 return true;
12295 /* Return a string giving the keyword associate with CODE. */
12297 static const char *
12298 tag_name (enum tag_types code)
12300 switch (code)
12302 case record_type:
12303 return "struct";
12304 case class_type:
12305 return "class";
12306 case union_type:
12307 return "union";
12308 case enum_type:
12309 return "enum";
12310 case typename_type:
12311 return "typename";
12312 default:
12313 gcc_unreachable ();
12317 /* Name lookup in an elaborated-type-specifier (after the keyword
12318 indicated by TAG_CODE) has found the TYPE_DECL DECL. If the
12319 elaborated-type-specifier is invalid, issue a diagnostic and return
12320 error_mark_node; otherwise, return the *_TYPE to which it referred.
12321 If ALLOW_TEMPLATE_P is true, TYPE may be a class template. */
12323 tree
12324 check_elaborated_type_specifier (enum tag_types tag_code,
12325 tree decl,
12326 bool allow_template_p)
12328 tree type;
12330 /* In the case of:
12332 struct S { struct S *p; };
12334 name lookup will find the TYPE_DECL for the implicit "S::S"
12335 typedef. Adjust for that here. */
12336 if (DECL_SELF_REFERENCE_P (decl))
12337 decl = TYPE_NAME (TREE_TYPE (decl));
12339 type = TREE_TYPE (decl);
12341 /* Check TEMPLATE_TYPE_PARM first because DECL_IMPLICIT_TYPEDEF_P
12342 is false for this case as well. */
12343 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
12345 error ("using template type parameter %qT after %qs",
12346 type, tag_name (tag_code));
12347 return error_mark_node;
12349 /* Accept template template parameters. */
12350 else if (allow_template_p
12351 && (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM
12352 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM))
12354 /* [dcl.type.elab]
12356 If the identifier resolves to a typedef-name or the
12357 simple-template-id resolves to an alias template
12358 specialization, the elaborated-type-specifier is ill-formed.
12360 In other words, the only legitimate declaration to use in the
12361 elaborated type specifier is the implicit typedef created when
12362 the type is declared. */
12363 else if (!DECL_IMPLICIT_TYPEDEF_P (decl)
12364 && !DECL_SELF_REFERENCE_P (decl)
12365 && tag_code != typename_type)
12367 if (alias_template_specialization_p (type))
12368 error ("using alias template specialization %qT after %qs",
12369 type, tag_name (tag_code));
12370 else
12371 error ("using typedef-name %qD after %qs", decl, tag_name (tag_code));
12372 inform (DECL_SOURCE_LOCATION (decl),
12373 "%qD has a previous declaration here", decl);
12374 return error_mark_node;
12376 else if (TREE_CODE (type) != RECORD_TYPE
12377 && TREE_CODE (type) != UNION_TYPE
12378 && tag_code != enum_type
12379 && tag_code != typename_type)
12381 error ("%qT referred to as %qs", type, tag_name (tag_code));
12382 inform (input_location, "%q+T has a previous declaration here", type);
12383 return error_mark_node;
12385 else if (TREE_CODE (type) != ENUMERAL_TYPE
12386 && tag_code == enum_type)
12388 error ("%qT referred to as enum", type);
12389 inform (input_location, "%q+T has a previous declaration here", type);
12390 return error_mark_node;
12392 else if (!allow_template_p
12393 && TREE_CODE (type) == RECORD_TYPE
12394 && CLASSTYPE_IS_TEMPLATE (type))
12396 /* If a class template appears as elaborated type specifier
12397 without a template header such as:
12399 template <class T> class C {};
12400 void f(class C); // No template header here
12402 then the required template argument is missing. */
12403 error ("template argument required for %<%s %T%>",
12404 tag_name (tag_code),
12405 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)));
12406 return error_mark_node;
12409 return type;
12412 /* Lookup NAME in elaborate type specifier in scope according to
12413 SCOPE and issue diagnostics if necessary.
12414 Return *_TYPE node upon success, NULL_TREE when the NAME is not
12415 found, and ERROR_MARK_NODE for type error. */
12417 static tree
12418 lookup_and_check_tag (enum tag_types tag_code, tree name,
12419 tag_scope scope, bool template_header_p)
12421 tree t;
12422 tree decl;
12423 if (scope == ts_global)
12425 /* First try ordinary name lookup, ignoring hidden class name
12426 injected via friend declaration. */
12427 decl = lookup_name_prefer_type (name, 2);
12428 decl = strip_using_decl (decl);
12429 /* If that fails, the name will be placed in the smallest
12430 non-class, non-function-prototype scope according to 3.3.1/5.
12431 We may already have a hidden name declared as friend in this
12432 scope. So lookup again but not ignoring hidden names.
12433 If we find one, that name will be made visible rather than
12434 creating a new tag. */
12435 if (!decl)
12436 decl = lookup_type_scope (name, ts_within_enclosing_non_class);
12438 else
12439 decl = lookup_type_scope (name, scope);
12441 if (decl
12442 && (DECL_CLASS_TEMPLATE_P (decl)
12443 /* If scope is ts_current we're defining a class, so ignore a
12444 template template parameter. */
12445 || (scope != ts_current
12446 && DECL_TEMPLATE_TEMPLATE_PARM_P (decl))))
12447 decl = DECL_TEMPLATE_RESULT (decl);
12449 if (decl && TREE_CODE (decl) == TYPE_DECL)
12451 /* Look for invalid nested type:
12452 class C {
12453 class C {};
12454 }; */
12455 if (scope == ts_current && DECL_SELF_REFERENCE_P (decl))
12457 error ("%qD has the same name as the class in which it is "
12458 "declared",
12459 decl);
12460 return error_mark_node;
12463 /* Two cases we need to consider when deciding if a class
12464 template is allowed as an elaborated type specifier:
12465 1. It is a self reference to its own class.
12466 2. It comes with a template header.
12468 For example:
12470 template <class T> class C {
12471 class C *c1; // DECL_SELF_REFERENCE_P is true
12472 class D;
12474 template <class U> class C; // template_header_p is true
12475 template <class T> class C<T>::D {
12476 class C *c2; // DECL_SELF_REFERENCE_P is true
12477 }; */
12479 t = check_elaborated_type_specifier (tag_code,
12480 decl,
12481 template_header_p
12482 | DECL_SELF_REFERENCE_P (decl));
12483 return t;
12485 else if (decl && TREE_CODE (decl) == TREE_LIST)
12487 error ("reference to %qD is ambiguous", name);
12488 print_candidates (decl);
12489 return error_mark_node;
12491 else
12492 return NULL_TREE;
12495 /* Get the struct, enum or union (TAG_CODE says which) with tag NAME.
12496 Define the tag as a forward-reference if it is not defined.
12498 If a declaration is given, process it here, and report an error if
12499 multiple declarations are not identical.
12501 SCOPE is TS_CURRENT when this is also a definition. Only look in
12502 the current frame for the name (since C++ allows new names in any
12503 scope.) It is TS_WITHIN_ENCLOSING_NON_CLASS if this is a friend
12504 declaration. Only look beginning from the current scope outward up
12505 till the nearest non-class scope. Otherwise it is TS_GLOBAL.
12507 TEMPLATE_HEADER_P is true when this declaration is preceded by
12508 a set of template parameters. */
12510 static tree
12511 xref_tag_1 (enum tag_types tag_code, tree name,
12512 tag_scope orig_scope, bool template_header_p)
12514 enum tree_code code;
12515 tree t;
12516 tree context = NULL_TREE;
12517 tag_scope scope;
12519 gcc_assert (identifier_p (name));
12521 switch (tag_code)
12523 case record_type:
12524 case class_type:
12525 code = RECORD_TYPE;
12526 break;
12527 case union_type:
12528 code = UNION_TYPE;
12529 break;
12530 case enum_type:
12531 code = ENUMERAL_TYPE;
12532 break;
12533 default:
12534 gcc_unreachable ();
12537 if (orig_scope == ts_lambda)
12538 scope = ts_current;
12539 else
12540 scope = orig_scope;
12542 /* In case of anonymous name, xref_tag is only called to
12543 make type node and push name. Name lookup is not required. */
12544 if (ANON_AGGRNAME_P (name))
12545 t = NULL_TREE;
12546 else
12547 t = lookup_and_check_tag (tag_code, name,
12548 scope, template_header_p);
12550 if (t == error_mark_node)
12551 return error_mark_node;
12553 if (scope != ts_current && t && current_class_type
12554 && template_class_depth (current_class_type)
12555 && template_header_p)
12557 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
12558 return t;
12560 /* Since SCOPE is not TS_CURRENT, we are not looking at a
12561 definition of this tag. Since, in addition, we are currently
12562 processing a (member) template declaration of a template
12563 class, we must be very careful; consider:
12565 template <class X>
12566 struct S1
12568 template <class U>
12569 struct S2
12570 { template <class V>
12571 friend struct S1; };
12573 Here, the S2::S1 declaration should not be confused with the
12574 outer declaration. In particular, the inner version should
12575 have a template parameter of level 2, not level 1. This
12576 would be particularly important if the member declaration
12577 were instead:
12579 template <class V = U> friend struct S1;
12581 say, when we should tsubst into `U' when instantiating
12582 S2. On the other hand, when presented with:
12584 template <class T>
12585 struct S1 {
12586 template <class U>
12587 struct S2 {};
12588 template <class U>
12589 friend struct S2;
12592 we must find the inner binding eventually. We
12593 accomplish this by making sure that the new type we
12594 create to represent this declaration has the right
12595 TYPE_CONTEXT. */
12596 context = TYPE_CONTEXT (t);
12597 t = NULL_TREE;
12600 if (! t)
12602 /* If no such tag is yet defined, create a forward-reference node
12603 and record it as the "definition".
12604 When a real declaration of this type is found,
12605 the forward-reference will be altered into a real type. */
12606 if (code == ENUMERAL_TYPE)
12608 error ("use of enum %q#D without previous declaration", name);
12609 return error_mark_node;
12611 else
12613 t = make_class_type (code);
12614 TYPE_CONTEXT (t) = context;
12615 if (orig_scope == ts_lambda)
12616 /* Remember that we're declaring a lambda to avoid bogus errors
12617 in push_template_decl. */
12618 CLASSTYPE_LAMBDA_EXPR (t) = error_mark_node;
12619 t = pushtag (name, t, scope);
12622 else
12624 if (template_header_p && MAYBE_CLASS_TYPE_P (t))
12626 // Check that we aren't trying to overload a class with
12627 // different constraints.
12628 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
12629 tree constr = build_constraints (reqs, NULL_TREE);
12630 if (!redeclare_class_template (t, current_template_parms, constr))
12631 return error_mark_node;
12633 else if (!processing_template_decl
12634 && CLASS_TYPE_P (t)
12635 && CLASSTYPE_IS_TEMPLATE (t))
12637 error ("redeclaration of %qT as a non-template", t);
12638 error ("previous declaration %q+D", t);
12639 return error_mark_node;
12642 /* Make injected friend class visible. */
12643 if (scope != ts_within_enclosing_non_class
12644 && hidden_name_p (TYPE_NAME (t)))
12646 DECL_ANTICIPATED (TYPE_NAME (t)) = 0;
12647 DECL_FRIEND_P (TYPE_NAME (t)) = 0;
12649 if (TYPE_TEMPLATE_INFO (t))
12651 DECL_ANTICIPATED (TYPE_TI_TEMPLATE (t)) = 0;
12652 DECL_FRIEND_P (TYPE_TI_TEMPLATE (t)) = 0;
12657 return t;
12660 /* Wrapper for xref_tag_1. */
12662 tree
12663 xref_tag (enum tag_types tag_code, tree name,
12664 tag_scope scope, bool template_header_p)
12666 tree ret;
12667 bool subtime;
12668 subtime = timevar_cond_start (TV_NAME_LOOKUP);
12669 ret = xref_tag_1 (tag_code, name, scope, template_header_p);
12670 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
12671 return ret;
12675 tree
12676 xref_tag_from_type (tree old, tree id, tag_scope scope)
12678 enum tag_types tag_kind;
12680 if (TREE_CODE (old) == RECORD_TYPE)
12681 tag_kind = (CLASSTYPE_DECLARED_CLASS (old) ? class_type : record_type);
12682 else
12683 tag_kind = union_type;
12685 if (id == NULL_TREE)
12686 id = TYPE_IDENTIFIER (old);
12688 return xref_tag (tag_kind, id, scope, false);
12691 /* Create the binfo hierarchy for REF with (possibly NULL) base list
12692 BASE_LIST. For each element on BASE_LIST the TREE_PURPOSE is an
12693 access_* node, and the TREE_VALUE is the type of the base-class.
12694 Non-NULL TREE_TYPE indicates virtual inheritance.
12696 Returns true if the binfo hierarchy was successfully created,
12697 false if an error was detected. */
12699 bool
12700 xref_basetypes (tree ref, tree base_list)
12702 tree *basep;
12703 tree binfo, base_binfo;
12704 unsigned max_vbases = 0; /* Maximum direct & indirect virtual bases. */
12705 unsigned max_bases = 0; /* Maximum direct bases. */
12706 int i;
12707 tree default_access;
12708 tree igo_prev; /* Track Inheritance Graph Order. */
12710 if (ref == error_mark_node)
12711 return false;
12713 /* The base of a derived class is private by default, all others are
12714 public. */
12715 default_access = (TREE_CODE (ref) == RECORD_TYPE
12716 && CLASSTYPE_DECLARED_CLASS (ref)
12717 ? access_private_node : access_public_node);
12719 /* First, make sure that any templates in base-classes are
12720 instantiated. This ensures that if we call ourselves recursively
12721 we do not get confused about which classes are marked and which
12722 are not. */
12723 basep = &base_list;
12724 while (*basep)
12726 tree basetype = TREE_VALUE (*basep);
12728 /* The dependent_type_p call below should really be dependent_scope_p
12729 so that we give a hard error about using an incomplete type as a
12730 base, but we allow it with a pedwarn for backward
12731 compatibility. */
12732 if (processing_template_decl
12733 && CLASS_TYPE_P (basetype) && TYPE_BEING_DEFINED (basetype))
12734 cxx_incomplete_type_diagnostic (NULL_TREE, basetype, DK_PEDWARN);
12735 if (!dependent_type_p (basetype)
12736 && !complete_type_or_else (basetype, NULL))
12737 /* An incomplete type. Remove it from the list. */
12738 *basep = TREE_CHAIN (*basep);
12739 else
12741 max_bases++;
12742 if (TREE_TYPE (*basep))
12743 max_vbases++;
12744 if (CLASS_TYPE_P (basetype))
12745 max_vbases += vec_safe_length (CLASSTYPE_VBASECLASSES (basetype));
12746 basep = &TREE_CHAIN (*basep);
12750 TYPE_MARKED_P (ref) = 1;
12752 /* The binfo slot should be empty, unless this is an (ill-formed)
12753 redefinition. */
12754 if (TYPE_BINFO (ref) && !TYPE_SIZE (ref))
12756 error ("redefinition of %q#T", ref);
12757 return false;
12760 gcc_assert (TYPE_MAIN_VARIANT (ref) == ref);
12762 binfo = make_tree_binfo (max_bases);
12764 TYPE_BINFO (ref) = binfo;
12765 BINFO_OFFSET (binfo) = size_zero_node;
12766 BINFO_TYPE (binfo) = ref;
12768 /* Apply base-class info set up to the variants of this type. */
12769 fixup_type_variants (ref);
12771 if (max_bases)
12773 vec_alloc (BINFO_BASE_ACCESSES (binfo), max_bases);
12774 /* An aggregate cannot have baseclasses. */
12775 CLASSTYPE_NON_AGGREGATE (ref) = 1;
12777 if (TREE_CODE (ref) == UNION_TYPE)
12779 error ("derived union %qT invalid", ref);
12780 return false;
12784 if (max_bases > 1)
12786 if (TYPE_FOR_JAVA (ref))
12788 error ("Java class %qT cannot have multiple bases", ref);
12789 return false;
12793 if (max_vbases)
12795 vec_alloc (CLASSTYPE_VBASECLASSES (ref), max_vbases);
12797 if (TYPE_FOR_JAVA (ref))
12799 error ("Java class %qT cannot have virtual bases", ref);
12800 return false;
12804 for (igo_prev = binfo; base_list; base_list = TREE_CHAIN (base_list))
12806 tree access = TREE_PURPOSE (base_list);
12807 int via_virtual = TREE_TYPE (base_list) != NULL_TREE;
12808 tree basetype = TREE_VALUE (base_list);
12810 if (access == access_default_node)
12811 access = default_access;
12813 if (PACK_EXPANSION_P (basetype))
12814 basetype = PACK_EXPANSION_PATTERN (basetype);
12815 if (TREE_CODE (basetype) == TYPE_DECL)
12816 basetype = TREE_TYPE (basetype);
12817 if (!MAYBE_CLASS_TYPE_P (basetype) || TREE_CODE (basetype) == UNION_TYPE)
12819 error ("base type %qT fails to be a struct or class type",
12820 basetype);
12821 return false;
12824 if (TYPE_FOR_JAVA (basetype) && (current_lang_depth () == 0))
12825 TYPE_FOR_JAVA (ref) = 1;
12827 base_binfo = NULL_TREE;
12828 if (CLASS_TYPE_P (basetype) && !dependent_scope_p (basetype))
12830 base_binfo = TYPE_BINFO (basetype);
12831 /* The original basetype could have been a typedef'd type. */
12832 basetype = BINFO_TYPE (base_binfo);
12834 /* Inherit flags from the base. */
12835 TYPE_HAS_NEW_OPERATOR (ref)
12836 |= TYPE_HAS_NEW_OPERATOR (basetype);
12837 TYPE_HAS_ARRAY_NEW_OPERATOR (ref)
12838 |= TYPE_HAS_ARRAY_NEW_OPERATOR (basetype);
12839 TYPE_GETS_DELETE (ref) |= TYPE_GETS_DELETE (basetype);
12840 TYPE_HAS_CONVERSION (ref) |= TYPE_HAS_CONVERSION (basetype);
12841 CLASSTYPE_DIAMOND_SHAPED_P (ref)
12842 |= CLASSTYPE_DIAMOND_SHAPED_P (basetype);
12843 CLASSTYPE_REPEATED_BASE_P (ref)
12844 |= CLASSTYPE_REPEATED_BASE_P (basetype);
12847 /* We must do this test after we've seen through a typedef
12848 type. */
12849 if (TYPE_MARKED_P (basetype))
12851 if (basetype == ref)
12852 error ("recursive type %qT undefined", basetype);
12853 else
12854 error ("duplicate base type %qT invalid", basetype);
12855 return false;
12858 if (PACK_EXPANSION_P (TREE_VALUE (base_list)))
12859 /* Regenerate the pack expansion for the bases. */
12860 basetype = make_pack_expansion (basetype);
12862 TYPE_MARKED_P (basetype) = 1;
12864 base_binfo = copy_binfo (base_binfo, basetype, ref,
12865 &igo_prev, via_virtual);
12866 if (!BINFO_INHERITANCE_CHAIN (base_binfo))
12867 BINFO_INHERITANCE_CHAIN (base_binfo) = binfo;
12869 BINFO_BASE_APPEND (binfo, base_binfo);
12870 BINFO_BASE_ACCESS_APPEND (binfo, access);
12873 if (vec_safe_length (CLASSTYPE_VBASECLASSES (ref)) < max_vbases)
12874 /* If we didn't get max_vbases vbases, we must have shared at
12875 least one of them, and are therefore diamond shaped. */
12876 CLASSTYPE_DIAMOND_SHAPED_P (ref) = 1;
12878 /* Unmark all the types. */
12879 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
12880 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
12881 TYPE_MARKED_P (ref) = 0;
12883 /* Now see if we have a repeated base type. */
12884 if (!CLASSTYPE_REPEATED_BASE_P (ref))
12886 for (base_binfo = binfo; base_binfo;
12887 base_binfo = TREE_CHAIN (base_binfo))
12889 if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
12891 CLASSTYPE_REPEATED_BASE_P (ref) = 1;
12892 break;
12894 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 1;
12896 for (base_binfo = binfo; base_binfo;
12897 base_binfo = TREE_CHAIN (base_binfo))
12898 if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
12899 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
12900 else
12901 break;
12904 return true;
12908 /* Copies the enum-related properties from type SRC to type DST.
12909 Used with the underlying type of an enum and the enum itself. */
12910 static void
12911 copy_type_enum (tree dst, tree src)
12913 tree t;
12914 for (t = dst; t; t = TYPE_NEXT_VARIANT (t))
12916 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (src);
12917 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (src);
12918 TYPE_SIZE (t) = TYPE_SIZE (src);
12919 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (src);
12920 SET_TYPE_MODE (dst, TYPE_MODE (src));
12921 TYPE_PRECISION (t) = TYPE_PRECISION (src);
12922 TYPE_ALIGN (t) = TYPE_ALIGN (src);
12923 TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (src);
12924 TYPE_UNSIGNED (t) = TYPE_UNSIGNED (src);
12928 /* Begin compiling the definition of an enumeration type.
12929 NAME is its name,
12931 if ENUMTYPE is not NULL_TREE then the type has alredy been found.
12933 UNDERLYING_TYPE is the type that will be used as the storage for
12934 the enumeration type. This should be NULL_TREE if no storage type
12935 was specified.
12937 SCOPED_ENUM_P is true if this is a scoped enumeration type.
12939 if IS_NEW is not NULL, gets TRUE iff a new type is created.
12941 Returns the type object, as yet incomplete.
12942 Also records info about it so that build_enumerator
12943 may be used to declare the individual values as they are read. */
12945 tree
12946 start_enum (tree name, tree enumtype, tree underlying_type,
12947 bool scoped_enum_p, bool *is_new)
12949 tree prevtype = NULL_TREE;
12950 gcc_assert (identifier_p (name));
12952 if (is_new)
12953 *is_new = false;
12954 /* [C++0x dcl.enum]p5:
12956 If not explicitly specified, the underlying type of a scoped
12957 enumeration type is int. */
12958 if (!underlying_type && scoped_enum_p)
12959 underlying_type = integer_type_node;
12961 if (underlying_type)
12962 underlying_type = cv_unqualified (underlying_type);
12964 /* If this is the real definition for a previous forward reference,
12965 fill in the contents in the same object that used to be the
12966 forward reference. */
12967 if (!enumtype)
12968 enumtype = lookup_and_check_tag (enum_type, name,
12969 /*tag_scope=*/ts_current,
12970 /*template_header_p=*/false);
12972 /* In case of a template_decl, the only check that should be deferred
12973 to instantiation time is the comparison of underlying types. */
12974 if (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE)
12976 if (scoped_enum_p != SCOPED_ENUM_P (enumtype))
12978 error_at (input_location, "scoped/unscoped mismatch "
12979 "in enum %q#T", enumtype);
12980 error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
12981 "previous definition here");
12982 enumtype = error_mark_node;
12984 else if (ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) != !! underlying_type)
12986 error_at (input_location, "underlying type mismatch "
12987 "in enum %q#T", enumtype);
12988 error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
12989 "previous definition here");
12990 enumtype = error_mark_node;
12992 else if (underlying_type && ENUM_UNDERLYING_TYPE (enumtype)
12993 && !dependent_type_p (underlying_type)
12994 && !dependent_type_p (ENUM_UNDERLYING_TYPE (enumtype))
12995 && !same_type_p (underlying_type,
12996 ENUM_UNDERLYING_TYPE (enumtype)))
12998 error_at (input_location, "different underlying type "
12999 "in enum %q#T", enumtype);
13000 error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
13001 "previous definition here");
13002 underlying_type = NULL_TREE;
13006 if (!enumtype || TREE_CODE (enumtype) != ENUMERAL_TYPE
13007 || processing_template_decl)
13009 /* In case of error, make a dummy enum to allow parsing to
13010 continue. */
13011 if (enumtype == error_mark_node)
13013 name = make_anon_name ();
13014 enumtype = NULL_TREE;
13017 /* enumtype may be an ENUMERAL_TYPE if this is a redefinition
13018 of an opaque enum, or an opaque enum of an already defined
13019 enumeration (C++0x only).
13020 In any other case, it'll be NULL_TREE. */
13021 if (!enumtype)
13023 if (is_new)
13024 *is_new = true;
13026 prevtype = enumtype;
13028 /* Do not push the decl more than once, unless we need to
13029 compare underlying types at instantiation time */
13030 if (!enumtype
13031 || TREE_CODE (enumtype) != ENUMERAL_TYPE
13032 || (underlying_type
13033 && dependent_type_p (underlying_type))
13034 || (ENUM_UNDERLYING_TYPE (enumtype)
13035 && dependent_type_p (ENUM_UNDERLYING_TYPE (enumtype))))
13037 enumtype = cxx_make_type (ENUMERAL_TYPE);
13038 enumtype = pushtag (name, enumtype, /*tag_scope=*/ts_current);
13040 else
13041 enumtype = xref_tag (enum_type, name, /*tag_scope=*/ts_current,
13042 false);
13044 if (enumtype == error_mark_node)
13045 return error_mark_node;
13047 /* The enum is considered opaque until the opening '{' of the
13048 enumerator list. */
13049 SET_OPAQUE_ENUM_P (enumtype, true);
13050 ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) = !! underlying_type;
13053 SET_SCOPED_ENUM_P (enumtype, scoped_enum_p);
13055 if (underlying_type)
13057 if (CP_INTEGRAL_TYPE_P (underlying_type))
13059 copy_type_enum (enumtype, underlying_type);
13060 ENUM_UNDERLYING_TYPE (enumtype) = underlying_type;
13062 else if (dependent_type_p (underlying_type))
13063 ENUM_UNDERLYING_TYPE (enumtype) = underlying_type;
13064 else
13065 error ("underlying type %<%T%> of %<%T%> must be an integral type",
13066 underlying_type, enumtype);
13069 /* If into a template class, the returned enum is always the first
13070 declaration (opaque or not) seen. This way all the references to
13071 this type will be to the same declaration. The following ones are used
13072 only to check for definition errors. */
13073 if (prevtype && processing_template_decl)
13074 return prevtype;
13075 else
13076 return enumtype;
13079 /* After processing and defining all the values of an enumeration type,
13080 install their decls in the enumeration type.
13081 ENUMTYPE is the type object. */
13083 void
13084 finish_enum_value_list (tree enumtype)
13086 tree values;
13087 tree underlying_type;
13088 tree decl;
13089 tree value;
13090 tree minnode, maxnode;
13091 tree t;
13093 bool fixed_underlying_type_p
13094 = ENUM_UNDERLYING_TYPE (enumtype) != NULL_TREE;
13096 /* We built up the VALUES in reverse order. */
13097 TYPE_VALUES (enumtype) = nreverse (TYPE_VALUES (enumtype));
13099 /* For an enum defined in a template, just set the type of the values;
13100 all further processing is postponed until the template is
13101 instantiated. We need to set the type so that tsubst of a CONST_DECL
13102 works. */
13103 if (processing_template_decl)
13105 for (values = TYPE_VALUES (enumtype);
13106 values;
13107 values = TREE_CHAIN (values))
13108 TREE_TYPE (TREE_VALUE (values)) = enumtype;
13109 return;
13112 /* Determine the minimum and maximum values of the enumerators. */
13113 if (TYPE_VALUES (enumtype))
13115 minnode = maxnode = NULL_TREE;
13117 for (values = TYPE_VALUES (enumtype);
13118 values;
13119 values = TREE_CHAIN (values))
13121 decl = TREE_VALUE (values);
13123 /* [dcl.enum]: Following the closing brace of an enum-specifier,
13124 each enumerator has the type of its enumeration. Prior to the
13125 closing brace, the type of each enumerator is the type of its
13126 initializing value. */
13127 TREE_TYPE (decl) = enumtype;
13129 /* Update the minimum and maximum values, if appropriate. */
13130 value = DECL_INITIAL (decl);
13131 if (value == error_mark_node)
13132 value = integer_zero_node;
13133 /* Figure out what the minimum and maximum values of the
13134 enumerators are. */
13135 if (!minnode)
13136 minnode = maxnode = value;
13137 else if (tree_int_cst_lt (maxnode, value))
13138 maxnode = value;
13139 else if (tree_int_cst_lt (value, minnode))
13140 minnode = value;
13143 else
13144 /* [dcl.enum]
13146 If the enumerator-list is empty, the underlying type is as if
13147 the enumeration had a single enumerator with value 0. */
13148 minnode = maxnode = integer_zero_node;
13150 if (!fixed_underlying_type_p)
13152 /* Compute the number of bits require to represent all values of the
13153 enumeration. We must do this before the type of MINNODE and
13154 MAXNODE are transformed, since tree_int_cst_min_precision relies
13155 on the TREE_TYPE of the value it is passed. */
13156 signop sgn = tree_int_cst_sgn (minnode) >= 0 ? UNSIGNED : SIGNED;
13157 int lowprec = tree_int_cst_min_precision (minnode, sgn);
13158 int highprec = tree_int_cst_min_precision (maxnode, sgn);
13159 int precision = MAX (lowprec, highprec);
13160 unsigned int itk;
13161 bool use_short_enum;
13163 /* Determine the underlying type of the enumeration.
13165 [dcl.enum]
13167 The underlying type of an enumeration is an integral type that
13168 can represent all the enumerator values defined in the
13169 enumeration. It is implementation-defined which integral type is
13170 used as the underlying type for an enumeration except that the
13171 underlying type shall not be larger than int unless the value of
13172 an enumerator cannot fit in an int or unsigned int.
13174 We use "int" or an "unsigned int" as the underlying type, even if
13175 a smaller integral type would work, unless the user has
13176 explicitly requested that we use the smallest possible type. The
13177 user can request that for all enumerations with a command line
13178 flag, or for just one enumeration with an attribute. */
13180 use_short_enum = flag_short_enums
13181 || lookup_attribute ("packed", TYPE_ATTRIBUTES (enumtype));
13183 for (itk = (use_short_enum ? itk_char : itk_int);
13184 itk != itk_none;
13185 itk++)
13187 underlying_type = integer_types[itk];
13188 if (underlying_type != NULL_TREE
13189 && TYPE_PRECISION (underlying_type) >= precision
13190 && TYPE_SIGN (underlying_type) == sgn)
13191 break;
13193 if (itk == itk_none)
13195 /* DR 377
13197 IF no integral type can represent all the enumerator values, the
13198 enumeration is ill-formed. */
13199 error ("no integral type can represent all of the enumerator values "
13200 "for %qT", enumtype);
13201 precision = TYPE_PRECISION (long_long_integer_type_node);
13202 underlying_type = integer_types[itk_unsigned_long_long];
13205 /* [dcl.enum]
13207 The value of sizeof() applied to an enumeration type, an object
13208 of an enumeration type, or an enumerator, is the value of sizeof()
13209 applied to the underlying type. */
13210 copy_type_enum (enumtype, underlying_type);
13212 /* Compute the minimum and maximum values for the type.
13214 [dcl.enum]
13216 For an enumeration where emin is the smallest enumerator and emax
13217 is the largest, the values of the enumeration are the values of the
13218 underlying type in the range bmin to bmax, where bmin and bmax are,
13219 respectively, the smallest and largest values of the smallest bit-
13220 field that can store emin and emax. */
13222 /* The middle-end currently assumes that types with TYPE_PRECISION
13223 narrower than their underlying type are suitably zero or sign
13224 extended to fill their mode. Similarly, it assumes that the front
13225 end assures that a value of a particular type must be within
13226 TYPE_MIN_VALUE and TYPE_MAX_VALUE.
13228 We used to set these fields based on bmin and bmax, but that led
13229 to invalid assumptions like optimizing away bounds checking. So
13230 now we just set the TYPE_PRECISION, TYPE_MIN_VALUE, and
13231 TYPE_MAX_VALUE to the values for the mode above and only restrict
13232 the ENUM_UNDERLYING_TYPE for the benefit of diagnostics. */
13233 ENUM_UNDERLYING_TYPE (enumtype)
13234 = build_distinct_type_copy (underlying_type);
13235 TYPE_PRECISION (ENUM_UNDERLYING_TYPE (enumtype)) = precision;
13236 set_min_and_max_values_for_integral_type
13237 (ENUM_UNDERLYING_TYPE (enumtype), precision, sgn);
13239 /* If -fstrict-enums, still constrain TYPE_MIN/MAX_VALUE. */
13240 if (flag_strict_enums)
13241 set_min_and_max_values_for_integral_type (enumtype, precision, sgn);
13243 else
13244 underlying_type = ENUM_UNDERLYING_TYPE (enumtype);
13246 /* Convert each of the enumerators to the type of the underlying
13247 type of the enumeration. */
13248 for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values))
13250 location_t saved_location;
13252 decl = TREE_VALUE (values);
13253 saved_location = input_location;
13254 input_location = DECL_SOURCE_LOCATION (decl);
13255 if (fixed_underlying_type_p)
13256 /* If the enumeration type has a fixed underlying type, we
13257 already checked all of the enumerator values. */
13258 value = DECL_INITIAL (decl);
13259 else
13260 value = perform_implicit_conversion (underlying_type,
13261 DECL_INITIAL (decl),
13262 tf_warning_or_error);
13263 input_location = saved_location;
13265 /* Do not clobber shared ints. */
13266 value = copy_node (value);
13268 TREE_TYPE (value) = enumtype;
13269 DECL_INITIAL (decl) = value;
13272 /* Fix up all variant types of this enum type. */
13273 for (t = TYPE_MAIN_VARIANT (enumtype); t; t = TYPE_NEXT_VARIANT (t))
13274 TYPE_VALUES (t) = TYPE_VALUES (enumtype);
13276 if (at_class_scope_p ()
13277 && COMPLETE_TYPE_P (current_class_type)
13278 && UNSCOPED_ENUM_P (enumtype))
13279 insert_late_enum_def_into_classtype_sorted_fields (enumtype,
13280 current_class_type);
13282 /* Finish debugging output for this type. */
13283 rest_of_type_compilation (enumtype, namespace_bindings_p ());
13286 /* Finishes the enum type. This is called only the first time an
13287 enumeration is seen, be it opaque or odinary.
13288 ENUMTYPE is the type object. */
13290 void
13291 finish_enum (tree enumtype)
13293 if (processing_template_decl)
13295 if (at_function_scope_p ())
13296 add_stmt (build_min (TAG_DEFN, enumtype));
13297 return;
13300 /* If this is a forward declaration, there should not be any variants,
13301 though we can get a variant in the middle of an enum-specifier with
13302 wacky code like 'enum E { e = sizeof(const E*) };' */
13303 gcc_assert (enumtype == TYPE_MAIN_VARIANT (enumtype)
13304 && (TYPE_VALUES (enumtype)
13305 || !TYPE_NEXT_VARIANT (enumtype)));
13308 /* Build and install a CONST_DECL for an enumeration constant of the
13309 enumeration type ENUMTYPE whose NAME and VALUE (if any) are provided.
13310 LOC is the location of NAME.
13311 Assignment of sequential values by default is handled here. */
13313 void
13314 build_enumerator (tree name, tree value, tree enumtype, location_t loc)
13316 tree decl;
13317 tree context;
13318 tree type;
13320 /* scalar_constant_value will pull out this expression, so make sure
13321 it's folded as appropriate. */
13322 if (processing_template_decl)
13323 value = fold_non_dependent_expr (value);
13325 /* If the VALUE was erroneous, pretend it wasn't there; that will
13326 result in the enum being assigned the next value in sequence. */
13327 if (value == error_mark_node)
13328 value = NULL_TREE;
13330 /* Remove no-op casts from the value. */
13331 if (value)
13332 STRIP_TYPE_NOPS (value);
13334 if (! processing_template_decl)
13336 /* Validate and default VALUE. */
13337 if (value != NULL_TREE)
13339 if (!ENUM_UNDERLYING_TYPE (enumtype))
13341 tree tmp_value = build_expr_type_conversion (WANT_INT | WANT_ENUM,
13342 value, true);
13343 if (tmp_value)
13344 value = tmp_value;
13346 else if (! INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (value)))
13347 value = perform_implicit_conversion_flags
13348 (ENUM_UNDERLYING_TYPE (enumtype), value, tf_warning_or_error,
13349 LOOKUP_IMPLICIT | LOOKUP_NO_NARROWING);
13351 if (value == error_mark_node)
13352 value = NULL_TREE;
13354 if (value != NULL_TREE)
13356 value = cxx_constant_value (value);
13358 if (TREE_CODE (value) != INTEGER_CST
13359 || ! INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (value)))
13361 error ("enumerator value for %qD is not an integer constant",
13362 name);
13363 value = NULL_TREE;
13368 /* Default based on previous value. */
13369 if (value == NULL_TREE)
13371 if (TYPE_VALUES (enumtype))
13373 tree prev_value;
13374 bool overflowed;
13376 /* C++03 7.2/4: If no initializer is specified for the first
13377 enumerator, the type is an unspecified integral
13378 type. Otherwise the type is the same as the type of the
13379 initializing value of the preceding enumerator unless the
13380 incremented value is not representable in that type, in
13381 which case the type is an unspecified integral type
13382 sufficient to contain the incremented value. */
13383 prev_value = DECL_INITIAL (TREE_VALUE (TYPE_VALUES (enumtype)));
13384 if (error_operand_p (prev_value))
13385 value = error_mark_node;
13386 else
13388 tree type = TREE_TYPE (prev_value);
13389 signop sgn = TYPE_SIGN (type);
13390 widest_int wi = wi::add (wi::to_widest (prev_value), 1, sgn,
13391 &overflowed);
13392 if (!overflowed)
13394 bool pos = !wi::neg_p (wi, sgn);
13395 if (!wi::fits_to_tree_p (wi, type))
13397 unsigned int itk;
13398 for (itk = itk_int; itk != itk_none; itk++)
13400 type = integer_types[itk];
13401 if (type != NULL_TREE
13402 && (pos || !TYPE_UNSIGNED (type))
13403 && wi::fits_to_tree_p (wi, type))
13404 break;
13406 if (type && cxx_dialect < cxx11
13407 && itk > itk_unsigned_long)
13408 pedwarn (input_location, OPT_Wlong_long, pos ? "\
13409 incremented enumerator value is too large for %<unsigned long%>" : "\
13410 incremented enumerator value is too large for %<long%>");
13412 if (type == NULL_TREE)
13413 overflowed = true;
13414 else
13415 value = wide_int_to_tree (type, wi);
13418 if (overflowed)
13420 error ("overflow in enumeration values at %qD", name);
13421 value = error_mark_node;
13425 else
13426 value = integer_zero_node;
13429 /* Remove no-op casts from the value. */
13430 STRIP_TYPE_NOPS (value);
13432 /* If the underlying type of the enum is fixed, check whether
13433 the enumerator values fits in the underlying type. If it
13434 does not fit, the program is ill-formed [C++0x dcl.enum]. */
13435 if (ENUM_UNDERLYING_TYPE (enumtype)
13436 && value
13437 && TREE_CODE (value) == INTEGER_CST)
13439 if (!int_fits_type_p (value, ENUM_UNDERLYING_TYPE (enumtype)))
13440 error ("enumerator value %E is outside the range of underlying "
13441 "type %<%T%>", value, ENUM_UNDERLYING_TYPE (enumtype));
13443 /* Convert the value to the appropriate type. */
13444 value = convert (ENUM_UNDERLYING_TYPE (enumtype), value);
13448 /* C++ associates enums with global, function, or class declarations. */
13449 context = current_scope ();
13451 /* Build the actual enumeration constant. Note that the enumeration
13452 constants have the underlying type of the enum (if it is fixed)
13453 or the type of their initializer (if the underlying type of the
13454 enum is not fixed):
13456 [ C++0x dcl.enum ]
13458 If the underlying type is fixed, the type of each enumerator
13459 prior to the closing brace is the underlying type; if the
13460 initializing value of an enumerator cannot be represented by
13461 the underlying type, the program is ill-formed. If the
13462 underlying type is not fixed, the type of each enumerator is
13463 the type of its initializing value.
13465 If the underlying type is not fixed, it will be computed by
13466 finish_enum and we will reset the type of this enumerator. Of
13467 course, if we're processing a template, there may be no value. */
13468 type = value ? TREE_TYPE (value) : NULL_TREE;
13470 decl = build_decl (loc, CONST_DECL, name, type);
13472 DECL_CONTEXT (decl) = enumtype;
13473 TREE_CONSTANT (decl) = 1;
13474 TREE_READONLY (decl) = 1;
13475 DECL_INITIAL (decl) = value;
13477 if (context && context == current_class_type && !SCOPED_ENUM_P (enumtype))
13478 /* In something like `struct S { enum E { i = 7 }; };' we put `i'
13479 on the TYPE_FIELDS list for `S'. (That's so that you can say
13480 things like `S::i' later.) */
13481 finish_member_declaration (decl);
13482 else
13483 pushdecl (decl);
13485 /* Add this enumeration constant to the list for this type. */
13486 TYPE_VALUES (enumtype) = tree_cons (name, decl, TYPE_VALUES (enumtype));
13489 /* Look for an enumerator with the given NAME within the enumeration
13490 type ENUMTYPE. This routine is used primarily for qualified name
13491 lookup into an enumerator in C++0x, e.g.,
13493 enum class Color { Red, Green, Blue };
13495 Color color = Color::Red;
13497 Returns the value corresponding to the enumerator, or
13498 NULL_TREE if no such enumerator was found. */
13499 tree
13500 lookup_enumerator (tree enumtype, tree name)
13502 tree e;
13503 gcc_assert (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE);
13505 e = purpose_member (name, TYPE_VALUES (enumtype));
13506 return e? TREE_VALUE (e) : NULL_TREE;
13510 /* We're defining DECL. Make sure that its type is OK. */
13512 static void
13513 check_function_type (tree decl, tree current_function_parms)
13515 tree fntype = TREE_TYPE (decl);
13516 tree return_type = complete_type (TREE_TYPE (fntype));
13518 /* In a function definition, arg types must be complete. */
13519 require_complete_types_for_parms (current_function_parms);
13521 if (dependent_type_p (return_type)
13522 || type_uses_auto (return_type))
13523 return;
13524 if (!COMPLETE_OR_VOID_TYPE_P (return_type)
13525 || (TYPE_FOR_JAVA (return_type) && MAYBE_CLASS_TYPE_P (return_type)))
13527 tree args = TYPE_ARG_TYPES (fntype);
13529 if (!COMPLETE_OR_VOID_TYPE_P (return_type))
13530 error ("return type %q#T is incomplete", return_type);
13531 else
13532 error ("return type has Java class type %q#T", return_type);
13534 /* Make it return void instead. */
13535 if (TREE_CODE (fntype) == METHOD_TYPE)
13536 fntype = build_method_type_directly (TREE_TYPE (TREE_VALUE (args)),
13537 void_type_node,
13538 TREE_CHAIN (args));
13539 else
13540 fntype = build_function_type (void_type_node, args);
13541 fntype
13542 = build_exception_variant (fntype,
13543 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl)));
13544 fntype = (cp_build_type_attribute_variant
13545 (fntype, TYPE_ATTRIBUTES (TREE_TYPE (decl))));
13546 TREE_TYPE (decl) = fntype;
13548 else
13549 abstract_virtuals_error (decl, TREE_TYPE (fntype));
13552 /* Create the FUNCTION_DECL for a function definition.
13553 DECLSPECS and DECLARATOR are the parts of the declaration;
13554 they describe the function's name and the type it returns,
13555 but twisted together in a fashion that parallels the syntax of C.
13557 FLAGS is a bitwise or of SF_PRE_PARSED (indicating that the
13558 DECLARATOR is really the DECL for the function we are about to
13559 process and that DECLSPECS should be ignored), SF_INCLASS_INLINE
13560 indicating that the function is an inline defined in-class.
13562 This function creates a binding context for the function body
13563 as well as setting up the FUNCTION_DECL in current_function_decl.
13565 For C++, we must first check whether that datum makes any sense.
13566 For example, "class A local_a(1,2);" means that variable local_a
13567 is an aggregate of type A, which should have a constructor
13568 applied to it with the argument list [1, 2].
13570 On entry, DECL_INITIAL (decl1) should be NULL_TREE or error_mark_node,
13571 or may be a BLOCK if the function has been defined previously
13572 in this translation unit. On exit, DECL_INITIAL (decl1) will be
13573 error_mark_node if the function has never been defined, or
13574 a BLOCK if the function has been defined somewhere. */
13576 bool
13577 start_preparsed_function (tree decl1, tree attrs, int flags)
13579 tree ctype = NULL_TREE;
13580 tree fntype;
13581 tree restype;
13582 int doing_friend = 0;
13583 cp_binding_level *bl;
13584 tree current_function_parms;
13585 struct c_fileinfo *finfo
13586 = get_fileinfo (LOCATION_FILE (DECL_SOURCE_LOCATION (decl1)));
13587 bool honor_interface;
13589 /* Sanity check. */
13590 gcc_assert (VOID_TYPE_P (TREE_VALUE (void_list_node)));
13591 gcc_assert (TREE_CHAIN (void_list_node) == NULL_TREE);
13593 fntype = TREE_TYPE (decl1);
13594 if (TREE_CODE (fntype) == METHOD_TYPE)
13595 ctype = TYPE_METHOD_BASETYPE (fntype);
13597 /* ISO C++ 11.4/5. A friend function defined in a class is in
13598 the (lexical) scope of the class in which it is defined. */
13599 if (!ctype && DECL_FRIEND_P (decl1))
13601 ctype = DECL_FRIEND_CONTEXT (decl1);
13603 /* CTYPE could be null here if we're dealing with a template;
13604 for example, `inline friend float foo()' inside a template
13605 will have no CTYPE set. */
13606 if (ctype && TREE_CODE (ctype) != RECORD_TYPE)
13607 ctype = NULL_TREE;
13608 else
13609 doing_friend = 1;
13612 if (DECL_DECLARED_INLINE_P (decl1)
13613 && lookup_attribute ("noinline", attrs))
13614 warning (0, "inline function %q+D given attribute noinline", decl1);
13616 /* Handle gnu_inline attribute. */
13617 if (GNU_INLINE_P (decl1))
13619 DECL_EXTERNAL (decl1) = 1;
13620 DECL_NOT_REALLY_EXTERN (decl1) = 0;
13621 DECL_INTERFACE_KNOWN (decl1) = 1;
13622 DECL_DISREGARD_INLINE_LIMITS (decl1) = 1;
13625 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl1))
13626 /* This is a constructor, we must ensure that any default args
13627 introduced by this definition are propagated to the clones
13628 now. The clones are used directly in overload resolution. */
13629 adjust_clone_args (decl1);
13631 /* Sometimes we don't notice that a function is a static member, and
13632 build a METHOD_TYPE for it. Fix that up now. */
13633 gcc_assert (!(ctype != NULL_TREE && DECL_STATIC_FUNCTION_P (decl1)
13634 && TREE_CODE (TREE_TYPE (decl1)) == METHOD_TYPE));
13636 /* Set up current_class_type, and enter the scope of the class, if
13637 appropriate. */
13638 if (ctype)
13639 push_nested_class (ctype);
13640 else if (DECL_STATIC_FUNCTION_P (decl1))
13641 push_nested_class (DECL_CONTEXT (decl1));
13643 /* Now that we have entered the scope of the class, we must restore
13644 the bindings for any template parameters surrounding DECL1, if it
13645 is an inline member template. (Order is important; consider the
13646 case where a template parameter has the same name as a field of
13647 the class.) It is not until after this point that
13648 PROCESSING_TEMPLATE_DECL is guaranteed to be set up correctly. */
13649 if (flags & SF_INCLASS_INLINE)
13650 maybe_begin_member_template_processing (decl1);
13652 /* Effective C++ rule 15. */
13653 if (warn_ecpp
13654 && DECL_OVERLOADED_OPERATOR_P (decl1) == NOP_EXPR
13655 && VOID_TYPE_P (TREE_TYPE (fntype)))
13656 warning (OPT_Weffc__, "%<operator=%> should return a reference to %<*this%>");
13658 /* Make the init_value nonzero so pushdecl knows this is not tentative.
13659 error_mark_node is replaced below (in poplevel) with the BLOCK. */
13660 if (!DECL_INITIAL (decl1))
13661 DECL_INITIAL (decl1) = error_mark_node;
13663 /* This function exists in static storage.
13664 (This does not mean `static' in the C sense!) */
13665 TREE_STATIC (decl1) = 1;
13667 /* We must call push_template_decl after current_class_type is set
13668 up. (If we are processing inline definitions after exiting a
13669 class scope, current_class_type will be NULL_TREE until set above
13670 by push_nested_class.) */
13671 if (processing_template_decl)
13673 tree newdecl1 = push_template_decl (decl1);
13674 if (newdecl1 == error_mark_node)
13676 if (ctype || DECL_STATIC_FUNCTION_P (decl1))
13677 pop_nested_class ();
13678 return false;
13680 decl1 = newdecl1;
13683 /* We are now in the scope of the function being defined. */
13684 current_function_decl = decl1;
13686 /* Save the parm names or decls from this function's declarator
13687 where store_parm_decls will find them. */
13688 current_function_parms = DECL_ARGUMENTS (decl1);
13690 /* Make sure the parameter and return types are reasonable. When
13691 you declare a function, these types can be incomplete, but they
13692 must be complete when you define the function. */
13693 check_function_type (decl1, current_function_parms);
13695 /* Build the return declaration for the function. */
13696 restype = TREE_TYPE (fntype);
13698 if (DECL_RESULT (decl1) == NULL_TREE)
13700 tree resdecl;
13702 resdecl = build_decl (input_location, RESULT_DECL, 0, restype);
13703 DECL_ARTIFICIAL (resdecl) = 1;
13704 DECL_IGNORED_P (resdecl) = 1;
13705 DECL_RESULT (decl1) = resdecl;
13707 cp_apply_type_quals_to_decl (cp_type_quals (restype), resdecl);
13710 /* Let the user know we're compiling this function. */
13711 announce_function (decl1);
13713 /* Record the decl so that the function name is defined.
13714 If we already have a decl for this name, and it is a FUNCTION_DECL,
13715 use the old decl. */
13716 if (!processing_template_decl && !(flags & SF_PRE_PARSED))
13718 /* A specialization is not used to guide overload resolution. */
13719 if (!DECL_FUNCTION_MEMBER_P (decl1)
13720 && !(DECL_USE_TEMPLATE (decl1) &&
13721 PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl1))))
13723 tree olddecl = pushdecl (decl1);
13725 if (olddecl == error_mark_node)
13726 /* If something went wrong when registering the declaration,
13727 use DECL1; we have to have a FUNCTION_DECL to use when
13728 parsing the body of the function. */
13730 else
13732 /* Otherwise, OLDDECL is either a previous declaration
13733 of the same function or DECL1 itself. */
13735 if (warn_missing_declarations
13736 && olddecl == decl1
13737 && !DECL_MAIN_P (decl1)
13738 && TREE_PUBLIC (decl1)
13739 && !DECL_DECLARED_INLINE_P (decl1))
13741 tree context;
13743 /* Check whether DECL1 is in an anonymous
13744 namespace. */
13745 for (context = DECL_CONTEXT (decl1);
13746 context;
13747 context = DECL_CONTEXT (context))
13749 if (TREE_CODE (context) == NAMESPACE_DECL
13750 && DECL_NAME (context) == NULL_TREE)
13751 break;
13754 if (context == NULL)
13755 warning (OPT_Wmissing_declarations,
13756 "no previous declaration for %q+D", decl1);
13759 decl1 = olddecl;
13762 else
13764 /* We need to set the DECL_CONTEXT. */
13765 if (!DECL_CONTEXT (decl1) && DECL_TEMPLATE_INFO (decl1))
13766 DECL_CONTEXT (decl1) = DECL_CONTEXT (DECL_TI_TEMPLATE (decl1));
13768 fntype = TREE_TYPE (decl1);
13769 restype = TREE_TYPE (fntype);
13771 /* If #pragma weak applies, mark the decl appropriately now.
13772 The pragma only applies to global functions. Because
13773 determining whether or not the #pragma applies involves
13774 computing the mangled name for the declaration, we cannot
13775 apply the pragma until after we have merged this declaration
13776 with any previous declarations; if the original declaration
13777 has a linkage specification, that specification applies to
13778 the definition as well, and may affect the mangled name. */
13779 if (DECL_FILE_SCOPE_P (decl1))
13780 maybe_apply_pragma_weak (decl1);
13783 /* Reset this in case the call to pushdecl changed it. */
13784 current_function_decl = decl1;
13786 gcc_assert (DECL_INITIAL (decl1));
13788 /* This function may already have been parsed, in which case just
13789 return; our caller will skip over the body without parsing. */
13790 if (DECL_INITIAL (decl1) != error_mark_node)
13791 return true;
13793 /* Initialize RTL machinery. We cannot do this until
13794 CURRENT_FUNCTION_DECL and DECL_RESULT are set up. We do this
13795 even when processing a template; this is how we get
13796 CFUN set up, and our per-function variables initialized.
13797 FIXME factor out the non-RTL stuff. */
13798 bl = current_binding_level;
13799 allocate_struct_function (decl1, processing_template_decl);
13801 /* Initialize the language data structures. Whenever we start
13802 a new function, we destroy temporaries in the usual way. */
13803 cfun->language = ggc_cleared_alloc<language_function> ();
13804 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
13805 current_binding_level = bl;
13807 if (!processing_template_decl && type_uses_auto (restype))
13809 FNDECL_USED_AUTO (decl1) = true;
13810 current_function_auto_return_pattern = restype;
13813 /* Start the statement-tree, start the tree now. */
13814 DECL_SAVED_TREE (decl1) = push_stmt_list ();
13816 /* If we are (erroneously) defining a function that we have already
13817 defined before, wipe out what we knew before. */
13818 if (!DECL_PENDING_INLINE_P (decl1))
13819 DECL_SAVED_FUNCTION_DATA (decl1) = NULL;
13821 if (ctype && !doing_friend && !DECL_STATIC_FUNCTION_P (decl1))
13823 /* We know that this was set up by `grokclassfn'. We do not
13824 wait until `store_parm_decls', since evil parse errors may
13825 never get us to that point. Here we keep the consistency
13826 between `current_class_type' and `current_class_ptr'. */
13827 tree t = DECL_ARGUMENTS (decl1);
13829 gcc_assert (t != NULL_TREE && TREE_CODE (t) == PARM_DECL);
13830 gcc_assert (TYPE_PTR_P (TREE_TYPE (t)));
13832 cp_function_chain->x_current_class_ref
13833 = cp_build_indirect_ref (t, RO_NULL, tf_warning_or_error);
13834 /* Set this second to avoid shortcut in cp_build_indirect_ref. */
13835 cp_function_chain->x_current_class_ptr = t;
13837 /* Constructors and destructors need to know whether they're "in
13838 charge" of initializing virtual base classes. */
13839 t = DECL_CHAIN (t);
13840 if (DECL_HAS_IN_CHARGE_PARM_P (decl1))
13842 current_in_charge_parm = t;
13843 t = DECL_CHAIN (t);
13845 if (DECL_HAS_VTT_PARM_P (decl1))
13847 gcc_assert (DECL_NAME (t) == vtt_parm_identifier);
13848 current_vtt_parm = t;
13852 honor_interface = (!DECL_TEMPLATE_INSTANTIATION (decl1)
13853 /* Implicitly-defined methods (like the
13854 destructor for a class in which no destructor
13855 is explicitly declared) must not be defined
13856 until their definition is needed. So, we
13857 ignore interface specifications for
13858 compiler-generated functions. */
13859 && !DECL_ARTIFICIAL (decl1));
13861 if (processing_template_decl)
13862 /* Don't mess with interface flags. */;
13863 else if (DECL_INTERFACE_KNOWN (decl1))
13865 tree ctx = decl_function_context (decl1);
13867 if (DECL_NOT_REALLY_EXTERN (decl1))
13868 DECL_EXTERNAL (decl1) = 0;
13870 if (ctx != NULL_TREE && vague_linkage_p (ctx))
13871 /* This is a function in a local class in an extern inline
13872 or template function. */
13873 comdat_linkage (decl1);
13875 /* If this function belongs to an interface, it is public.
13876 If it belongs to someone else's interface, it is also external.
13877 This only affects inlines and template instantiations. */
13878 else if (!finfo->interface_unknown && honor_interface)
13880 if (DECL_DECLARED_INLINE_P (decl1)
13881 || DECL_TEMPLATE_INSTANTIATION (decl1))
13883 DECL_EXTERNAL (decl1)
13884 = (finfo->interface_only
13885 || (DECL_DECLARED_INLINE_P (decl1)
13886 && ! flag_implement_inlines
13887 && !DECL_VINDEX (decl1)));
13889 /* For WIN32 we also want to put these in linkonce sections. */
13890 maybe_make_one_only (decl1);
13892 else
13893 DECL_EXTERNAL (decl1) = 0;
13894 DECL_INTERFACE_KNOWN (decl1) = 1;
13895 /* If this function is in an interface implemented in this file,
13896 make sure that the back end knows to emit this function
13897 here. */
13898 if (!DECL_EXTERNAL (decl1))
13899 mark_needed (decl1);
13901 else if (finfo->interface_unknown && finfo->interface_only
13902 && honor_interface)
13904 /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma
13905 interface, we will have both finfo->interface_unknown and
13906 finfo->interface_only set. In that case, we don't want to
13907 use the normal heuristics because someone will supply a
13908 #pragma implementation elsewhere, and deducing it here would
13909 produce a conflict. */
13910 comdat_linkage (decl1);
13911 DECL_EXTERNAL (decl1) = 0;
13912 DECL_INTERFACE_KNOWN (decl1) = 1;
13913 DECL_DEFER_OUTPUT (decl1) = 1;
13915 else
13917 /* This is a definition, not a reference.
13918 So clear DECL_EXTERNAL, unless this is a GNU extern inline. */
13919 if (!GNU_INLINE_P (decl1))
13920 DECL_EXTERNAL (decl1) = 0;
13922 if ((DECL_DECLARED_INLINE_P (decl1)
13923 || DECL_TEMPLATE_INSTANTIATION (decl1))
13924 && ! DECL_INTERFACE_KNOWN (decl1))
13925 DECL_DEFER_OUTPUT (decl1) = 1;
13926 else
13927 DECL_INTERFACE_KNOWN (decl1) = 1;
13930 /* Determine the ELF visibility attribute for the function. We must not
13931 do this before calling "pushdecl", as we must allow "duplicate_decls"
13932 to merge any attributes appropriately. We also need to wait until
13933 linkage is set. */
13934 if (!DECL_CLONED_FUNCTION_P (decl1))
13935 determine_visibility (decl1);
13937 if (!processing_template_decl)
13938 maybe_instantiate_noexcept (decl1);
13940 begin_scope (sk_function_parms, decl1);
13942 ++function_depth;
13944 if (DECL_DESTRUCTOR_P (decl1)
13945 || (DECL_CONSTRUCTOR_P (decl1)
13946 && targetm.cxx.cdtor_returns_this ()))
13948 cdtor_label = create_artificial_label (input_location);
13951 start_fname_decls ();
13953 store_parm_decls (current_function_parms);
13955 return true;
13959 /* Like start_preparsed_function, except that instead of a
13960 FUNCTION_DECL, this function takes DECLSPECS and DECLARATOR.
13962 Returns true on success. If the DECLARATOR is not suitable
13963 for a function, we return false, which tells the parser to
13964 skip the entire function. */
13966 bool
13967 start_function (cp_decl_specifier_seq *declspecs,
13968 const cp_declarator *declarator,
13969 tree attrs)
13971 tree decl1;
13973 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, &attrs);
13974 if (decl1 == error_mark_node)
13975 return false;
13976 /* If the declarator is not suitable for a function definition,
13977 cause a syntax error. */
13978 if (decl1 == NULL_TREE || TREE_CODE (decl1) != FUNCTION_DECL)
13980 error ("invalid function declaration");
13981 return false;
13984 if (DECL_MAIN_P (decl1))
13985 /* main must return int. grokfndecl should have corrected it
13986 (and issued a diagnostic) if the user got it wrong. */
13987 gcc_assert (same_type_p (TREE_TYPE (TREE_TYPE (decl1)),
13988 integer_type_node));
13990 return start_preparsed_function (decl1, attrs, /*flags=*/SF_DEFAULT);
13993 /* Returns true iff an EH_SPEC_BLOCK should be created in the body of
13994 FN. */
13996 static bool
13997 use_eh_spec_block (tree fn)
13999 return (flag_exceptions && flag_enforce_eh_specs
14000 && !processing_template_decl
14001 && !type_throw_all_p (TREE_TYPE (fn))
14002 /* We insert the EH_SPEC_BLOCK only in the original
14003 function; then, it is copied automatically to the
14004 clones. */
14005 && !DECL_CLONED_FUNCTION_P (fn)
14006 /* Implicitly-generated constructors and destructors have
14007 exception specifications. However, those specifications
14008 are the union of the possible exceptions specified by the
14009 constructors/destructors for bases and members, so no
14010 unallowed exception will ever reach this function. By
14011 not creating the EH_SPEC_BLOCK we save a little memory,
14012 and we avoid spurious warnings about unreachable
14013 code. */
14014 && !DECL_DEFAULTED_FN (fn));
14017 /* Store the parameter declarations into the current function declaration.
14018 This is called after parsing the parameter declarations, before
14019 digesting the body of the function.
14021 Also install to binding contour return value identifier, if any. */
14023 static void
14024 store_parm_decls (tree current_function_parms)
14026 tree fndecl = current_function_decl;
14027 tree parm;
14029 /* This is a chain of any other decls that came in among the parm
14030 declarations. If a parm is declared with enum {foo, bar} x;
14031 then CONST_DECLs for foo and bar are put here. */
14032 tree nonparms = NULL_TREE;
14034 if (current_function_parms)
14036 /* This case is when the function was defined with an ANSI prototype.
14037 The parms already have decls, so we need not do anything here
14038 except record them as in effect
14039 and complain if any redundant old-style parm decls were written. */
14041 tree specparms = current_function_parms;
14042 tree next;
14044 /* Must clear this because it might contain TYPE_DECLs declared
14045 at class level. */
14046 current_binding_level->names = NULL;
14048 /* If we're doing semantic analysis, then we'll call pushdecl
14049 for each of these. We must do them in reverse order so that
14050 they end in the correct forward order. */
14051 specparms = nreverse (specparms);
14053 for (parm = specparms; parm; parm = next)
14055 next = DECL_CHAIN (parm);
14056 if (TREE_CODE (parm) == PARM_DECL)
14058 if (DECL_NAME (parm) == NULL_TREE
14059 || !VOID_TYPE_P (parm))
14060 pushdecl (parm);
14061 else
14062 error ("parameter %qD declared void", parm);
14064 else
14066 /* If we find an enum constant or a type tag,
14067 put it aside for the moment. */
14068 TREE_CHAIN (parm) = NULL_TREE;
14069 nonparms = chainon (nonparms, parm);
14073 /* Get the decls in their original chain order and record in the
14074 function. This is all and only the PARM_DECLs that were
14075 pushed into scope by the loop above. */
14076 DECL_ARGUMENTS (fndecl) = getdecls ();
14078 else
14079 DECL_ARGUMENTS (fndecl) = NULL_TREE;
14081 /* Now store the final chain of decls for the arguments
14082 as the decl-chain of the current lexical scope.
14083 Put the enumerators in as well, at the front so that
14084 DECL_ARGUMENTS is not modified. */
14085 current_binding_level->names = chainon (nonparms, DECL_ARGUMENTS (fndecl));
14087 if (use_eh_spec_block (current_function_decl))
14088 current_eh_spec_block = begin_eh_spec_block ();
14091 // Bring the parameters of a function declaration back into
14092 // scope without entering the function body. The declarator
14093 // must be a function declarator. The caller is responsible
14094 // for calling finish_scope.
14095 void
14096 push_function_parms (cp_declarator *declarator)
14098 // Find the actual function declarator.
14099 while (declarator)
14101 if (declarator->kind == cdk_function)
14102 break;
14103 declarator = declarator->declarator;
14106 begin_scope (sk_function_parms, NULL_TREE);
14107 tree p = declarator->u.function.parameters;
14108 while (p != NULL_TREE && !VOID_TYPE_P (TREE_VALUE (p)))
14110 pushdecl (TREE_VALUE (p));
14111 p = TREE_CHAIN (p);
14116 /* We have finished doing semantic analysis on DECL, but have not yet
14117 generated RTL for its body. Save away our current state, so that
14118 when we want to generate RTL later we know what to do. */
14120 static void
14121 save_function_data (tree decl)
14123 struct language_function *f;
14125 /* Save the language-specific per-function data so that we can
14126 get it back when we really expand this function. */
14127 gcc_assert (!DECL_PENDING_INLINE_P (decl));
14129 /* Make a copy. */
14130 f = ggc_alloc<language_function> ();
14131 memcpy (f, cp_function_chain, sizeof (struct language_function));
14132 DECL_SAVED_FUNCTION_DATA (decl) = f;
14134 /* Clear out the bits we don't need. */
14135 f->base.x_stmt_tree.x_cur_stmt_list = NULL;
14136 f->bindings = NULL;
14137 f->x_local_names = NULL;
14138 f->base.local_typedefs = NULL;
14142 /* Set the return value of the constructor (if present). */
14144 static void
14145 finish_constructor_body (void)
14147 tree val;
14148 tree exprstmt;
14150 if (targetm.cxx.cdtor_returns_this ()
14151 && (! TYPE_FOR_JAVA (current_class_type)))
14153 /* Any return from a constructor will end up here. */
14154 add_stmt (build_stmt (input_location, LABEL_EXPR, cdtor_label));
14156 val = DECL_ARGUMENTS (current_function_decl);
14157 val = build2 (MODIFY_EXPR, TREE_TYPE (val),
14158 DECL_RESULT (current_function_decl), val);
14159 /* Return the address of the object. */
14160 exprstmt = build_stmt (input_location, RETURN_EXPR, val);
14161 add_stmt (exprstmt);
14165 /* Do all the processing for the beginning of a destructor; set up the
14166 vtable pointers and cleanups for bases and members. */
14168 static void
14169 begin_destructor_body (void)
14171 tree compound_stmt;
14173 /* If the CURRENT_CLASS_TYPE is incomplete, we will have already
14174 issued an error message. We still want to try to process the
14175 body of the function, but initialize_vtbl_ptrs will crash if
14176 TYPE_BINFO is NULL. */
14177 if (COMPLETE_TYPE_P (current_class_type))
14179 compound_stmt = begin_compound_stmt (0);
14180 /* Make all virtual function table pointers in non-virtual base
14181 classes point to CURRENT_CLASS_TYPE's virtual function
14182 tables. */
14183 initialize_vtbl_ptrs (current_class_ptr);
14184 finish_compound_stmt (compound_stmt);
14186 if (flag_lifetime_dse)
14188 /* Insert a cleanup to let the back end know that the object is dead
14189 when we exit the destructor, either normally or via exception. */
14190 tree btype = CLASSTYPE_AS_BASE (current_class_type);
14191 tree clobber = build_constructor (btype, NULL);
14192 TREE_THIS_VOLATILE (clobber) = true;
14193 tree bref = build_nop (build_reference_type (btype),
14194 current_class_ptr);
14195 bref = convert_from_reference (bref);
14196 tree exprstmt = build2 (MODIFY_EXPR, btype, bref, clobber);
14197 finish_decl_cleanup (NULL_TREE, exprstmt);
14200 /* And insert cleanups for our bases and members so that they
14201 will be properly destroyed if we throw. */
14202 push_base_cleanups ();
14206 /* At the end of every destructor we generate code to delete the object if
14207 necessary. Do that now. */
14209 static void
14210 finish_destructor_body (void)
14212 tree exprstmt;
14214 /* Any return from a destructor will end up here; that way all base
14215 and member cleanups will be run when the function returns. */
14216 add_stmt (build_stmt (input_location, LABEL_EXPR, cdtor_label));
14218 /* In a virtual destructor, we must call delete. */
14219 if (DECL_VIRTUAL_P (current_function_decl))
14221 tree if_stmt;
14222 tree virtual_size = cxx_sizeof (current_class_type);
14224 /* [class.dtor]
14226 At the point of definition of a virtual destructor (including
14227 an implicit definition), non-placement operator delete shall
14228 be looked up in the scope of the destructor's class and if
14229 found shall be accessible and unambiguous. */
14230 exprstmt = build_op_delete_call (DELETE_EXPR, current_class_ptr,
14231 virtual_size,
14232 /*global_p=*/false,
14233 /*placement=*/NULL_TREE,
14234 /*alloc_fn=*/NULL_TREE,
14235 tf_warning_or_error);
14237 if_stmt = begin_if_stmt ();
14238 finish_if_stmt_cond (build2 (BIT_AND_EXPR, integer_type_node,
14239 current_in_charge_parm,
14240 integer_one_node),
14241 if_stmt);
14242 finish_expr_stmt (exprstmt);
14243 finish_then_clause (if_stmt);
14244 finish_if_stmt (if_stmt);
14247 if (targetm.cxx.cdtor_returns_this ())
14249 tree val;
14251 val = DECL_ARGUMENTS (current_function_decl);
14252 val = build2 (MODIFY_EXPR, TREE_TYPE (val),
14253 DECL_RESULT (current_function_decl), val);
14254 /* Return the address of the object. */
14255 exprstmt = build_stmt (input_location, RETURN_EXPR, val);
14256 add_stmt (exprstmt);
14260 /* Do the necessary processing for the beginning of a function body, which
14261 in this case includes member-initializers, but not the catch clauses of
14262 a function-try-block. Currently, this means opening a binding level
14263 for the member-initializers (in a ctor), member cleanups (in a dtor),
14264 and capture proxies (in a lambda operator()). */
14266 tree
14267 begin_function_body (void)
14269 tree stmt;
14271 if (! FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
14272 return NULL_TREE;
14274 if (processing_template_decl)
14275 /* Do nothing now. */;
14276 else
14277 /* Always keep the BLOCK node associated with the outermost pair of
14278 curly braces of a function. These are needed for correct
14279 operation of dwarfout.c. */
14280 keep_next_level (true);
14282 stmt = begin_compound_stmt (BCS_FN_BODY);
14284 if (processing_template_decl)
14285 /* Do nothing now. */;
14286 else if (DECL_DESTRUCTOR_P (current_function_decl))
14287 begin_destructor_body ();
14289 return stmt;
14292 /* Do the processing for the end of a function body. Currently, this means
14293 closing out the cleanups for fully-constructed bases and members, and in
14294 the case of the destructor, deleting the object if desired. Again, this
14295 is only meaningful for [cd]tors, since they are the only functions where
14296 there is a significant distinction between the main body and any
14297 function catch clauses. Handling, say, main() return semantics here
14298 would be wrong, as flowing off the end of a function catch clause for
14299 main() would also need to return 0. */
14301 void
14302 finish_function_body (tree compstmt)
14304 if (compstmt == NULL_TREE)
14305 return;
14307 /* Close the block. */
14308 finish_compound_stmt (compstmt);
14310 if (processing_template_decl)
14311 /* Do nothing now. */;
14312 else if (DECL_CONSTRUCTOR_P (current_function_decl))
14313 finish_constructor_body ();
14314 else if (DECL_DESTRUCTOR_P (current_function_decl))
14315 finish_destructor_body ();
14318 /* Given a function, returns the BLOCK corresponding to the outermost level
14319 of curly braces, skipping the artificial block created for constructor
14320 initializers. */
14322 tree
14323 outer_curly_brace_block (tree fndecl)
14325 tree block = DECL_INITIAL (fndecl);
14326 if (BLOCK_OUTER_CURLY_BRACE_P (block))
14327 return block;
14328 block = BLOCK_SUBBLOCKS (block);
14329 if (BLOCK_OUTER_CURLY_BRACE_P (block))
14330 return block;
14331 block = BLOCK_SUBBLOCKS (block);
14332 gcc_assert (BLOCK_OUTER_CURLY_BRACE_P (block));
14333 return block;
14336 /* If FNDECL is a class's key method, add the class to the list of
14337 keyed classes that should be emitted. */
14339 static void
14340 record_key_method_defined (tree fndecl)
14342 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fndecl)
14343 && DECL_VIRTUAL_P (fndecl)
14344 && !processing_template_decl)
14346 tree fnclass = DECL_CONTEXT (fndecl);
14347 if (fndecl == CLASSTYPE_KEY_METHOD (fnclass))
14348 keyed_classes = tree_cons (NULL_TREE, fnclass, keyed_classes);
14352 /* Subroutine of finish_function.
14353 Save the body of constexpr functions for possible
14354 future compile time evaluation. */
14356 static void
14357 maybe_save_function_definition (tree fun)
14359 if (!processing_template_decl
14360 && DECL_DECLARED_CONSTEXPR_P (fun)
14361 && !cp_function_chain->invalid_constexpr
14362 && !DECL_CLONED_FUNCTION_P (fun))
14363 register_constexpr_fundef (fun, DECL_SAVED_TREE (fun));
14366 /* Finish up a function declaration and compile that function
14367 all the way to assembler language output. The free the storage
14368 for the function definition.
14370 FLAGS is a bitwise or of the following values:
14371 2 - INCLASS_INLINE
14372 We just finished processing the body of an in-class inline
14373 function definition. (This processing will have taken place
14374 after the class definition is complete.) */
14376 tree
14377 finish_function (int flags)
14379 tree fndecl = current_function_decl;
14380 tree fntype, ctype = NULL_TREE;
14381 int inclass_inline = (flags & 2) != 0;
14383 /* When we get some parse errors, we can end up without a
14384 current_function_decl, so cope. */
14385 if (fndecl == NULL_TREE)
14386 return error_mark_node;
14388 if (c_dialect_objc ())
14389 objc_finish_function ();
14391 gcc_assert (!defer_mark_used_calls);
14392 defer_mark_used_calls = true;
14394 record_key_method_defined (fndecl);
14396 fntype = TREE_TYPE (fndecl);
14398 /* TREE_READONLY (fndecl) = 1;
14399 This caused &foo to be of type ptr-to-const-function
14400 which then got a warning when stored in a ptr-to-function variable. */
14402 gcc_assert (building_stmt_list_p ());
14403 /* The current function is being defined, so its DECL_INITIAL should
14404 be set, and unless there's a multiple definition, it should be
14405 error_mark_node. */
14406 gcc_assert (DECL_INITIAL (fndecl) == error_mark_node);
14408 /* For a cloned function, we've already got all the code we need;
14409 there's no need to add any extra bits. */
14410 if (!DECL_CLONED_FUNCTION_P (fndecl))
14412 /* Make it so that `main' always returns 0 by default. */
14413 if (DECL_MAIN_P (current_function_decl))
14414 finish_return_stmt (integer_zero_node);
14416 if (use_eh_spec_block (current_function_decl))
14417 finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
14418 (TREE_TYPE (current_function_decl)),
14419 current_eh_spec_block);
14422 /* If we're saving up tree structure, tie off the function now. */
14423 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
14425 if (fn_contains_cilk_spawn_p (cfun) && !processing_template_decl)
14426 cfun->cilk_frame_decl = insert_cilk_frame (fndecl);
14428 finish_fname_decls ();
14430 /* If this function can't throw any exceptions, remember that. */
14431 if (!processing_template_decl
14432 && !cp_function_chain->can_throw
14433 && !flag_non_call_exceptions
14434 && !decl_replaceable_p (fndecl))
14435 TREE_NOTHROW (fndecl) = 1;
14437 /* This must come after expand_function_end because cleanups might
14438 have declarations (from inline functions) that need to go into
14439 this function's blocks. */
14441 /* If the current binding level isn't the outermost binding level
14442 for this function, either there is a bug, or we have experienced
14443 syntax errors and the statement tree is malformed. */
14444 if (current_binding_level->kind != sk_function_parms)
14446 /* Make sure we have already experienced errors. */
14447 gcc_assert (errorcount);
14449 /* Throw away the broken statement tree and extra binding
14450 levels. */
14451 DECL_SAVED_TREE (fndecl) = alloc_stmt_list ();
14453 while (current_binding_level->kind != sk_function_parms)
14455 if (current_binding_level->kind == sk_class)
14456 pop_nested_class ();
14457 else
14458 poplevel (0, 0, 0);
14461 poplevel (1, 0, 1);
14463 /* Statements should always be full-expressions at the outermost set
14464 of curly braces for a function. */
14465 gcc_assert (stmts_are_full_exprs_p ());
14467 /* If there are no return statements in a function with auto return type,
14468 the return type is void. But if the declared type is something like
14469 auto*, this is an error. */
14470 if (!processing_template_decl && FNDECL_USED_AUTO (fndecl)
14471 && TREE_TYPE (fntype) == current_function_auto_return_pattern)
14473 if (!is_auto (current_function_auto_return_pattern)
14474 && !current_function_returns_value && !current_function_returns_null)
14476 error ("no return statements in function returning %qT",
14477 current_function_auto_return_pattern);
14478 inform (input_location, "only plain %<auto%> return type can be "
14479 "deduced to %<void%>");
14481 apply_deduced_return_type (fndecl, void_type_node);
14482 fntype = TREE_TYPE (fndecl);
14485 // If this is a concept, check that the definition is reasonable.
14486 if (DECL_DECLARED_CONCEPT_P (fndecl))
14487 check_function_concept (fndecl);
14489 /* Save constexpr function body before it gets munged by
14490 the NRV transformation. */
14491 maybe_save_function_definition (fndecl);
14493 /* Set up the named return value optimization, if we can. Candidate
14494 variables are selected in check_return_expr. */
14495 if (current_function_return_value)
14497 tree r = current_function_return_value;
14498 tree outer;
14500 if (r != error_mark_node
14501 /* This is only worth doing for fns that return in memory--and
14502 simpler, since we don't have to worry about promoted modes. */
14503 && aggregate_value_p (TREE_TYPE (TREE_TYPE (fndecl)), fndecl)
14504 /* Only allow this for variables declared in the outer scope of
14505 the function so we know that their lifetime always ends with a
14506 return; see g++.dg/opt/nrv6.C. We could be more flexible if
14507 we were to do this optimization in tree-ssa. */
14508 && (outer = outer_curly_brace_block (fndecl))
14509 && chain_member (r, BLOCK_VARS (outer)))
14510 finalize_nrv (&DECL_SAVED_TREE (fndecl), r, DECL_RESULT (fndecl));
14512 current_function_return_value = NULL_TREE;
14515 /* Remember that we were in class scope. */
14516 if (current_class_name)
14517 ctype = current_class_type;
14519 /* Must mark the RESULT_DECL as being in this function. */
14520 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
14522 /* Set the BLOCK_SUPERCONTEXT of the outermost function scope to point
14523 to the FUNCTION_DECL node itself. */
14524 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
14526 /* Save away current state, if appropriate. */
14527 if (!processing_template_decl)
14528 save_function_data (fndecl);
14530 /* Complain if there's just no return statement. */
14531 if (warn_return_type
14532 && !VOID_TYPE_P (TREE_TYPE (fntype))
14533 && !dependent_type_p (TREE_TYPE (fntype))
14534 && !current_function_returns_value && !current_function_returns_null
14535 /* Don't complain if we abort or throw. */
14536 && !current_function_returns_abnormally
14537 /* Don't complain if there's an infinite loop. */
14538 && !current_function_infinite_loop
14539 /* Don't complain if we are declared noreturn. */
14540 && !TREE_THIS_VOLATILE (fndecl)
14541 && !DECL_NAME (DECL_RESULT (fndecl))
14542 && !TREE_NO_WARNING (fndecl)
14543 /* Structor return values (if any) are set by the compiler. */
14544 && !DECL_CONSTRUCTOR_P (fndecl)
14545 && !DECL_DESTRUCTOR_P (fndecl)
14546 && targetm.warn_func_return (fndecl))
14548 warning (OPT_Wreturn_type,
14549 "no return statement in function returning non-void");
14550 TREE_NO_WARNING (fndecl) = 1;
14553 /* Store the end of the function, so that we get good line number
14554 info for the epilogue. */
14555 cfun->function_end_locus = input_location;
14557 /* Complain about parameters that are only set, but never otherwise used. */
14558 if (warn_unused_but_set_parameter
14559 && !processing_template_decl
14560 && errorcount == unused_but_set_errorcount
14561 && !DECL_CLONED_FUNCTION_P (fndecl))
14563 tree decl;
14565 for (decl = DECL_ARGUMENTS (fndecl);
14566 decl;
14567 decl = DECL_CHAIN (decl))
14568 if (TREE_USED (decl)
14569 && TREE_CODE (decl) == PARM_DECL
14570 && !DECL_READ_P (decl)
14571 && DECL_NAME (decl)
14572 && !DECL_ARTIFICIAL (decl)
14573 && !TREE_NO_WARNING (decl)
14574 && !DECL_IN_SYSTEM_HEADER (decl)
14575 && TREE_TYPE (decl) != error_mark_node
14576 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
14577 && (!CLASS_TYPE_P (TREE_TYPE (decl))
14578 || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl))))
14579 warning (OPT_Wunused_but_set_parameter,
14580 "parameter %q+D set but not used", decl);
14581 unused_but_set_errorcount = errorcount;
14584 /* Complain about locally defined typedefs that are not used in this
14585 function. */
14586 maybe_warn_unused_local_typedefs ();
14588 /* Genericize before inlining. */
14589 if (!processing_template_decl)
14591 struct language_function *f = DECL_SAVED_FUNCTION_DATA (fndecl);
14592 invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE, fndecl);
14593 cp_genericize (fndecl);
14594 /* Clear out the bits we don't need. */
14595 f->x_current_class_ptr = NULL;
14596 f->x_current_class_ref = NULL;
14597 f->x_eh_spec_block = NULL;
14598 f->x_in_charge_parm = NULL;
14599 f->x_vtt_parm = NULL;
14600 f->x_return_value = NULL;
14601 f->bindings = NULL;
14602 f->extern_decl_map = NULL;
14603 f->infinite_loops = NULL;
14605 /* Clear out the bits we don't need. */
14606 local_names = NULL;
14608 /* We're leaving the context of this function, so zap cfun. It's still in
14609 DECL_STRUCT_FUNCTION, and we'll restore it in tree_rest_of_compilation. */
14610 set_cfun (NULL);
14611 current_function_decl = NULL;
14613 /* If this is an in-class inline definition, we may have to pop the
14614 bindings for the template parameters that we added in
14615 maybe_begin_member_template_processing when start_function was
14616 called. */
14617 if (inclass_inline)
14618 maybe_end_member_template_processing ();
14620 /* Leave the scope of the class. */
14621 if (ctype)
14622 pop_nested_class ();
14624 --function_depth;
14626 /* Clean up. */
14627 current_function_decl = NULL_TREE;
14629 defer_mark_used_calls = false;
14630 if (deferred_mark_used_calls)
14632 unsigned int i;
14633 tree decl;
14635 FOR_EACH_VEC_SAFE_ELT (deferred_mark_used_calls, i, decl)
14636 mark_used (decl);
14637 vec_free (deferred_mark_used_calls);
14640 return fndecl;
14643 /* Create the FUNCTION_DECL for a function definition.
14644 DECLSPECS and DECLARATOR are the parts of the declaration;
14645 they describe the return type and the name of the function,
14646 but twisted together in a fashion that parallels the syntax of C.
14648 This function creates a binding context for the function body
14649 as well as setting up the FUNCTION_DECL in current_function_decl.
14651 Returns a FUNCTION_DECL on success.
14653 If the DECLARATOR is not suitable for a function (it defines a datum
14654 instead), we return 0, which tells yyparse to report a parse error.
14656 May return void_type_node indicating that this method is actually
14657 a friend. See grokfield for more details.
14659 Came here with a `.pushlevel' .
14661 DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
14662 CHANGES TO CODE IN `grokfield'. */
14664 tree
14665 grokmethod (cp_decl_specifier_seq *declspecs,
14666 const cp_declarator *declarator, tree attrlist)
14668 tree fndecl = grokdeclarator (declarator, declspecs, MEMFUNCDEF, 0,
14669 &attrlist);
14671 if (fndecl == error_mark_node)
14672 return error_mark_node;
14674 if (fndecl == NULL || TREE_CODE (fndecl) != FUNCTION_DECL)
14676 error ("invalid member function declaration");
14677 return error_mark_node;
14680 if (attrlist)
14681 cplus_decl_attributes (&fndecl, attrlist, 0);
14683 /* Pass friends other than inline friend functions back. */
14684 if (fndecl == void_type_node)
14685 return fndecl;
14687 if (DECL_IN_AGGR_P (fndecl))
14689 if (DECL_CLASS_SCOPE_P (fndecl))
14690 error ("%qD is already defined in class %qT", fndecl,
14691 DECL_CONTEXT (fndecl));
14692 return error_mark_node;
14695 check_template_shadow (fndecl);
14697 DECL_COMDAT (fndecl) = 1;
14698 DECL_DECLARED_INLINE_P (fndecl) = 1;
14699 DECL_NO_INLINE_WARNING_P (fndecl) = 1;
14701 /* We process method specializations in finish_struct_1. */
14702 if (processing_template_decl && !DECL_TEMPLATE_SPECIALIZATION (fndecl))
14704 fndecl = push_template_decl (fndecl);
14705 if (fndecl == error_mark_node)
14706 return fndecl;
14709 if (! DECL_FRIEND_P (fndecl))
14711 if (DECL_CHAIN (fndecl))
14713 fndecl = copy_node (fndecl);
14714 TREE_CHAIN (fndecl) = NULL_TREE;
14718 cp_finish_decl (fndecl, NULL_TREE, false, NULL_TREE, 0);
14720 DECL_IN_AGGR_P (fndecl) = 1;
14721 return fndecl;
14725 /* VAR is a VAR_DECL. If its type is incomplete, remember VAR so that
14726 we can lay it out later, when and if its type becomes complete.
14728 Also handle constexpr variables where the initializer involves
14729 an unlowered PTRMEM_CST because the class isn't complete yet. */
14731 void
14732 maybe_register_incomplete_var (tree var)
14734 gcc_assert (VAR_P (var));
14736 /* Keep track of variables with incomplete types. */
14737 if (!processing_template_decl && TREE_TYPE (var) != error_mark_node
14738 && DECL_EXTERNAL (var))
14740 tree inner_type = TREE_TYPE (var);
14742 while (TREE_CODE (inner_type) == ARRAY_TYPE)
14743 inner_type = TREE_TYPE (inner_type);
14744 inner_type = TYPE_MAIN_VARIANT (inner_type);
14746 if ((!COMPLETE_TYPE_P (inner_type) && CLASS_TYPE_P (inner_type))
14747 /* RTTI TD entries are created while defining the type_info. */
14748 || (TYPE_LANG_SPECIFIC (inner_type)
14749 && TYPE_BEING_DEFINED (inner_type)))
14751 incomplete_var iv = {var, inner_type};
14752 vec_safe_push (incomplete_vars, iv);
14754 else if (!(DECL_LANG_SPECIFIC (var) && DECL_TEMPLATE_INFO (var))
14755 && decl_constant_var_p (var)
14756 && (TYPE_PTRMEM_P (inner_type) || CLASS_TYPE_P (inner_type)))
14758 /* When the outermost open class is complete we can resolve any
14759 pointers-to-members. */
14760 tree context = outermost_open_class ();
14761 incomplete_var iv = {var, context};
14762 vec_safe_push (incomplete_vars, iv);
14767 /* Called when a class type (given by TYPE) is defined. If there are
14768 any existing VAR_DECLs whose type has been completed by this
14769 declaration, update them now. */
14771 void
14772 complete_vars (tree type)
14774 unsigned ix;
14775 incomplete_var *iv;
14777 for (ix = 0; vec_safe_iterate (incomplete_vars, ix, &iv); )
14779 if (same_type_p (type, iv->incomplete_type))
14781 tree var = iv->decl;
14782 tree type = TREE_TYPE (var);
14784 if (TYPE_MAIN_VARIANT (strip_array_types (type))
14785 == iv->incomplete_type)
14787 /* Complete the type of the variable. The VAR_DECL itself
14788 will be laid out in expand_expr. */
14789 complete_type (type);
14790 cp_apply_type_quals_to_decl (cp_type_quals (type), var);
14793 if (DECL_INITIAL (var)
14794 && decl_constant_var_p (var))
14795 DECL_INITIAL (var) = cplus_expand_constant (DECL_INITIAL (var));
14797 /* Remove this entry from the list. */
14798 incomplete_vars->unordered_remove (ix);
14800 else
14801 ix++;
14804 /* Check for pending declarations which may have abstract type. */
14805 complete_type_check_abstract (type);
14808 /* If DECL is of a type which needs a cleanup, build and return an
14809 expression to perform that cleanup here. Return NULL_TREE if no
14810 cleanup need be done. */
14812 tree
14813 cxx_maybe_build_cleanup (tree decl, tsubst_flags_t complain)
14815 tree type;
14816 tree attr;
14817 tree cleanup;
14819 /* Assume no cleanup is required. */
14820 cleanup = NULL_TREE;
14822 if (error_operand_p (decl))
14823 return cleanup;
14825 /* Handle "__attribute__((cleanup))". We run the cleanup function
14826 before the destructor since the destructor is what actually
14827 terminates the lifetime of the object. */
14828 attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
14829 if (attr)
14831 tree id;
14832 tree fn;
14833 tree arg;
14835 /* Get the name specified by the user for the cleanup function. */
14836 id = TREE_VALUE (TREE_VALUE (attr));
14837 /* Look up the name to find the cleanup function to call. It is
14838 important to use lookup_name here because that is what is
14839 used in c-common.c:handle_cleanup_attribute when performing
14840 initial checks on the attribute. Note that those checks
14841 include ensuring that the function found is not an overloaded
14842 function, or an object with an overloaded call operator,
14843 etc.; we can rely on the fact that the function found is an
14844 ordinary FUNCTION_DECL. */
14845 fn = lookup_name (id);
14846 arg = build_address (decl);
14847 mark_used (decl);
14848 cleanup = cp_build_function_call_nary (fn, complain, arg, NULL_TREE);
14849 if (cleanup == error_mark_node)
14850 return error_mark_node;
14852 /* Handle ordinary C++ destructors. */
14853 type = TREE_TYPE (decl);
14854 if (type_build_dtor_call (type))
14856 int flags = LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR;
14857 tree addr;
14858 tree call;
14860 if (TREE_CODE (type) == ARRAY_TYPE)
14861 addr = decl;
14862 else
14863 addr = build_address (decl);
14865 call = build_delete (TREE_TYPE (addr), addr,
14866 sfk_complete_destructor, flags, 0, complain);
14867 if (call == error_mark_node)
14868 cleanup = error_mark_node;
14869 else if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
14870 /* Discard the call. */;
14871 else if (cleanup)
14872 cleanup = cp_build_compound_expr (cleanup, call, complain);
14873 else
14874 cleanup = call;
14877 /* build_delete sets the location of the destructor call to the
14878 current location, even though the destructor is going to be
14879 called later, at the end of the current scope. This can lead to
14880 a "jumpy" behaviour for users of debuggers when they step around
14881 the end of the block. So let's unset the location of the
14882 destructor call instead. */
14883 if (cleanup != NULL && EXPR_P (cleanup))
14884 SET_EXPR_LOCATION (cleanup, UNKNOWN_LOCATION);
14886 if (cleanup
14887 && !lookup_attribute ("warn_unused", TYPE_ATTRIBUTES (TREE_TYPE (decl))))
14888 /* Treat objects with destructors as used; the destructor may do
14889 something substantive. */
14890 mark_used (decl);
14892 return cleanup;
14896 /* Return the FUNCTION_TYPE that corresponds to MEMFNTYPE, which can be a
14897 FUNCTION_DECL, METHOD_TYPE, FUNCTION_TYPE, pointer or reference to
14898 METHOD_TYPE or FUNCTION_TYPE, or pointer to member function. */
14900 tree
14901 static_fn_type (tree memfntype)
14903 tree fntype;
14904 tree args;
14906 if (TYPE_PTRMEMFUNC_P (memfntype))
14907 memfntype = TYPE_PTRMEMFUNC_FN_TYPE (memfntype);
14908 if (POINTER_TYPE_P (memfntype)
14909 || TREE_CODE (memfntype) == FUNCTION_DECL)
14910 memfntype = TREE_TYPE (memfntype);
14911 if (TREE_CODE (memfntype) == FUNCTION_TYPE)
14912 return memfntype;
14913 gcc_assert (TREE_CODE (memfntype) == METHOD_TYPE);
14914 args = TYPE_ARG_TYPES (memfntype);
14915 cp_ref_qualifier rqual = type_memfn_rqual (memfntype);
14916 fntype = build_function_type (TREE_TYPE (memfntype), TREE_CHAIN (args));
14917 fntype = apply_memfn_quals (fntype, type_memfn_quals (memfntype), rqual);
14918 fntype = (cp_build_type_attribute_variant
14919 (fntype, TYPE_ATTRIBUTES (memfntype)));
14920 fntype = (build_exception_variant
14921 (fntype, TYPE_RAISES_EXCEPTIONS (memfntype)));
14922 if (TYPE_HAS_LATE_RETURN_TYPE (memfntype))
14923 TYPE_HAS_LATE_RETURN_TYPE (fntype) = 1;
14924 return fntype;
14927 /* DECL was originally constructed as a non-static member function,
14928 but turned out to be static. Update it accordingly. */
14930 void
14931 revert_static_member_fn (tree decl)
14933 tree stype = static_fn_type (decl);
14934 cp_cv_quals quals = type_memfn_quals (stype);
14935 cp_ref_qualifier rqual = type_memfn_rqual (stype);
14937 if (quals != TYPE_UNQUALIFIED || rqual != REF_QUAL_NONE)
14938 stype = apply_memfn_quals (stype, TYPE_UNQUALIFIED, REF_QUAL_NONE);
14940 TREE_TYPE (decl) = stype;
14942 if (DECL_ARGUMENTS (decl))
14943 DECL_ARGUMENTS (decl) = DECL_CHAIN (DECL_ARGUMENTS (decl));
14944 DECL_STATIC_FUNCTION_P (decl) = 1;
14947 /* Return which tree structure is used by T, or TS_CP_GENERIC if T is
14948 one of the language-independent trees. */
14950 enum cp_tree_node_structure_enum
14951 cp_tree_node_structure (union lang_tree_node * t)
14953 switch (TREE_CODE (&t->generic))
14955 case DEFAULT_ARG: return TS_CP_DEFAULT_ARG;
14956 case DEFERRED_NOEXCEPT: return TS_CP_DEFERRED_NOEXCEPT;
14957 case IDENTIFIER_NODE: return TS_CP_IDENTIFIER;
14958 case OVERLOAD: return TS_CP_OVERLOAD;
14959 case TEMPLATE_PARM_INDEX: return TS_CP_TPI;
14960 case PTRMEM_CST: return TS_CP_PTRMEM;
14961 case BASELINK: return TS_CP_BASELINK;
14962 case TEMPLATE_DECL: return TS_CP_TEMPLATE_DECL;
14963 case STATIC_ASSERT: return TS_CP_STATIC_ASSERT;
14964 case ARGUMENT_PACK_SELECT: return TS_CP_ARGUMENT_PACK_SELECT;
14965 case TRAIT_EXPR: return TS_CP_TRAIT_EXPR;
14966 case LAMBDA_EXPR: return TS_CP_LAMBDA_EXPR;
14967 case TEMPLATE_INFO: return TS_CP_TEMPLATE_INFO;
14968 case CONSTRAINT_INFO: return TS_CP_CONSTRAINT_INFO;
14969 case USERDEF_LITERAL: return TS_CP_USERDEF_LITERAL;
14970 default: return TS_CP_GENERIC;
14974 /* Build the void_list_node (void_type_node having been created). */
14975 tree
14976 build_void_list_node (void)
14978 tree t = build_tree_list (NULL_TREE, void_type_node);
14979 return t;
14982 bool
14983 cp_missing_noreturn_ok_p (tree decl)
14985 /* A missing noreturn is ok for the `main' function. */
14986 return DECL_MAIN_P (decl);
14989 /* Return the decl used to identify the COMDAT group into which DECL should
14990 be placed. */
14992 tree
14993 cxx_comdat_group (tree decl)
14995 /* Virtual tables, construction virtual tables, and virtual table
14996 tables all go in a single COMDAT group, named after the primary
14997 virtual table. */
14998 if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl))
14999 decl = CLASSTYPE_VTABLES (DECL_CONTEXT (decl));
15000 /* For all other DECLs, the COMDAT group is the mangled name of the
15001 declaration itself. */
15002 else
15004 while (DECL_THUNK_P (decl))
15006 /* If TARGET_USE_LOCAL_THUNK_ALIAS_P, use_thunk puts the thunk
15007 into the same section as the target function. In that case
15008 we must return target's name. */
15009 tree target = THUNK_TARGET (decl);
15010 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (target)
15011 && DECL_SECTION_NAME (target) != NULL
15012 && DECL_ONE_ONLY (target))
15013 decl = target;
15014 else
15015 break;
15019 return decl;
15022 /* Returns the return type for FN as written by the user, which may include
15023 a placeholder for a deduced return type. */
15025 tree
15026 fndecl_declared_return_type (tree fn)
15028 fn = STRIP_TEMPLATE (fn);
15029 if (FNDECL_USED_AUTO (fn))
15031 struct language_function *f = NULL;
15032 if (DECL_STRUCT_FUNCTION (fn))
15033 f = DECL_STRUCT_FUNCTION (fn)->language;
15034 if (f == NULL)
15035 f = DECL_SAVED_FUNCTION_DATA (fn);
15036 return f->x_auto_return_pattern;
15038 return TREE_TYPE (TREE_TYPE (fn));
15041 /* Returns true iff DECL was declared with an auto return type and it has
15042 not yet been deduced to a real type. */
15044 bool
15045 undeduced_auto_decl (tree decl)
15047 if (cxx_dialect < cxx14)
15048 return false;
15049 return type_uses_auto (TREE_TYPE (decl));
15052 /* Complain if DECL has an undeduced return type. */
15054 void
15055 require_deduced_type (tree decl)
15057 if (undeduced_auto_decl (decl))
15058 error ("use of %qD before deduction of %<auto%>", decl);
15061 #include "gt-cp-decl.h"