PR c++/64901
[official-gcc.git] / gcc / cp / decl.c
blobd2b0814543d561c85eee81633d1d002dea50784e
1 /* Process declarations and variables for C++ compiler.
2 Copyright (C) 1988-2015 Free Software Foundation, Inc.
3 Contributed by Michael Tiemann (tiemann@cygnus.com)
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 /* Process declarations and symbol lookup for C++ front end.
23 Also constructs types; the standard scalar types at initialization,
24 and structure, union, array and enum types when they are declared. */
26 /* ??? not all decl nodes are given the most useful possible
27 line numbers. For example, the CONST_DECLs for enum values. */
29 #include "config.h"
30 #include "system.h"
31 #include "coretypes.h"
32 #include "tm.h"
33 #include "hash-set.h"
34 #include "machmode.h"
35 #include "vec.h"
36 #include "double-int.h"
37 #include "input.h"
38 #include "alias.h"
39 #include "symtab.h"
40 #include "wide-int.h"
41 #include "inchash.h"
42 #include "tree.h"
43 #include "tree-hasher.h"
44 #include "stringpool.h"
45 #include "stor-layout.h"
46 #include "varasm.h"
47 #include "attribs.h"
48 #include "calls.h"
49 #include "flags.h"
50 #include "cp-tree.h"
51 #include "tree-iterator.h"
52 #include "tree-inline.h"
53 #include "decl.h"
54 #include "intl.h"
55 #include "toplev.h"
56 #include "hashtab.h"
57 #include "tm_p.h"
58 #include "target.h"
59 #include "c-family/c-common.h"
60 #include "c-family/c-objc.h"
61 #include "c-family/c-pragma.h"
62 #include "c-family/c-target.h"
63 #include "c-family/c-ubsan.h"
64 #include "diagnostic.h"
65 #include "intl.h"
66 #include "debug.h"
67 #include "timevar.h"
68 #include "splay-tree.h"
69 #include "plugin.h"
70 #include "hash-map.h"
71 #include "is-a.h"
72 #include "plugin-api.h"
73 #include "hard-reg-set.h"
74 #include "input.h"
75 #include "function.h"
76 #include "ipa-ref.h"
77 #include "cgraph.h"
78 #include "cilk.h"
79 #include "wide-int.h"
80 #include "builtins.h"
82 /* Possible cases of bad specifiers type used by bad_specifiers. */
83 enum bad_spec_place {
84 BSP_VAR, /* variable */
85 BSP_PARM, /* parameter */
86 BSP_TYPE, /* type */
87 BSP_FIELD /* field */
90 static tree grokparms (tree parmlist, tree *);
91 static const char *redeclaration_error_message (tree, tree);
93 static int decl_jump_unsafe (tree);
94 static void require_complete_types_for_parms (tree);
95 static int ambi_op_p (enum tree_code);
96 static int unary_op_p (enum tree_code);
97 static void push_local_name (tree);
98 static tree grok_reference_init (tree, tree, tree, int);
99 static tree grokvardecl (tree, tree, tree, const cp_decl_specifier_seq *,
100 int, int, int, tree);
101 static int check_static_variable_definition (tree, tree);
102 static void record_unknown_type (tree, const char *);
103 static tree builtin_function_1 (tree, tree, bool);
104 static int member_function_or_else (tree, tree, enum overload_flags);
105 static void bad_specifiers (tree, enum bad_spec_place, int, int, int, int,
106 int);
107 static void check_for_uninitialized_const_var (tree);
108 static tree local_variable_p_walkfn (tree *, int *, void *);
109 static tree record_builtin_java_type (const char *, int);
110 static const char *tag_name (enum tag_types);
111 static tree lookup_and_check_tag (enum tag_types, tree, tag_scope, bool);
112 static int walk_namespaces_r (tree, walk_namespaces_fn, void *);
113 static void maybe_deduce_size_from_array_init (tree, tree);
114 static void layout_var_decl (tree);
115 static tree check_initializer (tree, tree, int, vec<tree, va_gc> **);
116 static void make_rtl_for_nonlocal_decl (tree, tree, const char *);
117 static void save_function_data (tree);
118 static void copy_type_enum (tree , tree);
119 static void check_function_type (tree, tree);
120 static void finish_constructor_body (void);
121 static void begin_destructor_body (void);
122 static void finish_destructor_body (void);
123 static void record_key_method_defined (tree);
124 static tree create_array_type_for_decl (tree, tree, tree);
125 static tree get_atexit_node (void);
126 static tree get_dso_handle_node (void);
127 static tree start_cleanup_fn (void);
128 static void end_cleanup_fn (void);
129 static tree cp_make_fname_decl (location_t, tree, int);
130 static void initialize_predefined_identifiers (void);
131 static tree check_special_function_return_type
132 (special_function_kind, tree, tree);
133 static tree push_cp_library_fn (enum tree_code, tree, int);
134 static tree build_cp_library_fn (tree, enum tree_code, tree, int);
135 static void store_parm_decls (tree);
136 static void initialize_local_var (tree, tree);
137 static void expand_static_init (tree, tree);
139 /* The following symbols are subsumed in the cp_global_trees array, and
140 listed here individually for documentation purposes.
142 C++ extensions
143 tree wchar_decl_node;
145 tree vtable_entry_type;
146 tree delta_type_node;
147 tree __t_desc_type_node;
149 tree class_type_node;
150 tree unknown_type_node;
152 Array type `vtable_entry_type[]'
154 tree vtbl_type_node;
155 tree vtbl_ptr_type_node;
157 Namespaces,
159 tree std_node;
160 tree abi_node;
162 A FUNCTION_DECL which can call `abort'. Not necessarily the
163 one that the user will declare, but sufficient to be called
164 by routines that want to abort the program.
166 tree abort_fndecl;
168 Used by RTTI
169 tree type_info_type_node, tinfo_decl_id, tinfo_decl_type;
170 tree tinfo_var_id; */
172 tree cp_global_trees[CPTI_MAX];
174 /* Indicates that there is a type value in some namespace, although
175 that is not necessarily in scope at the moment. */
177 tree global_type_node;
179 /* The node that holds the "name" of the global scope. */
180 tree global_scope_name;
182 #define local_names cp_function_chain->x_local_names
184 /* A list of objects which have constructors or destructors
185 which reside in the global scope. The decl is stored in
186 the TREE_VALUE slot and the initializer is stored
187 in the TREE_PURPOSE slot. */
188 tree static_aggregates;
190 /* Like static_aggregates, but for thread_local variables. */
191 tree tls_aggregates;
193 /* -- end of C++ */
195 /* A node for the integer constant 2. */
197 tree integer_two_node;
199 /* Used only for jumps to as-yet undefined labels, since jumps to
200 defined labels can have their validity checked immediately. */
202 struct GTY((chain_next ("%h.next"))) named_label_use_entry {
203 struct named_label_use_entry *next;
204 /* The binding level to which this entry is *currently* attached.
205 This is initially the binding level in which the goto appeared,
206 but is modified as scopes are closed. */
207 cp_binding_level *binding_level;
208 /* The head of the names list that was current when the goto appeared,
209 or the inner scope popped. These are the decls that will *not* be
210 skipped when jumping to the label. */
211 tree names_in_scope;
212 /* The location of the goto, for error reporting. */
213 location_t o_goto_locus;
214 /* True if an OpenMP structured block scope has been closed since
215 the goto appeared. This means that the branch from the label will
216 illegally exit an OpenMP scope. */
217 bool in_omp_scope;
220 /* A list of all LABEL_DECLs in the function that have names. Here so
221 we can clear out their names' definitions at the end of the
222 function, and so we can check the validity of jumps to these labels. */
224 struct GTY((for_user)) named_label_entry {
225 /* The decl itself. */
226 tree label_decl;
228 /* The binding level to which the label is *currently* attached.
229 This is initially set to the binding level in which the label
230 is defined, but is modified as scopes are closed. */
231 cp_binding_level *binding_level;
232 /* The head of the names list that was current when the label was
233 defined, or the inner scope popped. These are the decls that will
234 be skipped when jumping to the label. */
235 tree names_in_scope;
236 /* A vector of all decls from all binding levels that would be
237 crossed by a backward branch to the label. */
238 vec<tree, va_gc> *bad_decls;
240 /* A list of uses of the label, before the label is defined. */
241 struct named_label_use_entry *uses;
243 /* The following bits are set after the label is defined, and are
244 updated as scopes are popped. They indicate that a backward jump
245 to the label will illegally enter a scope of the given flavor. */
246 bool in_try_scope;
247 bool in_catch_scope;
248 bool in_omp_scope;
251 #define named_labels cp_function_chain->x_named_labels
253 /* The number of function bodies which we are currently processing.
254 (Zero if we are at namespace scope, one inside the body of a
255 function, two inside the body of a function in a local class, etc.) */
256 int function_depth;
258 /* To avoid unwanted recursion, finish_function defers all mark_used calls
259 encountered during its execution until it finishes. */
260 bool defer_mark_used_calls;
261 vec<tree, va_gc> *deferred_mark_used_calls;
263 /* States indicating how grokdeclarator() should handle declspecs marked
264 with __attribute__((deprecated)). An object declared as
265 __attribute__((deprecated)) suppresses warnings of uses of other
266 deprecated items. */
267 enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
270 /* A list of VAR_DECLs whose type was incomplete at the time the
271 variable was declared. */
273 typedef struct GTY(()) incomplete_var_d {
274 tree decl;
275 tree incomplete_type;
276 } incomplete_var;
279 static GTY(()) vec<incomplete_var, va_gc> *incomplete_vars;
281 /* Returns the kind of template specialization we are currently
282 processing, given that it's declaration contained N_CLASS_SCOPES
283 explicit scope qualifications. */
285 tmpl_spec_kind
286 current_tmpl_spec_kind (int n_class_scopes)
288 int n_template_parm_scopes = 0;
289 int seen_specialization_p = 0;
290 int innermost_specialization_p = 0;
291 cp_binding_level *b;
293 /* Scan through the template parameter scopes. */
294 for (b = current_binding_level;
295 b->kind == sk_template_parms;
296 b = b->level_chain)
298 /* If we see a specialization scope inside a parameter scope,
299 then something is wrong. That corresponds to a declaration
300 like:
302 template <class T> template <> ...
304 which is always invalid since [temp.expl.spec] forbids the
305 specialization of a class member template if the enclosing
306 class templates are not explicitly specialized as well. */
307 if (b->explicit_spec_p)
309 if (n_template_parm_scopes == 0)
310 innermost_specialization_p = 1;
311 else
312 seen_specialization_p = 1;
314 else if (seen_specialization_p == 1)
315 return tsk_invalid_member_spec;
317 ++n_template_parm_scopes;
320 /* Handle explicit instantiations. */
321 if (processing_explicit_instantiation)
323 if (n_template_parm_scopes != 0)
324 /* We've seen a template parameter list during an explicit
325 instantiation. For example:
327 template <class T> template void f(int);
329 This is erroneous. */
330 return tsk_invalid_expl_inst;
331 else
332 return tsk_expl_inst;
335 if (n_template_parm_scopes < n_class_scopes)
336 /* We've not seen enough template headers to match all the
337 specialized classes present. For example:
339 template <class T> void R<T>::S<T>::f(int);
341 This is invalid; there needs to be one set of template
342 parameters for each class. */
343 return tsk_insufficient_parms;
344 else if (n_template_parm_scopes == n_class_scopes)
345 /* We're processing a non-template declaration (even though it may
346 be a member of a template class.) For example:
348 template <class T> void S<T>::f(int);
350 The `class T' matches the `S<T>', leaving no template headers
351 corresponding to the `f'. */
352 return tsk_none;
353 else if (n_template_parm_scopes > n_class_scopes + 1)
354 /* We've got too many template headers. For example:
356 template <> template <class T> void f (T);
358 There need to be more enclosing classes. */
359 return tsk_excessive_parms;
360 else
361 /* This must be a template. It's of the form:
363 template <class T> template <class U> void S<T>::f(U);
365 This is a specialization if the innermost level was a
366 specialization; otherwise it's just a definition of the
367 template. */
368 return innermost_specialization_p ? tsk_expl_spec : tsk_template;
371 /* Exit the current scope. */
373 void
374 finish_scope (void)
376 poplevel (0, 0, 0);
379 /* When a label goes out of scope, check to see if that label was used
380 in a valid manner, and issue any appropriate warnings or errors. */
382 static void
383 pop_label (tree label, tree old_value)
385 if (!processing_template_decl)
387 if (DECL_INITIAL (label) == NULL_TREE)
389 location_t location;
391 error ("label %q+D used but not defined", label);
392 location = input_location;
393 /* FIXME want (LOCATION_FILE (input_location), (line)0) */
394 /* Avoid crashing later. */
395 define_label (location, DECL_NAME (label));
397 else
398 warn_for_unused_label (label);
401 SET_IDENTIFIER_LABEL_VALUE (DECL_NAME (label), old_value);
404 /* At the end of a function, all labels declared within the function
405 go out of scope. BLOCK is the top-level block for the
406 function. */
409 pop_labels_1 (named_label_entry **slot, tree block)
411 struct named_label_entry *ent = *slot;
413 pop_label (ent->label_decl, NULL_TREE);
415 /* Put the labels into the "variables" of the top-level block,
416 so debugger can see them. */
417 DECL_CHAIN (ent->label_decl) = BLOCK_VARS (block);
418 BLOCK_VARS (block) = ent->label_decl;
420 named_labels->clear_slot (slot);
422 return 1;
425 static void
426 pop_labels (tree block)
428 if (named_labels)
430 named_labels->traverse<tree, pop_labels_1> (block);
431 named_labels = NULL;
435 /* At the end of a block with local labels, restore the outer definition. */
437 static void
438 pop_local_label (tree label, tree old_value)
440 struct named_label_entry dummy;
442 pop_label (label, old_value);
444 dummy.label_decl = label;
445 named_label_entry **slot = named_labels->find_slot (&dummy, NO_INSERT);
446 named_labels->clear_slot (slot);
449 /* The following two routines are used to interface to Objective-C++.
450 The binding level is purposely treated as an opaque type. */
452 void *
453 objc_get_current_scope (void)
455 return current_binding_level;
458 /* The following routine is used by the NeXT-style SJLJ exceptions;
459 variables get marked 'volatile' so as to not be clobbered by
460 _setjmp()/_longjmp() calls. All variables in the current scope,
461 as well as parent scopes up to (but not including) ENCLOSING_BLK
462 shall be thusly marked. */
464 void
465 objc_mark_locals_volatile (void *enclosing_blk)
467 cp_binding_level *scope;
469 for (scope = current_binding_level;
470 scope && scope != enclosing_blk;
471 scope = scope->level_chain)
473 tree decl;
475 for (decl = scope->names; decl; decl = TREE_CHAIN (decl))
476 objc_volatilize_decl (decl);
478 /* Do not climb up past the current function. */
479 if (scope->kind == sk_function_parms)
480 break;
484 /* Update data for defined and undefined labels when leaving a scope. */
487 poplevel_named_label_1 (named_label_entry **slot, cp_binding_level *bl)
489 named_label_entry *ent = *slot;
490 cp_binding_level *obl = bl->level_chain;
492 if (ent->binding_level == bl)
494 tree decl;
496 /* ENT->NAMES_IN_SCOPE may contain a mixture of DECLs and
497 TREE_LISTs representing OVERLOADs, so be careful. */
498 for (decl = ent->names_in_scope; decl; decl = (DECL_P (decl)
499 ? DECL_CHAIN (decl)
500 : TREE_CHAIN (decl)))
501 if (decl_jump_unsafe (decl))
502 vec_safe_push (ent->bad_decls, decl);
504 ent->binding_level = obl;
505 ent->names_in_scope = obl->names;
506 switch (bl->kind)
508 case sk_try:
509 ent->in_try_scope = true;
510 break;
511 case sk_catch:
512 ent->in_catch_scope = true;
513 break;
514 case sk_omp:
515 ent->in_omp_scope = true;
516 break;
517 default:
518 break;
521 else if (ent->uses)
523 struct named_label_use_entry *use;
525 for (use = ent->uses; use ; use = use->next)
526 if (use->binding_level == bl)
528 use->binding_level = obl;
529 use->names_in_scope = obl->names;
530 if (bl->kind == sk_omp)
531 use->in_omp_scope = true;
535 return 1;
538 /* Saved errorcount to avoid -Wunused-but-set-{parameter,variable} warnings
539 when errors were reported, except for -Werror-unused-but-set-*. */
540 static int unused_but_set_errorcount;
542 /* Exit a binding level.
543 Pop the level off, and restore the state of the identifier-decl mappings
544 that were in effect when this level was entered.
546 If KEEP == 1, this level had explicit declarations, so
547 and create a "block" (a BLOCK node) for the level
548 to record its declarations and subblocks for symbol table output.
550 If FUNCTIONBODY is nonzero, this level is the body of a function,
551 so create a block as if KEEP were set and also clear out all
552 label names.
554 If REVERSE is nonzero, reverse the order of decls before putting
555 them into the BLOCK. */
557 tree
558 poplevel (int keep, int reverse, int functionbody)
560 tree link;
561 /* The chain of decls was accumulated in reverse order.
562 Put it into forward order, just for cleanliness. */
563 tree decls;
564 tree subblocks;
565 tree block;
566 tree decl;
567 int leaving_for_scope;
568 scope_kind kind;
569 unsigned ix;
570 cp_label_binding *label_bind;
572 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
573 restart:
575 block = NULL_TREE;
577 gcc_assert (current_binding_level->kind != sk_class);
579 if (current_binding_level->kind == sk_cleanup)
580 functionbody = 0;
581 subblocks = functionbody >= 0 ? current_binding_level->blocks : 0;
583 gcc_assert (!vec_safe_length (current_binding_level->class_shadowed));
585 /* We used to use KEEP == 2 to indicate that the new block should go
586 at the beginning of the list of blocks at this binding level,
587 rather than the end. This hack is no longer used. */
588 gcc_assert (keep == 0 || keep == 1);
590 if (current_binding_level->keep)
591 keep = 1;
593 /* Any uses of undefined labels, and any defined labels, now operate
594 under constraints of next binding contour. */
595 if (cfun && !functionbody && named_labels)
596 named_labels->traverse<cp_binding_level *, poplevel_named_label_1>
597 (current_binding_level);
599 /* Get the decls in the order they were written.
600 Usually current_binding_level->names is in reverse order.
601 But parameter decls were previously put in forward order. */
603 if (reverse)
604 current_binding_level->names
605 = decls = nreverse (current_binding_level->names);
606 else
607 decls = current_binding_level->names;
609 /* If there were any declarations or structure tags in that level,
610 or if this level is a function body,
611 create a BLOCK to record them for the life of this function. */
612 block = NULL_TREE;
613 if (keep == 1 || functionbody)
614 block = make_node (BLOCK);
615 if (block != NULL_TREE)
617 BLOCK_VARS (block) = decls;
618 BLOCK_SUBBLOCKS (block) = subblocks;
621 /* In each subblock, record that this is its superior. */
622 if (keep >= 0)
623 for (link = subblocks; link; link = BLOCK_CHAIN (link))
624 BLOCK_SUPERCONTEXT (link) = block;
626 /* We still support the old for-scope rules, whereby the variables
627 in a for-init statement were in scope after the for-statement
628 ended. We only use the new rules if flag_new_for_scope is
629 nonzero. */
630 leaving_for_scope
631 = current_binding_level->kind == sk_for && flag_new_for_scope == 1;
633 /* Before we remove the declarations first check for unused variables. */
634 if ((warn_unused_variable || warn_unused_but_set_variable)
635 && current_binding_level->kind != sk_template_parms
636 && !processing_template_decl)
637 for (tree d = getdecls (); d; d = TREE_CHAIN (d))
639 /* There are cases where D itself is a TREE_LIST. See in
640 push_local_binding where the list of decls returned by
641 getdecls is built. */
642 decl = TREE_CODE (d) == TREE_LIST ? TREE_VALUE (d) : d;
643 tree type = TREE_TYPE (decl);
644 if (VAR_P (decl)
645 && (! TREE_USED (decl) || !DECL_READ_P (decl))
646 && ! DECL_IN_SYSTEM_HEADER (decl)
647 && DECL_NAME (decl) && ! DECL_ARTIFICIAL (decl)
648 && type != error_mark_node
649 && (!CLASS_TYPE_P (type)
650 || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
651 || lookup_attribute ("warn_unused",
652 TYPE_ATTRIBUTES (TREE_TYPE (decl)))))
654 if (! TREE_USED (decl))
655 warning (OPT_Wunused_variable, "unused variable %q+D", decl);
656 else if (DECL_CONTEXT (decl) == current_function_decl
657 // For -Wunused-but-set-variable leave references alone.
658 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
659 && errorcount == unused_but_set_errorcount)
661 warning (OPT_Wunused_but_set_variable,
662 "variable %q+D set but not used", decl);
663 unused_but_set_errorcount = errorcount;
668 /* Remove declarations for all the DECLs in this level. */
669 for (link = decls; link; link = TREE_CHAIN (link))
671 if (leaving_for_scope && VAR_P (link)
672 /* It's hard to make this ARM compatibility hack play nicely with
673 lambdas, and it really isn't necessary in C++11 mode. */
674 && cxx_dialect < cxx11
675 && DECL_NAME (link))
677 tree name = DECL_NAME (link);
678 cxx_binding *ob;
679 tree ns_binding;
681 ob = outer_binding (name,
682 IDENTIFIER_BINDING (name),
683 /*class_p=*/true);
684 if (!ob)
685 ns_binding = IDENTIFIER_NAMESPACE_VALUE (name);
686 else
687 ns_binding = NULL_TREE;
689 if (ob && ob->scope == current_binding_level->level_chain)
690 /* We have something like:
692 int i;
693 for (int i; ;);
695 and we are leaving the `for' scope. There's no reason to
696 keep the binding of the inner `i' in this case. */
697 pop_binding (name, link);
698 else if ((ob && (TREE_CODE (ob->value) == TYPE_DECL))
699 || (ns_binding && TREE_CODE (ns_binding) == TYPE_DECL))
700 /* Here, we have something like:
702 typedef int I;
704 void f () {
705 for (int I; ;);
708 We must pop the for-scope binding so we know what's a
709 type and what isn't. */
710 pop_binding (name, link);
711 else
713 /* Mark this VAR_DECL as dead so that we can tell we left it
714 there only for backward compatibility. */
715 DECL_DEAD_FOR_LOCAL (link) = 1;
717 /* Keep track of what should have happened when we
718 popped the binding. */
719 if (ob && ob->value)
721 SET_DECL_SHADOWED_FOR_VAR (link, ob->value);
722 DECL_HAS_SHADOWED_FOR_VAR_P (link) = 1;
725 /* Add it to the list of dead variables in the next
726 outermost binding to that we can remove these when we
727 leave that binding. */
728 vec_safe_push (
729 current_binding_level->level_chain->dead_vars_from_for,
730 link);
732 /* Although we don't pop the cxx_binding, we do clear
733 its SCOPE since the scope is going away now. */
734 IDENTIFIER_BINDING (name)->scope
735 = current_binding_level->level_chain;
738 else
740 tree name;
742 /* Remove the binding. */
743 decl = link;
745 if (TREE_CODE (decl) == TREE_LIST)
746 decl = TREE_VALUE (decl);
747 name = decl;
749 if (TREE_CODE (name) == OVERLOAD)
750 name = OVL_FUNCTION (name);
752 gcc_assert (DECL_P (name));
753 pop_binding (DECL_NAME (name), decl);
757 /* Remove declarations for any `for' variables from inner scopes
758 that we kept around. */
759 FOR_EACH_VEC_SAFE_ELT_REVERSE (current_binding_level->dead_vars_from_for,
760 ix, decl)
761 pop_binding (DECL_NAME (decl), decl);
763 /* Restore the IDENTIFIER_TYPE_VALUEs. */
764 for (link = current_binding_level->type_shadowed;
765 link; link = TREE_CHAIN (link))
766 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link), TREE_VALUE (link));
768 /* Restore the IDENTIFIER_LABEL_VALUEs for local labels. */
769 FOR_EACH_VEC_SAFE_ELT_REVERSE (current_binding_level->shadowed_labels,
770 ix, label_bind)
771 pop_local_label (label_bind->label, label_bind->prev_value);
773 /* There may be OVERLOADs (wrapped in TREE_LISTs) on the BLOCK_VARs
774 list if a `using' declaration put them there. The debugging
775 back ends won't understand OVERLOAD, so we remove them here.
776 Because the BLOCK_VARS are (temporarily) shared with
777 CURRENT_BINDING_LEVEL->NAMES we must do this fixup after we have
778 popped all the bindings. */
779 if (block)
781 tree* d;
783 for (d = &BLOCK_VARS (block); *d; )
785 if (TREE_CODE (*d) == TREE_LIST)
786 *d = TREE_CHAIN (*d);
787 else
788 d = &DECL_CHAIN (*d);
792 /* If the level being exited is the top level of a function,
793 check over all the labels. */
794 if (functionbody)
796 /* Since this is the top level block of a function, the vars are
797 the function's parameters. Don't leave them in the BLOCK
798 because they are found in the FUNCTION_DECL instead. */
799 BLOCK_VARS (block) = 0;
800 pop_labels (block);
803 kind = current_binding_level->kind;
804 if (kind == sk_cleanup)
806 tree stmt;
808 /* If this is a temporary binding created for a cleanup, then we'll
809 have pushed a statement list level. Pop that, create a new
810 BIND_EXPR for the block, and insert it into the stream. */
811 stmt = pop_stmt_list (current_binding_level->statement_list);
812 stmt = c_build_bind_expr (input_location, block, stmt);
813 add_stmt (stmt);
816 leave_scope ();
817 if (functionbody)
819 /* The current function is being defined, so its DECL_INITIAL
820 should be error_mark_node. */
821 gcc_assert (DECL_INITIAL (current_function_decl) == error_mark_node);
822 DECL_INITIAL (current_function_decl) = block;
824 else if (block)
825 current_binding_level->blocks
826 = block_chainon (current_binding_level->blocks, block);
828 /* If we did not make a block for the level just exited,
829 any blocks made for inner levels
830 (since they cannot be recorded as subblocks in that level)
831 must be carried forward so they will later become subblocks
832 of something else. */
833 else if (subblocks)
834 current_binding_level->blocks
835 = block_chainon (current_binding_level->blocks, subblocks);
837 /* Each and every BLOCK node created here in `poplevel' is important
838 (e.g. for proper debugging information) so if we created one
839 earlier, mark it as "used". */
840 if (block)
841 TREE_USED (block) = 1;
843 /* All temporary bindings created for cleanups are popped silently. */
844 if (kind == sk_cleanup)
845 goto restart;
847 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
848 return block;
851 /* Walk all the namespaces contained NAMESPACE, including NAMESPACE
852 itself, calling F for each. The DATA is passed to F as well. */
854 static int
855 walk_namespaces_r (tree name_space, walk_namespaces_fn f, void* data)
857 int result = 0;
858 tree current = NAMESPACE_LEVEL (name_space)->namespaces;
860 result |= (*f) (name_space, data);
862 for (; current; current = DECL_CHAIN (current))
863 result |= walk_namespaces_r (current, f, data);
865 return result;
868 /* Walk all the namespaces, calling F for each. The DATA is passed to
869 F as well. */
872 walk_namespaces (walk_namespaces_fn f, void* data)
874 return walk_namespaces_r (global_namespace, f, data);
877 /* Call wrapup_globals_declarations for the globals in NAMESPACE. If
878 DATA is non-NULL, this is the last time we will call
879 wrapup_global_declarations for this NAMESPACE. */
882 wrapup_globals_for_namespace (tree name_space, void* data)
884 cp_binding_level *level = NAMESPACE_LEVEL (name_space);
885 vec<tree, va_gc> *statics = level->static_decls;
886 tree *vec = statics->address ();
887 int len = statics->length ();
888 int last_time = (data != 0);
890 if (last_time)
892 check_global_declarations (vec, len);
893 emit_debug_global_declarations (vec, len);
894 return 0;
897 /* Write out any globals that need to be output. */
898 return wrapup_global_declarations (vec, len);
902 /* In C++, you don't have to write `struct S' to refer to `S'; you
903 can just use `S'. We accomplish this by creating a TYPE_DECL as
904 if the user had written `typedef struct S S'. Create and return
905 the TYPE_DECL for TYPE. */
907 tree
908 create_implicit_typedef (tree name, tree type)
910 tree decl;
912 decl = build_decl (input_location, TYPE_DECL, name, type);
913 DECL_ARTIFICIAL (decl) = 1;
914 /* There are other implicit type declarations, like the one *within*
915 a class that allows you to write `S::S'. We must distinguish
916 amongst these. */
917 SET_DECL_IMPLICIT_TYPEDEF_P (decl);
918 TYPE_NAME (type) = decl;
919 TYPE_STUB_DECL (type) = decl;
921 return decl;
924 /* Remember a local name for name-mangling purposes. */
926 static void
927 push_local_name (tree decl)
929 size_t i, nelts;
930 tree t, name;
932 timevar_start (TV_NAME_LOOKUP);
934 name = DECL_NAME (decl);
936 nelts = vec_safe_length (local_names);
937 for (i = 0; i < nelts; i++)
939 t = (*local_names)[i];
940 if (DECL_NAME (t) == name)
942 if (!DECL_LANG_SPECIFIC (decl))
943 retrofit_lang_decl (decl);
944 DECL_LANG_SPECIFIC (decl)->u.base.u2sel = 1;
945 if (DECL_DISCRIMINATOR_SET_P (t))
946 DECL_DISCRIMINATOR (decl) = DECL_DISCRIMINATOR (t) + 1;
947 else
948 DECL_DISCRIMINATOR (decl) = 1;
950 (*local_names)[i] = decl;
951 timevar_stop (TV_NAME_LOOKUP);
952 return;
956 vec_safe_push (local_names, decl);
957 timevar_stop (TV_NAME_LOOKUP);
960 /* Subroutine of duplicate_decls: return truthvalue of whether
961 or not types of these decls match.
963 For C++, we must compare the parameter list so that `int' can match
964 `int&' in a parameter position, but `int&' is not confused with
965 `const int&'. */
968 decls_match (tree newdecl, tree olddecl)
970 int types_match;
972 if (newdecl == olddecl)
973 return 1;
975 if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
976 /* If the two DECLs are not even the same kind of thing, we're not
977 interested in their types. */
978 return 0;
980 gcc_assert (DECL_P (newdecl));
982 if (TREE_CODE (newdecl) == FUNCTION_DECL)
984 tree f1 = TREE_TYPE (newdecl);
985 tree f2 = TREE_TYPE (olddecl);
986 tree p1 = TYPE_ARG_TYPES (f1);
987 tree p2 = TYPE_ARG_TYPES (f2);
988 tree r2;
990 /* Specializations of different templates are different functions
991 even if they have the same type. */
992 tree t1 = (DECL_USE_TEMPLATE (newdecl)
993 ? DECL_TI_TEMPLATE (newdecl)
994 : NULL_TREE);
995 tree t2 = (DECL_USE_TEMPLATE (olddecl)
996 ? DECL_TI_TEMPLATE (olddecl)
997 : NULL_TREE);
998 if (t1 != t2)
999 return 0;
1001 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
1002 && ! (DECL_EXTERN_C_P (newdecl)
1003 && DECL_EXTERN_C_P (olddecl)))
1004 return 0;
1006 /* A new declaration doesn't match a built-in one unless it
1007 is also extern "C". */
1008 if (DECL_IS_BUILTIN (olddecl)
1009 && DECL_EXTERN_C_P (olddecl) && !DECL_EXTERN_C_P (newdecl))
1010 return 0;
1012 if (TREE_CODE (f1) != TREE_CODE (f2))
1013 return 0;
1015 /* A declaration with deduced return type should use its pre-deduction
1016 type for declaration matching. */
1017 r2 = fndecl_declared_return_type (olddecl);
1019 if (same_type_p (TREE_TYPE (f1), r2))
1021 if (!prototype_p (f2) && DECL_EXTERN_C_P (olddecl)
1022 && (DECL_BUILT_IN (olddecl)
1023 #ifndef NO_IMPLICIT_EXTERN_C
1024 || (DECL_IN_SYSTEM_HEADER (newdecl) && !DECL_CLASS_SCOPE_P (newdecl))
1025 || (DECL_IN_SYSTEM_HEADER (olddecl) && !DECL_CLASS_SCOPE_P (olddecl))
1026 #endif
1029 types_match = self_promoting_args_p (p1);
1030 if (p1 == void_list_node)
1031 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
1033 #ifndef NO_IMPLICIT_EXTERN_C
1034 else if (!prototype_p (f1)
1035 && (DECL_EXTERN_C_P (olddecl)
1036 && DECL_IN_SYSTEM_HEADER (olddecl)
1037 && !DECL_CLASS_SCOPE_P (olddecl))
1038 && (DECL_EXTERN_C_P (newdecl)
1039 && DECL_IN_SYSTEM_HEADER (newdecl)
1040 && !DECL_CLASS_SCOPE_P (newdecl)))
1042 types_match = self_promoting_args_p (p2);
1043 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
1045 #endif
1046 else
1047 types_match =
1048 compparms (p1, p2)
1049 && type_memfn_rqual (f1) == type_memfn_rqual (f2)
1050 && (TYPE_ATTRIBUTES (TREE_TYPE (newdecl)) == NULL_TREE
1051 || comp_type_attributes (TREE_TYPE (newdecl),
1052 TREE_TYPE (olddecl)) != 0);
1054 else
1055 types_match = 0;
1057 /* The decls dont match if they correspond to two different versions
1058 of the same function. Disallow extern "C" functions to be
1059 versions for now. */
1060 if (types_match
1061 && !DECL_EXTERN_C_P (newdecl)
1062 && !DECL_EXTERN_C_P (olddecl)
1063 && targetm.target_option.function_versions (newdecl, olddecl))
1065 /* Mark functions as versions if necessary. Modify the mangled decl
1066 name if necessary. */
1067 if (DECL_FUNCTION_VERSIONED (newdecl)
1068 && DECL_FUNCTION_VERSIONED (olddecl))
1069 return 0;
1070 if (!DECL_FUNCTION_VERSIONED (newdecl))
1072 DECL_FUNCTION_VERSIONED (newdecl) = 1;
1073 if (DECL_ASSEMBLER_NAME_SET_P (newdecl))
1074 mangle_decl (newdecl);
1076 if (!DECL_FUNCTION_VERSIONED (olddecl))
1078 DECL_FUNCTION_VERSIONED (olddecl) = 1;
1079 if (DECL_ASSEMBLER_NAME_SET_P (olddecl))
1080 mangle_decl (olddecl);
1082 cgraph_node::record_function_versions (olddecl, newdecl);
1083 return 0;
1086 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1088 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl))
1089 != TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)))
1090 return 0;
1092 if (!comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
1093 DECL_TEMPLATE_PARMS (olddecl)))
1094 return 0;
1096 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
1097 types_match = same_type_p (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl)),
1098 TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl)));
1099 else
1100 types_match = decls_match (DECL_TEMPLATE_RESULT (olddecl),
1101 DECL_TEMPLATE_RESULT (newdecl));
1103 else
1105 /* Need to check scope for variable declaration (VAR_DECL).
1106 For typedef (TYPE_DECL), scope is ignored. */
1107 if (VAR_P (newdecl)
1108 && CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
1109 /* [dcl.link]
1110 Two declarations for an object with C language linkage
1111 with the same name (ignoring the namespace that qualify
1112 it) that appear in different namespace scopes refer to
1113 the same object. */
1114 && !(DECL_EXTERN_C_P (olddecl) && DECL_EXTERN_C_P (newdecl)))
1115 return 0;
1117 if (TREE_TYPE (newdecl) == error_mark_node)
1118 types_match = TREE_TYPE (olddecl) == error_mark_node;
1119 else if (TREE_TYPE (olddecl) == NULL_TREE)
1120 types_match = TREE_TYPE (newdecl) == NULL_TREE;
1121 else if (TREE_TYPE (newdecl) == NULL_TREE)
1122 types_match = 0;
1123 else
1124 types_match = comptypes (TREE_TYPE (newdecl),
1125 TREE_TYPE (olddecl),
1126 COMPARE_REDECLARATION);
1129 return types_match;
1132 /* If NEWDECL is `static' and an `extern' was seen previously,
1133 warn about it. OLDDECL is the previous declaration.
1135 Note that this does not apply to the C++ case of declaring
1136 a variable `extern const' and then later `const'.
1138 Don't complain about built-in functions, since they are beyond
1139 the user's control. */
1141 void
1142 warn_extern_redeclared_static (tree newdecl, tree olddecl)
1144 if (TREE_CODE (newdecl) == TYPE_DECL
1145 || TREE_CODE (newdecl) == TEMPLATE_DECL
1146 || TREE_CODE (newdecl) == CONST_DECL
1147 || TREE_CODE (newdecl) == NAMESPACE_DECL)
1148 return;
1150 /* Don't get confused by static member functions; that's a different
1151 use of `static'. */
1152 if (TREE_CODE (newdecl) == FUNCTION_DECL
1153 && DECL_STATIC_FUNCTION_P (newdecl))
1154 return;
1156 /* If the old declaration was `static', or the new one isn't, then
1157 everything is OK. */
1158 if (DECL_THIS_STATIC (olddecl) || !DECL_THIS_STATIC (newdecl))
1159 return;
1161 /* It's OK to declare a builtin function as `static'. */
1162 if (TREE_CODE (olddecl) == FUNCTION_DECL
1163 && DECL_ARTIFICIAL (olddecl))
1164 return;
1166 if (permerror (input_location,
1167 "%qD was declared %<extern%> and later %<static%>", newdecl))
1168 inform (input_location, "previous declaration of %q+D", olddecl);
1171 /* NEW_DECL is a redeclaration of OLD_DECL; both are functions or
1172 function templates. If their exception specifications do not
1173 match, issue a diagnostic. */
1175 static void
1176 check_redeclaration_exception_specification (tree new_decl,
1177 tree old_decl)
1179 tree new_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (new_decl));
1180 tree old_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old_decl));
1182 /* Two default specs are equivalent, don't force evaluation. */
1183 if (UNEVALUATED_NOEXCEPT_SPEC_P (new_exceptions)
1184 && UNEVALUATED_NOEXCEPT_SPEC_P (old_exceptions))
1185 return;
1187 maybe_instantiate_noexcept (new_decl);
1188 maybe_instantiate_noexcept (old_decl);
1189 new_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (new_decl));
1190 old_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old_decl));
1192 /* [except.spec]
1194 If any declaration of a function has an exception-specification,
1195 all declarations, including the definition and an explicit
1196 specialization, of that function shall have an
1197 exception-specification with the same set of type-ids. */
1198 if ((pedantic || ! DECL_IN_SYSTEM_HEADER (old_decl))
1199 && ! DECL_IS_BUILTIN (old_decl)
1200 && flag_exceptions
1201 && !comp_except_specs (new_exceptions, old_exceptions, ce_normal))
1203 error ("declaration of %qF has a different exception specifier",
1204 new_decl);
1205 error ("from previous declaration %q+F", old_decl);
1209 /* Return true if OLD_DECL and NEW_DECL agree on constexprness.
1210 Otherwise issue diagnostics. */
1212 static bool
1213 validate_constexpr_redeclaration (tree old_decl, tree new_decl)
1215 old_decl = STRIP_TEMPLATE (old_decl);
1216 new_decl = STRIP_TEMPLATE (new_decl);
1217 if (!VAR_OR_FUNCTION_DECL_P (old_decl)
1218 || !VAR_OR_FUNCTION_DECL_P (new_decl))
1219 return true;
1220 if (DECL_DECLARED_CONSTEXPR_P (old_decl)
1221 == DECL_DECLARED_CONSTEXPR_P (new_decl))
1222 return true;
1223 if (TREE_CODE (old_decl) == FUNCTION_DECL)
1225 if (DECL_BUILT_IN (old_decl))
1227 /* Hide a built-in declaration. */
1228 DECL_DECLARED_CONSTEXPR_P (old_decl)
1229 = DECL_DECLARED_CONSTEXPR_P (new_decl);
1230 return true;
1232 /* 7.1.5 [dcl.constexpr]
1233 Note: An explicit specialization can differ from the template
1234 declaration with respect to the constexpr specifier. */
1235 if (! DECL_TEMPLATE_SPECIALIZATION (old_decl)
1236 && DECL_TEMPLATE_SPECIALIZATION (new_decl))
1237 return true;
1239 error ("redeclaration %qD differs in %<constexpr%>", new_decl);
1240 error ("from previous declaration %q+D", old_decl);
1241 return false;
1243 return true;
1246 /* DECL is a redeclaration of a function or function template. If
1247 it does have default arguments issue a diagnostic. Note: this
1248 function is used to enforce the requirements in C++11 8.3.6 about
1249 no default arguments in redeclarations. */
1251 static void
1252 check_redeclaration_no_default_args (tree decl)
1254 gcc_assert (DECL_DECLARES_FUNCTION_P (decl));
1256 for (tree t = FUNCTION_FIRST_USER_PARMTYPE (decl);
1257 t && t != void_list_node; t = TREE_CHAIN (t))
1258 if (TREE_PURPOSE (t))
1260 permerror (input_location,
1261 "redeclaration of %q#D may not have default "
1262 "arguments", decl);
1263 return;
1267 #define GNU_INLINE_P(fn) (DECL_DECLARED_INLINE_P (fn) \
1268 && lookup_attribute ("gnu_inline", \
1269 DECL_ATTRIBUTES (fn)))
1271 /* If NEWDECL is a redeclaration of OLDDECL, merge the declarations.
1272 If the redeclaration is invalid, a diagnostic is issued, and the
1273 error_mark_node is returned. Otherwise, OLDDECL is returned.
1275 If NEWDECL is not a redeclaration of OLDDECL, NULL_TREE is
1276 returned.
1278 NEWDECL_IS_FRIEND is true if NEWDECL was declared as a friend. */
1280 tree
1281 duplicate_decls (tree newdecl, tree olddecl, bool newdecl_is_friend)
1283 unsigned olddecl_uid = DECL_UID (olddecl);
1284 int olddecl_friend = 0, types_match = 0, hidden_friend = 0;
1285 int new_defines_function = 0;
1286 tree new_template_info;
1288 if (newdecl == olddecl)
1289 return olddecl;
1291 types_match = decls_match (newdecl, olddecl);
1293 /* If either the type of the new decl or the type of the old decl is an
1294 error_mark_node, then that implies that we have already issued an
1295 error (earlier) for some bogus type specification, and in that case,
1296 it is rather pointless to harass the user with yet more error message
1297 about the same declaration, so just pretend the types match here. */
1298 if (TREE_TYPE (newdecl) == error_mark_node
1299 || TREE_TYPE (olddecl) == error_mark_node)
1300 return error_mark_node;
1302 if (UDLIT_OPER_P (DECL_NAME (newdecl))
1303 && UDLIT_OPER_P (DECL_NAME (olddecl)))
1305 if (TREE_CODE (newdecl) == TEMPLATE_DECL
1306 && TREE_CODE (olddecl) != TEMPLATE_DECL
1307 && check_raw_literal_operator (olddecl))
1308 error ("literal operator template %q+D conflicts with"
1309 " raw literal operator %qD", newdecl, olddecl);
1310 else if (TREE_CODE (newdecl) != TEMPLATE_DECL
1311 && TREE_CODE (olddecl) == TEMPLATE_DECL
1312 && check_raw_literal_operator (newdecl))
1313 error ("raw literal operator %q+D conflicts with"
1314 " literal operator template %qD", newdecl, olddecl);
1317 if (DECL_P (olddecl)
1318 && TREE_CODE (newdecl) == FUNCTION_DECL
1319 && TREE_CODE (olddecl) == FUNCTION_DECL
1320 && (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl)))
1322 if (DECL_DECLARED_INLINE_P (newdecl)
1323 && DECL_UNINLINABLE (newdecl)
1324 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1325 /* Already warned elsewhere. */;
1326 else if (DECL_DECLARED_INLINE_P (olddecl)
1327 && DECL_UNINLINABLE (olddecl)
1328 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1329 /* Already warned. */;
1330 else if (DECL_DECLARED_INLINE_P (newdecl)
1331 && DECL_UNINLINABLE (olddecl)
1332 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1334 if (warning (OPT_Wattributes, "function %q+D redeclared as inline",
1335 newdecl))
1336 inform (DECL_SOURCE_LOCATION (olddecl),
1337 "previous declaration of %qD with attribute noinline",
1338 olddecl);
1340 else if (DECL_DECLARED_INLINE_P (olddecl)
1341 && DECL_UNINLINABLE (newdecl)
1342 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1344 if (warning (OPT_Wattributes, "function %q+D redeclared with "
1345 "attribute noinline", newdecl))
1346 inform (DECL_SOURCE_LOCATION (olddecl),
1347 "previous declaration of %qD was inline",
1348 olddecl);
1352 /* Check for redeclaration and other discrepancies. */
1353 if (TREE_CODE (olddecl) == FUNCTION_DECL
1354 && DECL_ARTIFICIAL (olddecl))
1356 gcc_assert (!DECL_HIDDEN_FRIEND_P (olddecl));
1357 if (TREE_CODE (newdecl) != FUNCTION_DECL)
1359 /* Avoid warnings redeclaring built-ins which have not been
1360 explicitly declared. */
1361 if (DECL_ANTICIPATED (olddecl))
1362 return NULL_TREE;
1364 /* If you declare a built-in or predefined function name as static,
1365 the old definition is overridden, but optionally warn this was a
1366 bad choice of name. */
1367 if (! TREE_PUBLIC (newdecl))
1369 warning (OPT_Wshadow,
1370 DECL_BUILT_IN (olddecl)
1371 ? G_("shadowing built-in function %q#D")
1372 : G_("shadowing library function %q#D"), olddecl);
1373 /* Discard the old built-in function. */
1374 return NULL_TREE;
1376 /* If the built-in is not ansi, then programs can override
1377 it even globally without an error. */
1378 else if (! DECL_BUILT_IN (olddecl))
1379 warning (0, "library function %q#D redeclared as non-function %q#D",
1380 olddecl, newdecl);
1381 else
1382 error ("declaration of %q#D conflicts with built-in "
1383 "declaration %q#D", newdecl, olddecl);
1384 return NULL_TREE;
1386 else if (DECL_OMP_DECLARE_REDUCTION_P (olddecl))
1388 gcc_assert (DECL_OMP_DECLARE_REDUCTION_P (newdecl));
1389 error_at (DECL_SOURCE_LOCATION (newdecl),
1390 "redeclaration of %<pragma omp declare reduction%>");
1391 inform (DECL_SOURCE_LOCATION (olddecl),
1392 "previous %<pragma omp declare reduction%> declaration");
1393 return error_mark_node;
1395 else if (!types_match)
1397 /* Avoid warnings redeclaring built-ins which have not been
1398 explicitly declared. */
1399 if (DECL_ANTICIPATED (olddecl))
1401 /* Deal with fileptr_type_node. FILE type is not known
1402 at the time we create the builtins. */
1403 tree t1, t2;
1405 for (t1 = TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1406 t2 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1407 t1 || t2;
1408 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1409 if (!t1 || !t2)
1410 break;
1411 else if (TREE_VALUE (t2) == fileptr_type_node)
1413 tree t = TREE_VALUE (t1);
1415 if (TYPE_PTR_P (t)
1416 && TYPE_IDENTIFIER (TREE_TYPE (t))
1417 == get_identifier ("FILE")
1418 && compparms (TREE_CHAIN (t1), TREE_CHAIN (t2)))
1420 tree oldargs = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1422 TYPE_ARG_TYPES (TREE_TYPE (olddecl))
1423 = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
1424 types_match = decls_match (newdecl, olddecl);
1425 if (types_match)
1426 return duplicate_decls (newdecl, olddecl,
1427 newdecl_is_friend);
1428 TYPE_ARG_TYPES (TREE_TYPE (olddecl)) = oldargs;
1431 else if (! same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1432 break;
1434 else if ((DECL_EXTERN_C_P (newdecl)
1435 && DECL_EXTERN_C_P (olddecl))
1436 || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1437 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
1439 /* A near match; override the builtin. */
1441 if (TREE_PUBLIC (newdecl))
1442 warning (0, "new declaration %q#D ambiguates built-in "
1443 "declaration %q#D", newdecl, olddecl);
1444 else
1445 warning (OPT_Wshadow,
1446 DECL_BUILT_IN (olddecl)
1447 ? G_("shadowing built-in function %q#D")
1448 : G_("shadowing library function %q#D"), olddecl);
1450 else
1451 /* Discard the old built-in function. */
1452 return NULL_TREE;
1454 /* Replace the old RTL to avoid problems with inlining. */
1455 COPY_DECL_RTL (newdecl, olddecl);
1457 /* Even if the types match, prefer the new declarations type for
1458 built-ins which have not been explicitly declared, for
1459 exception lists, etc... */
1460 else if (DECL_IS_BUILTIN (olddecl))
1462 tree type = TREE_TYPE (newdecl);
1463 tree attribs = (*targetm.merge_type_attributes)
1464 (TREE_TYPE (olddecl), type);
1466 type = cp_build_type_attribute_variant (type, attribs);
1467 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = type;
1470 /* If a function is explicitly declared "throw ()", propagate that to
1471 the corresponding builtin. */
1472 if (DECL_BUILT_IN_CLASS (olddecl) == BUILT_IN_NORMAL
1473 && DECL_ANTICIPATED (olddecl)
1474 && TREE_NOTHROW (newdecl)
1475 && !TREE_NOTHROW (olddecl))
1477 enum built_in_function fncode = DECL_FUNCTION_CODE (olddecl);
1478 tree tmpdecl = builtin_decl_explicit (fncode);
1479 if (tmpdecl && tmpdecl != olddecl && types_match)
1480 TREE_NOTHROW (tmpdecl) = 1;
1483 /* Whether or not the builtin can throw exceptions has no
1484 bearing on this declarator. */
1485 TREE_NOTHROW (olddecl) = 0;
1487 if (DECL_THIS_STATIC (newdecl) && !DECL_THIS_STATIC (olddecl))
1489 /* If a builtin function is redeclared as `static', merge
1490 the declarations, but make the original one static. */
1491 DECL_THIS_STATIC (olddecl) = 1;
1492 TREE_PUBLIC (olddecl) = 0;
1494 /* Make the old declaration consistent with the new one so
1495 that all remnants of the builtin-ness of this function
1496 will be banished. */
1497 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
1498 COPY_DECL_RTL (newdecl, olddecl);
1501 else if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1503 /* C++ Standard, 3.3, clause 4:
1504 "[Note: a namespace name or a class template name must be unique
1505 in its declarative region (7.3.2, clause 14). ]" */
1506 if (TREE_CODE (olddecl) != NAMESPACE_DECL
1507 && TREE_CODE (newdecl) != NAMESPACE_DECL
1508 && (TREE_CODE (olddecl) != TEMPLATE_DECL
1509 || TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) != TYPE_DECL)
1510 && (TREE_CODE (newdecl) != TEMPLATE_DECL
1511 || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) != TYPE_DECL))
1513 if ((TREE_CODE (olddecl) == TYPE_DECL && DECL_ARTIFICIAL (olddecl)
1514 && TREE_CODE (newdecl) != TYPE_DECL)
1515 || (TREE_CODE (newdecl) == TYPE_DECL && DECL_ARTIFICIAL (newdecl)
1516 && TREE_CODE (olddecl) != TYPE_DECL))
1518 /* We do nothing special here, because C++ does such nasty
1519 things with TYPE_DECLs. Instead, just let the TYPE_DECL
1520 get shadowed, and know that if we need to find a TYPE_DECL
1521 for a given name, we can look in the IDENTIFIER_TYPE_VALUE
1522 slot of the identifier. */
1523 return NULL_TREE;
1526 if ((TREE_CODE (newdecl) == FUNCTION_DECL
1527 && DECL_FUNCTION_TEMPLATE_P (olddecl))
1528 || (TREE_CODE (olddecl) == FUNCTION_DECL
1529 && DECL_FUNCTION_TEMPLATE_P (newdecl)))
1530 return NULL_TREE;
1533 error ("%q#D redeclared as different kind of symbol", newdecl);
1534 if (TREE_CODE (olddecl) == TREE_LIST)
1535 olddecl = TREE_VALUE (olddecl);
1536 inform (DECL_SOURCE_LOCATION (olddecl),
1537 "previous declaration %q#D", olddecl);
1539 return error_mark_node;
1541 else if (!types_match)
1543 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl))
1544 /* These are certainly not duplicate declarations; they're
1545 from different scopes. */
1546 return NULL_TREE;
1548 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1550 /* The name of a class template may not be declared to refer to
1551 any other template, class, function, object, namespace, value,
1552 or type in the same scope. */
1553 if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == TYPE_DECL
1554 || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
1556 error ("conflicting declaration of template %q#D", newdecl);
1557 inform (DECL_SOURCE_LOCATION (olddecl),
1558 "previous declaration %q#D", olddecl);
1559 return error_mark_node;
1561 else if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == FUNCTION_DECL
1562 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL
1563 && compparms (TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl))),
1564 TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl))))
1565 && comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
1566 DECL_TEMPLATE_PARMS (olddecl))
1567 /* Template functions can be disambiguated by
1568 return type. */
1569 && same_type_p (TREE_TYPE (TREE_TYPE (newdecl)),
1570 TREE_TYPE (TREE_TYPE (olddecl))))
1572 error ("ambiguating new declaration %q#D", newdecl);
1573 inform (DECL_SOURCE_LOCATION (olddecl),
1574 "old declaration %q#D", olddecl);
1576 return NULL_TREE;
1578 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1580 if (DECL_EXTERN_C_P (newdecl) && DECL_EXTERN_C_P (olddecl))
1582 error ("conflicting declaration of C function %q#D",
1583 newdecl);
1584 inform (DECL_SOURCE_LOCATION (olddecl),
1585 "previous declaration %q#D", olddecl);
1586 return NULL_TREE;
1588 /* For function versions, params and types match, but they
1589 are not ambiguous. */
1590 else if ((!DECL_FUNCTION_VERSIONED (newdecl)
1591 && !DECL_FUNCTION_VERSIONED (olddecl))
1592 && compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1593 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
1595 error ("ambiguating new declaration of %q#D", newdecl);
1596 inform (DECL_SOURCE_LOCATION (olddecl),
1597 "old declaration %q#D", olddecl);
1598 return error_mark_node;
1600 else
1601 return NULL_TREE;
1603 else
1605 error ("conflicting declaration %q#D", newdecl);
1606 inform (DECL_SOURCE_LOCATION (olddecl),
1607 "previous declaration as %q#D", olddecl);
1608 return error_mark_node;
1611 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1612 && ((DECL_TEMPLATE_SPECIALIZATION (olddecl)
1613 && (!DECL_TEMPLATE_INFO (newdecl)
1614 || (DECL_TI_TEMPLATE (newdecl)
1615 != DECL_TI_TEMPLATE (olddecl))))
1616 || (DECL_TEMPLATE_SPECIALIZATION (newdecl)
1617 && (!DECL_TEMPLATE_INFO (olddecl)
1618 || (DECL_TI_TEMPLATE (olddecl)
1619 != DECL_TI_TEMPLATE (newdecl))))))
1620 /* It's OK to have a template specialization and a non-template
1621 with the same type, or to have specializations of two
1622 different templates with the same type. Note that if one is a
1623 specialization, and the other is an instantiation of the same
1624 template, that we do not exit at this point. That situation
1625 can occur if we instantiate a template class, and then
1626 specialize one of its methods. This situation is valid, but
1627 the declarations must be merged in the usual way. */
1628 return NULL_TREE;
1629 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1630 && ((DECL_TEMPLATE_INSTANTIATION (olddecl)
1631 && !DECL_USE_TEMPLATE (newdecl))
1632 || (DECL_TEMPLATE_INSTANTIATION (newdecl)
1633 && !DECL_USE_TEMPLATE (olddecl))))
1634 /* One of the declarations is a template instantiation, and the
1635 other is not a template at all. That's OK. */
1636 return NULL_TREE;
1637 else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
1639 /* In [namespace.alias] we have:
1641 In a declarative region, a namespace-alias-definition can be
1642 used to redefine a namespace-alias declared in that declarative
1643 region to refer only to the namespace to which it already
1644 refers.
1646 Therefore, if we encounter a second alias directive for the same
1647 alias, we can just ignore the second directive. */
1648 if (DECL_NAMESPACE_ALIAS (newdecl)
1649 && (DECL_NAMESPACE_ALIAS (newdecl)
1650 == DECL_NAMESPACE_ALIAS (olddecl)))
1651 return olddecl;
1652 /* [namespace.alias]
1654 A namespace-name or namespace-alias shall not be declared as
1655 the name of any other entity in the same declarative region.
1656 A namespace-name defined at global scope shall not be
1657 declared as the name of any other entity in any global scope
1658 of the program. */
1659 error ("conflicting declaration of namespace %qD", newdecl);
1660 inform (DECL_SOURCE_LOCATION (olddecl),
1661 "previous declaration of namespace %qD here", olddecl);
1662 return error_mark_node;
1664 else
1666 const char *errmsg = redeclaration_error_message (newdecl, olddecl);
1667 if (errmsg)
1669 error_at (DECL_SOURCE_LOCATION (newdecl), errmsg, newdecl);
1670 if (DECL_NAME (olddecl) != NULL_TREE)
1671 inform (input_location,
1672 (DECL_INITIAL (olddecl) && namespace_bindings_p ())
1673 ? G_("%q+#D previously defined here")
1674 : G_("%q+#D previously declared here"), olddecl);
1675 return error_mark_node;
1677 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1678 && DECL_INITIAL (olddecl) != NULL_TREE
1679 && !prototype_p (TREE_TYPE (olddecl))
1680 && prototype_p (TREE_TYPE (newdecl)))
1682 /* Prototype decl follows defn w/o prototype. */
1683 if (warning_at (DECL_SOURCE_LOCATION (newdecl), 0,
1684 "prototype specified for %q#D", newdecl))
1685 inform (DECL_SOURCE_LOCATION (olddecl),
1686 "previous non-prototype definition here");
1688 else if (VAR_OR_FUNCTION_DECL_P (olddecl)
1689 && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl))
1691 /* [dcl.link]
1692 If two declarations of the same function or object
1693 specify different linkage-specifications ..., the program
1694 is ill-formed.... Except for functions with C++ linkage,
1695 a function declaration without a linkage specification
1696 shall not precede the first linkage specification for
1697 that function. A function can be declared without a
1698 linkage specification after an explicit linkage
1699 specification has been seen; the linkage explicitly
1700 specified in the earlier declaration is not affected by
1701 such a function declaration.
1703 DR 563 raises the question why the restrictions on
1704 functions should not also apply to objects. Older
1705 versions of G++ silently ignore the linkage-specification
1706 for this example:
1708 namespace N {
1709 extern int i;
1710 extern "C" int i;
1713 which is clearly wrong. Therefore, we now treat objects
1714 like functions. */
1715 if (current_lang_depth () == 0)
1717 /* There is no explicit linkage-specification, so we use
1718 the linkage from the previous declaration. */
1719 if (!DECL_LANG_SPECIFIC (newdecl))
1720 retrofit_lang_decl (newdecl);
1721 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
1723 else
1725 error ("conflicting declaration of %q#D with %qL linkage",
1726 newdecl, DECL_LANGUAGE (newdecl));
1727 inform (DECL_SOURCE_LOCATION (olddecl),
1728 "previous declaration with %qL linkage",
1729 DECL_LANGUAGE (olddecl));
1733 if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl))
1735 else if (TREE_CODE (olddecl) == FUNCTION_DECL)
1737 /* Note: free functions, as TEMPLATE_DECLs, are handled below. */
1738 if (DECL_FUNCTION_MEMBER_P (olddecl)
1739 && (/* grokfndecl passes member function templates too
1740 as FUNCTION_DECLs. */
1741 DECL_TEMPLATE_INFO (olddecl)
1742 /* C++11 8.3.6/6.
1743 Default arguments for a member function of a class
1744 template shall be specified on the initial declaration
1745 of the member function within the class template. */
1746 || CLASSTYPE_TEMPLATE_INFO (CP_DECL_CONTEXT (olddecl))))
1747 check_redeclaration_no_default_args (newdecl);
1748 else
1750 tree t1 = FUNCTION_FIRST_USER_PARMTYPE (olddecl);
1751 tree t2 = FUNCTION_FIRST_USER_PARMTYPE (newdecl);
1752 int i = 1;
1754 for (; t1 && t1 != void_list_node;
1755 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++)
1756 if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
1758 if (1 == simple_cst_equal (TREE_PURPOSE (t1),
1759 TREE_PURPOSE (t2)))
1761 if (permerror (input_location,
1762 "default argument given for parameter "
1763 "%d of %q#D", i, newdecl))
1764 inform (DECL_SOURCE_LOCATION (olddecl),
1765 "previous specification in %q#D here",
1766 olddecl);
1768 else
1770 error ("default argument given for parameter %d "
1771 "of %q#D", i, newdecl);
1772 inform (DECL_SOURCE_LOCATION (olddecl),
1773 "previous specification in %q#D here",
1774 olddecl);
1781 /* Do not merge an implicit typedef with an explicit one. In:
1783 class A;
1785 typedef class A A __attribute__ ((foo));
1787 the attribute should apply only to the typedef. */
1788 if (TREE_CODE (olddecl) == TYPE_DECL
1789 && (DECL_IMPLICIT_TYPEDEF_P (olddecl)
1790 || DECL_IMPLICIT_TYPEDEF_P (newdecl)))
1791 return NULL_TREE;
1793 /* If new decl is `static' and an `extern' was seen previously,
1794 warn about it. */
1795 warn_extern_redeclared_static (newdecl, olddecl);
1797 if (!validate_constexpr_redeclaration (olddecl, newdecl))
1798 return error_mark_node;
1800 /* We have committed to returning 1 at this point. */
1801 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1803 /* Now that functions must hold information normally held
1804 by field decls, there is extra work to do so that
1805 declaration information does not get destroyed during
1806 definition. */
1807 if (DECL_VINDEX (olddecl))
1808 DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl);
1809 if (DECL_CONTEXT (olddecl))
1810 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1811 DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl);
1812 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1813 DECL_PURE_VIRTUAL_P (newdecl) |= DECL_PURE_VIRTUAL_P (olddecl);
1814 DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl);
1815 DECL_INVALID_OVERRIDER_P (newdecl) |= DECL_INVALID_OVERRIDER_P (olddecl);
1816 DECL_FINAL_P (newdecl) |= DECL_FINAL_P (olddecl);
1817 DECL_OVERRIDE_P (newdecl) |= DECL_OVERRIDE_P (olddecl);
1818 DECL_THIS_STATIC (newdecl) |= DECL_THIS_STATIC (olddecl);
1819 if (DECL_OVERLOADED_OPERATOR_P (olddecl) != ERROR_MARK)
1820 SET_OVERLOADED_OPERATOR_CODE
1821 (newdecl, DECL_OVERLOADED_OPERATOR_P (olddecl));
1822 new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE;
1824 /* Optionally warn about more than one declaration for the same
1825 name, but don't warn about a function declaration followed by a
1826 definition. */
1827 if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl)
1828 && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE)
1829 /* Don't warn about extern decl followed by definition. */
1830 && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl))
1831 /* Don't warn about friends, let add_friend take care of it. */
1832 && ! (newdecl_is_friend || DECL_FRIEND_P (olddecl))
1833 /* Don't warn about declaration followed by specialization. */
1834 && (! DECL_TEMPLATE_SPECIALIZATION (newdecl)
1835 || DECL_TEMPLATE_SPECIALIZATION (olddecl)))
1837 if (warning (OPT_Wredundant_decls,
1838 "redundant redeclaration of %qD in same scope",
1839 newdecl))
1840 inform (DECL_SOURCE_LOCATION (olddecl),
1841 "previous declaration of %qD", olddecl);
1844 if (!(DECL_TEMPLATE_INSTANTIATION (olddecl)
1845 && DECL_TEMPLATE_SPECIALIZATION (newdecl)))
1847 if (DECL_DELETED_FN (newdecl))
1849 error ("deleted definition of %qD", newdecl);
1850 inform (DECL_SOURCE_LOCATION (olddecl),
1851 "previous declaration of %qD", olddecl);
1853 DECL_DELETED_FN (newdecl) |= DECL_DELETED_FN (olddecl);
1857 /* Deal with C++: must preserve virtual function table size. */
1858 if (TREE_CODE (olddecl) == TYPE_DECL)
1860 tree newtype = TREE_TYPE (newdecl);
1861 tree oldtype = TREE_TYPE (olddecl);
1863 if (newtype != error_mark_node && oldtype != error_mark_node
1864 && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype))
1865 CLASSTYPE_FRIEND_CLASSES (newtype)
1866 = CLASSTYPE_FRIEND_CLASSES (oldtype);
1868 DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl);
1871 /* Copy all the DECL_... slots specified in the new decl
1872 except for any that we copy here from the old type. */
1873 DECL_ATTRIBUTES (newdecl)
1874 = (*targetm.merge_decl_attributes) (olddecl, newdecl);
1876 if (DECL_DECLARES_FUNCTION_P (olddecl) && DECL_DECLARES_FUNCTION_P (newdecl))
1878 olddecl_friend = DECL_FRIEND_P (olddecl);
1879 hidden_friend = (DECL_ANTICIPATED (olddecl)
1880 && DECL_HIDDEN_FRIEND_P (olddecl)
1881 && newdecl_is_friend);
1882 if (!hidden_friend)
1884 DECL_ANTICIPATED (olddecl) = 0;
1885 DECL_HIDDEN_FRIEND_P (olddecl) = 0;
1889 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1891 tree old_result;
1892 tree new_result;
1893 old_result = DECL_TEMPLATE_RESULT (olddecl);
1894 new_result = DECL_TEMPLATE_RESULT (newdecl);
1895 TREE_TYPE (olddecl) = TREE_TYPE (old_result);
1896 DECL_TEMPLATE_SPECIALIZATIONS (olddecl)
1897 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (olddecl),
1898 DECL_TEMPLATE_SPECIALIZATIONS (newdecl));
1900 DECL_ATTRIBUTES (old_result)
1901 = (*targetm.merge_decl_attributes) (old_result, new_result);
1903 if (DECL_FUNCTION_TEMPLATE_P (newdecl))
1905 /* Per C++11 8.3.6/4, default arguments cannot be added in later
1906 declarations of a function template. */
1907 check_redeclaration_no_default_args (newdecl);
1909 check_default_args (newdecl);
1911 if (GNU_INLINE_P (old_result) != GNU_INLINE_P (new_result)
1912 && DECL_INITIAL (new_result))
1914 if (DECL_INITIAL (old_result))
1915 DECL_UNINLINABLE (old_result) = 1;
1916 else
1917 DECL_UNINLINABLE (old_result) = DECL_UNINLINABLE (new_result);
1918 DECL_EXTERNAL (old_result) = DECL_EXTERNAL (new_result);
1919 DECL_NOT_REALLY_EXTERN (old_result)
1920 = DECL_NOT_REALLY_EXTERN (new_result);
1921 DECL_INTERFACE_KNOWN (old_result)
1922 = DECL_INTERFACE_KNOWN (new_result);
1923 DECL_DECLARED_INLINE_P (old_result)
1924 = DECL_DECLARED_INLINE_P (new_result);
1925 DECL_DISREGARD_INLINE_LIMITS (old_result)
1926 |= DECL_DISREGARD_INLINE_LIMITS (new_result);
1929 else
1931 DECL_DECLARED_INLINE_P (old_result)
1932 |= DECL_DECLARED_INLINE_P (new_result);
1933 DECL_DISREGARD_INLINE_LIMITS (old_result)
1934 |= DECL_DISREGARD_INLINE_LIMITS (new_result);
1935 check_redeclaration_exception_specification (newdecl, olddecl);
1939 /* If the new declaration is a definition, update the file and
1940 line information on the declaration, and also make
1941 the old declaration the same definition. */
1942 if (DECL_INITIAL (new_result) != NULL_TREE)
1944 DECL_SOURCE_LOCATION (olddecl)
1945 = DECL_SOURCE_LOCATION (old_result)
1946 = DECL_SOURCE_LOCATION (newdecl);
1947 DECL_INITIAL (old_result) = DECL_INITIAL (new_result);
1948 if (DECL_FUNCTION_TEMPLATE_P (newdecl))
1950 tree parm;
1951 DECL_ARGUMENTS (old_result)
1952 = DECL_ARGUMENTS (new_result);
1953 for (parm = DECL_ARGUMENTS (old_result); parm;
1954 parm = DECL_CHAIN (parm))
1955 DECL_CONTEXT (parm) = old_result;
1959 return olddecl;
1962 if (types_match)
1964 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1965 check_redeclaration_exception_specification (newdecl, olddecl);
1967 /* Automatically handles default parameters. */
1968 tree oldtype = TREE_TYPE (olddecl);
1969 tree newtype;
1971 /* For typedefs use the old type, as the new type's DECL_NAME points
1972 at newdecl, which will be ggc_freed. */
1973 if (TREE_CODE (newdecl) == TYPE_DECL)
1974 newtype = oldtype;
1975 else
1976 /* Merge the data types specified in the two decls. */
1977 newtype = merge_types (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
1979 if (VAR_P (newdecl))
1981 DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl);
1982 DECL_INITIALIZED_P (newdecl) |= DECL_INITIALIZED_P (olddecl);
1983 DECL_NONTRIVIALLY_INITIALIZED_P (newdecl)
1984 |= DECL_NONTRIVIALLY_INITIALIZED_P (olddecl);
1985 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (newdecl)
1986 |= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (olddecl);
1988 /* Merge the threadprivate attribute from OLDDECL into NEWDECL. */
1989 if (DECL_LANG_SPECIFIC (olddecl)
1990 && CP_DECL_THREADPRIVATE_P (olddecl))
1992 /* Allocate a LANG_SPECIFIC structure for NEWDECL, if needed. */
1993 if (!DECL_LANG_SPECIFIC (newdecl))
1994 retrofit_lang_decl (newdecl);
1996 CP_DECL_THREADPRIVATE_P (newdecl) = 1;
2000 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype;
2002 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2003 check_default_args (newdecl);
2005 /* Lay the type out, unless already done. */
2006 if (! same_type_p (newtype, oldtype)
2007 && TREE_TYPE (newdecl) != error_mark_node
2008 && !(processing_template_decl && uses_template_parms (newdecl)))
2009 layout_type (TREE_TYPE (newdecl));
2011 if ((VAR_P (newdecl)
2012 || TREE_CODE (newdecl) == PARM_DECL
2013 || TREE_CODE (newdecl) == RESULT_DECL
2014 || TREE_CODE (newdecl) == FIELD_DECL
2015 || TREE_CODE (newdecl) == TYPE_DECL)
2016 && !(processing_template_decl && uses_template_parms (newdecl)))
2017 layout_decl (newdecl, 0);
2019 /* Merge the type qualifiers. */
2020 if (TREE_READONLY (newdecl))
2021 TREE_READONLY (olddecl) = 1;
2022 if (TREE_THIS_VOLATILE (newdecl))
2023 TREE_THIS_VOLATILE (olddecl) = 1;
2024 if (TREE_NOTHROW (newdecl))
2025 TREE_NOTHROW (olddecl) = 1;
2027 /* Merge deprecatedness. */
2028 if (TREE_DEPRECATED (newdecl))
2029 TREE_DEPRECATED (olddecl) = 1;
2031 /* Preserve function specific target and optimization options */
2032 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2034 if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
2035 && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
2036 DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
2037 = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
2039 if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
2040 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
2041 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
2042 = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
2045 /* Merge the initialization information. */
2046 if (DECL_INITIAL (newdecl) == NULL_TREE
2047 && DECL_INITIAL (olddecl) != NULL_TREE)
2049 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2050 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2051 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2053 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
2054 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
2058 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2060 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
2061 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
2062 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
2063 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
2064 TREE_NOTHROW (newdecl) |= TREE_NOTHROW (olddecl);
2065 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
2066 DECL_IS_OPERATOR_NEW (newdecl) |= DECL_IS_OPERATOR_NEW (olddecl);
2067 DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
2068 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
2069 DECL_LOOPING_CONST_OR_PURE_P (newdecl)
2070 |= DECL_LOOPING_CONST_OR_PURE_P (olddecl);
2071 /* Keep the old RTL. */
2072 COPY_DECL_RTL (olddecl, newdecl);
2074 else if (VAR_P (newdecl)
2075 && (DECL_SIZE (olddecl) || !DECL_SIZE (newdecl)))
2077 /* Keep the old RTL. We cannot keep the old RTL if the old
2078 declaration was for an incomplete object and the new
2079 declaration is not since many attributes of the RTL will
2080 change. */
2081 COPY_DECL_RTL (olddecl, newdecl);
2084 /* If cannot merge, then use the new type and qualifiers,
2085 and don't preserve the old rtl. */
2086 else
2088 /* Clean out any memory we had of the old declaration. */
2089 tree oldstatic = value_member (olddecl, static_aggregates);
2090 if (oldstatic)
2091 TREE_VALUE (oldstatic) = error_mark_node;
2093 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
2094 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
2095 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
2096 TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
2099 /* Merge the storage class information. */
2100 merge_weak (newdecl, olddecl);
2102 DECL_DEFER_OUTPUT (newdecl) |= DECL_DEFER_OUTPUT (olddecl);
2103 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
2104 TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl);
2105 if (! DECL_EXTERNAL (olddecl))
2106 DECL_EXTERNAL (newdecl) = 0;
2108 new_template_info = NULL_TREE;
2109 if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl))
2111 bool new_redefines_gnu_inline = false;
2113 if (new_defines_function
2114 && ((DECL_INTERFACE_KNOWN (olddecl)
2115 && TREE_CODE (olddecl) == FUNCTION_DECL)
2116 || (TREE_CODE (olddecl) == TEMPLATE_DECL
2117 && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl))
2118 == FUNCTION_DECL))))
2120 tree fn = olddecl;
2122 if (TREE_CODE (fn) == TEMPLATE_DECL)
2123 fn = DECL_TEMPLATE_RESULT (olddecl);
2125 new_redefines_gnu_inline = GNU_INLINE_P (fn) && DECL_INITIAL (fn);
2128 if (!new_redefines_gnu_inline)
2130 DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl);
2131 DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl);
2132 DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl);
2134 DECL_TEMPLATE_INSTANTIATED (newdecl)
2135 |= DECL_TEMPLATE_INSTANTIATED (olddecl);
2136 DECL_ODR_USED (newdecl) |= DECL_ODR_USED (olddecl);
2138 /* If the OLDDECL is an instantiation and/or specialization,
2139 then the NEWDECL must be too. But, it may not yet be marked
2140 as such if the caller has created NEWDECL, but has not yet
2141 figured out that it is a redeclaration. */
2142 if (!DECL_USE_TEMPLATE (newdecl))
2143 DECL_USE_TEMPLATE (newdecl) = DECL_USE_TEMPLATE (olddecl);
2145 /* Don't really know how much of the language-specific
2146 values we should copy from old to new. */
2147 DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl);
2148 DECL_REPO_AVAILABLE_P (newdecl) = DECL_REPO_AVAILABLE_P (olddecl);
2149 DECL_INITIALIZED_IN_CLASS_P (newdecl)
2150 |= DECL_INITIALIZED_IN_CLASS_P (olddecl);
2152 if (LANG_DECL_HAS_MIN (newdecl))
2154 DECL_LANG_SPECIFIC (newdecl)->u.min.u2 =
2155 DECL_LANG_SPECIFIC (olddecl)->u.min.u2;
2156 if (DECL_TEMPLATE_INFO (newdecl))
2158 new_template_info = DECL_TEMPLATE_INFO (newdecl);
2159 if (DECL_TEMPLATE_INSTANTIATION (olddecl)
2160 && DECL_TEMPLATE_SPECIALIZATION (newdecl))
2161 /* Remember the presence of explicit specialization args. */
2162 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (olddecl))
2163 = TINFO_USED_TEMPLATE_ID (new_template_info);
2165 DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl);
2167 /* Only functions have these fields. */
2168 if (DECL_DECLARES_FUNCTION_P (newdecl))
2170 DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl);
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) = (TREE_PUBLIC (newdecl)
2222 && DECL_DECLARED_INLINE_P (newdecl));
2224 /* Don't propagate visibility from the template to the
2225 specialization here. We'll do that in determine_visibility if
2226 appropriate. */
2227 DECL_VISIBILITY_SPECIFIED (olddecl) = 0;
2229 /* [temp.expl.spec/14] We don't inline explicit specialization
2230 just because the primary template says so. */
2232 /* But still keep DECL_DISREGARD_INLINE_LIMITS in sync with
2233 the always_inline attribute. */
2234 if (DECL_DISREGARD_INLINE_LIMITS (olddecl)
2235 && !DECL_DISREGARD_INLINE_LIMITS (newdecl))
2237 if (DECL_DECLARED_INLINE_P (newdecl))
2238 DECL_DISREGARD_INLINE_LIMITS (newdecl) = true;
2239 else
2240 DECL_ATTRIBUTES (newdecl)
2241 = remove_attribute ("always_inline",
2242 DECL_ATTRIBUTES (newdecl));
2245 else if (new_defines_function && DECL_INITIAL (olddecl))
2247 /* Never inline re-defined extern inline functions.
2248 FIXME: this could be better handled by keeping both
2249 function as separate declarations. */
2250 DECL_UNINLINABLE (newdecl) = 1;
2252 else
2254 if (DECL_PENDING_INLINE_INFO (newdecl) == 0)
2255 DECL_PENDING_INLINE_INFO (newdecl) = DECL_PENDING_INLINE_INFO (olddecl);
2257 DECL_DECLARED_INLINE_P (newdecl) |= DECL_DECLARED_INLINE_P (olddecl);
2259 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
2260 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
2262 DECL_DISREGARD_INLINE_LIMITS (newdecl)
2263 = DECL_DISREGARD_INLINE_LIMITS (olddecl)
2264 = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
2265 || DECL_DISREGARD_INLINE_LIMITS (olddecl));
2268 /* Preserve abstractness on cloned [cd]tors. */
2269 DECL_ABSTRACT_P (newdecl) = DECL_ABSTRACT_P (olddecl);
2271 /* Update newdecl's parms to point at olddecl. */
2272 for (parm = DECL_ARGUMENTS (newdecl); parm;
2273 parm = DECL_CHAIN (parm))
2274 DECL_CONTEXT (parm) = olddecl;
2276 if (! types_match)
2278 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
2279 COPY_DECL_ASSEMBLER_NAME (newdecl, olddecl);
2280 COPY_DECL_RTL (newdecl, olddecl);
2282 if (! types_match || new_defines_function)
2284 /* These need to be copied so that the names are available.
2285 Note that if the types do match, we'll preserve inline
2286 info and other bits, but if not, we won't. */
2287 DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl);
2288 DECL_RESULT (olddecl) = DECL_RESULT (newdecl);
2290 /* If redeclaring a builtin function, it stays built in
2291 if newdecl is a gnu_inline definition, or if newdecl is just
2292 a declaration. */
2293 if (DECL_BUILT_IN (olddecl)
2294 && (new_defines_function ? GNU_INLINE_P (newdecl) : types_match))
2296 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
2297 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
2298 /* If we're keeping the built-in definition, keep the rtl,
2299 regardless of declaration matches. */
2300 COPY_DECL_RTL (olddecl, newdecl);
2301 if (DECL_BUILT_IN_CLASS (newdecl) == BUILT_IN_NORMAL)
2303 enum built_in_function fncode = DECL_FUNCTION_CODE (newdecl);
2304 switch (fncode)
2306 /* If a compatible prototype of these builtin functions
2307 is seen, assume the runtime implements it with the
2308 expected semantics. */
2309 case BUILT_IN_STPCPY:
2310 if (builtin_decl_explicit_p (fncode))
2311 set_builtin_decl_implicit_p (fncode, true);
2312 break;
2313 default:
2314 if (builtin_decl_explicit_p (fncode))
2315 set_builtin_decl_declared_p (fncode, true);
2316 break;
2320 if (new_defines_function)
2321 /* If defining a function declared with other language
2322 linkage, use the previously declared language linkage. */
2323 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
2324 else if (types_match)
2326 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
2327 /* Don't clear out the arguments if we're just redeclaring a
2328 function. */
2329 if (DECL_ARGUMENTS (olddecl))
2330 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
2333 else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
2334 NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl);
2336 /* Now preserve various other info from the definition. */
2337 TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl);
2338 TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl);
2339 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
2340 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
2342 /* Warn about conflicting visibility specifications. */
2343 if (DECL_VISIBILITY_SPECIFIED (olddecl)
2344 && DECL_VISIBILITY_SPECIFIED (newdecl)
2345 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
2347 if (warning_at (DECL_SOURCE_LOCATION (newdecl), OPT_Wattributes,
2348 "%qD: visibility attribute ignored because it "
2349 "conflicts with previous declaration", newdecl))
2350 inform (DECL_SOURCE_LOCATION (olddecl),
2351 "previous declaration of %qD", olddecl);
2353 /* Choose the declaration which specified visibility. */
2354 if (DECL_VISIBILITY_SPECIFIED (olddecl))
2356 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
2357 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
2359 /* Init priority used to be merged from newdecl to olddecl by the memcpy,
2360 so keep this behavior. */
2361 if (VAR_P (newdecl) && DECL_HAS_INIT_PRIORITY_P (newdecl))
2363 SET_DECL_INIT_PRIORITY (olddecl, DECL_INIT_PRIORITY (newdecl));
2364 DECL_HAS_INIT_PRIORITY_P (olddecl) = 1;
2366 /* Likewise for DECL_ALIGN, DECL_USER_ALIGN and DECL_PACKED. */
2367 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
2369 DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
2370 DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
2372 DECL_USER_ALIGN (olddecl) = DECL_USER_ALIGN (newdecl);
2373 if (TREE_CODE (newdecl) == FIELD_DECL)
2374 DECL_PACKED (olddecl) = DECL_PACKED (newdecl);
2376 /* The DECL_LANG_SPECIFIC information in OLDDECL will be replaced
2377 with that from NEWDECL below. */
2378 if (DECL_LANG_SPECIFIC (olddecl))
2380 gcc_assert (DECL_LANG_SPECIFIC (olddecl)
2381 != DECL_LANG_SPECIFIC (newdecl));
2382 ggc_free (DECL_LANG_SPECIFIC (olddecl));
2385 /* Merge the USED information. */
2386 if (TREE_USED (olddecl))
2387 TREE_USED (newdecl) = 1;
2388 else if (TREE_USED (newdecl))
2389 TREE_USED (olddecl) = 1;
2390 if (VAR_P (newdecl))
2392 if (DECL_READ_P (olddecl))
2393 DECL_READ_P (newdecl) = 1;
2394 else if (DECL_READ_P (newdecl))
2395 DECL_READ_P (olddecl) = 1;
2397 if (DECL_PRESERVE_P (olddecl))
2398 DECL_PRESERVE_P (newdecl) = 1;
2399 else if (DECL_PRESERVE_P (newdecl))
2400 DECL_PRESERVE_P (olddecl) = 1;
2402 /* Merge the DECL_FUNCTION_VERSIONED information. newdecl will be copied
2403 to olddecl and deleted. */
2404 if (TREE_CODE (newdecl) == FUNCTION_DECL
2405 && DECL_FUNCTION_VERSIONED (olddecl))
2407 /* Set the flag for newdecl so that it gets copied to olddecl. */
2408 DECL_FUNCTION_VERSIONED (newdecl) = 1;
2409 /* newdecl will be purged after copying to olddecl and is no longer
2410 a version. */
2411 cgraph_node::delete_function_version (newdecl);
2414 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2416 int function_size;
2417 struct symtab_node *snode = symtab_node::get (olddecl);
2419 function_size = sizeof (struct tree_decl_common);
2421 memcpy ((char *) olddecl + sizeof (struct tree_common),
2422 (char *) newdecl + sizeof (struct tree_common),
2423 function_size - sizeof (struct tree_common));
2425 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2426 (char *) newdecl + sizeof (struct tree_decl_common),
2427 sizeof (struct tree_function_decl) - sizeof (struct tree_decl_common));
2429 /* Preserve symtab node mapping. */
2430 olddecl->decl_with_vis.symtab_node = snode;
2432 if (new_template_info)
2433 /* If newdecl is a template instantiation, it is possible that
2434 the following sequence of events has occurred:
2436 o A friend function was declared in a class template. The
2437 class template was instantiated.
2439 o The instantiation of the friend declaration was
2440 recorded on the instantiation list, and is newdecl.
2442 o Later, however, instantiate_class_template called pushdecl
2443 on the newdecl to perform name injection. But, pushdecl in
2444 turn called duplicate_decls when it discovered that another
2445 declaration of a global function with the same name already
2446 existed.
2448 o Here, in duplicate_decls, we decided to clobber newdecl.
2450 If we're going to do that, we'd better make sure that
2451 olddecl, and not newdecl, is on the list of
2452 instantiations so that if we try to do the instantiation
2453 again we won't get the clobbered declaration. */
2454 reregister_specialization (newdecl,
2455 new_template_info,
2456 olddecl);
2458 else
2460 size_t size = tree_code_size (TREE_CODE (newdecl));
2462 memcpy ((char *) olddecl + sizeof (struct tree_common),
2463 (char *) newdecl + sizeof (struct tree_common),
2464 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
2465 switch (TREE_CODE (newdecl))
2467 case LABEL_DECL:
2468 case VAR_DECL:
2469 case RESULT_DECL:
2470 case PARM_DECL:
2471 case FIELD_DECL:
2472 case TYPE_DECL:
2473 case CONST_DECL:
2475 struct symtab_node *snode = NULL;
2477 if (TREE_CODE (newdecl) == VAR_DECL
2478 && (TREE_STATIC (olddecl) || TREE_PUBLIC (olddecl) || DECL_EXTERNAL (olddecl)))
2479 snode = symtab_node::get (olddecl);
2480 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2481 (char *) newdecl + sizeof (struct tree_decl_common),
2482 size - sizeof (struct tree_decl_common)
2483 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
2484 if (TREE_CODE (newdecl) == VAR_DECL)
2485 olddecl->decl_with_vis.symtab_node = snode;
2487 break;
2488 default:
2489 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2490 (char *) newdecl + sizeof (struct tree_decl_common),
2491 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common)
2492 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
2493 break;
2497 if (TREE_CODE (newdecl) == FUNCTION_DECL
2498 || TREE_CODE (newdecl) == VAR_DECL)
2500 if (DECL_EXTERNAL (olddecl)
2501 || TREE_PUBLIC (olddecl)
2502 || TREE_STATIC (olddecl))
2504 /* Merge the section attribute.
2505 We want to issue an error if the sections conflict but that must be
2506 done later in decl_attributes since we are called before attributes
2507 are assigned. */
2508 if (DECL_SECTION_NAME (newdecl) != NULL)
2509 set_decl_section_name (olddecl, DECL_SECTION_NAME (newdecl));
2511 if (DECL_ONE_ONLY (newdecl))
2513 struct symtab_node *oldsym, *newsym;
2514 if (TREE_CODE (olddecl) == FUNCTION_DECL)
2515 oldsym = cgraph_node::get_create (olddecl);
2516 else
2517 oldsym = varpool_node::get_create (olddecl);
2518 newsym = symtab_node::get (newdecl);
2519 oldsym->set_comdat_group (newsym->get_comdat_group ());
2523 if (TREE_CODE (newdecl) == VAR_DECL
2524 && DECL_THREAD_LOCAL_P (newdecl))
2525 set_decl_tls_model (olddecl, DECL_TLS_MODEL (newdecl));
2528 DECL_UID (olddecl) = olddecl_uid;
2529 if (olddecl_friend)
2530 DECL_FRIEND_P (olddecl) = 1;
2531 if (hidden_friend)
2533 DECL_ANTICIPATED (olddecl) = 1;
2534 DECL_HIDDEN_FRIEND_P (olddecl) = 1;
2537 /* NEWDECL contains the merged attribute lists.
2538 Update OLDDECL to be the same. */
2539 DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
2541 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
2542 so that encode_section_info has a chance to look at the new decl
2543 flags and attributes. */
2544 if (DECL_RTL_SET_P (olddecl)
2545 && (TREE_CODE (olddecl) == FUNCTION_DECL
2546 || (VAR_P (olddecl)
2547 && TREE_STATIC (olddecl))))
2548 make_decl_rtl (olddecl);
2550 /* The NEWDECL will no longer be needed. Because every out-of-class
2551 declaration of a member results in a call to duplicate_decls,
2552 freeing these nodes represents in a significant savings.
2554 Before releasing the node, be sore to remove function from symbol
2555 table that might have been inserted there to record comdat group.
2556 Be sure to however do not free DECL_STRUCT_FUNCTION becuase this
2557 structure is shared in between newdecl and oldecl. */
2558 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2559 DECL_STRUCT_FUNCTION (newdecl) = NULL;
2560 if (TREE_CODE (newdecl) == FUNCTION_DECL
2561 || TREE_CODE (newdecl) == VAR_DECL)
2563 struct symtab_node *snode = symtab_node::get (newdecl);
2564 if (snode)
2565 snode->remove ();
2567 ggc_free (newdecl);
2569 return olddecl;
2572 /* Return zero if the declaration NEWDECL is valid
2573 when the declaration OLDDECL (assumed to be for the same name)
2574 has already been seen.
2575 Otherwise return an error message format string with a %s
2576 where the identifier should go. */
2578 static const char *
2579 redeclaration_error_message (tree newdecl, tree olddecl)
2581 if (TREE_CODE (newdecl) == TYPE_DECL)
2583 /* Because C++ can put things into name space for free,
2584 constructs like "typedef struct foo { ... } foo"
2585 would look like an erroneous redeclaration. */
2586 if (same_type_p (TREE_TYPE (newdecl), TREE_TYPE (olddecl)))
2587 return NULL;
2588 else
2589 return G_("redefinition of %q#D");
2591 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
2593 /* If this is a pure function, its olddecl will actually be
2594 the original initialization to `0' (which we force to call
2595 abort()). Don't complain about redefinition in this case. */
2596 if (DECL_LANG_SPECIFIC (olddecl) && DECL_PURE_VIRTUAL_P (olddecl)
2597 && DECL_INITIAL (olddecl) == NULL_TREE)
2598 return NULL;
2600 /* If both functions come from different namespaces, this is not
2601 a redeclaration - this is a conflict with a used function. */
2602 if (DECL_NAMESPACE_SCOPE_P (olddecl)
2603 && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl)
2604 && ! decls_match (olddecl, newdecl))
2605 return G_("%qD conflicts with used function");
2607 /* We'll complain about linkage mismatches in
2608 warn_extern_redeclared_static. */
2610 /* Defining the same name twice is no good. */
2611 if (DECL_INITIAL (olddecl) != NULL_TREE
2612 && DECL_INITIAL (newdecl) != NULL_TREE)
2614 if (DECL_NAME (olddecl) == NULL_TREE)
2615 return G_("%q#D not declared in class");
2616 else if (!GNU_INLINE_P (olddecl)
2617 || GNU_INLINE_P (newdecl))
2618 return G_("redefinition of %q#D");
2621 if (DECL_DECLARED_INLINE_P (olddecl) && DECL_DECLARED_INLINE_P (newdecl))
2623 bool olda = GNU_INLINE_P (olddecl);
2624 bool newa = GNU_INLINE_P (newdecl);
2626 if (olda != newa)
2628 if (newa)
2629 return G_("%q+D redeclared inline with "
2630 "%<gnu_inline%> attribute");
2631 else
2632 return G_("%q+D redeclared inline without "
2633 "%<gnu_inline%> attribute");
2637 check_abi_tag_redeclaration
2638 (olddecl, lookup_attribute ("abi_tag", DECL_ATTRIBUTES (olddecl)),
2639 lookup_attribute ("abi_tag", DECL_ATTRIBUTES (newdecl)));
2641 return NULL;
2643 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2645 tree nt, ot;
2647 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
2649 if (COMPLETE_TYPE_P (TREE_TYPE (newdecl))
2650 && COMPLETE_TYPE_P (TREE_TYPE (olddecl)))
2651 return G_("redefinition of %q#D");
2652 return NULL;
2655 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) != FUNCTION_DECL
2656 || (DECL_TEMPLATE_RESULT (newdecl)
2657 == DECL_TEMPLATE_RESULT (olddecl)))
2658 return NULL;
2660 nt = DECL_TEMPLATE_RESULT (newdecl);
2661 if (DECL_TEMPLATE_INFO (nt))
2662 nt = DECL_TEMPLATE_RESULT (template_for_substitution (nt));
2663 ot = DECL_TEMPLATE_RESULT (olddecl);
2664 if (DECL_TEMPLATE_INFO (ot))
2665 ot = DECL_TEMPLATE_RESULT (template_for_substitution (ot));
2666 if (DECL_INITIAL (nt) && DECL_INITIAL (ot)
2667 && (!GNU_INLINE_P (ot) || GNU_INLINE_P (nt)))
2668 return G_("redefinition of %q#D");
2670 if (DECL_DECLARED_INLINE_P (ot) && DECL_DECLARED_INLINE_P (nt))
2672 bool olda = GNU_INLINE_P (ot);
2673 bool newa = GNU_INLINE_P (nt);
2675 if (olda != newa)
2677 if (newa)
2678 return G_("%q+D redeclared inline with "
2679 "%<gnu_inline%> attribute");
2680 else
2681 return G_("%q+D redeclared inline without "
2682 "%<gnu_inline%> attribute");
2686 /* Core issue #226 (C++0x):
2688 If a friend function template declaration specifies a
2689 default template-argument, that declaration shall be a
2690 definition and shall be the only declaration of the
2691 function template in the translation unit. */
2692 if ((cxx_dialect != cxx98)
2693 && TREE_CODE (ot) == FUNCTION_DECL && DECL_FRIEND_P (ot)
2694 && !check_default_tmpl_args (nt, DECL_TEMPLATE_PARMS (newdecl),
2695 /*is_primary=*/true,
2696 /*is_partial=*/false,
2697 /*is_friend_decl=*/2))
2698 return G_("redeclaration of friend %q#D "
2699 "may not have default template arguments");
2701 return NULL;
2703 else if (VAR_P (newdecl)
2704 && DECL_THREAD_LOCAL_P (newdecl) != DECL_THREAD_LOCAL_P (olddecl)
2705 && (! DECL_LANG_SPECIFIC (olddecl)
2706 || ! CP_DECL_THREADPRIVATE_P (olddecl)
2707 || DECL_THREAD_LOCAL_P (newdecl)))
2709 /* Only variables can be thread-local, and all declarations must
2710 agree on this property. */
2711 if (DECL_THREAD_LOCAL_P (newdecl))
2712 return G_("thread-local declaration of %q#D follows "
2713 "non-thread-local declaration");
2714 else
2715 return G_("non-thread-local declaration of %q#D follows "
2716 "thread-local declaration");
2718 else if (toplevel_bindings_p () || DECL_NAMESPACE_SCOPE_P (newdecl))
2720 /* The objects have been declared at namespace scope. If either
2721 is a member of an anonymous union, then this is an invalid
2722 redeclaration. For example:
2724 int i;
2725 union { int i; };
2727 is invalid. */
2728 if ((VAR_P (newdecl) && DECL_ANON_UNION_VAR_P (newdecl))
2729 || (VAR_P (olddecl) && DECL_ANON_UNION_VAR_P (olddecl)))
2730 return G_("redeclaration of %q#D");
2731 /* If at least one declaration is a reference, there is no
2732 conflict. For example:
2734 int i = 3;
2735 extern int i;
2737 is valid. */
2738 if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
2739 return NULL;
2740 /* Reject two definitions. */
2741 return G_("redefinition of %q#D");
2743 else
2745 /* Objects declared with block scope: */
2746 /* Reject two definitions, and reject a definition
2747 together with an external reference. */
2748 if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl)))
2749 return G_("redeclaration of %q#D");
2750 return NULL;
2754 /* Hash and equality functions for the named_label table. */
2756 hashval_t
2757 named_label_hasher::hash (named_label_entry *ent)
2759 return DECL_UID (ent->label_decl);
2762 bool
2763 named_label_hasher::equal (named_label_entry *a, named_label_entry *b)
2765 return a->label_decl == b->label_decl;
2768 /* Create a new label, named ID. */
2770 static tree
2771 make_label_decl (tree id, int local_p)
2773 struct named_label_entry *ent;
2774 tree decl;
2776 decl = build_decl (input_location, LABEL_DECL, id, void_type_node);
2778 DECL_CONTEXT (decl) = current_function_decl;
2779 DECL_MODE (decl) = VOIDmode;
2780 C_DECLARED_LABEL_FLAG (decl) = local_p;
2782 /* Say where one reference is to the label, for the sake of the
2783 error if it is not defined. */
2784 DECL_SOURCE_LOCATION (decl) = input_location;
2786 /* Record the fact that this identifier is bound to this label. */
2787 SET_IDENTIFIER_LABEL_VALUE (id, decl);
2789 /* Create the label htab for the function on demand. */
2790 if (!named_labels)
2791 named_labels = hash_table<named_label_hasher>::create_ggc (13);
2793 /* Record this label on the list of labels used in this function.
2794 We do this before calling make_label_decl so that we get the
2795 IDENTIFIER_LABEL_VALUE before the new label is declared. */
2796 ent = ggc_cleared_alloc<named_label_entry> ();
2797 ent->label_decl = decl;
2799 named_label_entry **slot = named_labels->find_slot (ent, INSERT);
2800 gcc_assert (*slot == NULL);
2801 *slot = ent;
2803 return decl;
2806 /* Look for a label named ID in the current function. If one cannot
2807 be found, create one. (We keep track of used, but undefined,
2808 labels, and complain about them at the end of a function.) */
2810 static tree
2811 lookup_label_1 (tree id)
2813 tree decl;
2815 /* You can't use labels at global scope. */
2816 if (current_function_decl == NULL_TREE)
2818 error ("label %qE referenced outside of any function", id);
2819 return NULL_TREE;
2822 /* See if we've already got this label. */
2823 decl = IDENTIFIER_LABEL_VALUE (id);
2824 if (decl != NULL_TREE && DECL_CONTEXT (decl) == current_function_decl)
2825 return decl;
2827 decl = make_label_decl (id, /*local_p=*/0);
2828 return decl;
2831 /* Wrapper for lookup_label_1. */
2833 tree
2834 lookup_label (tree id)
2836 tree ret;
2837 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
2838 ret = lookup_label_1 (id);
2839 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
2840 return ret;
2843 /* Declare a local label named ID. */
2845 tree
2846 declare_local_label (tree id)
2848 tree decl;
2849 cp_label_binding bind;
2851 /* Add a new entry to the SHADOWED_LABELS list so that when we leave
2852 this scope we can restore the old value of IDENTIFIER_TYPE_VALUE. */
2853 bind.prev_value = IDENTIFIER_LABEL_VALUE (id);
2855 decl = make_label_decl (id, /*local_p=*/1);
2856 bind.label = decl;
2857 vec_safe_push (current_binding_level->shadowed_labels, bind);
2859 return decl;
2862 /* Returns nonzero if it is ill-formed to jump past the declaration of
2863 DECL. Returns 2 if it's also a real problem. */
2865 static int
2866 decl_jump_unsafe (tree decl)
2868 /* [stmt.dcl]/3: A program that jumps from a point where a local variable
2869 with automatic storage duration is not in scope to a point where it is
2870 in scope is ill-formed unless the variable has scalar type, class type
2871 with a trivial default constructor and a trivial destructor, a
2872 cv-qualified version of one of these types, or an array of one of the
2873 preceding types and is declared without an initializer (8.5). */
2874 tree type = TREE_TYPE (decl);
2876 if (!VAR_P (decl) || TREE_STATIC (decl)
2877 || type == error_mark_node)
2878 return 0;
2880 if (DECL_NONTRIVIALLY_INITIALIZED_P (decl)
2881 || variably_modified_type_p (type, NULL_TREE))
2882 return 2;
2884 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
2885 return 1;
2887 return 0;
2890 /* A subroutine of check_previous_goto_1 to identify a branch to the user. */
2892 static bool
2893 identify_goto (tree decl, const location_t *locus)
2895 bool complained = (decl
2896 ? permerror (input_location, "jump to label %qD", decl)
2897 : permerror (input_location, "jump to case label"));
2898 if (complained && locus)
2899 inform (*locus, " from here");
2900 return complained;
2903 /* Check that a single previously seen jump to a newly defined label
2904 is OK. DECL is the LABEL_DECL or 0; LEVEL is the binding_level for
2905 the jump context; NAMES are the names in scope in LEVEL at the jump
2906 context; LOCUS is the source position of the jump or 0. Returns
2907 true if all is well. */
2909 static bool
2910 check_previous_goto_1 (tree decl, cp_binding_level* level, tree names,
2911 bool exited_omp, const location_t *locus)
2913 cp_binding_level *b;
2914 bool identified = false, complained = false;
2915 bool saw_eh = false, saw_omp = false;
2917 if (exited_omp)
2919 complained = identify_goto (decl, locus);
2920 if (complained)
2921 inform (input_location, " exits OpenMP structured block");
2922 identified = saw_omp = true;
2925 for (b = current_binding_level; b ; b = b->level_chain)
2927 tree new_decls, old_decls = (b == level ? names : NULL_TREE);
2929 for (new_decls = b->names; new_decls != old_decls;
2930 new_decls = (DECL_P (new_decls) ? DECL_CHAIN (new_decls)
2931 : TREE_CHAIN (new_decls)))
2933 int problem = decl_jump_unsafe (new_decls);
2934 if (! problem)
2935 continue;
2937 if (!identified)
2939 complained = identify_goto (decl, locus);
2940 identified = true;
2942 if (complained)
2944 if (problem > 1)
2945 inform (input_location,
2946 " crosses initialization of %q+#D", new_decls);
2947 else
2948 inform (input_location, " enters scope of %q+#D which has "
2949 "non-trivial destructor", new_decls);
2953 if (b == level)
2954 break;
2955 if ((b->kind == sk_try || b->kind == sk_catch) && !saw_eh)
2957 if (!identified)
2959 complained = identify_goto (decl, locus);
2960 identified = true;
2962 if (complained)
2964 if (b->kind == sk_try)
2965 inform (input_location, " enters try block");
2966 else
2967 inform (input_location, " enters catch block");
2969 saw_eh = true;
2971 if (b->kind == sk_omp && !saw_omp)
2973 if (!identified)
2975 complained = identify_goto (decl, locus);
2976 identified = true;
2978 if (complained)
2979 inform (input_location, " enters OpenMP structured block");
2980 saw_omp = true;
2984 return !identified;
2987 static void
2988 check_previous_goto (tree decl, struct named_label_use_entry *use)
2990 check_previous_goto_1 (decl, use->binding_level,
2991 use->names_in_scope, use->in_omp_scope,
2992 &use->o_goto_locus);
2995 static bool
2996 check_switch_goto (cp_binding_level* level)
2998 return check_previous_goto_1 (NULL_TREE, level, level->names, false, NULL);
3001 /* Check that a new jump to a label DECL is OK. Called by
3002 finish_goto_stmt. */
3004 void
3005 check_goto (tree decl)
3007 struct named_label_entry *ent, dummy;
3008 bool saw_catch = false, identified = false, complained = false;
3009 tree bad;
3010 unsigned ix;
3012 /* We can't know where a computed goto is jumping.
3013 So we assume that it's OK. */
3014 if (TREE_CODE (decl) != LABEL_DECL)
3015 return;
3017 /* We didn't record any information about this label when we created it,
3018 and there's not much point since it's trivial to analyze as a return. */
3019 if (decl == cdtor_label)
3020 return;
3022 dummy.label_decl = decl;
3023 ent = named_labels->find (&dummy);
3024 gcc_assert (ent != NULL);
3026 /* If the label hasn't been defined yet, defer checking. */
3027 if (! DECL_INITIAL (decl))
3029 struct named_label_use_entry *new_use;
3031 /* Don't bother creating another use if the last goto had the
3032 same data, and will therefore create the same set of errors. */
3033 if (ent->uses
3034 && ent->uses->names_in_scope == current_binding_level->names)
3035 return;
3037 new_use = ggc_alloc<named_label_use_entry> ();
3038 new_use->binding_level = current_binding_level;
3039 new_use->names_in_scope = current_binding_level->names;
3040 new_use->o_goto_locus = input_location;
3041 new_use->in_omp_scope = false;
3043 new_use->next = ent->uses;
3044 ent->uses = new_use;
3045 return;
3048 if (ent->in_try_scope || ent->in_catch_scope
3049 || ent->in_omp_scope || !vec_safe_is_empty (ent->bad_decls))
3051 complained = permerror (input_location, "jump to label %q+D", decl);
3052 if (complained)
3053 inform (input_location, " from here");
3054 identified = true;
3057 FOR_EACH_VEC_SAFE_ELT (ent->bad_decls, ix, bad)
3059 int u = decl_jump_unsafe (bad);
3061 if (u > 1 && DECL_ARTIFICIAL (bad))
3063 /* Can't skip init of __exception_info. */
3064 if (complained)
3065 inform (DECL_SOURCE_LOCATION (bad), " enters catch block");
3066 saw_catch = true;
3068 else if (complained)
3070 if (u > 1)
3071 inform (input_location, " skips initialization of %q+#D", bad);
3072 else
3073 inform (input_location, " enters scope of %q+#D which has "
3074 "non-trivial destructor", bad);
3078 if (complained)
3080 if (ent->in_try_scope)
3081 inform (input_location, " enters try block");
3082 else if (ent->in_catch_scope && !saw_catch)
3083 inform (input_location, " enters catch block");
3086 if (ent->in_omp_scope)
3088 if (complained)
3089 inform (input_location, " enters OpenMP structured block");
3091 else if (flag_openmp)
3093 cp_binding_level *b;
3094 for (b = current_binding_level; b ; b = b->level_chain)
3096 if (b == ent->binding_level)
3097 break;
3098 if (b->kind == sk_omp)
3100 if (!identified)
3102 complained = permerror (input_location,
3103 "jump to label %q+D", decl);
3104 if (complained)
3105 inform (input_location, " from here");
3106 identified = true;
3108 if (complained)
3109 inform (input_location, " exits OpenMP structured block");
3110 break;
3116 /* Check that a return is ok wrt OpenMP structured blocks.
3117 Called by finish_return_stmt. Returns true if all is well. */
3119 bool
3120 check_omp_return (void)
3122 cp_binding_level *b;
3123 for (b = current_binding_level; b ; b = b->level_chain)
3124 if (b->kind == sk_omp)
3126 error ("invalid exit from OpenMP structured block");
3127 return false;
3129 else if (b->kind == sk_function_parms)
3130 break;
3131 return true;
3134 /* Define a label, specifying the location in the source file.
3135 Return the LABEL_DECL node for the label. */
3137 static tree
3138 define_label_1 (location_t location, tree name)
3140 struct named_label_entry *ent, dummy;
3141 cp_binding_level *p;
3142 tree decl;
3144 decl = lookup_label (name);
3146 dummy.label_decl = decl;
3147 ent = named_labels->find (&dummy);
3148 gcc_assert (ent != NULL);
3150 /* After labels, make any new cleanups in the function go into their
3151 own new (temporary) binding contour. */
3152 for (p = current_binding_level;
3153 p->kind != sk_function_parms;
3154 p = p->level_chain)
3155 p->more_cleanups_ok = 0;
3157 if (name == get_identifier ("wchar_t"))
3158 permerror (input_location, "label named wchar_t");
3160 if (DECL_INITIAL (decl) != NULL_TREE)
3162 error ("duplicate label %qD", decl);
3163 return error_mark_node;
3165 else
3167 struct named_label_use_entry *use;
3169 /* Mark label as having been defined. */
3170 DECL_INITIAL (decl) = error_mark_node;
3171 /* Say where in the source. */
3172 DECL_SOURCE_LOCATION (decl) = location;
3174 ent->binding_level = current_binding_level;
3175 ent->names_in_scope = current_binding_level->names;
3177 for (use = ent->uses; use ; use = use->next)
3178 check_previous_goto (decl, use);
3179 ent->uses = NULL;
3182 return decl;
3185 /* Wrapper for define_label_1. */
3187 tree
3188 define_label (location_t location, tree name)
3190 tree ret;
3191 bool running = timevar_cond_start (TV_NAME_LOOKUP);
3192 ret = define_label_1 (location, name);
3193 timevar_cond_stop (TV_NAME_LOOKUP, running);
3194 return ret;
3198 struct cp_switch
3200 cp_binding_level *level;
3201 struct cp_switch *next;
3202 /* The SWITCH_STMT being built. */
3203 tree switch_stmt;
3204 /* A splay-tree mapping the low element of a case range to the high
3205 element, or NULL_TREE if there is no high element. Used to
3206 determine whether or not a new case label duplicates an old case
3207 label. We need a tree, rather than simply a hash table, because
3208 of the GNU case range extension. */
3209 splay_tree cases;
3212 /* A stack of the currently active switch statements. The innermost
3213 switch statement is on the top of the stack. There is no need to
3214 mark the stack for garbage collection because it is only active
3215 during the processing of the body of a function, and we never
3216 collect at that point. */
3218 static struct cp_switch *switch_stack;
3220 /* Called right after a switch-statement condition is parsed.
3221 SWITCH_STMT is the switch statement being parsed. */
3223 void
3224 push_switch (tree switch_stmt)
3226 struct cp_switch *p = XNEW (struct cp_switch);
3227 p->level = current_binding_level;
3228 p->next = switch_stack;
3229 p->switch_stmt = switch_stmt;
3230 p->cases = splay_tree_new (case_compare, NULL, NULL);
3231 switch_stack = p;
3234 void
3235 pop_switch (void)
3237 struct cp_switch *cs = switch_stack;
3238 location_t switch_location;
3240 /* Emit warnings as needed. */
3241 switch_location = EXPR_LOC_OR_LOC (cs->switch_stmt, input_location);
3242 if (!processing_template_decl)
3243 c_do_switch_warnings (cs->cases, switch_location,
3244 SWITCH_STMT_TYPE (cs->switch_stmt),
3245 SWITCH_STMT_COND (cs->switch_stmt));
3247 splay_tree_delete (cs->cases);
3248 switch_stack = switch_stack->next;
3249 free (cs);
3252 /* Convert a case constant VALUE in a switch to the type TYPE of the switch
3253 condition. Note that if TYPE and VALUE are already integral we don't
3254 really do the conversion because the language-independent
3255 warning/optimization code will work better that way. */
3257 static tree
3258 case_conversion (tree type, tree value)
3260 if (value == NULL_TREE)
3261 return value;
3263 if (cxx_dialect >= cxx11
3264 && (SCOPED_ENUM_P (type)
3265 || !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (value))))
3267 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
3268 type = type_promotes_to (type);
3269 value = (perform_implicit_conversion_flags
3270 (type, value, tf_warning_or_error,
3271 LOOKUP_IMPLICIT | LOOKUP_NO_NON_INTEGRAL));
3273 return cxx_constant_value (value);
3276 /* Note that we've seen a definition of a case label, and complain if this
3277 is a bad place for one. */
3279 tree
3280 finish_case_label (location_t loc, tree low_value, tree high_value)
3282 tree cond, r;
3283 cp_binding_level *p;
3284 tree type;
3286 if (processing_template_decl)
3288 tree label;
3290 /* For templates, just add the case label; we'll do semantic
3291 analysis at instantiation-time. */
3292 label = build_decl (loc, LABEL_DECL, NULL_TREE, NULL_TREE);
3293 return add_stmt (build_case_label (low_value, high_value, label));
3296 /* Find the condition on which this switch statement depends. */
3297 cond = SWITCH_STMT_COND (switch_stack->switch_stmt);
3298 if (cond && TREE_CODE (cond) == TREE_LIST)
3299 cond = TREE_VALUE (cond);
3301 if (!check_switch_goto (switch_stack->level))
3302 return error_mark_node;
3304 type = SWITCH_STMT_TYPE (switch_stack->switch_stmt);
3306 low_value = case_conversion (type, low_value);
3307 high_value = case_conversion (type, high_value);
3309 r = c_add_case_label (loc, switch_stack->cases, cond, type,
3310 low_value, high_value);
3312 /* After labels, make any new cleanups in the function go into their
3313 own new (temporary) binding contour. */
3314 for (p = current_binding_level;
3315 p->kind != sk_function_parms;
3316 p = p->level_chain)
3317 p->more_cleanups_ok = 0;
3319 return r;
3322 struct typename_info {
3323 tree scope;
3324 tree name;
3325 tree template_id;
3326 bool enum_p;
3327 bool class_p;
3330 struct typename_hasher : ggc_hasher<tree>
3332 typedef typename_info *compare_type;
3334 /* Hash a TYPENAME_TYPE. */
3336 static hashval_t
3337 hash (tree t)
3339 hashval_t hash;
3341 hash = (htab_hash_pointer (TYPE_CONTEXT (t))
3342 ^ htab_hash_pointer (TYPE_IDENTIFIER (t)));
3344 return hash;
3347 /* Compare two TYPENAME_TYPEs. */
3349 static bool
3350 equal (tree t1, const typename_info *t2)
3352 return (TYPE_IDENTIFIER (t1) == t2->name
3353 && TYPE_CONTEXT (t1) == t2->scope
3354 && TYPENAME_TYPE_FULLNAME (t1) == t2->template_id
3355 && TYPENAME_IS_ENUM_P (t1) == t2->enum_p
3356 && TYPENAME_IS_CLASS_P (t1) == t2->class_p);
3360 /* Build a TYPENAME_TYPE. If the type is `typename T::t', CONTEXT is
3361 the type of `T', NAME is the IDENTIFIER_NODE for `t'.
3363 Returns the new TYPENAME_TYPE. */
3365 static GTY (()) hash_table<typename_hasher> *typename_htab;
3367 static tree
3368 build_typename_type (tree context, tree name, tree fullname,
3369 enum tag_types tag_type)
3371 tree t;
3372 tree d;
3373 typename_info ti;
3374 tree *e;
3375 hashval_t hash;
3377 if (typename_htab == NULL)
3378 typename_htab = hash_table<typename_hasher>::create_ggc (61);
3380 ti.scope = FROB_CONTEXT (context);
3381 ti.name = name;
3382 ti.template_id = fullname;
3383 ti.enum_p = tag_type == enum_type;
3384 ti.class_p = (tag_type == class_type
3385 || tag_type == record_type
3386 || tag_type == union_type);
3387 hash = (htab_hash_pointer (ti.scope)
3388 ^ htab_hash_pointer (ti.name));
3390 /* See if we already have this type. */
3391 e = typename_htab->find_slot_with_hash (&ti, hash, INSERT);
3392 if (*e)
3393 t = *e;
3394 else
3396 /* Build the TYPENAME_TYPE. */
3397 t = cxx_make_type (TYPENAME_TYPE);
3398 TYPE_CONTEXT (t) = ti.scope;
3399 TYPENAME_TYPE_FULLNAME (t) = ti.template_id;
3400 TYPENAME_IS_ENUM_P (t) = ti.enum_p;
3401 TYPENAME_IS_CLASS_P (t) = ti.class_p;
3403 /* Build the corresponding TYPE_DECL. */
3404 d = build_decl (input_location, TYPE_DECL, name, t);
3405 TYPE_NAME (TREE_TYPE (d)) = d;
3406 TYPE_STUB_DECL (TREE_TYPE (d)) = d;
3407 DECL_CONTEXT (d) = FROB_CONTEXT (context);
3408 DECL_ARTIFICIAL (d) = 1;
3410 /* Store it in the hash table. */
3411 *e = t;
3413 /* TYPENAME_TYPEs must always be compared structurally, because
3414 they may or may not resolve down to another type depending on
3415 the currently open classes. */
3416 SET_TYPE_STRUCTURAL_EQUALITY (t);
3419 return t;
3422 /* Resolve `typename CONTEXT::NAME'. TAG_TYPE indicates the tag
3423 provided to name the type. Returns an appropriate type, unless an
3424 error occurs, in which case error_mark_node is returned. If we
3425 locate a non-artificial TYPE_DECL and TF_KEEP_TYPE_DECL is set, we
3426 return that, rather than the _TYPE it corresponds to, in other
3427 cases we look through the type decl. If TF_ERROR is set, complain
3428 about errors, otherwise be quiet. */
3430 tree
3431 make_typename_type (tree context, tree name, enum tag_types tag_type,
3432 tsubst_flags_t complain)
3434 tree fullname;
3435 tree t;
3436 bool want_template;
3438 if (name == error_mark_node
3439 || context == NULL_TREE
3440 || context == error_mark_node)
3441 return error_mark_node;
3443 if (TYPE_P (name))
3445 if (!(TYPE_LANG_SPECIFIC (name)
3446 && (CLASSTYPE_IS_TEMPLATE (name)
3447 || CLASSTYPE_USE_TEMPLATE (name))))
3448 name = TYPE_IDENTIFIER (name);
3449 else
3450 /* Create a TEMPLATE_ID_EXPR for the type. */
3451 name = build_nt (TEMPLATE_ID_EXPR,
3452 CLASSTYPE_TI_TEMPLATE (name),
3453 CLASSTYPE_TI_ARGS (name));
3455 else if (TREE_CODE (name) == TYPE_DECL)
3456 name = DECL_NAME (name);
3458 fullname = name;
3460 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3462 name = TREE_OPERAND (name, 0);
3463 if (TREE_CODE (name) == TEMPLATE_DECL)
3464 name = TREE_OPERAND (fullname, 0) = DECL_NAME (name);
3465 else if (TREE_CODE (name) == OVERLOAD)
3467 if (complain & tf_error)
3468 error ("%qD is not a type", name);
3469 return error_mark_node;
3472 if (TREE_CODE (name) == TEMPLATE_DECL)
3474 if (complain & tf_error)
3475 error ("%qD used without template parameters", name);
3476 return error_mark_node;
3478 gcc_assert (identifier_p (name));
3479 gcc_assert (TYPE_P (context));
3481 if (!MAYBE_CLASS_TYPE_P (context))
3483 if (complain & tf_error)
3484 error ("%q#T is not a class", context);
3485 return error_mark_node;
3488 /* When the CONTEXT is a dependent type, NAME could refer to a
3489 dependent base class of CONTEXT. But look inside it anyway
3490 if CONTEXT is a currently open scope, in case it refers to a
3491 member of the current instantiation or a non-dependent base;
3492 lookup will stop when we hit a dependent base. */
3493 if (!dependent_scope_p (context))
3494 /* We should only set WANT_TYPE when we're a nested typename type.
3495 Then we can give better diagnostics if we find a non-type. */
3496 t = lookup_field (context, name, 2, /*want_type=*/true);
3497 else
3498 t = NULL_TREE;
3500 if ((!t || TREE_CODE (t) == TREE_LIST) && dependent_type_p (context))
3501 return build_typename_type (context, name, fullname, tag_type);
3503 want_template = TREE_CODE (fullname) == TEMPLATE_ID_EXPR;
3505 if (!t)
3507 if (complain & tf_error)
3508 error (want_template ? G_("no class template named %q#T in %q#T")
3509 : G_("no type named %q#T in %q#T"), name, context);
3510 return error_mark_node;
3513 /* Pull out the template from an injected-class-name (or multiple). */
3514 if (want_template)
3515 t = maybe_get_template_decl_from_type_decl (t);
3517 if (TREE_CODE (t) == TREE_LIST)
3519 if (complain & tf_error)
3521 error ("lookup of %qT in %qT is ambiguous", name, context);
3522 print_candidates (t);
3524 return error_mark_node;
3527 if (want_template && !DECL_TYPE_TEMPLATE_P (t))
3529 if (complain & tf_error)
3530 error ("%<typename %T::%D%> names %q#T, which is not a class template",
3531 context, name, t);
3532 return error_mark_node;
3534 if (!want_template && TREE_CODE (t) != TYPE_DECL)
3536 if (complain & tf_error)
3537 error ("%<typename %T::%D%> names %q#T, which is not a type",
3538 context, name, t);
3539 return error_mark_node;
3542 if (!perform_or_defer_access_check (TYPE_BINFO (context), t, t, complain))
3543 return error_mark_node;
3545 /* If we are currently parsing a template and if T is a typedef accessed
3546 through CONTEXT then we need to remember and check access of T at
3547 template instantiation time. */
3548 add_typedef_to_current_template_for_access_check (t, context, input_location);
3550 if (want_template)
3551 return lookup_template_class (t, TREE_OPERAND (fullname, 1),
3552 NULL_TREE, context,
3553 /*entering_scope=*/0,
3554 tf_warning_or_error | tf_user);
3556 if (DECL_ARTIFICIAL (t) || !(complain & tf_keep_type_decl))
3557 t = TREE_TYPE (t);
3559 maybe_record_typedef_use (t);
3561 return t;
3564 /* Resolve `CONTEXT::template NAME'. Returns a TEMPLATE_DECL if the name
3565 can be resolved or an UNBOUND_CLASS_TEMPLATE, unless an error occurs,
3566 in which case error_mark_node is returned.
3568 If PARM_LIST is non-NULL, also make sure that the template parameter
3569 list of TEMPLATE_DECL matches.
3571 If COMPLAIN zero, don't complain about any errors that occur. */
3573 tree
3574 make_unbound_class_template (tree context, tree name, tree parm_list,
3575 tsubst_flags_t complain)
3577 tree t;
3578 tree d;
3580 if (TYPE_P (name))
3581 name = TYPE_IDENTIFIER (name);
3582 else if (DECL_P (name))
3583 name = DECL_NAME (name);
3584 gcc_assert (identifier_p (name));
3586 if (!dependent_type_p (context)
3587 || currently_open_class (context))
3589 tree tmpl = NULL_TREE;
3591 if (MAYBE_CLASS_TYPE_P (context))
3592 tmpl = lookup_field (context, name, 0, false);
3594 if (tmpl && TREE_CODE (tmpl) == TYPE_DECL)
3595 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
3597 if (!tmpl || !DECL_TYPE_TEMPLATE_P (tmpl))
3599 if (complain & tf_error)
3600 error ("no class template named %q#T in %q#T", name, context);
3601 return error_mark_node;
3604 if (parm_list
3605 && !comp_template_parms (DECL_TEMPLATE_PARMS (tmpl), parm_list))
3607 if (complain & tf_error)
3609 error ("template parameters do not match template %qD", tmpl);
3610 inform (DECL_SOURCE_LOCATION (tmpl),
3611 "%qD declared here", tmpl);
3613 return error_mark_node;
3616 if (!perform_or_defer_access_check (TYPE_BINFO (context), tmpl, tmpl,
3617 complain))
3618 return error_mark_node;
3620 return tmpl;
3623 /* Build the UNBOUND_CLASS_TEMPLATE. */
3624 t = cxx_make_type (UNBOUND_CLASS_TEMPLATE);
3625 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
3626 TREE_TYPE (t) = NULL_TREE;
3627 SET_TYPE_STRUCTURAL_EQUALITY (t);
3629 /* Build the corresponding TEMPLATE_DECL. */
3630 d = build_decl (input_location, TEMPLATE_DECL, name, t);
3631 TYPE_NAME (TREE_TYPE (d)) = d;
3632 TYPE_STUB_DECL (TREE_TYPE (d)) = d;
3633 DECL_CONTEXT (d) = FROB_CONTEXT (context);
3634 DECL_ARTIFICIAL (d) = 1;
3635 DECL_TEMPLATE_PARMS (d) = parm_list;
3637 return t;
3642 /* Push the declarations of builtin types into the namespace.
3643 RID_INDEX is the index of the builtin type in the array
3644 RID_POINTERS. NAME is the name used when looking up the builtin
3645 type. TYPE is the _TYPE node for the builtin type. */
3647 void
3648 record_builtin_type (enum rid rid_index,
3649 const char* name,
3650 tree type)
3652 tree rname = NULL_TREE, tname = NULL_TREE;
3653 tree tdecl = NULL_TREE;
3655 if ((int) rid_index < (int) RID_MAX)
3656 rname = ridpointers[(int) rid_index];
3657 if (name)
3658 tname = get_identifier (name);
3660 /* The calls to SET_IDENTIFIER_GLOBAL_VALUE below should be
3661 eliminated. Built-in types should not be looked up name; their
3662 names are keywords that the parser can recognize. However, there
3663 is code in c-common.c that uses identifier_global_value to look
3664 up built-in types by name. */
3665 if (tname)
3667 tdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, tname, type);
3668 DECL_ARTIFICIAL (tdecl) = 1;
3669 SET_IDENTIFIER_GLOBAL_VALUE (tname, tdecl);
3671 if (rname)
3673 if (!tdecl)
3675 tdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, rname, type);
3676 DECL_ARTIFICIAL (tdecl) = 1;
3678 SET_IDENTIFIER_GLOBAL_VALUE (rname, tdecl);
3681 if (!TYPE_NAME (type))
3682 TYPE_NAME (type) = tdecl;
3684 if (tdecl)
3685 debug_hooks->type_decl (tdecl, 0);
3688 /* Record one of the standard Java types.
3689 * Declare it as having the given NAME.
3690 * If SIZE > 0, it is the size of one of the integral types;
3691 * otherwise it is the negative of the size of one of the other types. */
3693 static tree
3694 record_builtin_java_type (const char* name, int size)
3696 tree type, decl;
3697 if (size > 0)
3699 type = build_nonstandard_integer_type (size, 0);
3700 type = build_distinct_type_copy (type);
3702 else if (size > -32)
3704 tree stype;
3705 /* "__java_char" or ""__java_boolean". */
3706 type = build_nonstandard_integer_type (-size, 1);
3707 type = build_distinct_type_copy (type);
3708 /* Get the signed type cached and attached to the unsigned type,
3709 so it doesn't get garbage-collected at "random" times,
3710 causing potential codegen differences out of different UIDs
3711 and different alias set numbers. */
3712 stype = build_nonstandard_integer_type (-size, 0);
3713 stype = build_distinct_type_copy (stype);
3714 TREE_CHAIN (type) = stype;
3715 /*if (size == -1) TREE_SET_CODE (type, BOOLEAN_TYPE);*/
3717 else
3718 { /* "__java_float" or ""__java_double". */
3719 type = make_node (REAL_TYPE);
3720 TYPE_PRECISION (type) = - size;
3721 layout_type (type);
3723 record_builtin_type (RID_MAX, name, type);
3724 decl = TYPE_NAME (type);
3726 /* Suppress generate debug symbol entries for these types,
3727 since for normal C++ they are just clutter.
3728 However, push_lang_context undoes this if extern "Java" is seen. */
3729 DECL_IGNORED_P (decl) = 1;
3731 TYPE_FOR_JAVA (type) = 1;
3732 return type;
3735 /* Push a type into the namespace so that the back ends ignore it. */
3737 static void
3738 record_unknown_type (tree type, const char* name)
3740 tree decl = pushdecl (build_decl (UNKNOWN_LOCATION,
3741 TYPE_DECL, get_identifier (name), type));
3742 /* Make sure the "unknown type" typedecl gets ignored for debug info. */
3743 DECL_IGNORED_P (decl) = 1;
3744 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
3745 TYPE_SIZE (type) = TYPE_SIZE (void_type_node);
3746 TYPE_ALIGN (type) = 1;
3747 TYPE_USER_ALIGN (type) = 0;
3748 SET_TYPE_MODE (type, TYPE_MODE (void_type_node));
3751 /* A string for which we should create an IDENTIFIER_NODE at
3752 startup. */
3754 typedef struct predefined_identifier
3756 /* The name of the identifier. */
3757 const char *const name;
3758 /* The place where the IDENTIFIER_NODE should be stored. */
3759 tree *const node;
3760 /* Nonzero if this is the name of a constructor or destructor. */
3761 const int ctor_or_dtor_p;
3762 } predefined_identifier;
3764 /* Create all the predefined identifiers. */
3766 static void
3767 initialize_predefined_identifiers (void)
3769 const predefined_identifier *pid;
3771 /* A table of identifiers to create at startup. */
3772 static const predefined_identifier predefined_identifiers[] = {
3773 { "C++", &lang_name_cplusplus, 0 },
3774 { "C", &lang_name_c, 0 },
3775 { "Java", &lang_name_java, 0 },
3776 /* Some of these names have a trailing space so that it is
3777 impossible for them to conflict with names written by users. */
3778 { "__ct ", &ctor_identifier, 1 },
3779 { "__base_ctor ", &base_ctor_identifier, 1 },
3780 { "__comp_ctor ", &complete_ctor_identifier, 1 },
3781 { "__dt ", &dtor_identifier, 1 },
3782 { "__comp_dtor ", &complete_dtor_identifier, 1 },
3783 { "__base_dtor ", &base_dtor_identifier, 1 },
3784 { "__deleting_dtor ", &deleting_dtor_identifier, 1 },
3785 { IN_CHARGE_NAME, &in_charge_identifier, 0 },
3786 { "nelts", &nelts_identifier, 0 },
3787 { THIS_NAME, &this_identifier, 0 },
3788 { VTABLE_DELTA_NAME, &delta_identifier, 0 },
3789 { VTABLE_PFN_NAME, &pfn_identifier, 0 },
3790 { "_vptr", &vptr_identifier, 0 },
3791 { "__vtt_parm", &vtt_parm_identifier, 0 },
3792 { "::", &global_scope_name, 0 },
3793 { "std", &std_identifier, 0 },
3794 { NULL, NULL, 0 }
3797 for (pid = predefined_identifiers; pid->name; ++pid)
3799 *pid->node = get_identifier (pid->name);
3800 if (pid->ctor_or_dtor_p)
3801 IDENTIFIER_CTOR_OR_DTOR_P (*pid->node) = 1;
3805 /* Create the predefined scalar types of C,
3806 and some nodes representing standard constants (0, 1, (void *)0).
3807 Initialize the global binding level.
3808 Make definitions for built-in primitive functions. */
3810 void
3811 cxx_init_decl_processing (void)
3813 tree void_ftype;
3814 tree void_ftype_ptr;
3816 /* Create all the identifiers we need. */
3817 initialize_predefined_identifiers ();
3819 /* Create the global variables. */
3820 push_to_top_level ();
3822 current_function_decl = NULL_TREE;
3823 current_binding_level = NULL;
3824 /* Enter the global namespace. */
3825 gcc_assert (global_namespace == NULL_TREE);
3826 global_namespace = build_lang_decl (NAMESPACE_DECL, global_scope_name,
3827 void_type_node);
3828 DECL_CONTEXT (global_namespace) = build_translation_unit_decl (NULL_TREE);
3829 TREE_PUBLIC (global_namespace) = 1;
3830 begin_scope (sk_namespace, global_namespace);
3832 if (flag_visibility_ms_compat)
3833 default_visibility = VISIBILITY_HIDDEN;
3835 /* Initially, C. */
3836 current_lang_name = lang_name_c;
3838 /* Create the `std' namespace. */
3839 push_namespace (std_identifier);
3840 std_node = current_namespace;
3841 pop_namespace ();
3843 c_common_nodes_and_builtins ();
3845 java_byte_type_node = record_builtin_java_type ("__java_byte", 8);
3846 java_short_type_node = record_builtin_java_type ("__java_short", 16);
3847 java_int_type_node = record_builtin_java_type ("__java_int", 32);
3848 java_long_type_node = record_builtin_java_type ("__java_long", 64);
3849 java_float_type_node = record_builtin_java_type ("__java_float", -32);
3850 java_double_type_node = record_builtin_java_type ("__java_double", -64);
3851 java_char_type_node = record_builtin_java_type ("__java_char", -16);
3852 java_boolean_type_node = record_builtin_java_type ("__java_boolean", -1);
3854 integer_two_node = build_int_cst (NULL_TREE, 2);
3856 record_builtin_type (RID_BOOL, "bool", boolean_type_node);
3857 truthvalue_type_node = boolean_type_node;
3858 truthvalue_false_node = boolean_false_node;
3859 truthvalue_true_node = boolean_true_node;
3861 empty_except_spec = build_tree_list (NULL_TREE, NULL_TREE);
3862 noexcept_true_spec = build_tree_list (boolean_true_node, NULL_TREE);
3863 noexcept_false_spec = build_tree_list (boolean_false_node, NULL_TREE);
3865 #if 0
3866 record_builtin_type (RID_MAX, NULL, string_type_node);
3867 #endif
3869 delta_type_node = ptrdiff_type_node;
3870 vtable_index_type = ptrdiff_type_node;
3872 vtt_parm_type = build_pointer_type (const_ptr_type_node);
3873 void_ftype = build_function_type_list (void_type_node, NULL_TREE);
3874 void_ftype_ptr = build_function_type_list (void_type_node,
3875 ptr_type_node, NULL_TREE);
3876 void_ftype_ptr
3877 = build_exception_variant (void_ftype_ptr, empty_except_spec);
3879 /* C++ extensions */
3881 unknown_type_node = make_node (LANG_TYPE);
3882 record_unknown_type (unknown_type_node, "unknown type");
3884 /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node. */
3885 TREE_TYPE (unknown_type_node) = unknown_type_node;
3887 /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same
3888 result. */
3889 TYPE_POINTER_TO (unknown_type_node) = unknown_type_node;
3890 TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node;
3892 init_list_type_node = make_node (LANG_TYPE);
3893 record_unknown_type (init_list_type_node, "init list");
3896 /* Make sure we get a unique function type, so we can give
3897 its pointer type a name. (This wins for gdb.) */
3898 tree vfunc_type = make_node (FUNCTION_TYPE);
3899 TREE_TYPE (vfunc_type) = integer_type_node;
3900 TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
3901 layout_type (vfunc_type);
3903 vtable_entry_type = build_pointer_type (vfunc_type);
3905 record_builtin_type (RID_MAX, VTBL_PTR_TYPE, vtable_entry_type);
3907 vtbl_type_node
3908 = build_cplus_array_type (vtable_entry_type, NULL_TREE);
3909 layout_type (vtbl_type_node);
3910 vtbl_type_node = cp_build_qualified_type (vtbl_type_node, TYPE_QUAL_CONST);
3911 record_builtin_type (RID_MAX, NULL, vtbl_type_node);
3912 vtbl_ptr_type_node = build_pointer_type (vtable_entry_type);
3913 layout_type (vtbl_ptr_type_node);
3914 record_builtin_type (RID_MAX, NULL, vtbl_ptr_type_node);
3916 push_namespace (get_identifier ("__cxxabiv1"));
3917 abi_node = current_namespace;
3918 pop_namespace ();
3920 global_type_node = make_node (LANG_TYPE);
3921 record_unknown_type (global_type_node, "global type");
3923 /* Now, C++. */
3924 current_lang_name = lang_name_cplusplus;
3927 tree newattrs, extvisattr;
3928 tree newtype, deltype;
3929 tree ptr_ftype_sizetype;
3930 tree new_eh_spec;
3932 ptr_ftype_sizetype
3933 = build_function_type_list (ptr_type_node, size_type_node, NULL_TREE);
3934 if (cxx_dialect == cxx98)
3936 tree bad_alloc_id;
3937 tree bad_alloc_type_node;
3938 tree bad_alloc_decl;
3940 push_namespace (std_identifier);
3941 bad_alloc_id = get_identifier ("bad_alloc");
3942 bad_alloc_type_node = make_class_type (RECORD_TYPE);
3943 TYPE_CONTEXT (bad_alloc_type_node) = current_namespace;
3944 bad_alloc_decl
3945 = create_implicit_typedef (bad_alloc_id, bad_alloc_type_node);
3946 DECL_CONTEXT (bad_alloc_decl) = current_namespace;
3947 pop_namespace ();
3949 new_eh_spec
3950 = add_exception_specifier (NULL_TREE, bad_alloc_type_node, -1);
3952 else
3953 new_eh_spec = noexcept_false_spec;
3955 /* Ensure attribs.c is initialized. */
3956 init_attributes ();
3957 extvisattr = build_tree_list (get_identifier ("externally_visible"),
3958 NULL_TREE);
3959 newattrs = tree_cons (get_identifier ("alloc_size"),
3960 build_tree_list (NULL_TREE, integer_one_node),
3961 extvisattr);
3962 newtype = cp_build_type_attribute_variant (ptr_ftype_sizetype, newattrs);
3963 newtype = build_exception_variant (newtype, new_eh_spec);
3964 deltype = cp_build_type_attribute_variant (void_ftype_ptr, extvisattr);
3965 deltype = build_exception_variant (deltype, empty_except_spec);
3966 tree opnew = push_cp_library_fn (NEW_EXPR, newtype, 0);
3967 DECL_IS_MALLOC (opnew) = 1;
3968 DECL_IS_OPERATOR_NEW (opnew) = 1;
3969 opnew = push_cp_library_fn (VEC_NEW_EXPR, newtype, 0);
3970 DECL_IS_MALLOC (opnew) = 1;
3971 DECL_IS_OPERATOR_NEW (opnew) = 1;
3972 push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
3973 push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
3974 if (flag_sized_deallocation)
3976 /* Also push the sized deallocation variants:
3977 void operator delete(void*, std::size_t) throw();
3978 void operator delete[](void*, std::size_t) throw(); */
3979 tree void_ftype_ptr_size
3980 = build_function_type_list (void_type_node, ptr_type_node,
3981 size_type_node, NULL_TREE);
3982 deltype = cp_build_type_attribute_variant (void_ftype_ptr_size,
3983 extvisattr);
3984 deltype = build_exception_variant (deltype, empty_except_spec);
3985 push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
3986 push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
3989 nullptr_type_node = make_node (NULLPTR_TYPE);
3990 TYPE_SIZE (nullptr_type_node) = bitsize_int (GET_MODE_BITSIZE (ptr_mode));
3991 TYPE_SIZE_UNIT (nullptr_type_node) = size_int (GET_MODE_SIZE (ptr_mode));
3992 TYPE_UNSIGNED (nullptr_type_node) = 1;
3993 TYPE_PRECISION (nullptr_type_node) = GET_MODE_BITSIZE (ptr_mode);
3994 SET_TYPE_MODE (nullptr_type_node, ptr_mode);
3995 record_builtin_type (RID_MAX, "decltype(nullptr)", nullptr_type_node);
3996 nullptr_node = build_int_cst (nullptr_type_node, 0);
3999 abort_fndecl
4000 = build_library_fn_ptr ("__cxa_pure_virtual", void_ftype,
4001 ECF_NORETURN | ECF_NOTHROW);
4003 /* Perform other language dependent initializations. */
4004 init_class_processing ();
4005 init_rtti_processing ();
4006 init_template_processing ();
4008 if (flag_exceptions)
4009 init_exception_processing ();
4011 if (! supports_one_only ())
4012 flag_weak = 0;
4014 make_fname_decl = cp_make_fname_decl;
4015 start_fname_decls ();
4017 /* Show we use EH for cleanups. */
4018 if (flag_exceptions)
4019 using_eh_for_cleanups ();
4022 /* Generate an initializer for a function naming variable from
4023 NAME. NAME may be NULL, to indicate a dependent name. TYPE_P is
4024 filled in with the type of the init. */
4026 tree
4027 cp_fname_init (const char* name, tree *type_p)
4029 tree domain = NULL_TREE;
4030 tree type;
4031 tree init = NULL_TREE;
4032 size_t length = 0;
4034 if (name)
4036 length = strlen (name);
4037 domain = build_index_type (size_int (length));
4038 init = build_string (length + 1, name);
4041 type = cp_build_qualified_type (char_type_node, TYPE_QUAL_CONST);
4042 type = build_cplus_array_type (type, domain);
4044 *type_p = type;
4046 if (init)
4047 TREE_TYPE (init) = type;
4048 else
4049 init = error_mark_node;
4051 return init;
4054 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give
4055 the decl, LOC is the location to give the decl, NAME is the
4056 initialization string and TYPE_DEP indicates whether NAME depended
4057 on the type of the function. We make use of that to detect
4058 __PRETTY_FUNCTION__ inside a template fn. This is being done lazily
4059 at the point of first use, so we mustn't push the decl now. */
4061 static tree
4062 cp_make_fname_decl (location_t loc, tree id, int type_dep)
4064 const char *const name = (type_dep && processing_template_decl
4065 ? NULL : fname_as_string (type_dep));
4066 tree type;
4067 tree init = cp_fname_init (name, &type);
4068 tree decl = build_decl (loc, VAR_DECL, id, type);
4070 if (name)
4071 free (CONST_CAST (char *, name));
4073 /* As we're using pushdecl_with_scope, we must set the context. */
4074 DECL_CONTEXT (decl) = current_function_decl;
4076 TREE_STATIC (decl) = 1;
4077 TREE_READONLY (decl) = 1;
4078 DECL_ARTIFICIAL (decl) = 1;
4080 TREE_USED (decl) = 1;
4082 if (current_function_decl)
4084 cp_binding_level *b = current_binding_level;
4085 if (b->kind == sk_function_parms)
4086 return error_mark_node;
4087 while (b->level_chain->kind != sk_function_parms)
4088 b = b->level_chain;
4089 pushdecl_with_scope (decl, b, /*is_friend=*/false);
4090 cp_finish_decl (decl, init, /*init_const_expr_p=*/false, NULL_TREE,
4091 LOOKUP_ONLYCONVERTING);
4093 else
4095 DECL_THIS_STATIC (decl) = true;
4096 pushdecl_top_level_and_finish (decl, init);
4099 return decl;
4102 static tree
4103 builtin_function_1 (tree decl, tree context, bool is_global)
4105 tree id = DECL_NAME (decl);
4106 const char *name = IDENTIFIER_POINTER (id);
4108 retrofit_lang_decl (decl);
4110 DECL_ARTIFICIAL (decl) = 1;
4111 SET_OVERLOADED_OPERATOR_CODE (decl, ERROR_MARK);
4112 SET_DECL_LANGUAGE (decl, lang_c);
4113 /* Runtime library routines are, by definition, available in an
4114 external shared object. */
4115 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
4116 DECL_VISIBILITY_SPECIFIED (decl) = 1;
4118 DECL_CONTEXT (decl) = context;
4120 if (is_global)
4121 pushdecl_top_level (decl);
4122 else
4123 pushdecl (decl);
4125 /* A function in the user's namespace should have an explicit
4126 declaration before it is used. Mark the built-in function as
4127 anticipated but not actually declared. */
4128 if (name[0] != '_' || name[1] != '_')
4129 DECL_ANTICIPATED (decl) = 1;
4130 else if (strncmp (name + 2, "builtin_", strlen ("builtin_")) != 0)
4132 size_t len = strlen (name);
4134 /* Treat __*_chk fortification functions as anticipated as well,
4135 unless they are __builtin_*. */
4136 if (len > strlen ("___chk")
4137 && memcmp (name + len - strlen ("_chk"),
4138 "_chk", strlen ("_chk") + 1) == 0)
4139 DECL_ANTICIPATED (decl) = 1;
4142 return decl;
4145 tree
4146 cxx_builtin_function (tree decl)
4148 tree id = DECL_NAME (decl);
4149 const char *name = IDENTIFIER_POINTER (id);
4150 /* All builtins that don't begin with an '_' should additionally
4151 go in the 'std' namespace. */
4152 if (name[0] != '_')
4154 tree decl2 = copy_node(decl);
4155 push_namespace (std_identifier);
4156 builtin_function_1 (decl2, std_node, false);
4157 pop_namespace ();
4160 return builtin_function_1 (decl, NULL_TREE, false);
4163 /* Like cxx_builtin_function, but guarantee the function is added to the global
4164 scope. This is to allow function specific options to add new machine
4165 dependent builtins when the target ISA changes via attribute((target(...)))
4166 which saves space on program startup if the program does not use non-generic
4167 ISAs. */
4169 tree
4170 cxx_builtin_function_ext_scope (tree decl)
4173 tree id = DECL_NAME (decl);
4174 const char *name = IDENTIFIER_POINTER (id);
4175 /* All builtins that don't begin with an '_' should additionally
4176 go in the 'std' namespace. */
4177 if (name[0] != '_')
4179 tree decl2 = copy_node(decl);
4180 push_namespace (std_identifier);
4181 builtin_function_1 (decl2, std_node, true);
4182 pop_namespace ();
4185 return builtin_function_1 (decl, NULL_TREE, true);
4188 /* Generate a FUNCTION_DECL with the typical flags for a runtime library
4189 function. Not called directly. */
4191 static tree
4192 build_library_fn (tree name, enum tree_code operator_code, tree type,
4193 int ecf_flags)
4195 tree fn = build_lang_decl (FUNCTION_DECL, name, type);
4196 DECL_EXTERNAL (fn) = 1;
4197 TREE_PUBLIC (fn) = 1;
4198 DECL_ARTIFICIAL (fn) = 1;
4199 SET_OVERLOADED_OPERATOR_CODE (fn, operator_code);
4200 SET_DECL_LANGUAGE (fn, lang_c);
4201 /* Runtime library routines are, by definition, available in an
4202 external shared object. */
4203 DECL_VISIBILITY (fn) = VISIBILITY_DEFAULT;
4204 DECL_VISIBILITY_SPECIFIED (fn) = 1;
4205 set_call_expr_flags (fn, ecf_flags);
4206 return fn;
4209 /* Returns the _DECL for a library function with C++ linkage. */
4211 static tree
4212 build_cp_library_fn (tree name, enum tree_code operator_code, tree type,
4213 int ecf_flags)
4215 tree fn = build_library_fn (name, operator_code, type, ecf_flags);
4216 DECL_CONTEXT (fn) = FROB_CONTEXT (current_namespace);
4217 SET_DECL_LANGUAGE (fn, lang_cplusplus);
4218 return fn;
4221 /* Like build_library_fn, but takes a C string instead of an
4222 IDENTIFIER_NODE. */
4224 tree
4225 build_library_fn_ptr (const char* name, tree type, int ecf_flags)
4227 return build_library_fn (get_identifier (name), ERROR_MARK, type, ecf_flags);
4230 /* Like build_cp_library_fn, but takes a C string instead of an
4231 IDENTIFIER_NODE. */
4233 tree
4234 build_cp_library_fn_ptr (const char* name, tree type, int ecf_flags)
4236 return build_cp_library_fn (get_identifier (name), ERROR_MARK, type,
4237 ecf_flags);
4240 /* Like build_library_fn, but also pushes the function so that we will
4241 be able to find it via IDENTIFIER_GLOBAL_VALUE. Also, the function
4242 may throw exceptions listed in RAISES. */
4244 tree
4245 push_library_fn (tree name, tree type, tree raises, int ecf_flags)
4247 tree fn;
4249 if (raises)
4250 type = build_exception_variant (type, raises);
4252 fn = build_library_fn (name, ERROR_MARK, type, ecf_flags);
4253 pushdecl_top_level (fn);
4254 return fn;
4257 /* Like build_cp_library_fn, but also pushes the function so that it
4258 will be found by normal lookup. */
4260 static tree
4261 push_cp_library_fn (enum tree_code operator_code, tree type,
4262 int ecf_flags)
4264 tree fn = build_cp_library_fn (ansi_opname (operator_code),
4265 operator_code,
4266 type, ecf_flags);
4267 pushdecl (fn);
4268 if (flag_tm)
4269 apply_tm_attr (fn, get_identifier ("transaction_safe"));
4270 return fn;
4273 /* Like push_library_fn, but takes a TREE_LIST of parm types rather than
4274 a FUNCTION_TYPE. */
4276 tree
4277 push_void_library_fn (tree name, tree parmtypes, int ecf_flags)
4279 tree type = build_function_type (void_type_node, parmtypes);
4280 return push_library_fn (name, type, NULL_TREE, ecf_flags);
4283 /* Like push_library_fn, but also note that this function throws
4284 and does not return. Used for __throw_foo and the like. */
4286 tree
4287 push_throw_library_fn (tree name, tree type)
4289 tree fn = push_library_fn (name, type, NULL_TREE, ECF_NORETURN);
4290 return fn;
4293 /* When we call finish_struct for an anonymous union, we create
4294 default copy constructors and such. But, an anonymous union
4295 shouldn't have such things; this function undoes the damage to the
4296 anonymous union type T.
4298 (The reason that we create the synthesized methods is that we don't
4299 distinguish `union { int i; }' from `typedef union { int i; } U'.
4300 The first is an anonymous union; the second is just an ordinary
4301 union type.) */
4303 void
4304 fixup_anonymous_aggr (tree t)
4306 tree *q;
4308 /* Wipe out memory of synthesized methods. */
4309 TYPE_HAS_USER_CONSTRUCTOR (t) = 0;
4310 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0;
4311 TYPE_HAS_COPY_CTOR (t) = 0;
4312 TYPE_HAS_CONST_COPY_CTOR (t) = 0;
4313 TYPE_HAS_COPY_ASSIGN (t) = 0;
4314 TYPE_HAS_CONST_COPY_ASSIGN (t) = 0;
4316 /* Splice the implicitly generated functions out of the TYPE_METHODS
4317 list. */
4318 q = &TYPE_METHODS (t);
4319 while (*q)
4321 if (DECL_ARTIFICIAL (*q))
4322 *q = TREE_CHAIN (*q);
4323 else
4324 q = &DECL_CHAIN (*q);
4327 /* ISO C++ 9.5.3. Anonymous unions may not have function members. */
4328 if (TYPE_METHODS (t))
4330 tree decl = TYPE_MAIN_DECL (t);
4332 if (TREE_CODE (t) != UNION_TYPE)
4333 error_at (DECL_SOURCE_LOCATION (decl),
4334 "an anonymous struct cannot have function members");
4335 else
4336 error_at (DECL_SOURCE_LOCATION (decl),
4337 "an anonymous union cannot have function members");
4340 /* Anonymous aggregates cannot have fields with ctors, dtors or complex
4341 assignment operators (because they cannot have these methods themselves).
4342 For anonymous unions this is already checked because they are not allowed
4343 in any union, otherwise we have to check it. */
4344 if (TREE_CODE (t) != UNION_TYPE)
4346 tree field, type;
4348 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
4349 if (TREE_CODE (field) == FIELD_DECL)
4351 type = TREE_TYPE (field);
4352 if (CLASS_TYPE_P (type))
4354 if (TYPE_NEEDS_CONSTRUCTING (type))
4355 error ("member %q+#D with constructor not allowed "
4356 "in anonymous aggregate", field);
4357 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
4358 error ("member %q+#D with destructor not allowed "
4359 "in anonymous aggregate", field);
4360 if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type))
4361 error ("member %q+#D with copy assignment operator "
4362 "not allowed in anonymous aggregate", field);
4368 /* Warn for an attribute located at LOCATION that appertains to the
4369 class type CLASS_TYPE that has not been properly placed after its
4370 class-key, in it class-specifier. */
4372 void
4373 warn_misplaced_attr_for_class_type (source_location location,
4374 tree class_type)
4376 gcc_assert (OVERLOAD_TYPE_P (class_type));
4378 if (warning_at (location, OPT_Wattributes,
4379 "attribute ignored in declaration "
4380 "of %q#T", class_type))
4381 inform (location,
4382 "attribute for %q#T must follow the %qs keyword",
4383 class_type, class_key_or_enum_as_string (class_type));
4386 /* Make sure that a declaration with no declarator is well-formed, i.e.
4387 just declares a tagged type or anonymous union.
4389 Returns the type declared; or NULL_TREE if none. */
4391 tree
4392 check_tag_decl (cp_decl_specifier_seq *declspecs,
4393 bool explicit_type_instantiation_p)
4395 int saw_friend = decl_spec_seq_has_spec_p (declspecs, ds_friend);
4396 int saw_typedef = decl_spec_seq_has_spec_p (declspecs, ds_typedef);
4397 /* If a class, struct, or enum type is declared by the DECLSPECS
4398 (i.e, if a class-specifier, enum-specifier, or non-typename
4399 elaborated-type-specifier appears in the DECLSPECS),
4400 DECLARED_TYPE is set to the corresponding type. */
4401 tree declared_type = NULL_TREE;
4402 bool error_p = false;
4404 if (declspecs->multiple_types_p)
4405 error ("multiple types in one declaration");
4406 else if (declspecs->redefined_builtin_type)
4408 if (!in_system_header_at (input_location))
4409 permerror (declspecs->locations[ds_redefined_builtin_type_spec],
4410 "redeclaration of C++ built-in type %qT",
4411 declspecs->redefined_builtin_type);
4412 return NULL_TREE;
4415 if (declspecs->type
4416 && TYPE_P (declspecs->type)
4417 && ((TREE_CODE (declspecs->type) != TYPENAME_TYPE
4418 && MAYBE_CLASS_TYPE_P (declspecs->type))
4419 || TREE_CODE (declspecs->type) == ENUMERAL_TYPE))
4420 declared_type = declspecs->type;
4421 else if (declspecs->type == error_mark_node)
4422 error_p = true;
4423 if (declared_type == NULL_TREE && ! saw_friend && !error_p)
4424 permerror (input_location, "declaration does not declare anything");
4425 else if (declared_type != NULL_TREE && type_uses_auto (declared_type))
4427 error ("%<auto%> can only be specified for variables "
4428 "or function declarations");
4429 return error_mark_node;
4431 /* Check for an anonymous union. */
4432 else if (declared_type && RECORD_OR_UNION_CODE_P (TREE_CODE (declared_type))
4433 && TYPE_ANONYMOUS_P (declared_type))
4435 /* 7/3 In a simple-declaration, the optional init-declarator-list
4436 can be omitted only when declaring a class (clause 9) or
4437 enumeration (7.2), that is, when the decl-specifier-seq contains
4438 either a class-specifier, an elaborated-type-specifier with
4439 a class-key (9.1), or an enum-specifier. In these cases and
4440 whenever a class-specifier or enum-specifier is present in the
4441 decl-specifier-seq, the identifiers in these specifiers are among
4442 the names being declared by the declaration (as class-name,
4443 enum-names, or enumerators, depending on the syntax). In such
4444 cases, and except for the declaration of an unnamed bit-field (9.6),
4445 the decl-specifier-seq shall introduce one or more names into the
4446 program, or shall redeclare a name introduced by a previous
4447 declaration. [Example:
4448 enum { }; // ill-formed
4449 typedef class { }; // ill-formed
4450 --end example] */
4451 if (saw_typedef)
4453 error ("missing type-name in typedef-declaration");
4454 return NULL_TREE;
4456 /* Anonymous unions are objects, so they can have specifiers. */;
4457 SET_ANON_AGGR_TYPE_P (declared_type);
4459 if (TREE_CODE (declared_type) != UNION_TYPE
4460 && !in_system_header_at (input_location))
4461 pedwarn (input_location, OPT_Wpedantic, "ISO C++ prohibits anonymous structs");
4464 else
4466 if (decl_spec_seq_has_spec_p (declspecs, ds_inline)
4467 || decl_spec_seq_has_spec_p (declspecs, ds_virtual))
4468 error ("%qs can only be specified for functions",
4469 decl_spec_seq_has_spec_p (declspecs, ds_inline)
4470 ? "inline" : "virtual");
4471 else if (saw_friend
4472 && (!current_class_type
4473 || current_scope () != current_class_type))
4474 error ("%<friend%> can only be specified inside a class");
4475 else if (decl_spec_seq_has_spec_p (declspecs, ds_explicit))
4476 error ("%<explicit%> can only be specified for constructors");
4477 else if (declspecs->storage_class)
4478 error ("a storage class can only be specified for objects "
4479 "and functions");
4480 else if (decl_spec_seq_has_spec_p (declspecs, ds_const)
4481 || decl_spec_seq_has_spec_p (declspecs, ds_volatile)
4482 || decl_spec_seq_has_spec_p (declspecs, ds_restrict)
4483 || decl_spec_seq_has_spec_p (declspecs, ds_thread))
4484 error ("qualifiers can only be specified for objects "
4485 "and functions");
4486 else if (saw_typedef)
4487 warning (0, "%<typedef%> was ignored in this declaration");
4488 else if (decl_spec_seq_has_spec_p (declspecs, ds_constexpr))
4489 error ("%<constexpr%> cannot be used for type declarations");
4492 if (declspecs->attributes && warn_attributes && declared_type)
4494 location_t loc;
4495 if (!CLASS_TYPE_P (declared_type)
4496 || !CLASSTYPE_TEMPLATE_INSTANTIATION (declared_type))
4497 /* For a non-template class, use the name location. */
4498 loc = location_of (declared_type);
4499 else
4500 /* For a template class (an explicit instantiation), use the
4501 current location. */
4502 loc = input_location;
4504 if (explicit_type_instantiation_p)
4505 /* [dcl.attr.grammar]/4:
4507 No attribute-specifier-seq shall appertain to an explicit
4508 instantiation. */
4510 if (warning_at (loc, OPT_Wattributes,
4511 "attribute ignored in explicit instantiation %q#T",
4512 declared_type))
4513 inform (loc,
4514 "no attribute can be applied to "
4515 "an explicit instantiation");
4517 else
4518 warn_misplaced_attr_for_class_type (loc, declared_type);
4521 return declared_type;
4524 /* Called when a declaration is seen that contains no names to declare.
4525 If its type is a reference to a structure, union or enum inherited
4526 from a containing scope, shadow that tag name for the current scope
4527 with a forward reference.
4528 If its type defines a new named structure or union
4529 or defines an enum, it is valid but we need not do anything here.
4530 Otherwise, it is an error.
4532 C++: may have to grok the declspecs to learn about static,
4533 complain for anonymous unions.
4535 Returns the TYPE declared -- or NULL_TREE if none. */
4537 tree
4538 shadow_tag (cp_decl_specifier_seq *declspecs)
4540 tree t = check_tag_decl (declspecs,
4541 /*explicit_type_instantiation_p=*/false);
4543 if (!t)
4544 return NULL_TREE;
4546 if (maybe_process_partial_specialization (t) == error_mark_node)
4547 return NULL_TREE;
4549 /* This is where the variables in an anonymous union are
4550 declared. An anonymous union declaration looks like:
4551 union { ... } ;
4552 because there is no declarator after the union, the parser
4553 sends that declaration here. */
4554 if (ANON_AGGR_TYPE_P (t))
4556 fixup_anonymous_aggr (t);
4558 if (TYPE_FIELDS (t))
4560 tree decl = grokdeclarator (/*declarator=*/NULL,
4561 declspecs, NORMAL, 0, NULL);
4562 finish_anon_union (decl);
4566 return t;
4569 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
4571 tree
4572 groktypename (cp_decl_specifier_seq *type_specifiers,
4573 const cp_declarator *declarator,
4574 bool is_template_arg)
4576 tree attrs;
4577 tree type;
4578 enum decl_context context
4579 = is_template_arg ? TEMPLATE_TYPE_ARG : TYPENAME;
4580 attrs = type_specifiers->attributes;
4581 type_specifiers->attributes = NULL_TREE;
4582 type = grokdeclarator (declarator, type_specifiers, context, 0, &attrs);
4583 if (attrs && type != error_mark_node)
4585 if (CLASS_TYPE_P (type))
4586 warning (OPT_Wattributes, "ignoring attributes applied to class type %qT "
4587 "outside of definition", type);
4588 else if (MAYBE_CLASS_TYPE_P (type))
4589 /* A template type parameter or other dependent type. */
4590 warning (OPT_Wattributes, "ignoring attributes applied to dependent "
4591 "type %qT without an associated declaration", type);
4592 else
4593 cplus_decl_attributes (&type, attrs, 0);
4595 return type;
4598 /* Process a DECLARATOR for a function-scope variable declaration,
4599 namespace-scope variable declaration, or function declaration.
4600 (Function definitions go through start_function; class member
4601 declarations appearing in the body of the class go through
4602 grokfield.) The DECL corresponding to the DECLARATOR is returned.
4603 If an error occurs, the error_mark_node is returned instead.
4605 DECLSPECS are the decl-specifiers for the declaration. INITIALIZED is
4606 SD_INITIALIZED if an explicit initializer is present, or SD_DEFAULTED
4607 for an explicitly defaulted function, or SD_DELETED for an explicitly
4608 deleted function, but 0 (SD_UNINITIALIZED) if this is a variable
4609 implicitly initialized via a default constructor. ATTRIBUTES and
4610 PREFIX_ATTRIBUTES are GNU attributes associated with this declaration.
4612 The scope represented by the context of the returned DECL is pushed
4613 (if it is not the global namespace) and is assigned to
4614 *PUSHED_SCOPE_P. The caller is then responsible for calling
4615 pop_scope on *PUSHED_SCOPE_P if it is set. */
4617 tree
4618 start_decl (const cp_declarator *declarator,
4619 cp_decl_specifier_seq *declspecs,
4620 int initialized,
4621 tree attributes,
4622 tree prefix_attributes,
4623 tree *pushed_scope_p)
4625 tree decl;
4626 tree context;
4627 bool was_public;
4628 int flags;
4629 bool alias;
4631 *pushed_scope_p = NULL_TREE;
4633 /* An object declared as __attribute__((deprecated)) suppresses
4634 warnings of uses of other deprecated items. */
4635 if (lookup_attribute ("deprecated", attributes))
4636 deprecated_state = DEPRECATED_SUPPRESS;
4638 attributes = chainon (attributes, prefix_attributes);
4640 decl = grokdeclarator (declarator, declspecs, NORMAL, initialized,
4641 &attributes);
4643 deprecated_state = DEPRECATED_NORMAL;
4645 if (decl == NULL_TREE || VOID_TYPE_P (decl)
4646 || decl == error_mark_node)
4647 return error_mark_node;
4649 context = CP_DECL_CONTEXT (decl);
4650 if (context != global_namespace)
4651 *pushed_scope_p = push_scope (context);
4653 /* Is it valid for this decl to have an initializer at all?
4654 If not, set INITIALIZED to zero, which will indirectly
4655 tell `cp_finish_decl' to ignore the initializer once it is parsed. */
4656 if (initialized
4657 && TREE_CODE (decl) == TYPE_DECL)
4659 error ("typedef %qD is initialized (use decltype instead)", decl);
4660 return error_mark_node;
4663 if (initialized)
4665 if (! toplevel_bindings_p ()
4666 && DECL_EXTERNAL (decl))
4667 warning (0, "declaration of %q#D has %<extern%> and is initialized",
4668 decl);
4669 DECL_EXTERNAL (decl) = 0;
4670 if (toplevel_bindings_p ())
4671 TREE_STATIC (decl) = 1;
4673 alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl)) != 0;
4675 if (alias && TREE_CODE (decl) == FUNCTION_DECL)
4676 record_key_method_defined (decl);
4678 /* If this is a typedef that names the class for linkage purposes
4679 (7.1.3p8), apply any attributes directly to the type. */
4680 if (TREE_CODE (decl) == TYPE_DECL
4681 && OVERLOAD_TYPE_P (TREE_TYPE (decl))
4682 && decl == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (decl))))
4683 flags = ATTR_FLAG_TYPE_IN_PLACE;
4684 else
4685 flags = 0;
4687 /* Set attributes here so if duplicate decl, will have proper attributes. */
4688 cplus_decl_attributes (&decl, attributes, flags);
4690 /* Dllimported symbols cannot be defined. Static data members (which
4691 can be initialized in-class and dllimported) go through grokfield,
4692 not here, so we don't need to exclude those decls when checking for
4693 a definition. */
4694 if (initialized && DECL_DLLIMPORT_P (decl))
4696 error ("definition of %q#D is marked %<dllimport%>", decl);
4697 DECL_DLLIMPORT_P (decl) = 0;
4700 /* If #pragma weak was used, mark the decl weak now. */
4701 if (!processing_template_decl)
4702 maybe_apply_pragma_weak (decl);
4704 if (TREE_CODE (decl) == FUNCTION_DECL
4705 && DECL_DECLARED_INLINE_P (decl)
4706 && DECL_UNINLINABLE (decl)
4707 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
4708 warning (0, "inline function %q+D given attribute noinline", decl);
4710 if (TYPE_P (context) && COMPLETE_TYPE_P (complete_type (context)))
4712 bool this_tmpl = (processing_template_decl
4713 > template_class_depth (context));
4714 if (VAR_P (decl))
4716 tree field = lookup_field (context, DECL_NAME (decl), 0, false);
4717 if (field == NULL_TREE
4718 || !(VAR_P (field) || variable_template_p (field)))
4719 error ("%q+#D is not a static data member of %q#T", decl, context);
4720 else if (variable_template_p (field) && !this_tmpl)
4722 if (DECL_LANG_SPECIFIC (decl)
4723 && DECL_TEMPLATE_SPECIALIZATION (decl))
4724 /* OK, specialization was already checked. */;
4725 else
4727 error_at (DECL_SOURCE_LOCATION (decl),
4728 "non-member-template declaration of %qD", decl);
4729 inform (DECL_SOURCE_LOCATION (field), "does not match "
4730 "member template declaration here");
4731 return error_mark_node;
4734 else
4736 if (variable_template_p (field))
4737 field = DECL_TEMPLATE_RESULT (field);
4739 if (DECL_CONTEXT (field) != context)
4741 if (!same_type_p (DECL_CONTEXT (field), context))
4742 permerror (input_location, "ISO C++ does not permit %<%T::%D%> "
4743 "to be defined as %<%T::%D%>",
4744 DECL_CONTEXT (field), DECL_NAME (decl),
4745 context, DECL_NAME (decl));
4746 DECL_CONTEXT (decl) = DECL_CONTEXT (field);
4748 /* Static data member are tricky; an in-class initialization
4749 still doesn't provide a definition, so the in-class
4750 declaration will have DECL_EXTERNAL set, but will have an
4751 initialization. Thus, duplicate_decls won't warn
4752 about this situation, and so we check here. */
4753 if (initialized && DECL_INITIALIZED_IN_CLASS_P (field))
4754 error ("duplicate initialization of %qD", decl);
4755 if (duplicate_decls (decl, field, /*newdecl_is_friend=*/false))
4756 decl = field;
4757 if (decl_spec_seq_has_spec_p (declspecs, ds_constexpr)
4758 && !DECL_DECLARED_CONSTEXPR_P (field))
4759 error ("%qD declared %<constexpr%> outside its class", field);
4762 else
4764 tree field = check_classfn (context, decl,
4765 this_tmpl
4766 ? current_template_parms
4767 : NULL_TREE);
4768 if (field && field != error_mark_node
4769 && duplicate_decls (decl, field,
4770 /*newdecl_is_friend=*/false))
4771 decl = field;
4774 /* cp_finish_decl sets DECL_EXTERNAL if DECL_IN_AGGR_P is set. */
4775 DECL_IN_AGGR_P (decl) = 0;
4776 /* Do not mark DECL as an explicit specialization if it was not
4777 already marked as an instantiation; a declaration should
4778 never be marked as a specialization unless we know what
4779 template is being specialized. */
4780 if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
4782 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
4783 if (TREE_CODE (decl) == FUNCTION_DECL)
4784 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
4785 && DECL_DECLARED_INLINE_P (decl));
4786 else
4787 DECL_COMDAT (decl) = false;
4789 /* [temp.expl.spec] An explicit specialization of a static data
4790 member of a template is a definition if the declaration
4791 includes an initializer; otherwise, it is a declaration.
4793 We check for processing_specialization so this only applies
4794 to the new specialization syntax. */
4795 if (!initialized && processing_specialization)
4796 DECL_EXTERNAL (decl) = 1;
4799 if (DECL_EXTERNAL (decl) && ! DECL_TEMPLATE_SPECIALIZATION (decl)
4800 /* Aliases are definitions. */
4801 && !alias)
4802 permerror (input_location, "declaration of %q#D outside of class is not definition",
4803 decl);
4806 was_public = TREE_PUBLIC (decl);
4808 /* Enter this declaration into the symbol table. */
4809 decl = maybe_push_decl (decl);
4811 if (processing_template_decl)
4812 decl = push_template_decl (decl);
4813 if (decl == error_mark_node)
4814 return error_mark_node;
4816 if (VAR_P (decl)
4817 && DECL_NAMESPACE_SCOPE_P (decl) && !TREE_PUBLIC (decl) && !was_public
4818 && !DECL_THIS_STATIC (decl) && !DECL_ARTIFICIAL (decl))
4820 /* This is a const variable with implicit 'static'. Set
4821 DECL_THIS_STATIC so we can tell it from variables that are
4822 !TREE_PUBLIC because of the anonymous namespace. */
4823 gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (decl)) || errorcount);
4824 DECL_THIS_STATIC (decl) = 1;
4827 if (current_function_decl && VAR_P (decl)
4828 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
4830 bool ok = false;
4831 if (DECL_THREAD_LOCAL_P (decl))
4832 error ("%qD declared %<thread_local%> in %<constexpr%> function",
4833 decl);
4834 else if (TREE_STATIC (decl))
4835 error ("%qD declared %<static%> in %<constexpr%> function", decl);
4836 else
4837 ok = true;
4838 if (!ok)
4839 cp_function_chain->invalid_constexpr = true;
4842 if (!processing_template_decl && VAR_P (decl))
4843 start_decl_1 (decl, initialized);
4845 return decl;
4848 /* Process the declaration of a variable DECL. INITIALIZED is true
4849 iff DECL is explicitly initialized. (INITIALIZED is false if the
4850 variable is initialized via an implicitly-called constructor.)
4851 This function must be called for ordinary variables (including, for
4852 example, implicit instantiations of templates), but must not be
4853 called for template declarations. */
4855 void
4856 start_decl_1 (tree decl, bool initialized)
4858 tree type;
4859 bool complete_p;
4860 bool aggregate_definition_p;
4862 gcc_assert (!processing_template_decl);
4864 if (error_operand_p (decl))
4865 return;
4867 gcc_assert (VAR_P (decl));
4869 type = TREE_TYPE (decl);
4870 complete_p = COMPLETE_TYPE_P (type);
4871 aggregate_definition_p = MAYBE_CLASS_TYPE_P (type) && !DECL_EXTERNAL (decl);
4873 /* If an explicit initializer is present, or if this is a definition
4874 of an aggregate, then we need a complete type at this point.
4875 (Scalars are always complete types, so there is nothing to
4876 check.) This code just sets COMPLETE_P; errors (if necessary)
4877 are issued below. */
4878 if ((initialized || aggregate_definition_p)
4879 && !complete_p
4880 && COMPLETE_TYPE_P (complete_type (type)))
4882 complete_p = true;
4883 /* We will not yet have set TREE_READONLY on DECL if the type
4884 was "const", but incomplete, before this point. But, now, we
4885 have a complete type, so we can try again. */
4886 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
4889 if (initialized)
4890 /* Is it valid for this decl to have an initializer at all? */
4892 /* Don't allow initializations for incomplete types except for
4893 arrays which might be completed by the initialization. */
4894 if (complete_p)
4895 ; /* A complete type is ok. */
4896 else if (type_uses_auto (type))
4897 ; /* An auto type is ok. */
4898 else if (TREE_CODE (type) != ARRAY_TYPE)
4900 error ("variable %q#D has initializer but incomplete type", decl);
4901 type = TREE_TYPE (decl) = error_mark_node;
4903 else if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
4905 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
4906 error ("elements of array %q#D have incomplete type", decl);
4907 /* else we already gave an error in start_decl. */
4910 else if (aggregate_definition_p && !complete_p)
4912 if (type_uses_auto (type))
4913 error ("declaration of %q#D has no initializer", decl);
4914 else
4915 error ("aggregate %q#D has incomplete type and cannot be defined",
4916 decl);
4917 /* Change the type so that assemble_variable will give
4918 DECL an rtl we can live with: (mem (const_int 0)). */
4919 type = TREE_TYPE (decl) = error_mark_node;
4922 /* Create a new scope to hold this declaration if necessary.
4923 Whether or not a new scope is necessary cannot be determined
4924 until after the type has been completed; if the type is a
4925 specialization of a class template it is not until after
4926 instantiation has occurred that TYPE_HAS_NONTRIVIAL_DESTRUCTOR
4927 will be set correctly. */
4928 maybe_push_cleanup_level (type);
4931 /* Handle initialization of references. DECL, TYPE, and INIT have the
4932 same meaning as in cp_finish_decl. *CLEANUP must be NULL on entry,
4933 but will be set to a new CLEANUP_STMT if a temporary is created
4934 that must be destroyed subsequently.
4936 Returns an initializer expression to use to initialize DECL, or
4937 NULL if the initialization can be performed statically.
4939 Quotes on semantics can be found in ARM 8.4.3. */
4941 static tree
4942 grok_reference_init (tree decl, tree type, tree init, int flags)
4944 if (init == NULL_TREE)
4946 if ((DECL_LANG_SPECIFIC (decl) == 0
4947 || DECL_IN_AGGR_P (decl) == 0)
4948 && ! DECL_THIS_EXTERN (decl))
4949 error ("%qD declared as reference but not initialized", decl);
4950 return NULL_TREE;
4953 if (TREE_CODE (init) == TREE_LIST)
4954 init = build_x_compound_expr_from_list (init, ELK_INIT,
4955 tf_warning_or_error);
4957 tree ttype = TREE_TYPE (type);
4958 if (TREE_CODE (ttype) != ARRAY_TYPE
4959 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
4960 /* Note: default conversion is only called in very special cases. */
4961 init = decay_conversion (init, tf_warning_or_error);
4963 /* check_initializer handles this for non-reference variables, but for
4964 references we need to do it here or the initializer will get the
4965 incomplete array type and confuse later calls to
4966 cp_complete_array_type. */
4967 if (TREE_CODE (ttype) == ARRAY_TYPE
4968 && TYPE_DOMAIN (ttype) == NULL_TREE
4969 && (BRACE_ENCLOSED_INITIALIZER_P (init)
4970 || TREE_CODE (init) == STRING_CST))
4972 cp_complete_array_type (&ttype, init, false);
4973 if (ttype != TREE_TYPE (type))
4974 type = cp_build_reference_type (ttype, TYPE_REF_IS_RVALUE (type));
4977 /* Convert INIT to the reference type TYPE. This may involve the
4978 creation of a temporary, whose lifetime must be the same as that
4979 of the reference. If so, a DECL_EXPR for the temporary will be
4980 added just after the DECL_EXPR for DECL. That's why we don't set
4981 DECL_INITIAL for local references (instead assigning to them
4982 explicitly); we need to allow the temporary to be initialized
4983 first. */
4984 return initialize_reference (type, init, flags,
4985 tf_warning_or_error);
4988 /* Designated initializers in arrays are not supported in GNU C++.
4989 The parser cannot detect this error since it does not know whether
4990 a given brace-enclosed initializer is for a class type or for an
4991 array. This function checks that CE does not use a designated
4992 initializer. If it does, an error is issued. Returns true if CE
4993 is valid, i.e., does not have a designated initializer. */
4995 static bool
4996 check_array_designated_initializer (constructor_elt *ce,
4997 unsigned HOST_WIDE_INT index)
4999 /* Designated initializers for array elements are not supported. */
5000 if (ce->index)
5002 /* The parser only allows identifiers as designated
5003 initializers. */
5004 if (ce->index == error_mark_node)
5006 error ("name used in a GNU-style designated "
5007 "initializer for an array");
5008 return false;
5010 else if (identifier_p (ce->index))
5012 error ("name %qD used in a GNU-style designated "
5013 "initializer for an array", ce->index);
5014 return false;
5017 tree ce_index = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5018 ce->index, true);
5019 if (ce_index
5020 && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (ce_index))
5021 && (TREE_CODE (ce_index = maybe_constant_value (ce_index))
5022 == INTEGER_CST))
5024 /* A C99 designator is OK if it matches the current index. */
5025 if (wi::eq_p (ce_index, index))
5026 return true;
5027 else
5028 sorry ("non-trivial designated initializers not supported");
5030 else
5031 error ("C99 designator %qE is not an integral constant-expression",
5032 ce->index);
5034 return false;
5037 return true;
5040 /* When parsing `int a[] = {1, 2};' we don't know the size of the
5041 array until we finish parsing the initializer. If that's the
5042 situation we're in, update DECL accordingly. */
5044 static void
5045 maybe_deduce_size_from_array_init (tree decl, tree init)
5047 tree type = TREE_TYPE (decl);
5049 if (TREE_CODE (type) == ARRAY_TYPE
5050 && TYPE_DOMAIN (type) == NULL_TREE
5051 && TREE_CODE (decl) != TYPE_DECL)
5053 /* do_default is really a C-ism to deal with tentative definitions.
5054 But let's leave it here to ease the eventual merge. */
5055 int do_default = !DECL_EXTERNAL (decl);
5056 tree initializer = init ? init : DECL_INITIAL (decl);
5057 int failure = 0;
5059 /* Check that there are no designated initializers in INIT, as
5060 those are not supported in GNU C++, and as the middle-end
5061 will crash if presented with a non-numeric designated
5062 initializer. */
5063 if (initializer && BRACE_ENCLOSED_INITIALIZER_P (initializer))
5065 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initializer);
5066 constructor_elt *ce;
5067 HOST_WIDE_INT i;
5068 FOR_EACH_VEC_SAFE_ELT (v, i, ce)
5069 if (!check_array_designated_initializer (ce, i))
5070 failure = 1;
5073 if (!failure)
5075 failure = cp_complete_array_type (&TREE_TYPE (decl), initializer,
5076 do_default);
5077 if (failure == 1)
5079 error ("initializer fails to determine size of %qD", decl);
5081 else if (failure == 2)
5083 if (do_default)
5085 error ("array size missing in %qD", decl);
5087 /* If a `static' var's size isn't known, make it extern as
5088 well as static, so it does not get allocated. If it's not
5089 `static', then don't mark it extern; finish_incomplete_decl
5090 will give it a default size and it will get allocated. */
5091 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
5092 DECL_EXTERNAL (decl) = 1;
5094 else if (failure == 3)
5096 error ("zero-size array %qD", decl);
5100 cp_apply_type_quals_to_decl (cp_type_quals (TREE_TYPE (decl)), decl);
5102 relayout_decl (decl);
5106 /* Set DECL_SIZE, DECL_ALIGN, etc. for DECL (a VAR_DECL), and issue
5107 any appropriate error messages regarding the layout. */
5109 static void
5110 layout_var_decl (tree decl)
5112 tree type;
5114 type = TREE_TYPE (decl);
5115 if (type == error_mark_node)
5116 return;
5118 /* If we haven't already laid out this declaration, do so now.
5119 Note that we must not call complete type for an external object
5120 because it's type might involve templates that we are not
5121 supposed to instantiate yet. (And it's perfectly valid to say
5122 `extern X x' for some incomplete type `X'.) */
5123 if (!DECL_EXTERNAL (decl))
5124 complete_type (type);
5125 if (!DECL_SIZE (decl)
5126 && TREE_TYPE (decl) != error_mark_node
5127 && (COMPLETE_TYPE_P (type)
5128 || (TREE_CODE (type) == ARRAY_TYPE
5129 && !TYPE_DOMAIN (type)
5130 && COMPLETE_TYPE_P (TREE_TYPE (type)))))
5131 layout_decl (decl, 0);
5133 if (!DECL_EXTERNAL (decl) && DECL_SIZE (decl) == NULL_TREE)
5135 /* An automatic variable with an incomplete type: that is an error.
5136 Don't talk about array types here, since we took care of that
5137 message in grokdeclarator. */
5138 error ("storage size of %qD isn%'t known", decl);
5139 TREE_TYPE (decl) = error_mark_node;
5141 #if 0
5142 /* Keep this code around in case we later want to control debug info
5143 based on whether a type is "used". (jason 1999-11-11) */
5145 else if (!DECL_EXTERNAL (decl) && MAYBE_CLASS_TYPE_P (ttype))
5146 /* Let debugger know it should output info for this type. */
5147 note_debug_info_needed (ttype);
5149 if (TREE_STATIC (decl) && DECL_CLASS_SCOPE_P (decl))
5150 note_debug_info_needed (DECL_CONTEXT (decl));
5151 #endif
5153 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
5154 && DECL_SIZE (decl) != NULL_TREE
5155 && ! TREE_CONSTANT (DECL_SIZE (decl)))
5157 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
5158 constant_expression_warning (DECL_SIZE (decl));
5159 else
5161 error ("storage size of %qD isn%'t constant", decl);
5162 TREE_TYPE (decl) = error_mark_node;
5167 /* If a local static variable is declared in an inline function, or if
5168 we have a weak definition, we must endeavor to create only one
5169 instance of the variable at link-time. */
5171 void
5172 maybe_commonize_var (tree decl)
5174 /* Static data in a function with comdat linkage also has comdat
5175 linkage. */
5176 if (TREE_STATIC (decl)
5177 /* Don't mess with __FUNCTION__. */
5178 && ! DECL_ARTIFICIAL (decl)
5179 && DECL_FUNCTION_SCOPE_P (decl)
5180 && vague_linkage_p (DECL_CONTEXT (decl)))
5182 if (flag_weak)
5184 /* With weak symbols, we simply make the variable COMDAT;
5185 that will cause copies in multiple translations units to
5186 be merged. */
5187 comdat_linkage (decl);
5189 else
5191 if (DECL_INITIAL (decl) == NULL_TREE
5192 || DECL_INITIAL (decl) == error_mark_node)
5194 /* Without weak symbols, we can use COMMON to merge
5195 uninitialized variables. */
5196 TREE_PUBLIC (decl) = 1;
5197 DECL_COMMON (decl) = 1;
5199 else
5201 /* While for initialized variables, we must use internal
5202 linkage -- which means that multiple copies will not
5203 be merged. */
5204 TREE_PUBLIC (decl) = 0;
5205 DECL_COMMON (decl) = 0;
5206 if (warning_at (DECL_SOURCE_LOCATION (decl), 0,
5207 "sorry: semantics of inline function static "
5208 "data %q#D are wrong (you%'ll wind up "
5209 "with multiple copies)", decl))
5210 inform (DECL_SOURCE_LOCATION (decl),
5211 "you can work around this by removing the initializer");
5217 /* Issue an error message if DECL is an uninitialized const variable. */
5219 static void
5220 check_for_uninitialized_const_var (tree decl)
5222 tree type = strip_array_types (TREE_TYPE (decl));
5224 /* ``Unless explicitly declared extern, a const object does not have
5225 external linkage and must be initialized. ($8.4; $12.1)'' ARM
5226 7.1.6 */
5227 if (VAR_P (decl)
5228 && TREE_CODE (type) != REFERENCE_TYPE
5229 && (CP_TYPE_CONST_P (type) || var_in_constexpr_fn (decl))
5230 && !DECL_INITIAL (decl))
5232 tree field = default_init_uninitialized_part (type);
5233 if (!field)
5234 return;
5236 if (CP_TYPE_CONST_P (type))
5237 permerror (DECL_SOURCE_LOCATION (decl),
5238 "uninitialized const %qD", decl);
5239 else
5241 error_at (DECL_SOURCE_LOCATION (decl),
5242 "uninitialized variable %qD in %<constexpr%> function",
5243 decl);
5244 cp_function_chain->invalid_constexpr = true;
5247 if (CLASS_TYPE_P (type))
5249 tree defaulted_ctor;
5251 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
5252 "%q#T has no user-provided default constructor", type);
5253 defaulted_ctor = in_class_defaulted_default_constructor (type);
5254 if (defaulted_ctor)
5255 inform (DECL_SOURCE_LOCATION (defaulted_ctor),
5256 "constructor is not user-provided because it is "
5257 "explicitly defaulted in the class body");
5258 inform (0, "and the implicitly-defined constructor does not "
5259 "initialize %q+#D", field);
5264 /* Structure holding the current initializer being processed by reshape_init.
5265 CUR is a pointer to the current element being processed, END is a pointer
5266 after the last element present in the initializer. */
5267 typedef struct reshape_iterator_t
5269 constructor_elt *cur;
5270 constructor_elt *end;
5271 } reshape_iter;
5273 static tree reshape_init_r (tree, reshape_iter *, bool, tsubst_flags_t);
5275 /* FIELD is a FIELD_DECL or NULL. In the former case, the value
5276 returned is the next FIELD_DECL (possibly FIELD itself) that can be
5277 initialized. If there are no more such fields, the return value
5278 will be NULL. */
5280 tree
5281 next_initializable_field (tree field)
5283 while (field
5284 && (TREE_CODE (field) != FIELD_DECL
5285 || (DECL_C_BIT_FIELD (field) && !DECL_NAME (field))
5286 || DECL_ARTIFICIAL (field)))
5287 field = DECL_CHAIN (field);
5289 return field;
5292 /* Subroutine of reshape_init_array and reshape_init_vector, which does
5293 the actual work. ELT_TYPE is the element type of the array. MAX_INDEX is an
5294 INTEGER_CST representing the size of the array minus one (the maximum index),
5295 or NULL_TREE if the array was declared without specifying the size. D is
5296 the iterator within the constructor. */
5298 static tree
5299 reshape_init_array_1 (tree elt_type, tree max_index, reshape_iter *d,
5300 tsubst_flags_t complain)
5302 tree new_init;
5303 bool sized_array_p = (max_index && TREE_CONSTANT (max_index));
5304 unsigned HOST_WIDE_INT max_index_cst = 0;
5305 unsigned HOST_WIDE_INT index;
5307 /* The initializer for an array is always a CONSTRUCTOR. */
5308 new_init = build_constructor (init_list_type_node, NULL);
5310 if (sized_array_p)
5312 /* Minus 1 is used for zero sized arrays. */
5313 if (integer_all_onesp (max_index))
5314 return new_init;
5316 if (tree_fits_uhwi_p (max_index))
5317 max_index_cst = tree_to_uhwi (max_index);
5318 /* sizetype is sign extended, not zero extended. */
5319 else
5320 max_index_cst = tree_to_uhwi (fold_convert (size_type_node, max_index));
5323 /* Loop until there are no more initializers. */
5324 for (index = 0;
5325 d->cur != d->end && (!sized_array_p || index <= max_index_cst);
5326 ++index)
5328 tree elt_init;
5329 constructor_elt *old_cur = d->cur;
5331 check_array_designated_initializer (d->cur, index);
5332 elt_init = reshape_init_r (elt_type, d, /*first_initializer_p=*/false,
5333 complain);
5334 if (elt_init == error_mark_node)
5335 return error_mark_node;
5336 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init),
5337 size_int (index), elt_init);
5338 if (!TREE_CONSTANT (elt_init))
5339 TREE_CONSTANT (new_init) = false;
5341 /* This can happen with an invalid initializer (c++/54501). */
5342 if (d->cur == old_cur && !sized_array_p)
5343 break;
5346 return new_init;
5349 /* Subroutine of reshape_init_r, processes the initializers for arrays.
5350 Parameters are the same of reshape_init_r. */
5352 static tree
5353 reshape_init_array (tree type, reshape_iter *d, tsubst_flags_t complain)
5355 tree max_index = NULL_TREE;
5357 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
5359 if (TYPE_DOMAIN (type))
5360 max_index = array_type_nelts (type);
5362 return reshape_init_array_1 (TREE_TYPE (type), max_index, d, complain);
5365 /* Subroutine of reshape_init_r, processes the initializers for vectors.
5366 Parameters are the same of reshape_init_r. */
5368 static tree
5369 reshape_init_vector (tree type, reshape_iter *d, tsubst_flags_t complain)
5371 tree max_index = NULL_TREE;
5373 gcc_assert (TREE_CODE (type) == VECTOR_TYPE);
5375 if (COMPOUND_LITERAL_P (d->cur->value))
5377 tree value = d->cur->value;
5378 if (!same_type_p (TREE_TYPE (value), type))
5380 if (complain & tf_error)
5381 error ("invalid type %qT as initializer for a vector of type %qT",
5382 TREE_TYPE (d->cur->value), type);
5383 value = error_mark_node;
5385 ++d->cur;
5386 return value;
5389 /* For a vector, we initialize it as an array of the appropriate size. */
5390 if (TREE_CODE (type) == VECTOR_TYPE)
5391 max_index = size_int (TYPE_VECTOR_SUBPARTS (type) - 1);
5393 return reshape_init_array_1 (TREE_TYPE (type), max_index, d, complain);
5396 /* Subroutine of reshape_init_r, processes the initializers for classes
5397 or union. Parameters are the same of reshape_init_r. */
5399 static tree
5400 reshape_init_class (tree type, reshape_iter *d, bool first_initializer_p,
5401 tsubst_flags_t complain)
5403 tree field;
5404 tree new_init;
5406 gcc_assert (CLASS_TYPE_P (type));
5408 /* The initializer for a class is always a CONSTRUCTOR. */
5409 new_init = build_constructor (init_list_type_node, NULL);
5410 field = next_initializable_field (TYPE_FIELDS (type));
5412 if (!field)
5414 /* [dcl.init.aggr]
5416 An initializer for an aggregate member that is an
5417 empty class shall have the form of an empty
5418 initializer-list {}. */
5419 if (!first_initializer_p)
5421 if (complain & tf_error)
5422 error ("initializer for %qT must be brace-enclosed", type);
5423 return error_mark_node;
5425 return new_init;
5428 /* Loop through the initializable fields, gathering initializers. */
5429 while (d->cur != d->end)
5431 tree field_init;
5432 constructor_elt *old_cur = d->cur;
5434 /* Handle designated initializers, as an extension. */
5435 if (d->cur->index)
5437 if (d->cur->index == error_mark_node)
5438 return error_mark_node;
5440 if (TREE_CODE (d->cur->index) == FIELD_DECL)
5441 /* We already reshaped this. */
5442 gcc_assert (d->cur->index == field);
5443 else if (TREE_CODE (d->cur->index) == IDENTIFIER_NODE)
5444 field = lookup_field_1 (type, d->cur->index, /*want_type=*/false);
5445 else
5447 if (complain & tf_error)
5448 error ("%<[%E] =%> used in a GNU-style designated initializer"
5449 " for class %qT", d->cur->index, type);
5450 return error_mark_node;
5453 if (!field || TREE_CODE (field) != FIELD_DECL)
5455 if (complain & tf_error)
5456 error ("%qT has no non-static data member named %qD", type,
5457 d->cur->index);
5458 return error_mark_node;
5462 /* If we processed all the member of the class, we are done. */
5463 if (!field)
5464 break;
5466 field_init = reshape_init_r (TREE_TYPE (field), d,
5467 /*first_initializer_p=*/false, complain);
5468 if (field_init == error_mark_node)
5469 return error_mark_node;
5471 if (d->cur == old_cur && d->cur->index)
5473 /* This can happen with an invalid initializer for a flexible
5474 array member (c++/54441). */
5475 if (complain & tf_error)
5476 error ("invalid initializer for %q#D", field);
5477 return error_mark_node;
5480 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init), field, field_init);
5482 /* [dcl.init.aggr]
5484 When a union is initialized with a brace-enclosed
5485 initializer, the braces shall only contain an
5486 initializer for the first member of the union. */
5487 if (TREE_CODE (type) == UNION_TYPE)
5488 break;
5490 field = next_initializable_field (DECL_CHAIN (field));
5493 return new_init;
5496 /* Subroutine of reshape_init_r. We're in a context where C99 initializer
5497 designators are not valid; either complain or return true to indicate
5498 that reshape_init_r should return error_mark_node. */
5500 static bool
5501 has_designator_problem (reshape_iter *d, tsubst_flags_t complain)
5503 if (d->cur->index)
5505 if (complain & tf_error)
5506 error ("C99 designator %qE outside aggregate initializer",
5507 d->cur->index);
5508 else
5509 return true;
5511 return false;
5514 /* Subroutine of reshape_init, which processes a single initializer (part of
5515 a CONSTRUCTOR). TYPE is the type of the variable being initialized, D is the
5516 iterator within the CONSTRUCTOR which points to the initializer to process.
5517 FIRST_INITIALIZER_P is true if this is the first initializer of the
5518 outermost CONSTRUCTOR node. */
5520 static tree
5521 reshape_init_r (tree type, reshape_iter *d, bool first_initializer_p,
5522 tsubst_flags_t complain)
5524 tree init = d->cur->value;
5526 if (error_operand_p (init))
5527 return error_mark_node;
5529 if (first_initializer_p && !CP_AGGREGATE_TYPE_P (type)
5530 && has_designator_problem (d, complain))
5531 return error_mark_node;
5533 if (TREE_CODE (type) == COMPLEX_TYPE)
5535 /* A complex type can be initialized from one or two initializers,
5536 but braces are not elided. */
5537 d->cur++;
5538 if (BRACE_ENCLOSED_INITIALIZER_P (init))
5540 if (CONSTRUCTOR_NELTS (init) > 2)
5542 if (complain & tf_error)
5543 error ("too many initializers for %qT", type);
5544 else
5545 return error_mark_node;
5548 else if (first_initializer_p && d->cur != d->end)
5550 vec<constructor_elt, va_gc> *v = 0;
5551 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, init);
5552 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, d->cur->value);
5553 if (has_designator_problem (d, complain))
5554 return error_mark_node;
5555 d->cur++;
5556 init = build_constructor (init_list_type_node, v);
5558 return init;
5561 /* A non-aggregate type is always initialized with a single
5562 initializer. */
5563 if (!CP_AGGREGATE_TYPE_P (type))
5565 /* It is invalid to initialize a non-aggregate type with a
5566 brace-enclosed initializer before C++0x.
5567 We need to check for BRACE_ENCLOSED_INITIALIZER_P here because
5568 of g++.old-deja/g++.mike/p7626.C: a pointer-to-member constant is
5569 a CONSTRUCTOR (with a record type). */
5570 if (TREE_CODE (init) == CONSTRUCTOR
5571 /* Don't complain about a capture-init. */
5572 && !CONSTRUCTOR_IS_DIRECT_INIT (init)
5573 && BRACE_ENCLOSED_INITIALIZER_P (init)) /* p7626.C */
5575 if (SCALAR_TYPE_P (type))
5577 if (cxx_dialect < cxx11
5578 /* Isn't value-initialization. */
5579 || CONSTRUCTOR_NELTS (init) > 0)
5581 if (complain & tf_error)
5582 error ("braces around scalar initializer for type %qT",
5583 type);
5584 init = error_mark_node;
5587 else
5588 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5591 d->cur++;
5592 return init;
5595 /* "If T is a class type and the initializer list has a single element of
5596 type cv U, where U is T or a class derived from T, the object is
5597 initialized from that element." Even if T is an aggregate. */
5598 if (cxx_dialect >= cxx11 && (CLASS_TYPE_P (type) || VECTOR_TYPE_P (type))
5599 && first_initializer_p
5600 && d->end - d->cur == 1
5601 && reference_related_p (type, TREE_TYPE (init)))
5603 d->cur++;
5604 return init;
5607 /* [dcl.init.aggr]
5609 All implicit type conversions (clause _conv_) are considered when
5610 initializing the aggregate member with an initializer from an
5611 initializer-list. If the initializer can initialize a member,
5612 the member is initialized. Otherwise, if the member is itself a
5613 non-empty subaggregate, brace elision is assumed and the
5614 initializer is considered for the initialization of the first
5615 member of the subaggregate. */
5616 if (TREE_CODE (init) != CONSTRUCTOR
5617 /* But don't try this for the first initializer, since that would be
5618 looking through the outermost braces; A a2 = { a1 }; is not a
5619 valid aggregate initialization. */
5620 && !first_initializer_p
5621 && (same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (init))
5622 || can_convert_arg (type, TREE_TYPE (init), init, LOOKUP_NORMAL,
5623 complain)))
5625 d->cur++;
5626 return init;
5629 /* [dcl.init.string]
5631 A char array (whether plain char, signed char, or unsigned char)
5632 can be initialized by a string-literal (optionally enclosed in
5633 braces); a wchar_t array can be initialized by a wide
5634 string-literal (optionally enclosed in braces). */
5635 if (TREE_CODE (type) == ARRAY_TYPE
5636 && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type))))
5638 tree str_init = init;
5640 /* Strip one level of braces if and only if they enclose a single
5641 element (as allowed by [dcl.init.string]). */
5642 if (!first_initializer_p
5643 && TREE_CODE (str_init) == CONSTRUCTOR
5644 && vec_safe_length (CONSTRUCTOR_ELTS (str_init)) == 1)
5646 str_init = (*CONSTRUCTOR_ELTS (str_init))[0].value;
5649 /* If it's a string literal, then it's the initializer for the array
5650 as a whole. Otherwise, continue with normal initialization for
5651 array types (one value per array element). */
5652 if (TREE_CODE (str_init) == STRING_CST)
5654 if (has_designator_problem (d, complain))
5655 return error_mark_node;
5656 d->cur++;
5657 return str_init;
5661 /* The following cases are about aggregates. If we are not within a full
5662 initializer already, and there is not a CONSTRUCTOR, it means that there
5663 is a missing set of braces (that is, we are processing the case for
5664 which reshape_init exists). */
5665 if (!first_initializer_p)
5667 if (TREE_CODE (init) == CONSTRUCTOR)
5669 if (TREE_TYPE (init) && TYPE_PTRMEMFUNC_P (TREE_TYPE (init)))
5670 /* There is no need to reshape pointer-to-member function
5671 initializers, as they are always constructed correctly
5672 by the front end. */
5674 else if (COMPOUND_LITERAL_P (init))
5675 /* For a nested compound literal, there is no need to reshape since
5676 brace elision is not allowed. Even if we decided to allow it,
5677 we should add a call to reshape_init in finish_compound_literal,
5678 before calling digest_init, so changing this code would still
5679 not be necessary. */
5680 gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (init));
5681 else
5683 ++d->cur;
5684 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
5685 return reshape_init (type, init, complain);
5689 warning (OPT_Wmissing_braces, "missing braces around initializer for %qT",
5690 type);
5693 /* Dispatch to specialized routines. */
5694 if (CLASS_TYPE_P (type))
5695 return reshape_init_class (type, d, first_initializer_p, complain);
5696 else if (TREE_CODE (type) == ARRAY_TYPE)
5697 return reshape_init_array (type, d, complain);
5698 else if (TREE_CODE (type) == VECTOR_TYPE)
5699 return reshape_init_vector (type, d, complain);
5700 else
5701 gcc_unreachable();
5704 /* Undo the brace-elision allowed by [dcl.init.aggr] in a
5705 brace-enclosed aggregate initializer.
5707 INIT is the CONSTRUCTOR containing the list of initializers describing
5708 a brace-enclosed initializer for an entity of the indicated aggregate TYPE.
5709 It may not presently match the shape of the TYPE; for example:
5711 struct S { int a; int b; };
5712 struct S a[] = { 1, 2, 3, 4 };
5714 Here INIT will hold a vector of four elements, rather than a
5715 vector of two elements, each itself a vector of two elements. This
5716 routine transforms INIT from the former form into the latter. The
5717 revised CONSTRUCTOR node is returned. */
5719 tree
5720 reshape_init (tree type, tree init, tsubst_flags_t complain)
5722 vec<constructor_elt, va_gc> *v;
5723 reshape_iter d;
5724 tree new_init;
5726 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
5728 v = CONSTRUCTOR_ELTS (init);
5730 /* An empty constructor does not need reshaping, and it is always a valid
5731 initializer. */
5732 if (vec_safe_is_empty (v))
5733 return init;
5735 /* Recurse on this CONSTRUCTOR. */
5736 d.cur = &(*v)[0];
5737 d.end = d.cur + v->length ();
5739 new_init = reshape_init_r (type, &d, true, complain);
5740 if (new_init == error_mark_node)
5741 return error_mark_node;
5743 /* Make sure all the element of the constructor were used. Otherwise,
5744 issue an error about exceeding initializers. */
5745 if (d.cur != d.end)
5747 if (complain & tf_error)
5748 error ("too many initializers for %qT", type);
5749 else
5750 return error_mark_node;
5753 return new_init;
5756 /* Verify array initializer. Returns true if errors have been reported. */
5758 bool
5759 check_array_initializer (tree decl, tree type, tree init)
5761 tree element_type = TREE_TYPE (type);
5763 /* The array type itself need not be complete, because the
5764 initializer may tell us how many elements are in the array.
5765 But, the elements of the array must be complete. */
5766 if (!COMPLETE_TYPE_P (complete_type (element_type)))
5768 if (decl)
5769 error ("elements of array %q#D have incomplete type", decl);
5770 else
5771 error ("elements of array %q#T have incomplete type", type);
5772 return true;
5774 /* A compound literal can't have variable size. */
5775 if (init && !decl
5776 && ((COMPLETE_TYPE_P (type) && !TREE_CONSTANT (TYPE_SIZE (type)))
5777 || !TREE_CONSTANT (TYPE_SIZE (element_type))))
5779 error ("variable-sized compound literal");
5780 return true;
5782 return false;
5785 /* Subroutine of check_initializer; args are passed down from that function.
5786 Set stmts_are_full_exprs_p to 1 across a call to build_aggr_init. */
5788 static tree
5789 build_aggr_init_full_exprs (tree decl, tree init, int flags)
5792 gcc_assert (stmts_are_full_exprs_p ());
5793 return build_aggr_init (decl, init, flags, tf_warning_or_error);
5796 /* Verify INIT (the initializer for DECL), and record the
5797 initialization in DECL_INITIAL, if appropriate. CLEANUP is as for
5798 grok_reference_init.
5800 If the return value is non-NULL, it is an expression that must be
5801 evaluated dynamically to initialize DECL. */
5803 static tree
5804 check_initializer (tree decl, tree init, int flags, vec<tree, va_gc> **cleanups)
5806 tree type = TREE_TYPE (decl);
5807 tree init_code = NULL;
5808 tree core_type;
5810 /* Things that are going to be initialized need to have complete
5811 type. */
5812 TREE_TYPE (decl) = type = complete_type (TREE_TYPE (decl));
5814 if (DECL_HAS_VALUE_EXPR_P (decl))
5816 /* A variable with DECL_HAS_VALUE_EXPR_P set is just a placeholder,
5817 it doesn't have storage to be initialized. */
5818 gcc_assert (init == NULL_TREE);
5819 return NULL_TREE;
5822 if (type == error_mark_node)
5823 /* We will have already complained. */
5824 return NULL_TREE;
5826 if (TREE_CODE (type) == ARRAY_TYPE)
5828 if (check_array_initializer (decl, type, init))
5829 return NULL_TREE;
5831 else if (!COMPLETE_TYPE_P (type))
5833 error ("%q#D has incomplete type", decl);
5834 TREE_TYPE (decl) = error_mark_node;
5835 return NULL_TREE;
5837 else
5838 /* There is no way to make a variable-sized class type in GNU C++. */
5839 gcc_assert (TREE_CONSTANT (TYPE_SIZE (type)));
5841 if (init && BRACE_ENCLOSED_INITIALIZER_P (init))
5843 int init_len = vec_safe_length (CONSTRUCTOR_ELTS (init));
5844 if (SCALAR_TYPE_P (type))
5846 if (init_len == 0)
5848 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5849 init = build_zero_init (type, NULL_TREE, false);
5851 else if (init_len != 1 && TREE_CODE (type) != COMPLEX_TYPE)
5853 error ("scalar object %qD requires one element in initializer",
5854 decl);
5855 TREE_TYPE (decl) = error_mark_node;
5856 return NULL_TREE;
5861 if (TREE_CODE (decl) == CONST_DECL)
5863 gcc_assert (TREE_CODE (type) != REFERENCE_TYPE);
5865 DECL_INITIAL (decl) = init;
5867 gcc_assert (init != NULL_TREE);
5868 init = NULL_TREE;
5870 else if (!init && DECL_REALLY_EXTERN (decl))
5872 else if (init || type_build_ctor_call (type)
5873 || TREE_CODE (type) == REFERENCE_TYPE)
5875 if (TREE_CODE (type) == REFERENCE_TYPE)
5877 init = grok_reference_init (decl, type, init, flags);
5878 flags |= LOOKUP_ALREADY_DIGESTED;
5880 else if (!init)
5881 check_for_uninitialized_const_var (decl);
5882 /* Do not reshape constructors of vectors (they don't need to be
5883 reshaped. */
5884 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
5886 if (is_std_init_list (type))
5888 init = perform_implicit_conversion (type, init,
5889 tf_warning_or_error);
5890 flags |= LOOKUP_ALREADY_DIGESTED;
5892 else if (TYPE_NON_AGGREGATE_CLASS (type))
5894 /* Don't reshape if the class has constructors. */
5895 if (cxx_dialect == cxx98)
5896 error ("in C++98 %qD must be initialized by constructor, "
5897 "not by %<{...}%>",
5898 decl);
5900 else if (TREE_CODE (type) == VECTOR_TYPE && TYPE_VECTOR_OPAQUE (type))
5902 error ("opaque vector types cannot be initialized");
5903 init = error_mark_node;
5905 else
5907 init = reshape_init (type, init, tf_warning_or_error);
5908 flags |= LOOKUP_NO_NARROWING;
5911 else if (TREE_CODE (init) == TREE_LIST
5912 && TREE_TYPE (init) != unknown_type_node
5913 && !MAYBE_CLASS_TYPE_P (type))
5915 gcc_assert (TREE_CODE (decl) != RESULT_DECL);
5917 /* We get here with code like `int a (2);' */
5918 init = build_x_compound_expr_from_list (init, ELK_INIT,
5919 tf_warning_or_error);
5922 /* If DECL has an array type without a specific bound, deduce the
5923 array size from the initializer. */
5924 maybe_deduce_size_from_array_init (decl, init);
5925 type = TREE_TYPE (decl);
5926 if (type == error_mark_node)
5927 return NULL_TREE;
5929 if ((type_build_ctor_call (type) || CLASS_TYPE_P (type))
5930 && !(flags & LOOKUP_ALREADY_DIGESTED)
5931 && !(init && BRACE_ENCLOSED_INITIALIZER_P (init)
5932 && CP_AGGREGATE_TYPE_P (type)
5933 && (CLASS_TYPE_P (type)
5934 || !TYPE_NEEDS_CONSTRUCTING (type)
5935 || type_has_extended_temps (type))))
5937 init_code = build_aggr_init_full_exprs (decl, init, flags);
5939 /* A constructor call is a non-trivial initializer even if
5940 it isn't explicitly written. */
5941 if (TREE_SIDE_EFFECTS (init_code))
5942 DECL_NONTRIVIALLY_INITIALIZED_P (decl) = true;
5944 /* If this is a constexpr initializer, expand_default_init will
5945 have returned an INIT_EXPR rather than a CALL_EXPR. In that
5946 case, pull the initializer back out and pass it down into
5947 store_init_value. */
5948 while (TREE_CODE (init_code) == EXPR_STMT
5949 || TREE_CODE (init_code) == CONVERT_EXPR)
5950 init_code = TREE_OPERAND (init_code, 0);
5951 if (TREE_CODE (init_code) == INIT_EXPR)
5953 init = TREE_OPERAND (init_code, 1);
5954 init_code = NULL_TREE;
5955 /* Don't call digest_init; it's unnecessary and will complain
5956 about aggregate initialization of non-aggregate classes. */
5957 flags |= LOOKUP_ALREADY_DIGESTED;
5959 else if (DECL_DECLARED_CONSTEXPR_P (decl))
5961 /* Declared constexpr, but no suitable initializer; massage
5962 init appropriately so we can pass it into store_init_value
5963 for the error. */
5964 if (CLASS_TYPE_P (type)
5965 && (!init || TREE_CODE (init) == TREE_LIST))
5967 init = build_functional_cast (type, init, tf_none);
5968 if (TREE_CODE (init) == TARGET_EXPR)
5969 TARGET_EXPR_DIRECT_INIT_P (init) = true;
5971 init_code = NULL_TREE;
5973 else
5974 init = NULL_TREE;
5977 if (init && TREE_CODE (init) != TREE_VEC)
5979 /* In aggregate initialization of a variable, each element
5980 initialization is a full-expression because there is no
5981 enclosing expression. */
5982 gcc_assert (stmts_are_full_exprs_p ());
5984 init_code = store_init_value (decl, init, cleanups, flags);
5986 if (pedantic && TREE_CODE (type) == ARRAY_TYPE
5987 && DECL_INITIAL (decl)
5988 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
5989 && PAREN_STRING_LITERAL_P (DECL_INITIAL (decl)))
5990 warning (0, "array %qD initialized by parenthesized string literal %qE",
5991 decl, DECL_INITIAL (decl));
5992 init = NULL;
5995 else
5997 if (CLASS_TYPE_P (core_type = strip_array_types (type))
5998 && (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type)
5999 || CLASSTYPE_REF_FIELDS_NEED_INIT (core_type)))
6000 diagnose_uninitialized_cst_or_ref_member (core_type, /*using_new=*/false,
6001 /*complain=*/true);
6003 check_for_uninitialized_const_var (decl);
6006 if (init && init != error_mark_node)
6007 init_code = build2 (INIT_EXPR, type, decl, init);
6009 if (init_code)
6011 /* We might have set these in cp_finish_decl. */
6012 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = false;
6013 TREE_CONSTANT (decl) = false;
6016 if (init_code && DECL_IN_AGGR_P (decl))
6018 static int explained = 0;
6020 if (cxx_dialect < cxx11)
6021 error ("initializer invalid for static member with constructor");
6022 else
6023 error ("non-constant in-class initialization invalid for static "
6024 "member %qD", decl);
6025 if (!explained)
6027 inform (input_location,
6028 "(an out of class initialization is required)");
6029 explained = 1;
6031 return NULL_TREE;
6034 return init_code;
6037 /* If DECL is not a local variable, give it RTL. */
6039 static void
6040 make_rtl_for_nonlocal_decl (tree decl, tree init, const char* asmspec)
6042 int toplev = toplevel_bindings_p ();
6043 int defer_p;
6044 const char *filename;
6046 /* Set the DECL_ASSEMBLER_NAME for the object. */
6047 if (asmspec)
6049 /* The `register' keyword, when used together with an
6050 asm-specification, indicates that the variable should be
6051 placed in a particular register. */
6052 if (VAR_P (decl) && DECL_REGISTER (decl))
6054 set_user_assembler_name (decl, asmspec);
6055 DECL_HARD_REGISTER (decl) = 1;
6057 else
6059 if (TREE_CODE (decl) == FUNCTION_DECL
6060 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
6061 set_builtin_user_assembler_name (decl, asmspec);
6062 set_user_assembler_name (decl, asmspec);
6066 /* Handle non-variables up front. */
6067 if (!VAR_P (decl))
6069 rest_of_decl_compilation (decl, toplev, at_eof);
6070 return;
6073 /* If we see a class member here, it should be a static data
6074 member. */
6075 if (DECL_LANG_SPECIFIC (decl) && DECL_IN_AGGR_P (decl))
6077 gcc_assert (TREE_STATIC (decl));
6078 /* An in-class declaration of a static data member should be
6079 external; it is only a declaration, and not a definition. */
6080 if (init == NULL_TREE)
6081 gcc_assert (DECL_EXTERNAL (decl) || !TREE_PUBLIC (decl));
6084 /* We don't create any RTL for local variables. */
6085 if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
6086 return;
6088 /* We defer emission of local statics until the corresponding
6089 DECL_EXPR is expanded. */
6090 defer_p = DECL_FUNCTION_SCOPE_P (decl) || DECL_VIRTUAL_P (decl);
6092 /* We try to defer namespace-scope static constants so that they are
6093 not emitted into the object file unnecessarily. */
6094 filename = LOCATION_FILE (input_location);
6095 if (!DECL_VIRTUAL_P (decl)
6096 && TREE_READONLY (decl)
6097 && DECL_INITIAL (decl) != NULL_TREE
6098 && DECL_INITIAL (decl) != error_mark_node
6099 && filename != NULL
6100 && ! EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl))
6101 && toplev
6102 && !TREE_PUBLIC (decl))
6104 /* Fool with the linkage of static consts according to #pragma
6105 interface. */
6106 struct c_fileinfo *finfo = get_fileinfo (filename);
6107 if (!finfo->interface_unknown && !TREE_PUBLIC (decl))
6109 TREE_PUBLIC (decl) = 1;
6110 DECL_EXTERNAL (decl) = finfo->interface_only;
6113 defer_p = 1;
6115 /* Likewise for template instantiations. */
6116 else if (DECL_LANG_SPECIFIC (decl)
6117 && DECL_IMPLICIT_INSTANTIATION (decl))
6118 defer_p = 1;
6120 /* If we're not deferring, go ahead and assemble the variable. */
6121 if (!defer_p)
6122 rest_of_decl_compilation (decl, toplev, at_eof);
6125 /* walk_tree helper for wrap_temporary_cleanups, below. */
6127 static tree
6128 wrap_cleanups_r (tree *stmt_p, int *walk_subtrees, void *data)
6130 /* Stop at types or full-expression boundaries. */
6131 if (TYPE_P (*stmt_p)
6132 || TREE_CODE (*stmt_p) == CLEANUP_POINT_EXPR)
6134 *walk_subtrees = 0;
6135 return NULL_TREE;
6138 if (TREE_CODE (*stmt_p) == TARGET_EXPR)
6140 tree guard = (tree)data;
6141 tree tcleanup = TARGET_EXPR_CLEANUP (*stmt_p);
6143 tcleanup = build2 (TRY_CATCH_EXPR, void_type_node, tcleanup, guard);
6144 /* Tell honor_protect_cleanup_actions to handle this as a separate
6145 cleanup. */
6146 TRY_CATCH_IS_CLEANUP (tcleanup) = 1;
6148 TARGET_EXPR_CLEANUP (*stmt_p) = tcleanup;
6151 return NULL_TREE;
6154 /* We're initializing a local variable which has a cleanup GUARD. If there
6155 are any temporaries used in the initializer INIT of this variable, we
6156 need to wrap their cleanups with TRY_CATCH_EXPR (, GUARD) so that the
6157 variable will be cleaned up properly if one of them throws.
6159 Unfortunately, there's no way to express this properly in terms of
6160 nesting, as the regions for the temporaries overlap the region for the
6161 variable itself; if there are two temporaries, the variable needs to be
6162 the first thing destroyed if either of them throws. However, we only
6163 want to run the variable's cleanup if it actually got constructed. So
6164 we need to guard the temporary cleanups with the variable's cleanup if
6165 they are run on the normal path, but not if they are run on the
6166 exceptional path. We implement this by telling
6167 honor_protect_cleanup_actions to strip the variable cleanup from the
6168 exceptional path. */
6170 static void
6171 wrap_temporary_cleanups (tree init, tree guard)
6173 cp_walk_tree_without_duplicates (&init, wrap_cleanups_r, (void *)guard);
6176 /* Generate code to initialize DECL (a local variable). */
6178 static void
6179 initialize_local_var (tree decl, tree init)
6181 tree type = TREE_TYPE (decl);
6182 tree cleanup;
6183 int already_used;
6185 gcc_assert (VAR_P (decl)
6186 || TREE_CODE (decl) == RESULT_DECL);
6187 gcc_assert (!TREE_STATIC (decl));
6189 if (DECL_SIZE (decl) == NULL_TREE)
6191 /* If we used it already as memory, it must stay in memory. */
6192 DECL_INITIAL (decl) = NULL_TREE;
6193 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
6194 return;
6197 if (type == error_mark_node)
6198 return;
6200 /* Compute and store the initial value. */
6201 already_used = TREE_USED (decl) || TREE_USED (type);
6202 if (TREE_USED (type))
6203 DECL_READ_P (decl) = 1;
6205 /* Generate a cleanup, if necessary. */
6206 cleanup = cxx_maybe_build_cleanup (decl, tf_warning_or_error);
6208 /* Perform the initialization. */
6209 if (init)
6211 tree rinit = (TREE_CODE (init) == INIT_EXPR
6212 ? TREE_OPERAND (init, 1) : NULL_TREE);
6213 if (rinit && !TREE_SIDE_EFFECTS (rinit))
6215 /* Stick simple initializers in DECL_INITIAL so that
6216 -Wno-init-self works (c++/34772). */
6217 gcc_assert (TREE_OPERAND (init, 0) == decl);
6218 DECL_INITIAL (decl) = rinit;
6220 if (warn_init_self && TREE_CODE (type) == REFERENCE_TYPE)
6222 STRIP_NOPS (rinit);
6223 if (rinit == decl)
6224 warning_at (DECL_SOURCE_LOCATION (decl),
6225 OPT_Winit_self,
6226 "reference %qD is initialized with itself", decl);
6229 else
6231 int saved_stmts_are_full_exprs_p;
6233 /* If we're only initializing a single object, guard the
6234 destructors of any temporaries used in its initializer with
6235 its destructor. This isn't right for arrays because each
6236 element initialization is a full-expression. */
6237 if (cleanup && TREE_CODE (type) != ARRAY_TYPE)
6238 wrap_temporary_cleanups (init, cleanup);
6240 gcc_assert (building_stmt_list_p ());
6241 saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
6242 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
6243 finish_expr_stmt (init);
6244 current_stmt_tree ()->stmts_are_full_exprs_p =
6245 saved_stmts_are_full_exprs_p;
6249 /* Set this to 0 so we can tell whether an aggregate which was
6250 initialized was ever used. Don't do this if it has a
6251 destructor, so we don't complain about the 'resource
6252 allocation is initialization' idiom. Now set
6253 attribute((unused)) on types so decls of that type will be
6254 marked used. (see TREE_USED, above.) */
6255 if (TYPE_NEEDS_CONSTRUCTING (type)
6256 && ! already_used
6257 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type)
6258 && DECL_NAME (decl))
6259 TREE_USED (decl) = 0;
6260 else if (already_used)
6261 TREE_USED (decl) = 1;
6263 if (cleanup)
6264 finish_decl_cleanup (decl, cleanup);
6267 /* DECL is a VAR_DECL for a compiler-generated variable with static
6268 storage duration (like a virtual table) whose initializer is a
6269 compile-time constant. Initialize the variable and provide it to the
6270 back end. */
6272 void
6273 initialize_artificial_var (tree decl, vec<constructor_elt, va_gc> *v)
6275 tree init;
6276 gcc_assert (DECL_ARTIFICIAL (decl));
6277 init = build_constructor (TREE_TYPE (decl), v);
6278 gcc_assert (TREE_CODE (init) == CONSTRUCTOR);
6279 DECL_INITIAL (decl) = init;
6280 DECL_INITIALIZED_P (decl) = 1;
6281 determine_visibility (decl);
6282 layout_var_decl (decl);
6283 maybe_commonize_var (decl);
6284 make_rtl_for_nonlocal_decl (decl, init, /*asmspec=*/NULL);
6287 /* INIT is the initializer for a variable, as represented by the
6288 parser. Returns true iff INIT is type-dependent. */
6290 static bool
6291 type_dependent_init_p (tree init)
6293 if (TREE_CODE (init) == TREE_LIST)
6294 /* A parenthesized initializer, e.g.: int i (3, 2); ? */
6295 return any_type_dependent_elements_p (init);
6296 else if (TREE_CODE (init) == CONSTRUCTOR)
6297 /* A brace-enclosed initializer, e.g.: int i = { 3 }; ? */
6299 vec<constructor_elt, va_gc> *elts;
6300 size_t nelts;
6301 size_t i;
6303 elts = CONSTRUCTOR_ELTS (init);
6304 nelts = vec_safe_length (elts);
6305 for (i = 0; i < nelts; ++i)
6306 if (type_dependent_init_p ((*elts)[i].value))
6307 return true;
6309 else
6310 /* It must be a simple expression, e.g., int i = 3; */
6311 return type_dependent_expression_p (init);
6313 return false;
6316 /* INIT is the initializer for a variable, as represented by the
6317 parser. Returns true iff INIT is value-dependent. */
6319 static bool
6320 value_dependent_init_p (tree init)
6322 if (TREE_CODE (init) == TREE_LIST)
6323 /* A parenthesized initializer, e.g.: int i (3, 2); ? */
6324 return any_value_dependent_elements_p (init);
6325 else if (TREE_CODE (init) == CONSTRUCTOR)
6326 /* A brace-enclosed initializer, e.g.: int i = { 3 }; ? */
6328 vec<constructor_elt, va_gc> *elts;
6329 size_t nelts;
6330 size_t i;
6332 elts = CONSTRUCTOR_ELTS (init);
6333 nelts = vec_safe_length (elts);
6334 for (i = 0; i < nelts; ++i)
6335 if (value_dependent_init_p ((*elts)[i].value))
6336 return true;
6338 else
6339 /* It must be a simple expression, e.g., int i = 3; */
6340 return value_dependent_expression_p (init);
6342 return false;
6345 /* Finish processing of a declaration;
6346 install its line number and initial value.
6347 If the length of an array type is not known before,
6348 it must be determined now, from the initial value, or it is an error.
6350 INIT is the initializer (if any) for DECL. If INIT_CONST_EXPR_P is
6351 true, then INIT is an integral constant expression.
6353 FLAGS is LOOKUP_ONLYCONVERTING if the = init syntax was used, else 0
6354 if the (init) syntax was used. */
6356 void
6357 cp_finish_decl (tree decl, tree init, bool init_const_expr_p,
6358 tree asmspec_tree, int flags)
6360 tree type;
6361 vec<tree, va_gc> *cleanups = NULL;
6362 const char *asmspec = NULL;
6363 int was_readonly = 0;
6364 bool var_definition_p = false;
6365 tree auto_node;
6367 if (decl == error_mark_node)
6368 return;
6369 else if (! decl)
6371 if (init)
6372 error ("assignment (not initialization) in declaration");
6373 return;
6376 gcc_assert (TREE_CODE (decl) != RESULT_DECL);
6377 /* Parameters are handled by store_parm_decls, not cp_finish_decl. */
6378 gcc_assert (TREE_CODE (decl) != PARM_DECL);
6380 type = TREE_TYPE (decl);
6381 if (type == error_mark_node)
6382 return;
6384 /* If a name was specified, get the string. */
6385 if (at_namespace_scope_p ())
6386 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
6387 if (asmspec_tree && asmspec_tree != error_mark_node)
6388 asmspec = TREE_STRING_POINTER (asmspec_tree);
6390 if (current_class_type
6391 && CP_DECL_CONTEXT (decl) == current_class_type
6392 && TYPE_BEING_DEFINED (current_class_type)
6393 && !CLASSTYPE_TEMPLATE_INSTANTIATION (current_class_type)
6394 && (DECL_INITIAL (decl) || init))
6395 DECL_INITIALIZED_IN_CLASS_P (decl) = 1;
6397 if (TREE_CODE (decl) != FUNCTION_DECL
6398 && (auto_node = type_uses_auto (type)))
6400 tree d_init;
6401 if (init == NULL_TREE)
6403 if (DECL_LANG_SPECIFIC (decl)
6404 && DECL_TEMPLATE_INSTANTIATION (decl)
6405 && !DECL_TEMPLATE_INSTANTIATED (decl))
6407 /* init is null because we're deferring instantiating the
6408 initializer until we need it. Well, we need it now. */
6409 instantiate_decl (decl, /*defer_ok*/true, /*expl*/false);
6410 return;
6413 error ("declaration of %q#D has no initializer", decl);
6414 TREE_TYPE (decl) = error_mark_node;
6415 return;
6417 d_init = init;
6418 if (TREE_CODE (d_init) == TREE_LIST)
6419 d_init = build_x_compound_expr_from_list (d_init, ELK_INIT,
6420 tf_warning_or_error);
6421 d_init = resolve_nondeduced_context (d_init);
6422 type = TREE_TYPE (decl) = do_auto_deduction (type, d_init,
6423 auto_node);
6424 if (type == error_mark_node)
6425 return;
6426 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
6429 if (!ensure_literal_type_for_constexpr_object (decl))
6430 DECL_DECLARED_CONSTEXPR_P (decl) = 0;
6432 if (VAR_P (decl)
6433 && DECL_CLASS_SCOPE_P (decl)
6434 && DECL_INITIALIZED_IN_CLASS_P (decl))
6435 check_static_variable_definition (decl, type);
6437 if (init && TREE_CODE (decl) == FUNCTION_DECL)
6439 tree clone;
6440 if (init == ridpointers[(int)RID_DELETE])
6442 /* FIXME check this is 1st decl. */
6443 DECL_DELETED_FN (decl) = 1;
6444 DECL_DECLARED_INLINE_P (decl) = 1;
6445 DECL_INITIAL (decl) = error_mark_node;
6446 FOR_EACH_CLONE (clone, decl)
6448 DECL_DELETED_FN (clone) = 1;
6449 DECL_DECLARED_INLINE_P (clone) = 1;
6450 DECL_INITIAL (clone) = error_mark_node;
6452 init = NULL_TREE;
6454 else if (init == ridpointers[(int)RID_DEFAULT])
6456 if (defaultable_fn_check (decl))
6457 DECL_DEFAULTED_FN (decl) = 1;
6458 else
6459 DECL_INITIAL (decl) = NULL_TREE;
6463 if (init && VAR_P (decl))
6465 DECL_NONTRIVIALLY_INITIALIZED_P (decl) = 1;
6466 /* If DECL is a reference, then we want to know whether init is a
6467 reference constant; init_const_expr_p as passed tells us whether
6468 it's an rvalue constant. */
6469 if (TREE_CODE (type) == REFERENCE_TYPE)
6470 init_const_expr_p = potential_constant_expression (init);
6471 if (init_const_expr_p)
6473 /* Set these flags now for templates. We'll update the flags in
6474 store_init_value for instantiations. */
6475 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
6476 if (decl_maybe_constant_var_p (decl))
6477 TREE_CONSTANT (decl) = 1;
6481 if (processing_template_decl)
6483 bool type_dependent_p;
6485 /* Add this declaration to the statement-tree. */
6486 if (at_function_scope_p ())
6487 add_decl_expr (decl);
6489 type_dependent_p = dependent_type_p (type);
6491 if (check_for_bare_parameter_packs (init))
6493 init = NULL_TREE;
6494 DECL_INITIAL (decl) = NULL_TREE;
6497 /* Generally, initializers in templates are expanded when the
6498 template is instantiated. But, if DECL is a variable constant
6499 then it can be used in future constant expressions, so its value
6500 must be available. */
6502 if (!VAR_P (decl) || dependent_type_p (type))
6503 /* We can't do anything if the decl has dependent type. */;
6504 else if (init
6505 && init_const_expr_p
6506 && !type_dependent_p
6507 && TREE_CODE (type) != REFERENCE_TYPE
6508 && decl_maybe_constant_var_p (decl)
6509 && !type_dependent_init_p (init)
6510 && !value_dependent_init_p (init))
6512 /* This variable seems to be a non-dependent constant, so process
6513 its initializer. If check_initializer returns non-null the
6514 initialization wasn't constant after all. */
6515 tree init_code;
6516 cleanups = make_tree_vector ();
6517 init_code = check_initializer (decl, init, flags, &cleanups);
6518 if (init_code == NULL_TREE)
6519 init = NULL_TREE;
6520 release_tree_vector (cleanups);
6522 else if (!DECL_PRETTY_FUNCTION_P (decl))
6524 /* Deduce array size even if the initializer is dependent. */
6525 maybe_deduce_size_from_array_init (decl, init);
6526 /* And complain about multiple initializers. */
6527 if (init && TREE_CODE (init) == TREE_LIST && TREE_CHAIN (init)
6528 && !MAYBE_CLASS_TYPE_P (type))
6529 init = build_x_compound_expr_from_list (init, ELK_INIT,
6530 tf_warning_or_error);
6533 if (init)
6534 DECL_INITIAL (decl) = init;
6535 return;
6538 /* Just store non-static data member initializers for later. */
6539 if (init && TREE_CODE (decl) == FIELD_DECL)
6540 DECL_INITIAL (decl) = init;
6542 /* Take care of TYPE_DECLs up front. */
6543 if (TREE_CODE (decl) == TYPE_DECL)
6545 if (type != error_mark_node
6546 && MAYBE_CLASS_TYPE_P (type) && DECL_NAME (decl))
6548 if (TREE_TYPE (DECL_NAME (decl)) && TREE_TYPE (decl) != type)
6549 warning (0, "shadowing previous type declaration of %q#D", decl);
6550 set_identifier_type_value (DECL_NAME (decl), decl);
6553 /* If we have installed this as the canonical typedef for this
6554 type, and that type has not been defined yet, delay emitting
6555 the debug information for it, as we will emit it later. */
6556 if (TYPE_MAIN_DECL (TREE_TYPE (decl)) == decl
6557 && !COMPLETE_TYPE_P (TREE_TYPE (decl)))
6558 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
6560 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl),
6561 at_eof);
6562 return;
6565 /* A reference will be modified here, as it is initialized. */
6566 if (! DECL_EXTERNAL (decl)
6567 && TREE_READONLY (decl)
6568 && TREE_CODE (type) == REFERENCE_TYPE)
6570 was_readonly = 1;
6571 TREE_READONLY (decl) = 0;
6574 if (VAR_P (decl))
6576 /* If this is a local variable that will need a mangled name,
6577 register it now. We must do this before processing the
6578 initializer for the variable, since the initialization might
6579 require a guard variable, and since the mangled name of the
6580 guard variable will depend on the mangled name of this
6581 variable. */
6582 if (DECL_FUNCTION_SCOPE_P (decl)
6583 && TREE_STATIC (decl)
6584 && !DECL_ARTIFICIAL (decl))
6586 push_local_name (decl);
6587 if (DECL_CONSTRUCTOR_P (current_function_decl)
6588 || DECL_DESTRUCTOR_P (current_function_decl))
6589 /* Normally local_decls is populated during GIMPLE lowering,
6590 but [cd]tors are never actually compiled directly. We need
6591 to put statics on the list so we can deal with the label
6592 address extension. FIXME. */
6593 add_local_decl (cfun, decl);
6596 /* Convert the initializer to the type of DECL, if we have not
6597 already initialized DECL. */
6598 if (!DECL_INITIALIZED_P (decl)
6599 /* If !DECL_EXTERNAL then DECL is being defined. In the
6600 case of a static data member initialized inside the
6601 class-specifier, there can be an initializer even if DECL
6602 is *not* defined. */
6603 && (!DECL_EXTERNAL (decl) || init))
6605 if (TYPE_FOR_JAVA (type) && MAYBE_CLASS_TYPE_P (type))
6607 tree jclass
6608 = IDENTIFIER_GLOBAL_VALUE (get_identifier ("jclass"));
6609 /* Allow libjava/prims.cc define primitive classes. */
6610 if (init != NULL_TREE
6611 || jclass == NULL_TREE
6612 || TREE_CODE (jclass) != TYPE_DECL
6613 || !POINTER_TYPE_P (TREE_TYPE (jclass))
6614 || !same_type_ignoring_top_level_qualifiers_p
6615 (type, TREE_TYPE (TREE_TYPE (jclass))))
6616 error ("Java object %qD not allocated with %<new%>", decl);
6617 init = NULL_TREE;
6619 cleanups = make_tree_vector ();
6620 init = check_initializer (decl, init, flags, &cleanups);
6622 /* Handle:
6624 [dcl.init]
6626 The memory occupied by any object of static storage
6627 duration is zero-initialized at program startup before
6628 any other initialization takes place.
6630 We cannot create an appropriate initializer until after
6631 the type of DECL is finalized. If DECL_INITIAL is set,
6632 then the DECL is statically initialized, and any
6633 necessary zero-initialization has already been performed. */
6634 if (TREE_STATIC (decl) && !DECL_INITIAL (decl))
6635 DECL_INITIAL (decl) = build_zero_init (TREE_TYPE (decl),
6636 /*nelts=*/NULL_TREE,
6637 /*static_storage_p=*/true);
6638 /* Remember that the initialization for this variable has
6639 taken place. */
6640 DECL_INITIALIZED_P (decl) = 1;
6641 /* This declaration is the definition of this variable,
6642 unless we are initializing a static data member within
6643 the class specifier. */
6644 if (!DECL_EXTERNAL (decl))
6645 var_definition_p = true;
6647 /* If the variable has an array type, lay out the type, even if
6648 there is no initializer. It is valid to index through the
6649 array, and we must get TYPE_ALIGN set correctly on the array
6650 type. */
6651 else if (TREE_CODE (type) == ARRAY_TYPE)
6652 layout_type (type);
6654 if (TREE_STATIC (decl)
6655 && !at_function_scope_p ()
6656 && current_function_decl == NULL)
6657 /* So decl is a global variable or a static member of a
6658 non local class. Record the types it uses
6659 so that we can decide later to emit debug info for them. */
6660 record_types_used_by_current_var_decl (decl);
6662 else if (TREE_CODE (decl) == FIELD_DECL
6663 && TYPE_FOR_JAVA (type) && MAYBE_CLASS_TYPE_P (type))
6664 error ("non-static data member %qD has Java class type", decl);
6666 /* Add this declaration to the statement-tree. This needs to happen
6667 after the call to check_initializer so that the DECL_EXPR for a
6668 reference temp is added before the DECL_EXPR for the reference itself. */
6669 if (DECL_FUNCTION_SCOPE_P (decl))
6671 /* If we're building a variable sized type, and we might be
6672 reachable other than via the top of the current binding
6673 level, then create a new BIND_EXPR so that we deallocate
6674 the object at the right time. */
6675 if (VAR_P (decl)
6676 && DECL_SIZE (decl)
6677 && !TREE_CONSTANT (DECL_SIZE (decl))
6678 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
6680 tree bind;
6681 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
6682 TREE_SIDE_EFFECTS (bind) = 1;
6683 add_stmt (bind);
6684 BIND_EXPR_BODY (bind) = push_stmt_list ();
6686 add_decl_expr (decl);
6689 /* Let the middle end know about variables and functions -- but not
6690 static data members in uninstantiated class templates. */
6691 if (VAR_OR_FUNCTION_DECL_P (decl))
6693 if (VAR_P (decl))
6695 layout_var_decl (decl);
6696 maybe_commonize_var (decl);
6699 /* This needs to happen after the linkage is set. */
6700 determine_visibility (decl);
6702 if (var_definition_p && TREE_STATIC (decl))
6704 /* If a TREE_READONLY variable needs initialization
6705 at runtime, it is no longer readonly and we need to
6706 avoid MEM_READONLY_P being set on RTL created for it. */
6707 if (init)
6709 if (TREE_READONLY (decl))
6710 TREE_READONLY (decl) = 0;
6711 was_readonly = 0;
6713 else if (was_readonly)
6714 TREE_READONLY (decl) = 1;
6716 /* Likewise if it needs destruction. */
6717 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
6718 TREE_READONLY (decl) = 0;
6721 make_rtl_for_nonlocal_decl (decl, init, asmspec);
6723 /* Check for abstractness of the type. Notice that there is no
6724 need to strip array types here since the check for those types
6725 is already done within create_array_type_for_decl. */
6726 abstract_virtuals_error (decl, type);
6728 if (TREE_TYPE (decl) == error_mark_node)
6729 /* No initialization required. */
6731 else if (TREE_CODE (decl) == FUNCTION_DECL)
6733 if (init)
6735 if (init == ridpointers[(int)RID_DEFAULT])
6737 /* An out-of-class default definition is defined at
6738 the point where it is explicitly defaulted. */
6739 if (DECL_DELETED_FN (decl))
6740 maybe_explain_implicit_delete (decl);
6741 else if (DECL_INITIAL (decl) == error_mark_node)
6742 synthesize_method (decl);
6744 else
6745 error ("function %q#D is initialized like a variable", decl);
6747 /* else no initialization required. */
6749 else if (DECL_EXTERNAL (decl)
6750 && ! (DECL_LANG_SPECIFIC (decl)
6751 && DECL_NOT_REALLY_EXTERN (decl)))
6753 if (init)
6754 DECL_INITIAL (decl) = init;
6756 /* A variable definition. */
6757 else if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
6758 /* Initialize the local variable. */
6759 initialize_local_var (decl, init);
6761 /* If a variable is defined, and then a subsequent
6762 definition with external linkage is encountered, we will
6763 get here twice for the same variable. We want to avoid
6764 calling expand_static_init more than once. For variables
6765 that are not static data members, we can call
6766 expand_static_init only when we actually process the
6767 initializer. It is not legal to redeclare a static data
6768 member, so this issue does not arise in that case. */
6769 else if (var_definition_p && TREE_STATIC (decl))
6770 expand_static_init (decl, init);
6773 /* If a CLEANUP_STMT was created to destroy a temporary bound to a
6774 reference, insert it in the statement-tree now. */
6775 if (cleanups)
6777 unsigned i; tree t;
6778 FOR_EACH_VEC_ELT (*cleanups, i, t)
6779 push_cleanup (decl, t, false);
6780 release_tree_vector (cleanups);
6783 if (was_readonly)
6784 TREE_READONLY (decl) = 1;
6786 invoke_plugin_callbacks (PLUGIN_FINISH_DECL, decl);
6789 /* Returns a declaration for a VAR_DECL as if:
6791 extern "C" TYPE NAME;
6793 had been seen. Used to create compiler-generated global
6794 variables. */
6796 static tree
6797 declare_global_var (tree name, tree type)
6799 tree decl;
6801 push_to_top_level ();
6802 decl = build_decl (input_location, VAR_DECL, name, type);
6803 TREE_PUBLIC (decl) = 1;
6804 DECL_EXTERNAL (decl) = 1;
6805 DECL_ARTIFICIAL (decl) = 1;
6806 /* If the user has explicitly declared this variable (perhaps
6807 because the code we are compiling is part of a low-level runtime
6808 library), then it is possible that our declaration will be merged
6809 with theirs by pushdecl. */
6810 decl = pushdecl (decl);
6811 cp_finish_decl (decl, NULL_TREE, false, NULL_TREE, 0);
6812 pop_from_top_level ();
6814 return decl;
6817 /* Returns the type for the argument to "__cxa_atexit" (or "atexit",
6818 if "__cxa_atexit" is not being used) corresponding to the function
6819 to be called when the program exits. */
6821 static tree
6822 get_atexit_fn_ptr_type (void)
6824 tree fn_type;
6826 if (!atexit_fn_ptr_type_node)
6828 tree arg_type;
6829 if (flag_use_cxa_atexit
6830 && !targetm.cxx.use_atexit_for_cxa_atexit ())
6831 /* The parameter to "__cxa_atexit" is "void (*)(void *)". */
6832 arg_type = ptr_type_node;
6833 else
6834 /* The parameter to "atexit" is "void (*)(void)". */
6835 arg_type = NULL_TREE;
6837 fn_type = build_function_type_list (void_type_node,
6838 arg_type, NULL_TREE);
6839 atexit_fn_ptr_type_node = build_pointer_type (fn_type);
6842 return atexit_fn_ptr_type_node;
6845 /* Returns a pointer to the `atexit' function. Note that if
6846 FLAG_USE_CXA_ATEXIT is nonzero, then this will actually be the new
6847 `__cxa_atexit' function specified in the IA64 C++ ABI. */
6849 static tree
6850 get_atexit_node (void)
6852 tree atexit_fndecl;
6853 tree fn_type;
6854 tree fn_ptr_type;
6855 const char *name;
6856 bool use_aeabi_atexit;
6858 if (atexit_node)
6859 return atexit_node;
6861 if (flag_use_cxa_atexit && !targetm.cxx.use_atexit_for_cxa_atexit ())
6863 /* The declaration for `__cxa_atexit' is:
6865 int __cxa_atexit (void (*)(void *), void *, void *)
6867 We build up the argument types and then the function type
6868 itself. */
6869 tree argtype0, argtype1, argtype2;
6871 use_aeabi_atexit = targetm.cxx.use_aeabi_atexit ();
6872 /* First, build the pointer-to-function type for the first
6873 argument. */
6874 fn_ptr_type = get_atexit_fn_ptr_type ();
6875 /* Then, build the rest of the argument types. */
6876 argtype2 = ptr_type_node;
6877 if (use_aeabi_atexit)
6879 argtype1 = fn_ptr_type;
6880 argtype0 = ptr_type_node;
6882 else
6884 argtype1 = ptr_type_node;
6885 argtype0 = fn_ptr_type;
6887 /* And the final __cxa_atexit type. */
6888 fn_type = build_function_type_list (integer_type_node,
6889 argtype0, argtype1, argtype2,
6890 NULL_TREE);
6891 if (use_aeabi_atexit)
6892 name = "__aeabi_atexit";
6893 else
6894 name = "__cxa_atexit";
6896 else
6898 /* The declaration for `atexit' is:
6900 int atexit (void (*)());
6902 We build up the argument types and then the function type
6903 itself. */
6904 fn_ptr_type = get_atexit_fn_ptr_type ();
6905 /* Build the final atexit type. */
6906 fn_type = build_function_type_list (integer_type_node,
6907 fn_ptr_type, NULL_TREE);
6908 name = "atexit";
6911 /* Now, build the function declaration. */
6912 push_lang_context (lang_name_c);
6913 atexit_fndecl = build_library_fn_ptr (name, fn_type, ECF_LEAF | ECF_NOTHROW);
6914 mark_used (atexit_fndecl);
6915 pop_lang_context ();
6916 atexit_node = decay_conversion (atexit_fndecl, tf_warning_or_error);
6918 return atexit_node;
6921 /* Like get_atexit_node, but for thread-local cleanups. */
6923 static tree
6924 get_thread_atexit_node (void)
6926 /* The declaration for `__cxa_thread_atexit' is:
6928 int __cxa_thread_atexit (void (*)(void *), void *, void *) */
6929 tree fn_type = build_function_type_list (integer_type_node,
6930 get_atexit_fn_ptr_type (),
6931 ptr_type_node, ptr_type_node,
6932 NULL_TREE);
6934 /* Now, build the function declaration. */
6935 tree atexit_fndecl = build_library_fn_ptr ("__cxa_thread_atexit", fn_type,
6936 ECF_LEAF | ECF_NOTHROW);
6937 return decay_conversion (atexit_fndecl, tf_warning_or_error);
6940 /* Returns the __dso_handle VAR_DECL. */
6942 static tree
6943 get_dso_handle_node (void)
6945 if (dso_handle_node)
6946 return dso_handle_node;
6948 /* Declare the variable. */
6949 dso_handle_node = declare_global_var (get_identifier ("__dso_handle"),
6950 ptr_type_node);
6952 #ifdef HAVE_GAS_HIDDEN
6953 if (dso_handle_node != error_mark_node)
6955 DECL_VISIBILITY (dso_handle_node) = VISIBILITY_HIDDEN;
6956 DECL_VISIBILITY_SPECIFIED (dso_handle_node) = 1;
6958 #endif
6960 return dso_handle_node;
6963 /* Begin a new function with internal linkage whose job will be simply
6964 to destroy some particular variable. */
6966 static GTY(()) int start_cleanup_cnt;
6968 static tree
6969 start_cleanup_fn (void)
6971 char name[32];
6972 tree fntype;
6973 tree fndecl;
6974 bool use_cxa_atexit = flag_use_cxa_atexit
6975 && !targetm.cxx.use_atexit_for_cxa_atexit ();
6977 push_to_top_level ();
6979 /* No need to mangle this. */
6980 push_lang_context (lang_name_c);
6982 /* Build the name of the function. */
6983 sprintf (name, "__tcf_%d", start_cleanup_cnt++);
6984 /* Build the function declaration. */
6985 fntype = TREE_TYPE (get_atexit_fn_ptr_type ());
6986 fndecl = build_lang_decl (FUNCTION_DECL, get_identifier (name), fntype);
6987 /* It's a function with internal linkage, generated by the
6988 compiler. */
6989 TREE_PUBLIC (fndecl) = 0;
6990 DECL_ARTIFICIAL (fndecl) = 1;
6991 /* Make the function `inline' so that it is only emitted if it is
6992 actually needed. It is unlikely that it will be inlined, since
6993 it is only called via a function pointer, but we avoid unnecessary
6994 emissions this way. */
6995 DECL_DECLARED_INLINE_P (fndecl) = 1;
6996 DECL_INTERFACE_KNOWN (fndecl) = 1;
6997 /* Build the parameter. */
6998 if (use_cxa_atexit)
7000 tree parmdecl;
7002 parmdecl = cp_build_parm_decl (NULL_TREE, ptr_type_node);
7003 DECL_CONTEXT (parmdecl) = fndecl;
7004 TREE_USED (parmdecl) = 1;
7005 DECL_READ_P (parmdecl) = 1;
7006 DECL_ARGUMENTS (fndecl) = parmdecl;
7009 pushdecl (fndecl);
7010 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
7012 pop_lang_context ();
7014 return current_function_decl;
7017 /* Finish the cleanup function begun by start_cleanup_fn. */
7019 static void
7020 end_cleanup_fn (void)
7022 expand_or_defer_fn (finish_function (0));
7024 pop_from_top_level ();
7027 /* Generate code to handle the destruction of DECL, an object with
7028 static storage duration. */
7030 tree
7031 register_dtor_fn (tree decl)
7033 tree cleanup;
7034 tree addr;
7035 tree compound_stmt;
7036 tree fcall;
7037 tree type;
7038 bool ob_parm, dso_parm, use_dtor;
7039 tree arg0, arg1, arg2;
7040 tree atex_node;
7042 type = TREE_TYPE (decl);
7043 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
7044 return void_node;
7046 /* If we're using "__cxa_atexit" (or "__cxa_thread_atexit" or
7047 "__aeabi_atexit"), and DECL is a class object, we can just pass the
7048 destructor to "__cxa_atexit"; we don't have to build a temporary
7049 function to do the cleanup. */
7050 dso_parm = (flag_use_cxa_atexit
7051 && !targetm.cxx.use_atexit_for_cxa_atexit ());
7052 ob_parm = (DECL_THREAD_LOCAL_P (decl) || dso_parm);
7053 use_dtor = ob_parm && CLASS_TYPE_P (type);
7054 if (use_dtor)
7056 int idx;
7058 /* Find the destructor. */
7059 idx = lookup_fnfields_1 (type, complete_dtor_identifier);
7060 gcc_assert (idx >= 0);
7061 cleanup = (*CLASSTYPE_METHOD_VEC (type))[idx];
7062 /* Make sure it is accessible. */
7063 perform_or_defer_access_check (TYPE_BINFO (type), cleanup, cleanup,
7064 tf_warning_or_error);
7066 else
7068 /* Call build_cleanup before we enter the anonymous function so
7069 that any access checks will be done relative to the current
7070 scope, rather than the scope of the anonymous function. */
7071 build_cleanup (decl);
7073 /* Now start the function. */
7074 cleanup = start_cleanup_fn ();
7076 /* Now, recompute the cleanup. It may contain SAVE_EXPRs that refer
7077 to the original function, rather than the anonymous one. That
7078 will make the back end think that nested functions are in use,
7079 which causes confusion. */
7080 push_deferring_access_checks (dk_no_check);
7081 fcall = build_cleanup (decl);
7082 pop_deferring_access_checks ();
7084 /* Create the body of the anonymous function. */
7085 compound_stmt = begin_compound_stmt (BCS_FN_BODY);
7086 finish_expr_stmt (fcall);
7087 finish_compound_stmt (compound_stmt);
7088 end_cleanup_fn ();
7091 /* Call atexit with the cleanup function. */
7092 mark_used (cleanup);
7093 cleanup = build_address (cleanup);
7095 if (DECL_THREAD_LOCAL_P (decl))
7096 atex_node = get_thread_atexit_node ();
7097 else
7098 atex_node = get_atexit_node ();
7100 if (use_dtor)
7102 /* We must convert CLEANUP to the type that "__cxa_atexit"
7103 expects. */
7104 cleanup = build_nop (get_atexit_fn_ptr_type (), cleanup);
7105 /* "__cxa_atexit" will pass the address of DECL to the
7106 cleanup function. */
7107 mark_used (decl);
7108 addr = build_address (decl);
7109 /* The declared type of the parameter to "__cxa_atexit" is
7110 "void *". For plain "T*", we could just let the
7111 machinery in cp_build_function_call convert it -- but if the
7112 type is "cv-qualified T *", then we need to convert it
7113 before passing it in, to avoid spurious errors. */
7114 addr = build_nop (ptr_type_node, addr);
7116 else
7117 /* Since the cleanup functions we build ignore the address
7118 they're given, there's no reason to pass the actual address
7119 in, and, in general, it's cheaper to pass NULL than any
7120 other value. */
7121 addr = null_pointer_node;
7123 if (dso_parm)
7124 arg2 = cp_build_addr_expr (get_dso_handle_node (),
7125 tf_warning_or_error);
7126 else if (ob_parm)
7127 /* Just pass NULL to the dso handle parm if we don't actually
7128 have a DSO handle on this target. */
7129 arg2 = null_pointer_node;
7130 else
7131 arg2 = NULL_TREE;
7133 if (ob_parm)
7135 if (!DECL_THREAD_LOCAL_P (decl)
7136 && targetm.cxx.use_aeabi_atexit ())
7138 arg1 = cleanup;
7139 arg0 = addr;
7141 else
7143 arg1 = addr;
7144 arg0 = cleanup;
7147 else
7149 arg0 = cleanup;
7150 arg1 = NULL_TREE;
7152 return cp_build_function_call_nary (atex_node, tf_warning_or_error,
7153 arg0, arg1, arg2, NULL_TREE);
7156 /* DECL is a VAR_DECL with static storage duration. INIT, if present,
7157 is its initializer. Generate code to handle the construction
7158 and destruction of DECL. */
7160 static void
7161 expand_static_init (tree decl, tree init)
7163 gcc_assert (VAR_P (decl));
7164 gcc_assert (TREE_STATIC (decl));
7166 /* Some variables require no dynamic initialization. */
7167 if (!init
7168 && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
7170 /* Make sure the destructor is callable. */
7171 cxx_maybe_build_cleanup (decl, tf_warning_or_error);
7172 return;
7175 if (DECL_THREAD_LOCAL_P (decl) && DECL_GNU_TLS_P (decl)
7176 && !DECL_FUNCTION_SCOPE_P (decl))
7178 if (init)
7179 error ("non-local variable %qD declared %<__thread%> "
7180 "needs dynamic initialization", decl);
7181 else
7182 error ("non-local variable %qD declared %<__thread%> "
7183 "has a non-trivial destructor", decl);
7184 static bool informed;
7185 if (!informed)
7187 inform (DECL_SOURCE_LOCATION (decl),
7188 "C++11 %<thread_local%> allows dynamic initialization "
7189 "and destruction");
7190 informed = true;
7192 return;
7195 if (DECL_FUNCTION_SCOPE_P (decl))
7197 /* Emit code to perform this initialization but once. */
7198 tree if_stmt = NULL_TREE, inner_if_stmt = NULL_TREE;
7199 tree then_clause = NULL_TREE, inner_then_clause = NULL_TREE;
7200 tree guard, guard_addr;
7201 tree flag, begin;
7202 /* We don't need thread-safety code for thread-local vars. */
7203 bool thread_guard = (flag_threadsafe_statics
7204 && !DECL_THREAD_LOCAL_P (decl));
7206 /* Emit code to perform this initialization but once. This code
7207 looks like:
7209 static <type> guard;
7210 if (!guard.first_byte) {
7211 if (__cxa_guard_acquire (&guard)) {
7212 bool flag = false;
7213 try {
7214 // Do initialization.
7215 flag = true; __cxa_guard_release (&guard);
7216 // Register variable for destruction at end of program.
7217 } catch {
7218 if (!flag) __cxa_guard_abort (&guard);
7222 Note that the `flag' variable is only set to 1 *after* the
7223 initialization is complete. This ensures that an exception,
7224 thrown during the construction, will cause the variable to
7225 reinitialized when we pass through this code again, as per:
7227 [stmt.dcl]
7229 If the initialization exits by throwing an exception, the
7230 initialization is not complete, so it will be tried again
7231 the next time control enters the declaration.
7233 This process should be thread-safe, too; multiple threads
7234 should not be able to initialize the variable more than
7235 once. */
7237 /* Create the guard variable. */
7238 guard = get_guard (decl);
7240 /* This optimization isn't safe on targets with relaxed memory
7241 consistency. On such targets we force synchronization in
7242 __cxa_guard_acquire. */
7243 if (!targetm.relaxed_ordering || !thread_guard)
7245 /* Begin the conditional initialization. */
7246 if_stmt = begin_if_stmt ();
7247 finish_if_stmt_cond (get_guard_cond (guard), if_stmt);
7248 then_clause = begin_compound_stmt (BCS_NO_SCOPE);
7251 if (thread_guard)
7253 tree vfntype = NULL_TREE;
7254 tree acquire_name, release_name, abort_name;
7255 tree acquire_fn, release_fn, abort_fn;
7256 guard_addr = build_address (guard);
7258 acquire_name = get_identifier ("__cxa_guard_acquire");
7259 release_name = get_identifier ("__cxa_guard_release");
7260 abort_name = get_identifier ("__cxa_guard_abort");
7261 acquire_fn = identifier_global_value (acquire_name);
7262 release_fn = identifier_global_value (release_name);
7263 abort_fn = identifier_global_value (abort_name);
7264 if (!acquire_fn)
7265 acquire_fn = push_library_fn
7266 (acquire_name, build_function_type_list (integer_type_node,
7267 TREE_TYPE (guard_addr),
7268 NULL_TREE),
7269 NULL_TREE, ECF_NOTHROW | ECF_LEAF);
7270 if (!release_fn || !abort_fn)
7271 vfntype = build_function_type_list (void_type_node,
7272 TREE_TYPE (guard_addr),
7273 NULL_TREE);
7274 if (!release_fn)
7275 release_fn = push_library_fn (release_name, vfntype, NULL_TREE,
7276 ECF_NOTHROW | ECF_LEAF);
7277 if (!abort_fn)
7278 abort_fn = push_library_fn (abort_name, vfntype, NULL_TREE,
7279 ECF_NOTHROW | ECF_LEAF);
7281 inner_if_stmt = begin_if_stmt ();
7282 finish_if_stmt_cond (build_call_n (acquire_fn, 1, guard_addr),
7283 inner_if_stmt);
7285 inner_then_clause = begin_compound_stmt (BCS_NO_SCOPE);
7286 begin = get_target_expr (boolean_false_node);
7287 flag = TARGET_EXPR_SLOT (begin);
7289 TARGET_EXPR_CLEANUP (begin)
7290 = build3 (COND_EXPR, void_type_node, flag,
7291 void_node,
7292 build_call_n (abort_fn, 1, guard_addr));
7293 CLEANUP_EH_ONLY (begin) = 1;
7295 /* Do the initialization itself. */
7296 init = add_stmt_to_compound (begin, init);
7297 init = add_stmt_to_compound
7298 (init, build2 (MODIFY_EXPR, void_type_node, flag, boolean_true_node));
7299 init = add_stmt_to_compound
7300 (init, build_call_n (release_fn, 1, guard_addr));
7302 else
7303 init = add_stmt_to_compound (init, set_guard (guard));
7305 /* Use atexit to register a function for destroying this static
7306 variable. */
7307 init = add_stmt_to_compound (init, register_dtor_fn (decl));
7309 finish_expr_stmt (init);
7311 if (thread_guard)
7313 finish_compound_stmt (inner_then_clause);
7314 finish_then_clause (inner_if_stmt);
7315 finish_if_stmt (inner_if_stmt);
7318 if (!targetm.relaxed_ordering || !thread_guard)
7320 finish_compound_stmt (then_clause);
7321 finish_then_clause (if_stmt);
7322 finish_if_stmt (if_stmt);
7325 else if (DECL_THREAD_LOCAL_P (decl))
7326 tls_aggregates = tree_cons (init, decl, tls_aggregates);
7327 else
7328 static_aggregates = tree_cons (init, decl, static_aggregates);
7332 /* Make TYPE a complete type based on INITIAL_VALUE.
7333 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
7334 2 if there was no information (in which case assume 0 if DO_DEFAULT),
7335 3 if the initializer list is empty (in pedantic mode). */
7338 cp_complete_array_type (tree *ptype, tree initial_value, bool do_default)
7340 int failure;
7341 tree type, elt_type;
7343 /* Don't get confused by a CONSTRUCTOR for some other type. */
7344 if (initial_value && TREE_CODE (initial_value) == CONSTRUCTOR
7345 && !BRACE_ENCLOSED_INITIALIZER_P (initial_value))
7346 return 1;
7348 if (initial_value)
7350 unsigned HOST_WIDE_INT i;
7351 tree value;
7353 /* An array of character type can be initialized from a
7354 brace-enclosed string constant.
7356 FIXME: this code is duplicated from reshape_init. Probably
7357 we should just call reshape_init here? */
7358 if (char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (*ptype)))
7359 && TREE_CODE (initial_value) == CONSTRUCTOR
7360 && !vec_safe_is_empty (CONSTRUCTOR_ELTS (initial_value)))
7362 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value);
7363 tree value = (*v)[0].value;
7365 if (TREE_CODE (value) == STRING_CST
7366 && v->length () == 1)
7367 initial_value = value;
7370 /* If any of the elements are parameter packs, we can't actually
7371 complete this type now because the array size is dependent. */
7372 if (TREE_CODE (initial_value) == CONSTRUCTOR)
7374 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (initial_value),
7375 i, value)
7377 if (PACK_EXPANSION_P (value))
7378 return 0;
7383 failure = complete_array_type (ptype, initial_value, do_default);
7385 /* We can create the array before the element type is complete, which
7386 means that we didn't have these two bits set in the original type
7387 either. In completing the type, we are expected to propagate these
7388 bits. See also complete_type which does the same thing for arrays
7389 of fixed size. */
7390 type = *ptype;
7391 if (TYPE_DOMAIN (type))
7393 elt_type = TREE_TYPE (type);
7394 TYPE_NEEDS_CONSTRUCTING (type) = TYPE_NEEDS_CONSTRUCTING (elt_type);
7395 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
7396 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (elt_type);
7399 return failure;
7402 /* As above, but either give an error or reject zero-size arrays, depending
7403 on COMPLAIN. */
7406 cp_complete_array_type_or_error (tree *ptype, tree initial_value,
7407 bool do_default, tsubst_flags_t complain)
7409 int failure;
7410 bool sfinae = !(complain & tf_error);
7411 /* In SFINAE context we can't be lenient about zero-size arrays. */
7412 if (sfinae)
7413 ++pedantic;
7414 failure = cp_complete_array_type (ptype, initial_value, do_default);
7415 if (sfinae)
7416 --pedantic;
7417 if (failure)
7419 if (sfinae)
7420 /* Not an error. */;
7421 else if (failure == 1)
7422 error ("initializer fails to determine size of %qT", *ptype);
7423 else if (failure == 2)
7425 if (do_default)
7426 error ("array size missing in %qT", *ptype);
7428 else if (failure == 3)
7429 error ("zero-size array %qT", *ptype);
7430 *ptype = error_mark_node;
7432 return failure;
7435 /* Return zero if something is declared to be a member of type
7436 CTYPE when in the context of CUR_TYPE. STRING is the error
7437 message to print in that case. Otherwise, quietly return 1. */
7439 static int
7440 member_function_or_else (tree ctype, tree cur_type, enum overload_flags flags)
7442 if (ctype && ctype != cur_type)
7444 if (flags == DTOR_FLAG)
7445 error ("destructor for alien class %qT cannot be a member", ctype);
7446 else
7447 error ("constructor for alien class %qT cannot be a member", ctype);
7448 return 0;
7450 return 1;
7453 /* Subroutine of `grokdeclarator'. */
7455 /* Generate errors possibly applicable for a given set of specifiers.
7456 This is for ARM $7.1.2. */
7458 static void
7459 bad_specifiers (tree object,
7460 enum bad_spec_place type,
7461 int virtualp,
7462 int quals,
7463 int inlinep,
7464 int friendp,
7465 int raises)
7467 switch (type)
7469 case BSP_VAR:
7470 if (virtualp)
7471 error ("%qD declared as a %<virtual%> variable", object);
7472 if (inlinep)
7473 error ("%qD declared as an %<inline%> variable", object);
7474 if (quals)
7475 error ("%<const%> and %<volatile%> function specifiers on "
7476 "%qD invalid in variable declaration", object);
7477 break;
7478 case BSP_PARM:
7479 if (virtualp)
7480 error ("%qD declared as a %<virtual%> parameter", object);
7481 if (inlinep)
7482 error ("%qD declared as an %<inline%> parameter", object);
7483 if (quals)
7484 error ("%<const%> and %<volatile%> function specifiers on "
7485 "%qD invalid in parameter declaration", object);
7486 break;
7487 case BSP_TYPE:
7488 if (virtualp)
7489 error ("%qD declared as a %<virtual%> type", object);
7490 if (inlinep)
7491 error ("%qD declared as an %<inline%> type", object);
7492 if (quals)
7493 error ("%<const%> and %<volatile%> function specifiers on "
7494 "%qD invalid in type declaration", object);
7495 break;
7496 case BSP_FIELD:
7497 if (virtualp)
7498 error ("%qD declared as a %<virtual%> field", object);
7499 if (inlinep)
7500 error ("%qD declared as an %<inline%> field", object);
7501 if (quals)
7502 error ("%<const%> and %<volatile%> function specifiers on "
7503 "%qD invalid in field declaration", object);
7504 break;
7505 default:
7506 gcc_unreachable();
7508 if (friendp)
7509 error ("%q+D declared as a friend", object);
7510 if (raises
7511 && (TREE_CODE (object) == TYPE_DECL
7512 || (!TYPE_PTRFN_P (TREE_TYPE (object))
7513 && !TYPE_REFFN_P (TREE_TYPE (object))
7514 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (object)))))
7515 error ("%q+D declared with an exception specification", object);
7518 /* DECL is a member function or static data member and is presently
7519 being defined. Check that the definition is taking place in a
7520 valid namespace. */
7522 static void
7523 check_class_member_definition_namespace (tree decl)
7525 /* These checks only apply to member functions and static data
7526 members. */
7527 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
7528 /* We check for problems with specializations in pt.c in
7529 check_specialization_namespace, where we can issue better
7530 diagnostics. */
7531 if (processing_specialization)
7532 return;
7533 /* There are no restrictions on the placement of
7534 explicit instantiations. */
7535 if (processing_explicit_instantiation)
7536 return;
7537 /* [class.mfct]
7539 A member function definition that appears outside of the
7540 class definition shall appear in a namespace scope enclosing
7541 the class definition.
7543 [class.static.data]
7545 The definition for a static data member shall appear in a
7546 namespace scope enclosing the member's class definition. */
7547 if (!is_ancestor (current_namespace, DECL_CONTEXT (decl)))
7548 permerror (input_location, "definition of %qD is not in namespace enclosing %qT",
7549 decl, DECL_CONTEXT (decl));
7552 /* Build a PARM_DECL for the "this" parameter. TYPE is the
7553 METHOD_TYPE for a non-static member function; QUALS are the
7554 cv-qualifiers that apply to the function. */
7556 tree
7557 build_this_parm (tree type, cp_cv_quals quals)
7559 tree this_type;
7560 tree qual_type;
7561 tree parm;
7562 cp_cv_quals this_quals;
7564 if (CLASS_TYPE_P (type))
7566 this_type
7567 = cp_build_qualified_type (type, quals & ~TYPE_QUAL_RESTRICT);
7568 this_type = build_pointer_type (this_type);
7570 else
7571 this_type = type_of_this_parm (type);
7572 /* The `this' parameter is implicitly `const'; it cannot be
7573 assigned to. */
7574 this_quals = (quals & TYPE_QUAL_RESTRICT) | TYPE_QUAL_CONST;
7575 qual_type = cp_build_qualified_type (this_type, this_quals);
7576 parm = build_artificial_parm (this_identifier, qual_type);
7577 cp_apply_type_quals_to_decl (this_quals, parm);
7578 return parm;
7581 /* DECL is a static member function. Complain if it was declared
7582 with function-cv-quals. */
7584 static void
7585 check_static_quals (tree decl, cp_cv_quals quals)
7587 if (quals != TYPE_UNQUALIFIED)
7588 error ("static member function %q#D declared with type qualifiers",
7589 decl);
7592 /* Helper function. Replace the temporary this parameter injected
7593 during cp_finish_omp_declare_simd with the real this parameter. */
7595 static tree
7596 declare_simd_adjust_this (tree *tp, int *walk_subtrees, void *data)
7598 tree this_parm = (tree) data;
7599 if (TREE_CODE (*tp) == PARM_DECL
7600 && DECL_NAME (*tp) == this_identifier
7601 && *tp != this_parm)
7602 *tp = this_parm;
7603 else if (TYPE_P (*tp))
7604 *walk_subtrees = 0;
7605 return NULL_TREE;
7608 /* CTYPE is class type, or null if non-class.
7609 TYPE is type this FUNCTION_DECL should have, either FUNCTION_TYPE
7610 or METHOD_TYPE.
7611 DECLARATOR is the function's name.
7612 PARMS is a chain of PARM_DECLs for the function.
7613 VIRTUALP is truthvalue of whether the function is virtual or not.
7614 FLAGS are to be passed through to `grokclassfn'.
7615 QUALS are qualifiers indicating whether the function is `const'
7616 or `volatile'.
7617 RAISES is a list of exceptions that this function can raise.
7618 CHECK is 1 if we must find this method in CTYPE, 0 if we should
7619 not look, and -1 if we should not call `grokclassfn' at all.
7621 SFK is the kind of special function (if any) for the new function.
7623 Returns `NULL_TREE' if something goes wrong, after issuing
7624 applicable error messages. */
7626 static tree
7627 grokfndecl (tree ctype,
7628 tree type,
7629 tree declarator,
7630 tree parms,
7631 tree orig_declarator,
7632 int virtualp,
7633 enum overload_flags flags,
7634 cp_cv_quals quals,
7635 cp_ref_qualifier rqual,
7636 tree raises,
7637 int check,
7638 int friendp,
7639 int publicp,
7640 int inlinep,
7641 bool deletedp,
7642 special_function_kind sfk,
7643 bool funcdef_flag,
7644 int template_count,
7645 tree in_namespace,
7646 tree* attrlist,
7647 location_t location)
7649 tree decl;
7650 int staticp = ctype && TREE_CODE (type) == FUNCTION_TYPE;
7651 tree t;
7653 if (rqual)
7654 type = build_ref_qualified_type (type, rqual);
7655 if (raises)
7656 type = build_exception_variant (type, raises);
7658 decl = build_lang_decl (FUNCTION_DECL, declarator, type);
7660 /* If we have an explicit location, use it, otherwise use whatever
7661 build_lang_decl used (probably input_location). */
7662 if (location != UNKNOWN_LOCATION)
7663 DECL_SOURCE_LOCATION (decl) = location;
7665 if (TREE_CODE (type) == METHOD_TYPE)
7667 tree parm;
7668 parm = build_this_parm (type, quals);
7669 DECL_CHAIN (parm) = parms;
7670 parms = parm;
7672 DECL_ARGUMENTS (decl) = parms;
7673 for (t = parms; t; t = DECL_CHAIN (t))
7674 DECL_CONTEXT (t) = decl;
7675 /* Propagate volatile out from type to decl. */
7676 if (TYPE_VOLATILE (type))
7677 TREE_THIS_VOLATILE (decl) = 1;
7679 /* Setup decl according to sfk. */
7680 switch (sfk)
7682 case sfk_constructor:
7683 case sfk_copy_constructor:
7684 case sfk_move_constructor:
7685 DECL_CONSTRUCTOR_P (decl) = 1;
7686 break;
7687 case sfk_destructor:
7688 DECL_DESTRUCTOR_P (decl) = 1;
7689 break;
7690 default:
7691 break;
7694 /* If pointers to member functions use the least significant bit to
7695 indicate whether a function is virtual, ensure a pointer
7696 to this function will have that bit clear. */
7697 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn
7698 && TREE_CODE (type) == METHOD_TYPE
7699 && DECL_ALIGN (decl) < 2 * BITS_PER_UNIT)
7700 DECL_ALIGN (decl) = 2 * BITS_PER_UNIT;
7702 if (friendp
7703 && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)
7705 if (funcdef_flag)
7706 error
7707 ("defining explicit specialization %qD in friend declaration",
7708 orig_declarator);
7709 else
7711 tree fns = TREE_OPERAND (orig_declarator, 0);
7712 tree args = TREE_OPERAND (orig_declarator, 1);
7714 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
7716 /* Something like `template <class T> friend void f<T>()'. */
7717 error ("invalid use of template-id %qD in declaration "
7718 "of primary template",
7719 orig_declarator);
7720 return NULL_TREE;
7724 /* A friend declaration of the form friend void f<>(). Record
7725 the information in the TEMPLATE_ID_EXPR. */
7726 SET_DECL_IMPLICIT_INSTANTIATION (decl);
7728 gcc_assert (identifier_p (fns) || TREE_CODE (fns) == OVERLOAD);
7729 DECL_TEMPLATE_INFO (decl) = build_template_info (fns, args);
7731 for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t))
7732 if (TREE_PURPOSE (t)
7733 && TREE_CODE (TREE_PURPOSE (t)) == DEFAULT_ARG)
7735 error ("default arguments are not allowed in declaration "
7736 "of friend template specialization %qD",
7737 decl);
7738 return NULL_TREE;
7741 if (inlinep & 1)
7742 error ("%<inline%> is not allowed in declaration of friend "
7743 "template specialization %qD",
7744 decl);
7745 if (inlinep & 2)
7746 error ("%<constexpr%> is not allowed in declaration of friend "
7747 "template specialization %qD",
7748 decl);
7749 if (inlinep)
7750 return NULL_TREE;
7754 /* If this decl has namespace scope, set that up. */
7755 if (in_namespace)
7756 set_decl_namespace (decl, in_namespace, friendp);
7757 else if (!ctype)
7758 DECL_CONTEXT (decl) = FROB_CONTEXT (current_decl_namespace ());
7760 /* `main' and builtins have implicit 'C' linkage. */
7761 if ((MAIN_NAME_P (declarator)
7762 || (IDENTIFIER_LENGTH (declarator) > 10
7763 && IDENTIFIER_POINTER (declarator)[0] == '_'
7764 && IDENTIFIER_POINTER (declarator)[1] == '_'
7765 && strncmp (IDENTIFIER_POINTER (declarator)+2, "builtin_", 8) == 0)
7766 || (targetcm.cxx_implicit_extern_c
7767 && targetcm.cxx_implicit_extern_c(IDENTIFIER_POINTER (declarator))))
7768 && current_lang_name == lang_name_cplusplus
7769 && ctype == NULL_TREE
7770 && DECL_FILE_SCOPE_P (decl))
7771 SET_DECL_LANGUAGE (decl, lang_c);
7773 /* Should probably propagate const out from type to decl I bet (mrs). */
7774 if (staticp)
7776 DECL_STATIC_FUNCTION_P (decl) = 1;
7777 DECL_CONTEXT (decl) = ctype;
7780 if (deletedp)
7781 DECL_DELETED_FN (decl) = 1;
7783 if (ctype)
7785 DECL_CONTEXT (decl) = ctype;
7786 if (funcdef_flag)
7787 check_class_member_definition_namespace (decl);
7790 if (ctype == NULL_TREE && DECL_MAIN_P (decl))
7792 if (PROCESSING_REAL_TEMPLATE_DECL_P())
7793 error ("cannot declare %<::main%> to be a template");
7794 if (inlinep & 1)
7795 error ("cannot declare %<::main%> to be inline");
7796 if (inlinep & 2)
7797 error ("cannot declare %<::main%> to be constexpr");
7798 if (!publicp)
7799 error ("cannot declare %<::main%> to be static");
7800 inlinep = 0;
7801 publicp = 1;
7804 /* Members of anonymous types and local classes have no linkage; make
7805 them internal. If a typedef is made later, this will be changed. */
7806 if (ctype && (TYPE_ANONYMOUS_P (ctype)
7807 || decl_function_context (TYPE_MAIN_DECL (ctype))))
7808 publicp = 0;
7810 if (publicp && cxx_dialect == cxx98)
7812 /* [basic.link]: A name with no linkage (notably, the name of a class
7813 or enumeration declared in a local scope) shall not be used to
7814 declare an entity with linkage.
7816 DR 757 relaxes this restriction for C++0x. */
7817 no_linkage_error (decl);
7820 TREE_PUBLIC (decl) = publicp;
7821 if (! publicp)
7823 DECL_INTERFACE_KNOWN (decl) = 1;
7824 DECL_NOT_REALLY_EXTERN (decl) = 1;
7827 /* If the declaration was declared inline, mark it as such. */
7828 if (inlinep)
7830 DECL_DECLARED_INLINE_P (decl) = 1;
7831 if (publicp)
7832 DECL_COMDAT (decl) = 1;
7834 if (inlinep & 2)
7835 DECL_DECLARED_CONSTEXPR_P (decl) = true;
7837 DECL_EXTERNAL (decl) = 1;
7838 if (TREE_CODE (type) == FUNCTION_TYPE)
7840 if (quals)
7842 error (ctype
7843 ? G_("static member function %qD cannot have cv-qualifier")
7844 : G_("non-member function %qD cannot have cv-qualifier"),
7845 decl);
7846 quals = TYPE_UNQUALIFIED;
7849 if (rqual)
7851 error (ctype
7852 ? G_("static member function %qD cannot have ref-qualifier")
7853 : G_("non-member function %qD cannot have ref-qualifier"),
7854 decl);
7855 rqual = REF_QUAL_NONE;
7859 if (IDENTIFIER_OPNAME_P (DECL_NAME (decl))
7860 && !grok_op_properties (decl, /*complain=*/true))
7861 return NULL_TREE;
7862 else if (UDLIT_OPER_P (DECL_NAME (decl)))
7864 bool long_long_unsigned_p;
7865 bool long_double_p;
7866 const char *suffix = NULL;
7867 /* [over.literal]/6: Literal operators shall not have C linkage. */
7868 if (DECL_LANGUAGE (decl) == lang_c)
7870 error ("literal operator with C linkage");
7871 return NULL_TREE;
7874 if (DECL_NAMESPACE_SCOPE_P (decl))
7876 if (!check_literal_operator_args (decl, &long_long_unsigned_p,
7877 &long_double_p))
7879 error ("%qD has invalid argument list", decl);
7880 return NULL_TREE;
7883 suffix = UDLIT_OP_SUFFIX (DECL_NAME (decl));
7884 if (long_long_unsigned_p)
7886 if (cpp_interpret_int_suffix (parse_in, suffix, strlen (suffix)))
7887 warning (0, "integer suffix %<%s%>"
7888 " shadowed by implementation", suffix);
7890 else if (long_double_p)
7892 if (cpp_interpret_float_suffix (parse_in, suffix, strlen (suffix)))
7893 warning (0, "floating point suffix %<%s%>"
7894 " shadowed by implementation", suffix);
7897 else
7899 error ("%qD must be a non-member function", decl);
7900 return NULL_TREE;
7904 if (funcdef_flag)
7905 /* Make the init_value nonzero so pushdecl knows this is not
7906 tentative. error_mark_node is replaced later with the BLOCK. */
7907 DECL_INITIAL (decl) = error_mark_node;
7909 if (TYPE_NOTHROW_P (type) || nothrow_libfn_p (decl))
7910 TREE_NOTHROW (decl) = 1;
7912 if (flag_openmp || flag_cilkplus)
7914 /* Adjust "omp declare simd" attributes. */
7915 tree ods = lookup_attribute ("omp declare simd", *attrlist);
7916 if (ods)
7918 tree attr;
7919 for (attr = ods; attr;
7920 attr = lookup_attribute ("omp declare simd", TREE_CHAIN (attr)))
7922 if (TREE_CODE (type) == METHOD_TYPE)
7923 walk_tree (&TREE_VALUE (attr), declare_simd_adjust_this,
7924 DECL_ARGUMENTS (decl), NULL);
7925 if (TREE_VALUE (attr) != NULL_TREE)
7927 tree cl = TREE_VALUE (TREE_VALUE (attr));
7928 cl = c_omp_declare_simd_clauses_to_numbers
7929 (DECL_ARGUMENTS (decl), cl);
7930 if (cl)
7931 TREE_VALUE (TREE_VALUE (attr)) = cl;
7932 else
7933 TREE_VALUE (attr) = NULL_TREE;
7939 /* Caller will do the rest of this. */
7940 if (check < 0)
7941 return decl;
7943 if (ctype != NULL_TREE)
7944 grokclassfn (ctype, decl, flags);
7946 /* 12.4/3 */
7947 if (cxx_dialect >= cxx11
7948 && DECL_DESTRUCTOR_P (decl)
7949 && !TYPE_BEING_DEFINED (DECL_CONTEXT (decl))
7950 && !processing_template_decl)
7951 deduce_noexcept_on_destructor (decl);
7953 decl = check_explicit_specialization (orig_declarator, decl,
7954 template_count,
7955 2 * funcdef_flag +
7956 4 * (friendp != 0));
7957 if (decl == error_mark_node)
7958 return NULL_TREE;
7960 if (DECL_STATIC_FUNCTION_P (decl))
7961 check_static_quals (decl, quals);
7963 if (attrlist)
7965 cplus_decl_attributes (&decl, *attrlist, 0);
7966 *attrlist = NULL_TREE;
7969 /* Check main's type after attributes have been applied. */
7970 if (ctype == NULL_TREE && DECL_MAIN_P (decl))
7972 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
7973 integer_type_node))
7975 tree oldtypeargs = TYPE_ARG_TYPES (TREE_TYPE (decl));
7976 tree newtype;
7977 error ("%<::main%> must return %<int%>");
7978 newtype = build_function_type (integer_type_node, oldtypeargs);
7979 TREE_TYPE (decl) = newtype;
7981 if (warn_main)
7982 check_main_parameter_types (decl);
7985 if (ctype != NULL_TREE
7986 && (! TYPE_FOR_JAVA (ctype) || check_java_method (decl))
7987 && check)
7989 tree old_decl = check_classfn (ctype, decl,
7990 (processing_template_decl
7991 > template_class_depth (ctype))
7992 ? current_template_parms
7993 : NULL_TREE);
7995 if (old_decl == error_mark_node)
7996 return NULL_TREE;
7998 if (old_decl)
8000 tree ok;
8001 tree pushed_scope;
8003 if (TREE_CODE (old_decl) == TEMPLATE_DECL)
8004 /* Because grokfndecl is always supposed to return a
8005 FUNCTION_DECL, we pull out the DECL_TEMPLATE_RESULT
8006 here. We depend on our callers to figure out that its
8007 really a template that's being returned. */
8008 old_decl = DECL_TEMPLATE_RESULT (old_decl);
8010 if (DECL_STATIC_FUNCTION_P (old_decl)
8011 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
8013 /* Remove the `this' parm added by grokclassfn. */
8014 revert_static_member_fn (decl);
8015 check_static_quals (decl, quals);
8017 if (DECL_ARTIFICIAL (old_decl))
8019 error ("definition of implicitly-declared %qD", old_decl);
8020 return NULL_TREE;
8022 else if (DECL_DEFAULTED_FN (old_decl))
8024 error ("definition of explicitly-defaulted %q+D", decl);
8025 error ("%q+#D explicitly defaulted here", old_decl);
8026 return NULL_TREE;
8029 /* Since we've smashed OLD_DECL to its
8030 DECL_TEMPLATE_RESULT, we must do the same to DECL. */
8031 if (TREE_CODE (decl) == TEMPLATE_DECL)
8032 decl = DECL_TEMPLATE_RESULT (decl);
8034 /* Attempt to merge the declarations. This can fail, in
8035 the case of some invalid specialization declarations. */
8036 pushed_scope = push_scope (ctype);
8037 ok = duplicate_decls (decl, old_decl, friendp);
8038 if (pushed_scope)
8039 pop_scope (pushed_scope);
8040 if (!ok)
8042 error ("no %q#D member function declared in class %qT",
8043 decl, ctype);
8044 return NULL_TREE;
8046 if (ok == error_mark_node)
8047 return NULL_TREE;
8048 return old_decl;
8052 if (DECL_CONSTRUCTOR_P (decl) && !grok_ctor_properties (ctype, decl))
8053 return NULL_TREE;
8055 if (ctype == NULL_TREE || check)
8056 return decl;
8058 if (virtualp)
8059 DECL_VIRTUAL_P (decl) = 1;
8061 return decl;
8064 /* decl is a FUNCTION_DECL.
8065 specifiers are the parsed virt-specifiers.
8067 Set flags to reflect the virt-specifiers.
8069 Returns decl. */
8071 static tree
8072 set_virt_specifiers (tree decl, cp_virt_specifiers specifiers)
8074 if (decl == NULL_TREE)
8075 return decl;
8076 if (specifiers & VIRT_SPEC_OVERRIDE)
8077 DECL_OVERRIDE_P (decl) = 1;
8078 if (specifiers & VIRT_SPEC_FINAL)
8079 DECL_FINAL_P (decl) = 1;
8080 return decl;
8083 /* DECL is a VAR_DECL for a static data member. Set flags to reflect
8084 the linkage that DECL will receive in the object file. */
8086 static void
8087 set_linkage_for_static_data_member (tree decl)
8089 /* A static data member always has static storage duration and
8090 external linkage. Note that static data members are forbidden in
8091 local classes -- the only situation in which a class has
8092 non-external linkage. */
8093 TREE_PUBLIC (decl) = 1;
8094 TREE_STATIC (decl) = 1;
8095 /* For non-template classes, static data members are always put
8096 out in exactly those files where they are defined, just as
8097 with ordinary namespace-scope variables. */
8098 if (!processing_template_decl)
8099 DECL_INTERFACE_KNOWN (decl) = 1;
8102 /* Create a VAR_DECL named NAME with the indicated TYPE.
8104 If SCOPE is non-NULL, it is the class type or namespace containing
8105 the variable. If SCOPE is NULL, the variable should is created in
8106 the innermost enclosing scope. */
8108 static tree
8109 grokvardecl (tree type,
8110 tree name,
8111 tree orig_declarator,
8112 const cp_decl_specifier_seq *declspecs,
8113 int initialized,
8114 int constp,
8115 int template_count,
8116 tree scope)
8118 tree decl;
8119 tree explicit_scope;
8121 gcc_assert (!name || identifier_p (name));
8123 /* Compute the scope in which to place the variable, but remember
8124 whether or not that scope was explicitly specified by the user. */
8125 explicit_scope = scope;
8126 if (!scope)
8128 /* An explicit "extern" specifier indicates a namespace-scope
8129 variable. */
8130 if (declspecs->storage_class == sc_extern)
8131 scope = current_decl_namespace ();
8132 else if (!at_function_scope_p ())
8133 scope = current_scope ();
8136 if (scope
8137 && (/* If the variable is a namespace-scope variable declared in a
8138 template, we need DECL_LANG_SPECIFIC. */
8139 (TREE_CODE (scope) == NAMESPACE_DECL && processing_template_decl)
8140 /* Similarly for namespace-scope variables with language linkage
8141 other than C++. */
8142 || (TREE_CODE (scope) == NAMESPACE_DECL
8143 && current_lang_name != lang_name_cplusplus)
8144 /* Similarly for static data members. */
8145 || TYPE_P (scope)
8146 /* Similarly for explicit specializations. */
8147 || (orig_declarator
8148 && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)))
8149 decl = build_lang_decl (VAR_DECL, name, type);
8150 else
8151 decl = build_decl (input_location, VAR_DECL, name, type);
8153 if (explicit_scope && TREE_CODE (explicit_scope) == NAMESPACE_DECL)
8154 set_decl_namespace (decl, explicit_scope, 0);
8155 else
8156 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
8158 if (declspecs->storage_class == sc_extern)
8160 DECL_THIS_EXTERN (decl) = 1;
8161 DECL_EXTERNAL (decl) = !initialized;
8164 if (DECL_CLASS_SCOPE_P (decl))
8166 set_linkage_for_static_data_member (decl);
8167 /* This function is only called with out-of-class definitions. */
8168 DECL_EXTERNAL (decl) = 0;
8169 check_class_member_definition_namespace (decl);
8171 /* At top level, either `static' or no s.c. makes a definition
8172 (perhaps tentative), and absence of `static' makes it public. */
8173 else if (toplevel_bindings_p ())
8175 TREE_PUBLIC (decl) = (declspecs->storage_class != sc_static
8176 && (DECL_THIS_EXTERN (decl) || ! constp));
8177 TREE_STATIC (decl) = ! DECL_EXTERNAL (decl);
8179 /* Not at top level, only `static' makes a static definition. */
8180 else
8182 TREE_STATIC (decl) = declspecs->storage_class == sc_static;
8183 TREE_PUBLIC (decl) = DECL_EXTERNAL (decl);
8186 if (decl_spec_seq_has_spec_p (declspecs, ds_thread))
8188 if (DECL_EXTERNAL (decl) || TREE_STATIC (decl))
8189 set_decl_tls_model (decl, decl_default_tls_model (decl));
8190 if (declspecs->gnu_thread_keyword_p)
8191 DECL_GNU_TLS_P (decl) = true;
8194 /* If the type of the decl has no linkage, make sure that we'll
8195 notice that in mark_used. */
8196 if (cxx_dialect > cxx98
8197 && decl_linkage (decl) != lk_none
8198 && DECL_LANG_SPECIFIC (decl) == NULL
8199 && !DECL_EXTERN_C_P (decl)
8200 && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
8201 retrofit_lang_decl (decl);
8203 if (TREE_PUBLIC (decl))
8205 /* [basic.link]: A name with no linkage (notably, the name of a class
8206 or enumeration declared in a local scope) shall not be used to
8207 declare an entity with linkage.
8209 DR 757 relaxes this restriction for C++0x. */
8210 if (cxx_dialect < cxx11)
8211 no_linkage_error (decl);
8213 else
8214 DECL_INTERFACE_KNOWN (decl) = 1;
8216 // Handle explicit specializations and instantiations of variable templates.
8217 if (orig_declarator)
8218 decl = check_explicit_specialization (orig_declarator, decl,
8219 template_count, 0);
8221 return decl != error_mark_node ? decl : NULL_TREE;
8224 /* Create and return a canonical pointer to member function type, for
8225 TYPE, which is a POINTER_TYPE to a METHOD_TYPE. */
8227 tree
8228 build_ptrmemfunc_type (tree type)
8230 tree field, fields;
8231 tree t;
8233 if (type == error_mark_node)
8234 return type;
8236 /* If a canonical type already exists for this type, use it. We use
8237 this method instead of type_hash_canon, because it only does a
8238 simple equality check on the list of field members. */
8240 if ((t = TYPE_GET_PTRMEMFUNC_TYPE (type)))
8241 return t;
8243 /* Make sure that we always have the unqualified pointer-to-member
8244 type first. */
8245 if (cp_cv_quals quals = cp_type_quals (type))
8247 tree unqual = build_ptrmemfunc_type (TYPE_MAIN_VARIANT (type));
8248 return cp_build_qualified_type (unqual, quals);
8251 t = make_node (RECORD_TYPE);
8253 /* Let the front end know this is a pointer to member function. */
8254 TYPE_PTRMEMFUNC_FLAG (t) = 1;
8256 field = build_decl (input_location, FIELD_DECL, pfn_identifier, type);
8257 fields = field;
8259 field = build_decl (input_location, FIELD_DECL, delta_identifier,
8260 delta_type_node);
8261 DECL_CHAIN (field) = fields;
8262 fields = field;
8264 finish_builtin_struct (t, "__ptrmemfunc_type", fields, ptr_type_node);
8266 /* Zap out the name so that the back end will give us the debugging
8267 information for this anonymous RECORD_TYPE. */
8268 TYPE_NAME (t) = NULL_TREE;
8270 /* Cache this pointer-to-member type so that we can find it again
8271 later. */
8272 TYPE_SET_PTRMEMFUNC_TYPE (type, t);
8274 if (TYPE_STRUCTURAL_EQUALITY_P (type))
8275 SET_TYPE_STRUCTURAL_EQUALITY (t);
8276 else if (TYPE_CANONICAL (type) != type)
8277 TYPE_CANONICAL (t) = build_ptrmemfunc_type (TYPE_CANONICAL (type));
8279 return t;
8282 /* Create and return a pointer to data member type. */
8284 tree
8285 build_ptrmem_type (tree class_type, tree member_type)
8287 if (TREE_CODE (member_type) == METHOD_TYPE)
8289 cp_cv_quals quals = type_memfn_quals (member_type);
8290 cp_ref_qualifier rqual = type_memfn_rqual (member_type);
8291 member_type = build_memfn_type (member_type, class_type, quals, rqual);
8292 return build_ptrmemfunc_type (build_pointer_type (member_type));
8294 else
8296 gcc_assert (TREE_CODE (member_type) != FUNCTION_TYPE);
8297 return build_offset_type (class_type, member_type);
8301 /* DECL is a VAR_DECL defined in-class, whose TYPE is also given.
8302 Check to see that the definition is valid. Issue appropriate error
8303 messages. Return 1 if the definition is particularly bad, or 0
8304 otherwise. */
8306 static int
8307 check_static_variable_definition (tree decl, tree type)
8309 /* Can't check yet if we don't know the type. */
8310 if (dependent_type_p (type))
8311 return 0;
8312 /* If DECL is declared constexpr, we'll do the appropriate checks
8313 in check_initializer. */
8314 if (DECL_P (decl) && DECL_DECLARED_CONSTEXPR_P (decl))
8315 return 0;
8316 else if (cxx_dialect >= cxx11 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
8318 if (!COMPLETE_TYPE_P (type))
8319 error ("in-class initialization of static data member %q#D of "
8320 "incomplete type", decl);
8321 else if (literal_type_p (type))
8322 permerror (input_location,
8323 "%<constexpr%> needed for in-class initialization of "
8324 "static data member %q#D of non-integral type", decl);
8325 else
8326 error ("in-class initialization of static data member %q#D of "
8327 "non-literal type", decl);
8328 return 1;
8331 /* Motion 10 at San Diego: If a static const integral data member is
8332 initialized with an integral constant expression, the initializer
8333 may appear either in the declaration (within the class), or in
8334 the definition, but not both. If it appears in the class, the
8335 member is a member constant. The file-scope definition is always
8336 required. */
8337 if (!ARITHMETIC_TYPE_P (type) && TREE_CODE (type) != ENUMERAL_TYPE)
8339 error ("invalid in-class initialization of static data member "
8340 "of non-integral type %qT",
8341 type);
8342 return 1;
8344 else if (!CP_TYPE_CONST_P (type))
8345 error ("ISO C++ forbids in-class initialization of non-const "
8346 "static member %qD",
8347 decl);
8348 else if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type))
8349 pedwarn (input_location, OPT_Wpedantic, "ISO C++ forbids initialization of member constant "
8350 "%qD of non-integral type %qT", decl, type);
8352 return 0;
8355 /* *expr_p is part of the TYPE_SIZE of a variably-sized array. If any
8356 SAVE_EXPRs in *expr_p wrap expressions with side-effects, break those
8357 expressions out into temporary variables so that walk_tree doesn't
8358 step into them (c++/15764). */
8360 static tree
8361 stabilize_save_expr_r (tree *expr_p, int *walk_subtrees, void *data)
8363 hash_set<tree> *pset = (hash_set<tree> *)data;
8364 tree expr = *expr_p;
8365 if (TREE_CODE (expr) == SAVE_EXPR)
8367 tree op = TREE_OPERAND (expr, 0);
8368 cp_walk_tree (&op, stabilize_save_expr_r, data, pset);
8369 if (TREE_SIDE_EFFECTS (op))
8370 TREE_OPERAND (expr, 0) = get_temp_regvar (TREE_TYPE (op), op);
8371 *walk_subtrees = 0;
8373 else if (!EXPR_P (expr) || !TREE_SIDE_EFFECTS (expr))
8374 *walk_subtrees = 0;
8375 return NULL;
8378 /* Entry point for the above. */
8380 static void
8381 stabilize_vla_size (tree size)
8383 hash_set<tree> pset;
8384 /* Break out any function calls into temporary variables. */
8385 cp_walk_tree (&size, stabilize_save_expr_r, &pset, &pset);
8388 /* Helper function for compute_array_index_type. Look for SIZEOF_EXPR
8389 not inside of SAVE_EXPR and fold them. */
8391 static tree
8392 fold_sizeof_expr_r (tree *expr_p, int *walk_subtrees, void *data)
8394 tree expr = *expr_p;
8395 if (TREE_CODE (expr) == SAVE_EXPR || TYPE_P (expr))
8396 *walk_subtrees = 0;
8397 else if (TREE_CODE (expr) == SIZEOF_EXPR)
8399 *(bool *)data = true;
8400 if (SIZEOF_EXPR_TYPE_P (expr))
8401 expr = cxx_sizeof_or_alignof_type (TREE_TYPE (TREE_OPERAND (expr, 0)),
8402 SIZEOF_EXPR, false);
8403 else if (TYPE_P (TREE_OPERAND (expr, 0)))
8404 expr = cxx_sizeof_or_alignof_type (TREE_OPERAND (expr, 0), SIZEOF_EXPR,
8405 false);
8406 else
8407 expr = cxx_sizeof_or_alignof_expr (TREE_OPERAND (expr, 0), SIZEOF_EXPR,
8408 false);
8409 if (expr == error_mark_node)
8410 expr = size_one_node;
8411 *expr_p = expr;
8412 *walk_subtrees = 0;
8414 return NULL;
8417 /* Given the SIZE (i.e., number of elements) in an array, compute an
8418 appropriate index type for the array. If non-NULL, NAME is the
8419 name of the thing being declared. */
8421 tree
8422 compute_array_index_type (tree name, tree size, tsubst_flags_t complain)
8424 tree itype;
8425 tree osize = size;
8427 if (error_operand_p (size))
8428 return error_mark_node;
8430 if (!type_dependent_expression_p (size))
8432 tree type = TREE_TYPE (size);
8434 mark_rvalue_use (size);
8436 if (cxx_dialect < cxx11 && TREE_CODE (size) == NOP_EXPR
8437 && TREE_SIDE_EFFECTS (size))
8438 /* In C++98, we mark a non-constant array bound with a magic
8439 NOP_EXPR with TREE_SIDE_EFFECTS; don't fold in that case. */;
8440 else
8442 size = instantiate_non_dependent_expr_sfinae (size, complain);
8444 if (CLASS_TYPE_P (type)
8445 && CLASSTYPE_LITERAL_P (type))
8447 size = build_expr_type_conversion (WANT_INT, size, true);
8448 if (!size)
8450 if (!(complain & tf_error))
8451 return error_mark_node;
8452 if (name)
8453 error ("size of array %qD has non-integral type %qT",
8454 name, type);
8455 else
8456 error ("size of array has non-integral type %qT", type);
8457 size = integer_one_node;
8459 if (size == error_mark_node)
8460 return error_mark_node;
8461 type = TREE_TYPE (size);
8464 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
8465 size = maybe_constant_value (size);
8467 if (!TREE_CONSTANT (size))
8468 size = osize;
8471 if (error_operand_p (size))
8472 return error_mark_node;
8474 /* The array bound must be an integer type. */
8475 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
8477 if (!(complain & tf_error))
8478 return error_mark_node;
8479 if (name)
8480 error ("size of array %qD has non-integral type %qT", name, type);
8481 else
8482 error ("size of array has non-integral type %qT", type);
8483 size = integer_one_node;
8484 type = TREE_TYPE (size);
8488 /* A type is dependent if it is...an array type constructed from any
8489 dependent type or whose size is specified by a constant expression
8490 that is value-dependent. */
8491 /* We can only call value_dependent_expression_p on integral constant
8492 expressions; treat non-constant expressions as dependent, too. */
8493 if (processing_template_decl
8494 && (type_dependent_expression_p (size)
8495 || !TREE_CONSTANT (size) || value_dependent_expression_p (size)))
8497 /* We cannot do any checking for a SIZE that isn't known to be
8498 constant. Just build the index type and mark that it requires
8499 structural equality checks. */
8500 itype = build_index_type (build_min (MINUS_EXPR, sizetype,
8501 size, size_one_node));
8502 TYPE_DEPENDENT_P (itype) = 1;
8503 TYPE_DEPENDENT_P_VALID (itype) = 1;
8504 SET_TYPE_STRUCTURAL_EQUALITY (itype);
8505 return itype;
8508 /* Normally, the array-bound will be a constant. */
8509 if (TREE_CODE (size) == INTEGER_CST)
8511 /* Check to see if the array bound overflowed. Make that an
8512 error, no matter how generous we're being. */
8513 constant_expression_error (size);
8515 /* An array must have a positive number of elements. */
8516 if (tree_int_cst_lt (size, integer_zero_node))
8518 if (!(complain & tf_error))
8519 return error_mark_node;
8520 if (name)
8521 error ("size of array %qD is negative", name);
8522 else
8523 error ("size of array is negative");
8524 size = integer_one_node;
8526 /* As an extension we allow zero-sized arrays. */
8527 else if (integer_zerop (size))
8529 if (!(complain & tf_error))
8530 /* We must fail if performing argument deduction (as
8531 indicated by the state of complain), so that
8532 another substitution can be found. */
8533 return error_mark_node;
8534 else if (in_system_header_at (input_location))
8535 /* Allow them in system headers because glibc uses them. */;
8536 else if (name)
8537 pedwarn (input_location, OPT_Wpedantic, "ISO C++ forbids zero-size array %qD", name);
8538 else
8539 pedwarn (input_location, OPT_Wpedantic, "ISO C++ forbids zero-size array");
8542 else if (TREE_CONSTANT (size)
8543 /* We don't allow VLAs at non-function scopes, or during
8544 tentative template substitution. */
8545 || !at_function_scope_p ()
8546 || !(complain & tf_error))
8548 if (!(complain & tf_error))
8549 return error_mark_node;
8550 /* `(int) &fn' is not a valid array bound. */
8551 if (name)
8552 error ("size of array %qD is not an integral constant-expression",
8553 name);
8554 else
8555 error ("size of array is not an integral constant-expression");
8556 size = integer_one_node;
8558 else if (pedantic && warn_vla != 0)
8560 if (name)
8561 pedwarn (input_location, OPT_Wvla, "ISO C++ forbids variable length array %qD", name);
8562 else
8563 pedwarn (input_location, OPT_Wvla, "ISO C++ forbids variable length array");
8565 else if (warn_vla > 0)
8567 if (name)
8568 warning (OPT_Wvla,
8569 "variable length array %qD is used", name);
8570 else
8571 warning (OPT_Wvla,
8572 "variable length array is used");
8575 if (processing_template_decl && !TREE_CONSTANT (size))
8576 /* A variable sized array. */
8577 itype = build_min (MINUS_EXPR, sizetype, size, integer_one_node);
8578 else
8580 HOST_WIDE_INT saved_processing_template_decl;
8582 /* Compute the index of the largest element in the array. It is
8583 one less than the number of elements in the array. We save
8584 and restore PROCESSING_TEMPLATE_DECL so that computations in
8585 cp_build_binary_op will be appropriately folded. */
8586 saved_processing_template_decl = processing_template_decl;
8587 processing_template_decl = 0;
8588 itype = cp_build_binary_op (input_location,
8589 MINUS_EXPR,
8590 cp_convert (ssizetype, size, complain),
8591 cp_convert (ssizetype, integer_one_node,
8592 complain),
8593 complain);
8594 itype = fold (itype);
8595 processing_template_decl = saved_processing_template_decl;
8597 if (!TREE_CONSTANT (itype))
8599 /* A variable sized array. */
8600 itype = variable_size (itype);
8602 if (TREE_CODE (itype) != SAVE_EXPR)
8604 /* Look for SIZEOF_EXPRs in itype and fold them, otherwise
8605 they might survive till gimplification. */
8606 tree newitype = itype;
8607 bool found = false;
8608 cp_walk_tree_without_duplicates (&newitype,
8609 fold_sizeof_expr_r, &found);
8610 if (found)
8611 itype = variable_size (fold (newitype));
8614 stabilize_vla_size (itype);
8616 if (flag_sanitize & SANITIZE_VLA
8617 && do_ubsan_in_current_function ())
8619 /* We have to add 1 -- in the ubsan routine we generate
8620 LE_EXPR rather than LT_EXPR. */
8621 tree t = fold_build2 (PLUS_EXPR, TREE_TYPE (itype), itype,
8622 build_one_cst (TREE_TYPE (itype)));
8623 t = ubsan_instrument_vla (input_location, t);
8624 finish_expr_stmt (t);
8627 /* Make sure that there was no overflow when creating to a signed
8628 index type. (For example, on a 32-bit machine, an array with
8629 size 2^32 - 1 is too big.) */
8630 else if (TREE_CODE (itype) == INTEGER_CST
8631 && TREE_OVERFLOW (itype))
8633 if (!(complain & tf_error))
8634 return error_mark_node;
8635 error ("overflow in array dimension");
8636 TREE_OVERFLOW (itype) = 0;
8640 /* Create and return the appropriate index type. */
8641 itype = build_index_type (itype);
8643 /* If the index type were dependent, we would have returned early, so
8644 remember that it isn't. */
8645 TYPE_DEPENDENT_P (itype) = 0;
8646 TYPE_DEPENDENT_P_VALID (itype) = 1;
8647 return itype;
8650 /* Returns the scope (if any) in which the entity declared by
8651 DECLARATOR will be located. If the entity was declared with an
8652 unqualified name, NULL_TREE is returned. */
8654 tree
8655 get_scope_of_declarator (const cp_declarator *declarator)
8657 while (declarator && declarator->kind != cdk_id)
8658 declarator = declarator->declarator;
8660 /* If the declarator-id is a SCOPE_REF, the scope in which the
8661 declaration occurs is the first operand. */
8662 if (declarator
8663 && declarator->u.id.qualifying_scope)
8664 return declarator->u.id.qualifying_scope;
8666 /* Otherwise, the declarator is not a qualified name; the entity will
8667 be declared in the current scope. */
8668 return NULL_TREE;
8671 /* Returns an ARRAY_TYPE for an array with SIZE elements of the
8672 indicated TYPE. If non-NULL, NAME is the NAME of the declaration
8673 with this type. */
8675 static tree
8676 create_array_type_for_decl (tree name, tree type, tree size)
8678 tree itype = NULL_TREE;
8680 /* If things have already gone awry, bail now. */
8681 if (type == error_mark_node || size == error_mark_node)
8682 return error_mark_node;
8684 /* 8.3.4/1: If the type of the identifier of D contains the auto
8685 type-specifier, the program is ill-formed. */
8686 if (type_uses_auto (type))
8688 error ("%qD declared as array of %qT", name, type);
8689 return error_mark_node;
8692 /* If there are some types which cannot be array elements,
8693 issue an error-message and return. */
8694 switch (TREE_CODE (type))
8696 case VOID_TYPE:
8697 if (name)
8698 error ("declaration of %qD as array of void", name);
8699 else
8700 error ("creating array of void");
8701 return error_mark_node;
8703 case FUNCTION_TYPE:
8704 if (name)
8705 error ("declaration of %qD as array of functions", name);
8706 else
8707 error ("creating array of functions");
8708 return error_mark_node;
8710 case REFERENCE_TYPE:
8711 if (name)
8712 error ("declaration of %qD as array of references", name);
8713 else
8714 error ("creating array of references");
8715 return error_mark_node;
8717 case METHOD_TYPE:
8718 if (name)
8719 error ("declaration of %qD as array of function members", name);
8720 else
8721 error ("creating array of function members");
8722 return error_mark_node;
8724 default:
8725 break;
8728 /* [dcl.array]
8730 The constant expressions that specify the bounds of the arrays
8731 can be omitted only for the first member of the sequence. */
8732 if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type))
8734 if (name)
8735 error ("declaration of %qD as multidimensional array must "
8736 "have bounds for all dimensions except the first",
8737 name);
8738 else
8739 error ("multidimensional array must have bounds for all "
8740 "dimensions except the first");
8742 return error_mark_node;
8745 /* Figure out the index type for the array. */
8746 if (size)
8747 itype = compute_array_index_type (name, size, tf_warning_or_error);
8749 /* [dcl.array]
8750 T is called the array element type; this type shall not be [...] an
8751 abstract class type. */
8752 abstract_virtuals_error (name, type);
8754 return build_cplus_array_type (type, itype);
8757 /* Check that it's OK to declare a function with the indicated TYPE.
8758 SFK indicates the kind of special function (if any) that this
8759 function is. OPTYPE is the type given in a conversion operator
8760 declaration, or the class type for a constructor/destructor.
8761 Returns the actual return type of the function; that
8762 may be different than TYPE if an error occurs, or for certain
8763 special functions. */
8765 static tree
8766 check_special_function_return_type (special_function_kind sfk,
8767 tree type,
8768 tree optype)
8770 switch (sfk)
8772 case sfk_constructor:
8773 if (type)
8774 error ("return type specification for constructor invalid");
8776 if (targetm.cxx.cdtor_returns_this () && !TYPE_FOR_JAVA (optype))
8777 type = build_pointer_type (optype);
8778 else
8779 type = void_type_node;
8780 break;
8782 case sfk_destructor:
8783 if (type)
8784 error ("return type specification for destructor invalid");
8785 /* We can't use the proper return type here because we run into
8786 problems with ambiguous bases and covariant returns.
8787 Java classes are left unchanged because (void *) isn't a valid
8788 Java type, and we don't want to change the Java ABI. */
8789 if (targetm.cxx.cdtor_returns_this () && !TYPE_FOR_JAVA (optype))
8790 type = build_pointer_type (void_type_node);
8791 else
8792 type = void_type_node;
8793 break;
8795 case sfk_conversion:
8796 if (type)
8797 error ("return type specified for %<operator %T%>", optype);
8798 type = optype;
8799 break;
8801 default:
8802 gcc_unreachable ();
8805 return type;
8808 /* A variable or data member (whose unqualified name is IDENTIFIER)
8809 has been declared with the indicated TYPE. If the TYPE is not
8810 acceptable, issue an error message and return a type to use for
8811 error-recovery purposes. */
8813 tree
8814 check_var_type (tree identifier, tree type)
8816 if (VOID_TYPE_P (type))
8818 if (!identifier)
8819 error ("unnamed variable or field declared void");
8820 else if (identifier_p (identifier))
8822 gcc_assert (!IDENTIFIER_OPNAME_P (identifier));
8823 error ("variable or field %qE declared void", identifier);
8825 else
8826 error ("variable or field declared void");
8827 type = error_mark_node;
8830 return type;
8833 /* Given declspecs and a declarator (abstract or otherwise), determine
8834 the name and type of the object declared and construct a DECL node
8835 for it.
8837 DECLSPECS points to the representation of declaration-specifier
8838 sequence that precedes declarator.
8840 DECL_CONTEXT says which syntactic context this declaration is in:
8841 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
8842 FUNCDEF for a function definition. Like NORMAL but a few different
8843 error messages in each case. Return value may be zero meaning
8844 this definition is too screwy to try to parse.
8845 MEMFUNCDEF for a function definition. Like FUNCDEF but prepares to
8846 handle member functions (which have FIELD context).
8847 Return value may be zero meaning this definition is too screwy to
8848 try to parse.
8849 PARM for a parameter declaration (either within a function prototype
8850 or before a function body). Make a PARM_DECL, or return void_type_node.
8851 TPARM for a template parameter declaration.
8852 CATCHPARM for a parameter declaration before a catch clause.
8853 TYPENAME if for a typename (in a cast or sizeof).
8854 Don't make a DECL node; just return the ..._TYPE node.
8855 FIELD for a struct or union field; make a FIELD_DECL.
8856 BITFIELD for a field with specified width.
8858 INITIALIZED is as for start_decl.
8860 ATTRLIST is a pointer to the list of attributes, which may be NULL
8861 if there are none; *ATTRLIST may be modified if attributes from inside
8862 the declarator should be applied to the declaration.
8864 When this function is called, scoping variables (such as
8865 CURRENT_CLASS_TYPE) should reflect the scope in which the
8866 declaration occurs, not the scope in which the new declaration will
8867 be placed. For example, on:
8869 void S::f() { ... }
8871 when grokdeclarator is called for `S::f', the CURRENT_CLASS_TYPE
8872 should not be `S'.
8874 Returns a DECL (if a declarator is present), a TYPE (if there is no
8875 declarator, in cases like "struct S;"), or the ERROR_MARK_NODE if an
8876 error occurs. */
8878 tree
8879 grokdeclarator (const cp_declarator *declarator,
8880 cp_decl_specifier_seq *declspecs,
8881 enum decl_context decl_context,
8882 int initialized,
8883 tree* attrlist)
8885 tree type = NULL_TREE;
8886 int longlong = 0;
8887 int explicit_intN = 0;
8888 int virtualp, explicitp, friendp, inlinep, staticp;
8889 int explicit_int = 0;
8890 int explicit_char = 0;
8891 int defaulted_int = 0;
8893 tree typedef_decl = NULL_TREE;
8894 const char *name = NULL;
8895 tree typedef_type = NULL_TREE;
8896 /* True if this declarator is a function definition. */
8897 bool funcdef_flag = false;
8898 cp_declarator_kind innermost_code = cdk_error;
8899 int bitfield = 0;
8900 #if 0
8901 /* See the code below that used this. */
8902 tree decl_attr = NULL_TREE;
8903 #endif
8905 /* Keep track of what sort of function is being processed
8906 so that we can warn about default return values, or explicit
8907 return values which do not match prescribed defaults. */
8908 special_function_kind sfk = sfk_none;
8910 tree dname = NULL_TREE;
8911 tree ctor_return_type = NULL_TREE;
8912 enum overload_flags flags = NO_SPECIAL;
8913 /* cv-qualifiers that apply to the declarator, for a declaration of
8914 a member function. */
8915 cp_cv_quals memfn_quals = TYPE_UNQUALIFIED;
8916 /* virt-specifiers that apply to the declarator, for a declaration of
8917 a member function. */
8918 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
8919 /* ref-qualifier that applies to the declarator, for a declaration of
8920 a member function. */
8921 cp_ref_qualifier rqual = REF_QUAL_NONE;
8922 /* cv-qualifiers that apply to the type specified by the DECLSPECS. */
8923 int type_quals;
8924 tree raises = NULL_TREE;
8925 int template_count = 0;
8926 tree returned_attrs = NULL_TREE;
8927 tree parms = NULL_TREE;
8928 const cp_declarator *id_declarator;
8929 /* The unqualified name of the declarator; either an
8930 IDENTIFIER_NODE, BIT_NOT_EXPR, or TEMPLATE_ID_EXPR. */
8931 tree unqualified_id;
8932 /* The class type, if any, in which this entity is located,
8933 or NULL_TREE if none. Note that this value may be different from
8934 the current class type; for example if an attempt is made to declare
8935 "A::f" inside "B", this value will be "A". */
8936 tree ctype = current_class_type;
8937 /* The NAMESPACE_DECL for the namespace in which this entity is
8938 located. If an unqualified name is used to declare the entity,
8939 this value will be NULL_TREE, even if the entity is located at
8940 namespace scope. */
8941 tree in_namespace = NULL_TREE;
8942 cp_storage_class storage_class;
8943 bool unsigned_p, signed_p, short_p, long_p, thread_p;
8944 bool type_was_error_mark_node = false;
8945 bool parameter_pack_p = declarator? declarator->parameter_pack_p : false;
8946 bool template_type_arg = false;
8947 bool template_parm_flag = false;
8948 bool typedef_p = decl_spec_seq_has_spec_p (declspecs, ds_typedef);
8949 bool constexpr_p = decl_spec_seq_has_spec_p (declspecs, ds_constexpr);
8950 bool late_return_type_p = false;
8951 bool array_parameter_p = false;
8952 source_location saved_loc = input_location;
8953 const char *errmsg;
8955 signed_p = decl_spec_seq_has_spec_p (declspecs, ds_signed);
8956 unsigned_p = decl_spec_seq_has_spec_p (declspecs, ds_unsigned);
8957 short_p = decl_spec_seq_has_spec_p (declspecs, ds_short);
8958 long_p = decl_spec_seq_has_spec_p (declspecs, ds_long);
8959 longlong = decl_spec_seq_has_spec_p (declspecs, ds_long_long);
8960 explicit_intN = declspecs->explicit_intN_p;
8961 thread_p = decl_spec_seq_has_spec_p (declspecs, ds_thread);
8963 if (decl_context == FUNCDEF)
8964 funcdef_flag = true, decl_context = NORMAL;
8965 else if (decl_context == MEMFUNCDEF)
8966 funcdef_flag = true, decl_context = FIELD;
8967 else if (decl_context == BITFIELD)
8968 bitfield = 1, decl_context = FIELD;
8969 else if (decl_context == TEMPLATE_TYPE_ARG)
8970 template_type_arg = true, decl_context = TYPENAME;
8971 else if (decl_context == TPARM)
8972 template_parm_flag = true, decl_context = PARM;
8974 if (initialized > 1)
8975 funcdef_flag = true;
8977 /* Look inside a declarator for the name being declared
8978 and get it as a string, for an error message. */
8979 for (id_declarator = declarator;
8980 id_declarator;
8981 id_declarator = id_declarator->declarator)
8983 if (id_declarator->kind != cdk_id)
8984 innermost_code = id_declarator->kind;
8986 switch (id_declarator->kind)
8988 case cdk_function:
8989 if (id_declarator->declarator
8990 && id_declarator->declarator->kind == cdk_id)
8992 sfk = id_declarator->declarator->u.id.sfk;
8993 if (sfk == sfk_destructor)
8994 flags = DTOR_FLAG;
8996 break;
8998 case cdk_id:
9000 tree qualifying_scope = id_declarator->u.id.qualifying_scope;
9001 tree decl = id_declarator->u.id.unqualified_name;
9002 if (!decl)
9003 break;
9004 if (qualifying_scope)
9006 if (at_function_scope_p ())
9008 /* [dcl.meaning]
9010 A declarator-id shall not be qualified except
9011 for ...
9013 None of the cases are permitted in block
9014 scope. */
9015 if (qualifying_scope == global_namespace)
9016 error ("invalid use of qualified-name %<::%D%>",
9017 decl);
9018 else if (TYPE_P (qualifying_scope))
9019 error ("invalid use of qualified-name %<%T::%D%>",
9020 qualifying_scope, decl);
9021 else
9022 error ("invalid use of qualified-name %<%D::%D%>",
9023 qualifying_scope, decl);
9024 return error_mark_node;
9026 else if (TYPE_P (qualifying_scope))
9028 ctype = qualifying_scope;
9029 if (!MAYBE_CLASS_TYPE_P (ctype))
9031 error ("%q#T is not a class or a namespace", ctype);
9032 ctype = NULL_TREE;
9034 else if (innermost_code != cdk_function
9035 && current_class_type
9036 && !uniquely_derived_from_p (ctype,
9037 current_class_type))
9039 error ("invalid use of qualified-name %<%T::%D%>",
9040 qualifying_scope, decl);
9041 return error_mark_node;
9044 else if (TREE_CODE (qualifying_scope) == NAMESPACE_DECL)
9045 in_namespace = qualifying_scope;
9047 switch (TREE_CODE (decl))
9049 case BIT_NOT_EXPR:
9051 tree type;
9053 if (innermost_code != cdk_function)
9055 error ("declaration of %qD as non-function", decl);
9056 return error_mark_node;
9058 else if (!qualifying_scope
9059 && !(current_class_type && at_class_scope_p ()))
9061 error ("declaration of %qD as non-member", decl);
9062 return error_mark_node;
9065 type = TREE_OPERAND (decl, 0);
9066 if (TYPE_P (type))
9067 type = constructor_name (type);
9068 name = identifier_to_locale (IDENTIFIER_POINTER (type));
9069 dname = decl;
9071 break;
9073 case TEMPLATE_ID_EXPR:
9075 tree fns = TREE_OPERAND (decl, 0);
9077 dname = fns;
9078 if (!identifier_p (dname))
9080 if (variable_template_p (dname))
9081 dname = DECL_NAME (dname);
9082 else
9084 gcc_assert (is_overloaded_fn (dname));
9085 dname = DECL_NAME (get_first_fn (dname));
9089 /* Fall through. */
9091 case IDENTIFIER_NODE:
9092 if (identifier_p (decl))
9093 dname = decl;
9095 if (C_IS_RESERVED_WORD (dname))
9097 error ("declarator-id missing; using reserved word %qD",
9098 dname);
9099 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
9101 else if (!IDENTIFIER_TYPENAME_P (dname))
9102 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
9103 else
9105 gcc_assert (flags == NO_SPECIAL);
9106 flags = TYPENAME_FLAG;
9107 ctor_return_type = TREE_TYPE (dname);
9108 sfk = sfk_conversion;
9109 if (is_typename_at_global_scope (dname))
9110 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
9111 else
9112 name = "<invalid operator>";
9114 break;
9116 default:
9117 gcc_unreachable ();
9119 break;
9122 case cdk_array:
9123 case cdk_pointer:
9124 case cdk_reference:
9125 case cdk_ptrmem:
9126 break;
9128 case cdk_error:
9129 return error_mark_node;
9131 default:
9132 gcc_unreachable ();
9134 if (id_declarator->kind == cdk_id)
9135 break;
9138 /* [dcl.fct.edf]
9140 The declarator in a function-definition shall have the form
9141 D1 ( parameter-declaration-clause) ... */
9142 if (funcdef_flag && innermost_code != cdk_function)
9144 error ("function definition does not declare parameters");
9145 return error_mark_node;
9148 if (flags == TYPENAME_FLAG
9149 && innermost_code != cdk_function
9150 && ! (ctype && !declspecs->any_specifiers_p))
9152 error ("declaration of %qD as non-function", dname);
9153 return error_mark_node;
9156 if (dname
9157 && identifier_p (dname)
9158 && UDLIT_OPER_P (dname)
9159 && innermost_code != cdk_function)
9161 error ("declaration of %qD as non-function", dname);
9162 return error_mark_node;
9165 if (dname && IDENTIFIER_OPNAME_P (dname))
9167 if (typedef_p)
9169 error ("declaration of %qD as %<typedef%>", dname);
9170 return error_mark_node;
9172 else if (decl_context == PARM || decl_context == CATCHPARM)
9174 error ("declaration of %qD as parameter", dname);
9175 return error_mark_node;
9179 /* Anything declared one level down from the top level
9180 must be one of the parameters of a function
9181 (because the body is at least two levels down). */
9183 /* This heuristic cannot be applied to C++ nodes! Fixed, however,
9184 by not allowing C++ class definitions to specify their parameters
9185 with xdecls (must be spec.d in the parmlist).
9187 Since we now wait to push a class scope until we are sure that
9188 we are in a legitimate method context, we must set oldcname
9189 explicitly (since current_class_name is not yet alive).
9191 We also want to avoid calling this a PARM if it is in a namespace. */
9193 if (decl_context == NORMAL && !toplevel_bindings_p ())
9195 cp_binding_level *b = current_binding_level;
9196 current_binding_level = b->level_chain;
9197 if (current_binding_level != 0 && toplevel_bindings_p ())
9198 decl_context = PARM;
9199 current_binding_level = b;
9202 if (name == NULL)
9203 name = decl_context == PARM ? "parameter" : "type name";
9205 if (constexpr_p && typedef_p)
9207 error ("%<constexpr%> cannot appear in a typedef declaration");
9208 return error_mark_node;
9211 /* If there were multiple types specified in the decl-specifier-seq,
9212 issue an error message. */
9213 if (declspecs->multiple_types_p)
9215 error ("two or more data types in declaration of %qs", name);
9216 return error_mark_node;
9219 if (declspecs->conflicting_specifiers_p)
9221 error ("conflicting specifiers in declaration of %qs", name);
9222 return error_mark_node;
9225 /* Extract the basic type from the decl-specifier-seq. */
9226 type = declspecs->type;
9227 if (type == error_mark_node)
9229 type = NULL_TREE;
9230 type_was_error_mark_node = true;
9232 /* If the entire declaration is itself tagged as deprecated then
9233 suppress reports of deprecated items. */
9234 if (type && TREE_DEPRECATED (type)
9235 && deprecated_state != DEPRECATED_SUPPRESS)
9236 warn_deprecated_use (type, NULL_TREE);
9237 if (type && TREE_CODE (type) == TYPE_DECL)
9239 typedef_decl = type;
9240 type = TREE_TYPE (typedef_decl);
9241 if (TREE_DEPRECATED (type)
9242 && DECL_ARTIFICIAL (typedef_decl)
9243 && deprecated_state != DEPRECATED_SUPPRESS)
9244 warn_deprecated_use (type, NULL_TREE);
9246 /* No type at all: default to `int', and set DEFAULTED_INT
9247 because it was not a user-defined typedef. */
9248 if (type == NULL_TREE)
9250 if (signed_p || unsigned_p || long_p || short_p)
9252 /* These imply 'int'. */
9253 type = integer_type_node;
9254 defaulted_int = 1;
9256 /* If we just have "complex", it is equivalent to "complex double". */
9257 else if (!longlong && !explicit_intN
9258 && decl_spec_seq_has_spec_p (declspecs, ds_complex))
9260 type = double_type_node;
9261 pedwarn (declspecs->locations[ds_complex], OPT_Wpedantic,
9262 "ISO C++ does not support plain %<complex%> meaning "
9263 "%<double complex%>");
9266 /* Gather flags. */
9267 explicit_int = declspecs->explicit_int_p;
9268 explicit_char = declspecs->explicit_char_p;
9270 #if 0
9271 /* See the code below that used this. */
9272 if (typedef_decl)
9273 decl_attr = DECL_ATTRIBUTES (typedef_decl);
9274 #endif
9275 typedef_type = type;
9278 if (sfk != sfk_conversion)
9279 ctor_return_type = ctype;
9281 if (sfk != sfk_none)
9282 type = check_special_function_return_type (sfk, type,
9283 ctor_return_type);
9284 else if (type == NULL_TREE)
9286 int is_main;
9288 explicit_int = -1;
9290 /* We handle `main' specially here, because 'main () { }' is so
9291 common. With no options, it is allowed. With -Wreturn-type,
9292 it is a warning. It is only an error with -pedantic-errors. */
9293 is_main = (funcdef_flag
9294 && dname && identifier_p (dname)
9295 && MAIN_NAME_P (dname)
9296 && ctype == NULL_TREE
9297 && in_namespace == NULL_TREE
9298 && current_namespace == global_namespace);
9300 if (type_was_error_mark_node)
9301 /* We've already issued an error, don't complain more. */;
9302 else if (in_system_header_at (input_location) || flag_ms_extensions)
9303 /* Allow it, sigh. */;
9304 else if (! is_main)
9305 permerror (input_location, "ISO C++ forbids declaration of %qs with no type", name);
9306 else if (pedantic)
9307 pedwarn (input_location, OPT_Wpedantic,
9308 "ISO C++ forbids declaration of %qs with no type", name);
9309 else
9310 warning (OPT_Wreturn_type,
9311 "ISO C++ forbids declaration of %qs with no type", name);
9313 type = integer_type_node;
9316 ctype = NULL_TREE;
9318 if (explicit_intN)
9320 if (! int_n_enabled_p[declspecs->int_n_idx])
9322 error ("%<__int%d%> is not supported by this target",
9323 int_n_data[declspecs->int_n_idx].bitsize);
9324 explicit_intN = false;
9326 else if (pedantic && ! in_system_header_at (input_location))
9327 pedwarn (input_location, OPT_Wpedantic,
9328 "ISO C++ does not support %<__int%d%> for %qs",
9329 int_n_data[declspecs->int_n_idx].bitsize, name);
9332 /* Now process the modifiers that were specified
9333 and check for invalid combinations. */
9335 /* Long double is a special combination. */
9336 if (long_p && !longlong && TYPE_MAIN_VARIANT (type) == double_type_node)
9338 long_p = false;
9339 type = cp_build_qualified_type (long_double_type_node,
9340 cp_type_quals (type));
9343 /* Check all other uses of type modifiers. */
9345 if (unsigned_p || signed_p || long_p || short_p)
9347 int ok = 0;
9349 if ((signed_p || unsigned_p) && TREE_CODE (type) != INTEGER_TYPE)
9350 error ("%<signed%> or %<unsigned%> invalid for %qs", name);
9351 else if (signed_p && unsigned_p)
9352 error ("%<signed%> and %<unsigned%> specified together for %qs", name);
9353 else if (longlong && TREE_CODE (type) != INTEGER_TYPE)
9354 error ("%<long long%> invalid for %qs", name);
9355 else if (long_p && TREE_CODE (type) == REAL_TYPE)
9356 error ("%<long%> invalid for %qs", name);
9357 else if (short_p && TREE_CODE (type) == REAL_TYPE)
9358 error ("%<short%> invalid for %qs", name);
9359 else if ((long_p || short_p) && TREE_CODE (type) != INTEGER_TYPE)
9360 error ("%<long%> or %<short%> invalid for %qs", name);
9361 else if ((long_p || short_p || explicit_char || explicit_int) && explicit_intN)
9362 error ("%<long%>, %<int%>, %<short%>, or %<char%> invalid for %qs", name);
9363 else if ((long_p || short_p) && explicit_char)
9364 error ("%<long%> or %<short%> specified with char for %qs", name);
9365 else if (long_p && short_p)
9366 error ("%<long%> and %<short%> specified together for %qs", name);
9367 else if (type == char16_type_node || type == char32_type_node)
9369 if (signed_p || unsigned_p)
9370 error ("%<signed%> or %<unsigned%> invalid for %qs", name);
9371 else if (short_p || long_p)
9372 error ("%<short%> or %<long%> invalid for %qs", name);
9374 else
9376 ok = 1;
9377 if (!explicit_int && !defaulted_int && !explicit_char && !explicit_intN && pedantic)
9379 pedwarn (input_location, OPT_Wpedantic,
9380 "long, short, signed or unsigned used invalidly for %qs",
9381 name);
9382 if (flag_pedantic_errors)
9383 ok = 0;
9387 /* Discard the type modifiers if they are invalid. */
9388 if (! ok)
9390 unsigned_p = false;
9391 signed_p = false;
9392 long_p = false;
9393 short_p = false;
9394 longlong = 0;
9398 /* Decide whether an integer type is signed or not.
9399 Optionally treat bitfields as signed by default. */
9400 if (unsigned_p
9401 /* [class.bit]
9403 It is implementation-defined whether a plain (neither
9404 explicitly signed or unsigned) char, short, int, or long
9405 bit-field is signed or unsigned.
9407 Naturally, we extend this to long long as well. Note that
9408 this does not include wchar_t. */
9409 || (bitfield && !flag_signed_bitfields
9410 && !signed_p
9411 /* A typedef for plain `int' without `signed' can be
9412 controlled just like plain `int', but a typedef for
9413 `signed int' cannot be so controlled. */
9414 && !(typedef_decl
9415 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl))
9416 && TREE_CODE (type) == INTEGER_TYPE
9417 && !same_type_p (TYPE_MAIN_VARIANT (type), wchar_type_node)))
9419 if (explicit_intN)
9420 type = int_n_trees[declspecs->int_n_idx].unsigned_type;
9421 else if (longlong)
9422 type = long_long_unsigned_type_node;
9423 else if (long_p)
9424 type = long_unsigned_type_node;
9425 else if (short_p)
9426 type = short_unsigned_type_node;
9427 else if (type == char_type_node)
9428 type = unsigned_char_type_node;
9429 else if (typedef_decl)
9430 type = unsigned_type_for (type);
9431 else
9432 type = unsigned_type_node;
9434 else if (signed_p && type == char_type_node)
9435 type = signed_char_type_node;
9436 else if (explicit_intN)
9437 type = int_n_trees[declspecs->int_n_idx].signed_type;
9438 else if (longlong)
9439 type = long_long_integer_type_node;
9440 else if (long_p)
9441 type = long_integer_type_node;
9442 else if (short_p)
9443 type = short_integer_type_node;
9445 if (decl_spec_seq_has_spec_p (declspecs, ds_complex))
9447 if (TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
9448 error ("complex invalid for %qs", name);
9449 /* If a modifier is specified, the resulting complex is the complex
9450 form of TYPE. E.g, "complex short" is "complex short int". */
9451 else if (type == integer_type_node)
9452 type = complex_integer_type_node;
9453 else if (type == float_type_node)
9454 type = complex_float_type_node;
9455 else if (type == double_type_node)
9456 type = complex_double_type_node;
9457 else if (type == long_double_type_node)
9458 type = complex_long_double_type_node;
9459 else
9460 type = build_complex_type (type);
9463 type_quals = TYPE_UNQUALIFIED;
9464 if (decl_spec_seq_has_spec_p (declspecs, ds_const))
9465 type_quals |= TYPE_QUAL_CONST;
9466 if (decl_spec_seq_has_spec_p (declspecs, ds_volatile))
9467 type_quals |= TYPE_QUAL_VOLATILE;
9468 if (decl_spec_seq_has_spec_p (declspecs, ds_restrict))
9469 type_quals |= TYPE_QUAL_RESTRICT;
9470 if (sfk == sfk_conversion && type_quals != TYPE_UNQUALIFIED)
9471 error ("qualifiers are not allowed on declaration of %<operator %T%>",
9472 ctor_return_type);
9474 /* If we're using the injected-class-name to form a compound type or a
9475 declaration, replace it with the underlying class so we don't get
9476 redundant typedefs in the debug output. But if we are returning the
9477 type unchanged, leave it alone so that it's available to
9478 maybe_get_template_decl_from_type_decl. */
9479 if (CLASS_TYPE_P (type)
9480 && DECL_SELF_REFERENCE_P (TYPE_NAME (type))
9481 && type == TREE_TYPE (TYPE_NAME (type))
9482 && (declarator || type_quals))
9483 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
9485 type_quals |= cp_type_quals (type);
9486 type = cp_build_qualified_type_real
9487 (type, type_quals, ((((typedef_decl && !DECL_ARTIFICIAL (typedef_decl))
9488 || declspecs->decltype_p)
9489 ? tf_ignore_bad_quals : 0) | tf_warning_or_error));
9490 /* We might have ignored or rejected some of the qualifiers. */
9491 type_quals = cp_type_quals (type);
9493 staticp = 0;
9494 inlinep = decl_spec_seq_has_spec_p (declspecs, ds_inline);
9495 virtualp = decl_spec_seq_has_spec_p (declspecs, ds_virtual);
9496 explicitp = decl_spec_seq_has_spec_p (declspecs, ds_explicit);
9498 storage_class = declspecs->storage_class;
9499 if (storage_class == sc_static)
9500 staticp = 1 + (decl_context == FIELD);
9502 if (virtualp && staticp == 2)
9504 error ("member %qD cannot be declared both virtual and static", dname);
9505 storage_class = sc_none;
9506 staticp = 0;
9508 friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
9510 /* Issue errors about use of storage classes for parameters. */
9511 if (decl_context == PARM)
9513 if (typedef_p)
9515 error ("typedef declaration invalid in parameter declaration");
9516 return error_mark_node;
9518 else if (template_parm_flag && storage_class != sc_none)
9520 error ("storage class specified for template parameter %qs", name);
9521 return error_mark_node;
9523 else if (storage_class == sc_static
9524 || storage_class == sc_extern
9525 || thread_p)
9526 error ("storage class specifiers invalid in parameter declarations");
9528 /* Function parameters cannot be constexpr. If we saw one, moan
9529 and pretend it wasn't there. */
9530 if (constexpr_p)
9532 error ("a parameter cannot be declared %<constexpr%>");
9533 constexpr_p = 0;
9537 /* Give error if `virtual' is used outside of class declaration. */
9538 if (virtualp
9539 && (current_class_name == NULL_TREE || decl_context != FIELD))
9541 error ("%<virtual%> outside class declaration");
9542 virtualp = 0;
9545 /* Static anonymous unions are dealt with here. */
9546 if (staticp && decl_context == TYPENAME
9547 && declspecs->type
9548 && ANON_AGGR_TYPE_P (declspecs->type))
9549 decl_context = FIELD;
9551 /* Warn about storage classes that are invalid for certain
9552 kinds of declarations (parameters, typenames, etc.). */
9553 if (thread_p
9554 && ((storage_class
9555 && storage_class != sc_extern
9556 && storage_class != sc_static)
9557 || typedef_p))
9559 error ("multiple storage classes in declaration of %qs", name);
9560 thread_p = false;
9562 if (decl_context != NORMAL
9563 && ((storage_class != sc_none
9564 && storage_class != sc_mutable)
9565 || thread_p))
9567 if ((decl_context == PARM || decl_context == CATCHPARM)
9568 && (storage_class == sc_register
9569 || storage_class == sc_auto))
9571 else if (typedef_p)
9573 else if (decl_context == FIELD
9574 /* C++ allows static class elements. */
9575 && storage_class == sc_static)
9576 /* C++ also allows inlines and signed and unsigned elements,
9577 but in those cases we don't come in here. */
9579 else
9581 if (decl_context == FIELD)
9582 error ("storage class specified for %qs", name);
9583 else
9585 if (decl_context == PARM || decl_context == CATCHPARM)
9586 error ("storage class specified for parameter %qs", name);
9587 else
9588 error ("storage class specified for typename");
9590 if (storage_class == sc_register
9591 || storage_class == sc_auto
9592 || storage_class == sc_extern
9593 || thread_p)
9594 storage_class = sc_none;
9597 else if (storage_class == sc_extern && funcdef_flag
9598 && ! toplevel_bindings_p ())
9599 error ("nested function %qs declared %<extern%>", name);
9600 else if (toplevel_bindings_p ())
9602 if (storage_class == sc_auto)
9603 error ("top-level declaration of %qs specifies %<auto%>", name);
9605 else if (thread_p
9606 && storage_class != sc_extern
9607 && storage_class != sc_static)
9609 if (declspecs->gnu_thread_keyword_p)
9610 pedwarn (input_location, 0, "function-scope %qs implicitly auto and "
9611 "declared %<__thread%>", name);
9613 /* When thread_local is applied to a variable of block scope the
9614 storage-class-specifier static is implied if it does not appear
9615 explicitly. */
9616 storage_class = declspecs->storage_class = sc_static;
9617 staticp = 1;
9620 if (storage_class && friendp)
9622 error ("storage class specifiers invalid in friend function declarations");
9623 storage_class = sc_none;
9624 staticp = 0;
9627 if (!id_declarator)
9628 unqualified_id = NULL_TREE;
9629 else
9631 unqualified_id = id_declarator->u.id.unqualified_name;
9632 switch (TREE_CODE (unqualified_id))
9634 case BIT_NOT_EXPR:
9635 unqualified_id = TREE_OPERAND (unqualified_id, 0);
9636 if (TYPE_P (unqualified_id))
9637 unqualified_id = constructor_name (unqualified_id);
9638 break;
9640 case IDENTIFIER_NODE:
9641 case TEMPLATE_ID_EXPR:
9642 break;
9644 default:
9645 gcc_unreachable ();
9649 if (declspecs->std_attributes)
9651 /* Apply the c++11 attributes to the type preceding them. */
9652 input_location = declspecs->locations[ds_std_attribute];
9653 decl_attributes (&type, declspecs->std_attributes, 0);
9654 input_location = saved_loc;
9657 /* Determine the type of the entity declared by recurring on the
9658 declarator. */
9659 for (; declarator; declarator = declarator->declarator)
9661 const cp_declarator *inner_declarator;
9662 tree attrs;
9664 if (type == error_mark_node)
9665 return error_mark_node;
9667 attrs = declarator->attributes;
9668 if (attrs)
9670 int attr_flags;
9672 attr_flags = 0;
9673 if (declarator == NULL || declarator->kind == cdk_id)
9674 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
9675 if (declarator->kind == cdk_function)
9676 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
9677 if (declarator->kind == cdk_array)
9678 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
9679 returned_attrs = decl_attributes (&type,
9680 chainon (returned_attrs, attrs),
9681 attr_flags);
9684 if (declarator->kind == cdk_id)
9685 break;
9687 inner_declarator = declarator->declarator;
9689 switch (declarator->kind)
9691 case cdk_array:
9692 type = create_array_type_for_decl (dname, type,
9693 declarator->u.array.bounds);
9694 if (declarator->std_attributes)
9695 /* [dcl.array]/1:
9697 The optional attribute-specifier-seq appertains to the
9698 array. */
9699 returned_attrs = chainon (returned_attrs,
9700 declarator->std_attributes);
9701 break;
9703 case cdk_function:
9705 tree arg_types;
9706 int funcdecl_p;
9708 /* Declaring a function type.
9709 Make sure we have a valid type for the function to return. */
9711 if (type_quals != TYPE_UNQUALIFIED)
9713 if (SCALAR_TYPE_P (type) || VOID_TYPE_P (type))
9714 warning (OPT_Wignored_qualifiers,
9715 "type qualifiers ignored on function return type");
9716 /* We now know that the TYPE_QUALS don't apply to the
9717 decl, but to its return type. */
9718 type_quals = TYPE_UNQUALIFIED;
9720 errmsg = targetm.invalid_return_type (type);
9721 if (errmsg)
9723 error (errmsg);
9724 type = integer_type_node;
9727 /* Error about some types functions can't return. */
9729 if (TREE_CODE (type) == FUNCTION_TYPE)
9731 error ("%qs declared as function returning a function", name);
9732 return error_mark_node;
9734 if (TREE_CODE (type) == ARRAY_TYPE)
9736 error ("%qs declared as function returning an array", name);
9737 return error_mark_node;
9740 input_location = declspecs->locations[ds_type_spec];
9741 abstract_virtuals_error (ACU_RETURN, type);
9742 input_location = saved_loc;
9744 /* Pick up type qualifiers which should be applied to `this'. */
9745 memfn_quals = declarator->u.function.qualifiers;
9746 /* Pick up virt-specifiers. */
9747 virt_specifiers = declarator->u.function.virt_specifiers;
9748 /* And ref-qualifier, too */
9749 rqual = declarator->u.function.ref_qualifier;
9750 /* Pick up the exception specifications. */
9751 raises = declarator->u.function.exception_specification;
9752 /* If the exception-specification is ill-formed, let's pretend
9753 there wasn't one. */
9754 if (raises == error_mark_node)
9755 raises = NULL_TREE;
9757 /* Say it's a definition only for the CALL_EXPR
9758 closest to the identifier. */
9759 funcdecl_p = inner_declarator && inner_declarator->kind == cdk_id;
9761 /* Handle a late-specified return type. */
9762 if (funcdecl_p)
9764 if (type_uses_auto (type))
9766 if (!declarator->u.function.late_return_type)
9768 if (current_class_type
9769 && LAMBDA_TYPE_P (current_class_type))
9770 /* OK for C++11 lambdas. */;
9771 else if (cxx_dialect < cxx14)
9773 error ("%qs function uses "
9774 "%<auto%> type specifier without trailing "
9775 "return type", name);
9776 inform (input_location, "deduced return type "
9777 "only available with -std=c++14 or "
9778 "-std=gnu++14");
9780 else if (virtualp)
9782 error ("virtual function cannot "
9783 "have deduced return type");
9784 virtualp = false;
9787 else if (!is_auto (type))
9789 error ("%qs function with trailing return type has"
9790 " %qT as its type rather than plain %<auto%>",
9791 name, type);
9792 return error_mark_node;
9795 else if (declarator->u.function.late_return_type)
9797 if (cxx_dialect < cxx11)
9798 /* Not using maybe_warn_cpp0x because this should
9799 always be an error. */
9800 error ("trailing return type only available with "
9801 "-std=c++11 or -std=gnu++11");
9802 else
9803 error ("%qs function with trailing return type not "
9804 "declared with %<auto%> type specifier", name);
9805 return error_mark_node;
9808 type = splice_late_return_type
9809 (type, declarator->u.function.late_return_type);
9810 if (type == error_mark_node)
9811 return error_mark_node;
9813 if (declarator->u.function.late_return_type)
9814 late_return_type_p = true;
9816 if (ctype == NULL_TREE
9817 && decl_context == FIELD
9818 && funcdecl_p
9819 && friendp == 0)
9820 ctype = current_class_type;
9822 if (ctype && (sfk == sfk_constructor
9823 || sfk == sfk_destructor))
9825 /* We are within a class's scope. If our declarator name
9826 is the same as the class name, and we are defining
9827 a function, then it is a constructor/destructor, and
9828 therefore returns a void type. */
9830 /* ISO C++ 12.4/2. A destructor may not be declared
9831 const or volatile. A destructor may not be static.
9832 A destructor may not be declared with ref-qualifier.
9834 ISO C++ 12.1. A constructor may not be declared
9835 const or volatile. A constructor may not be
9836 virtual. A constructor may not be static.
9837 A constructor may not be declared with ref-qualifier. */
9838 if (staticp == 2)
9839 error ((flags == DTOR_FLAG)
9840 ? G_("destructor cannot be static member function")
9841 : G_("constructor cannot be static member function"));
9842 if (memfn_quals)
9844 error ((flags == DTOR_FLAG)
9845 ? G_("destructors may not be cv-qualified")
9846 : G_("constructors may not be cv-qualified"));
9847 memfn_quals = TYPE_UNQUALIFIED;
9850 if (rqual)
9852 maybe_warn_cpp0x (CPP0X_REF_QUALIFIER);
9853 error ((flags == DTOR_FLAG)
9854 ? "destructors may not be ref-qualified"
9855 : "constructors may not be ref-qualified");
9856 rqual = REF_QUAL_NONE;
9859 if (decl_context == FIELD
9860 && !member_function_or_else (ctype,
9861 current_class_type,
9862 flags))
9863 return error_mark_node;
9865 if (flags != DTOR_FLAG)
9867 /* It's a constructor. */
9868 if (explicitp == 1)
9869 explicitp = 2;
9870 if (virtualp)
9872 permerror (input_location, "constructors cannot be declared virtual");
9873 virtualp = 0;
9875 if (decl_context == FIELD
9876 && sfk != sfk_constructor)
9877 return error_mark_node;
9879 if (decl_context == FIELD)
9880 staticp = 0;
9882 else if (friendp)
9884 if (virtualp)
9886 /* Cannot be both friend and virtual. */
9887 error ("virtual functions cannot be friends");
9888 friendp = 0;
9890 if (decl_context == NORMAL)
9891 error ("friend declaration not in class definition");
9892 if (current_function_decl && funcdef_flag)
9893 error ("can%'t define friend function %qs in a local "
9894 "class definition",
9895 name);
9897 else if (ctype && sfk == sfk_conversion)
9899 if (explicitp == 1)
9901 maybe_warn_cpp0x (CPP0X_EXPLICIT_CONVERSION);
9902 explicitp = 2;
9906 arg_types = grokparms (declarator->u.function.parameters,
9907 &parms);
9909 if (inner_declarator
9910 && inner_declarator->kind == cdk_id
9911 && inner_declarator->u.id.sfk == sfk_destructor
9912 && arg_types != void_list_node)
9914 error ("destructors may not have parameters");
9915 arg_types = void_list_node;
9916 parms = NULL_TREE;
9919 type = build_function_type (type, arg_types);
9920 if (declarator->std_attributes)
9921 /* [dcl.fct]/2:
9923 The optional attribute-specifier-seq appertains to
9924 the function type. */
9925 decl_attributes (&type, declarator->std_attributes,
9928 break;
9930 case cdk_pointer:
9931 case cdk_reference:
9932 case cdk_ptrmem:
9933 /* Filter out pointers-to-references and references-to-references.
9934 We can get these if a TYPE_DECL is used. */
9936 if (TREE_CODE (type) == REFERENCE_TYPE)
9938 if (declarator->kind != cdk_reference)
9940 error ("cannot declare pointer to %q#T", type);
9941 type = TREE_TYPE (type);
9944 /* In C++0x, we allow reference to reference declarations
9945 that occur indirectly through typedefs [7.1.3/8 dcl.typedef]
9946 and template type arguments [14.3.1/4 temp.arg.type]. The
9947 check for direct reference to reference declarations, which
9948 are still forbidden, occurs below. Reasoning behind the change
9949 can be found in DR106, DR540, and the rvalue reference
9950 proposals. */
9951 else if (cxx_dialect == cxx98)
9953 error ("cannot declare reference to %q#T", type);
9954 type = TREE_TYPE (type);
9957 else if (VOID_TYPE_P (type))
9959 if (declarator->kind == cdk_reference)
9960 error ("cannot declare reference to %q#T", type);
9961 else if (declarator->kind == cdk_ptrmem)
9962 error ("cannot declare pointer to %q#T member", type);
9965 /* We now know that the TYPE_QUALS don't apply to the decl,
9966 but to the target of the pointer. */
9967 type_quals = TYPE_UNQUALIFIED;
9969 /* This code used to handle METHOD_TYPE, but I don't think it's
9970 possible to get it here anymore. */
9971 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
9972 if (declarator->kind == cdk_ptrmem
9973 && TREE_CODE (type) == FUNCTION_TYPE)
9975 memfn_quals |= type_memfn_quals (type);
9976 type = build_memfn_type (type,
9977 declarator->u.pointer.class_type,
9978 memfn_quals,
9979 rqual);
9980 if (type == error_mark_node)
9981 return error_mark_node;
9983 rqual = REF_QUAL_NONE;
9984 memfn_quals = TYPE_UNQUALIFIED;
9987 if (TREE_CODE (type) == FUNCTION_TYPE
9988 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
9989 || type_memfn_rqual (type) != REF_QUAL_NONE))
9990 error (declarator->kind == cdk_reference
9991 ? G_("cannot declare reference to qualified function type %qT")
9992 : G_("cannot declare pointer to qualified function type %qT"),
9993 type);
9995 /* When the pointed-to type involves components of variable size,
9996 care must be taken to ensure that the size evaluation code is
9997 emitted early enough to dominate all the possible later uses
9998 and late enough for the variables on which it depends to have
9999 been assigned.
10001 This is expected to happen automatically when the pointed-to
10002 type has a name/declaration of it's own, but special attention
10003 is required if the type is anonymous.
10005 We handle the NORMAL and FIELD contexts here by inserting a
10006 dummy statement that just evaluates the size at a safe point
10007 and ensures it is not deferred until e.g. within a deeper
10008 conditional context (c++/43555).
10010 We expect nothing to be needed here for PARM or TYPENAME.
10011 Evaluating the size at this point for TYPENAME would
10012 actually be incorrect, as we might be in the middle of an
10013 expression with side effects on the pointed-to type size
10014 "arguments" prior to the pointer declaration point and the
10015 size evaluation could end up prior to the side effects. */
10017 if (!TYPE_NAME (type)
10018 && (decl_context == NORMAL || decl_context == FIELD)
10019 && at_function_scope_p ()
10020 && variably_modified_type_p (type, NULL_TREE))
10021 /* Force evaluation of the SAVE_EXPR. */
10022 finish_expr_stmt (TYPE_SIZE (type));
10024 if (declarator->kind == cdk_reference)
10026 /* In C++0x, the type we are creating a reference to might be
10027 a typedef which is itself a reference type. In that case,
10028 we follow the reference collapsing rules in
10029 [7.1.3/8 dcl.typedef] to create the final reference type:
10031 "If a typedef TD names a type that is a reference to a type
10032 T, an attempt to create the type 'lvalue reference to cv TD'
10033 creates the type 'lvalue reference to T,' while an attempt
10034 to create the type "rvalue reference to cv TD' creates the
10035 type TD."
10037 if (VOID_TYPE_P (type))
10038 /* We already gave an error. */;
10039 else if (TREE_CODE (type) == REFERENCE_TYPE)
10041 if (declarator->u.reference.rvalue_ref)
10042 /* Leave type alone. */;
10043 else
10044 type = cp_build_reference_type (TREE_TYPE (type), false);
10046 else
10047 type = cp_build_reference_type
10048 (type, declarator->u.reference.rvalue_ref);
10050 /* In C++0x, we need this check for direct reference to
10051 reference declarations, which are forbidden by
10052 [8.3.2/5 dcl.ref]. Reference to reference declarations
10053 are only allowed indirectly through typedefs and template
10054 type arguments. Example:
10056 void foo(int & &); // invalid ref-to-ref decl
10058 typedef int & int_ref;
10059 void foo(int_ref &); // valid ref-to-ref decl
10061 if (inner_declarator && inner_declarator->kind == cdk_reference)
10062 error ("cannot declare reference to %q#T, which is not "
10063 "a typedef or a template type argument", type);
10065 else if (TREE_CODE (type) == METHOD_TYPE)
10066 type = build_ptrmemfunc_type (build_pointer_type (type));
10067 else if (declarator->kind == cdk_ptrmem)
10069 gcc_assert (TREE_CODE (declarator->u.pointer.class_type)
10070 != NAMESPACE_DECL);
10071 if (declarator->u.pointer.class_type == error_mark_node)
10072 /* We will already have complained. */
10073 type = error_mark_node;
10074 else
10075 type = build_ptrmem_type (declarator->u.pointer.class_type,
10076 type);
10078 else
10079 type = build_pointer_type (type);
10081 /* Process a list of type modifier keywords (such as
10082 const or volatile) that were given inside the `*' or `&'. */
10084 if (declarator->u.pointer.qualifiers)
10086 type
10087 = cp_build_qualified_type (type,
10088 declarator->u.pointer.qualifiers);
10089 type_quals = cp_type_quals (type);
10092 /* Apply C++11 attributes to the pointer, and not to the
10093 type pointed to. This is unlike what is done for GNU
10094 attributes above. It is to comply with [dcl.ptr]/1:
10096 [the optional attribute-specifier-seq (7.6.1) appertains
10097 to the pointer and not to the object pointed to]. */
10098 if (declarator->std_attributes)
10099 decl_attributes (&type, declarator->std_attributes,
10102 ctype = NULL_TREE;
10103 break;
10105 case cdk_error:
10106 break;
10108 default:
10109 gcc_unreachable ();
10113 /* A `constexpr' specifier used in an object declaration declares
10114 the object as `const'. */
10115 if (constexpr_p && innermost_code != cdk_function)
10117 if (type_quals & TYPE_QUAL_VOLATILE)
10118 error ("both %<volatile%> and %<constexpr%> cannot be used here");
10119 if (TREE_CODE (type) != REFERENCE_TYPE)
10121 type_quals |= TYPE_QUAL_CONST;
10122 type = cp_build_qualified_type (type, type_quals);
10126 if (unqualified_id && TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR
10127 && TREE_CODE (type) != FUNCTION_TYPE
10128 && TREE_CODE (type) != METHOD_TYPE
10129 && !variable_template_p (TREE_OPERAND (unqualified_id, 0)))
10131 error ("template-id %qD used as a declarator",
10132 unqualified_id);
10133 unqualified_id = dname;
10136 /* If TYPE is a FUNCTION_TYPE, but the function name was explicitly
10137 qualified with a class-name, turn it into a METHOD_TYPE, unless
10138 we know that the function is static. We take advantage of this
10139 opportunity to do other processing that pertains to entities
10140 explicitly declared to be class members. Note that if DECLARATOR
10141 is non-NULL, we know it is a cdk_id declarator; otherwise, we
10142 would not have exited the loop above. */
10143 if (declarator
10144 && declarator->u.id.qualifying_scope
10145 && MAYBE_CLASS_TYPE_P (declarator->u.id.qualifying_scope))
10147 ctype = declarator->u.id.qualifying_scope;
10148 ctype = TYPE_MAIN_VARIANT (ctype);
10149 template_count = num_template_headers_for_class (ctype);
10151 if (ctype == current_class_type)
10153 if (friendp)
10155 permerror (input_location, "member functions are implicitly friends of their class");
10156 friendp = 0;
10158 else
10159 permerror (declarator->id_loc,
10160 "extra qualification %<%T::%> on member %qs",
10161 ctype, name);
10163 else if (/* If the qualifying type is already complete, then we
10164 can skip the following checks. */
10165 !COMPLETE_TYPE_P (ctype)
10166 && (/* If the function is being defined, then
10167 qualifying type must certainly be complete. */
10168 funcdef_flag
10169 /* A friend declaration of "T::f" is OK, even if
10170 "T" is a template parameter. But, if this
10171 function is not a friend, the qualifying type
10172 must be a class. */
10173 || (!friendp && !CLASS_TYPE_P (ctype))
10174 /* For a declaration, the type need not be
10175 complete, if either it is dependent (since there
10176 is no meaningful definition of complete in that
10177 case) or the qualifying class is currently being
10178 defined. */
10179 || !(dependent_type_p (ctype)
10180 || currently_open_class (ctype)))
10181 /* Check that the qualifying type is complete. */
10182 && !complete_type_or_else (ctype, NULL_TREE))
10183 return error_mark_node;
10184 else if (TREE_CODE (type) == FUNCTION_TYPE)
10186 if (current_class_type
10187 && (!friendp || funcdef_flag))
10189 error (funcdef_flag
10190 ? G_("cannot define member function %<%T::%s%> "
10191 "within %<%T%>")
10192 : G_("cannot declare member function %<%T::%s%> "
10193 "within %<%T%>"),
10194 ctype, name, current_class_type);
10195 return error_mark_node;
10198 else if (typedef_p && current_class_type)
10200 error ("cannot declare member %<%T::%s%> within %qT",
10201 ctype, name, current_class_type);
10202 return error_mark_node;
10206 if (ctype == NULL_TREE && decl_context == FIELD && friendp == 0)
10207 ctype = current_class_type;
10209 /* Now TYPE has the actual type. */
10211 if (returned_attrs)
10213 if (attrlist)
10214 *attrlist = chainon (returned_attrs, *attrlist);
10215 else
10216 attrlist = &returned_attrs;
10219 if (declarator
10220 && declarator->kind == cdk_id
10221 && declarator->std_attributes)
10222 /* [dcl.meaning]/1: The optional attribute-specifier-seq following
10223 a declarator-id appertains to the entity that is declared. */
10224 *attrlist = chainon (*attrlist, declarator->std_attributes);
10226 /* Handle parameter packs. */
10227 if (parameter_pack_p)
10229 if (decl_context == PARM)
10230 /* Turn the type into a pack expansion.*/
10231 type = make_pack_expansion (type);
10232 else
10233 error ("non-parameter %qs cannot be a parameter pack", name);
10236 /* Did array size calculations overflow or does the array cover more
10237 than half of the address-space? */
10238 if (TREE_CODE (type) == ARRAY_TYPE
10239 && COMPLETE_TYPE_P (type)
10240 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
10241 && ! valid_constant_size_p (TYPE_SIZE_UNIT (type)))
10243 error ("size of array %qs is too large", name);
10244 /* If we proceed with the array type as it is, we'll eventually
10245 crash in tree_to_[su]hwi(). */
10246 type = error_mark_node;
10249 if ((decl_context == FIELD || decl_context == PARM)
10250 && !processing_template_decl
10251 && variably_modified_type_p (type, NULL_TREE))
10253 if (decl_context == FIELD)
10254 error ("data member may not have variably modified type %qT", type);
10255 else
10256 error ("parameter may not have variably modified type %qT", type);
10257 type = error_mark_node;
10260 if (explicitp == 1 || (explicitp && friendp))
10262 /* [dcl.fct.spec] (C++11) The explicit specifier shall be used only
10263 in the declaration of a constructor or conversion function within
10264 a class definition. */
10265 if (!current_class_type)
10266 error ("%<explicit%> outside class declaration");
10267 else if (friendp)
10268 error ("%<explicit%> in friend declaration");
10269 else
10270 error ("only declarations of constructors and conversion operators "
10271 "can be %<explicit%>");
10272 explicitp = 0;
10275 if (storage_class == sc_mutable)
10277 if (decl_context != FIELD || friendp)
10279 error ("non-member %qs cannot be declared %<mutable%>", name);
10280 storage_class = sc_none;
10282 else if (decl_context == TYPENAME || typedef_p)
10284 error ("non-object member %qs cannot be declared %<mutable%>", name);
10285 storage_class = sc_none;
10287 else if (TREE_CODE (type) == FUNCTION_TYPE
10288 || TREE_CODE (type) == METHOD_TYPE)
10290 error ("function %qs cannot be declared %<mutable%>", name);
10291 storage_class = sc_none;
10293 else if (staticp)
10295 error ("static %qs cannot be declared %<mutable%>", name);
10296 storage_class = sc_none;
10298 else if (type_quals & TYPE_QUAL_CONST)
10300 error ("const %qs cannot be declared %<mutable%>", name);
10301 storage_class = sc_none;
10303 else if (TREE_CODE (type) == REFERENCE_TYPE)
10305 permerror (input_location, "reference %qs cannot be declared "
10306 "%<mutable%>", name);
10307 storage_class = sc_none;
10311 /* If this is declaring a typedef name, return a TYPE_DECL. */
10312 if (typedef_p && decl_context != TYPENAME)
10314 tree decl;
10316 /* Note that the grammar rejects storage classes
10317 in typenames, fields or parameters. */
10318 if (current_lang_name == lang_name_java)
10319 TYPE_FOR_JAVA (type) = 1;
10321 /* This declaration:
10323 typedef void f(int) const;
10325 declares a function type which is not a member of any
10326 particular class, but which is cv-qualified; for
10327 example "f S::*" declares a pointer to a const-qualified
10328 member function of S. We record the cv-qualification in the
10329 function type. */
10330 if ((rqual || memfn_quals) && TREE_CODE (type) == FUNCTION_TYPE)
10332 type = apply_memfn_quals (type, memfn_quals, rqual);
10334 /* We have now dealt with these qualifiers. */
10335 memfn_quals = TYPE_UNQUALIFIED;
10336 rqual = REF_QUAL_NONE;
10339 if (type_uses_auto (type))
10341 error ("typedef declared %<auto%>");
10342 type = error_mark_node;
10345 if (decl_context == FIELD)
10346 decl = build_lang_decl (TYPE_DECL, unqualified_id, type);
10347 else
10348 decl = build_decl (input_location, TYPE_DECL, unqualified_id, type);
10349 if (id_declarator && declarator->u.id.qualifying_scope) {
10350 error_at (DECL_SOURCE_LOCATION (decl),
10351 "typedef name may not be a nested-name-specifier");
10352 TREE_TYPE (decl) = error_mark_node;
10355 if (decl_context != FIELD)
10357 if (!current_function_decl)
10358 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
10359 else if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (current_function_decl)
10360 || (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P
10361 (current_function_decl)))
10362 /* The TYPE_DECL is "abstract" because there will be
10363 clones of this constructor/destructor, and there will
10364 be copies of this TYPE_DECL generated in those
10365 clones. The decloning optimization (for space) may
10366 revert this subsequently if it determines that
10367 the clones should share a common implementation. */
10368 DECL_ABSTRACT_P (decl) = true;
10370 else if (current_class_type
10371 && constructor_name_p (unqualified_id, current_class_type))
10372 permerror (input_location, "ISO C++ forbids nested type %qD with same name "
10373 "as enclosing class",
10374 unqualified_id);
10376 /* If the user declares "typedef struct {...} foo" then the
10377 struct will have an anonymous name. Fill that name in now.
10378 Nothing can refer to it, so nothing needs know about the name
10379 change. */
10380 if (type != error_mark_node
10381 && unqualified_id
10382 && TYPE_NAME (type)
10383 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10384 && TYPE_ANONYMOUS_P (type)
10385 && declspecs->type_definition_p
10386 && attributes_naming_typedef_ok (*attrlist)
10387 && cp_type_quals (type) == TYPE_UNQUALIFIED)
10389 tree t;
10391 /* Replace the anonymous name with the real name everywhere. */
10392 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
10394 if (ANON_AGGRNAME_P (TYPE_IDENTIFIER (t)))
10395 /* We do not rename the debug info representing the
10396 anonymous tagged type because the standard says in
10397 [dcl.typedef] that the naming applies only for
10398 linkage purposes. */
10399 /*debug_hooks->set_name (t, decl);*/
10400 TYPE_NAME (t) = decl;
10403 if (TYPE_LANG_SPECIFIC (type))
10404 TYPE_WAS_ANONYMOUS (type) = 1;
10406 /* If this is a typedef within a template class, the nested
10407 type is a (non-primary) template. The name for the
10408 template needs updating as well. */
10409 if (TYPE_LANG_SPECIFIC (type) && CLASSTYPE_TEMPLATE_INFO (type))
10410 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type))
10411 = TYPE_IDENTIFIER (type);
10413 /* Adjust linkage now that we aren't anonymous anymore. */
10414 reset_type_linkage (type);
10416 /* FIXME remangle member functions; member functions of a
10417 type with external linkage have external linkage. */
10420 if (signed_p
10421 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
10422 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
10424 bad_specifiers (decl, BSP_TYPE, virtualp,
10425 memfn_quals != TYPE_UNQUALIFIED,
10426 inlinep, friendp, raises != NULL_TREE);
10428 if (decl_spec_seq_has_spec_p (declspecs, ds_alias))
10429 /* Acknowledge that this was written:
10430 `using analias = atype;'. */
10431 TYPE_DECL_ALIAS_P (decl) = 1;
10433 return decl;
10436 /* Detect the case of an array type of unspecified size
10437 which came, as such, direct from a typedef name.
10438 We must copy the type, so that the array's domain can be
10439 individually set by the object's initializer. */
10441 if (type && typedef_type
10442 && TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
10443 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
10444 type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
10446 /* Detect where we're using a typedef of function type to declare a
10447 function. PARMS will not be set, so we must create it now. */
10449 if (type == typedef_type && TREE_CODE (type) == FUNCTION_TYPE)
10451 tree decls = NULL_TREE;
10452 tree args;
10454 for (args = TYPE_ARG_TYPES (type);
10455 args && args != void_list_node;
10456 args = TREE_CHAIN (args))
10458 tree decl = cp_build_parm_decl (NULL_TREE, TREE_VALUE (args));
10460 DECL_CHAIN (decl) = decls;
10461 decls = decl;
10464 parms = nreverse (decls);
10466 if (decl_context != TYPENAME)
10468 /* The qualifiers on the function type become the qualifiers on
10469 the non-static member function. */
10470 memfn_quals |= type_memfn_quals (type);
10471 rqual = type_memfn_rqual (type);
10472 type_quals = TYPE_UNQUALIFIED;
10476 /* If this is a type name (such as, in a cast or sizeof),
10477 compute the type and return it now. */
10479 if (decl_context == TYPENAME)
10481 /* Note that the grammar rejects storage classes
10482 in typenames, fields or parameters. */
10483 if (type_quals != TYPE_UNQUALIFIED)
10484 type_quals = TYPE_UNQUALIFIED;
10486 /* Special case: "friend class foo" looks like a TYPENAME context. */
10487 if (friendp)
10489 if (type_quals != TYPE_UNQUALIFIED)
10491 error ("type qualifiers specified for friend class declaration");
10492 type_quals = TYPE_UNQUALIFIED;
10494 if (inlinep)
10496 error ("%<inline%> specified for friend class declaration");
10497 inlinep = 0;
10500 if (!current_aggr)
10502 /* Don't allow friend declaration without a class-key. */
10503 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
10504 permerror (input_location, "template parameters cannot be friends");
10505 else if (TREE_CODE (type) == TYPENAME_TYPE)
10506 permerror (input_location, "friend declaration requires class-key, "
10507 "i.e. %<friend class %T::%D%>",
10508 TYPE_CONTEXT (type), TYPENAME_TYPE_FULLNAME (type));
10509 else
10510 permerror (input_location, "friend declaration requires class-key, "
10511 "i.e. %<friend %#T%>",
10512 type);
10515 /* Only try to do this stuff if we didn't already give up. */
10516 if (type != integer_type_node)
10518 /* A friendly class? */
10519 if (current_class_type)
10520 make_friend_class (current_class_type, TYPE_MAIN_VARIANT (type),
10521 /*complain=*/true);
10522 else
10523 error ("trying to make class %qT a friend of global scope",
10524 type);
10526 type = void_type_node;
10529 else if (memfn_quals || rqual)
10531 if (ctype == NULL_TREE
10532 && TREE_CODE (type) == METHOD_TYPE)
10533 ctype = TYPE_METHOD_BASETYPE (type);
10535 if (ctype)
10536 type = build_memfn_type (type, ctype, memfn_quals, rqual);
10537 /* Core issue #547: need to allow this in template type args.
10538 Allow it in general in C++11 for alias-declarations. */
10539 else if ((template_type_arg || cxx_dialect >= cxx11)
10540 && TREE_CODE (type) == FUNCTION_TYPE)
10541 type = apply_memfn_quals (type, memfn_quals, rqual);
10542 else
10543 error ("invalid qualifiers on non-member function type");
10546 return type;
10548 else if (unqualified_id == NULL_TREE && decl_context != PARM
10549 && decl_context != CATCHPARM
10550 && TREE_CODE (type) != UNION_TYPE
10551 && ! bitfield)
10553 error ("abstract declarator %qT used as declaration", type);
10554 return error_mark_node;
10557 /* Only functions may be declared using an operator-function-id. */
10558 if (unqualified_id
10559 && IDENTIFIER_OPNAME_P (unqualified_id)
10560 && TREE_CODE (type) != FUNCTION_TYPE
10561 && TREE_CODE (type) != METHOD_TYPE)
10563 error ("declaration of %qD as non-function", unqualified_id);
10564 return error_mark_node;
10567 /* We don't check parameter types here because we can emit a better
10568 error message later. */
10569 if (decl_context != PARM)
10571 type = check_var_type (unqualified_id, type);
10572 if (type == error_mark_node)
10573 return error_mark_node;
10576 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
10577 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
10579 if (decl_context == PARM || decl_context == CATCHPARM)
10581 if (ctype || in_namespace)
10582 error ("cannot use %<::%> in parameter declaration");
10584 if (type_uses_auto (type))
10586 if (cxx_dialect >= cxx14)
10587 error ("%<auto%> parameter not permitted in this context");
10588 else
10589 error ("parameter declared %<auto%>");
10590 type = error_mark_node;
10593 /* A parameter declared as an array of T is really a pointer to T.
10594 One declared as a function is really a pointer to a function.
10595 One declared as a member is really a pointer to member. */
10597 if (TREE_CODE (type) == ARRAY_TYPE)
10599 /* Transfer const-ness of array into that of type pointed to. */
10600 type = build_pointer_type (TREE_TYPE (type));
10601 type_quals = TYPE_UNQUALIFIED;
10602 array_parameter_p = true;
10604 else if (TREE_CODE (type) == FUNCTION_TYPE)
10605 type = build_pointer_type (type);
10608 if (ctype && TREE_CODE (type) == FUNCTION_TYPE && staticp < 2
10609 && !NEW_DELETE_OPNAME_P (unqualified_id))
10611 cp_cv_quals real_quals = memfn_quals;
10612 if (cxx_dialect < cxx14 && constexpr_p
10613 && sfk != sfk_constructor && sfk != sfk_destructor)
10614 real_quals |= TYPE_QUAL_CONST;
10615 type = build_memfn_type (type, ctype, real_quals, rqual);
10619 tree decl;
10621 if (decl_context == PARM)
10623 decl = cp_build_parm_decl (unqualified_id, type);
10624 DECL_ARRAY_PARAMETER_P (decl) = array_parameter_p;
10626 bad_specifiers (decl, BSP_PARM, virtualp,
10627 memfn_quals != TYPE_UNQUALIFIED,
10628 inlinep, friendp, raises != NULL_TREE);
10630 else if (decl_context == FIELD)
10632 if (!staticp && TREE_CODE (type) != METHOD_TYPE
10633 && type_uses_auto (type))
10635 error ("non-static data member declared %<auto%>");
10636 type = error_mark_node;
10639 /* The C99 flexible array extension. */
10640 if (!staticp && TREE_CODE (type) == ARRAY_TYPE
10641 && TYPE_DOMAIN (type) == NULL_TREE)
10643 tree itype = compute_array_index_type (dname, integer_zero_node,
10644 tf_warning_or_error);
10645 type = build_cplus_array_type (TREE_TYPE (type), itype);
10648 if (type == error_mark_node)
10650 /* Happens when declaring arrays of sizes which
10651 are error_mark_node, for example. */
10652 decl = NULL_TREE;
10654 else if (in_namespace && !friendp)
10656 /* Something like struct S { int N::j; }; */
10657 error ("invalid use of %<::%>");
10658 return error_mark_node;
10660 else if (TREE_CODE (type) == FUNCTION_TYPE
10661 || TREE_CODE (type) == METHOD_TYPE)
10663 int publicp = 0;
10664 tree function_context;
10666 if (friendp == 0)
10668 /* This should never happen in pure C++ (the check
10669 could be an assert). It could happen in
10670 Objective-C++ if someone writes invalid code that
10671 uses a function declaration for an instance
10672 variable or property (instance variables and
10673 properties are parsed as FIELD_DECLs, but they are
10674 part of an Objective-C class, not a C++ class).
10675 That code is invalid and is caught by this
10676 check. */
10677 if (!ctype)
10679 error ("declaration of function %qD in invalid context",
10680 unqualified_id);
10681 return error_mark_node;
10684 /* ``A union may [ ... ] not [ have ] virtual functions.''
10685 ARM 9.5 */
10686 if (virtualp && TREE_CODE (ctype) == UNION_TYPE)
10688 error ("function %qD declared virtual inside a union",
10689 unqualified_id);
10690 return error_mark_node;
10693 if (NEW_DELETE_OPNAME_P (unqualified_id))
10695 if (virtualp)
10697 error ("%qD cannot be declared virtual, since it "
10698 "is always static",
10699 unqualified_id);
10700 virtualp = 0;
10705 /* Check that the name used for a destructor makes sense. */
10706 if (sfk == sfk_destructor)
10708 tree uqname = id_declarator->u.id.unqualified_name;
10710 if (!ctype)
10712 gcc_assert (friendp);
10713 error ("expected qualified name in friend declaration "
10714 "for destructor %qD", uqname);
10715 return error_mark_node;
10718 if (!check_dtor_name (ctype, TREE_OPERAND (uqname, 0)))
10720 error ("declaration of %qD as member of %qT",
10721 uqname, ctype);
10722 return error_mark_node;
10724 if (constexpr_p)
10726 error ("a destructor cannot be %<constexpr%>");
10727 return error_mark_node;
10730 else if (sfk == sfk_constructor && friendp && !ctype)
10732 error ("expected qualified name in friend declaration "
10733 "for constructor %qD",
10734 id_declarator->u.id.unqualified_name);
10735 return error_mark_node;
10738 if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
10740 tree tmpl = TREE_OPERAND (unqualified_id, 0);
10741 if (variable_template_p (tmpl))
10743 error ("specialization of variable template %qD "
10744 "declared as function", tmpl);
10745 inform (DECL_SOURCE_LOCATION (tmpl),
10746 "variable template declared here");
10747 return error_mark_node;
10751 /* Tell grokfndecl if it needs to set TREE_PUBLIC on the node. */
10752 function_context = (ctype != NULL_TREE) ?
10753 decl_function_context (TYPE_MAIN_DECL (ctype)) : NULL_TREE;
10754 publicp = (! friendp || ! staticp)
10755 && function_context == NULL_TREE;
10757 if (late_return_type_p)
10758 TYPE_HAS_LATE_RETURN_TYPE (type) = 1;
10760 decl = grokfndecl (ctype, type,
10761 TREE_CODE (unqualified_id) != TEMPLATE_ID_EXPR
10762 ? unqualified_id : dname,
10763 parms,
10764 unqualified_id,
10765 virtualp, flags, memfn_quals, rqual, raises,
10766 friendp ? -1 : 0, friendp, publicp,
10767 inlinep | (2 * constexpr_p),
10768 initialized == SD_DELETED, sfk,
10769 funcdef_flag, template_count, in_namespace,
10770 attrlist, declarator->id_loc);
10771 decl = set_virt_specifiers (decl, virt_specifiers);
10772 if (decl == NULL_TREE)
10773 return error_mark_node;
10774 #if 0
10775 /* This clobbers the attrs stored in `decl' from `attrlist'. */
10776 /* The decl and setting of decl_attr is also turned off. */
10777 decl = build_decl_attribute_variant (decl, decl_attr);
10778 #endif
10780 /* [class.conv.ctor]
10782 A constructor declared without the function-specifier
10783 explicit that can be called with a single parameter
10784 specifies a conversion from the type of its first
10785 parameter to the type of its class. Such a constructor
10786 is called a converting constructor. */
10787 if (explicitp == 2)
10788 DECL_NONCONVERTING_P (decl) = 1;
10790 else if (!staticp && !dependent_type_p (type)
10791 && !COMPLETE_TYPE_P (complete_type (type))
10792 && (TREE_CODE (type) != ARRAY_TYPE
10793 || !COMPLETE_TYPE_P (TREE_TYPE (type))
10794 || initialized == 0))
10796 if (unqualified_id)
10798 error ("field %qD has incomplete type %qT",
10799 unqualified_id, type);
10800 cxx_incomplete_type_inform (strip_array_types (type));
10802 else
10803 error ("name %qT has incomplete type", type);
10805 type = error_mark_node;
10806 decl = NULL_TREE;
10808 else
10810 if (friendp)
10812 error ("%qE is neither function nor member function; "
10813 "cannot be declared friend", unqualified_id);
10814 friendp = 0;
10816 decl = NULL_TREE;
10819 if (friendp)
10821 /* Friends are treated specially. */
10822 if (ctype == current_class_type)
10823 ; /* We already issued a permerror. */
10824 else if (decl && DECL_NAME (decl))
10826 if (template_class_depth (current_class_type) == 0)
10828 decl = check_explicit_specialization
10829 (unqualified_id, decl, template_count,
10830 2 * funcdef_flag + 4);
10831 if (decl == error_mark_node)
10832 return error_mark_node;
10835 decl = do_friend (ctype, unqualified_id, decl,
10836 *attrlist, flags,
10837 funcdef_flag);
10838 return decl;
10840 else
10841 return error_mark_node;
10844 /* Structure field. It may not be a function, except for C++. */
10846 if (decl == NULL_TREE)
10848 if (staticp)
10850 /* C++ allows static class members. All other work
10851 for this is done by grokfield. */
10852 decl = build_lang_decl_loc (declarator
10853 ? declarator->id_loc
10854 : input_location,
10855 VAR_DECL, unqualified_id, type);
10856 set_linkage_for_static_data_member (decl);
10857 /* Even if there is an in-class initialization, DECL
10858 is considered undefined until an out-of-class
10859 definition is provided. */
10860 DECL_EXTERNAL (decl) = 1;
10862 if (thread_p)
10864 set_decl_tls_model (decl, decl_default_tls_model (decl));
10865 if (declspecs->gnu_thread_keyword_p)
10866 DECL_GNU_TLS_P (decl) = true;
10869 if (constexpr_p && !initialized)
10871 error ("constexpr static data member %qD must have an "
10872 "initializer", decl);
10873 constexpr_p = false;
10876 else
10878 if (constexpr_p)
10880 error ("non-static data member %qE declared %<constexpr%>",
10881 unqualified_id);
10882 constexpr_p = false;
10884 decl = build_decl (input_location,
10885 FIELD_DECL, unqualified_id, type);
10886 DECL_NONADDRESSABLE_P (decl) = bitfield;
10887 if (bitfield && !unqualified_id)
10888 TREE_NO_WARNING (decl) = 1;
10890 if (storage_class == sc_mutable)
10892 DECL_MUTABLE_P (decl) = 1;
10893 storage_class = sc_none;
10896 if (initialized)
10898 /* An attempt is being made to initialize a non-static
10899 member. This is new in C++11. */
10900 maybe_warn_cpp0x (CPP0X_NSDMI);
10902 /* If this has been parsed with static storage class, but
10903 errors forced staticp to be cleared, ensure NSDMI is
10904 not present. */
10905 if (declspecs->storage_class == sc_static)
10906 DECL_INITIAL (decl) = error_mark_node;
10910 bad_specifiers (decl, BSP_FIELD, virtualp,
10911 memfn_quals != TYPE_UNQUALIFIED,
10912 inlinep, friendp, raises != NULL_TREE);
10915 else if (TREE_CODE (type) == FUNCTION_TYPE
10916 || TREE_CODE (type) == METHOD_TYPE)
10918 tree original_name;
10919 int publicp = 0;
10921 if (!unqualified_id)
10922 return error_mark_node;
10924 if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
10925 original_name = dname;
10926 else
10927 original_name = unqualified_id;
10929 if (storage_class == sc_auto)
10930 error ("storage class %<auto%> invalid for function %qs", name);
10931 else if (storage_class == sc_register)
10932 error ("storage class %<register%> invalid for function %qs", name);
10933 else if (thread_p)
10935 if (declspecs->gnu_thread_keyword_p)
10936 error ("storage class %<__thread%> invalid for function %qs",
10937 name);
10938 else
10939 error ("storage class %<thread_local%> invalid for function %qs",
10940 name);
10943 if (virt_specifiers)
10944 error ("virt-specifiers in %qs not allowed outside a class definition", name);
10945 /* Function declaration not at top level.
10946 Storage classes other than `extern' are not allowed
10947 and `extern' makes no difference. */
10948 if (! toplevel_bindings_p ()
10949 && (storage_class == sc_static
10950 || decl_spec_seq_has_spec_p (declspecs, ds_inline))
10951 && pedantic)
10953 if (storage_class == sc_static)
10954 pedwarn (input_location, OPT_Wpedantic,
10955 "%<static%> specified invalid for function %qs "
10956 "declared out of global scope", name);
10957 else
10958 pedwarn (input_location, OPT_Wpedantic,
10959 "%<inline%> specifier invalid for function %qs "
10960 "declared out of global scope", name);
10963 if (ctype == NULL_TREE)
10965 if (virtualp)
10967 error ("virtual non-class function %qs", name);
10968 virtualp = 0;
10970 else if (sfk == sfk_constructor
10971 || sfk == sfk_destructor)
10973 error (funcdef_flag
10974 ? G_("%qs defined in a non-class scope")
10975 : G_("%qs declared in a non-class scope"), name);
10976 sfk = sfk_none;
10980 /* Record whether the function is public. */
10981 publicp = (ctype != NULL_TREE
10982 || storage_class != sc_static);
10984 if (late_return_type_p)
10985 TYPE_HAS_LATE_RETURN_TYPE (type) = 1;
10987 decl = grokfndecl (ctype, type, original_name, parms, unqualified_id,
10988 virtualp, flags, memfn_quals, rqual, raises,
10989 1, friendp,
10990 publicp, inlinep | (2 * constexpr_p),
10991 initialized == SD_DELETED, sfk,
10992 funcdef_flag,
10993 template_count, in_namespace, attrlist,
10994 declarator->id_loc);
10995 if (decl == NULL_TREE)
10996 return error_mark_node;
10998 if (staticp == 1)
11000 int invalid_static = 0;
11002 /* Don't allow a static member function in a class, and forbid
11003 declaring main to be static. */
11004 if (TREE_CODE (type) == METHOD_TYPE)
11006 permerror (input_location, "cannot declare member function %qD to have "
11007 "static linkage", decl);
11008 invalid_static = 1;
11010 else if (current_function_decl)
11012 /* FIXME need arm citation */
11013 error ("cannot declare static function inside another function");
11014 invalid_static = 1;
11017 if (invalid_static)
11019 staticp = 0;
11020 storage_class = sc_none;
11024 else
11026 /* It's a variable. */
11028 /* An uninitialized decl with `extern' is a reference. */
11029 decl = grokvardecl (type, dname, unqualified_id,
11030 declspecs,
11031 initialized,
11032 (type_quals & TYPE_QUAL_CONST) != 0,
11033 template_count,
11034 ctype ? ctype : in_namespace);
11035 if (decl == NULL_TREE)
11036 return error_mark_node;
11038 bad_specifiers (decl, BSP_VAR, virtualp,
11039 memfn_quals != TYPE_UNQUALIFIED,
11040 inlinep, friendp, raises != NULL_TREE);
11042 if (ctype)
11044 DECL_CONTEXT (decl) = ctype;
11045 if (staticp == 1)
11047 permerror (input_location, "%<static%> may not be used when defining "
11048 "(as opposed to declaring) a static data member");
11049 staticp = 0;
11050 storage_class = sc_none;
11052 if (storage_class == sc_register && TREE_STATIC (decl))
11054 error ("static member %qD declared %<register%>", decl);
11055 storage_class = sc_none;
11057 if (storage_class == sc_extern && pedantic)
11059 pedwarn (input_location, OPT_Wpedantic,
11060 "cannot explicitly declare member %q#D to have "
11061 "extern linkage", decl);
11062 storage_class = sc_none;
11065 else if (constexpr_p && DECL_EXTERNAL (decl))
11067 error ("declaration of constexpr variable %qD is not a definition",
11068 decl);
11069 constexpr_p = false;
11073 if (storage_class == sc_extern && initialized && !funcdef_flag)
11075 if (toplevel_bindings_p ())
11077 /* It's common practice (and completely valid) to have a const
11078 be initialized and declared extern. */
11079 if (!(type_quals & TYPE_QUAL_CONST))
11080 warning (0, "%qs initialized and declared %<extern%>", name);
11082 else
11084 error ("%qs has both %<extern%> and initializer", name);
11085 return error_mark_node;
11089 /* Record `register' declaration for warnings on &
11090 and in case doing stupid register allocation. */
11092 if (storage_class == sc_register)
11093 DECL_REGISTER (decl) = 1;
11094 else if (storage_class == sc_extern)
11095 DECL_THIS_EXTERN (decl) = 1;
11096 else if (storage_class == sc_static)
11097 DECL_THIS_STATIC (decl) = 1;
11099 /* Set constexpr flag on vars (functions got it in grokfndecl). */
11100 if (constexpr_p && VAR_P (decl))
11101 DECL_DECLARED_CONSTEXPR_P (decl) = true;
11103 /* Record constancy and volatility on the DECL itself . There's
11104 no need to do this when processing a template; we'll do this
11105 for the instantiated declaration based on the type of DECL. */
11106 if (!processing_template_decl)
11107 cp_apply_type_quals_to_decl (type_quals, decl);
11109 return decl;
11113 /* Subroutine of start_function. Ensure that each of the parameter
11114 types (as listed in PARMS) is complete, as is required for a
11115 function definition. */
11117 static void
11118 require_complete_types_for_parms (tree parms)
11120 for (; parms; parms = DECL_CHAIN (parms))
11122 if (dependent_type_p (TREE_TYPE (parms)))
11123 continue;
11124 if (!VOID_TYPE_P (TREE_TYPE (parms))
11125 && complete_type_or_else (TREE_TYPE (parms), parms))
11127 relayout_decl (parms);
11128 DECL_ARG_TYPE (parms) = type_passed_as (TREE_TYPE (parms));
11130 else
11131 /* grokparms or complete_type_or_else will have already issued
11132 an error. */
11133 TREE_TYPE (parms) = error_mark_node;
11137 /* Returns nonzero if T is a local variable. */
11140 local_variable_p (const_tree t)
11142 if ((VAR_P (t)
11143 /* A VAR_DECL with a context that is a _TYPE is a static data
11144 member. */
11145 && !TYPE_P (CP_DECL_CONTEXT (t))
11146 /* Any other non-local variable must be at namespace scope. */
11147 && !DECL_NAMESPACE_SCOPE_P (t))
11148 || (TREE_CODE (t) == PARM_DECL))
11149 return 1;
11151 return 0;
11154 /* Like local_variable_p, but suitable for use as a tree-walking
11155 function. */
11157 static tree
11158 local_variable_p_walkfn (tree *tp, int *walk_subtrees,
11159 void * /*data*/)
11161 if (local_variable_p (*tp)
11162 && (!DECL_ARTIFICIAL (*tp) || DECL_NAME (*tp) == this_identifier))
11163 return *tp;
11164 else if (TYPE_P (*tp))
11165 *walk_subtrees = 0;
11167 return NULL_TREE;
11170 /* Check that ARG, which is a default-argument expression for a
11171 parameter DECL, is valid. Returns ARG, or ERROR_MARK_NODE, if
11172 something goes wrong. DECL may also be a _TYPE node, rather than a
11173 DECL, if there is no DECL available. */
11175 tree
11176 check_default_argument (tree decl, tree arg, tsubst_flags_t complain)
11178 tree var;
11179 tree decl_type;
11181 if (TREE_CODE (arg) == DEFAULT_ARG)
11182 /* We get a DEFAULT_ARG when looking at an in-class declaration
11183 with a default argument. Ignore the argument for now; we'll
11184 deal with it after the class is complete. */
11185 return arg;
11187 if (TYPE_P (decl))
11189 decl_type = decl;
11190 decl = NULL_TREE;
11192 else
11193 decl_type = TREE_TYPE (decl);
11195 if (arg == error_mark_node
11196 || decl == error_mark_node
11197 || TREE_TYPE (arg) == error_mark_node
11198 || decl_type == error_mark_node)
11199 /* Something already went wrong. There's no need to check
11200 further. */
11201 return error_mark_node;
11203 /* [dcl.fct.default]
11205 A default argument expression is implicitly converted to the
11206 parameter type. */
11207 ++cp_unevaluated_operand;
11208 perform_implicit_conversion_flags (decl_type, arg, complain,
11209 LOOKUP_IMPLICIT);
11210 --cp_unevaluated_operand;
11212 if (warn_zero_as_null_pointer_constant
11213 && TYPE_PTR_OR_PTRMEM_P (decl_type)
11214 && null_ptr_cst_p (arg)
11215 && (complain & tf_warning)
11216 && maybe_warn_zero_as_null_pointer_constant (arg, input_location))
11217 return nullptr_node;
11219 /* [dcl.fct.default]
11221 Local variables shall not be used in default argument
11222 expressions.
11224 The keyword `this' shall not be used in a default argument of a
11225 member function. */
11226 var = cp_walk_tree_without_duplicates (&arg, local_variable_p_walkfn, NULL);
11227 if (var)
11229 if (complain & tf_warning_or_error)
11231 if (DECL_NAME (var) == this_identifier)
11232 permerror (input_location, "default argument %qE uses %qD",
11233 arg, var);
11234 else
11235 error ("default argument %qE uses local variable %qD", arg, var);
11237 return error_mark_node;
11240 /* All is well. */
11241 return arg;
11244 /* Returns a deprecated type used within TYPE, or NULL_TREE if none. */
11246 static tree
11247 type_is_deprecated (tree type)
11249 enum tree_code code;
11250 if (TREE_DEPRECATED (type))
11251 return type;
11252 if (TYPE_NAME (type)
11253 && TREE_DEPRECATED (TYPE_NAME (type)))
11254 return type;
11256 /* Do warn about using typedefs to a deprecated class. */
11257 if (OVERLOAD_TYPE_P (type) && type != TYPE_MAIN_VARIANT (type))
11258 return type_is_deprecated (TYPE_MAIN_VARIANT (type));
11260 code = TREE_CODE (type);
11262 if (code == POINTER_TYPE || code == REFERENCE_TYPE
11263 || code == OFFSET_TYPE || code == FUNCTION_TYPE
11264 || code == METHOD_TYPE || code == ARRAY_TYPE)
11265 return type_is_deprecated (TREE_TYPE (type));
11267 if (TYPE_PTRMEMFUNC_P (type))
11268 return type_is_deprecated
11269 (TREE_TYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type))));
11271 return NULL_TREE;
11274 /* Decode the list of parameter types for a function type.
11275 Given the list of things declared inside the parens,
11276 return a list of types.
11278 If this parameter does not end with an ellipsis, we append
11279 void_list_node.
11281 *PARMS is set to the chain of PARM_DECLs created. */
11283 static tree
11284 grokparms (tree parmlist, tree *parms)
11286 tree result = NULL_TREE;
11287 tree decls = NULL_TREE;
11288 tree parm;
11289 int any_error = 0;
11291 for (parm = parmlist; parm != NULL_TREE; parm = TREE_CHAIN (parm))
11293 tree type = NULL_TREE;
11294 tree init = TREE_PURPOSE (parm);
11295 tree decl = TREE_VALUE (parm);
11296 const char *errmsg;
11298 if (parm == void_list_node)
11299 break;
11301 if (! decl || TREE_TYPE (decl) == error_mark_node)
11302 continue;
11304 type = TREE_TYPE (decl);
11305 if (VOID_TYPE_P (type))
11307 if (same_type_p (type, void_type_node)
11308 && !init
11309 && !DECL_NAME (decl) && !result
11310 && TREE_CHAIN (parm) == void_list_node)
11311 /* DR 577: A parameter list consisting of a single
11312 unnamed parameter of non-dependent type 'void'. */
11313 break;
11314 else if (cv_qualified_p (type))
11315 error_at (DECL_SOURCE_LOCATION (decl),
11316 "invalid use of cv-qualified type %qT in "
11317 "parameter declaration", type);
11318 else
11319 error_at (DECL_SOURCE_LOCATION (decl),
11320 "invalid use of type %<void%> in parameter "
11321 "declaration");
11322 /* It's not a good idea to actually create parameters of
11323 type `void'; other parts of the compiler assume that a
11324 void type terminates the parameter list. */
11325 type = error_mark_node;
11326 TREE_TYPE (decl) = error_mark_node;
11329 if (type != error_mark_node
11330 && TYPE_FOR_JAVA (type)
11331 && MAYBE_CLASS_TYPE_P (type))
11333 error ("parameter %qD has Java class type", decl);
11334 type = error_mark_node;
11335 TREE_TYPE (decl) = error_mark_node;
11336 init = NULL_TREE;
11339 if (type != error_mark_node
11340 && (errmsg = targetm.invalid_parameter_type (type)))
11342 error (errmsg);
11343 type = error_mark_node;
11344 TREE_TYPE (decl) = error_mark_node;
11347 if (type != error_mark_node)
11349 if (deprecated_state != DEPRECATED_SUPPRESS)
11351 tree deptype = type_is_deprecated (type);
11352 if (deptype)
11353 warn_deprecated_use (deptype, NULL_TREE);
11356 /* Top-level qualifiers on the parameters are
11357 ignored for function types. */
11358 type = cp_build_qualified_type (type, 0);
11359 if (TREE_CODE (type) == METHOD_TYPE)
11361 error ("parameter %qD invalidly declared method type", decl);
11362 type = build_pointer_type (type);
11363 TREE_TYPE (decl) = type;
11365 else if (abstract_virtuals_error (decl, type))
11366 any_error = 1; /* Seems like a good idea. */
11367 else if (POINTER_TYPE_P (type))
11369 /* [dcl.fct]/6, parameter types cannot contain pointers
11370 (references) to arrays of unknown bound. */
11371 tree t = TREE_TYPE (type);
11372 int ptr = TYPE_PTR_P (type);
11374 while (1)
11376 if (TYPE_PTR_P (t))
11377 ptr = 1;
11378 else if (TREE_CODE (t) != ARRAY_TYPE)
11379 break;
11380 else if (!TYPE_DOMAIN (t))
11381 break;
11382 t = TREE_TYPE (t);
11384 if (TREE_CODE (t) == ARRAY_TYPE)
11385 error (ptr
11386 ? G_("parameter %qD includes pointer to array of "
11387 "unknown bound %qT")
11388 : G_("parameter %qD includes reference to array of "
11389 "unknown bound %qT"),
11390 decl, t);
11393 if (any_error)
11394 init = NULL_TREE;
11395 else if (init && !processing_template_decl)
11396 init = check_default_argument (decl, init, tf_warning_or_error);
11399 DECL_CHAIN (decl) = decls;
11400 decls = decl;
11401 result = tree_cons (init, type, result);
11403 decls = nreverse (decls);
11404 result = nreverse (result);
11405 if (parm)
11406 result = chainon (result, void_list_node);
11407 *parms = decls;
11409 return result;
11413 /* D is a constructor or overloaded `operator='.
11415 Let T be the class in which D is declared. Then, this function
11416 returns:
11418 -1 if D's is an ill-formed constructor or copy assignment operator
11419 whose first parameter is of type `T'.
11420 0 if D is not a copy constructor or copy assignment
11421 operator.
11422 1 if D is a copy constructor or copy assignment operator whose
11423 first parameter is a reference to non-const qualified T.
11424 2 if D is a copy constructor or copy assignment operator whose
11425 first parameter is a reference to const qualified T.
11427 This function can be used as a predicate. Positive values indicate
11428 a copy constructor and nonzero values indicate a copy assignment
11429 operator. */
11432 copy_fn_p (const_tree d)
11434 tree args;
11435 tree arg_type;
11436 int result = 1;
11438 gcc_assert (DECL_FUNCTION_MEMBER_P (d));
11440 if (TREE_CODE (d) == TEMPLATE_DECL
11441 || (DECL_TEMPLATE_INFO (d)
11442 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d))))
11443 /* Instantiations of template member functions are never copy
11444 functions. Note that member functions of templated classes are
11445 represented as template functions internally, and we must
11446 accept those as copy functions. */
11447 return 0;
11449 args = FUNCTION_FIRST_USER_PARMTYPE (d);
11450 if (!args)
11451 return 0;
11453 arg_type = TREE_VALUE (args);
11454 if (arg_type == error_mark_node)
11455 return 0;
11457 if (TYPE_MAIN_VARIANT (arg_type) == DECL_CONTEXT (d))
11459 /* Pass by value copy assignment operator. */
11460 result = -1;
11462 else if (TREE_CODE (arg_type) == REFERENCE_TYPE
11463 && !TYPE_REF_IS_RVALUE (arg_type)
11464 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)) == DECL_CONTEXT (d))
11466 if (CP_TYPE_CONST_P (TREE_TYPE (arg_type)))
11467 result = 2;
11469 else
11470 return 0;
11472 args = TREE_CHAIN (args);
11474 if (args && args != void_list_node && !TREE_PURPOSE (args))
11475 /* There are more non-optional args. */
11476 return 0;
11478 return result;
11481 /* D is a constructor or overloaded `operator='.
11483 Let T be the class in which D is declared. Then, this function
11484 returns true when D is a move constructor or move assignment
11485 operator, false otherwise. */
11487 bool
11488 move_fn_p (const_tree d)
11490 gcc_assert (DECL_FUNCTION_MEMBER_P (d));
11492 if (cxx_dialect == cxx98)
11493 /* There are no move constructors if we are in C++98 mode. */
11494 return false;
11496 if (TREE_CODE (d) == TEMPLATE_DECL
11497 || (DECL_TEMPLATE_INFO (d)
11498 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d))))
11499 /* Instantiations of template member functions are never move
11500 functions. Note that member functions of templated classes are
11501 represented as template functions internally, and we must
11502 accept those as move functions. */
11503 return 0;
11505 return move_signature_fn_p (d);
11508 /* D is a constructor or overloaded `operator='.
11510 Then, this function returns true when D has the same signature as a move
11511 constructor or move assignment operator (because either it is such a
11512 ctor/op= or it is a template specialization with the same signature),
11513 false otherwise. */
11515 bool
11516 move_signature_fn_p (const_tree d)
11518 tree args;
11519 tree arg_type;
11520 bool result = false;
11522 args = FUNCTION_FIRST_USER_PARMTYPE (d);
11523 if (!args)
11524 return 0;
11526 arg_type = TREE_VALUE (args);
11527 if (arg_type == error_mark_node)
11528 return 0;
11530 if (TREE_CODE (arg_type) == REFERENCE_TYPE
11531 && TYPE_REF_IS_RVALUE (arg_type)
11532 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)),
11533 DECL_CONTEXT (d)))
11534 result = true;
11536 args = TREE_CHAIN (args);
11538 if (args && args != void_list_node && !TREE_PURPOSE (args))
11539 /* There are more non-optional args. */
11540 return false;
11542 return result;
11545 /* Remember any special properties of member function DECL. */
11547 void
11548 grok_special_member_properties (tree decl)
11550 tree class_type;
11552 if (!DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
11553 return;
11555 class_type = DECL_CONTEXT (decl);
11556 if (DECL_CONSTRUCTOR_P (decl))
11558 int ctor = copy_fn_p (decl);
11560 if (!DECL_ARTIFICIAL (decl))
11561 TYPE_HAS_USER_CONSTRUCTOR (class_type) = 1;
11563 if (ctor > 0)
11565 /* [class.copy]
11567 A non-template constructor for class X is a copy
11568 constructor if its first parameter is of type X&, const
11569 X&, volatile X& or const volatile X&, and either there
11570 are no other parameters or else all other parameters have
11571 default arguments. */
11572 TYPE_HAS_COPY_CTOR (class_type) = 1;
11573 if (user_provided_p (decl))
11574 TYPE_HAS_COMPLEX_COPY_CTOR (class_type) = 1;
11575 if (ctor > 1)
11576 TYPE_HAS_CONST_COPY_CTOR (class_type) = 1;
11578 else if (sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (decl)))
11579 TYPE_HAS_DEFAULT_CONSTRUCTOR (class_type) = 1;
11580 else if (move_fn_p (decl) && user_provided_p (decl))
11581 TYPE_HAS_COMPLEX_MOVE_CTOR (class_type) = 1;
11582 else if (is_list_ctor (decl))
11583 TYPE_HAS_LIST_CTOR (class_type) = 1;
11585 if (DECL_DECLARED_CONSTEXPR_P (decl)
11586 && !copy_fn_p (decl) && !move_fn_p (decl))
11587 TYPE_HAS_CONSTEXPR_CTOR (class_type) = 1;
11589 else if (DECL_OVERLOADED_OPERATOR_P (decl) == NOP_EXPR)
11591 /* [class.copy]
11593 A non-template assignment operator for class X is a copy
11594 assignment operator if its parameter is of type X, X&, const
11595 X&, volatile X& or const volatile X&. */
11597 int assop = copy_fn_p (decl);
11599 if (assop)
11601 TYPE_HAS_COPY_ASSIGN (class_type) = 1;
11602 if (user_provided_p (decl))
11603 TYPE_HAS_COMPLEX_COPY_ASSIGN (class_type) = 1;
11604 if (assop != 1)
11605 TYPE_HAS_CONST_COPY_ASSIGN (class_type) = 1;
11607 else if (move_fn_p (decl) && user_provided_p (decl))
11608 TYPE_HAS_COMPLEX_MOVE_ASSIGN (class_type) = 1;
11610 /* Destructors are handled in check_methods. */
11613 /* Check a constructor DECL has the correct form. Complains
11614 if the class has a constructor of the form X(X). */
11617 grok_ctor_properties (const_tree ctype, const_tree decl)
11619 int ctor_parm = copy_fn_p (decl);
11621 if (ctor_parm < 0)
11623 /* [class.copy]
11625 A declaration of a constructor for a class X is ill-formed if
11626 its first parameter is of type (optionally cv-qualified) X
11627 and either there are no other parameters or else all other
11628 parameters have default arguments.
11630 We *don't* complain about member template instantiations that
11631 have this form, though; they can occur as we try to decide
11632 what constructor to use during overload resolution. Since
11633 overload resolution will never prefer such a constructor to
11634 the non-template copy constructor (which is either explicitly
11635 or implicitly defined), there's no need to worry about their
11636 existence. Theoretically, they should never even be
11637 instantiated, but that's hard to forestall. */
11638 error ("invalid constructor; you probably meant %<%T (const %T&)%>",
11639 ctype, ctype);
11640 return 0;
11643 return 1;
11646 /* An operator with this code is unary, but can also be binary. */
11648 static int
11649 ambi_op_p (enum tree_code code)
11651 return (code == INDIRECT_REF
11652 || code == ADDR_EXPR
11653 || code == UNARY_PLUS_EXPR
11654 || code == NEGATE_EXPR
11655 || code == PREINCREMENT_EXPR
11656 || code == PREDECREMENT_EXPR);
11659 /* An operator with this name can only be unary. */
11661 static int
11662 unary_op_p (enum tree_code code)
11664 return (code == TRUTH_NOT_EXPR
11665 || code == BIT_NOT_EXPR
11666 || code == COMPONENT_REF
11667 || code == TYPE_EXPR);
11670 /* DECL is a declaration for an overloaded operator. If COMPLAIN is true,
11671 errors are issued for invalid declarations. */
11673 bool
11674 grok_op_properties (tree decl, bool complain)
11676 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
11677 tree argtype;
11678 int methodp = (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE);
11679 tree name = DECL_NAME (decl);
11680 enum tree_code operator_code;
11681 int arity;
11682 bool ellipsis_p;
11683 tree class_type;
11685 /* Count the number of arguments and check for ellipsis. */
11686 for (argtype = argtypes, arity = 0;
11687 argtype && argtype != void_list_node;
11688 argtype = TREE_CHAIN (argtype))
11689 ++arity;
11690 ellipsis_p = !argtype;
11692 class_type = DECL_CONTEXT (decl);
11693 if (class_type && !CLASS_TYPE_P (class_type))
11694 class_type = NULL_TREE;
11696 if (DECL_CONV_FN_P (decl))
11697 operator_code = TYPE_EXPR;
11698 else
11701 #define DEF_OPERATOR(NAME, CODE, MANGLING, ARITY, ASSN_P) \
11702 if (ansi_opname (CODE) == name) \
11704 operator_code = (CODE); \
11705 break; \
11707 else if (ansi_assopname (CODE) == name) \
11709 operator_code = (CODE); \
11710 DECL_ASSIGNMENT_OPERATOR_P (decl) = 1; \
11711 break; \
11714 #include "operators.def"
11715 #undef DEF_OPERATOR
11717 gcc_unreachable ();
11719 while (0);
11720 gcc_assert (operator_code != MAX_TREE_CODES);
11721 SET_OVERLOADED_OPERATOR_CODE (decl, operator_code);
11723 if (class_type)
11724 switch (operator_code)
11726 case NEW_EXPR:
11727 TYPE_HAS_NEW_OPERATOR (class_type) = 1;
11728 break;
11730 case DELETE_EXPR:
11731 TYPE_GETS_DELETE (class_type) |= 1;
11732 break;
11734 case VEC_NEW_EXPR:
11735 TYPE_HAS_ARRAY_NEW_OPERATOR (class_type) = 1;
11736 break;
11738 case VEC_DELETE_EXPR:
11739 TYPE_GETS_DELETE (class_type) |= 2;
11740 break;
11742 default:
11743 break;
11746 /* [basic.std.dynamic.allocation]/1:
11748 A program is ill-formed if an allocation function is declared
11749 in a namespace scope other than global scope or declared static
11750 in global scope.
11752 The same also holds true for deallocation functions. */
11753 if (operator_code == NEW_EXPR || operator_code == VEC_NEW_EXPR
11754 || operator_code == DELETE_EXPR || operator_code == VEC_DELETE_EXPR)
11756 if (DECL_NAMESPACE_SCOPE_P (decl))
11758 if (CP_DECL_CONTEXT (decl) != global_namespace)
11760 error ("%qD may not be declared within a namespace", decl);
11761 return false;
11763 else if (!TREE_PUBLIC (decl))
11765 error ("%qD may not be declared as static", decl);
11766 return false;
11768 if (!flag_sized_deallocation && warn_cxx14_compat)
11770 tree parm = FUNCTION_ARG_CHAIN (decl);
11771 if (parm && same_type_p (TREE_VALUE (parm), size_type_node)
11772 && TREE_CHAIN (parm) == void_list_node)
11773 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc__14_compat,
11774 "%qD is a usual (non-placement) deallocation "
11775 "function in C++14 (or with -fsized-deallocation)",
11776 decl);
11781 if (operator_code == NEW_EXPR || operator_code == VEC_NEW_EXPR)
11783 TREE_TYPE (decl) = coerce_new_type (TREE_TYPE (decl));
11784 DECL_IS_OPERATOR_NEW (decl) = 1;
11786 else if (operator_code == DELETE_EXPR || operator_code == VEC_DELETE_EXPR)
11787 TREE_TYPE (decl) = coerce_delete_type (TREE_TYPE (decl));
11788 else
11790 /* An operator function must either be a non-static member function
11791 or have at least one parameter of a class, a reference to a class,
11792 an enumeration, or a reference to an enumeration. 13.4.0.6 */
11793 if (! methodp || DECL_STATIC_FUNCTION_P (decl))
11795 if (operator_code == TYPE_EXPR
11796 || operator_code == CALL_EXPR
11797 || operator_code == COMPONENT_REF
11798 || operator_code == ARRAY_REF
11799 || operator_code == NOP_EXPR)
11801 error ("%qD must be a nonstatic member function", decl);
11802 return false;
11804 else
11806 tree p;
11808 if (DECL_STATIC_FUNCTION_P (decl))
11810 error ("%qD must be either a non-static member "
11811 "function or a non-member function", decl);
11812 return false;
11815 for (p = argtypes; p && p != void_list_node; p = TREE_CHAIN (p))
11817 tree arg = non_reference (TREE_VALUE (p));
11818 if (arg == error_mark_node)
11819 return false;
11821 /* MAYBE_CLASS_TYPE_P, rather than CLASS_TYPE_P, is used
11822 because these checks are performed even on
11823 template functions. */
11824 if (MAYBE_CLASS_TYPE_P (arg)
11825 || TREE_CODE (arg) == ENUMERAL_TYPE)
11826 break;
11829 if (!p || p == void_list_node)
11831 if (complain)
11832 error ("%qD must have an argument of class or "
11833 "enumerated type", decl);
11834 return false;
11839 /* There are no restrictions on the arguments to an overloaded
11840 "operator ()". */
11841 if (operator_code == CALL_EXPR)
11842 return true;
11844 /* Warn about conversion operators that will never be used. */
11845 if (IDENTIFIER_TYPENAME_P (name)
11846 && ! DECL_TEMPLATE_INFO (decl)
11847 && warn_conversion
11848 /* Warn only declaring the function; there is no need to
11849 warn again about out-of-class definitions. */
11850 && class_type == current_class_type)
11852 tree t = TREE_TYPE (name);
11853 int ref = (TREE_CODE (t) == REFERENCE_TYPE);
11855 if (ref)
11856 t = TYPE_MAIN_VARIANT (TREE_TYPE (t));
11858 if (VOID_TYPE_P (t))
11859 warning (OPT_Wconversion,
11861 ? G_("conversion to a reference to void "
11862 "will never use a type conversion operator")
11863 : G_("conversion to void "
11864 "will never use a type conversion operator"));
11865 else if (class_type)
11867 if (t == class_type)
11868 warning (OPT_Wconversion,
11870 ? G_("conversion to a reference to the same type "
11871 "will never use a type conversion operator")
11872 : G_("conversion to the same type "
11873 "will never use a type conversion operator"));
11874 /* Don't force t to be complete here. */
11875 else if (MAYBE_CLASS_TYPE_P (t)
11876 && COMPLETE_TYPE_P (t)
11877 && DERIVED_FROM_P (t, class_type))
11878 warning (OPT_Wconversion,
11880 ? G_("conversion to a reference to a base class "
11881 "will never use a type conversion operator")
11882 : G_("conversion to a base class "
11883 "will never use a type conversion operator"));
11888 if (operator_code == COND_EXPR)
11890 /* 13.4.0.3 */
11891 error ("ISO C++ prohibits overloading operator ?:");
11892 return false;
11894 else if (ellipsis_p)
11896 error ("%qD must not have variable number of arguments", decl);
11897 return false;
11899 else if (ambi_op_p (operator_code))
11901 if (arity == 1)
11902 /* We pick the one-argument operator codes by default, so
11903 we don't have to change anything. */
11905 else if (arity == 2)
11907 /* If we thought this was a unary operator, we now know
11908 it to be a binary operator. */
11909 switch (operator_code)
11911 case INDIRECT_REF:
11912 operator_code = MULT_EXPR;
11913 break;
11915 case ADDR_EXPR:
11916 operator_code = BIT_AND_EXPR;
11917 break;
11919 case UNARY_PLUS_EXPR:
11920 operator_code = PLUS_EXPR;
11921 break;
11923 case NEGATE_EXPR:
11924 operator_code = MINUS_EXPR;
11925 break;
11927 case PREINCREMENT_EXPR:
11928 operator_code = POSTINCREMENT_EXPR;
11929 break;
11931 case PREDECREMENT_EXPR:
11932 operator_code = POSTDECREMENT_EXPR;
11933 break;
11935 default:
11936 gcc_unreachable ();
11939 SET_OVERLOADED_OPERATOR_CODE (decl, operator_code);
11941 if ((operator_code == POSTINCREMENT_EXPR
11942 || operator_code == POSTDECREMENT_EXPR)
11943 && ! processing_template_decl
11944 && ! same_type_p (TREE_VALUE (TREE_CHAIN (argtypes)), integer_type_node))
11946 if (methodp)
11947 error ("postfix %qD must take %<int%> as its argument",
11948 decl);
11949 else
11950 error ("postfix %qD must take %<int%> as its second "
11951 "argument", decl);
11952 return false;
11955 else
11957 if (methodp)
11958 error ("%qD must take either zero or one argument", decl);
11959 else
11960 error ("%qD must take either one or two arguments", decl);
11961 return false;
11964 /* More Effective C++ rule 6. */
11965 if (warn_ecpp
11966 && (operator_code == POSTINCREMENT_EXPR
11967 || operator_code == POSTDECREMENT_EXPR
11968 || operator_code == PREINCREMENT_EXPR
11969 || operator_code == PREDECREMENT_EXPR))
11971 tree arg = TREE_VALUE (argtypes);
11972 tree ret = TREE_TYPE (TREE_TYPE (decl));
11973 if (methodp || TREE_CODE (arg) == REFERENCE_TYPE)
11974 arg = TREE_TYPE (arg);
11975 arg = TYPE_MAIN_VARIANT (arg);
11976 if (operator_code == PREINCREMENT_EXPR
11977 || operator_code == PREDECREMENT_EXPR)
11979 if (TREE_CODE (ret) != REFERENCE_TYPE
11980 || !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (ret)),
11981 arg))
11982 warning (OPT_Weffc__, "prefix %qD should return %qT", decl,
11983 build_reference_type (arg));
11985 else
11987 if (!same_type_p (TYPE_MAIN_VARIANT (ret), arg))
11988 warning (OPT_Weffc__, "postfix %qD should return %qT", decl, arg);
11992 else if (unary_op_p (operator_code))
11994 if (arity != 1)
11996 if (methodp)
11997 error ("%qD must take %<void%>", decl);
11998 else
11999 error ("%qD must take exactly one argument", decl);
12000 return false;
12003 else /* if (binary_op_p (operator_code)) */
12005 if (arity != 2)
12007 if (methodp)
12008 error ("%qD must take exactly one argument", decl);
12009 else
12010 error ("%qD must take exactly two arguments", decl);
12011 return false;
12014 /* More Effective C++ rule 7. */
12015 if (warn_ecpp
12016 && (operator_code == TRUTH_ANDIF_EXPR
12017 || operator_code == TRUTH_ORIF_EXPR
12018 || operator_code == COMPOUND_EXPR))
12019 warning (OPT_Weffc__, "user-defined %qD always evaluates both arguments",
12020 decl);
12023 /* Effective C++ rule 23. */
12024 if (warn_ecpp
12025 && arity == 2
12026 && !DECL_ASSIGNMENT_OPERATOR_P (decl)
12027 && (operator_code == PLUS_EXPR
12028 || operator_code == MINUS_EXPR
12029 || operator_code == TRUNC_DIV_EXPR
12030 || operator_code == MULT_EXPR
12031 || operator_code == TRUNC_MOD_EXPR)
12032 && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == REFERENCE_TYPE)
12033 warning (OPT_Weffc__, "%qD should return by value", decl);
12035 /* [over.oper]/8 */
12036 for (; argtypes && argtypes != void_list_node;
12037 argtypes = TREE_CHAIN (argtypes))
12038 if (TREE_PURPOSE (argtypes))
12040 TREE_PURPOSE (argtypes) = NULL_TREE;
12041 if (operator_code == POSTINCREMENT_EXPR
12042 || operator_code == POSTDECREMENT_EXPR)
12044 pedwarn (input_location, OPT_Wpedantic, "%qD cannot have default arguments",
12045 decl);
12047 else
12049 error ("%qD cannot have default arguments", decl);
12050 return false;
12054 return true;
12057 /* Return a string giving the keyword associate with CODE. */
12059 static const char *
12060 tag_name (enum tag_types code)
12062 switch (code)
12064 case record_type:
12065 return "struct";
12066 case class_type:
12067 return "class";
12068 case union_type:
12069 return "union";
12070 case enum_type:
12071 return "enum";
12072 case typename_type:
12073 return "typename";
12074 default:
12075 gcc_unreachable ();
12079 /* Name lookup in an elaborated-type-specifier (after the keyword
12080 indicated by TAG_CODE) has found the TYPE_DECL DECL. If the
12081 elaborated-type-specifier is invalid, issue a diagnostic and return
12082 error_mark_node; otherwise, return the *_TYPE to which it referred.
12083 If ALLOW_TEMPLATE_P is true, TYPE may be a class template. */
12085 tree
12086 check_elaborated_type_specifier (enum tag_types tag_code,
12087 tree decl,
12088 bool allow_template_p)
12090 tree type;
12092 /* In the case of:
12094 struct S { struct S *p; };
12096 name lookup will find the TYPE_DECL for the implicit "S::S"
12097 typedef. Adjust for that here. */
12098 if (DECL_SELF_REFERENCE_P (decl))
12099 decl = TYPE_NAME (TREE_TYPE (decl));
12101 type = TREE_TYPE (decl);
12103 /* Check TEMPLATE_TYPE_PARM first because DECL_IMPLICIT_TYPEDEF_P
12104 is false for this case as well. */
12105 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
12107 error ("using template type parameter %qT after %qs",
12108 type, tag_name (tag_code));
12109 return error_mark_node;
12111 /* Accept template template parameters. */
12112 else if (allow_template_p
12113 && (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM
12114 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM))
12116 /* [dcl.type.elab]
12118 If the identifier resolves to a typedef-name or the
12119 simple-template-id resolves to an alias template
12120 specialization, the elaborated-type-specifier is ill-formed.
12122 In other words, the only legitimate declaration to use in the
12123 elaborated type specifier is the implicit typedef created when
12124 the type is declared. */
12125 else if (!DECL_IMPLICIT_TYPEDEF_P (decl)
12126 && !DECL_SELF_REFERENCE_P (decl)
12127 && tag_code != typename_type)
12129 if (alias_template_specialization_p (type))
12130 error ("using alias template specialization %qT after %qs",
12131 type, tag_name (tag_code));
12132 else
12133 error ("using typedef-name %qD after %qs", decl, tag_name (tag_code));
12134 inform (DECL_SOURCE_LOCATION (decl),
12135 "%qD has a previous declaration here", decl);
12136 return error_mark_node;
12138 else if (TREE_CODE (type) != RECORD_TYPE
12139 && TREE_CODE (type) != UNION_TYPE
12140 && tag_code != enum_type
12141 && tag_code != typename_type)
12143 error ("%qT referred to as %qs", type, tag_name (tag_code));
12144 inform (input_location, "%q+T has a previous declaration here", type);
12145 return error_mark_node;
12147 else if (TREE_CODE (type) != ENUMERAL_TYPE
12148 && tag_code == enum_type)
12150 error ("%qT referred to as enum", type);
12151 inform (input_location, "%q+T has a previous declaration here", type);
12152 return error_mark_node;
12154 else if (!allow_template_p
12155 && TREE_CODE (type) == RECORD_TYPE
12156 && CLASSTYPE_IS_TEMPLATE (type))
12158 /* If a class template appears as elaborated type specifier
12159 without a template header such as:
12161 template <class T> class C {};
12162 void f(class C); // No template header here
12164 then the required template argument is missing. */
12165 error ("template argument required for %<%s %T%>",
12166 tag_name (tag_code),
12167 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)));
12168 return error_mark_node;
12171 return type;
12174 /* Lookup NAME in elaborate type specifier in scope according to
12175 SCOPE and issue diagnostics if necessary.
12176 Return *_TYPE node upon success, NULL_TREE when the NAME is not
12177 found, and ERROR_MARK_NODE for type error. */
12179 static tree
12180 lookup_and_check_tag (enum tag_types tag_code, tree name,
12181 tag_scope scope, bool template_header_p)
12183 tree t;
12184 tree decl;
12185 if (scope == ts_global)
12187 /* First try ordinary name lookup, ignoring hidden class name
12188 injected via friend declaration. */
12189 decl = lookup_name_prefer_type (name, 2);
12190 /* If that fails, the name will be placed in the smallest
12191 non-class, non-function-prototype scope according to 3.3.1/5.
12192 We may already have a hidden name declared as friend in this
12193 scope. So lookup again but not ignoring hidden names.
12194 If we find one, that name will be made visible rather than
12195 creating a new tag. */
12196 if (!decl)
12197 decl = lookup_type_scope (name, ts_within_enclosing_non_class);
12199 else
12200 decl = lookup_type_scope (name, scope);
12202 if (decl
12203 && (DECL_CLASS_TEMPLATE_P (decl)
12204 /* If scope is ts_current we're defining a class, so ignore a
12205 template template parameter. */
12206 || (scope != ts_current
12207 && DECL_TEMPLATE_TEMPLATE_PARM_P (decl))))
12208 decl = DECL_TEMPLATE_RESULT (decl);
12210 if (decl && TREE_CODE (decl) == TYPE_DECL)
12212 /* Look for invalid nested type:
12213 class C {
12214 class C {};
12215 }; */
12216 if (scope == ts_current && DECL_SELF_REFERENCE_P (decl))
12218 error ("%qD has the same name as the class in which it is "
12219 "declared",
12220 decl);
12221 return error_mark_node;
12224 /* Two cases we need to consider when deciding if a class
12225 template is allowed as an elaborated type specifier:
12226 1. It is a self reference to its own class.
12227 2. It comes with a template header.
12229 For example:
12231 template <class T> class C {
12232 class C *c1; // DECL_SELF_REFERENCE_P is true
12233 class D;
12235 template <class U> class C; // template_header_p is true
12236 template <class T> class C<T>::D {
12237 class C *c2; // DECL_SELF_REFERENCE_P is true
12238 }; */
12240 t = check_elaborated_type_specifier (tag_code,
12241 decl,
12242 template_header_p
12243 | DECL_SELF_REFERENCE_P (decl));
12244 return t;
12246 else if (decl && TREE_CODE (decl) == TREE_LIST)
12248 error ("reference to %qD is ambiguous", name);
12249 print_candidates (decl);
12250 return error_mark_node;
12252 else
12253 return NULL_TREE;
12256 /* Get the struct, enum or union (TAG_CODE says which) with tag NAME.
12257 Define the tag as a forward-reference if it is not defined.
12259 If a declaration is given, process it here, and report an error if
12260 multiple declarations are not identical.
12262 SCOPE is TS_CURRENT when this is also a definition. Only look in
12263 the current frame for the name (since C++ allows new names in any
12264 scope.) It is TS_WITHIN_ENCLOSING_NON_CLASS if this is a friend
12265 declaration. Only look beginning from the current scope outward up
12266 till the nearest non-class scope. Otherwise it is TS_GLOBAL.
12268 TEMPLATE_HEADER_P is true when this declaration is preceded by
12269 a set of template parameters. */
12271 static tree
12272 xref_tag_1 (enum tag_types tag_code, tree name,
12273 tag_scope orig_scope, bool template_header_p)
12275 enum tree_code code;
12276 tree t;
12277 tree context = NULL_TREE;
12278 tag_scope scope;
12280 gcc_assert (identifier_p (name));
12282 switch (tag_code)
12284 case record_type:
12285 case class_type:
12286 code = RECORD_TYPE;
12287 break;
12288 case union_type:
12289 code = UNION_TYPE;
12290 break;
12291 case enum_type:
12292 code = ENUMERAL_TYPE;
12293 break;
12294 default:
12295 gcc_unreachable ();
12298 if (orig_scope == ts_lambda)
12299 scope = ts_current;
12300 else
12301 scope = orig_scope;
12303 /* In case of anonymous name, xref_tag is only called to
12304 make type node and push name. Name lookup is not required. */
12305 if (ANON_AGGRNAME_P (name))
12306 t = NULL_TREE;
12307 else
12308 t = lookup_and_check_tag (tag_code, name,
12309 scope, template_header_p);
12311 if (t == error_mark_node)
12312 return error_mark_node;
12314 if (scope != ts_current && t && current_class_type
12315 && template_class_depth (current_class_type)
12316 && template_header_p)
12318 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
12319 return t;
12321 /* Since SCOPE is not TS_CURRENT, we are not looking at a
12322 definition of this tag. Since, in addition, we are currently
12323 processing a (member) template declaration of a template
12324 class, we must be very careful; consider:
12326 template <class X>
12327 struct S1
12329 template <class U>
12330 struct S2
12331 { template <class V>
12332 friend struct S1; };
12334 Here, the S2::S1 declaration should not be confused with the
12335 outer declaration. In particular, the inner version should
12336 have a template parameter of level 2, not level 1. This
12337 would be particularly important if the member declaration
12338 were instead:
12340 template <class V = U> friend struct S1;
12342 say, when we should tsubst into `U' when instantiating
12343 S2. On the other hand, when presented with:
12345 template <class T>
12346 struct S1 {
12347 template <class U>
12348 struct S2 {};
12349 template <class U>
12350 friend struct S2;
12353 we must find the inner binding eventually. We
12354 accomplish this by making sure that the new type we
12355 create to represent this declaration has the right
12356 TYPE_CONTEXT. */
12357 context = TYPE_CONTEXT (t);
12358 t = NULL_TREE;
12361 if (! t)
12363 /* If no such tag is yet defined, create a forward-reference node
12364 and record it as the "definition".
12365 When a real declaration of this type is found,
12366 the forward-reference will be altered into a real type. */
12367 if (code == ENUMERAL_TYPE)
12369 error ("use of enum %q#D without previous declaration", name);
12370 return error_mark_node;
12372 else
12374 t = make_class_type (code);
12375 TYPE_CONTEXT (t) = context;
12376 if (orig_scope == ts_lambda)
12377 /* Remember that we're declaring a lambda to avoid bogus errors
12378 in push_template_decl. */
12379 CLASSTYPE_LAMBDA_EXPR (t) = error_mark_node;
12380 t = pushtag (name, t, scope);
12383 else
12385 if (template_header_p && MAYBE_CLASS_TYPE_P (t))
12387 if (!redeclare_class_template (t, current_template_parms))
12388 return error_mark_node;
12390 else if (!processing_template_decl
12391 && CLASS_TYPE_P (t)
12392 && CLASSTYPE_IS_TEMPLATE (t))
12394 error ("redeclaration of %qT as a non-template", t);
12395 error ("previous declaration %q+D", t);
12396 return error_mark_node;
12399 /* Make injected friend class visible. */
12400 if (scope != ts_within_enclosing_non_class
12401 && hidden_name_p (TYPE_NAME (t)))
12403 DECL_ANTICIPATED (TYPE_NAME (t)) = 0;
12404 DECL_FRIEND_P (TYPE_NAME (t)) = 0;
12406 if (TYPE_TEMPLATE_INFO (t))
12408 DECL_ANTICIPATED (TYPE_TI_TEMPLATE (t)) = 0;
12409 DECL_FRIEND_P (TYPE_TI_TEMPLATE (t)) = 0;
12414 return t;
12417 /* Wrapper for xref_tag_1. */
12419 tree
12420 xref_tag (enum tag_types tag_code, tree name,
12421 tag_scope scope, bool template_header_p)
12423 tree ret;
12424 bool subtime;
12425 subtime = timevar_cond_start (TV_NAME_LOOKUP);
12426 ret = xref_tag_1 (tag_code, name, scope, template_header_p);
12427 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
12428 return ret;
12432 tree
12433 xref_tag_from_type (tree old, tree id, tag_scope scope)
12435 enum tag_types tag_kind;
12437 if (TREE_CODE (old) == RECORD_TYPE)
12438 tag_kind = (CLASSTYPE_DECLARED_CLASS (old) ? class_type : record_type);
12439 else
12440 tag_kind = union_type;
12442 if (id == NULL_TREE)
12443 id = TYPE_IDENTIFIER (old);
12445 return xref_tag (tag_kind, id, scope, false);
12448 /* Create the binfo hierarchy for REF with (possibly NULL) base list
12449 BASE_LIST. For each element on BASE_LIST the TREE_PURPOSE is an
12450 access_* node, and the TREE_VALUE is the type of the base-class.
12451 Non-NULL TREE_TYPE indicates virtual inheritance.
12453 Returns true if the binfo hierarchy was successfully created,
12454 false if an error was detected. */
12456 bool
12457 xref_basetypes (tree ref, tree base_list)
12459 tree *basep;
12460 tree binfo, base_binfo;
12461 unsigned max_vbases = 0; /* Maximum direct & indirect virtual bases. */
12462 unsigned max_bases = 0; /* Maximum direct bases. */
12463 int i;
12464 tree default_access;
12465 tree igo_prev; /* Track Inheritance Graph Order. */
12467 if (ref == error_mark_node)
12468 return false;
12470 /* The base of a derived class is private by default, all others are
12471 public. */
12472 default_access = (TREE_CODE (ref) == RECORD_TYPE
12473 && CLASSTYPE_DECLARED_CLASS (ref)
12474 ? access_private_node : access_public_node);
12476 /* First, make sure that any templates in base-classes are
12477 instantiated. This ensures that if we call ourselves recursively
12478 we do not get confused about which classes are marked and which
12479 are not. */
12480 basep = &base_list;
12481 while (*basep)
12483 tree basetype = TREE_VALUE (*basep);
12485 /* The dependent_type_p call below should really be dependent_scope_p
12486 so that we give a hard error about using an incomplete type as a
12487 base, but we allow it with a pedwarn for backward
12488 compatibility. */
12489 if (processing_template_decl
12490 && CLASS_TYPE_P (basetype) && TYPE_BEING_DEFINED (basetype))
12491 cxx_incomplete_type_diagnostic (NULL_TREE, basetype, DK_PEDWARN);
12492 if (!dependent_type_p (basetype)
12493 && !complete_type_or_else (basetype, NULL))
12494 /* An incomplete type. Remove it from the list. */
12495 *basep = TREE_CHAIN (*basep);
12496 else
12498 max_bases++;
12499 if (TREE_TYPE (*basep))
12500 max_vbases++;
12501 if (CLASS_TYPE_P (basetype))
12502 max_vbases += vec_safe_length (CLASSTYPE_VBASECLASSES (basetype));
12503 basep = &TREE_CHAIN (*basep);
12507 TYPE_MARKED_P (ref) = 1;
12509 /* The binfo slot should be empty, unless this is an (ill-formed)
12510 redefinition. */
12511 if (TYPE_BINFO (ref) && !TYPE_SIZE (ref))
12513 error ("redefinition of %q#T", ref);
12514 return false;
12517 gcc_assert (TYPE_MAIN_VARIANT (ref) == ref);
12519 binfo = make_tree_binfo (max_bases);
12521 TYPE_BINFO (ref) = binfo;
12522 BINFO_OFFSET (binfo) = size_zero_node;
12523 BINFO_TYPE (binfo) = ref;
12525 /* Apply base-class info set up to the variants of this type. */
12526 fixup_type_variants (ref);
12528 if (max_bases)
12530 vec_alloc (BINFO_BASE_ACCESSES (binfo), max_bases);
12531 /* An aggregate cannot have baseclasses. */
12532 CLASSTYPE_NON_AGGREGATE (ref) = 1;
12534 if (TREE_CODE (ref) == UNION_TYPE)
12536 error ("derived union %qT invalid", ref);
12537 return false;
12541 if (max_bases > 1)
12543 if (TYPE_FOR_JAVA (ref))
12545 error ("Java class %qT cannot have multiple bases", ref);
12546 return false;
12550 if (max_vbases)
12552 vec_alloc (CLASSTYPE_VBASECLASSES (ref), max_vbases);
12554 if (TYPE_FOR_JAVA (ref))
12556 error ("Java class %qT cannot have virtual bases", ref);
12557 return false;
12561 for (igo_prev = binfo; base_list; base_list = TREE_CHAIN (base_list))
12563 tree access = TREE_PURPOSE (base_list);
12564 int via_virtual = TREE_TYPE (base_list) != NULL_TREE;
12565 tree basetype = TREE_VALUE (base_list);
12567 if (access == access_default_node)
12568 access = default_access;
12570 if (PACK_EXPANSION_P (basetype))
12571 basetype = PACK_EXPANSION_PATTERN (basetype);
12572 if (TREE_CODE (basetype) == TYPE_DECL)
12573 basetype = TREE_TYPE (basetype);
12574 if (!MAYBE_CLASS_TYPE_P (basetype) || TREE_CODE (basetype) == UNION_TYPE)
12576 error ("base type %qT fails to be a struct or class type",
12577 basetype);
12578 return false;
12581 if (TYPE_FOR_JAVA (basetype) && (current_lang_depth () == 0))
12582 TYPE_FOR_JAVA (ref) = 1;
12584 base_binfo = NULL_TREE;
12585 if (CLASS_TYPE_P (basetype) && !dependent_scope_p (basetype))
12587 base_binfo = TYPE_BINFO (basetype);
12588 /* The original basetype could have been a typedef'd type. */
12589 basetype = BINFO_TYPE (base_binfo);
12591 /* Inherit flags from the base. */
12592 TYPE_HAS_NEW_OPERATOR (ref)
12593 |= TYPE_HAS_NEW_OPERATOR (basetype);
12594 TYPE_HAS_ARRAY_NEW_OPERATOR (ref)
12595 |= TYPE_HAS_ARRAY_NEW_OPERATOR (basetype);
12596 TYPE_GETS_DELETE (ref) |= TYPE_GETS_DELETE (basetype);
12597 TYPE_HAS_CONVERSION (ref) |= TYPE_HAS_CONVERSION (basetype);
12598 CLASSTYPE_DIAMOND_SHAPED_P (ref)
12599 |= CLASSTYPE_DIAMOND_SHAPED_P (basetype);
12600 CLASSTYPE_REPEATED_BASE_P (ref)
12601 |= CLASSTYPE_REPEATED_BASE_P (basetype);
12604 /* We must do this test after we've seen through a typedef
12605 type. */
12606 if (TYPE_MARKED_P (basetype))
12608 if (basetype == ref)
12609 error ("recursive type %qT undefined", basetype);
12610 else
12611 error ("duplicate base type %qT invalid", basetype);
12612 return false;
12615 if (PACK_EXPANSION_P (TREE_VALUE (base_list)))
12616 /* Regenerate the pack expansion for the bases. */
12617 basetype = make_pack_expansion (basetype);
12619 TYPE_MARKED_P (basetype) = 1;
12621 base_binfo = copy_binfo (base_binfo, basetype, ref,
12622 &igo_prev, via_virtual);
12623 if (!BINFO_INHERITANCE_CHAIN (base_binfo))
12624 BINFO_INHERITANCE_CHAIN (base_binfo) = binfo;
12626 BINFO_BASE_APPEND (binfo, base_binfo);
12627 BINFO_BASE_ACCESS_APPEND (binfo, access);
12630 if (vec_safe_length (CLASSTYPE_VBASECLASSES (ref)) < max_vbases)
12631 /* If we didn't get max_vbases vbases, we must have shared at
12632 least one of them, and are therefore diamond shaped. */
12633 CLASSTYPE_DIAMOND_SHAPED_P (ref) = 1;
12635 /* Unmark all the types. */
12636 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
12637 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
12638 TYPE_MARKED_P (ref) = 0;
12640 /* Now see if we have a repeated base type. */
12641 if (!CLASSTYPE_REPEATED_BASE_P (ref))
12643 for (base_binfo = binfo; base_binfo;
12644 base_binfo = TREE_CHAIN (base_binfo))
12646 if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
12648 CLASSTYPE_REPEATED_BASE_P (ref) = 1;
12649 break;
12651 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 1;
12653 for (base_binfo = binfo; base_binfo;
12654 base_binfo = TREE_CHAIN (base_binfo))
12655 if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
12656 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
12657 else
12658 break;
12661 return true;
12665 /* Copies the enum-related properties from type SRC to type DST.
12666 Used with the underlying type of an enum and the enum itself. */
12667 static void
12668 copy_type_enum (tree dst, tree src)
12670 tree t;
12671 for (t = dst; t; t = TYPE_NEXT_VARIANT (t))
12673 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (src);
12674 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (src);
12675 TYPE_SIZE (t) = TYPE_SIZE (src);
12676 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (src);
12677 SET_TYPE_MODE (dst, TYPE_MODE (src));
12678 TYPE_PRECISION (t) = TYPE_PRECISION (src);
12679 TYPE_ALIGN (t) = TYPE_ALIGN (src);
12680 TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (src);
12681 TYPE_UNSIGNED (t) = TYPE_UNSIGNED (src);
12685 /* Begin compiling the definition of an enumeration type.
12686 NAME is its name,
12688 if ENUMTYPE is not NULL_TREE then the type has alredy been found.
12690 UNDERLYING_TYPE is the type that will be used as the storage for
12691 the enumeration type. This should be NULL_TREE if no storage type
12692 was specified.
12694 SCOPED_ENUM_P is true if this is a scoped enumeration type.
12696 if IS_NEW is not NULL, gets TRUE iff a new type is created.
12698 Returns the type object, as yet incomplete.
12699 Also records info about it so that build_enumerator
12700 may be used to declare the individual values as they are read. */
12702 tree
12703 start_enum (tree name, tree enumtype, tree underlying_type,
12704 bool scoped_enum_p, bool *is_new)
12706 tree prevtype = NULL_TREE;
12707 gcc_assert (identifier_p (name));
12709 if (is_new)
12710 *is_new = false;
12711 /* [C++0x dcl.enum]p5:
12713 If not explicitly specified, the underlying type of a scoped
12714 enumeration type is int. */
12715 if (!underlying_type && scoped_enum_p)
12716 underlying_type = integer_type_node;
12718 if (underlying_type)
12719 underlying_type = cv_unqualified (underlying_type);
12721 /* If this is the real definition for a previous forward reference,
12722 fill in the contents in the same object that used to be the
12723 forward reference. */
12724 if (!enumtype)
12725 enumtype = lookup_and_check_tag (enum_type, name,
12726 /*tag_scope=*/ts_current,
12727 /*template_header_p=*/false);
12729 /* In case of a template_decl, the only check that should be deferred
12730 to instantiation time is the comparison of underlying types. */
12731 if (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE)
12733 if (scoped_enum_p != SCOPED_ENUM_P (enumtype))
12735 error_at (input_location, "scoped/unscoped mismatch "
12736 "in enum %q#T", enumtype);
12737 error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
12738 "previous definition here");
12739 enumtype = error_mark_node;
12741 else if (ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) != !! underlying_type)
12743 error_at (input_location, "underlying type mismatch "
12744 "in enum %q#T", enumtype);
12745 error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
12746 "previous definition here");
12747 enumtype = error_mark_node;
12749 else if (underlying_type && ENUM_UNDERLYING_TYPE (enumtype)
12750 && !dependent_type_p (underlying_type)
12751 && !dependent_type_p (ENUM_UNDERLYING_TYPE (enumtype))
12752 && !same_type_p (underlying_type,
12753 ENUM_UNDERLYING_TYPE (enumtype)))
12755 error_at (input_location, "different underlying type "
12756 "in enum %q#T", enumtype);
12757 error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
12758 "previous definition here");
12759 underlying_type = NULL_TREE;
12763 if (!enumtype || TREE_CODE (enumtype) != ENUMERAL_TYPE
12764 || processing_template_decl)
12766 /* In case of error, make a dummy enum to allow parsing to
12767 continue. */
12768 if (enumtype == error_mark_node)
12770 name = make_anon_name ();
12771 enumtype = NULL_TREE;
12774 /* enumtype may be an ENUMERAL_TYPE if this is a redefinition
12775 of an opaque enum, or an opaque enum of an already defined
12776 enumeration (C++0x only).
12777 In any other case, it'll be NULL_TREE. */
12778 if (!enumtype)
12780 if (is_new)
12781 *is_new = true;
12783 prevtype = enumtype;
12785 /* Do not push the decl more than once, unless we need to
12786 compare underlying types at instantiation time */
12787 if (!enumtype
12788 || TREE_CODE (enumtype) != ENUMERAL_TYPE
12789 || (underlying_type
12790 && dependent_type_p (underlying_type))
12791 || (ENUM_UNDERLYING_TYPE (enumtype)
12792 && dependent_type_p (ENUM_UNDERLYING_TYPE (enumtype))))
12794 enumtype = cxx_make_type (ENUMERAL_TYPE);
12795 enumtype = pushtag (name, enumtype, /*tag_scope=*/ts_current);
12797 else
12798 enumtype = xref_tag (enum_type, name, /*tag_scope=*/ts_current,
12799 false);
12801 if (enumtype == error_mark_node)
12802 return error_mark_node;
12804 /* The enum is considered opaque until the opening '{' of the
12805 enumerator list. */
12806 SET_OPAQUE_ENUM_P (enumtype, true);
12807 ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) = !! underlying_type;
12810 SET_SCOPED_ENUM_P (enumtype, scoped_enum_p);
12812 if (underlying_type)
12814 if (CP_INTEGRAL_TYPE_P (underlying_type))
12816 copy_type_enum (enumtype, underlying_type);
12817 ENUM_UNDERLYING_TYPE (enumtype) = underlying_type;
12819 else if (dependent_type_p (underlying_type))
12820 ENUM_UNDERLYING_TYPE (enumtype) = underlying_type;
12821 else
12822 error ("underlying type %<%T%> of %<%T%> must be an integral type",
12823 underlying_type, enumtype);
12826 /* If into a template class, the returned enum is always the first
12827 declaration (opaque or not) seen. This way all the references to
12828 this type will be to the same declaration. The following ones are used
12829 only to check for definition errors. */
12830 if (prevtype && processing_template_decl)
12831 return prevtype;
12832 else
12833 return enumtype;
12836 /* After processing and defining all the values of an enumeration type,
12837 install their decls in the enumeration type.
12838 ENUMTYPE is the type object. */
12840 void
12841 finish_enum_value_list (tree enumtype)
12843 tree values;
12844 tree underlying_type;
12845 tree decl;
12846 tree value;
12847 tree minnode, maxnode;
12848 tree t;
12850 bool fixed_underlying_type_p
12851 = ENUM_UNDERLYING_TYPE (enumtype) != NULL_TREE;
12853 /* We built up the VALUES in reverse order. */
12854 TYPE_VALUES (enumtype) = nreverse (TYPE_VALUES (enumtype));
12856 /* For an enum defined in a template, just set the type of the values;
12857 all further processing is postponed until the template is
12858 instantiated. We need to set the type so that tsubst of a CONST_DECL
12859 works. */
12860 if (processing_template_decl)
12862 for (values = TYPE_VALUES (enumtype);
12863 values;
12864 values = TREE_CHAIN (values))
12865 TREE_TYPE (TREE_VALUE (values)) = enumtype;
12866 return;
12869 /* Determine the minimum and maximum values of the enumerators. */
12870 if (TYPE_VALUES (enumtype))
12872 minnode = maxnode = NULL_TREE;
12874 for (values = TYPE_VALUES (enumtype);
12875 values;
12876 values = TREE_CHAIN (values))
12878 decl = TREE_VALUE (values);
12880 /* [dcl.enum]: Following the closing brace of an enum-specifier,
12881 each enumerator has the type of its enumeration. Prior to the
12882 closing brace, the type of each enumerator is the type of its
12883 initializing value. */
12884 TREE_TYPE (decl) = enumtype;
12886 /* Update the minimum and maximum values, if appropriate. */
12887 value = DECL_INITIAL (decl);
12888 if (value == error_mark_node)
12889 value = integer_zero_node;
12890 /* Figure out what the minimum and maximum values of the
12891 enumerators are. */
12892 if (!minnode)
12893 minnode = maxnode = value;
12894 else if (tree_int_cst_lt (maxnode, value))
12895 maxnode = value;
12896 else if (tree_int_cst_lt (value, minnode))
12897 minnode = value;
12900 else
12901 /* [dcl.enum]
12903 If the enumerator-list is empty, the underlying type is as if
12904 the enumeration had a single enumerator with value 0. */
12905 minnode = maxnode = integer_zero_node;
12907 if (!fixed_underlying_type_p)
12909 /* Compute the number of bits require to represent all values of the
12910 enumeration. We must do this before the type of MINNODE and
12911 MAXNODE are transformed, since tree_int_cst_min_precision relies
12912 on the TREE_TYPE of the value it is passed. */
12913 signop sgn = tree_int_cst_sgn (minnode) >= 0 ? UNSIGNED : SIGNED;
12914 int lowprec = tree_int_cst_min_precision (minnode, sgn);
12915 int highprec = tree_int_cst_min_precision (maxnode, sgn);
12916 int precision = MAX (lowprec, highprec);
12917 unsigned int itk;
12918 bool use_short_enum;
12920 /* Determine the underlying type of the enumeration.
12922 [dcl.enum]
12924 The underlying type of an enumeration is an integral type that
12925 can represent all the enumerator values defined in the
12926 enumeration. It is implementation-defined which integral type is
12927 used as the underlying type for an enumeration except that the
12928 underlying type shall not be larger than int unless the value of
12929 an enumerator cannot fit in an int or unsigned int.
12931 We use "int" or an "unsigned int" as the underlying type, even if
12932 a smaller integral type would work, unless the user has
12933 explicitly requested that we use the smallest possible type. The
12934 user can request that for all enumerations with a command line
12935 flag, or for just one enumeration with an attribute. */
12937 use_short_enum = flag_short_enums
12938 || lookup_attribute ("packed", TYPE_ATTRIBUTES (enumtype));
12940 for (itk = (use_short_enum ? itk_char : itk_int);
12941 itk != itk_none;
12942 itk++)
12944 underlying_type = integer_types[itk];
12945 if (underlying_type != NULL_TREE
12946 && TYPE_PRECISION (underlying_type) >= precision
12947 && TYPE_SIGN (underlying_type) == sgn)
12948 break;
12950 if (itk == itk_none)
12952 /* DR 377
12954 IF no integral type can represent all the enumerator values, the
12955 enumeration is ill-formed. */
12956 error ("no integral type can represent all of the enumerator values "
12957 "for %qT", enumtype);
12958 precision = TYPE_PRECISION (long_long_integer_type_node);
12959 underlying_type = integer_types[itk_unsigned_long_long];
12962 /* [dcl.enum]
12964 The value of sizeof() applied to an enumeration type, an object
12965 of an enumeration type, or an enumerator, is the value of sizeof()
12966 applied to the underlying type. */
12967 copy_type_enum (enumtype, underlying_type);
12969 /* Compute the minimum and maximum values for the type.
12971 [dcl.enum]
12973 For an enumeration where emin is the smallest enumerator and emax
12974 is the largest, the values of the enumeration are the values of the
12975 underlying type in the range bmin to bmax, where bmin and bmax are,
12976 respectively, the smallest and largest values of the smallest bit-
12977 field that can store emin and emax. */
12979 /* The middle-end currently assumes that types with TYPE_PRECISION
12980 narrower than their underlying type are suitably zero or sign
12981 extended to fill their mode. Similarly, it assumes that the front
12982 end assures that a value of a particular type must be within
12983 TYPE_MIN_VALUE and TYPE_MAX_VALUE.
12985 We used to set these fields based on bmin and bmax, but that led
12986 to invalid assumptions like optimizing away bounds checking. So
12987 now we just set the TYPE_PRECISION, TYPE_MIN_VALUE, and
12988 TYPE_MAX_VALUE to the values for the mode above and only restrict
12989 the ENUM_UNDERLYING_TYPE for the benefit of diagnostics. */
12990 ENUM_UNDERLYING_TYPE (enumtype)
12991 = build_distinct_type_copy (underlying_type);
12992 TYPE_PRECISION (ENUM_UNDERLYING_TYPE (enumtype)) = precision;
12993 set_min_and_max_values_for_integral_type
12994 (ENUM_UNDERLYING_TYPE (enumtype), precision, sgn);
12996 /* If -fstrict-enums, still constrain TYPE_MIN/MAX_VALUE. */
12997 if (flag_strict_enums)
12998 set_min_and_max_values_for_integral_type (enumtype, precision, sgn);
13000 else
13001 underlying_type = ENUM_UNDERLYING_TYPE (enumtype);
13003 /* Convert each of the enumerators to the type of the underlying
13004 type of the enumeration. */
13005 for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values))
13007 location_t saved_location;
13009 decl = TREE_VALUE (values);
13010 saved_location = input_location;
13011 input_location = DECL_SOURCE_LOCATION (decl);
13012 if (fixed_underlying_type_p)
13013 /* If the enumeration type has a fixed underlying type, we
13014 already checked all of the enumerator values. */
13015 value = DECL_INITIAL (decl);
13016 else
13017 value = perform_implicit_conversion (underlying_type,
13018 DECL_INITIAL (decl),
13019 tf_warning_or_error);
13020 input_location = saved_location;
13022 /* Do not clobber shared ints. */
13023 value = copy_node (value);
13025 TREE_TYPE (value) = enumtype;
13026 DECL_INITIAL (decl) = value;
13029 /* Fix up all variant types of this enum type. */
13030 for (t = TYPE_MAIN_VARIANT (enumtype); t; t = TYPE_NEXT_VARIANT (t))
13031 TYPE_VALUES (t) = TYPE_VALUES (enumtype);
13033 if (at_class_scope_p ()
13034 && COMPLETE_TYPE_P (current_class_type)
13035 && UNSCOPED_ENUM_P (enumtype))
13036 insert_late_enum_def_into_classtype_sorted_fields (enumtype,
13037 current_class_type);
13039 /* Finish debugging output for this type. */
13040 rest_of_type_compilation (enumtype, namespace_bindings_p ());
13043 /* Finishes the enum type. This is called only the first time an
13044 enumeration is seen, be it opaque or odinary.
13045 ENUMTYPE is the type object. */
13047 void
13048 finish_enum (tree enumtype)
13050 if (processing_template_decl)
13052 if (at_function_scope_p ())
13053 add_stmt (build_min (TAG_DEFN, enumtype));
13054 return;
13057 /* If this is a forward declaration, there should not be any variants,
13058 though we can get a variant in the middle of an enum-specifier with
13059 wacky code like 'enum E { e = sizeof(const E*) };' */
13060 gcc_assert (enumtype == TYPE_MAIN_VARIANT (enumtype)
13061 && (TYPE_VALUES (enumtype)
13062 || !TYPE_NEXT_VARIANT (enumtype)));
13065 /* Build and install a CONST_DECL for an enumeration constant of the
13066 enumeration type ENUMTYPE whose NAME and VALUE (if any) are provided.
13067 LOC is the location of NAME.
13068 Assignment of sequential values by default is handled here. */
13070 void
13071 build_enumerator (tree name, tree value, tree enumtype, location_t loc)
13073 tree decl;
13074 tree context;
13075 tree type;
13077 /* scalar_constant_value will pull out this expression, so make sure
13078 it's folded as appropriate. */
13079 if (processing_template_decl)
13080 value = fold_non_dependent_expr (value);
13082 /* If the VALUE was erroneous, pretend it wasn't there; that will
13083 result in the enum being assigned the next value in sequence. */
13084 if (value == error_mark_node)
13085 value = NULL_TREE;
13087 /* Remove no-op casts from the value. */
13088 if (value)
13089 STRIP_TYPE_NOPS (value);
13091 if (! processing_template_decl)
13093 /* Validate and default VALUE. */
13094 if (value != NULL_TREE)
13096 if (!ENUM_UNDERLYING_TYPE (enumtype))
13098 tree tmp_value = build_expr_type_conversion (WANT_INT | WANT_ENUM,
13099 value, true);
13100 if (tmp_value)
13101 value = tmp_value;
13103 else if (! INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (value)))
13104 value = perform_implicit_conversion_flags
13105 (ENUM_UNDERLYING_TYPE (enumtype), value, tf_warning_or_error,
13106 LOOKUP_IMPLICIT | LOOKUP_NO_NARROWING);
13108 if (value == error_mark_node)
13109 value = NULL_TREE;
13111 if (value != NULL_TREE)
13113 value = cxx_constant_value (value);
13115 if (TREE_CODE (value) != INTEGER_CST
13116 || ! INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (value)))
13118 error ("enumerator value for %qD is not an integer constant",
13119 name);
13120 value = NULL_TREE;
13125 /* Default based on previous value. */
13126 if (value == NULL_TREE)
13128 if (TYPE_VALUES (enumtype))
13130 tree prev_value;
13131 bool overflowed;
13133 /* C++03 7.2/4: If no initializer is specified for the first
13134 enumerator, the type is an unspecified integral
13135 type. Otherwise the type is the same as the type of the
13136 initializing value of the preceding enumerator unless the
13137 incremented value is not representable in that type, in
13138 which case the type is an unspecified integral type
13139 sufficient to contain the incremented value. */
13140 prev_value = DECL_INITIAL (TREE_VALUE (TYPE_VALUES (enumtype)));
13141 if (error_operand_p (prev_value))
13142 value = error_mark_node;
13143 else
13145 tree type = TREE_TYPE (prev_value);
13146 signop sgn = TYPE_SIGN (type);
13147 widest_int wi = wi::add (wi::to_widest (prev_value), 1, sgn,
13148 &overflowed);
13149 if (!overflowed)
13151 bool pos = !wi::neg_p (wi, sgn);
13152 if (!wi::fits_to_tree_p (wi, type))
13154 unsigned int itk;
13155 for (itk = itk_int; itk != itk_none; itk++)
13157 type = integer_types[itk];
13158 if (type != NULL_TREE
13159 && (pos || !TYPE_UNSIGNED (type))
13160 && wi::fits_to_tree_p (wi, type))
13161 break;
13163 if (type && cxx_dialect < cxx11
13164 && itk > itk_unsigned_long)
13165 pedwarn (input_location, OPT_Wlong_long, pos ? "\
13166 incremented enumerator value is too large for %<unsigned long%>" : "\
13167 incremented enumerator value is too large for %<long%>");
13169 if (type == NULL_TREE)
13170 overflowed = true;
13171 else
13172 value = wide_int_to_tree (type, wi);
13175 if (overflowed)
13177 error ("overflow in enumeration values at %qD", name);
13178 value = error_mark_node;
13182 else
13183 value = integer_zero_node;
13186 /* Remove no-op casts from the value. */
13187 STRIP_TYPE_NOPS (value);
13189 /* If the underlying type of the enum is fixed, check whether
13190 the enumerator values fits in the underlying type. If it
13191 does not fit, the program is ill-formed [C++0x dcl.enum]. */
13192 if (ENUM_UNDERLYING_TYPE (enumtype)
13193 && value
13194 && TREE_CODE (value) == INTEGER_CST)
13196 if (!int_fits_type_p (value, ENUM_UNDERLYING_TYPE (enumtype)))
13197 error ("enumerator value %E is outside the range of underlying "
13198 "type %<%T%>", value, ENUM_UNDERLYING_TYPE (enumtype));
13200 /* Convert the value to the appropriate type. */
13201 value = convert (ENUM_UNDERLYING_TYPE (enumtype), value);
13205 /* C++ associates enums with global, function, or class declarations. */
13206 context = current_scope ();
13208 /* Build the actual enumeration constant. Note that the enumeration
13209 constants have the underlying type of the enum (if it is fixed)
13210 or the type of their initializer (if the underlying type of the
13211 enum is not fixed):
13213 [ C++0x dcl.enum ]
13215 If the underlying type is fixed, the type of each enumerator
13216 prior to the closing brace is the underlying type; if the
13217 initializing value of an enumerator cannot be represented by
13218 the underlying type, the program is ill-formed. If the
13219 underlying type is not fixed, the type of each enumerator is
13220 the type of its initializing value.
13222 If the underlying type is not fixed, it will be computed by
13223 finish_enum and we will reset the type of this enumerator. Of
13224 course, if we're processing a template, there may be no value. */
13225 type = value ? TREE_TYPE (value) : NULL_TREE;
13227 decl = build_decl (loc, CONST_DECL, name, type);
13229 DECL_CONTEXT (decl) = enumtype;
13230 TREE_CONSTANT (decl) = 1;
13231 TREE_READONLY (decl) = 1;
13232 DECL_INITIAL (decl) = value;
13234 if (context && context == current_class_type && !SCOPED_ENUM_P (enumtype))
13235 /* In something like `struct S { enum E { i = 7 }; };' we put `i'
13236 on the TYPE_FIELDS list for `S'. (That's so that you can say
13237 things like `S::i' later.) */
13238 finish_member_declaration (decl);
13239 else
13240 pushdecl (decl);
13242 /* Add this enumeration constant to the list for this type. */
13243 TYPE_VALUES (enumtype) = tree_cons (name, decl, TYPE_VALUES (enumtype));
13246 /* Look for an enumerator with the given NAME within the enumeration
13247 type ENUMTYPE. This routine is used primarily for qualified name
13248 lookup into an enumerator in C++0x, e.g.,
13250 enum class Color { Red, Green, Blue };
13252 Color color = Color::Red;
13254 Returns the value corresponding to the enumerator, or
13255 NULL_TREE if no such enumerator was found. */
13256 tree
13257 lookup_enumerator (tree enumtype, tree name)
13259 tree e;
13260 gcc_assert (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE);
13262 e = purpose_member (name, TYPE_VALUES (enumtype));
13263 return e? TREE_VALUE (e) : NULL_TREE;
13267 /* We're defining DECL. Make sure that its type is OK. */
13269 static void
13270 check_function_type (tree decl, tree current_function_parms)
13272 tree fntype = TREE_TYPE (decl);
13273 tree return_type = complete_type (TREE_TYPE (fntype));
13275 /* In a function definition, arg types must be complete. */
13276 require_complete_types_for_parms (current_function_parms);
13278 if (dependent_type_p (return_type)
13279 || type_uses_auto (return_type))
13280 return;
13281 if (!COMPLETE_OR_VOID_TYPE_P (return_type)
13282 || (TYPE_FOR_JAVA (return_type) && MAYBE_CLASS_TYPE_P (return_type)))
13284 tree args = TYPE_ARG_TYPES (fntype);
13286 if (!COMPLETE_OR_VOID_TYPE_P (return_type))
13287 error ("return type %q#T is incomplete", return_type);
13288 else
13289 error ("return type has Java class type %q#T", return_type);
13291 /* Make it return void instead. */
13292 if (TREE_CODE (fntype) == METHOD_TYPE)
13293 fntype = build_method_type_directly (TREE_TYPE (TREE_VALUE (args)),
13294 void_type_node,
13295 TREE_CHAIN (args));
13296 else
13297 fntype = build_function_type (void_type_node, args);
13298 fntype
13299 = build_exception_variant (fntype,
13300 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl)));
13301 fntype = (cp_build_type_attribute_variant
13302 (fntype, TYPE_ATTRIBUTES (TREE_TYPE (decl))));
13303 TREE_TYPE (decl) = fntype;
13305 else
13306 abstract_virtuals_error (decl, TREE_TYPE (fntype));
13309 /* Create the FUNCTION_DECL for a function definition.
13310 DECLSPECS and DECLARATOR are the parts of the declaration;
13311 they describe the function's name and the type it returns,
13312 but twisted together in a fashion that parallels the syntax of C.
13314 FLAGS is a bitwise or of SF_PRE_PARSED (indicating that the
13315 DECLARATOR is really the DECL for the function we are about to
13316 process and that DECLSPECS should be ignored), SF_INCLASS_INLINE
13317 indicating that the function is an inline defined in-class.
13319 This function creates a binding context for the function body
13320 as well as setting up the FUNCTION_DECL in current_function_decl.
13322 For C++, we must first check whether that datum makes any sense.
13323 For example, "class A local_a(1,2);" means that variable local_a
13324 is an aggregate of type A, which should have a constructor
13325 applied to it with the argument list [1, 2].
13327 On entry, DECL_INITIAL (decl1) should be NULL_TREE or error_mark_node,
13328 or may be a BLOCK if the function has been defined previously
13329 in this translation unit. On exit, DECL_INITIAL (decl1) will be
13330 error_mark_node if the function has never been defined, or
13331 a BLOCK if the function has been defined somewhere. */
13333 bool
13334 start_preparsed_function (tree decl1, tree attrs, int flags)
13336 tree ctype = NULL_TREE;
13337 tree fntype;
13338 tree restype;
13339 int doing_friend = 0;
13340 cp_binding_level *bl;
13341 tree current_function_parms;
13342 struct c_fileinfo *finfo
13343 = get_fileinfo (LOCATION_FILE (DECL_SOURCE_LOCATION (decl1)));
13344 bool honor_interface;
13346 /* Sanity check. */
13347 gcc_assert (VOID_TYPE_P (TREE_VALUE (void_list_node)));
13348 gcc_assert (TREE_CHAIN (void_list_node) == NULL_TREE);
13350 fntype = TREE_TYPE (decl1);
13351 if (TREE_CODE (fntype) == METHOD_TYPE)
13352 ctype = TYPE_METHOD_BASETYPE (fntype);
13354 /* ISO C++ 11.4/5. A friend function defined in a class is in
13355 the (lexical) scope of the class in which it is defined. */
13356 if (!ctype && DECL_FRIEND_P (decl1))
13358 ctype = DECL_FRIEND_CONTEXT (decl1);
13360 /* CTYPE could be null here if we're dealing with a template;
13361 for example, `inline friend float foo()' inside a template
13362 will have no CTYPE set. */
13363 if (ctype && TREE_CODE (ctype) != RECORD_TYPE)
13364 ctype = NULL_TREE;
13365 else
13366 doing_friend = 1;
13369 if (DECL_DECLARED_INLINE_P (decl1)
13370 && lookup_attribute ("noinline", attrs))
13371 warning (0, "inline function %q+D given attribute noinline", decl1);
13373 /* Handle gnu_inline attribute. */
13374 if (GNU_INLINE_P (decl1))
13376 DECL_EXTERNAL (decl1) = 1;
13377 DECL_NOT_REALLY_EXTERN (decl1) = 0;
13378 DECL_INTERFACE_KNOWN (decl1) = 1;
13379 DECL_DISREGARD_INLINE_LIMITS (decl1) = 1;
13382 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl1))
13383 /* This is a constructor, we must ensure that any default args
13384 introduced by this definition are propagated to the clones
13385 now. The clones are used directly in overload resolution. */
13386 adjust_clone_args (decl1);
13388 /* Sometimes we don't notice that a function is a static member, and
13389 build a METHOD_TYPE for it. Fix that up now. */
13390 gcc_assert (!(ctype != NULL_TREE && DECL_STATIC_FUNCTION_P (decl1)
13391 && TREE_CODE (TREE_TYPE (decl1)) == METHOD_TYPE));
13393 /* Set up current_class_type, and enter the scope of the class, if
13394 appropriate. */
13395 if (ctype)
13396 push_nested_class (ctype);
13397 else if (DECL_STATIC_FUNCTION_P (decl1))
13398 push_nested_class (DECL_CONTEXT (decl1));
13400 /* Now that we have entered the scope of the class, we must restore
13401 the bindings for any template parameters surrounding DECL1, if it
13402 is an inline member template. (Order is important; consider the
13403 case where a template parameter has the same name as a field of
13404 the class.) It is not until after this point that
13405 PROCESSING_TEMPLATE_DECL is guaranteed to be set up correctly. */
13406 if (flags & SF_INCLASS_INLINE)
13407 maybe_begin_member_template_processing (decl1);
13409 /* Effective C++ rule 15. */
13410 if (warn_ecpp
13411 && DECL_OVERLOADED_OPERATOR_P (decl1) == NOP_EXPR
13412 && VOID_TYPE_P (TREE_TYPE (fntype)))
13413 warning (OPT_Weffc__, "%<operator=%> should return a reference to %<*this%>");
13415 /* Make the init_value nonzero so pushdecl knows this is not tentative.
13416 error_mark_node is replaced below (in poplevel) with the BLOCK. */
13417 if (!DECL_INITIAL (decl1))
13418 DECL_INITIAL (decl1) = error_mark_node;
13420 /* This function exists in static storage.
13421 (This does not mean `static' in the C sense!) */
13422 TREE_STATIC (decl1) = 1;
13424 /* We must call push_template_decl after current_class_type is set
13425 up. (If we are processing inline definitions after exiting a
13426 class scope, current_class_type will be NULL_TREE until set above
13427 by push_nested_class.) */
13428 if (processing_template_decl)
13430 tree newdecl1 = push_template_decl (decl1);
13431 if (newdecl1 == error_mark_node)
13433 if (ctype || DECL_STATIC_FUNCTION_P (decl1))
13434 pop_nested_class ();
13435 return false;
13437 decl1 = newdecl1;
13440 /* We are now in the scope of the function being defined. */
13441 current_function_decl = decl1;
13443 /* Save the parm names or decls from this function's declarator
13444 where store_parm_decls will find them. */
13445 current_function_parms = DECL_ARGUMENTS (decl1);
13447 /* Make sure the parameter and return types are reasonable. When
13448 you declare a function, these types can be incomplete, but they
13449 must be complete when you define the function. */
13450 check_function_type (decl1, current_function_parms);
13452 /* Build the return declaration for the function. */
13453 restype = TREE_TYPE (fntype);
13455 if (DECL_RESULT (decl1) == NULL_TREE)
13457 tree resdecl;
13459 resdecl = build_decl (input_location, RESULT_DECL, 0, restype);
13460 DECL_ARTIFICIAL (resdecl) = 1;
13461 DECL_IGNORED_P (resdecl) = 1;
13462 DECL_RESULT (decl1) = resdecl;
13464 cp_apply_type_quals_to_decl (cp_type_quals (restype), resdecl);
13467 /* Let the user know we're compiling this function. */
13468 announce_function (decl1);
13470 /* Record the decl so that the function name is defined.
13471 If we already have a decl for this name, and it is a FUNCTION_DECL,
13472 use the old decl. */
13473 if (!processing_template_decl && !(flags & SF_PRE_PARSED))
13475 /* A specialization is not used to guide overload resolution. */
13476 if (!DECL_FUNCTION_MEMBER_P (decl1)
13477 && !(DECL_USE_TEMPLATE (decl1) &&
13478 PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl1))))
13480 tree olddecl = pushdecl (decl1);
13482 if (olddecl == error_mark_node)
13483 /* If something went wrong when registering the declaration,
13484 use DECL1; we have to have a FUNCTION_DECL to use when
13485 parsing the body of the function. */
13487 else
13489 /* Otherwise, OLDDECL is either a previous declaration
13490 of the same function or DECL1 itself. */
13492 if (warn_missing_declarations
13493 && olddecl == decl1
13494 && !DECL_MAIN_P (decl1)
13495 && TREE_PUBLIC (decl1)
13496 && !DECL_DECLARED_INLINE_P (decl1))
13498 tree context;
13500 /* Check whether DECL1 is in an anonymous
13501 namespace. */
13502 for (context = DECL_CONTEXT (decl1);
13503 context;
13504 context = DECL_CONTEXT (context))
13506 if (TREE_CODE (context) == NAMESPACE_DECL
13507 && DECL_NAME (context) == NULL_TREE)
13508 break;
13511 if (context == NULL)
13512 warning (OPT_Wmissing_declarations,
13513 "no previous declaration for %q+D", decl1);
13516 decl1 = olddecl;
13519 else
13521 /* We need to set the DECL_CONTEXT. */
13522 if (!DECL_CONTEXT (decl1) && DECL_TEMPLATE_INFO (decl1))
13523 DECL_CONTEXT (decl1) = DECL_CONTEXT (DECL_TI_TEMPLATE (decl1));
13525 fntype = TREE_TYPE (decl1);
13526 restype = TREE_TYPE (fntype);
13528 /* If #pragma weak applies, mark the decl appropriately now.
13529 The pragma only applies to global functions. Because
13530 determining whether or not the #pragma applies involves
13531 computing the mangled name for the declaration, we cannot
13532 apply the pragma until after we have merged this declaration
13533 with any previous declarations; if the original declaration
13534 has a linkage specification, that specification applies to
13535 the definition as well, and may affect the mangled name. */
13536 if (DECL_FILE_SCOPE_P (decl1))
13537 maybe_apply_pragma_weak (decl1);
13540 /* Reset this in case the call to pushdecl changed it. */
13541 current_function_decl = decl1;
13543 gcc_assert (DECL_INITIAL (decl1));
13545 /* This function may already have been parsed, in which case just
13546 return; our caller will skip over the body without parsing. */
13547 if (DECL_INITIAL (decl1) != error_mark_node)
13548 return true;
13550 /* Initialize RTL machinery. We cannot do this until
13551 CURRENT_FUNCTION_DECL and DECL_RESULT are set up. We do this
13552 even when processing a template; this is how we get
13553 CFUN set up, and our per-function variables initialized.
13554 FIXME factor out the non-RTL stuff. */
13555 bl = current_binding_level;
13556 allocate_struct_function (decl1, processing_template_decl);
13558 /* Initialize the language data structures. Whenever we start
13559 a new function, we destroy temporaries in the usual way. */
13560 cfun->language = ggc_cleared_alloc<language_function> ();
13561 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
13562 current_binding_level = bl;
13564 if (!processing_template_decl && type_uses_auto (restype))
13566 FNDECL_USED_AUTO (decl1) = true;
13567 current_function_auto_return_pattern = restype;
13570 /* Start the statement-tree, start the tree now. */
13571 DECL_SAVED_TREE (decl1) = push_stmt_list ();
13573 /* If we are (erroneously) defining a function that we have already
13574 defined before, wipe out what we knew before. */
13575 if (!DECL_PENDING_INLINE_P (decl1))
13576 DECL_SAVED_FUNCTION_DATA (decl1) = NULL;
13578 if (ctype && !doing_friend && !DECL_STATIC_FUNCTION_P (decl1))
13580 /* We know that this was set up by `grokclassfn'. We do not
13581 wait until `store_parm_decls', since evil parse errors may
13582 never get us to that point. Here we keep the consistency
13583 between `current_class_type' and `current_class_ptr'. */
13584 tree t = DECL_ARGUMENTS (decl1);
13586 gcc_assert (t != NULL_TREE && TREE_CODE (t) == PARM_DECL);
13587 gcc_assert (TYPE_PTR_P (TREE_TYPE (t)));
13589 cp_function_chain->x_current_class_ref
13590 = cp_build_indirect_ref (t, RO_NULL, tf_warning_or_error);
13591 /* Set this second to avoid shortcut in cp_build_indirect_ref. */
13592 cp_function_chain->x_current_class_ptr = t;
13594 /* Constructors and destructors need to know whether they're "in
13595 charge" of initializing virtual base classes. */
13596 t = DECL_CHAIN (t);
13597 if (DECL_HAS_IN_CHARGE_PARM_P (decl1))
13599 current_in_charge_parm = t;
13600 t = DECL_CHAIN (t);
13602 if (DECL_HAS_VTT_PARM_P (decl1))
13604 gcc_assert (DECL_NAME (t) == vtt_parm_identifier);
13605 current_vtt_parm = t;
13609 honor_interface = (!DECL_TEMPLATE_INSTANTIATION (decl1)
13610 /* Implicitly-defined methods (like the
13611 destructor for a class in which no destructor
13612 is explicitly declared) must not be defined
13613 until their definition is needed. So, we
13614 ignore interface specifications for
13615 compiler-generated functions. */
13616 && !DECL_ARTIFICIAL (decl1));
13618 if (processing_template_decl)
13619 /* Don't mess with interface flags. */;
13620 else if (DECL_INTERFACE_KNOWN (decl1))
13622 tree ctx = decl_function_context (decl1);
13624 if (DECL_NOT_REALLY_EXTERN (decl1))
13625 DECL_EXTERNAL (decl1) = 0;
13627 if (ctx != NULL_TREE && vague_linkage_p (ctx))
13628 /* This is a function in a local class in an extern inline
13629 or template function. */
13630 comdat_linkage (decl1);
13632 /* If this function belongs to an interface, it is public.
13633 If it belongs to someone else's interface, it is also external.
13634 This only affects inlines and template instantiations. */
13635 else if (!finfo->interface_unknown && honor_interface)
13637 if (DECL_DECLARED_INLINE_P (decl1)
13638 || DECL_TEMPLATE_INSTANTIATION (decl1))
13640 DECL_EXTERNAL (decl1)
13641 = (finfo->interface_only
13642 || (DECL_DECLARED_INLINE_P (decl1)
13643 && ! flag_implement_inlines
13644 && !DECL_VINDEX (decl1)));
13646 /* For WIN32 we also want to put these in linkonce sections. */
13647 maybe_make_one_only (decl1);
13649 else
13650 DECL_EXTERNAL (decl1) = 0;
13651 DECL_INTERFACE_KNOWN (decl1) = 1;
13652 /* If this function is in an interface implemented in this file,
13653 make sure that the back end knows to emit this function
13654 here. */
13655 if (!DECL_EXTERNAL (decl1))
13656 mark_needed (decl1);
13658 else if (finfo->interface_unknown && finfo->interface_only
13659 && honor_interface)
13661 /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma
13662 interface, we will have both finfo->interface_unknown and
13663 finfo->interface_only set. In that case, we don't want to
13664 use the normal heuristics because someone will supply a
13665 #pragma implementation elsewhere, and deducing it here would
13666 produce a conflict. */
13667 comdat_linkage (decl1);
13668 DECL_EXTERNAL (decl1) = 0;
13669 DECL_INTERFACE_KNOWN (decl1) = 1;
13670 DECL_DEFER_OUTPUT (decl1) = 1;
13672 else
13674 /* This is a definition, not a reference.
13675 So clear DECL_EXTERNAL, unless this is a GNU extern inline. */
13676 if (!GNU_INLINE_P (decl1))
13677 DECL_EXTERNAL (decl1) = 0;
13679 if ((DECL_DECLARED_INLINE_P (decl1)
13680 || DECL_TEMPLATE_INSTANTIATION (decl1))
13681 && ! DECL_INTERFACE_KNOWN (decl1))
13682 DECL_DEFER_OUTPUT (decl1) = 1;
13683 else
13684 DECL_INTERFACE_KNOWN (decl1) = 1;
13687 /* Determine the ELF visibility attribute for the function. We must not
13688 do this before calling "pushdecl", as we must allow "duplicate_decls"
13689 to merge any attributes appropriately. We also need to wait until
13690 linkage is set. */
13691 if (!DECL_CLONED_FUNCTION_P (decl1))
13692 determine_visibility (decl1);
13694 if (!processing_template_decl)
13695 maybe_instantiate_noexcept (decl1);
13697 begin_scope (sk_function_parms, decl1);
13699 ++function_depth;
13701 if (DECL_DESTRUCTOR_P (decl1)
13702 || (DECL_CONSTRUCTOR_P (decl1)
13703 && targetm.cxx.cdtor_returns_this ()))
13705 cdtor_label = build_decl (input_location,
13706 LABEL_DECL, NULL_TREE, NULL_TREE);
13707 DECL_CONTEXT (cdtor_label) = current_function_decl;
13710 start_fname_decls ();
13712 store_parm_decls (current_function_parms);
13714 return true;
13718 /* Like start_preparsed_function, except that instead of a
13719 FUNCTION_DECL, this function takes DECLSPECS and DECLARATOR.
13721 Returns true on success. If the DECLARATOR is not suitable
13722 for a function, we return false, which tells the parser to
13723 skip the entire function. */
13725 bool
13726 start_function (cp_decl_specifier_seq *declspecs,
13727 const cp_declarator *declarator,
13728 tree attrs)
13730 tree decl1;
13732 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, &attrs);
13733 if (decl1 == error_mark_node)
13734 return false;
13735 /* If the declarator is not suitable for a function definition,
13736 cause a syntax error. */
13737 if (decl1 == NULL_TREE || TREE_CODE (decl1) != FUNCTION_DECL)
13739 error ("invalid function declaration");
13740 return false;
13743 if (DECL_MAIN_P (decl1))
13744 /* main must return int. grokfndecl should have corrected it
13745 (and issued a diagnostic) if the user got it wrong. */
13746 gcc_assert (same_type_p (TREE_TYPE (TREE_TYPE (decl1)),
13747 integer_type_node));
13749 return start_preparsed_function (decl1, attrs, /*flags=*/SF_DEFAULT);
13752 /* Returns true iff an EH_SPEC_BLOCK should be created in the body of
13753 FN. */
13755 static bool
13756 use_eh_spec_block (tree fn)
13758 return (flag_exceptions && flag_enforce_eh_specs
13759 && !processing_template_decl
13760 && !type_throw_all_p (TREE_TYPE (fn))
13761 /* We insert the EH_SPEC_BLOCK only in the original
13762 function; then, it is copied automatically to the
13763 clones. */
13764 && !DECL_CLONED_FUNCTION_P (fn)
13765 /* Implicitly-generated constructors and destructors have
13766 exception specifications. However, those specifications
13767 are the union of the possible exceptions specified by the
13768 constructors/destructors for bases and members, so no
13769 unallowed exception will ever reach this function. By
13770 not creating the EH_SPEC_BLOCK we save a little memory,
13771 and we avoid spurious warnings about unreachable
13772 code. */
13773 && !DECL_DEFAULTED_FN (fn));
13776 /* Store the parameter declarations into the current function declaration.
13777 This is called after parsing the parameter declarations, before
13778 digesting the body of the function.
13780 Also install to binding contour return value identifier, if any. */
13782 static void
13783 store_parm_decls (tree current_function_parms)
13785 tree fndecl = current_function_decl;
13786 tree parm;
13788 /* This is a chain of any other decls that came in among the parm
13789 declarations. If a parm is declared with enum {foo, bar} x;
13790 then CONST_DECLs for foo and bar are put here. */
13791 tree nonparms = NULL_TREE;
13793 if (current_function_parms)
13795 /* This case is when the function was defined with an ANSI prototype.
13796 The parms already have decls, so we need not do anything here
13797 except record them as in effect
13798 and complain if any redundant old-style parm decls were written. */
13800 tree specparms = current_function_parms;
13801 tree next;
13803 /* Must clear this because it might contain TYPE_DECLs declared
13804 at class level. */
13805 current_binding_level->names = NULL;
13807 /* If we're doing semantic analysis, then we'll call pushdecl
13808 for each of these. We must do them in reverse order so that
13809 they end in the correct forward order. */
13810 specparms = nreverse (specparms);
13812 for (parm = specparms; parm; parm = next)
13814 next = DECL_CHAIN (parm);
13815 if (TREE_CODE (parm) == PARM_DECL)
13817 if (DECL_NAME (parm) == NULL_TREE
13818 || !VOID_TYPE_P (parm))
13819 pushdecl (parm);
13820 else
13821 error ("parameter %qD declared void", parm);
13823 else
13825 /* If we find an enum constant or a type tag,
13826 put it aside for the moment. */
13827 TREE_CHAIN (parm) = NULL_TREE;
13828 nonparms = chainon (nonparms, parm);
13832 /* Get the decls in their original chain order and record in the
13833 function. This is all and only the PARM_DECLs that were
13834 pushed into scope by the loop above. */
13835 DECL_ARGUMENTS (fndecl) = getdecls ();
13837 else
13838 DECL_ARGUMENTS (fndecl) = NULL_TREE;
13840 /* Now store the final chain of decls for the arguments
13841 as the decl-chain of the current lexical scope.
13842 Put the enumerators in as well, at the front so that
13843 DECL_ARGUMENTS is not modified. */
13844 current_binding_level->names = chainon (nonparms, DECL_ARGUMENTS (fndecl));
13846 if (use_eh_spec_block (current_function_decl))
13847 current_eh_spec_block = begin_eh_spec_block ();
13851 /* We have finished doing semantic analysis on DECL, but have not yet
13852 generated RTL for its body. Save away our current state, so that
13853 when we want to generate RTL later we know what to do. */
13855 static void
13856 save_function_data (tree decl)
13858 struct language_function *f;
13860 /* Save the language-specific per-function data so that we can
13861 get it back when we really expand this function. */
13862 gcc_assert (!DECL_PENDING_INLINE_P (decl));
13864 /* Make a copy. */
13865 f = ggc_alloc<language_function> ();
13866 memcpy (f, cp_function_chain, sizeof (struct language_function));
13867 DECL_SAVED_FUNCTION_DATA (decl) = f;
13869 /* Clear out the bits we don't need. */
13870 f->base.x_stmt_tree.x_cur_stmt_list = NULL;
13871 f->bindings = NULL;
13872 f->x_local_names = NULL;
13873 f->base.local_typedefs = NULL;
13877 /* Set the return value of the constructor (if present). */
13879 static void
13880 finish_constructor_body (void)
13882 tree val;
13883 tree exprstmt;
13885 if (targetm.cxx.cdtor_returns_this ()
13886 && (! TYPE_FOR_JAVA (current_class_type)))
13888 /* Any return from a constructor will end up here. */
13889 add_stmt (build_stmt (input_location, LABEL_EXPR, cdtor_label));
13891 val = DECL_ARGUMENTS (current_function_decl);
13892 val = build2 (MODIFY_EXPR, TREE_TYPE (val),
13893 DECL_RESULT (current_function_decl), val);
13894 /* Return the address of the object. */
13895 exprstmt = build_stmt (input_location, RETURN_EXPR, val);
13896 add_stmt (exprstmt);
13900 /* Do all the processing for the beginning of a destructor; set up the
13901 vtable pointers and cleanups for bases and members. */
13903 static void
13904 begin_destructor_body (void)
13906 tree compound_stmt;
13908 /* If the CURRENT_CLASS_TYPE is incomplete, we will have already
13909 issued an error message. We still want to try to process the
13910 body of the function, but initialize_vtbl_ptrs will crash if
13911 TYPE_BINFO is NULL. */
13912 if (COMPLETE_TYPE_P (current_class_type))
13914 compound_stmt = begin_compound_stmt (0);
13915 /* Make all virtual function table pointers in non-virtual base
13916 classes point to CURRENT_CLASS_TYPE's virtual function
13917 tables. */
13918 initialize_vtbl_ptrs (current_class_ptr);
13919 finish_compound_stmt (compound_stmt);
13921 /* Insert a cleanup to let the back end know that the object is dead
13922 when we exit the destructor, either normally or via exception. */
13923 tree btype = CLASSTYPE_AS_BASE (current_class_type);
13924 tree clobber = build_constructor (btype, NULL);
13925 TREE_THIS_VOLATILE (clobber) = true;
13926 tree bref = build_nop (build_reference_type (btype), current_class_ptr);
13927 bref = convert_from_reference (bref);
13928 tree exprstmt = build2 (MODIFY_EXPR, btype, bref, clobber);
13929 finish_decl_cleanup (NULL_TREE, exprstmt);
13931 /* And insert cleanups for our bases and members so that they
13932 will be properly destroyed if we throw. */
13933 push_base_cleanups ();
13937 /* At the end of every destructor we generate code to delete the object if
13938 necessary. Do that now. */
13940 static void
13941 finish_destructor_body (void)
13943 tree exprstmt;
13945 /* Any return from a destructor will end up here; that way all base
13946 and member cleanups will be run when the function returns. */
13947 add_stmt (build_stmt (input_location, LABEL_EXPR, cdtor_label));
13949 /* In a virtual destructor, we must call delete. */
13950 if (DECL_VIRTUAL_P (current_function_decl))
13952 tree if_stmt;
13953 tree virtual_size = cxx_sizeof (current_class_type);
13955 /* [class.dtor]
13957 At the point of definition of a virtual destructor (including
13958 an implicit definition), non-placement operator delete shall
13959 be looked up in the scope of the destructor's class and if
13960 found shall be accessible and unambiguous. */
13961 exprstmt = build_op_delete_call (DELETE_EXPR, current_class_ptr,
13962 virtual_size,
13963 /*global_p=*/false,
13964 /*placement=*/NULL_TREE,
13965 /*alloc_fn=*/NULL_TREE,
13966 tf_warning_or_error);
13968 if_stmt = begin_if_stmt ();
13969 finish_if_stmt_cond (build2 (BIT_AND_EXPR, integer_type_node,
13970 current_in_charge_parm,
13971 integer_one_node),
13972 if_stmt);
13973 finish_expr_stmt (exprstmt);
13974 finish_then_clause (if_stmt);
13975 finish_if_stmt (if_stmt);
13978 if (targetm.cxx.cdtor_returns_this ())
13980 tree val;
13982 val = DECL_ARGUMENTS (current_function_decl);
13983 val = build2 (MODIFY_EXPR, TREE_TYPE (val),
13984 DECL_RESULT (current_function_decl), val);
13985 /* Return the address of the object. */
13986 exprstmt = build_stmt (input_location, RETURN_EXPR, val);
13987 add_stmt (exprstmt);
13991 /* Do the necessary processing for the beginning of a function body, which
13992 in this case includes member-initializers, but not the catch clauses of
13993 a function-try-block. Currently, this means opening a binding level
13994 for the member-initializers (in a ctor), member cleanups (in a dtor),
13995 and capture proxies (in a lambda operator()). */
13997 tree
13998 begin_function_body (void)
14000 tree stmt;
14002 if (! FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
14003 return NULL_TREE;
14005 if (processing_template_decl)
14006 /* Do nothing now. */;
14007 else
14008 /* Always keep the BLOCK node associated with the outermost pair of
14009 curly braces of a function. These are needed for correct
14010 operation of dwarfout.c. */
14011 keep_next_level (true);
14013 stmt = begin_compound_stmt (BCS_FN_BODY);
14015 if (processing_template_decl)
14016 /* Do nothing now. */;
14017 else if (DECL_DESTRUCTOR_P (current_function_decl))
14018 begin_destructor_body ();
14020 return stmt;
14023 /* Do the processing for the end of a function body. Currently, this means
14024 closing out the cleanups for fully-constructed bases and members, and in
14025 the case of the destructor, deleting the object if desired. Again, this
14026 is only meaningful for [cd]tors, since they are the only functions where
14027 there is a significant distinction between the main body and any
14028 function catch clauses. Handling, say, main() return semantics here
14029 would be wrong, as flowing off the end of a function catch clause for
14030 main() would also need to return 0. */
14032 void
14033 finish_function_body (tree compstmt)
14035 if (compstmt == NULL_TREE)
14036 return;
14038 /* Close the block. */
14039 finish_compound_stmt (compstmt);
14041 if (processing_template_decl)
14042 /* Do nothing now. */;
14043 else if (DECL_CONSTRUCTOR_P (current_function_decl))
14044 finish_constructor_body ();
14045 else if (DECL_DESTRUCTOR_P (current_function_decl))
14046 finish_destructor_body ();
14049 /* Given a function, returns the BLOCK corresponding to the outermost level
14050 of curly braces, skipping the artificial block created for constructor
14051 initializers. */
14053 tree
14054 outer_curly_brace_block (tree fndecl)
14056 tree block = BLOCK_SUBBLOCKS (DECL_INITIAL (fndecl));
14057 if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
14058 /* Skip the artificial function body block. */
14059 block = BLOCK_SUBBLOCKS (block);
14060 return block;
14063 /* If FNDECL is a class's key method, add the class to the list of
14064 keyed classes that should be emitted. */
14066 static void
14067 record_key_method_defined (tree fndecl)
14069 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fndecl)
14070 && DECL_VIRTUAL_P (fndecl)
14071 && !processing_template_decl)
14073 tree fnclass = DECL_CONTEXT (fndecl);
14074 if (fndecl == CLASSTYPE_KEY_METHOD (fnclass))
14075 keyed_classes = tree_cons (NULL_TREE, fnclass, keyed_classes);
14079 /* Subroutine of finish_function.
14080 Save the body of constexpr functions for possible
14081 future compile time evaluation. */
14083 static void
14084 maybe_save_function_definition (tree fun)
14086 if (!processing_template_decl
14087 && DECL_DECLARED_CONSTEXPR_P (fun)
14088 && !cp_function_chain->invalid_constexpr
14089 && !DECL_CLONED_FUNCTION_P (fun))
14090 register_constexpr_fundef (fun, DECL_SAVED_TREE (fun));
14093 /* Finish up a function declaration and compile that function
14094 all the way to assembler language output. The free the storage
14095 for the function definition.
14097 FLAGS is a bitwise or of the following values:
14098 2 - INCLASS_INLINE
14099 We just finished processing the body of an in-class inline
14100 function definition. (This processing will have taken place
14101 after the class definition is complete.) */
14103 tree
14104 finish_function (int flags)
14106 tree fndecl = current_function_decl;
14107 tree fntype, ctype = NULL_TREE;
14108 int inclass_inline = (flags & 2) != 0;
14110 /* When we get some parse errors, we can end up without a
14111 current_function_decl, so cope. */
14112 if (fndecl == NULL_TREE)
14113 return error_mark_node;
14115 if (c_dialect_objc ())
14116 objc_finish_function ();
14118 gcc_assert (!defer_mark_used_calls);
14119 defer_mark_used_calls = true;
14121 record_key_method_defined (fndecl);
14123 fntype = TREE_TYPE (fndecl);
14125 /* TREE_READONLY (fndecl) = 1;
14126 This caused &foo to be of type ptr-to-const-function
14127 which then got a warning when stored in a ptr-to-function variable. */
14129 gcc_assert (building_stmt_list_p ());
14130 /* The current function is being defined, so its DECL_INITIAL should
14131 be set, and unless there's a multiple definition, it should be
14132 error_mark_node. */
14133 gcc_assert (DECL_INITIAL (fndecl) == error_mark_node);
14135 /* For a cloned function, we've already got all the code we need;
14136 there's no need to add any extra bits. */
14137 if (!DECL_CLONED_FUNCTION_P (fndecl))
14139 /* Make it so that `main' always returns 0 by default. */
14140 if (DECL_MAIN_P (current_function_decl))
14141 finish_return_stmt (integer_zero_node);
14143 if (use_eh_spec_block (current_function_decl))
14144 finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
14145 (TREE_TYPE (current_function_decl)),
14146 current_eh_spec_block);
14149 /* If we're saving up tree structure, tie off the function now. */
14150 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
14152 if (fn_contains_cilk_spawn_p (cfun) && !processing_template_decl)
14153 cfun->cilk_frame_decl = insert_cilk_frame (fndecl);
14155 finish_fname_decls ();
14157 /* If this function can't throw any exceptions, remember that. */
14158 if (!processing_template_decl
14159 && !cp_function_chain->can_throw
14160 && !flag_non_call_exceptions
14161 && !decl_replaceable_p (fndecl))
14162 TREE_NOTHROW (fndecl) = 1;
14164 /* This must come after expand_function_end because cleanups might
14165 have declarations (from inline functions) that need to go into
14166 this function's blocks. */
14168 /* If the current binding level isn't the outermost binding level
14169 for this function, either there is a bug, or we have experienced
14170 syntax errors and the statement tree is malformed. */
14171 if (current_binding_level->kind != sk_function_parms)
14173 /* Make sure we have already experienced errors. */
14174 gcc_assert (errorcount);
14176 /* Throw away the broken statement tree and extra binding
14177 levels. */
14178 DECL_SAVED_TREE (fndecl) = alloc_stmt_list ();
14180 while (current_binding_level->kind != sk_function_parms)
14182 if (current_binding_level->kind == sk_class)
14183 pop_nested_class ();
14184 else
14185 poplevel (0, 0, 0);
14188 poplevel (1, 0, 1);
14190 /* Statements should always be full-expressions at the outermost set
14191 of curly braces for a function. */
14192 gcc_assert (stmts_are_full_exprs_p ());
14194 /* If there are no return statements in a function with auto return type,
14195 the return type is void. But if the declared type is something like
14196 auto*, this is an error. */
14197 if (!processing_template_decl && FNDECL_USED_AUTO (fndecl)
14198 && TREE_TYPE (fntype) == current_function_auto_return_pattern)
14200 if (!is_auto (current_function_auto_return_pattern)
14201 && !current_function_returns_value && !current_function_returns_null)
14203 error ("no return statements in function returning %qT",
14204 current_function_auto_return_pattern);
14205 inform (input_location, "only plain %<auto%> return type can be "
14206 "deduced to %<void%>");
14208 apply_deduced_return_type (fndecl, void_type_node);
14209 fntype = TREE_TYPE (fndecl);
14212 /* Save constexpr function body before it gets munged by
14213 the NRV transformation. */
14214 maybe_save_function_definition (fndecl);
14216 /* Set up the named return value optimization, if we can. Candidate
14217 variables are selected in check_return_expr. */
14218 if (current_function_return_value)
14220 tree r = current_function_return_value;
14221 tree outer;
14223 if (r != error_mark_node
14224 /* This is only worth doing for fns that return in memory--and
14225 simpler, since we don't have to worry about promoted modes. */
14226 && aggregate_value_p (TREE_TYPE (TREE_TYPE (fndecl)), fndecl)
14227 /* Only allow this for variables declared in the outer scope of
14228 the function so we know that their lifetime always ends with a
14229 return; see g++.dg/opt/nrv6.C. We could be more flexible if
14230 we were to do this optimization in tree-ssa. */
14231 && (outer = outer_curly_brace_block (fndecl))
14232 && chain_member (r, BLOCK_VARS (outer)))
14233 finalize_nrv (&DECL_SAVED_TREE (fndecl), r, DECL_RESULT (fndecl));
14235 current_function_return_value = NULL_TREE;
14238 /* Remember that we were in class scope. */
14239 if (current_class_name)
14240 ctype = current_class_type;
14242 /* Must mark the RESULT_DECL as being in this function. */
14243 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
14245 /* Set the BLOCK_SUPERCONTEXT of the outermost function scope to point
14246 to the FUNCTION_DECL node itself. */
14247 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
14249 /* Save away current state, if appropriate. */
14250 if (!processing_template_decl)
14251 save_function_data (fndecl);
14253 /* Complain if there's just no return statement. */
14254 if (warn_return_type
14255 && !VOID_TYPE_P (TREE_TYPE (fntype))
14256 && !dependent_type_p (TREE_TYPE (fntype))
14257 && !current_function_returns_value && !current_function_returns_null
14258 /* Don't complain if we abort or throw. */
14259 && !current_function_returns_abnormally
14260 /* Don't complain if there's an infinite loop. */
14261 && !current_function_infinite_loop
14262 /* Don't complain if we are declared noreturn. */
14263 && !TREE_THIS_VOLATILE (fndecl)
14264 && !DECL_NAME (DECL_RESULT (fndecl))
14265 && !TREE_NO_WARNING (fndecl)
14266 /* Structor return values (if any) are set by the compiler. */
14267 && !DECL_CONSTRUCTOR_P (fndecl)
14268 && !DECL_DESTRUCTOR_P (fndecl)
14269 && targetm.warn_func_return (fndecl))
14271 warning (OPT_Wreturn_type,
14272 "no return statement in function returning non-void");
14273 TREE_NO_WARNING (fndecl) = 1;
14276 /* Store the end of the function, so that we get good line number
14277 info for the epilogue. */
14278 cfun->function_end_locus = input_location;
14280 /* Complain about parameters that are only set, but never otherwise used. */
14281 if (warn_unused_but_set_parameter
14282 && !processing_template_decl
14283 && errorcount == unused_but_set_errorcount
14284 && !DECL_CLONED_FUNCTION_P (fndecl))
14286 tree decl;
14288 for (decl = DECL_ARGUMENTS (fndecl);
14289 decl;
14290 decl = DECL_CHAIN (decl))
14291 if (TREE_USED (decl)
14292 && TREE_CODE (decl) == PARM_DECL
14293 && !DECL_READ_P (decl)
14294 && DECL_NAME (decl)
14295 && !DECL_ARTIFICIAL (decl)
14296 && !TREE_NO_WARNING (decl)
14297 && !DECL_IN_SYSTEM_HEADER (decl)
14298 && TREE_TYPE (decl) != error_mark_node
14299 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
14300 && (!CLASS_TYPE_P (TREE_TYPE (decl))
14301 || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl))))
14302 warning (OPT_Wunused_but_set_parameter,
14303 "parameter %q+D set but not used", decl);
14304 unused_but_set_errorcount = errorcount;
14307 /* Complain about locally defined typedefs that are not used in this
14308 function. */
14309 maybe_warn_unused_local_typedefs ();
14311 /* Genericize before inlining. */
14312 if (!processing_template_decl)
14314 struct language_function *f = DECL_SAVED_FUNCTION_DATA (fndecl);
14315 invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE, fndecl);
14316 cp_genericize (fndecl);
14317 /* Clear out the bits we don't need. */
14318 f->x_current_class_ptr = NULL;
14319 f->x_current_class_ref = NULL;
14320 f->x_eh_spec_block = NULL;
14321 f->x_in_charge_parm = NULL;
14322 f->x_vtt_parm = NULL;
14323 f->x_return_value = NULL;
14324 f->bindings = NULL;
14325 f->extern_decl_map = NULL;
14326 f->infinite_loops = NULL;
14328 /* Clear out the bits we don't need. */
14329 local_names = NULL;
14331 /* We're leaving the context of this function, so zap cfun. It's still in
14332 DECL_STRUCT_FUNCTION, and we'll restore it in tree_rest_of_compilation. */
14333 set_cfun (NULL);
14334 current_function_decl = NULL;
14336 /* If this is an in-class inline definition, we may have to pop the
14337 bindings for the template parameters that we added in
14338 maybe_begin_member_template_processing when start_function was
14339 called. */
14340 if (inclass_inline)
14341 maybe_end_member_template_processing ();
14343 /* Leave the scope of the class. */
14344 if (ctype)
14345 pop_nested_class ();
14347 --function_depth;
14349 /* Clean up. */
14350 current_function_decl = NULL_TREE;
14352 defer_mark_used_calls = false;
14353 if (deferred_mark_used_calls)
14355 unsigned int i;
14356 tree decl;
14358 FOR_EACH_VEC_SAFE_ELT (deferred_mark_used_calls, i, decl)
14359 mark_used (decl);
14360 vec_free (deferred_mark_used_calls);
14363 return fndecl;
14366 /* Create the FUNCTION_DECL for a function definition.
14367 DECLSPECS and DECLARATOR are the parts of the declaration;
14368 they describe the return type and the name of the function,
14369 but twisted together in a fashion that parallels the syntax of C.
14371 This function creates a binding context for the function body
14372 as well as setting up the FUNCTION_DECL in current_function_decl.
14374 Returns a FUNCTION_DECL on success.
14376 If the DECLARATOR is not suitable for a function (it defines a datum
14377 instead), we return 0, which tells yyparse to report a parse error.
14379 May return void_type_node indicating that this method is actually
14380 a friend. See grokfield for more details.
14382 Came here with a `.pushlevel' .
14384 DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
14385 CHANGES TO CODE IN `grokfield'. */
14387 tree
14388 grokmethod (cp_decl_specifier_seq *declspecs,
14389 const cp_declarator *declarator, tree attrlist)
14391 tree fndecl = grokdeclarator (declarator, declspecs, MEMFUNCDEF, 0,
14392 &attrlist);
14394 if (fndecl == error_mark_node)
14395 return error_mark_node;
14397 if (fndecl == NULL || TREE_CODE (fndecl) != FUNCTION_DECL)
14399 error ("invalid member function declaration");
14400 return error_mark_node;
14403 if (attrlist)
14404 cplus_decl_attributes (&fndecl, attrlist, 0);
14406 /* Pass friends other than inline friend functions back. */
14407 if (fndecl == void_type_node)
14408 return fndecl;
14410 if (DECL_IN_AGGR_P (fndecl))
14412 if (DECL_CLASS_SCOPE_P (fndecl))
14413 error ("%qD is already defined in class %qT", fndecl,
14414 DECL_CONTEXT (fndecl));
14415 return error_mark_node;
14418 check_template_shadow (fndecl);
14420 DECL_COMDAT (fndecl) = 1;
14421 DECL_DECLARED_INLINE_P (fndecl) = 1;
14422 DECL_NO_INLINE_WARNING_P (fndecl) = 1;
14424 /* We process method specializations in finish_struct_1. */
14425 if (processing_template_decl && !DECL_TEMPLATE_SPECIALIZATION (fndecl))
14427 fndecl = push_template_decl (fndecl);
14428 if (fndecl == error_mark_node)
14429 return fndecl;
14432 if (! DECL_FRIEND_P (fndecl))
14434 if (DECL_CHAIN (fndecl))
14436 fndecl = copy_node (fndecl);
14437 TREE_CHAIN (fndecl) = NULL_TREE;
14441 cp_finish_decl (fndecl, NULL_TREE, false, NULL_TREE, 0);
14443 DECL_IN_AGGR_P (fndecl) = 1;
14444 return fndecl;
14448 /* VAR is a VAR_DECL. If its type is incomplete, remember VAR so that
14449 we can lay it out later, when and if its type becomes complete.
14451 Also handle constexpr variables where the initializer involves
14452 an unlowered PTRMEM_CST because the class isn't complete yet. */
14454 void
14455 maybe_register_incomplete_var (tree var)
14457 gcc_assert (VAR_P (var));
14459 /* Keep track of variables with incomplete types. */
14460 if (!processing_template_decl && TREE_TYPE (var) != error_mark_node
14461 && DECL_EXTERNAL (var))
14463 tree inner_type = TREE_TYPE (var);
14465 while (TREE_CODE (inner_type) == ARRAY_TYPE)
14466 inner_type = TREE_TYPE (inner_type);
14467 inner_type = TYPE_MAIN_VARIANT (inner_type);
14469 if ((!COMPLETE_TYPE_P (inner_type) && CLASS_TYPE_P (inner_type))
14470 /* RTTI TD entries are created while defining the type_info. */
14471 || (TYPE_LANG_SPECIFIC (inner_type)
14472 && TYPE_BEING_DEFINED (inner_type)))
14474 incomplete_var iv = {var, inner_type};
14475 vec_safe_push (incomplete_vars, iv);
14477 else if (!(DECL_LANG_SPECIFIC (var) && DECL_TEMPLATE_INFO (var))
14478 && decl_constant_var_p (var)
14479 && (TYPE_PTRMEM_P (inner_type) || CLASS_TYPE_P (inner_type)))
14481 /* When the outermost open class is complete we can resolve any
14482 pointers-to-members. */
14483 tree context = outermost_open_class ();
14484 incomplete_var iv = {var, context};
14485 vec_safe_push (incomplete_vars, iv);
14490 /* Called when a class type (given by TYPE) is defined. If there are
14491 any existing VAR_DECLs whose type has been completed by this
14492 declaration, update them now. */
14494 void
14495 complete_vars (tree type)
14497 unsigned ix;
14498 incomplete_var *iv;
14500 for (ix = 0; vec_safe_iterate (incomplete_vars, ix, &iv); )
14502 if (same_type_p (type, iv->incomplete_type))
14504 tree var = iv->decl;
14505 tree type = TREE_TYPE (var);
14507 if (TYPE_MAIN_VARIANT (strip_array_types (type))
14508 == iv->incomplete_type)
14510 /* Complete the type of the variable. The VAR_DECL itself
14511 will be laid out in expand_expr. */
14512 complete_type (type);
14513 cp_apply_type_quals_to_decl (cp_type_quals (type), var);
14516 if (DECL_INITIAL (var)
14517 && decl_constant_var_p (var))
14518 DECL_INITIAL (var) = cplus_expand_constant (DECL_INITIAL (var));
14520 /* Remove this entry from the list. */
14521 incomplete_vars->unordered_remove (ix);
14523 else
14524 ix++;
14527 /* Check for pending declarations which may have abstract type. */
14528 complete_type_check_abstract (type);
14531 /* If DECL is of a type which needs a cleanup, build and return an
14532 expression to perform that cleanup here. Return NULL_TREE if no
14533 cleanup need be done. */
14535 tree
14536 cxx_maybe_build_cleanup (tree decl, tsubst_flags_t complain)
14538 tree type;
14539 tree attr;
14540 tree cleanup;
14542 /* Assume no cleanup is required. */
14543 cleanup = NULL_TREE;
14545 if (error_operand_p (decl))
14546 return cleanup;
14548 /* Handle "__attribute__((cleanup))". We run the cleanup function
14549 before the destructor since the destructor is what actually
14550 terminates the lifetime of the object. */
14551 attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
14552 if (attr)
14554 tree id;
14555 tree fn;
14556 tree arg;
14558 /* Get the name specified by the user for the cleanup function. */
14559 id = TREE_VALUE (TREE_VALUE (attr));
14560 /* Look up the name to find the cleanup function to call. It is
14561 important to use lookup_name here because that is what is
14562 used in c-common.c:handle_cleanup_attribute when performing
14563 initial checks on the attribute. Note that those checks
14564 include ensuring that the function found is not an overloaded
14565 function, or an object with an overloaded call operator,
14566 etc.; we can rely on the fact that the function found is an
14567 ordinary FUNCTION_DECL. */
14568 fn = lookup_name (id);
14569 arg = build_address (decl);
14570 mark_used (decl);
14571 cleanup = cp_build_function_call_nary (fn, complain, arg, NULL_TREE);
14572 if (cleanup == error_mark_node)
14573 return error_mark_node;
14575 /* Handle ordinary C++ destructors. */
14576 type = TREE_TYPE (decl);
14577 if (type_build_dtor_call (type))
14579 int flags = LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR;
14580 tree addr;
14581 tree call;
14583 if (TREE_CODE (type) == ARRAY_TYPE)
14584 addr = decl;
14585 else
14586 addr = build_address (decl);
14588 call = build_delete (TREE_TYPE (addr), addr,
14589 sfk_complete_destructor, flags, 0, complain);
14590 if (call == error_mark_node)
14591 cleanup = error_mark_node;
14592 else if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
14593 /* Discard the call. */;
14594 else if (cleanup)
14595 cleanup = cp_build_compound_expr (cleanup, call, complain);
14596 else
14597 cleanup = call;
14600 /* build_delete sets the location of the destructor call to the
14601 current location, even though the destructor is going to be
14602 called later, at the end of the current scope. This can lead to
14603 a "jumpy" behaviour for users of debuggers when they step around
14604 the end of the block. So let's unset the location of the
14605 destructor call instead. */
14606 if (cleanup != NULL && EXPR_P (cleanup))
14607 SET_EXPR_LOCATION (cleanup, UNKNOWN_LOCATION);
14609 if (cleanup
14610 && !lookup_attribute ("warn_unused", TYPE_ATTRIBUTES (TREE_TYPE (decl))))
14611 /* Treat objects with destructors as used; the destructor may do
14612 something substantive. */
14613 mark_used (decl);
14615 return cleanup;
14619 /* Return the FUNCTION_TYPE that corresponds to MEMFNTYPE, which can be a
14620 FUNCTION_DECL, METHOD_TYPE, FUNCTION_TYPE, pointer or reference to
14621 METHOD_TYPE or FUNCTION_TYPE, or pointer to member function. */
14623 tree
14624 static_fn_type (tree memfntype)
14626 tree fntype;
14627 tree args;
14629 if (TYPE_PTRMEMFUNC_P (memfntype))
14630 memfntype = TYPE_PTRMEMFUNC_FN_TYPE (memfntype);
14631 if (POINTER_TYPE_P (memfntype)
14632 || TREE_CODE (memfntype) == FUNCTION_DECL)
14633 memfntype = TREE_TYPE (memfntype);
14634 if (TREE_CODE (memfntype) == FUNCTION_TYPE)
14635 return memfntype;
14636 gcc_assert (TREE_CODE (memfntype) == METHOD_TYPE);
14637 args = TYPE_ARG_TYPES (memfntype);
14638 cp_ref_qualifier rqual = type_memfn_rqual (memfntype);
14639 fntype = build_function_type (TREE_TYPE (memfntype), TREE_CHAIN (args));
14640 fntype = apply_memfn_quals (fntype, type_memfn_quals (memfntype), rqual);
14641 fntype = (cp_build_type_attribute_variant
14642 (fntype, TYPE_ATTRIBUTES (memfntype)));
14643 fntype = (build_exception_variant
14644 (fntype, TYPE_RAISES_EXCEPTIONS (memfntype)));
14645 if (TYPE_HAS_LATE_RETURN_TYPE (memfntype))
14646 TYPE_HAS_LATE_RETURN_TYPE (fntype) = 1;
14647 return fntype;
14650 /* DECL was originally constructed as a non-static member function,
14651 but turned out to be static. Update it accordingly. */
14653 void
14654 revert_static_member_fn (tree decl)
14656 tree stype = static_fn_type (decl);
14657 cp_cv_quals quals = type_memfn_quals (stype);
14658 cp_ref_qualifier rqual = type_memfn_rqual (stype);
14660 if (quals != TYPE_UNQUALIFIED || rqual != REF_QUAL_NONE)
14661 stype = apply_memfn_quals (stype, TYPE_UNQUALIFIED, REF_QUAL_NONE);
14663 TREE_TYPE (decl) = stype;
14665 if (DECL_ARGUMENTS (decl))
14666 DECL_ARGUMENTS (decl) = DECL_CHAIN (DECL_ARGUMENTS (decl));
14667 DECL_STATIC_FUNCTION_P (decl) = 1;
14670 /* Return which tree structure is used by T, or TS_CP_GENERIC if T is
14671 one of the language-independent trees. */
14673 enum cp_tree_node_structure_enum
14674 cp_tree_node_structure (union lang_tree_node * t)
14676 switch (TREE_CODE (&t->generic))
14678 case DEFAULT_ARG: return TS_CP_DEFAULT_ARG;
14679 case DEFERRED_NOEXCEPT: return TS_CP_DEFERRED_NOEXCEPT;
14680 case IDENTIFIER_NODE: return TS_CP_IDENTIFIER;
14681 case OVERLOAD: return TS_CP_OVERLOAD;
14682 case TEMPLATE_PARM_INDEX: return TS_CP_TPI;
14683 case PTRMEM_CST: return TS_CP_PTRMEM;
14684 case BASELINK: return TS_CP_BASELINK;
14685 case TEMPLATE_DECL: return TS_CP_TEMPLATE_DECL;
14686 case STATIC_ASSERT: return TS_CP_STATIC_ASSERT;
14687 case ARGUMENT_PACK_SELECT: return TS_CP_ARGUMENT_PACK_SELECT;
14688 case TRAIT_EXPR: return TS_CP_TRAIT_EXPR;
14689 case LAMBDA_EXPR: return TS_CP_LAMBDA_EXPR;
14690 case TEMPLATE_INFO: return TS_CP_TEMPLATE_INFO;
14691 case USERDEF_LITERAL: return TS_CP_USERDEF_LITERAL;
14692 default: return TS_CP_GENERIC;
14696 /* Build the void_list_node (void_type_node having been created). */
14697 tree
14698 build_void_list_node (void)
14700 tree t = build_tree_list (NULL_TREE, void_type_node);
14701 return t;
14704 bool
14705 cp_missing_noreturn_ok_p (tree decl)
14707 /* A missing noreturn is ok for the `main' function. */
14708 return DECL_MAIN_P (decl);
14711 /* Return the decl used to identify the COMDAT group into which DECL should
14712 be placed. */
14714 tree
14715 cxx_comdat_group (tree decl)
14717 /* Virtual tables, construction virtual tables, and virtual table
14718 tables all go in a single COMDAT group, named after the primary
14719 virtual table. */
14720 if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl))
14721 decl = CLASSTYPE_VTABLES (DECL_CONTEXT (decl));
14722 /* For all other DECLs, the COMDAT group is the mangled name of the
14723 declaration itself. */
14724 else
14726 while (DECL_THUNK_P (decl))
14728 /* If TARGET_USE_LOCAL_THUNK_ALIAS_P, use_thunk puts the thunk
14729 into the same section as the target function. In that case
14730 we must return target's name. */
14731 tree target = THUNK_TARGET (decl);
14732 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (target)
14733 && DECL_SECTION_NAME (target) != NULL
14734 && DECL_ONE_ONLY (target))
14735 decl = target;
14736 else
14737 break;
14741 return decl;
14744 /* Returns the return type for FN as written by the user, which may include
14745 a placeholder for a deduced return type. */
14747 tree
14748 fndecl_declared_return_type (tree fn)
14750 fn = STRIP_TEMPLATE (fn);
14751 if (FNDECL_USED_AUTO (fn))
14753 struct language_function *f = NULL;
14754 if (DECL_STRUCT_FUNCTION (fn))
14755 f = DECL_STRUCT_FUNCTION (fn)->language;
14756 if (f == NULL)
14757 f = DECL_SAVED_FUNCTION_DATA (fn);
14758 return f->x_auto_return_pattern;
14760 return TREE_TYPE (TREE_TYPE (fn));
14763 /* Returns true iff DECL was declared with an auto return type and it has
14764 not yet been deduced to a real type. */
14766 bool
14767 undeduced_auto_decl (tree decl)
14769 if (cxx_dialect < cxx14)
14770 return false;
14771 return type_uses_auto (TREE_TYPE (decl));
14774 /* Complain if DECL has an undeduced return type. */
14776 void
14777 require_deduced_type (tree decl)
14779 if (undeduced_auto_decl (decl))
14780 error ("use of %qD before deduction of %<auto%>", decl);
14783 #include "gt-cp-decl.h"