Merge from trunk:
[official-gcc.git] / main / gcc / cp / decl.c
blobc8dc6c5bf024b5d426f16234fd65adecc8ae48ff
1 /* Process declarations and variables for C++ compiler.
2 Copyright (C) 1988-2014 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 "tree.h"
34 #include "stringpool.h"
35 #include "stor-layout.h"
36 #include "varasm.h"
37 #include "attribs.h"
38 #include "calls.h"
39 #include "flags.h"
40 #include "cp-tree.h"
41 #include "tree-iterator.h"
42 #include "tree-inline.h"
43 #include "decl.h"
44 #include "intl.h"
45 #include "toplev.h"
46 #include "hashtab.h"
47 #include "tm_p.h"
48 #include "target.h"
49 #include "c-family/c-common.h"
50 #include "c-family/c-objc.h"
51 #include "c-family/c-pragma.h"
52 #include "c-family/c-target.h"
53 #include "c-family/c-ubsan.h"
54 #include "diagnostic.h"
55 #include "intl.h"
56 #include "debug.h"
57 #include "timevar.h"
58 #include "pointer-set.h"
59 #include "splay-tree.h"
60 #include "cgraph.h"
61 #include "plugin.h"
62 #include "cgraph.h"
63 #include "cilk.h"
64 #include "wide-int.h"
65 #include "builtins.h"
67 /* Possible cases of bad specifiers type used by bad_specifiers. */
68 enum bad_spec_place {
69 BSP_VAR, /* variable */
70 BSP_PARM, /* parameter */
71 BSP_TYPE, /* type */
72 BSP_FIELD /* field */
75 static tree grokparms (tree parmlist, tree *);
76 static const char *redeclaration_error_message (tree, tree);
78 static int decl_jump_unsafe (tree);
79 static void require_complete_types_for_parms (tree);
80 static int ambi_op_p (enum tree_code);
81 static int unary_op_p (enum tree_code);
82 static void push_local_name (tree);
83 static tree grok_reference_init (tree, tree, tree, int);
84 static tree grokvardecl (tree, tree, const cp_decl_specifier_seq *,
85 int, int, tree);
86 static int check_static_variable_definition (tree, tree);
87 static void record_unknown_type (tree, const char *);
88 static tree builtin_function_1 (tree, tree, bool);
89 static int member_function_or_else (tree, tree, enum overload_flags);
90 static void bad_specifiers (tree, enum bad_spec_place, int, int, int, int,
91 int);
92 static void check_for_uninitialized_const_var (tree);
93 static hashval_t typename_hash (const void *);
94 static int typename_compare (const void *, const void *);
95 static tree local_variable_p_walkfn (tree *, int *, void *);
96 static tree record_builtin_java_type (const char *, int);
97 static const char *tag_name (enum tag_types);
98 static tree lookup_and_check_tag (enum tag_types, tree, tag_scope, bool);
99 static int walk_namespaces_r (tree, walk_namespaces_fn, void *);
100 static void maybe_deduce_size_from_array_init (tree, tree);
101 static void layout_var_decl (tree);
102 static tree check_initializer (tree, tree, int, vec<tree, va_gc> **);
103 static void make_rtl_for_nonlocal_decl (tree, tree, const char *);
104 static void save_function_data (tree);
105 static void copy_type_enum (tree , tree);
106 static void check_function_type (tree, tree);
107 static void finish_constructor_body (void);
108 static void begin_destructor_body (void);
109 static void finish_destructor_body (void);
110 static void record_key_method_defined (tree);
111 static tree create_array_type_for_decl (tree, tree, tree);
112 static tree get_atexit_node (void);
113 static tree get_dso_handle_node (void);
114 static tree start_cleanup_fn (void);
115 static void end_cleanup_fn (void);
116 static tree cp_make_fname_decl (location_t, tree, int);
117 static void initialize_predefined_identifiers (void);
118 static tree check_special_function_return_type
119 (special_function_kind, tree, tree);
120 static tree push_cp_library_fn (enum tree_code, tree, int);
121 static tree build_cp_library_fn (tree, enum tree_code, tree, int);
122 static void store_parm_decls (tree);
123 static void initialize_local_var (tree, tree);
124 static void expand_static_init (tree, tree);
126 /* The following symbols are subsumed in the cp_global_trees array, and
127 listed here individually for documentation purposes.
129 C++ extensions
130 tree wchar_decl_node;
132 tree vtable_entry_type;
133 tree delta_type_node;
134 tree __t_desc_type_node;
136 tree class_type_node;
137 tree unknown_type_node;
139 Array type `vtable_entry_type[]'
141 tree vtbl_type_node;
142 tree vtbl_ptr_type_node;
144 Namespaces,
146 tree std_node;
147 tree abi_node;
149 A FUNCTION_DECL which can call `abort'. Not necessarily the
150 one that the user will declare, but sufficient to be called
151 by routines that want to abort the program.
153 tree abort_fndecl;
155 The FUNCTION_DECL for the default `::operator delete'.
157 tree global_delete_fndecl;
159 Used by RTTI
160 tree type_info_type_node, tinfo_decl_id, tinfo_decl_type;
161 tree tinfo_var_id; */
163 tree cp_global_trees[CPTI_MAX];
165 /* Indicates that there is a type value in some namespace, although
166 that is not necessarily in scope at the moment. */
168 tree global_type_node;
170 /* The node that holds the "name" of the global scope. */
171 tree global_scope_name;
173 #define local_names cp_function_chain->x_local_names
175 /* A list of objects which have constructors or destructors
176 which reside in the global scope. The decl is stored in
177 the TREE_VALUE slot and the initializer is stored
178 in the TREE_PURPOSE slot. */
179 tree static_aggregates;
181 /* Like static_aggregates, but for thread_local variables. */
182 tree tls_aggregates;
184 /* -- end of C++ */
186 /* A node for the integer constant 2. */
188 tree integer_two_node;
190 /* Used only for jumps to as-yet undefined labels, since jumps to
191 defined labels can have their validity checked immediately. */
193 struct GTY((chain_next ("%h.next"))) named_label_use_entry {
194 struct named_label_use_entry *next;
195 /* The binding level to which this entry is *currently* attached.
196 This is initially the binding level in which the goto appeared,
197 but is modified as scopes are closed. */
198 cp_binding_level *binding_level;
199 /* The head of the names list that was current when the goto appeared,
200 or the inner scope popped. These are the decls that will *not* be
201 skipped when jumping to the label. */
202 tree names_in_scope;
203 /* The location of the goto, for error reporting. */
204 location_t o_goto_locus;
205 /* True if an OpenMP structured block scope has been closed since
206 the goto appeared. This means that the branch from the label will
207 illegally exit an OpenMP scope. */
208 bool in_omp_scope;
211 /* A list of all LABEL_DECLs in the function that have names. Here so
212 we can clear out their names' definitions at the end of the
213 function, and so we can check the validity of jumps to these labels. */
215 struct GTY(()) named_label_entry {
216 /* The decl itself. */
217 tree label_decl;
219 /* The binding level to which the label is *currently* attached.
220 This is initially set to the binding level in which the label
221 is defined, but is modified as scopes are closed. */
222 cp_binding_level *binding_level;
223 /* The head of the names list that was current when the label was
224 defined, or the inner scope popped. These are the decls that will
225 be skipped when jumping to the label. */
226 tree names_in_scope;
227 /* A vector of all decls from all binding levels that would be
228 crossed by a backward branch to the label. */
229 vec<tree, va_gc> *bad_decls;
231 /* A list of uses of the label, before the label is defined. */
232 struct named_label_use_entry *uses;
234 /* The following bits are set after the label is defined, and are
235 updated as scopes are popped. They indicate that a backward jump
236 to the label will illegally enter a scope of the given flavor. */
237 bool in_try_scope;
238 bool in_catch_scope;
239 bool in_omp_scope;
242 #define named_labels cp_function_chain->x_named_labels
244 /* The number of function bodies which we are currently processing.
245 (Zero if we are at namespace scope, one inside the body of a
246 function, two inside the body of a function in a local class, etc.) */
247 int function_depth;
249 /* To avoid unwanted recursion, finish_function defers all mark_used calls
250 encountered during its execution until it finishes. */
251 bool defer_mark_used_calls;
252 vec<tree, va_gc> *deferred_mark_used_calls;
254 /* States indicating how grokdeclarator() should handle declspecs marked
255 with __attribute__((deprecated)). An object declared as
256 __attribute__((deprecated)) suppresses warnings of uses of other
257 deprecated items. */
258 enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
261 /* A list of VAR_DECLs whose type was incomplete at the time the
262 variable was declared. */
264 typedef struct GTY(()) incomplete_var_d {
265 tree decl;
266 tree incomplete_type;
267 } incomplete_var;
270 static GTY(()) vec<incomplete_var, va_gc> *incomplete_vars;
272 /* Returns the kind of template specialization we are currently
273 processing, given that it's declaration contained N_CLASS_SCOPES
274 explicit scope qualifications. */
276 tmpl_spec_kind
277 current_tmpl_spec_kind (int n_class_scopes)
279 int n_template_parm_scopes = 0;
280 int seen_specialization_p = 0;
281 int innermost_specialization_p = 0;
282 cp_binding_level *b;
284 /* Scan through the template parameter scopes. */
285 for (b = current_binding_level;
286 b->kind == sk_template_parms;
287 b = b->level_chain)
289 /* If we see a specialization scope inside a parameter scope,
290 then something is wrong. That corresponds to a declaration
291 like:
293 template <class T> template <> ...
295 which is always invalid since [temp.expl.spec] forbids the
296 specialization of a class member template if the enclosing
297 class templates are not explicitly specialized as well. */
298 if (b->explicit_spec_p)
300 if (n_template_parm_scopes == 0)
301 innermost_specialization_p = 1;
302 else
303 seen_specialization_p = 1;
305 else if (seen_specialization_p == 1)
306 return tsk_invalid_member_spec;
308 ++n_template_parm_scopes;
311 /* Handle explicit instantiations. */
312 if (processing_explicit_instantiation)
314 if (n_template_parm_scopes != 0)
315 /* We've seen a template parameter list during an explicit
316 instantiation. For example:
318 template <class T> template void f(int);
320 This is erroneous. */
321 return tsk_invalid_expl_inst;
322 else
323 return tsk_expl_inst;
326 if (n_template_parm_scopes < n_class_scopes)
327 /* We've not seen enough template headers to match all the
328 specialized classes present. For example:
330 template <class T> void R<T>::S<T>::f(int);
332 This is invalid; there needs to be one set of template
333 parameters for each class. */
334 return tsk_insufficient_parms;
335 else if (n_template_parm_scopes == n_class_scopes)
336 /* We're processing a non-template declaration (even though it may
337 be a member of a template class.) For example:
339 template <class T> void S<T>::f(int);
341 The `class T' matches the `S<T>', leaving no template headers
342 corresponding to the `f'. */
343 return tsk_none;
344 else if (n_template_parm_scopes > n_class_scopes + 1)
345 /* We've got too many template headers. For example:
347 template <> template <class T> void f (T);
349 There need to be more enclosing classes. */
350 return tsk_excessive_parms;
351 else
352 /* This must be a template. It's of the form:
354 template <class T> template <class U> void S<T>::f(U);
356 This is a specialization if the innermost level was a
357 specialization; otherwise it's just a definition of the
358 template. */
359 return innermost_specialization_p ? tsk_expl_spec : tsk_template;
362 /* Exit the current scope. */
364 void
365 finish_scope (void)
367 poplevel (0, 0, 0);
370 /* When a label goes out of scope, check to see if that label was used
371 in a valid manner, and issue any appropriate warnings or errors. */
373 static void
374 pop_label (tree label, tree old_value)
376 if (!processing_template_decl)
378 if (DECL_INITIAL (label) == NULL_TREE)
380 location_t location;
382 error ("label %q+D used but not defined", label);
383 location = input_location;
384 /* FIXME want (LOCATION_FILE (input_location), (line)0) */
385 /* Avoid crashing later. */
386 define_label (location, DECL_NAME (label));
388 else
389 warn_for_unused_label (label);
392 SET_IDENTIFIER_LABEL_VALUE (DECL_NAME (label), old_value);
395 /* At the end of a function, all labels declared within the function
396 go out of scope. BLOCK is the top-level block for the
397 function. */
399 static int
400 pop_labels_1 (void **slot, void *data)
402 struct named_label_entry *ent = (struct named_label_entry *) *slot;
403 tree block = (tree) data;
405 pop_label (ent->label_decl, NULL_TREE);
407 /* Put the labels into the "variables" of the top-level block,
408 so debugger can see them. */
409 DECL_CHAIN (ent->label_decl) = BLOCK_VARS (block);
410 BLOCK_VARS (block) = ent->label_decl;
412 htab_clear_slot (named_labels, slot);
414 return 1;
417 static void
418 pop_labels (tree block)
420 if (named_labels)
422 htab_traverse (named_labels, pop_labels_1, block);
423 named_labels = NULL;
427 /* At the end of a block with local labels, restore the outer definition. */
429 static void
430 pop_local_label (tree label, tree old_value)
432 struct named_label_entry dummy;
433 void **slot;
435 pop_label (label, old_value);
437 dummy.label_decl = label;
438 slot = htab_find_slot (named_labels, &dummy, NO_INSERT);
439 htab_clear_slot (named_labels, slot);
442 /* The following two routines are used to interface to Objective-C++.
443 The binding level is purposely treated as an opaque type. */
445 void *
446 objc_get_current_scope (void)
448 return current_binding_level;
451 /* The following routine is used by the NeXT-style SJLJ exceptions;
452 variables get marked 'volatile' so as to not be clobbered by
453 _setjmp()/_longjmp() calls. All variables in the current scope,
454 as well as parent scopes up to (but not including) ENCLOSING_BLK
455 shall be thusly marked. */
457 void
458 objc_mark_locals_volatile (void *enclosing_blk)
460 cp_binding_level *scope;
462 for (scope = current_binding_level;
463 scope && scope != enclosing_blk;
464 scope = scope->level_chain)
466 tree decl;
468 for (decl = scope->names; decl; decl = TREE_CHAIN (decl))
469 objc_volatilize_decl (decl);
471 /* Do not climb up past the current function. */
472 if (scope->kind == sk_function_parms)
473 break;
477 /* Update data for defined and undefined labels when leaving a scope. */
479 static int
480 poplevel_named_label_1 (void **slot, void *data)
482 struct named_label_entry *ent = (struct named_label_entry *) *slot;
483 cp_binding_level *bl = (cp_binding_level *) data;
484 cp_binding_level *obl = bl->level_chain;
486 if (ent->binding_level == bl)
488 tree decl;
490 /* ENT->NAMES_IN_SCOPE may contain a mixture of DECLs and
491 TREE_LISTs representing OVERLOADs, so be careful. */
492 for (decl = ent->names_in_scope; decl; decl = (DECL_P (decl)
493 ? DECL_CHAIN (decl)
494 : TREE_CHAIN (decl)))
495 if (decl_jump_unsafe (decl))
496 vec_safe_push (ent->bad_decls, decl);
498 ent->binding_level = obl;
499 ent->names_in_scope = obl->names;
500 switch (bl->kind)
502 case sk_try:
503 ent->in_try_scope = true;
504 break;
505 case sk_catch:
506 ent->in_catch_scope = true;
507 break;
508 case sk_omp:
509 ent->in_omp_scope = true;
510 break;
511 default:
512 break;
515 else if (ent->uses)
517 struct named_label_use_entry *use;
519 for (use = ent->uses; use ; use = use->next)
520 if (use->binding_level == bl)
522 use->binding_level = obl;
523 use->names_in_scope = obl->names;
524 if (bl->kind == sk_omp)
525 use->in_omp_scope = true;
529 return 1;
532 /* Saved errorcount to avoid -Wunused-but-set-{parameter,variable} warnings
533 when errors were reported, except for -Werror-unused-but-set-*. */
534 static int unused_but_set_errorcount;
536 /* Exit a binding level.
537 Pop the level off, and restore the state of the identifier-decl mappings
538 that were in effect when this level was entered.
540 If KEEP == 1, this level had explicit declarations, so
541 and create a "block" (a BLOCK node) for the level
542 to record its declarations and subblocks for symbol table output.
544 If FUNCTIONBODY is nonzero, this level is the body of a function,
545 so create a block as if KEEP were set and also clear out all
546 label names.
548 If REVERSE is nonzero, reverse the order of decls before putting
549 them into the BLOCK. */
551 tree
552 poplevel (int keep, int reverse, int functionbody)
554 tree link;
555 /* The chain of decls was accumulated in reverse order.
556 Put it into forward order, just for cleanliness. */
557 tree decls;
558 tree subblocks;
559 tree block;
560 tree decl;
561 int leaving_for_scope;
562 scope_kind kind;
563 unsigned ix;
564 cp_label_binding *label_bind;
566 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
568 restart:
570 block = NULL_TREE;
572 gcc_assert (current_binding_level->kind != sk_class);
574 if (current_binding_level->kind == sk_cleanup)
575 functionbody = 0;
576 subblocks = functionbody >= 0 ? current_binding_level->blocks : 0;
578 gcc_assert (!vec_safe_length (current_binding_level->class_shadowed));
580 /* We used to use KEEP == 2 to indicate that the new block should go
581 at the beginning of the list of blocks at this binding level,
582 rather than the end. This hack is no longer used. */
583 gcc_assert (keep == 0 || keep == 1);
585 if (current_binding_level->keep)
586 keep = 1;
588 /* Any uses of undefined labels, and any defined labels, now operate
589 under constraints of next binding contour. */
590 if (cfun && !functionbody && named_labels)
591 htab_traverse (named_labels, poplevel_named_label_1,
592 current_binding_level);
594 /* Get the decls in the order they were written.
595 Usually current_binding_level->names is in reverse order.
596 But parameter decls were previously put in forward order. */
598 if (reverse)
599 current_binding_level->names
600 = decls = nreverse (current_binding_level->names);
601 else
602 decls = current_binding_level->names;
604 /* If there were any declarations or structure tags in that level,
605 or if this level is a function body,
606 create a BLOCK to record them for the life of this function. */
607 block = NULL_TREE;
608 if (keep == 1 || functionbody)
609 block = make_node (BLOCK);
610 if (block != NULL_TREE)
612 BLOCK_VARS (block) = decls;
613 BLOCK_SUBBLOCKS (block) = subblocks;
616 /* In each subblock, record that this is its superior. */
617 if (keep >= 0)
618 for (link = subblocks; link; link = BLOCK_CHAIN (link))
619 BLOCK_SUPERCONTEXT (link) = block;
621 /* We still support the old for-scope rules, whereby the variables
622 in a for-init statement were in scope after the for-statement
623 ended. We only use the new rules if flag_new_for_scope is
624 nonzero. */
625 leaving_for_scope
626 = current_binding_level->kind == sk_for && flag_new_for_scope == 1;
628 /* Before we remove the declarations first check for unused variables. */
629 if ((warn_unused_variable || warn_unused_but_set_variable)
630 && !processing_template_decl)
631 for (tree d = getdecls (); d; d = TREE_CHAIN (d))
633 /* There are cases where D itself is a TREE_LIST. See in
634 push_local_binding where the list of decls returned by
635 getdecls is built. */
636 decl = TREE_CODE (d) == TREE_LIST ? TREE_VALUE (d) : d;
637 // See through references for improved -Wunused-variable (PR 38958).
638 tree type = non_reference (TREE_TYPE (decl));
639 if (VAR_P (decl)
640 && (! TREE_USED (decl) || !DECL_READ_P (decl))
641 && ! DECL_IN_SYSTEM_HEADER (decl)
642 && DECL_NAME (decl) && ! DECL_ARTIFICIAL (decl)
643 && type != error_mark_node
644 && (!CLASS_TYPE_P (type)
645 || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
646 || lookup_attribute ("warn_unused",
647 TYPE_ATTRIBUTES (TREE_TYPE (decl)))))
649 if (! TREE_USED (decl))
650 warning (OPT_Wunused_variable, "unused variable %q+D", decl);
651 else if (DECL_CONTEXT (decl) == current_function_decl
652 // For -Wunused-but-set-variable leave references alone.
653 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
654 && errorcount == unused_but_set_errorcount)
656 warning (OPT_Wunused_but_set_variable,
657 "variable %q+D set but not used", decl);
658 unused_but_set_errorcount = errorcount;
663 /* Remove declarations for all the DECLs in this level. */
664 for (link = decls; link; link = TREE_CHAIN (link))
666 if (leaving_for_scope && VAR_P (link)
667 /* It's hard to make this ARM compatibility hack play nicely with
668 lambdas, and it really isn't necessary in C++11 mode. */
669 && cxx_dialect < cxx11
670 && DECL_NAME (link))
672 tree name = DECL_NAME (link);
673 cxx_binding *ob;
674 tree ns_binding;
676 ob = outer_binding (name,
677 IDENTIFIER_BINDING (name),
678 /*class_p=*/true);
679 if (!ob)
680 ns_binding = IDENTIFIER_NAMESPACE_VALUE (name);
681 else
682 ns_binding = NULL_TREE;
684 if (ob && ob->scope == current_binding_level->level_chain)
685 /* We have something like:
687 int i;
688 for (int i; ;);
690 and we are leaving the `for' scope. There's no reason to
691 keep the binding of the inner `i' in this case. */
692 pop_binding (name, link);
693 else if ((ob && (TREE_CODE (ob->value) == TYPE_DECL))
694 || (ns_binding && TREE_CODE (ns_binding) == TYPE_DECL))
695 /* Here, we have something like:
697 typedef int I;
699 void f () {
700 for (int I; ;);
703 We must pop the for-scope binding so we know what's a
704 type and what isn't. */
705 pop_binding (name, link);
706 else
708 /* Mark this VAR_DECL as dead so that we can tell we left it
709 there only for backward compatibility. */
710 DECL_DEAD_FOR_LOCAL (link) = 1;
712 /* Keep track of what should have happened when we
713 popped the binding. */
714 if (ob && ob->value)
716 SET_DECL_SHADOWED_FOR_VAR (link, ob->value);
717 DECL_HAS_SHADOWED_FOR_VAR_P (link) = 1;
720 /* Add it to the list of dead variables in the next
721 outermost binding to that we can remove these when we
722 leave that binding. */
723 vec_safe_push (
724 current_binding_level->level_chain->dead_vars_from_for,
725 link);
727 /* Although we don't pop the cxx_binding, we do clear
728 its SCOPE since the scope is going away now. */
729 IDENTIFIER_BINDING (name)->scope
730 = current_binding_level->level_chain;
733 else
735 tree name;
737 /* Remove the binding. */
738 decl = link;
740 if (TREE_CODE (decl) == TREE_LIST)
741 decl = TREE_VALUE (decl);
742 name = decl;
744 if (TREE_CODE (name) == OVERLOAD)
745 name = OVL_FUNCTION (name);
747 gcc_assert (DECL_P (name));
748 pop_binding (DECL_NAME (name), decl);
752 /* Remove declarations for any `for' variables from inner scopes
753 that we kept around. */
754 FOR_EACH_VEC_SAFE_ELT_REVERSE (current_binding_level->dead_vars_from_for,
755 ix, decl)
756 pop_binding (DECL_NAME (decl), decl);
758 /* Restore the IDENTIFIER_TYPE_VALUEs. */
759 for (link = current_binding_level->type_shadowed;
760 link; link = TREE_CHAIN (link))
761 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link), TREE_VALUE (link));
763 /* Restore the IDENTIFIER_LABEL_VALUEs for local labels. */
764 FOR_EACH_VEC_SAFE_ELT_REVERSE (current_binding_level->shadowed_labels,
765 ix, label_bind)
766 pop_local_label (label_bind->label, label_bind->prev_value);
768 /* There may be OVERLOADs (wrapped in TREE_LISTs) on the BLOCK_VARs
769 list if a `using' declaration put them there. The debugging
770 back ends won't understand OVERLOAD, so we remove them here.
771 Because the BLOCK_VARS are (temporarily) shared with
772 CURRENT_BINDING_LEVEL->NAMES we must do this fixup after we have
773 popped all the bindings. */
774 if (block)
776 tree* d;
778 for (d = &BLOCK_VARS (block); *d; )
780 if (TREE_CODE (*d) == TREE_LIST)
781 *d = TREE_CHAIN (*d);
782 else
783 d = &DECL_CHAIN (*d);
787 /* If the level being exited is the top level of a function,
788 check over all the labels. */
789 if (functionbody)
791 /* Since this is the top level block of a function, the vars are
792 the function's parameters. Don't leave them in the BLOCK
793 because they are found in the FUNCTION_DECL instead. */
794 BLOCK_VARS (block) = 0;
795 pop_labels (block);
798 kind = current_binding_level->kind;
799 if (kind == sk_cleanup)
801 tree stmt;
803 /* If this is a temporary binding created for a cleanup, then we'll
804 have pushed a statement list level. Pop that, create a new
805 BIND_EXPR for the block, and insert it into the stream. */
806 stmt = pop_stmt_list (current_binding_level->statement_list);
807 stmt = c_build_bind_expr (input_location, block, stmt);
808 add_stmt (stmt);
811 leave_scope ();
812 if (functionbody)
814 /* The current function is being defined, so its DECL_INITIAL
815 should be error_mark_node. */
816 gcc_assert (DECL_INITIAL (current_function_decl) == error_mark_node);
817 DECL_INITIAL (current_function_decl) = block;
819 else if (block)
820 current_binding_level->blocks
821 = block_chainon (current_binding_level->blocks, block);
823 /* If we did not make a block for the level just exited,
824 any blocks made for inner levels
825 (since they cannot be recorded as subblocks in that level)
826 must be carried forward so they will later become subblocks
827 of something else. */
828 else if (subblocks)
829 current_binding_level->blocks
830 = block_chainon (current_binding_level->blocks, subblocks);
832 /* Each and every BLOCK node created here in `poplevel' is important
833 (e.g. for proper debugging information) so if we created one
834 earlier, mark it as "used". */
835 if (block)
836 TREE_USED (block) = 1;
838 /* All temporary bindings created for cleanups are popped silently. */
839 if (kind == sk_cleanup)
840 goto restart;
842 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
843 return block;
846 /* Walk all the namespaces contained NAMESPACE, including NAMESPACE
847 itself, calling F for each. The DATA is passed to F as well. */
849 static int
850 walk_namespaces_r (tree name_space, walk_namespaces_fn f, void* data)
852 int result = 0;
853 tree current = NAMESPACE_LEVEL (name_space)->namespaces;
855 result |= (*f) (name_space, data);
857 for (; current; current = DECL_CHAIN (current))
858 result |= walk_namespaces_r (current, f, data);
860 return result;
863 /* Walk all the namespaces, calling F for each. The DATA is passed to
864 F as well. */
867 walk_namespaces (walk_namespaces_fn f, void* data)
869 return walk_namespaces_r (global_namespace, f, data);
872 /* Call wrapup_globals_declarations for the globals in NAMESPACE. If
873 DATA is non-NULL, this is the last time we will call
874 wrapup_global_declarations for this NAMESPACE. */
877 wrapup_globals_for_namespace (tree name_space, void *data)
879 cp_binding_level *level = NAMESPACE_LEVEL (name_space);
880 vec<tree, va_gc> *statics = level->static_decls;
881 tree *vec = statics->address ();
882 int len = statics->length ();
883 int last_time = (data != 0);
885 if (last_time)
887 check_global_declarations (vec, len);
888 emit_debug_global_declarations (vec, len);
889 return 0;
892 /* Write out any globals that need to be output. */
893 return wrapup_global_declarations (vec, len);
897 /* In C++, you don't have to write `struct S' to refer to `S'; you
898 can just use `S'. We accomplish this by creating a TYPE_DECL as
899 if the user had written `typedef struct S S'. Create and return
900 the TYPE_DECL for TYPE. */
902 tree
903 create_implicit_typedef (tree name, tree type)
905 tree decl;
907 decl = build_decl (input_location, TYPE_DECL, name, type);
908 DECL_ARTIFICIAL (decl) = 1;
909 /* There are other implicit type declarations, like the one *within*
910 a class that allows you to write `S::S'. We must distinguish
911 amongst these. */
912 SET_DECL_IMPLICIT_TYPEDEF_P (decl);
913 TYPE_NAME (type) = decl;
914 TYPE_STUB_DECL (type) = decl;
916 return decl;
919 /* Remember a local name for name-mangling purposes. */
921 static void
922 push_local_name (tree decl)
924 size_t i, nelts;
925 tree t, name;
927 timevar_start (TV_NAME_LOOKUP);
929 name = DECL_NAME (decl);
931 nelts = vec_safe_length (local_names);
932 for (i = 0; i < nelts; i++)
934 t = (*local_names)[i];
935 if (DECL_NAME (t) == name)
937 if (!DECL_LANG_SPECIFIC (decl))
938 retrofit_lang_decl (decl);
939 DECL_LANG_SPECIFIC (decl)->u.base.u2sel = 1;
940 if (DECL_DISCRIMINATOR_SET_P (t))
941 DECL_DISCRIMINATOR (decl) = DECL_DISCRIMINATOR (t) + 1;
942 else
943 DECL_DISCRIMINATOR (decl) = 1;
945 (*local_names)[i] = decl;
946 timevar_stop (TV_NAME_LOOKUP);
947 return;
951 vec_safe_push (local_names, decl);
952 timevar_stop (TV_NAME_LOOKUP);
955 /* Subroutine of duplicate_decls: return truthvalue of whether
956 or not types of these decls match.
958 For C++, we must compare the parameter list so that `int' can match
959 `int&' in a parameter position, but `int&' is not confused with
960 `const int&'. */
963 decls_match (tree newdecl, tree olddecl)
965 int types_match;
967 if (newdecl == olddecl)
968 return 1;
970 if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
971 /* If the two DECLs are not even the same kind of thing, we're not
972 interested in their types. */
973 return 0;
975 gcc_assert (DECL_P (newdecl));
977 if (TREE_CODE (newdecl) == FUNCTION_DECL)
979 tree f1 = TREE_TYPE (newdecl);
980 tree f2 = TREE_TYPE (olddecl);
981 tree p1 = TYPE_ARG_TYPES (f1);
982 tree p2 = TYPE_ARG_TYPES (f2);
983 tree r2;
985 /* Specializations of different templates are different functions
986 even if they have the same type. */
987 tree t1 = (DECL_USE_TEMPLATE (newdecl)
988 ? DECL_TI_TEMPLATE (newdecl)
989 : NULL_TREE);
990 tree t2 = (DECL_USE_TEMPLATE (olddecl)
991 ? DECL_TI_TEMPLATE (olddecl)
992 : NULL_TREE);
993 if (t1 != t2)
994 return 0;
996 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
997 && ! (DECL_EXTERN_C_P (newdecl)
998 && DECL_EXTERN_C_P (olddecl)))
999 return 0;
1001 /* A new declaration doesn't match a built-in one unless it
1002 is also extern "C". */
1003 if (DECL_IS_BUILTIN (olddecl)
1004 && DECL_EXTERN_C_P (olddecl) && !DECL_EXTERN_C_P (newdecl))
1005 return 0;
1007 if (TREE_CODE (f1) != TREE_CODE (f2))
1008 return 0;
1010 /* A declaration with deduced return type should use its pre-deduction
1011 type for declaration matching. */
1012 r2 = fndecl_declared_return_type (olddecl);
1014 if (same_type_p (TREE_TYPE (f1), r2))
1016 if (!prototype_p (f2) && DECL_EXTERN_C_P (olddecl)
1017 && (DECL_BUILT_IN (olddecl)
1018 #ifndef NO_IMPLICIT_EXTERN_C
1019 || (DECL_IN_SYSTEM_HEADER (newdecl) && !DECL_CLASS_SCOPE_P (newdecl))
1020 || (DECL_IN_SYSTEM_HEADER (olddecl) && !DECL_CLASS_SCOPE_P (olddecl))
1021 #endif
1024 types_match = self_promoting_args_p (p1);
1025 if (p1 == void_list_node)
1026 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
1028 #ifndef NO_IMPLICIT_EXTERN_C
1029 else if (!prototype_p (f1)
1030 && (DECL_EXTERN_C_P (olddecl)
1031 && DECL_IN_SYSTEM_HEADER (olddecl)
1032 && !DECL_CLASS_SCOPE_P (olddecl))
1033 && (DECL_EXTERN_C_P (newdecl)
1034 && DECL_IN_SYSTEM_HEADER (newdecl)
1035 && !DECL_CLASS_SCOPE_P (newdecl)))
1037 types_match = self_promoting_args_p (p2);
1038 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
1040 #endif
1041 else
1042 types_match =
1043 compparms (p1, p2)
1044 && type_memfn_rqual (f1) == type_memfn_rqual (f2)
1045 && (TYPE_ATTRIBUTES (TREE_TYPE (newdecl)) == NULL_TREE
1046 || comp_type_attributes (TREE_TYPE (newdecl),
1047 TREE_TYPE (olddecl)) != 0);
1049 else
1050 types_match = 0;
1052 /* The decls dont match if they correspond to two different versions
1053 of the same function. Disallow extern "C" functions to be
1054 versions for now. */
1055 if (types_match
1056 && !DECL_EXTERN_C_P (newdecl)
1057 && !DECL_EXTERN_C_P (olddecl)
1058 && targetm.target_option.function_versions (newdecl, olddecl))
1060 /* Mark functions as versions if necessary. Modify the mangled decl
1061 name if necessary. */
1062 if (DECL_FUNCTION_VERSIONED (newdecl)
1063 && DECL_FUNCTION_VERSIONED (olddecl))
1064 return 0;
1065 if (!DECL_FUNCTION_VERSIONED (newdecl))
1067 DECL_FUNCTION_VERSIONED (newdecl) = 1;
1068 if (DECL_ASSEMBLER_NAME_SET_P (newdecl))
1069 mangle_decl (newdecl);
1071 if (!DECL_FUNCTION_VERSIONED (olddecl))
1073 DECL_FUNCTION_VERSIONED (olddecl) = 1;
1074 if (DECL_ASSEMBLER_NAME_SET_P (olddecl))
1075 mangle_decl (olddecl);
1077 cgraph_node::record_function_versions (olddecl, newdecl);
1078 return 0;
1081 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1083 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl))
1084 != TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)))
1085 return 0;
1087 if (!comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
1088 DECL_TEMPLATE_PARMS (olddecl)))
1089 return 0;
1091 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
1092 types_match = same_type_p (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl)),
1093 TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl)));
1094 else
1095 types_match = decls_match (DECL_TEMPLATE_RESULT (olddecl),
1096 DECL_TEMPLATE_RESULT (newdecl));
1098 else
1100 /* Need to check scope for variable declaration (VAR_DECL).
1101 For typedef (TYPE_DECL), scope is ignored. */
1102 if (VAR_P (newdecl)
1103 && CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
1104 /* [dcl.link]
1105 Two declarations for an object with C language linkage
1106 with the same name (ignoring the namespace that qualify
1107 it) that appear in different namespace scopes refer to
1108 the same object. */
1109 && !(DECL_EXTERN_C_P (olddecl) && DECL_EXTERN_C_P (newdecl)))
1110 return 0;
1112 if (TREE_TYPE (newdecl) == error_mark_node)
1113 types_match = TREE_TYPE (olddecl) == error_mark_node;
1114 else if (TREE_TYPE (olddecl) == NULL_TREE)
1115 types_match = TREE_TYPE (newdecl) == NULL_TREE;
1116 else if (TREE_TYPE (newdecl) == NULL_TREE)
1117 types_match = 0;
1118 else
1119 types_match = comptypes (TREE_TYPE (newdecl),
1120 TREE_TYPE (olddecl),
1121 COMPARE_REDECLARATION);
1124 return types_match;
1127 /* If NEWDECL is `static' and an `extern' was seen previously,
1128 warn about it. OLDDECL is the previous declaration.
1130 Note that this does not apply to the C++ case of declaring
1131 a variable `extern const' and then later `const'.
1133 Don't complain about built-in functions, since they are beyond
1134 the user's control. */
1136 void
1137 warn_extern_redeclared_static (tree newdecl, tree olddecl)
1139 if (TREE_CODE (newdecl) == TYPE_DECL
1140 || TREE_CODE (newdecl) == TEMPLATE_DECL
1141 || TREE_CODE (newdecl) == CONST_DECL
1142 || TREE_CODE (newdecl) == NAMESPACE_DECL)
1143 return;
1145 /* Don't get confused by static member functions; that's a different
1146 use of `static'. */
1147 if (TREE_CODE (newdecl) == FUNCTION_DECL
1148 && DECL_STATIC_FUNCTION_P (newdecl))
1149 return;
1151 /* If the old declaration was `static', or the new one isn't, then
1152 everything is OK. */
1153 if (DECL_THIS_STATIC (olddecl) || !DECL_THIS_STATIC (newdecl))
1154 return;
1156 /* It's OK to declare a builtin function as `static'. */
1157 if (TREE_CODE (olddecl) == FUNCTION_DECL
1158 && DECL_ARTIFICIAL (olddecl))
1159 return;
1161 if (permerror (input_location,
1162 "%qD was declared %<extern%> and later %<static%>", newdecl))
1163 inform (input_location, "previous declaration of %q+D", olddecl);
1166 /* NEW_DECL is a redeclaration of OLD_DECL; both are functions or
1167 function templates. If their exception specifications do not
1168 match, issue a diagnostic. */
1170 static void
1171 check_redeclaration_exception_specification (tree new_decl,
1172 tree old_decl)
1174 tree new_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (new_decl));
1175 tree old_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old_decl));
1177 /* Two default specs are equivalent, don't force evaluation. */
1178 if (UNEVALUATED_NOEXCEPT_SPEC_P (new_exceptions)
1179 && UNEVALUATED_NOEXCEPT_SPEC_P (old_exceptions))
1180 return;
1182 maybe_instantiate_noexcept (new_decl);
1183 maybe_instantiate_noexcept (old_decl);
1184 new_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (new_decl));
1185 old_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old_decl));
1187 /* [except.spec]
1189 If any declaration of a function has an exception-specification,
1190 all declarations, including the definition and an explicit
1191 specialization, of that function shall have an
1192 exception-specification with the same set of type-ids. */
1193 if ((pedantic || ! DECL_IN_SYSTEM_HEADER (old_decl))
1194 && ! DECL_IS_BUILTIN (old_decl)
1195 && flag_exceptions
1196 && !comp_except_specs (new_exceptions, old_exceptions, ce_normal))
1198 error ("declaration of %qF has a different exception specifier",
1199 new_decl);
1200 error ("from previous declaration %q+F", old_decl);
1204 /* Return true if OLD_DECL and NEW_DECL agree on constexprness.
1205 Otherwise issue diagnostics. */
1207 static bool
1208 validate_constexpr_redeclaration (tree old_decl, tree new_decl)
1210 old_decl = STRIP_TEMPLATE (old_decl);
1211 new_decl = STRIP_TEMPLATE (new_decl);
1212 if (!VAR_OR_FUNCTION_DECL_P (old_decl)
1213 || !VAR_OR_FUNCTION_DECL_P (new_decl))
1214 return true;
1215 if (DECL_DECLARED_CONSTEXPR_P (old_decl)
1216 == DECL_DECLARED_CONSTEXPR_P (new_decl))
1217 return true;
1218 if (TREE_CODE (old_decl) == FUNCTION_DECL)
1220 if (DECL_BUILT_IN (old_decl))
1222 /* Hide a built-in declaration. */
1223 DECL_DECLARED_CONSTEXPR_P (old_decl)
1224 = DECL_DECLARED_CONSTEXPR_P (new_decl);
1225 return true;
1227 /* 7.1.5 [dcl.constexpr]
1228 Note: An explicit specialization can differ from the template
1229 declaration with respect to the constexpr specifier. */
1230 if (! DECL_TEMPLATE_SPECIALIZATION (old_decl)
1231 && DECL_TEMPLATE_SPECIALIZATION (new_decl))
1232 return true;
1234 error ("redeclaration %qD differs in %<constexpr%>", new_decl);
1235 error ("from previous declaration %q+D", old_decl);
1236 return false;
1238 return true;
1241 /* DECL is a redeclaration of a function or function template. If
1242 it does have default arguments issue a diagnostic. Note: this
1243 function is used to enforce the requirements in C++11 8.3.6 about
1244 no default arguments in redeclarations. */
1246 static void
1247 check_redeclaration_no_default_args (tree decl)
1249 gcc_assert (DECL_DECLARES_FUNCTION_P (decl));
1251 for (tree t = FUNCTION_FIRST_USER_PARMTYPE (decl);
1252 t && t != void_list_node; t = TREE_CHAIN (t))
1253 if (TREE_PURPOSE (t))
1255 permerror (input_location,
1256 "redeclaration of %q#D may not have default "
1257 "arguments", decl);
1258 return;
1262 #define GNU_INLINE_P(fn) (DECL_DECLARED_INLINE_P (fn) \
1263 && lookup_attribute ("gnu_inline", \
1264 DECL_ATTRIBUTES (fn)))
1266 /* If NEWDECL is a redeclaration of OLDDECL, merge the declarations.
1267 If the redeclaration is invalid, a diagnostic is issued, and the
1268 error_mark_node is returned. Otherwise, OLDDECL is returned.
1270 If NEWDECL is not a redeclaration of OLDDECL, NULL_TREE is
1271 returned.
1273 NEWDECL_IS_FRIEND is true if NEWDECL was declared as a friend. */
1275 tree
1276 duplicate_decls (tree newdecl, tree olddecl, bool newdecl_is_friend)
1278 unsigned olddecl_uid = DECL_UID (olddecl);
1279 int olddecl_friend = 0, types_match = 0, hidden_friend = 0;
1280 int new_defines_function = 0;
1281 tree new_template_info;
1283 if (newdecl == olddecl)
1284 return olddecl;
1286 types_match = decls_match (newdecl, olddecl);
1288 /* If either the type of the new decl or the type of the old decl is an
1289 error_mark_node, then that implies that we have already issued an
1290 error (earlier) for some bogus type specification, and in that case,
1291 it is rather pointless to harass the user with yet more error message
1292 about the same declaration, so just pretend the types match here. */
1293 if (TREE_TYPE (newdecl) == error_mark_node
1294 || TREE_TYPE (olddecl) == error_mark_node)
1295 return error_mark_node;
1297 if (UDLIT_OPER_P (DECL_NAME (newdecl))
1298 && UDLIT_OPER_P (DECL_NAME (olddecl)))
1300 if (TREE_CODE (newdecl) == TEMPLATE_DECL
1301 && TREE_CODE (olddecl) != TEMPLATE_DECL
1302 && check_raw_literal_operator (olddecl))
1303 error ("literal operator template %q+D conflicts with"
1304 " raw literal operator %qD", newdecl, olddecl);
1305 else if (TREE_CODE (newdecl) != TEMPLATE_DECL
1306 && TREE_CODE (olddecl) == TEMPLATE_DECL
1307 && check_raw_literal_operator (newdecl))
1308 error ("raw literal operator %q+D conflicts with"
1309 " literal operator template %qD", newdecl, olddecl);
1312 if (DECL_P (olddecl)
1313 && TREE_CODE (newdecl) == FUNCTION_DECL
1314 && TREE_CODE (olddecl) == FUNCTION_DECL
1315 && (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl)))
1317 if (DECL_DECLARED_INLINE_P (newdecl)
1318 && DECL_UNINLINABLE (newdecl)
1319 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1320 /* Already warned elsewhere. */;
1321 else if (DECL_DECLARED_INLINE_P (olddecl)
1322 && DECL_UNINLINABLE (olddecl)
1323 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1324 /* Already warned. */;
1325 else if (DECL_DECLARED_INLINE_P (newdecl)
1326 && DECL_UNINLINABLE (olddecl)
1327 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1329 if (warning (OPT_Wattributes, "function %q+D redeclared as inline",
1330 newdecl))
1331 inform (DECL_SOURCE_LOCATION (olddecl),
1332 "previous declaration of %qD with attribute noinline",
1333 olddecl);
1335 else if (DECL_DECLARED_INLINE_P (olddecl)
1336 && DECL_UNINLINABLE (newdecl)
1337 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1339 if (warning (OPT_Wattributes, "function %q+D redeclared with "
1340 "attribute noinline", newdecl))
1341 inform (DECL_SOURCE_LOCATION (olddecl),
1342 "previous declaration of %qD was inline",
1343 olddecl);
1347 /* Check for redeclaration and other discrepancies. */
1348 if (TREE_CODE (olddecl) == FUNCTION_DECL
1349 && DECL_ARTIFICIAL (olddecl))
1351 gcc_assert (!DECL_HIDDEN_FRIEND_P (olddecl));
1352 if (TREE_CODE (newdecl) != FUNCTION_DECL)
1354 /* Avoid warnings redeclaring built-ins which have not been
1355 explicitly declared. */
1356 if (DECL_ANTICIPATED (olddecl))
1357 return NULL_TREE;
1359 /* If you declare a built-in or predefined function name as static,
1360 the old definition is overridden, but optionally warn this was a
1361 bad choice of name. */
1362 if (! TREE_PUBLIC (newdecl))
1364 warning (OPT_Wshadow,
1365 DECL_BUILT_IN (olddecl)
1366 ? G_("shadowing built-in function %q#D")
1367 : G_("shadowing library function %q#D"), olddecl);
1368 /* Discard the old built-in function. */
1369 return NULL_TREE;
1371 /* If the built-in is not ansi, then programs can override
1372 it even globally without an error. */
1373 else if (! DECL_BUILT_IN (olddecl))
1374 warning (0, "library function %q#D redeclared as non-function %q#D",
1375 olddecl, newdecl);
1376 else
1377 error ("declaration of %q#D conflicts with built-in "
1378 "declaration %q#D", newdecl, olddecl);
1379 return NULL_TREE;
1381 else if (DECL_OMP_DECLARE_REDUCTION_P (olddecl))
1383 gcc_assert (DECL_OMP_DECLARE_REDUCTION_P (newdecl));
1384 error_at (DECL_SOURCE_LOCATION (newdecl),
1385 "redeclaration of %<pragma omp declare reduction%>");
1386 inform (DECL_SOURCE_LOCATION (olddecl),
1387 "previous %<pragma omp declare reduction%> declaration");
1388 return error_mark_node;
1390 else if (!types_match)
1392 /* Avoid warnings redeclaring built-ins which have not been
1393 explicitly declared. */
1394 if (DECL_ANTICIPATED (olddecl))
1396 /* Deal with fileptr_type_node. FILE type is not known
1397 at the time we create the builtins. */
1398 tree t1, t2;
1400 for (t1 = TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1401 t2 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1402 t1 || t2;
1403 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1404 if (!t1 || !t2)
1405 break;
1406 else if (TREE_VALUE (t2) == fileptr_type_node)
1408 tree t = TREE_VALUE (t1);
1410 if (TYPE_PTR_P (t)
1411 && TYPE_IDENTIFIER (TREE_TYPE (t))
1412 == get_identifier ("FILE")
1413 && compparms (TREE_CHAIN (t1), TREE_CHAIN (t2)))
1415 tree oldargs = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1417 TYPE_ARG_TYPES (TREE_TYPE (olddecl))
1418 = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
1419 types_match = decls_match (newdecl, olddecl);
1420 if (types_match)
1421 return duplicate_decls (newdecl, olddecl,
1422 newdecl_is_friend);
1423 TYPE_ARG_TYPES (TREE_TYPE (olddecl)) = oldargs;
1426 else if (! same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1427 break;
1429 else if ((DECL_EXTERN_C_P (newdecl)
1430 && DECL_EXTERN_C_P (olddecl))
1431 || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1432 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
1434 /* A near match; override the builtin. */
1436 if (TREE_PUBLIC (newdecl))
1437 warning (0, "new declaration %q#D ambiguates built-in "
1438 "declaration %q#D", newdecl, olddecl);
1439 else
1440 warning (OPT_Wshadow,
1441 DECL_BUILT_IN (olddecl)
1442 ? G_("shadowing built-in function %q#D")
1443 : G_("shadowing library function %q#D"), olddecl);
1445 else
1446 /* Discard the old built-in function. */
1447 return NULL_TREE;
1449 /* Replace the old RTL to avoid problems with inlining. */
1450 COPY_DECL_RTL (newdecl, olddecl);
1452 /* Even if the types match, prefer the new declarations type for
1453 built-ins which have not been explicitly declared, for
1454 exception lists, etc... */
1455 else if (DECL_IS_BUILTIN (olddecl))
1457 tree type = TREE_TYPE (newdecl);
1458 tree attribs = (*targetm.merge_type_attributes)
1459 (TREE_TYPE (olddecl), type);
1461 type = cp_build_type_attribute_variant (type, attribs);
1462 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = type;
1465 /* If a function is explicitly declared "throw ()", propagate that to
1466 the corresponding builtin. */
1467 if (DECL_BUILT_IN_CLASS (olddecl) == BUILT_IN_NORMAL
1468 && DECL_ANTICIPATED (olddecl)
1469 && TREE_NOTHROW (newdecl)
1470 && !TREE_NOTHROW (olddecl))
1472 enum built_in_function fncode = DECL_FUNCTION_CODE (olddecl);
1473 tree tmpdecl = builtin_decl_explicit (fncode);
1474 if (tmpdecl && tmpdecl != olddecl && types_match)
1475 TREE_NOTHROW (tmpdecl) = 1;
1478 /* Whether or not the builtin can throw exceptions has no
1479 bearing on this declarator. */
1480 TREE_NOTHROW (olddecl) = 0;
1482 if (DECL_THIS_STATIC (newdecl) && !DECL_THIS_STATIC (olddecl))
1484 /* If a builtin function is redeclared as `static', merge
1485 the declarations, but make the original one static. */
1486 DECL_THIS_STATIC (olddecl) = 1;
1487 TREE_PUBLIC (olddecl) = 0;
1489 /* Make the old declaration consistent with the new one so
1490 that all remnants of the builtin-ness of this function
1491 will be banished. */
1492 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
1493 COPY_DECL_RTL (newdecl, olddecl);
1496 else if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1498 /* C++ Standard, 3.3, clause 4:
1499 "[Note: a namespace name or a class template name must be unique
1500 in its declarative region (7.3.2, clause 14). ]" */
1501 if (TREE_CODE (olddecl) != NAMESPACE_DECL
1502 && TREE_CODE (newdecl) != NAMESPACE_DECL
1503 && (TREE_CODE (olddecl) != TEMPLATE_DECL
1504 || TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) != TYPE_DECL)
1505 && (TREE_CODE (newdecl) != TEMPLATE_DECL
1506 || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) != TYPE_DECL))
1508 if ((TREE_CODE (olddecl) == TYPE_DECL && DECL_ARTIFICIAL (olddecl)
1509 && TREE_CODE (newdecl) != TYPE_DECL)
1510 || (TREE_CODE (newdecl) == TYPE_DECL && DECL_ARTIFICIAL (newdecl)
1511 && TREE_CODE (olddecl) != TYPE_DECL))
1513 /* We do nothing special here, because C++ does such nasty
1514 things with TYPE_DECLs. Instead, just let the TYPE_DECL
1515 get shadowed, and know that if we need to find a TYPE_DECL
1516 for a given name, we can look in the IDENTIFIER_TYPE_VALUE
1517 slot of the identifier. */
1518 return NULL_TREE;
1521 if ((TREE_CODE (newdecl) == FUNCTION_DECL
1522 && DECL_FUNCTION_TEMPLATE_P (olddecl))
1523 || (TREE_CODE (olddecl) == FUNCTION_DECL
1524 && DECL_FUNCTION_TEMPLATE_P (newdecl)))
1525 return NULL_TREE;
1528 error ("%q#D redeclared as different kind of symbol", newdecl);
1529 if (TREE_CODE (olddecl) == TREE_LIST)
1530 olddecl = TREE_VALUE (olddecl);
1531 inform (DECL_SOURCE_LOCATION (olddecl),
1532 "previous declaration %q#D", olddecl);
1534 return error_mark_node;
1536 else if (!types_match)
1538 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl))
1539 /* These are certainly not duplicate declarations; they're
1540 from different scopes. */
1541 return NULL_TREE;
1543 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1545 /* The name of a class template may not be declared to refer to
1546 any other template, class, function, object, namespace, value,
1547 or type in the same scope. */
1548 if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == TYPE_DECL
1549 || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
1551 error ("conflicting declaration of template %q#D", newdecl);
1552 inform (DECL_SOURCE_LOCATION (olddecl),
1553 "previous declaration %q#D", olddecl);
1554 return error_mark_node;
1556 else if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == FUNCTION_DECL
1557 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL
1558 && compparms (TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl))),
1559 TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl))))
1560 && comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
1561 DECL_TEMPLATE_PARMS (olddecl))
1562 /* Template functions can be disambiguated by
1563 return type. */
1564 && same_type_p (TREE_TYPE (TREE_TYPE (newdecl)),
1565 TREE_TYPE (TREE_TYPE (olddecl))))
1567 error ("ambiguating new declaration %q#D", newdecl);
1568 inform (DECL_SOURCE_LOCATION (olddecl),
1569 "old declaration %q#D", olddecl);
1571 return NULL_TREE;
1573 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1575 if (DECL_EXTERN_C_P (newdecl) && DECL_EXTERN_C_P (olddecl))
1577 error ("conflicting declaration of C function %q#D",
1578 newdecl);
1579 inform (DECL_SOURCE_LOCATION (olddecl),
1580 "previous declaration %q#D", olddecl);
1581 return NULL_TREE;
1583 /* For function versions, params and types match, but they
1584 are not ambiguous. */
1585 else if ((!DECL_FUNCTION_VERSIONED (newdecl)
1586 && !DECL_FUNCTION_VERSIONED (olddecl))
1587 && compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1588 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
1590 error ("ambiguating new declaration of %q#D", newdecl);
1591 inform (DECL_SOURCE_LOCATION (olddecl),
1592 "old declaration %q#D", olddecl);
1593 return error_mark_node;
1595 else
1596 return NULL_TREE;
1598 else
1600 error ("conflicting declaration %q#D", newdecl);
1601 inform (DECL_SOURCE_LOCATION (olddecl),
1602 "previous declaration as %q#D", olddecl);
1603 return error_mark_node;
1606 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1607 && ((DECL_TEMPLATE_SPECIALIZATION (olddecl)
1608 && (!DECL_TEMPLATE_INFO (newdecl)
1609 || (DECL_TI_TEMPLATE (newdecl)
1610 != DECL_TI_TEMPLATE (olddecl))))
1611 || (DECL_TEMPLATE_SPECIALIZATION (newdecl)
1612 && (!DECL_TEMPLATE_INFO (olddecl)
1613 || (DECL_TI_TEMPLATE (olddecl)
1614 != DECL_TI_TEMPLATE (newdecl))))))
1615 /* It's OK to have a template specialization and a non-template
1616 with the same type, or to have specializations of two
1617 different templates with the same type. Note that if one is a
1618 specialization, and the other is an instantiation of the same
1619 template, that we do not exit at this point. That situation
1620 can occur if we instantiate a template class, and then
1621 specialize one of its methods. This situation is valid, but
1622 the declarations must be merged in the usual way. */
1623 return NULL_TREE;
1624 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1625 && ((DECL_TEMPLATE_INSTANTIATION (olddecl)
1626 && !DECL_USE_TEMPLATE (newdecl))
1627 || (DECL_TEMPLATE_INSTANTIATION (newdecl)
1628 && !DECL_USE_TEMPLATE (olddecl))))
1629 /* One of the declarations is a template instantiation, and the
1630 other is not a template at all. That's OK. */
1631 return NULL_TREE;
1632 else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
1634 /* In [namespace.alias] we have:
1636 In a declarative region, a namespace-alias-definition can be
1637 used to redefine a namespace-alias declared in that declarative
1638 region to refer only to the namespace to which it already
1639 refers.
1641 Therefore, if we encounter a second alias directive for the same
1642 alias, we can just ignore the second directive. */
1643 if (DECL_NAMESPACE_ALIAS (newdecl)
1644 && (DECL_NAMESPACE_ALIAS (newdecl)
1645 == DECL_NAMESPACE_ALIAS (olddecl)))
1646 return olddecl;
1647 /* [namespace.alias]
1649 A namespace-name or namespace-alias shall not be declared as
1650 the name of any other entity in the same declarative region.
1651 A namespace-name defined at global scope shall not be
1652 declared as the name of any other entity in any global scope
1653 of the program. */
1654 error ("conflicting declaration of namespace %qD", newdecl);
1655 inform (DECL_SOURCE_LOCATION (olddecl),
1656 "previous declaration of namespace %qD here", olddecl);
1657 return error_mark_node;
1659 else
1661 const char *errmsg = redeclaration_error_message (newdecl, olddecl);
1662 if (errmsg)
1664 error_at (DECL_SOURCE_LOCATION (newdecl), errmsg, newdecl);
1665 if (DECL_NAME (olddecl) != NULL_TREE)
1666 inform (input_location,
1667 (DECL_INITIAL (olddecl) && namespace_bindings_p ())
1668 ? G_("%q+#D previously defined here")
1669 : G_("%q+#D previously declared here"), olddecl);
1670 return error_mark_node;
1672 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1673 && DECL_INITIAL (olddecl) != NULL_TREE
1674 && !prototype_p (TREE_TYPE (olddecl))
1675 && prototype_p (TREE_TYPE (newdecl)))
1677 /* Prototype decl follows defn w/o prototype. */
1678 if (warning_at (DECL_SOURCE_LOCATION (newdecl), 0,
1679 "prototype specified for %q#D", newdecl))
1680 inform (DECL_SOURCE_LOCATION (olddecl),
1681 "previous non-prototype definition here");
1683 else if (VAR_OR_FUNCTION_DECL_P (olddecl)
1684 && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl))
1686 /* [dcl.link]
1687 If two declarations of the same function or object
1688 specify different linkage-specifications ..., the program
1689 is ill-formed.... Except for functions with C++ linkage,
1690 a function declaration without a linkage specification
1691 shall not precede the first linkage specification for
1692 that function. A function can be declared without a
1693 linkage specification after an explicit linkage
1694 specification has been seen; the linkage explicitly
1695 specified in the earlier declaration is not affected by
1696 such a function declaration.
1698 DR 563 raises the question why the restrictions on
1699 functions should not also apply to objects. Older
1700 versions of G++ silently ignore the linkage-specification
1701 for this example:
1703 namespace N {
1704 extern int i;
1705 extern "C" int i;
1708 which is clearly wrong. Therefore, we now treat objects
1709 like functions. */
1710 if (current_lang_depth () == 0)
1712 /* There is no explicit linkage-specification, so we use
1713 the linkage from the previous declaration. */
1714 if (!DECL_LANG_SPECIFIC (newdecl))
1715 retrofit_lang_decl (newdecl);
1716 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
1718 else
1720 error ("conflicting declaration of %q#D with %qL linkage",
1721 newdecl, DECL_LANGUAGE (newdecl));
1722 inform (DECL_SOURCE_LOCATION (olddecl),
1723 "previous declaration with %qL linkage",
1724 DECL_LANGUAGE (olddecl));
1728 if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl))
1730 else if (TREE_CODE (olddecl) == FUNCTION_DECL)
1732 /* Note: free functions, as TEMPLATE_DECLs, are handled below. */
1733 if (DECL_FUNCTION_MEMBER_P (olddecl)
1734 && (/* grokfndecl passes member function templates too
1735 as FUNCTION_DECLs. */
1736 DECL_TEMPLATE_INFO (olddecl)
1737 /* C++11 8.3.6/6.
1738 Default arguments for a member function of a class
1739 template shall be specified on the initial declaration
1740 of the member function within the class template. */
1741 || CLASSTYPE_TEMPLATE_INFO (CP_DECL_CONTEXT (olddecl))))
1742 check_redeclaration_no_default_args (newdecl);
1743 else
1745 tree t1 = FUNCTION_FIRST_USER_PARMTYPE (olddecl);
1746 tree t2 = FUNCTION_FIRST_USER_PARMTYPE (newdecl);
1747 int i = 1;
1749 for (; t1 && t1 != void_list_node;
1750 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++)
1751 if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
1753 if (1 == simple_cst_equal (TREE_PURPOSE (t1),
1754 TREE_PURPOSE (t2)))
1756 if (permerror (input_location,
1757 "default argument given for parameter "
1758 "%d of %q#D", i, newdecl))
1759 inform (DECL_SOURCE_LOCATION (olddecl),
1760 "previous specification in %q#D here",
1761 olddecl);
1763 else
1765 error ("default argument given for parameter %d "
1766 "of %q#D", i, newdecl);
1767 inform (DECL_SOURCE_LOCATION (olddecl),
1768 "previous specification in %q#D here",
1769 olddecl);
1776 /* Do not merge an implicit typedef with an explicit one. In:
1778 class A;
1780 typedef class A A __attribute__ ((foo));
1782 the attribute should apply only to the typedef. */
1783 if (TREE_CODE (olddecl) == TYPE_DECL
1784 && (DECL_IMPLICIT_TYPEDEF_P (olddecl)
1785 || DECL_IMPLICIT_TYPEDEF_P (newdecl)))
1786 return NULL_TREE;
1788 /* If new decl is `static' and an `extern' was seen previously,
1789 warn about it. */
1790 warn_extern_redeclared_static (newdecl, olddecl);
1792 if (!validate_constexpr_redeclaration (olddecl, newdecl))
1793 return error_mark_node;
1795 /* We have committed to returning 1 at this point. */
1796 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1798 /* Now that functions must hold information normally held
1799 by field decls, there is extra work to do so that
1800 declaration information does not get destroyed during
1801 definition. */
1802 if (DECL_VINDEX (olddecl))
1803 DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl);
1804 if (DECL_CONTEXT (olddecl))
1805 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1806 DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl);
1807 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1808 DECL_PURE_VIRTUAL_P (newdecl) |= DECL_PURE_VIRTUAL_P (olddecl);
1809 DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl);
1810 DECL_INVALID_OVERRIDER_P (newdecl) |= DECL_INVALID_OVERRIDER_P (olddecl);
1811 DECL_THIS_STATIC (newdecl) |= DECL_THIS_STATIC (olddecl);
1812 if (DECL_OVERLOADED_OPERATOR_P (olddecl) != ERROR_MARK)
1813 SET_OVERLOADED_OPERATOR_CODE
1814 (newdecl, DECL_OVERLOADED_OPERATOR_P (olddecl));
1815 new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE;
1817 /* Optionally warn about more than one declaration for the same
1818 name, but don't warn about a function declaration followed by a
1819 definition. */
1820 if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl)
1821 && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE)
1822 /* Don't warn about extern decl followed by definition. */
1823 && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl))
1824 /* Don't warn about friends, let add_friend take care of it. */
1825 && ! (newdecl_is_friend || DECL_FRIEND_P (olddecl))
1826 /* Don't warn about declaration followed by specialization. */
1827 && (! DECL_TEMPLATE_SPECIALIZATION (newdecl)
1828 || DECL_TEMPLATE_SPECIALIZATION (olddecl)))
1830 if (warning (OPT_Wredundant_decls,
1831 "redundant redeclaration of %qD in same scope",
1832 newdecl))
1833 inform (DECL_SOURCE_LOCATION (olddecl),
1834 "previous declaration of %qD", olddecl);
1837 if (!(DECL_TEMPLATE_INSTANTIATION (olddecl)
1838 && DECL_TEMPLATE_SPECIALIZATION (newdecl)))
1840 if (DECL_DELETED_FN (newdecl))
1842 error ("deleted definition of %qD", newdecl);
1843 inform (DECL_SOURCE_LOCATION (olddecl),
1844 "previous declaration of %qD", olddecl);
1846 DECL_DELETED_FN (newdecl) |= DECL_DELETED_FN (olddecl);
1850 /* Deal with C++: must preserve virtual function table size. */
1851 if (TREE_CODE (olddecl) == TYPE_DECL)
1853 tree newtype = TREE_TYPE (newdecl);
1854 tree oldtype = TREE_TYPE (olddecl);
1856 if (newtype != error_mark_node && oldtype != error_mark_node
1857 && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype))
1858 CLASSTYPE_FRIEND_CLASSES (newtype)
1859 = CLASSTYPE_FRIEND_CLASSES (oldtype);
1861 DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl);
1864 /* Copy all the DECL_... slots specified in the new decl
1865 except for any that we copy here from the old type. */
1866 DECL_ATTRIBUTES (newdecl)
1867 = (*targetm.merge_decl_attributes) (olddecl, newdecl);
1869 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1871 tree old_result;
1872 tree new_result;
1873 old_result = DECL_TEMPLATE_RESULT (olddecl);
1874 new_result = DECL_TEMPLATE_RESULT (newdecl);
1875 TREE_TYPE (olddecl) = TREE_TYPE (old_result);
1876 DECL_TEMPLATE_SPECIALIZATIONS (olddecl)
1877 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (olddecl),
1878 DECL_TEMPLATE_SPECIALIZATIONS (newdecl));
1880 DECL_ATTRIBUTES (old_result)
1881 = (*targetm.merge_decl_attributes) (old_result, new_result);
1883 if (DECL_FUNCTION_TEMPLATE_P (newdecl))
1885 /* Per C++11 8.3.6/4, default arguments cannot be added in later
1886 declarations of a function template. */
1887 check_redeclaration_no_default_args (newdecl);
1889 check_default_args (newdecl);
1891 if (GNU_INLINE_P (old_result) != GNU_INLINE_P (new_result)
1892 && DECL_INITIAL (new_result))
1894 if (DECL_INITIAL (old_result))
1895 DECL_UNINLINABLE (old_result) = 1;
1896 else
1897 DECL_UNINLINABLE (old_result) = DECL_UNINLINABLE (new_result);
1898 DECL_EXTERNAL (old_result) = DECL_EXTERNAL (new_result);
1899 DECL_NOT_REALLY_EXTERN (old_result)
1900 = DECL_NOT_REALLY_EXTERN (new_result);
1901 DECL_INTERFACE_KNOWN (old_result)
1902 = DECL_INTERFACE_KNOWN (new_result);
1903 DECL_DECLARED_INLINE_P (old_result)
1904 = DECL_DECLARED_INLINE_P (new_result);
1905 DECL_DISREGARD_INLINE_LIMITS (old_result)
1906 |= DECL_DISREGARD_INLINE_LIMITS (new_result);
1909 else
1911 DECL_DECLARED_INLINE_P (old_result)
1912 |= DECL_DECLARED_INLINE_P (new_result);
1913 DECL_DISREGARD_INLINE_LIMITS (old_result)
1914 |= DECL_DISREGARD_INLINE_LIMITS (new_result);
1915 check_redeclaration_exception_specification (newdecl, olddecl);
1919 /* If the new declaration is a definition, update the file and
1920 line information on the declaration, and also make
1921 the old declaration the same definition. */
1922 if (DECL_INITIAL (new_result) != NULL_TREE)
1924 DECL_SOURCE_LOCATION (olddecl)
1925 = DECL_SOURCE_LOCATION (old_result)
1926 = DECL_SOURCE_LOCATION (newdecl);
1927 DECL_INITIAL (old_result) = DECL_INITIAL (new_result);
1928 if (DECL_FUNCTION_TEMPLATE_P (newdecl))
1930 tree parm;
1931 DECL_ARGUMENTS (old_result)
1932 = DECL_ARGUMENTS (new_result);
1933 for (parm = DECL_ARGUMENTS (old_result); parm;
1934 parm = DECL_CHAIN (parm))
1935 DECL_CONTEXT (parm) = old_result;
1939 return olddecl;
1942 if (types_match)
1944 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1945 check_redeclaration_exception_specification (newdecl, olddecl);
1947 /* Automatically handles default parameters. */
1948 tree oldtype = TREE_TYPE (olddecl);
1949 tree newtype;
1951 /* For typedefs use the old type, as the new type's DECL_NAME points
1952 at newdecl, which will be ggc_freed. */
1953 if (TREE_CODE (newdecl) == TYPE_DECL)
1954 newtype = oldtype;
1955 else
1956 /* Merge the data types specified in the two decls. */
1957 newtype = merge_types (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
1959 if (VAR_P (newdecl))
1961 DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl);
1962 DECL_INITIALIZED_P (newdecl) |= DECL_INITIALIZED_P (olddecl);
1963 DECL_NONTRIVIALLY_INITIALIZED_P (newdecl)
1964 |= DECL_NONTRIVIALLY_INITIALIZED_P (olddecl);
1965 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (newdecl)
1966 |= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (olddecl);
1968 /* Merge the threadprivate attribute from OLDDECL into NEWDECL. */
1969 if (DECL_LANG_SPECIFIC (olddecl)
1970 && CP_DECL_THREADPRIVATE_P (olddecl))
1972 /* Allocate a LANG_SPECIFIC structure for NEWDECL, if needed. */
1973 if (!DECL_LANG_SPECIFIC (newdecl))
1974 retrofit_lang_decl (newdecl);
1976 set_decl_tls_model (newdecl, DECL_TLS_MODEL (olddecl));
1977 CP_DECL_THREADPRIVATE_P (newdecl) = 1;
1981 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype;
1983 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1984 check_default_args (newdecl);
1986 /* Lay the type out, unless already done. */
1987 if (! same_type_p (newtype, oldtype)
1988 && TREE_TYPE (newdecl) != error_mark_node
1989 && !(processing_template_decl && uses_template_parms (newdecl)))
1990 layout_type (TREE_TYPE (newdecl));
1992 if ((VAR_P (newdecl)
1993 || TREE_CODE (newdecl) == PARM_DECL
1994 || TREE_CODE (newdecl) == RESULT_DECL
1995 || TREE_CODE (newdecl) == FIELD_DECL
1996 || TREE_CODE (newdecl) == TYPE_DECL)
1997 && !(processing_template_decl && uses_template_parms (newdecl)))
1998 layout_decl (newdecl, 0);
2000 /* Merge the type qualifiers. */
2001 if (TREE_READONLY (newdecl))
2002 TREE_READONLY (olddecl) = 1;
2003 if (TREE_THIS_VOLATILE (newdecl))
2004 TREE_THIS_VOLATILE (olddecl) = 1;
2005 if (TREE_NOTHROW (newdecl))
2006 TREE_NOTHROW (olddecl) = 1;
2008 /* Merge deprecatedness. */
2009 if (TREE_DEPRECATED (newdecl))
2010 TREE_DEPRECATED (olddecl) = 1;
2012 /* Preserve function specific target and optimization options */
2013 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2015 if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
2016 && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
2017 DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
2018 = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
2020 if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
2021 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
2022 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
2023 = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
2026 /* Merge the initialization information. */
2027 if (DECL_INITIAL (newdecl) == NULL_TREE
2028 && DECL_INITIAL (olddecl) != NULL_TREE)
2030 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2031 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2032 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2034 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
2035 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
2039 /* Merge the section attribute.
2040 We want to issue an error if the sections conflict but that must be
2041 done later in decl_attributes since we are called before attributes
2042 are assigned. */
2043 if ((DECL_EXTERNAL (olddecl) || TREE_PUBLIC (olddecl) || TREE_STATIC (olddecl))
2044 && DECL_SECTION_NAME (newdecl) == NULL
2045 && DECL_SECTION_NAME (olddecl) != NULL)
2046 set_decl_section_name (newdecl, DECL_SECTION_NAME (olddecl));
2048 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2050 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
2051 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
2052 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
2053 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
2054 TREE_NOTHROW (newdecl) |= TREE_NOTHROW (olddecl);
2055 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
2056 DECL_IS_OPERATOR_NEW (newdecl) |= DECL_IS_OPERATOR_NEW (olddecl);
2057 DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
2058 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
2059 DECL_LOOPING_CONST_OR_PURE_P (newdecl)
2060 |= DECL_LOOPING_CONST_OR_PURE_P (olddecl);
2061 /* Keep the old RTL. */
2062 COPY_DECL_RTL (olddecl, newdecl);
2064 else if (VAR_P (newdecl)
2065 && (DECL_SIZE (olddecl) || !DECL_SIZE (newdecl)))
2067 /* Keep the old RTL. We cannot keep the old RTL if the old
2068 declaration was for an incomplete object and the new
2069 declaration is not since many attributes of the RTL will
2070 change. */
2071 COPY_DECL_RTL (olddecl, newdecl);
2074 /* If cannot merge, then use the new type and qualifiers,
2075 and don't preserve the old rtl. */
2076 else
2078 /* Clean out any memory we had of the old declaration. */
2079 tree oldstatic = value_member (olddecl, static_aggregates);
2080 if (oldstatic)
2081 TREE_VALUE (oldstatic) = error_mark_node;
2083 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
2084 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
2085 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
2086 TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
2089 /* Merge the storage class information. */
2090 merge_weak (newdecl, olddecl);
2092 if ((TREE_CODE (olddecl) == FUNCTION_DECL || TREE_CODE (olddecl) == VAR_DECL)
2093 && (DECL_EXTERNAL (olddecl) || TREE_PUBLIC (olddecl) || TREE_STATIC (olddecl))
2094 && DECL_ONE_ONLY (olddecl))
2096 struct symtab_node *symbol;
2097 if (TREE_CODE (olddecl) == FUNCTION_DECL)
2098 symbol = cgraph_node::get_create (newdecl);
2099 else
2100 symbol = varpool_node::get_create (newdecl);
2101 symbol->set_comdat_group (symtab_node::get
2102 (olddecl)->get_comdat_group ());
2105 DECL_DEFER_OUTPUT (newdecl) |= DECL_DEFER_OUTPUT (olddecl);
2106 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
2107 TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl);
2108 if (! DECL_EXTERNAL (olddecl))
2109 DECL_EXTERNAL (newdecl) = 0;
2111 new_template_info = NULL_TREE;
2112 if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl))
2114 bool new_redefines_gnu_inline = false;
2116 if (new_defines_function
2117 && ((DECL_INTERFACE_KNOWN (olddecl)
2118 && TREE_CODE (olddecl) == FUNCTION_DECL)
2119 || (TREE_CODE (olddecl) == TEMPLATE_DECL
2120 && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl))
2121 == FUNCTION_DECL))))
2123 tree fn = olddecl;
2125 if (TREE_CODE (fn) == TEMPLATE_DECL)
2126 fn = DECL_TEMPLATE_RESULT (olddecl);
2128 new_redefines_gnu_inline = GNU_INLINE_P (fn) && DECL_INITIAL (fn);
2131 if (!new_redefines_gnu_inline)
2133 DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl);
2134 DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl);
2135 DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl);
2137 DECL_TEMPLATE_INSTANTIATED (newdecl)
2138 |= DECL_TEMPLATE_INSTANTIATED (olddecl);
2139 DECL_ODR_USED (newdecl) |= DECL_ODR_USED (olddecl);
2141 /* If the OLDDECL is an instantiation and/or specialization,
2142 then the NEWDECL must be too. But, it may not yet be marked
2143 as such if the caller has created NEWDECL, but has not yet
2144 figured out that it is a redeclaration. */
2145 if (!DECL_USE_TEMPLATE (newdecl))
2146 DECL_USE_TEMPLATE (newdecl) = DECL_USE_TEMPLATE (olddecl);
2148 /* Don't really know how much of the language-specific
2149 values we should copy from old to new. */
2150 DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl);
2151 DECL_REPO_AVAILABLE_P (newdecl) = DECL_REPO_AVAILABLE_P (olddecl);
2152 DECL_INITIALIZED_IN_CLASS_P (newdecl)
2153 |= DECL_INITIALIZED_IN_CLASS_P (olddecl);
2155 if (LANG_DECL_HAS_MIN (newdecl))
2157 DECL_LANG_SPECIFIC (newdecl)->u.min.u2 =
2158 DECL_LANG_SPECIFIC (olddecl)->u.min.u2;
2159 if (DECL_TEMPLATE_INFO (newdecl))
2160 new_template_info = DECL_TEMPLATE_INFO (newdecl);
2161 DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl);
2163 /* Only functions have these fields. */
2164 if (DECL_DECLARES_FUNCTION_P (newdecl))
2166 DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl);
2167 olddecl_friend = DECL_FRIEND_P (olddecl);
2168 hidden_friend = (DECL_ANTICIPATED (olddecl)
2169 && DECL_HIDDEN_FRIEND_P (olddecl)
2170 && newdecl_is_friend);
2171 DECL_BEFRIENDING_CLASSES (newdecl)
2172 = chainon (DECL_BEFRIENDING_CLASSES (newdecl),
2173 DECL_BEFRIENDING_CLASSES (olddecl));
2174 /* DECL_THUNKS is only valid for virtual functions,
2175 otherwise it is a DECL_FRIEND_CONTEXT. */
2176 if (DECL_VIRTUAL_P (newdecl))
2177 SET_DECL_THUNKS (newdecl, DECL_THUNKS (olddecl));
2179 /* Only variables have this field. */
2180 else if (VAR_P (newdecl)
2181 && VAR_HAD_UNKNOWN_BOUND (olddecl))
2182 SET_VAR_HAD_UNKNOWN_BOUND (newdecl);
2185 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2187 tree parm;
2189 /* Merge parameter attributes. */
2190 tree oldarg, newarg;
2191 for (oldarg = DECL_ARGUMENTS(olddecl),
2192 newarg = DECL_ARGUMENTS(newdecl);
2193 oldarg && newarg;
2194 oldarg = DECL_CHAIN(oldarg), newarg = DECL_CHAIN(newarg)) {
2195 DECL_ATTRIBUTES (newarg)
2196 = (*targetm.merge_decl_attributes) (oldarg, newarg);
2197 DECL_ATTRIBUTES (oldarg) = DECL_ATTRIBUTES (newarg);
2200 if (DECL_TEMPLATE_INSTANTIATION (olddecl)
2201 && !DECL_TEMPLATE_INSTANTIATION (newdecl))
2203 /* If newdecl is not a specialization, then it is not a
2204 template-related function at all. And that means that we
2205 should have exited above, returning 0. */
2206 gcc_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl));
2208 if (DECL_ODR_USED (olddecl))
2209 /* From [temp.expl.spec]:
2211 If a template, a member template or the member of a class
2212 template is explicitly specialized then that
2213 specialization shall be declared before the first use of
2214 that specialization that would cause an implicit
2215 instantiation to take place, in every translation unit in
2216 which such a use occurs. */
2217 error ("explicit specialization of %qD after first use",
2218 olddecl);
2220 SET_DECL_TEMPLATE_SPECIALIZATION (olddecl);
2221 DECL_COMDAT (newdecl) = DECL_DECLARED_INLINE_P (olddecl);
2223 /* Don't propagate visibility from the template to the
2224 specialization here. We'll do that in determine_visibility if
2225 appropriate. */
2226 DECL_VISIBILITY_SPECIFIED (olddecl) = 0;
2228 /* [temp.expl.spec/14] We don't inline explicit specialization
2229 just because the primary template says so. */
2231 /* But still keep DECL_DISREGARD_INLINE_LIMITS in sync with
2232 the always_inline attribute. */
2233 if (DECL_DISREGARD_INLINE_LIMITS (olddecl)
2234 && !DECL_DISREGARD_INLINE_LIMITS (newdecl))
2236 if (DECL_DECLARED_INLINE_P (newdecl))
2237 DECL_DISREGARD_INLINE_LIMITS (newdecl) = true;
2238 else
2239 DECL_ATTRIBUTES (newdecl)
2240 = remove_attribute ("always_inline",
2241 DECL_ATTRIBUTES (newdecl));
2244 else if (new_defines_function && DECL_INITIAL (olddecl))
2246 /* Never inline re-defined extern inline functions.
2247 FIXME: this could be better handled by keeping both
2248 function as separate declarations. */
2249 DECL_UNINLINABLE (newdecl) = 1;
2251 else
2253 if (DECL_PENDING_INLINE_INFO (newdecl) == 0)
2254 DECL_PENDING_INLINE_INFO (newdecl) = DECL_PENDING_INLINE_INFO (olddecl);
2256 DECL_DECLARED_INLINE_P (newdecl) |= DECL_DECLARED_INLINE_P (olddecl);
2258 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
2259 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
2261 DECL_DISREGARD_INLINE_LIMITS (newdecl)
2262 = DECL_DISREGARD_INLINE_LIMITS (olddecl)
2263 = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
2264 || DECL_DISREGARD_INLINE_LIMITS (olddecl));
2267 /* Preserve abstractness on cloned [cd]tors. */
2268 DECL_ABSTRACT (newdecl) = DECL_ABSTRACT (olddecl);
2270 /* Update newdecl's parms to point at olddecl. */
2271 for (parm = DECL_ARGUMENTS (newdecl); parm;
2272 parm = DECL_CHAIN (parm))
2273 DECL_CONTEXT (parm) = olddecl;
2275 if (! types_match)
2277 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
2278 COPY_DECL_ASSEMBLER_NAME (newdecl, olddecl);
2279 COPY_DECL_RTL (newdecl, olddecl);
2281 if (! types_match || new_defines_function)
2283 /* These need to be copied so that the names are available.
2284 Note that if the types do match, we'll preserve inline
2285 info and other bits, but if not, we won't. */
2286 DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl);
2287 DECL_RESULT (olddecl) = DECL_RESULT (newdecl);
2289 /* If redeclaring a builtin function, it stays built in
2290 if newdecl is a gnu_inline definition, or if newdecl is just
2291 a declaration. */
2292 if (DECL_BUILT_IN (olddecl)
2293 && (new_defines_function ? GNU_INLINE_P (newdecl) : types_match))
2295 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
2296 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
2297 /* If we're keeping the built-in definition, keep the rtl,
2298 regardless of declaration matches. */
2299 COPY_DECL_RTL (olddecl, newdecl);
2300 if (DECL_BUILT_IN_CLASS (newdecl) == BUILT_IN_NORMAL)
2302 enum built_in_function fncode = DECL_FUNCTION_CODE (newdecl);
2303 switch (fncode)
2305 /* If a compatible prototype of these builtin functions
2306 is seen, assume the runtime implements it with the
2307 expected semantics. */
2308 case BUILT_IN_STPCPY:
2309 if (builtin_decl_explicit_p (fncode))
2310 set_builtin_decl_implicit_p (fncode, true);
2311 break;
2312 default:
2313 break;
2317 if (new_defines_function)
2318 /* If defining a function declared with other language
2319 linkage, use the previously declared language linkage. */
2320 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
2321 else if (types_match)
2323 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
2324 /* Don't clear out the arguments if we're just redeclaring a
2325 function. */
2326 if (DECL_ARGUMENTS (olddecl))
2327 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
2330 else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
2331 NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl);
2333 /* Now preserve various other info from the definition. */
2334 TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl);
2335 TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl);
2336 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
2337 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
2339 /* Warn about conflicting visibility specifications. */
2340 if (DECL_VISIBILITY_SPECIFIED (olddecl)
2341 && DECL_VISIBILITY_SPECIFIED (newdecl)
2342 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
2344 if (warning_at (DECL_SOURCE_LOCATION (newdecl), OPT_Wattributes,
2345 "%qD: visibility attribute ignored because it "
2346 "conflicts with previous declaration", newdecl))
2347 inform (DECL_SOURCE_LOCATION (olddecl),
2348 "previous declaration of %qD", olddecl);
2350 /* Choose the declaration which specified visibility. */
2351 if (DECL_VISIBILITY_SPECIFIED (olddecl))
2353 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
2354 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
2356 /* Init priority used to be merged from newdecl to olddecl by the memcpy,
2357 so keep this behavior. */
2358 if (VAR_P (newdecl) && DECL_HAS_INIT_PRIORITY_P (newdecl))
2360 SET_DECL_INIT_PRIORITY (olddecl, DECL_INIT_PRIORITY (newdecl));
2361 DECL_HAS_INIT_PRIORITY_P (olddecl) = 1;
2363 /* Likewise for DECL_ALIGN, DECL_USER_ALIGN and DECL_PACKED. */
2364 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
2366 DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
2367 DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
2369 DECL_USER_ALIGN (olddecl) = DECL_USER_ALIGN (newdecl);
2370 if (TREE_CODE (newdecl) == FIELD_DECL)
2371 DECL_PACKED (olddecl) = DECL_PACKED (newdecl);
2373 /* The DECL_LANG_SPECIFIC information in OLDDECL will be replaced
2374 with that from NEWDECL below. */
2375 if (DECL_LANG_SPECIFIC (olddecl))
2377 gcc_assert (DECL_LANG_SPECIFIC (olddecl)
2378 != DECL_LANG_SPECIFIC (newdecl));
2379 ggc_free (DECL_LANG_SPECIFIC (olddecl));
2382 /* Merge the USED information. */
2383 if (TREE_USED (olddecl))
2384 TREE_USED (newdecl) = 1;
2385 else if (TREE_USED (newdecl))
2386 TREE_USED (olddecl) = 1;
2387 if (VAR_P (newdecl))
2389 if (DECL_READ_P (olddecl))
2390 DECL_READ_P (newdecl) = 1;
2391 else if (DECL_READ_P (newdecl))
2392 DECL_READ_P (olddecl) = 1;
2394 if (DECL_PRESERVE_P (olddecl))
2395 DECL_PRESERVE_P (newdecl) = 1;
2396 else if (DECL_PRESERVE_P (newdecl))
2397 DECL_PRESERVE_P (olddecl) = 1;
2399 /* Merge the DECL_FUNCTION_VERSIONED information. newdecl will be copied
2400 to olddecl and deleted. */
2401 if (TREE_CODE (newdecl) == FUNCTION_DECL
2402 && DECL_FUNCTION_VERSIONED (olddecl))
2404 /* Set the flag for newdecl so that it gets copied to olddecl. */
2405 DECL_FUNCTION_VERSIONED (newdecl) = 1;
2406 /* newdecl will be purged after copying to olddecl and is no longer
2407 a version. */
2408 cgraph_node::delete_function_version (newdecl);
2411 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2413 int function_size;
2414 struct symtab_node *snode = symtab_node::get (olddecl);
2416 function_size = sizeof (struct tree_decl_common);
2418 memcpy ((char *) olddecl + sizeof (struct tree_common),
2419 (char *) newdecl + sizeof (struct tree_common),
2420 function_size - sizeof (struct tree_common));
2422 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2423 (char *) newdecl + sizeof (struct tree_decl_common),
2424 sizeof (struct tree_function_decl) - sizeof (struct tree_decl_common));
2426 /* Preserve symtab node mapping. */
2427 olddecl->decl_with_vis.symtab_node = snode;
2429 if (new_template_info)
2430 /* If newdecl is a template instantiation, it is possible that
2431 the following sequence of events has occurred:
2433 o A friend function was declared in a class template. The
2434 class template was instantiated.
2436 o The instantiation of the friend declaration was
2437 recorded on the instantiation list, and is newdecl.
2439 o Later, however, instantiate_class_template called pushdecl
2440 on the newdecl to perform name injection. But, pushdecl in
2441 turn called duplicate_decls when it discovered that another
2442 declaration of a global function with the same name already
2443 existed.
2445 o Here, in duplicate_decls, we decided to clobber newdecl.
2447 If we're going to do that, we'd better make sure that
2448 olddecl, and not newdecl, is on the list of
2449 instantiations so that if we try to do the instantiation
2450 again we won't get the clobbered declaration. */
2451 reregister_specialization (newdecl,
2452 new_template_info,
2453 olddecl);
2455 else
2457 size_t size = tree_code_size (TREE_CODE (olddecl));
2459 memcpy ((char *) olddecl + sizeof (struct tree_common),
2460 (char *) newdecl + sizeof (struct tree_common),
2461 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
2462 switch (TREE_CODE (olddecl))
2464 case LABEL_DECL:
2465 case VAR_DECL:
2466 case RESULT_DECL:
2467 case PARM_DECL:
2468 case FIELD_DECL:
2469 case TYPE_DECL:
2470 case CONST_DECL:
2472 struct symtab_node *snode = NULL;
2474 if (TREE_CODE (olddecl) == VAR_DECL
2475 && (TREE_STATIC (olddecl) || TREE_PUBLIC (olddecl) || DECL_EXTERNAL (olddecl)))
2476 snode = symtab_node::get (olddecl);
2477 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2478 (char *) newdecl + sizeof (struct tree_decl_common),
2479 size - sizeof (struct tree_decl_common)
2480 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
2481 if (TREE_CODE (olddecl) == VAR_DECL)
2482 olddecl->decl_with_vis.symtab_node = snode;
2484 break;
2485 default:
2486 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2487 (char *) newdecl + sizeof (struct tree_decl_common),
2488 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common)
2489 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
2490 break;
2493 DECL_UID (olddecl) = olddecl_uid;
2494 if (olddecl_friend)
2495 DECL_FRIEND_P (olddecl) = 1;
2496 if (hidden_friend)
2498 DECL_ANTICIPATED (olddecl) = 1;
2499 DECL_HIDDEN_FRIEND_P (olddecl) = 1;
2502 /* NEWDECL contains the merged attribute lists.
2503 Update OLDDECL to be the same. */
2504 DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
2506 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
2507 so that encode_section_info has a chance to look at the new decl
2508 flags and attributes. */
2509 if (DECL_RTL_SET_P (olddecl)
2510 && (TREE_CODE (olddecl) == FUNCTION_DECL
2511 || (VAR_P (olddecl)
2512 && TREE_STATIC (olddecl))))
2513 make_decl_rtl (olddecl);
2515 /* The NEWDECL will no longer be needed. Because every out-of-class
2516 declaration of a member results in a call to duplicate_decls,
2517 freeing these nodes represents in a significant savings.
2519 Before releasing the node, be sore to remove function from symbol
2520 table that might have been inserted there to record comdat group.
2521 Be sure to however do not free DECL_STRUCT_FUNCTION becuase this
2522 structure is shared in between newdecl and oldecl. */
2523 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2524 DECL_STRUCT_FUNCTION (newdecl) = NULL;
2525 if (TREE_CODE (newdecl) == FUNCTION_DECL
2526 || TREE_CODE (newdecl) == VAR_DECL)
2528 struct symtab_node *snode = symtab_node::get (newdecl);
2529 if (snode)
2530 snode->remove ();
2533 tree clone;
2534 bool found_clone = false;
2535 /* Fix dangling reference. */
2536 FOR_EACH_CLONE (clone, newdecl)
2538 if (DECL_CLONED_FUNCTION (clone) == newdecl)
2540 found_clone = true;
2541 break;
2543 if (DECL_ABSTRACT_ORIGIN (clone) == newdecl)
2545 found_clone = true;
2546 break;
2549 if (!found_clone)
2550 ggc_free (newdecl);
2553 return olddecl;
2556 /* Return zero if the declaration NEWDECL is valid
2557 when the declaration OLDDECL (assumed to be for the same name)
2558 has already been seen.
2559 Otherwise return an error message format string with a %s
2560 where the identifier should go. */
2562 static const char *
2563 redeclaration_error_message (tree newdecl, tree olddecl)
2565 if (TREE_CODE (newdecl) == TYPE_DECL)
2567 /* Because C++ can put things into name space for free,
2568 constructs like "typedef struct foo { ... } foo"
2569 would look like an erroneous redeclaration. */
2570 if (same_type_p (TREE_TYPE (newdecl), TREE_TYPE (olddecl)))
2571 return NULL;
2572 else
2573 return G_("redefinition of %q#D");
2575 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
2577 /* If this is a pure function, its olddecl will actually be
2578 the original initialization to `0' (which we force to call
2579 abort()). Don't complain about redefinition in this case. */
2580 if (DECL_LANG_SPECIFIC (olddecl) && DECL_PURE_VIRTUAL_P (olddecl)
2581 && DECL_INITIAL (olddecl) == NULL_TREE)
2582 return NULL;
2584 /* If both functions come from different namespaces, this is not
2585 a redeclaration - this is a conflict with a used function. */
2586 if (DECL_NAMESPACE_SCOPE_P (olddecl)
2587 && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl)
2588 && ! decls_match (olddecl, newdecl))
2589 return G_("%qD conflicts with used function");
2591 /* We'll complain about linkage mismatches in
2592 warn_extern_redeclared_static. */
2594 /* Defining the same name twice is no good. */
2595 if (DECL_INITIAL (olddecl) != NULL_TREE
2596 && DECL_INITIAL (newdecl) != NULL_TREE)
2598 if (DECL_NAME (olddecl) == NULL_TREE)
2599 return G_("%q#D not declared in class");
2600 else if (!GNU_INLINE_P (olddecl)
2601 || GNU_INLINE_P (newdecl))
2602 return G_("redefinition of %q#D");
2605 if (DECL_DECLARED_INLINE_P (olddecl) && DECL_DECLARED_INLINE_P (newdecl))
2607 bool olda = GNU_INLINE_P (olddecl);
2608 bool newa = GNU_INLINE_P (newdecl);
2610 if (olda != newa)
2612 if (newa)
2613 return G_("%q+D redeclared inline with "
2614 "%<gnu_inline%> attribute");
2615 else
2616 return G_("%q+D redeclared inline without "
2617 "%<gnu_inline%> attribute");
2621 check_abi_tag_redeclaration
2622 (olddecl, lookup_attribute ("abi_tag", DECL_ATTRIBUTES (olddecl)),
2623 lookup_attribute ("abi_tag", DECL_ATTRIBUTES (newdecl)));
2625 return NULL;
2627 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2629 tree nt, ot;
2631 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
2633 if (COMPLETE_TYPE_P (TREE_TYPE (newdecl))
2634 && COMPLETE_TYPE_P (TREE_TYPE (olddecl)))
2635 return G_("redefinition of %q#D");
2636 return NULL;
2639 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) != FUNCTION_DECL
2640 || (DECL_TEMPLATE_RESULT (newdecl)
2641 == DECL_TEMPLATE_RESULT (olddecl)))
2642 return NULL;
2644 nt = DECL_TEMPLATE_RESULT (newdecl);
2645 if (DECL_TEMPLATE_INFO (nt))
2646 nt = DECL_TEMPLATE_RESULT (template_for_substitution (nt));
2647 ot = DECL_TEMPLATE_RESULT (olddecl);
2648 if (DECL_TEMPLATE_INFO (ot))
2649 ot = DECL_TEMPLATE_RESULT (template_for_substitution (ot));
2650 if (DECL_INITIAL (nt) && DECL_INITIAL (ot)
2651 && (!GNU_INLINE_P (ot) || GNU_INLINE_P (nt)))
2652 return G_("redefinition of %q#D");
2654 if (DECL_DECLARED_INLINE_P (ot) && DECL_DECLARED_INLINE_P (nt))
2656 bool olda = GNU_INLINE_P (ot);
2657 bool newa = GNU_INLINE_P (nt);
2659 if (olda != newa)
2661 if (newa)
2662 return G_("%q+D redeclared inline with "
2663 "%<gnu_inline%> attribute");
2664 else
2665 return G_("%q+D redeclared inline without "
2666 "%<gnu_inline%> attribute");
2670 /* Core issue #226 (C++0x):
2672 If a friend function template declaration specifies a
2673 default template-argument, that declaration shall be a
2674 definition and shall be the only declaration of the
2675 function template in the translation unit. */
2676 if ((cxx_dialect != cxx98)
2677 && TREE_CODE (ot) == FUNCTION_DECL && DECL_FRIEND_P (ot)
2678 && !check_default_tmpl_args (nt, DECL_TEMPLATE_PARMS (newdecl),
2679 /*is_primary=*/true,
2680 /*is_partial=*/false,
2681 /*is_friend_decl=*/2))
2682 return G_("redeclaration of friend %q#D "
2683 "may not have default template arguments");
2685 return NULL;
2687 else if (VAR_P (newdecl)
2688 && DECL_THREAD_LOCAL_P (newdecl) != DECL_THREAD_LOCAL_P (olddecl)
2689 && (! DECL_LANG_SPECIFIC (olddecl)
2690 || ! CP_DECL_THREADPRIVATE_P (olddecl)
2691 || DECL_THREAD_LOCAL_P (newdecl)))
2693 /* Only variables can be thread-local, and all declarations must
2694 agree on this property. */
2695 if (DECL_THREAD_LOCAL_P (newdecl))
2696 return G_("thread-local declaration of %q#D follows "
2697 "non-thread-local declaration");
2698 else
2699 return G_("non-thread-local declaration of %q#D follows "
2700 "thread-local declaration");
2702 else if (toplevel_bindings_p () || DECL_NAMESPACE_SCOPE_P (newdecl))
2704 /* The objects have been declared at namespace scope. If either
2705 is a member of an anonymous union, then this is an invalid
2706 redeclaration. For example:
2708 int i;
2709 union { int i; };
2711 is invalid. */
2712 if ((VAR_P (newdecl) && DECL_ANON_UNION_VAR_P (newdecl))
2713 || (VAR_P (olddecl) && DECL_ANON_UNION_VAR_P (olddecl)))
2714 return G_("redeclaration of %q#D");
2715 /* If at least one declaration is a reference, there is no
2716 conflict. For example:
2718 int i = 3;
2719 extern int i;
2721 is valid. */
2722 if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
2723 return NULL;
2724 /* Reject two definitions. */
2725 return G_("redefinition of %q#D");
2727 else
2729 /* Objects declared with block scope: */
2730 /* Reject two definitions, and reject a definition
2731 together with an external reference. */
2732 if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl)))
2733 return G_("redeclaration of %q#D");
2734 return NULL;
2738 /* Hash and equality functions for the named_label table. */
2740 static hashval_t
2741 named_label_entry_hash (const void *data)
2743 const struct named_label_entry *ent = (const struct named_label_entry *) data;
2744 return DECL_UID (ent->label_decl);
2747 static int
2748 named_label_entry_eq (const void *a, const void *b)
2750 const struct named_label_entry *ent_a = (const struct named_label_entry *) a;
2751 const struct named_label_entry *ent_b = (const struct named_label_entry *) b;
2752 return ent_a->label_decl == ent_b->label_decl;
2755 /* Create a new label, named ID. */
2757 static tree
2758 make_label_decl (tree id, int local_p)
2760 struct named_label_entry *ent;
2761 void **slot;
2762 tree decl;
2764 decl = build_decl (input_location, LABEL_DECL, id, void_type_node);
2766 DECL_CONTEXT (decl) = current_function_decl;
2767 DECL_MODE (decl) = VOIDmode;
2768 C_DECLARED_LABEL_FLAG (decl) = local_p;
2770 /* Say where one reference is to the label, for the sake of the
2771 error if it is not defined. */
2772 DECL_SOURCE_LOCATION (decl) = input_location;
2774 /* Record the fact that this identifier is bound to this label. */
2775 SET_IDENTIFIER_LABEL_VALUE (id, decl);
2777 /* Create the label htab for the function on demand. */
2778 if (!named_labels)
2779 named_labels = htab_create_ggc (13, named_label_entry_hash,
2780 named_label_entry_eq, NULL);
2782 /* Record this label on the list of labels used in this function.
2783 We do this before calling make_label_decl so that we get the
2784 IDENTIFIER_LABEL_VALUE before the new label is declared. */
2785 ent = ggc_cleared_alloc<named_label_entry> ();
2786 ent->label_decl = decl;
2788 slot = htab_find_slot (named_labels, ent, INSERT);
2789 gcc_assert (*slot == NULL);
2790 *slot = ent;
2792 return decl;
2795 /* Look for a label named ID in the current function. If one cannot
2796 be found, create one. (We keep track of used, but undefined,
2797 labels, and complain about them at the end of a function.) */
2799 static tree
2800 lookup_label_1 (tree id)
2802 tree decl;
2804 /* You can't use labels at global scope. */
2805 if (current_function_decl == NULL_TREE)
2807 error ("label %qE referenced outside of any function", id);
2808 return NULL_TREE;
2811 /* See if we've already got this label. */
2812 decl = IDENTIFIER_LABEL_VALUE (id);
2813 if (decl != NULL_TREE && DECL_CONTEXT (decl) == current_function_decl)
2814 return decl;
2816 decl = make_label_decl (id, /*local_p=*/0);
2817 return decl;
2820 /* Wrapper for lookup_label_1. */
2822 tree
2823 lookup_label (tree id)
2825 tree ret;
2826 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
2827 ret = lookup_label_1 (id);
2828 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
2829 return ret;
2832 /* Declare a local label named ID. */
2834 tree
2835 declare_local_label (tree id)
2837 tree decl;
2838 cp_label_binding bind;
2840 /* Add a new entry to the SHADOWED_LABELS list so that when we leave
2841 this scope we can restore the old value of IDENTIFIER_TYPE_VALUE. */
2842 bind.prev_value = IDENTIFIER_LABEL_VALUE (id);
2844 decl = make_label_decl (id, /*local_p=*/1);
2845 bind.label = decl;
2846 vec_safe_push (current_binding_level->shadowed_labels, bind);
2848 return decl;
2851 /* Returns nonzero if it is ill-formed to jump past the declaration of
2852 DECL. Returns 2 if it's also a real problem. */
2854 static int
2855 decl_jump_unsafe (tree decl)
2857 /* [stmt.dcl]/3: A program that jumps from a point where a local variable
2858 with automatic storage duration is not in scope to a point where it is
2859 in scope is ill-formed unless the variable has scalar type, class type
2860 with a trivial default constructor and a trivial destructor, a
2861 cv-qualified version of one of these types, or an array of one of the
2862 preceding types and is declared without an initializer (8.5). */
2863 tree type = TREE_TYPE (decl);
2865 if (!VAR_P (decl) || TREE_STATIC (decl)
2866 || type == error_mark_node)
2867 return 0;
2869 if (DECL_NONTRIVIALLY_INITIALIZED_P (decl)
2870 || variably_modified_type_p (type, NULL_TREE))
2871 return 2;
2873 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
2874 return 1;
2876 return 0;
2879 /* A subroutine of check_previous_goto_1 to identify a branch to the user. */
2881 static void
2882 identify_goto (tree decl, const location_t *locus)
2884 if (decl)
2885 permerror (input_location, "jump to label %qD", decl);
2886 else
2887 permerror (input_location, "jump to case label");
2888 if (locus)
2889 permerror (*locus, " from here");
2892 /* Check that a single previously seen jump to a newly defined label
2893 is OK. DECL is the LABEL_DECL or 0; LEVEL is the binding_level for
2894 the jump context; NAMES are the names in scope in LEVEL at the jump
2895 context; LOCUS is the source position of the jump or 0. Returns
2896 true if all is well. */
2898 static bool
2899 check_previous_goto_1 (tree decl, cp_binding_level* level, tree names,
2900 bool exited_omp, const location_t *locus)
2902 cp_binding_level *b;
2903 bool identified = false, saw_eh = false, saw_omp = false;
2905 if (exited_omp)
2907 identify_goto (decl, locus);
2908 error (" exits OpenMP structured block");
2909 identified = saw_omp = true;
2912 for (b = current_binding_level; b ; b = b->level_chain)
2914 tree new_decls, old_decls = (b == level ? names : NULL_TREE);
2916 for (new_decls = b->names; new_decls != old_decls;
2917 new_decls = (DECL_P (new_decls) ? DECL_CHAIN (new_decls)
2918 : TREE_CHAIN (new_decls)))
2920 int problem = decl_jump_unsafe (new_decls);
2921 if (! problem)
2922 continue;
2924 if (!identified)
2926 identify_goto (decl, locus);
2927 identified = true;
2929 if (problem > 1)
2930 error (" crosses initialization of %q+#D", new_decls);
2931 else
2932 permerror (input_location, " enters scope of %q+#D which has "
2933 "non-trivial destructor", new_decls);
2936 if (b == level)
2937 break;
2938 if ((b->kind == sk_try || b->kind == sk_catch) && !saw_eh)
2940 if (!identified)
2942 identify_goto (decl, locus);
2943 identified = true;
2945 if (b->kind == sk_try)
2946 error (" enters try block");
2947 else
2948 error (" enters catch block");
2949 saw_eh = true;
2951 if (b->kind == sk_omp && !saw_omp)
2953 if (!identified)
2955 identify_goto (decl, locus);
2956 identified = true;
2958 error (" enters OpenMP structured block");
2959 saw_omp = true;
2963 return !identified;
2966 static void
2967 check_previous_goto (tree decl, struct named_label_use_entry *use)
2969 check_previous_goto_1 (decl, use->binding_level,
2970 use->names_in_scope, use->in_omp_scope,
2971 &use->o_goto_locus);
2974 static bool
2975 check_switch_goto (cp_binding_level* level)
2977 return check_previous_goto_1 (NULL_TREE, level, level->names, false, NULL);
2980 /* Check that a new jump to a label DECL is OK. Called by
2981 finish_goto_stmt. */
2983 void
2984 check_goto (tree decl)
2986 struct named_label_entry *ent, dummy;
2987 bool saw_catch = false, identified = false;
2988 tree bad;
2989 unsigned ix;
2991 /* We can't know where a computed goto is jumping.
2992 So we assume that it's OK. */
2993 if (TREE_CODE (decl) != LABEL_DECL)
2994 return;
2996 /* We didn't record any information about this label when we created it,
2997 and there's not much point since it's trivial to analyze as a return. */
2998 if (decl == cdtor_label)
2999 return;
3001 dummy.label_decl = decl;
3002 ent = (struct named_label_entry *) htab_find (named_labels, &dummy);
3003 gcc_assert (ent != NULL);
3005 /* If the label hasn't been defined yet, defer checking. */
3006 if (! DECL_INITIAL (decl))
3008 struct named_label_use_entry *new_use;
3010 /* Don't bother creating another use if the last goto had the
3011 same data, and will therefore create the same set of errors. */
3012 if (ent->uses
3013 && ent->uses->names_in_scope == current_binding_level->names)
3014 return;
3016 new_use = ggc_alloc<named_label_use_entry> ();
3017 new_use->binding_level = current_binding_level;
3018 new_use->names_in_scope = current_binding_level->names;
3019 new_use->o_goto_locus = input_location;
3020 new_use->in_omp_scope = false;
3022 new_use->next = ent->uses;
3023 ent->uses = new_use;
3024 return;
3027 if (ent->in_try_scope || ent->in_catch_scope
3028 || ent->in_omp_scope || !vec_safe_is_empty (ent->bad_decls))
3030 permerror (input_location, "jump to label %q+D", decl);
3031 permerror (input_location, " from here");
3032 identified = true;
3035 FOR_EACH_VEC_SAFE_ELT (ent->bad_decls, ix, bad)
3037 int u = decl_jump_unsafe (bad);
3039 if (u > 1 && DECL_ARTIFICIAL (bad))
3041 /* Can't skip init of __exception_info. */
3042 error_at (DECL_SOURCE_LOCATION (bad), " enters catch block");
3043 saw_catch = true;
3045 else if (u > 1)
3046 error (" skips initialization of %q+#D", bad);
3047 else
3048 permerror (input_location, " enters scope of %q+#D which has "
3049 "non-trivial destructor", bad);
3052 if (ent->in_try_scope)
3053 error (" enters try block");
3054 else if (ent->in_catch_scope && !saw_catch)
3055 error (" enters catch block");
3057 if (ent->in_omp_scope)
3058 error (" enters OpenMP structured block");
3059 else if (flag_openmp)
3061 cp_binding_level *b;
3062 for (b = current_binding_level; b ; b = b->level_chain)
3064 if (b == ent->binding_level)
3065 break;
3066 if (b->kind == sk_omp)
3068 if (!identified)
3070 permerror (input_location, "jump to label %q+D", decl);
3071 permerror (input_location, " from here");
3072 identified = true;
3074 error (" exits OpenMP structured block");
3075 break;
3081 /* Check that a return is ok wrt OpenMP structured blocks.
3082 Called by finish_return_stmt. Returns true if all is well. */
3084 bool
3085 check_omp_return (void)
3087 cp_binding_level *b;
3088 for (b = current_binding_level; b ; b = b->level_chain)
3089 if (b->kind == sk_omp)
3091 error ("invalid exit from OpenMP structured block");
3092 return false;
3094 else if (b->kind == sk_function_parms)
3095 break;
3096 return true;
3099 /* Define a label, specifying the location in the source file.
3100 Return the LABEL_DECL node for the label. */
3102 static tree
3103 define_label_1 (location_t location, tree name)
3105 struct named_label_entry *ent, dummy;
3106 cp_binding_level *p;
3107 tree decl;
3109 decl = lookup_label (name);
3111 dummy.label_decl = decl;
3112 ent = (struct named_label_entry *) htab_find (named_labels, &dummy);
3113 gcc_assert (ent != NULL);
3115 /* After labels, make any new cleanups in the function go into their
3116 own new (temporary) binding contour. */
3117 for (p = current_binding_level;
3118 p->kind != sk_function_parms;
3119 p = p->level_chain)
3120 p->more_cleanups_ok = 0;
3122 if (name == get_identifier ("wchar_t"))
3123 permerror (input_location, "label named wchar_t");
3125 if (DECL_INITIAL (decl) != NULL_TREE)
3127 error ("duplicate label %qD", decl);
3128 return error_mark_node;
3130 else
3132 struct named_label_use_entry *use;
3134 /* Mark label as having been defined. */
3135 DECL_INITIAL (decl) = error_mark_node;
3136 /* Say where in the source. */
3137 DECL_SOURCE_LOCATION (decl) = location;
3139 ent->binding_level = current_binding_level;
3140 ent->names_in_scope = current_binding_level->names;
3142 for (use = ent->uses; use ; use = use->next)
3143 check_previous_goto (decl, use);
3144 ent->uses = NULL;
3147 return decl;
3150 /* Wrapper for define_label_1. */
3152 tree
3153 define_label (location_t location, tree name)
3155 tree ret;
3156 bool running = timevar_cond_start (TV_NAME_LOOKUP);
3157 ret = define_label_1 (location, name);
3158 timevar_cond_stop (TV_NAME_LOOKUP, running);
3159 return ret;
3163 struct cp_switch
3165 cp_binding_level *level;
3166 struct cp_switch *next;
3167 /* The SWITCH_STMT being built. */
3168 tree switch_stmt;
3169 /* A splay-tree mapping the low element of a case range to the high
3170 element, or NULL_TREE if there is no high element. Used to
3171 determine whether or not a new case label duplicates an old case
3172 label. We need a tree, rather than simply a hash table, because
3173 of the GNU case range extension. */
3174 splay_tree cases;
3177 /* A stack of the currently active switch statements. The innermost
3178 switch statement is on the top of the stack. There is no need to
3179 mark the stack for garbage collection because it is only active
3180 during the processing of the body of a function, and we never
3181 collect at that point. */
3183 static struct cp_switch *switch_stack;
3185 /* Called right after a switch-statement condition is parsed.
3186 SWITCH_STMT is the switch statement being parsed. */
3188 void
3189 push_switch (tree switch_stmt)
3191 struct cp_switch *p = XNEW (struct cp_switch);
3192 p->level = current_binding_level;
3193 p->next = switch_stack;
3194 p->switch_stmt = switch_stmt;
3195 p->cases = splay_tree_new (case_compare, NULL, NULL);
3196 switch_stack = p;
3199 void
3200 pop_switch (void)
3202 struct cp_switch *cs = switch_stack;
3203 location_t switch_location;
3205 /* Emit warnings as needed. */
3206 switch_location = EXPR_LOC_OR_LOC (cs->switch_stmt, input_location);
3207 if (!processing_template_decl)
3208 c_do_switch_warnings (cs->cases, switch_location,
3209 SWITCH_STMT_TYPE (cs->switch_stmt),
3210 SWITCH_STMT_COND (cs->switch_stmt));
3212 splay_tree_delete (cs->cases);
3213 switch_stack = switch_stack->next;
3214 free (cs);
3217 /* Convert a case constant VALUE in a switch to the type TYPE of the switch
3218 condition. Note that if TYPE and VALUE are already integral we don't
3219 really do the conversion because the language-independent
3220 warning/optimization code will work better that way. */
3222 static tree
3223 case_conversion (tree type, tree value)
3225 if (value == NULL_TREE)
3226 return value;
3228 if (cxx_dialect >= cxx11
3229 && (SCOPED_ENUM_P (type)
3230 || !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (value))))
3232 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
3233 type = type_promotes_to (type);
3234 value = (perform_implicit_conversion_flags
3235 (type, value, tf_warning_or_error,
3236 LOOKUP_IMPLICIT | LOOKUP_NO_NON_INTEGRAL));
3238 return cxx_constant_value (value);
3241 /* Note that we've seen a definition of a case label, and complain if this
3242 is a bad place for one. */
3244 tree
3245 finish_case_label (location_t loc, tree low_value, tree high_value)
3247 tree cond, r;
3248 cp_binding_level *p;
3249 tree type;
3251 if (processing_template_decl)
3253 tree label;
3255 /* For templates, just add the case label; we'll do semantic
3256 analysis at instantiation-time. */
3257 label = build_decl (loc, LABEL_DECL, NULL_TREE, NULL_TREE);
3258 return add_stmt (build_case_label (low_value, high_value, label));
3261 /* Find the condition on which this switch statement depends. */
3262 cond = SWITCH_STMT_COND (switch_stack->switch_stmt);
3263 if (cond && TREE_CODE (cond) == TREE_LIST)
3264 cond = TREE_VALUE (cond);
3266 if (!check_switch_goto (switch_stack->level))
3267 return error_mark_node;
3269 type = SWITCH_STMT_TYPE (switch_stack->switch_stmt);
3271 low_value = case_conversion (type, low_value);
3272 high_value = case_conversion (type, high_value);
3274 r = c_add_case_label (loc, switch_stack->cases, cond, type,
3275 low_value, high_value);
3277 /* After labels, make any new cleanups in the function go into their
3278 own new (temporary) binding contour. */
3279 for (p = current_binding_level;
3280 p->kind != sk_function_parms;
3281 p = p->level_chain)
3282 p->more_cleanups_ok = 0;
3284 return r;
3287 /* Hash a TYPENAME_TYPE. K is really of type `tree'. */
3289 static hashval_t
3290 typename_hash (const void* k)
3292 hashval_t hash;
3293 const_tree const t = (const_tree) k;
3295 hash = (htab_hash_pointer (TYPE_CONTEXT (t))
3296 ^ htab_hash_pointer (TYPE_IDENTIFIER (t)));
3298 return hash;
3301 typedef struct typename_info {
3302 tree scope;
3303 tree name;
3304 tree template_id;
3305 bool enum_p;
3306 bool class_p;
3307 } typename_info;
3309 /* Compare two TYPENAME_TYPEs. K1 is really of type `tree', K2 is
3310 really of type `typename_info*' */
3312 static int
3313 typename_compare (const void * k1, const void * k2)
3315 const_tree const t1 = (const_tree) k1;
3316 const typename_info *const t2 = (const typename_info *) k2;
3318 return (TYPE_IDENTIFIER (t1) == t2->name
3319 && TYPE_CONTEXT (t1) == t2->scope
3320 && TYPENAME_TYPE_FULLNAME (t1) == t2->template_id
3321 && TYPENAME_IS_ENUM_P (t1) == t2->enum_p
3322 && TYPENAME_IS_CLASS_P (t1) == t2->class_p);
3325 /* Build a TYPENAME_TYPE. If the type is `typename T::t', CONTEXT is
3326 the type of `T', NAME is the IDENTIFIER_NODE for `t'.
3328 Returns the new TYPENAME_TYPE. */
3330 static GTY ((param_is (union tree_node))) htab_t typename_htab;
3332 static tree
3333 build_typename_type (tree context, tree name, tree fullname,
3334 enum tag_types tag_type)
3336 tree t;
3337 tree d;
3338 typename_info ti;
3339 void **e;
3340 hashval_t hash;
3342 if (typename_htab == NULL)
3343 typename_htab = htab_create_ggc (61, &typename_hash,
3344 &typename_compare, NULL);
3346 ti.scope = FROB_CONTEXT (context);
3347 ti.name = name;
3348 ti.template_id = fullname;
3349 ti.enum_p = tag_type == enum_type;
3350 ti.class_p = (tag_type == class_type
3351 || tag_type == record_type
3352 || tag_type == union_type);
3353 hash = (htab_hash_pointer (ti.scope)
3354 ^ htab_hash_pointer (ti.name));
3356 /* See if we already have this type. */
3357 e = htab_find_slot_with_hash (typename_htab, &ti, hash, INSERT);
3358 if (*e)
3359 t = (tree) *e;
3360 else
3362 /* Build the TYPENAME_TYPE. */
3363 t = cxx_make_type (TYPENAME_TYPE);
3364 TYPE_CONTEXT (t) = ti.scope;
3365 TYPENAME_TYPE_FULLNAME (t) = ti.template_id;
3366 TYPENAME_IS_ENUM_P (t) = ti.enum_p;
3367 TYPENAME_IS_CLASS_P (t) = ti.class_p;
3369 /* Build the corresponding TYPE_DECL. */
3370 d = build_decl (input_location, TYPE_DECL, name, t);
3371 TYPE_NAME (TREE_TYPE (d)) = d;
3372 TYPE_STUB_DECL (TREE_TYPE (d)) = d;
3373 DECL_CONTEXT (d) = FROB_CONTEXT (context);
3374 DECL_ARTIFICIAL (d) = 1;
3376 /* Store it in the hash table. */
3377 *e = t;
3379 /* TYPENAME_TYPEs must always be compared structurally, because
3380 they may or may not resolve down to another type depending on
3381 the currently open classes. */
3382 SET_TYPE_STRUCTURAL_EQUALITY (t);
3385 return t;
3388 /* Resolve `typename CONTEXT::NAME'. TAG_TYPE indicates the tag
3389 provided to name the type. Returns an appropriate type, unless an
3390 error occurs, in which case error_mark_node is returned. If we
3391 locate a non-artificial TYPE_DECL and TF_KEEP_TYPE_DECL is set, we
3392 return that, rather than the _TYPE it corresponds to, in other
3393 cases we look through the type decl. If TF_ERROR is set, complain
3394 about errors, otherwise be quiet. */
3396 tree
3397 make_typename_type (tree context, tree name, enum tag_types tag_type,
3398 tsubst_flags_t complain)
3400 tree fullname;
3401 tree t;
3402 bool want_template;
3404 if (name == error_mark_node
3405 || context == NULL_TREE
3406 || context == error_mark_node)
3407 return error_mark_node;
3409 if (TYPE_P (name))
3411 if (!(TYPE_LANG_SPECIFIC (name)
3412 && (CLASSTYPE_IS_TEMPLATE (name)
3413 || CLASSTYPE_USE_TEMPLATE (name))))
3414 name = TYPE_IDENTIFIER (name);
3415 else
3416 /* Create a TEMPLATE_ID_EXPR for the type. */
3417 name = build_nt (TEMPLATE_ID_EXPR,
3418 CLASSTYPE_TI_TEMPLATE (name),
3419 CLASSTYPE_TI_ARGS (name));
3421 else if (TREE_CODE (name) == TYPE_DECL)
3422 name = DECL_NAME (name);
3424 fullname = name;
3426 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3428 name = TREE_OPERAND (name, 0);
3429 if (TREE_CODE (name) == TEMPLATE_DECL)
3430 name = TREE_OPERAND (fullname, 0) = DECL_NAME (name);
3431 else if (TREE_CODE (name) == OVERLOAD)
3433 if (complain & tf_error)
3434 error ("%qD is not a type", name);
3435 return error_mark_node;
3438 if (TREE_CODE (name) == TEMPLATE_DECL)
3440 if (complain & tf_error)
3441 error ("%qD used without template parameters", name);
3442 return error_mark_node;
3444 gcc_assert (identifier_p (name));
3445 gcc_assert (TYPE_P (context));
3447 if (!MAYBE_CLASS_TYPE_P (context))
3449 if (complain & tf_error)
3450 error ("%q#T is not a class", context);
3451 return error_mark_node;
3454 /* When the CONTEXT is a dependent type, NAME could refer to a
3455 dependent base class of CONTEXT. But look inside it anyway
3456 if CONTEXT is a currently open scope, in case it refers to a
3457 member of the current instantiation or a non-dependent base;
3458 lookup will stop when we hit a dependent base. */
3459 if (!dependent_scope_p (context))
3460 /* We should only set WANT_TYPE when we're a nested typename type.
3461 Then we can give better diagnostics if we find a non-type. */
3462 t = lookup_field (context, name, 2, /*want_type=*/true);
3463 else
3464 t = NULL_TREE;
3466 if ((!t || TREE_CODE (t) == TREE_LIST) && dependent_type_p (context))
3467 return build_typename_type (context, name, fullname, tag_type);
3469 want_template = TREE_CODE (fullname) == TEMPLATE_ID_EXPR;
3471 if (!t)
3473 if (complain & tf_error)
3474 error (want_template ? G_("no class template named %q#T in %q#T")
3475 : G_("no type named %q#T in %q#T"), name, context);
3476 return error_mark_node;
3479 /* Pull out the template from an injected-class-name (or multiple). */
3480 if (want_template)
3481 t = maybe_get_template_decl_from_type_decl (t);
3483 if (TREE_CODE (t) == TREE_LIST)
3485 if (complain & tf_error)
3487 error ("lookup of %qT in %qT is ambiguous", name, context);
3488 print_candidates (t);
3490 return error_mark_node;
3493 if (want_template && !DECL_TYPE_TEMPLATE_P (t))
3495 if (complain & tf_error)
3496 error ("%<typename %T::%D%> names %q#T, which is not a class template",
3497 context, name, t);
3498 return error_mark_node;
3500 if (!want_template && TREE_CODE (t) != TYPE_DECL)
3502 if (complain & tf_error)
3503 error ("%<typename %T::%D%> names %q#T, which is not a type",
3504 context, name, t);
3505 return error_mark_node;
3508 if (!perform_or_defer_access_check (TYPE_BINFO (context), t, t, complain))
3509 return error_mark_node;
3511 /* If we are currently parsing a template and if T is a typedef accessed
3512 through CONTEXT then we need to remember and check access of T at
3513 template instantiation time. */
3514 add_typedef_to_current_template_for_access_check (t, context, input_location);
3516 if (want_template)
3517 return lookup_template_class (t, TREE_OPERAND (fullname, 1),
3518 NULL_TREE, context,
3519 /*entering_scope=*/0,
3520 tf_warning_or_error | tf_user);
3522 if (DECL_ARTIFICIAL (t) || !(complain & tf_keep_type_decl))
3523 t = TREE_TYPE (t);
3525 maybe_record_typedef_use (t);
3527 return t;
3530 /* Resolve `CONTEXT::template NAME'. Returns a TEMPLATE_DECL if the name
3531 can be resolved or an UNBOUND_CLASS_TEMPLATE, unless an error occurs,
3532 in which case error_mark_node is returned.
3534 If PARM_LIST is non-NULL, also make sure that the template parameter
3535 list of TEMPLATE_DECL matches.
3537 If COMPLAIN zero, don't complain about any errors that occur. */
3539 tree
3540 make_unbound_class_template (tree context, tree name, tree parm_list,
3541 tsubst_flags_t complain)
3543 tree t;
3544 tree d;
3546 if (TYPE_P (name))
3547 name = TYPE_IDENTIFIER (name);
3548 else if (DECL_P (name))
3549 name = DECL_NAME (name);
3550 gcc_assert (identifier_p (name));
3552 if (!dependent_type_p (context)
3553 || currently_open_class (context))
3555 tree tmpl = NULL_TREE;
3557 if (MAYBE_CLASS_TYPE_P (context))
3558 tmpl = lookup_field (context, name, 0, false);
3560 if (tmpl && TREE_CODE (tmpl) == TYPE_DECL)
3561 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
3563 if (!tmpl || !DECL_TYPE_TEMPLATE_P (tmpl))
3565 if (complain & tf_error)
3566 error ("no class template named %q#T in %q#T", name, context);
3567 return error_mark_node;
3570 if (parm_list
3571 && !comp_template_parms (DECL_TEMPLATE_PARMS (tmpl), parm_list))
3573 if (complain & tf_error)
3575 error ("template parameters do not match template %qD", tmpl);
3576 inform (DECL_SOURCE_LOCATION (tmpl),
3577 "%qD declared here", tmpl);
3579 return error_mark_node;
3582 if (!perform_or_defer_access_check (TYPE_BINFO (context), tmpl, tmpl,
3583 complain))
3584 return error_mark_node;
3586 return tmpl;
3589 /* Build the UNBOUND_CLASS_TEMPLATE. */
3590 t = cxx_make_type (UNBOUND_CLASS_TEMPLATE);
3591 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
3592 TREE_TYPE (t) = NULL_TREE;
3593 SET_TYPE_STRUCTURAL_EQUALITY (t);
3595 /* Build the corresponding TEMPLATE_DECL. */
3596 d = build_decl (input_location, TEMPLATE_DECL, name, t);
3597 TYPE_NAME (TREE_TYPE (d)) = d;
3598 TYPE_STUB_DECL (TREE_TYPE (d)) = d;
3599 DECL_CONTEXT (d) = FROB_CONTEXT (context);
3600 DECL_ARTIFICIAL (d) = 1;
3601 DECL_TEMPLATE_PARMS (d) = parm_list;
3603 return t;
3608 /* Push the declarations of builtin types into the namespace.
3609 RID_INDEX is the index of the builtin type in the array
3610 RID_POINTERS. NAME is the name used when looking up the builtin
3611 type. TYPE is the _TYPE node for the builtin type. */
3613 void
3614 record_builtin_type (enum rid rid_index,
3615 const char* name,
3616 tree type)
3618 tree rname = NULL_TREE, tname = NULL_TREE;
3619 tree tdecl = NULL_TREE;
3621 if ((int) rid_index < (int) RID_MAX)
3622 rname = ridpointers[(int) rid_index];
3623 if (name)
3624 tname = get_identifier (name);
3626 /* The calls to SET_IDENTIFIER_GLOBAL_VALUE below should be
3627 eliminated. Built-in types should not be looked up name; their
3628 names are keywords that the parser can recognize. However, there
3629 is code in c-common.c that uses identifier_global_value to look
3630 up built-in types by name. */
3631 if (tname)
3633 tdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, tname, type);
3634 DECL_ARTIFICIAL (tdecl) = 1;
3635 SET_IDENTIFIER_GLOBAL_VALUE (tname, tdecl);
3637 if (rname)
3639 if (!tdecl)
3641 tdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, rname, type);
3642 DECL_ARTIFICIAL (tdecl) = 1;
3644 SET_IDENTIFIER_GLOBAL_VALUE (rname, tdecl);
3647 if (!TYPE_NAME (type))
3648 TYPE_NAME (type) = tdecl;
3650 if (tdecl)
3651 debug_hooks->type_decl (tdecl, 0);
3654 /* Record one of the standard Java types.
3655 * Declare it as having the given NAME.
3656 * If SIZE > 0, it is the size of one of the integral types;
3657 * otherwise it is the negative of the size of one of the other types. */
3659 static tree
3660 record_builtin_java_type (const char* name, int size)
3662 tree type, decl;
3663 if (size > 0)
3665 type = build_nonstandard_integer_type (size, 0);
3666 type = build_distinct_type_copy (type);
3668 else if (size > -32)
3670 tree stype;
3671 /* "__java_char" or ""__java_boolean". */
3672 type = build_nonstandard_integer_type (-size, 1);
3673 type = build_distinct_type_copy (type);
3674 /* Get the signed type cached and attached to the unsigned type,
3675 so it doesn't get garbage-collected at "random" times,
3676 causing potential codegen differences out of different UIDs
3677 and different alias set numbers. */
3678 stype = build_nonstandard_integer_type (-size, 0);
3679 stype = build_distinct_type_copy (stype);
3680 TREE_CHAIN (type) = stype;
3681 /*if (size == -1) TREE_SET_CODE (type, BOOLEAN_TYPE);*/
3683 else
3684 { /* "__java_float" or ""__java_double". */
3685 type = make_node (REAL_TYPE);
3686 TYPE_PRECISION (type) = - size;
3687 layout_type (type);
3689 record_builtin_type (RID_MAX, name, type);
3690 decl = TYPE_NAME (type);
3692 /* Suppress generate debug symbol entries for these types,
3693 since for normal C++ they are just clutter.
3694 However, push_lang_context undoes this if extern "Java" is seen. */
3695 DECL_IGNORED_P (decl) = 1;
3697 TYPE_FOR_JAVA (type) = 1;
3698 return type;
3701 /* Push a type into the namespace so that the back ends ignore it. */
3703 static void
3704 record_unknown_type (tree type, const char* name)
3706 tree decl = pushdecl (build_decl (UNKNOWN_LOCATION,
3707 TYPE_DECL, get_identifier (name), type));
3708 /* Make sure the "unknown type" typedecl gets ignored for debug info. */
3709 DECL_IGNORED_P (decl) = 1;
3710 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
3711 TYPE_SIZE (type) = TYPE_SIZE (void_type_node);
3712 TYPE_ALIGN (type) = 1;
3713 TYPE_USER_ALIGN (type) = 0;
3714 SET_TYPE_MODE (type, TYPE_MODE (void_type_node));
3717 /* A string for which we should create an IDENTIFIER_NODE at
3718 startup. */
3720 typedef struct predefined_identifier
3722 /* The name of the identifier. */
3723 const char *const name;
3724 /* The place where the IDENTIFIER_NODE should be stored. */
3725 tree *const node;
3726 /* Nonzero if this is the name of a constructor or destructor. */
3727 const int ctor_or_dtor_p;
3728 } predefined_identifier;
3730 /* Create all the predefined identifiers. */
3732 static void
3733 initialize_predefined_identifiers (void)
3735 const predefined_identifier *pid;
3737 /* A table of identifiers to create at startup. */
3738 static const predefined_identifier predefined_identifiers[] = {
3739 { "C++", &lang_name_cplusplus, 0 },
3740 { "C", &lang_name_c, 0 },
3741 { "Java", &lang_name_java, 0 },
3742 /* Some of these names have a trailing space so that it is
3743 impossible for them to conflict with names written by users. */
3744 { "__ct ", &ctor_identifier, 1 },
3745 { "__base_ctor ", &base_ctor_identifier, 1 },
3746 { "__comp_ctor ", &complete_ctor_identifier, 1 },
3747 { "__dt ", &dtor_identifier, 1 },
3748 { "__comp_dtor ", &complete_dtor_identifier, 1 },
3749 { "__base_dtor ", &base_dtor_identifier, 1 },
3750 { "__deleting_dtor ", &deleting_dtor_identifier, 1 },
3751 { IN_CHARGE_NAME, &in_charge_identifier, 0 },
3752 { "nelts", &nelts_identifier, 0 },
3753 { THIS_NAME, &this_identifier, 0 },
3754 { VTABLE_DELTA_NAME, &delta_identifier, 0 },
3755 { VTABLE_PFN_NAME, &pfn_identifier, 0 },
3756 { "_vptr", &vptr_identifier, 0 },
3757 { "__vtt_parm", &vtt_parm_identifier, 0 },
3758 { "::", &global_scope_name, 0 },
3759 { "std", &std_identifier, 0 },
3760 { NULL, NULL, 0 }
3763 for (pid = predefined_identifiers; pid->name; ++pid)
3765 *pid->node = get_identifier (pid->name);
3766 if (pid->ctor_or_dtor_p)
3767 IDENTIFIER_CTOR_OR_DTOR_P (*pid->node) = 1;
3771 /* Create the predefined scalar types of C,
3772 and some nodes representing standard constants (0, 1, (void *)0).
3773 Initialize the global binding level.
3774 Make definitions for built-in primitive functions. */
3776 void
3777 cxx_init_decl_processing (void)
3779 tree void_ftype;
3780 tree void_ftype_ptr;
3781 tree void_ftype_ptr_sizetype;
3783 /* Create all the identifiers we need. */
3784 initialize_predefined_identifiers ();
3786 /* Create the global variables. */
3787 push_to_top_level ();
3789 current_function_decl = NULL_TREE;
3790 current_binding_level = NULL;
3791 /* Enter the global namespace. */
3792 gcc_assert (global_namespace == NULL_TREE);
3793 global_namespace = build_lang_decl (NAMESPACE_DECL, global_scope_name,
3794 void_type_node);
3795 DECL_CONTEXT (global_namespace) = build_translation_unit_decl (NULL_TREE);
3796 TREE_PUBLIC (global_namespace) = 1;
3797 begin_scope (sk_namespace, global_namespace);
3799 if (flag_visibility_ms_compat)
3800 default_visibility = VISIBILITY_HIDDEN;
3802 /* Initially, C. */
3803 current_lang_name = lang_name_c;
3805 /* Create the `std' namespace. */
3806 push_namespace (std_identifier);
3807 std_node = current_namespace;
3808 pop_namespace ();
3810 c_common_nodes_and_builtins ();
3812 java_byte_type_node = record_builtin_java_type ("__java_byte", 8);
3813 java_short_type_node = record_builtin_java_type ("__java_short", 16);
3814 java_int_type_node = record_builtin_java_type ("__java_int", 32);
3815 java_long_type_node = record_builtin_java_type ("__java_long", 64);
3816 java_float_type_node = record_builtin_java_type ("__java_float", -32);
3817 java_double_type_node = record_builtin_java_type ("__java_double", -64);
3818 java_char_type_node = record_builtin_java_type ("__java_char", -16);
3819 java_boolean_type_node = record_builtin_java_type ("__java_boolean", -1);
3821 integer_two_node = build_int_cst (NULL_TREE, 2);
3823 record_builtin_type (RID_BOOL, "bool", boolean_type_node);
3824 truthvalue_type_node = boolean_type_node;
3825 truthvalue_false_node = boolean_false_node;
3826 truthvalue_true_node = boolean_true_node;
3828 empty_except_spec = build_tree_list (NULL_TREE, NULL_TREE);
3829 noexcept_true_spec = build_tree_list (boolean_true_node, NULL_TREE);
3830 noexcept_false_spec = build_tree_list (boolean_false_node, NULL_TREE);
3832 #if 0
3833 record_builtin_type (RID_MAX, NULL, string_type_node);
3834 #endif
3836 delta_type_node = ptrdiff_type_node;
3837 vtable_index_type = ptrdiff_type_node;
3839 vtt_parm_type = build_pointer_type (const_ptr_type_node);
3840 void_ftype = build_function_type_list (void_type_node, NULL_TREE);
3841 void_ftype_ptr = build_function_type_list (void_type_node,
3842 ptr_type_node, NULL_TREE);
3843 void_ftype_ptr_sizetype = build_function_type_list (void_type_node,
3844 ptr_type_node,
3845 size_type_node,
3846 NULL_TREE);
3847 void_ftype_ptr
3848 = build_exception_variant (void_ftype_ptr, empty_except_spec);
3849 void_ftype_ptr_sizetype
3850 = build_exception_variant (void_ftype_ptr_sizetype, empty_except_spec);
3852 /* C++ extensions */
3854 unknown_type_node = make_node (LANG_TYPE);
3855 record_unknown_type (unknown_type_node, "unknown type");
3857 /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node. */
3858 TREE_TYPE (unknown_type_node) = unknown_type_node;
3860 /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same
3861 result. */
3862 TYPE_POINTER_TO (unknown_type_node) = unknown_type_node;
3863 TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node;
3865 init_list_type_node = make_node (LANG_TYPE);
3866 record_unknown_type (init_list_type_node, "init list");
3869 /* Make sure we get a unique function type, so we can give
3870 its pointer type a name. (This wins for gdb.) */
3871 tree vfunc_type = make_node (FUNCTION_TYPE);
3872 TREE_TYPE (vfunc_type) = integer_type_node;
3873 TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
3874 layout_type (vfunc_type);
3876 vtable_entry_type = build_pointer_type (vfunc_type);
3878 record_builtin_type (RID_MAX, VTBL_PTR_TYPE, vtable_entry_type);
3880 vtbl_type_node
3881 = build_cplus_array_type (vtable_entry_type, NULL_TREE);
3882 layout_type (vtbl_type_node);
3883 vtbl_type_node = cp_build_qualified_type (vtbl_type_node, TYPE_QUAL_CONST);
3884 record_builtin_type (RID_MAX, NULL, vtbl_type_node);
3885 vtbl_ptr_type_node = build_pointer_type (vtable_entry_type);
3886 layout_type (vtbl_ptr_type_node);
3887 record_builtin_type (RID_MAX, NULL, vtbl_ptr_type_node);
3889 push_namespace (get_identifier ("__cxxabiv1"));
3890 abi_node = current_namespace;
3891 pop_namespace ();
3893 global_type_node = make_node (LANG_TYPE);
3894 record_unknown_type (global_type_node, "global type");
3896 /* Now, C++. */
3897 current_lang_name = lang_name_cplusplus;
3900 tree newattrs, extvisattr;
3901 tree newtype, deltype, deltype2;
3902 tree ptr_ftype_sizetype;
3903 tree new_eh_spec;
3905 ptr_ftype_sizetype
3906 = build_function_type_list (ptr_type_node, size_type_node, NULL_TREE);
3907 if (cxx_dialect == cxx98)
3909 tree bad_alloc_id;
3910 tree bad_alloc_type_node;
3911 tree bad_alloc_decl;
3913 push_namespace (std_identifier);
3914 bad_alloc_id = get_identifier ("bad_alloc");
3915 bad_alloc_type_node = make_class_type (RECORD_TYPE);
3916 TYPE_CONTEXT (bad_alloc_type_node) = current_namespace;
3917 bad_alloc_decl
3918 = create_implicit_typedef (bad_alloc_id, bad_alloc_type_node);
3919 DECL_CONTEXT (bad_alloc_decl) = current_namespace;
3920 pop_namespace ();
3922 new_eh_spec
3923 = add_exception_specifier (NULL_TREE, bad_alloc_type_node, -1);
3925 else
3926 new_eh_spec = noexcept_false_spec;
3928 /* Ensure attribs.c is initialized. */
3929 init_attributes ();
3930 extvisattr = build_tree_list (get_identifier ("externally_visible"),
3931 NULL_TREE);
3932 newattrs = tree_cons (get_identifier ("alloc_size"),
3933 build_tree_list (NULL_TREE, integer_one_node),
3934 extvisattr);
3935 newtype = cp_build_type_attribute_variant (ptr_ftype_sizetype, newattrs);
3936 newtype = build_exception_variant (newtype, new_eh_spec);
3937 deltype = cp_build_type_attribute_variant (void_ftype_ptr, extvisattr);
3938 deltype = build_exception_variant (deltype, empty_except_spec);
3939 deltype2 = build_exception_variant (void_ftype_ptr_sizetype, empty_except_spec);
3940 tree opnew = push_cp_library_fn (NEW_EXPR, newtype, 0);
3941 DECL_IS_MALLOC (opnew) = 1;
3942 DECL_IS_OPERATOR_NEW (opnew) = 1;
3943 opnew = push_cp_library_fn (VEC_NEW_EXPR, newtype, 0);
3944 DECL_IS_MALLOC (opnew) = 1;
3945 DECL_IS_OPERATOR_NEW (opnew) = 1;
3946 global_delete_fndecl = push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
3947 push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
3948 push_cp_library_fn (DELETE_EXPR, deltype2, ECF_NOTHROW);
3950 nullptr_type_node = make_node (NULLPTR_TYPE);
3951 TYPE_SIZE (nullptr_type_node) = bitsize_int (GET_MODE_BITSIZE (ptr_mode));
3952 TYPE_SIZE_UNIT (nullptr_type_node) = size_int (GET_MODE_SIZE (ptr_mode));
3953 TYPE_UNSIGNED (nullptr_type_node) = 1;
3954 TYPE_PRECISION (nullptr_type_node) = GET_MODE_BITSIZE (ptr_mode);
3955 SET_TYPE_MODE (nullptr_type_node, ptr_mode);
3956 record_builtin_type (RID_MAX, "decltype(nullptr)", nullptr_type_node);
3957 nullptr_node = build_int_cst (nullptr_type_node, 0);
3960 abort_fndecl
3961 = build_library_fn_ptr ("__cxa_pure_virtual", void_ftype,
3962 ECF_NORETURN | ECF_NOTHROW);
3964 /* Perform other language dependent initializations. */
3965 init_class_processing ();
3966 init_rtti_processing ();
3967 init_template_processing ();
3969 if (flag_exceptions)
3970 init_exception_processing ();
3972 if (! supports_one_only ())
3973 flag_weak = 0;
3975 make_fname_decl = cp_make_fname_decl;
3976 start_fname_decls ();
3978 /* Show we use EH for cleanups. */
3979 if (flag_exceptions)
3980 using_eh_for_cleanups ();
3983 /* Generate an initializer for a function naming variable from
3984 NAME. NAME may be NULL, to indicate a dependent name. TYPE_P is
3985 filled in with the type of the init. */
3987 tree
3988 cp_fname_init (const char* name, tree *type_p)
3990 tree domain = NULL_TREE;
3991 tree type;
3992 tree init = NULL_TREE;
3993 size_t length = 0;
3995 if (name)
3997 length = strlen (name);
3998 domain = build_index_type (size_int (length));
3999 init = build_string (length + 1, name);
4002 type = cp_build_qualified_type (char_type_node, TYPE_QUAL_CONST);
4003 type = build_cplus_array_type (type, domain);
4005 *type_p = type;
4007 if (init)
4008 TREE_TYPE (init) = type;
4009 else
4010 init = error_mark_node;
4012 return init;
4015 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give
4016 the decl, LOC is the location to give the decl, NAME is the
4017 initialization string and TYPE_DEP indicates whether NAME depended
4018 on the type of the function. We make use of that to detect
4019 __PRETTY_FUNCTION__ inside a template fn. This is being done lazily
4020 at the point of first use, so we mustn't push the decl now. */
4022 static tree
4023 cp_make_fname_decl (location_t loc, tree id, int type_dep)
4025 const char *const name = (type_dep && processing_template_decl
4026 ? NULL : fname_as_string (type_dep));
4027 tree type;
4028 tree init = cp_fname_init (name, &type);
4029 tree decl = build_decl (loc, VAR_DECL, id, type);
4031 if (name)
4032 free (CONST_CAST (char *, name));
4034 /* As we're using pushdecl_with_scope, we must set the context. */
4035 DECL_CONTEXT (decl) = current_function_decl;
4037 TREE_STATIC (decl) = 1;
4038 TREE_READONLY (decl) = 1;
4039 DECL_ARTIFICIAL (decl) = 1;
4041 TREE_USED (decl) = 1;
4043 if (current_function_decl)
4045 cp_binding_level *b = current_binding_level;
4046 if (b->kind == sk_function_parms)
4047 return error_mark_node;
4048 while (b->level_chain->kind != sk_function_parms)
4049 b = b->level_chain;
4050 pushdecl_with_scope (decl, b, /*is_friend=*/false);
4051 cp_finish_decl (decl, init, /*init_const_expr_p=*/false, NULL_TREE,
4052 LOOKUP_ONLYCONVERTING);
4054 else
4056 DECL_THIS_STATIC (decl) = true;
4057 pushdecl_top_level_and_finish (decl, init);
4060 return decl;
4063 static tree
4064 builtin_function_1 (tree decl, tree context, bool is_global)
4066 tree id = DECL_NAME (decl);
4067 const char *name = IDENTIFIER_POINTER (id);
4069 retrofit_lang_decl (decl);
4071 DECL_ARTIFICIAL (decl) = 1;
4072 SET_OVERLOADED_OPERATOR_CODE (decl, ERROR_MARK);
4073 SET_DECL_LANGUAGE (decl, lang_c);
4074 /* Runtime library routines are, by definition, available in an
4075 external shared object. */
4076 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
4077 DECL_VISIBILITY_SPECIFIED (decl) = 1;
4079 DECL_CONTEXT (decl) = context;
4081 if (is_global)
4082 pushdecl_top_level (decl);
4083 else
4084 pushdecl (decl);
4086 /* A function in the user's namespace should have an explicit
4087 declaration before it is used. Mark the built-in function as
4088 anticipated but not actually declared. */
4089 if (name[0] != '_' || name[1] != '_')
4090 DECL_ANTICIPATED (decl) = 1;
4091 else if (strncmp (name + 2, "builtin_", strlen ("builtin_")) != 0)
4093 size_t len = strlen (name);
4095 /* Treat __*_chk fortification functions as anticipated as well,
4096 unless they are __builtin_*. */
4097 if (len > strlen ("___chk")
4098 && memcmp (name + len - strlen ("_chk"),
4099 "_chk", strlen ("_chk") + 1) == 0)
4100 DECL_ANTICIPATED (decl) = 1;
4103 return decl;
4106 tree
4107 cxx_builtin_function (tree decl)
4109 tree id = DECL_NAME (decl);
4110 const char *name = IDENTIFIER_POINTER (id);
4111 /* All builtins that don't begin with an '_' should additionally
4112 go in the 'std' namespace. */
4113 if (name[0] != '_')
4115 tree decl2 = copy_node(decl);
4116 push_namespace (std_identifier);
4117 builtin_function_1 (decl2, std_node, false);
4118 pop_namespace ();
4121 return builtin_function_1 (decl, NULL_TREE, false);
4124 /* Like cxx_builtin_function, but guarantee the function is added to the global
4125 scope. This is to allow function specific options to add new machine
4126 dependent builtins when the target ISA changes via attribute((target(...)))
4127 which saves space on program startup if the program does not use non-generic
4128 ISAs. */
4130 tree
4131 cxx_builtin_function_ext_scope (tree decl)
4134 tree id = DECL_NAME (decl);
4135 const char *name = IDENTIFIER_POINTER (id);
4136 /* All builtins that don't begin with an '_' should additionally
4137 go in the 'std' namespace. */
4138 if (name[0] != '_')
4140 tree decl2 = copy_node(decl);
4141 push_namespace (std_identifier);
4142 builtin_function_1 (decl2, std_node, true);
4143 pop_namespace ();
4146 return builtin_function_1 (decl, NULL_TREE, true);
4149 /* Generate a FUNCTION_DECL with the typical flags for a runtime library
4150 function. Not called directly. */
4152 static tree
4153 build_library_fn (tree name, enum tree_code operator_code, tree type,
4154 int ecf_flags)
4156 tree fn = build_lang_decl (FUNCTION_DECL, name, type);
4157 DECL_EXTERNAL (fn) = 1;
4158 TREE_PUBLIC (fn) = 1;
4159 DECL_ARTIFICIAL (fn) = 1;
4160 SET_OVERLOADED_OPERATOR_CODE (fn, operator_code);
4161 SET_DECL_LANGUAGE (fn, lang_c);
4162 /* Runtime library routines are, by definition, available in an
4163 external shared object. */
4164 DECL_VISIBILITY (fn) = VISIBILITY_DEFAULT;
4165 DECL_VISIBILITY_SPECIFIED (fn) = 1;
4166 set_call_expr_flags (fn, ecf_flags);
4167 return fn;
4170 /* Returns the _DECL for a library function with C++ linkage. */
4172 static tree
4173 build_cp_library_fn (tree name, enum tree_code operator_code, tree type,
4174 int ecf_flags)
4176 tree fn = build_library_fn (name, operator_code, type, ecf_flags);
4177 DECL_CONTEXT (fn) = FROB_CONTEXT (current_namespace);
4178 SET_DECL_LANGUAGE (fn, lang_cplusplus);
4179 return fn;
4182 /* Like build_library_fn, but takes a C string instead of an
4183 IDENTIFIER_NODE. */
4185 tree
4186 build_library_fn_ptr (const char* name, tree type, int ecf_flags)
4188 return build_library_fn (get_identifier (name), ERROR_MARK, type, ecf_flags);
4191 /* Like build_cp_library_fn, but takes a C string instead of an
4192 IDENTIFIER_NODE. */
4194 tree
4195 build_cp_library_fn_ptr (const char* name, tree type, int ecf_flags)
4197 return build_cp_library_fn (get_identifier (name), ERROR_MARK, type,
4198 ecf_flags);
4201 /* Like build_library_fn, but also pushes the function so that we will
4202 be able to find it via IDENTIFIER_GLOBAL_VALUE. Also, the function
4203 may throw exceptions listed in RAISES. */
4205 tree
4206 push_library_fn (tree name, tree type, tree raises, int ecf_flags)
4208 tree fn;
4210 if (raises)
4211 type = build_exception_variant (type, raises);
4213 fn = build_library_fn (name, ERROR_MARK, type, ecf_flags);
4214 pushdecl_top_level (fn);
4215 return fn;
4218 /* Like build_cp_library_fn, but also pushes the function so that it
4219 will be found by normal lookup. */
4221 static tree
4222 push_cp_library_fn (enum tree_code operator_code, tree type,
4223 int ecf_flags)
4225 tree fn = build_cp_library_fn (ansi_opname (operator_code),
4226 operator_code,
4227 type, ecf_flags);
4228 pushdecl (fn);
4229 if (flag_tm)
4230 apply_tm_attr (fn, get_identifier ("transaction_safe"));
4231 return fn;
4234 /* Like push_library_fn, but takes a TREE_LIST of parm types rather than
4235 a FUNCTION_TYPE. */
4237 tree
4238 push_void_library_fn (tree name, tree parmtypes, int ecf_flags)
4240 tree type = build_function_type (void_type_node, parmtypes);
4241 return push_library_fn (name, type, NULL_TREE, ecf_flags);
4244 /* Like push_library_fn, but also note that this function throws
4245 and does not return. Used for __throw_foo and the like. */
4247 tree
4248 push_throw_library_fn (tree name, tree type)
4250 tree fn = push_library_fn (name, type, NULL_TREE, ECF_NORETURN);
4251 return fn;
4254 /* When we call finish_struct for an anonymous union, we create
4255 default copy constructors and such. But, an anonymous union
4256 shouldn't have such things; this function undoes the damage to the
4257 anonymous union type T.
4259 (The reason that we create the synthesized methods is that we don't
4260 distinguish `union { int i; }' from `typedef union { int i; } U'.
4261 The first is an anonymous union; the second is just an ordinary
4262 union type.) */
4264 void
4265 fixup_anonymous_aggr (tree t)
4267 tree *q;
4269 /* Wipe out memory of synthesized methods. */
4270 TYPE_HAS_USER_CONSTRUCTOR (t) = 0;
4271 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0;
4272 TYPE_HAS_COPY_CTOR (t) = 0;
4273 TYPE_HAS_CONST_COPY_CTOR (t) = 0;
4274 TYPE_HAS_COPY_ASSIGN (t) = 0;
4275 TYPE_HAS_CONST_COPY_ASSIGN (t) = 0;
4277 /* Splice the implicitly generated functions out of the TYPE_METHODS
4278 list. */
4279 q = &TYPE_METHODS (t);
4280 while (*q)
4282 if (DECL_ARTIFICIAL (*q))
4283 *q = TREE_CHAIN (*q);
4284 else
4285 q = &DECL_CHAIN (*q);
4288 /* ISO C++ 9.5.3. Anonymous unions may not have function members. */
4289 if (TYPE_METHODS (t))
4291 tree decl = TYPE_MAIN_DECL (t);
4293 if (TREE_CODE (t) != UNION_TYPE)
4294 error_at (DECL_SOURCE_LOCATION (decl),
4295 "an anonymous struct cannot have function members");
4296 else
4297 error_at (DECL_SOURCE_LOCATION (decl),
4298 "an anonymous union cannot have function members");
4301 /* Anonymous aggregates cannot have fields with ctors, dtors or complex
4302 assignment operators (because they cannot have these methods themselves).
4303 For anonymous unions this is already checked because they are not allowed
4304 in any union, otherwise we have to check it. */
4305 if (TREE_CODE (t) != UNION_TYPE)
4307 tree field, type;
4309 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
4310 if (TREE_CODE (field) == FIELD_DECL)
4312 type = TREE_TYPE (field);
4313 if (CLASS_TYPE_P (type))
4315 if (TYPE_NEEDS_CONSTRUCTING (type))
4316 error ("member %q+#D with constructor not allowed "
4317 "in anonymous aggregate", field);
4318 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
4319 error ("member %q+#D with destructor not allowed "
4320 "in anonymous aggregate", field);
4321 if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type))
4322 error ("member %q+#D with copy assignment operator "
4323 "not allowed in anonymous aggregate", field);
4329 /* Warn for an attribute located at LOCATION that appertains to the
4330 class type CLASS_TYPE that has not been properly placed after its
4331 class-key, in it class-specifier. */
4333 void
4334 warn_misplaced_attr_for_class_type (source_location location,
4335 tree class_type)
4337 gcc_assert (OVERLOAD_TYPE_P (class_type));
4339 if (warning_at (location, OPT_Wattributes,
4340 "attribute ignored in declaration "
4341 "of %q#T", class_type))
4342 inform (location,
4343 "attribute for %q#T must follow the %qs keyword",
4344 class_type, class_key_or_enum_as_string (class_type));
4347 /* Make sure that a declaration with no declarator is well-formed, i.e.
4348 just declares a tagged type or anonymous union.
4350 Returns the type declared; or NULL_TREE if none. */
4352 tree
4353 check_tag_decl (cp_decl_specifier_seq *declspecs,
4354 bool explicit_type_instantiation_p)
4356 int saw_friend = decl_spec_seq_has_spec_p (declspecs, ds_friend);
4357 int saw_typedef = decl_spec_seq_has_spec_p (declspecs, ds_typedef);
4358 /* If a class, struct, or enum type is declared by the DECLSPECS
4359 (i.e, if a class-specifier, enum-specifier, or non-typename
4360 elaborated-type-specifier appears in the DECLSPECS),
4361 DECLARED_TYPE is set to the corresponding type. */
4362 tree declared_type = NULL_TREE;
4363 bool error_p = false;
4365 if (declspecs->multiple_types_p)
4366 error ("multiple types in one declaration");
4367 else if (declspecs->redefined_builtin_type)
4369 if (!in_system_header_at (input_location))
4370 permerror (declspecs->locations[ds_redefined_builtin_type_spec],
4371 "redeclaration of C++ built-in type %qT",
4372 declspecs->redefined_builtin_type);
4373 return NULL_TREE;
4376 if (declspecs->type
4377 && TYPE_P (declspecs->type)
4378 && ((TREE_CODE (declspecs->type) != TYPENAME_TYPE
4379 && MAYBE_CLASS_TYPE_P (declspecs->type))
4380 || TREE_CODE (declspecs->type) == ENUMERAL_TYPE))
4381 declared_type = declspecs->type;
4382 else if (declspecs->type == error_mark_node)
4383 error_p = true;
4384 if (declared_type == NULL_TREE && ! saw_friend && !error_p)
4385 permerror (input_location, "declaration does not declare anything");
4386 else if (declared_type != NULL_TREE && type_uses_auto (declared_type))
4388 error ("%<auto%> can only be specified for variables "
4389 "or function declarations");
4390 return error_mark_node;
4392 /* Check for an anonymous union. */
4393 else if (declared_type && RECORD_OR_UNION_CODE_P (TREE_CODE (declared_type))
4394 && TYPE_ANONYMOUS_P (declared_type))
4396 /* 7/3 In a simple-declaration, the optional init-declarator-list
4397 can be omitted only when declaring a class (clause 9) or
4398 enumeration (7.2), that is, when the decl-specifier-seq contains
4399 either a class-specifier, an elaborated-type-specifier with
4400 a class-key (9.1), or an enum-specifier. In these cases and
4401 whenever a class-specifier or enum-specifier is present in the
4402 decl-specifier-seq, the identifiers in these specifiers are among
4403 the names being declared by the declaration (as class-name,
4404 enum-names, or enumerators, depending on the syntax). In such
4405 cases, and except for the declaration of an unnamed bit-field (9.6),
4406 the decl-specifier-seq shall introduce one or more names into the
4407 program, or shall redeclare a name introduced by a previous
4408 declaration. [Example:
4409 enum { }; // ill-formed
4410 typedef class { }; // ill-formed
4411 --end example] */
4412 if (saw_typedef)
4414 error ("missing type-name in typedef-declaration");
4415 return NULL_TREE;
4417 /* Anonymous unions are objects, so they can have specifiers. */;
4418 SET_ANON_AGGR_TYPE_P (declared_type);
4420 if (TREE_CODE (declared_type) != UNION_TYPE
4421 && !in_system_header_at (input_location))
4422 pedwarn (input_location, OPT_Wpedantic, "ISO C++ prohibits anonymous structs");
4425 else
4427 if (decl_spec_seq_has_spec_p (declspecs, ds_inline)
4428 || decl_spec_seq_has_spec_p (declspecs, ds_virtual))
4429 error ("%qs can only be specified for functions",
4430 decl_spec_seq_has_spec_p (declspecs, ds_inline)
4431 ? "inline" : "virtual");
4432 else if (saw_friend
4433 && (!current_class_type
4434 || current_scope () != current_class_type))
4435 error ("%<friend%> can only be specified inside a class");
4436 else if (decl_spec_seq_has_spec_p (declspecs, ds_explicit))
4437 error ("%<explicit%> can only be specified for constructors");
4438 else if (declspecs->storage_class)
4439 error ("a storage class can only be specified for objects "
4440 "and functions");
4441 else if (decl_spec_seq_has_spec_p (declspecs, ds_const)
4442 || decl_spec_seq_has_spec_p (declspecs, ds_volatile)
4443 || decl_spec_seq_has_spec_p (declspecs, ds_restrict)
4444 || decl_spec_seq_has_spec_p (declspecs, ds_thread))
4445 error ("qualifiers can only be specified for objects "
4446 "and functions");
4447 else if (saw_typedef)
4448 warning (0, "%<typedef%> was ignored in this declaration");
4449 else if (decl_spec_seq_has_spec_p (declspecs, ds_constexpr))
4450 error ("%<constexpr%> cannot be used for type declarations");
4453 if (declspecs->attributes && warn_attributes && declared_type)
4455 location_t loc;
4456 if (!CLASS_TYPE_P (declared_type)
4457 || !CLASSTYPE_TEMPLATE_INSTANTIATION (declared_type))
4458 /* For a non-template class, use the name location. */
4459 loc = location_of (declared_type);
4460 else
4461 /* For a template class (an explicit instantiation), use the
4462 current location. */
4463 loc = input_location;
4465 if (explicit_type_instantiation_p)
4466 /* [dcl.attr.grammar]/4:
4468 No attribute-specifier-seq shall appertain to an explicit
4469 instantiation. */
4471 if (warning_at (loc, OPT_Wattributes,
4472 "attribute ignored in explicit instantiation %q#T",
4473 declared_type))
4474 inform (loc,
4475 "no attribute can be applied to "
4476 "an explicit instantiation");
4478 else
4479 warn_misplaced_attr_for_class_type (loc, declared_type);
4482 return declared_type;
4485 /* Called when a declaration is seen that contains no names to declare.
4486 If its type is a reference to a structure, union or enum inherited
4487 from a containing scope, shadow that tag name for the current scope
4488 with a forward reference.
4489 If its type defines a new named structure or union
4490 or defines an enum, it is valid but we need not do anything here.
4491 Otherwise, it is an error.
4493 C++: may have to grok the declspecs to learn about static,
4494 complain for anonymous unions.
4496 Returns the TYPE declared -- or NULL_TREE if none. */
4498 tree
4499 shadow_tag (cp_decl_specifier_seq *declspecs)
4501 tree t = check_tag_decl (declspecs,
4502 /*explicit_type_instantiation_p=*/false);
4504 if (!t)
4505 return NULL_TREE;
4507 if (maybe_process_partial_specialization (t) == error_mark_node)
4508 return NULL_TREE;
4510 /* This is where the variables in an anonymous union are
4511 declared. An anonymous union declaration looks like:
4512 union { ... } ;
4513 because there is no declarator after the union, the parser
4514 sends that declaration here. */
4515 if (ANON_AGGR_TYPE_P (t))
4517 fixup_anonymous_aggr (t);
4519 if (TYPE_FIELDS (t))
4521 tree decl = grokdeclarator (/*declarator=*/NULL,
4522 declspecs, NORMAL, 0, NULL);
4523 finish_anon_union (decl);
4527 return t;
4530 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
4532 tree
4533 groktypename (cp_decl_specifier_seq *type_specifiers,
4534 const cp_declarator *declarator,
4535 bool is_template_arg)
4537 tree attrs;
4538 tree type;
4539 enum decl_context context
4540 = is_template_arg ? TEMPLATE_TYPE_ARG : TYPENAME;
4541 attrs = type_specifiers->attributes;
4542 type_specifiers->attributes = NULL_TREE;
4543 type = grokdeclarator (declarator, type_specifiers, context, 0, &attrs);
4544 if (attrs && type != error_mark_node)
4546 if (CLASS_TYPE_P (type))
4547 warning (OPT_Wattributes, "ignoring attributes applied to class type %qT "
4548 "outside of definition", type);
4549 else if (MAYBE_CLASS_TYPE_P (type))
4550 /* A template type parameter or other dependent type. */
4551 warning (OPT_Wattributes, "ignoring attributes applied to dependent "
4552 "type %qT without an associated declaration", type);
4553 else
4554 cplus_decl_attributes (&type, attrs, 0);
4556 return type;
4559 /* Process a DECLARATOR for a function-scope variable declaration,
4560 namespace-scope variable declaration, or function declaration.
4561 (Function definitions go through start_function; class member
4562 declarations appearing in the body of the class go through
4563 grokfield.) The DECL corresponding to the DECLARATOR is returned.
4564 If an error occurs, the error_mark_node is returned instead.
4566 DECLSPECS are the decl-specifiers for the declaration. INITIALIZED is
4567 SD_INITIALIZED if an explicit initializer is present, or SD_DEFAULTED
4568 for an explicitly defaulted function, or SD_DELETED for an explicitly
4569 deleted function, but 0 (SD_UNINITIALIZED) if this is a variable
4570 implicitly initialized via a default constructor. ATTRIBUTES and
4571 PREFIX_ATTRIBUTES are GNU attributes associated with this declaration.
4573 The scope represented by the context of the returned DECL is pushed
4574 (if it is not the global namespace) and is assigned to
4575 *PUSHED_SCOPE_P. The caller is then responsible for calling
4576 pop_scope on *PUSHED_SCOPE_P if it is set. */
4578 tree
4579 start_decl (const cp_declarator *declarator,
4580 cp_decl_specifier_seq *declspecs,
4581 int initialized,
4582 tree attributes,
4583 tree prefix_attributes,
4584 tree *pushed_scope_p)
4586 tree decl;
4587 tree context;
4588 bool was_public;
4589 int flags;
4590 bool alias;
4592 *pushed_scope_p = NULL_TREE;
4594 /* An object declared as __attribute__((deprecated)) suppresses
4595 warnings of uses of other deprecated items. */
4596 if (lookup_attribute ("deprecated", attributes))
4597 deprecated_state = DEPRECATED_SUPPRESS;
4599 attributes = chainon (attributes, prefix_attributes);
4601 decl = grokdeclarator (declarator, declspecs, NORMAL, initialized,
4602 &attributes);
4604 deprecated_state = DEPRECATED_NORMAL;
4606 if (decl == NULL_TREE || VOID_TYPE_P (decl)
4607 || decl == error_mark_node)
4608 return error_mark_node;
4610 context = CP_DECL_CONTEXT (decl);
4611 if (context != global_namespace)
4612 *pushed_scope_p = push_scope (context);
4614 if (initialized)
4615 /* Is it valid for this decl to have an initializer at all?
4616 If not, set INITIALIZED to zero, which will indirectly
4617 tell `cp_finish_decl' to ignore the initializer once it is parsed. */
4618 switch (TREE_CODE (decl))
4620 case TYPE_DECL:
4621 error ("typedef %qD is initialized (use decltype instead)", decl);
4622 return error_mark_node;
4624 case FUNCTION_DECL:
4625 if (initialized == SD_DELETED)
4626 /* We'll handle the rest of the semantics later, but we need to
4627 set this now so it's visible to duplicate_decls. */
4628 DECL_DELETED_FN (decl) = 1;
4629 break;
4631 default:
4632 break;
4635 if (initialized)
4637 if (! toplevel_bindings_p ()
4638 && DECL_EXTERNAL (decl))
4639 warning (0, "declaration of %q#D has %<extern%> and is initialized",
4640 decl);
4641 DECL_EXTERNAL (decl) = 0;
4642 if (toplevel_bindings_p ())
4643 TREE_STATIC (decl) = 1;
4645 alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl)) != 0;
4647 if (alias && TREE_CODE (decl) == FUNCTION_DECL)
4648 record_key_method_defined (decl);
4650 /* If this is a typedef that names the class for linkage purposes
4651 (7.1.3p8), apply any attributes directly to the type. */
4652 if (TREE_CODE (decl) == TYPE_DECL
4653 && OVERLOAD_TYPE_P (TREE_TYPE (decl))
4654 && decl == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (decl))))
4655 flags = ATTR_FLAG_TYPE_IN_PLACE;
4656 else
4657 flags = 0;
4659 /* Set attributes here so if duplicate decl, will have proper attributes. */
4660 cplus_decl_attributes (&decl, attributes, flags);
4662 /* Dllimported symbols cannot be defined. Static data members (which
4663 can be initialized in-class and dllimported) go through grokfield,
4664 not here, so we don't need to exclude those decls when checking for
4665 a definition. */
4666 if (initialized && DECL_DLLIMPORT_P (decl))
4668 error ("definition of %q#D is marked %<dllimport%>", decl);
4669 DECL_DLLIMPORT_P (decl) = 0;
4672 /* If #pragma weak was used, mark the decl weak now. */
4673 if (!processing_template_decl)
4674 maybe_apply_pragma_weak (decl);
4676 if (TREE_CODE (decl) == FUNCTION_DECL
4677 && DECL_DECLARED_INLINE_P (decl)
4678 && DECL_UNINLINABLE (decl)
4679 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
4680 warning (0, "inline function %q+D given attribute noinline", decl);
4682 if (TYPE_P (context) && COMPLETE_TYPE_P (complete_type (context)))
4684 if (VAR_P (decl))
4686 tree field = lookup_field (context, DECL_NAME (decl), 0, false);
4687 if (field == NULL_TREE || !VAR_P (field))
4688 error ("%q#D is not a static member of %q#T", decl, context);
4689 else
4691 if (DECL_CONTEXT (field) != context)
4693 if (!same_type_p (DECL_CONTEXT (field), context))
4694 permerror (input_location, "ISO C++ does not permit %<%T::%D%> "
4695 "to be defined as %<%T::%D%>",
4696 DECL_CONTEXT (field), DECL_NAME (decl),
4697 context, DECL_NAME (decl));
4698 DECL_CONTEXT (decl) = DECL_CONTEXT (field);
4700 /* Static data member are tricky; an in-class initialization
4701 still doesn't provide a definition, so the in-class
4702 declaration will have DECL_EXTERNAL set, but will have an
4703 initialization. Thus, duplicate_decls won't warn
4704 about this situation, and so we check here. */
4705 if (initialized && DECL_INITIALIZED_IN_CLASS_P (field))
4706 error ("duplicate initialization of %qD", decl);
4707 if (duplicate_decls (decl, field, /*newdecl_is_friend=*/false))
4708 decl = field;
4709 if (decl_spec_seq_has_spec_p (declspecs, ds_constexpr)
4710 && !DECL_DECLARED_CONSTEXPR_P (field))
4711 error ("%qD declared %<constexpr%> outside its class", field);
4714 else
4716 tree field = check_classfn (context, decl,
4717 (processing_template_decl
4718 > template_class_depth (context))
4719 ? current_template_parms
4720 : NULL_TREE);
4721 if (field && field != error_mark_node
4722 && duplicate_decls (decl, field,
4723 /*newdecl_is_friend=*/false))
4724 decl = field;
4727 /* cp_finish_decl sets DECL_EXTERNAL if DECL_IN_AGGR_P is set. */
4728 DECL_IN_AGGR_P (decl) = 0;
4729 /* Do not mark DECL as an explicit specialization if it was not
4730 already marked as an instantiation; a declaration should
4731 never be marked as a specialization unless we know what
4732 template is being specialized. */
4733 if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
4735 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
4736 if (TREE_CODE (decl) == FUNCTION_DECL)
4737 DECL_COMDAT (decl) = DECL_DECLARED_INLINE_P (decl);
4738 else
4739 DECL_COMDAT (decl) = false;
4741 /* [temp.expl.spec] An explicit specialization of a static data
4742 member of a template is a definition if the declaration
4743 includes an initializer; otherwise, it is a declaration.
4745 We check for processing_specialization so this only applies
4746 to the new specialization syntax. */
4747 if (!initialized && processing_specialization)
4748 DECL_EXTERNAL (decl) = 1;
4751 if (DECL_EXTERNAL (decl) && ! DECL_TEMPLATE_SPECIALIZATION (decl)
4752 /* Aliases are definitions. */
4753 && !alias)
4754 permerror (input_location, "declaration of %q#D outside of class is not definition",
4755 decl);
4758 was_public = TREE_PUBLIC (decl);
4760 /* Enter this declaration into the symbol table. */
4761 decl = maybe_push_decl (decl);
4763 if (processing_template_decl)
4764 decl = push_template_decl (decl);
4765 if (decl == error_mark_node)
4766 return error_mark_node;
4768 if (VAR_P (decl)
4769 && DECL_NAMESPACE_SCOPE_P (decl) && !TREE_PUBLIC (decl) && !was_public
4770 && !DECL_THIS_STATIC (decl) && !DECL_ARTIFICIAL (decl))
4772 /* This is a const variable with implicit 'static'. Set
4773 DECL_THIS_STATIC so we can tell it from variables that are
4774 !TREE_PUBLIC because of the anonymous namespace. */
4775 gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (decl)) || errorcount);
4776 DECL_THIS_STATIC (decl) = 1;
4779 if (!processing_template_decl && VAR_P (decl))
4780 start_decl_1 (decl, initialized);
4782 return decl;
4785 /* Process the declaration of a variable DECL. INITIALIZED is true
4786 iff DECL is explicitly initialized. (INITIALIZED is false if the
4787 variable is initialized via an implicitly-called constructor.)
4788 This function must be called for ordinary variables (including, for
4789 example, implicit instantiations of templates), but must not be
4790 called for template declarations. */
4792 void
4793 start_decl_1 (tree decl, bool initialized)
4795 tree type;
4796 bool complete_p;
4797 bool aggregate_definition_p;
4799 gcc_assert (!processing_template_decl);
4801 if (error_operand_p (decl))
4802 return;
4804 gcc_assert (VAR_P (decl));
4806 type = TREE_TYPE (decl);
4807 complete_p = COMPLETE_TYPE_P (type);
4808 aggregate_definition_p = MAYBE_CLASS_TYPE_P (type) && !DECL_EXTERNAL (decl);
4810 /* If an explicit initializer is present, or if this is a definition
4811 of an aggregate, then we need a complete type at this point.
4812 (Scalars are always complete types, so there is nothing to
4813 check.) This code just sets COMPLETE_P; errors (if necessary)
4814 are issued below. */
4815 if ((initialized || aggregate_definition_p)
4816 && !complete_p
4817 && COMPLETE_TYPE_P (complete_type (type)))
4819 complete_p = true;
4820 /* We will not yet have set TREE_READONLY on DECL if the type
4821 was "const", but incomplete, before this point. But, now, we
4822 have a complete type, so we can try again. */
4823 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
4826 if (initialized)
4827 /* Is it valid for this decl to have an initializer at all? */
4829 /* Don't allow initializations for incomplete types except for
4830 arrays which might be completed by the initialization. */
4831 if (complete_p)
4832 ; /* A complete type is ok. */
4833 else if (type_uses_auto (type))
4834 ; /* An auto type is ok. */
4835 else if (TREE_CODE (type) != ARRAY_TYPE)
4837 error ("variable %q#D has initializer but incomplete type", decl);
4838 type = TREE_TYPE (decl) = error_mark_node;
4840 else if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
4842 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
4843 error ("elements of array %q#D have incomplete type", decl);
4844 /* else we already gave an error in start_decl. */
4847 else if (aggregate_definition_p && !complete_p)
4849 if (type_uses_auto (type))
4850 error ("declaration of %q#D has no initializer", decl);
4851 else
4852 error ("aggregate %q#D has incomplete type and cannot be defined",
4853 decl);
4854 /* Change the type so that assemble_variable will give
4855 DECL an rtl we can live with: (mem (const_int 0)). */
4856 type = TREE_TYPE (decl) = error_mark_node;
4859 /* Create a new scope to hold this declaration if necessary.
4860 Whether or not a new scope is necessary cannot be determined
4861 until after the type has been completed; if the type is a
4862 specialization of a class template it is not until after
4863 instantiation has occurred that TYPE_HAS_NONTRIVIAL_DESTRUCTOR
4864 will be set correctly. */
4865 maybe_push_cleanup_level (type);
4868 /* Handle initialization of references. DECL, TYPE, and INIT have the
4869 same meaning as in cp_finish_decl. *CLEANUP must be NULL on entry,
4870 but will be set to a new CLEANUP_STMT if a temporary is created
4871 that must be destroyed subsequently.
4873 Returns an initializer expression to use to initialize DECL, or
4874 NULL if the initialization can be performed statically.
4876 Quotes on semantics can be found in ARM 8.4.3. */
4878 static tree
4879 grok_reference_init (tree decl, tree type, tree init, int flags)
4881 if (init == NULL_TREE)
4883 if ((DECL_LANG_SPECIFIC (decl) == 0
4884 || DECL_IN_AGGR_P (decl) == 0)
4885 && ! DECL_THIS_EXTERN (decl))
4886 error ("%qD declared as reference but not initialized", decl);
4887 return NULL_TREE;
4890 if (TREE_CODE (init) == TREE_LIST)
4891 init = build_x_compound_expr_from_list (init, ELK_INIT,
4892 tf_warning_or_error);
4894 if (TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE
4895 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
4896 /* Note: default conversion is only called in very special cases. */
4897 init = decay_conversion (init, tf_warning_or_error);
4899 /* Convert INIT to the reference type TYPE. This may involve the
4900 creation of a temporary, whose lifetime must be the same as that
4901 of the reference. If so, a DECL_EXPR for the temporary will be
4902 added just after the DECL_EXPR for DECL. That's why we don't set
4903 DECL_INITIAL for local references (instead assigning to them
4904 explicitly); we need to allow the temporary to be initialized
4905 first. */
4906 return initialize_reference (type, init, flags,
4907 tf_warning_or_error);
4910 /* Designated initializers in arrays are not supported in GNU C++.
4911 The parser cannot detect this error since it does not know whether
4912 a given brace-enclosed initializer is for a class type or for an
4913 array. This function checks that CE does not use a designated
4914 initializer. If it does, an error is issued. Returns true if CE
4915 is valid, i.e., does not have a designated initializer. */
4917 static bool
4918 check_array_designated_initializer (constructor_elt *ce,
4919 unsigned HOST_WIDE_INT index)
4921 /* Designated initializers for array elements are not supported. */
4922 if (ce->index)
4924 /* The parser only allows identifiers as designated
4925 initializers. */
4926 if (ce->index == error_mark_node)
4928 error ("name used in a GNU-style designated "
4929 "initializer for an array");
4930 return false;
4932 else if (identifier_p (ce->index))
4934 error ("name %qD used in a GNU-style designated "
4935 "initializer for an array", ce->index);
4936 return false;
4939 ce->index = cxx_constant_value (ce->index);
4941 if (TREE_CODE (ce->index) == INTEGER_CST)
4943 /* A C99 designator is OK if it matches the current index. */
4944 if (wi::eq_p (ce->index, index))
4945 return true;
4946 else
4947 sorry ("non-trivial designated initializers not supported");
4949 else
4950 gcc_unreachable ();
4952 return false;
4955 return true;
4958 /* When parsing `int a[] = {1, 2};' we don't know the size of the
4959 array until we finish parsing the initializer. If that's the
4960 situation we're in, update DECL accordingly. */
4962 static void
4963 maybe_deduce_size_from_array_init (tree decl, tree init)
4965 tree type = TREE_TYPE (decl);
4967 if (TREE_CODE (type) == ARRAY_TYPE
4968 && TYPE_DOMAIN (type) == NULL_TREE
4969 && TREE_CODE (decl) != TYPE_DECL)
4971 /* do_default is really a C-ism to deal with tentative definitions.
4972 But let's leave it here to ease the eventual merge. */
4973 int do_default = !DECL_EXTERNAL (decl);
4974 tree initializer = init ? init : DECL_INITIAL (decl);
4975 int failure = 0;
4977 /* Check that there are no designated initializers in INIT, as
4978 those are not supported in GNU C++, and as the middle-end
4979 will crash if presented with a non-numeric designated
4980 initializer. */
4981 if (initializer && BRACE_ENCLOSED_INITIALIZER_P (initializer))
4983 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initializer);
4984 constructor_elt *ce;
4985 HOST_WIDE_INT i;
4986 FOR_EACH_VEC_SAFE_ELT (v, i, ce)
4987 if (!check_array_designated_initializer (ce, i))
4988 failure = 1;
4991 if (!failure)
4993 failure = cp_complete_array_type (&TREE_TYPE (decl), initializer,
4994 do_default);
4995 if (failure == 1)
4997 error ("initializer fails to determine size of %qD", decl);
4999 else if (failure == 2)
5001 if (do_default)
5003 error ("array size missing in %qD", decl);
5005 /* If a `static' var's size isn't known, make it extern as
5006 well as static, so it does not get allocated. If it's not
5007 `static', then don't mark it extern; finish_incomplete_decl
5008 will give it a default size and it will get allocated. */
5009 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
5010 DECL_EXTERNAL (decl) = 1;
5012 else if (failure == 3)
5014 error ("zero-size array %qD", decl);
5018 cp_apply_type_quals_to_decl (cp_type_quals (TREE_TYPE (decl)), decl);
5020 relayout_decl (decl);
5024 /* Set DECL_SIZE, DECL_ALIGN, etc. for DECL (a VAR_DECL), and issue
5025 any appropriate error messages regarding the layout. */
5027 static void
5028 layout_var_decl (tree decl)
5030 tree type;
5032 type = TREE_TYPE (decl);
5033 if (type == error_mark_node)
5034 return;
5036 /* If we haven't already laid out this declaration, do so now.
5037 Note that we must not call complete type for an external object
5038 because it's type might involve templates that we are not
5039 supposed to instantiate yet. (And it's perfectly valid to say
5040 `extern X x' for some incomplete type `X'.) */
5041 if (!DECL_EXTERNAL (decl))
5042 complete_type (type);
5043 if (!DECL_SIZE (decl)
5044 && TREE_TYPE (decl) != error_mark_node
5045 && (COMPLETE_TYPE_P (type)
5046 || (TREE_CODE (type) == ARRAY_TYPE
5047 && !TYPE_DOMAIN (type)
5048 && COMPLETE_TYPE_P (TREE_TYPE (type)))))
5049 layout_decl (decl, 0);
5051 if (!DECL_EXTERNAL (decl) && DECL_SIZE (decl) == NULL_TREE)
5053 /* An automatic variable with an incomplete type: that is an error.
5054 Don't talk about array types here, since we took care of that
5055 message in grokdeclarator. */
5056 error ("storage size of %qD isn%'t known", decl);
5057 TREE_TYPE (decl) = error_mark_node;
5059 #if 0
5060 /* Keep this code around in case we later want to control debug info
5061 based on whether a type is "used". (jason 1999-11-11) */
5063 else if (!DECL_EXTERNAL (decl) && MAYBE_CLASS_TYPE_P (ttype))
5064 /* Let debugger know it should output info for this type. */
5065 note_debug_info_needed (ttype);
5067 if (TREE_STATIC (decl) && DECL_CLASS_SCOPE_P (decl))
5068 note_debug_info_needed (DECL_CONTEXT (decl));
5069 #endif
5071 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
5072 && DECL_SIZE (decl) != NULL_TREE
5073 && ! TREE_CONSTANT (DECL_SIZE (decl)))
5075 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
5076 constant_expression_warning (DECL_SIZE (decl));
5077 else
5079 error ("storage size of %qD isn%'t constant", decl);
5080 TREE_TYPE (decl) = error_mark_node;
5085 /* If a local static variable is declared in an inline function, or if
5086 we have a weak definition, we must endeavor to create only one
5087 instance of the variable at link-time. */
5089 void
5090 maybe_commonize_var (tree decl)
5092 /* Static data in a function with comdat linkage also has comdat
5093 linkage. */
5094 if (TREE_STATIC (decl)
5095 /* Don't mess with __FUNCTION__. */
5096 && ! DECL_ARTIFICIAL (decl)
5097 && DECL_FUNCTION_SCOPE_P (decl)
5098 && vague_linkage_p (DECL_CONTEXT (decl)))
5100 if (flag_weak)
5102 /* With weak symbols, we simply make the variable COMDAT;
5103 that will cause copies in multiple translations units to
5104 be merged. */
5105 comdat_linkage (decl);
5107 else
5109 if (DECL_INITIAL (decl) == NULL_TREE
5110 || DECL_INITIAL (decl) == error_mark_node)
5112 /* Without weak symbols, we can use COMMON to merge
5113 uninitialized variables. */
5114 TREE_PUBLIC (decl) = 1;
5115 DECL_COMMON (decl) = 1;
5117 else
5119 /* While for initialized variables, we must use internal
5120 linkage -- which means that multiple copies will not
5121 be merged. */
5122 TREE_PUBLIC (decl) = 0;
5123 DECL_COMMON (decl) = 0;
5124 if (warning_at (DECL_SOURCE_LOCATION (decl), 0,
5125 "sorry: semantics of inline function static "
5126 "data %q#D are wrong (you%'ll wind up "
5127 "with multiple copies)", decl))
5128 inform (DECL_SOURCE_LOCATION (decl),
5129 "you can work around this by removing the initializer");
5133 else if (DECL_LANG_SPECIFIC (decl) && DECL_COMDAT (decl))
5134 /* Set it up again; we might have set DECL_INITIAL since the last
5135 time. */
5136 comdat_linkage (decl);
5139 /* Issue an error message if DECL is an uninitialized const variable. */
5141 static void
5142 check_for_uninitialized_const_var (tree decl)
5144 tree type = strip_array_types (TREE_TYPE (decl));
5146 /* ``Unless explicitly declared extern, a const object does not have
5147 external linkage and must be initialized. ($8.4; $12.1)'' ARM
5148 7.1.6 */
5149 if (VAR_P (decl)
5150 && TREE_CODE (type) != REFERENCE_TYPE
5151 && CP_TYPE_CONST_P (type)
5152 && !DECL_INITIAL (decl))
5154 tree field = default_init_uninitialized_part (type);
5155 if (!field)
5156 return;
5158 permerror (DECL_SOURCE_LOCATION (decl),
5159 "uninitialized const %qD", decl);
5161 if (CLASS_TYPE_P (type))
5163 tree defaulted_ctor;
5165 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
5166 "%q#T has no user-provided default constructor", type);
5167 defaulted_ctor = in_class_defaulted_default_constructor (type);
5168 if (defaulted_ctor)
5169 inform (DECL_SOURCE_LOCATION (defaulted_ctor),
5170 "constructor is not user-provided because it is "
5171 "explicitly defaulted in the class body");
5172 inform (0, "and the implicitly-defined constructor does not "
5173 "initialize %q+#D", field);
5178 /* Structure holding the current initializer being processed by reshape_init.
5179 CUR is a pointer to the current element being processed, END is a pointer
5180 after the last element present in the initializer. */
5181 typedef struct reshape_iterator_t
5183 constructor_elt *cur;
5184 constructor_elt *end;
5185 } reshape_iter;
5187 static tree reshape_init_r (tree, reshape_iter *, bool, tsubst_flags_t);
5189 /* FIELD is a FIELD_DECL or NULL. In the former case, the value
5190 returned is the next FIELD_DECL (possibly FIELD itself) that can be
5191 initialized. If there are no more such fields, the return value
5192 will be NULL. */
5194 tree
5195 next_initializable_field (tree field)
5197 while (field
5198 && (TREE_CODE (field) != FIELD_DECL
5199 || (DECL_C_BIT_FIELD (field) && !DECL_NAME (field))
5200 || DECL_ARTIFICIAL (field)))
5201 field = DECL_CHAIN (field);
5203 return field;
5206 /* Subroutine of reshape_init_array and reshape_init_vector, which does
5207 the actual work. ELT_TYPE is the element type of the array. MAX_INDEX is an
5208 INTEGER_CST representing the size of the array minus one (the maximum index),
5209 or NULL_TREE if the array was declared without specifying the size. D is
5210 the iterator within the constructor. */
5212 static tree
5213 reshape_init_array_1 (tree elt_type, tree max_index, reshape_iter *d,
5214 tsubst_flags_t complain)
5216 tree new_init;
5217 bool sized_array_p = (max_index && TREE_CONSTANT (max_index));
5218 unsigned HOST_WIDE_INT max_index_cst = 0;
5219 unsigned HOST_WIDE_INT index;
5221 /* The initializer for an array is always a CONSTRUCTOR. */
5222 new_init = build_constructor (init_list_type_node, NULL);
5224 if (sized_array_p)
5226 /* Minus 1 is used for zero sized arrays. */
5227 if (integer_all_onesp (max_index))
5228 return new_init;
5230 if (tree_fits_uhwi_p (max_index))
5231 max_index_cst = tree_to_uhwi (max_index);
5232 /* sizetype is sign extended, not zero extended. */
5233 else
5234 max_index_cst = tree_to_uhwi (fold_convert (size_type_node, max_index));
5237 /* Loop until there are no more initializers. */
5238 for (index = 0;
5239 d->cur != d->end && (!sized_array_p || index <= max_index_cst);
5240 ++index)
5242 tree elt_init;
5243 constructor_elt *old_cur = d->cur;
5245 check_array_designated_initializer (d->cur, index);
5246 elt_init = reshape_init_r (elt_type, d, /*first_initializer_p=*/false,
5247 complain);
5248 if (elt_init == error_mark_node)
5249 return error_mark_node;
5250 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init),
5251 size_int (index), elt_init);
5252 if (!TREE_CONSTANT (elt_init))
5253 TREE_CONSTANT (new_init) = false;
5255 /* This can happen with an invalid initializer (c++/54501). */
5256 if (d->cur == old_cur && !sized_array_p)
5257 break;
5260 return new_init;
5263 /* Subroutine of reshape_init_r, processes the initializers for arrays.
5264 Parameters are the same of reshape_init_r. */
5266 static tree
5267 reshape_init_array (tree type, reshape_iter *d, tsubst_flags_t complain)
5269 tree max_index = NULL_TREE;
5271 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
5273 if (TYPE_DOMAIN (type))
5274 max_index = array_type_nelts (type);
5276 return reshape_init_array_1 (TREE_TYPE (type), max_index, d, complain);
5279 /* Subroutine of reshape_init_r, processes the initializers for vectors.
5280 Parameters are the same of reshape_init_r. */
5282 static tree
5283 reshape_init_vector (tree type, reshape_iter *d, tsubst_flags_t complain)
5285 tree max_index = NULL_TREE;
5287 gcc_assert (TREE_CODE (type) == VECTOR_TYPE);
5289 if (COMPOUND_LITERAL_P (d->cur->value))
5291 tree value = d->cur->value;
5292 if (!same_type_p (TREE_TYPE (value), type))
5294 if (complain & tf_error)
5295 error ("invalid type %qT as initializer for a vector of type %qT",
5296 TREE_TYPE (d->cur->value), type);
5297 value = error_mark_node;
5299 ++d->cur;
5300 return value;
5303 /* For a vector, we initialize it as an array of the appropriate size. */
5304 if (TREE_CODE (type) == VECTOR_TYPE)
5305 max_index = size_int (TYPE_VECTOR_SUBPARTS (type) - 1);
5307 return reshape_init_array_1 (TREE_TYPE (type), max_index, d, complain);
5310 /* Subroutine of reshape_init_r, processes the initializers for classes
5311 or union. Parameters are the same of reshape_init_r. */
5313 static tree
5314 reshape_init_class (tree type, reshape_iter *d, bool first_initializer_p,
5315 tsubst_flags_t complain)
5317 tree field;
5318 tree new_init;
5320 gcc_assert (CLASS_TYPE_P (type));
5322 /* The initializer for a class is always a CONSTRUCTOR. */
5323 new_init = build_constructor (init_list_type_node, NULL);
5324 field = next_initializable_field (TYPE_FIELDS (type));
5326 if (!field)
5328 /* [dcl.init.aggr]
5330 An initializer for an aggregate member that is an
5331 empty class shall have the form of an empty
5332 initializer-list {}. */
5333 if (!first_initializer_p)
5335 if (complain & tf_error)
5336 error ("initializer for %qT must be brace-enclosed", type);
5337 return error_mark_node;
5339 return new_init;
5342 /* Loop through the initializable fields, gathering initializers. */
5343 while (d->cur != d->end)
5345 tree field_init;
5346 constructor_elt *old_cur = d->cur;
5348 /* Handle designated initializers, as an extension. */
5349 if (d->cur->index)
5351 if (d->cur->index == error_mark_node)
5352 return error_mark_node;
5354 if (TREE_CODE (d->cur->index) == FIELD_DECL)
5355 /* We already reshaped this. */
5356 gcc_assert (d->cur->index == field);
5357 else if (TREE_CODE (d->cur->index) == IDENTIFIER_NODE)
5358 field = lookup_field_1 (type, d->cur->index, /*want_type=*/false);
5359 else
5361 if (complain & tf_error)
5362 error ("%<[%E] =%> used in a GNU-style designated initializer"
5363 " for class %qT", d->cur->index, type);
5364 return error_mark_node;
5367 if (!field || TREE_CODE (field) != FIELD_DECL)
5369 if (complain & tf_error)
5370 error ("%qT has no non-static data member named %qD", type,
5371 d->cur->index);
5372 return error_mark_node;
5376 /* If we processed all the member of the class, we are done. */
5377 if (!field)
5378 break;
5380 field_init = reshape_init_r (TREE_TYPE (field), d,
5381 /*first_initializer_p=*/false, complain);
5382 if (field_init == error_mark_node)
5383 return error_mark_node;
5385 if (d->cur == old_cur && d->cur->index)
5387 /* This can happen with an invalid initializer for a flexible
5388 array member (c++/54441). */
5389 if (complain & tf_error)
5390 error ("invalid initializer for %q#D", field);
5391 return error_mark_node;
5394 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init), field, field_init);
5396 /* [dcl.init.aggr]
5398 When a union is initialized with a brace-enclosed
5399 initializer, the braces shall only contain an
5400 initializer for the first member of the union. */
5401 if (TREE_CODE (type) == UNION_TYPE)
5402 break;
5404 field = next_initializable_field (DECL_CHAIN (field));
5407 return new_init;
5410 /* Subroutine of reshape_init_r. We're in a context where C99 initializer
5411 designators are not valid; either complain or return true to indicate
5412 that reshape_init_r should return error_mark_node. */
5414 static bool
5415 has_designator_problem (reshape_iter *d, tsubst_flags_t complain)
5417 if (d->cur->index)
5419 if (complain & tf_error)
5420 error ("C99 designator %qE outside aggregate initializer",
5421 d->cur->index);
5422 else
5423 return true;
5425 return false;
5428 /* Subroutine of reshape_init, which processes a single initializer (part of
5429 a CONSTRUCTOR). TYPE is the type of the variable being initialized, D is the
5430 iterator within the CONSTRUCTOR which points to the initializer to process.
5431 FIRST_INITIALIZER_P is true if this is the first initializer of the
5432 outermost CONSTRUCTOR node. */
5434 static tree
5435 reshape_init_r (tree type, reshape_iter *d, bool first_initializer_p,
5436 tsubst_flags_t complain)
5438 tree init = d->cur->value;
5440 if (error_operand_p (init))
5441 return error_mark_node;
5443 if (first_initializer_p && !CP_AGGREGATE_TYPE_P (type)
5444 && has_designator_problem (d, complain))
5445 return error_mark_node;
5447 if (TREE_CODE (type) == COMPLEX_TYPE)
5449 /* A complex type can be initialized from one or two initializers,
5450 but braces are not elided. */
5451 d->cur++;
5452 if (BRACE_ENCLOSED_INITIALIZER_P (init))
5454 if (CONSTRUCTOR_NELTS (init) > 2)
5456 if (complain & tf_error)
5457 error ("too many initializers for %qT", type);
5458 else
5459 return error_mark_node;
5462 else if (first_initializer_p && d->cur != d->end)
5464 vec<constructor_elt, va_gc> *v = 0;
5465 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, init);
5466 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, d->cur->value);
5467 if (has_designator_problem (d, complain))
5468 return error_mark_node;
5469 d->cur++;
5470 init = build_constructor (init_list_type_node, v);
5472 return init;
5475 /* A non-aggregate type is always initialized with a single
5476 initializer. */
5477 if (!CP_AGGREGATE_TYPE_P (type))
5479 /* It is invalid to initialize a non-aggregate type with a
5480 brace-enclosed initializer before C++0x.
5481 We need to check for BRACE_ENCLOSED_INITIALIZER_P here because
5482 of g++.old-deja/g++.mike/p7626.C: a pointer-to-member constant is
5483 a CONSTRUCTOR (with a record type). */
5484 if (TREE_CODE (init) == CONSTRUCTOR
5485 && BRACE_ENCLOSED_INITIALIZER_P (init)) /* p7626.C */
5487 if (SCALAR_TYPE_P (type))
5489 if (complain & tf_error)
5490 error ("braces around scalar initializer for type %qT", type);
5491 init = error_mark_node;
5493 else
5494 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5497 d->cur++;
5498 return init;
5501 /* "If T is a class type and the initializer list has a single element of
5502 type cv U, where U is T or a class derived from T, the object is
5503 initialized from that element." Even if T is an aggregate. */
5504 if (cxx_dialect >= cxx11 && (CLASS_TYPE_P (type) || VECTOR_TYPE_P (type))
5505 && first_initializer_p
5506 && d->end - d->cur == 1
5507 && reference_related_p (type, TREE_TYPE (init)))
5509 d->cur++;
5510 return init;
5513 /* [dcl.init.aggr]
5515 All implicit type conversions (clause _conv_) are considered when
5516 initializing the aggregate member with an initializer from an
5517 initializer-list. If the initializer can initialize a member,
5518 the member is initialized. Otherwise, if the member is itself a
5519 non-empty subaggregate, brace elision is assumed and the
5520 initializer is considered for the initialization of the first
5521 member of the subaggregate. */
5522 if (TREE_CODE (init) != CONSTRUCTOR
5523 /* But don't try this for the first initializer, since that would be
5524 looking through the outermost braces; A a2 = { a1 }; is not a
5525 valid aggregate initialization. */
5526 && !first_initializer_p
5527 && (same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (init))
5528 || can_convert_arg (type, TREE_TYPE (init), init, LOOKUP_NORMAL,
5529 complain)))
5531 d->cur++;
5532 return init;
5535 /* [dcl.init.string]
5537 A char array (whether plain char, signed char, or unsigned char)
5538 can be initialized by a string-literal (optionally enclosed in
5539 braces); a wchar_t array can be initialized by a wide
5540 string-literal (optionally enclosed in braces). */
5541 if (TREE_CODE (type) == ARRAY_TYPE
5542 && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type))))
5544 tree str_init = init;
5546 /* Strip one level of braces if and only if they enclose a single
5547 element (as allowed by [dcl.init.string]). */
5548 if (!first_initializer_p
5549 && TREE_CODE (str_init) == CONSTRUCTOR
5550 && vec_safe_length (CONSTRUCTOR_ELTS (str_init)) == 1)
5552 str_init = (*CONSTRUCTOR_ELTS (str_init))[0].value;
5555 /* If it's a string literal, then it's the initializer for the array
5556 as a whole. Otherwise, continue with normal initialization for
5557 array types (one value per array element). */
5558 if (TREE_CODE (str_init) == STRING_CST)
5560 if (has_designator_problem (d, complain))
5561 return error_mark_node;
5562 d->cur++;
5563 return str_init;
5567 /* The following cases are about aggregates. If we are not within a full
5568 initializer already, and there is not a CONSTRUCTOR, it means that there
5569 is a missing set of braces (that is, we are processing the case for
5570 which reshape_init exists). */
5571 if (!first_initializer_p)
5573 if (TREE_CODE (init) == CONSTRUCTOR)
5575 if (TREE_TYPE (init) && TYPE_PTRMEMFUNC_P (TREE_TYPE (init)))
5576 /* There is no need to reshape pointer-to-member function
5577 initializers, as they are always constructed correctly
5578 by the front end. */
5580 else if (COMPOUND_LITERAL_P (init))
5581 /* For a nested compound literal, there is no need to reshape since
5582 brace elision is not allowed. Even if we decided to allow it,
5583 we should add a call to reshape_init in finish_compound_literal,
5584 before calling digest_init, so changing this code would still
5585 not be necessary. */
5586 gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (init));
5587 else
5589 ++d->cur;
5590 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
5591 return reshape_init (type, init, complain);
5595 warning (OPT_Wmissing_braces, "missing braces around initializer for %qT",
5596 type);
5599 /* Dispatch to specialized routines. */
5600 if (CLASS_TYPE_P (type))
5601 return reshape_init_class (type, d, first_initializer_p, complain);
5602 else if (TREE_CODE (type) == ARRAY_TYPE)
5603 return reshape_init_array (type, d, complain);
5604 else if (TREE_CODE (type) == VECTOR_TYPE)
5605 return reshape_init_vector (type, d, complain);
5606 else
5607 gcc_unreachable();
5610 /* Undo the brace-elision allowed by [dcl.init.aggr] in a
5611 brace-enclosed aggregate initializer.
5613 INIT is the CONSTRUCTOR containing the list of initializers describing
5614 a brace-enclosed initializer for an entity of the indicated aggregate TYPE.
5615 It may not presently match the shape of the TYPE; for example:
5617 struct S { int a; int b; };
5618 struct S a[] = { 1, 2, 3, 4 };
5620 Here INIT will hold a vector of four elements, rather than a
5621 vector of two elements, each itself a vector of two elements. This
5622 routine transforms INIT from the former form into the latter. The
5623 revised CONSTRUCTOR node is returned. */
5625 tree
5626 reshape_init (tree type, tree init, tsubst_flags_t complain)
5628 vec<constructor_elt, va_gc> *v;
5629 reshape_iter d;
5630 tree new_init;
5632 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
5634 v = CONSTRUCTOR_ELTS (init);
5636 /* An empty constructor does not need reshaping, and it is always a valid
5637 initializer. */
5638 if (vec_safe_is_empty (v))
5639 return init;
5641 /* Recurse on this CONSTRUCTOR. */
5642 d.cur = &(*v)[0];
5643 d.end = d.cur + v->length ();
5645 new_init = reshape_init_r (type, &d, true, complain);
5646 if (new_init == error_mark_node)
5647 return error_mark_node;
5649 /* Make sure all the element of the constructor were used. Otherwise,
5650 issue an error about exceeding initializers. */
5651 if (d.cur != d.end)
5653 if (complain & tf_error)
5654 error ("too many initializers for %qT", type);
5655 else
5656 return error_mark_node;
5659 return new_init;
5662 /* Verify array initializer. Returns true if errors have been reported. */
5664 bool
5665 check_array_initializer (tree decl, tree type, tree init)
5667 tree element_type = TREE_TYPE (type);
5669 /* The array type itself need not be complete, because the
5670 initializer may tell us how many elements are in the array.
5671 But, the elements of the array must be complete. */
5672 if (!COMPLETE_TYPE_P (complete_type (element_type)))
5674 if (decl)
5675 error ("elements of array %q#D have incomplete type", decl);
5676 else
5677 error ("elements of array %q#T have incomplete type", type);
5678 return true;
5680 /* A compound literal can't have variable size. */
5681 if (init && !decl
5682 && ((COMPLETE_TYPE_P (type) && !TREE_CONSTANT (TYPE_SIZE (type)))
5683 || !TREE_CONSTANT (TYPE_SIZE (element_type))))
5685 error ("variable-sized compound literal");
5686 return true;
5688 return false;
5691 /* Subroutine of check_initializer; args are passed down from that function.
5692 Set stmts_are_full_exprs_p to 1 across a call to build_aggr_init. */
5694 static tree
5695 build_aggr_init_full_exprs (tree decl, tree init, int flags)
5698 gcc_assert (stmts_are_full_exprs_p ());
5699 return build_aggr_init (decl, init, flags, tf_warning_or_error);
5702 /* Verify INIT (the initializer for DECL), and record the
5703 initialization in DECL_INITIAL, if appropriate. CLEANUP is as for
5704 grok_reference_init.
5706 If the return value is non-NULL, it is an expression that must be
5707 evaluated dynamically to initialize DECL. */
5709 static tree
5710 check_initializer (tree decl, tree init, int flags, vec<tree, va_gc> **cleanups)
5712 tree type = TREE_TYPE (decl);
5713 tree init_code = NULL;
5714 tree core_type;
5716 /* Things that are going to be initialized need to have complete
5717 type. */
5718 TREE_TYPE (decl) = type = complete_type (TREE_TYPE (decl));
5720 if (DECL_HAS_VALUE_EXPR_P (decl))
5722 /* A variable with DECL_HAS_VALUE_EXPR_P set is just a placeholder,
5723 it doesn't have storage to be initialized. */
5724 gcc_assert (init == NULL_TREE);
5725 return NULL_TREE;
5728 if (type == error_mark_node)
5729 /* We will have already complained. */
5730 return NULL_TREE;
5732 if (TREE_CODE (type) == ARRAY_TYPE)
5734 if (check_array_initializer (decl, type, init))
5735 return NULL_TREE;
5737 else if (!COMPLETE_TYPE_P (type))
5739 error ("%q#D has incomplete type", decl);
5740 TREE_TYPE (decl) = error_mark_node;
5741 return NULL_TREE;
5743 else
5744 /* There is no way to make a variable-sized class type in GNU C++. */
5745 gcc_assert (TREE_CONSTANT (TYPE_SIZE (type)));
5747 if (init && BRACE_ENCLOSED_INITIALIZER_P (init))
5749 int init_len = vec_safe_length (CONSTRUCTOR_ELTS (init));
5750 if (SCALAR_TYPE_P (type))
5752 if (init_len == 0)
5754 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5755 init = build_zero_init (type, NULL_TREE, false);
5757 else if (init_len != 1 && TREE_CODE (type) != COMPLEX_TYPE)
5759 error ("scalar object %qD requires one element in initializer",
5760 decl);
5761 TREE_TYPE (decl) = error_mark_node;
5762 return NULL_TREE;
5767 if (TREE_CODE (decl) == CONST_DECL)
5769 gcc_assert (TREE_CODE (type) != REFERENCE_TYPE);
5771 DECL_INITIAL (decl) = init;
5773 gcc_assert (init != NULL_TREE);
5774 init = NULL_TREE;
5776 else if (!init && DECL_REALLY_EXTERN (decl))
5778 else if (init || type_build_ctor_call (type)
5779 || TREE_CODE (type) == REFERENCE_TYPE)
5781 if (TREE_CODE (type) == REFERENCE_TYPE)
5783 init = grok_reference_init (decl, type, init, flags);
5784 flags |= LOOKUP_ALREADY_DIGESTED;
5786 else if (!init)
5787 check_for_uninitialized_const_var (decl);
5788 /* Do not reshape constructors of vectors (they don't need to be
5789 reshaped. */
5790 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
5792 if (is_std_init_list (type))
5794 init = perform_implicit_conversion (type, init,
5795 tf_warning_or_error);
5796 flags |= LOOKUP_ALREADY_DIGESTED;
5798 else if (TYPE_NON_AGGREGATE_CLASS (type))
5800 /* Don't reshape if the class has constructors. */
5801 if (cxx_dialect == cxx98)
5802 error ("in C++98 %qD must be initialized by constructor, "
5803 "not by %<{...}%>",
5804 decl);
5806 else if (TREE_CODE (type) == VECTOR_TYPE && TYPE_VECTOR_OPAQUE (type))
5808 error ("opaque vector types cannot be initialized");
5809 init = error_mark_node;
5811 else
5813 init = reshape_init (type, init, tf_warning_or_error);
5814 flags |= LOOKUP_NO_NARROWING;
5817 else if (TREE_CODE (init) == TREE_LIST
5818 && TREE_TYPE (init) != unknown_type_node
5819 && !MAYBE_CLASS_TYPE_P (type))
5821 gcc_assert (TREE_CODE (decl) != RESULT_DECL);
5823 /* We get here with code like `int a (2);' */
5824 init = build_x_compound_expr_from_list (init, ELK_INIT,
5825 tf_warning_or_error);
5828 /* If DECL has an array type without a specific bound, deduce the
5829 array size from the initializer. */
5830 maybe_deduce_size_from_array_init (decl, init);
5831 type = TREE_TYPE (decl);
5832 if (type == error_mark_node)
5833 return NULL_TREE;
5835 if ((type_build_ctor_call (type) || CLASS_TYPE_P (type))
5836 && !(flags & LOOKUP_ALREADY_DIGESTED)
5837 && !(init && BRACE_ENCLOSED_INITIALIZER_P (init)
5838 && CP_AGGREGATE_TYPE_P (type)
5839 && (CLASS_TYPE_P (type)
5840 || !TYPE_NEEDS_CONSTRUCTING (type)
5841 || type_has_extended_temps (type))))
5843 init_code = build_aggr_init_full_exprs (decl, init, flags);
5845 /* A constructor call is a non-trivial initializer even if
5846 it isn't explicitly written. */
5847 if (TREE_SIDE_EFFECTS (init_code))
5848 DECL_NONTRIVIALLY_INITIALIZED_P (decl) = true;
5850 /* If this is a constexpr initializer, expand_default_init will
5851 have returned an INIT_EXPR rather than a CALL_EXPR. In that
5852 case, pull the initializer back out and pass it down into
5853 store_init_value. */
5854 while (TREE_CODE (init_code) == EXPR_STMT
5855 || TREE_CODE (init_code) == CONVERT_EXPR)
5856 init_code = TREE_OPERAND (init_code, 0);
5857 if (TREE_CODE (init_code) == INIT_EXPR)
5859 init = TREE_OPERAND (init_code, 1);
5860 init_code = NULL_TREE;
5861 /* Don't call digest_init; it's unnecessary and will complain
5862 about aggregate initialization of non-aggregate classes. */
5863 flags |= LOOKUP_ALREADY_DIGESTED;
5865 else if (DECL_DECLARED_CONSTEXPR_P (decl))
5867 /* Declared constexpr, but no suitable initializer; massage
5868 init appropriately so we can pass it into store_init_value
5869 for the error. */
5870 if (CLASS_TYPE_P (type)
5871 && (!init || TREE_CODE (init) == TREE_LIST))
5873 init = build_functional_cast (type, init, tf_none);
5874 if (TREE_CODE (init) == TARGET_EXPR)
5875 TARGET_EXPR_DIRECT_INIT_P (init) = true;
5877 init_code = NULL_TREE;
5879 else
5880 init = NULL_TREE;
5883 if (init && TREE_CODE (init) != TREE_VEC)
5885 /* In aggregate initialization of a variable, each element
5886 initialization is a full-expression because there is no
5887 enclosing expression. */
5888 gcc_assert (stmts_are_full_exprs_p ());
5890 init_code = store_init_value (decl, init, cleanups, flags);
5892 if (pedantic && TREE_CODE (type) == ARRAY_TYPE
5893 && DECL_INITIAL (decl)
5894 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
5895 && PAREN_STRING_LITERAL_P (DECL_INITIAL (decl)))
5896 warning (0, "array %qD initialized by parenthesized string literal %qE",
5897 decl, DECL_INITIAL (decl));
5898 init = NULL;
5901 else
5903 if (CLASS_TYPE_P (core_type = strip_array_types (type))
5904 && (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type)
5905 || CLASSTYPE_REF_FIELDS_NEED_INIT (core_type)))
5906 diagnose_uninitialized_cst_or_ref_member (core_type, /*using_new=*/false,
5907 /*complain=*/true);
5909 check_for_uninitialized_const_var (decl);
5912 if (init && init != error_mark_node)
5913 init_code = build2 (INIT_EXPR, type, decl, init);
5915 if (init_code)
5917 /* We might have set these in cp_finish_decl. */
5918 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = false;
5919 TREE_CONSTANT (decl) = false;
5922 if (init_code && DECL_IN_AGGR_P (decl))
5924 static int explained = 0;
5926 if (cxx_dialect < cxx11)
5927 error ("initializer invalid for static member with constructor");
5928 else
5929 error ("non-constant in-class initialization invalid for static "
5930 "member %qD", decl);
5931 if (!explained)
5933 inform (input_location,
5934 "(an out of class initialization is required)");
5935 explained = 1;
5937 return NULL_TREE;
5940 return init_code;
5943 /* If DECL is not a local variable, give it RTL. */
5945 static void
5946 make_rtl_for_nonlocal_decl (tree decl, tree init, const char* asmspec)
5948 int toplev = toplevel_bindings_p ();
5949 int defer_p;
5950 const char *filename;
5952 /* Set the DECL_ASSEMBLER_NAME for the object. */
5953 if (asmspec)
5955 /* The `register' keyword, when used together with an
5956 asm-specification, indicates that the variable should be
5957 placed in a particular register. */
5958 if (VAR_P (decl) && DECL_REGISTER (decl))
5960 set_user_assembler_name (decl, asmspec);
5961 DECL_HARD_REGISTER (decl) = 1;
5963 else
5965 if (TREE_CODE (decl) == FUNCTION_DECL
5966 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
5967 set_builtin_user_assembler_name (decl, asmspec);
5968 set_user_assembler_name (decl, asmspec);
5972 /* Handle non-variables up front. */
5973 if (!VAR_P (decl))
5975 rest_of_decl_compilation (decl, toplev, at_eof);
5976 return;
5979 /* If we see a class member here, it should be a static data
5980 member. */
5981 if (DECL_LANG_SPECIFIC (decl) && DECL_IN_AGGR_P (decl))
5983 gcc_assert (TREE_STATIC (decl));
5984 /* An in-class declaration of a static data member should be
5985 external; it is only a declaration, and not a definition. */
5986 if (init == NULL_TREE)
5987 gcc_assert (DECL_EXTERNAL (decl) || !TREE_PUBLIC (decl));
5990 /* We don't create any RTL for local variables. */
5991 if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
5992 return;
5994 /* We defer emission of local statics until the corresponding
5995 DECL_EXPR is expanded. */
5996 defer_p = DECL_FUNCTION_SCOPE_P (decl) || DECL_VIRTUAL_P (decl);
5998 /* We try to defer namespace-scope static constants so that they are
5999 not emitted into the object file unnecessarily. */
6000 filename = LOCATION_FILE (input_location);
6001 if (!DECL_VIRTUAL_P (decl)
6002 && TREE_READONLY (decl)
6003 && DECL_INITIAL (decl) != NULL_TREE
6004 && DECL_INITIAL (decl) != error_mark_node
6005 && filename != NULL
6006 && ! EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl))
6007 && toplev
6008 && !TREE_PUBLIC (decl))
6010 /* Fool with the linkage of static consts according to #pragma
6011 interface. */
6012 struct c_fileinfo *finfo = get_fileinfo (filename);
6013 if (!finfo->interface_unknown && !TREE_PUBLIC (decl))
6015 TREE_PUBLIC (decl) = 1;
6016 DECL_EXTERNAL (decl) = finfo->interface_only;
6019 defer_p = 1;
6021 /* Likewise for template instantiations. */
6022 else if (DECL_LANG_SPECIFIC (decl)
6023 && DECL_IMPLICIT_INSTANTIATION (decl))
6024 defer_p = 1;
6026 /* Capture the current module info. */
6027 if (L_IPO_COMP_MODE)
6028 varpool_node::get_create (decl);
6030 /* If we're not deferring, go ahead and assemble the variable. */
6031 if (!defer_p)
6032 rest_of_decl_compilation (decl, toplev, at_eof);
6035 /* walk_tree helper for wrap_temporary_cleanups, below. */
6037 static tree
6038 wrap_cleanups_r (tree *stmt_p, int *walk_subtrees, void *data)
6040 /* Stop at types or full-expression boundaries. */
6041 if (TYPE_P (*stmt_p)
6042 || TREE_CODE (*stmt_p) == CLEANUP_POINT_EXPR)
6044 *walk_subtrees = 0;
6045 return NULL_TREE;
6048 if (TREE_CODE (*stmt_p) == TARGET_EXPR)
6050 tree guard = (tree)data;
6051 tree tcleanup = TARGET_EXPR_CLEANUP (*stmt_p);
6053 tcleanup = build2 (TRY_CATCH_EXPR, void_type_node, tcleanup, guard);
6054 /* Tell honor_protect_cleanup_actions to handle this as a separate
6055 cleanup. */
6056 TRY_CATCH_IS_CLEANUP (tcleanup) = 1;
6058 TARGET_EXPR_CLEANUP (*stmt_p) = tcleanup;
6061 return NULL_TREE;
6064 /* We're initializing a local variable which has a cleanup GUARD. If there
6065 are any temporaries used in the initializer INIT of this variable, we
6066 need to wrap their cleanups with TRY_CATCH_EXPR (, GUARD) so that the
6067 variable will be cleaned up properly if one of them throws.
6069 Unfortunately, there's no way to express this properly in terms of
6070 nesting, as the regions for the temporaries overlap the region for the
6071 variable itself; if there are two temporaries, the variable needs to be
6072 the first thing destroyed if either of them throws. However, we only
6073 want to run the variable's cleanup if it actually got constructed. So
6074 we need to guard the temporary cleanups with the variable's cleanup if
6075 they are run on the normal path, but not if they are run on the
6076 exceptional path. We implement this by telling
6077 honor_protect_cleanup_actions to strip the variable cleanup from the
6078 exceptional path. */
6080 static void
6081 wrap_temporary_cleanups (tree init, tree guard)
6083 cp_walk_tree_without_duplicates (&init, wrap_cleanups_r, (void *)guard);
6086 /* Generate code to initialize DECL (a local variable). */
6088 static void
6089 initialize_local_var (tree decl, tree init)
6091 tree type = TREE_TYPE (decl);
6092 tree cleanup;
6093 int already_used;
6095 gcc_assert (VAR_P (decl)
6096 || TREE_CODE (decl) == RESULT_DECL);
6097 gcc_assert (!TREE_STATIC (decl));
6099 if (DECL_SIZE (decl) == NULL_TREE)
6101 /* If we used it already as memory, it must stay in memory. */
6102 DECL_INITIAL (decl) = NULL_TREE;
6103 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
6104 return;
6107 if (type == error_mark_node)
6108 return;
6110 /* Compute and store the initial value. */
6111 already_used = TREE_USED (decl) || TREE_USED (type);
6112 if (TREE_USED (type))
6113 DECL_READ_P (decl) = 1;
6115 /* Generate a cleanup, if necessary. */
6116 cleanup = cxx_maybe_build_cleanup (decl, tf_warning_or_error);
6118 /* Perform the initialization. */
6119 if (init)
6121 if (TREE_CODE (init) == INIT_EXPR
6122 && !TREE_SIDE_EFFECTS (TREE_OPERAND (init, 1)))
6124 /* Stick simple initializers in DECL_INITIAL so that
6125 -Wno-init-self works (c++/34772). */
6126 gcc_assert (TREE_OPERAND (init, 0) == decl);
6127 DECL_INITIAL (decl) = TREE_OPERAND (init, 1);
6129 else
6131 int saved_stmts_are_full_exprs_p;
6133 /* If we're only initializing a single object, guard the
6134 destructors of any temporaries used in its initializer with
6135 its destructor. This isn't right for arrays because each
6136 element initialization is a full-expression. */
6137 if (cleanup && TREE_CODE (type) != ARRAY_TYPE)
6138 wrap_temporary_cleanups (init, cleanup);
6140 gcc_assert (building_stmt_list_p ());
6141 saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
6142 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
6143 finish_expr_stmt (init);
6144 current_stmt_tree ()->stmts_are_full_exprs_p =
6145 saved_stmts_are_full_exprs_p;
6149 /* Set this to 0 so we can tell whether an aggregate which was
6150 initialized was ever used. Don't do this if it has a
6151 destructor, so we don't complain about the 'resource
6152 allocation is initialization' idiom. Now set
6153 attribute((unused)) on types so decls of that type will be
6154 marked used. (see TREE_USED, above.) */
6155 if (TYPE_NEEDS_CONSTRUCTING (type)
6156 && ! already_used
6157 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type)
6158 && DECL_NAME (decl))
6159 TREE_USED (decl) = 0;
6160 else if (already_used)
6161 TREE_USED (decl) = 1;
6163 if (cleanup)
6164 finish_decl_cleanup (decl, cleanup);
6167 /* DECL is a VAR_DECL for a compiler-generated variable with static
6168 storage duration (like a virtual table) whose initializer is a
6169 compile-time constant. Initialize the variable and provide it to the
6170 back end. */
6172 void
6173 initialize_artificial_var (tree decl, vec<constructor_elt, va_gc> *v)
6175 tree init;
6176 gcc_assert (DECL_ARTIFICIAL (decl));
6177 init = build_constructor (TREE_TYPE (decl), v);
6178 gcc_assert (TREE_CODE (init) == CONSTRUCTOR);
6179 DECL_INITIAL (decl) = init;
6180 DECL_INITIALIZED_P (decl) = 1;
6181 determine_visibility (decl);
6182 layout_var_decl (decl);
6183 maybe_commonize_var (decl);
6184 make_rtl_for_nonlocal_decl (decl, init, /*asmspec=*/NULL);
6187 /* INIT is the initializer for a variable, as represented by the
6188 parser. Returns true iff INIT is type-dependent. */
6190 static bool
6191 type_dependent_init_p (tree init)
6193 if (TREE_CODE (init) == TREE_LIST)
6194 /* A parenthesized initializer, e.g.: int i (3, 2); ? */
6195 return any_type_dependent_elements_p (init);
6196 else if (TREE_CODE (init) == CONSTRUCTOR)
6197 /* A brace-enclosed initializer, e.g.: int i = { 3 }; ? */
6199 vec<constructor_elt, va_gc> *elts;
6200 size_t nelts;
6201 size_t i;
6203 elts = CONSTRUCTOR_ELTS (init);
6204 nelts = vec_safe_length (elts);
6205 for (i = 0; i < nelts; ++i)
6206 if (type_dependent_init_p ((*elts)[i].value))
6207 return true;
6209 else
6210 /* It must be a simple expression, e.g., int i = 3; */
6211 return type_dependent_expression_p (init);
6213 return false;
6216 /* INIT is the initializer for a variable, as represented by the
6217 parser. Returns true iff INIT is value-dependent. */
6219 static bool
6220 value_dependent_init_p (tree init)
6222 if (TREE_CODE (init) == TREE_LIST)
6223 /* A parenthesized initializer, e.g.: int i (3, 2); ? */
6224 return any_value_dependent_elements_p (init);
6225 else if (TREE_CODE (init) == CONSTRUCTOR)
6226 /* A brace-enclosed initializer, e.g.: int i = { 3 }; ? */
6228 vec<constructor_elt, va_gc> *elts;
6229 size_t nelts;
6230 size_t i;
6232 elts = CONSTRUCTOR_ELTS (init);
6233 nelts = vec_safe_length (elts);
6234 for (i = 0; i < nelts; ++i)
6235 if (value_dependent_init_p ((*elts)[i].value))
6236 return true;
6238 else
6239 /* It must be a simple expression, e.g., int i = 3; */
6240 return value_dependent_expression_p (init);
6242 return false;
6245 /* Finish processing of a declaration;
6246 install its line number and initial value.
6247 If the length of an array type is not known before,
6248 it must be determined now, from the initial value, or it is an error.
6250 INIT is the initializer (if any) for DECL. If INIT_CONST_EXPR_P is
6251 true, then INIT is an integral constant expression.
6253 FLAGS is LOOKUP_ONLYCONVERTING if the = init syntax was used, else 0
6254 if the (init) syntax was used. */
6256 void
6257 cp_finish_decl (tree decl, tree init, bool init_const_expr_p,
6258 tree asmspec_tree, int flags)
6260 tree type;
6261 vec<tree, va_gc> *cleanups = NULL;
6262 const char *asmspec = NULL;
6263 int was_readonly = 0;
6264 bool var_definition_p = false;
6265 tree auto_node;
6267 if (decl == error_mark_node)
6268 return;
6269 else if (! decl)
6271 if (init)
6272 error ("assignment (not initialization) in declaration");
6273 return;
6276 gcc_assert (TREE_CODE (decl) != RESULT_DECL);
6277 /* Parameters are handled by store_parm_decls, not cp_finish_decl. */
6278 gcc_assert (TREE_CODE (decl) != PARM_DECL);
6280 type = TREE_TYPE (decl);
6281 if (type == error_mark_node)
6282 return;
6284 /* If a name was specified, get the string. */
6285 if (at_namespace_scope_p ())
6286 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
6287 if (asmspec_tree && asmspec_tree != error_mark_node)
6288 asmspec = TREE_STRING_POINTER (asmspec_tree);
6290 if (current_class_type
6291 && CP_DECL_CONTEXT (decl) == current_class_type
6292 && TYPE_BEING_DEFINED (current_class_type)
6293 && !CLASSTYPE_TEMPLATE_INSTANTIATION (current_class_type)
6294 && (DECL_INITIAL (decl) || init))
6295 DECL_INITIALIZED_IN_CLASS_P (decl) = 1;
6297 if (TREE_CODE (decl) != FUNCTION_DECL
6298 && (auto_node = type_uses_auto (type)))
6300 tree d_init;
6301 if (init == NULL_TREE)
6303 if (DECL_LANG_SPECIFIC (decl)
6304 && DECL_TEMPLATE_INSTANTIATION (decl)
6305 && !DECL_TEMPLATE_INSTANTIATED (decl))
6307 /* init is null because we're deferring instantiating the
6308 initializer until we need it. Well, we need it now. */
6309 instantiate_decl (decl, /*defer_ok*/true, /*expl*/false);
6310 return;
6313 error ("declaration of %q#D has no initializer", decl);
6314 TREE_TYPE (decl) = error_mark_node;
6315 return;
6317 d_init = init;
6318 if (TREE_CODE (d_init) == TREE_LIST)
6319 d_init = build_x_compound_expr_from_list (d_init, ELK_INIT,
6320 tf_warning_or_error);
6321 d_init = resolve_nondeduced_context (d_init);
6322 type = TREE_TYPE (decl) = do_auto_deduction (type, d_init,
6323 auto_node);
6324 if (type == error_mark_node)
6325 return;
6326 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
6329 if (!ensure_literal_type_for_constexpr_object (decl))
6330 DECL_DECLARED_CONSTEXPR_P (decl) = 0;
6332 if (VAR_P (decl)
6333 && DECL_CLASS_SCOPE_P (decl)
6334 && DECL_INITIALIZED_IN_CLASS_P (decl))
6335 check_static_variable_definition (decl, type);
6337 if (init && TREE_CODE (decl) == FUNCTION_DECL)
6339 tree clone;
6340 if (init == ridpointers[(int)RID_DELETE])
6342 /* FIXME check this is 1st decl. */
6343 DECL_DELETED_FN (decl) = 1;
6344 DECL_DECLARED_INLINE_P (decl) = 1;
6345 DECL_INITIAL (decl) = error_mark_node;
6346 FOR_EACH_CLONE (clone, decl)
6348 DECL_DELETED_FN (clone) = 1;
6349 DECL_DECLARED_INLINE_P (clone) = 1;
6350 DECL_INITIAL (clone) = error_mark_node;
6352 init = NULL_TREE;
6354 else if (init == ridpointers[(int)RID_DEFAULT])
6356 if (defaultable_fn_check (decl))
6357 DECL_DEFAULTED_FN (decl) = 1;
6358 else
6359 DECL_INITIAL (decl) = NULL_TREE;
6363 if (init && VAR_P (decl))
6365 DECL_NONTRIVIALLY_INITIALIZED_P (decl) = 1;
6366 /* If DECL is a reference, then we want to know whether init is a
6367 reference constant; init_const_expr_p as passed tells us whether
6368 it's an rvalue constant. */
6369 if (TREE_CODE (type) == REFERENCE_TYPE)
6370 init_const_expr_p = potential_constant_expression (init);
6371 if (init_const_expr_p)
6373 /* Set these flags now for templates. We'll update the flags in
6374 store_init_value for instantiations. */
6375 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
6376 if (decl_maybe_constant_var_p (decl))
6377 TREE_CONSTANT (decl) = 1;
6381 if (processing_template_decl)
6383 bool type_dependent_p;
6385 /* Add this declaration to the statement-tree. */
6386 if (at_function_scope_p ())
6387 add_decl_expr (decl);
6389 type_dependent_p = dependent_type_p (type);
6391 if (check_for_bare_parameter_packs (init))
6393 init = NULL_TREE;
6394 DECL_INITIAL (decl) = NULL_TREE;
6397 /* Generally, initializers in templates are expanded when the
6398 template is instantiated. But, if DECL is a variable constant
6399 then it can be used in future constant expressions, so its value
6400 must be available. */
6402 if (!VAR_P (decl) || dependent_type_p (type))
6403 /* We can't do anything if the decl has dependent type. */;
6404 else if (init
6405 && init_const_expr_p
6406 && !type_dependent_p
6407 && decl_maybe_constant_var_p (decl)
6408 && !type_dependent_init_p (init)
6409 && !value_dependent_init_p (init))
6411 /* This variable seems to be a non-dependent constant, so process
6412 its initializer. If check_initializer returns non-null the
6413 initialization wasn't constant after all. */
6414 tree init_code;
6415 cleanups = make_tree_vector ();
6416 init_code = check_initializer (decl, init, flags, &cleanups);
6417 if (init_code == NULL_TREE)
6418 init = NULL_TREE;
6419 release_tree_vector (cleanups);
6421 else if (!DECL_PRETTY_FUNCTION_P (decl))
6423 /* Deduce array size even if the initializer is dependent. */
6424 maybe_deduce_size_from_array_init (decl, init);
6425 /* And complain about multiple initializers. */
6426 if (init && TREE_CODE (init) == TREE_LIST && TREE_CHAIN (init)
6427 && !MAYBE_CLASS_TYPE_P (type))
6428 init = build_x_compound_expr_from_list (init, ELK_INIT,
6429 tf_warning_or_error);
6432 if (init)
6433 DECL_INITIAL (decl) = init;
6434 return;
6437 /* Just store non-static data member initializers for later. */
6438 if (init && TREE_CODE (decl) == FIELD_DECL)
6439 DECL_INITIAL (decl) = init;
6441 /* Take care of TYPE_DECLs up front. */
6442 if (TREE_CODE (decl) == TYPE_DECL)
6444 if (type != error_mark_node
6445 && MAYBE_CLASS_TYPE_P (type) && DECL_NAME (decl))
6447 if (TREE_TYPE (DECL_NAME (decl)) && TREE_TYPE (decl) != type)
6448 warning (0, "shadowing previous type declaration of %q#D", decl);
6449 set_identifier_type_value (DECL_NAME (decl), decl);
6452 /* If we have installed this as the canonical typedef for this
6453 type, and that type has not been defined yet, delay emitting
6454 the debug information for it, as we will emit it later. */
6455 if (TYPE_MAIN_DECL (TREE_TYPE (decl)) == decl
6456 && !COMPLETE_TYPE_P (TREE_TYPE (decl)))
6457 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
6459 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl),
6460 at_eof);
6461 return;
6464 /* A reference will be modified here, as it is initialized. */
6465 if (! DECL_EXTERNAL (decl)
6466 && TREE_READONLY (decl)
6467 && TREE_CODE (type) == REFERENCE_TYPE)
6469 was_readonly = 1;
6470 TREE_READONLY (decl) = 0;
6473 if (VAR_P (decl))
6475 /* If this is a local variable that will need a mangled name,
6476 register it now. We must do this before processing the
6477 initializer for the variable, since the initialization might
6478 require a guard variable, and since the mangled name of the
6479 guard variable will depend on the mangled name of this
6480 variable. */
6481 if (DECL_FUNCTION_SCOPE_P (decl)
6482 && TREE_STATIC (decl)
6483 && !DECL_ARTIFICIAL (decl))
6485 push_local_name (decl);
6486 if (DECL_CONSTRUCTOR_P (current_function_decl)
6487 || DECL_DESTRUCTOR_P (current_function_decl))
6488 /* Normally local_decls is populated during GIMPLE lowering,
6489 but [cd]tors are never actually compiled directly. We need
6490 to put statics on the list so we can deal with the label
6491 address extension. FIXME. */
6492 add_local_decl (cfun, decl);
6495 /* Convert the initializer to the type of DECL, if we have not
6496 already initialized DECL. */
6497 if (!DECL_INITIALIZED_P (decl)
6498 /* If !DECL_EXTERNAL then DECL is being defined. In the
6499 case of a static data member initialized inside the
6500 class-specifier, there can be an initializer even if DECL
6501 is *not* defined. */
6502 && (!DECL_EXTERNAL (decl) || init))
6504 if (TYPE_FOR_JAVA (type) && MAYBE_CLASS_TYPE_P (type))
6506 tree jclass
6507 = IDENTIFIER_GLOBAL_VALUE (get_identifier ("jclass"));
6508 /* Allow libjava/prims.cc define primitive classes. */
6509 if (init != NULL_TREE
6510 || jclass == NULL_TREE
6511 || TREE_CODE (jclass) != TYPE_DECL
6512 || !POINTER_TYPE_P (TREE_TYPE (jclass))
6513 || !same_type_ignoring_top_level_qualifiers_p
6514 (type, TREE_TYPE (TREE_TYPE (jclass))))
6515 error ("Java object %qD not allocated with %<new%>", decl);
6516 init = NULL_TREE;
6518 cleanups = make_tree_vector ();
6519 init = check_initializer (decl, init, flags, &cleanups);
6521 /* Handle:
6523 [dcl.init]
6525 The memory occupied by any object of static storage
6526 duration is zero-initialized at program startup before
6527 any other initialization takes place.
6529 We cannot create an appropriate initializer until after
6530 the type of DECL is finalized. If DECL_INITIAL is set,
6531 then the DECL is statically initialized, and any
6532 necessary zero-initialization has already been performed. */
6533 if (TREE_STATIC (decl) && !DECL_INITIAL (decl))
6534 DECL_INITIAL (decl) = build_zero_init (TREE_TYPE (decl),
6535 /*nelts=*/NULL_TREE,
6536 /*static_storage_p=*/true);
6537 /* Remember that the initialization for this variable has
6538 taken place. */
6539 DECL_INITIALIZED_P (decl) = 1;
6540 /* This declaration is the definition of this variable,
6541 unless we are initializing a static data member within
6542 the class specifier. */
6543 if (!DECL_EXTERNAL (decl))
6544 var_definition_p = true;
6546 /* If the variable has an array type, lay out the type, even if
6547 there is no initializer. It is valid to index through the
6548 array, and we must get TYPE_ALIGN set correctly on the array
6549 type. */
6550 else if (TREE_CODE (type) == ARRAY_TYPE)
6551 layout_type (type);
6553 if (TREE_STATIC (decl)
6554 && !at_function_scope_p ()
6555 && current_function_decl == NULL)
6556 /* So decl is a global variable or a static member of a
6557 non local class. Record the types it uses
6558 so that we can decide later to emit debug info for them. */
6559 record_types_used_by_current_var_decl (decl);
6561 else if (TREE_CODE (decl) == FIELD_DECL
6562 && TYPE_FOR_JAVA (type) && MAYBE_CLASS_TYPE_P (type))
6563 error ("non-static data member %qD has Java class type", decl);
6565 /* Add this declaration to the statement-tree. This needs to happen
6566 after the call to check_initializer so that the DECL_EXPR for a
6567 reference temp is added before the DECL_EXPR for the reference itself. */
6568 if (DECL_FUNCTION_SCOPE_P (decl))
6570 /* If we're building a variable sized type, and we might be
6571 reachable other than via the top of the current binding
6572 level, then create a new BIND_EXPR so that we deallocate
6573 the object at the right time. */
6574 if (VAR_P (decl)
6575 && DECL_SIZE (decl)
6576 && !TREE_CONSTANT (DECL_SIZE (decl))
6577 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
6579 tree bind;
6580 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
6581 TREE_SIDE_EFFECTS (bind) = 1;
6582 add_stmt (bind);
6583 BIND_EXPR_BODY (bind) = push_stmt_list ();
6585 add_decl_expr (decl);
6588 /* Let the middle end know about variables and functions -- but not
6589 static data members in uninstantiated class templates. */
6590 if (VAR_OR_FUNCTION_DECL_P (decl))
6592 if (VAR_P (decl))
6594 layout_var_decl (decl);
6595 maybe_commonize_var (decl);
6598 /* This needs to happen after the linkage is set. */
6599 determine_visibility (decl);
6601 if (var_definition_p && TREE_STATIC (decl))
6603 /* If a TREE_READONLY variable needs initialization
6604 at runtime, it is no longer readonly and we need to
6605 avoid MEM_READONLY_P being set on RTL created for it. */
6606 if (init)
6608 if (TREE_READONLY (decl))
6609 TREE_READONLY (decl) = 0;
6610 was_readonly = 0;
6612 else if (was_readonly)
6613 TREE_READONLY (decl) = 1;
6615 /* Likewise if it needs destruction. */
6616 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
6617 TREE_READONLY (decl) = 0;
6620 make_rtl_for_nonlocal_decl (decl, init, asmspec);
6622 /* Check for abstractness of the type. Notice that there is no
6623 need to strip array types here since the check for those types
6624 is already done within create_array_type_for_decl. */
6625 abstract_virtuals_error (decl, type);
6627 if (TREE_TYPE (decl) == error_mark_node)
6628 /* No initialization required. */
6630 else if (TREE_CODE (decl) == FUNCTION_DECL)
6632 if (init)
6634 if (init == ridpointers[(int)RID_DEFAULT])
6636 /* An out-of-class default definition is defined at
6637 the point where it is explicitly defaulted. */
6638 if (DECL_DELETED_FN (decl))
6639 maybe_explain_implicit_delete (decl);
6640 else if (DECL_INITIAL (decl) == error_mark_node)
6641 synthesize_method (decl);
6643 else
6644 error ("function %q#D is initialized like a variable", decl);
6646 /* else no initialization required. */
6648 else if (DECL_EXTERNAL (decl)
6649 && ! (DECL_LANG_SPECIFIC (decl)
6650 && DECL_NOT_REALLY_EXTERN (decl)))
6652 if (init)
6653 DECL_INITIAL (decl) = init;
6655 /* A variable definition. */
6656 else if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
6657 /* Initialize the local variable. */
6658 initialize_local_var (decl, init);
6660 /* If a variable is defined, and then a subsequent
6661 definition with external linkage is encountered, we will
6662 get here twice for the same variable. We want to avoid
6663 calling expand_static_init more than once. For variables
6664 that are not static data members, we can call
6665 expand_static_init only when we actually process the
6666 initializer. It is not legal to redeclare a static data
6667 member, so this issue does not arise in that case. */
6668 else if (var_definition_p && TREE_STATIC (decl))
6669 expand_static_init (decl, init);
6672 /* If a CLEANUP_STMT was created to destroy a temporary bound to a
6673 reference, insert it in the statement-tree now. */
6674 if (cleanups)
6676 unsigned i; tree t;
6677 FOR_EACH_VEC_ELT (*cleanups, i, t)
6678 push_cleanup (decl, t, false);
6679 release_tree_vector (cleanups);
6682 if (was_readonly)
6683 TREE_READONLY (decl) = 1;
6685 invoke_plugin_callbacks (PLUGIN_FINISH_DECL, decl);
6688 /* Returns a declaration for a VAR_DECL as if:
6690 extern "C" TYPE NAME;
6692 had been seen. Used to create compiler-generated global
6693 variables. */
6695 static tree
6696 declare_global_var (tree name, tree type)
6698 tree decl;
6700 push_to_top_level ();
6701 decl = build_decl (input_location, VAR_DECL, name, type);
6702 TREE_PUBLIC (decl) = 1;
6703 DECL_EXTERNAL (decl) = 1;
6704 DECL_ARTIFICIAL (decl) = 1;
6705 /* If the user has explicitly declared this variable (perhaps
6706 because the code we are compiling is part of a low-level runtime
6707 library), then it is possible that our declaration will be merged
6708 with theirs by pushdecl. */
6709 decl = pushdecl (decl);
6710 cp_finish_decl (decl, NULL_TREE, false, NULL_TREE, 0);
6711 pop_from_top_level ();
6713 return decl;
6716 /* Returns the type for the argument to "__cxa_atexit" (or "atexit",
6717 if "__cxa_atexit" is not being used) corresponding to the function
6718 to be called when the program exits. */
6720 static tree
6721 get_atexit_fn_ptr_type (void)
6723 tree fn_type;
6725 if (!atexit_fn_ptr_type_node)
6727 tree arg_type;
6728 if (flag_use_cxa_atexit
6729 && !targetm.cxx.use_atexit_for_cxa_atexit ())
6730 /* The parameter to "__cxa_atexit" is "void (*)(void *)". */
6731 arg_type = ptr_type_node;
6732 else
6733 /* The parameter to "atexit" is "void (*)(void)". */
6734 arg_type = NULL_TREE;
6736 fn_type = build_function_type_list (void_type_node,
6737 arg_type, NULL_TREE);
6738 atexit_fn_ptr_type_node = build_pointer_type (fn_type);
6741 return atexit_fn_ptr_type_node;
6744 /* Returns a pointer to the `atexit' function. Note that if
6745 FLAG_USE_CXA_ATEXIT is nonzero, then this will actually be the new
6746 `__cxa_atexit' function specified in the IA64 C++ ABI. */
6748 static tree
6749 get_atexit_node (void)
6751 tree atexit_fndecl;
6752 tree fn_type;
6753 tree fn_ptr_type;
6754 const char *name;
6755 bool use_aeabi_atexit;
6757 if (atexit_node)
6758 return atexit_node;
6760 if (flag_use_cxa_atexit && !targetm.cxx.use_atexit_for_cxa_atexit ())
6762 /* The declaration for `__cxa_atexit' is:
6764 int __cxa_atexit (void (*)(void *), void *, void *)
6766 We build up the argument types and then the function type
6767 itself. */
6768 tree argtype0, argtype1, argtype2;
6770 use_aeabi_atexit = targetm.cxx.use_aeabi_atexit ();
6771 /* First, build the pointer-to-function type for the first
6772 argument. */
6773 fn_ptr_type = get_atexit_fn_ptr_type ();
6774 /* Then, build the rest of the argument types. */
6775 argtype2 = ptr_type_node;
6776 if (use_aeabi_atexit)
6778 argtype1 = fn_ptr_type;
6779 argtype0 = ptr_type_node;
6781 else
6783 argtype1 = ptr_type_node;
6784 argtype0 = fn_ptr_type;
6786 /* And the final __cxa_atexit type. */
6787 fn_type = build_function_type_list (integer_type_node,
6788 argtype0, argtype1, argtype2,
6789 NULL_TREE);
6790 if (use_aeabi_atexit)
6791 name = "__aeabi_atexit";
6792 else
6793 name = "__cxa_atexit";
6795 else
6797 /* The declaration for `atexit' is:
6799 int atexit (void (*)());
6801 We build up the argument types and then the function type
6802 itself. */
6803 fn_ptr_type = get_atexit_fn_ptr_type ();
6804 /* Build the final atexit type. */
6805 fn_type = build_function_type_list (integer_type_node,
6806 fn_ptr_type, NULL_TREE);
6807 name = "atexit";
6810 /* Now, build the function declaration. */
6811 push_lang_context (lang_name_c);
6812 atexit_fndecl = build_library_fn_ptr (name, fn_type, ECF_LEAF | ECF_NOTHROW);
6813 mark_used (atexit_fndecl);
6814 pop_lang_context ();
6815 atexit_node = decay_conversion (atexit_fndecl, tf_warning_or_error);
6817 return atexit_node;
6820 /* Like get_atexit_node, but for thread-local cleanups. */
6822 static tree
6823 get_thread_atexit_node (void)
6825 /* The declaration for `__cxa_thread_atexit' is:
6827 int __cxa_thread_atexit (void (*)(void *), void *, void *) */
6828 tree fn_type = build_function_type_list (integer_type_node,
6829 get_atexit_fn_ptr_type (),
6830 ptr_type_node, ptr_type_node,
6831 NULL_TREE);
6833 /* Now, build the function declaration. */
6834 tree atexit_fndecl = build_library_fn_ptr ("__cxa_thread_atexit", fn_type,
6835 ECF_LEAF | ECF_NOTHROW);
6836 return decay_conversion (atexit_fndecl, tf_warning_or_error);
6839 /* Returns the __dso_handle VAR_DECL. */
6841 static tree
6842 get_dso_handle_node (void)
6844 if (dso_handle_node)
6845 return dso_handle_node;
6847 /* Declare the variable. */
6848 dso_handle_node = declare_global_var (get_identifier ("__dso_handle"),
6849 ptr_type_node);
6851 #ifdef HAVE_GAS_HIDDEN
6852 if (dso_handle_node != error_mark_node)
6854 DECL_VISIBILITY (dso_handle_node) = VISIBILITY_HIDDEN;
6855 DECL_VISIBILITY_SPECIFIED (dso_handle_node) = 1;
6857 #endif
6859 return dso_handle_node;
6862 /* Begin a new function with internal linkage whose job will be simply
6863 to destroy some particular variable. */
6865 static GTY(()) int start_cleanup_cnt;
6867 static tree
6868 start_cleanup_fn (void)
6870 char name[32];
6871 tree fntype;
6872 tree fndecl;
6873 bool use_cxa_atexit = flag_use_cxa_atexit
6874 && !targetm.cxx.use_atexit_for_cxa_atexit ();
6876 push_to_top_level ();
6878 /* No need to mangle this. */
6879 push_lang_context (lang_name_c);
6881 /* Build the name of the function. */
6882 sprintf (name, "__tcf_%d", start_cleanup_cnt++);
6883 /* Build the function declaration. */
6884 fntype = TREE_TYPE (get_atexit_fn_ptr_type ());
6885 fndecl = build_lang_decl (FUNCTION_DECL, get_identifier (name), fntype);
6886 /* It's a function with internal linkage, generated by the
6887 compiler. */
6888 TREE_PUBLIC (fndecl) = 0;
6889 DECL_ARTIFICIAL (fndecl) = 1;
6890 /* Make the function `inline' so that it is only emitted if it is
6891 actually needed. It is unlikely that it will be inlined, since
6892 it is only called via a function pointer, but we avoid unnecessary
6893 emissions this way. */
6894 DECL_DECLARED_INLINE_P (fndecl) = 1;
6895 DECL_INTERFACE_KNOWN (fndecl) = 1;
6896 /* Build the parameter. */
6897 if (use_cxa_atexit)
6899 tree parmdecl;
6901 parmdecl = cp_build_parm_decl (NULL_TREE, ptr_type_node);
6902 DECL_CONTEXT (parmdecl) = fndecl;
6903 TREE_USED (parmdecl) = 1;
6904 DECL_READ_P (parmdecl) = 1;
6905 DECL_ARGUMENTS (fndecl) = parmdecl;
6908 pushdecl (fndecl);
6909 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
6911 pop_lang_context ();
6913 return current_function_decl;
6916 /* Finish the cleanup function begun by start_cleanup_fn. */
6918 static void
6919 end_cleanup_fn (void)
6921 expand_or_defer_fn (finish_function (0));
6923 pop_from_top_level ();
6926 /* Generate code to handle the destruction of DECL, an object with
6927 static storage duration. */
6929 tree
6930 register_dtor_fn (tree decl)
6932 tree cleanup;
6933 tree addr;
6934 tree compound_stmt;
6935 tree fcall;
6936 tree type;
6937 bool ob_parm, dso_parm, use_dtor;
6938 tree arg0, arg1, arg2;
6939 tree atex_node;
6941 type = TREE_TYPE (decl);
6942 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
6943 return void_node;
6945 /* If we're using "__cxa_atexit" (or "__cxa_thread_atexit" or
6946 "__aeabi_atexit"), and DECL is a class object, we can just pass the
6947 destructor to "__cxa_atexit"; we don't have to build a temporary
6948 function to do the cleanup. */
6949 dso_parm = (flag_use_cxa_atexit
6950 && !targetm.cxx.use_atexit_for_cxa_atexit ());
6951 ob_parm = (DECL_THREAD_LOCAL_P (decl) || dso_parm);
6952 use_dtor = ob_parm && CLASS_TYPE_P (type);
6953 if (use_dtor)
6955 int idx;
6957 /* Find the destructor. */
6958 idx = lookup_fnfields_1 (type, complete_dtor_identifier);
6959 gcc_assert (idx >= 0);
6960 cleanup = (*CLASSTYPE_METHOD_VEC (type))[idx];
6961 /* Make sure it is accessible. */
6962 perform_or_defer_access_check (TYPE_BINFO (type), cleanup, cleanup,
6963 tf_warning_or_error);
6965 else
6967 /* Call build_cleanup before we enter the anonymous function so
6968 that any access checks will be done relative to the current
6969 scope, rather than the scope of the anonymous function. */
6970 build_cleanup (decl);
6972 /* Now start the function. */
6973 cleanup = start_cleanup_fn ();
6975 /* Now, recompute the cleanup. It may contain SAVE_EXPRs that refer
6976 to the original function, rather than the anonymous one. That
6977 will make the back end think that nested functions are in use,
6978 which causes confusion. */
6979 push_deferring_access_checks (dk_no_check);
6980 fcall = build_cleanup (decl);
6981 pop_deferring_access_checks ();
6983 /* Create the body of the anonymous function. */
6984 compound_stmt = begin_compound_stmt (BCS_FN_BODY);
6985 finish_expr_stmt (fcall);
6986 finish_compound_stmt (compound_stmt);
6987 end_cleanup_fn ();
6990 /* Call atexit with the cleanup function. */
6991 mark_used (cleanup);
6992 cleanup = build_address (cleanup);
6994 if (DECL_THREAD_LOCAL_P (decl))
6995 atex_node = get_thread_atexit_node ();
6996 else
6997 atex_node = get_atexit_node ();
6999 if (use_dtor)
7001 /* We must convert CLEANUP to the type that "__cxa_atexit"
7002 expects. */
7003 cleanup = build_nop (get_atexit_fn_ptr_type (), cleanup);
7004 /* "__cxa_atexit" will pass the address of DECL to the
7005 cleanup function. */
7006 mark_used (decl);
7007 addr = build_address (decl);
7008 /* The declared type of the parameter to "__cxa_atexit" is
7009 "void *". For plain "T*", we could just let the
7010 machinery in cp_build_function_call convert it -- but if the
7011 type is "cv-qualified T *", then we need to convert it
7012 before passing it in, to avoid spurious errors. */
7013 addr = build_nop (ptr_type_node, addr);
7015 else
7016 /* Since the cleanup functions we build ignore the address
7017 they're given, there's no reason to pass the actual address
7018 in, and, in general, it's cheaper to pass NULL than any
7019 other value. */
7020 addr = null_pointer_node;
7022 if (dso_parm)
7023 arg2 = cp_build_addr_expr (get_dso_handle_node (),
7024 tf_warning_or_error);
7025 else if (ob_parm)
7026 /* Just pass NULL to the dso handle parm if we don't actually
7027 have a DSO handle on this target. */
7028 arg2 = null_pointer_node;
7029 else
7030 arg2 = NULL_TREE;
7032 if (ob_parm)
7034 if (!DECL_THREAD_LOCAL_P (decl)
7035 && targetm.cxx.use_aeabi_atexit ())
7037 arg1 = cleanup;
7038 arg0 = addr;
7040 else
7042 arg1 = addr;
7043 arg0 = cleanup;
7046 else
7048 arg0 = cleanup;
7049 arg1 = NULL_TREE;
7051 return cp_build_function_call_nary (atex_node, tf_warning_or_error,
7052 arg0, arg1, arg2, NULL_TREE);
7055 /* DECL is a VAR_DECL with static storage duration. INIT, if present,
7056 is its initializer. Generate code to handle the construction
7057 and destruction of DECL. */
7059 static void
7060 expand_static_init (tree decl, tree init)
7062 gcc_assert (VAR_P (decl));
7063 gcc_assert (TREE_STATIC (decl));
7065 /* Some variables require no dynamic initialization. */
7066 if (!init
7067 && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
7069 /* Make sure the destructor is callable. */
7070 cxx_maybe_build_cleanup (decl, tf_warning_or_error);
7071 return;
7074 if (DECL_THREAD_LOCAL_P (decl) && DECL_GNU_TLS_P (decl)
7075 && !DECL_FUNCTION_SCOPE_P (decl))
7077 if (init)
7078 error ("non-local variable %qD declared %<__thread%> "
7079 "needs dynamic initialization", decl);
7080 else
7081 error ("non-local variable %qD declared %<__thread%> "
7082 "has a non-trivial destructor", decl);
7083 static bool informed;
7084 if (!informed)
7086 inform (DECL_SOURCE_LOCATION (decl),
7087 "C++11 %<thread_local%> allows dynamic initialization "
7088 "and destruction");
7089 informed = true;
7091 return;
7094 if (DECL_FUNCTION_SCOPE_P (decl))
7096 /* Emit code to perform this initialization but once. */
7097 tree if_stmt = NULL_TREE, inner_if_stmt = NULL_TREE;
7098 tree then_clause = NULL_TREE, inner_then_clause = NULL_TREE;
7099 tree guard, guard_addr;
7100 tree flag, begin;
7101 /* We don't need thread-safety code for thread-local vars. */
7102 bool thread_guard = (flag_threadsafe_statics
7103 && !DECL_THREAD_LOCAL_P (decl));
7105 /* Emit code to perform this initialization but once. This code
7106 looks like:
7108 static <type> guard;
7109 if (!guard.first_byte) {
7110 if (__cxa_guard_acquire (&guard)) {
7111 bool flag = false;
7112 try {
7113 // Do initialization.
7114 flag = true; __cxa_guard_release (&guard);
7115 // Register variable for destruction at end of program.
7116 } catch {
7117 if (!flag) __cxa_guard_abort (&guard);
7121 Note that the `flag' variable is only set to 1 *after* the
7122 initialization is complete. This ensures that an exception,
7123 thrown during the construction, will cause the variable to
7124 reinitialized when we pass through this code again, as per:
7126 [stmt.dcl]
7128 If the initialization exits by throwing an exception, the
7129 initialization is not complete, so it will be tried again
7130 the next time control enters the declaration.
7132 This process should be thread-safe, too; multiple threads
7133 should not be able to initialize the variable more than
7134 once. */
7136 /* Create the guard variable. */
7137 guard = get_guard (decl);
7139 /* This optimization isn't safe on targets with relaxed memory
7140 consistency. On such targets we force synchronization in
7141 __cxa_guard_acquire. */
7142 if (!targetm.relaxed_ordering || !thread_guard)
7144 /* Begin the conditional initialization. */
7145 if_stmt = begin_if_stmt ();
7146 finish_if_stmt_cond (get_guard_cond (guard), if_stmt);
7147 then_clause = begin_compound_stmt (BCS_NO_SCOPE);
7150 if (thread_guard)
7152 tree vfntype = NULL_TREE;
7153 tree acquire_name, release_name, abort_name;
7154 tree acquire_fn, release_fn, abort_fn;
7155 guard_addr = build_address (guard);
7157 acquire_name = get_identifier ("__cxa_guard_acquire");
7158 release_name = get_identifier ("__cxa_guard_release");
7159 abort_name = get_identifier ("__cxa_guard_abort");
7160 acquire_fn = identifier_global_value (acquire_name);
7161 release_fn = identifier_global_value (release_name);
7162 abort_fn = identifier_global_value (abort_name);
7163 if (!acquire_fn)
7164 acquire_fn = push_library_fn
7165 (acquire_name, build_function_type_list (integer_type_node,
7166 TREE_TYPE (guard_addr),
7167 NULL_TREE),
7168 NULL_TREE, ECF_NOTHROW | ECF_LEAF);
7169 if (!release_fn || !abort_fn)
7170 vfntype = build_function_type_list (void_type_node,
7171 TREE_TYPE (guard_addr),
7172 NULL_TREE);
7173 if (!release_fn)
7174 release_fn = push_library_fn (release_name, vfntype, NULL_TREE,
7175 ECF_NOTHROW | ECF_LEAF);
7176 if (!abort_fn)
7177 abort_fn = push_library_fn (abort_name, vfntype, NULL_TREE,
7178 ECF_NOTHROW | ECF_LEAF);
7180 inner_if_stmt = begin_if_stmt ();
7181 finish_if_stmt_cond (build_call_n (acquire_fn, 1, guard_addr),
7182 inner_if_stmt);
7184 inner_then_clause = begin_compound_stmt (BCS_NO_SCOPE);
7185 begin = get_target_expr (boolean_false_node);
7186 flag = TARGET_EXPR_SLOT (begin);
7188 TARGET_EXPR_CLEANUP (begin)
7189 = build3 (COND_EXPR, void_type_node, flag,
7190 void_node,
7191 build_call_n (abort_fn, 1, guard_addr));
7192 CLEANUP_EH_ONLY (begin) = 1;
7194 /* Do the initialization itself. */
7195 init = add_stmt_to_compound (begin, init);
7196 init = add_stmt_to_compound
7197 (init, build2 (MODIFY_EXPR, void_type_node, flag, boolean_true_node));
7198 init = add_stmt_to_compound
7199 (init, build_call_n (release_fn, 1, guard_addr));
7201 else
7202 init = add_stmt_to_compound (init, set_guard (guard));
7204 /* Use atexit to register a function for destroying this static
7205 variable. */
7206 init = add_stmt_to_compound (init, register_dtor_fn (decl));
7208 finish_expr_stmt (init);
7210 if (thread_guard)
7212 finish_compound_stmt (inner_then_clause);
7213 finish_then_clause (inner_if_stmt);
7214 finish_if_stmt (inner_if_stmt);
7217 if (!targetm.relaxed_ordering || !thread_guard)
7219 finish_compound_stmt (then_clause);
7220 finish_then_clause (if_stmt);
7221 finish_if_stmt (if_stmt);
7224 else if (DECL_THREAD_LOCAL_P (decl))
7225 tls_aggregates = tree_cons (init, decl, tls_aggregates);
7226 else
7227 static_aggregates = tree_cons (init, decl, static_aggregates);
7231 /* Make TYPE a complete type based on INITIAL_VALUE.
7232 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
7233 2 if there was no information (in which case assume 0 if DO_DEFAULT),
7234 3 if the initializer list is empty (in pedantic mode). */
7237 cp_complete_array_type (tree *ptype, tree initial_value, bool do_default)
7239 int failure;
7240 tree type, elt_type;
7242 /* Don't get confused by a CONSTRUCTOR for some other type. */
7243 if (initial_value && TREE_CODE (initial_value) == CONSTRUCTOR
7244 && !BRACE_ENCLOSED_INITIALIZER_P (initial_value))
7245 return 1;
7247 if (initial_value)
7249 unsigned HOST_WIDE_INT i;
7250 tree value;
7252 /* An array of character type can be initialized from a
7253 brace-enclosed string constant.
7255 FIXME: this code is duplicated from reshape_init. Probably
7256 we should just call reshape_init here? */
7257 if (char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (*ptype)))
7258 && TREE_CODE (initial_value) == CONSTRUCTOR
7259 && !vec_safe_is_empty (CONSTRUCTOR_ELTS (initial_value)))
7261 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value);
7262 tree value = (*v)[0].value;
7264 if (TREE_CODE (value) == STRING_CST
7265 && v->length () == 1)
7266 initial_value = value;
7269 /* If any of the elements are parameter packs, we can't actually
7270 complete this type now because the array size is dependent. */
7271 if (TREE_CODE (initial_value) == CONSTRUCTOR)
7273 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (initial_value),
7274 i, value)
7276 if (PACK_EXPANSION_P (value))
7277 return 0;
7282 failure = complete_array_type (ptype, initial_value, do_default);
7284 /* We can create the array before the element type is complete, which
7285 means that we didn't have these two bits set in the original type
7286 either. In completing the type, we are expected to propagate these
7287 bits. See also complete_type which does the same thing for arrays
7288 of fixed size. */
7289 type = *ptype;
7290 if (TYPE_DOMAIN (type))
7292 elt_type = TREE_TYPE (type);
7293 TYPE_NEEDS_CONSTRUCTING (type) = TYPE_NEEDS_CONSTRUCTING (elt_type);
7294 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
7295 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (elt_type);
7298 return failure;
7301 /* As above, but either give an error or reject zero-size arrays, depending
7302 on COMPLAIN. */
7305 cp_complete_array_type_or_error (tree *ptype, tree initial_value,
7306 bool do_default, tsubst_flags_t complain)
7308 int failure;
7309 bool sfinae = !(complain & tf_error);
7310 /* In SFINAE context we can't be lenient about zero-size arrays. */
7311 if (sfinae)
7312 ++pedantic;
7313 failure = cp_complete_array_type (ptype, initial_value, do_default);
7314 if (sfinae)
7315 --pedantic;
7316 if (failure)
7318 if (sfinae)
7319 /* Not an error. */;
7320 else if (failure == 1)
7321 error ("initializer fails to determine size of %qT", *ptype);
7322 else if (failure == 2)
7324 if (do_default)
7325 error ("array size missing in %qT", *ptype);
7327 else if (failure == 3)
7328 error ("zero-size array %qT", *ptype);
7329 *ptype = error_mark_node;
7331 return failure;
7334 /* Return zero if something is declared to be a member of type
7335 CTYPE when in the context of CUR_TYPE. STRING is the error
7336 message to print in that case. Otherwise, quietly return 1. */
7338 static int
7339 member_function_or_else (tree ctype, tree cur_type, enum overload_flags flags)
7341 if (ctype && ctype != cur_type)
7343 if (flags == DTOR_FLAG)
7344 error ("destructor for alien class %qT cannot be a member", ctype);
7345 else
7346 error ("constructor for alien class %qT cannot be a member", ctype);
7347 return 0;
7349 return 1;
7352 /* Subroutine of `grokdeclarator'. */
7354 /* Generate errors possibly applicable for a given set of specifiers.
7355 This is for ARM $7.1.2. */
7357 static void
7358 bad_specifiers (tree object,
7359 enum bad_spec_place type,
7360 int virtualp,
7361 int quals,
7362 int inlinep,
7363 int friendp,
7364 int raises)
7366 switch (type)
7368 case BSP_VAR:
7369 if (virtualp)
7370 error ("%qD declared as a %<virtual%> variable", object);
7371 if (inlinep)
7372 error ("%qD declared as an %<inline%> variable", object);
7373 if (quals)
7374 error ("%<const%> and %<volatile%> function specifiers on "
7375 "%qD invalid in variable declaration", object);
7376 break;
7377 case BSP_PARM:
7378 if (virtualp)
7379 error ("%qD declared as a %<virtual%> parameter", object);
7380 if (inlinep)
7381 error ("%qD declared as an %<inline%> parameter", object);
7382 if (quals)
7383 error ("%<const%> and %<volatile%> function specifiers on "
7384 "%qD invalid in parameter declaration", object);
7385 break;
7386 case BSP_TYPE:
7387 if (virtualp)
7388 error ("%qD declared as a %<virtual%> type", object);
7389 if (inlinep)
7390 error ("%qD declared as an %<inline%> type", object);
7391 if (quals)
7392 error ("%<const%> and %<volatile%> function specifiers on "
7393 "%qD invalid in type declaration", object);
7394 break;
7395 case BSP_FIELD:
7396 if (virtualp)
7397 error ("%qD declared as a %<virtual%> field", object);
7398 if (inlinep)
7399 error ("%qD declared as an %<inline%> field", object);
7400 if (quals)
7401 error ("%<const%> and %<volatile%> function specifiers on "
7402 "%qD invalid in field declaration", object);
7403 break;
7404 default:
7405 gcc_unreachable();
7407 if (friendp)
7408 error ("%q+D declared as a friend", object);
7409 if (raises
7410 && (TREE_CODE (object) == TYPE_DECL
7411 || (!TYPE_PTRFN_P (TREE_TYPE (object))
7412 && !TYPE_REFFN_P (TREE_TYPE (object))
7413 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (object)))))
7414 error ("%q+D declared with an exception specification", object);
7417 /* DECL is a member function or static data member and is presently
7418 being defined. Check that the definition is taking place in a
7419 valid namespace. */
7421 static void
7422 check_class_member_definition_namespace (tree decl)
7424 /* These checks only apply to member functions and static data
7425 members. */
7426 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
7427 /* We check for problems with specializations in pt.c in
7428 check_specialization_namespace, where we can issue better
7429 diagnostics. */
7430 if (processing_specialization)
7431 return;
7432 /* There are no restrictions on the placement of
7433 explicit instantiations. */
7434 if (processing_explicit_instantiation)
7435 return;
7436 /* [class.mfct]
7438 A member function definition that appears outside of the
7439 class definition shall appear in a namespace scope enclosing
7440 the class definition.
7442 [class.static.data]
7444 The definition for a static data member shall appear in a
7445 namespace scope enclosing the member's class definition. */
7446 if (!is_ancestor (current_namespace, DECL_CONTEXT (decl)))
7447 permerror (input_location, "definition of %qD is not in namespace enclosing %qT",
7448 decl, DECL_CONTEXT (decl));
7451 /* Build a PARM_DECL for the "this" parameter. TYPE is the
7452 METHOD_TYPE for a non-static member function; QUALS are the
7453 cv-qualifiers that apply to the function. */
7455 tree
7456 build_this_parm (tree type, cp_cv_quals quals)
7458 tree this_type;
7459 tree qual_type;
7460 tree parm;
7461 cp_cv_quals this_quals;
7463 if (CLASS_TYPE_P (type))
7465 this_type
7466 = cp_build_qualified_type (type, quals & ~TYPE_QUAL_RESTRICT);
7467 this_type = build_pointer_type (this_type);
7469 else
7470 this_type = type_of_this_parm (type);
7471 /* The `this' parameter is implicitly `const'; it cannot be
7472 assigned to. */
7473 this_quals = (quals & TYPE_QUAL_RESTRICT) | TYPE_QUAL_CONST;
7474 qual_type = cp_build_qualified_type (this_type, this_quals);
7475 parm = build_artificial_parm (this_identifier, qual_type);
7476 cp_apply_type_quals_to_decl (this_quals, parm);
7477 return parm;
7480 /* DECL is a static member function. Complain if it was declared
7481 with function-cv-quals. */
7483 static void
7484 check_static_quals (tree decl, cp_cv_quals quals)
7486 if (quals != TYPE_UNQUALIFIED)
7487 error ("static member function %q#D declared with type qualifiers",
7488 decl);
7491 /* Helper function. Replace the temporary this parameter injected
7492 during cp_finish_omp_declare_simd with the real this parameter. */
7494 static tree
7495 declare_simd_adjust_this (tree *tp, int *walk_subtrees, void *data)
7497 tree this_parm = (tree) data;
7498 if (TREE_CODE (*tp) == PARM_DECL
7499 && DECL_NAME (*tp) == this_identifier
7500 && *tp != this_parm)
7501 *tp = this_parm;
7502 else if (TYPE_P (*tp))
7503 *walk_subtrees = 0;
7504 return NULL_TREE;
7507 /* CTYPE is class type, or null if non-class.
7508 TYPE is type this FUNCTION_DECL should have, either FUNCTION_TYPE
7509 or METHOD_TYPE.
7510 DECLARATOR is the function's name.
7511 PARMS is a chain of PARM_DECLs for the function.
7512 VIRTUALP is truthvalue of whether the function is virtual or not.
7513 FLAGS are to be passed through to `grokclassfn'.
7514 QUALS are qualifiers indicating whether the function is `const'
7515 or `volatile'.
7516 RAISES is a list of exceptions that this function can raise.
7517 CHECK is 1 if we must find this method in CTYPE, 0 if we should
7518 not look, and -1 if we should not call `grokclassfn' at all.
7520 SFK is the kind of special function (if any) for the new function.
7522 Returns `NULL_TREE' if something goes wrong, after issuing
7523 applicable error messages. */
7525 static tree
7526 grokfndecl (tree ctype,
7527 tree type,
7528 tree declarator,
7529 tree parms,
7530 tree orig_declarator,
7531 int virtualp,
7532 enum overload_flags flags,
7533 cp_cv_quals quals,
7534 cp_ref_qualifier rqual,
7535 tree raises,
7536 int check,
7537 int friendp,
7538 int publicp,
7539 int inlinep,
7540 special_function_kind sfk,
7541 bool funcdef_flag,
7542 int template_count,
7543 tree in_namespace,
7544 tree* attrlist,
7545 location_t location)
7547 tree decl;
7548 int staticp = ctype && TREE_CODE (type) == FUNCTION_TYPE;
7549 tree t;
7551 if (rqual)
7552 type = build_ref_qualified_type (type, rqual);
7553 if (raises)
7554 type = build_exception_variant (type, raises);
7556 decl = build_lang_decl (FUNCTION_DECL, declarator, type);
7558 /* If we have an explicit location, use it, otherwise use whatever
7559 build_lang_decl used (probably input_location). */
7560 if (location != UNKNOWN_LOCATION)
7561 DECL_SOURCE_LOCATION (decl) = location;
7563 if (TREE_CODE (type) == METHOD_TYPE)
7565 tree parm;
7566 parm = build_this_parm (type, quals);
7567 DECL_CHAIN (parm) = parms;
7568 parms = parm;
7570 DECL_ARGUMENTS (decl) = parms;
7571 for (t = parms; t; t = DECL_CHAIN (t))
7572 DECL_CONTEXT (t) = decl;
7573 /* Propagate volatile out from type to decl. */
7574 if (TYPE_VOLATILE (type))
7575 TREE_THIS_VOLATILE (decl) = 1;
7577 /* Setup decl according to sfk. */
7578 switch (sfk)
7580 case sfk_constructor:
7581 case sfk_copy_constructor:
7582 case sfk_move_constructor:
7583 DECL_CONSTRUCTOR_P (decl) = 1;
7584 break;
7585 case sfk_destructor:
7586 DECL_DESTRUCTOR_P (decl) = 1;
7587 break;
7588 default:
7589 break;
7592 /* If pointers to member functions use the least significant bit to
7593 indicate whether a function is virtual, ensure a pointer
7594 to this function will have that bit clear. */
7595 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn
7596 && TREE_CODE (type) == METHOD_TYPE
7597 && DECL_ALIGN (decl) < 2 * BITS_PER_UNIT)
7598 DECL_ALIGN (decl) = 2 * BITS_PER_UNIT;
7600 if (friendp
7601 && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)
7603 if (funcdef_flag)
7604 error
7605 ("defining explicit specialization %qD in friend declaration",
7606 orig_declarator);
7607 else
7609 tree fns = TREE_OPERAND (orig_declarator, 0);
7610 tree args = TREE_OPERAND (orig_declarator, 1);
7612 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
7614 /* Something like `template <class T> friend void f<T>()'. */
7615 error ("invalid use of template-id %qD in declaration "
7616 "of primary template",
7617 orig_declarator);
7618 return NULL_TREE;
7622 /* A friend declaration of the form friend void f<>(). Record
7623 the information in the TEMPLATE_ID_EXPR. */
7624 SET_DECL_IMPLICIT_INSTANTIATION (decl);
7626 gcc_assert (identifier_p (fns) || TREE_CODE (fns) == OVERLOAD);
7627 DECL_TEMPLATE_INFO (decl) = build_template_info (fns, args);
7629 for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t))
7630 if (TREE_PURPOSE (t)
7631 && TREE_CODE (TREE_PURPOSE (t)) == DEFAULT_ARG)
7633 error ("default arguments are not allowed in declaration "
7634 "of friend template specialization %qD",
7635 decl);
7636 return NULL_TREE;
7639 if (inlinep & 1)
7640 error ("%<inline%> is not allowed in declaration of friend "
7641 "template specialization %qD",
7642 decl);
7643 if (inlinep & 2)
7644 error ("%<constexpr%> is not allowed in declaration of friend "
7645 "template specialization %qD",
7646 decl);
7647 if (inlinep)
7648 return NULL_TREE;
7652 /* If this decl has namespace scope, set that up. */
7653 if (in_namespace)
7654 set_decl_namespace (decl, in_namespace, friendp);
7655 else if (!ctype)
7656 DECL_CONTEXT (decl) = FROB_CONTEXT (current_decl_namespace ());
7658 /* `main' and builtins have implicit 'C' linkage. */
7659 if ((MAIN_NAME_P (declarator)
7660 || (IDENTIFIER_LENGTH (declarator) > 10
7661 && IDENTIFIER_POINTER (declarator)[0] == '_'
7662 && IDENTIFIER_POINTER (declarator)[1] == '_'
7663 && strncmp (IDENTIFIER_POINTER (declarator)+2, "builtin_", 8) == 0)
7664 || (targetcm.cxx_implicit_extern_c
7665 && targetcm.cxx_implicit_extern_c(IDENTIFIER_POINTER (declarator))))
7666 && current_lang_name == lang_name_cplusplus
7667 && ctype == NULL_TREE
7668 && DECL_FILE_SCOPE_P (decl))
7669 SET_DECL_LANGUAGE (decl, lang_c);
7671 /* Should probably propagate const out from type to decl I bet (mrs). */
7672 if (staticp)
7674 DECL_STATIC_FUNCTION_P (decl) = 1;
7675 DECL_CONTEXT (decl) = ctype;
7678 if (ctype)
7680 DECL_CONTEXT (decl) = ctype;
7681 if (funcdef_flag)
7682 check_class_member_definition_namespace (decl);
7685 if (ctype == NULL_TREE && DECL_MAIN_P (decl))
7687 if (PROCESSING_REAL_TEMPLATE_DECL_P())
7688 error ("cannot declare %<::main%> to be a template");
7689 if (inlinep & 1)
7690 error ("cannot declare %<::main%> to be inline");
7691 if (inlinep & 2)
7692 error ("cannot declare %<::main%> to be constexpr");
7693 if (!publicp)
7694 error ("cannot declare %<::main%> to be static");
7695 inlinep = 0;
7696 publicp = 1;
7699 /* Members of anonymous types and local classes have no linkage; make
7700 them internal. If a typedef is made later, this will be changed. */
7701 if (ctype && (TYPE_ANONYMOUS_P (ctype)
7702 || decl_function_context (TYPE_MAIN_DECL (ctype))))
7703 publicp = 0;
7705 if (publicp && cxx_dialect == cxx98)
7707 /* [basic.link]: A name with no linkage (notably, the name of a class
7708 or enumeration declared in a local scope) shall not be used to
7709 declare an entity with linkage.
7711 DR 757 relaxes this restriction for C++0x. */
7712 no_linkage_error (decl);
7715 TREE_PUBLIC (decl) = publicp;
7716 if (! publicp)
7718 DECL_INTERFACE_KNOWN (decl) = 1;
7719 DECL_NOT_REALLY_EXTERN (decl) = 1;
7722 /* If the declaration was declared inline, mark it as such. */
7723 if (inlinep)
7725 DECL_DECLARED_INLINE_P (decl) = 1;
7726 DECL_COMDAT (decl) = 1;
7728 if (inlinep & 2)
7729 DECL_DECLARED_CONSTEXPR_P (decl) = true;
7731 DECL_EXTERNAL (decl) = 1;
7732 if (TREE_CODE (type) == FUNCTION_TYPE)
7734 if (quals)
7736 error (ctype
7737 ? G_("static member function %qD cannot have cv-qualifier")
7738 : G_("non-member function %qD cannot have cv-qualifier"),
7739 decl);
7740 quals = TYPE_UNQUALIFIED;
7743 if (rqual)
7745 error (ctype
7746 ? G_("static member function %qD cannot have ref-qualifier")
7747 : G_("non-member function %qD cannot have ref-qualifier"),
7748 decl);
7749 rqual = REF_QUAL_NONE;
7753 if (IDENTIFIER_OPNAME_P (DECL_NAME (decl))
7754 && !grok_op_properties (decl, /*complain=*/true))
7755 return NULL_TREE;
7756 else if (UDLIT_OPER_P (DECL_NAME (decl)))
7758 bool long_long_unsigned_p;
7759 bool long_double_p;
7760 const char *suffix = NULL;
7761 /* [over.literal]/6: Literal operators shall not have C linkage. */
7762 if (DECL_LANGUAGE (decl) == lang_c)
7764 error ("literal operator with C linkage");
7765 return NULL_TREE;
7768 if (DECL_NAMESPACE_SCOPE_P (decl))
7770 if (!check_literal_operator_args (decl, &long_long_unsigned_p,
7771 &long_double_p))
7773 error ("%qD has invalid argument list", decl);
7774 return NULL_TREE;
7777 suffix = UDLIT_OP_SUFFIX (DECL_NAME (decl));
7778 if (long_long_unsigned_p)
7780 if (cpp_interpret_int_suffix (parse_in, suffix, strlen (suffix)))
7781 warning (0, "integer suffix %<%s%>"
7782 " shadowed by implementation", suffix);
7784 else if (long_double_p)
7786 if (cpp_interpret_float_suffix (parse_in, suffix, strlen (suffix)))
7787 warning (0, "floating point suffix %<%s%>"
7788 " shadowed by implementation", suffix);
7791 else
7793 error ("%qD must be a non-member function", decl);
7794 return NULL_TREE;
7798 if (funcdef_flag)
7799 /* Make the init_value nonzero so pushdecl knows this is not
7800 tentative. error_mark_node is replaced later with the BLOCK. */
7801 DECL_INITIAL (decl) = error_mark_node;
7803 if (TYPE_NOTHROW_P (type) || nothrow_libfn_p (decl))
7804 TREE_NOTHROW (decl) = 1;
7806 if (flag_openmp || flag_cilkplus)
7808 /* Adjust "omp declare simd" attributes. */
7809 tree ods = lookup_attribute ("omp declare simd", *attrlist);
7810 if (ods)
7812 tree attr;
7813 for (attr = ods; attr;
7814 attr = lookup_attribute ("omp declare simd", TREE_CHAIN (attr)))
7816 if (TREE_CODE (type) == METHOD_TYPE)
7817 walk_tree (&TREE_VALUE (attr), declare_simd_adjust_this,
7818 DECL_ARGUMENTS (decl), NULL);
7819 if (TREE_VALUE (attr) != NULL_TREE)
7821 tree cl = TREE_VALUE (TREE_VALUE (attr));
7822 cl = c_omp_declare_simd_clauses_to_numbers
7823 (DECL_ARGUMENTS (decl), cl);
7824 if (cl)
7825 TREE_VALUE (TREE_VALUE (attr)) = cl;
7826 else
7827 TREE_VALUE (attr) = NULL_TREE;
7833 /* Caller will do the rest of this. */
7834 if (check < 0)
7835 return decl;
7837 if (ctype != NULL_TREE)
7838 grokclassfn (ctype, decl, flags);
7840 /* 12.4/3 */
7841 if (cxx_dialect >= cxx11
7842 && DECL_DESTRUCTOR_P (decl)
7843 && !TYPE_BEING_DEFINED (DECL_CONTEXT (decl))
7844 && !processing_template_decl)
7845 deduce_noexcept_on_destructor (decl);
7847 decl = check_explicit_specialization (orig_declarator, decl,
7848 template_count,
7849 2 * funcdef_flag +
7850 4 * (friendp != 0));
7851 if (decl == error_mark_node)
7852 return NULL_TREE;
7854 if (DECL_STATIC_FUNCTION_P (decl))
7855 check_static_quals (decl, quals);
7857 if (attrlist)
7859 cplus_decl_attributes (&decl, *attrlist, 0);
7860 *attrlist = NULL_TREE;
7863 /* Check main's type after attributes have been applied. */
7864 if (ctype == NULL_TREE && DECL_MAIN_P (decl))
7866 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
7867 integer_type_node))
7869 tree oldtypeargs = TYPE_ARG_TYPES (TREE_TYPE (decl));
7870 tree newtype;
7871 error ("%<::main%> must return %<int%>");
7872 newtype = build_function_type (integer_type_node, oldtypeargs);
7873 TREE_TYPE (decl) = newtype;
7875 if (warn_main)
7876 check_main_parameter_types (decl);
7879 if (ctype != NULL_TREE
7880 && (! TYPE_FOR_JAVA (ctype) || check_java_method (decl))
7881 && check)
7883 tree old_decl = check_classfn (ctype, decl,
7884 (processing_template_decl
7885 > template_class_depth (ctype))
7886 ? current_template_parms
7887 : NULL_TREE);
7889 if (old_decl == error_mark_node)
7890 return NULL_TREE;
7892 if (old_decl)
7894 tree ok;
7895 tree pushed_scope;
7897 if (TREE_CODE (old_decl) == TEMPLATE_DECL)
7898 /* Because grokfndecl is always supposed to return a
7899 FUNCTION_DECL, we pull out the DECL_TEMPLATE_RESULT
7900 here. We depend on our callers to figure out that its
7901 really a template that's being returned. */
7902 old_decl = DECL_TEMPLATE_RESULT (old_decl);
7904 if (DECL_STATIC_FUNCTION_P (old_decl)
7905 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
7907 /* Remove the `this' parm added by grokclassfn. */
7908 revert_static_member_fn (decl);
7909 check_static_quals (decl, quals);
7911 if (DECL_ARTIFICIAL (old_decl))
7913 error ("definition of implicitly-declared %qD", old_decl);
7914 return NULL_TREE;
7916 else if (DECL_DEFAULTED_FN (old_decl))
7918 error ("definition of explicitly-defaulted %q+D", decl);
7919 error ("%q+#D explicitly defaulted here", old_decl);
7920 return NULL_TREE;
7923 /* Since we've smashed OLD_DECL to its
7924 DECL_TEMPLATE_RESULT, we must do the same to DECL. */
7925 if (TREE_CODE (decl) == TEMPLATE_DECL)
7926 decl = DECL_TEMPLATE_RESULT (decl);
7928 /* Attempt to merge the declarations. This can fail, in
7929 the case of some invalid specialization declarations. */
7930 pushed_scope = push_scope (ctype);
7931 ok = duplicate_decls (decl, old_decl, friendp);
7932 if (pushed_scope)
7933 pop_scope (pushed_scope);
7934 if (!ok)
7936 error ("no %q#D member function declared in class %qT",
7937 decl, ctype);
7938 return NULL_TREE;
7940 if (ok == error_mark_node)
7941 return NULL_TREE;
7942 return old_decl;
7946 if (DECL_CONSTRUCTOR_P (decl) && !grok_ctor_properties (ctype, decl))
7947 return NULL_TREE;
7949 if (ctype == NULL_TREE || check)
7950 return decl;
7952 if (virtualp)
7953 DECL_VIRTUAL_P (decl) = 1;
7955 return decl;
7958 /* decl is a FUNCTION_DECL.
7959 specifiers are the parsed virt-specifiers.
7961 Set flags to reflect the virt-specifiers.
7963 Returns decl. */
7965 static tree
7966 set_virt_specifiers (tree decl, cp_virt_specifiers specifiers)
7968 if (decl == NULL_TREE)
7969 return decl;
7970 if (specifiers & VIRT_SPEC_OVERRIDE)
7971 DECL_OVERRIDE_P (decl) = 1;
7972 if (specifiers & VIRT_SPEC_FINAL)
7973 DECL_FINAL_P (decl) = 1;
7974 return decl;
7977 /* DECL is a VAR_DECL for a static data member. Set flags to reflect
7978 the linkage that DECL will receive in the object file. */
7980 static void
7981 set_linkage_for_static_data_member (tree decl)
7983 /* A static data member always has static storage duration and
7984 external linkage. Note that static data members are forbidden in
7985 local classes -- the only situation in which a class has
7986 non-external linkage. */
7987 TREE_PUBLIC (decl) = 1;
7988 TREE_STATIC (decl) = 1;
7989 /* For non-template classes, static data members are always put
7990 out in exactly those files where they are defined, just as
7991 with ordinary namespace-scope variables. */
7992 if (!processing_template_decl)
7993 DECL_INTERFACE_KNOWN (decl) = 1;
7996 /* Create a VAR_DECL named NAME with the indicated TYPE.
7998 If SCOPE is non-NULL, it is the class type or namespace containing
7999 the variable. If SCOPE is NULL, the variable should is created in
8000 the innermost enclosing scope. */
8002 static tree
8003 grokvardecl (tree type,
8004 tree name,
8005 const cp_decl_specifier_seq *declspecs,
8006 int initialized,
8007 int constp,
8008 tree scope)
8010 tree decl;
8011 tree explicit_scope;
8013 gcc_assert (!name || identifier_p (name));
8015 /* Compute the scope in which to place the variable, but remember
8016 whether or not that scope was explicitly specified by the user. */
8017 explicit_scope = scope;
8018 if (!scope)
8020 /* An explicit "extern" specifier indicates a namespace-scope
8021 variable. */
8022 if (declspecs->storage_class == sc_extern)
8023 scope = current_decl_namespace ();
8024 else if (!at_function_scope_p ())
8025 scope = current_scope ();
8028 if (scope
8029 && (/* If the variable is a namespace-scope variable declared in a
8030 template, we need DECL_LANG_SPECIFIC. */
8031 (TREE_CODE (scope) == NAMESPACE_DECL && processing_template_decl)
8032 /* Similarly for namespace-scope variables with language linkage
8033 other than C++. */
8034 || (TREE_CODE (scope) == NAMESPACE_DECL
8035 && current_lang_name != lang_name_cplusplus)
8036 /* Similarly for static data members. */
8037 || TYPE_P (scope)))
8038 decl = build_lang_decl (VAR_DECL, name, type);
8039 else
8040 decl = build_decl (input_location, VAR_DECL, name, type);
8042 if (explicit_scope && TREE_CODE (explicit_scope) == NAMESPACE_DECL)
8043 set_decl_namespace (decl, explicit_scope, 0);
8044 else
8045 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
8047 if (declspecs->storage_class == sc_extern)
8049 DECL_THIS_EXTERN (decl) = 1;
8050 DECL_EXTERNAL (decl) = !initialized;
8053 if (DECL_CLASS_SCOPE_P (decl))
8055 set_linkage_for_static_data_member (decl);
8056 /* This function is only called with out-of-class definitions. */
8057 DECL_EXTERNAL (decl) = 0;
8058 check_class_member_definition_namespace (decl);
8060 /* At top level, either `static' or no s.c. makes a definition
8061 (perhaps tentative), and absence of `static' makes it public. */
8062 else if (toplevel_bindings_p ())
8064 TREE_PUBLIC (decl) = (declspecs->storage_class != sc_static
8065 && (DECL_THIS_EXTERN (decl) || ! constp));
8066 TREE_STATIC (decl) = ! DECL_EXTERNAL (decl);
8068 /* Not at top level, only `static' makes a static definition. */
8069 else
8071 TREE_STATIC (decl) = declspecs->storage_class == sc_static;
8072 TREE_PUBLIC (decl) = DECL_EXTERNAL (decl);
8075 if (decl_spec_seq_has_spec_p (declspecs, ds_thread))
8077 if (DECL_EXTERNAL (decl) || TREE_STATIC (decl))
8078 set_decl_tls_model (decl, decl_default_tls_model (decl));
8079 if (declspecs->gnu_thread_keyword_p)
8080 DECL_GNU_TLS_P (decl) = true;
8083 /* If the type of the decl has no linkage, make sure that we'll
8084 notice that in mark_used. */
8085 if (cxx_dialect > cxx98
8086 && decl_linkage (decl) != lk_none
8087 && DECL_LANG_SPECIFIC (decl) == NULL
8088 && !DECL_EXTERN_C_P (decl)
8089 && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
8090 retrofit_lang_decl (decl);
8092 if (TREE_PUBLIC (decl))
8094 /* [basic.link]: A name with no linkage (notably, the name of a class
8095 or enumeration declared in a local scope) shall not be used to
8096 declare an entity with linkage.
8098 DR 757 relaxes this restriction for C++0x. */
8099 if (cxx_dialect < cxx11)
8100 no_linkage_error (decl);
8102 else
8103 DECL_INTERFACE_KNOWN (decl) = 1;
8105 return decl;
8108 /* Create and return a canonical pointer to member function type, for
8109 TYPE, which is a POINTER_TYPE to a METHOD_TYPE. */
8111 tree
8112 build_ptrmemfunc_type (tree type)
8114 tree field, fields;
8115 tree t;
8116 tree unqualified_variant = NULL_TREE;
8118 if (type == error_mark_node)
8119 return type;
8121 /* If a canonical type already exists for this type, use it. We use
8122 this method instead of type_hash_canon, because it only does a
8123 simple equality check on the list of field members. */
8125 if ((t = TYPE_GET_PTRMEMFUNC_TYPE (type)))
8126 return t;
8128 /* Make sure that we always have the unqualified pointer-to-member
8129 type first. */
8130 if (cp_type_quals (type) != TYPE_UNQUALIFIED)
8131 unqualified_variant
8132 = build_ptrmemfunc_type (TYPE_MAIN_VARIANT (type));
8134 t = make_node (RECORD_TYPE);
8136 /* Let the front end know this is a pointer to member function. */
8137 TYPE_PTRMEMFUNC_FLAG (t) = 1;
8139 field = build_decl (input_location, FIELD_DECL, pfn_identifier, type);
8140 fields = field;
8142 field = build_decl (input_location, FIELD_DECL, delta_identifier,
8143 delta_type_node);
8144 DECL_CHAIN (field) = fields;
8145 fields = field;
8147 finish_builtin_struct (t, "__ptrmemfunc_type", fields, ptr_type_node);
8149 /* Zap out the name so that the back end will give us the debugging
8150 information for this anonymous RECORD_TYPE. */
8151 TYPE_NAME (t) = NULL_TREE;
8153 /* If this is not the unqualified form of this pointer-to-member
8154 type, set the TYPE_MAIN_VARIANT for this type to be the
8155 unqualified type. Since they are actually RECORD_TYPEs that are
8156 not variants of each other, we must do this manually.
8157 As we just built a new type there is no need to do yet another copy. */
8158 if (cp_type_quals (type) != TYPE_UNQUALIFIED)
8160 int type_quals = cp_type_quals (type);
8161 TYPE_READONLY (t) = (type_quals & TYPE_QUAL_CONST) != 0;
8162 TYPE_VOLATILE (t) = (type_quals & TYPE_QUAL_VOLATILE) != 0;
8163 TYPE_RESTRICT (t) = (type_quals & TYPE_QUAL_RESTRICT) != 0;
8164 TYPE_MAIN_VARIANT (t) = unqualified_variant;
8165 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (unqualified_variant);
8166 TYPE_NEXT_VARIANT (unqualified_variant) = t;
8169 /* Cache this pointer-to-member type so that we can find it again
8170 later. */
8171 TYPE_SET_PTRMEMFUNC_TYPE (type, t);
8173 if (TYPE_STRUCTURAL_EQUALITY_P (type))
8174 SET_TYPE_STRUCTURAL_EQUALITY (t);
8175 else if (TYPE_CANONICAL (type) != type)
8176 TYPE_CANONICAL (t) = build_ptrmemfunc_type (TYPE_CANONICAL (type));
8178 return t;
8181 /* Create and return a pointer to data member type. */
8183 tree
8184 build_ptrmem_type (tree class_type, tree member_type)
8186 if (TREE_CODE (member_type) == METHOD_TYPE)
8188 cp_cv_quals quals = type_memfn_quals (member_type);
8189 cp_ref_qualifier rqual = type_memfn_rqual (member_type);
8190 member_type = build_memfn_type (member_type, class_type, quals, rqual);
8191 return build_ptrmemfunc_type (build_pointer_type (member_type));
8193 else
8195 gcc_assert (TREE_CODE (member_type) != FUNCTION_TYPE);
8196 return build_offset_type (class_type, member_type);
8200 /* DECL is a VAR_DECL defined in-class, whose TYPE is also given.
8201 Check to see that the definition is valid. Issue appropriate error
8202 messages. Return 1 if the definition is particularly bad, or 0
8203 otherwise. */
8205 static int
8206 check_static_variable_definition (tree decl, tree type)
8208 /* Can't check yet if we don't know the type. */
8209 if (dependent_type_p (type))
8210 return 0;
8211 /* If DECL is declared constexpr, we'll do the appropriate checks
8212 in check_initializer. */
8213 if (DECL_P (decl) && DECL_DECLARED_CONSTEXPR_P (decl))
8214 return 0;
8215 else if (cxx_dialect >= cxx11 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
8217 if (!COMPLETE_TYPE_P (type))
8218 error ("in-class initialization of static data member %q#D of "
8219 "incomplete type", decl);
8220 else if (literal_type_p (type))
8221 permerror (input_location,
8222 "%<constexpr%> needed for in-class initialization of "
8223 "static data member %q#D of non-integral type", decl);
8224 else
8225 error ("in-class initialization of static data member %q#D of "
8226 "non-literal type", decl);
8227 return 1;
8230 /* Motion 10 at San Diego: If a static const integral data member is
8231 initialized with an integral constant expression, the initializer
8232 may appear either in the declaration (within the class), or in
8233 the definition, but not both. If it appears in the class, the
8234 member is a member constant. The file-scope definition is always
8235 required. */
8236 if (!ARITHMETIC_TYPE_P (type) && TREE_CODE (type) != ENUMERAL_TYPE)
8238 error ("invalid in-class initialization of static data member "
8239 "of non-integral type %qT",
8240 type);
8241 return 1;
8243 else if (!CP_TYPE_CONST_P (type))
8244 error ("ISO C++ forbids in-class initialization of non-const "
8245 "static member %qD",
8246 decl);
8247 else if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type))
8248 pedwarn (input_location, OPT_Wpedantic, "ISO C++ forbids initialization of member constant "
8249 "%qD of non-integral type %qT", decl, type);
8251 return 0;
8254 /* *expr_p is part of the TYPE_SIZE of a variably-sized array. If any
8255 SAVE_EXPRs in *expr_p wrap expressions with side-effects, break those
8256 expressions out into temporary variables so that walk_tree doesn't
8257 step into them (c++/15764). */
8259 static tree
8260 stabilize_save_expr_r (tree *expr_p, int *walk_subtrees, void *data)
8262 hash_set<tree> *pset = (hash_set<tree> *)data;
8263 tree expr = *expr_p;
8264 if (TREE_CODE (expr) == SAVE_EXPR)
8266 tree op = TREE_OPERAND (expr, 0);
8267 cp_walk_tree (&op, stabilize_save_expr_r, data, pset);
8268 if (TREE_SIDE_EFFECTS (op))
8269 TREE_OPERAND (expr, 0) = get_temp_regvar (TREE_TYPE (op), op);
8270 *walk_subtrees = 0;
8272 else if (!EXPR_P (expr) || !TREE_SIDE_EFFECTS (expr))
8273 *walk_subtrees = 0;
8274 return NULL;
8277 /* Entry point for the above. */
8279 static void
8280 stabilize_vla_size (tree size)
8282 hash_set<tree> pset;
8283 /* Break out any function calls into temporary variables. */
8284 cp_walk_tree (&size, stabilize_save_expr_r, &pset, &pset);
8287 /* Helper function for compute_array_index_type. Look for SIZEOF_EXPR
8288 not inside of SAVE_EXPR and fold them. */
8290 static tree
8291 fold_sizeof_expr_r (tree *expr_p, int *walk_subtrees, void *data)
8293 tree expr = *expr_p;
8294 if (TREE_CODE (expr) == SAVE_EXPR || TYPE_P (expr))
8295 *walk_subtrees = 0;
8296 else if (TREE_CODE (expr) == SIZEOF_EXPR)
8298 *(bool *)data = true;
8299 if (SIZEOF_EXPR_TYPE_P (expr))
8300 expr = cxx_sizeof_or_alignof_type (TREE_TYPE (TREE_OPERAND (expr, 0)),
8301 SIZEOF_EXPR, false);
8302 else if (TYPE_P (TREE_OPERAND (expr, 0)))
8303 expr = cxx_sizeof_or_alignof_type (TREE_OPERAND (expr, 0), SIZEOF_EXPR,
8304 false);
8305 else
8306 expr = cxx_sizeof_or_alignof_expr (TREE_OPERAND (expr, 0), SIZEOF_EXPR,
8307 false);
8308 if (expr == error_mark_node)
8309 expr = size_one_node;
8310 *expr_p = expr;
8311 *walk_subtrees = 0;
8313 return NULL;
8316 /* Given the SIZE (i.e., number of elements) in an array, compute an
8317 appropriate index type for the array. If non-NULL, NAME is the
8318 name of the thing being declared. */
8320 tree
8321 compute_array_index_type (tree name, tree size, tsubst_flags_t complain)
8323 tree itype;
8324 tree osize = size;
8326 if (error_operand_p (size))
8327 return error_mark_node;
8329 if (!type_dependent_expression_p (size))
8331 tree type = TREE_TYPE (size);
8333 mark_rvalue_use (size);
8335 if (cxx_dialect < cxx11 && TREE_CODE (size) == NOP_EXPR
8336 && TREE_SIDE_EFFECTS (size))
8337 /* In C++98, we mark a non-constant array bound with a magic
8338 NOP_EXPR with TREE_SIDE_EFFECTS; don't fold in that case. */;
8339 else
8341 size = fold_non_dependent_expr_sfinae (size, complain);
8343 if (CLASS_TYPE_P (type)
8344 && CLASSTYPE_LITERAL_P (type))
8346 size = build_expr_type_conversion (WANT_INT, size, true);
8347 if (!size)
8349 if (!(complain & tf_error))
8350 return error_mark_node;
8351 if (name)
8352 error ("size of array %qD has non-integral type %qT",
8353 name, type);
8354 else
8355 error ("size of array has non-integral type %qT", type);
8356 size = integer_one_node;
8358 if (size == error_mark_node)
8359 return error_mark_node;
8360 type = TREE_TYPE (size);
8363 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
8364 size = maybe_constant_value (size);
8366 if (!TREE_CONSTANT (size))
8367 size = osize;
8370 if (error_operand_p (size))
8371 return error_mark_node;
8373 /* The array bound must be an integer type. */
8374 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
8376 if (!(complain & tf_error))
8377 return error_mark_node;
8378 if (name)
8379 error ("size of array %qD has non-integral type %qT", name, type);
8380 else
8381 error ("size of array has non-integral type %qT", type);
8382 size = integer_one_node;
8383 type = TREE_TYPE (size);
8387 /* A type is dependent if it is...an array type constructed from any
8388 dependent type or whose size is specified by a constant expression
8389 that is value-dependent. */
8390 /* We can only call value_dependent_expression_p on integral constant
8391 expressions; treat non-constant expressions as dependent, too. */
8392 if (processing_template_decl
8393 && (type_dependent_expression_p (size)
8394 || !TREE_CONSTANT (size) || value_dependent_expression_p (size)))
8396 /* We cannot do any checking for a SIZE that isn't known to be
8397 constant. Just build the index type and mark that it requires
8398 structural equality checks. */
8399 itype = build_index_type (build_min (MINUS_EXPR, sizetype,
8400 size, size_one_node));
8401 TYPE_DEPENDENT_P (itype) = 1;
8402 TYPE_DEPENDENT_P_VALID (itype) = 1;
8403 SET_TYPE_STRUCTURAL_EQUALITY (itype);
8404 return itype;
8407 /* Normally, the array-bound will be a constant. */
8408 if (TREE_CODE (size) == INTEGER_CST)
8410 /* Check to see if the array bound overflowed. Make that an
8411 error, no matter how generous we're being. */
8412 constant_expression_error (size);
8414 /* An array must have a positive number of elements. */
8415 if (tree_int_cst_lt (size, integer_zero_node))
8417 if (!(complain & tf_error))
8418 return error_mark_node;
8419 if (name)
8420 error ("size of array %qD is negative", name);
8421 else
8422 error ("size of array is negative");
8423 size = integer_one_node;
8425 /* As an extension we allow zero-sized arrays. */
8426 else if (integer_zerop (size))
8428 if (!(complain & tf_error))
8429 /* We must fail if performing argument deduction (as
8430 indicated by the state of complain), so that
8431 another substitution can be found. */
8432 return error_mark_node;
8433 else if (in_system_header_at (input_location))
8434 /* Allow them in system headers because glibc uses them. */;
8435 else if (name)
8436 pedwarn (input_location, OPT_Wpedantic, "ISO C++ forbids zero-size array %qD", name);
8437 else
8438 pedwarn (input_location, OPT_Wpedantic, "ISO C++ forbids zero-size array");
8441 else if (TREE_CONSTANT (size)
8442 /* We don't allow VLAs at non-function scopes, or during
8443 tentative template substitution. */
8444 || !at_function_scope_p ()
8445 || (cxx_dialect < cxx1y && !(complain & tf_error)))
8447 if (!(complain & tf_error))
8448 return error_mark_node;
8449 /* `(int) &fn' is not a valid array bound. */
8450 if (name)
8451 error ("size of array %qD is not an integral constant-expression",
8452 name);
8453 else
8454 error ("size of array is not an integral constant-expression");
8455 size = integer_one_node;
8457 else if (cxx_dialect < cxx1y && pedantic && warn_vla != 0)
8459 if (name)
8460 pedwarn (input_location, OPT_Wvla, "ISO C++ forbids variable length array %qD", name);
8461 else
8462 pedwarn (input_location, OPT_Wvla, "ISO C++ forbids variable length array");
8464 else if (warn_vla > 0)
8466 if (name)
8467 warning (OPT_Wvla,
8468 "variable length array %qD is used", name);
8469 else
8470 warning (OPT_Wvla,
8471 "variable length array is used");
8474 if (processing_template_decl && !TREE_CONSTANT (size))
8475 /* A variable sized array. */
8476 itype = build_min (MINUS_EXPR, sizetype, size, integer_one_node);
8477 else
8479 HOST_WIDE_INT saved_processing_template_decl;
8481 /* Compute the index of the largest element in the array. It is
8482 one less than the number of elements in the array. We save
8483 and restore PROCESSING_TEMPLATE_DECL so that computations in
8484 cp_build_binary_op will be appropriately folded. */
8485 saved_processing_template_decl = processing_template_decl;
8486 processing_template_decl = 0;
8487 itype = cp_build_binary_op (input_location,
8488 MINUS_EXPR,
8489 cp_convert (ssizetype, size, complain),
8490 cp_convert (ssizetype, integer_one_node,
8491 complain),
8492 complain);
8493 itype = fold (itype);
8494 processing_template_decl = saved_processing_template_decl;
8496 if (!TREE_CONSTANT (itype))
8498 /* A variable sized array. */
8499 itype = variable_size (itype);
8501 if (TREE_CODE (itype) != SAVE_EXPR)
8503 /* Look for SIZEOF_EXPRs in itype and fold them, otherwise
8504 they might survive till gimplification. */
8505 tree newitype = itype;
8506 bool found = false;
8507 cp_walk_tree_without_duplicates (&newitype,
8508 fold_sizeof_expr_r, &found);
8509 if (found)
8510 itype = variable_size (fold (newitype));
8513 stabilize_vla_size (itype);
8515 if (cxx_dialect >= cxx1y && flag_exceptions)
8517 /* If the VLA bound is larger than half the address space,
8518 or less than zero, throw std::bad_array_length. */
8519 tree comp = build2 (LT_EXPR, boolean_type_node, itype,
8520 ssize_int (-1));
8521 comp = build3 (COND_EXPR, void_type_node, comp,
8522 throw_bad_array_length (), void_node);
8523 finish_expr_stmt (comp);
8525 else if (flag_sanitize & SANITIZE_VLA
8526 && current_function_decl != NULL_TREE
8527 && !lookup_attribute ("no_sanitize_undefined",
8528 DECL_ATTRIBUTES
8529 (current_function_decl)))
8531 /* From C++1y onwards, we throw an exception on a negative
8532 length size of an array; see above. */
8534 /* We have to add 1 -- in the ubsan routine we generate
8535 LE_EXPR rather than LT_EXPR. */
8536 tree t = fold_build2 (PLUS_EXPR, TREE_TYPE (itype), itype,
8537 build_one_cst (TREE_TYPE (itype)));
8538 t = ubsan_instrument_vla (input_location, t);
8539 finish_expr_stmt (t);
8542 /* Make sure that there was no overflow when creating to a signed
8543 index type. (For example, on a 32-bit machine, an array with
8544 size 2^32 - 1 is too big.) */
8545 else if (TREE_CODE (itype) == INTEGER_CST
8546 && TREE_OVERFLOW (itype))
8548 if (!(complain & tf_error))
8549 return error_mark_node;
8550 error ("overflow in array dimension");
8551 TREE_OVERFLOW (itype) = 0;
8555 /* Create and return the appropriate index type. */
8556 itype = build_index_type (itype);
8558 /* If the index type were dependent, we would have returned early, so
8559 remember that it isn't. */
8560 TYPE_DEPENDENT_P (itype) = 0;
8561 TYPE_DEPENDENT_P_VALID (itype) = 1;
8562 return itype;
8565 /* Returns the scope (if any) in which the entity declared by
8566 DECLARATOR will be located. If the entity was declared with an
8567 unqualified name, NULL_TREE is returned. */
8569 tree
8570 get_scope_of_declarator (const cp_declarator *declarator)
8572 while (declarator && declarator->kind != cdk_id)
8573 declarator = declarator->declarator;
8575 /* If the declarator-id is a SCOPE_REF, the scope in which the
8576 declaration occurs is the first operand. */
8577 if (declarator
8578 && declarator->u.id.qualifying_scope)
8579 return declarator->u.id.qualifying_scope;
8581 /* Otherwise, the declarator is not a qualified name; the entity will
8582 be declared in the current scope. */
8583 return NULL_TREE;
8586 /* Returns an ARRAY_TYPE for an array with SIZE elements of the
8587 indicated TYPE. If non-NULL, NAME is the NAME of the declaration
8588 with this type. */
8590 static tree
8591 create_array_type_for_decl (tree name, tree type, tree size)
8593 tree itype = NULL_TREE;
8595 /* If things have already gone awry, bail now. */
8596 if (type == error_mark_node || size == error_mark_node)
8597 return error_mark_node;
8599 /* 8.3.4/1: If the type of the identifier of D contains the auto
8600 type-specifier, the program is ill-formed. */
8601 if (type_uses_auto (type))
8603 error ("%qD declared as array of %qT", name, type);
8604 return error_mark_node;
8607 /* If there are some types which cannot be array elements,
8608 issue an error-message and return. */
8609 switch (TREE_CODE (type))
8611 case VOID_TYPE:
8612 if (name)
8613 error ("declaration of %qD as array of void", name);
8614 else
8615 error ("creating array of void");
8616 return error_mark_node;
8618 case FUNCTION_TYPE:
8619 if (name)
8620 error ("declaration of %qD as array of functions", name);
8621 else
8622 error ("creating array of functions");
8623 return error_mark_node;
8625 case REFERENCE_TYPE:
8626 if (name)
8627 error ("declaration of %qD as array of references", name);
8628 else
8629 error ("creating array of references");
8630 return error_mark_node;
8632 case METHOD_TYPE:
8633 if (name)
8634 error ("declaration of %qD as array of function members", name);
8635 else
8636 error ("creating array of function members");
8637 return error_mark_node;
8639 default:
8640 break;
8643 /* [dcl.array]
8645 The constant expressions that specify the bounds of the arrays
8646 can be omitted only for the first member of the sequence. */
8647 if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type))
8649 if (name)
8650 error ("declaration of %qD as multidimensional array must "
8651 "have bounds for all dimensions except the first",
8652 name);
8653 else
8654 error ("multidimensional array must have bounds for all "
8655 "dimensions except the first");
8657 return error_mark_node;
8660 if (cxx_dialect >= cxx1y && array_of_runtime_bound_p (type)
8661 && (flag_iso || warn_vla > 0))
8662 pedwarn (input_location, OPT_Wvla, "array of array of runtime bound");
8664 /* Figure out the index type for the array. */
8665 if (size)
8666 itype = compute_array_index_type (name, size, tf_warning_or_error);
8668 /* [dcl.array]
8669 T is called the array element type; this type shall not be [...] an
8670 abstract class type. */
8671 abstract_virtuals_error (name, type);
8673 return build_cplus_array_type (type, itype);
8676 /* Check that it's OK to declare a function with the indicated TYPE.
8677 SFK indicates the kind of special function (if any) that this
8678 function is. OPTYPE is the type given in a conversion operator
8679 declaration, or the class type for a constructor/destructor.
8680 Returns the actual return type of the function; that
8681 may be different than TYPE if an error occurs, or for certain
8682 special functions. */
8684 static tree
8685 check_special_function_return_type (special_function_kind sfk,
8686 tree type,
8687 tree optype)
8689 switch (sfk)
8691 case sfk_constructor:
8692 if (type)
8693 error ("return type specification for constructor invalid");
8695 if (targetm.cxx.cdtor_returns_this () && !TYPE_FOR_JAVA (optype))
8696 type = build_pointer_type (optype);
8697 else
8698 type = void_type_node;
8699 break;
8701 case sfk_destructor:
8702 if (type)
8703 error ("return type specification for destructor invalid");
8704 /* We can't use the proper return type here because we run into
8705 problems with ambiguous bases and covariant returns.
8706 Java classes are left unchanged because (void *) isn't a valid
8707 Java type, and we don't want to change the Java ABI. */
8708 if (targetm.cxx.cdtor_returns_this () && !TYPE_FOR_JAVA (optype))
8709 type = build_pointer_type (void_type_node);
8710 else
8711 type = void_type_node;
8712 break;
8714 case sfk_conversion:
8715 if (type)
8716 error ("return type specified for %<operator %T%>", optype);
8717 type = optype;
8718 break;
8720 default:
8721 gcc_unreachable ();
8724 return type;
8727 /* A variable or data member (whose unqualified name is IDENTIFIER)
8728 has been declared with the indicated TYPE. If the TYPE is not
8729 acceptable, issue an error message and return a type to use for
8730 error-recovery purposes. */
8732 tree
8733 check_var_type (tree identifier, tree type)
8735 if (VOID_TYPE_P (type))
8737 if (!identifier)
8738 error ("unnamed variable or field declared void");
8739 else if (identifier_p (identifier))
8741 gcc_assert (!IDENTIFIER_OPNAME_P (identifier));
8742 error ("variable or field %qE declared void", identifier);
8744 else
8745 error ("variable or field declared void");
8746 type = error_mark_node;
8749 return type;
8752 /* Given declspecs and a declarator (abstract or otherwise), determine
8753 the name and type of the object declared and construct a DECL node
8754 for it.
8756 DECLSPECS points to the representation of declaration-specifier
8757 sequence that precedes declarator.
8759 DECL_CONTEXT says which syntactic context this declaration is in:
8760 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
8761 FUNCDEF for a function definition. Like NORMAL but a few different
8762 error messages in each case. Return value may be zero meaning
8763 this definition is too screwy to try to parse.
8764 MEMFUNCDEF for a function definition. Like FUNCDEF but prepares to
8765 handle member functions (which have FIELD context).
8766 Return value may be zero meaning this definition is too screwy to
8767 try to parse.
8768 PARM for a parameter declaration (either within a function prototype
8769 or before a function body). Make a PARM_DECL, or return void_type_node.
8770 TPARM for a template parameter declaration.
8771 CATCHPARM for a parameter declaration before a catch clause.
8772 TYPENAME if for a typename (in a cast or sizeof).
8773 Don't make a DECL node; just return the ..._TYPE node.
8774 FIELD for a struct or union field; make a FIELD_DECL.
8775 BITFIELD for a field with specified width.
8777 INITIALIZED is as for start_decl.
8779 ATTRLIST is a pointer to the list of attributes, which may be NULL
8780 if there are none; *ATTRLIST may be modified if attributes from inside
8781 the declarator should be applied to the declaration.
8783 When this function is called, scoping variables (such as
8784 CURRENT_CLASS_TYPE) should reflect the scope in which the
8785 declaration occurs, not the scope in which the new declaration will
8786 be placed. For example, on:
8788 void S::f() { ... }
8790 when grokdeclarator is called for `S::f', the CURRENT_CLASS_TYPE
8791 should not be `S'.
8793 Returns a DECL (if a declarator is present), a TYPE (if there is no
8794 declarator, in cases like "struct S;"), or the ERROR_MARK_NODE if an
8795 error occurs. */
8797 tree
8798 grokdeclarator (const cp_declarator *declarator,
8799 cp_decl_specifier_seq *declspecs,
8800 enum decl_context decl_context,
8801 int initialized,
8802 tree* attrlist)
8804 tree type = NULL_TREE;
8805 int longlong = 0;
8806 int explicit_int128 = 0;
8807 int virtualp, explicitp, friendp, inlinep, staticp;
8808 int explicit_int = 0;
8809 int explicit_char = 0;
8810 int defaulted_int = 0;
8812 tree typedef_decl = NULL_TREE;
8813 const char *name = NULL;
8814 tree typedef_type = NULL_TREE;
8815 /* True if this declarator is a function definition. */
8816 bool funcdef_flag = false;
8817 cp_declarator_kind innermost_code = cdk_error;
8818 int bitfield = 0;
8819 #if 0
8820 /* See the code below that used this. */
8821 tree decl_attr = NULL_TREE;
8822 #endif
8824 /* Keep track of what sort of function is being processed
8825 so that we can warn about default return values, or explicit
8826 return values which do not match prescribed defaults. */
8827 special_function_kind sfk = sfk_none;
8829 tree dname = NULL_TREE;
8830 tree ctor_return_type = NULL_TREE;
8831 enum overload_flags flags = NO_SPECIAL;
8832 /* cv-qualifiers that apply to the declarator, for a declaration of
8833 a member function. */
8834 cp_cv_quals memfn_quals = TYPE_UNQUALIFIED;
8835 /* virt-specifiers that apply to the declarator, for a declaration of
8836 a member function. */
8837 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
8838 /* ref-qualifier that applies to the declarator, for a declaration of
8839 a member function. */
8840 cp_ref_qualifier rqual = REF_QUAL_NONE;
8841 /* cv-qualifiers that apply to the type specified by the DECLSPECS. */
8842 int type_quals;
8843 tree raises = NULL_TREE;
8844 int template_count = 0;
8845 tree returned_attrs = NULL_TREE;
8846 tree parms = NULL_TREE;
8847 const cp_declarator *id_declarator;
8848 /* The unqualified name of the declarator; either an
8849 IDENTIFIER_NODE, BIT_NOT_EXPR, or TEMPLATE_ID_EXPR. */
8850 tree unqualified_id;
8851 /* The class type, if any, in which this entity is located,
8852 or NULL_TREE if none. Note that this value may be different from
8853 the current class type; for example if an attempt is made to declare
8854 "A::f" inside "B", this value will be "A". */
8855 tree ctype = current_class_type;
8856 /* The NAMESPACE_DECL for the namespace in which this entity is
8857 located. If an unqualified name is used to declare the entity,
8858 this value will be NULL_TREE, even if the entity is located at
8859 namespace scope. */
8860 tree in_namespace = NULL_TREE;
8861 cp_storage_class storage_class;
8862 bool unsigned_p, signed_p, short_p, long_p, thread_p;
8863 bool type_was_error_mark_node = false;
8864 bool parameter_pack_p = declarator? declarator->parameter_pack_p : false;
8865 bool template_type_arg = false;
8866 bool template_parm_flag = false;
8867 bool typedef_p = decl_spec_seq_has_spec_p (declspecs, ds_typedef);
8868 bool constexpr_p = decl_spec_seq_has_spec_p (declspecs, ds_constexpr);
8869 bool late_return_type_p = false;
8870 bool array_parameter_p = false;
8871 source_location saved_loc = input_location;
8872 const char *errmsg;
8874 signed_p = decl_spec_seq_has_spec_p (declspecs, ds_signed);
8875 unsigned_p = decl_spec_seq_has_spec_p (declspecs, ds_unsigned);
8876 short_p = decl_spec_seq_has_spec_p (declspecs, ds_short);
8877 long_p = decl_spec_seq_has_spec_p (declspecs, ds_long);
8878 longlong = decl_spec_seq_has_spec_p (declspecs, ds_long_long);
8879 explicit_int128 = declspecs->explicit_int128_p;
8880 thread_p = decl_spec_seq_has_spec_p (declspecs, ds_thread);
8882 if (decl_context == FUNCDEF)
8883 funcdef_flag = true, decl_context = NORMAL;
8884 else if (decl_context == MEMFUNCDEF)
8885 funcdef_flag = true, decl_context = FIELD;
8886 else if (decl_context == BITFIELD)
8887 bitfield = 1, decl_context = FIELD;
8888 else if (decl_context == TEMPLATE_TYPE_ARG)
8889 template_type_arg = true, decl_context = TYPENAME;
8890 else if (decl_context == TPARM)
8891 template_parm_flag = true, decl_context = PARM;
8893 if (initialized > 1)
8894 funcdef_flag = true;
8896 /* Look inside a declarator for the name being declared
8897 and get it as a string, for an error message. */
8898 for (id_declarator = declarator;
8899 id_declarator;
8900 id_declarator = id_declarator->declarator)
8902 if (id_declarator->kind != cdk_id)
8903 innermost_code = id_declarator->kind;
8905 switch (id_declarator->kind)
8907 case cdk_function:
8908 if (id_declarator->declarator
8909 && id_declarator->declarator->kind == cdk_id)
8911 sfk = id_declarator->declarator->u.id.sfk;
8912 if (sfk == sfk_destructor)
8913 flags = DTOR_FLAG;
8915 break;
8917 case cdk_id:
8919 tree qualifying_scope = id_declarator->u.id.qualifying_scope;
8920 tree decl = id_declarator->u.id.unqualified_name;
8921 if (!decl)
8922 break;
8923 if (qualifying_scope)
8925 if (at_function_scope_p ())
8927 /* [dcl.meaning]
8929 A declarator-id shall not be qualified except
8930 for ...
8932 None of the cases are permitted in block
8933 scope. */
8934 if (qualifying_scope == global_namespace)
8935 error ("invalid use of qualified-name %<::%D%>",
8936 decl);
8937 else if (TYPE_P (qualifying_scope))
8938 error ("invalid use of qualified-name %<%T::%D%>",
8939 qualifying_scope, decl);
8940 else
8941 error ("invalid use of qualified-name %<%D::%D%>",
8942 qualifying_scope, decl);
8943 return error_mark_node;
8945 else if (TYPE_P (qualifying_scope))
8947 ctype = qualifying_scope;
8948 if (!MAYBE_CLASS_TYPE_P (ctype))
8950 error ("%q#T is not a class or a namespace", ctype);
8951 ctype = NULL_TREE;
8953 else if (innermost_code != cdk_function
8954 && current_class_type
8955 && !uniquely_derived_from_p (ctype,
8956 current_class_type))
8958 error ("invalid use of qualified-name %<%T::%D%>",
8959 qualifying_scope, decl);
8960 return error_mark_node;
8963 else if (TREE_CODE (qualifying_scope) == NAMESPACE_DECL)
8964 in_namespace = qualifying_scope;
8966 switch (TREE_CODE (decl))
8968 case BIT_NOT_EXPR:
8970 tree type;
8972 if (innermost_code != cdk_function)
8974 error ("declaration of %qD as non-function", decl);
8975 return error_mark_node;
8977 else if (!qualifying_scope
8978 && !(current_class_type && at_class_scope_p ()))
8980 error ("declaration of %qD as non-member", decl);
8981 return error_mark_node;
8984 type = TREE_OPERAND (decl, 0);
8985 if (TYPE_P (type))
8986 type = constructor_name (type);
8987 name = identifier_to_locale (IDENTIFIER_POINTER (type));
8988 dname = decl;
8990 break;
8992 case TEMPLATE_ID_EXPR:
8994 tree fns = TREE_OPERAND (decl, 0);
8996 dname = fns;
8997 if (!identifier_p (dname))
8999 gcc_assert (is_overloaded_fn (dname));
9000 dname = DECL_NAME (get_first_fn (dname));
9003 /* Fall through. */
9005 case IDENTIFIER_NODE:
9006 if (identifier_p (decl))
9007 dname = decl;
9009 if (C_IS_RESERVED_WORD (dname))
9011 error ("declarator-id missing; using reserved word %qD",
9012 dname);
9013 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
9015 else if (!IDENTIFIER_TYPENAME_P (dname))
9016 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
9017 else
9019 gcc_assert (flags == NO_SPECIAL);
9020 flags = TYPENAME_FLAG;
9021 ctor_return_type = TREE_TYPE (dname);
9022 sfk = sfk_conversion;
9023 if (is_typename_at_global_scope (dname))
9024 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
9025 else
9026 name = "<invalid operator>";
9028 break;
9030 default:
9031 gcc_unreachable ();
9033 break;
9036 case cdk_array:
9037 case cdk_pointer:
9038 case cdk_reference:
9039 case cdk_ptrmem:
9040 break;
9042 case cdk_error:
9043 return error_mark_node;
9045 default:
9046 gcc_unreachable ();
9048 if (id_declarator->kind == cdk_id)
9049 break;
9052 /* [dcl.fct.edf]
9054 The declarator in a function-definition shall have the form
9055 D1 ( parameter-declaration-clause) ... */
9056 if (funcdef_flag && innermost_code != cdk_function)
9058 error ("function definition does not declare parameters");
9059 return error_mark_node;
9062 if (flags == TYPENAME_FLAG
9063 && innermost_code != cdk_function
9064 && ! (ctype && !declspecs->any_specifiers_p))
9066 error ("declaration of %qD as non-function", dname);
9067 return error_mark_node;
9070 if (dname
9071 && identifier_p (dname)
9072 && UDLIT_OPER_P (dname)
9073 && innermost_code != cdk_function)
9075 error ("declaration of %qD as non-function", dname);
9076 return error_mark_node;
9079 if (dname && IDENTIFIER_OPNAME_P (dname))
9081 if (typedef_p)
9083 error ("declaration of %qD as %<typedef%>", dname);
9084 return error_mark_node;
9086 else if (decl_context == PARM || decl_context == CATCHPARM)
9088 error ("declaration of %qD as parameter", dname);
9089 return error_mark_node;
9093 /* Anything declared one level down from the top level
9094 must be one of the parameters of a function
9095 (because the body is at least two levels down). */
9097 /* This heuristic cannot be applied to C++ nodes! Fixed, however,
9098 by not allowing C++ class definitions to specify their parameters
9099 with xdecls (must be spec.d in the parmlist).
9101 Since we now wait to push a class scope until we are sure that
9102 we are in a legitimate method context, we must set oldcname
9103 explicitly (since current_class_name is not yet alive).
9105 We also want to avoid calling this a PARM if it is in a namespace. */
9107 if (decl_context == NORMAL && !toplevel_bindings_p ())
9109 cp_binding_level *b = current_binding_level;
9110 current_binding_level = b->level_chain;
9111 if (current_binding_level != 0 && toplevel_bindings_p ())
9112 decl_context = PARM;
9113 current_binding_level = b;
9116 if (name == NULL)
9117 name = decl_context == PARM ? "parameter" : "type name";
9119 if (constexpr_p && typedef_p)
9121 error ("%<constexpr%> cannot appear in a typedef declaration");
9122 return error_mark_node;
9125 /* If there were multiple types specified in the decl-specifier-seq,
9126 issue an error message. */
9127 if (declspecs->multiple_types_p)
9129 error ("two or more data types in declaration of %qs", name);
9130 return error_mark_node;
9133 if (declspecs->conflicting_specifiers_p)
9135 error ("conflicting specifiers in declaration of %qs", name);
9136 return error_mark_node;
9139 /* Extract the basic type from the decl-specifier-seq. */
9140 type = declspecs->type;
9141 if (type == error_mark_node)
9143 type = NULL_TREE;
9144 type_was_error_mark_node = true;
9146 /* If the entire declaration is itself tagged as deprecated then
9147 suppress reports of deprecated items. */
9148 if (type && TREE_DEPRECATED (type)
9149 && deprecated_state != DEPRECATED_SUPPRESS)
9150 warn_deprecated_use (type, NULL_TREE);
9151 if (type && TREE_CODE (type) == TYPE_DECL)
9153 typedef_decl = type;
9154 type = TREE_TYPE (typedef_decl);
9155 if (TREE_DEPRECATED (type)
9156 && DECL_ARTIFICIAL (typedef_decl)
9157 && deprecated_state != DEPRECATED_SUPPRESS)
9158 warn_deprecated_use (type, NULL_TREE);
9160 /* No type at all: default to `int', and set DEFAULTED_INT
9161 because it was not a user-defined typedef. */
9162 if (type == NULL_TREE && (signed_p || unsigned_p || long_p || short_p))
9164 /* These imply 'int'. */
9165 type = integer_type_node;
9166 defaulted_int = 1;
9168 /* Gather flags. */
9169 explicit_int = declspecs->explicit_int_p;
9170 explicit_char = declspecs->explicit_char_p;
9172 #if 0
9173 /* See the code below that used this. */
9174 if (typedef_decl)
9175 decl_attr = DECL_ATTRIBUTES (typedef_decl);
9176 #endif
9177 typedef_type = type;
9180 if (sfk != sfk_conversion)
9181 ctor_return_type = ctype;
9183 if (sfk != sfk_none)
9184 type = check_special_function_return_type (sfk, type,
9185 ctor_return_type);
9186 else if (type == NULL_TREE)
9188 int is_main;
9190 explicit_int = -1;
9192 /* We handle `main' specially here, because 'main () { }' is so
9193 common. With no options, it is allowed. With -Wreturn-type,
9194 it is a warning. It is only an error with -pedantic-errors. */
9195 is_main = (funcdef_flag
9196 && dname && identifier_p (dname)
9197 && MAIN_NAME_P (dname)
9198 && ctype == NULL_TREE
9199 && in_namespace == NULL_TREE
9200 && current_namespace == global_namespace);
9202 if (type_was_error_mark_node)
9203 /* We've already issued an error, don't complain more. */;
9204 else if (in_system_header_at (input_location) || flag_ms_extensions)
9205 /* Allow it, sigh. */;
9206 else if (! is_main)
9207 permerror (input_location, "ISO C++ forbids declaration of %qs with no type", name);
9208 else if (pedantic)
9209 pedwarn (input_location, OPT_Wpedantic,
9210 "ISO C++ forbids declaration of %qs with no type", name);
9211 else
9212 warning (OPT_Wreturn_type,
9213 "ISO C++ forbids declaration of %qs with no type", name);
9215 type = integer_type_node;
9218 ctype = NULL_TREE;
9220 if (explicit_int128)
9222 if (int128_integer_type_node == NULL_TREE)
9224 error ("%<__int128%> is not supported by this target");
9225 explicit_int128 = false;
9227 else if (pedantic && ! in_system_header_at (input_location))
9228 pedwarn (input_location, OPT_Wpedantic,
9229 "ISO C++ does not support %<__int128%> for %qs", name);
9232 /* Now process the modifiers that were specified
9233 and check for invalid combinations. */
9235 /* Long double is a special combination. */
9236 if (long_p && !longlong && TYPE_MAIN_VARIANT (type) == double_type_node)
9238 long_p = false;
9239 type = cp_build_qualified_type (long_double_type_node,
9240 cp_type_quals (type));
9243 /* Check all other uses of type modifiers. */
9245 if (unsigned_p || signed_p || long_p || short_p)
9247 int ok = 0;
9249 if ((signed_p || unsigned_p) && TREE_CODE (type) != INTEGER_TYPE)
9250 error ("%<signed%> or %<unsigned%> invalid for %qs", name);
9251 else if (signed_p && unsigned_p)
9252 error ("%<signed%> and %<unsigned%> specified together for %qs", name);
9253 else if (longlong && TREE_CODE (type) != INTEGER_TYPE)
9254 error ("%<long long%> invalid for %qs", name);
9255 else if (long_p && TREE_CODE (type) == REAL_TYPE)
9256 error ("%<long%> invalid for %qs", name);
9257 else if (short_p && TREE_CODE (type) == REAL_TYPE)
9258 error ("%<short%> invalid for %qs", name);
9259 else if ((long_p || short_p) && TREE_CODE (type) != INTEGER_TYPE)
9260 error ("%<long%> or %<short%> invalid for %qs", name);
9261 else if ((long_p || short_p || explicit_char || explicit_int) && explicit_int128)
9262 error ("%<long%>, %<int%>, %<short%>, or %<char%> invalid for %qs", name);
9263 else if ((long_p || short_p) && explicit_char)
9264 error ("%<long%> or %<short%> specified with char for %qs", name);
9265 else if (long_p && short_p)
9266 error ("%<long%> and %<short%> specified together for %qs", name);
9267 else if (type == char16_type_node || type == char32_type_node)
9269 if (signed_p || unsigned_p)
9270 error ("%<signed%> or %<unsigned%> invalid for %qs", name);
9271 else if (short_p || long_p)
9272 error ("%<short%> or %<long%> invalid for %qs", name);
9274 else
9276 ok = 1;
9277 if (!explicit_int && !defaulted_int && !explicit_char && !explicit_int128 && pedantic)
9279 pedwarn (input_location, OPT_Wpedantic,
9280 "long, short, signed or unsigned used invalidly for %qs",
9281 name);
9282 if (flag_pedantic_errors)
9283 ok = 0;
9287 /* Discard the type modifiers if they are invalid. */
9288 if (! ok)
9290 unsigned_p = false;
9291 signed_p = false;
9292 long_p = false;
9293 short_p = false;
9294 longlong = 0;
9298 /* Decide whether an integer type is signed or not.
9299 Optionally treat bitfields as signed by default. */
9300 if (unsigned_p
9301 /* [class.bit]
9303 It is implementation-defined whether a plain (neither
9304 explicitly signed or unsigned) char, short, int, or long
9305 bit-field is signed or unsigned.
9307 Naturally, we extend this to long long as well. Note that
9308 this does not include wchar_t. */
9309 || (bitfield && !flag_signed_bitfields
9310 && !signed_p
9311 /* A typedef for plain `int' without `signed' can be
9312 controlled just like plain `int', but a typedef for
9313 `signed int' cannot be so controlled. */
9314 && !(typedef_decl
9315 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl))
9316 && TREE_CODE (type) == INTEGER_TYPE
9317 && !same_type_p (TYPE_MAIN_VARIANT (type), wchar_type_node)))
9319 if (explicit_int128)
9320 type = int128_unsigned_type_node;
9321 else if (longlong)
9322 type = long_long_unsigned_type_node;
9323 else if (long_p)
9324 type = long_unsigned_type_node;
9325 else if (short_p)
9326 type = short_unsigned_type_node;
9327 else if (type == char_type_node)
9328 type = unsigned_char_type_node;
9329 else if (typedef_decl)
9330 type = unsigned_type_for (type);
9331 else
9332 type = unsigned_type_node;
9334 else if (signed_p && type == char_type_node)
9335 type = signed_char_type_node;
9336 else if (explicit_int128)
9337 type = int128_integer_type_node;
9338 else if (longlong)
9339 type = long_long_integer_type_node;
9340 else if (long_p)
9341 type = long_integer_type_node;
9342 else if (short_p)
9343 type = short_integer_type_node;
9345 if (decl_spec_seq_has_spec_p (declspecs, ds_complex))
9347 if (TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
9348 error ("complex invalid for %qs", name);
9349 /* If we just have "complex", it is equivalent to
9350 "complex double", but if any modifiers at all are specified it is
9351 the complex form of TYPE. E.g, "complex short" is
9352 "complex short int". */
9353 else if (defaulted_int && ! longlong && ! explicit_int128
9354 && ! (long_p || short_p || signed_p || unsigned_p))
9355 type = complex_double_type_node;
9356 else if (type == integer_type_node)
9357 type = complex_integer_type_node;
9358 else if (type == float_type_node)
9359 type = complex_float_type_node;
9360 else if (type == double_type_node)
9361 type = complex_double_type_node;
9362 else if (type == long_double_type_node)
9363 type = complex_long_double_type_node;
9364 else
9365 type = build_complex_type (type);
9368 type_quals = TYPE_UNQUALIFIED;
9369 if (decl_spec_seq_has_spec_p (declspecs, ds_const))
9370 type_quals |= TYPE_QUAL_CONST;
9371 if (decl_spec_seq_has_spec_p (declspecs, ds_volatile))
9372 type_quals |= TYPE_QUAL_VOLATILE;
9373 if (decl_spec_seq_has_spec_p (declspecs, ds_restrict))
9374 type_quals |= TYPE_QUAL_RESTRICT;
9375 if (sfk == sfk_conversion && type_quals != TYPE_UNQUALIFIED)
9376 error ("qualifiers are not allowed on declaration of %<operator %T%>",
9377 ctor_return_type);
9379 /* If we're using the injected-class-name to form a compound type or a
9380 declaration, replace it with the underlying class so we don't get
9381 redundant typedefs in the debug output. But if we are returning the
9382 type unchanged, leave it alone so that it's available to
9383 maybe_get_template_decl_from_type_decl. */
9384 if (CLASS_TYPE_P (type)
9385 && DECL_SELF_REFERENCE_P (TYPE_NAME (type))
9386 && type == TREE_TYPE (TYPE_NAME (type))
9387 && (declarator || type_quals))
9388 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
9390 type_quals |= cp_type_quals (type);
9391 type = cp_build_qualified_type_real
9392 (type, type_quals, ((typedef_decl && !DECL_ARTIFICIAL (typedef_decl)
9393 ? tf_ignore_bad_quals : 0) | tf_warning_or_error));
9394 /* We might have ignored or rejected some of the qualifiers. */
9395 type_quals = cp_type_quals (type);
9397 staticp = 0;
9398 inlinep = decl_spec_seq_has_spec_p (declspecs, ds_inline);
9399 virtualp = decl_spec_seq_has_spec_p (declspecs, ds_virtual);
9400 explicitp = decl_spec_seq_has_spec_p (declspecs, ds_explicit);
9402 storage_class = declspecs->storage_class;
9403 if (storage_class == sc_static)
9404 staticp = 1 + (decl_context == FIELD);
9406 if (virtualp && staticp == 2)
9408 error ("member %qD cannot be declared both virtual and static", dname);
9409 storage_class = sc_none;
9410 staticp = 0;
9412 friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
9414 /* Issue errors about use of storage classes for parameters. */
9415 if (decl_context == PARM)
9417 if (typedef_p)
9419 error ("typedef declaration invalid in parameter declaration");
9420 return error_mark_node;
9422 else if (template_parm_flag && storage_class != sc_none)
9424 error ("storage class specified for template parameter %qs", name);
9425 return error_mark_node;
9427 else if (storage_class == sc_static
9428 || storage_class == sc_extern
9429 || thread_p)
9430 error ("storage class specifiers invalid in parameter declarations");
9432 /* Function parameters cannot be constexpr. If we saw one, moan
9433 and pretend it wasn't there. */
9434 if (constexpr_p)
9436 error ("a parameter cannot be declared %<constexpr%>");
9437 constexpr_p = 0;
9441 /* Give error if `virtual' is used outside of class declaration. */
9442 if (virtualp
9443 && (current_class_name == NULL_TREE || decl_context != FIELD))
9445 error ("%<virtual%> outside class declaration");
9446 virtualp = 0;
9449 /* Static anonymous unions are dealt with here. */
9450 if (staticp && decl_context == TYPENAME
9451 && declspecs->type
9452 && ANON_AGGR_TYPE_P (declspecs->type))
9453 decl_context = FIELD;
9455 /* Warn about storage classes that are invalid for certain
9456 kinds of declarations (parameters, typenames, etc.). */
9457 if (thread_p
9458 && ((storage_class
9459 && storage_class != sc_extern
9460 && storage_class != sc_static)
9461 || typedef_p))
9463 error ("multiple storage classes in declaration of %qs", name);
9464 thread_p = false;
9466 if (decl_context != NORMAL
9467 && ((storage_class != sc_none
9468 && storage_class != sc_mutable)
9469 || thread_p))
9471 if ((decl_context == PARM || decl_context == CATCHPARM)
9472 && (storage_class == sc_register
9473 || storage_class == sc_auto))
9475 else if (typedef_p)
9477 else if (decl_context == FIELD
9478 /* C++ allows static class elements. */
9479 && storage_class == sc_static)
9480 /* C++ also allows inlines and signed and unsigned elements,
9481 but in those cases we don't come in here. */
9483 else
9485 if (decl_context == FIELD)
9486 error ("storage class specified for %qs", name);
9487 else
9489 if (decl_context == PARM || decl_context == CATCHPARM)
9490 error ("storage class specified for parameter %qs", name);
9491 else
9492 error ("storage class specified for typename");
9494 if (storage_class == sc_register
9495 || storage_class == sc_auto
9496 || storage_class == sc_extern
9497 || thread_p)
9498 storage_class = sc_none;
9501 else if (storage_class == sc_extern && funcdef_flag
9502 && ! toplevel_bindings_p ())
9503 error ("nested function %qs declared %<extern%>", name);
9504 else if (toplevel_bindings_p ())
9506 if (storage_class == sc_auto)
9507 error ("top-level declaration of %qs specifies %<auto%>", name);
9509 else if (thread_p
9510 && storage_class != sc_extern
9511 && storage_class != sc_static)
9513 if (declspecs->gnu_thread_keyword_p)
9514 pedwarn (input_location, 0, "function-scope %qs implicitly auto and "
9515 "declared %<__thread%>", name);
9517 /* When thread_local is applied to a variable of block scope the
9518 storage-class-specifier static is implied if it does not appear
9519 explicitly. */
9520 storage_class = declspecs->storage_class = sc_static;
9521 staticp = 1;
9524 if (storage_class && friendp)
9526 error ("storage class specifiers invalid in friend function declarations");
9527 storage_class = sc_none;
9528 staticp = 0;
9531 if (!id_declarator)
9532 unqualified_id = NULL_TREE;
9533 else
9535 unqualified_id = id_declarator->u.id.unqualified_name;
9536 switch (TREE_CODE (unqualified_id))
9538 case BIT_NOT_EXPR:
9539 unqualified_id = TREE_OPERAND (unqualified_id, 0);
9540 if (TYPE_P (unqualified_id))
9541 unqualified_id = constructor_name (unqualified_id);
9542 break;
9544 case IDENTIFIER_NODE:
9545 case TEMPLATE_ID_EXPR:
9546 break;
9548 default:
9549 gcc_unreachable ();
9553 if (declspecs->std_attributes)
9555 /* Apply the c++11 attributes to the type preceding them. */
9556 input_location = declspecs->locations[ds_std_attribute];
9557 decl_attributes (&type, declspecs->std_attributes, 0);
9558 input_location = saved_loc;
9561 /* Determine the type of the entity declared by recurring on the
9562 declarator. */
9563 for (; declarator; declarator = declarator->declarator)
9565 const cp_declarator *inner_declarator;
9566 tree attrs;
9568 if (type == error_mark_node)
9569 return error_mark_node;
9571 attrs = declarator->attributes;
9572 if (attrs)
9574 int attr_flags;
9576 attr_flags = 0;
9577 if (declarator == NULL || declarator->kind == cdk_id)
9578 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
9579 if (declarator->kind == cdk_function)
9580 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
9581 if (declarator->kind == cdk_array)
9582 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
9583 returned_attrs = decl_attributes (&type,
9584 chainon (returned_attrs, attrs),
9585 attr_flags);
9588 if (declarator->kind == cdk_id)
9589 break;
9591 inner_declarator = declarator->declarator;
9593 switch (declarator->kind)
9595 case cdk_array:
9596 type = create_array_type_for_decl (dname, type,
9597 declarator->u.array.bounds);
9598 if (declarator->std_attributes)
9599 /* [dcl.array]/1:
9601 The optional attribute-specifier-seq appertains to the
9602 array. */
9603 returned_attrs = chainon (returned_attrs,
9604 declarator->std_attributes);
9605 break;
9607 case cdk_function:
9609 tree arg_types;
9610 int funcdecl_p;
9612 /* Declaring a function type.
9613 Make sure we have a valid type for the function to return. */
9615 if (type_quals != TYPE_UNQUALIFIED)
9617 if (SCALAR_TYPE_P (type) || VOID_TYPE_P (type))
9618 warning (OPT_Wignored_qualifiers,
9619 "type qualifiers ignored on function return type");
9620 /* We now know that the TYPE_QUALS don't apply to the
9621 decl, but to its return type. */
9622 type_quals = TYPE_UNQUALIFIED;
9624 errmsg = targetm.invalid_return_type (type);
9625 if (errmsg)
9627 error (errmsg);
9628 type = integer_type_node;
9631 /* Error about some types functions can't return. */
9633 if (TREE_CODE (type) == FUNCTION_TYPE)
9635 error ("%qs declared as function returning a function", name);
9636 return error_mark_node;
9638 if (TREE_CODE (type) == ARRAY_TYPE)
9640 error ("%qs declared as function returning an array", name);
9641 return error_mark_node;
9644 input_location = declspecs->locations[ds_type_spec];
9645 abstract_virtuals_error (ACU_RETURN, type);
9646 input_location = saved_loc;
9648 /* Pick up type qualifiers which should be applied to `this'. */
9649 memfn_quals = declarator->u.function.qualifiers;
9650 /* Pick up virt-specifiers. */
9651 virt_specifiers = declarator->u.function.virt_specifiers;
9652 /* And ref-qualifier, too */
9653 rqual = declarator->u.function.ref_qualifier;
9654 /* Pick up the exception specifications. */
9655 raises = declarator->u.function.exception_specification;
9656 /* If the exception-specification is ill-formed, let's pretend
9657 there wasn't one. */
9658 if (raises == error_mark_node)
9659 raises = NULL_TREE;
9661 /* Say it's a definition only for the CALL_EXPR
9662 closest to the identifier. */
9663 funcdecl_p = inner_declarator && inner_declarator->kind == cdk_id;
9665 /* Handle a late-specified return type. */
9666 if (funcdecl_p)
9668 if (type_uses_auto (type))
9670 if (!declarator->u.function.late_return_type)
9672 if (current_class_type
9673 && LAMBDA_TYPE_P (current_class_type))
9674 /* OK for C++11 lambdas. */;
9675 else if (cxx_dialect < cxx1y)
9677 error ("%qs function uses "
9678 "%<auto%> type specifier without trailing "
9679 "return type", name);
9680 inform (input_location, "deduced return type "
9681 "only available with -std=c++1y or "
9682 "-std=gnu++1y");
9684 else if (virtualp)
9686 error ("virtual function cannot "
9687 "have deduced return type");
9688 virtualp = false;
9691 else if (!is_auto (type))
9693 error ("%qs function with trailing return type has"
9694 " %qT as its type rather than plain %<auto%>",
9695 name, type);
9696 return error_mark_node;
9699 else if (declarator->u.function.late_return_type)
9701 if (cxx_dialect < cxx11)
9702 /* Not using maybe_warn_cpp0x because this should
9703 always be an error. */
9704 error ("trailing return type only available with "
9705 "-std=c++11 or -std=gnu++11");
9706 else
9707 error ("%qs function with trailing return type not "
9708 "declared with %<auto%> type specifier", name);
9709 return error_mark_node;
9712 type = splice_late_return_type
9713 (type, declarator->u.function.late_return_type);
9714 if (type == error_mark_node)
9715 return error_mark_node;
9717 if (declarator->u.function.late_return_type)
9718 late_return_type_p = true;
9720 if (ctype == NULL_TREE
9721 && decl_context == FIELD
9722 && funcdecl_p
9723 && friendp == 0)
9724 ctype = current_class_type;
9726 if (ctype && (sfk == sfk_constructor
9727 || sfk == sfk_destructor))
9729 /* We are within a class's scope. If our declarator name
9730 is the same as the class name, and we are defining
9731 a function, then it is a constructor/destructor, and
9732 therefore returns a void type. */
9734 /* ISO C++ 12.4/2. A destructor may not be declared
9735 const or volatile. A destructor may not be static.
9736 A destructor may not be declared with ref-qualifier.
9738 ISO C++ 12.1. A constructor may not be declared
9739 const or volatile. A constructor may not be
9740 virtual. A constructor may not be static.
9741 A constructor may not be declared with ref-qualifier. */
9742 if (staticp == 2)
9743 error ((flags == DTOR_FLAG)
9744 ? G_("destructor cannot be static member function")
9745 : G_("constructor cannot be static member function"));
9746 if (memfn_quals)
9748 error ((flags == DTOR_FLAG)
9749 ? G_("destructors may not be cv-qualified")
9750 : G_("constructors may not be cv-qualified"));
9751 memfn_quals = TYPE_UNQUALIFIED;
9754 if (rqual)
9756 maybe_warn_cpp0x (CPP0X_REF_QUALIFIER);
9757 error ((flags == DTOR_FLAG)
9758 ? "destructors may not be ref-qualified"
9759 : "constructors may not be ref-qualified");
9760 rqual = REF_QUAL_NONE;
9763 if (decl_context == FIELD
9764 && !member_function_or_else (ctype,
9765 current_class_type,
9766 flags))
9767 return error_mark_node;
9769 if (flags != DTOR_FLAG)
9771 /* It's a constructor. */
9772 if (explicitp == 1)
9773 explicitp = 2;
9774 if (virtualp)
9776 permerror (input_location, "constructors cannot be declared virtual");
9777 virtualp = 0;
9779 if (decl_context == FIELD
9780 && sfk != sfk_constructor)
9781 return error_mark_node;
9783 if (decl_context == FIELD)
9784 staticp = 0;
9786 else if (friendp)
9788 if (initialized)
9789 error ("can%'t initialize friend function %qs", name);
9790 if (virtualp)
9792 /* Cannot be both friend and virtual. */
9793 error ("virtual functions cannot be friends");
9794 friendp = 0;
9796 if (decl_context == NORMAL)
9797 error ("friend declaration not in class definition");
9798 if (current_function_decl && funcdef_flag)
9799 error ("can%'t define friend function %qs in a local "
9800 "class definition",
9801 name);
9803 else if (ctype && sfk == sfk_conversion)
9805 if (explicitp == 1)
9807 maybe_warn_cpp0x (CPP0X_EXPLICIT_CONVERSION);
9808 explicitp = 2;
9812 arg_types = grokparms (declarator->u.function.parameters,
9813 &parms);
9815 if (inner_declarator
9816 && inner_declarator->kind == cdk_id
9817 && inner_declarator->u.id.sfk == sfk_destructor
9818 && arg_types != void_list_node)
9820 error ("destructors may not have parameters");
9821 arg_types = void_list_node;
9822 parms = NULL_TREE;
9825 type = build_function_type (type, arg_types);
9826 if (declarator->std_attributes)
9827 /* [dcl.fct]/2:
9829 The optional attribute-specifier-seq appertains to
9830 the function type. */
9831 decl_attributes (&type, declarator->std_attributes,
9834 break;
9836 case cdk_pointer:
9837 case cdk_reference:
9838 case cdk_ptrmem:
9839 /* Filter out pointers-to-references and references-to-references.
9840 We can get these if a TYPE_DECL is used. */
9842 if (TREE_CODE (type) == REFERENCE_TYPE)
9844 if (declarator->kind != cdk_reference)
9846 error ("cannot declare pointer to %q#T", type);
9847 type = TREE_TYPE (type);
9850 /* In C++0x, we allow reference to reference declarations
9851 that occur indirectly through typedefs [7.1.3/8 dcl.typedef]
9852 and template type arguments [14.3.1/4 temp.arg.type]. The
9853 check for direct reference to reference declarations, which
9854 are still forbidden, occurs below. Reasoning behind the change
9855 can be found in DR106, DR540, and the rvalue reference
9856 proposals. */
9857 else if (cxx_dialect == cxx98)
9859 error ("cannot declare reference to %q#T", type);
9860 type = TREE_TYPE (type);
9863 else if (VOID_TYPE_P (type))
9865 if (declarator->kind == cdk_reference)
9866 error ("cannot declare reference to %q#T", type);
9867 else if (declarator->kind == cdk_ptrmem)
9868 error ("cannot declare pointer to %q#T member", type);
9871 /* We now know that the TYPE_QUALS don't apply to the decl,
9872 but to the target of the pointer. */
9873 type_quals = TYPE_UNQUALIFIED;
9875 /* This code used to handle METHOD_TYPE, but I don't think it's
9876 possible to get it here anymore. */
9877 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
9878 if (declarator->kind == cdk_ptrmem
9879 && TREE_CODE (type) == FUNCTION_TYPE)
9881 memfn_quals |= type_memfn_quals (type);
9882 type = build_memfn_type (type,
9883 declarator->u.pointer.class_type,
9884 memfn_quals,
9885 rqual);
9886 if (type == error_mark_node)
9887 return error_mark_node;
9889 rqual = REF_QUAL_NONE;
9890 memfn_quals = TYPE_UNQUALIFIED;
9893 if (TREE_CODE (type) == FUNCTION_TYPE
9894 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
9895 || type_memfn_rqual (type) != REF_QUAL_NONE))
9896 error (declarator->kind == cdk_reference
9897 ? G_("cannot declare reference to qualified function type %qT")
9898 : G_("cannot declare pointer to qualified function type %qT"),
9899 type);
9901 if (cxx_dialect >= cxx1y && array_of_runtime_bound_p (type)
9902 && (flag_iso || warn_vla > 0))
9903 pedwarn (input_location, OPT_Wvla,
9904 declarator->kind == cdk_reference
9905 ? G_("reference to array of runtime bound")
9906 : G_("pointer to array of runtime bound"));
9908 /* When the pointed-to type involves components of variable size,
9909 care must be taken to ensure that the size evaluation code is
9910 emitted early enough to dominate all the possible later uses
9911 and late enough for the variables on which it depends to have
9912 been assigned.
9914 This is expected to happen automatically when the pointed-to
9915 type has a name/declaration of it's own, but special attention
9916 is required if the type is anonymous.
9918 We handle the NORMAL and FIELD contexts here by inserting a
9919 dummy statement that just evaluates the size at a safe point
9920 and ensures it is not deferred until e.g. within a deeper
9921 conditional context (c++/43555).
9923 We expect nothing to be needed here for PARM or TYPENAME.
9924 Evaluating the size at this point for TYPENAME would
9925 actually be incorrect, as we might be in the middle of an
9926 expression with side effects on the pointed-to type size
9927 "arguments" prior to the pointer declaration point and the
9928 size evaluation could end up prior to the side effects. */
9930 if (!TYPE_NAME (type)
9931 && (decl_context == NORMAL || decl_context == FIELD)
9932 && at_function_scope_p ()
9933 && variably_modified_type_p (type, NULL_TREE))
9934 /* Force evaluation of the SAVE_EXPR. */
9935 finish_expr_stmt (TYPE_SIZE (type));
9937 if (declarator->kind == cdk_reference)
9939 /* In C++0x, the type we are creating a reference to might be
9940 a typedef which is itself a reference type. In that case,
9941 we follow the reference collapsing rules in
9942 [7.1.3/8 dcl.typedef] to create the final reference type:
9944 "If a typedef TD names a type that is a reference to a type
9945 T, an attempt to create the type 'lvalue reference to cv TD'
9946 creates the type 'lvalue reference to T,' while an attempt
9947 to create the type "rvalue reference to cv TD' creates the
9948 type TD."
9950 if (VOID_TYPE_P (type))
9951 /* We already gave an error. */;
9952 else if (TREE_CODE (type) == REFERENCE_TYPE)
9954 if (declarator->u.reference.rvalue_ref)
9955 /* Leave type alone. */;
9956 else
9957 type = cp_build_reference_type (TREE_TYPE (type), false);
9959 else
9960 type = cp_build_reference_type
9961 (type, declarator->u.reference.rvalue_ref);
9963 /* In C++0x, we need this check for direct reference to
9964 reference declarations, which are forbidden by
9965 [8.3.2/5 dcl.ref]. Reference to reference declarations
9966 are only allowed indirectly through typedefs and template
9967 type arguments. Example:
9969 void foo(int & &); // invalid ref-to-ref decl
9971 typedef int & int_ref;
9972 void foo(int_ref &); // valid ref-to-ref decl
9974 if (inner_declarator && inner_declarator->kind == cdk_reference)
9975 error ("cannot declare reference to %q#T, which is not "
9976 "a typedef or a template type argument", type);
9978 else if (TREE_CODE (type) == METHOD_TYPE)
9979 type = build_ptrmemfunc_type (build_pointer_type (type));
9980 else if (declarator->kind == cdk_ptrmem)
9982 gcc_assert (TREE_CODE (declarator->u.pointer.class_type)
9983 != NAMESPACE_DECL);
9984 if (declarator->u.pointer.class_type == error_mark_node)
9985 /* We will already have complained. */
9986 type = error_mark_node;
9987 else
9988 type = build_ptrmem_type (declarator->u.pointer.class_type,
9989 type);
9991 else
9992 type = build_pointer_type (type);
9994 /* Process a list of type modifier keywords (such as
9995 const or volatile) that were given inside the `*' or `&'. */
9997 if (declarator->u.pointer.qualifiers)
9999 type
10000 = cp_build_qualified_type (type,
10001 declarator->u.pointer.qualifiers);
10002 type_quals = cp_type_quals (type);
10005 /* Apply C++11 attributes to the pointer, and not to the
10006 type pointed to. This is unlike what is done for GNU
10007 attributes above. It is to comply with [dcl.ptr]/1:
10009 [the optional attribute-specifier-seq (7.6.1) appertains
10010 to the pointer and not to the object pointed to]. */
10011 if (declarator->std_attributes)
10012 decl_attributes (&type, declarator->std_attributes,
10015 ctype = NULL_TREE;
10016 break;
10018 case cdk_error:
10019 break;
10021 default:
10022 gcc_unreachable ();
10026 /* A `constexpr' specifier used in an object declaration declares
10027 the object as `const'. */
10028 if (constexpr_p && innermost_code != cdk_function)
10030 if (type_quals & TYPE_QUAL_VOLATILE)
10031 error ("both %<volatile%> and %<constexpr%> cannot be used here");
10032 if (TREE_CODE (type) != REFERENCE_TYPE)
10034 type_quals |= TYPE_QUAL_CONST;
10035 type = cp_build_qualified_type (type, type_quals);
10039 if (unqualified_id && TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR
10040 && TREE_CODE (type) != FUNCTION_TYPE
10041 && TREE_CODE (type) != METHOD_TYPE)
10043 error ("template-id %qD used as a declarator",
10044 unqualified_id);
10045 unqualified_id = dname;
10048 /* If TYPE is a FUNCTION_TYPE, but the function name was explicitly
10049 qualified with a class-name, turn it into a METHOD_TYPE, unless
10050 we know that the function is static. We take advantage of this
10051 opportunity to do other processing that pertains to entities
10052 explicitly declared to be class members. Note that if DECLARATOR
10053 is non-NULL, we know it is a cdk_id declarator; otherwise, we
10054 would not have exited the loop above. */
10055 if (declarator
10056 && declarator->u.id.qualifying_scope
10057 && MAYBE_CLASS_TYPE_P (declarator->u.id.qualifying_scope))
10059 ctype = declarator->u.id.qualifying_scope;
10060 ctype = TYPE_MAIN_VARIANT (ctype);
10061 template_count = num_template_headers_for_class (ctype);
10063 if (ctype == current_class_type)
10065 if (friendp)
10067 permerror (input_location, "member functions are implicitly friends of their class");
10068 friendp = 0;
10070 else
10071 permerror (declarator->id_loc,
10072 "extra qualification %<%T::%> on member %qs",
10073 ctype, name);
10075 else if (/* If the qualifying type is already complete, then we
10076 can skip the following checks. */
10077 !COMPLETE_TYPE_P (ctype)
10078 && (/* If the function is being defined, then
10079 qualifying type must certainly be complete. */
10080 funcdef_flag
10081 /* A friend declaration of "T::f" is OK, even if
10082 "T" is a template parameter. But, if this
10083 function is not a friend, the qualifying type
10084 must be a class. */
10085 || (!friendp && !CLASS_TYPE_P (ctype))
10086 /* For a declaration, the type need not be
10087 complete, if either it is dependent (since there
10088 is no meaningful definition of complete in that
10089 case) or the qualifying class is currently being
10090 defined. */
10091 || !(dependent_type_p (ctype)
10092 || currently_open_class (ctype)))
10093 /* Check that the qualifying type is complete. */
10094 && !complete_type_or_else (ctype, NULL_TREE))
10095 return error_mark_node;
10096 else if (TREE_CODE (type) == FUNCTION_TYPE)
10098 if (current_class_type
10099 && (!friendp || funcdef_flag))
10101 error (funcdef_flag
10102 ? G_("cannot define member function %<%T::%s%> "
10103 "within %<%T%>")
10104 : G_("cannot declare member function %<%T::%s%> "
10105 "within %<%T%>"),
10106 ctype, name, current_class_type);
10107 return error_mark_node;
10110 else if (typedef_p && current_class_type)
10112 error ("cannot declare member %<%T::%s%> within %qT",
10113 ctype, name, current_class_type);
10114 return error_mark_node;
10118 if (ctype == NULL_TREE && decl_context == FIELD && friendp == 0)
10119 ctype = current_class_type;
10121 /* Now TYPE has the actual type. */
10123 if (returned_attrs)
10125 if (attrlist)
10126 *attrlist = chainon (returned_attrs, *attrlist);
10127 else
10128 attrlist = &returned_attrs;
10131 if (declarator
10132 && declarator->kind == cdk_id
10133 && declarator->std_attributes)
10134 /* [dcl.meaning]/1: The optional attribute-specifier-seq following
10135 a declarator-id appertains to the entity that is declared. */
10136 *attrlist = chainon (*attrlist, declarator->std_attributes);
10138 /* Handle parameter packs. */
10139 if (parameter_pack_p)
10141 if (decl_context == PARM)
10142 /* Turn the type into a pack expansion.*/
10143 type = make_pack_expansion (type);
10144 else
10145 error ("non-parameter %qs cannot be a parameter pack", name);
10148 /* Did array size calculations overflow or does the array cover more
10149 than half of the address-space? */
10150 if (TREE_CODE (type) == ARRAY_TYPE
10151 && COMPLETE_TYPE_P (type)
10152 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
10153 && ! valid_constant_size_p (TYPE_SIZE_UNIT (type)))
10155 error ("size of array %qs is too large", name);
10156 /* If we proceed with the array type as it is, we'll eventually
10157 crash in tree_to_[su]hwi(). */
10158 type = error_mark_node;
10161 if ((decl_context == FIELD || decl_context == PARM)
10162 && !processing_template_decl
10163 && variably_modified_type_p (type, NULL_TREE))
10165 if (decl_context == FIELD)
10166 error ("data member may not have variably modified type %qT", type);
10167 else
10168 error ("parameter may not have variably modified type %qT", type);
10169 type = error_mark_node;
10172 if (explicitp == 1 || (explicitp && friendp))
10174 /* [dcl.fct.spec] (C++11) The explicit specifier shall be used only
10175 in the declaration of a constructor or conversion function within
10176 a class definition. */
10177 if (!current_class_type)
10178 error ("%<explicit%> outside class declaration");
10179 else if (friendp)
10180 error ("%<explicit%> in friend declaration");
10181 else
10182 error ("only declarations of constructors and conversion operators "
10183 "can be %<explicit%>");
10184 explicitp = 0;
10187 if (storage_class == sc_mutable)
10189 if (decl_context != FIELD || friendp)
10191 error ("non-member %qs cannot be declared %<mutable%>", name);
10192 storage_class = sc_none;
10194 else if (decl_context == TYPENAME || typedef_p)
10196 error ("non-object member %qs cannot be declared %<mutable%>", name);
10197 storage_class = sc_none;
10199 else if (TREE_CODE (type) == FUNCTION_TYPE
10200 || TREE_CODE (type) == METHOD_TYPE)
10202 error ("function %qs cannot be declared %<mutable%>", name);
10203 storage_class = sc_none;
10205 else if (staticp)
10207 error ("static %qs cannot be declared %<mutable%>", name);
10208 storage_class = sc_none;
10210 else if (type_quals & TYPE_QUAL_CONST)
10212 error ("const %qs cannot be declared %<mutable%>", name);
10213 storage_class = sc_none;
10215 else if (TREE_CODE (type) == REFERENCE_TYPE)
10217 permerror (input_location, "reference %qs cannot be declared "
10218 "%<mutable%>", name);
10219 storage_class = sc_none;
10223 /* If this is declaring a typedef name, return a TYPE_DECL. */
10224 if (typedef_p && decl_context != TYPENAME)
10226 tree decl;
10228 /* Note that the grammar rejects storage classes
10229 in typenames, fields or parameters. */
10230 if (current_lang_name == lang_name_java)
10231 TYPE_FOR_JAVA (type) = 1;
10233 /* This declaration:
10235 typedef void f(int) const;
10237 declares a function type which is not a member of any
10238 particular class, but which is cv-qualified; for
10239 example "f S::*" declares a pointer to a const-qualified
10240 member function of S. We record the cv-qualification in the
10241 function type. */
10242 if ((rqual || memfn_quals) && TREE_CODE (type) == FUNCTION_TYPE)
10244 type = apply_memfn_quals (type, memfn_quals, rqual);
10246 /* We have now dealt with these qualifiers. */
10247 memfn_quals = TYPE_UNQUALIFIED;
10248 rqual = REF_QUAL_NONE;
10251 if (type_uses_auto (type))
10253 error ("typedef declared %<auto%>");
10254 type = error_mark_node;
10257 if (cxx_dialect >= cxx1y && array_of_runtime_bound_p (type)
10258 && (flag_iso || warn_vla > 0))
10259 pedwarn (input_location, OPT_Wvla,
10260 "typedef naming array of runtime bound");
10262 if (decl_context == FIELD)
10263 decl = build_lang_decl (TYPE_DECL, unqualified_id, type);
10264 else
10265 decl = build_decl (input_location, TYPE_DECL, unqualified_id, type);
10266 if (id_declarator && declarator->u.id.qualifying_scope) {
10267 error_at (DECL_SOURCE_LOCATION (decl),
10268 "typedef name may not be a nested-name-specifier");
10269 TREE_TYPE (decl) = error_mark_node;
10272 if (decl_context != FIELD)
10274 if (!current_function_decl)
10275 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
10276 else if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (current_function_decl)
10277 || (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P
10278 (current_function_decl)))
10279 /* The TYPE_DECL is "abstract" because there will be
10280 clones of this constructor/destructor, and there will
10281 be copies of this TYPE_DECL generated in those
10282 clones. The decloning optimization (for space) may
10283 revert this subsequently if it determines that
10284 the clones should share a common implementation. */
10285 DECL_ABSTRACT (decl) = 1;
10287 else if (current_class_type
10288 && constructor_name_p (unqualified_id, current_class_type))
10289 permerror (input_location, "ISO C++ forbids nested type %qD with same name "
10290 "as enclosing class",
10291 unqualified_id);
10293 /* If the user declares "typedef struct {...} foo" then the
10294 struct will have an anonymous name. Fill that name in now.
10295 Nothing can refer to it, so nothing needs know about the name
10296 change. */
10297 if (type != error_mark_node
10298 && unqualified_id
10299 && TYPE_NAME (type)
10300 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10301 && TYPE_ANONYMOUS_P (type)
10302 && declspecs->type_definition_p
10303 && attributes_naming_typedef_ok (*attrlist)
10304 && cp_type_quals (type) == TYPE_UNQUALIFIED)
10306 tree t;
10308 /* Replace the anonymous name with the real name everywhere. */
10309 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
10311 if (ANON_AGGRNAME_P (TYPE_IDENTIFIER (t)))
10312 /* We do not rename the debug info representing the
10313 anonymous tagged type because the standard says in
10314 [dcl.typedef] that the naming applies only for
10315 linkage purposes. */
10316 /*debug_hooks->set_name (t, decl);*/
10317 TYPE_NAME (t) = decl;
10320 if (TYPE_LANG_SPECIFIC (type))
10321 TYPE_WAS_ANONYMOUS (type) = 1;
10323 /* If this is a typedef within a template class, the nested
10324 type is a (non-primary) template. The name for the
10325 template needs updating as well. */
10326 if (TYPE_LANG_SPECIFIC (type) && CLASSTYPE_TEMPLATE_INFO (type))
10327 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type))
10328 = TYPE_IDENTIFIER (type);
10330 /* Adjust linkage now that we aren't anonymous anymore. */
10331 reset_type_linkage (type);
10333 /* FIXME remangle member functions; member functions of a
10334 type with external linkage have external linkage. */
10337 if (signed_p
10338 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
10339 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
10341 bad_specifiers (decl, BSP_TYPE, virtualp,
10342 memfn_quals != TYPE_UNQUALIFIED,
10343 inlinep, friendp, raises != NULL_TREE);
10345 if (decl_spec_seq_has_spec_p (declspecs, ds_alias))
10346 /* Acknowledge that this was written:
10347 `using analias = atype;'. */
10348 TYPE_DECL_ALIAS_P (decl) = 1;
10350 return decl;
10353 /* Detect the case of an array type of unspecified size
10354 which came, as such, direct from a typedef name.
10355 We must copy the type, so that the array's domain can be
10356 individually set by the object's initializer. */
10358 if (type && typedef_type
10359 && TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
10360 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
10361 type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
10363 /* Detect where we're using a typedef of function type to declare a
10364 function. PARMS will not be set, so we must create it now. */
10366 if (type == typedef_type && TREE_CODE (type) == FUNCTION_TYPE)
10368 tree decls = NULL_TREE;
10369 tree args;
10371 for (args = TYPE_ARG_TYPES (type);
10372 args && args != void_list_node;
10373 args = TREE_CHAIN (args))
10375 tree decl = cp_build_parm_decl (NULL_TREE, TREE_VALUE (args));
10377 DECL_CHAIN (decl) = decls;
10378 decls = decl;
10381 parms = nreverse (decls);
10383 if (decl_context != TYPENAME)
10385 /* The qualifiers on the function type become the qualifiers on
10386 the non-static member function. */
10387 memfn_quals |= type_memfn_quals (type);
10388 rqual = type_memfn_rqual (type);
10389 type_quals = TYPE_UNQUALIFIED;
10393 /* If this is a type name (such as, in a cast or sizeof),
10394 compute the type and return it now. */
10396 if (decl_context == TYPENAME)
10398 /* Note that the grammar rejects storage classes
10399 in typenames, fields or parameters. */
10400 if (type_quals != TYPE_UNQUALIFIED)
10401 type_quals = TYPE_UNQUALIFIED;
10403 /* Special case: "friend class foo" looks like a TYPENAME context. */
10404 if (friendp)
10406 if (type_quals != TYPE_UNQUALIFIED)
10408 error ("type qualifiers specified for friend class declaration");
10409 type_quals = TYPE_UNQUALIFIED;
10411 if (inlinep)
10413 error ("%<inline%> specified for friend class declaration");
10414 inlinep = 0;
10417 if (!current_aggr)
10419 /* Don't allow friend declaration without a class-key. */
10420 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
10421 permerror (input_location, "template parameters cannot be friends");
10422 else if (TREE_CODE (type) == TYPENAME_TYPE)
10423 permerror (input_location, "friend declaration requires class-key, "
10424 "i.e. %<friend class %T::%D%>",
10425 TYPE_CONTEXT (type), TYPENAME_TYPE_FULLNAME (type));
10426 else
10427 permerror (input_location, "friend declaration requires class-key, "
10428 "i.e. %<friend %#T%>",
10429 type);
10432 /* Only try to do this stuff if we didn't already give up. */
10433 if (type != integer_type_node)
10435 /* A friendly class? */
10436 if (current_class_type)
10437 make_friend_class (current_class_type, TYPE_MAIN_VARIANT (type),
10438 /*complain=*/true);
10439 else
10440 error ("trying to make class %qT a friend of global scope",
10441 type);
10443 type = void_type_node;
10446 else if (memfn_quals || rqual)
10448 if (ctype == NULL_TREE
10449 && TREE_CODE (type) == METHOD_TYPE)
10450 ctype = TYPE_METHOD_BASETYPE (type);
10452 if (ctype)
10453 type = build_memfn_type (type, ctype, memfn_quals, rqual);
10454 /* Core issue #547: need to allow this in template type args.
10455 Allow it in general in C++11 for alias-declarations. */
10456 else if ((template_type_arg || cxx_dialect >= cxx11)
10457 && TREE_CODE (type) == FUNCTION_TYPE)
10458 type = apply_memfn_quals (type, memfn_quals, rqual);
10459 else
10460 error ("invalid qualifiers on non-member function type");
10463 return type;
10465 else if (unqualified_id == NULL_TREE && decl_context != PARM
10466 && decl_context != CATCHPARM
10467 && TREE_CODE (type) != UNION_TYPE
10468 && ! bitfield)
10470 error ("abstract declarator %qT used as declaration", type);
10471 return error_mark_node;
10474 /* Only functions may be declared using an operator-function-id. */
10475 if (unqualified_id
10476 && IDENTIFIER_OPNAME_P (unqualified_id)
10477 && TREE_CODE (type) != FUNCTION_TYPE
10478 && TREE_CODE (type) != METHOD_TYPE)
10480 error ("declaration of %qD as non-function", unqualified_id);
10481 return error_mark_node;
10484 /* We don't check parameter types here because we can emit a better
10485 error message later. */
10486 if (decl_context != PARM)
10488 type = check_var_type (unqualified_id, type);
10489 if (type == error_mark_node)
10490 return error_mark_node;
10493 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
10494 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
10496 if (decl_context == PARM || decl_context == CATCHPARM)
10498 if (ctype || in_namespace)
10499 error ("cannot use %<::%> in parameter declaration");
10501 if (type_uses_auto (type))
10503 if (cxx_dialect >= cxx1y)
10504 error ("%<auto%> parameter not permitted in this context");
10505 else
10506 error ("parameter declared %<auto%>");
10507 type = error_mark_node;
10510 /* A parameter declared as an array of T is really a pointer to T.
10511 One declared as a function is really a pointer to a function.
10512 One declared as a member is really a pointer to member. */
10514 if (TREE_CODE (type) == ARRAY_TYPE)
10516 /* Transfer const-ness of array into that of type pointed to. */
10517 type = build_pointer_type (TREE_TYPE (type));
10518 type_quals = TYPE_UNQUALIFIED;
10519 array_parameter_p = true;
10521 else if (TREE_CODE (type) == FUNCTION_TYPE)
10522 type = build_pointer_type (type);
10525 if (ctype && TREE_CODE (type) == FUNCTION_TYPE && staticp < 2
10526 && !NEW_DELETE_OPNAME_P (unqualified_id))
10528 cp_cv_quals real_quals = memfn_quals;
10529 if (constexpr_p && sfk != sfk_constructor && sfk != sfk_destructor)
10530 real_quals |= TYPE_QUAL_CONST;
10531 type = build_memfn_type (type, ctype, real_quals, rqual);
10535 tree decl;
10537 if (decl_context == PARM)
10539 decl = cp_build_parm_decl (unqualified_id, type);
10540 DECL_ARRAY_PARAMETER_P (decl) = array_parameter_p;
10542 bad_specifiers (decl, BSP_PARM, virtualp,
10543 memfn_quals != TYPE_UNQUALIFIED,
10544 inlinep, friendp, raises != NULL_TREE);
10546 else if (decl_context == FIELD)
10548 if (!staticp && TREE_CODE (type) != METHOD_TYPE
10549 && type_uses_auto (type))
10551 error ("non-static data member declared %<auto%>");
10552 type = error_mark_node;
10555 /* The C99 flexible array extension. */
10556 if (!staticp && TREE_CODE (type) == ARRAY_TYPE
10557 && TYPE_DOMAIN (type) == NULL_TREE)
10559 tree itype = compute_array_index_type (dname, integer_zero_node,
10560 tf_warning_or_error);
10561 type = build_cplus_array_type (TREE_TYPE (type), itype);
10564 if (type == error_mark_node)
10566 /* Happens when declaring arrays of sizes which
10567 are error_mark_node, for example. */
10568 decl = NULL_TREE;
10570 else if (in_namespace && !friendp)
10572 /* Something like struct S { int N::j; }; */
10573 error ("invalid use of %<::%>");
10574 return error_mark_node;
10576 else if (TREE_CODE (type) == FUNCTION_TYPE
10577 || TREE_CODE (type) == METHOD_TYPE)
10579 int publicp = 0;
10580 tree function_context;
10582 if (friendp == 0)
10584 /* This should never happen in pure C++ (the check
10585 could be an assert). It could happen in
10586 Objective-C++ if someone writes invalid code that
10587 uses a function declaration for an instance
10588 variable or property (instance variables and
10589 properties are parsed as FIELD_DECLs, but they are
10590 part of an Objective-C class, not a C++ class).
10591 That code is invalid and is caught by this
10592 check. */
10593 if (!ctype)
10595 error ("declaration of function %qD in invalid context",
10596 unqualified_id);
10597 return error_mark_node;
10600 /* ``A union may [ ... ] not [ have ] virtual functions.''
10601 ARM 9.5 */
10602 if (virtualp && TREE_CODE (ctype) == UNION_TYPE)
10604 error ("function %qD declared virtual inside a union",
10605 unqualified_id);
10606 return error_mark_node;
10609 if (NEW_DELETE_OPNAME_P (unqualified_id))
10611 if (virtualp)
10613 error ("%qD cannot be declared virtual, since it "
10614 "is always static",
10615 unqualified_id);
10616 virtualp = 0;
10621 /* Check that the name used for a destructor makes sense. */
10622 if (sfk == sfk_destructor)
10624 tree uqname = id_declarator->u.id.unqualified_name;
10626 if (!ctype)
10628 gcc_assert (friendp);
10629 error ("expected qualified name in friend declaration "
10630 "for destructor %qD", uqname);
10631 return error_mark_node;
10634 if (!check_dtor_name (ctype, TREE_OPERAND (uqname, 0)))
10636 error ("declaration of %qD as member of %qT",
10637 uqname, ctype);
10638 return error_mark_node;
10640 if (constexpr_p)
10642 error ("a destructor cannot be %<constexpr%>");
10643 return error_mark_node;
10646 else if (sfk == sfk_constructor && friendp && !ctype)
10648 error ("expected qualified name in friend declaration "
10649 "for constructor %qD",
10650 id_declarator->u.id.unqualified_name);
10651 return error_mark_node;
10654 /* Tell grokfndecl if it needs to set TREE_PUBLIC on the node. */
10655 function_context = (ctype != NULL_TREE) ?
10656 decl_function_context (TYPE_MAIN_DECL (ctype)) : NULL_TREE;
10657 publicp = (! friendp || ! staticp)
10658 && function_context == NULL_TREE;
10660 if (late_return_type_p)
10661 TYPE_HAS_LATE_RETURN_TYPE (type) = 1;
10663 decl = grokfndecl (ctype, type,
10664 TREE_CODE (unqualified_id) != TEMPLATE_ID_EXPR
10665 ? unqualified_id : dname,
10666 parms,
10667 unqualified_id,
10668 virtualp, flags, memfn_quals, rqual, raises,
10669 friendp ? -1 : 0, friendp, publicp,
10670 inlinep | (2 * constexpr_p),
10671 sfk,
10672 funcdef_flag, template_count, in_namespace,
10673 attrlist, declarator->id_loc);
10674 decl = set_virt_specifiers (decl, virt_specifiers);
10675 if (decl == NULL_TREE)
10676 return error_mark_node;
10677 #if 0
10678 /* This clobbers the attrs stored in `decl' from `attrlist'. */
10679 /* The decl and setting of decl_attr is also turned off. */
10680 decl = build_decl_attribute_variant (decl, decl_attr);
10681 #endif
10683 /* [class.conv.ctor]
10685 A constructor declared without the function-specifier
10686 explicit that can be called with a single parameter
10687 specifies a conversion from the type of its first
10688 parameter to the type of its class. Such a constructor
10689 is called a converting constructor. */
10690 if (explicitp == 2)
10691 DECL_NONCONVERTING_P (decl) = 1;
10693 else if (!staticp && !dependent_type_p (type)
10694 && !COMPLETE_TYPE_P (complete_type (type))
10695 && (TREE_CODE (type) != ARRAY_TYPE
10696 || !COMPLETE_TYPE_P (TREE_TYPE (type))
10697 || initialized == 0))
10699 if (unqualified_id)
10701 error ("field %qD has incomplete type %qT",
10702 unqualified_id, type);
10703 cxx_incomplete_type_inform (strip_array_types (type));
10705 else
10706 error ("name %qT has incomplete type", type);
10708 type = error_mark_node;
10709 decl = NULL_TREE;
10711 else
10713 if (friendp)
10715 error ("%qE is neither function nor member function; "
10716 "cannot be declared friend", unqualified_id);
10717 friendp = 0;
10719 decl = NULL_TREE;
10722 if (friendp)
10724 /* Friends are treated specially. */
10725 if (ctype == current_class_type)
10726 ; /* We already issued a permerror. */
10727 else if (decl && DECL_NAME (decl))
10729 if (template_class_depth (current_class_type) == 0)
10731 decl = check_explicit_specialization
10732 (unqualified_id, decl, template_count,
10733 2 * funcdef_flag + 4);
10734 if (decl == error_mark_node)
10735 return error_mark_node;
10738 decl = do_friend (ctype, unqualified_id, decl,
10739 *attrlist, flags,
10740 funcdef_flag);
10741 return decl;
10743 else
10744 return error_mark_node;
10747 /* Structure field. It may not be a function, except for C++. */
10749 if (decl == NULL_TREE)
10751 if (staticp)
10753 /* C++ allows static class members. All other work
10754 for this is done by grokfield. */
10755 decl = build_lang_decl_loc (declarator
10756 ? declarator->id_loc
10757 : input_location,
10758 VAR_DECL, unqualified_id, type);
10759 set_linkage_for_static_data_member (decl);
10760 /* Even if there is an in-class initialization, DECL
10761 is considered undefined until an out-of-class
10762 definition is provided. */
10763 DECL_EXTERNAL (decl) = 1;
10765 if (thread_p)
10767 set_decl_tls_model (decl, decl_default_tls_model (decl));
10768 if (declspecs->gnu_thread_keyword_p)
10769 DECL_GNU_TLS_P (decl) = true;
10772 if (constexpr_p && !initialized)
10774 error ("constexpr static data member %qD must have an "
10775 "initializer", decl);
10776 constexpr_p = false;
10779 else
10781 if (constexpr_p)
10783 error ("non-static data member %qE declared %<constexpr%>",
10784 unqualified_id);
10785 constexpr_p = false;
10787 decl = build_decl (input_location,
10788 FIELD_DECL, unqualified_id, type);
10789 DECL_NONADDRESSABLE_P (decl) = bitfield;
10790 if (bitfield && !unqualified_id)
10791 TREE_NO_WARNING (decl) = 1;
10793 if (storage_class == sc_mutable)
10795 DECL_MUTABLE_P (decl) = 1;
10796 storage_class = sc_none;
10799 if (initialized)
10801 /* An attempt is being made to initialize a non-static
10802 member. This is new in C++11. */
10803 maybe_warn_cpp0x (CPP0X_NSDMI);
10805 /* If this has been parsed with static storage class, but
10806 errors forced staticp to be cleared, ensure NSDMI is
10807 not present. */
10808 if (declspecs->storage_class == sc_static)
10809 DECL_INITIAL (decl) = error_mark_node;
10813 bad_specifiers (decl, BSP_FIELD, virtualp,
10814 memfn_quals != TYPE_UNQUALIFIED,
10815 inlinep, friendp, raises != NULL_TREE);
10818 else if (TREE_CODE (type) == FUNCTION_TYPE
10819 || TREE_CODE (type) == METHOD_TYPE)
10821 tree original_name;
10822 int publicp = 0;
10824 if (!unqualified_id)
10825 return error_mark_node;
10827 if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
10828 original_name = dname;
10829 else
10830 original_name = unqualified_id;
10832 if (storage_class == sc_auto)
10833 error ("storage class %<auto%> invalid for function %qs", name);
10834 else if (storage_class == sc_register)
10835 error ("storage class %<register%> invalid for function %qs", name);
10836 else if (thread_p)
10838 if (declspecs->gnu_thread_keyword_p)
10839 error ("storage class %<__thread%> invalid for function %qs",
10840 name);
10841 else
10842 error ("storage class %<thread_local%> invalid for function %qs",
10843 name);
10846 if (virt_specifiers)
10847 error ("virt-specifiers in %qs not allowed outside a class definition", name);
10848 /* Function declaration not at top level.
10849 Storage classes other than `extern' are not allowed
10850 and `extern' makes no difference. */
10851 if (! toplevel_bindings_p ()
10852 && (storage_class == sc_static
10853 || decl_spec_seq_has_spec_p (declspecs, ds_inline))
10854 && pedantic)
10856 if (storage_class == sc_static)
10857 pedwarn (input_location, OPT_Wpedantic,
10858 "%<static%> specified invalid for function %qs "
10859 "declared out of global scope", name);
10860 else
10861 pedwarn (input_location, OPT_Wpedantic,
10862 "%<inline%> specifier invalid for function %qs "
10863 "declared out of global scope", name);
10866 if (ctype == NULL_TREE)
10868 if (virtualp)
10870 error ("virtual non-class function %qs", name);
10871 virtualp = 0;
10873 else if (sfk == sfk_constructor
10874 || sfk == sfk_destructor)
10876 error (funcdef_flag
10877 ? G_("%qs defined in a non-class scope")
10878 : G_("%qs declared in a non-class scope"), name);
10879 sfk = sfk_none;
10883 /* Record whether the function is public. */
10884 publicp = (ctype != NULL_TREE
10885 || storage_class != sc_static);
10887 if (late_return_type_p)
10888 TYPE_HAS_LATE_RETURN_TYPE (type) = 1;
10890 decl = grokfndecl (ctype, type, original_name, parms, unqualified_id,
10891 virtualp, flags, memfn_quals, rqual, raises,
10892 1, friendp,
10893 publicp, inlinep | (2 * constexpr_p), sfk,
10894 funcdef_flag,
10895 template_count, in_namespace, attrlist,
10896 declarator->id_loc);
10897 if (decl == NULL_TREE)
10898 return error_mark_node;
10900 if (staticp == 1)
10902 int invalid_static = 0;
10904 /* Don't allow a static member function in a class, and forbid
10905 declaring main to be static. */
10906 if (TREE_CODE (type) == METHOD_TYPE)
10908 permerror (input_location, "cannot declare member function %qD to have "
10909 "static linkage", decl);
10910 invalid_static = 1;
10912 else if (current_function_decl)
10914 /* FIXME need arm citation */
10915 error ("cannot declare static function inside another function");
10916 invalid_static = 1;
10919 if (invalid_static)
10921 staticp = 0;
10922 storage_class = sc_none;
10926 else
10928 /* It's a variable. */
10930 /* An uninitialized decl with `extern' is a reference. */
10931 decl = grokvardecl (type, unqualified_id,
10932 declspecs,
10933 initialized,
10934 (type_quals & TYPE_QUAL_CONST) != 0,
10935 ctype ? ctype : in_namespace);
10936 bad_specifiers (decl, BSP_VAR, virtualp,
10937 memfn_quals != TYPE_UNQUALIFIED,
10938 inlinep, friendp, raises != NULL_TREE);
10940 if (ctype)
10942 DECL_CONTEXT (decl) = ctype;
10943 if (staticp == 1)
10945 permerror (input_location, "%<static%> may not be used when defining "
10946 "(as opposed to declaring) a static data member");
10947 staticp = 0;
10948 storage_class = sc_none;
10950 if (storage_class == sc_register && TREE_STATIC (decl))
10952 error ("static member %qD declared %<register%>", decl);
10953 storage_class = sc_none;
10955 if (storage_class == sc_extern && pedantic)
10957 pedwarn (input_location, OPT_Wpedantic,
10958 "cannot explicitly declare member %q#D to have "
10959 "extern linkage", decl);
10960 storage_class = sc_none;
10963 else if (constexpr_p && DECL_EXTERNAL (decl))
10965 error ("declaration of constexpr variable %qD is not a definition",
10966 decl);
10967 constexpr_p = false;
10971 if (storage_class == sc_extern && initialized && !funcdef_flag)
10973 if (toplevel_bindings_p ())
10975 /* It's common practice (and completely valid) to have a const
10976 be initialized and declared extern. */
10977 if (!(type_quals & TYPE_QUAL_CONST))
10978 warning (0, "%qs initialized and declared %<extern%>", name);
10980 else
10982 error ("%qs has both %<extern%> and initializer", name);
10983 return error_mark_node;
10987 /* Record `register' declaration for warnings on &
10988 and in case doing stupid register allocation. */
10990 if (storage_class == sc_register)
10991 DECL_REGISTER (decl) = 1;
10992 else if (storage_class == sc_extern)
10993 DECL_THIS_EXTERN (decl) = 1;
10994 else if (storage_class == sc_static)
10995 DECL_THIS_STATIC (decl) = 1;
10997 /* Set constexpr flag on vars (functions got it in grokfndecl). */
10998 if (constexpr_p && VAR_P (decl))
10999 DECL_DECLARED_CONSTEXPR_P (decl) = true;
11001 /* Record constancy and volatility on the DECL itself . There's
11002 no need to do this when processing a template; we'll do this
11003 for the instantiated declaration based on the type of DECL. */
11004 if (!processing_template_decl)
11005 cp_apply_type_quals_to_decl (type_quals, decl);
11007 return decl;
11011 /* Subroutine of start_function. Ensure that each of the parameter
11012 types (as listed in PARMS) is complete, as is required for a
11013 function definition. */
11015 static void
11016 require_complete_types_for_parms (tree parms)
11018 for (; parms; parms = DECL_CHAIN (parms))
11020 if (dependent_type_p (TREE_TYPE (parms)))
11021 continue;
11022 if (!VOID_TYPE_P (TREE_TYPE (parms))
11023 && complete_type_or_else (TREE_TYPE (parms), parms))
11025 relayout_decl (parms);
11026 DECL_ARG_TYPE (parms) = type_passed_as (TREE_TYPE (parms));
11028 else
11029 /* grokparms or complete_type_or_else will have already issued
11030 an error. */
11031 TREE_TYPE (parms) = error_mark_node;
11035 /* Returns nonzero if T is a local variable. */
11038 local_variable_p (const_tree t)
11040 if ((VAR_P (t)
11041 /* A VAR_DECL with a context that is a _TYPE is a static data
11042 member. */
11043 && !TYPE_P (CP_DECL_CONTEXT (t))
11044 /* Any other non-local variable must be at namespace scope. */
11045 && !DECL_NAMESPACE_SCOPE_P (t))
11046 || (TREE_CODE (t) == PARM_DECL))
11047 return 1;
11049 return 0;
11052 /* Like local_variable_p, but suitable for use as a tree-walking
11053 function. */
11055 static tree
11056 local_variable_p_walkfn (tree *tp, int *walk_subtrees,
11057 void * /*data*/)
11059 if (local_variable_p (*tp)
11060 && (!DECL_ARTIFICIAL (*tp) || DECL_NAME (*tp) == this_identifier))
11061 return *tp;
11062 else if (TYPE_P (*tp))
11063 *walk_subtrees = 0;
11065 return NULL_TREE;
11068 /* Check that ARG, which is a default-argument expression for a
11069 parameter DECL, is valid. Returns ARG, or ERROR_MARK_NODE, if
11070 something goes wrong. DECL may also be a _TYPE node, rather than a
11071 DECL, if there is no DECL available. */
11073 tree
11074 check_default_argument (tree decl, tree arg, tsubst_flags_t complain)
11076 tree var;
11077 tree decl_type;
11079 if (TREE_CODE (arg) == DEFAULT_ARG)
11080 /* We get a DEFAULT_ARG when looking at an in-class declaration
11081 with a default argument. Ignore the argument for now; we'll
11082 deal with it after the class is complete. */
11083 return arg;
11085 if (TYPE_P (decl))
11087 decl_type = decl;
11088 decl = NULL_TREE;
11090 else
11091 decl_type = TREE_TYPE (decl);
11093 if (arg == error_mark_node
11094 || decl == error_mark_node
11095 || TREE_TYPE (arg) == error_mark_node
11096 || decl_type == error_mark_node)
11097 /* Something already went wrong. There's no need to check
11098 further. */
11099 return error_mark_node;
11101 /* [dcl.fct.default]
11103 A default argument expression is implicitly converted to the
11104 parameter type. */
11105 ++cp_unevaluated_operand;
11106 perform_implicit_conversion_flags (decl_type, arg, complain,
11107 LOOKUP_IMPLICIT);
11108 --cp_unevaluated_operand;
11110 if (warn_zero_as_null_pointer_constant
11111 && TYPE_PTR_OR_PTRMEM_P (decl_type)
11112 && null_ptr_cst_p (arg)
11113 && (complain & tf_warning)
11114 && maybe_warn_zero_as_null_pointer_constant (arg, input_location))
11115 return nullptr_node;
11117 /* [dcl.fct.default]
11119 Local variables shall not be used in default argument
11120 expressions.
11122 The keyword `this' shall not be used in a default argument of a
11123 member function. */
11124 var = cp_walk_tree_without_duplicates (&arg, local_variable_p_walkfn, NULL);
11125 if (var)
11127 if (complain & tf_warning_or_error)
11129 if (DECL_NAME (var) == this_identifier)
11130 permerror (input_location, "default argument %qE uses %qD",
11131 arg, var);
11132 else
11133 error ("default argument %qE uses local variable %qD", arg, var);
11135 return error_mark_node;
11138 /* All is well. */
11139 return arg;
11142 /* Returns a deprecated type used within TYPE, or NULL_TREE if none. */
11144 static tree
11145 type_is_deprecated (tree type)
11147 enum tree_code code;
11148 if (TREE_DEPRECATED (type))
11149 return type;
11150 if (TYPE_NAME (type)
11151 && TREE_DEPRECATED (TYPE_NAME (type)))
11152 return type;
11154 /* Do warn about using typedefs to a deprecated class. */
11155 if (OVERLOAD_TYPE_P (type) && type != TYPE_MAIN_VARIANT (type))
11156 return type_is_deprecated (TYPE_MAIN_VARIANT (type));
11158 code = TREE_CODE (type);
11160 if (code == POINTER_TYPE || code == REFERENCE_TYPE
11161 || code == OFFSET_TYPE || code == FUNCTION_TYPE
11162 || code == METHOD_TYPE || code == ARRAY_TYPE)
11163 return type_is_deprecated (TREE_TYPE (type));
11165 if (TYPE_PTRMEMFUNC_P (type))
11166 return type_is_deprecated
11167 (TREE_TYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type))));
11169 return NULL_TREE;
11172 /* Decode the list of parameter types for a function type.
11173 Given the list of things declared inside the parens,
11174 return a list of types.
11176 If this parameter does not end with an ellipsis, we append
11177 void_list_node.
11179 *PARMS is set to the chain of PARM_DECLs created. */
11181 static tree
11182 grokparms (tree parmlist, tree *parms)
11184 tree result = NULL_TREE;
11185 tree decls = NULL_TREE;
11186 tree parm;
11187 int any_error = 0;
11189 for (parm = parmlist; parm != NULL_TREE; parm = TREE_CHAIN (parm))
11191 tree type = NULL_TREE;
11192 tree init = TREE_PURPOSE (parm);
11193 tree decl = TREE_VALUE (parm);
11194 const char *errmsg;
11196 if (parm == void_list_node)
11197 break;
11199 if (! decl || TREE_TYPE (decl) == error_mark_node)
11200 continue;
11202 type = TREE_TYPE (decl);
11203 if (VOID_TYPE_P (type))
11205 if (same_type_p (type, void_type_node)
11206 && !init
11207 && !DECL_NAME (decl) && !result
11208 && TREE_CHAIN (parm) == void_list_node)
11209 /* DR 577: A parameter list consisting of a single
11210 unnamed parameter of non-dependent type 'void'. */
11211 break;
11212 else if (cv_qualified_p (type))
11213 error_at (DECL_SOURCE_LOCATION (decl),
11214 "invalid use of cv-qualified type %qT in "
11215 "parameter declaration", type);
11216 else
11217 error_at (DECL_SOURCE_LOCATION (decl),
11218 "invalid use of type %<void%> in parameter "
11219 "declaration");
11220 /* It's not a good idea to actually create parameters of
11221 type `void'; other parts of the compiler assume that a
11222 void type terminates the parameter list. */
11223 type = error_mark_node;
11224 TREE_TYPE (decl) = error_mark_node;
11227 if (type != error_mark_node
11228 && TYPE_FOR_JAVA (type)
11229 && MAYBE_CLASS_TYPE_P (type))
11231 error ("parameter %qD has Java class type", decl);
11232 type = error_mark_node;
11233 TREE_TYPE (decl) = error_mark_node;
11234 init = NULL_TREE;
11237 if (type != error_mark_node
11238 && (errmsg = targetm.invalid_parameter_type (type)))
11240 error (errmsg);
11241 type = error_mark_node;
11242 TREE_TYPE (decl) = error_mark_node;
11245 if (type != error_mark_node)
11247 if (deprecated_state != DEPRECATED_SUPPRESS)
11249 tree deptype = type_is_deprecated (type);
11250 if (deptype)
11251 warn_deprecated_use (deptype, NULL_TREE);
11254 /* Top-level qualifiers on the parameters are
11255 ignored for function types. */
11256 type = cp_build_qualified_type (type, 0);
11257 if (TREE_CODE (type) == METHOD_TYPE)
11259 error ("parameter %qD invalidly declared method type", decl);
11260 type = build_pointer_type (type);
11261 TREE_TYPE (decl) = type;
11263 else if (abstract_virtuals_error (decl, type))
11264 any_error = 1; /* Seems like a good idea. */
11265 else if (POINTER_TYPE_P (type))
11267 /* [dcl.fct]/6, parameter types cannot contain pointers
11268 (references) to arrays of unknown bound. */
11269 tree t = TREE_TYPE (type);
11270 int ptr = TYPE_PTR_P (type);
11272 while (1)
11274 if (TYPE_PTR_P (t))
11275 ptr = 1;
11276 else if (TREE_CODE (t) != ARRAY_TYPE)
11277 break;
11278 else if (!TYPE_DOMAIN (t))
11279 break;
11280 t = TREE_TYPE (t);
11282 if (TREE_CODE (t) == ARRAY_TYPE)
11283 error (ptr
11284 ? G_("parameter %qD includes pointer to array of "
11285 "unknown bound %qT")
11286 : G_("parameter %qD includes reference to array of "
11287 "unknown bound %qT"),
11288 decl, t);
11291 if (any_error)
11292 init = NULL_TREE;
11293 else if (init && !processing_template_decl)
11294 init = check_default_argument (decl, init, tf_warning_or_error);
11297 DECL_CHAIN (decl) = decls;
11298 decls = decl;
11299 result = tree_cons (init, type, result);
11301 decls = nreverse (decls);
11302 result = nreverse (result);
11303 if (parm)
11304 result = chainon (result, void_list_node);
11305 *parms = decls;
11307 return result;
11311 /* D is a constructor or overloaded `operator='.
11313 Let T be the class in which D is declared. Then, this function
11314 returns:
11316 -1 if D's is an ill-formed constructor or copy assignment operator
11317 whose first parameter is of type `T'.
11318 0 if D is not a copy constructor or copy assignment
11319 operator.
11320 1 if D is a copy constructor or copy assignment operator whose
11321 first parameter is a reference to non-const qualified T.
11322 2 if D is a copy constructor or copy assignment operator whose
11323 first parameter is a reference to const qualified T.
11325 This function can be used as a predicate. Positive values indicate
11326 a copy constructor and nonzero values indicate a copy assignment
11327 operator. */
11330 copy_fn_p (const_tree d)
11332 tree args;
11333 tree arg_type;
11334 int result = 1;
11336 gcc_assert (DECL_FUNCTION_MEMBER_P (d));
11338 if (TREE_CODE (d) == TEMPLATE_DECL
11339 || (DECL_TEMPLATE_INFO (d)
11340 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d))))
11341 /* Instantiations of template member functions are never copy
11342 functions. Note that member functions of templated classes are
11343 represented as template functions internally, and we must
11344 accept those as copy functions. */
11345 return 0;
11347 args = FUNCTION_FIRST_USER_PARMTYPE (d);
11348 if (!args)
11349 return 0;
11351 arg_type = TREE_VALUE (args);
11352 if (arg_type == error_mark_node)
11353 return 0;
11355 if (TYPE_MAIN_VARIANT (arg_type) == DECL_CONTEXT (d))
11357 /* Pass by value copy assignment operator. */
11358 result = -1;
11360 else if (TREE_CODE (arg_type) == REFERENCE_TYPE
11361 && !TYPE_REF_IS_RVALUE (arg_type)
11362 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)) == DECL_CONTEXT (d))
11364 if (CP_TYPE_CONST_P (TREE_TYPE (arg_type)))
11365 result = 2;
11367 else
11368 return 0;
11370 args = TREE_CHAIN (args);
11372 if (args && args != void_list_node && !TREE_PURPOSE (args))
11373 /* There are more non-optional args. */
11374 return 0;
11376 return result;
11379 /* D is a constructor or overloaded `operator='.
11381 Let T be the class in which D is declared. Then, this function
11382 returns true when D is a move constructor or move assignment
11383 operator, false otherwise. */
11385 bool
11386 move_fn_p (const_tree d)
11388 gcc_assert (DECL_FUNCTION_MEMBER_P (d));
11390 if (cxx_dialect == cxx98)
11391 /* There are no move constructors if we are in C++98 mode. */
11392 return false;
11394 if (TREE_CODE (d) == TEMPLATE_DECL
11395 || (DECL_TEMPLATE_INFO (d)
11396 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d))))
11397 /* Instantiations of template member functions are never move
11398 functions. Note that member functions of templated classes are
11399 represented as template functions internally, and we must
11400 accept those as move functions. */
11401 return 0;
11403 return move_signature_fn_p (d);
11406 /* D is a constructor or overloaded `operator='.
11408 Then, this function returns true when D has the same signature as a move
11409 constructor or move assignment operator (because either it is such a
11410 ctor/op= or it is a template specialization with the same signature),
11411 false otherwise. */
11413 bool
11414 move_signature_fn_p (const_tree d)
11416 tree args;
11417 tree arg_type;
11418 bool result = false;
11420 args = FUNCTION_FIRST_USER_PARMTYPE (d);
11421 if (!args)
11422 return 0;
11424 arg_type = TREE_VALUE (args);
11425 if (arg_type == error_mark_node)
11426 return 0;
11428 if (TREE_CODE (arg_type) == REFERENCE_TYPE
11429 && TYPE_REF_IS_RVALUE (arg_type)
11430 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)),
11431 DECL_CONTEXT (d)))
11432 result = true;
11434 args = TREE_CHAIN (args);
11436 if (args && args != void_list_node && !TREE_PURPOSE (args))
11437 /* There are more non-optional args. */
11438 return false;
11440 return result;
11443 /* Remember any special properties of member function DECL. */
11445 void
11446 grok_special_member_properties (tree decl)
11448 tree class_type;
11450 if (!DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
11451 return;
11453 class_type = DECL_CONTEXT (decl);
11454 if (DECL_CONSTRUCTOR_P (decl))
11456 int ctor = copy_fn_p (decl);
11458 if (!DECL_ARTIFICIAL (decl))
11459 TYPE_HAS_USER_CONSTRUCTOR (class_type) = 1;
11461 if (ctor > 0)
11463 /* [class.copy]
11465 A non-template constructor for class X is a copy
11466 constructor if its first parameter is of type X&, const
11467 X&, volatile X& or const volatile X&, and either there
11468 are no other parameters or else all other parameters have
11469 default arguments. */
11470 TYPE_HAS_COPY_CTOR (class_type) = 1;
11471 if (user_provided_p (decl))
11472 TYPE_HAS_COMPLEX_COPY_CTOR (class_type) = 1;
11473 if (ctor > 1)
11474 TYPE_HAS_CONST_COPY_CTOR (class_type) = 1;
11476 else if (sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (decl)))
11478 TYPE_HAS_DEFAULT_CONSTRUCTOR (class_type) = 1;
11479 if (user_provided_p (decl))
11480 TYPE_HAS_COMPLEX_DFLT (class_type) = 1;
11482 else if (move_fn_p (decl) && user_provided_p (decl))
11483 TYPE_HAS_COMPLEX_MOVE_CTOR (class_type) = 1;
11484 else if (is_list_ctor (decl))
11485 TYPE_HAS_LIST_CTOR (class_type) = 1;
11487 if (DECL_DECLARED_CONSTEXPR_P (decl)
11488 && !copy_fn_p (decl) && !move_fn_p (decl))
11489 TYPE_HAS_CONSTEXPR_CTOR (class_type) = 1;
11491 else if (DECL_OVERLOADED_OPERATOR_P (decl) == NOP_EXPR)
11493 /* [class.copy]
11495 A non-template assignment operator for class X is a copy
11496 assignment operator if its parameter is of type X, X&, const
11497 X&, volatile X& or const volatile X&. */
11499 int assop = copy_fn_p (decl);
11501 if (assop)
11503 TYPE_HAS_COPY_ASSIGN (class_type) = 1;
11504 if (user_provided_p (decl))
11505 TYPE_HAS_COMPLEX_COPY_ASSIGN (class_type) = 1;
11506 if (assop != 1)
11507 TYPE_HAS_CONST_COPY_ASSIGN (class_type) = 1;
11509 else if (move_fn_p (decl) && user_provided_p (decl))
11510 TYPE_HAS_COMPLEX_MOVE_ASSIGN (class_type) = 1;
11512 /* Destructors are handled in check_methods. */
11515 /* Check a constructor DECL has the correct form. Complains
11516 if the class has a constructor of the form X(X). */
11519 grok_ctor_properties (const_tree ctype, const_tree decl)
11521 int ctor_parm = copy_fn_p (decl);
11523 if (ctor_parm < 0)
11525 /* [class.copy]
11527 A declaration of a constructor for a class X is ill-formed if
11528 its first parameter is of type (optionally cv-qualified) X
11529 and either there are no other parameters or else all other
11530 parameters have default arguments.
11532 We *don't* complain about member template instantiations that
11533 have this form, though; they can occur as we try to decide
11534 what constructor to use during overload resolution. Since
11535 overload resolution will never prefer such a constructor to
11536 the non-template copy constructor (which is either explicitly
11537 or implicitly defined), there's no need to worry about their
11538 existence. Theoretically, they should never even be
11539 instantiated, but that's hard to forestall. */
11540 error ("invalid constructor; you probably meant %<%T (const %T&)%>",
11541 ctype, ctype);
11542 return 0;
11545 return 1;
11548 /* An operator with this code is unary, but can also be binary. */
11550 static int
11551 ambi_op_p (enum tree_code code)
11553 return (code == INDIRECT_REF
11554 || code == ADDR_EXPR
11555 || code == UNARY_PLUS_EXPR
11556 || code == NEGATE_EXPR
11557 || code == PREINCREMENT_EXPR
11558 || code == PREDECREMENT_EXPR);
11561 /* An operator with this name can only be unary. */
11563 static int
11564 unary_op_p (enum tree_code code)
11566 return (code == TRUTH_NOT_EXPR
11567 || code == BIT_NOT_EXPR
11568 || code == COMPONENT_REF
11569 || code == TYPE_EXPR);
11572 /* DECL is a declaration for an overloaded operator. If COMPLAIN is true,
11573 errors are issued for invalid declarations. */
11575 bool
11576 grok_op_properties (tree decl, bool complain)
11578 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
11579 tree argtype;
11580 int methodp = (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE);
11581 tree name = DECL_NAME (decl);
11582 enum tree_code operator_code;
11583 int arity;
11584 bool ellipsis_p;
11585 tree class_type;
11587 /* Count the number of arguments and check for ellipsis. */
11588 for (argtype = argtypes, arity = 0;
11589 argtype && argtype != void_list_node;
11590 argtype = TREE_CHAIN (argtype))
11591 ++arity;
11592 ellipsis_p = !argtype;
11594 class_type = DECL_CONTEXT (decl);
11595 if (class_type && !CLASS_TYPE_P (class_type))
11596 class_type = NULL_TREE;
11598 if (DECL_CONV_FN_P (decl))
11599 operator_code = TYPE_EXPR;
11600 else
11603 #define DEF_OPERATOR(NAME, CODE, MANGLING, ARITY, ASSN_P) \
11604 if (ansi_opname (CODE) == name) \
11606 operator_code = (CODE); \
11607 break; \
11609 else if (ansi_assopname (CODE) == name) \
11611 operator_code = (CODE); \
11612 DECL_ASSIGNMENT_OPERATOR_P (decl) = 1; \
11613 break; \
11616 #include "operators.def"
11617 #undef DEF_OPERATOR
11619 gcc_unreachable ();
11621 while (0);
11622 gcc_assert (operator_code != MAX_TREE_CODES);
11623 SET_OVERLOADED_OPERATOR_CODE (decl, operator_code);
11625 if (class_type)
11626 switch (operator_code)
11628 case NEW_EXPR:
11629 TYPE_HAS_NEW_OPERATOR (class_type) = 1;
11630 break;
11632 case DELETE_EXPR:
11633 TYPE_GETS_DELETE (class_type) |= 1;
11634 break;
11636 case VEC_NEW_EXPR:
11637 TYPE_HAS_ARRAY_NEW_OPERATOR (class_type) = 1;
11638 break;
11640 case VEC_DELETE_EXPR:
11641 TYPE_GETS_DELETE (class_type) |= 2;
11642 break;
11644 default:
11645 break;
11648 /* [basic.std.dynamic.allocation]/1:
11650 A program is ill-formed if an allocation function is declared
11651 in a namespace scope other than global scope or declared static
11652 in global scope.
11654 The same also holds true for deallocation functions. */
11655 if (operator_code == NEW_EXPR || operator_code == VEC_NEW_EXPR
11656 || operator_code == DELETE_EXPR || operator_code == VEC_DELETE_EXPR)
11658 if (DECL_NAMESPACE_SCOPE_P (decl))
11660 if (CP_DECL_CONTEXT (decl) != global_namespace)
11662 error ("%qD may not be declared within a namespace", decl);
11663 return false;
11665 else if (!TREE_PUBLIC (decl))
11667 error ("%qD may not be declared as static", decl);
11668 return false;
11673 if (operator_code == NEW_EXPR || operator_code == VEC_NEW_EXPR)
11675 TREE_TYPE (decl) = coerce_new_type (TREE_TYPE (decl));
11676 DECL_IS_OPERATOR_NEW (decl) = 1;
11678 else if (operator_code == DELETE_EXPR || operator_code == VEC_DELETE_EXPR)
11679 TREE_TYPE (decl) = coerce_delete_type (TREE_TYPE (decl));
11680 else
11682 /* An operator function must either be a non-static member function
11683 or have at least one parameter of a class, a reference to a class,
11684 an enumeration, or a reference to an enumeration. 13.4.0.6 */
11685 if (! methodp || DECL_STATIC_FUNCTION_P (decl))
11687 if (operator_code == TYPE_EXPR
11688 || operator_code == CALL_EXPR
11689 || operator_code == COMPONENT_REF
11690 || operator_code == ARRAY_REF
11691 || operator_code == NOP_EXPR)
11693 error ("%qD must be a nonstatic member function", decl);
11694 return false;
11696 else
11698 tree p;
11700 if (DECL_STATIC_FUNCTION_P (decl))
11702 error ("%qD must be either a non-static member "
11703 "function or a non-member function", decl);
11704 return false;
11707 for (p = argtypes; p && p != void_list_node; p = TREE_CHAIN (p))
11709 tree arg = non_reference (TREE_VALUE (p));
11710 if (arg == error_mark_node)
11711 return false;
11713 /* MAYBE_CLASS_TYPE_P, rather than CLASS_TYPE_P, is used
11714 because these checks are performed even on
11715 template functions. */
11716 if (MAYBE_CLASS_TYPE_P (arg)
11717 || TREE_CODE (arg) == ENUMERAL_TYPE)
11718 break;
11721 if (!p || p == void_list_node)
11723 if (complain)
11724 error ("%qD must have an argument of class or "
11725 "enumerated type", decl);
11726 return false;
11731 /* There are no restrictions on the arguments to an overloaded
11732 "operator ()". */
11733 if (operator_code == CALL_EXPR)
11734 return true;
11736 /* Warn about conversion operators that will never be used. */
11737 if (IDENTIFIER_TYPENAME_P (name)
11738 && ! DECL_TEMPLATE_INFO (decl)
11739 && warn_conversion
11740 /* Warn only declaring the function; there is no need to
11741 warn again about out-of-class definitions. */
11742 && class_type == current_class_type)
11744 tree t = TREE_TYPE (name);
11745 int ref = (TREE_CODE (t) == REFERENCE_TYPE);
11747 if (ref)
11748 t = TYPE_MAIN_VARIANT (TREE_TYPE (t));
11750 if (VOID_TYPE_P (t))
11751 warning (OPT_Wconversion,
11753 ? G_("conversion to a reference to void "
11754 "will never use a type conversion operator")
11755 : G_("conversion to void "
11756 "will never use a type conversion operator"));
11757 else if (class_type)
11759 if (t == class_type)
11760 warning (OPT_Wconversion,
11762 ? G_("conversion to a reference to the same type "
11763 "will never use a type conversion operator")
11764 : G_("conversion to the same type "
11765 "will never use a type conversion operator"));
11766 /* Don't force t to be complete here. */
11767 else if (MAYBE_CLASS_TYPE_P (t)
11768 && COMPLETE_TYPE_P (t)
11769 && DERIVED_FROM_P (t, class_type))
11770 warning (OPT_Wconversion,
11772 ? G_("conversion to a reference to a base class "
11773 "will never use a type conversion operator")
11774 : G_("conversion to a base class "
11775 "will never use a type conversion operator"));
11780 if (operator_code == COND_EXPR)
11782 /* 13.4.0.3 */
11783 error ("ISO C++ prohibits overloading operator ?:");
11784 return false;
11786 else if (ellipsis_p)
11788 error ("%qD must not have variable number of arguments", decl);
11789 return false;
11791 else if (ambi_op_p (operator_code))
11793 if (arity == 1)
11794 /* We pick the one-argument operator codes by default, so
11795 we don't have to change anything. */
11797 else if (arity == 2)
11799 /* If we thought this was a unary operator, we now know
11800 it to be a binary operator. */
11801 switch (operator_code)
11803 case INDIRECT_REF:
11804 operator_code = MULT_EXPR;
11805 break;
11807 case ADDR_EXPR:
11808 operator_code = BIT_AND_EXPR;
11809 break;
11811 case UNARY_PLUS_EXPR:
11812 operator_code = PLUS_EXPR;
11813 break;
11815 case NEGATE_EXPR:
11816 operator_code = MINUS_EXPR;
11817 break;
11819 case PREINCREMENT_EXPR:
11820 operator_code = POSTINCREMENT_EXPR;
11821 break;
11823 case PREDECREMENT_EXPR:
11824 operator_code = POSTDECREMENT_EXPR;
11825 break;
11827 default:
11828 gcc_unreachable ();
11831 SET_OVERLOADED_OPERATOR_CODE (decl, operator_code);
11833 if ((operator_code == POSTINCREMENT_EXPR
11834 || operator_code == POSTDECREMENT_EXPR)
11835 && ! processing_template_decl
11836 && ! same_type_p (TREE_VALUE (TREE_CHAIN (argtypes)), integer_type_node))
11838 if (methodp)
11839 error ("postfix %qD must take %<int%> as its argument",
11840 decl);
11841 else
11842 error ("postfix %qD must take %<int%> as its second "
11843 "argument", decl);
11844 return false;
11847 else
11849 if (methodp)
11850 error ("%qD must take either zero or one argument", decl);
11851 else
11852 error ("%qD must take either one or two arguments", decl);
11853 return false;
11856 /* More Effective C++ rule 6. */
11857 if (warn_ecpp
11858 && (operator_code == POSTINCREMENT_EXPR
11859 || operator_code == POSTDECREMENT_EXPR
11860 || operator_code == PREINCREMENT_EXPR
11861 || operator_code == PREDECREMENT_EXPR))
11863 tree arg = TREE_VALUE (argtypes);
11864 tree ret = TREE_TYPE (TREE_TYPE (decl));
11865 if (methodp || TREE_CODE (arg) == REFERENCE_TYPE)
11866 arg = TREE_TYPE (arg);
11867 arg = TYPE_MAIN_VARIANT (arg);
11868 if (operator_code == PREINCREMENT_EXPR
11869 || operator_code == PREDECREMENT_EXPR)
11871 if (TREE_CODE (ret) != REFERENCE_TYPE
11872 || !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (ret)),
11873 arg))
11874 warning (OPT_Weffc__, "prefix %qD should return %qT", decl,
11875 build_reference_type (arg));
11877 else
11879 if (!same_type_p (TYPE_MAIN_VARIANT (ret), arg))
11880 warning (OPT_Weffc__, "postfix %qD should return %qT", decl, arg);
11884 else if (unary_op_p (operator_code))
11886 if (arity != 1)
11888 if (methodp)
11889 error ("%qD must take %<void%>", decl);
11890 else
11891 error ("%qD must take exactly one argument", decl);
11892 return false;
11895 else /* if (binary_op_p (operator_code)) */
11897 if (arity != 2)
11899 if (methodp)
11900 error ("%qD must take exactly one argument", decl);
11901 else
11902 error ("%qD must take exactly two arguments", decl);
11903 return false;
11906 /* More Effective C++ rule 7. */
11907 if (warn_ecpp
11908 && (operator_code == TRUTH_ANDIF_EXPR
11909 || operator_code == TRUTH_ORIF_EXPR
11910 || operator_code == COMPOUND_EXPR))
11911 warning (OPT_Weffc__, "user-defined %qD always evaluates both arguments",
11912 decl);
11915 /* Effective C++ rule 23. */
11916 if (warn_ecpp
11917 && arity == 2
11918 && !DECL_ASSIGNMENT_OPERATOR_P (decl)
11919 && (operator_code == PLUS_EXPR
11920 || operator_code == MINUS_EXPR
11921 || operator_code == TRUNC_DIV_EXPR
11922 || operator_code == MULT_EXPR
11923 || operator_code == TRUNC_MOD_EXPR)
11924 && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == REFERENCE_TYPE)
11925 warning (OPT_Weffc__, "%qD should return by value", decl);
11927 /* [over.oper]/8 */
11928 for (; argtypes && argtypes != void_list_node;
11929 argtypes = TREE_CHAIN (argtypes))
11930 if (TREE_PURPOSE (argtypes))
11932 TREE_PURPOSE (argtypes) = NULL_TREE;
11933 if (operator_code == POSTINCREMENT_EXPR
11934 || operator_code == POSTDECREMENT_EXPR)
11936 pedwarn (input_location, OPT_Wpedantic, "%qD cannot have default arguments",
11937 decl);
11939 else
11941 error ("%qD cannot have default arguments", decl);
11942 return false;
11946 return true;
11949 /* Return a string giving the keyword associate with CODE. */
11951 static const char *
11952 tag_name (enum tag_types code)
11954 switch (code)
11956 case record_type:
11957 return "struct";
11958 case class_type:
11959 return "class";
11960 case union_type:
11961 return "union";
11962 case enum_type:
11963 return "enum";
11964 case typename_type:
11965 return "typename";
11966 default:
11967 gcc_unreachable ();
11971 /* Name lookup in an elaborated-type-specifier (after the keyword
11972 indicated by TAG_CODE) has found the TYPE_DECL DECL. If the
11973 elaborated-type-specifier is invalid, issue a diagnostic and return
11974 error_mark_node; otherwise, return the *_TYPE to which it referred.
11975 If ALLOW_TEMPLATE_P is true, TYPE may be a class template. */
11977 tree
11978 check_elaborated_type_specifier (enum tag_types tag_code,
11979 tree decl,
11980 bool allow_template_p)
11982 tree type;
11984 /* In the case of:
11986 struct S { struct S *p; };
11988 name lookup will find the TYPE_DECL for the implicit "S::S"
11989 typedef. Adjust for that here. */
11990 if (DECL_SELF_REFERENCE_P (decl))
11991 decl = TYPE_NAME (TREE_TYPE (decl));
11993 type = TREE_TYPE (decl);
11995 /* Check TEMPLATE_TYPE_PARM first because DECL_IMPLICIT_TYPEDEF_P
11996 is false for this case as well. */
11997 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
11999 error ("using template type parameter %qT after %qs",
12000 type, tag_name (tag_code));
12001 return error_mark_node;
12003 /* Accept template template parameters. */
12004 else if (allow_template_p
12005 && (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM
12006 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM))
12008 /* [dcl.type.elab]
12010 If the identifier resolves to a typedef-name or the
12011 simple-template-id resolves to an alias template
12012 specialization, the elaborated-type-specifier is ill-formed.
12014 In other words, the only legitimate declaration to use in the
12015 elaborated type specifier is the implicit typedef created when
12016 the type is declared. */
12017 else if (!DECL_IMPLICIT_TYPEDEF_P (decl)
12018 && !DECL_SELF_REFERENCE_P (decl)
12019 && tag_code != typename_type)
12021 if (alias_template_specialization_p (type))
12022 error ("using alias template specialization %qT after %qs",
12023 type, tag_name (tag_code));
12024 else
12025 error ("using typedef-name %qD after %qs", decl, tag_name (tag_code));
12026 inform (DECL_SOURCE_LOCATION (decl),
12027 "%qD has a previous declaration here", decl);
12028 return error_mark_node;
12030 else if (TREE_CODE (type) != RECORD_TYPE
12031 && TREE_CODE (type) != UNION_TYPE
12032 && tag_code != enum_type
12033 && tag_code != typename_type)
12035 error ("%qT referred to as %qs", type, tag_name (tag_code));
12036 inform (input_location, "%q+T has a previous declaration here", type);
12037 return error_mark_node;
12039 else if (TREE_CODE (type) != ENUMERAL_TYPE
12040 && tag_code == enum_type)
12042 error ("%qT referred to as enum", type);
12043 inform (input_location, "%q+T has a previous declaration here", type);
12044 return error_mark_node;
12046 else if (!allow_template_p
12047 && TREE_CODE (type) == RECORD_TYPE
12048 && CLASSTYPE_IS_TEMPLATE (type))
12050 /* If a class template appears as elaborated type specifier
12051 without a template header such as:
12053 template <class T> class C {};
12054 void f(class C); // No template header here
12056 then the required template argument is missing. */
12057 error ("template argument required for %<%s %T%>",
12058 tag_name (tag_code),
12059 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)));
12060 return error_mark_node;
12063 return type;
12066 /* Lookup NAME in elaborate type specifier in scope according to
12067 SCOPE and issue diagnostics if necessary.
12068 Return *_TYPE node upon success, NULL_TREE when the NAME is not
12069 found, and ERROR_MARK_NODE for type error. */
12071 static tree
12072 lookup_and_check_tag (enum tag_types tag_code, tree name,
12073 tag_scope scope, bool template_header_p)
12075 tree t;
12076 tree decl;
12077 if (scope == ts_global)
12079 /* First try ordinary name lookup, ignoring hidden class name
12080 injected via friend declaration. */
12081 decl = lookup_name_prefer_type (name, 2);
12082 /* If that fails, the name will be placed in the smallest
12083 non-class, non-function-prototype scope according to 3.3.1/5.
12084 We may already have a hidden name declared as friend in this
12085 scope. So lookup again but not ignoring hidden names.
12086 If we find one, that name will be made visible rather than
12087 creating a new tag. */
12088 if (!decl)
12089 decl = lookup_type_scope (name, ts_within_enclosing_non_class);
12091 else
12092 decl = lookup_type_scope (name, scope);
12094 if (decl
12095 && (DECL_CLASS_TEMPLATE_P (decl)
12096 /* If scope is ts_current we're defining a class, so ignore a
12097 template template parameter. */
12098 || (scope != ts_current
12099 && DECL_TEMPLATE_TEMPLATE_PARM_P (decl))))
12100 decl = DECL_TEMPLATE_RESULT (decl);
12102 if (decl && TREE_CODE (decl) == TYPE_DECL)
12104 /* Look for invalid nested type:
12105 class C {
12106 class C {};
12107 }; */
12108 if (scope == ts_current && DECL_SELF_REFERENCE_P (decl))
12110 error ("%qD has the same name as the class in which it is "
12111 "declared",
12112 decl);
12113 return error_mark_node;
12116 /* Two cases we need to consider when deciding if a class
12117 template is allowed as an elaborated type specifier:
12118 1. It is a self reference to its own class.
12119 2. It comes with a template header.
12121 For example:
12123 template <class T> class C {
12124 class C *c1; // DECL_SELF_REFERENCE_P is true
12125 class D;
12127 template <class U> class C; // template_header_p is true
12128 template <class T> class C<T>::D {
12129 class C *c2; // DECL_SELF_REFERENCE_P is true
12130 }; */
12132 t = check_elaborated_type_specifier (tag_code,
12133 decl,
12134 template_header_p
12135 | DECL_SELF_REFERENCE_P (decl));
12136 return t;
12138 else if (decl && TREE_CODE (decl) == TREE_LIST)
12140 error ("reference to %qD is ambiguous", name);
12141 print_candidates (decl);
12142 return error_mark_node;
12144 else
12145 return NULL_TREE;
12148 /* Get the struct, enum or union (TAG_CODE says which) with tag NAME.
12149 Define the tag as a forward-reference if it is not defined.
12151 If a declaration is given, process it here, and report an error if
12152 multiple declarations are not identical.
12154 SCOPE is TS_CURRENT when this is also a definition. Only look in
12155 the current frame for the name (since C++ allows new names in any
12156 scope.) It is TS_WITHIN_ENCLOSING_NON_CLASS if this is a friend
12157 declaration. Only look beginning from the current scope outward up
12158 till the nearest non-class scope. Otherwise it is TS_GLOBAL.
12160 TEMPLATE_HEADER_P is true when this declaration is preceded by
12161 a set of template parameters. */
12163 static tree
12164 xref_tag_1 (enum tag_types tag_code, tree name,
12165 tag_scope orig_scope, bool template_header_p)
12167 enum tree_code code;
12168 tree t;
12169 tree context = NULL_TREE;
12170 tag_scope scope;
12172 gcc_assert (identifier_p (name));
12174 switch (tag_code)
12176 case record_type:
12177 case class_type:
12178 code = RECORD_TYPE;
12179 break;
12180 case union_type:
12181 code = UNION_TYPE;
12182 break;
12183 case enum_type:
12184 code = ENUMERAL_TYPE;
12185 break;
12186 default:
12187 gcc_unreachable ();
12190 if (orig_scope == ts_lambda)
12191 scope = ts_current;
12192 else
12193 scope = orig_scope;
12195 /* In case of anonymous name, xref_tag is only called to
12196 make type node and push name. Name lookup is not required. */
12197 if (ANON_AGGRNAME_P (name))
12198 t = NULL_TREE;
12199 else
12200 t = lookup_and_check_tag (tag_code, name,
12201 scope, template_header_p);
12203 if (t == error_mark_node)
12204 return error_mark_node;
12206 if (scope != ts_current && t && current_class_type
12207 && template_class_depth (current_class_type)
12208 && template_header_p)
12210 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
12211 return t;
12213 /* Since SCOPE is not TS_CURRENT, we are not looking at a
12214 definition of this tag. Since, in addition, we are currently
12215 processing a (member) template declaration of a template
12216 class, we must be very careful; consider:
12218 template <class X>
12219 struct S1
12221 template <class U>
12222 struct S2
12223 { template <class V>
12224 friend struct S1; };
12226 Here, the S2::S1 declaration should not be confused with the
12227 outer declaration. In particular, the inner version should
12228 have a template parameter of level 2, not level 1. This
12229 would be particularly important if the member declaration
12230 were instead:
12232 template <class V = U> friend struct S1;
12234 say, when we should tsubst into `U' when instantiating
12235 S2. On the other hand, when presented with:
12237 template <class T>
12238 struct S1 {
12239 template <class U>
12240 struct S2 {};
12241 template <class U>
12242 friend struct S2;
12245 we must find the inner binding eventually. We
12246 accomplish this by making sure that the new type we
12247 create to represent this declaration has the right
12248 TYPE_CONTEXT. */
12249 context = TYPE_CONTEXT (t);
12250 t = NULL_TREE;
12253 if (! t)
12255 /* If no such tag is yet defined, create a forward-reference node
12256 and record it as the "definition".
12257 When a real declaration of this type is found,
12258 the forward-reference will be altered into a real type. */
12259 if (code == ENUMERAL_TYPE)
12261 error ("use of enum %q#D without previous declaration", name);
12262 return error_mark_node;
12264 else
12266 t = make_class_type (code);
12267 TYPE_CONTEXT (t) = context;
12268 if (orig_scope == ts_lambda)
12269 /* Remember that we're declaring a lambda to avoid bogus errors
12270 in push_template_decl. */
12271 CLASSTYPE_LAMBDA_EXPR (t) = error_mark_node;
12272 t = pushtag (name, t, scope);
12275 else
12277 if (template_header_p && MAYBE_CLASS_TYPE_P (t))
12279 if (!redeclare_class_template (t, current_template_parms))
12280 return error_mark_node;
12282 else if (!processing_template_decl
12283 && CLASS_TYPE_P (t)
12284 && CLASSTYPE_IS_TEMPLATE (t))
12286 error ("redeclaration of %qT as a non-template", t);
12287 error ("previous declaration %q+D", t);
12288 return error_mark_node;
12291 /* Make injected friend class visible. */
12292 if (scope != ts_within_enclosing_non_class
12293 && hidden_name_p (TYPE_NAME (t)))
12295 DECL_ANTICIPATED (TYPE_NAME (t)) = 0;
12296 DECL_FRIEND_P (TYPE_NAME (t)) = 0;
12298 if (TYPE_TEMPLATE_INFO (t))
12300 DECL_ANTICIPATED (TYPE_TI_TEMPLATE (t)) = 0;
12301 DECL_FRIEND_P (TYPE_TI_TEMPLATE (t)) = 0;
12306 return t;
12309 /* Wrapper for xref_tag_1. */
12311 tree
12312 xref_tag (enum tag_types tag_code, tree name,
12313 tag_scope scope, bool template_header_p)
12315 tree ret;
12316 bool subtime;
12317 subtime = timevar_cond_start (TV_NAME_LOOKUP);
12318 ret = xref_tag_1 (tag_code, name, scope, template_header_p);
12319 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
12320 return ret;
12324 tree
12325 xref_tag_from_type (tree old, tree id, tag_scope scope)
12327 enum tag_types tag_kind;
12329 if (TREE_CODE (old) == RECORD_TYPE)
12330 tag_kind = (CLASSTYPE_DECLARED_CLASS (old) ? class_type : record_type);
12331 else
12332 tag_kind = union_type;
12334 if (id == NULL_TREE)
12335 id = TYPE_IDENTIFIER (old);
12337 return xref_tag (tag_kind, id, scope, false);
12340 /* Create the binfo hierarchy for REF with (possibly NULL) base list
12341 BASE_LIST. For each element on BASE_LIST the TREE_PURPOSE is an
12342 access_* node, and the TREE_VALUE is the type of the base-class.
12343 Non-NULL TREE_TYPE indicates virtual inheritance.
12345 Returns true if the binfo hierarchy was successfully created,
12346 false if an error was detected. */
12348 bool
12349 xref_basetypes (tree ref, tree base_list)
12351 tree *basep;
12352 tree binfo, base_binfo;
12353 unsigned max_vbases = 0; /* Maximum direct & indirect virtual bases. */
12354 unsigned max_bases = 0; /* Maximum direct bases. */
12355 int i;
12356 tree default_access;
12357 tree igo_prev; /* Track Inheritance Graph Order. */
12359 if (ref == error_mark_node)
12360 return false;
12362 /* The base of a derived class is private by default, all others are
12363 public. */
12364 default_access = (TREE_CODE (ref) == RECORD_TYPE
12365 && CLASSTYPE_DECLARED_CLASS (ref)
12366 ? access_private_node : access_public_node);
12368 /* First, make sure that any templates in base-classes are
12369 instantiated. This ensures that if we call ourselves recursively
12370 we do not get confused about which classes are marked and which
12371 are not. */
12372 basep = &base_list;
12373 while (*basep)
12375 tree basetype = TREE_VALUE (*basep);
12377 /* The dependent_type_p call below should really be dependent_scope_p
12378 so that we give a hard error about using an incomplete type as a
12379 base, but we allow it with a pedwarn for backward
12380 compatibility. */
12381 if (processing_template_decl
12382 && CLASS_TYPE_P (basetype) && TYPE_BEING_DEFINED (basetype))
12383 cxx_incomplete_type_diagnostic (NULL_TREE, basetype, DK_PEDWARN);
12384 if (!dependent_type_p (basetype)
12385 && !complete_type_or_else (basetype, NULL))
12386 /* An incomplete type. Remove it from the list. */
12387 *basep = TREE_CHAIN (*basep);
12388 else
12390 max_bases++;
12391 if (TREE_TYPE (*basep))
12392 max_vbases++;
12393 if (CLASS_TYPE_P (basetype))
12394 max_vbases += vec_safe_length (CLASSTYPE_VBASECLASSES (basetype));
12395 basep = &TREE_CHAIN (*basep);
12399 TYPE_MARKED_P (ref) = 1;
12401 /* The binfo slot should be empty, unless this is an (ill-formed)
12402 redefinition. */
12403 if (TYPE_BINFO (ref) && !TYPE_SIZE (ref))
12405 error ("redefinition of %q#T", ref);
12406 return false;
12409 gcc_assert (TYPE_MAIN_VARIANT (ref) == ref);
12411 binfo = make_tree_binfo (max_bases);
12413 TYPE_BINFO (ref) = binfo;
12414 BINFO_OFFSET (binfo) = size_zero_node;
12415 BINFO_TYPE (binfo) = ref;
12417 /* Apply base-class info set up to the variants of this type. */
12418 fixup_type_variants (ref);
12420 if (max_bases)
12422 vec_alloc (BINFO_BASE_ACCESSES (binfo), max_bases);
12423 /* An aggregate cannot have baseclasses. */
12424 CLASSTYPE_NON_AGGREGATE (ref) = 1;
12426 if (TREE_CODE (ref) == UNION_TYPE)
12428 error ("derived union %qT invalid", ref);
12429 return false;
12433 if (max_bases > 1)
12435 if (TYPE_FOR_JAVA (ref))
12437 error ("Java class %qT cannot have multiple bases", ref);
12438 return false;
12442 if (max_vbases)
12444 vec_alloc (CLASSTYPE_VBASECLASSES (ref), max_vbases);
12446 if (TYPE_FOR_JAVA (ref))
12448 error ("Java class %qT cannot have virtual bases", ref);
12449 return false;
12453 for (igo_prev = binfo; base_list; base_list = TREE_CHAIN (base_list))
12455 tree access = TREE_PURPOSE (base_list);
12456 int via_virtual = TREE_TYPE (base_list) != NULL_TREE;
12457 tree basetype = TREE_VALUE (base_list);
12459 if (access == access_default_node)
12460 access = default_access;
12462 if (PACK_EXPANSION_P (basetype))
12463 basetype = PACK_EXPANSION_PATTERN (basetype);
12464 if (TREE_CODE (basetype) == TYPE_DECL)
12465 basetype = TREE_TYPE (basetype);
12466 if (!MAYBE_CLASS_TYPE_P (basetype) || TREE_CODE (basetype) == UNION_TYPE)
12468 error ("base type %qT fails to be a struct or class type",
12469 basetype);
12470 return false;
12473 if (TYPE_FOR_JAVA (basetype) && (current_lang_depth () == 0))
12474 TYPE_FOR_JAVA (ref) = 1;
12476 base_binfo = NULL_TREE;
12477 if (CLASS_TYPE_P (basetype) && !dependent_scope_p (basetype))
12479 base_binfo = TYPE_BINFO (basetype);
12480 /* The original basetype could have been a typedef'd type. */
12481 basetype = BINFO_TYPE (base_binfo);
12483 /* Inherit flags from the base. */
12484 TYPE_HAS_NEW_OPERATOR (ref)
12485 |= TYPE_HAS_NEW_OPERATOR (basetype);
12486 TYPE_HAS_ARRAY_NEW_OPERATOR (ref)
12487 |= TYPE_HAS_ARRAY_NEW_OPERATOR (basetype);
12488 TYPE_GETS_DELETE (ref) |= TYPE_GETS_DELETE (basetype);
12489 TYPE_HAS_CONVERSION (ref) |= TYPE_HAS_CONVERSION (basetype);
12490 CLASSTYPE_DIAMOND_SHAPED_P (ref)
12491 |= CLASSTYPE_DIAMOND_SHAPED_P (basetype);
12492 CLASSTYPE_REPEATED_BASE_P (ref)
12493 |= CLASSTYPE_REPEATED_BASE_P (basetype);
12496 /* We must do this test after we've seen through a typedef
12497 type. */
12498 if (TYPE_MARKED_P (basetype))
12500 if (basetype == ref)
12501 error ("recursive type %qT undefined", basetype);
12502 else
12503 error ("duplicate base type %qT invalid", basetype);
12504 return false;
12507 if (PACK_EXPANSION_P (TREE_VALUE (base_list)))
12508 /* Regenerate the pack expansion for the bases. */
12509 basetype = make_pack_expansion (basetype);
12511 TYPE_MARKED_P (basetype) = 1;
12513 base_binfo = copy_binfo (base_binfo, basetype, ref,
12514 &igo_prev, via_virtual);
12515 if (!BINFO_INHERITANCE_CHAIN (base_binfo))
12516 BINFO_INHERITANCE_CHAIN (base_binfo) = binfo;
12518 BINFO_BASE_APPEND (binfo, base_binfo);
12519 BINFO_BASE_ACCESS_APPEND (binfo, access);
12522 if (vec_safe_length (CLASSTYPE_VBASECLASSES (ref)) < max_vbases)
12523 /* If we didn't get max_vbases vbases, we must have shared at
12524 least one of them, and are therefore diamond shaped. */
12525 CLASSTYPE_DIAMOND_SHAPED_P (ref) = 1;
12527 /* Unmark all the types. */
12528 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
12529 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
12530 TYPE_MARKED_P (ref) = 0;
12532 /* Now see if we have a repeated base type. */
12533 if (!CLASSTYPE_REPEATED_BASE_P (ref))
12535 for (base_binfo = binfo; base_binfo;
12536 base_binfo = TREE_CHAIN (base_binfo))
12538 if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
12540 CLASSTYPE_REPEATED_BASE_P (ref) = 1;
12541 break;
12543 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 1;
12545 for (base_binfo = binfo; base_binfo;
12546 base_binfo = TREE_CHAIN (base_binfo))
12547 if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
12548 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
12549 else
12550 break;
12553 return true;
12557 /* Copies the enum-related properties from type SRC to type DST.
12558 Used with the underlying type of an enum and the enum itself. */
12559 static void
12560 copy_type_enum (tree dst, tree src)
12562 tree t;
12563 for (t = dst; t; t = TYPE_NEXT_VARIANT (t))
12565 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (src);
12566 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (src);
12567 TYPE_SIZE (t) = TYPE_SIZE (src);
12568 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (src);
12569 SET_TYPE_MODE (dst, TYPE_MODE (src));
12570 TYPE_PRECISION (t) = TYPE_PRECISION (src);
12571 TYPE_ALIGN (t) = TYPE_ALIGN (src);
12572 TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (src);
12573 TYPE_UNSIGNED (t) = TYPE_UNSIGNED (src);
12577 /* Begin compiling the definition of an enumeration type.
12578 NAME is its name,
12580 if ENUMTYPE is not NULL_TREE then the type has alredy been found.
12582 UNDERLYING_TYPE is the type that will be used as the storage for
12583 the enumeration type. This should be NULL_TREE if no storage type
12584 was specified.
12586 SCOPED_ENUM_P is true if this is a scoped enumeration type.
12588 if IS_NEW is not NULL, gets TRUE iff a new type is created.
12590 Returns the type object, as yet incomplete.
12591 Also records info about it so that build_enumerator
12592 may be used to declare the individual values as they are read. */
12594 tree
12595 start_enum (tree name, tree enumtype, tree underlying_type,
12596 bool scoped_enum_p, bool *is_new)
12598 tree prevtype = NULL_TREE;
12599 gcc_assert (identifier_p (name));
12601 if (is_new)
12602 *is_new = false;
12603 /* [C++0x dcl.enum]p5:
12605 If not explicitly specified, the underlying type of a scoped
12606 enumeration type is int. */
12607 if (!underlying_type && scoped_enum_p)
12608 underlying_type = integer_type_node;
12610 if (underlying_type)
12611 underlying_type = cv_unqualified (underlying_type);
12613 /* If this is the real definition for a previous forward reference,
12614 fill in the contents in the same object that used to be the
12615 forward reference. */
12616 if (!enumtype)
12617 enumtype = lookup_and_check_tag (enum_type, name,
12618 /*tag_scope=*/ts_current,
12619 /*template_header_p=*/false);
12621 /* In case of a template_decl, the only check that should be deferred
12622 to instantiation time is the comparison of underlying types. */
12623 if (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE)
12625 if (scoped_enum_p != SCOPED_ENUM_P (enumtype))
12627 error_at (input_location, "scoped/unscoped mismatch "
12628 "in enum %q#T", enumtype);
12629 error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
12630 "previous definition here");
12631 enumtype = error_mark_node;
12633 else if (ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) != !! underlying_type)
12635 error_at (input_location, "underlying type mismatch "
12636 "in enum %q#T", enumtype);
12637 error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
12638 "previous definition here");
12639 enumtype = error_mark_node;
12641 else if (underlying_type && ENUM_UNDERLYING_TYPE (enumtype)
12642 && !dependent_type_p (underlying_type)
12643 && !dependent_type_p (ENUM_UNDERLYING_TYPE (enumtype))
12644 && !same_type_p (underlying_type,
12645 ENUM_UNDERLYING_TYPE (enumtype)))
12647 error_at (input_location, "different underlying type "
12648 "in enum %q#T", enumtype);
12649 error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
12650 "previous definition here");
12651 underlying_type = NULL_TREE;
12655 if (!enumtype || TREE_CODE (enumtype) != ENUMERAL_TYPE
12656 || processing_template_decl)
12658 /* In case of error, make a dummy enum to allow parsing to
12659 continue. */
12660 if (enumtype == error_mark_node)
12662 name = make_anon_name ();
12663 enumtype = NULL_TREE;
12666 /* enumtype may be an ENUMERAL_TYPE if this is a redefinition
12667 of an opaque enum, or an opaque enum of an already defined
12668 enumeration (C++0x only).
12669 In any other case, it'll be NULL_TREE. */
12670 if (!enumtype)
12672 if (is_new)
12673 *is_new = true;
12675 prevtype = enumtype;
12677 /* Do not push the decl more than once, unless we need to
12678 compare underlying types at instantiation time */
12679 if (!enumtype
12680 || TREE_CODE (enumtype) != ENUMERAL_TYPE
12681 || (underlying_type
12682 && dependent_type_p (underlying_type))
12683 || (ENUM_UNDERLYING_TYPE (enumtype)
12684 && dependent_type_p (ENUM_UNDERLYING_TYPE (enumtype))))
12686 enumtype = cxx_make_type (ENUMERAL_TYPE);
12687 enumtype = pushtag (name, enumtype, /*tag_scope=*/ts_current);
12689 else
12690 enumtype = xref_tag (enum_type, name, /*tag_scope=*/ts_current,
12691 false);
12693 if (enumtype == error_mark_node)
12694 return error_mark_node;
12696 /* The enum is considered opaque until the opening '{' of the
12697 enumerator list. */
12698 SET_OPAQUE_ENUM_P (enumtype, true);
12699 ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) = !! underlying_type;
12702 SET_SCOPED_ENUM_P (enumtype, scoped_enum_p);
12704 if (underlying_type)
12706 if (CP_INTEGRAL_TYPE_P (underlying_type))
12708 copy_type_enum (enumtype, underlying_type);
12709 ENUM_UNDERLYING_TYPE (enumtype) = underlying_type;
12711 else if (dependent_type_p (underlying_type))
12712 ENUM_UNDERLYING_TYPE (enumtype) = underlying_type;
12713 else
12714 error ("underlying type %<%T%> of %<%T%> must be an integral type",
12715 underlying_type, enumtype);
12718 /* If into a template class, the returned enum is always the first
12719 declaration (opaque or not) seen. This way all the references to
12720 this type will be to the same declaration. The following ones are used
12721 only to check for definition errors. */
12722 if (prevtype && processing_template_decl)
12723 return prevtype;
12724 else
12725 return enumtype;
12728 /* After processing and defining all the values of an enumeration type,
12729 install their decls in the enumeration type.
12730 ENUMTYPE is the type object. */
12732 void
12733 finish_enum_value_list (tree enumtype)
12735 tree values;
12736 tree underlying_type;
12737 tree decl;
12738 tree value;
12739 tree minnode, maxnode;
12740 tree t;
12742 bool fixed_underlying_type_p
12743 = ENUM_UNDERLYING_TYPE (enumtype) != NULL_TREE;
12745 /* We built up the VALUES in reverse order. */
12746 TYPE_VALUES (enumtype) = nreverse (TYPE_VALUES (enumtype));
12748 /* For an enum defined in a template, just set the type of the values;
12749 all further processing is postponed until the template is
12750 instantiated. We need to set the type so that tsubst of a CONST_DECL
12751 works. */
12752 if (processing_template_decl)
12754 for (values = TYPE_VALUES (enumtype);
12755 values;
12756 values = TREE_CHAIN (values))
12757 TREE_TYPE (TREE_VALUE (values)) = enumtype;
12758 return;
12761 /* Determine the minimum and maximum values of the enumerators. */
12762 if (TYPE_VALUES (enumtype))
12764 minnode = maxnode = NULL_TREE;
12766 for (values = TYPE_VALUES (enumtype);
12767 values;
12768 values = TREE_CHAIN (values))
12770 decl = TREE_VALUE (values);
12772 /* [dcl.enum]: Following the closing brace of an enum-specifier,
12773 each enumerator has the type of its enumeration. Prior to the
12774 closing brace, the type of each enumerator is the type of its
12775 initializing value. */
12776 TREE_TYPE (decl) = enumtype;
12778 /* Update the minimum and maximum values, if appropriate. */
12779 value = DECL_INITIAL (decl);
12780 if (value == error_mark_node)
12781 value = integer_zero_node;
12782 /* Figure out what the minimum and maximum values of the
12783 enumerators are. */
12784 if (!minnode)
12785 minnode = maxnode = value;
12786 else if (tree_int_cst_lt (maxnode, value))
12787 maxnode = value;
12788 else if (tree_int_cst_lt (value, minnode))
12789 minnode = value;
12792 else
12793 /* [dcl.enum]
12795 If the enumerator-list is empty, the underlying type is as if
12796 the enumeration had a single enumerator with value 0. */
12797 minnode = maxnode = integer_zero_node;
12799 if (!fixed_underlying_type_p)
12801 /* Compute the number of bits require to represent all values of the
12802 enumeration. We must do this before the type of MINNODE and
12803 MAXNODE are transformed, since tree_int_cst_min_precision relies
12804 on the TREE_TYPE of the value it is passed. */
12805 signop sgn = tree_int_cst_sgn (minnode) >= 0 ? UNSIGNED : SIGNED;
12806 int lowprec = tree_int_cst_min_precision (minnode, sgn);
12807 int highprec = tree_int_cst_min_precision (maxnode, sgn);
12808 int precision = MAX (lowprec, highprec);
12809 unsigned int itk;
12810 bool use_short_enum;
12812 /* Determine the underlying type of the enumeration.
12814 [dcl.enum]
12816 The underlying type of an enumeration is an integral type that
12817 can represent all the enumerator values defined in the
12818 enumeration. It is implementation-defined which integral type is
12819 used as the underlying type for an enumeration except that the
12820 underlying type shall not be larger than int unless the value of
12821 an enumerator cannot fit in an int or unsigned int.
12823 We use "int" or an "unsigned int" as the underlying type, even if
12824 a smaller integral type would work, unless the user has
12825 explicitly requested that we use the smallest possible type. The
12826 user can request that for all enumerations with a command line
12827 flag, or for just one enumeration with an attribute. */
12829 use_short_enum = flag_short_enums
12830 || lookup_attribute ("packed", TYPE_ATTRIBUTES (enumtype));
12832 for (itk = (use_short_enum ? itk_char : itk_int);
12833 itk != itk_none;
12834 itk++)
12836 underlying_type = integer_types[itk];
12837 if (underlying_type != NULL_TREE
12838 && TYPE_PRECISION (underlying_type) >= precision
12839 && TYPE_SIGN (underlying_type) == sgn)
12840 break;
12842 if (itk == itk_none)
12844 /* DR 377
12846 IF no integral type can represent all the enumerator values, the
12847 enumeration is ill-formed. */
12848 error ("no integral type can represent all of the enumerator values "
12849 "for %qT", enumtype);
12850 precision = TYPE_PRECISION (long_long_integer_type_node);
12851 underlying_type = integer_types[itk_unsigned_long_long];
12854 /* [dcl.enum]
12856 The value of sizeof() applied to an enumeration type, an object
12857 of an enumeration type, or an enumerator, is the value of sizeof()
12858 applied to the underlying type. */
12859 copy_type_enum (enumtype, underlying_type);
12861 /* Compute the minimum and maximum values for the type.
12863 [dcl.enum]
12865 For an enumeration where emin is the smallest enumerator and emax
12866 is the largest, the values of the enumeration are the values of the
12867 underlying type in the range bmin to bmax, where bmin and bmax are,
12868 respectively, the smallest and largest values of the smallest bit-
12869 field that can store emin and emax. */
12871 /* The middle-end currently assumes that types with TYPE_PRECISION
12872 narrower than their underlying type are suitably zero or sign
12873 extended to fill their mode. Similarly, it assumes that the front
12874 end assures that a value of a particular type must be within
12875 TYPE_MIN_VALUE and TYPE_MAX_VALUE.
12877 We used to set these fields based on bmin and bmax, but that led
12878 to invalid assumptions like optimizing away bounds checking. So
12879 now we just set the TYPE_PRECISION, TYPE_MIN_VALUE, and
12880 TYPE_MAX_VALUE to the values for the mode above and only restrict
12881 the ENUM_UNDERLYING_TYPE for the benefit of diagnostics. */
12882 ENUM_UNDERLYING_TYPE (enumtype)
12883 = build_distinct_type_copy (underlying_type);
12884 TYPE_PRECISION (ENUM_UNDERLYING_TYPE (enumtype)) = precision;
12885 set_min_and_max_values_for_integral_type
12886 (ENUM_UNDERLYING_TYPE (enumtype), precision, sgn);
12888 /* If -fstrict-enums, still constrain TYPE_MIN/MAX_VALUE. */
12889 if (flag_strict_enums)
12890 set_min_and_max_values_for_integral_type (enumtype, precision, sgn);
12892 else
12893 underlying_type = ENUM_UNDERLYING_TYPE (enumtype);
12895 /* Convert each of the enumerators to the type of the underlying
12896 type of the enumeration. */
12897 for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values))
12899 location_t saved_location;
12901 decl = TREE_VALUE (values);
12902 saved_location = input_location;
12903 input_location = DECL_SOURCE_LOCATION (decl);
12904 if (fixed_underlying_type_p)
12905 /* If the enumeration type has a fixed underlying type, we
12906 already checked all of the enumerator values. */
12907 value = DECL_INITIAL (decl);
12908 else
12909 value = perform_implicit_conversion (underlying_type,
12910 DECL_INITIAL (decl),
12911 tf_warning_or_error);
12912 input_location = saved_location;
12914 /* Do not clobber shared ints. */
12915 value = copy_node (value);
12917 TREE_TYPE (value) = enumtype;
12918 DECL_INITIAL (decl) = value;
12921 /* Fix up all variant types of this enum type. */
12922 for (t = TYPE_MAIN_VARIANT (enumtype); t; t = TYPE_NEXT_VARIANT (t))
12923 TYPE_VALUES (t) = TYPE_VALUES (enumtype);
12925 if (at_class_scope_p ()
12926 && COMPLETE_TYPE_P (current_class_type)
12927 && UNSCOPED_ENUM_P (enumtype))
12928 insert_late_enum_def_into_classtype_sorted_fields (enumtype,
12929 current_class_type);
12931 /* Finish debugging output for this type. */
12932 rest_of_type_compilation (enumtype, namespace_bindings_p ());
12935 /* Finishes the enum type. This is called only the first time an
12936 enumeration is seen, be it opaque or odinary.
12937 ENUMTYPE is the type object. */
12939 void
12940 finish_enum (tree enumtype)
12942 if (processing_template_decl)
12944 if (at_function_scope_p ())
12945 add_stmt (build_min (TAG_DEFN, enumtype));
12946 return;
12949 /* If this is a forward declaration, there should not be any variants,
12950 though we can get a variant in the middle of an enum-specifier with
12951 wacky code like 'enum E { e = sizeof(const E*) };' */
12952 gcc_assert (enumtype == TYPE_MAIN_VARIANT (enumtype)
12953 && (TYPE_VALUES (enumtype)
12954 || !TYPE_NEXT_VARIANT (enumtype)));
12957 /* Build and install a CONST_DECL for an enumeration constant of the
12958 enumeration type ENUMTYPE whose NAME and VALUE (if any) are provided.
12959 LOC is the location of NAME.
12960 Assignment of sequential values by default is handled here. */
12962 void
12963 build_enumerator (tree name, tree value, tree enumtype, location_t loc)
12965 tree decl;
12966 tree context;
12967 tree type;
12969 /* If the VALUE was erroneous, pretend it wasn't there; that will
12970 result in the enum being assigned the next value in sequence. */
12971 if (value == error_mark_node)
12972 value = NULL_TREE;
12974 /* Remove no-op casts from the value. */
12975 if (value)
12976 STRIP_TYPE_NOPS (value);
12978 if (! processing_template_decl)
12980 /* Validate and default VALUE. */
12981 if (value != NULL_TREE)
12983 value = cxx_constant_value (value);
12985 if (TREE_CODE (value) != INTEGER_CST
12986 || ! INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (value)))
12988 error ("enumerator value for %qD is not an integer constant",
12989 name);
12990 value = NULL_TREE;
12994 /* Default based on previous value. */
12995 if (value == NULL_TREE)
12997 if (TYPE_VALUES (enumtype))
12999 tree prev_value;
13000 bool overflowed;
13002 /* C++03 7.2/4: If no initializer is specified for the first
13003 enumerator, the type is an unspecified integral
13004 type. Otherwise the type is the same as the type of the
13005 initializing value of the preceding enumerator unless the
13006 incremented value is not representable in that type, in
13007 which case the type is an unspecified integral type
13008 sufficient to contain the incremented value. */
13009 prev_value = DECL_INITIAL (TREE_VALUE (TYPE_VALUES (enumtype)));
13010 if (error_operand_p (prev_value))
13011 value = error_mark_node;
13012 else
13014 tree type = TREE_TYPE (prev_value);
13015 signop sgn = TYPE_SIGN (type);
13016 widest_int wi = wi::add (wi::to_widest (prev_value), 1, sgn,
13017 &overflowed);
13018 if (!overflowed)
13020 bool pos = !wi::neg_p (wi, sgn);
13021 if (!wi::fits_to_tree_p (wi, type))
13023 unsigned int itk;
13024 for (itk = itk_int; itk != itk_none; itk++)
13026 type = integer_types[itk];
13027 if (type != NULL_TREE
13028 && (pos || !TYPE_UNSIGNED (type))
13029 && wi::fits_to_tree_p (wi, type))
13030 break;
13032 if (type && cxx_dialect < cxx11
13033 && itk > itk_unsigned_long)
13034 pedwarn (input_location, OPT_Wlong_long, pos ? "\
13035 incremented enumerator value is too large for %<unsigned long%>" : "\
13036 incremented enumerator value is too large for %<long%>");
13038 if (type == NULL_TREE)
13039 overflowed = true;
13040 else
13041 value = wide_int_to_tree (type, wi);
13044 if (overflowed)
13046 error ("overflow in enumeration values at %qD", name);
13047 value = error_mark_node;
13051 else
13052 value = integer_zero_node;
13055 /* Remove no-op casts from the value. */
13056 STRIP_TYPE_NOPS (value);
13058 /* If the underlying type of the enum is fixed, check whether
13059 the enumerator values fits in the underlying type. If it
13060 does not fit, the program is ill-formed [C++0x dcl.enum]. */
13061 if (ENUM_UNDERLYING_TYPE (enumtype)
13062 && value
13063 && TREE_CODE (value) == INTEGER_CST)
13065 if (!int_fits_type_p (value, ENUM_UNDERLYING_TYPE (enumtype)))
13066 error ("enumerator value %E is outside the range of underlying "
13067 "type %<%T%>", value, ENUM_UNDERLYING_TYPE (enumtype));
13069 /* Convert the value to the appropriate type. */
13070 value = convert (ENUM_UNDERLYING_TYPE (enumtype), value);
13074 /* C++ associates enums with global, function, or class declarations. */
13075 context = current_scope ();
13077 /* Build the actual enumeration constant. Note that the enumeration
13078 constants have the underlying type of the enum (if it is fixed)
13079 or the type of their initializer (if the underlying type of the
13080 enum is not fixed):
13082 [ C++0x dcl.enum ]
13084 If the underlying type is fixed, the type of each enumerator
13085 prior to the closing brace is the underlying type; if the
13086 initializing value of an enumerator cannot be represented by
13087 the underlying type, the program is ill-formed. If the
13088 underlying type is not fixed, the type of each enumerator is
13089 the type of its initializing value.
13091 If the underlying type is not fixed, it will be computed by
13092 finish_enum and we will reset the type of this enumerator. Of
13093 course, if we're processing a template, there may be no value. */
13094 type = value ? TREE_TYPE (value) : NULL_TREE;
13096 decl = build_decl (loc, CONST_DECL, name, type);
13098 DECL_CONTEXT (decl) = enumtype;
13099 TREE_CONSTANT (decl) = 1;
13100 TREE_READONLY (decl) = 1;
13101 DECL_INITIAL (decl) = value;
13103 if (context && context == current_class_type && !SCOPED_ENUM_P (enumtype))
13104 /* In something like `struct S { enum E { i = 7 }; };' we put `i'
13105 on the TYPE_FIELDS list for `S'. (That's so that you can say
13106 things like `S::i' later.) */
13107 finish_member_declaration (decl);
13108 else
13109 pushdecl (decl);
13111 /* Add this enumeration constant to the list for this type. */
13112 TYPE_VALUES (enumtype) = tree_cons (name, decl, TYPE_VALUES (enumtype));
13115 /* Look for an enumerator with the given NAME within the enumeration
13116 type ENUMTYPE. This routine is used primarily for qualified name
13117 lookup into an enumerator in C++0x, e.g.,
13119 enum class Color { Red, Green, Blue };
13121 Color color = Color::Red;
13123 Returns the value corresponding to the enumerator, or
13124 NULL_TREE if no such enumerator was found. */
13125 tree
13126 lookup_enumerator (tree enumtype, tree name)
13128 tree e;
13129 gcc_assert (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE);
13131 e = purpose_member (name, TYPE_VALUES (enumtype));
13132 return e? TREE_VALUE (e) : NULL_TREE;
13136 /* We're defining DECL. Make sure that its type is OK. */
13138 static void
13139 check_function_type (tree decl, tree current_function_parms)
13141 tree fntype = TREE_TYPE (decl);
13142 tree return_type = complete_type (TREE_TYPE (fntype));
13144 /* In a function definition, arg types must be complete. */
13145 require_complete_types_for_parms (current_function_parms);
13147 if (dependent_type_p (return_type)
13148 || type_uses_auto (return_type))
13149 return;
13150 if (!COMPLETE_OR_VOID_TYPE_P (return_type)
13151 || (TYPE_FOR_JAVA (return_type) && MAYBE_CLASS_TYPE_P (return_type)))
13153 tree args = TYPE_ARG_TYPES (fntype);
13155 if (!COMPLETE_OR_VOID_TYPE_P (return_type))
13156 error ("return type %q#T is incomplete", return_type);
13157 else
13158 error ("return type has Java class type %q#T", return_type);
13160 /* Make it return void instead. */
13161 if (TREE_CODE (fntype) == METHOD_TYPE)
13162 fntype = build_method_type_directly (TREE_TYPE (TREE_VALUE (args)),
13163 void_type_node,
13164 TREE_CHAIN (args));
13165 else
13166 fntype = build_function_type (void_type_node, args);
13167 fntype
13168 = build_exception_variant (fntype,
13169 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl)));
13170 fntype = (cp_build_type_attribute_variant
13171 (fntype, TYPE_ATTRIBUTES (TREE_TYPE (decl))));
13172 TREE_TYPE (decl) = fntype;
13174 else
13175 abstract_virtuals_error (decl, TREE_TYPE (fntype));
13178 /* Create the FUNCTION_DECL for a function definition.
13179 DECLSPECS and DECLARATOR are the parts of the declaration;
13180 they describe the function's name and the type it returns,
13181 but twisted together in a fashion that parallels the syntax of C.
13183 FLAGS is a bitwise or of SF_PRE_PARSED (indicating that the
13184 DECLARATOR is really the DECL for the function we are about to
13185 process and that DECLSPECS should be ignored), SF_INCLASS_INLINE
13186 indicating that the function is an inline defined in-class.
13188 This function creates a binding context for the function body
13189 as well as setting up the FUNCTION_DECL in current_function_decl.
13191 For C++, we must first check whether that datum makes any sense.
13192 For example, "class A local_a(1,2);" means that variable local_a
13193 is an aggregate of type A, which should have a constructor
13194 applied to it with the argument list [1, 2].
13196 On entry, DECL_INITIAL (decl1) should be NULL_TREE or error_mark_node,
13197 or may be a BLOCK if the function has been defined previously
13198 in this translation unit. On exit, DECL_INITIAL (decl1) will be
13199 error_mark_node if the function has never been defined, or
13200 a BLOCK if the function has been defined somewhere. */
13202 bool
13203 start_preparsed_function (tree decl1, tree attrs, int flags)
13205 tree ctype = NULL_TREE;
13206 tree fntype;
13207 tree restype;
13208 int doing_friend = 0;
13209 cp_binding_level *bl;
13210 tree current_function_parms;
13211 struct c_fileinfo *finfo
13212 = get_fileinfo (LOCATION_FILE (DECL_SOURCE_LOCATION (decl1)));
13213 bool honor_interface;
13215 /* Sanity check. */
13216 gcc_assert (VOID_TYPE_P (TREE_VALUE (void_list_node)));
13217 gcc_assert (TREE_CHAIN (void_list_node) == NULL_TREE);
13219 fntype = TREE_TYPE (decl1);
13220 if (TREE_CODE (fntype) == METHOD_TYPE)
13221 ctype = TYPE_METHOD_BASETYPE (fntype);
13223 /* ISO C++ 11.4/5. A friend function defined in a class is in
13224 the (lexical) scope of the class in which it is defined. */
13225 if (!ctype && DECL_FRIEND_P (decl1))
13227 ctype = DECL_FRIEND_CONTEXT (decl1);
13229 /* CTYPE could be null here if we're dealing with a template;
13230 for example, `inline friend float foo()' inside a template
13231 will have no CTYPE set. */
13232 if (ctype && TREE_CODE (ctype) != RECORD_TYPE)
13233 ctype = NULL_TREE;
13234 else
13235 doing_friend = 1;
13238 if (DECL_DECLARED_INLINE_P (decl1)
13239 && lookup_attribute ("noinline", attrs))
13240 warning (0, "inline function %q+D given attribute noinline", decl1);
13242 /* Handle gnu_inline attribute. */
13243 if (GNU_INLINE_P (decl1))
13245 DECL_EXTERNAL (decl1) = 1;
13246 DECL_NOT_REALLY_EXTERN (decl1) = 0;
13247 DECL_INTERFACE_KNOWN (decl1) = 1;
13248 DECL_DISREGARD_INLINE_LIMITS (decl1) = 1;
13251 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl1))
13252 /* This is a constructor, we must ensure that any default args
13253 introduced by this definition are propagated to the clones
13254 now. The clones are used directly in overload resolution. */
13255 adjust_clone_args (decl1);
13257 /* Sometimes we don't notice that a function is a static member, and
13258 build a METHOD_TYPE for it. Fix that up now. */
13259 gcc_assert (!(ctype != NULL_TREE && DECL_STATIC_FUNCTION_P (decl1)
13260 && TREE_CODE (TREE_TYPE (decl1)) == METHOD_TYPE));
13262 /* Set up current_class_type, and enter the scope of the class, if
13263 appropriate. */
13264 if (ctype)
13265 push_nested_class (ctype);
13266 else if (DECL_STATIC_FUNCTION_P (decl1))
13267 push_nested_class (DECL_CONTEXT (decl1));
13269 /* Now that we have entered the scope of the class, we must restore
13270 the bindings for any template parameters surrounding DECL1, if it
13271 is an inline member template. (Order is important; consider the
13272 case where a template parameter has the same name as a field of
13273 the class.) It is not until after this point that
13274 PROCESSING_TEMPLATE_DECL is guaranteed to be set up correctly. */
13275 if (flags & SF_INCLASS_INLINE)
13276 maybe_begin_member_template_processing (decl1);
13278 /* Effective C++ rule 15. */
13279 if (warn_ecpp
13280 && DECL_OVERLOADED_OPERATOR_P (decl1) == NOP_EXPR
13281 && VOID_TYPE_P (TREE_TYPE (fntype)))
13282 warning (OPT_Weffc__, "%<operator=%> should return a reference to %<*this%>");
13284 /* Make the init_value nonzero so pushdecl knows this is not tentative.
13285 error_mark_node is replaced below (in poplevel) with the BLOCK. */
13286 if (!DECL_INITIAL (decl1))
13287 DECL_INITIAL (decl1) = error_mark_node;
13289 /* This function exists in static storage.
13290 (This does not mean `static' in the C sense!) */
13291 TREE_STATIC (decl1) = 1;
13293 /* We must call push_template_decl after current_class_type is set
13294 up. (If we are processing inline definitions after exiting a
13295 class scope, current_class_type will be NULL_TREE until set above
13296 by push_nested_class.) */
13297 if (processing_template_decl)
13299 tree newdecl1 = push_template_decl (decl1);
13300 if (newdecl1 == error_mark_node)
13302 if (ctype || DECL_STATIC_FUNCTION_P (decl1))
13303 pop_nested_class ();
13304 return false;
13306 decl1 = newdecl1;
13309 /* We are now in the scope of the function being defined. */
13310 current_function_decl = decl1;
13312 /* Save the parm names or decls from this function's declarator
13313 where store_parm_decls will find them. */
13314 current_function_parms = DECL_ARGUMENTS (decl1);
13316 /* Make sure the parameter and return types are reasonable. When
13317 you declare a function, these types can be incomplete, but they
13318 must be complete when you define the function. */
13319 check_function_type (decl1, current_function_parms);
13321 /* Build the return declaration for the function. */
13322 restype = TREE_TYPE (fntype);
13324 if (DECL_RESULT (decl1) == NULL_TREE)
13326 tree resdecl;
13328 resdecl = build_decl (input_location, RESULT_DECL, 0, restype);
13329 DECL_ARTIFICIAL (resdecl) = 1;
13330 DECL_IGNORED_P (resdecl) = 1;
13331 DECL_RESULT (decl1) = resdecl;
13333 cp_apply_type_quals_to_decl (cp_type_quals (restype), resdecl);
13336 /* Let the user know we're compiling this function. */
13337 announce_function (decl1);
13339 /* Record the decl so that the function name is defined.
13340 If we already have a decl for this name, and it is a FUNCTION_DECL,
13341 use the old decl. */
13342 if (!processing_template_decl && !(flags & SF_PRE_PARSED))
13344 /* A specialization is not used to guide overload resolution. */
13345 if (!DECL_FUNCTION_MEMBER_P (decl1)
13346 && !(DECL_USE_TEMPLATE (decl1) &&
13347 PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl1))))
13349 tree olddecl = pushdecl (decl1);
13351 if (olddecl == error_mark_node)
13352 /* If something went wrong when registering the declaration,
13353 use DECL1; we have to have a FUNCTION_DECL to use when
13354 parsing the body of the function. */
13356 else
13358 /* Otherwise, OLDDECL is either a previous declaration
13359 of the same function or DECL1 itself. */
13361 if (warn_missing_declarations
13362 && olddecl == decl1
13363 && !DECL_MAIN_P (decl1)
13364 && TREE_PUBLIC (decl1)
13365 && !DECL_DECLARED_INLINE_P (decl1))
13367 tree context;
13369 /* Check whether DECL1 is in an anonymous
13370 namespace. */
13371 for (context = DECL_CONTEXT (decl1);
13372 context;
13373 context = DECL_CONTEXT (context))
13375 if (TREE_CODE (context) == NAMESPACE_DECL
13376 && DECL_NAME (context) == NULL_TREE)
13377 break;
13380 if (context == NULL)
13381 warning (OPT_Wmissing_declarations,
13382 "no previous declaration for %q+D", decl1);
13385 decl1 = olddecl;
13388 else
13390 /* We need to set the DECL_CONTEXT. */
13391 if (!DECL_CONTEXT (decl1) && DECL_TEMPLATE_INFO (decl1))
13392 DECL_CONTEXT (decl1) = DECL_CONTEXT (DECL_TI_TEMPLATE (decl1));
13394 fntype = TREE_TYPE (decl1);
13395 restype = TREE_TYPE (fntype);
13397 /* If #pragma weak applies, mark the decl appropriately now.
13398 The pragma only applies to global functions. Because
13399 determining whether or not the #pragma applies involves
13400 computing the mangled name for the declaration, we cannot
13401 apply the pragma until after we have merged this declaration
13402 with any previous declarations; if the original declaration
13403 has a linkage specification, that specification applies to
13404 the definition as well, and may affect the mangled name. */
13405 if (DECL_FILE_SCOPE_P (decl1))
13406 maybe_apply_pragma_weak (decl1);
13409 /* Reset this in case the call to pushdecl changed it. */
13410 current_function_decl = decl1;
13412 gcc_assert (DECL_INITIAL (decl1));
13414 /* This function may already have been parsed, in which case just
13415 return; our caller will skip over the body without parsing. */
13416 if (DECL_INITIAL (decl1) != error_mark_node)
13417 return true;
13419 /* Initialize RTL machinery. We cannot do this until
13420 CURRENT_FUNCTION_DECL and DECL_RESULT are set up. We do this
13421 even when processing a template; this is how we get
13422 CFUN set up, and our per-function variables initialized.
13423 FIXME factor out the non-RTL stuff. */
13424 bl = current_binding_level;
13425 allocate_struct_function (decl1, processing_template_decl);
13427 /* Initialize the language data structures. Whenever we start
13428 a new function, we destroy temporaries in the usual way. */
13429 cfun->language = ggc_cleared_alloc<language_function> ();
13430 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
13431 current_binding_level = bl;
13433 if (!processing_template_decl && type_uses_auto (restype))
13435 FNDECL_USED_AUTO (decl1) = true;
13436 current_function_auto_return_pattern = restype;
13439 /* Start the statement-tree, start the tree now. */
13440 DECL_SAVED_TREE (decl1) = push_stmt_list ();
13442 /* If we are (erroneously) defining a function that we have already
13443 defined before, wipe out what we knew before. */
13444 if (!DECL_PENDING_INLINE_P (decl1))
13445 DECL_SAVED_FUNCTION_DATA (decl1) = NULL;
13447 if (ctype && !doing_friend && !DECL_STATIC_FUNCTION_P (decl1))
13449 /* We know that this was set up by `grokclassfn'. We do not
13450 wait until `store_parm_decls', since evil parse errors may
13451 never get us to that point. Here we keep the consistency
13452 between `current_class_type' and `current_class_ptr'. */
13453 tree t = DECL_ARGUMENTS (decl1);
13455 gcc_assert (t != NULL_TREE && TREE_CODE (t) == PARM_DECL);
13456 gcc_assert (TYPE_PTR_P (TREE_TYPE (t)));
13458 cp_function_chain->x_current_class_ref
13459 = cp_build_indirect_ref (t, RO_NULL, tf_warning_or_error);
13460 /* Set this second to avoid shortcut in cp_build_indirect_ref. */
13461 cp_function_chain->x_current_class_ptr = t;
13463 /* Constructors and destructors need to know whether they're "in
13464 charge" of initializing virtual base classes. */
13465 t = DECL_CHAIN (t);
13466 if (DECL_HAS_IN_CHARGE_PARM_P (decl1))
13468 current_in_charge_parm = t;
13469 t = DECL_CHAIN (t);
13471 if (DECL_HAS_VTT_PARM_P (decl1))
13473 gcc_assert (DECL_NAME (t) == vtt_parm_identifier);
13474 current_vtt_parm = t;
13478 honor_interface = (!DECL_TEMPLATE_INSTANTIATION (decl1)
13479 /* Implicitly-defined methods (like the
13480 destructor for a class in which no destructor
13481 is explicitly declared) must not be defined
13482 until their definition is needed. So, we
13483 ignore interface specifications for
13484 compiler-generated functions. */
13485 && !DECL_ARTIFICIAL (decl1));
13487 if (processing_template_decl)
13488 /* Don't mess with interface flags. */;
13489 else if (DECL_INTERFACE_KNOWN (decl1))
13491 tree ctx = decl_function_context (decl1);
13493 if (DECL_NOT_REALLY_EXTERN (decl1))
13494 DECL_EXTERNAL (decl1) = 0;
13496 if (ctx != NULL_TREE && vague_linkage_p (ctx))
13497 /* This is a function in a local class in an extern inline
13498 or template function. */
13499 comdat_linkage (decl1);
13501 /* If this function belongs to an interface, it is public.
13502 If it belongs to someone else's interface, it is also external.
13503 This only affects inlines and template instantiations. */
13504 else if (!finfo->interface_unknown && honor_interface)
13506 if (DECL_DECLARED_INLINE_P (decl1)
13507 || DECL_TEMPLATE_INSTANTIATION (decl1))
13509 DECL_EXTERNAL (decl1)
13510 = (finfo->interface_only
13511 || (DECL_DECLARED_INLINE_P (decl1)
13512 && ! flag_implement_inlines
13513 && !DECL_VINDEX (decl1)));
13515 /* For WIN32 we also want to put these in linkonce sections. */
13516 maybe_make_one_only (decl1);
13518 else
13519 DECL_EXTERNAL (decl1) = 0;
13520 DECL_INTERFACE_KNOWN (decl1) = 1;
13521 /* If this function is in an interface implemented in this file,
13522 make sure that the back end knows to emit this function
13523 here. */
13524 if (!DECL_EXTERNAL (decl1))
13525 mark_needed (decl1);
13527 else if (finfo->interface_unknown && finfo->interface_only
13528 && honor_interface)
13530 /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma
13531 interface, we will have both finfo->interface_unknown and
13532 finfo->interface_only set. In that case, we don't want to
13533 use the normal heuristics because someone will supply a
13534 #pragma implementation elsewhere, and deducing it here would
13535 produce a conflict. */
13536 comdat_linkage (decl1);
13537 DECL_EXTERNAL (decl1) = 0;
13538 DECL_INTERFACE_KNOWN (decl1) = 1;
13539 DECL_DEFER_OUTPUT (decl1) = 1;
13541 else
13543 /* This is a definition, not a reference.
13544 So clear DECL_EXTERNAL, unless this is a GNU extern inline. */
13545 if (!GNU_INLINE_P (decl1))
13546 DECL_EXTERNAL (decl1) = 0;
13548 if ((DECL_DECLARED_INLINE_P (decl1)
13549 || DECL_TEMPLATE_INSTANTIATION (decl1))
13550 && ! DECL_INTERFACE_KNOWN (decl1))
13551 DECL_DEFER_OUTPUT (decl1) = 1;
13552 else
13553 DECL_INTERFACE_KNOWN (decl1) = 1;
13556 /* Determine the ELF visibility attribute for the function. We must not
13557 do this before calling "pushdecl", as we must allow "duplicate_decls"
13558 to merge any attributes appropriately. We also need to wait until
13559 linkage is set. */
13560 if (!DECL_CLONED_FUNCTION_P (decl1))
13561 determine_visibility (decl1);
13563 if (!processing_template_decl)
13564 maybe_instantiate_noexcept (decl1);
13566 begin_scope (sk_function_parms, decl1);
13568 ++function_depth;
13570 if (DECL_DESTRUCTOR_P (decl1)
13571 || (DECL_CONSTRUCTOR_P (decl1)
13572 && targetm.cxx.cdtor_returns_this ()))
13574 cdtor_label = build_decl (input_location,
13575 LABEL_DECL, NULL_TREE, NULL_TREE);
13576 DECL_CONTEXT (cdtor_label) = current_function_decl;
13579 start_fname_decls ();
13581 store_parm_decls (current_function_parms);
13583 return true;
13587 /* Like start_preparsed_function, except that instead of a
13588 FUNCTION_DECL, this function takes DECLSPECS and DECLARATOR.
13590 Returns true on success. If the DECLARATOR is not suitable
13591 for a function, we return false, which tells the parser to
13592 skip the entire function. */
13594 bool
13595 start_function (cp_decl_specifier_seq *declspecs,
13596 const cp_declarator *declarator,
13597 tree attrs)
13599 tree decl1;
13601 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, &attrs);
13602 if (decl1 == error_mark_node)
13603 return false;
13604 /* If the declarator is not suitable for a function definition,
13605 cause a syntax error. */
13606 if (decl1 == NULL_TREE || TREE_CODE (decl1) != FUNCTION_DECL)
13608 error ("invalid function declaration");
13609 return false;
13612 if (DECL_MAIN_P (decl1))
13613 /* main must return int. grokfndecl should have corrected it
13614 (and issued a diagnostic) if the user got it wrong. */
13615 gcc_assert (same_type_p (TREE_TYPE (TREE_TYPE (decl1)),
13616 integer_type_node));
13618 return start_preparsed_function (decl1, attrs, /*flags=*/SF_DEFAULT);
13621 /* Returns true iff an EH_SPEC_BLOCK should be created in the body of
13622 FN. */
13624 static bool
13625 use_eh_spec_block (tree fn)
13627 return (flag_exceptions && flag_enforce_eh_specs
13628 && !processing_template_decl
13629 && !type_throw_all_p (TREE_TYPE (fn))
13630 /* We insert the EH_SPEC_BLOCK only in the original
13631 function; then, it is copied automatically to the
13632 clones. */
13633 && !DECL_CLONED_FUNCTION_P (fn)
13634 /* Implicitly-generated constructors and destructors have
13635 exception specifications. However, those specifications
13636 are the union of the possible exceptions specified by the
13637 constructors/destructors for bases and members, so no
13638 unallowed exception will ever reach this function. By
13639 not creating the EH_SPEC_BLOCK we save a little memory,
13640 and we avoid spurious warnings about unreachable
13641 code. */
13642 && !DECL_DEFAULTED_FN (fn));
13645 /* Store the parameter declarations into the current function declaration.
13646 This is called after parsing the parameter declarations, before
13647 digesting the body of the function.
13649 Also install to binding contour return value identifier, if any. */
13651 static void
13652 store_parm_decls (tree current_function_parms)
13654 tree fndecl = current_function_decl;
13655 tree parm;
13657 /* This is a chain of any other decls that came in among the parm
13658 declarations. If a parm is declared with enum {foo, bar} x;
13659 then CONST_DECLs for foo and bar are put here. */
13660 tree nonparms = NULL_TREE;
13662 if (current_function_parms)
13664 /* This case is when the function was defined with an ANSI prototype.
13665 The parms already have decls, so we need not do anything here
13666 except record them as in effect
13667 and complain if any redundant old-style parm decls were written. */
13669 tree specparms = current_function_parms;
13670 tree next;
13672 /* Must clear this because it might contain TYPE_DECLs declared
13673 at class level. */
13674 current_binding_level->names = NULL;
13676 /* If we're doing semantic analysis, then we'll call pushdecl
13677 for each of these. We must do them in reverse order so that
13678 they end in the correct forward order. */
13679 specparms = nreverse (specparms);
13681 for (parm = specparms; parm; parm = next)
13683 next = DECL_CHAIN (parm);
13684 if (TREE_CODE (parm) == PARM_DECL)
13686 if (DECL_NAME (parm) == NULL_TREE
13687 || !VOID_TYPE_P (parm))
13688 pushdecl (parm);
13689 else
13690 error ("parameter %qD declared void", parm);
13692 else
13694 /* If we find an enum constant or a type tag,
13695 put it aside for the moment. */
13696 TREE_CHAIN (parm) = NULL_TREE;
13697 nonparms = chainon (nonparms, parm);
13701 /* Get the decls in their original chain order and record in the
13702 function. This is all and only the PARM_DECLs that were
13703 pushed into scope by the loop above. */
13704 DECL_ARGUMENTS (fndecl) = getdecls ();
13706 else
13707 DECL_ARGUMENTS (fndecl) = NULL_TREE;
13709 /* Now store the final chain of decls for the arguments
13710 as the decl-chain of the current lexical scope.
13711 Put the enumerators in as well, at the front so that
13712 DECL_ARGUMENTS is not modified. */
13713 current_binding_level->names = chainon (nonparms, DECL_ARGUMENTS (fndecl));
13715 if (use_eh_spec_block (current_function_decl))
13716 current_eh_spec_block = begin_eh_spec_block ();
13720 /* We have finished doing semantic analysis on DECL, but have not yet
13721 generated RTL for its body. Save away our current state, so that
13722 when we want to generate RTL later we know what to do. */
13724 static void
13725 save_function_data (tree decl)
13727 struct language_function *f;
13729 /* Save the language-specific per-function data so that we can
13730 get it back when we really expand this function. */
13731 gcc_assert (!DECL_PENDING_INLINE_P (decl));
13733 /* Make a copy. */
13734 f = ggc_alloc<language_function> ();
13735 memcpy (f, cp_function_chain, sizeof (struct language_function));
13736 DECL_SAVED_FUNCTION_DATA (decl) = f;
13738 /* Clear out the bits we don't need. */
13739 f->base.x_stmt_tree.x_cur_stmt_list = NULL;
13740 f->bindings = NULL;
13741 f->x_local_names = NULL;
13742 f->base.local_typedefs = NULL;
13746 /* Set the return value of the constructor (if present). */
13748 static void
13749 finish_constructor_body (void)
13751 tree val;
13752 tree exprstmt;
13754 if (targetm.cxx.cdtor_returns_this ()
13755 && (! TYPE_FOR_JAVA (current_class_type)))
13757 /* Any return from a constructor will end up here. */
13758 add_stmt (build_stmt (input_location, LABEL_EXPR, cdtor_label));
13760 val = DECL_ARGUMENTS (current_function_decl);
13761 val = build2 (MODIFY_EXPR, TREE_TYPE (val),
13762 DECL_RESULT (current_function_decl), val);
13763 /* Return the address of the object. */
13764 exprstmt = build_stmt (input_location, RETURN_EXPR, val);
13765 add_stmt (exprstmt);
13769 /* Do all the processing for the beginning of a destructor; set up the
13770 vtable pointers and cleanups for bases and members. */
13772 static void
13773 begin_destructor_body (void)
13775 tree compound_stmt;
13777 /* If the CURRENT_CLASS_TYPE is incomplete, we will have already
13778 issued an error message. We still want to try to process the
13779 body of the function, but initialize_vtbl_ptrs will crash if
13780 TYPE_BINFO is NULL. */
13781 if (COMPLETE_TYPE_P (current_class_type))
13783 compound_stmt = begin_compound_stmt (0);
13784 /* Make all virtual function table pointers in non-virtual base
13785 classes point to CURRENT_CLASS_TYPE's virtual function
13786 tables. */
13787 initialize_vtbl_ptrs (current_class_ptr);
13788 finish_compound_stmt (compound_stmt);
13790 /* Insert a cleanup to let the back end know that the object is dead
13791 when we exit the destructor, either normally or via exception. */
13792 tree clobber = build_constructor (current_class_type, NULL);
13793 TREE_THIS_VOLATILE (clobber) = true;
13794 tree exprstmt = build2 (MODIFY_EXPR, current_class_type,
13795 current_class_ref, clobber);
13796 finish_decl_cleanup (NULL_TREE, exprstmt);
13798 /* And insert cleanups for our bases and members so that they
13799 will be properly destroyed if we throw. */
13800 push_base_cleanups ();
13804 /* At the end of every destructor we generate code to delete the object if
13805 necessary. Do that now. */
13807 static void
13808 finish_destructor_body (void)
13810 tree exprstmt;
13812 /* Any return from a destructor will end up here; that way all base
13813 and member cleanups will be run when the function returns. */
13814 add_stmt (build_stmt (input_location, LABEL_EXPR, cdtor_label));
13816 /* In a virtual destructor, we must call delete. */
13817 if (DECL_VIRTUAL_P (current_function_decl))
13819 tree if_stmt;
13820 tree virtual_size = cxx_sizeof (current_class_type);
13822 /* [class.dtor]
13824 At the point of definition of a virtual destructor (including
13825 an implicit definition), non-placement operator delete shall
13826 be looked up in the scope of the destructor's class and if
13827 found shall be accessible and unambiguous. */
13828 exprstmt = build_op_delete_call (DELETE_EXPR, current_class_ptr,
13829 virtual_size,
13830 /*global_p=*/false,
13831 /*placement=*/NULL_TREE,
13832 /*alloc_fn=*/NULL_TREE,
13833 tf_warning_or_error);
13835 if_stmt = begin_if_stmt ();
13836 finish_if_stmt_cond (build2 (BIT_AND_EXPR, integer_type_node,
13837 current_in_charge_parm,
13838 integer_one_node),
13839 if_stmt);
13840 finish_expr_stmt (exprstmt);
13841 finish_then_clause (if_stmt);
13842 finish_if_stmt (if_stmt);
13845 if (targetm.cxx.cdtor_returns_this ())
13847 tree val;
13849 val = DECL_ARGUMENTS (current_function_decl);
13850 val = build2 (MODIFY_EXPR, TREE_TYPE (val),
13851 DECL_RESULT (current_function_decl), val);
13852 /* Return the address of the object. */
13853 exprstmt = build_stmt (input_location, RETURN_EXPR, val);
13854 add_stmt (exprstmt);
13858 /* Do the necessary processing for the beginning of a function body, which
13859 in this case includes member-initializers, but not the catch clauses of
13860 a function-try-block. Currently, this means opening a binding level
13861 for the member-initializers (in a ctor), member cleanups (in a dtor),
13862 and capture proxies (in a lambda operator()). */
13864 tree
13865 begin_function_body (void)
13867 tree stmt;
13869 if (! FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
13870 return NULL_TREE;
13872 if (processing_template_decl)
13873 /* Do nothing now. */;
13874 else
13875 /* Always keep the BLOCK node associated with the outermost pair of
13876 curly braces of a function. These are needed for correct
13877 operation of dwarfout.c. */
13878 keep_next_level (true);
13880 stmt = begin_compound_stmt (BCS_FN_BODY);
13882 if (processing_template_decl)
13883 /* Do nothing now. */;
13884 else if (DECL_DESTRUCTOR_P (current_function_decl))
13885 begin_destructor_body ();
13887 return stmt;
13890 /* Do the processing for the end of a function body. Currently, this means
13891 closing out the cleanups for fully-constructed bases and members, and in
13892 the case of the destructor, deleting the object if desired. Again, this
13893 is only meaningful for [cd]tors, since they are the only functions where
13894 there is a significant distinction between the main body and any
13895 function catch clauses. Handling, say, main() return semantics here
13896 would be wrong, as flowing off the end of a function catch clause for
13897 main() would also need to return 0. */
13899 void
13900 finish_function_body (tree compstmt)
13902 if (compstmt == NULL_TREE)
13903 return;
13905 /* Close the block. */
13906 finish_compound_stmt (compstmt);
13908 if (processing_template_decl)
13909 /* Do nothing now. */;
13910 else if (DECL_CONSTRUCTOR_P (current_function_decl))
13911 finish_constructor_body ();
13912 else if (DECL_DESTRUCTOR_P (current_function_decl))
13913 finish_destructor_body ();
13916 /* Given a function, returns the BLOCK corresponding to the outermost level
13917 of curly braces, skipping the artificial block created for constructor
13918 initializers. */
13920 tree
13921 outer_curly_brace_block (tree fndecl)
13923 tree block = BLOCK_SUBBLOCKS (DECL_INITIAL (fndecl));
13924 if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
13925 /* Skip the artificial function body block. */
13926 block = BLOCK_SUBBLOCKS (block);
13927 return block;
13930 /* If FNDECL is a class's key method, add the class to the list of
13931 keyed classes that should be emitted. */
13933 static void
13934 record_key_method_defined (tree fndecl)
13936 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fndecl)
13937 && DECL_VIRTUAL_P (fndecl)
13938 && !processing_template_decl)
13940 tree fnclass = DECL_CONTEXT (fndecl);
13941 if (fndecl == CLASSTYPE_KEY_METHOD (fnclass))
13942 keyed_classes = tree_cons (NULL_TREE, fnclass, keyed_classes);
13946 /* Subroutine of finish_function.
13947 Save the body of constexpr functions for possible
13948 future compile time evaluation. */
13950 static void
13951 maybe_save_function_definition (tree fun)
13953 if (!processing_template_decl
13954 && DECL_DECLARED_CONSTEXPR_P (fun)
13955 && !DECL_CLONED_FUNCTION_P (fun))
13956 register_constexpr_fundef (fun, DECL_SAVED_TREE (fun));
13959 /* Finish up a function declaration and compile that function
13960 all the way to assembler language output. The free the storage
13961 for the function definition.
13963 FLAGS is a bitwise or of the following values:
13964 2 - INCLASS_INLINE
13965 We just finished processing the body of an in-class inline
13966 function definition. (This processing will have taken place
13967 after the class definition is complete.) */
13969 tree
13970 finish_function (int flags)
13972 tree fndecl = current_function_decl;
13973 tree fntype, ctype = NULL_TREE;
13974 int inclass_inline = (flags & 2) != 0;
13976 /* When we get some parse errors, we can end up without a
13977 current_function_decl, so cope. */
13978 if (fndecl == NULL_TREE)
13979 return error_mark_node;
13981 if (c_dialect_objc ())
13982 objc_finish_function ();
13984 gcc_assert (!defer_mark_used_calls);
13985 defer_mark_used_calls = true;
13987 record_key_method_defined (fndecl);
13989 fntype = TREE_TYPE (fndecl);
13991 /* TREE_READONLY (fndecl) = 1;
13992 This caused &foo to be of type ptr-to-const-function
13993 which then got a warning when stored in a ptr-to-function variable. */
13995 gcc_assert (building_stmt_list_p ());
13996 /* The current function is being defined, so its DECL_INITIAL should
13997 be set, and unless there's a multiple definition, it should be
13998 error_mark_node. */
13999 gcc_assert (DECL_INITIAL (fndecl) == error_mark_node);
14001 /* For a cloned function, we've already got all the code we need;
14002 there's no need to add any extra bits. */
14003 if (!DECL_CLONED_FUNCTION_P (fndecl))
14005 /* Make it so that `main' always returns 0 by default. */
14006 if (DECL_MAIN_P (current_function_decl))
14007 finish_return_stmt (integer_zero_node);
14009 if (use_eh_spec_block (current_function_decl))
14010 finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
14011 (TREE_TYPE (current_function_decl)),
14012 current_eh_spec_block);
14015 /* If we're saving up tree structure, tie off the function now. */
14016 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
14018 if (fn_contains_cilk_spawn_p (cfun) && !processing_template_decl)
14019 cfun->cilk_frame_decl = insert_cilk_frame (fndecl);
14021 finish_fname_decls ();
14023 /* If this function can't throw any exceptions, remember that. */
14024 if (!processing_template_decl
14025 && !cp_function_chain->can_throw
14026 && !flag_non_call_exceptions
14027 && !decl_replaceable_p (fndecl))
14028 TREE_NOTHROW (fndecl) = 1;
14030 /* This must come after expand_function_end because cleanups might
14031 have declarations (from inline functions) that need to go into
14032 this function's blocks. */
14034 /* If the current binding level isn't the outermost binding level
14035 for this function, either there is a bug, or we have experienced
14036 syntax errors and the statement tree is malformed. */
14037 if (current_binding_level->kind != sk_function_parms)
14039 /* Make sure we have already experienced errors. */
14040 gcc_assert (errorcount);
14042 /* Throw away the broken statement tree and extra binding
14043 levels. */
14044 DECL_SAVED_TREE (fndecl) = alloc_stmt_list ();
14046 while (current_binding_level->kind != sk_function_parms)
14048 if (current_binding_level->kind == sk_class)
14049 pop_nested_class ();
14050 else
14051 poplevel (0, 0, 0);
14054 poplevel (1, 0, 1);
14056 /* Statements should always be full-expressions at the outermost set
14057 of curly braces for a function. */
14058 gcc_assert (stmts_are_full_exprs_p ());
14060 /* If there are no return statements in a function with auto return type,
14061 the return type is void. But if the declared type is something like
14062 auto*, this is an error. */
14063 if (!processing_template_decl && FNDECL_USED_AUTO (fndecl)
14064 && TREE_TYPE (fntype) == current_function_auto_return_pattern)
14066 if (!is_auto (current_function_auto_return_pattern)
14067 && !current_function_returns_value && !current_function_returns_null)
14069 error ("no return statements in function returning %qT",
14070 current_function_auto_return_pattern);
14071 inform (input_location, "only plain %<auto%> return type can be "
14072 "deduced to %<void%>");
14074 apply_deduced_return_type (fndecl, void_type_node);
14075 fntype = TREE_TYPE (fndecl);
14078 /* Save constexpr function body before it gets munged by
14079 the NRV transformation. */
14080 maybe_save_function_definition (fndecl);
14082 /* Set up the named return value optimization, if we can. Candidate
14083 variables are selected in check_return_expr. */
14084 if (current_function_return_value)
14086 tree r = current_function_return_value;
14087 tree outer;
14089 if (r != error_mark_node
14090 /* This is only worth doing for fns that return in memory--and
14091 simpler, since we don't have to worry about promoted modes. */
14092 && aggregate_value_p (TREE_TYPE (TREE_TYPE (fndecl)), fndecl)
14093 /* Only allow this for variables declared in the outer scope of
14094 the function so we know that their lifetime always ends with a
14095 return; see g++.dg/opt/nrv6.C. We could be more flexible if
14096 we were to do this optimization in tree-ssa. */
14097 && (outer = outer_curly_brace_block (fndecl))
14098 && chain_member (r, BLOCK_VARS (outer)))
14099 finalize_nrv (&DECL_SAVED_TREE (fndecl), r, DECL_RESULT (fndecl));
14101 current_function_return_value = NULL_TREE;
14104 /* Remember that we were in class scope. */
14105 if (current_class_name)
14106 ctype = current_class_type;
14108 /* Must mark the RESULT_DECL as being in this function. */
14109 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
14111 /* Set the BLOCK_SUPERCONTEXT of the outermost function scope to point
14112 to the FUNCTION_DECL node itself. */
14113 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
14115 /* Save away current state, if appropriate. */
14116 if (!processing_template_decl)
14117 save_function_data (fndecl);
14119 /* Complain if there's just no return statement. */
14120 if (warn_return_type
14121 && !VOID_TYPE_P (TREE_TYPE (fntype))
14122 && !dependent_type_p (TREE_TYPE (fntype))
14123 && !current_function_returns_value && !current_function_returns_null
14124 /* Don't complain if we abort or throw. */
14125 && !current_function_returns_abnormally
14126 /* Don't complain if there's an infinite loop. */
14127 && !current_function_infinite_loop
14128 /* Don't complain if we are declared noreturn. */
14129 && !TREE_THIS_VOLATILE (fndecl)
14130 && !DECL_NAME (DECL_RESULT (fndecl))
14131 && !TREE_NO_WARNING (fndecl)
14132 /* Structor return values (if any) are set by the compiler. */
14133 && !DECL_CONSTRUCTOR_P (fndecl)
14134 && !DECL_DESTRUCTOR_P (fndecl)
14135 && targetm.warn_func_return (fndecl))
14137 warning (OPT_Wreturn_type,
14138 "no return statement in function returning non-void");
14139 TREE_NO_WARNING (fndecl) = 1;
14142 /* Store the end of the function, so that we get good line number
14143 info for the epilogue. */
14144 cfun->function_end_locus = input_location;
14146 /* Complain about parameters that are only set, but never otherwise used. */
14147 if (warn_unused_but_set_parameter
14148 && !processing_template_decl
14149 && errorcount == unused_but_set_errorcount
14150 && !DECL_CLONED_FUNCTION_P (fndecl))
14152 tree decl;
14154 for (decl = DECL_ARGUMENTS (fndecl);
14155 decl;
14156 decl = DECL_CHAIN (decl))
14157 if (TREE_USED (decl)
14158 && TREE_CODE (decl) == PARM_DECL
14159 && !DECL_READ_P (decl)
14160 && DECL_NAME (decl)
14161 && !DECL_ARTIFICIAL (decl)
14162 && !TREE_NO_WARNING (decl)
14163 && !DECL_IN_SYSTEM_HEADER (decl)
14164 && TREE_TYPE (decl) != error_mark_node
14165 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
14166 && (!CLASS_TYPE_P (TREE_TYPE (decl))
14167 || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl))))
14168 warning (OPT_Wunused_but_set_parameter,
14169 "parameter %q+D set but not used", decl);
14170 unused_but_set_errorcount = errorcount;
14173 /* Complain about locally defined typedefs that are not used in this
14174 function. */
14175 maybe_warn_unused_local_typedefs ();
14177 /* Genericize before inlining. */
14178 if (!processing_template_decl)
14180 struct language_function *f = DECL_SAVED_FUNCTION_DATA (fndecl);
14181 invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE, fndecl);
14182 cp_genericize (fndecl);
14183 /* Clear out the bits we don't need. */
14184 f->x_current_class_ptr = NULL;
14185 f->x_current_class_ref = NULL;
14186 f->x_eh_spec_block = NULL;
14187 f->x_in_charge_parm = NULL;
14188 f->x_vtt_parm = NULL;
14189 f->x_return_value = NULL;
14190 f->bindings = NULL;
14191 f->extern_decl_map = NULL;
14192 f->infinite_loops = NULL;
14194 /* Clear out the bits we don't need. */
14195 local_names = NULL;
14197 /* We're leaving the context of this function, so zap cfun. It's still in
14198 DECL_STRUCT_FUNCTION, and we'll restore it in tree_rest_of_compilation. */
14199 set_cfun (NULL);
14200 current_function_decl = NULL;
14202 /* If this is an in-class inline definition, we may have to pop the
14203 bindings for the template parameters that we added in
14204 maybe_begin_member_template_processing when start_function was
14205 called. */
14206 if (inclass_inline)
14207 maybe_end_member_template_processing ();
14209 /* Leave the scope of the class. */
14210 if (ctype)
14211 pop_nested_class ();
14213 --function_depth;
14215 /* Clean up. */
14216 current_function_decl = NULL_TREE;
14218 defer_mark_used_calls = false;
14219 if (deferred_mark_used_calls)
14221 unsigned int i;
14222 tree decl;
14224 FOR_EACH_VEC_SAFE_ELT (deferred_mark_used_calls, i, decl)
14225 mark_used (decl);
14226 vec_free (deferred_mark_used_calls);
14229 return fndecl;
14232 /* Create the FUNCTION_DECL for a function definition.
14233 DECLSPECS and DECLARATOR are the parts of the declaration;
14234 they describe the return type and the name of the function,
14235 but twisted together in a fashion that parallels the syntax of C.
14237 This function creates a binding context for the function body
14238 as well as setting up the FUNCTION_DECL in current_function_decl.
14240 Returns a FUNCTION_DECL on success.
14242 If the DECLARATOR is not suitable for a function (it defines a datum
14243 instead), we return 0, which tells yyparse to report a parse error.
14245 May return void_type_node indicating that this method is actually
14246 a friend. See grokfield for more details.
14248 Came here with a `.pushlevel' .
14250 DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
14251 CHANGES TO CODE IN `grokfield'. */
14253 tree
14254 grokmethod (cp_decl_specifier_seq *declspecs,
14255 const cp_declarator *declarator, tree attrlist)
14257 tree fndecl = grokdeclarator (declarator, declspecs, MEMFUNCDEF, 0,
14258 &attrlist);
14260 if (fndecl == error_mark_node)
14261 return error_mark_node;
14263 if (fndecl == NULL || TREE_CODE (fndecl) != FUNCTION_DECL)
14265 error ("invalid member function declaration");
14266 return error_mark_node;
14269 if (attrlist)
14270 cplus_decl_attributes (&fndecl, attrlist, 0);
14272 /* Pass friends other than inline friend functions back. */
14273 if (fndecl == void_type_node)
14274 return fndecl;
14276 if (DECL_IN_AGGR_P (fndecl))
14278 if (DECL_CLASS_SCOPE_P (fndecl))
14279 error ("%qD is already defined in class %qT", fndecl,
14280 DECL_CONTEXT (fndecl));
14281 return error_mark_node;
14284 check_template_shadow (fndecl);
14286 DECL_COMDAT (fndecl) = 1;
14287 DECL_DECLARED_INLINE_P (fndecl) = 1;
14288 DECL_NO_INLINE_WARNING_P (fndecl) = 1;
14290 /* We process method specializations in finish_struct_1. */
14291 if (processing_template_decl && !DECL_TEMPLATE_SPECIALIZATION (fndecl))
14293 fndecl = push_template_decl (fndecl);
14294 if (fndecl == error_mark_node)
14295 return fndecl;
14298 if (! DECL_FRIEND_P (fndecl))
14300 if (DECL_CHAIN (fndecl))
14302 fndecl = copy_node (fndecl);
14303 TREE_CHAIN (fndecl) = NULL_TREE;
14307 cp_finish_decl (fndecl, NULL_TREE, false, NULL_TREE, 0);
14309 DECL_IN_AGGR_P (fndecl) = 1;
14310 return fndecl;
14314 /* VAR is a VAR_DECL. If its type is incomplete, remember VAR so that
14315 we can lay it out later, when and if its type becomes complete.
14317 Also handle constexpr pointer to member variables where the initializer
14318 is an unlowered PTRMEM_CST because the class isn't complete yet. */
14320 void
14321 maybe_register_incomplete_var (tree var)
14323 gcc_assert (VAR_P (var));
14325 /* Keep track of variables with incomplete types. */
14326 if (!processing_template_decl && TREE_TYPE (var) != error_mark_node
14327 && DECL_EXTERNAL (var))
14329 tree inner_type = TREE_TYPE (var);
14331 while (TREE_CODE (inner_type) == ARRAY_TYPE)
14332 inner_type = TREE_TYPE (inner_type);
14333 inner_type = TYPE_MAIN_VARIANT (inner_type);
14335 if ((!COMPLETE_TYPE_P (inner_type) && CLASS_TYPE_P (inner_type))
14336 /* RTTI TD entries are created while defining the type_info. */
14337 || (TYPE_LANG_SPECIFIC (inner_type)
14338 && TYPE_BEING_DEFINED (inner_type)))
14340 incomplete_var iv = {var, inner_type};
14341 vec_safe_push (incomplete_vars, iv);
14343 else if (TYPE_PTRMEM_P (inner_type)
14344 && DECL_INITIAL (var)
14345 && TREE_CODE (DECL_INITIAL (var)) == PTRMEM_CST)
14347 tree context = TYPE_PTRMEM_CLASS_TYPE (inner_type);
14348 gcc_assert (TYPE_BEING_DEFINED (context));
14349 incomplete_var iv = {var, context};
14350 vec_safe_push (incomplete_vars, iv);
14355 /* Called when a class type (given by TYPE) is defined. If there are
14356 any existing VAR_DECLs whose type has been completed by this
14357 declaration, update them now. */
14359 void
14360 complete_vars (tree type)
14362 unsigned ix;
14363 incomplete_var *iv;
14365 for (ix = 0; vec_safe_iterate (incomplete_vars, ix, &iv); )
14367 if (same_type_p (type, iv->incomplete_type))
14369 tree var = iv->decl;
14370 tree type = TREE_TYPE (var);
14372 if (TYPE_PTRMEM_P (type))
14373 DECL_INITIAL (var) = cplus_expand_constant (DECL_INITIAL (var));
14374 else
14376 /* Complete the type of the variable. The VAR_DECL itself
14377 will be laid out in expand_expr. */
14378 complete_type (type);
14379 cp_apply_type_quals_to_decl (cp_type_quals (type), var);
14382 /* Remove this entry from the list. */
14383 incomplete_vars->unordered_remove (ix);
14385 else
14386 ix++;
14389 /* Check for pending declarations which may have abstract type. */
14390 complete_type_check_abstract (type);
14393 /* If DECL is of a type which needs a cleanup, build and return an
14394 expression to perform that cleanup here. Return NULL_TREE if no
14395 cleanup need be done. */
14397 tree
14398 cxx_maybe_build_cleanup (tree decl, tsubst_flags_t complain)
14400 tree type;
14401 tree attr;
14402 tree cleanup;
14404 /* Assume no cleanup is required. */
14405 cleanup = NULL_TREE;
14407 if (error_operand_p (decl))
14408 return cleanup;
14410 /* Handle "__attribute__((cleanup))". We run the cleanup function
14411 before the destructor since the destructor is what actually
14412 terminates the lifetime of the object. */
14413 attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
14414 if (attr)
14416 tree id;
14417 tree fn;
14418 tree arg;
14420 /* Get the name specified by the user for the cleanup function. */
14421 id = TREE_VALUE (TREE_VALUE (attr));
14422 /* Look up the name to find the cleanup function to call. It is
14423 important to use lookup_name here because that is what is
14424 used in c-common.c:handle_cleanup_attribute when performing
14425 initial checks on the attribute. Note that those checks
14426 include ensuring that the function found is not an overloaded
14427 function, or an object with an overloaded call operator,
14428 etc.; we can rely on the fact that the function found is an
14429 ordinary FUNCTION_DECL. */
14430 fn = lookup_name (id);
14431 arg = build_address (decl);
14432 mark_used (decl);
14433 cleanup = cp_build_function_call_nary (fn, complain, arg, NULL_TREE);
14434 if (cleanup == error_mark_node)
14435 return error_mark_node;
14437 /* Handle ordinary C++ destructors. */
14438 type = TREE_TYPE (decl);
14439 if (type_build_dtor_call (type))
14441 int flags = LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR;
14442 tree addr;
14443 tree call;
14445 if (TREE_CODE (type) == ARRAY_TYPE)
14446 addr = decl;
14447 else
14448 addr = build_address (decl);
14450 call = build_delete (TREE_TYPE (addr), addr,
14451 sfk_complete_destructor, flags, 0, complain);
14452 if (call == error_mark_node)
14453 cleanup = error_mark_node;
14454 else if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
14455 /* Discard the call. */;
14456 else if (cleanup)
14457 cleanup = cp_build_compound_expr (cleanup, call, complain);
14458 else
14459 cleanup = call;
14462 /* build_delete sets the location of the destructor call to the
14463 current location, even though the destructor is going to be
14464 called later, at the end of the current scope. This can lead to
14465 a "jumpy" behaviour for users of debuggers when they step around
14466 the end of the block. So let's unset the location of the
14467 destructor call instead. */
14468 if (cleanup != NULL && EXPR_P (cleanup))
14469 SET_EXPR_LOCATION (cleanup, UNKNOWN_LOCATION);
14471 if (cleanup
14472 && !lookup_attribute ("warn_unused", TYPE_ATTRIBUTES (TREE_TYPE (decl))))
14473 /* Treat objects with destructors as used; the destructor may do
14474 something substantive. */
14475 mark_used (decl);
14477 return cleanup;
14481 /* Return the FUNCTION_TYPE that corresponds to MEMFNTYPE, which can be a
14482 FUNCTION_DECL, METHOD_TYPE, FUNCTION_TYPE, pointer or reference to
14483 METHOD_TYPE or FUNCTION_TYPE, or pointer to member function. */
14485 tree
14486 static_fn_type (tree memfntype)
14488 tree fntype;
14489 tree args;
14491 if (TYPE_PTRMEMFUNC_P (memfntype))
14492 memfntype = TYPE_PTRMEMFUNC_FN_TYPE (memfntype);
14493 if (POINTER_TYPE_P (memfntype)
14494 || TREE_CODE (memfntype) == FUNCTION_DECL)
14495 memfntype = TREE_TYPE (memfntype);
14496 if (TREE_CODE (memfntype) == FUNCTION_TYPE)
14497 return memfntype;
14498 gcc_assert (TREE_CODE (memfntype) == METHOD_TYPE);
14499 args = TYPE_ARG_TYPES (memfntype);
14500 cp_ref_qualifier rqual = type_memfn_rqual (memfntype);
14501 fntype = build_function_type (TREE_TYPE (memfntype), TREE_CHAIN (args));
14502 fntype = apply_memfn_quals (fntype, type_memfn_quals (memfntype), rqual);
14503 fntype = (cp_build_type_attribute_variant
14504 (fntype, TYPE_ATTRIBUTES (memfntype)));
14505 fntype = (build_exception_variant
14506 (fntype, TYPE_RAISES_EXCEPTIONS (memfntype)));
14507 if (TYPE_HAS_LATE_RETURN_TYPE (memfntype))
14508 TYPE_HAS_LATE_RETURN_TYPE (fntype) = 1;
14509 return fntype;
14512 /* DECL was originally constructed as a non-static member function,
14513 but turned out to be static. Update it accordingly. */
14515 void
14516 revert_static_member_fn (tree decl)
14518 tree stype = static_fn_type (decl);
14519 cp_cv_quals quals = type_memfn_quals (stype);
14520 cp_ref_qualifier rqual = type_memfn_rqual (stype);
14522 if (quals != TYPE_UNQUALIFIED || rqual != REF_QUAL_NONE)
14523 stype = apply_memfn_quals (stype, TYPE_UNQUALIFIED, REF_QUAL_NONE);
14525 TREE_TYPE (decl) = stype;
14527 if (DECL_ARGUMENTS (decl))
14528 DECL_ARGUMENTS (decl) = DECL_CHAIN (DECL_ARGUMENTS (decl));
14529 DECL_STATIC_FUNCTION_P (decl) = 1;
14532 /* Return which tree structure is used by T, or TS_CP_GENERIC if T is
14533 one of the language-independent trees. */
14535 enum cp_tree_node_structure_enum
14536 cp_tree_node_structure (union lang_tree_node * t)
14538 switch (TREE_CODE (&t->generic))
14540 case DEFAULT_ARG: return TS_CP_DEFAULT_ARG;
14541 case DEFERRED_NOEXCEPT: return TS_CP_DEFERRED_NOEXCEPT;
14542 case IDENTIFIER_NODE: return TS_CP_IDENTIFIER;
14543 case OVERLOAD: return TS_CP_OVERLOAD;
14544 case TEMPLATE_PARM_INDEX: return TS_CP_TPI;
14545 case PTRMEM_CST: return TS_CP_PTRMEM;
14546 case BASELINK: return TS_CP_BASELINK;
14547 case TEMPLATE_DECL: return TS_CP_TEMPLATE_DECL;
14548 case STATIC_ASSERT: return TS_CP_STATIC_ASSERT;
14549 case ARGUMENT_PACK_SELECT: return TS_CP_ARGUMENT_PACK_SELECT;
14550 case TRAIT_EXPR: return TS_CP_TRAIT_EXPR;
14551 case LAMBDA_EXPR: return TS_CP_LAMBDA_EXPR;
14552 case TEMPLATE_INFO: return TS_CP_TEMPLATE_INFO;
14553 case USERDEF_LITERAL: return TS_CP_USERDEF_LITERAL;
14554 default: return TS_CP_GENERIC;
14558 /* Build the void_list_node (void_type_node having been created). */
14559 tree
14560 build_void_list_node (void)
14562 tree t = build_tree_list (NULL_TREE, void_type_node);
14563 return t;
14566 bool
14567 cp_missing_noreturn_ok_p (tree decl)
14569 /* A missing noreturn is ok for the `main' function. */
14570 return DECL_MAIN_P (decl);
14573 /* Return the decl used to identify the COMDAT group into which DECL should
14574 be placed. */
14576 tree
14577 cxx_comdat_group (tree decl)
14579 /* Virtual tables, construction virtual tables, and virtual table
14580 tables all go in a single COMDAT group, named after the primary
14581 virtual table. */
14582 if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl))
14583 decl = CLASSTYPE_VTABLES (DECL_CONTEXT (decl));
14584 /* For all other DECLs, the COMDAT group is the mangled name of the
14585 declaration itself. */
14586 else
14588 while (DECL_THUNK_P (decl))
14590 /* If TARGET_USE_LOCAL_THUNK_ALIAS_P, use_thunk puts the thunk
14591 into the same section as the target function. In that case
14592 we must return target's name. */
14593 tree target = THUNK_TARGET (decl);
14594 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (target)
14595 && DECL_SECTION_NAME (target) != NULL
14596 && DECL_ONE_ONLY (target))
14597 decl = target;
14598 else
14599 break;
14603 return decl;
14606 /* Returns the return type for FN as written by the user, which may include
14607 a placeholder for a deduced return type. */
14609 tree
14610 fndecl_declared_return_type (tree fn)
14612 fn = STRIP_TEMPLATE (fn);
14613 if (FNDECL_USED_AUTO (fn))
14615 struct language_function *f = NULL;
14616 if (DECL_STRUCT_FUNCTION (fn))
14617 f = DECL_STRUCT_FUNCTION (fn)->language;
14618 if (f == NULL)
14619 f = DECL_SAVED_FUNCTION_DATA (fn);
14620 return f->x_auto_return_pattern;
14622 return TREE_TYPE (TREE_TYPE (fn));
14625 /* Returns true iff DECL was declared with an auto return type and it has
14626 not yet been deduced to a real type. */
14628 bool
14629 undeduced_auto_decl (tree decl)
14631 if (cxx_dialect < cxx1y)
14632 return false;
14633 return type_uses_auto (TREE_TYPE (decl));
14636 /* Complain if DECL has an undeduced return type. */
14638 void
14639 require_deduced_type (tree decl)
14641 if (undeduced_auto_decl (decl))
14642 error ("use of %qD before deduction of %<auto%>", decl);
14645 #include "gt-cp-decl.h"